org.el 874 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro org-with-limited-levels.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches an headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-at-clock-log-p "org-clock" ())
  107. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  108. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  109. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  110. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  111. (declare-function orgtbl-mode "org-table" (&optional arg))
  112. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  113. (declare-function org-beamer-mode "ox-beamer" ())
  114. (declare-function org-table-edit-field "org-table" (arg))
  115. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  116. (declare-function org-id-get-create "org-id" (&optional force))
  117. (declare-function org-id-find-id-file "org-id" (id))
  118. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  119. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. (defcustom org-babel-load-languages '((emacs-lisp . t))
  163. "Languages which can be evaluated in Org-mode buffers.
  164. This list can be used to load support for any of the languages
  165. below, note that each language will depend on a different set of
  166. system executables and/or Emacs modes. When a language is
  167. \"loaded\", then code blocks in that language can be evaluated
  168. with `org-babel-execute-src-block' bound by default to C-c
  169. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  170. be set to remove code block evaluation from the C-c C-c
  171. keybinding. By default only Emacs Lisp (which has no
  172. requirements) is loaded."
  173. :group 'org-babel
  174. :set 'org-babel-do-load-languages
  175. :version "24.1"
  176. :type '(alist :tag "Babel Languages"
  177. :key-type
  178. (choice
  179. (const :tag "Awk" awk)
  180. (const :tag "C" C)
  181. (const :tag "R" R)
  182. (const :tag "Asymptote" asymptote)
  183. (const :tag "Calc" calc)
  184. (const :tag "Clojure" clojure)
  185. (const :tag "CSS" css)
  186. (const :tag "Ditaa" ditaa)
  187. (const :tag "Dot" dot)
  188. (const :tag "Emacs Lisp" emacs-lisp)
  189. (const :tag "Fortran" fortran)
  190. (const :tag "Gnuplot" gnuplot)
  191. (const :tag "Haskell" haskell)
  192. (const :tag "IO" io)
  193. (const :tag "Java" java)
  194. (const :tag "Javascript" js)
  195. (const :tag "LaTeX" latex)
  196. (const :tag "Ledger" ledger)
  197. (const :tag "Lilypond" lilypond)
  198. (const :tag "Lisp" lisp)
  199. (const :tag "Makefile" makefile)
  200. (const :tag "Maxima" maxima)
  201. (const :tag "Matlab" matlab)
  202. (const :tag "Mscgen" mscgen)
  203. (const :tag "Ocaml" ocaml)
  204. (const :tag "Octave" octave)
  205. (const :tag "Org" org)
  206. (const :tag "Perl" perl)
  207. (const :tag "Pico Lisp" picolisp)
  208. (const :tag "PlantUML" plantuml)
  209. (const :tag "Python" python)
  210. (const :tag "Ruby" ruby)
  211. (const :tag "Sass" sass)
  212. (const :tag "Scala" scala)
  213. (const :tag "Scheme" scheme)
  214. (const :tag "Screen" screen)
  215. (const :tag "Shell Script" sh)
  216. (const :tag "Shen" shen)
  217. (const :tag "Sql" sql)
  218. (const :tag "Sqlite" sqlite))
  219. :value-type (boolean :tag "Activate" :value t)))
  220. ;;;; Customization variables
  221. (defcustom org-clone-delete-id nil
  222. "Remove ID property of clones of a subtree.
  223. When non-nil, clones of a subtree don't inherit the ID property.
  224. Otherwise they inherit the ID property with a new unique
  225. identifier."
  226. :type 'boolean
  227. :version "24.1"
  228. :group 'org-id)
  229. ;;; Version
  230. (org-check-version)
  231. ;;;###autoload
  232. (defun org-version (&optional here full message)
  233. "Show the org-mode version in the echo area.
  234. With prefix argument HERE, insert it at point.
  235. When FULL is non-nil, use a verbose version string.
  236. When MESSAGE is non-nil, display a message with the version."
  237. (interactive "P")
  238. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  239. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  240. (load-suffixes (list ".el"))
  241. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  242. (org-trash (or
  243. (and (fboundp 'org-release) (fboundp 'org-git-version))
  244. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  245. (load-suffixes save-load-suffixes)
  246. (org-version (org-release))
  247. (git-version (org-git-version))
  248. (version (format "Org-mode version %s (%s @ %s)"
  249. org-version
  250. git-version
  251. (if org-install-dir
  252. (if (string= org-dir org-install-dir)
  253. org-install-dir
  254. (concat "mixed installation! " org-install-dir " and " org-dir))
  255. "org-loaddefs.el can not be found!")))
  256. (_version (if full version org-version)))
  257. (if (org-called-interactively-p 'interactive)
  258. (if here
  259. (insert version)
  260. (message version))
  261. (if message (message _version))
  262. _version)))
  263. (defconst org-version (org-version))
  264. ;;; Compatibility constants
  265. ;;; The custom variables
  266. (defgroup org nil
  267. "Outline-based notes management and organizer."
  268. :tag "Org"
  269. :group 'outlines
  270. :group 'calendar)
  271. (defcustom org-mode-hook nil
  272. "Mode hook for Org-mode, run after the mode was turned on."
  273. :group 'org
  274. :type 'hook)
  275. (defcustom org-load-hook nil
  276. "Hook that is run after org.el has been loaded."
  277. :group 'org
  278. :type 'hook)
  279. (defcustom org-log-buffer-setup-hook nil
  280. "Hook that is run after an Org log buffer is created."
  281. :group 'org
  282. :version "24.1"
  283. :type 'hook)
  284. (defvar org-modules) ; defined below
  285. (defvar org-modules-loaded nil
  286. "Have the modules been loaded already?")
  287. (defun org-load-modules-maybe (&optional force)
  288. "Load all extensions listed in `org-modules'."
  289. (when (or force (not org-modules-loaded))
  290. (mapc (lambda (ext)
  291. (condition-case nil (require ext)
  292. (error (message "Problems while trying to load feature `%s'" ext))))
  293. org-modules)
  294. (setq org-modules-loaded t)))
  295. (defun org-set-modules (var value)
  296. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  297. (set var value)
  298. (when (featurep 'org)
  299. (org-load-modules-maybe 'force)))
  300. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  301. "Modules that should always be loaded together with org.el.
  302. If a description starts with <C>, the file is not part of Emacs
  303. and loading it will require that you have downloaded and properly
  304. installed the Org mode distribution.
  305. You can also use this system to load external packages (i.e. neither Org
  306. core modules, nor modules from the CONTRIB directory). Just add symbols
  307. to the end of the list. If the package is called org-xyz.el, then you need
  308. to add the symbol `xyz', and the package must have a call to:
  309. \(provide 'org-xyz)
  310. For export specific modules, see also `org-export-backends'."
  311. :group 'org
  312. :set 'org-set-modules
  313. :type
  314. '(set :greedy t
  315. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  316. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  317. (const :tag " crypt: Encryption of subtrees" org-crypt)
  318. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  319. (const :tag " docview: Links to doc-view buffers" org-docview)
  320. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  321. (const :tag " id: Global IDs for identifying entries" org-id)
  322. (const :tag " info: Links to Info nodes" org-info)
  323. (const :tag " habit: Track your consistency with habits" org-habit)
  324. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  325. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  326. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  327. (const :tag " mew Links to Mew folders/messages" org-mew)
  328. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  329. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  330. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  331. (const :tag " vm: Links to VM folders/messages" org-vm)
  332. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  333. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  334. (const :tag " mouse: Additional mouse support" org-mouse)
  335. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  336. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  337. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  338. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  339. (const :tag "C collector: Collect properties into tables" org-collector)
  340. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  341. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  342. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  343. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  344. (const :tag "C eval: Include command output as text" org-eval)
  345. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  346. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  347. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  348. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  349. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  350. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  351. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  352. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  353. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  354. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  355. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  356. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  357. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  358. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  359. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  360. (const :tag "C registry: A registry for Org-mode links" org-registry)
  361. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  362. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  363. (const :tag "C secretary: Team management with org-mode" org-secretary)
  364. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  365. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  366. (const :tag "C track: Keep up with Org-mode development" org-track)
  367. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  368. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  369. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  370. (defvar org-export-registered-backends) ; From ox.el
  371. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  372. (defcustom org-export-backends '(ascii html icalendar latex)
  373. "List of export back-ends that should be always available.
  374. If a description starts with <C>, the file is not part of Emacs
  375. and loading it will require that you have downloaded and properly
  376. installed the Org mode distribution.
  377. Unlike to `org-modules', libraries in this list will not be
  378. loaded along with Org, but only once the export framework is
  379. needed.
  380. This variable needs to be set before org.el is loaded. If you
  381. need to make a change while Emacs is running, use the customize
  382. interface or run the following code, where VALUE stands for the
  383. new value of the variable, after updating it:
  384. \(progn
  385. \(setq org-export-registered-backends
  386. \(org-remove-if-not
  387. \(lambda (backend)
  388. \(or (memq backend val)
  389. \(catch 'parentp
  390. \(mapc
  391. \(lambda (b)
  392. \(and (org-export-derived-backend-p b (car backend))
  393. \(throw 'parentp t)))
  394. val)
  395. nil)))
  396. org-export-registered-backends))
  397. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  398. \(dolist (backend val)
  399. \(cond
  400. \((not (load (format \"ox-%s\" backend) t t))
  401. \(message \"Problems while trying to load export back-end `%s'\"
  402. backend))
  403. \((not (memq backend new-list)) (push backend new-list))))
  404. \(set-default var new-list)))
  405. Adding a back-end to this list will also pull the back-end it
  406. depends on, if any."
  407. :group 'org
  408. :group 'org-export
  409. :set (lambda (var val)
  410. (if (not (featurep 'ox)) (set-default var val)
  411. ;; Any back-end not required anymore (not present in VAL and not
  412. ;; a parent of any back-end in the new value) is removed from the
  413. ;; list of registered back-ends.
  414. (setq org-export-registered-backends
  415. (org-remove-if-not
  416. (lambda (backend)
  417. (or (memq backend val)
  418. (catch 'parentp
  419. (mapc
  420. (lambda (b)
  421. (and (org-export-derived-backend-p b (car backend))
  422. (throw 'parentp t)))
  423. val)
  424. nil)))
  425. org-export-registered-backends))
  426. ;; Now build NEW-LIST of both new back-ends and required
  427. ;; parents.
  428. (let ((new-list (mapcar 'car org-export-registered-backends)))
  429. (dolist (backend val)
  430. (cond
  431. ((not (load (format "ox-%s" backend) t t))
  432. (message "Problems while trying to load export back-end `%s'"
  433. backend))
  434. ((not (memq backend new-list)) (push backend new-list))))
  435. ;; Set VAR to that list with fixed dependencies.
  436. (set-default var new-list))))
  437. :type '(set :greedy t
  438. (const :tag " ascii Export buffer to ASCII format" ascii)
  439. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  440. (const :tag " html Export buffer to HTML format" html)
  441. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  442. (const :tag " latex Export buffer to LaTeX format" latex)
  443. (const :tag " man Export buffer to MAN format" man)
  444. (const :tag " md Export buffer to Markdown format" md)
  445. (const :tag " odt Export buffer to ODT format" odt)
  446. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  447. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" jsinfo)
  448. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  449. (const :tag "C groff Export buffer to Groff format" groff)
  450. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)))
  451. (eval-after-load 'ox
  452. '(mapc
  453. (lambda (backend)
  454. (condition-case nil (require (intern (format "ox-%s" backend)))
  455. (error (message "Problems while trying to load export back-end `%s'"
  456. backend))))
  457. org-export-backends))
  458. (defcustom org-support-shift-select nil
  459. "Non-nil means make shift-cursor commands select text when possible.
  460. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  461. start selecting a region, or enlarge regions started in this way.
  462. In Org-mode, in special contexts, these same keys are used for
  463. other purposes, important enough to compete with shift selection.
  464. Org tries to balance these needs by supporting `shift-select-mode'
  465. outside these special contexts, under control of this variable.
  466. The default of this variable is nil, to avoid confusing behavior. Shifted
  467. cursor keys will then execute Org commands in the following contexts:
  468. - on a headline, changing TODO state (left/right) and priority (up/down)
  469. - on a time stamp, changing the time
  470. - in a plain list item, changing the bullet type
  471. - in a property definition line, switching between allowed values
  472. - in the BEGIN line of a clock table (changing the time block).
  473. Outside these contexts, the commands will throw an error.
  474. When this variable is t and the cursor is not in a special
  475. context, Org-mode will support shift-selection for making and
  476. enlarging regions. To make this more effective, the bullet
  477. cycling will no longer happen anywhere in an item line, but only
  478. if the cursor is exactly on the bullet.
  479. If you set this variable to the symbol `always', then the keys
  480. will not be special in headlines, property lines, and item lines,
  481. to make shift selection work there as well. If this is what you
  482. want, you can use the following alternative commands: `C-c C-t'
  483. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  484. can be used to switch TODO sets, `C-c -' to cycle item bullet
  485. types, and properties can be edited by hand or in column view.
  486. However, when the cursor is on a timestamp, shift-cursor commands
  487. will still edit the time stamp - this is just too good to give up.
  488. XEmacs user should have this variable set to nil, because
  489. `shift-select-mode' is in Emacs 23 or later only."
  490. :group 'org
  491. :type '(choice
  492. (const :tag "Never" nil)
  493. (const :tag "When outside special context" t)
  494. (const :tag "Everywhere except timestamps" always)))
  495. (defcustom org-loop-over-headlines-in-active-region nil
  496. "Shall some commands act upon headlines in the active region?
  497. When set to `t', some commands will be performed in all headlines
  498. within the active region.
  499. When set to `start-level', some commands will be performed in all
  500. headlines within the active region, provided that these headlines
  501. are of the same level than the first one.
  502. When set to a string, those commands will be performed on the
  503. matching headlines within the active region. Such string must be
  504. a tags/property/todo match as it is used in the agenda tags view.
  505. The list of commands is: `org-schedule', `org-deadline',
  506. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  507. `org-archive-to-archive-sibling'. The archiving commands skip
  508. already archived entries."
  509. :type '(choice (const :tag "Don't loop" nil)
  510. (const :tag "All headlines in active region" t)
  511. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  512. (string :tag "Tags/Property/Todo matcher"))
  513. :version "24.1"
  514. :group 'org-todo
  515. :group 'org-archive)
  516. (defgroup org-startup nil
  517. "Options concerning startup of Org-mode."
  518. :tag "Org Startup"
  519. :group 'org)
  520. (defcustom org-startup-folded t
  521. "Non-nil means entering Org-mode will switch to OVERVIEW.
  522. This can also be configured on a per-file basis by adding one of
  523. the following lines anywhere in the buffer:
  524. #+STARTUP: fold (or `overview', this is equivalent)
  525. #+STARTUP: nofold (or `showall', this is equivalent)
  526. #+STARTUP: content
  527. #+STARTUP: showeverything
  528. By default, this option is ignored when Org opens agenda files
  529. for the first time. If you want the agenda to honor the startup
  530. option, set `org-agenda-inhibit-startup' to nil."
  531. :group 'org-startup
  532. :type '(choice
  533. (const :tag "nofold: show all" nil)
  534. (const :tag "fold: overview" t)
  535. (const :tag "content: all headlines" content)
  536. (const :tag "show everything, even drawers" showeverything)))
  537. (defcustom org-startup-truncated t
  538. "Non-nil means entering Org-mode will set `truncate-lines'.
  539. This is useful since some lines containing links can be very long and
  540. uninteresting. Also tables look terrible when wrapped."
  541. :group 'org-startup
  542. :type 'boolean)
  543. (defcustom org-startup-indented nil
  544. "Non-nil means turn on `org-indent-mode' on startup.
  545. This can also be configured on a per-file basis by adding one of
  546. the following lines anywhere in the buffer:
  547. #+STARTUP: indent
  548. #+STARTUP: noindent"
  549. :group 'org-structure
  550. :type '(choice
  551. (const :tag "Not" nil)
  552. (const :tag "Globally (slow on startup in large files)" t)))
  553. (defcustom org-use-sub-superscripts t
  554. "Non-nil means interpret \"_\" and \"^\" for display.
  555. When this option is turned on, you can use TeX-like syntax for sub- and
  556. superscripts. Several characters after \"_\" or \"^\" will be
  557. considered as a single item - so grouping with {} is normally not
  558. needed. For example, the following things will be parsed as single
  559. sub- or superscripts.
  560. 10^24 or 10^tau several digits will be considered 1 item.
  561. 10^-12 or 10^-tau a leading sign with digits or a word
  562. x^2-y^3 will be read as x^2 - y^3, because items are
  563. terminated by almost any nonword/nondigit char.
  564. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  565. Still, ambiguity is possible - so when in doubt use {} to enclose
  566. the sub/superscript. If you set this variable to the symbol
  567. `{}', the braces are *required* in order to trigger
  568. interpretations as sub/superscript. This can be helpful in
  569. documents that need \"_\" frequently in plain text."
  570. :group 'org-startup
  571. :version "24.1"
  572. :type '(choice
  573. (const :tag "Always interpret" t)
  574. (const :tag "Only with braces" {})
  575. (const :tag "Never interpret" nil)))
  576. (defcustom org-startup-with-beamer-mode nil
  577. "Non-nil means turn on `org-beamer-mode' on startup.
  578. This can also be configured on a per-file basis by adding one of
  579. the following lines anywhere in the buffer:
  580. #+STARTUP: beamer"
  581. :group 'org-startup
  582. :version "24.1"
  583. :type 'boolean)
  584. (defcustom org-startup-align-all-tables nil
  585. "Non-nil means align all tables when visiting a file.
  586. This is useful when the column width in tables is forced with <N> cookies
  587. in table fields. Such tables will look correct only after the first re-align.
  588. This can also be configured on a per-file basis by adding one of
  589. the following lines anywhere in the buffer:
  590. #+STARTUP: align
  591. #+STARTUP: noalign"
  592. :group 'org-startup
  593. :type 'boolean)
  594. (defcustom org-startup-with-inline-images nil
  595. "Non-nil means show inline images when loading a new Org file.
  596. This can also be configured on a per-file basis by adding one of
  597. the following lines anywhere in the buffer:
  598. #+STARTUP: inlineimages
  599. #+STARTUP: noinlineimages"
  600. :group 'org-startup
  601. :version "24.1"
  602. :type 'boolean)
  603. (defcustom org-startup-with-latex-preview nil
  604. "Non-nil means preview LaTeX fragments when loading a new Org file.
  605. This can also be configured on a per-file basis by adding one of
  606. the followinglines anywhere in the buffer:
  607. #+STARTUP: latexpreview
  608. #+STARTUP: nolatexpreview"
  609. :group 'org-startup
  610. :version "24.3"
  611. :type 'boolean)
  612. (defcustom org-insert-mode-line-in-empty-file nil
  613. "Non-nil means insert the first line setting Org-mode in empty files.
  614. When the function `org-mode' is called interactively in an empty file, this
  615. normally means that the file name does not automatically trigger Org-mode.
  616. To ensure that the file will always be in Org-mode in the future, a
  617. line enforcing Org-mode will be inserted into the buffer, if this option
  618. has been set."
  619. :group 'org-startup
  620. :type 'boolean)
  621. (defcustom org-replace-disputed-keys nil
  622. "Non-nil means use alternative key bindings for some keys.
  623. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  624. These keys are also used by other packages like shift-selection-mode'
  625. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  626. If you want to use Org-mode together with one of these other modes,
  627. or more generally if you would like to move some Org-mode commands to
  628. other keys, set this variable and configure the keys with the variable
  629. `org-disputed-keys'.
  630. This option is only relevant at load-time of Org-mode, and must be set
  631. *before* org.el is loaded. Changing it requires a restart of Emacs to
  632. become effective."
  633. :group 'org-startup
  634. :type 'boolean)
  635. (defcustom org-use-extra-keys nil
  636. "Non-nil means use extra key sequence definitions for certain commands.
  637. This happens automatically if you run XEmacs or if `window-system'
  638. is nil. This variable lets you do the same manually. You must
  639. set it before loading org.
  640. Example: on Carbon Emacs 22 running graphically, with an external
  641. keyboard on a Powerbook, the default way of setting M-left might
  642. not work for either Alt or ESC. Setting this variable will make
  643. it work for ESC."
  644. :group 'org-startup
  645. :type 'boolean)
  646. (if (fboundp 'defvaralias)
  647. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  648. (defcustom org-disputed-keys
  649. '(([(shift up)] . [(meta p)])
  650. ([(shift down)] . [(meta n)])
  651. ([(shift left)] . [(meta -)])
  652. ([(shift right)] . [(meta +)])
  653. ([(control shift right)] . [(meta shift +)])
  654. ([(control shift left)] . [(meta shift -)]))
  655. "Keys for which Org-mode and other modes compete.
  656. This is an alist, cars are the default keys, second element specifies
  657. the alternative to use when `org-replace-disputed-keys' is t.
  658. Keys can be specified in any syntax supported by `define-key'.
  659. The value of this option takes effect only at Org-mode's startup,
  660. therefore you'll have to restart Emacs to apply it after changing."
  661. :group 'org-startup
  662. :type 'alist)
  663. (defun org-key (key)
  664. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  665. Or return the original if not disputed.
  666. Also apply the translations defined in `org-xemacs-key-equivalents'."
  667. (when org-replace-disputed-keys
  668. (let* ((nkey (key-description key))
  669. (x (org-find-if (lambda (x)
  670. (equal (key-description (car x)) nkey))
  671. org-disputed-keys)))
  672. (setq key (if x (cdr x) key))))
  673. (when (featurep 'xemacs)
  674. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  675. key)
  676. (defun org-find-if (predicate seq)
  677. (catch 'exit
  678. (while seq
  679. (if (funcall predicate (car seq))
  680. (throw 'exit (car seq))
  681. (pop seq)))))
  682. (defun org-defkey (keymap key def)
  683. "Define a key, possibly translated, as returned by `org-key'."
  684. (define-key keymap (org-key key) def))
  685. (defcustom org-ellipsis nil
  686. "The ellipsis to use in the Org-mode outline.
  687. When nil, just use the standard three dots. When a string, use that instead,
  688. When a face, use the standard 3 dots, but with the specified face.
  689. The change affects only Org-mode (which will then use its own display table).
  690. Changing this requires executing `M-x org-mode' in a buffer to become
  691. effective."
  692. :group 'org-startup
  693. :type '(choice (const :tag "Default" nil)
  694. (face :tag "Face" :value org-warning)
  695. (string :tag "String" :value "...#")))
  696. (defvar org-display-table nil
  697. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  698. (defgroup org-keywords nil
  699. "Keywords in Org-mode."
  700. :tag "Org Keywords"
  701. :group 'org)
  702. (defcustom org-deadline-string "DEADLINE:"
  703. "String to mark deadline entries.
  704. A deadline is this string, followed by a time stamp. Should be a word,
  705. terminated by a colon. You can insert a schedule keyword and
  706. a timestamp with \\[org-deadline].
  707. Changes become only effective after restarting Emacs."
  708. :group 'org-keywords
  709. :type 'string)
  710. (defcustom org-scheduled-string "SCHEDULED:"
  711. "String to mark scheduled TODO entries.
  712. A schedule is this string, followed by a time stamp. Should be a word,
  713. terminated by a colon. You can insert a schedule keyword and
  714. a timestamp with \\[org-schedule].
  715. Changes become only effective after restarting Emacs."
  716. :group 'org-keywords
  717. :type 'string)
  718. (defcustom org-closed-string "CLOSED:"
  719. "String used as the prefix for timestamps logging closing a TODO entry."
  720. :group 'org-keywords
  721. :type 'string)
  722. (defcustom org-clock-string "CLOCK:"
  723. "String used as prefix for timestamps clocking work hours on an item."
  724. :group 'org-keywords
  725. :type 'string)
  726. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  727. org-scheduled-string "\\|"
  728. org-deadline-string "\\|"
  729. org-closed-string "\\|"
  730. org-clock-string "\\)")
  731. "Matches a line with planning or clock info.")
  732. (defcustom org-comment-string "COMMENT"
  733. "Entries starting with this keyword will never be exported.
  734. An entry can be toggled between COMMENT and normal with
  735. \\[org-toggle-comment].
  736. Changes become only effective after restarting Emacs."
  737. :group 'org-keywords
  738. :type 'string)
  739. (defcustom org-quote-string "QUOTE"
  740. "Entries starting with this keyword will be exported in fixed-width font.
  741. Quoting applies only to the text in the entry following the headline, and does
  742. not extend beyond the next headline, even if that is lower level.
  743. An entry can be toggled between QUOTE and normal with
  744. \\[org-toggle-fixed-width-section]."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defconst org-repeat-re
  748. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  749. "Regular expression for specifying repeated events.
  750. After a match, group 1 contains the repeat expression.")
  751. (defgroup org-structure nil
  752. "Options concerning the general structure of Org-mode files."
  753. :tag "Org Structure"
  754. :group 'org)
  755. (defgroup org-reveal-location nil
  756. "Options about how to make context of a location visible."
  757. :tag "Org Reveal Location"
  758. :group 'org-structure)
  759. (defconst org-context-choice
  760. '(choice
  761. (const :tag "Always" t)
  762. (const :tag "Never" nil)
  763. (repeat :greedy t :tag "Individual contexts"
  764. (cons
  765. (choice :tag "Context"
  766. (const agenda)
  767. (const org-goto)
  768. (const occur-tree)
  769. (const tags-tree)
  770. (const link-search)
  771. (const mark-goto)
  772. (const bookmark-jump)
  773. (const isearch)
  774. (const default))
  775. (boolean))))
  776. "Contexts for the reveal options.")
  777. (defcustom org-show-hierarchy-above '((default . t))
  778. "Non-nil means show full hierarchy when revealing a location.
  779. Org-mode often shows locations in an org-mode file which might have
  780. been invisible before. When this is set, the hierarchy of headings
  781. above the exposed location is shown.
  782. Turning this off for example for sparse trees makes them very compact.
  783. Instead of t, this can also be an alist specifying this option for different
  784. contexts. Valid contexts are
  785. agenda when exposing an entry from the agenda
  786. org-goto when using the command `org-goto' on key C-c C-j
  787. occur-tree when using the command `org-occur' on key C-c /
  788. tags-tree when constructing a sparse tree based on tags matches
  789. link-search when exposing search matches associated with a link
  790. mark-goto when exposing the jump goal of a mark
  791. bookmark-jump when exposing a bookmark location
  792. isearch when exiting from an incremental search
  793. default default for all contexts not set explicitly"
  794. :group 'org-reveal-location
  795. :type org-context-choice)
  796. (defcustom org-show-following-heading '((default . nil))
  797. "Non-nil means show following heading when revealing a location.
  798. Org-mode often shows locations in an org-mode file which might have
  799. been invisible before. When this is set, the heading following the
  800. match is shown.
  801. Turning this off for example for sparse trees makes them very compact,
  802. but makes it harder to edit the location of the match. In such a case,
  803. use the command \\[org-reveal] to show more context.
  804. Instead of t, this can also be an alist specifying this option for different
  805. contexts. See `org-show-hierarchy-above' for valid contexts."
  806. :group 'org-reveal-location
  807. :type org-context-choice)
  808. (defcustom org-show-siblings '((default . nil) (isearch t))
  809. "Non-nil means show all sibling heading when revealing a location.
  810. Org-mode often shows locations in an org-mode file which might have
  811. been invisible before. When this is set, the sibling of the current entry
  812. heading are all made visible. If `org-show-hierarchy-above' is t,
  813. the same happens on each level of the hierarchy above the current entry.
  814. By default this is on for the isearch context, off for all other contexts.
  815. Turning this off for example for sparse trees makes them very compact,
  816. but makes it harder to edit the location of the match. In such a case,
  817. use the command \\[org-reveal] to show more context.
  818. Instead of t, this can also be an alist specifying this option for different
  819. contexts. See `org-show-hierarchy-above' for valid contexts."
  820. :group 'org-reveal-location
  821. :type org-context-choice)
  822. (defcustom org-show-entry-below '((default . nil))
  823. "Non-nil means show the entry below a headline when revealing a location.
  824. Org-mode often shows locations in an org-mode file which might have
  825. been invisible before. When this is set, the text below the headline that is
  826. exposed is also shown.
  827. By default this is off for all contexts.
  828. Instead of t, this can also be an alist specifying this option for different
  829. contexts. See `org-show-hierarchy-above' for valid contexts."
  830. :group 'org-reveal-location
  831. :type org-context-choice)
  832. (defcustom org-indirect-buffer-display 'other-window
  833. "How should indirect tree buffers be displayed?
  834. This applies to indirect buffers created with the commands
  835. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  836. Valid values are:
  837. current-window Display in the current window
  838. other-window Just display in another window.
  839. dedicated-frame Create one new frame, and re-use it each time.
  840. new-frame Make a new frame each time. Note that in this case
  841. previously-made indirect buffers are kept, and you need to
  842. kill these buffers yourself."
  843. :group 'org-structure
  844. :group 'org-agenda-windows
  845. :type '(choice
  846. (const :tag "In current window" current-window)
  847. (const :tag "In current frame, other window" other-window)
  848. (const :tag "Each time a new frame" new-frame)
  849. (const :tag "One dedicated frame" dedicated-frame)))
  850. (defcustom org-use-speed-commands nil
  851. "Non-nil means activate single letter commands at beginning of a headline.
  852. This may also be a function to test for appropriate locations where speed
  853. commands should be active."
  854. :group 'org-structure
  855. :type '(choice
  856. (const :tag "Never" nil)
  857. (const :tag "At beginning of headline stars" t)
  858. (function)))
  859. (defcustom org-speed-commands-user nil
  860. "Alist of additional speed commands.
  861. This list will be checked before `org-speed-commands-default'
  862. when the variable `org-use-speed-commands' is non-nil
  863. and when the cursor is at the beginning of a headline.
  864. The car if each entry is a string with a single letter, which must
  865. be assigned to `self-insert-command' in the global map.
  866. The cdr is either a command to be called interactively, a function
  867. to be called, or a form to be evaluated.
  868. An entry that is just a list with a single string will be interpreted
  869. as a descriptive headline that will be added when listing the speed
  870. commands in the Help buffer using the `?' speed command."
  871. :group 'org-structure
  872. :type '(repeat :value ("k" . ignore)
  873. (choice :value ("k" . ignore)
  874. (list :tag "Descriptive Headline" (string :tag "Headline"))
  875. (cons :tag "Letter and Command"
  876. (string :tag "Command letter")
  877. (choice
  878. (function)
  879. (sexp))))))
  880. (defgroup org-cycle nil
  881. "Options concerning visibility cycling in Org-mode."
  882. :tag "Org Cycle"
  883. :group 'org-structure)
  884. (defcustom org-cycle-skip-children-state-if-no-children t
  885. "Non-nil means skip CHILDREN state in entries that don't have any."
  886. :group 'org-cycle
  887. :type 'boolean)
  888. (defcustom org-cycle-max-level nil
  889. "Maximum level which should still be subject to visibility cycling.
  890. Levels higher than this will, for cycling, be treated as text, not a headline.
  891. When `org-odd-levels-only' is set, a value of N in this variable actually
  892. means 2N-1 stars as the limiting headline.
  893. When nil, cycle all levels.
  894. Note that the limiting level of cycling is also influenced by
  895. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  896. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  897. than its value."
  898. :group 'org-cycle
  899. :type '(choice
  900. (const :tag "No limit" nil)
  901. (integer :tag "Maximum level")))
  902. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  903. "Names of drawers. Drawers are not opened by cycling on the headline above.
  904. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  905. this:
  906. :DRAWERNAME:
  907. .....
  908. :END:
  909. The drawer \"PROPERTIES\" is special for capturing properties through
  910. the property API.
  911. Drawers can be defined on the per-file basis with a line like:
  912. #+DRAWERS: HIDDEN STATE PROPERTIES"
  913. :group 'org-structure
  914. :group 'org-cycle
  915. :type '(repeat (string :tag "Drawer Name")))
  916. (defcustom org-hide-block-startup nil
  917. "Non-nil means entering Org-mode will fold all blocks.
  918. This can also be set in on a per-file basis with
  919. #+STARTUP: hideblocks
  920. #+STARTUP: showblocks"
  921. :group 'org-startup
  922. :group 'org-cycle
  923. :type 'boolean)
  924. (defcustom org-cycle-global-at-bob nil
  925. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  926. This makes it possible to do global cycling without having to use S-TAB or
  927. \\[universal-argument] TAB. For this special case to work, the first line
  928. of the buffer must not be a headline -- it may be empty or some other text.
  929. When used in this way, `org-cycle-hook' is disabled temporarily to make
  930. sure the cursor stays at the beginning of the buffer. When this option is
  931. nil, don't do anything special at the beginning of the buffer."
  932. :group 'org-cycle
  933. :type 'boolean)
  934. (defcustom org-cycle-level-after-item/entry-creation t
  935. "Non-nil means cycle entry level or item indentation in new empty entries.
  936. When the cursor is at the end of an empty headline, i.e., with only stars
  937. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  938. and then all possible ancestor states, before returning to the original state.
  939. This makes data entry extremely fast: M-RET to create a new headline,
  940. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  941. When the cursor is at the end of an empty plain list item, one TAB will
  942. make it a subitem, two or more tabs will back up to make this an item
  943. higher up in the item hierarchy."
  944. :group 'org-cycle
  945. :type 'boolean)
  946. (defcustom org-cycle-emulate-tab t
  947. "Where should `org-cycle' emulate TAB.
  948. nil Never
  949. white Only in completely white lines
  950. whitestart Only at the beginning of lines, before the first non-white char
  951. t Everywhere except in headlines
  952. exc-hl-bol Everywhere except at the start of a headline
  953. If TAB is used in a place where it does not emulate TAB, the current subtree
  954. visibility is cycled."
  955. :group 'org-cycle
  956. :type '(choice (const :tag "Never" nil)
  957. (const :tag "Only in completely white lines" white)
  958. (const :tag "Before first char in a line" whitestart)
  959. (const :tag "Everywhere except in headlines" t)
  960. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  961. ))
  962. (defcustom org-cycle-separator-lines 2
  963. "Number of empty lines needed to keep an empty line between collapsed trees.
  964. If you leave an empty line between the end of a subtree and the following
  965. headline, this empty line is hidden when the subtree is folded.
  966. Org-mode will leave (exactly) one empty line visible if the number of
  967. empty lines is equal or larger to the number given in this variable.
  968. So the default 2 means at least 2 empty lines after the end of a subtree
  969. are needed to produce free space between a collapsed subtree and the
  970. following headline.
  971. If the number is negative, and the number of empty lines is at least -N,
  972. all empty lines are shown.
  973. Special case: when 0, never leave empty lines in collapsed view."
  974. :group 'org-cycle
  975. :type 'integer)
  976. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  977. (defcustom org-pre-cycle-hook nil
  978. "Hook that is run before visibility cycling is happening.
  979. The function(s) in this hook must accept a single argument which indicates
  980. the new state that will be set right after running this hook. The
  981. argument is a symbol. Before a global state change, it can have the values
  982. `overview', `content', or `all'. Before a local state change, it can have
  983. the values `folded', `children', or `subtree'."
  984. :group 'org-cycle
  985. :type 'hook)
  986. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  987. org-cycle-hide-drawers
  988. org-cycle-hide-inline-tasks
  989. org-cycle-show-empty-lines
  990. org-optimize-window-after-visibility-change)
  991. "Hook that is run after `org-cycle' has changed the buffer visibility.
  992. The function(s) in this hook must accept a single argument which indicates
  993. the new state that was set by the most recent `org-cycle' command. The
  994. argument is a symbol. After a global state change, it can have the values
  995. `overview', `contents', or `all'. After a local state change, it can have
  996. the values `folded', `children', or `subtree'."
  997. :group 'org-cycle
  998. :type 'hook)
  999. (defgroup org-edit-structure nil
  1000. "Options concerning structure editing in Org-mode."
  1001. :tag "Org Edit Structure"
  1002. :group 'org-structure)
  1003. (defcustom org-odd-levels-only nil
  1004. "Non-nil means skip even levels and only use odd levels for the outline.
  1005. This has the effect that two stars are being added/taken away in
  1006. promotion/demotion commands. It also influences how levels are
  1007. handled by the exporters.
  1008. Changing it requires restart of `font-lock-mode' to become effective
  1009. for fontification also in regions already fontified.
  1010. You may also set this on a per-file basis by adding one of the following
  1011. lines to the buffer:
  1012. #+STARTUP: odd
  1013. #+STARTUP: oddeven"
  1014. :group 'org-edit-structure
  1015. :group 'org-appearance
  1016. :type 'boolean)
  1017. (defcustom org-adapt-indentation t
  1018. "Non-nil means adapt indentation to outline node level.
  1019. When this variable is set, Org assumes that you write outlines by
  1020. indenting text in each node to align with the headline (after the stars).
  1021. The following issues are influenced by this variable:
  1022. - When this is set and the *entire* text in an entry is indented, the
  1023. indentation is increased by one space in a demotion command, and
  1024. decreased by one in a promotion command. If any line in the entry
  1025. body starts with text at column 0, indentation is not changed at all.
  1026. - Property drawers and planning information is inserted indented when
  1027. this variable s set. When nil, they will not be indented.
  1028. - TAB indents a line relative to context. The lines below a headline
  1029. will be indented when this variable is set.
  1030. Note that this is all about true indentation, by adding and removing
  1031. space characters. See also `org-indent.el' which does level-dependent
  1032. indentation in a virtual way, i.e. at display time in Emacs."
  1033. :group 'org-edit-structure
  1034. :type 'boolean)
  1035. (defcustom org-special-ctrl-a/e nil
  1036. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1037. When t, `C-a' will bring back the cursor to the beginning of the
  1038. headline text, i.e. after the stars and after a possible TODO
  1039. keyword. In an item, this will be the position after bullet and
  1040. check-box, if any. When the cursor is already at that position,
  1041. another `C-a' will bring it to the beginning of the line.
  1042. `C-e' will jump to the end of the headline, ignoring the presence
  1043. of tags in the headline. A second `C-e' will then jump to the
  1044. true end of the line, after any tags. This also means that, when
  1045. this variable is non-nil, `C-e' also will never jump beyond the
  1046. end of the heading of a folded section, i.e. not after the
  1047. ellipses.
  1048. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1049. normally, going to the true line boundary first. Only a directly
  1050. following, identical keypress will bring the cursor to the
  1051. special positions.
  1052. This may also be a cons cell where the behavior for `C-a' and
  1053. `C-e' is set separately."
  1054. :group 'org-edit-structure
  1055. :type '(choice
  1056. (const :tag "off" nil)
  1057. (const :tag "on: after stars/bullet and before tags first" t)
  1058. (const :tag "reversed: true line boundary first" reversed)
  1059. (cons :tag "Set C-a and C-e separately"
  1060. (choice :tag "Special C-a"
  1061. (const :tag "off" nil)
  1062. (const :tag "on: after stars/bullet first" t)
  1063. (const :tag "reversed: before stars/bullet first" reversed))
  1064. (choice :tag "Special C-e"
  1065. (const :tag "off" nil)
  1066. (const :tag "on: before tags first" t)
  1067. (const :tag "reversed: after tags first" reversed)))))
  1068. (if (fboundp 'defvaralias)
  1069. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  1070. (defcustom org-special-ctrl-k nil
  1071. "Non-nil means `C-k' will behave specially in headlines.
  1072. When nil, `C-k' will call the default `kill-line' command.
  1073. When t, the following will happen while the cursor is in the headline:
  1074. - When the cursor is at the beginning of a headline, kill the entire
  1075. line and possible the folded subtree below the line.
  1076. - When in the middle of the headline text, kill the headline up to the tags.
  1077. - When after the headline text, kill the tags."
  1078. :group 'org-edit-structure
  1079. :type 'boolean)
  1080. (defcustom org-ctrl-k-protect-subtree nil
  1081. "Non-nil means, do not delete a hidden subtree with C-k.
  1082. When set to the symbol `error', simply throw an error when C-k is
  1083. used to kill (part-of) a headline that has hidden text behind it.
  1084. Any other non-nil value will result in a query to the user, if it is
  1085. OK to kill that hidden subtree. When nil, kill without remorse."
  1086. :group 'org-edit-structure
  1087. :version "24.1"
  1088. :type '(choice
  1089. (const :tag "Do not protect hidden subtrees" nil)
  1090. (const :tag "Protect hidden subtrees with a security query" t)
  1091. (const :tag "Never kill a hidden subtree with C-k" error)))
  1092. (defcustom org-catch-invisible-edits nil
  1093. "Check if in invisible region before inserting or deleting a character.
  1094. Valid values are:
  1095. nil Do not check, so just do invisible edits.
  1096. error Throw an error and do nothing.
  1097. show Make point visible, and do the requested edit.
  1098. show-and-error Make point visible, then throw an error and abort the edit.
  1099. smart Make point visible, and do insertion/deletion if it is
  1100. adjacent to visible text and the change feels predictable.
  1101. Never delete a previously invisible character or add in the
  1102. middle or right after an invisible region. Basically, this
  1103. allows insertion and backward-delete right before ellipses.
  1104. FIXME: maybe in this case we should not even show?"
  1105. :group 'org-edit-structure
  1106. :version "24.1"
  1107. :type '(choice
  1108. (const :tag "Do not check" nil)
  1109. (const :tag "Throw error when trying to edit" error)
  1110. (const :tag "Unhide, but do not do the edit" show-and-error)
  1111. (const :tag "Show invisible part and do the edit" show)
  1112. (const :tag "Be smart and do the right thing" smart)))
  1113. (defcustom org-yank-folded-subtrees t
  1114. "Non-nil means when yanking subtrees, fold them.
  1115. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1116. it starts with a heading and all other headings in it are either children
  1117. or siblings, then fold all the subtrees. However, do this only if no
  1118. text after the yank would be swallowed into a folded tree by this action."
  1119. :group 'org-edit-structure
  1120. :type 'boolean)
  1121. (defcustom org-yank-adjusted-subtrees nil
  1122. "Non-nil means when yanking subtrees, adjust the level.
  1123. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1124. this function for details."
  1125. :group 'org-edit-structure
  1126. :type 'boolean)
  1127. (defcustom org-M-RET-may-split-line '((default . t))
  1128. "Non-nil means M-RET will split the line at the cursor position.
  1129. When nil, it will go to the end of the line before making a
  1130. new line.
  1131. You may also set this option in a different way for different
  1132. contexts. Valid contexts are:
  1133. headline when creating a new headline
  1134. item when creating a new item
  1135. table in a table field
  1136. default the value to be used for all contexts not explicitly
  1137. customized"
  1138. :group 'org-structure
  1139. :group 'org-table
  1140. :type '(choice
  1141. (const :tag "Always" t)
  1142. (const :tag "Never" nil)
  1143. (repeat :greedy t :tag "Individual contexts"
  1144. (cons
  1145. (choice :tag "Context"
  1146. (const headline)
  1147. (const item)
  1148. (const table)
  1149. (const default))
  1150. (boolean)))))
  1151. (defcustom org-insert-heading-respect-content nil
  1152. "Non-nil means insert new headings after the current subtree.
  1153. When nil, the new heading is created directly after the current line.
  1154. The commands \\[org-insert-heading-respect-content] and
  1155. \\[org-insert-todo-heading-respect-content] turn this variable on
  1156. for the duration of the command."
  1157. :group 'org-structure
  1158. :type 'boolean)
  1159. (defcustom org-blank-before-new-entry '((heading . auto)
  1160. (plain-list-item . auto))
  1161. "Should `org-insert-heading' leave a blank line before new heading/item?
  1162. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1163. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1164. which case Org will look at the surrounding headings/items and try to
  1165. make an intelligent decision whether to insert a blank line or not.
  1166. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1167. set, the setting here is ignored and no empty line is inserted, to avoid
  1168. breaking the list structure."
  1169. :group 'org-edit-structure
  1170. :type '(list
  1171. (cons (const heading)
  1172. (choice (const :tag "Never" nil)
  1173. (const :tag "Always" t)
  1174. (const :tag "Auto" auto)))
  1175. (cons (const plain-list-item)
  1176. (choice (const :tag "Never" nil)
  1177. (const :tag "Always" t)
  1178. (const :tag "Auto" auto)))))
  1179. (defcustom org-insert-heading-hook nil
  1180. "Hook being run after inserting a new heading."
  1181. :group 'org-edit-structure
  1182. :type 'hook)
  1183. (defcustom org-enable-fixed-width-editor t
  1184. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1185. This currently only means they are never auto-wrapped.
  1186. When nil, such lines will be treated like ordinary lines.
  1187. See also the QUOTE keyword."
  1188. :group 'org-edit-structure
  1189. :type 'boolean)
  1190. (defcustom org-goto-auto-isearch t
  1191. "Non-nil means typing characters in `org-goto' starts incremental search.
  1192. When nil, you can use these keybindings to navigate the buffer:
  1193. q Quit the org-goto interface
  1194. n Go to the next visible heading
  1195. p Go to the previous visible heading
  1196. f Go one heading forward on same level
  1197. b Go one heading backward on same level
  1198. u Go one heading up"
  1199. :group 'org-edit-structure
  1200. :type 'boolean)
  1201. (defgroup org-sparse-trees nil
  1202. "Options concerning sparse trees in Org-mode."
  1203. :tag "Org Sparse Trees"
  1204. :group 'org-structure)
  1205. (defcustom org-highlight-sparse-tree-matches t
  1206. "Non-nil means highlight all matches that define a sparse tree.
  1207. The highlights will automatically disappear the next time the buffer is
  1208. changed by an edit command."
  1209. :group 'org-sparse-trees
  1210. :type 'boolean)
  1211. (defcustom org-remove-highlights-with-change t
  1212. "Non-nil means any change to the buffer will remove temporary highlights.
  1213. Such highlights are created by `org-occur' and `org-clock-display'.
  1214. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1215. The highlights created by `org-preview-latex-fragment' always need
  1216. `C-c C-c' to be removed."
  1217. :group 'org-sparse-trees
  1218. :group 'org-time
  1219. :type 'boolean)
  1220. (defcustom org-occur-hook '(org-first-headline-recenter)
  1221. "Hook that is run after `org-occur' has constructed a sparse tree.
  1222. This can be used to recenter the window to show as much of the structure
  1223. as possible."
  1224. :group 'org-sparse-trees
  1225. :type 'hook)
  1226. (defgroup org-imenu-and-speedbar nil
  1227. "Options concerning imenu and speedbar in Org-mode."
  1228. :tag "Org Imenu and Speedbar"
  1229. :group 'org-structure)
  1230. (defcustom org-imenu-depth 2
  1231. "The maximum level for Imenu access to Org-mode headlines.
  1232. This also applied for speedbar access."
  1233. :group 'org-imenu-and-speedbar
  1234. :type 'integer)
  1235. (defgroup org-table nil
  1236. "Options concerning tables in Org-mode."
  1237. :tag "Org Table"
  1238. :group 'org)
  1239. (defcustom org-enable-table-editor 'optimized
  1240. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1241. When nil, such lines will be treated like ordinary lines.
  1242. When equal to the symbol `optimized', the table editor will be optimized to
  1243. do the following:
  1244. - Automatic overwrite mode in front of whitespace in table fields.
  1245. This makes the structure of the table stay in tact as long as the edited
  1246. field does not exceed the column width.
  1247. - Minimize the number of realigns. Normally, the table is aligned each time
  1248. TAB or RET are pressed to move to another field. With optimization this
  1249. happens only if changes to a field might have changed the column width.
  1250. Optimization requires replacing the functions `self-insert-command',
  1251. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1252. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1253. very good at guessing when a re-align will be necessary, but you can always
  1254. force one with \\[org-ctrl-c-ctrl-c].
  1255. If you would like to use the optimized version in Org-mode, but the
  1256. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1257. This variable can be used to turn on and off the table editor during a session,
  1258. but in order to toggle optimization, a restart is required.
  1259. See also the variable `org-table-auto-blank-field'."
  1260. :group 'org-table
  1261. :type '(choice
  1262. (const :tag "off" nil)
  1263. (const :tag "on" t)
  1264. (const :tag "on, optimized" optimized)))
  1265. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1266. (version<= emacs-version "24.1"))
  1267. "Non-nil means cluster self-insert commands for undo when possible.
  1268. If this is set, then, like in the Emacs command loop, 20 consecutive
  1269. characters will be undone together.
  1270. This is configurable, because there is some impact on typing performance."
  1271. :group 'org-table
  1272. :type 'boolean)
  1273. (defcustom org-table-tab-recognizes-table.el t
  1274. "Non-nil means TAB will automatically notice a table.el table.
  1275. When it sees such a table, it moves point into it and - if necessary -
  1276. calls `table-recognize-table'."
  1277. :group 'org-table-editing
  1278. :type 'boolean)
  1279. (defgroup org-link nil
  1280. "Options concerning links in Org-mode."
  1281. :tag "Org Link"
  1282. :group 'org)
  1283. (defvar org-link-abbrev-alist-local nil
  1284. "Buffer-local version of `org-link-abbrev-alist', which see.
  1285. The value of this is taken from the #+LINK lines.")
  1286. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1287. (defcustom org-link-abbrev-alist nil
  1288. "Alist of link abbreviations.
  1289. The car of each element is a string, to be replaced at the start of a link.
  1290. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1291. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1292. [[linkkey:tag][description]]
  1293. The 'linkkey' must be a word word, starting with a letter, followed
  1294. by letters, numbers, '-' or '_'.
  1295. If REPLACE is a string, the tag will simply be appended to create the link.
  1296. If the string contains \"%s\", the tag will be inserted there. If the string
  1297. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1298. at that point (see the function `url-hexify-string'). If the string contains
  1299. the specifier \"%(my-function)\", then the custom function `my-function' will
  1300. be invoked: this function takes the tag as its only argument and must return
  1301. a string.
  1302. REPLACE may also be a function that will be called with the tag as the
  1303. only argument to create the link, which should be returned as a string.
  1304. See the manual for examples."
  1305. :group 'org-link
  1306. :type '(repeat
  1307. (cons
  1308. (string :tag "Protocol")
  1309. (choice
  1310. (string :tag "Format")
  1311. (function)))))
  1312. (defcustom org-descriptive-links t
  1313. "Non-nil means Org will display descriptive links.
  1314. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1315. \"Org Website\", hiding the link itself and just displaying its
  1316. description. When set to `nil', Org will display the full links
  1317. literally.
  1318. You can interactively set the value of this variable by calling
  1319. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1320. :group 'org-link
  1321. :type 'boolean)
  1322. (defcustom org-link-file-path-type 'adaptive
  1323. "How the path name in file links should be stored.
  1324. Valid values are:
  1325. relative Relative to the current directory, i.e. the directory of the file
  1326. into which the link is being inserted.
  1327. absolute Absolute path, if possible with ~ for home directory.
  1328. noabbrev Absolute path, no abbreviation of home directory.
  1329. adaptive Use relative path for files in the current directory and sub-
  1330. directories of it. For other files, use an absolute path."
  1331. :group 'org-link
  1332. :type '(choice
  1333. (const relative)
  1334. (const absolute)
  1335. (const noabbrev)
  1336. (const adaptive)))
  1337. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1338. "Types of links that should be activated in Org-mode files.
  1339. This is a list of symbols, each leading to the activation of a certain link
  1340. type. In principle, it does not hurt to turn on most link types - there may
  1341. be a small gain when turning off unused link types. The types are:
  1342. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1343. angle Links in angular brackets that may contain whitespace like
  1344. <bbdb:Carsten Dominik>.
  1345. plain Plain links in normal text, no whitespace, like http://google.com.
  1346. radio Text that is matched by a radio target, see manual for details.
  1347. tag Tag settings in a headline (link to tag search).
  1348. date Time stamps (link to calendar).
  1349. footnote Footnote labels.
  1350. Changing this variable requires a restart of Emacs to become effective."
  1351. :group 'org-link
  1352. :type '(set :greedy t
  1353. (const :tag "Double bracket links" bracket)
  1354. (const :tag "Angular bracket links" angle)
  1355. (const :tag "Plain text links" plain)
  1356. (const :tag "Radio target matches" radio)
  1357. (const :tag "Tags" tag)
  1358. (const :tag "Timestamps" date)
  1359. (const :tag "Footnotes" footnote)))
  1360. (defcustom org-make-link-description-function nil
  1361. "Function to use for generating link descriptions from links.
  1362. When nil, the link location will be used. This function must take
  1363. two parameters: the first one is the link, the second one is the
  1364. description generated by `org-insert-link'. The function should
  1365. return the description to use."
  1366. :group 'org-link
  1367. :type 'function)
  1368. (defgroup org-link-store nil
  1369. "Options concerning storing links in Org-mode."
  1370. :tag "Org Store Link"
  1371. :group 'org-link)
  1372. (defcustom org-url-hexify-p t
  1373. "When non-nil, hexify URL when creating a link."
  1374. :type 'boolean
  1375. :version "24.3"
  1376. :group 'org-link-store)
  1377. (defcustom org-email-link-description-format "Email %c: %.30s"
  1378. "Format of the description part of a link to an email or usenet message.
  1379. The following %-escapes will be replaced by corresponding information:
  1380. %F full \"From\" field
  1381. %f name, taken from \"From\" field, address if no name
  1382. %T full \"To\" field
  1383. %t first name in \"To\" field, address if no name
  1384. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1385. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1386. %s subject
  1387. %d date
  1388. %m message-id.
  1389. You may use normal field width specification between the % and the letter.
  1390. This is for example useful to limit the length of the subject.
  1391. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1392. :group 'org-link-store
  1393. :type 'string)
  1394. (defcustom org-from-is-user-regexp
  1395. (let (r1 r2)
  1396. (when (and user-mail-address (not (string= user-mail-address "")))
  1397. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1398. (when (and user-full-name (not (string= user-full-name "")))
  1399. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1400. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1401. "Regexp matched against the \"From:\" header of an email or usenet message.
  1402. It should match if the message is from the user him/herself."
  1403. :group 'org-link-store
  1404. :type 'regexp)
  1405. (defcustom org-context-in-file-links t
  1406. "Non-nil means file links from `org-store-link' contain context.
  1407. A search string will be added to the file name with :: as separator and
  1408. used to find the context when the link is activated by the command
  1409. `org-open-at-point'. When this option is t, the entire active region
  1410. will be placed in the search string of the file link. If set to a
  1411. positive integer, only the first n lines of context will be stored.
  1412. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1413. negates this setting for the duration of the command."
  1414. :group 'org-link-store
  1415. :type '(choice boolean integer))
  1416. (defcustom org-keep-stored-link-after-insertion nil
  1417. "Non-nil means keep link in list for entire session.
  1418. The command `org-store-link' adds a link pointing to the current
  1419. location to an internal list. These links accumulate during a session.
  1420. The command `org-insert-link' can be used to insert links into any
  1421. Org-mode file (offering completion for all stored links). When this
  1422. option is nil, every link which has been inserted once using \\[org-insert-link]
  1423. will be removed from the list, to make completing the unused links
  1424. more efficient."
  1425. :group 'org-link-store
  1426. :type 'boolean)
  1427. (defgroup org-link-follow nil
  1428. "Options concerning following links in Org-mode."
  1429. :tag "Org Follow Link"
  1430. :group 'org-link)
  1431. (defcustom org-link-translation-function nil
  1432. "Function to translate links with different syntax to Org syntax.
  1433. This can be used to translate links created for example by the Planner
  1434. or emacs-wiki packages to Org syntax.
  1435. The function must accept two parameters, a TYPE containing the link
  1436. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1437. which is everything after the link protocol. It should return a cons
  1438. with possibly modified values of type and path.
  1439. Org contains a function for this, so if you set this variable to
  1440. `org-translate-link-from-planner', you should be able follow many
  1441. links created by planner."
  1442. :group 'org-link-follow
  1443. :type 'function)
  1444. (defcustom org-follow-link-hook nil
  1445. "Hook that is run after a link has been followed."
  1446. :group 'org-link-follow
  1447. :type 'hook)
  1448. (defcustom org-tab-follows-link nil
  1449. "Non-nil means on links TAB will follow the link.
  1450. Needs to be set before org.el is loaded.
  1451. This really should not be used, it does not make sense, and the
  1452. implementation is bad."
  1453. :group 'org-link-follow
  1454. :type 'boolean)
  1455. (defcustom org-return-follows-link nil
  1456. "Non-nil means on links RET will follow the link.
  1457. In tables, the special behavior of RET has precedence."
  1458. :group 'org-link-follow
  1459. :type 'boolean)
  1460. (defcustom org-mouse-1-follows-link
  1461. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1462. "Non-nil means mouse-1 on a link will follow the link.
  1463. A longer mouse click will still set point. Does not work on XEmacs.
  1464. Needs to be set before org.el is loaded."
  1465. :group 'org-link-follow
  1466. :type 'boolean)
  1467. (defcustom org-mark-ring-length 4
  1468. "Number of different positions to be recorded in the ring.
  1469. Changing this requires a restart of Emacs to work correctly."
  1470. :group 'org-link-follow
  1471. :type 'integer)
  1472. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1473. "Non-nil means internal links in Org files must exactly match a headline.
  1474. When nil, the link search tries to match a phrase with all words
  1475. in the search text."
  1476. :group 'org-link-follow
  1477. :version "24.1"
  1478. :type '(choice
  1479. (const :tag "Use fuzzy text search" nil)
  1480. (const :tag "Match only exact headline" t)
  1481. (const :tag "Match exact headline or query to create it"
  1482. query-to-create)))
  1483. (defcustom org-link-frame-setup
  1484. '((vm . vm-visit-folder-other-frame)
  1485. (vm-imap . vm-visit-imap-folder-other-frame)
  1486. (gnus . org-gnus-no-new-news)
  1487. (file . find-file-other-window)
  1488. (wl . wl-other-frame))
  1489. "Setup the frame configuration for following links.
  1490. When following a link with Emacs, it may often be useful to display
  1491. this link in another window or frame. This variable can be used to
  1492. set this up for the different types of links.
  1493. For VM, use any of
  1494. `vm-visit-folder'
  1495. `vm-visit-folder-other-window'
  1496. `vm-visit-folder-other-frame'
  1497. For Gnus, use any of
  1498. `gnus'
  1499. `gnus-other-frame'
  1500. `org-gnus-no-new-news'
  1501. For FILE, use any of
  1502. `find-file'
  1503. `find-file-other-window'
  1504. `find-file-other-frame'
  1505. For Wanderlust use any of
  1506. `wl'
  1507. `wl-other-frame'
  1508. For the calendar, use the variable `calendar-setup'.
  1509. For BBDB, it is currently only possible to display the matches in
  1510. another window."
  1511. :group 'org-link-follow
  1512. :type '(list
  1513. (cons (const vm)
  1514. (choice
  1515. (const vm-visit-folder)
  1516. (const vm-visit-folder-other-window)
  1517. (const vm-visit-folder-other-frame)))
  1518. (cons (const gnus)
  1519. (choice
  1520. (const gnus)
  1521. (const gnus-other-frame)
  1522. (const org-gnus-no-new-news)))
  1523. (cons (const file)
  1524. (choice
  1525. (const find-file)
  1526. (const find-file-other-window)
  1527. (const find-file-other-frame)))
  1528. (cons (const wl)
  1529. (choice
  1530. (const wl)
  1531. (const wl-other-frame)))))
  1532. (defcustom org-display-internal-link-with-indirect-buffer nil
  1533. "Non-nil means use indirect buffer to display infile links.
  1534. Activating internal links (from one location in a file to another location
  1535. in the same file) normally just jumps to the location. When the link is
  1536. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1537. is displayed in
  1538. another window. When this option is set, the other window actually displays
  1539. an indirect buffer clone of the current buffer, to avoid any visibility
  1540. changes to the current buffer."
  1541. :group 'org-link-follow
  1542. :type 'boolean)
  1543. (defcustom org-open-non-existing-files nil
  1544. "Non-nil means `org-open-file' will open non-existing files.
  1545. When nil, an error will be generated.
  1546. This variable applies only to external applications because they
  1547. might choke on non-existing files. If the link is to a file that
  1548. will be opened in Emacs, the variable is ignored."
  1549. :group 'org-link-follow
  1550. :type 'boolean)
  1551. (defcustom org-open-directory-means-index-dot-org nil
  1552. "Non-nil means a link to a directory really means to index.org.
  1553. When nil, following a directory link will run dired or open a finder/explorer
  1554. window on that directory."
  1555. :group 'org-link-follow
  1556. :type 'boolean)
  1557. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1558. "Function and arguments to call for following mailto links.
  1559. This is a list with the first element being a Lisp function, and the
  1560. remaining elements being arguments to the function. In string arguments,
  1561. %a will be replaced by the address, and %s will be replaced by the subject
  1562. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1563. :group 'org-link-follow
  1564. :type '(choice
  1565. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1566. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1567. (const :tag "message-mail" (message-mail "%a" "%s"))
  1568. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1569. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1570. "Non-nil means ask for confirmation before executing shell links.
  1571. Shell links can be dangerous: just think about a link
  1572. [[shell:rm -rf ~/*][Google Search]]
  1573. This link would show up in your Org-mode document as \"Google Search\",
  1574. but really it would remove your entire home directory.
  1575. Therefore we advise against setting this variable to nil.
  1576. Just change it to `y-or-n-p' if you want to confirm with a
  1577. single keystroke rather than having to type \"yes\"."
  1578. :group 'org-link-follow
  1579. :type '(choice
  1580. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1581. (const :tag "with y-or-n (faster)" y-or-n-p)
  1582. (const :tag "no confirmation (dangerous)" nil)))
  1583. (put 'org-confirm-shell-link-function
  1584. 'safe-local-variable
  1585. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1586. (defcustom org-confirm-shell-link-not-regexp ""
  1587. "A regexp to skip confirmation for shell links."
  1588. :group 'org-link-follow
  1589. :version "24.1"
  1590. :type 'regexp)
  1591. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1592. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1593. Elisp links can be dangerous: just think about a link
  1594. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1595. This link would show up in your Org-mode document as \"Google Search\",
  1596. but really it would remove your entire home directory.
  1597. Therefore we advise against setting this variable to nil.
  1598. Just change it to `y-or-n-p' if you want to confirm with a
  1599. single keystroke rather than having to type \"yes\"."
  1600. :group 'org-link-follow
  1601. :type '(choice
  1602. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1603. (const :tag "with y-or-n (faster)" y-or-n-p)
  1604. (const :tag "no confirmation (dangerous)" nil)))
  1605. (put 'org-confirm-shell-link-function
  1606. 'safe-local-variable
  1607. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1608. (defcustom org-confirm-elisp-link-not-regexp ""
  1609. "A regexp to skip confirmation for Elisp links."
  1610. :group 'org-link-follow
  1611. :version "24.1"
  1612. :type 'regexp)
  1613. (defconst org-file-apps-defaults-gnu
  1614. '((remote . emacs)
  1615. (system . mailcap)
  1616. (t . mailcap))
  1617. "Default file applications on a UNIX or GNU/Linux system.
  1618. See `org-file-apps'.")
  1619. (defconst org-file-apps-defaults-macosx
  1620. '((remote . emacs)
  1621. (t . "open %s")
  1622. (system . "open %s")
  1623. ("ps.gz" . "gv %s")
  1624. ("eps.gz" . "gv %s")
  1625. ("dvi" . "xdvi %s")
  1626. ("fig" . "xfig %s"))
  1627. "Default file applications on a MacOS X system.
  1628. The system \"open\" is known as a default, but we use X11 applications
  1629. for some files for which the OS does not have a good default.
  1630. See `org-file-apps'.")
  1631. (defconst org-file-apps-defaults-windowsnt
  1632. (list
  1633. '(remote . emacs)
  1634. (cons t
  1635. (list (if (featurep 'xemacs)
  1636. 'mswindows-shell-execute
  1637. 'w32-shell-execute)
  1638. "open" 'file))
  1639. (cons 'system
  1640. (list (if (featurep 'xemacs)
  1641. 'mswindows-shell-execute
  1642. 'w32-shell-execute)
  1643. "open" 'file)))
  1644. "Default file applications on a Windows NT system.
  1645. The system \"open\" is used for most files.
  1646. See `org-file-apps'.")
  1647. (defcustom org-file-apps
  1648. '(
  1649. (auto-mode . emacs)
  1650. ("\\.mm\\'" . default)
  1651. ("\\.x?html?\\'" . default)
  1652. ("\\.pdf\\'" . default)
  1653. )
  1654. "External applications for opening `file:path' items in a document.
  1655. Org-mode uses system defaults for different file types, but
  1656. you can use this variable to set the application for a given file
  1657. extension. The entries in this list are cons cells where the car identifies
  1658. files and the cdr the corresponding command. Possible values for the
  1659. file identifier are
  1660. \"string\" A string as a file identifier can be interpreted in different
  1661. ways, depending on its contents:
  1662. - Alphanumeric characters only:
  1663. Match links with this file extension.
  1664. Example: (\"pdf\" . \"evince %s\")
  1665. to open PDFs with evince.
  1666. - Regular expression: Match links where the
  1667. filename matches the regexp. If you want to
  1668. use groups here, use shy groups.
  1669. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1670. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1671. to open *.html and *.xhtml with firefox.
  1672. - Regular expression which contains (non-shy) groups:
  1673. Match links where the whole link, including \"::\", and
  1674. anything after that, matches the regexp.
  1675. In a custom command string, %1, %2, etc. are replaced with
  1676. the parts of the link that were matched by the groups.
  1677. For backwards compatibility, if a command string is given
  1678. that does not use any of the group matches, this case is
  1679. handled identically to the second one (i.e. match against
  1680. file name only).
  1681. In a custom lisp form, you can access the group matches with
  1682. (match-string n link).
  1683. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1684. to open [[file:document.pdf::5]] with evince at page 5.
  1685. `directory' Matches a directory
  1686. `remote' Matches a remote file, accessible through tramp or efs.
  1687. Remote files most likely should be visited through Emacs
  1688. because external applications cannot handle such paths.
  1689. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1690. so all files Emacs knows how to handle. Using this with
  1691. command `emacs' will open most files in Emacs. Beware that this
  1692. will also open html files inside Emacs, unless you add
  1693. (\"html\" . default) to the list as well.
  1694. t Default for files not matched by any of the other options.
  1695. `system' The system command to open files, like `open' on Windows
  1696. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1697. that will be selected if you call `C-c C-o' with a double
  1698. \\[universal-argument] \\[universal-argument] prefix.
  1699. Possible values for the command are:
  1700. `emacs' The file will be visited by the current Emacs process.
  1701. `default' Use the default application for this file type, which is the
  1702. association for t in the list, most likely in the system-specific
  1703. part.
  1704. This can be used to overrule an unwanted setting in the
  1705. system-specific variable.
  1706. `system' Use the system command for opening files, like \"open\".
  1707. This command is specified by the entry whose car is `system'.
  1708. Most likely, the system-specific version of this variable
  1709. does define this command, but you can overrule/replace it
  1710. here.
  1711. string A command to be executed by a shell; %s will be replaced
  1712. by the path to the file.
  1713. sexp A Lisp form which will be evaluated. The file path will
  1714. be available in the Lisp variable `file'.
  1715. For more examples, see the system specific constants
  1716. `org-file-apps-defaults-macosx'
  1717. `org-file-apps-defaults-windowsnt'
  1718. `org-file-apps-defaults-gnu'."
  1719. :group 'org-link-follow
  1720. :type '(repeat
  1721. (cons (choice :value ""
  1722. (string :tag "Extension")
  1723. (const :tag "System command to open files" system)
  1724. (const :tag "Default for unrecognized files" t)
  1725. (const :tag "Remote file" remote)
  1726. (const :tag "Links to a directory" directory)
  1727. (const :tag "Any files that have Emacs modes"
  1728. auto-mode))
  1729. (choice :value ""
  1730. (const :tag "Visit with Emacs" emacs)
  1731. (const :tag "Use default" default)
  1732. (const :tag "Use the system command" system)
  1733. (string :tag "Command")
  1734. (sexp :tag "Lisp form")))))
  1735. (defcustom org-doi-server-url "http://dx.doi.org/"
  1736. "The URL of the DOI server."
  1737. :type 'string
  1738. :version "24.3"
  1739. :group 'org-link-follow)
  1740. (defgroup org-refile nil
  1741. "Options concerning refiling entries in Org-mode."
  1742. :tag "Org Refile"
  1743. :group 'org)
  1744. (defcustom org-directory "~/org"
  1745. "Directory with org files.
  1746. This is just a default location to look for Org files. There is no need
  1747. at all to put your files into this directory. It is only used in the
  1748. following situations:
  1749. 1. When a capture template specifies a target file that is not an
  1750. absolute path. The path will then be interpreted relative to
  1751. `org-directory'
  1752. 2. When a capture note is filed away in an interactive way (when exiting the
  1753. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1754. with `org-directory' as the default path."
  1755. :group 'org-refile
  1756. :group 'org-remember
  1757. :group 'org-capture
  1758. :type 'directory)
  1759. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1760. "Default target for storing notes.
  1761. Used as a fall back file for org-remember.el and org-capture.el, for
  1762. templates that do not specify a target file."
  1763. :group 'org-refile
  1764. :group 'org-remember
  1765. :group 'org-capture
  1766. :type '(choice
  1767. (const :tag "Default from remember-data-file" nil)
  1768. file))
  1769. (defcustom org-goto-interface 'outline
  1770. "The default interface to be used for `org-goto'.
  1771. Allowed values are:
  1772. outline The interface shows an outline of the relevant file
  1773. and the correct heading is found by moving through
  1774. the outline or by searching with incremental search.
  1775. outline-path-completion Headlines in the current buffer are offered via
  1776. completion. This is the interface also used by
  1777. the refile command."
  1778. :group 'org-refile
  1779. :type '(choice
  1780. (const :tag "Outline" outline)
  1781. (const :tag "Outline-path-completion" outline-path-completion)))
  1782. (defcustom org-goto-max-level 5
  1783. "Maximum target level when running `org-goto' with refile interface."
  1784. :group 'org-refile
  1785. :type 'integer)
  1786. (defcustom org-reverse-note-order nil
  1787. "Non-nil means store new notes at the beginning of a file or entry.
  1788. When nil, new notes will be filed to the end of a file or entry.
  1789. This can also be a list with cons cells of regular expressions that
  1790. are matched against file names, and values."
  1791. :group 'org-remember
  1792. :group 'org-capture
  1793. :group 'org-refile
  1794. :type '(choice
  1795. (const :tag "Reverse always" t)
  1796. (const :tag "Reverse never" nil)
  1797. (repeat :tag "By file name regexp"
  1798. (cons regexp boolean))))
  1799. (defcustom org-log-refile nil
  1800. "Information to record when a task is refiled.
  1801. Possible values are:
  1802. nil Don't add anything
  1803. time Add a time stamp to the task
  1804. note Prompt for a note and add it with template `org-log-note-headings'
  1805. This option can also be set with on a per-file-basis with
  1806. #+STARTUP: nologrefile
  1807. #+STARTUP: logrefile
  1808. #+STARTUP: lognoterefile
  1809. You can have local logging settings for a subtree by setting the LOGGING
  1810. property to one or more of these keywords.
  1811. When bulk-refiling from the agenda, the value `note' is forbidden and
  1812. will temporarily be changed to `time'."
  1813. :group 'org-refile
  1814. :group 'org-progress
  1815. :version "24.1"
  1816. :type '(choice
  1817. (const :tag "No logging" nil)
  1818. (const :tag "Record timestamp" time)
  1819. (const :tag "Record timestamp with note." note)))
  1820. (defcustom org-refile-targets nil
  1821. "Targets for refiling entries with \\[org-refile].
  1822. This is a list of cons cells. Each cell contains:
  1823. - a specification of the files to be considered, either a list of files,
  1824. or a symbol whose function or variable value will be used to retrieve
  1825. a file name or a list of file names. If you use `org-agenda-files' for
  1826. that, all agenda files will be scanned for targets. Nil means consider
  1827. headings in the current buffer.
  1828. - A specification of how to find candidate refile targets. This may be
  1829. any of:
  1830. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1831. This tag has to be present in all target headlines, inheritance will
  1832. not be considered.
  1833. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1834. todo keyword.
  1835. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1836. headlines that are refiling targets.
  1837. - a cons cell (:level . N). Any headline of level N is considered a target.
  1838. Note that, when `org-odd-levels-only' is set, level corresponds to
  1839. order in hierarchy, not to the number of stars.
  1840. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1841. Note that, when `org-odd-levels-only' is set, level corresponds to
  1842. order in hierarchy, not to the number of stars.
  1843. Each element of this list generates a set of possible targets.
  1844. The union of these sets is presented (with completion) to
  1845. the user by `org-refile'.
  1846. You can set the variable `org-refile-target-verify-function' to a function
  1847. to verify each headline found by the simple criteria above.
  1848. When this variable is nil, all top-level headlines in the current buffer
  1849. are used, equivalent to the value `((nil . (:level . 1))'."
  1850. :group 'org-refile
  1851. :type '(repeat
  1852. (cons
  1853. (choice :value org-agenda-files
  1854. (const :tag "All agenda files" org-agenda-files)
  1855. (const :tag "Current buffer" nil)
  1856. (function) (variable) (file))
  1857. (choice :tag "Identify target headline by"
  1858. (cons :tag "Specific tag" (const :value :tag) (string))
  1859. (cons :tag "TODO keyword" (const :value :todo) (string))
  1860. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1861. (cons :tag "Level number" (const :value :level) (integer))
  1862. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1863. (defcustom org-refile-target-verify-function nil
  1864. "Function to verify if the headline at point should be a refile target.
  1865. The function will be called without arguments, with point at the
  1866. beginning of the headline. It should return t and leave point
  1867. where it is if the headline is a valid target for refiling.
  1868. If the target should not be selected, the function must return nil.
  1869. In addition to this, it may move point to a place from where the search
  1870. should be continued. For example, the function may decide that the entire
  1871. subtree of the current entry should be excluded and move point to the end
  1872. of the subtree."
  1873. :group 'org-refile
  1874. :type 'function)
  1875. (defcustom org-refile-use-cache nil
  1876. "Non-nil means cache refile targets to speed up the process.
  1877. The cache for a particular file will be updated automatically when
  1878. the buffer has been killed, or when any of the marker used for flagging
  1879. refile targets no longer points at a live buffer.
  1880. If you have added new entries to a buffer that might themselves be targets,
  1881. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1882. find that easier, `C-u C-u C-u C-c C-w'."
  1883. :group 'org-refile
  1884. :version "24.1"
  1885. :type 'boolean)
  1886. (defcustom org-refile-use-outline-path nil
  1887. "Non-nil means provide refile targets as paths.
  1888. So a level 3 headline will be available as level1/level2/level3.
  1889. When the value is `file', also include the file name (without directory)
  1890. into the path. In this case, you can also stop the completion after
  1891. the file name, to get entries inserted as top level in the file.
  1892. When `full-file-path', include the full file path."
  1893. :group 'org-refile
  1894. :type '(choice
  1895. (const :tag "Not" nil)
  1896. (const :tag "Yes" t)
  1897. (const :tag "Start with file name" file)
  1898. (const :tag "Start with full file path" full-file-path)))
  1899. (defcustom org-outline-path-complete-in-steps t
  1900. "Non-nil means complete the outline path in hierarchical steps.
  1901. When Org-mode uses the refile interface to select an outline path
  1902. \(see variable `org-refile-use-outline-path'), the completion of
  1903. the path can be done is a single go, or if can be done in steps down
  1904. the headline hierarchy. Going in steps is probably the best if you
  1905. do not use a special completion package like `ido' or `icicles'.
  1906. However, when using these packages, going in one step can be very
  1907. fast, while still showing the whole path to the entry."
  1908. :group 'org-refile
  1909. :type 'boolean)
  1910. (defcustom org-refile-allow-creating-parent-nodes nil
  1911. "Non-nil means allow to create new nodes as refile targets.
  1912. New nodes are then created by adding \"/new node name\" to the completion
  1913. of an existing node. When the value of this variable is `confirm',
  1914. new node creation must be confirmed by the user (recommended)
  1915. When nil, the completion must match an existing entry.
  1916. Note that, if the new heading is not seen by the criteria
  1917. listed in `org-refile-targets', multiple instances of the same
  1918. heading would be created by trying again to file under the new
  1919. heading."
  1920. :group 'org-refile
  1921. :type '(choice
  1922. (const :tag "Never" nil)
  1923. (const :tag "Always" t)
  1924. (const :tag "Prompt for confirmation" confirm)))
  1925. (defcustom org-refile-active-region-within-subtree nil
  1926. "Non-nil means also refile active region within a subtree.
  1927. By default `org-refile' doesn't allow refiling regions if they
  1928. don't contain a set of subtrees, but it might be convenient to
  1929. do so sometimes: in that case, the first line of the region is
  1930. converted to a headline before refiling."
  1931. :group 'org-refile
  1932. :version "24.1"
  1933. :type 'boolean)
  1934. (defgroup org-todo nil
  1935. "Options concerning TODO items in Org-mode."
  1936. :tag "Org TODO"
  1937. :group 'org)
  1938. (defgroup org-progress nil
  1939. "Options concerning Progress logging in Org-mode."
  1940. :tag "Org Progress"
  1941. :group 'org-time)
  1942. (defvar org-todo-interpretation-widgets
  1943. '((:tag "Sequence (cycling hits every state)" sequence)
  1944. (:tag "Type (cycling directly to DONE)" type))
  1945. "The available interpretation symbols for customizing `org-todo-keywords'.
  1946. Interested libraries should add to this list.")
  1947. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1948. "List of TODO entry keyword sequences and their interpretation.
  1949. \\<org-mode-map>This is a list of sequences.
  1950. Each sequence starts with a symbol, either `sequence' or `type',
  1951. indicating if the keywords should be interpreted as a sequence of
  1952. action steps, or as different types of TODO items. The first
  1953. keywords are states requiring action - these states will select a headline
  1954. for inclusion into the global TODO list Org-mode produces. If one of
  1955. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1956. signify that no further action is necessary. If \"|\" is not found,
  1957. the last keyword is treated as the only DONE state of the sequence.
  1958. The command \\[org-todo] cycles an entry through these states, and one
  1959. additional state where no keyword is present. For details about this
  1960. cycling, see the manual.
  1961. TODO keywords and interpretation can also be set on a per-file basis with
  1962. the special #+SEQ_TODO and #+TYP_TODO lines.
  1963. Each keyword can optionally specify a character for fast state selection
  1964. \(in combination with the variable `org-use-fast-todo-selection')
  1965. and specifiers for state change logging, using the same syntax that
  1966. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1967. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1968. indicates to record a time stamp each time this state is selected.
  1969. Each keyword may also specify if a timestamp or a note should be
  1970. recorded when entering or leaving the state, by adding additional
  1971. characters in the parenthesis after the keyword. This looks like this:
  1972. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1973. record only the time of the state change. With X and Y being either
  1974. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1975. Y when leaving the state if and only if the *target* state does not
  1976. define X. You may omit any of the fast-selection key or X or /Y,
  1977. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1978. For backward compatibility, this variable may also be just a list
  1979. of keywords. In this case the interpretation (sequence or type) will be
  1980. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1981. :group 'org-todo
  1982. :group 'org-keywords
  1983. :type '(choice
  1984. (repeat :tag "Old syntax, just keywords"
  1985. (string :tag "Keyword"))
  1986. (repeat :tag "New syntax"
  1987. (cons
  1988. (choice
  1989. :tag "Interpretation"
  1990. ;;Quick and dirty way to see
  1991. ;;`org-todo-interpretations'. This takes the
  1992. ;;place of item arguments
  1993. :convert-widget
  1994. (lambda (widget)
  1995. (widget-put widget
  1996. :args (mapcar
  1997. #'(lambda (x)
  1998. (widget-convert
  1999. (cons 'const x)))
  2000. org-todo-interpretation-widgets))
  2001. widget))
  2002. (repeat
  2003. (string :tag "Keyword"))))))
  2004. (defvar org-todo-keywords-1 nil
  2005. "All TODO and DONE keywords active in a buffer.")
  2006. (make-variable-buffer-local 'org-todo-keywords-1)
  2007. (defvar org-todo-keywords-for-agenda nil)
  2008. (defvar org-done-keywords-for-agenda nil)
  2009. (defvar org-drawers-for-agenda nil)
  2010. (defvar org-todo-keyword-alist-for-agenda nil)
  2011. (defvar org-tag-alist-for-agenda nil)
  2012. (defvar org-agenda-contributing-files nil)
  2013. (defvar org-not-done-keywords nil)
  2014. (make-variable-buffer-local 'org-not-done-keywords)
  2015. (defvar org-done-keywords nil)
  2016. (make-variable-buffer-local 'org-done-keywords)
  2017. (defvar org-todo-heads nil)
  2018. (make-variable-buffer-local 'org-todo-heads)
  2019. (defvar org-todo-sets nil)
  2020. (make-variable-buffer-local 'org-todo-sets)
  2021. (defvar org-todo-log-states nil)
  2022. (make-variable-buffer-local 'org-todo-log-states)
  2023. (defvar org-todo-kwd-alist nil)
  2024. (make-variable-buffer-local 'org-todo-kwd-alist)
  2025. (defvar org-todo-key-alist nil)
  2026. (make-variable-buffer-local 'org-todo-key-alist)
  2027. (defvar org-todo-key-trigger nil)
  2028. (make-variable-buffer-local 'org-todo-key-trigger)
  2029. (defcustom org-todo-interpretation 'sequence
  2030. "Controls how TODO keywords are interpreted.
  2031. This variable is in principle obsolete and is only used for
  2032. backward compatibility, if the interpretation of todo keywords is
  2033. not given already in `org-todo-keywords'. See that variable for
  2034. more information."
  2035. :group 'org-todo
  2036. :group 'org-keywords
  2037. :type '(choice (const sequence)
  2038. (const type)))
  2039. (defcustom org-use-fast-todo-selection t
  2040. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2041. This variable describes if and under what circumstances the cycling
  2042. mechanism for TODO keywords will be replaced by a single-key, direct
  2043. selection scheme.
  2044. When nil, fast selection is never used.
  2045. When the symbol `prefix', it will be used when `org-todo' is called
  2046. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2047. `C-u t' in an agenda buffer.
  2048. When t, fast selection is used by default. In this case, the prefix
  2049. argument forces cycling instead.
  2050. In all cases, the special interface is only used if access keys have
  2051. actually been assigned by the user, i.e. if keywords in the configuration
  2052. are followed by a letter in parenthesis, like TODO(t)."
  2053. :group 'org-todo
  2054. :type '(choice
  2055. (const :tag "Never" nil)
  2056. (const :tag "By default" t)
  2057. (const :tag "Only with C-u C-c C-t" prefix)))
  2058. (defcustom org-provide-todo-statistics t
  2059. "Non-nil means update todo statistics after insert and toggle.
  2060. ALL-HEADLINES means update todo statistics by including headlines
  2061. with no TODO keyword as well, counting them as not done.
  2062. A list of TODO keywords means the same, but skip keywords that are
  2063. not in this list.
  2064. When this is set, todo statistics is updated in the parent of the
  2065. current entry each time a todo state is changed."
  2066. :group 'org-todo
  2067. :type '(choice
  2068. (const :tag "Yes, only for TODO entries" t)
  2069. (const :tag "Yes, including all entries" 'all-headlines)
  2070. (repeat :tag "Yes, for TODOs in this list"
  2071. (string :tag "TODO keyword"))
  2072. (other :tag "No TODO statistics" nil)))
  2073. (defcustom org-hierarchical-todo-statistics t
  2074. "Non-nil means TODO statistics covers just direct children.
  2075. When nil, all entries in the subtree are considered.
  2076. This has only an effect if `org-provide-todo-statistics' is set.
  2077. To set this to nil for only a single subtree, use a COOKIE_DATA
  2078. property and include the word \"recursive\" into the value."
  2079. :group 'org-todo
  2080. :type 'boolean)
  2081. (defcustom org-after-todo-state-change-hook nil
  2082. "Hook which is run after the state of a TODO item was changed.
  2083. The new state (a string with a TODO keyword, or nil) is available in the
  2084. Lisp variable `org-state'."
  2085. :group 'org-todo
  2086. :type 'hook)
  2087. (defvar org-blocker-hook nil
  2088. "Hook for functions that are allowed to block a state change.
  2089. Functions in this hook should not modify the buffer.
  2090. Each function gets as its single argument a property list,
  2091. see `org-trigger-hook' for more information about this list.
  2092. If any of the functions in this hook returns nil, the state change
  2093. is blocked.")
  2094. (defvar org-trigger-hook nil
  2095. "Hook for functions that are triggered by a state change.
  2096. Each function gets as its single argument a property list with at
  2097. least the following elements:
  2098. (:type type-of-change :position pos-at-entry-start
  2099. :from old-state :to new-state)
  2100. Depending on the type, more properties may be present.
  2101. This mechanism is currently implemented for:
  2102. TODO state changes
  2103. ------------------
  2104. :type todo-state-change
  2105. :from previous state (keyword as a string), or nil, or a symbol
  2106. 'todo' or 'done', to indicate the general type of state.
  2107. :to new state, like in :from")
  2108. (defcustom org-enforce-todo-dependencies nil
  2109. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2110. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2111. be blocked if any prior sibling is not yet done.
  2112. Finally, if the parent is blocked because of ordered siblings of its own,
  2113. the child will also be blocked."
  2114. :set (lambda (var val)
  2115. (set var val)
  2116. (if val
  2117. (add-hook 'org-blocker-hook
  2118. 'org-block-todo-from-children-or-siblings-or-parent)
  2119. (remove-hook 'org-blocker-hook
  2120. 'org-block-todo-from-children-or-siblings-or-parent)))
  2121. :group 'org-todo
  2122. :type 'boolean)
  2123. (defcustom org-enforce-todo-checkbox-dependencies nil
  2124. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2125. When this is nil, checkboxes have no influence on switching TODO states.
  2126. When non-nil, you first need to check off all check boxes before the TODO
  2127. entry can be switched to DONE.
  2128. This variable needs to be set before org.el is loaded, and you need to
  2129. restart Emacs after a change to make the change effective. The only way
  2130. to change is while Emacs is running is through the customize interface."
  2131. :set (lambda (var val)
  2132. (set var val)
  2133. (if val
  2134. (add-hook 'org-blocker-hook
  2135. 'org-block-todo-from-checkboxes)
  2136. (remove-hook 'org-blocker-hook
  2137. 'org-block-todo-from-checkboxes)))
  2138. :group 'org-todo
  2139. :type 'boolean)
  2140. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2141. "Non-nil means inserting a TODO heading is treated as state change.
  2142. So when the command \\[org-insert-todo-heading] is used, state change
  2143. logging will apply if appropriate. When nil, the new TODO item will
  2144. be inserted directly, and no logging will take place."
  2145. :group 'org-todo
  2146. :type 'boolean)
  2147. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2148. "Non-nil means switching TODO states with S-cursor counts as state change.
  2149. This is the default behavior. However, setting this to nil allows a
  2150. convenient way to select a TODO state and bypass any logging associated
  2151. with that."
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-todo-state-tags-triggers nil
  2155. "Tag changes that should be triggered by TODO state changes.
  2156. This is a list. Each entry is
  2157. (state-change (tag . flag) .......)
  2158. State-change can be a string with a state, and empty string to indicate the
  2159. state that has no TODO keyword, or it can be one of the symbols `todo'
  2160. or `done', meaning any not-done or done state, respectively."
  2161. :group 'org-todo
  2162. :group 'org-tags
  2163. :type '(repeat
  2164. (cons (choice :tag "When changing to"
  2165. (const :tag "Not-done state" todo)
  2166. (const :tag "Done state" done)
  2167. (string :tag "State"))
  2168. (repeat
  2169. (cons :tag "Tag action"
  2170. (string :tag "Tag")
  2171. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2172. (defcustom org-log-done nil
  2173. "Information to record when a task moves to the DONE state.
  2174. Possible values are:
  2175. nil Don't add anything, just change the keyword
  2176. time Add a time stamp to the task
  2177. note Prompt for a note and add it with template `org-log-note-headings'
  2178. This option can also be set with on a per-file-basis with
  2179. #+STARTUP: nologdone
  2180. #+STARTUP: logdone
  2181. #+STARTUP: lognotedone
  2182. You can have local logging settings for a subtree by setting the LOGGING
  2183. property to one or more of these keywords."
  2184. :group 'org-todo
  2185. :group 'org-progress
  2186. :type '(choice
  2187. (const :tag "No logging" nil)
  2188. (const :tag "Record CLOSED timestamp" time)
  2189. (const :tag "Record CLOSED timestamp with note." note)))
  2190. ;; Normalize old uses of org-log-done.
  2191. (cond
  2192. ((eq org-log-done t) (setq org-log-done 'time))
  2193. ((and (listp org-log-done) (memq 'done org-log-done))
  2194. (setq org-log-done 'note)))
  2195. (defcustom org-log-reschedule nil
  2196. "Information to record when the scheduling date of a tasks is modified.
  2197. Possible values are:
  2198. nil Don't add anything, just change the date
  2199. time Add a time stamp to the task
  2200. note Prompt for a note and add it with template `org-log-note-headings'
  2201. This option can also be set with on a per-file-basis with
  2202. #+STARTUP: nologreschedule
  2203. #+STARTUP: logreschedule
  2204. #+STARTUP: lognotereschedule"
  2205. :group 'org-todo
  2206. :group 'org-progress
  2207. :type '(choice
  2208. (const :tag "No logging" nil)
  2209. (const :tag "Record timestamp" time)
  2210. (const :tag "Record timestamp with note." note)))
  2211. (defcustom org-log-redeadline nil
  2212. "Information to record when the deadline date of a tasks is modified.
  2213. Possible values are:
  2214. nil Don't add anything, just change the date
  2215. time Add a time stamp to the task
  2216. note Prompt for a note and add it with template `org-log-note-headings'
  2217. This option can also be set with on a per-file-basis with
  2218. #+STARTUP: nologredeadline
  2219. #+STARTUP: logredeadline
  2220. #+STARTUP: lognoteredeadline
  2221. You can have local logging settings for a subtree by setting the LOGGING
  2222. property to one or more of these keywords."
  2223. :group 'org-todo
  2224. :group 'org-progress
  2225. :type '(choice
  2226. (const :tag "No logging" nil)
  2227. (const :tag "Record timestamp" time)
  2228. (const :tag "Record timestamp with note." note)))
  2229. (defcustom org-log-note-clock-out nil
  2230. "Non-nil means record a note when clocking out of an item.
  2231. This can also be configured on a per-file basis by adding one of
  2232. the following lines anywhere in the buffer:
  2233. #+STARTUP: lognoteclock-out
  2234. #+STARTUP: nolognoteclock-out"
  2235. :group 'org-todo
  2236. :group 'org-progress
  2237. :type 'boolean)
  2238. (defcustom org-log-done-with-time t
  2239. "Non-nil means the CLOSED time stamp will contain date and time.
  2240. When nil, only the date will be recorded."
  2241. :group 'org-progress
  2242. :type 'boolean)
  2243. (defcustom org-log-note-headings
  2244. '((done . "CLOSING NOTE %t")
  2245. (state . "State %-12s from %-12S %t")
  2246. (note . "Note taken on %t")
  2247. (reschedule . "Rescheduled from %S on %t")
  2248. (delschedule . "Not scheduled, was %S on %t")
  2249. (redeadline . "New deadline from %S on %t")
  2250. (deldeadline . "Removed deadline, was %S on %t")
  2251. (refile . "Refiled on %t")
  2252. (clock-out . ""))
  2253. "Headings for notes added to entries.
  2254. The value is an alist, with the car being a symbol indicating the note
  2255. context, and the cdr is the heading to be used. The heading may also be the
  2256. empty string.
  2257. %t in the heading will be replaced by a time stamp.
  2258. %T will be an active time stamp instead the default inactive one
  2259. %d will be replaced by a short-format time stamp.
  2260. %D will be replaced by an active short-format time stamp.
  2261. %s will be replaced by the new TODO state, in double quotes.
  2262. %S will be replaced by the old TODO state, in double quotes.
  2263. %u will be replaced by the user name.
  2264. %U will be replaced by the full user name.
  2265. In fact, it is not a good idea to change the `state' entry, because
  2266. agenda log mode depends on the format of these entries."
  2267. :group 'org-todo
  2268. :group 'org-progress
  2269. :type '(list :greedy t
  2270. (cons (const :tag "Heading when closing an item" done) string)
  2271. (cons (const :tag
  2272. "Heading when changing todo state (todo sequence only)"
  2273. state) string)
  2274. (cons (const :tag "Heading when just taking a note" note) string)
  2275. (cons (const :tag "Heading when clocking out" clock-out) string)
  2276. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2277. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2278. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2279. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2280. (cons (const :tag "Heading when refiling" refile) string)))
  2281. (unless (assq 'note org-log-note-headings)
  2282. (push '(note . "%t") org-log-note-headings))
  2283. (defcustom org-log-into-drawer nil
  2284. "Non-nil means insert state change notes and time stamps into a drawer.
  2285. When nil, state changes notes will be inserted after the headline and
  2286. any scheduling and clock lines, but not inside a drawer.
  2287. The value of this variable should be the name of the drawer to use.
  2288. LOGBOOK is proposed as the default drawer for this purpose, you can
  2289. also set this to a string to define the drawer of your choice.
  2290. A value of t is also allowed, representing \"LOGBOOK\".
  2291. A value of t or nil can also be set with on a per-file-basis with
  2292. #+STARTUP: logdrawer
  2293. #+STARTUP: nologdrawer
  2294. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2295. will be ignored.
  2296. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2297. a subtree."
  2298. :group 'org-todo
  2299. :group 'org-progress
  2300. :type '(choice
  2301. (const :tag "Not into a drawer" nil)
  2302. (const :tag "LOGBOOK" t)
  2303. (string :tag "Other")))
  2304. (if (fboundp 'defvaralias)
  2305. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2306. (defun org-log-into-drawer ()
  2307. "Return the value of `org-log-into-drawer', but let properties overrule.
  2308. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2309. used instead of the default value."
  2310. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2311. (cond
  2312. ((not p) org-log-into-drawer)
  2313. ((equal p "nil") nil)
  2314. ((equal p "t") "LOGBOOK")
  2315. (t p))))
  2316. (defcustom org-log-state-notes-insert-after-drawers nil
  2317. "Non-nil means insert state change notes after any drawers in entry.
  2318. Only the drawers that *immediately* follow the headline and the
  2319. deadline/scheduled line are skipped.
  2320. When nil, insert notes right after the heading and perhaps the line
  2321. with deadline/scheduling if present.
  2322. This variable will have no effect if `org-log-into-drawer' is
  2323. set."
  2324. :group 'org-todo
  2325. :group 'org-progress
  2326. :type 'boolean)
  2327. (defcustom org-log-states-order-reversed t
  2328. "Non-nil means the latest state note will be directly after heading.
  2329. When nil, the state change notes will be ordered according to time.
  2330. This option can also be set with on a per-file-basis with
  2331. #+STARTUP: logstatesreversed
  2332. #+STARTUP: nologstatesreversed"
  2333. :group 'org-todo
  2334. :group 'org-progress
  2335. :type 'boolean)
  2336. (defcustom org-todo-repeat-to-state nil
  2337. "The TODO state to which a repeater should return the repeating task.
  2338. By default this is the first task in a TODO sequence, or the previous state
  2339. in a TODO_TYP set. But you can specify another task here.
  2340. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2341. :group 'org-todo
  2342. :version "24.1"
  2343. :type '(choice (const :tag "Head of sequence" nil)
  2344. (string :tag "Specific state")))
  2345. (defcustom org-log-repeat 'time
  2346. "Non-nil means record moving through the DONE state when triggering repeat.
  2347. An auto-repeating task is immediately switched back to TODO when
  2348. marked DONE. If you are not logging state changes (by adding \"@\"
  2349. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2350. record a closing note, there will be no record of the task moving
  2351. through DONE. This variable forces taking a note anyway.
  2352. nil Don't force a record
  2353. time Record a time stamp
  2354. note Prompt for a note and add it with template `org-log-note-headings'
  2355. This option can also be set with on a per-file-basis with
  2356. #+STARTUP: nologrepeat
  2357. #+STARTUP: logrepeat
  2358. #+STARTUP: lognoterepeat
  2359. You can have local logging settings for a subtree by setting the LOGGING
  2360. property to one or more of these keywords."
  2361. :group 'org-todo
  2362. :group 'org-progress
  2363. :type '(choice
  2364. (const :tag "Don't force a record" nil)
  2365. (const :tag "Force recording the DONE state" time)
  2366. (const :tag "Force recording a note with the DONE state" note)))
  2367. (defgroup org-priorities nil
  2368. "Priorities in Org-mode."
  2369. :tag "Org Priorities"
  2370. :group 'org-todo)
  2371. (defcustom org-enable-priority-commands t
  2372. "Non-nil means priority commands are active.
  2373. When nil, these commands will be disabled, so that you never accidentally
  2374. set a priority."
  2375. :group 'org-priorities
  2376. :type 'boolean)
  2377. (defcustom org-highest-priority ?A
  2378. "The highest priority of TODO items. A character like ?A, ?B etc.
  2379. Must have a smaller ASCII number than `org-lowest-priority'."
  2380. :group 'org-priorities
  2381. :type 'character)
  2382. (defcustom org-lowest-priority ?C
  2383. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2384. Must have a larger ASCII number than `org-highest-priority'."
  2385. :group 'org-priorities
  2386. :type 'character)
  2387. (defcustom org-default-priority ?B
  2388. "The default priority of TODO items.
  2389. This is the priority an item gets if no explicit priority is given.
  2390. When starting to cycle on an empty priority the first step in the cycle
  2391. depends on `org-priority-start-cycle-with-default'. The resulting first
  2392. step priority must not exceed the range from `org-highest-priority' to
  2393. `org-lowest-priority' which means that `org-default-priority' has to be
  2394. in this range exclusive or inclusive the range boundaries. Else the
  2395. first step refuses to set the default and the second will fall back
  2396. to (depending on the command used) the highest or lowest priority."
  2397. :group 'org-priorities
  2398. :type 'character)
  2399. (defcustom org-priority-start-cycle-with-default t
  2400. "Non-nil means start with default priority when starting to cycle.
  2401. When this is nil, the first step in the cycle will be (depending on the
  2402. command used) one higher or lower than the default priority.
  2403. See also `org-default-priority'."
  2404. :group 'org-priorities
  2405. :type 'boolean)
  2406. (defcustom org-get-priority-function nil
  2407. "Function to extract the priority from a string.
  2408. The string is normally the headline. If this is nil Org computes the
  2409. priority from the priority cookie like [#A] in the headline. It returns
  2410. an integer, increasing by 1000 for each priority level.
  2411. The user can set a different function here, which should take a string
  2412. as an argument and return the numeric priority."
  2413. :group 'org-priorities
  2414. :version "24.1"
  2415. :type 'function)
  2416. (defgroup org-time nil
  2417. "Options concerning time stamps and deadlines in Org-mode."
  2418. :tag "Org Time"
  2419. :group 'org)
  2420. (defcustom org-insert-labeled-timestamps-at-point nil
  2421. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2422. When nil, these labeled time stamps are forces into the second line of an
  2423. entry, just after the headline. When scheduling from the global TODO list,
  2424. the time stamp will always be forced into the second line."
  2425. :group 'org-time
  2426. :type 'boolean)
  2427. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2428. "Formats for `format-time-string' which are used for time stamps.
  2429. It is not recommended to change this constant.")
  2430. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2431. "Number of minutes to round time stamps to.
  2432. These are two values, the first applies when first creating a time stamp.
  2433. The second applies when changing it with the commands `S-up' and `S-down'.
  2434. When changing the time stamp, this means that it will change in steps
  2435. of N minutes, as given by the second value.
  2436. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2437. numbers should be factors of 60, so for example 5, 10, 15.
  2438. When this is larger than 1, you can still force an exact time stamp by using
  2439. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2440. and by using a prefix arg to `S-up/down' to specify the exact number
  2441. of minutes to shift."
  2442. :group 'org-time
  2443. :get #'(lambda (var) ; Make sure both elements are there
  2444. (if (integerp (default-value var))
  2445. (list (default-value var) 5)
  2446. (default-value var)))
  2447. :type '(list
  2448. (integer :tag "when inserting times")
  2449. (integer :tag "when modifying times")))
  2450. ;; Normalize old customizations of this variable.
  2451. (when (integerp org-time-stamp-rounding-minutes)
  2452. (setq org-time-stamp-rounding-minutes
  2453. (list org-time-stamp-rounding-minutes
  2454. org-time-stamp-rounding-minutes)))
  2455. (defcustom org-display-custom-times nil
  2456. "Non-nil means overlay custom formats over all time stamps.
  2457. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2458. To turn this on on a per-file basis, insert anywhere in the file:
  2459. #+STARTUP: customtime"
  2460. :group 'org-time
  2461. :set 'set-default
  2462. :type 'sexp)
  2463. (make-variable-buffer-local 'org-display-custom-times)
  2464. (defcustom org-time-stamp-custom-formats
  2465. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2466. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2467. These are overlaid over the default ISO format if the variable
  2468. `org-display-custom-times' is set. Time like %H:%M should be at the
  2469. end of the second format. The custom formats are also honored by export
  2470. commands, if custom time display is turned on at the time of export."
  2471. :group 'org-time
  2472. :type 'sexp)
  2473. (defun org-time-stamp-format (&optional long inactive)
  2474. "Get the right format for a time string."
  2475. (let ((f (if long (cdr org-time-stamp-formats)
  2476. (car org-time-stamp-formats))))
  2477. (if inactive
  2478. (concat "[" (substring f 1 -1) "]")
  2479. f)))
  2480. (defcustom org-time-clocksum-format
  2481. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2482. "The format string used when creating CLOCKSUM lines.
  2483. This is also used when Org mode generates a time duration.
  2484. The value can be a single format string containing two
  2485. %-sequences, which will be filled with the number of hours and
  2486. minutes in that order.
  2487. Alternatively, the value can be a plist associating any of the
  2488. keys :years, :months, :weeks, :days, :hours or :minutes with
  2489. format strings. The time duration is formatted using only the
  2490. time components that are needed and concatenating the results.
  2491. If a time unit in absent, it falls back to the next smallest
  2492. unit.
  2493. The keys :require-years, :require-months, :require-days,
  2494. :require-weeks, :require-hours, :require-minutes are also
  2495. meaningful. A non-nil value for these keys indicates that the
  2496. corresponding time component should always be included, even if
  2497. its value is 0.
  2498. For example,
  2499. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2500. :require-minutes t)
  2501. means durations longer than a day will be expressed in days,
  2502. hours and minutes, and durations less than a day will always be
  2503. expressed in hours and minutes (even for durations less than an
  2504. hour).
  2505. The value
  2506. \(:days \"%dd\" :minutes \"%dm\")
  2507. means durations longer than a day will be expressed in days and
  2508. minutes, and durations less than a day will be expressed entirely
  2509. in minutes (even for durations longer than an hour)."
  2510. :group 'org-time
  2511. :group 'org-clock
  2512. :version "24.3"
  2513. :type '(choice (string :tag "Format string")
  2514. (set :tag "Plist"
  2515. (group :inline t (const :tag "Years" :years)
  2516. (string :tag "Format string"))
  2517. (group :inline t
  2518. (const :tag "Always show years" :require-years)
  2519. (const t))
  2520. (group :inline t (const :tag "Months" :months)
  2521. (string :tag "Format string"))
  2522. (group :inline t
  2523. (const :tag "Always show months" :require-months)
  2524. (const t))
  2525. (group :inline t (const :tag "Weeks" :weeks)
  2526. (string :tag "Format string"))
  2527. (group :inline t
  2528. (const :tag "Always show weeks" :require-weeks)
  2529. (const t))
  2530. (group :inline t (const :tag "Days" :days)
  2531. (string :tag "Format string"))
  2532. (group :inline t
  2533. (const :tag "Always show days" :require-days)
  2534. (const t))
  2535. (group :inline t (const :tag "Hours" :hours)
  2536. (string :tag "Format string"))
  2537. (group :inline t
  2538. (const :tag "Always show hours" :require-hours)
  2539. (const t))
  2540. (group :inline t (const :tag "Minutes" :minutes)
  2541. (string :tag "Format string"))
  2542. (group :inline t
  2543. (const :tag "Always show minutes" :require-minutes)
  2544. (const t)))))
  2545. (defcustom org-time-clocksum-use-fractional nil
  2546. "When non-nil, \\[org-clock-display] uses fractional times.
  2547. See `org-time-clocksum-format' for more on time clock formats."
  2548. :group 'org-time
  2549. :group 'org-clock
  2550. :version "24.3"
  2551. :type 'boolean)
  2552. (defcustom org-time-clocksum-use-effort-durations t
  2553. "When non-nil, \\[org-clock-display] uses effort durations.
  2554. E.g. by default, one day is considered to be a 8 hours effort,
  2555. so a task that has been clocked for 16 hours will be displayed
  2556. as during 2 days in the clock display or in the clocktable.
  2557. See `org-effort-durations' on how to set effort durations
  2558. and `org-time-clocksum-format' for more on time clock formats."
  2559. :group 'org-time
  2560. :group 'org-clock
  2561. :version "24.3"
  2562. :type 'boolean)
  2563. (defcustom org-time-clocksum-fractional-format "%.2f"
  2564. "The format string used when creating CLOCKSUM lines,
  2565. or when Org mode generates a time duration, if
  2566. `org-time-clocksum-use-fractional' is enabled.
  2567. The value can be a single format string containing one
  2568. %-sequence, which will be filled with the number of hours as
  2569. a float.
  2570. Alternatively, the value can be a plist associating any of the
  2571. keys :years, :months, :weeks, :days, :hours or :minutes with
  2572. a format string. The time duration is formatted using the
  2573. largest time unit which gives a non-zero integer part. If all
  2574. specified formats have zero integer part, the smallest time unit
  2575. is used."
  2576. :group 'org-time
  2577. :type '(choice (string :tag "Format string")
  2578. (set (group :inline t (const :tag "Years" :years)
  2579. (string :tag "Format string"))
  2580. (group :inline t (const :tag "Months" :months)
  2581. (string :tag "Format string"))
  2582. (group :inline t (const :tag "Weeks" :weeks)
  2583. (string :tag "Format string"))
  2584. (group :inline t (const :tag "Days" :days)
  2585. (string :tag "Format string"))
  2586. (group :inline t (const :tag "Hours" :hours)
  2587. (string :tag "Format string"))
  2588. (group :inline t (const :tag "Minutes" :minutes)
  2589. (string :tag "Format string")))))
  2590. (defcustom org-deadline-warning-days 14
  2591. "Number of days before expiration during which a deadline becomes active.
  2592. This variable governs the display in sparse trees and in the agenda.
  2593. When 0 or negative, it means use this number (the absolute value of it)
  2594. even if a deadline has a different individual lead time specified.
  2595. Custom commands can set this variable in the options section."
  2596. :group 'org-time
  2597. :group 'org-agenda-daily/weekly
  2598. :type 'integer)
  2599. (defcustom org-scheduled-delay-days 0
  2600. "Number of days before a scheduled item becomes active.
  2601. This variable governs the display in sparse trees and in the agenda.
  2602. The default value (i.e. 0) means: don't delay scheduled item.
  2603. When negative, it means use this number (the absolute value of it)
  2604. even if a scheduled item has a different individual delay time
  2605. specified.
  2606. Custom commands can set this variable in the options section."
  2607. :group 'org-time
  2608. :group 'org-agenda-daily/weekly
  2609. :version "24.3"
  2610. :type 'integer)
  2611. (defcustom org-read-date-prefer-future t
  2612. "Non-nil means assume future for incomplete date input from user.
  2613. This affects the following situations:
  2614. 1. The user gives a month but not a year.
  2615. For example, if it is April and you enter \"feb 2\", this will be read
  2616. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2617. 2. The user gives a day, but no month.
  2618. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2619. read this as the third of *next* month. However, if you enter \"17\",
  2620. it will be considered as *this* month.
  2621. If you set this variable to the symbol `time', then also the following
  2622. will work:
  2623. 3. If the user gives a time.
  2624. If the time is before now, it will be interpreted as tomorrow.
  2625. Currently none of this works for ISO week specifications.
  2626. When this option is nil, the current day, month and year will always be
  2627. used as defaults.
  2628. See also `org-agenda-jump-prefer-future'."
  2629. :group 'org-time
  2630. :type '(choice
  2631. (const :tag "Never" nil)
  2632. (const :tag "Check month and day" t)
  2633. (const :tag "Check month, day, and time" time)))
  2634. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2635. "Should the agenda jump command prefer the future for incomplete dates?
  2636. The default is to do the same as configured in `org-read-date-prefer-future'.
  2637. But you can also set a deviating value here.
  2638. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2639. :group 'org-agenda
  2640. :group 'org-time
  2641. :version "24.1"
  2642. :type '(choice
  2643. (const :tag "Use org-read-date-prefer-future"
  2644. org-read-date-prefer-future)
  2645. (const :tag "Never" nil)
  2646. (const :tag "Always" t)))
  2647. (defcustom org-read-date-force-compatible-dates t
  2648. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2649. Depending on the system Emacs is running on, certain dates cannot
  2650. be represented with the type used internally to represent time.
  2651. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2652. correctly. Some systems allow for earlier dates, some for later,
  2653. some for both. One way to find out it to insert any date into an
  2654. Org buffer, putting the cursor on the year and hitting S-up and
  2655. S-down to test the range.
  2656. When this variable is set to t, the date/time prompt will not let
  2657. you specify dates outside the 1970-2037 range, so it is certain that
  2658. these dates will work in whatever version of Emacs you are
  2659. running, and also that you can move a file from one Emacs implementation
  2660. to another. WHenever Org is forcing the year for you, it will display
  2661. a message and beep.
  2662. When this variable is nil, Org will check if the date is
  2663. representable in the specific Emacs implementation you are using.
  2664. If not, it will force a year, usually the current year, and beep
  2665. to remind you. Currently this setting is not recommended because
  2666. the likelihood that you will open your Org files in an Emacs that
  2667. has limited date range is not negligible.
  2668. A workaround for this problem is to use diary sexp dates for time
  2669. stamps outside of this range."
  2670. :group 'org-time
  2671. :version "24.1"
  2672. :type 'boolean)
  2673. (defcustom org-read-date-display-live t
  2674. "Non-nil means display current interpretation of date prompt live.
  2675. This display will be in an overlay, in the minibuffer."
  2676. :group 'org-time
  2677. :type 'boolean)
  2678. (defcustom org-read-date-popup-calendar t
  2679. "Non-nil means pop up a calendar when prompting for a date.
  2680. In the calendar, the date can be selected with mouse-1. However, the
  2681. minibuffer will also be active, and you can simply enter the date as well.
  2682. When nil, only the minibuffer will be available."
  2683. :group 'org-time
  2684. :type 'boolean)
  2685. (if (fboundp 'defvaralias)
  2686. (defvaralias 'org-popup-calendar-for-date-prompt
  2687. 'org-read-date-popup-calendar))
  2688. (make-obsolete-variable
  2689. 'org-read-date-minibuffer-setup-hook
  2690. "Set `org-read-date-minibuffer-local-map' instead." "24.3")
  2691. (defcustom org-read-date-minibuffer-setup-hook nil
  2692. "Hook to be used to set up keys for the date/time interface.
  2693. Add key definitions to `minibuffer-local-map', which will be a
  2694. temporary copy.
  2695. WARNING: This option is obsolete, you should use
  2696. `org-read-date-minibuffer-local-map' to set up keys."
  2697. :group 'org-time
  2698. :type 'hook)
  2699. (defcustom org-extend-today-until 0
  2700. "The hour when your day really ends. Must be an integer.
  2701. This has influence for the following applications:
  2702. - When switching the agenda to \"today\". It it is still earlier than
  2703. the time given here, the day recognized as TODAY is actually yesterday.
  2704. - When a date is read from the user and it is still before the time given
  2705. here, the current date and time will be assumed to be yesterday, 23:59.
  2706. Also, timestamps inserted in capture templates follow this rule.
  2707. IMPORTANT: This is a feature whose implementation is and likely will
  2708. remain incomplete. Really, it is only here because past midnight seems to
  2709. be the favorite working time of John Wiegley :-)"
  2710. :group 'org-time
  2711. :type 'integer)
  2712. (defcustom org-use-effective-time nil
  2713. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2714. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2715. \"effective time\" of any timestamps between midnight and 8am will be
  2716. 23:59 of the previous day."
  2717. :group 'org-time
  2718. :version "24.1"
  2719. :type 'boolean)
  2720. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2721. "When non-nil, use the last clock out time for `org-todo'.
  2722. Note that this option has precedence over the combined use of
  2723. `org-use-effective-time' and `org-extend-today-until'."
  2724. :group 'org-time
  2725. ;; :version "24.3"
  2726. :type 'boolean)
  2727. (defcustom org-edit-timestamp-down-means-later nil
  2728. "Non-nil means S-down will increase the time in a time stamp.
  2729. When nil, S-up will increase."
  2730. :group 'org-time
  2731. :type 'boolean)
  2732. (defcustom org-calendar-follow-timestamp-change t
  2733. "Non-nil means make the calendar window follow timestamp changes.
  2734. When a timestamp is modified and the calendar window is visible, it will be
  2735. moved to the new date."
  2736. :group 'org-time
  2737. :type 'boolean)
  2738. (defgroup org-tags nil
  2739. "Options concerning tags in Org-mode."
  2740. :tag "Org Tags"
  2741. :group 'org)
  2742. (defcustom org-tag-alist nil
  2743. "List of tags allowed in Org-mode files.
  2744. When this list is nil, Org-mode will base TAG input on what is already in the
  2745. buffer.
  2746. The value of this variable is an alist, the car of each entry must be a
  2747. keyword as a string, the cdr may be a character that is used to select
  2748. that tag through the fast-tag-selection interface.
  2749. See the manual for details."
  2750. :group 'org-tags
  2751. :type '(repeat
  2752. (choice
  2753. (cons (string :tag "Tag name")
  2754. (character :tag "Access char"))
  2755. (list :tag "Start radio group"
  2756. (const :startgroup)
  2757. (option (string :tag "Group description")))
  2758. (list :tag "End radio group"
  2759. (const :endgroup)
  2760. (option (string :tag "Group description")))
  2761. (const :tag "New line" (:newline)))))
  2762. (defcustom org-tag-persistent-alist nil
  2763. "List of tags that will always appear in all Org-mode files.
  2764. This is in addition to any in buffer settings or customizations
  2765. of `org-tag-alist'.
  2766. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2767. The value of this variable is an alist, the car of each entry must be a
  2768. keyword as a string, the cdr may be a character that is used to select
  2769. that tag through the fast-tag-selection interface.
  2770. See the manual for details.
  2771. To disable these tags on a per-file basis, insert anywhere in the file:
  2772. #+STARTUP: noptag"
  2773. :group 'org-tags
  2774. :type '(repeat
  2775. (choice
  2776. (cons (string :tag "Tag name")
  2777. (character :tag "Access char"))
  2778. (const :tag "Start radio group" (:startgroup))
  2779. (const :tag "End radio group" (:endgroup))
  2780. (const :tag "New line" (:newline)))))
  2781. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2782. "If non-nil, always offer completion for all tags of all agenda files.
  2783. Instead of customizing this variable directly, you might want to
  2784. set it locally for capture buffers, because there no list of
  2785. tags in that file can be created dynamically (there are none).
  2786. (add-hook 'org-capture-mode-hook
  2787. (lambda ()
  2788. (set (make-local-variable
  2789. 'org-complete-tags-always-offer-all-agenda-tags)
  2790. t)))"
  2791. :group 'org-tags
  2792. :version "24.1"
  2793. :type 'boolean)
  2794. (defvar org-file-tags nil
  2795. "List of tags that can be inherited by all entries in the file.
  2796. The tags will be inherited if the variable `org-use-tag-inheritance'
  2797. says they should be.
  2798. This variable is populated from #+FILETAGS lines.")
  2799. (defcustom org-use-fast-tag-selection 'auto
  2800. "Non-nil means use fast tag selection scheme.
  2801. This is a special interface to select and deselect tags with single keys.
  2802. When nil, fast selection is never used.
  2803. When the symbol `auto', fast selection is used if and only if selection
  2804. characters for tags have been configured, either through the variable
  2805. `org-tag-alist' or through a #+TAGS line in the buffer.
  2806. When t, fast selection is always used and selection keys are assigned
  2807. automatically if necessary."
  2808. :group 'org-tags
  2809. :type '(choice
  2810. (const :tag "Always" t)
  2811. (const :tag "Never" nil)
  2812. (const :tag "When selection characters are configured" 'auto)))
  2813. (defcustom org-fast-tag-selection-single-key nil
  2814. "Non-nil means fast tag selection exits after first change.
  2815. When nil, you have to press RET to exit it.
  2816. During fast tag selection, you can toggle this flag with `C-c'.
  2817. This variable can also have the value `expert'. In this case, the window
  2818. displaying the tags menu is not even shown, until you press C-c again."
  2819. :group 'org-tags
  2820. :type '(choice
  2821. (const :tag "No" nil)
  2822. (const :tag "Yes" t)
  2823. (const :tag "Expert" expert)))
  2824. (defvar org-fast-tag-selection-include-todo nil
  2825. "Non-nil means fast tags selection interface will also offer TODO states.
  2826. This is an undocumented feature, you should not rely on it.")
  2827. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2828. "The column to which tags should be indented in a headline.
  2829. If this number is positive, it specifies the column. If it is negative,
  2830. it means that the tags should be flushright to that column. For example,
  2831. -80 works well for a normal 80 character screen.
  2832. When 0, place tags directly after headline text, with only one space in
  2833. between."
  2834. :group 'org-tags
  2835. :type 'integer)
  2836. (defcustom org-auto-align-tags t
  2837. "Non-nil keeps tags aligned when modifying headlines.
  2838. Some operations (i.e. demoting) change the length of a headline and
  2839. therefore shift the tags around. With this option turned on, after
  2840. each such operation the tags are again aligned to `org-tags-column'."
  2841. :group 'org-tags
  2842. :type 'boolean)
  2843. (defcustom org-use-tag-inheritance t
  2844. "Non-nil means tags in levels apply also for sublevels.
  2845. When nil, only the tags directly given in a specific line apply there.
  2846. This may also be a list of tags that should be inherited, or a regexp that
  2847. matches tags that should be inherited. Additional control is possible
  2848. with the variable `org-tags-exclude-from-inheritance' which gives an
  2849. explicit list of tags to be excluded from inheritance, even if the value of
  2850. `org-use-tag-inheritance' would select it for inheritance.
  2851. If this option is t, a match early-on in a tree can lead to a large
  2852. number of matches in the subtree when constructing the agenda or creating
  2853. a sparse tree. If you only want to see the first match in a tree during
  2854. a search, check out the variable `org-tags-match-list-sublevels'."
  2855. :group 'org-tags
  2856. :type '(choice
  2857. (const :tag "Not" nil)
  2858. (const :tag "Always" t)
  2859. (repeat :tag "Specific tags" (string :tag "Tag"))
  2860. (regexp :tag "Tags matched by regexp")))
  2861. (defcustom org-tags-exclude-from-inheritance nil
  2862. "List of tags that should never be inherited.
  2863. This is a way to exclude a few tags from inheritance. For way to do
  2864. the opposite, to actively allow inheritance for selected tags,
  2865. see the variable `org-use-tag-inheritance'."
  2866. :group 'org-tags
  2867. :type '(repeat (string :tag "Tag")))
  2868. (defun org-tag-inherit-p (tag)
  2869. "Check if TAG is one that should be inherited."
  2870. (cond
  2871. ((member tag org-tags-exclude-from-inheritance) nil)
  2872. ((eq org-use-tag-inheritance t) t)
  2873. ((not org-use-tag-inheritance) nil)
  2874. ((stringp org-use-tag-inheritance)
  2875. (string-match org-use-tag-inheritance tag))
  2876. ((listp org-use-tag-inheritance)
  2877. (member tag org-use-tag-inheritance))
  2878. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2879. (defcustom org-tags-match-list-sublevels t
  2880. "Non-nil means list also sublevels of headlines matching a search.
  2881. This variable applies to tags/property searches, and also to stuck
  2882. projects because this search is based on a tags match as well.
  2883. When set to the symbol `indented', sublevels are indented with
  2884. leading dots.
  2885. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2886. the sublevels of a headline matching a tag search often also match
  2887. the same search. Listing all of them can create very long lists.
  2888. Setting this variable to nil causes subtrees of a match to be skipped.
  2889. This variable is semi-obsolete and probably should always be true. It
  2890. is better to limit inheritance to certain tags using the variables
  2891. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2892. :group 'org-tags
  2893. :type '(choice
  2894. (const :tag "No, don't list them" nil)
  2895. (const :tag "Yes, do list them" t)
  2896. (const :tag "List them, indented with leading dots" indented)))
  2897. (defcustom org-tags-sort-function nil
  2898. "When set, tags are sorted using this function as a comparator."
  2899. :group 'org-tags
  2900. :type '(choice
  2901. (const :tag "No sorting" nil)
  2902. (const :tag "Alphabetical" string<)
  2903. (const :tag "Reverse alphabetical" string>)
  2904. (function :tag "Custom function" nil)))
  2905. (defvar org-tags-history nil
  2906. "History of minibuffer reads for tags.")
  2907. (defvar org-last-tags-completion-table nil
  2908. "The last used completion table for tags.")
  2909. (defvar org-after-tags-change-hook nil
  2910. "Hook that is run after the tags in a line have changed.")
  2911. (defgroup org-properties nil
  2912. "Options concerning properties in Org-mode."
  2913. :tag "Org Properties"
  2914. :group 'org)
  2915. (defcustom org-property-format "%-10s %s"
  2916. "How property key/value pairs should be formatted by `indent-line'.
  2917. When `indent-line' hits a property definition, it will format the line
  2918. according to this format, mainly to make sure that the values are
  2919. lined-up with respect to each other."
  2920. :group 'org-properties
  2921. :type 'string)
  2922. (defcustom org-properties-postprocess-alist nil
  2923. "Alist of properties and functions to adjust inserted values.
  2924. Elements of this alist must be of the form
  2925. ([string] [function])
  2926. where [string] must be a property name and [function] must be a
  2927. lambda expression: this lambda expression must take one argument,
  2928. the value to adjust, and return the new value as a string.
  2929. For example, this element will allow the property \"Remaining\"
  2930. to be updated wrt the relation between the \"Effort\" property
  2931. and the clock summary:
  2932. ((\"Remaining\" (lambda(value)
  2933. (let ((clocksum (org-clock-sum-current-item))
  2934. (effort (org-duration-string-to-minutes
  2935. (org-entry-get (point) \"Effort\"))))
  2936. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2937. :group 'org-properties
  2938. :version "24.1"
  2939. :type '(alist :key-type (string :tag "Property")
  2940. :value-type (function :tag "Function")))
  2941. (defcustom org-use-property-inheritance nil
  2942. "Non-nil means properties apply also for sublevels.
  2943. This setting is chiefly used during property searches. Turning it on can
  2944. cause significant overhead when doing a search, which is why it is not
  2945. on by default.
  2946. When nil, only the properties directly given in the current entry count.
  2947. When t, every property is inherited. The value may also be a list of
  2948. properties that should have inheritance, or a regular expression matching
  2949. properties that should be inherited.
  2950. However, note that some special properties use inheritance under special
  2951. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2952. and the properties ending in \"_ALL\" when they are used as descriptor
  2953. for valid values of a property.
  2954. Note for programmers:
  2955. When querying an entry with `org-entry-get', you can control if inheritance
  2956. should be used. By default, `org-entry-get' looks only at the local
  2957. properties. You can request inheritance by setting the inherit argument
  2958. to t (to force inheritance) or to `selective' (to respect the setting
  2959. in this variable)."
  2960. :group 'org-properties
  2961. :type '(choice
  2962. (const :tag "Not" nil)
  2963. (const :tag "Always" t)
  2964. (repeat :tag "Specific properties" (string :tag "Property"))
  2965. (regexp :tag "Properties matched by regexp")))
  2966. (defun org-property-inherit-p (property)
  2967. "Check if PROPERTY is one that should be inherited."
  2968. (cond
  2969. ((eq org-use-property-inheritance t) t)
  2970. ((not org-use-property-inheritance) nil)
  2971. ((stringp org-use-property-inheritance)
  2972. (string-match org-use-property-inheritance property))
  2973. ((listp org-use-property-inheritance)
  2974. (member property org-use-property-inheritance))
  2975. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2976. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2977. "The default column format, if no other format has been defined.
  2978. This variable can be set on the per-file basis by inserting a line
  2979. #+COLUMNS: %25ITEM ....."
  2980. :group 'org-properties
  2981. :type 'string)
  2982. (defcustom org-columns-ellipses ".."
  2983. "The ellipses to be used when a field in column view is truncated.
  2984. When this is the empty string, as many characters as possible are shown,
  2985. but then there will be no visual indication that the field has been truncated.
  2986. When this is a string of length N, the last N characters of a truncated
  2987. field are replaced by this string. If the column is narrower than the
  2988. ellipses string, only part of the ellipses string will be shown."
  2989. :group 'org-properties
  2990. :type 'string)
  2991. (defcustom org-columns-modify-value-for-display-function nil
  2992. "Function that modifies values for display in column view.
  2993. For example, it can be used to cut out a certain part from a time stamp.
  2994. The function must take 2 arguments:
  2995. column-title The title of the column (*not* the property name)
  2996. value The value that should be modified.
  2997. The function should return the value that should be displayed,
  2998. or nil if the normal value should be used."
  2999. :group 'org-properties
  3000. :type 'function)
  3001. (defcustom org-effort-property "Effort"
  3002. "The property that is being used to keep track of effort estimates.
  3003. Effort estimates given in this property need to have the format H:MM."
  3004. :group 'org-properties
  3005. :group 'org-progress
  3006. :type '(string :tag "Property"))
  3007. (defconst org-global-properties-fixed
  3008. '(("VISIBILITY_ALL" . "folded children content all")
  3009. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3010. "List of property/value pairs that can be inherited by any entry.
  3011. These are fixed values, for the preset properties. The user variable
  3012. that can be used to add to this list is `org-global-properties'.
  3013. The entries in this list are cons cells where the car is a property
  3014. name and cdr is a string with the value. If the value represents
  3015. multiple items like an \"_ALL\" property, separate the items by
  3016. spaces.")
  3017. (defcustom org-global-properties nil
  3018. "List of property/value pairs that can be inherited by any entry.
  3019. This list will be combined with the constant `org-global-properties-fixed'.
  3020. The entries in this list are cons cells where the car is a property
  3021. name and cdr is a string with the value.
  3022. You can set buffer-local values for the same purpose in the variable
  3023. `org-file-properties' this by adding lines like
  3024. #+PROPERTY: NAME VALUE"
  3025. :group 'org-properties
  3026. :type '(repeat
  3027. (cons (string :tag "Property")
  3028. (string :tag "Value"))))
  3029. (defvar org-file-properties nil
  3030. "List of property/value pairs that can be inherited by any entry.
  3031. Valid for the current buffer.
  3032. This variable is populated from #+PROPERTY lines.")
  3033. (make-variable-buffer-local 'org-file-properties)
  3034. (defgroup org-agenda nil
  3035. "Options concerning agenda views in Org-mode."
  3036. :tag "Org Agenda"
  3037. :group 'org)
  3038. (defvar org-category nil
  3039. "Variable used by org files to set a category for agenda display.
  3040. Such files should use a file variable to set it, for example
  3041. # -*- mode: org; org-category: \"ELisp\"
  3042. or contain a special line
  3043. #+CATEGORY: ELisp
  3044. If the file does not specify a category, then file's base name
  3045. is used instead.")
  3046. (make-variable-buffer-local 'org-category)
  3047. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3048. (defcustom org-agenda-files nil
  3049. "The files to be used for agenda display.
  3050. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3051. \\[org-remove-file]. You can also use customize to edit the list.
  3052. If an entry is a directory, all files in that directory that are matched by
  3053. `org-agenda-file-regexp' will be part of the file list.
  3054. If the value of the variable is not a list but a single file name, then
  3055. the list of agenda files is actually stored and maintained in that file, one
  3056. agenda file per line. In this file paths can be given relative to
  3057. `org-directory'. Tilde expansion and environment variable substitution
  3058. are also made."
  3059. :group 'org-agenda
  3060. :type '(choice
  3061. (repeat :tag "List of files and directories" file)
  3062. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3063. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3064. "Regular expression to match files for `org-agenda-files'.
  3065. If any element in the list in that variable contains a directory instead
  3066. of a normal file, all files in that directory that are matched by this
  3067. regular expression will be included."
  3068. :group 'org-agenda
  3069. :type 'regexp)
  3070. (defcustom org-agenda-text-search-extra-files nil
  3071. "List of extra files to be searched by text search commands.
  3072. These files will be search in addition to the agenda files by the
  3073. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3074. Note that these files will only be searched for text search commands,
  3075. not for the other agenda views like todo lists, tag searches or the weekly
  3076. agenda. This variable is intended to list notes and possibly archive files
  3077. that should also be searched by these two commands.
  3078. In fact, if the first element in the list is the symbol `agenda-archives',
  3079. than all archive files of all agenda files will be added to the search
  3080. scope."
  3081. :group 'org-agenda
  3082. :type '(set :greedy t
  3083. (const :tag "Agenda Archives" agenda-archives)
  3084. (repeat :inline t (file))))
  3085. (if (fboundp 'defvaralias)
  3086. (defvaralias 'org-agenda-multi-occur-extra-files
  3087. 'org-agenda-text-search-extra-files))
  3088. (defcustom org-agenda-skip-unavailable-files nil
  3089. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3090. A nil value means to remove them, after a query, from the list."
  3091. :group 'org-agenda
  3092. :type 'boolean)
  3093. (defcustom org-calendar-to-agenda-key [?c]
  3094. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3095. The command `org-calendar-goto-agenda' will be bound to this key. The
  3096. default is the character `c' because then `c' can be used to switch back and
  3097. forth between agenda and calendar."
  3098. :group 'org-agenda
  3099. :type 'sexp)
  3100. (defcustom org-calendar-insert-diary-entry-key [?i]
  3101. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3102. This option is irrelevant until `org-agenda-diary-file' has been configured
  3103. to point to an Org-mode file. When that is the case, the command
  3104. `org-agenda-diary-entry' will be bound to the key given here, by default
  3105. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3106. if you want to continue doing this, you need to change this to a different
  3107. key."
  3108. :group 'org-agenda
  3109. :type 'sexp)
  3110. (defcustom org-agenda-diary-file 'diary-file
  3111. "File to which to add new entries with the `i' key in agenda and calendar.
  3112. When this is the symbol `diary-file', the functionality in the Emacs
  3113. calendar will be used to add entries to the `diary-file'. But when this
  3114. points to a file, `org-agenda-diary-entry' will be used instead."
  3115. :group 'org-agenda
  3116. :type '(choice
  3117. (const :tag "The standard Emacs diary file" diary-file)
  3118. (file :tag "Special Org file diary entries")))
  3119. (eval-after-load "calendar"
  3120. '(progn
  3121. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3122. 'org-calendar-goto-agenda)
  3123. (add-hook 'calendar-mode-hook
  3124. (lambda ()
  3125. (unless (eq org-agenda-diary-file 'diary-file)
  3126. (define-key calendar-mode-map
  3127. org-calendar-insert-diary-entry-key
  3128. 'org-agenda-diary-entry))))))
  3129. (defgroup org-latex nil
  3130. "Options for embedding LaTeX code into Org-mode."
  3131. :tag "Org LaTeX"
  3132. :group 'org)
  3133. (defcustom org-format-latex-options
  3134. '(:foreground default :background default :scale 1.0
  3135. :html-foreground "Black" :html-background "Transparent"
  3136. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3137. "Options for creating images from LaTeX fragments.
  3138. This is a property list with the following properties:
  3139. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3140. `default' means use the foreground of the default face.
  3141. `auto' means use the foreground from the text face.
  3142. :background the background color, or \"Transparent\".
  3143. `default' means use the background of the default face.
  3144. `auto' means use the background from the text face.
  3145. :scale a scaling factor for the size of the images, to get more pixels
  3146. :html-foreground, :html-background, :html-scale
  3147. the same numbers for HTML export.
  3148. :matchers a list indicating which matchers should be used to
  3149. find LaTeX fragments. Valid members of this list are:
  3150. \"begin\" find environments
  3151. \"$1\" find single characters surrounded by $.$
  3152. \"$\" find math expressions surrounded by $...$
  3153. \"$$\" find math expressions surrounded by $$....$$
  3154. \"\\(\" find math expressions surrounded by \\(...\\)
  3155. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3156. :group 'org-latex
  3157. :type 'plist)
  3158. (defcustom org-format-latex-signal-error t
  3159. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3160. When nil, just push out a message."
  3161. :group 'org-latex
  3162. :version "24.1"
  3163. :type 'boolean)
  3164. (defcustom org-latex-to-mathml-jar-file nil
  3165. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3166. Use this to specify additional executable file say a jar file.
  3167. When using MathToWeb as the converter, specify the full-path to
  3168. your mathtoweb.jar file."
  3169. :group 'org-latex
  3170. :version "24.1"
  3171. :type '(choice
  3172. (const :tag "None" nil)
  3173. (file :tag "JAR file" :must-match t)))
  3174. (defcustom org-latex-to-mathml-convert-command nil
  3175. "Command to convert LaTeX fragments to MathML.
  3176. Replace format-specifiers in the command as noted below and use
  3177. `shell-command' to convert LaTeX to MathML.
  3178. %j: Executable file in fully expanded form as specified by
  3179. `org-latex-to-mathml-jar-file'.
  3180. %I: Input LaTeX file in fully expanded form
  3181. %o: Output MathML file
  3182. This command is used by `org-create-math-formula'.
  3183. When using MathToWeb as the converter, set this to
  3184. \"java -jar %j -unicode -force -df %o %I\"."
  3185. :group 'org-latex
  3186. :version "24.1"
  3187. :type '(choice
  3188. (const :tag "None" nil)
  3189. (string :tag "\nShell command")))
  3190. (defcustom org-latex-create-formula-image-program 'dvipng
  3191. "Program to convert LaTeX fragments with.
  3192. dvipng Process the LaTeX fragments to dvi file, then convert
  3193. dvi files to png files using dvipng.
  3194. This will also include processing of non-math environments.
  3195. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3196. to convert pdf files to png files"
  3197. :group 'org-latex
  3198. :version "24.1"
  3199. :type '(choice
  3200. (const :tag "dvipng" dvipng)
  3201. (const :tag "imagemagick" imagemagick)))
  3202. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3203. "Path to store latex preview images.
  3204. A relative path here creates many directories relative to the
  3205. processed org files paths. An absolute path puts all preview
  3206. images at the same place."
  3207. :group 'org-latex
  3208. :version "24.3"
  3209. :type 'string)
  3210. (defun org-format-latex-mathml-available-p ()
  3211. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3212. (save-match-data
  3213. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3214. org-latex-to-mathml-convert-command)
  3215. (let ((executable (car (split-string
  3216. org-latex-to-mathml-convert-command))))
  3217. (when (executable-find executable)
  3218. (if (string-match
  3219. "%j" org-latex-to-mathml-convert-command)
  3220. (file-readable-p org-latex-to-mathml-jar-file)
  3221. t))))))
  3222. (defcustom org-format-latex-header "\\documentclass{article}
  3223. \\usepackage[usenames]{color}
  3224. \\usepackage{amsmath}
  3225. \\usepackage[mathscr]{eucal}
  3226. \\pagestyle{empty} % do not remove
  3227. \[PACKAGES]
  3228. \[DEFAULT-PACKAGES]
  3229. % The settings below are copied from fullpage.sty
  3230. \\setlength{\\textwidth}{\\paperwidth}
  3231. \\addtolength{\\textwidth}{-3cm}
  3232. \\setlength{\\oddsidemargin}{1.5cm}
  3233. \\addtolength{\\oddsidemargin}{-2.54cm}
  3234. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3235. \\setlength{\\textheight}{\\paperheight}
  3236. \\addtolength{\\textheight}{-\\headheight}
  3237. \\addtolength{\\textheight}{-\\headsep}
  3238. \\addtolength{\\textheight}{-\\footskip}
  3239. \\addtolength{\\textheight}{-3cm}
  3240. \\setlength{\\topmargin}{1.5cm}
  3241. \\addtolength{\\topmargin}{-2.54cm}"
  3242. "The document header used for processing LaTeX fragments.
  3243. It is imperative that this header make sure that no page number
  3244. appears on the page. The package defined in the variables
  3245. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3246. will either replace the placeholder \"[PACKAGES]\" in this
  3247. header, or they will be appended."
  3248. :group 'org-latex
  3249. :type 'string)
  3250. (defun org-set-packages-alist (var val)
  3251. "Set the packages alist and make sure it has 3 elements per entry."
  3252. (set var (mapcar (lambda (x)
  3253. (if (and (consp x) (= (length x) 2))
  3254. (list (car x) (nth 1 x) t)
  3255. x))
  3256. val)))
  3257. (defun org-get-packages-alist (var)
  3258. "Get the packages alist and make sure it has 3 elements per entry."
  3259. (mapcar (lambda (x)
  3260. (if (and (consp x) (= (length x) 2))
  3261. (list (car x) (nth 1 x) t)
  3262. x))
  3263. (default-value var)))
  3264. (defcustom org-latex-default-packages-alist
  3265. '(("AUTO" "inputenc" t)
  3266. ("T1" "fontenc" t)
  3267. ("" "fixltx2e" nil)
  3268. ("" "graphicx" t)
  3269. ("" "longtable" nil)
  3270. ("" "float" nil)
  3271. ("" "wrapfig" nil)
  3272. ("" "soul" t)
  3273. ("" "textcomp" t)
  3274. ("" "marvosym" t)
  3275. ("" "wasysym" t)
  3276. ("" "latexsym" t)
  3277. ("" "amssymb" t)
  3278. ("" "hyperref" nil)
  3279. "\\tolerance=1000")
  3280. "Alist of default packages to be inserted in the header.
  3281. Change this only if one of the packages here causes an
  3282. incompatibility with another package you are using.
  3283. The packages in this list are needed by one part or another of
  3284. Org mode to function properly:
  3285. - inputenc, fontenc: for basic font and character selection
  3286. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3287. symbols used for interpreting the entities in `org-entities'.
  3288. You can skip some of these packages if you don't use any of the
  3289. symbols in it.
  3290. - graphicx: for including images
  3291. - float, wrapfig: for figure placement
  3292. - longtable: for long tables
  3293. - hyperref: for cross references
  3294. Therefore you should not modify this variable unless you know
  3295. what you are doing. The one reason to change it anyway is that
  3296. you might be loading some other package that conflicts with one
  3297. of the default packages. Each cell is of the format
  3298. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3299. the package also needs to be included when compiling LaTeX
  3300. snippets into images for inclusion into non-LaTeX output."
  3301. :group 'org-latex
  3302. :group 'org-export-latex
  3303. :set 'org-set-packages-alist
  3304. :get 'org-get-packages-alist
  3305. :version "24.1"
  3306. :type '(repeat
  3307. (choice
  3308. (list :tag "options/package pair"
  3309. (string :tag "options")
  3310. (string :tag "package")
  3311. (boolean :tag "Snippet"))
  3312. (string :tag "A line of LaTeX"))))
  3313. (defcustom org-latex-packages-alist nil
  3314. "Alist of packages to be inserted in every LaTeX header.
  3315. These will be inserted after `org-latex-default-packages-alist'.
  3316. Each cell is of the format:
  3317. \(\"options\" \"package\" snippet-flag)
  3318. SNIPPET-FLAG, when t, indicates that this package is also needed
  3319. when turning LaTeX snippets into images for inclusion into
  3320. non-LaTeX output.
  3321. Make sure that you only list packages here which:
  3322. - you want in every file
  3323. - do not conflict with the setup in `org-format-latex-header'.
  3324. - do not conflict with the default packages in
  3325. `org-latex-default-packages-alist'."
  3326. :group 'org-latex
  3327. :group 'org-export-latex
  3328. :set 'org-set-packages-alist
  3329. :get 'org-get-packages-alist
  3330. :type '(repeat
  3331. (choice
  3332. (list :tag "options/package pair"
  3333. (string :tag "options")
  3334. (string :tag "package")
  3335. (boolean :tag "Snippet"))
  3336. (string :tag "A line of LaTeX"))))
  3337. (defgroup org-appearance nil
  3338. "Settings for Org-mode appearance."
  3339. :tag "Org Appearance"
  3340. :group 'org)
  3341. (defcustom org-level-color-stars-only nil
  3342. "Non-nil means fontify only the stars in each headline.
  3343. When nil, the entire headline is fontified.
  3344. Changing it requires restart of `font-lock-mode' to become effective
  3345. also in regions already fontified."
  3346. :group 'org-appearance
  3347. :type 'boolean)
  3348. (defcustom org-hide-leading-stars nil
  3349. "Non-nil means hide the first N-1 stars in a headline.
  3350. This works by using the face `org-hide' for these stars. This
  3351. face is white for a light background, and black for a dark
  3352. background. You may have to customize the face `org-hide' to
  3353. make this work.
  3354. Changing it requires restart of `font-lock-mode' to become effective
  3355. also in regions already fontified.
  3356. You may also set this on a per-file basis by adding one of the following
  3357. lines to the buffer:
  3358. #+STARTUP: hidestars
  3359. #+STARTUP: showstars"
  3360. :group 'org-appearance
  3361. :type 'boolean)
  3362. (defcustom org-hidden-keywords nil
  3363. "List of symbols corresponding to keywords to be hidden the org buffer.
  3364. For example, a value '(title) for this list will make the document's title
  3365. appear in the buffer without the initial #+TITLE: keyword."
  3366. :group 'org-appearance
  3367. :version "24.1"
  3368. :type '(set (const :tag "#+AUTHOR" author)
  3369. (const :tag "#+DATE" date)
  3370. (const :tag "#+EMAIL" email)
  3371. (const :tag "#+TITLE" title)))
  3372. (defcustom org-custom-properties nil
  3373. "List of properties (as strings) with a special meaning.
  3374. The default use of these custom properties is to let the user
  3375. hide them with `org-toggle-custom-properties-visibility'."
  3376. :group 'org-properties
  3377. :group 'org-appearance
  3378. :version "24.3"
  3379. :type '(repeat (string :tag "Property Name")))
  3380. (defcustom org-fontify-done-headline nil
  3381. "Non-nil means change the face of a headline if it is marked DONE.
  3382. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3383. When this is non-nil, the headline after the keyword is set to the
  3384. `org-headline-done' as an additional indication."
  3385. :group 'org-appearance
  3386. :type 'boolean)
  3387. (defcustom org-fontify-emphasized-text t
  3388. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3389. Changing this variable requires a restart of Emacs to take effect."
  3390. :group 'org-appearance
  3391. :type 'boolean)
  3392. (defcustom org-fontify-whole-heading-line nil
  3393. "Non-nil means fontify the whole line for headings.
  3394. This is useful when setting a background color for the
  3395. org-level-* faces."
  3396. :group 'org-appearance
  3397. :type 'boolean)
  3398. (defcustom org-hide-emphasis-markers nil
  3399. "Non-nil mean font-lock should hide the emphasis marker characters."
  3400. :group 'org-appearance
  3401. :type 'boolean)
  3402. (defcustom org-pretty-entities nil
  3403. "Non-nil means show entities as UTF8 characters.
  3404. When nil, the \\name form remains in the buffer."
  3405. :group 'org-appearance
  3406. :version "24.1"
  3407. :type 'boolean)
  3408. (defcustom org-pretty-entities-include-sub-superscripts t
  3409. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3410. :group 'org-appearance
  3411. :version "24.1"
  3412. :type 'boolean)
  3413. (defvar org-emph-re nil
  3414. "Regular expression for matching emphasis.
  3415. After a match, the match groups contain these elements:
  3416. 0 The match of the full regular expression, including the characters
  3417. before and after the proper match
  3418. 1 The character before the proper match, or empty at beginning of line
  3419. 2 The proper match, including the leading and trailing markers
  3420. 3 The leading marker like * or /, indicating the type of highlighting
  3421. 4 The text between the emphasis markers, not including the markers
  3422. 5 The character after the match, empty at the end of a line")
  3423. (defvar org-verbatim-re nil
  3424. "Regular expression for matching verbatim text.")
  3425. (defvar org-emphasis-regexp-components) ; defined just below
  3426. (defvar org-emphasis-alist) ; defined just below
  3427. (defun org-set-emph-re (var val)
  3428. "Set variable and compute the emphasis regular expression."
  3429. (set var val)
  3430. (when (and (boundp 'org-emphasis-alist)
  3431. (boundp 'org-emphasis-regexp-components)
  3432. org-emphasis-alist org-emphasis-regexp-components)
  3433. (let* ((e org-emphasis-regexp-components)
  3434. (pre (car e))
  3435. (post (nth 1 e))
  3436. (border (nth 2 e))
  3437. (body (nth 3 e))
  3438. (nl (nth 4 e))
  3439. (body1 (concat body "*?"))
  3440. (markers (mapconcat 'car org-emphasis-alist ""))
  3441. (vmarkers (mapconcat
  3442. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3443. org-emphasis-alist "")))
  3444. ;; make sure special characters appear at the right position in the class
  3445. (if (string-match "\\^" markers)
  3446. (setq markers (concat (replace-match "" t t markers) "^")))
  3447. (if (string-match "-" markers)
  3448. (setq markers (concat (replace-match "" t t markers) "-")))
  3449. (if (string-match "\\^" vmarkers)
  3450. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3451. (if (string-match "-" vmarkers)
  3452. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3453. (if (> nl 0)
  3454. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3455. (int-to-string nl) "\\}")))
  3456. ;; Make the regexp
  3457. (setq org-emph-re
  3458. (concat "\\([" pre "]\\|^\\)"
  3459. "\\("
  3460. "\\([" markers "]\\)"
  3461. "\\("
  3462. "[^" border "]\\|"
  3463. "[^" border "]"
  3464. body1
  3465. "[^" border "]"
  3466. "\\)"
  3467. "\\3\\)"
  3468. "\\([" post "]\\|$\\)"))
  3469. (setq org-verbatim-re
  3470. (concat "\\([" pre "]\\|^\\)"
  3471. "\\("
  3472. "\\([" vmarkers "]\\)"
  3473. "\\("
  3474. "[^" border "]\\|"
  3475. "[^" border "]"
  3476. body1
  3477. "[^" border "]"
  3478. "\\)"
  3479. "\\3\\)"
  3480. "\\([" post "]\\|$\\)")))))
  3481. (defcustom org-emphasis-regexp-components
  3482. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3483. "Components used to build the regular expression for emphasis.
  3484. This is a list with five entries. Terminology: In an emphasis string
  3485. like \" *strong word* \", we call the initial space PREMATCH, the final
  3486. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3487. and \"trong wor\" is the body. The different components in this variable
  3488. specify what is allowed/forbidden in each part:
  3489. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3490. post Chars allowed as postmatch. End of line will be allowed too.
  3491. border The chars *forbidden* as border characters.
  3492. body-regexp A regexp like \".\" to match a body character. Don't use
  3493. non-shy groups here, and don't allow newline here.
  3494. newline The maximum number of newlines allowed in an emphasis exp.
  3495. Use customize to modify this, or restart Emacs after changing it."
  3496. :group 'org-appearance
  3497. :set 'org-set-emph-re
  3498. :type '(list
  3499. (sexp :tag "Allowed chars in pre ")
  3500. (sexp :tag "Allowed chars in post ")
  3501. (sexp :tag "Forbidden chars in border ")
  3502. (sexp :tag "Regexp for body ")
  3503. (integer :tag "number of newlines allowed")
  3504. (option (boolean :tag "Please ignore this button"))))
  3505. (defcustom org-emphasis-alist
  3506. `(("*" bold "<b>" "</b>")
  3507. ("/" italic "<i>" "</i>")
  3508. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3509. ("=" org-code "<code>" "</code>" verbatim)
  3510. ("~" org-verbatim "<code>" "</code>" verbatim)
  3511. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3512. "<del>" "</del>")
  3513. )
  3514. "Special syntax for emphasized text.
  3515. Text starting and ending with a special character will be emphasized, for
  3516. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3517. characters, the face to be used by font-lock for highlighting in Org-mode
  3518. Emacs buffers, and the HTML tags to be used for this.
  3519. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3520. Use customize to modify this, or restart Emacs after changing it."
  3521. :group 'org-appearance
  3522. :set 'org-set-emph-re
  3523. :type '(repeat
  3524. (list
  3525. (string :tag "Marker character")
  3526. (choice
  3527. (face :tag "Font-lock-face")
  3528. (plist :tag "Face property list"))
  3529. (string :tag "HTML start tag")
  3530. (string :tag "HTML end tag")
  3531. (option (const verbatim)))))
  3532. (defvar org-protecting-blocks
  3533. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3534. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3535. This is needed for font-lock setup.")
  3536. ;;; Miscellaneous options
  3537. (defgroup org-completion nil
  3538. "Completion in Org-mode."
  3539. :tag "Org Completion"
  3540. :group 'org)
  3541. (defcustom org-completion-use-ido nil
  3542. "Non-nil means use ido completion wherever possible.
  3543. Note that `ido-mode' must be active for this variable to be relevant.
  3544. If you decide to turn this variable on, you might well want to turn off
  3545. `org-outline-path-complete-in-steps'.
  3546. See also `org-completion-use-iswitchb'."
  3547. :group 'org-completion
  3548. :type 'boolean)
  3549. (defcustom org-completion-use-iswitchb nil
  3550. "Non-nil means use iswitchb completion wherever possible.
  3551. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3552. If you decide to turn this variable on, you might well want to turn off
  3553. `org-outline-path-complete-in-steps'.
  3554. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3555. :group 'org-completion
  3556. :type 'boolean)
  3557. (defcustom org-completion-fallback-command 'hippie-expand
  3558. "The expansion command called by \\[pcomplete] in normal context.
  3559. Normal means, no org-mode-specific context."
  3560. :group 'org-completion
  3561. :type 'function)
  3562. ;;; Functions and variables from their packages
  3563. ;; Declared here to avoid compiler warnings
  3564. ;; XEmacs only
  3565. (defvar outline-mode-menu-heading)
  3566. (defvar outline-mode-menu-show)
  3567. (defvar outline-mode-menu-hide)
  3568. (defvar zmacs-regions) ; XEmacs regions
  3569. ;; Emacs only
  3570. (defvar mark-active)
  3571. ;; Various packages
  3572. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3573. (declare-function calendar-forward-day "cal-move" (arg))
  3574. (declare-function calendar-goto-date "cal-move" (date))
  3575. (declare-function calendar-goto-today "cal-move" ())
  3576. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3577. (defvar calc-embedded-close-formula)
  3578. (defvar calc-embedded-open-formula)
  3579. (declare-function cdlatex-tab "ext:cdlatex" ())
  3580. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3581. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3582. (defvar font-lock-unfontify-region-function)
  3583. (declare-function iswitchb-read-buffer "iswitchb"
  3584. (prompt &optional default require-match start matches-set))
  3585. (defvar iswitchb-temp-buflist)
  3586. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3587. (defvar org-agenda-tags-todo-honor-ignore-options)
  3588. (declare-function org-agenda-skip "org-agenda" ())
  3589. (declare-function
  3590. org-agenda-format-item "org-agenda"
  3591. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3592. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3593. (declare-function org-agenda-change-all-lines "org-agenda"
  3594. (newhead hdmarker &optional fixface just-this))
  3595. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3596. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3597. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3598. (beg end))
  3599. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3600. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3601. "org-agenda" (&optional end))
  3602. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3603. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3604. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3605. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3606. (declare-function org-indent-mode "org-indent" (&optional arg))
  3607. (declare-function parse-time-string "parse-time" (string))
  3608. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3609. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3610. (defvar remember-data-file)
  3611. (defvar texmathp-why)
  3612. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3613. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3614. (defvar org-latex-regexps)
  3615. ;;; Autoload and prepare some org modules
  3616. ;; Some table stuff that needs to be defined here, because it is used
  3617. ;; by the functions setting up org-mode or checking for table context.
  3618. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3619. "Detect an org-type or table-type table.")
  3620. (defconst org-table-line-regexp "^[ \t]*|"
  3621. "Detect an org-type table line.")
  3622. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3623. "Detect an org-type table line.")
  3624. (defconst org-table-hline-regexp "^[ \t]*|-"
  3625. "Detect an org-type table hline.")
  3626. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3627. "Detect a table-type table hline.")
  3628. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3629. "Detect the first line outside a table when searching from within it.
  3630. This works for both table types.")
  3631. ;; Autoload the functions in org-table.el that are needed by functions here.
  3632. (eval-and-compile
  3633. (org-autoload "org-table"
  3634. '(org-table-begin org-table-blank-field org-table-end)))
  3635. ;;;###autoload
  3636. (defun turn-on-orgtbl ()
  3637. "Unconditionally turn on `orgtbl-mode'."
  3638. (require 'org-table)
  3639. (orgtbl-mode 1))
  3640. (defun org-at-table-p (&optional table-type)
  3641. "Return t if the cursor is inside an org-type table.
  3642. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3643. (if org-enable-table-editor
  3644. (save-excursion
  3645. (beginning-of-line 1)
  3646. (looking-at (if table-type org-table-any-line-regexp
  3647. org-table-line-regexp)))
  3648. nil))
  3649. (defsubst org-table-p () (org-at-table-p))
  3650. (defun org-at-table.el-p ()
  3651. "Return t if and only if we are at a table.el table."
  3652. (and (org-at-table-p 'any)
  3653. (save-excursion
  3654. (goto-char (org-table-begin 'any))
  3655. (looking-at org-table1-hline-regexp))))
  3656. (defun org-table-recognize-table.el ()
  3657. "If there is a table.el table nearby, recognize it and move into it."
  3658. (if org-table-tab-recognizes-table.el
  3659. (if (org-at-table.el-p)
  3660. (progn
  3661. (beginning-of-line 1)
  3662. (if (looking-at org-table-dataline-regexp)
  3663. nil
  3664. (if (looking-at org-table1-hline-regexp)
  3665. (progn
  3666. (beginning-of-line 2)
  3667. (if (looking-at org-table-any-border-regexp)
  3668. (beginning-of-line -1)))))
  3669. (if (re-search-forward "|" (org-table-end t) t)
  3670. (progn
  3671. (require 'table)
  3672. (if (table--at-cell-p (point))
  3673. t
  3674. (message "recognizing table.el table...")
  3675. (table-recognize-table)
  3676. (message "recognizing table.el table...done")))
  3677. (error "This should not happen"))
  3678. t)
  3679. nil)
  3680. nil))
  3681. (defun org-at-table-hline-p ()
  3682. "Return t if the cursor is inside a hline in a table."
  3683. (if org-enable-table-editor
  3684. (save-excursion
  3685. (beginning-of-line 1)
  3686. (looking-at org-table-hline-regexp))
  3687. nil))
  3688. (defvar org-table-clean-did-remove-column nil)
  3689. (defun org-table-map-tables (function &optional quietly)
  3690. "Apply FUNCTION to the start of all tables in the buffer."
  3691. (save-excursion
  3692. (save-restriction
  3693. (widen)
  3694. (goto-char (point-min))
  3695. (while (re-search-forward org-table-any-line-regexp nil t)
  3696. (unless quietly
  3697. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3698. (beginning-of-line 1)
  3699. (when (and (looking-at org-table-line-regexp)
  3700. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3701. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3702. (save-excursion (funcall function))
  3703. (or (looking-at org-table-line-regexp)
  3704. (forward-char 1)))
  3705. (re-search-forward org-table-any-border-regexp nil 1))))
  3706. (unless quietly (message "Mapping tables: done")))
  3707. ;; Declare and autoload functions from ox.el and al.
  3708. (declare-function org-export-get-environment "ox"
  3709. (&optional backend subtreep ext-plist))
  3710. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3711. ;; Declare and autoload functions from org-agenda.el
  3712. (eval-and-compile
  3713. (org-autoload "org-agenda"
  3714. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3715. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3716. (declare-function org-clock-update-mode-line "org-clock" ())
  3717. (declare-function org-resolve-clocks "org-clock"
  3718. (&optional also-non-dangling-p prompt last-valid))
  3719. (defvar org-clock-start-time)
  3720. (defvar org-clock-marker (make-marker)
  3721. "Marker recording the last clock-in.")
  3722. (defvar org-clock-hd-marker (make-marker)
  3723. "Marker recording the last clock-in, but the headline position.")
  3724. (defvar org-clock-heading ""
  3725. "The heading of the current clock entry.")
  3726. (defun org-clock-is-active ()
  3727. "Return non-nil if clock is currently running.
  3728. The return value is actually the clock marker."
  3729. (marker-buffer org-clock-marker))
  3730. (eval-and-compile
  3731. (org-autoload "org-clock" '(org-clock-remove-overlays
  3732. org-clock-update-time-maybe
  3733. org-clocktable-shift)))
  3734. (defun org-check-running-clock ()
  3735. "Check if the current buffer contains the running clock.
  3736. If yes, offer to stop it and to save the buffer with the changes."
  3737. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3738. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3739. (buffer-name))))
  3740. (org-clock-out)
  3741. (when (y-or-n-p "Save changed buffer?")
  3742. (save-buffer))))
  3743. (defun org-clocktable-try-shift (dir n)
  3744. "Check if this line starts a clock table, if yes, shift the time block."
  3745. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3746. (org-clocktable-shift dir n)))
  3747. ;;;###autoload
  3748. (defun org-clock-persistence-insinuate ()
  3749. "Set up hooks for clock persistence."
  3750. (require 'org-clock)
  3751. (add-hook 'org-mode-hook 'org-clock-load)
  3752. (add-hook 'kill-emacs-hook 'org-clock-save))
  3753. ;; Define the variable already here, to make sure we have it.
  3754. (defvar org-indent-mode nil
  3755. "Non-nil if Org-Indent mode is enabled.
  3756. Use the command `org-indent-mode' to change this variable.")
  3757. ;; Autoload archiving code
  3758. ;; The stuff that is needed for cycling and tags has to be defined here.
  3759. (defgroup org-archive nil
  3760. "Options concerning archiving in Org-mode."
  3761. :tag "Org Archive"
  3762. :group 'org-structure)
  3763. (defcustom org-archive-location "%s_archive::"
  3764. "The location where subtrees should be archived.
  3765. The value of this variable is a string, consisting of two parts,
  3766. separated by a double-colon. The first part is a filename and
  3767. the second part is a headline.
  3768. When the filename is omitted, archiving happens in the same file.
  3769. %s in the filename will be replaced by the current file
  3770. name (without the directory part). Archiving to a different file
  3771. is useful to keep archived entries from contributing to the
  3772. Org-mode Agenda.
  3773. The archived entries will be filed as subtrees of the specified
  3774. headline. When the headline is omitted, the subtrees are simply
  3775. filed away at the end of the file, as top-level entries. Also in
  3776. the heading you can use %s to represent the file name, this can be
  3777. useful when using the same archive for a number of different files.
  3778. Here are a few examples:
  3779. \"%s_archive::\"
  3780. If the current file is Projects.org, archive in file
  3781. Projects.org_archive, as top-level trees. This is the default.
  3782. \"::* Archived Tasks\"
  3783. Archive in the current file, under the top-level headline
  3784. \"* Archived Tasks\".
  3785. \"~/org/archive.org::\"
  3786. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3787. \"~/org/archive.org::* From %s\"
  3788. Archive in file ~/org/archive.org (absolute path), under headlines
  3789. \"From FILENAME\" where file name is the current file name.
  3790. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3791. The \"datetree/\" string is special, signifying to archive
  3792. items to the datetree. Items are placed in either the CLOSED
  3793. date of the item, or the current date if there is no CLOSED date.
  3794. The heading will be a subentry to the current date. There doesn't
  3795. need to be a heading, but there always needs to be a slash after
  3796. datetree. For example, to store archived items directly in the
  3797. datetree, use \"~/org/datetree.org::datetree/\".
  3798. \"basement::** Finished Tasks\"
  3799. Archive in file ./basement (relative path), as level 3 trees
  3800. below the level 2 heading \"** Finished Tasks\".
  3801. You may set this option on a per-file basis by adding to the buffer a
  3802. line like
  3803. #+ARCHIVE: basement::** Finished Tasks
  3804. You may also define it locally for a subtree by setting an ARCHIVE property
  3805. in the entry. If such a property is found in an entry, or anywhere up
  3806. the hierarchy, it will be used."
  3807. :group 'org-archive
  3808. :type 'string)
  3809. (defcustom org-archive-tag "ARCHIVE"
  3810. "The tag that marks a subtree as archived.
  3811. An archived subtree does not open during visibility cycling, and does
  3812. not contribute to the agenda listings.
  3813. After changing this, font-lock must be restarted in the relevant buffers to
  3814. get the proper fontification."
  3815. :group 'org-archive
  3816. :group 'org-keywords
  3817. :type 'string)
  3818. (defcustom org-agenda-skip-archived-trees t
  3819. "Non-nil means the agenda will skip any items located in archived trees.
  3820. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3821. variable is no longer recommended, you should leave it at the value t.
  3822. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3823. :group 'org-archive
  3824. :group 'org-agenda-skip
  3825. :type 'boolean)
  3826. (defcustom org-columns-skip-archived-trees t
  3827. "Non-nil means ignore archived trees when creating column view."
  3828. :group 'org-archive
  3829. :group 'org-properties
  3830. :type 'boolean)
  3831. (defcustom org-cycle-open-archived-trees nil
  3832. "Non-nil means `org-cycle' will open archived trees.
  3833. An archived tree is a tree marked with the tag ARCHIVE.
  3834. When nil, archived trees will stay folded. You can still open them with
  3835. normal outline commands like `show-all', but not with the cycling commands."
  3836. :group 'org-archive
  3837. :group 'org-cycle
  3838. :type 'boolean)
  3839. (defcustom org-sparse-tree-open-archived-trees nil
  3840. "Non-nil means sparse tree construction shows matches in archived trees.
  3841. When nil, matches in these trees are highlighted, but the trees are kept in
  3842. collapsed state."
  3843. :group 'org-archive
  3844. :group 'org-sparse-trees
  3845. :type 'boolean)
  3846. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3847. "The default date type when building a sparse tree.
  3848. When this is nil, a date is a scheduled or a deadline timestamp.
  3849. Otherwise, these types are allowed:
  3850. all: all timestamps
  3851. active: only active timestamps (<...>)
  3852. inactive: only inactive timestamps (<...)
  3853. scheduled: only scheduled timestamps
  3854. deadline: only deadline timestamps"
  3855. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3856. (const :tag "All timestamps" all)
  3857. (const :tag "Only active timestamps" active)
  3858. (const :tag "Only inactive timestamps" inactive)
  3859. (const :tag "Only scheduled timestamps" scheduled)
  3860. (const :tag "Only deadline timestamps" deadline))
  3861. :version "24.3"
  3862. :group 'org-sparse-trees)
  3863. (defun org-cycle-hide-archived-subtrees (state)
  3864. "Re-hide all archived subtrees after a visibility state change."
  3865. (when (and (not org-cycle-open-archived-trees)
  3866. (not (memq state '(overview folded))))
  3867. (save-excursion
  3868. (let* ((globalp (memq state '(contents all)))
  3869. (beg (if globalp (point-min) (point)))
  3870. (end (if globalp (point-max) (org-end-of-subtree t))))
  3871. (org-hide-archived-subtrees beg end)
  3872. (goto-char beg)
  3873. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3874. (message "%s" (substitute-command-keys
  3875. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3876. (defun org-force-cycle-archived ()
  3877. "Cycle subtree even if it is archived."
  3878. (interactive)
  3879. (setq this-command 'org-cycle)
  3880. (let ((org-cycle-open-archived-trees t))
  3881. (call-interactively 'org-cycle)))
  3882. (defun org-hide-archived-subtrees (beg end)
  3883. "Re-hide all archived subtrees after a visibility state change."
  3884. (save-excursion
  3885. (let* ((re (concat ":" org-archive-tag ":")))
  3886. (goto-char beg)
  3887. (while (re-search-forward re end t)
  3888. (when (org-at-heading-p)
  3889. (org-flag-subtree t)
  3890. (org-end-of-subtree t))))))
  3891. (declare-function outline-end-of-heading "outline" ())
  3892. (declare-function outline-flag-region "outline" (from to flag))
  3893. (defun org-flag-subtree (flag)
  3894. (save-excursion
  3895. (org-back-to-heading t)
  3896. (outline-end-of-heading)
  3897. (outline-flag-region (point)
  3898. (progn (org-end-of-subtree t) (point))
  3899. flag)))
  3900. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3901. (eval-and-compile
  3902. (org-autoload "org-archive"
  3903. '(org-add-archive-files)))
  3904. ;; Autoload Column View Code
  3905. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3906. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3907. (declare-function org-columns-compute "org-colview" (property))
  3908. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3909. '(org-columns-number-to-string
  3910. org-columns-get-format-and-top-level
  3911. org-columns-compute
  3912. org-columns-remove-overlays))
  3913. ;; Autoload ID code
  3914. (declare-function org-id-store-link "org-id")
  3915. (declare-function org-id-locations-load "org-id")
  3916. (declare-function org-id-locations-save "org-id")
  3917. (defvar org-id-track-globally)
  3918. (org-autoload "org-id"
  3919. '(org-id-new
  3920. org-id-copy
  3921. org-id-get-with-outline-path-completion
  3922. org-id-get-with-outline-drilling))
  3923. ;;; Variables for pre-computed regular expressions, all buffer local
  3924. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3925. "Matches first line of a hidden block.")
  3926. (make-variable-buffer-local 'org-drawer-regexp)
  3927. (defvar org-todo-regexp nil
  3928. "Matches any of the TODO state keywords.")
  3929. (make-variable-buffer-local 'org-todo-regexp)
  3930. (defvar org-not-done-regexp nil
  3931. "Matches any of the TODO state keywords except the last one.")
  3932. (make-variable-buffer-local 'org-not-done-regexp)
  3933. (defvar org-not-done-heading-regexp nil
  3934. "Matches a TODO headline that is not done.")
  3935. (make-variable-buffer-local 'org-not-done-regexp)
  3936. (defvar org-todo-line-regexp nil
  3937. "Matches a headline and puts TODO state into group 2 if present.")
  3938. (make-variable-buffer-local 'org-todo-line-regexp)
  3939. (defvar org-complex-heading-regexp nil
  3940. "Matches a headline and puts everything into groups:
  3941. group 1: the stars
  3942. group 2: The todo keyword, maybe
  3943. group 3: Priority cookie
  3944. group 4: True headline
  3945. group 5: Tags")
  3946. (make-variable-buffer-local 'org-complex-heading-regexp)
  3947. (defvar org-complex-heading-regexp-format nil
  3948. "Printf format to make regexp to match an exact headline.
  3949. This regexp will match the headline of any node which has the
  3950. exact headline text that is put into the format, but may have any
  3951. TODO state, priority and tags.")
  3952. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3953. (defvar org-todo-line-tags-regexp nil
  3954. "Matches a headline and puts TODO state into group 2 if present.
  3955. Also put tags into group 4 if tags are present.")
  3956. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3957. (defvar org-ds-keyword-length 12
  3958. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3959. (make-variable-buffer-local 'org-ds-keyword-length)
  3960. (defvar org-deadline-regexp nil
  3961. "Matches the DEADLINE keyword.")
  3962. (make-variable-buffer-local 'org-deadline-regexp)
  3963. (defvar org-deadline-time-regexp nil
  3964. "Matches the DEADLINE keyword together with a time stamp.")
  3965. (make-variable-buffer-local 'org-deadline-time-regexp)
  3966. (defvar org-deadline-line-regexp nil
  3967. "Matches the DEADLINE keyword and the rest of the line.")
  3968. (make-variable-buffer-local 'org-deadline-line-regexp)
  3969. (defvar org-scheduled-regexp nil
  3970. "Matches the SCHEDULED keyword.")
  3971. (make-variable-buffer-local 'org-scheduled-regexp)
  3972. (defvar org-scheduled-time-regexp nil
  3973. "Matches the SCHEDULED keyword together with a time stamp.")
  3974. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3975. (defvar org-closed-time-regexp nil
  3976. "Matches the CLOSED keyword together with a time stamp.")
  3977. (make-variable-buffer-local 'org-closed-time-regexp)
  3978. (defvar org-keyword-time-regexp nil
  3979. "Matches any of the 4 keywords, together with the time stamp.")
  3980. (make-variable-buffer-local 'org-keyword-time-regexp)
  3981. (defvar org-keyword-time-not-clock-regexp nil
  3982. "Matches any of the 3 keywords, together with the time stamp.")
  3983. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3984. (defvar org-maybe-keyword-time-regexp nil
  3985. "Matches a timestamp, possibly preceded by a keyword.")
  3986. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3987. (defvar org-all-time-keywords nil
  3988. "List of time keywords.")
  3989. (make-variable-buffer-local 'org-all-time-keywords)
  3990. (defconst org-plain-time-of-day-regexp
  3991. (concat
  3992. "\\(\\<[012]?[0-9]"
  3993. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3994. "\\(--?"
  3995. "\\(\\<[012]?[0-9]"
  3996. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3997. "\\)?")
  3998. "Regular expression to match a plain time or time range.
  3999. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4000. groups carry important information:
  4001. 0 the full match
  4002. 1 the first time, range or not
  4003. 8 the second time, if it is a range.")
  4004. (defconst org-plain-time-extension-regexp
  4005. (concat
  4006. "\\(\\<[012]?[0-9]"
  4007. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4008. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4009. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4010. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4011. groups carry important information:
  4012. 0 the full match
  4013. 7 hours of duration
  4014. 9 minutes of duration")
  4015. (defconst org-stamp-time-of-day-regexp
  4016. (concat
  4017. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4018. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4019. "\\(--?"
  4020. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4021. "Regular expression to match a timestamp time or time range.
  4022. After a match, the following groups carry important information:
  4023. 0 the full match
  4024. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4025. 2 the first time, range or not
  4026. 4 the second time, if it is a range.")
  4027. (defconst org-startup-options
  4028. '(("fold" org-startup-folded t)
  4029. ("overview" org-startup-folded t)
  4030. ("nofold" org-startup-folded nil)
  4031. ("showall" org-startup-folded nil)
  4032. ("showeverything" org-startup-folded showeverything)
  4033. ("content" org-startup-folded content)
  4034. ("indent" org-startup-indented t)
  4035. ("noindent" org-startup-indented nil)
  4036. ("hidestars" org-hide-leading-stars t)
  4037. ("showstars" org-hide-leading-stars nil)
  4038. ("odd" org-odd-levels-only t)
  4039. ("oddeven" org-odd-levels-only nil)
  4040. ("align" org-startup-align-all-tables t)
  4041. ("noalign" org-startup-align-all-tables nil)
  4042. ("inlineimages" org-startup-with-inline-images t)
  4043. ("noinlineimages" org-startup-with-inline-images nil)
  4044. ("latexpreview" org-startup-with-latex-preview t)
  4045. ("nolatexpreview" org-startup-with-latex-preview nil)
  4046. ("customtime" org-display-custom-times t)
  4047. ("logdone" org-log-done time)
  4048. ("lognotedone" org-log-done note)
  4049. ("nologdone" org-log-done nil)
  4050. ("lognoteclock-out" org-log-note-clock-out t)
  4051. ("nolognoteclock-out" org-log-note-clock-out nil)
  4052. ("logrepeat" org-log-repeat state)
  4053. ("lognoterepeat" org-log-repeat note)
  4054. ("logdrawer" org-log-into-drawer t)
  4055. ("nologdrawer" org-log-into-drawer nil)
  4056. ("logstatesreversed" org-log-states-order-reversed t)
  4057. ("nologstatesreversed" org-log-states-order-reversed nil)
  4058. ("nologrepeat" org-log-repeat nil)
  4059. ("logreschedule" org-log-reschedule time)
  4060. ("lognotereschedule" org-log-reschedule note)
  4061. ("nologreschedule" org-log-reschedule nil)
  4062. ("logredeadline" org-log-redeadline time)
  4063. ("lognoteredeadline" org-log-redeadline note)
  4064. ("nologredeadline" org-log-redeadline nil)
  4065. ("logrefile" org-log-refile time)
  4066. ("lognoterefile" org-log-refile note)
  4067. ("nologrefile" org-log-refile nil)
  4068. ("fninline" org-footnote-define-inline t)
  4069. ("nofninline" org-footnote-define-inline nil)
  4070. ("fnlocal" org-footnote-section nil)
  4071. ("fnauto" org-footnote-auto-label t)
  4072. ("fnprompt" org-footnote-auto-label nil)
  4073. ("fnconfirm" org-footnote-auto-label confirm)
  4074. ("fnplain" org-footnote-auto-label plain)
  4075. ("fnadjust" org-footnote-auto-adjust t)
  4076. ("nofnadjust" org-footnote-auto-adjust nil)
  4077. ("constcgs" constants-unit-system cgs)
  4078. ("constSI" constants-unit-system SI)
  4079. ("noptag" org-tag-persistent-alist nil)
  4080. ("hideblocks" org-hide-block-startup t)
  4081. ("nohideblocks" org-hide-block-startup nil)
  4082. ("beamer" org-startup-with-beamer-mode t)
  4083. ("entitiespretty" org-pretty-entities t)
  4084. ("entitiesplain" org-pretty-entities nil))
  4085. "Variable associated with STARTUP options for org-mode.
  4086. Each element is a list of three items: the startup options (as written
  4087. in the #+STARTUP line), the corresponding variable, and the value to set
  4088. this variable to if the option is found. An optional forth element PUSH
  4089. means to push this value onto the list in the variable.")
  4090. (defun org-update-property-plist (key val props)
  4091. "Update PROPS with KEY and VAL."
  4092. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4093. (key (if appending (substring key 0 (- (length key) 1)) key))
  4094. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4095. (previous (cdr (assoc key props))))
  4096. (if appending
  4097. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4098. (cons (cons key val) remainder))))
  4099. (defconst org-block-regexp
  4100. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4101. "Regular expression for hiding blocks.")
  4102. (defconst org-heading-keyword-regexp-format
  4103. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4104. "Printf format for a regexp matching an headline with some keyword.
  4105. This regexp will match the headline of any node which has the
  4106. exact keyword that is put into the format. The keyword isn't in
  4107. any group by default, but the stars and the body are.")
  4108. (defconst org-heading-keyword-maybe-regexp-format
  4109. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4110. "Printf format for a regexp matching an headline, possibly with some keyword.
  4111. This regexp can match any headline with the specified keyword, or
  4112. without a keyword. The keyword isn't in any group by default,
  4113. but the stars and the body are.")
  4114. (defun org-set-regexps-and-options ()
  4115. "Precompute regular expressions for current buffer."
  4116. (when (derived-mode-p 'org-mode)
  4117. (org-set-local 'org-todo-kwd-alist nil)
  4118. (org-set-local 'org-todo-key-alist nil)
  4119. (org-set-local 'org-todo-key-trigger nil)
  4120. (org-set-local 'org-todo-keywords-1 nil)
  4121. (org-set-local 'org-done-keywords nil)
  4122. (org-set-local 'org-todo-heads nil)
  4123. (org-set-local 'org-todo-sets nil)
  4124. (org-set-local 'org-todo-log-states nil)
  4125. (org-set-local 'org-file-properties nil)
  4126. (org-set-local 'org-file-tags nil)
  4127. (let ((re (org-make-options-regexp
  4128. '("CATEGORY" "TODO" "COLUMNS"
  4129. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4130. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4131. "OPTIONS")
  4132. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4133. (splitre "[ \t]+")
  4134. (scripts org-use-sub-superscripts)
  4135. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4136. tail sep kws1 prio props ftags drawers beamer-p
  4137. ext-setup-or-nil setup-contents (start 0))
  4138. (save-excursion
  4139. (save-restriction
  4140. (widen)
  4141. (goto-char (point-min))
  4142. (while (or (and ext-setup-or-nil
  4143. (string-match re ext-setup-or-nil start)
  4144. (setq start (match-end 0)))
  4145. (and (setq ext-setup-or-nil nil start 0)
  4146. (re-search-forward re nil t)))
  4147. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4148. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4149. (if (stringp value) (setq value (org-trim value)))
  4150. (cond
  4151. ((equal key "CATEGORY")
  4152. (setq cat value))
  4153. ((member key '("SEQ_TODO" "TODO"))
  4154. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4155. ((equal key "TYP_TODO")
  4156. (push (cons 'type (org-split-string value splitre)) kwds))
  4157. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4158. ;; general TODO-like setup
  4159. (push (cons (intern (downcase (match-string 1 key)))
  4160. (org-split-string value splitre)) kwds))
  4161. ((equal key "TAGS")
  4162. (setq tags (append tags (if tags '("\\n") nil)
  4163. (org-split-string value splitre))))
  4164. ((equal key "COLUMNS")
  4165. (org-set-local 'org-columns-default-format value))
  4166. ((equal key "LINK")
  4167. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4168. (push (cons (match-string 1 value)
  4169. (org-trim (match-string 2 value)))
  4170. links)))
  4171. ((equal key "PRIORITIES")
  4172. (setq prio (org-split-string value " +")))
  4173. ((equal key "PROPERTY")
  4174. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4175. (setq props (org-update-property-plist (match-string 1 value)
  4176. (match-string 2 value)
  4177. props))))
  4178. ((equal key "FILETAGS")
  4179. (when (string-match "\\S-" value)
  4180. (setq ftags
  4181. (append
  4182. ftags
  4183. (apply 'append
  4184. (mapcar (lambda (x) (org-split-string x ":"))
  4185. (org-split-string value)))))))
  4186. ((equal key "DRAWERS")
  4187. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4188. ((equal key "CONSTANTS")
  4189. (setq const (append const (org-split-string value splitre))))
  4190. ((equal key "STARTUP")
  4191. (let ((opts (org-split-string value splitre))
  4192. l var val)
  4193. (while (setq l (pop opts))
  4194. (when (setq l (assoc l org-startup-options))
  4195. (setq var (nth 1 l) val (nth 2 l))
  4196. (if (not (nth 3 l))
  4197. (set (make-local-variable var) val)
  4198. (if (not (listp (symbol-value var)))
  4199. (set (make-local-variable var) nil))
  4200. (set (make-local-variable var) (symbol-value var))
  4201. (add-to-list var val))))))
  4202. ((equal key "ARCHIVE")
  4203. (setq arch value)
  4204. (remove-text-properties 0 (length arch)
  4205. '(face t fontified t) arch))
  4206. ((equal key "LATEX_CLASS")
  4207. (setq beamer-p (equal value "beamer")))
  4208. ((equal key "OPTIONS")
  4209. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4210. (setq scripts (read (match-string 2 value)))))
  4211. ((equal key "SETUPFILE")
  4212. (setq setup-contents (org-file-contents
  4213. (expand-file-name
  4214. (org-remove-double-quotes value))
  4215. 'noerror))
  4216. (if (not ext-setup-or-nil)
  4217. (setq ext-setup-or-nil setup-contents start 0)
  4218. (setq ext-setup-or-nil
  4219. (concat (substring ext-setup-or-nil 0 start)
  4220. "\n" setup-contents "\n"
  4221. (substring ext-setup-or-nil start)))))))
  4222. ;; search for property blocks
  4223. (goto-char (point-min))
  4224. (while (re-search-forward org-block-regexp nil t)
  4225. (when (equal "PROPERTY" (upcase (match-string 1)))
  4226. (setq value (replace-regexp-in-string
  4227. "[\n\r]" " " (match-string 4)))
  4228. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4229. (setq props (org-update-property-plist (match-string 1 value)
  4230. (match-string 2 value)
  4231. props)))))))
  4232. (org-set-local 'org-use-sub-superscripts scripts)
  4233. (when cat
  4234. (org-set-local 'org-category (intern cat))
  4235. (push (cons "CATEGORY" cat) props))
  4236. (when prio
  4237. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4238. (setq prio (mapcar 'string-to-char prio))
  4239. (org-set-local 'org-highest-priority (nth 0 prio))
  4240. (org-set-local 'org-lowest-priority (nth 1 prio))
  4241. (org-set-local 'org-default-priority (nth 2 prio)))
  4242. (and props (org-set-local 'org-file-properties (nreverse props)))
  4243. (and ftags (org-set-local 'org-file-tags
  4244. (mapcar 'org-add-prop-inherited ftags)))
  4245. (and drawers (org-set-local 'org-drawers drawers))
  4246. (and arch (org-set-local 'org-archive-location arch))
  4247. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4248. ;; Process the TODO keywords
  4249. (unless kwds
  4250. ;; Use the global values as if they had been given locally.
  4251. (setq kwds (default-value 'org-todo-keywords))
  4252. (if (stringp (car kwds))
  4253. (setq kwds (list (cons org-todo-interpretation
  4254. (default-value 'org-todo-keywords)))))
  4255. (setq kwds (reverse kwds)))
  4256. (setq kwds (nreverse kwds))
  4257. (let (inter kws kw)
  4258. (while (setq kws (pop kwds))
  4259. (let ((kws (or
  4260. (run-hook-with-args-until-success
  4261. 'org-todo-setup-filter-hook kws)
  4262. kws)))
  4263. (setq inter (pop kws) sep (member "|" kws)
  4264. kws0 (delete "|" (copy-sequence kws))
  4265. kwsa nil
  4266. kws1 (mapcar
  4267. (lambda (x)
  4268. ;; 1 2
  4269. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4270. (progn
  4271. (setq kw (match-string 1 x)
  4272. key (and (match-end 2) (match-string 2 x))
  4273. log (org-extract-log-state-settings x))
  4274. (push (cons kw (and key (string-to-char key))) kwsa)
  4275. (and log (push log org-todo-log-states))
  4276. kw)
  4277. (error "Invalid TODO keyword %s" x)))
  4278. kws0)
  4279. kwsa (if kwsa (append '((:startgroup))
  4280. (nreverse kwsa)
  4281. '((:endgroup))))
  4282. hw (car kws1)
  4283. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4284. tail (list inter hw (car dws) (org-last dws))))
  4285. (add-to-list 'org-todo-heads hw 'append)
  4286. (push kws1 org-todo-sets)
  4287. (setq org-done-keywords (append org-done-keywords dws nil))
  4288. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4289. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4290. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4291. (setq org-todo-sets (nreverse org-todo-sets)
  4292. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4293. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4294. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4295. ;; Process the constants
  4296. (when const
  4297. (let (e cst)
  4298. (while (setq e (pop const))
  4299. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4300. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4301. (setq org-table-formula-constants-local cst)))
  4302. ;; Process the tags.
  4303. (when tags
  4304. (let (e tgs)
  4305. (while (setq e (pop tags))
  4306. (cond
  4307. ((equal e "{") (push '(:startgroup) tgs))
  4308. ((equal e "}") (push '(:endgroup) tgs))
  4309. ((equal e "\\n") (push '(:newline) tgs))
  4310. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4311. (push (cons (match-string 1 e)
  4312. (string-to-char (match-string 2 e)))
  4313. tgs))
  4314. (t (push (list e) tgs))))
  4315. (org-set-local 'org-tag-alist nil)
  4316. (while (setq e (pop tgs))
  4317. (or (and (stringp (car e))
  4318. (assoc (car e) org-tag-alist))
  4319. (push e org-tag-alist)))))
  4320. ;; Compute the regular expressions and other local variables.
  4321. ;; Using `org-outline-regexp-bol' would complicate them much,
  4322. ;; because of the fixed white space at the end of that string.
  4323. (if (not org-done-keywords)
  4324. (setq org-done-keywords (and org-todo-keywords-1
  4325. (list (org-last org-todo-keywords-1)))))
  4326. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4327. (length org-scheduled-string)
  4328. (length org-clock-string)
  4329. (length org-closed-string)))
  4330. org-drawer-regexp
  4331. (concat "^[ \t]*:\\("
  4332. (mapconcat 'regexp-quote org-drawers "\\|")
  4333. "\\):[ \t]*$")
  4334. org-not-done-keywords
  4335. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4336. org-todo-regexp
  4337. (concat "\\("
  4338. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4339. "\\)")
  4340. org-not-done-regexp
  4341. (concat "\\("
  4342. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4343. "\\)")
  4344. org-not-done-heading-regexp
  4345. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4346. org-todo-line-regexp
  4347. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4348. org-complex-heading-regexp
  4349. (concat "^\\(\\*+\\)"
  4350. "\\(?: +" org-todo-regexp "\\)?"
  4351. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4352. "\\(?: +\\(.*?\\)\\)??"
  4353. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4354. "[ \t]*$")
  4355. org-complex-heading-regexp-format
  4356. (concat "^\\(\\*+\\)"
  4357. "\\(?: +" org-todo-regexp "\\)?"
  4358. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4359. "\\(?: +"
  4360. ;; Stats cookies can be stuck to body.
  4361. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4362. "\\(%s\\)"
  4363. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4364. "\\)"
  4365. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4366. "[ \t]*$")
  4367. org-todo-line-tags-regexp
  4368. (concat "^\\(\\*+\\)"
  4369. "\\(?: +" org-todo-regexp "\\)?"
  4370. "\\(?: +\\(.*?\\)\\)??"
  4371. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4372. "[ \t]*$")
  4373. org-deadline-regexp (concat "\\<" org-deadline-string)
  4374. org-deadline-time-regexp
  4375. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4376. org-deadline-line-regexp
  4377. (concat "\\<\\(" org-deadline-string "\\).*")
  4378. org-scheduled-regexp
  4379. (concat "\\<" org-scheduled-string)
  4380. org-scheduled-time-regexp
  4381. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4382. org-closed-time-regexp
  4383. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4384. org-keyword-time-regexp
  4385. (concat "\\<\\(" org-scheduled-string
  4386. "\\|" org-deadline-string
  4387. "\\|" org-closed-string
  4388. "\\|" org-clock-string "\\)"
  4389. " *[[<]\\([^]>]+\\)[]>]")
  4390. org-keyword-time-not-clock-regexp
  4391. (concat "\\<\\(" org-scheduled-string
  4392. "\\|" org-deadline-string
  4393. "\\|" org-closed-string
  4394. "\\)"
  4395. " *[[<]\\([^]>]+\\)[]>]")
  4396. org-maybe-keyword-time-regexp
  4397. (concat "\\(\\<\\(" org-scheduled-string
  4398. "\\|" org-deadline-string
  4399. "\\|" org-closed-string
  4400. "\\|" org-clock-string "\\)\\)?"
  4401. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4402. org-all-time-keywords
  4403. (mapcar (lambda (w) (substring w 0 -1))
  4404. (list org-scheduled-string org-deadline-string
  4405. org-clock-string org-closed-string)))
  4406. (org-set-font-lock-defaults))))
  4407. (defun org-file-contents (file &optional noerror)
  4408. "Return the contents of FILE, as a string."
  4409. (if (or (not file)
  4410. (not (file-readable-p file)))
  4411. (if noerror
  4412. (progn
  4413. (message "Cannot read file \"%s\"" file)
  4414. (ding) (sit-for 2)
  4415. "")
  4416. (error "Cannot read file \"%s\"" file))
  4417. (with-temp-buffer
  4418. (insert-file-contents file)
  4419. (buffer-string))))
  4420. (defun org-extract-log-state-settings (x)
  4421. "Extract the log state setting from a TODO keyword string.
  4422. This will extract info from a string like \"WAIT(w@/!)\"."
  4423. (let (kw key log1 log2)
  4424. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4425. (setq kw (match-string 1 x)
  4426. key (and (match-end 2) (match-string 2 x))
  4427. log1 (and (match-end 3) (match-string 3 x))
  4428. log2 (and (match-end 4) (match-string 4 x)))
  4429. (and (or log1 log2)
  4430. (list kw
  4431. (and log1 (if (equal log1 "!") 'time 'note))
  4432. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4433. (defun org-remove-keyword-keys (list)
  4434. "Remove a pair of parenthesis at the end of each string in LIST."
  4435. (mapcar (lambda (x)
  4436. (if (string-match "(.*)$" x)
  4437. (substring x 0 (match-beginning 0))
  4438. x))
  4439. list))
  4440. (defun org-assign-fast-keys (alist)
  4441. "Assign fast keys to a keyword-key alist.
  4442. Respect keys that are already there."
  4443. (let (new e (alt ?0))
  4444. (while (setq e (pop alist))
  4445. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4446. (cdr e)) ;; Key already assigned.
  4447. (push e new)
  4448. (let ((clist (string-to-list (downcase (car e))))
  4449. (used (append new alist)))
  4450. (when (= (car clist) ?@)
  4451. (pop clist))
  4452. (while (and clist (rassoc (car clist) used))
  4453. (pop clist))
  4454. (unless clist
  4455. (while (rassoc alt used)
  4456. (incf alt)))
  4457. (push (cons (car e) (or (car clist) alt)) new))))
  4458. (nreverse new)))
  4459. ;;; Some variables used in various places
  4460. (defvar org-window-configuration nil
  4461. "Used in various places to store a window configuration.")
  4462. (defvar org-selected-window nil
  4463. "Used in various places to store a window configuration.")
  4464. (defvar org-finish-function nil
  4465. "Function to be called when `C-c C-c' is used.
  4466. This is for getting out of special buffers like capture.")
  4467. ;; FIXME: Occasionally check by commenting these, to make sure
  4468. ;; no other functions uses these, forgetting to let-bind them.
  4469. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4470. (defvar org-last-state)
  4471. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4472. ;; Defined somewhere in this file, but used before definition.
  4473. (defvar org-entities) ;; defined in org-entities.el
  4474. (defvar org-struct-menu)
  4475. (defvar org-org-menu)
  4476. (defvar org-tbl-menu)
  4477. ;;;; Define the Org-mode
  4478. ;; We use a before-change function to check if a table might need
  4479. ;; an update.
  4480. (defvar org-table-may-need-update t
  4481. "Indicates that a table might need an update.
  4482. This variable is set by `org-before-change-function'.
  4483. `org-table-align' sets it back to nil.")
  4484. (defun org-before-change-function (beg end)
  4485. "Every change indicates that a table might need an update."
  4486. (setq org-table-may-need-update t))
  4487. (defvar org-mode-map)
  4488. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4489. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4490. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4491. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4492. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4493. (defvar org-table-buffer-is-an nil)
  4494. (defvar bidi-paragraph-direction)
  4495. (defvar buffer-face-mode-face)
  4496. (require 'outline)
  4497. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4498. (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"))
  4499. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4500. ;; Other stuff we need.
  4501. (require 'time-date)
  4502. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4503. (require 'easymenu)
  4504. (require 'overlay)
  4505. ;; (require 'org-macs) moved higher up in the file before it is first used
  4506. (require 'org-entities)
  4507. ;; (require 'org-compat) moved higher up in the file before it is first used
  4508. (require 'org-faces)
  4509. (require 'org-list)
  4510. (require 'org-pcomplete)
  4511. (require 'org-src)
  4512. (require 'org-footnote)
  4513. (require 'org-macro)
  4514. ;; babel
  4515. (require 'ob)
  4516. ;;;###autoload
  4517. (define-derived-mode org-mode outline-mode "Org"
  4518. "Outline-based notes management and organizer, alias
  4519. \"Carsten's outline-mode for keeping track of everything.\"
  4520. Org-mode develops organizational tasks around a NOTES file which
  4521. contains information about projects as plain text. Org-mode is
  4522. implemented on top of outline-mode, which is ideal to keep the content
  4523. of large files well structured. It supports ToDo items, deadlines and
  4524. time stamps, which magically appear in the diary listing of the Emacs
  4525. calendar. Tables are easily created with a built-in table editor.
  4526. Plain text URL-like links connect to websites, emails (VM), Usenet
  4527. messages (Gnus), BBDB entries, and any files related to the project.
  4528. For printing and sharing of notes, an Org-mode file (or a part of it)
  4529. can be exported as a structured ASCII or HTML file.
  4530. The following commands are available:
  4531. \\{org-mode-map}"
  4532. ;; Get rid of Outline menus, they are not needed
  4533. ;; Need to do this here because define-derived-mode sets up
  4534. ;; the keymap so late. Still, it is a waste to call this each time
  4535. ;; we switch another buffer into org-mode.
  4536. (if (featurep 'xemacs)
  4537. (when (boundp 'outline-mode-menu-heading)
  4538. ;; Assume this is Greg's port, it uses easymenu
  4539. (easy-menu-remove outline-mode-menu-heading)
  4540. (easy-menu-remove outline-mode-menu-show)
  4541. (easy-menu-remove outline-mode-menu-hide))
  4542. (define-key org-mode-map [menu-bar headings] 'undefined)
  4543. (define-key org-mode-map [menu-bar hide] 'undefined)
  4544. (define-key org-mode-map [menu-bar show] 'undefined))
  4545. (org-load-modules-maybe)
  4546. (easy-menu-add org-org-menu)
  4547. (easy-menu-add org-tbl-menu)
  4548. (org-install-agenda-files-menu)
  4549. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4550. (add-to-invisibility-spec '(org-cwidth))
  4551. (add-to-invisibility-spec '(org-hide-block . t))
  4552. (when (featurep 'xemacs)
  4553. (org-set-local 'line-move-ignore-invisible t))
  4554. (org-set-local 'outline-regexp org-outline-regexp)
  4555. (org-set-local 'outline-level 'org-outline-level)
  4556. (setq bidi-paragraph-direction 'left-to-right)
  4557. (when (and org-ellipsis
  4558. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4559. (fboundp 'make-glyph-code))
  4560. (unless org-display-table
  4561. (setq org-display-table (make-display-table)))
  4562. (set-display-table-slot
  4563. org-display-table 4
  4564. (vconcat (mapcar
  4565. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4566. org-ellipsis)))
  4567. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4568. (setq buffer-display-table org-display-table))
  4569. (org-set-regexps-and-options)
  4570. (when (and org-tag-faces (not org-tags-special-faces-re))
  4571. ;; tag faces set outside customize.... force initialization.
  4572. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4573. ;; Calc embedded
  4574. (org-set-local 'calc-embedded-open-mode "# ")
  4575. (mapc (lambda(c) (modify-syntax-entry (string-to-char (car c)) "w p"))
  4576. org-emphasis-alist)
  4577. (modify-syntax-entry ?< "(")
  4578. (modify-syntax-entry ?> ")")
  4579. (modify-syntax-entry ?{ "(")
  4580. (modify-syntax-entry ?} ")")
  4581. (modify-syntax-entry ?@ "w")
  4582. (modify-syntax-entry ?\" "\"")
  4583. (if org-startup-truncated (setq truncate-lines t))
  4584. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4585. (org-set-local 'font-lock-unfontify-region-function
  4586. 'org-unfontify-region)
  4587. ;; Activate before-change-function
  4588. (org-set-local 'org-table-may-need-update t)
  4589. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4590. 'local)
  4591. ;; Check for running clock before killing a buffer
  4592. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4593. ;; Initialize macros templates.
  4594. (org-macro-initialize-templates)
  4595. ;; Initialize radio targets.
  4596. (org-update-radio-target-regexp)
  4597. ;; Indentation.
  4598. (org-set-local 'indent-line-function 'org-indent-line)
  4599. (org-set-local 'indent-region-function 'org-indent-region)
  4600. ;; Filling and auto-filling.
  4601. (org-setup-filling)
  4602. ;; Comments.
  4603. (org-setup-comments-handling)
  4604. ;; Beginning/end of defun
  4605. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4606. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4607. ;; Next error for sparse trees
  4608. (org-set-local 'next-error-function 'org-occur-next-match)
  4609. ;; Make sure dependence stuff works reliably, even for users who set it
  4610. ;; too late :-(
  4611. (if org-enforce-todo-dependencies
  4612. (add-hook 'org-blocker-hook
  4613. 'org-block-todo-from-children-or-siblings-or-parent)
  4614. (remove-hook 'org-blocker-hook
  4615. 'org-block-todo-from-children-or-siblings-or-parent))
  4616. (if org-enforce-todo-checkbox-dependencies
  4617. (add-hook 'org-blocker-hook
  4618. 'org-block-todo-from-checkboxes)
  4619. (remove-hook 'org-blocker-hook
  4620. 'org-block-todo-from-checkboxes))
  4621. ;; Align options lines
  4622. (org-set-local
  4623. 'align-mode-rules-list
  4624. '((org-in-buffer-settings
  4625. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4626. (modes . '(org-mode)))))
  4627. ;; Imenu
  4628. (org-set-local 'imenu-create-index-function
  4629. 'org-imenu-get-tree)
  4630. ;; Make isearch reveal context
  4631. (if (or (featurep 'xemacs)
  4632. (not (boundp 'outline-isearch-open-invisible-function)))
  4633. ;; Emacs 21 and XEmacs make use of the hook
  4634. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4635. ;; Emacs 22 deals with this through a special variable
  4636. (org-set-local 'outline-isearch-open-invisible-function
  4637. (lambda (&rest ignore) (org-show-context 'isearch))))
  4638. ;; Setup the pcomplete hooks
  4639. (set (make-local-variable 'pcomplete-command-completion-function)
  4640. 'org-pcomplete-initial)
  4641. (set (make-local-variable 'pcomplete-command-name-function)
  4642. 'org-command-at-point)
  4643. (set (make-local-variable 'pcomplete-default-completion-function)
  4644. 'ignore)
  4645. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4646. 'org-parse-arguments)
  4647. (set (make-local-variable 'pcomplete-termination-string) "")
  4648. (when (>= emacs-major-version 23)
  4649. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4650. ;; If empty file that did not turn on org-mode automatically, make it to.
  4651. (if (and org-insert-mode-line-in-empty-file
  4652. (org-called-interactively-p 'any)
  4653. (= (point-min) (point-max)))
  4654. (insert "# -*- mode: org -*-\n\n"))
  4655. (unless org-inhibit-startup
  4656. (org-unmodified
  4657. (and org-startup-with-beamer-mode (org-beamer-mode))
  4658. (when org-startup-align-all-tables
  4659. (org-table-map-tables 'org-table-align 'quietly))
  4660. (when org-startup-with-inline-images
  4661. (org-display-inline-images))
  4662. (when org-startup-with-latex-preview
  4663. (org-preview-latex-fragment))
  4664. (unless org-inhibit-startup-visibility-stuff
  4665. (org-set-startup-visibility))))
  4666. ;; Try to set org-hide correctly
  4667. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4668. (when (fboundp 'abbrev-table-put)
  4669. (abbrev-table-put org-mode-abbrev-table
  4670. :parents (list text-mode-abbrev-table)))
  4671. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4672. (defun org-find-invisible-foreground ()
  4673. (let ((candidates (remove
  4674. "unspecified-bg"
  4675. (nconc
  4676. (list (face-background 'default)
  4677. (face-background 'org-default))
  4678. (mapcar
  4679. (lambda (alist)
  4680. (when (boundp alist)
  4681. (cdr (assoc 'background-color (symbol-value alist)))))
  4682. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4683. (list (face-foreground 'org-hide))))))
  4684. (car (remove nil candidates))))
  4685. (defun org-current-time (&optional rounding-minutes)
  4686. "Current time, possibly rounded to ROUNDING-MINUTES.
  4687. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4688. `org-time-stamp-rounding-minutes'."
  4689. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4690. (car org-time-stamp-rounding-minutes)))
  4691. (time (decode-time)))
  4692. (if (> r 1)
  4693. (apply 'encode-time
  4694. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4695. (nthcdr 2 time)))
  4696. (current-time))))
  4697. (defun org-today ()
  4698. "Return today date, considering `org-extend-today-until'."
  4699. (time-to-days
  4700. (time-subtract (current-time)
  4701. (list 0 (* 3600 org-extend-today-until) 0))))
  4702. ;;;; Font-Lock stuff, including the activators
  4703. (defvar org-mouse-map (make-sparse-keymap))
  4704. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4705. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4706. (when org-mouse-1-follows-link
  4707. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4708. (when org-tab-follows-link
  4709. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4710. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4711. (require 'font-lock)
  4712. (defconst org-non-link-chars "]\t\n\r<>")
  4713. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4714. "shell" "elisp" "doi" "message"))
  4715. (defvar org-link-types-re nil
  4716. "Matches a link that has a url-like prefix like \"http:\"")
  4717. (defvar org-link-re-with-space nil
  4718. "Matches a link with spaces, optional angular brackets around it.")
  4719. (defvar org-link-re-with-space2 nil
  4720. "Matches a link with spaces, optional angular brackets around it.")
  4721. (defvar org-link-re-with-space3 nil
  4722. "Matches a link with spaces, only for internal part in bracket links.")
  4723. (defvar org-angle-link-re nil
  4724. "Matches link with angular brackets, spaces are allowed.")
  4725. (defvar org-plain-link-re nil
  4726. "Matches plain link, without spaces.")
  4727. (defvar org-bracket-link-regexp nil
  4728. "Matches a link in double brackets.")
  4729. (defvar org-bracket-link-analytic-regexp nil
  4730. "Regular expression used to analyze links.
  4731. Here is what the match groups contain after a match:
  4732. 1: http:
  4733. 2: http
  4734. 3: path
  4735. 4: [desc]
  4736. 5: desc")
  4737. (defvar org-bracket-link-analytic-regexp++ nil
  4738. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4739. (defvar org-any-link-re nil
  4740. "Regular expression matching any link.")
  4741. (defconst org-match-sexp-depth 3
  4742. "Number of stacked braces for sub/superscript matching.")
  4743. (defun org-create-multibrace-regexp (left right n)
  4744. "Create a regular expression which will match a balanced sexp.
  4745. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4746. as single character strings.
  4747. The regexp returned will match the entire expression including the
  4748. delimiters. It will also define a single group which contains the
  4749. match except for the outermost delimiters. The maximum depth of
  4750. stacked delimiters is N. Escaping delimiters is not possible."
  4751. (let* ((nothing (concat "[^" left right "]*?"))
  4752. (or "\\|")
  4753. (re nothing)
  4754. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4755. (while (> n 1)
  4756. (setq n (1- n)
  4757. re (concat re or next)
  4758. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4759. (concat left "\\(" re "\\)" right)))
  4760. (defvar org-match-substring-regexp
  4761. (concat
  4762. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4763. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4764. "\\|"
  4765. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4766. "\\|"
  4767. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4768. "The regular expression matching a sub- or superscript.")
  4769. (defvar org-match-substring-with-braces-regexp
  4770. (concat
  4771. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4772. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4773. "\\)")
  4774. "The regular expression matching a sub- or superscript, forcing braces.")
  4775. (defun org-make-link-regexps ()
  4776. "Update the link regular expressions.
  4777. This should be called after the variable `org-link-types' has changed."
  4778. (setq org-link-types-re
  4779. (concat
  4780. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4781. org-link-re-with-space
  4782. (concat
  4783. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4784. "\\([^" org-non-link-chars " ]"
  4785. "[^" org-non-link-chars "]*"
  4786. "[^" org-non-link-chars " ]\\)>?")
  4787. org-link-re-with-space2
  4788. (concat
  4789. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4790. "\\([^" org-non-link-chars " ]"
  4791. "[^\t\n\r]*"
  4792. "[^" org-non-link-chars " ]\\)>?")
  4793. org-link-re-with-space3
  4794. (concat
  4795. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4796. "\\([^" org-non-link-chars " ]"
  4797. "[^\t\n\r]*\\)")
  4798. org-angle-link-re
  4799. (concat
  4800. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4801. "\\([^" org-non-link-chars " ]"
  4802. "[^" org-non-link-chars "]*"
  4803. "\\)>")
  4804. org-plain-link-re
  4805. (concat
  4806. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4807. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4808. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4809. org-bracket-link-regexp
  4810. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4811. org-bracket-link-analytic-regexp
  4812. (concat
  4813. "\\[\\["
  4814. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4815. "\\([^]]+\\)"
  4816. "\\]"
  4817. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4818. "\\]")
  4819. org-bracket-link-analytic-regexp++
  4820. (concat
  4821. "\\[\\["
  4822. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4823. "\\([^]]+\\)"
  4824. "\\]"
  4825. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4826. "\\]")
  4827. org-any-link-re
  4828. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4829. org-angle-link-re "\\)\\|\\("
  4830. org-plain-link-re "\\)")))
  4831. (org-make-link-regexps)
  4832. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4833. "Regular expression for fast time stamp matching.")
  4834. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4835. "Regular expression for fast time stamp matching.")
  4836. (defconst org-ts-regexp0
  4837. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4838. "Regular expression matching time strings for analysis.
  4839. This one does not require the space after the date, so it can be used
  4840. on a string that terminates immediately after the date.")
  4841. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4842. "Regular expression matching time strings for analysis.")
  4843. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4844. "Regular expression matching time stamps, with groups.")
  4845. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4846. "Regular expression matching time stamps (also [..]), with groups.")
  4847. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4848. "Regular expression matching a time stamp range.")
  4849. (defconst org-tr-regexp-both
  4850. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4851. "Regular expression matching a time stamp range.")
  4852. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4853. org-ts-regexp "\\)?")
  4854. "Regular expression matching a time stamp or time stamp range.")
  4855. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4856. org-ts-regexp-both "\\)?")
  4857. "Regular expression matching a time stamp or time stamp range.
  4858. The time stamps may be either active or inactive.")
  4859. (defvar org-emph-face nil)
  4860. (defun org-do-emphasis-faces (limit)
  4861. "Run through the buffer and add overlays to emphasized strings."
  4862. (let (rtn a)
  4863. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4864. (if (not (= (char-after (match-beginning 3))
  4865. (char-after (match-beginning 4))))
  4866. (progn
  4867. (setq rtn t)
  4868. (setq a (assoc (match-string 3) org-emphasis-alist))
  4869. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4870. 'face
  4871. (nth 1 a))
  4872. (and (nth 4 a)
  4873. (org-remove-flyspell-overlays-in
  4874. (match-beginning 0) (match-end 0)))
  4875. (add-text-properties (match-beginning 2) (match-end 2)
  4876. '(font-lock-multiline t org-emphasis t))
  4877. (when org-hide-emphasis-markers
  4878. (add-text-properties (match-end 4) (match-beginning 5)
  4879. '(invisible org-link))
  4880. (add-text-properties (match-beginning 3) (match-end 3)
  4881. '(invisible org-link)))))
  4882. (backward-char 1))
  4883. rtn))
  4884. (defun org-emphasize (&optional char)
  4885. "Insert or change an emphasis, i.e. a font like bold or italic.
  4886. If there is an active region, change that region to a new emphasis.
  4887. If there is no region, just insert the marker characters and position
  4888. the cursor between them.
  4889. CHAR should be either the marker character, or the first character of the
  4890. HTML tag associated with that emphasis. If CHAR is a space, the means
  4891. to remove the emphasis of the selected region.
  4892. If char is not given (for example in an interactive call) it
  4893. will be prompted for."
  4894. (interactive)
  4895. (let ((eal org-emphasis-alist) e det
  4896. (erc org-emphasis-regexp-components)
  4897. (prompt "")
  4898. (string "") beg end move tag c s)
  4899. (if (org-region-active-p)
  4900. (setq beg (region-beginning) end (region-end)
  4901. string (buffer-substring beg end))
  4902. (setq move t))
  4903. (while (setq e (pop eal))
  4904. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4905. c (aref tag 0))
  4906. (push (cons c (string-to-char (car e))) det)
  4907. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4908. (substring tag 1)))))
  4909. (setq det (nreverse det))
  4910. (unless char
  4911. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4912. (setq char (read-char-exclusive)))
  4913. (setq char (or (cdr (assoc char det)) char))
  4914. (if (equal char ?\ )
  4915. (setq s "" move nil)
  4916. (unless (assoc (char-to-string char) org-emphasis-alist)
  4917. (error "No such emphasis marker: \"%c\"" char))
  4918. (setq s (char-to-string char)))
  4919. (while (and (> (length string) 1)
  4920. (equal (substring string 0 1) (substring string -1))
  4921. (assoc (substring string 0 1) org-emphasis-alist))
  4922. (setq string (substring string 1 -1)))
  4923. (setq string (concat s string s))
  4924. (if beg (delete-region beg end))
  4925. (unless (or (bolp)
  4926. (string-match (concat "[" (nth 0 erc) "\n]")
  4927. (char-to-string (char-before (point)))))
  4928. (insert " "))
  4929. (unless (or (eobp)
  4930. (string-match (concat "[" (nth 1 erc) "\n]")
  4931. (char-to-string (char-after (point)))))
  4932. (insert " ") (backward-char 1))
  4933. (insert string)
  4934. (and move (backward-char 1))))
  4935. (defconst org-nonsticky-props
  4936. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4937. (defsubst org-rear-nonsticky-at (pos)
  4938. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4939. (defun org-activate-plain-links (limit)
  4940. "Run through the buffer and add overlays to links."
  4941. (catch 'exit
  4942. (let (f hl)
  4943. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4944. (not (org-in-src-block-p)))
  4945. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4946. (setq f (get-text-property (match-beginning 0) 'face))
  4947. (setq hl (org-match-string-no-properties 0))
  4948. (if (or (eq f 'org-tag)
  4949. (and (listp f) (memq 'org-tag f)))
  4950. nil
  4951. (add-text-properties (match-beginning 0) (match-end 0)
  4952. (list 'mouse-face 'highlight
  4953. 'face 'org-link
  4954. 'htmlize-link `(:uri ,hl)
  4955. 'keymap org-mouse-map))
  4956. (org-rear-nonsticky-at (match-end 0)))
  4957. t))))
  4958. (defun org-activate-code (limit)
  4959. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4960. (progn
  4961. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4962. (remove-text-properties (match-beginning 0) (match-end 0)
  4963. '(display t invisible t intangible t))
  4964. t)))
  4965. (defcustom org-src-fontify-natively nil
  4966. "When non-nil, fontify code in code blocks."
  4967. :type 'boolean
  4968. :version "24.1"
  4969. :group 'org-appearance
  4970. :group 'org-babel)
  4971. (defcustom org-allow-promoting-top-level-subtree nil
  4972. "When non-nil, allow promoting a top level subtree.
  4973. The leading star of the top level headline will be replaced
  4974. by a #."
  4975. :type 'boolean
  4976. :version "24.1"
  4977. :group 'org-appearance)
  4978. (defun org-fontify-meta-lines-and-blocks (limit)
  4979. (condition-case nil
  4980. (org-fontify-meta-lines-and-blocks-1 limit)
  4981. (error (message "org-mode fontification error"))))
  4982. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4983. "Fontify #+ lines and blocks, in the correct ways."
  4984. (let ((case-fold-search t))
  4985. (if (re-search-forward
  4986. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4987. limit t)
  4988. (let ((beg (match-beginning 0))
  4989. (block-start (match-end 0))
  4990. (block-end nil)
  4991. (lang (match-string 7))
  4992. (beg1 (line-beginning-position 2))
  4993. (dc1 (downcase (match-string 2)))
  4994. (dc3 (downcase (match-string 3)))
  4995. end end1 quoting block-type ovl)
  4996. (cond
  4997. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  4998. ;; a single line of backend-specific content
  4999. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5000. (remove-text-properties (match-beginning 0) (match-end 0)
  5001. '(display t invisible t intangible t))
  5002. (add-text-properties (match-beginning 1) (match-end 3)
  5003. '(font-lock-fontified t face org-meta-line))
  5004. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5005. '(font-lock-fontified t face org-block))
  5006. ; for backend-specific code
  5007. t)
  5008. ((and (match-end 4) (equal dc3 "+begin"))
  5009. ;; Truly a block
  5010. (setq block-type (downcase (match-string 5))
  5011. quoting (member block-type org-protecting-blocks))
  5012. (when (re-search-forward
  5013. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5014. nil t) ;; on purpose, we look further than LIMIT
  5015. (setq end (min (point-max) (match-end 0))
  5016. end1 (min (point-max) (1- (match-beginning 0))))
  5017. (setq block-end (match-beginning 0))
  5018. (when quoting
  5019. (remove-text-properties beg end
  5020. '(display t invisible t intangible t)))
  5021. (add-text-properties
  5022. beg end
  5023. '(font-lock-fontified t font-lock-multiline t))
  5024. (add-text-properties beg beg1 '(face org-meta-line))
  5025. (add-text-properties end1 (min (point-max) (1+ end))
  5026. '(face org-meta-line)) ; for end_src
  5027. (cond
  5028. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5029. (org-src-font-lock-fontify-block lang block-start block-end)
  5030. ;; remove old background overlays
  5031. (mapc (lambda (ov)
  5032. (if (eq (overlay-get ov 'face) 'org-block-background)
  5033. (delete-overlay ov)))
  5034. (overlays-at (/ (+ beg1 block-end) 2)))
  5035. ;; add a background overlay
  5036. (setq ovl (make-overlay beg1 block-end))
  5037. (overlay-put ovl 'face 'org-block-background)
  5038. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5039. (quoting
  5040. (add-text-properties beg1 (min (point-max) (1+ end1))
  5041. '(face org-block))) ; end of source block
  5042. ((not org-fontify-quote-and-verse-blocks))
  5043. ((string= block-type "quote")
  5044. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5045. ((string= block-type "verse")
  5046. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5047. (add-text-properties beg beg1 '(face org-block-begin-line))
  5048. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5049. '(face org-block-end-line))
  5050. t))
  5051. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5052. (add-text-properties
  5053. beg (match-end 3)
  5054. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5055. '(font-lock-fontified t invisible t)
  5056. '(font-lock-fontified t face org-document-info-keyword)))
  5057. (add-text-properties
  5058. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5059. (if (string-equal dc1 "+title:")
  5060. '(font-lock-fontified t face org-document-title)
  5061. '(font-lock-fontified t face org-document-info))))
  5062. ((or (equal dc1 "+results")
  5063. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5064. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5065. "+call:" "+header:" "+headers:" "+name:"))
  5066. (and (match-end 4) (equal dc3 "+attr")))
  5067. (add-text-properties
  5068. beg (match-end 0)
  5069. '(font-lock-fontified t face org-meta-line))
  5070. t)
  5071. ((member dc3 '(" " ""))
  5072. (add-text-properties
  5073. beg (match-end 0)
  5074. '(font-lock-fontified t face font-lock-comment-face)))
  5075. ((not (member (char-after beg) '(?\ ?\t)))
  5076. ;; just any other in-buffer setting, but not indented
  5077. (add-text-properties
  5078. beg (match-end 0)
  5079. '(font-lock-fontified t face org-meta-line))
  5080. t)
  5081. (t nil))))))
  5082. (defun org-activate-angle-links (limit)
  5083. "Run through the buffer and add overlays to links."
  5084. (if (and (re-search-forward org-angle-link-re limit t)
  5085. (not (org-in-src-block-p)))
  5086. (progn
  5087. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5088. (add-text-properties (match-beginning 0) (match-end 0)
  5089. (list 'mouse-face 'highlight
  5090. 'keymap org-mouse-map))
  5091. (org-rear-nonsticky-at (match-end 0))
  5092. t)))
  5093. (defun org-activate-footnote-links (limit)
  5094. "Run through the buffer and add overlays to footnotes."
  5095. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5096. (when fn
  5097. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5098. (org-remove-flyspell-overlays-in beg end)
  5099. (add-text-properties beg end
  5100. (list 'mouse-face 'highlight
  5101. 'keymap org-mouse-map
  5102. 'help-echo
  5103. (if (= (point-at-bol) beg)
  5104. "Footnote definition"
  5105. "Footnote reference")
  5106. 'font-lock-fontified t
  5107. 'font-lock-multiline t
  5108. 'face 'org-footnote))))))
  5109. (defun org-activate-bracket-links (limit)
  5110. "Run through the buffer and add overlays to bracketed links."
  5111. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5112. (not (org-in-src-block-p)))
  5113. (let* ((hl (org-match-string-no-properties 1))
  5114. (help (concat "LINK: " hl))
  5115. ;; FIXME: Above we should remove the escapes. But that
  5116. ;; requires another match, protecting match data, a lot
  5117. ;; of overhead for font-lock.
  5118. (ip (org-maybe-intangible
  5119. (list 'invisible 'org-link
  5120. 'keymap org-mouse-map 'mouse-face 'highlight
  5121. 'font-lock-multiline t 'help-echo help
  5122. 'htmlize-link `(:uri ,hl))))
  5123. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5124. 'font-lock-multiline t 'help-echo help
  5125. 'htmlize-link `(:uri ,hl))))
  5126. ;; We need to remove the invisible property here. Table narrowing
  5127. ;; may have made some of this invisible.
  5128. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5129. (remove-text-properties (match-beginning 0) (match-end 0)
  5130. '(invisible nil))
  5131. (if (match-end 3)
  5132. (progn
  5133. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5134. (org-rear-nonsticky-at (match-beginning 3))
  5135. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5136. (org-rear-nonsticky-at (match-end 3))
  5137. (add-text-properties (match-end 3) (match-end 0) ip)
  5138. (org-rear-nonsticky-at (match-end 0)))
  5139. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5140. (org-rear-nonsticky-at (match-beginning 1))
  5141. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5142. (org-rear-nonsticky-at (match-end 1))
  5143. (add-text-properties (match-end 1) (match-end 0) ip)
  5144. (org-rear-nonsticky-at (match-end 0)))
  5145. t)))
  5146. (defun org-activate-dates (limit)
  5147. "Run through the buffer and add overlays to dates."
  5148. (if (re-search-forward org-tsr-regexp-both limit t)
  5149. (progn
  5150. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5151. (add-text-properties (match-beginning 0) (match-end 0)
  5152. (list 'mouse-face 'highlight
  5153. 'keymap org-mouse-map))
  5154. (org-rear-nonsticky-at (match-end 0))
  5155. (when org-display-custom-times
  5156. (if (match-end 3)
  5157. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5158. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5159. t)))
  5160. (defvar org-target-link-regexp nil
  5161. "Regular expression matching radio targets in plain text.")
  5162. (make-variable-buffer-local 'org-target-link-regexp)
  5163. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5164. "Regular expression matching a link target.")
  5165. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5166. "Regular expression matching a radio target.")
  5167. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5168. "Regular expression matching any target.")
  5169. (defun org-activate-target-links (limit)
  5170. "Run through the buffer and add overlays to target matches."
  5171. (when org-target-link-regexp
  5172. (let ((case-fold-search t))
  5173. (if (re-search-forward org-target-link-regexp limit t)
  5174. (progn
  5175. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5176. (add-text-properties (match-beginning 0) (match-end 0)
  5177. (list 'mouse-face 'highlight
  5178. 'keymap org-mouse-map
  5179. 'help-echo "Radio target link"
  5180. 'org-linked-text t))
  5181. (org-rear-nonsticky-at (match-end 0))
  5182. t)))))
  5183. (defun org-update-radio-target-regexp ()
  5184. "Find all radio targets in this file and update the regular expression."
  5185. (interactive)
  5186. (when (memq 'radio org-activate-links)
  5187. (setq org-target-link-regexp
  5188. (org-make-target-link-regexp (org-all-targets 'radio)))
  5189. (org-restart-font-lock)))
  5190. (defun org-hide-wide-columns (limit)
  5191. (let (s e)
  5192. (setq s (text-property-any (point) (or limit (point-max))
  5193. 'org-cwidth t))
  5194. (when s
  5195. (setq e (next-single-property-change s 'org-cwidth))
  5196. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5197. (goto-char e)
  5198. t)))
  5199. (defvar org-match-substring-regexp)
  5200. (defvar org-match-substring-with-braces-regexp)
  5201. (defun org-restart-font-lock ()
  5202. "Restart `font-lock-mode', to force refontification."
  5203. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5204. (font-lock-mode -1)
  5205. (font-lock-mode 1)))
  5206. (defun org-all-targets (&optional radio)
  5207. "Return a list of all targets in this file.
  5208. When optional argument RADIO is non-nil, only find radio
  5209. targets."
  5210. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5211. (save-excursion
  5212. (goto-char (point-min))
  5213. (while (re-search-forward re nil t)
  5214. ;; Make sure point is really within the object.
  5215. (backward-char)
  5216. (let ((obj (org-element-context)))
  5217. (when (memq (org-element-type obj) '(radio-target target))
  5218. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5219. rtn)))
  5220. (defun org-make-target-link-regexp (targets)
  5221. "Make regular expression matching all strings in TARGETS.
  5222. The regular expression finds the targets also if there is a line break
  5223. between words."
  5224. (and targets
  5225. (concat
  5226. "\\<\\("
  5227. (mapconcat
  5228. (lambda (x)
  5229. (setq x (regexp-quote x))
  5230. (while (string-match " +" x)
  5231. (setq x (replace-match "\\s-+" t t x)))
  5232. x)
  5233. targets
  5234. "\\|")
  5235. "\\)\\>")))
  5236. (defun org-activate-tags (limit)
  5237. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5238. (progn
  5239. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5240. (add-text-properties (match-beginning 1) (match-end 1)
  5241. (list 'mouse-face 'highlight
  5242. 'keymap org-mouse-map))
  5243. (org-rear-nonsticky-at (match-end 1))
  5244. t)))
  5245. (defun org-outline-level ()
  5246. "Compute the outline level of the heading at point.
  5247. If this is called at a normal headline, the level is the number of stars.
  5248. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5249. (save-excursion
  5250. (if (not (condition-case nil
  5251. (org-back-to-heading t)
  5252. (error nil)))
  5253. 0
  5254. (looking-at org-outline-regexp)
  5255. (1- (- (match-end 0) (match-beginning 0))))))
  5256. (defvar org-font-lock-keywords nil)
  5257. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5258. "Regular expression matching a property line.")
  5259. (defvar org-font-lock-hook nil
  5260. "Functions to be called for special font lock stuff.")
  5261. (defvar org-font-lock-set-keywords-hook nil
  5262. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5263. This is called after `org-font-lock-extra-keywords' is defined, but before
  5264. it is installed to be used by font lock. This can be useful if something
  5265. needs to be inserted at a specific position in the font-lock sequence.")
  5266. (defun org-font-lock-hook (limit)
  5267. "Run `org-font-lock-hook' within LIMIT."
  5268. (run-hook-with-args 'org-font-lock-hook limit))
  5269. (defun org-set-font-lock-defaults ()
  5270. "Set font lock defaults for the current buffer."
  5271. (let* ((em org-fontify-emphasized-text)
  5272. (lk org-activate-links)
  5273. (org-font-lock-extra-keywords
  5274. (list
  5275. ;; Call the hook
  5276. '(org-font-lock-hook)
  5277. ;; Headlines
  5278. `(,(if org-fontify-whole-heading-line
  5279. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5280. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5281. (1 (org-get-level-face 1))
  5282. (2 (org-get-level-face 2))
  5283. (3 (org-get-level-face 3)))
  5284. ;; Table lines
  5285. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5286. (1 'org-table t))
  5287. ;; Table internals
  5288. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5289. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5290. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5291. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5292. ;; Drawers
  5293. (list org-drawer-regexp '(0 'org-special-keyword t))
  5294. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5295. ;; Properties
  5296. (list org-property-re
  5297. '(1 'org-special-keyword t)
  5298. '(3 'org-property-value t))
  5299. ;; Links
  5300. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5301. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5302. (if (memq 'plain lk) '(org-activate-plain-links))
  5303. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5304. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5305. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5306. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5307. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5308. '(org-hide-wide-columns (0 nil append))
  5309. ;; TODO keyword
  5310. (list (format org-heading-keyword-regexp-format
  5311. org-todo-regexp)
  5312. '(2 (org-get-todo-face 2) t))
  5313. ;; DONE
  5314. (if org-fontify-done-headline
  5315. (list (format org-heading-keyword-regexp-format
  5316. (concat
  5317. "\\(?:"
  5318. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5319. "\\)"))
  5320. '(2 'org-headline-done t))
  5321. nil)
  5322. ;; Priorities
  5323. '(org-font-lock-add-priority-faces)
  5324. ;; Tags
  5325. '(org-font-lock-add-tag-faces)
  5326. ;; Special keywords
  5327. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5328. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5329. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5330. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5331. ;; Emphasis
  5332. (if em
  5333. (if (featurep 'xemacs)
  5334. '(org-do-emphasis-faces (0 nil append))
  5335. '(org-do-emphasis-faces)))
  5336. ;; Checkboxes
  5337. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5338. 1 'org-checkbox prepend)
  5339. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5340. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5341. (0 (org-get-checkbox-statistics-face) t)))
  5342. ;; Description list items
  5343. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5344. 1 'org-list-dt prepend)
  5345. ;; ARCHIVEd headings
  5346. (list (concat
  5347. org-outline-regexp-bol
  5348. "\\(.*:" org-archive-tag ":.*\\)")
  5349. '(1 'org-archived prepend))
  5350. ;; Specials
  5351. '(org-fontify-entities)
  5352. '(org-raise-scripts)
  5353. ;; Code
  5354. '(org-activate-code (1 'org-code t))
  5355. ;; COMMENT
  5356. (list (format org-heading-keyword-regexp-format
  5357. (concat "\\("
  5358. org-comment-string "\\|" org-quote-string
  5359. "\\)"))
  5360. '(2 'org-special-keyword t))
  5361. ;; Blocks and meta lines
  5362. '(org-fontify-meta-lines-and-blocks)
  5363. )))
  5364. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5365. (run-hooks 'org-font-lock-set-keywords-hook)
  5366. ;; Now set the full font-lock-keywords
  5367. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5368. (org-set-local 'font-lock-defaults
  5369. '(org-font-lock-keywords t nil nil backward-paragraph))
  5370. (kill-local-variable 'font-lock-keywords) nil))
  5371. (defun org-toggle-pretty-entities ()
  5372. "Toggle the composition display of entities as UTF8 characters."
  5373. (interactive)
  5374. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5375. (org-restart-font-lock)
  5376. (if org-pretty-entities
  5377. (message "Entities are displayed as UTF8 characters")
  5378. (save-restriction
  5379. (widen)
  5380. (org-decompose-region (point-min) (point-max))
  5381. (message "Entities are displayed plain"))))
  5382. (defvar org-custom-properties-overlays nil
  5383. "List of overlays used for custom properties.")
  5384. (make-variable-buffer-local 'org-custom-properties-overlays)
  5385. (defun org-toggle-custom-properties-visibility ()
  5386. "Display or hide properties in `org-custom-properties'."
  5387. (interactive)
  5388. (if org-custom-properties-overlays
  5389. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5390. (setq org-custom-properties-overlays nil))
  5391. (unless (not org-custom-properties)
  5392. (save-excursion
  5393. (save-restriction
  5394. (widen)
  5395. (goto-char (point-min))
  5396. (while (re-search-forward org-property-re nil t)
  5397. (mapc (lambda(p)
  5398. (when (equal p (substring (match-string 1) 1 -1))
  5399. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5400. (overlay-put o 'invisible t)
  5401. (overlay-put o 'org-custom-property t)
  5402. (push o org-custom-properties-overlays))))
  5403. org-custom-properties)))))))
  5404. (defun org-fontify-entities (limit)
  5405. "Find an entity to fontify."
  5406. (let (ee)
  5407. (when org-pretty-entities
  5408. (catch 'match
  5409. (while (re-search-forward
  5410. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5411. limit t)
  5412. (if (and (not (org-in-indented-comment-line))
  5413. (setq ee (org-entity-get (match-string 1)))
  5414. (= (length (nth 6 ee)) 1))
  5415. (let*
  5416. ((end (if (equal (match-string 2) "{}")
  5417. (match-end 2)
  5418. (match-end 1))))
  5419. (add-text-properties
  5420. (match-beginning 0) end
  5421. (list 'font-lock-fontified t))
  5422. (compose-region (match-beginning 0) end
  5423. (nth 6 ee) nil)
  5424. (backward-char 1)
  5425. (throw 'match t))))
  5426. nil))))
  5427. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5428. "Fontify string S like in Org-mode."
  5429. (with-temp-buffer
  5430. (insert s)
  5431. (let ((org-odd-levels-only odd-levels))
  5432. (org-mode)
  5433. (font-lock-fontify-buffer)
  5434. (buffer-string))))
  5435. (defvar org-m nil)
  5436. (defvar org-l nil)
  5437. (defvar org-f nil)
  5438. (defun org-get-level-face (n)
  5439. "Get the right face for match N in font-lock matching of headlines."
  5440. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5441. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5442. (if org-cycle-level-faces
  5443. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5444. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5445. (cond
  5446. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5447. ((eq n 2) org-f)
  5448. (t (if org-level-color-stars-only nil org-f))))
  5449. (defun org-get-todo-face (kwd)
  5450. "Get the right face for a TODO keyword KWD.
  5451. If KWD is a number, get the corresponding match group."
  5452. (if (numberp kwd) (setq kwd (match-string kwd)))
  5453. (or (org-face-from-face-or-color
  5454. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5455. (and (member kwd org-done-keywords) 'org-done)
  5456. 'org-todo))
  5457. (defun org-face-from-face-or-color (context inherit face-or-color)
  5458. "Create a face list that inherits INHERIT, but sets the foreground color.
  5459. When FACE-OR-COLOR is not a string, just return it."
  5460. (if (stringp face-or-color)
  5461. (list :inherit inherit
  5462. (cdr (assoc context org-faces-easy-properties))
  5463. face-or-color)
  5464. face-or-color))
  5465. (defun org-font-lock-add-tag-faces (limit)
  5466. "Add the special tag faces."
  5467. (when (and org-tag-faces org-tags-special-faces-re)
  5468. (while (re-search-forward org-tags-special-faces-re limit t)
  5469. (add-text-properties (match-beginning 1) (match-end 1)
  5470. (list 'face (org-get-tag-face 1)
  5471. 'font-lock-fontified t))
  5472. (backward-char 1))))
  5473. (defun org-font-lock-add-priority-faces (limit)
  5474. "Add the special priority faces."
  5475. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5476. (when (save-match-data (org-at-heading-p))
  5477. (add-text-properties
  5478. (match-beginning 0) (match-end 0)
  5479. (list 'face (or (org-face-from-face-or-color
  5480. 'priority 'org-priority
  5481. (cdr (assoc (char-after (match-beginning 1))
  5482. org-priority-faces)))
  5483. 'org-priority)
  5484. 'font-lock-fontified t)))))
  5485. (defun org-get-tag-face (kwd)
  5486. "Get the right face for a TODO keyword KWD.
  5487. If KWD is a number, get the corresponding match group."
  5488. (if (numberp kwd) (setq kwd (match-string kwd)))
  5489. (or (org-face-from-face-or-color
  5490. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5491. 'org-tag))
  5492. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5493. "Remove fontification and activation overlays from links."
  5494. (font-lock-default-unfontify-region beg end)
  5495. (let* ((buffer-undo-list t)
  5496. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5497. (inhibit-modification-hooks t)
  5498. deactivate-mark buffer-file-name buffer-file-truename)
  5499. (org-decompose-region beg end)
  5500. (remove-text-properties beg end
  5501. '(mouse-face t keymap t org-linked-text t
  5502. invisible t intangible t
  5503. org-no-flyspell t org-emphasis t))
  5504. (org-remove-font-lock-display-properties beg end)))
  5505. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5506. ((raise 0.3) (height 0.7))
  5507. ((raise -0.5))
  5508. ((raise 0.5)))
  5509. "Display properties for showing superscripts and subscripts.")
  5510. (defun org-remove-font-lock-display-properties (beg end)
  5511. "Remove specific display properties that have been added by font lock.
  5512. The will remove the raise properties that are used to show superscripts
  5513. and subscripts."
  5514. (let (next prop)
  5515. (while (< beg end)
  5516. (setq next (next-single-property-change beg 'display nil end)
  5517. prop (get-text-property beg 'display))
  5518. (if (member prop org-script-display)
  5519. (put-text-property beg next 'display nil))
  5520. (setq beg next))))
  5521. (defun org-raise-scripts (limit)
  5522. "Add raise properties to sub/superscripts."
  5523. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5524. (if (re-search-forward
  5525. (if (eq org-use-sub-superscripts t)
  5526. org-match-substring-regexp
  5527. org-match-substring-with-braces-regexp)
  5528. limit t)
  5529. (let* ((pos (point)) table-p comment-p
  5530. (mpos (match-beginning 3))
  5531. (emph-p (get-text-property mpos 'org-emphasis))
  5532. (link-p (get-text-property mpos 'mouse-face))
  5533. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5534. (goto-char (point-at-bol))
  5535. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5536. comment-p (org-looking-at-p "[ \t]*#"))
  5537. (goto-char pos)
  5538. ;; FIXME: Should we go back one character here, for a_b^c
  5539. ;; (goto-char (1- pos)) ;????????????????????
  5540. (if (or comment-p emph-p link-p keyw-p)
  5541. t
  5542. (put-text-property (match-beginning 3) (match-end 0)
  5543. 'display
  5544. (if (equal (char-after (match-beginning 2)) ?^)
  5545. (nth (if table-p 3 1) org-script-display)
  5546. (nth (if table-p 2 0) org-script-display)))
  5547. (add-text-properties (match-beginning 2) (match-end 2)
  5548. (list 'invisible t
  5549. 'org-dwidth t 'org-dwidth-n 1))
  5550. (if (and (eq (char-after (match-beginning 3)) ?{)
  5551. (eq (char-before (match-end 3)) ?}))
  5552. (progn
  5553. (add-text-properties
  5554. (match-beginning 3) (1+ (match-beginning 3))
  5555. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5556. (add-text-properties
  5557. (1- (match-end 3)) (match-end 3)
  5558. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5559. t)))))
  5560. ;;;; Visibility cycling, including org-goto and indirect buffer
  5561. ;;; Cycling
  5562. (defvar org-cycle-global-status nil)
  5563. (make-variable-buffer-local 'org-cycle-global-status)
  5564. (put 'org-cycle-global-status 'org-state t)
  5565. (defvar org-cycle-subtree-status nil)
  5566. (make-variable-buffer-local 'org-cycle-subtree-status)
  5567. (put 'org-cycle-subtree-status 'org-state t)
  5568. (defvar org-inlinetask-min-level)
  5569. ;;;###autoload
  5570. (defun org-cycle (&optional arg)
  5571. "TAB-action and visibility cycling for Org-mode.
  5572. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5573. is outline visibility cycling, but it also invokes other actions
  5574. in special contexts.
  5575. - When this function is called with a prefix argument, rotate the entire
  5576. buffer through 3 states (global cycling)
  5577. 1. OVERVIEW: Show only top-level headlines.
  5578. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5579. 3. SHOW ALL: Show everything.
  5580. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5581. determined by the variable `org-startup-folded', and by any VISIBILITY
  5582. properties in the buffer.
  5583. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5584. including any drawers.
  5585. - When inside a table, re-align the table and move to the next field.
  5586. - When point is at the beginning of a headline, rotate the subtree started
  5587. by this line through 3 different states (local cycling)
  5588. 1. FOLDED: Only the main headline is shown.
  5589. 2. CHILDREN: The main headline and the direct children are shown.
  5590. From this state, you can move to one of the children
  5591. and zoom in further.
  5592. 3. SUBTREE: Show the entire subtree, including body text.
  5593. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5594. - When point is at the beginning of an empty headline and the variable
  5595. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5596. of the headline by demoting and promoting it to likely levels. This
  5597. speeds up creation document structure by pressing TAB once or several
  5598. times right after creating a new headline.
  5599. - When there is a numeric prefix, go up to a heading with level ARG, do
  5600. a `show-subtree' and return to the previous cursor position. If ARG
  5601. is negative, go up that many levels.
  5602. - When point is not at the beginning of a headline, execute the global
  5603. binding for TAB, which is re-indenting the line. See the option
  5604. `org-cycle-emulate-tab' for details.
  5605. - Special case: if point is at the beginning of the buffer and there is
  5606. no headline in line 1, this function will act as if called with prefix arg
  5607. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5608. But only if also the variable `org-cycle-global-at-bob' is t."
  5609. (interactive "P")
  5610. (org-load-modules-maybe)
  5611. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5612. (and org-cycle-level-after-item/entry-creation
  5613. (or (org-cycle-level)
  5614. (org-cycle-item-indentation))))
  5615. (let* (message-log-max ; Don't populate the *Messages* buffer
  5616. (limit-level
  5617. (or org-cycle-max-level
  5618. (and (boundp 'org-inlinetask-min-level)
  5619. org-inlinetask-min-level
  5620. (1- org-inlinetask-min-level))))
  5621. (nstars (and limit-level
  5622. (if org-odd-levels-only
  5623. (and limit-level (1- (* limit-level 2)))
  5624. limit-level)))
  5625. (org-outline-regexp
  5626. (if (not (derived-mode-p 'org-mode))
  5627. outline-regexp
  5628. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5629. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5630. (not (looking-at org-outline-regexp))))
  5631. (org-cycle-hook
  5632. (if bob-special
  5633. (delq 'org-optimize-window-after-visibility-change
  5634. (copy-sequence org-cycle-hook))
  5635. org-cycle-hook))
  5636. (pos (point)))
  5637. (if (or bob-special (equal arg '(4)))
  5638. ;; special case: use global cycling
  5639. (setq arg t))
  5640. (cond
  5641. ((equal arg '(16))
  5642. (setq last-command 'dummy)
  5643. (org-set-startup-visibility)
  5644. (message "Startup visibility, plus VISIBILITY properties"))
  5645. ((equal arg '(64))
  5646. (show-all)
  5647. (message "Entire buffer visible, including drawers"))
  5648. ;; Table: enter it or move to the next field.
  5649. ((org-at-table-p 'any)
  5650. (if (org-at-table.el-p)
  5651. (message "Use C-c ' to edit table.el tables")
  5652. (if arg (org-table-edit-field t)
  5653. (org-table-justify-field-maybe)
  5654. (call-interactively 'org-table-next-field))))
  5655. ((run-hook-with-args-until-success
  5656. 'org-tab-after-check-for-table-hook))
  5657. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5658. ((eq arg t) (org-cycle-internal-global))
  5659. ;; Drawers: delegate to `org-flag-drawer'.
  5660. ((and org-drawers org-drawer-regexp
  5661. (save-excursion
  5662. (beginning-of-line 1)
  5663. (looking-at org-drawer-regexp)))
  5664. (org-flag-drawer ; toggle block visibility
  5665. (not (get-char-property (match-end 0) 'invisible))))
  5666. ;; Show-subtree, ARG levels up from here.
  5667. ((integerp arg)
  5668. (save-excursion
  5669. (org-back-to-heading)
  5670. (outline-up-heading (if (< arg 0) (- arg)
  5671. (- (funcall outline-level) arg)))
  5672. (org-show-subtree)))
  5673. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5674. ((and (featurep 'org-inlinetask)
  5675. (org-inlinetask-at-task-p)
  5676. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5677. (org-inlinetask-toggle-visibility))
  5678. ((org-try-cdlatex-tab))
  5679. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5680. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5681. (save-excursion (beginning-of-line 1)
  5682. (looking-at org-outline-regexp)))
  5683. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5684. (org-cycle-internal-local))
  5685. ;; From there: TAB emulation and template completion.
  5686. (buffer-read-only (org-back-to-heading))
  5687. ((run-hook-with-args-until-success
  5688. 'org-tab-after-check-for-cycling-hook))
  5689. ((org-try-structure-completion))
  5690. ((run-hook-with-args-until-success
  5691. 'org-tab-before-tab-emulation-hook))
  5692. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5693. (or (not (bolp))
  5694. (not (looking-at org-outline-regexp))))
  5695. (call-interactively (global-key-binding "\t")))
  5696. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5697. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5698. (or (and (eq org-cycle-emulate-tab 'white)
  5699. (= (match-end 0) (point-at-eol)))
  5700. (and (eq org-cycle-emulate-tab 'whitestart)
  5701. (>= (match-end 0) pos))))
  5702. t
  5703. (eq org-cycle-emulate-tab t))
  5704. (call-interactively (global-key-binding "\t")))
  5705. (t (save-excursion
  5706. (org-back-to-heading)
  5707. (org-cycle)))))))
  5708. (defun org-cycle-internal-global ()
  5709. "Do the global cycling action."
  5710. ;; Hack to avoid display of messages for .org attachments in Gnus
  5711. (let (message-log-max ; Don't populate the *Messages* buffer
  5712. (ga (string-match "\\*fontification" (buffer-name))))
  5713. (cond
  5714. ((and (eq last-command this-command)
  5715. (eq org-cycle-global-status 'overview))
  5716. ;; We just created the overview - now do table of contents
  5717. ;; This can be slow in very large buffers, so indicate action
  5718. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5719. (unless ga (message "CONTENTS..."))
  5720. (org-content)
  5721. (unless ga (message "CONTENTS...done"))
  5722. (setq org-cycle-global-status 'contents)
  5723. (run-hook-with-args 'org-cycle-hook 'contents))
  5724. ((and (eq last-command this-command)
  5725. (eq org-cycle-global-status 'contents))
  5726. ;; We just showed the table of contents - now show everything
  5727. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5728. (show-all)
  5729. (unless ga (message "SHOW ALL"))
  5730. (setq org-cycle-global-status 'all)
  5731. (run-hook-with-args 'org-cycle-hook 'all))
  5732. (t
  5733. ;; Default action: go to overview
  5734. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5735. (org-overview)
  5736. (unless ga (message "OVERVIEW"))
  5737. (setq org-cycle-global-status 'overview)
  5738. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5739. (defun org-cycle-internal-local ()
  5740. "Do the local cycling action."
  5741. (let (message-log-max ; Don't populate the *Messages* buffer
  5742. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5743. ;; First, determine end of headline (EOH), end of subtree or item
  5744. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5745. (save-excursion
  5746. (if (org-at-item-p)
  5747. (progn
  5748. (beginning-of-line)
  5749. (setq struct (org-list-struct))
  5750. (setq eoh (point-at-eol))
  5751. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5752. (setq has-children (org-list-has-child-p (point) struct)))
  5753. (org-back-to-heading)
  5754. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5755. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5756. (setq has-children
  5757. (or (save-excursion
  5758. (let ((level (funcall outline-level)))
  5759. (outline-next-heading)
  5760. (and (org-at-heading-p t)
  5761. (> (funcall outline-level) level))))
  5762. (save-excursion
  5763. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5764. ;; Determine end invisible part of buffer (EOL)
  5765. (beginning-of-line 2)
  5766. ;; XEmacs doesn't have `next-single-char-property-change'
  5767. (if (featurep 'xemacs)
  5768. (while (and (not (eobp)) ;; this is like `next-line'
  5769. (get-char-property (1- (point)) 'invisible))
  5770. (beginning-of-line 2))
  5771. (while (and (not (eobp)) ;; this is like `next-line'
  5772. (get-char-property (1- (point)) 'invisible))
  5773. (goto-char (next-single-char-property-change (point) 'invisible))
  5774. (and (eolp) (beginning-of-line 2))))
  5775. (setq eol (point)))
  5776. ;; Find out what to do next and set `this-command'
  5777. (cond
  5778. ((= eos eoh)
  5779. ;; Nothing is hidden behind this heading
  5780. (unless (org-before-first-heading-p)
  5781. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5782. (message "EMPTY ENTRY")
  5783. (setq org-cycle-subtree-status nil)
  5784. (save-excursion
  5785. (goto-char eos)
  5786. (outline-next-heading)
  5787. (if (outline-invisible-p) (org-flag-heading nil))))
  5788. ((and (or (>= eol eos)
  5789. (not (string-match "\\S-" (buffer-substring eol eos))))
  5790. (or has-children
  5791. (not (setq children-skipped
  5792. org-cycle-skip-children-state-if-no-children))))
  5793. ;; Entire subtree is hidden in one line: children view
  5794. (unless (org-before-first-heading-p)
  5795. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5796. (if (org-at-item-p)
  5797. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5798. (org-show-entry)
  5799. (org-with-limited-levels (show-children))
  5800. ;; FIXME: This slows down the func way too much.
  5801. ;; How keep drawers hidden in subtree anyway?
  5802. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5803. ;; (org-cycle-hide-drawers 'subtree))
  5804. ;; Fold every list in subtree to top-level items.
  5805. (when (eq org-cycle-include-plain-lists 'integrate)
  5806. (save-excursion
  5807. (org-back-to-heading)
  5808. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5809. (beginning-of-line 1)
  5810. (let* ((struct (org-list-struct))
  5811. (prevs (org-list-prevs-alist struct))
  5812. (end (org-list-get-bottom-point struct)))
  5813. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5814. (org-list-get-all-items (point) struct prevs))
  5815. (goto-char end))))))
  5816. (message "CHILDREN")
  5817. (save-excursion
  5818. (goto-char eos)
  5819. (outline-next-heading)
  5820. (if (outline-invisible-p) (org-flag-heading nil)))
  5821. (setq org-cycle-subtree-status 'children)
  5822. (unless (org-before-first-heading-p)
  5823. (run-hook-with-args 'org-cycle-hook 'children)))
  5824. ((or children-skipped
  5825. (and (eq last-command this-command)
  5826. (eq org-cycle-subtree-status 'children)))
  5827. ;; We just showed the children, or no children are there,
  5828. ;; now show everything.
  5829. (unless (org-before-first-heading-p)
  5830. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5831. (outline-flag-region eoh eos nil)
  5832. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5833. (setq org-cycle-subtree-status 'subtree)
  5834. (unless (org-before-first-heading-p)
  5835. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5836. (t
  5837. ;; Default action: hide the subtree.
  5838. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5839. (outline-flag-region eoh eos t)
  5840. (message "FOLDED")
  5841. (setq org-cycle-subtree-status 'folded)
  5842. (unless (org-before-first-heading-p)
  5843. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5844. ;;;###autoload
  5845. (defun org-global-cycle (&optional arg)
  5846. "Cycle the global visibility. For details see `org-cycle'.
  5847. With \\[universal-argument] prefix arg, switch to startup visibility.
  5848. With a numeric prefix, show all headlines up to that level."
  5849. (interactive "P")
  5850. (let ((org-cycle-include-plain-lists
  5851. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5852. (cond
  5853. ((integerp arg)
  5854. (show-all)
  5855. (hide-sublevels arg)
  5856. (setq org-cycle-global-status 'contents))
  5857. ((equal arg '(4))
  5858. (org-set-startup-visibility)
  5859. (message "Startup visibility, plus VISIBILITY properties."))
  5860. (t
  5861. (org-cycle '(4))))))
  5862. (defun org-set-startup-visibility ()
  5863. "Set the visibility required by startup options and properties."
  5864. (cond
  5865. ((eq org-startup-folded t)
  5866. (org-cycle '(4)))
  5867. ((eq org-startup-folded 'content)
  5868. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5869. (org-cycle '(4)) (org-cycle '(4)))))
  5870. (unless (eq org-startup-folded 'showeverything)
  5871. (if org-hide-block-startup (org-hide-block-all))
  5872. (org-set-visibility-according-to-property 'no-cleanup)
  5873. (org-cycle-hide-archived-subtrees 'all)
  5874. (org-cycle-hide-drawers 'all)
  5875. (org-cycle-show-empty-lines t)))
  5876. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5877. "Switch subtree visibilities according to :VISIBILITY: property."
  5878. (interactive)
  5879. (let (org-show-entry-below state)
  5880. (save-excursion
  5881. (goto-char (point-min))
  5882. (while (re-search-forward
  5883. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5884. nil t)
  5885. (setq state (match-string 1))
  5886. (save-excursion
  5887. (org-back-to-heading t)
  5888. (hide-subtree)
  5889. (org-reveal)
  5890. (cond
  5891. ((equal state '("fold" "folded"))
  5892. (hide-subtree))
  5893. ((equal state "children")
  5894. (org-show-hidden-entry)
  5895. (show-children))
  5896. ((equal state "content")
  5897. (save-excursion
  5898. (save-restriction
  5899. (org-narrow-to-subtree)
  5900. (org-content))))
  5901. ((member state '("all" "showall"))
  5902. (show-subtree)))))
  5903. (unless no-cleanup
  5904. (org-cycle-hide-archived-subtrees 'all)
  5905. (org-cycle-hide-drawers 'all)
  5906. (org-cycle-show-empty-lines 'all)))))
  5907. ;; This function uses outline-regexp instead of the more fundamental
  5908. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5909. ;; buffers, where outline-regexp is needed.
  5910. (defun org-overview ()
  5911. "Switch to overview mode, showing only top-level headlines.
  5912. Really, this shows all headlines with level equal or greater than the level
  5913. of the first headline in the buffer. This is important, because if the
  5914. first headline is not level one, then (hide-sublevels 1) gives confusing
  5915. results."
  5916. (interactive)
  5917. (let ((level (save-excursion
  5918. (goto-char (point-min))
  5919. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5920. (progn
  5921. (goto-char (match-beginning 0))
  5922. (funcall outline-level))))))
  5923. (and level (hide-sublevels level))))
  5924. (defun org-content (&optional arg)
  5925. "Show all headlines in the buffer, like a table of contents.
  5926. With numerical argument N, show content up to level N."
  5927. (interactive "P")
  5928. (save-excursion
  5929. ;; Visit all headings and show their offspring
  5930. (and (integerp arg) (org-overview))
  5931. (goto-char (point-max))
  5932. (catch 'exit
  5933. (while (and (progn (condition-case nil
  5934. (outline-previous-visible-heading 1)
  5935. (error (goto-char (point-min))))
  5936. t)
  5937. (looking-at org-outline-regexp))
  5938. (if (integerp arg)
  5939. (show-children (1- arg))
  5940. (show-branches))
  5941. (if (bobp) (throw 'exit nil))))))
  5942. (defun org-optimize-window-after-visibility-change (state)
  5943. "Adjust the window after a change in outline visibility.
  5944. This function is the default value of the hook `org-cycle-hook'."
  5945. (when (get-buffer-window (current-buffer))
  5946. (cond
  5947. ((eq state 'content) nil)
  5948. ((eq state 'all) nil)
  5949. ((eq state 'folded) nil)
  5950. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5951. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5952. (defun org-remove-empty-overlays-at (pos)
  5953. "Remove outline overlays that do not contain non-white stuff."
  5954. (mapc
  5955. (lambda (o)
  5956. (and (eq 'outline (overlay-get o 'invisible))
  5957. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5958. (overlay-end o))))
  5959. (delete-overlay o)))
  5960. (overlays-at pos)))
  5961. (defun org-clean-visibility-after-subtree-move ()
  5962. "Fix visibility issues after moving a subtree."
  5963. ;; First, find a reasonable region to look at:
  5964. ;; Start two siblings above, end three below
  5965. (let* ((beg (save-excursion
  5966. (and (org-get-last-sibling)
  5967. (org-get-last-sibling))
  5968. (point)))
  5969. (end (save-excursion
  5970. (and (org-get-next-sibling)
  5971. (org-get-next-sibling)
  5972. (org-get-next-sibling))
  5973. (if (org-at-heading-p)
  5974. (point-at-eol)
  5975. (point))))
  5976. (level (looking-at "\\*+"))
  5977. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5978. (save-excursion
  5979. (save-restriction
  5980. (narrow-to-region beg end)
  5981. (when re
  5982. ;; Properly fold already folded siblings
  5983. (goto-char (point-min))
  5984. (while (re-search-forward re nil t)
  5985. (if (and (not (outline-invisible-p))
  5986. (save-excursion
  5987. (goto-char (point-at-eol)) (outline-invisible-p)))
  5988. (hide-entry))))
  5989. (org-cycle-show-empty-lines 'overview)
  5990. (org-cycle-hide-drawers 'overview)))))
  5991. (defun org-cycle-show-empty-lines (state)
  5992. "Show empty lines above all visible headlines.
  5993. The region to be covered depends on STATE when called through
  5994. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5995. entire buffer covered. Note that an empty line is only shown if there
  5996. are at least `org-cycle-separator-lines' empty lines before the headline."
  5997. (when (not (= org-cycle-separator-lines 0))
  5998. (save-excursion
  5999. (let* ((n (abs org-cycle-separator-lines))
  6000. (re (cond
  6001. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6002. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6003. (t (let ((ns (number-to-string (- n 2))))
  6004. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6005. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6006. beg end b e)
  6007. (cond
  6008. ((memq state '(overview contents t))
  6009. (setq beg (point-min) end (point-max)))
  6010. ((memq state '(children folded))
  6011. (setq beg (point) end (progn (org-end-of-subtree t t)
  6012. (beginning-of-line 2)
  6013. (point)))))
  6014. (when beg
  6015. (goto-char beg)
  6016. (while (re-search-forward re end t)
  6017. (unless (get-char-property (match-end 1) 'invisible)
  6018. (setq e (match-end 1))
  6019. (if (< org-cycle-separator-lines 0)
  6020. (setq b (save-excursion
  6021. (goto-char (match-beginning 0))
  6022. (org-back-over-empty-lines)
  6023. (if (save-excursion
  6024. (goto-char (max (point-min) (1- (point))))
  6025. (org-at-heading-p))
  6026. (1- (point))
  6027. (point))))
  6028. (setq b (match-beginning 1)))
  6029. (outline-flag-region b e nil)))))))
  6030. ;; Never hide empty lines at the end of the file.
  6031. (save-excursion
  6032. (goto-char (point-max))
  6033. (outline-previous-heading)
  6034. (outline-end-of-heading)
  6035. (if (and (looking-at "[ \t\n]+")
  6036. (= (match-end 0) (point-max)))
  6037. (outline-flag-region (point) (match-end 0) nil))))
  6038. (defun org-show-empty-lines-in-parent ()
  6039. "Move to the parent and re-show empty lines before visible headlines."
  6040. (save-excursion
  6041. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6042. (org-cycle-show-empty-lines context))))
  6043. (defun org-files-list ()
  6044. "Return `org-agenda-files' list, plus all open org-mode files.
  6045. This is useful for operations that need to scan all of a user's
  6046. open and agenda-wise Org files."
  6047. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6048. (dolist (buf (buffer-list))
  6049. (with-current-buffer buf
  6050. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6051. (let ((file (expand-file-name (buffer-file-name))))
  6052. (unless (member file files)
  6053. (push file files))))))
  6054. files))
  6055. (defsubst org-entry-beginning-position ()
  6056. "Return the beginning position of the current entry."
  6057. (save-excursion (outline-back-to-heading t) (point)))
  6058. (defsubst org-entry-end-position ()
  6059. "Return the end position of the current entry."
  6060. (save-excursion (outline-next-heading) (point)))
  6061. (defun org-cycle-hide-drawers (state)
  6062. "Re-hide all drawers after a visibility state change."
  6063. (when (and (derived-mode-p 'org-mode)
  6064. (not (memq state '(overview folded contents))))
  6065. (save-excursion
  6066. (let* ((globalp (memq state '(contents all)))
  6067. (beg (if globalp (point-min) (point)))
  6068. (end (if globalp (point-max)
  6069. (if (eq state 'children)
  6070. (save-excursion (outline-next-heading) (point))
  6071. (org-end-of-subtree t)))))
  6072. (goto-char beg)
  6073. (while (re-search-forward org-drawer-regexp end t)
  6074. (org-flag-drawer t))))))
  6075. (defun org-cycle-hide-inline-tasks (state)
  6076. "Re-hide inline task when switching to 'contents visibility state."
  6077. (when (and (eq state 'contents)
  6078. (boundp 'org-inlinetask-min-level)
  6079. org-inlinetask-min-level)
  6080. (hide-sublevels (1- org-inlinetask-min-level))))
  6081. (defun org-flag-drawer (flag)
  6082. "When FLAG is non-nil, hide the drawer we are within.
  6083. Otherwise make it visible."
  6084. (save-excursion
  6085. (beginning-of-line 1)
  6086. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6087. (let ((b (match-end 0)))
  6088. (if (re-search-forward
  6089. "^[ \t]*:END:"
  6090. (save-excursion (outline-next-heading) (point)) t)
  6091. (outline-flag-region b (point-at-eol) flag)
  6092. (error ":END: line missing at position %s" b))))))
  6093. (defun org-subtree-end-visible-p ()
  6094. "Is the end of the current subtree visible?"
  6095. (pos-visible-in-window-p
  6096. (save-excursion (org-end-of-subtree t) (point))))
  6097. (defun org-first-headline-recenter (&optional N)
  6098. "Move cursor to the first headline and recenter the headline.
  6099. Optional argument N means put the headline into the Nth line of the window."
  6100. (goto-char (point-min))
  6101. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6102. (beginning-of-line)
  6103. (recenter (prefix-numeric-value N))))
  6104. ;;; Saving and restoring visibility
  6105. (defun org-outline-overlay-data (&optional use-markers)
  6106. "Return a list of the locations of all outline overlays.
  6107. These are overlays with the `invisible' property value `outline'.
  6108. The return value is a list of cons cells, with start and stop
  6109. positions for each overlay.
  6110. If USE-MARKERS is set, return the positions as markers."
  6111. (let (beg end)
  6112. (save-excursion
  6113. (save-restriction
  6114. (widen)
  6115. (delq nil
  6116. (mapcar (lambda (o)
  6117. (when (eq (overlay-get o 'invisible) 'outline)
  6118. (setq beg (overlay-start o)
  6119. end (overlay-end o))
  6120. (and beg end (> end beg)
  6121. (if use-markers
  6122. (cons (move-marker (make-marker) beg)
  6123. (move-marker (make-marker) end))
  6124. (cons beg end)))))
  6125. (overlays-in (point-min) (point-max))))))))
  6126. (defun org-set-outline-overlay-data (data)
  6127. "Create visibility overlays for all positions in DATA.
  6128. DATA should have been made by `org-outline-overlay-data'."
  6129. (let (o)
  6130. (save-excursion
  6131. (save-restriction
  6132. (widen)
  6133. (show-all)
  6134. (mapc (lambda (c)
  6135. (outline-flag-region (car c) (cdr c) t))
  6136. data)))))
  6137. ;;; Folding of blocks
  6138. (defvar org-hide-block-overlays nil
  6139. "Overlays hiding blocks.")
  6140. (make-variable-buffer-local 'org-hide-block-overlays)
  6141. (defun org-block-map (function &optional start end)
  6142. "Call FUNCTION at the head of all source blocks in the current buffer.
  6143. Optional arguments START and END can be used to limit the range."
  6144. (let ((start (or start (point-min)))
  6145. (end (or end (point-max))))
  6146. (save-excursion
  6147. (goto-char start)
  6148. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6149. (save-excursion
  6150. (save-match-data
  6151. (goto-char (match-beginning 0))
  6152. (funcall function)))))))
  6153. (defun org-hide-block-toggle-all ()
  6154. "Toggle the visibility of all blocks in the current buffer."
  6155. (org-block-map #'org-hide-block-toggle))
  6156. (defun org-hide-block-all ()
  6157. "Fold all blocks in the current buffer."
  6158. (interactive)
  6159. (org-show-block-all)
  6160. (org-block-map #'org-hide-block-toggle-maybe))
  6161. (defun org-show-block-all ()
  6162. "Unfold all blocks in the current buffer."
  6163. (interactive)
  6164. (mapc 'delete-overlay org-hide-block-overlays)
  6165. (setq org-hide-block-overlays nil))
  6166. (defun org-hide-block-toggle-maybe ()
  6167. "Toggle visibility of block at point."
  6168. (interactive)
  6169. (let ((case-fold-search t))
  6170. (if (save-excursion
  6171. (beginning-of-line 1)
  6172. (looking-at org-block-regexp))
  6173. (progn (org-hide-block-toggle)
  6174. t) ;; to signal that we took action
  6175. nil))) ;; to signal that we did not
  6176. (defun org-hide-block-toggle (&optional force)
  6177. "Toggle the visibility of the current block."
  6178. (interactive)
  6179. (save-excursion
  6180. (beginning-of-line)
  6181. (if (re-search-forward org-block-regexp nil t)
  6182. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6183. (end (match-end 0)) ;; end of entire body
  6184. ov)
  6185. (if (memq t (mapcar (lambda (overlay)
  6186. (eq (overlay-get overlay 'invisible)
  6187. 'org-hide-block))
  6188. (overlays-at start)))
  6189. (if (or (not force) (eq force 'off))
  6190. (mapc (lambda (ov)
  6191. (when (member ov org-hide-block-overlays)
  6192. (setq org-hide-block-overlays
  6193. (delq ov org-hide-block-overlays)))
  6194. (when (eq (overlay-get ov 'invisible)
  6195. 'org-hide-block)
  6196. (delete-overlay ov)))
  6197. (overlays-at start)))
  6198. (setq ov (make-overlay start end))
  6199. (overlay-put ov 'invisible 'org-hide-block)
  6200. ;; make the block accessible to isearch
  6201. (overlay-put
  6202. ov 'isearch-open-invisible
  6203. (lambda (ov)
  6204. (when (member ov org-hide-block-overlays)
  6205. (setq org-hide-block-overlays
  6206. (delq ov org-hide-block-overlays)))
  6207. (when (eq (overlay-get ov 'invisible)
  6208. 'org-hide-block)
  6209. (delete-overlay ov))))
  6210. (push ov org-hide-block-overlays)))
  6211. (error "Not looking at a source block"))))
  6212. ;; org-tab-after-check-for-cycling-hook
  6213. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6214. ;; Remove overlays when changing major mode
  6215. (add-hook 'org-mode-hook
  6216. (lambda () (org-add-hook 'change-major-mode-hook
  6217. 'org-show-block-all 'append 'local)))
  6218. ;;; Org-goto
  6219. (defvar org-goto-window-configuration nil)
  6220. (defvar org-goto-marker nil)
  6221. (defvar org-goto-map)
  6222. (defun org-goto-map ()
  6223. "Set the keymap `org-goto'."
  6224. (setq org-goto-map
  6225. (let ((map (make-sparse-keymap)))
  6226. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6227. mouse-drag-region universal-argument org-occur))
  6228. cmd)
  6229. (while (setq cmd (pop cmds))
  6230. (substitute-key-definition cmd cmd map global-map)))
  6231. (suppress-keymap map)
  6232. (org-defkey map "\C-m" 'org-goto-ret)
  6233. (org-defkey map [(return)] 'org-goto-ret)
  6234. (org-defkey map [(left)] 'org-goto-left)
  6235. (org-defkey map [(right)] 'org-goto-right)
  6236. (org-defkey map [(control ?g)] 'org-goto-quit)
  6237. (org-defkey map "\C-i" 'org-cycle)
  6238. (org-defkey map [(tab)] 'org-cycle)
  6239. (org-defkey map [(down)] 'outline-next-visible-heading)
  6240. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6241. (if org-goto-auto-isearch
  6242. (if (fboundp 'define-key-after)
  6243. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6244. nil)
  6245. (org-defkey map "q" 'org-goto-quit)
  6246. (org-defkey map "n" 'outline-next-visible-heading)
  6247. (org-defkey map "p" 'outline-previous-visible-heading)
  6248. (org-defkey map "f" 'outline-forward-same-level)
  6249. (org-defkey map "b" 'outline-backward-same-level)
  6250. (org-defkey map "u" 'outline-up-heading))
  6251. (org-defkey map "/" 'org-occur)
  6252. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6253. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6254. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6255. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6256. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6257. map)))
  6258. (defconst org-goto-help
  6259. "Browse buffer copy, to find location or copy text.%s
  6260. RET=jump to location C-g=quit and return to previous location
  6261. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6262. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6263. ;; FIXME: Docstring does not mention both interfaces
  6264. (defun org-goto (&optional alternative-interface)
  6265. "Look up a different location in the current file, keeping current visibility.
  6266. When you want look-up or go to a different location in a
  6267. document, the fastest way is often to fold the entire buffer and
  6268. then dive into the tree. This method has the disadvantage, that
  6269. the previous location will be folded, which may not be what you
  6270. want.
  6271. This command works around this by showing a copy of the current
  6272. buffer in an indirect buffer, in overview mode. You can dive
  6273. into the tree in that copy, use org-occur and incremental search
  6274. to find a location. When pressing RET or `Q', the command
  6275. returns to the original buffer in which the visibility is still
  6276. unchanged. After RET it will also jump to the location selected
  6277. in the indirect buffer and expose the headline hierarchy above.
  6278. With a prefix argument, use the alternative interface: e.g. if
  6279. `org-goto-interface' is 'outline use 'outline-path-completion."
  6280. (interactive "P")
  6281. (org-goto-map)
  6282. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6283. (org-refile-use-outline-path t)
  6284. (org-refile-target-verify-function nil)
  6285. (interface
  6286. (if (not alternative-interface)
  6287. org-goto-interface
  6288. (if (eq org-goto-interface 'outline)
  6289. 'outline-path-completion
  6290. 'outline)))
  6291. (org-goto-start-pos (point))
  6292. (selected-point
  6293. (if (eq interface 'outline)
  6294. (car (org-get-location (current-buffer) org-goto-help))
  6295. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6296. (org-refile-check-position pa)
  6297. (nth 3 pa)))))
  6298. (if selected-point
  6299. (progn
  6300. (org-mark-ring-push org-goto-start-pos)
  6301. (goto-char selected-point)
  6302. (if (or (outline-invisible-p) (org-invisible-p2))
  6303. (org-show-context 'org-goto)))
  6304. (message "Quit"))))
  6305. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6306. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6307. (defvar org-goto-local-auto-isearch-map) ; defined below
  6308. (defun org-get-location (buf help)
  6309. "Let the user select a location in the Org-mode buffer BUF.
  6310. This function uses a recursive edit. It returns the selected position
  6311. or nil."
  6312. (org-no-popups
  6313. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6314. (isearch-hide-immediately nil)
  6315. (isearch-search-fun-function
  6316. (lambda () 'org-goto-local-search-headings))
  6317. (org-goto-selected-point org-goto-exit-command))
  6318. (save-excursion
  6319. (save-window-excursion
  6320. (delete-other-windows)
  6321. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6322. (org-pop-to-buffer-same-window
  6323. (condition-case nil
  6324. (make-indirect-buffer (current-buffer) "*org-goto*")
  6325. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6326. (with-output-to-temp-buffer "*Org Help*"
  6327. (princ (format help (if org-goto-auto-isearch
  6328. " Just type for auto-isearch."
  6329. " n/p/f/b/u to navigate, q to quit."))))
  6330. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6331. (setq buffer-read-only nil)
  6332. (let ((org-startup-truncated t)
  6333. (org-startup-folded nil)
  6334. (org-startup-align-all-tables nil))
  6335. (org-mode)
  6336. (org-overview))
  6337. (setq buffer-read-only t)
  6338. (if (and (boundp 'org-goto-start-pos)
  6339. (integer-or-marker-p org-goto-start-pos))
  6340. (let ((org-show-hierarchy-above t)
  6341. (org-show-siblings t)
  6342. (org-show-following-heading t))
  6343. (goto-char org-goto-start-pos)
  6344. (and (outline-invisible-p) (org-show-context)))
  6345. (goto-char (point-min)))
  6346. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6347. (message "Select location and press RET")
  6348. (use-local-map org-goto-map)
  6349. (recursive-edit)))
  6350. (kill-buffer "*org-goto*")
  6351. (cons org-goto-selected-point org-goto-exit-command))))
  6352. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6353. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6354. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6355. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6356. (defun org-goto-local-search-headings (string bound noerror)
  6357. "Search and make sure that any matches are in headlines."
  6358. (catch 'return
  6359. (while (if isearch-forward
  6360. (search-forward string bound noerror)
  6361. (search-backward string bound noerror))
  6362. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6363. (and (member :headline context)
  6364. (not (member :tags context))))
  6365. (throw 'return (point))))))
  6366. (defun org-goto-local-auto-isearch ()
  6367. "Start isearch."
  6368. (interactive)
  6369. (goto-char (point-min))
  6370. (let ((keys (this-command-keys)))
  6371. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6372. (isearch-mode t)
  6373. (isearch-process-search-char (string-to-char keys)))))
  6374. (defun org-goto-ret (&optional arg)
  6375. "Finish `org-goto' by going to the new location."
  6376. (interactive "P")
  6377. (setq org-goto-selected-point (point)
  6378. org-goto-exit-command 'return)
  6379. (throw 'exit nil))
  6380. (defun org-goto-left ()
  6381. "Finish `org-goto' by going to the new location."
  6382. (interactive)
  6383. (if (org-at-heading-p)
  6384. (progn
  6385. (beginning-of-line 1)
  6386. (setq org-goto-selected-point (point)
  6387. org-goto-exit-command 'left)
  6388. (throw 'exit nil))
  6389. (error "Not on a heading")))
  6390. (defun org-goto-right ()
  6391. "Finish `org-goto' by going to the new location."
  6392. (interactive)
  6393. (if (org-at-heading-p)
  6394. (progn
  6395. (setq org-goto-selected-point (point)
  6396. org-goto-exit-command 'right)
  6397. (throw 'exit nil))
  6398. (error "Not on a heading")))
  6399. (defun org-goto-quit ()
  6400. "Finish `org-goto' without cursor motion."
  6401. (interactive)
  6402. (setq org-goto-selected-point nil)
  6403. (setq org-goto-exit-command 'quit)
  6404. (throw 'exit nil))
  6405. ;;; Indirect buffer display of subtrees
  6406. (defvar org-indirect-dedicated-frame nil
  6407. "This is the frame being used for indirect tree display.")
  6408. (defvar org-last-indirect-buffer nil)
  6409. (defun org-tree-to-indirect-buffer (&optional arg)
  6410. "Create indirect buffer and narrow it to current subtree.
  6411. With a numerical prefix ARG, go up to this level and then take that tree.
  6412. If ARG is negative, go up that many levels.
  6413. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6414. indirect buffer previously made with this command, to avoid proliferation of
  6415. indirect buffers. However, when you call the command with a \
  6416. \\[universal-argument] prefix, or
  6417. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6418. is kept so that you can work with several indirect buffers at the same time.
  6419. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6420. \\[universal-argument] prefix also
  6421. requests that a new frame be made for the new buffer, so that the dedicated
  6422. frame is not changed."
  6423. (interactive "P")
  6424. (let ((cbuf (current-buffer))
  6425. (cwin (selected-window))
  6426. (pos (point))
  6427. beg end level heading ibuf)
  6428. (save-excursion
  6429. (org-back-to-heading t)
  6430. (when (numberp arg)
  6431. (setq level (org-outline-level))
  6432. (if (< arg 0) (setq arg (+ level arg)))
  6433. (while (> (setq level (org-outline-level)) arg)
  6434. (org-up-heading-safe)))
  6435. (setq beg (point)
  6436. heading (org-get-heading))
  6437. (org-end-of-subtree t t)
  6438. (if (org-at-heading-p) (backward-char 1))
  6439. (setq end (point)))
  6440. (if (and (buffer-live-p org-last-indirect-buffer)
  6441. (not (eq org-indirect-buffer-display 'new-frame))
  6442. (not arg))
  6443. (kill-buffer org-last-indirect-buffer))
  6444. (setq ibuf (org-get-indirect-buffer cbuf)
  6445. org-last-indirect-buffer ibuf)
  6446. (cond
  6447. ((or (eq org-indirect-buffer-display 'new-frame)
  6448. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6449. (select-frame (make-frame))
  6450. (delete-other-windows)
  6451. (org-pop-to-buffer-same-window ibuf)
  6452. (org-set-frame-title heading))
  6453. ((eq org-indirect-buffer-display 'dedicated-frame)
  6454. (raise-frame
  6455. (select-frame (or (and org-indirect-dedicated-frame
  6456. (frame-live-p org-indirect-dedicated-frame)
  6457. org-indirect-dedicated-frame)
  6458. (setq org-indirect-dedicated-frame (make-frame)))))
  6459. (delete-other-windows)
  6460. (org-pop-to-buffer-same-window ibuf)
  6461. (org-set-frame-title (concat "Indirect: " heading)))
  6462. ((eq org-indirect-buffer-display 'current-window)
  6463. (org-pop-to-buffer-same-window ibuf))
  6464. ((eq org-indirect-buffer-display 'other-window)
  6465. (pop-to-buffer ibuf))
  6466. (t (error "Invalid value")))
  6467. (if (featurep 'xemacs)
  6468. (save-excursion (org-mode) (turn-on-font-lock)))
  6469. (narrow-to-region beg end)
  6470. (show-all)
  6471. (goto-char pos)
  6472. (run-hook-with-args 'org-cycle-hook 'all)
  6473. (and (window-live-p cwin) (select-window cwin))))
  6474. (defun org-get-indirect-buffer (&optional buffer)
  6475. (setq buffer (or buffer (current-buffer)))
  6476. (let ((n 1) (base (buffer-name buffer)) bname)
  6477. (while (buffer-live-p
  6478. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6479. (setq n (1+ n)))
  6480. (condition-case nil
  6481. (make-indirect-buffer buffer bname 'clone)
  6482. (error (make-indirect-buffer buffer bname)))))
  6483. (defun org-set-frame-title (title)
  6484. "Set the title of the current frame to the string TITLE."
  6485. ;; FIXME: how to name a single frame in XEmacs???
  6486. (unless (featurep 'xemacs)
  6487. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6488. ;;;; Structure editing
  6489. ;;; Inserting headlines
  6490. (defun org-previous-line-empty-p ()
  6491. (save-excursion
  6492. (and (not (bobp))
  6493. (or (beginning-of-line 0) t)
  6494. (save-match-data
  6495. (looking-at "[ \t]*$")))))
  6496. (defun org-insert-heading (&optional force-heading invisible-ok)
  6497. "Insert a new heading or item with same depth at point.
  6498. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6499. If point is at the beginning of a headline, insert a sibling before the
  6500. current headline. If point is not at the beginning, split the line,
  6501. create the new headline with the text in the current line after point
  6502. \(but see also the variable `org-M-RET-may-split-line').
  6503. With a double prefix arg, force the heading to be inserted at the
  6504. end of the parent subtree.
  6505. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6506. This is important for non-interactive uses of the command."
  6507. (interactive "P")
  6508. (if (or (= (buffer-size) 0)
  6509. (and (not (save-excursion
  6510. (and (ignore-errors (org-back-to-heading invisible-ok))
  6511. (org-at-heading-p))))
  6512. (or force-heading (not (org-in-item-p)))))
  6513. (progn
  6514. (insert "\n* ")
  6515. (run-hooks 'org-insert-heading-hook))
  6516. (when (or force-heading (not (org-insert-item)))
  6517. (let* ((empty-line-p nil)
  6518. (level nil)
  6519. (on-heading (org-at-heading-p))
  6520. (head (save-excursion
  6521. (condition-case nil
  6522. (progn
  6523. (org-back-to-heading invisible-ok)
  6524. (when (and (not on-heading)
  6525. (featurep 'org-inlinetask)
  6526. (integerp org-inlinetask-min-level)
  6527. (>= (length (match-string 0))
  6528. org-inlinetask-min-level))
  6529. ;; Find a heading level before the inline task
  6530. (while (and (setq level (org-up-heading-safe))
  6531. (>= level org-inlinetask-min-level)))
  6532. (if (org-at-heading-p)
  6533. (org-back-to-heading invisible-ok)
  6534. (error "This should not happen")))
  6535. (setq empty-line-p (org-previous-line-empty-p))
  6536. (match-string 0))
  6537. (error "*"))))
  6538. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6539. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6540. pos hide-previous previous-pos)
  6541. (cond
  6542. ((and (org-at-heading-p) (bolp)
  6543. (or (bobp)
  6544. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6545. ;; insert before the current line
  6546. (open-line (if blank 2 1)))
  6547. ((and (bolp)
  6548. (not org-insert-heading-respect-content)
  6549. (or (bobp)
  6550. (save-excursion
  6551. (backward-char 1) (not (outline-invisible-p)))))
  6552. ;; insert right here
  6553. nil)
  6554. (t
  6555. ;; somewhere in the line
  6556. (save-excursion
  6557. (setq previous-pos (point-at-bol))
  6558. (end-of-line)
  6559. (setq hide-previous (outline-invisible-p)))
  6560. (and org-insert-heading-respect-content (org-show-subtree))
  6561. (let ((split
  6562. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6563. (save-excursion
  6564. (let ((p (point)))
  6565. (goto-char (point-at-bol))
  6566. (and (looking-at org-complex-heading-regexp)
  6567. (match-beginning 4)
  6568. (> p (match-beginning 4)))))))
  6569. tags pos)
  6570. (cond
  6571. (org-insert-heading-respect-content
  6572. (if (not (equal force-heading '(16)))
  6573. (org-end-of-subtree nil t)
  6574. (org-up-heading-safe)
  6575. (org-end-of-subtree nil t))
  6576. (when (featurep 'org-inlinetask)
  6577. (while (and (not (eobp))
  6578. (looking-at "\\(\\*+\\)[ \t]+")
  6579. (>= (length (match-string 1))
  6580. org-inlinetask-min-level))
  6581. (org-end-of-subtree nil t)))
  6582. (or (bolp) (newline))
  6583. (or (org-previous-line-empty-p)
  6584. (and blank (newline)))
  6585. (open-line 1))
  6586. ((org-at-heading-p)
  6587. (when hide-previous
  6588. (show-children)
  6589. (org-show-entry))
  6590. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6591. (setq tags (and (match-end 2) (match-string 2)))
  6592. (and (match-end 1)
  6593. (delete-region (match-beginning 1) (match-end 1)))
  6594. (setq pos (point-at-bol))
  6595. (or split (end-of-line 1))
  6596. (delete-horizontal-space)
  6597. (if (string-match "\\`\\*+\\'"
  6598. (buffer-substring (point-at-bol) (point)))
  6599. (insert " "))
  6600. (newline (if blank 2 1))
  6601. (when tags
  6602. (save-excursion
  6603. (goto-char pos)
  6604. (end-of-line 1)
  6605. (insert " " tags)
  6606. (org-set-tags nil 'align))))
  6607. (t
  6608. (or split (end-of-line 1))
  6609. (newline (if blank 2 1)))))))
  6610. (insert head) (just-one-space)
  6611. (setq pos (point))
  6612. (end-of-line 1)
  6613. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6614. (when (and org-insert-heading-respect-content hide-previous)
  6615. (save-excursion
  6616. (goto-char previous-pos)
  6617. (hide-subtree)))
  6618. (run-hooks 'org-insert-heading-hook)))))
  6619. (defun org-get-heading (&optional no-tags no-todo)
  6620. "Return the heading of the current entry, without the stars.
  6621. When NO-TAGS is non-nil, don't include tags.
  6622. When NO-TODO is non-nil, don't include TODO keywords."
  6623. (save-excursion
  6624. (org-back-to-heading t)
  6625. (cond
  6626. ((and no-tags no-todo)
  6627. (looking-at org-complex-heading-regexp)
  6628. (match-string 4))
  6629. (no-tags
  6630. (looking-at (concat org-outline-regexp
  6631. "\\(.*?\\)"
  6632. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6633. (match-string 1))
  6634. (no-todo
  6635. (looking-at org-todo-line-regexp)
  6636. (match-string 3))
  6637. (t (looking-at org-heading-regexp)
  6638. (match-string 2)))))
  6639. (defvar orgstruct-mode) ; defined below
  6640. (defun org-heading-components ()
  6641. "Return the components of the current heading.
  6642. This is a list with the following elements:
  6643. - the level as an integer
  6644. - the reduced level, different if `org-odd-levels-only' is set.
  6645. - the TODO keyword, or nil
  6646. - the priority character, like ?A, or nil if no priority is given
  6647. - the headline text itself, or the tags string if no headline text
  6648. - the tags string, or nil."
  6649. (save-excursion
  6650. (org-back-to-heading t)
  6651. (if (let (case-fold-search)
  6652. (looking-at
  6653. (if orgstruct-mode
  6654. org-heading-regexp
  6655. org-complex-heading-regexp)))
  6656. (if orgstruct-mode
  6657. (list (length (match-string 1))
  6658. (org-reduced-level (length (match-string 1)))
  6659. nil
  6660. nil
  6661. (match-string 2)
  6662. nil)
  6663. (list (length (match-string 1))
  6664. (org-reduced-level (length (match-string 1)))
  6665. (org-match-string-no-properties 2)
  6666. (and (match-end 3) (aref (match-string 3) 2))
  6667. (org-match-string-no-properties 4)
  6668. (org-match-string-no-properties 5))))))
  6669. (defun org-get-entry ()
  6670. "Get the entry text, after heading, entire subtree."
  6671. (save-excursion
  6672. (org-back-to-heading t)
  6673. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6674. (defun org-insert-heading-after-current ()
  6675. "Insert a new heading with same level as current, after current subtree."
  6676. (interactive)
  6677. (org-back-to-heading)
  6678. (org-insert-heading)
  6679. (org-move-subtree-down)
  6680. (end-of-line 1))
  6681. (defun org-insert-heading-respect-content (invisible-ok)
  6682. "Insert heading with `org-insert-heading-respect-content' set to t."
  6683. (interactive "P")
  6684. (let ((org-insert-heading-respect-content t))
  6685. (org-insert-heading t invisible-ok)))
  6686. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6687. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6688. (interactive "P")
  6689. (let ((org-insert-heading-respect-content t))
  6690. (org-insert-todo-heading force-state t)))
  6691. (defun org-insert-todo-heading (arg &optional force-heading)
  6692. "Insert a new heading with the same level and TODO state as current heading.
  6693. If the heading has no TODO state, or if the state is DONE, use the first
  6694. state (TODO by default). Also one prefix arg, force first state. With two
  6695. prefix args, force inserting at the end of the parent subtree."
  6696. (interactive "P")
  6697. (when (or force-heading (not (org-insert-item 'checkbox)))
  6698. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6699. force-heading))
  6700. (save-excursion
  6701. (org-back-to-heading)
  6702. (outline-previous-heading)
  6703. (looking-at org-todo-line-regexp))
  6704. (let*
  6705. ((new-mark-x
  6706. (if (or arg
  6707. (not (match-beginning 2))
  6708. (member (match-string 2) org-done-keywords))
  6709. (car org-todo-keywords-1)
  6710. (match-string 2)))
  6711. (new-mark
  6712. (or
  6713. (run-hook-with-args-until-success
  6714. 'org-todo-get-default-hook new-mark-x nil)
  6715. new-mark-x)))
  6716. (beginning-of-line 1)
  6717. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6718. (if org-treat-insert-todo-heading-as-state-change
  6719. (org-todo new-mark)
  6720. (insert new-mark " "))))
  6721. (when org-provide-todo-statistics
  6722. (org-update-parent-todo-statistics))))
  6723. (defun org-insert-subheading (arg)
  6724. "Insert a new subheading and demote it.
  6725. Works for outline headings and for plain lists alike."
  6726. (interactive "P")
  6727. (org-insert-heading arg)
  6728. (cond
  6729. ((org-at-heading-p) (org-do-demote))
  6730. ((org-at-item-p) (org-indent-item))))
  6731. (defun org-insert-todo-subheading (arg)
  6732. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6733. Works for outline headings and for plain lists alike."
  6734. (interactive "P")
  6735. (org-insert-todo-heading arg)
  6736. (cond
  6737. ((org-at-heading-p) (org-do-demote))
  6738. ((org-at-item-p) (org-indent-item))))
  6739. ;;; Promotion and Demotion
  6740. (defvar org-after-demote-entry-hook nil
  6741. "Hook run after an entry has been demoted.
  6742. The cursor will be at the beginning of the entry.
  6743. When a subtree is being demoted, the hook will be called for each node.")
  6744. (defvar org-after-promote-entry-hook nil
  6745. "Hook run after an entry has been promoted.
  6746. The cursor will be at the beginning of the entry.
  6747. When a subtree is being promoted, the hook will be called for each node.")
  6748. (defun org-promote-subtree ()
  6749. "Promote the entire subtree.
  6750. See also `org-promote'."
  6751. (interactive)
  6752. (save-excursion
  6753. (org-with-limited-levels (org-map-tree 'org-promote)))
  6754. (org-fix-position-after-promote))
  6755. (defun org-demote-subtree ()
  6756. "Demote the entire subtree. See `org-demote'.
  6757. See also `org-promote'."
  6758. (interactive)
  6759. (save-excursion
  6760. (org-with-limited-levels (org-map-tree 'org-demote)))
  6761. (org-fix-position-after-promote))
  6762. (defun org-do-promote ()
  6763. "Promote the current heading higher up the tree.
  6764. If the region is active in `transient-mark-mode', promote all headings
  6765. in the region."
  6766. (interactive)
  6767. (save-excursion
  6768. (if (org-region-active-p)
  6769. (org-map-region 'org-promote (region-beginning) (region-end))
  6770. (org-promote)))
  6771. (org-fix-position-after-promote))
  6772. (defun org-do-demote ()
  6773. "Demote the current heading lower down the tree.
  6774. If the region is active in `transient-mark-mode', demote all headings
  6775. in the region."
  6776. (interactive)
  6777. (save-excursion
  6778. (if (org-region-active-p)
  6779. (org-map-region 'org-demote (region-beginning) (region-end))
  6780. (org-demote)))
  6781. (org-fix-position-after-promote))
  6782. (defun org-fix-position-after-promote ()
  6783. "Make sure that after pro/demotion cursor position is right."
  6784. (let ((pos (point)))
  6785. (when (save-excursion
  6786. (beginning-of-line 1)
  6787. (looking-at org-todo-line-regexp)
  6788. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6789. (cond ((eobp) (insert " "))
  6790. ((eolp) (insert " "))
  6791. ((equal (char-after) ?\ ) (forward-char 1))))))
  6792. (defun org-current-level ()
  6793. "Return the level of the current entry, or nil if before the first headline.
  6794. The level is the number of stars at the beginning of the headline."
  6795. (save-excursion
  6796. (org-with-limited-levels
  6797. (if (ignore-errors (org-back-to-heading t))
  6798. (funcall outline-level)))))
  6799. (defun org-get-previous-line-level ()
  6800. "Return the outline depth of the last headline before the current line.
  6801. Returns 0 for the first headline in the buffer, and nil if before the
  6802. first headline."
  6803. (let ((current-level (org-current-level))
  6804. (prev-level (when (> (line-number-at-pos) 1)
  6805. (save-excursion
  6806. (beginning-of-line 0)
  6807. (org-current-level)))))
  6808. (cond ((null current-level) nil) ; Before first headline
  6809. ((null prev-level) 0) ; At first headline
  6810. (prev-level))))
  6811. (defun org-reduced-level (l)
  6812. "Compute the effective level of a heading.
  6813. This takes into account the setting of `org-odd-levels-only'."
  6814. (cond
  6815. ((zerop l) 0)
  6816. (org-odd-levels-only (1+ (floor (/ l 2))))
  6817. (t l)))
  6818. (defun org-level-increment ()
  6819. "Return the number of stars that will be added or removed at a
  6820. time to headlines when structure editing, based on the value of
  6821. `org-odd-levels-only'."
  6822. (if org-odd-levels-only 2 1))
  6823. (defun org-get-valid-level (level &optional change)
  6824. "Rectify a level change under the influence of `org-odd-levels-only'
  6825. LEVEL is a current level, CHANGE is by how much the level should be
  6826. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6827. even level numbers will become the next higher odd number."
  6828. (if org-odd-levels-only
  6829. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6830. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6831. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6832. (max 1 (+ level (or change 0)))))
  6833. (if (boundp 'define-obsolete-function-alias)
  6834. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6835. (define-obsolete-function-alias 'org-get-legal-level
  6836. 'org-get-valid-level)
  6837. (define-obsolete-function-alias 'org-get-legal-level
  6838. 'org-get-valid-level "23.1")))
  6839. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6840. ;; ̀org-with-limited-levels'
  6841. (defun org-promote ()
  6842. "Promote the current heading higher up the tree.
  6843. If the region is active in `transient-mark-mode', promote all headings
  6844. in the region."
  6845. (org-back-to-heading t)
  6846. (let* ((level (save-match-data (funcall outline-level)))
  6847. (after-change-functions (remove 'flyspell-after-change-function
  6848. after-change-functions))
  6849. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6850. (diff (abs (- level (length up-head) -1))))
  6851. (cond ((and (= level 1) org-called-with-limited-levels
  6852. org-allow-promoting-top-level-subtree)
  6853. (replace-match "# " nil t))
  6854. ((= level 1)
  6855. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6856. (t (replace-match up-head nil t)))
  6857. ;; Fixup tag positioning
  6858. (unless (= level 1)
  6859. (and org-auto-align-tags (org-set-tags nil t))
  6860. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6861. (run-hooks 'org-after-promote-entry-hook)))
  6862. (defun org-demote ()
  6863. "Demote the current heading lower down the tree.
  6864. If the region is active in `transient-mark-mode', demote all headings
  6865. in the region."
  6866. (org-back-to-heading t)
  6867. (let* ((level (save-match-data (funcall outline-level)))
  6868. (after-change-functions (remove 'flyspell-after-change-function
  6869. after-change-functions))
  6870. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6871. (diff (abs (- level (length down-head) -1))))
  6872. (replace-match down-head nil t)
  6873. ;; Fixup tag positioning
  6874. (and org-auto-align-tags (org-set-tags nil t))
  6875. (if org-adapt-indentation (org-fixup-indentation diff))
  6876. (run-hooks 'org-after-demote-entry-hook)))
  6877. (defun org-cycle-level ()
  6878. "Cycle the level of an empty headline through possible states.
  6879. This goes first to child, then to parent, level, then up the hierarchy.
  6880. After top level, it switches back to sibling level."
  6881. (interactive)
  6882. (let ((org-adapt-indentation nil))
  6883. (when (org-point-at-end-of-empty-headline)
  6884. (setq this-command 'org-cycle-level) ; Only needed for caching
  6885. (let ((cur-level (org-current-level))
  6886. (prev-level (org-get-previous-line-level)))
  6887. (cond
  6888. ;; If first headline in file, promote to top-level.
  6889. ((= prev-level 0)
  6890. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6891. do (org-do-promote)))
  6892. ;; If same level as prev, demote one.
  6893. ((= prev-level cur-level)
  6894. (org-do-demote))
  6895. ;; If parent is top-level, promote to top level if not already.
  6896. ((= prev-level 1)
  6897. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6898. do (org-do-promote)))
  6899. ;; If top-level, return to prev-level.
  6900. ((= cur-level 1)
  6901. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6902. do (org-do-demote)))
  6903. ;; If less than prev-level, promote one.
  6904. ((< cur-level prev-level)
  6905. (org-do-promote))
  6906. ;; If deeper than prev-level, promote until higher than
  6907. ;; prev-level.
  6908. ((> cur-level prev-level)
  6909. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6910. do (org-do-promote))))
  6911. t))))
  6912. (defun org-map-tree (fun)
  6913. "Call FUN for every heading underneath the current one."
  6914. (org-back-to-heading)
  6915. (let ((level (funcall outline-level)))
  6916. (save-excursion
  6917. (funcall fun)
  6918. (while (and (progn
  6919. (outline-next-heading)
  6920. (> (funcall outline-level) level))
  6921. (not (eobp)))
  6922. (funcall fun)))))
  6923. (defun org-map-region (fun beg end)
  6924. "Call FUN for every heading between BEG and END."
  6925. (let ((org-ignore-region t))
  6926. (save-excursion
  6927. (setq end (copy-marker end))
  6928. (goto-char beg)
  6929. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6930. (< (point) end))
  6931. (funcall fun))
  6932. (while (and (progn
  6933. (outline-next-heading)
  6934. (< (point) end))
  6935. (not (eobp)))
  6936. (funcall fun)))))
  6937. (defvar org-property-end-re) ; silence byte-compiler
  6938. (defun org-fixup-indentation (diff)
  6939. "Change the indentation in the current entry by DIFF.
  6940. However, if any line in the current entry has no indentation, or if it
  6941. would end up with no indentation after the change, nothing at all is done."
  6942. (save-excursion
  6943. (let ((end (save-excursion (outline-next-heading)
  6944. (point-marker)))
  6945. (prohibit (if (> diff 0)
  6946. "^\\S-"
  6947. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6948. col)
  6949. (unless (save-excursion (end-of-line 1)
  6950. (re-search-forward prohibit end t))
  6951. (while (and (< (point) end)
  6952. (re-search-forward "^[ \t]+" end t))
  6953. (goto-char (match-end 0))
  6954. (setq col (current-column))
  6955. (if (< diff 0) (replace-match ""))
  6956. (org-indent-to-column (+ diff col))))
  6957. (move-marker end nil))))
  6958. (defun org-convert-to-odd-levels ()
  6959. "Convert an org-mode file with all levels allowed to one with odd levels.
  6960. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6961. level 5 etc."
  6962. (interactive)
  6963. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6964. (let ((outline-level 'org-outline-level)
  6965. (org-odd-levels-only nil) n)
  6966. (save-excursion
  6967. (goto-char (point-min))
  6968. (while (re-search-forward "^\\*\\*+ " nil t)
  6969. (setq n (- (length (match-string 0)) 2))
  6970. (while (>= (setq n (1- n)) 0)
  6971. (org-demote))
  6972. (end-of-line 1))))))
  6973. (defun org-convert-to-oddeven-levels ()
  6974. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6975. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6976. file contains a section with an even level, conversion would
  6977. destroy the structure of the file. An error is signaled in this
  6978. case."
  6979. (interactive)
  6980. (goto-char (point-min))
  6981. ;; First check if there are no even levels
  6982. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6983. (org-show-context t)
  6984. (error "Not all levels are odd in this file. Conversion not possible"))
  6985. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6986. (let ((outline-regexp org-outline-regexp)
  6987. (outline-level 'org-outline-level)
  6988. (org-odd-levels-only nil) n)
  6989. (save-excursion
  6990. (goto-char (point-min))
  6991. (while (re-search-forward "^\\*\\*+ " nil t)
  6992. (setq n (/ (1- (length (match-string 0))) 2))
  6993. (while (>= (setq n (1- n)) 0)
  6994. (org-promote))
  6995. (end-of-line 1))))))
  6996. (defun org-tr-level (n)
  6997. "Make N odd if required."
  6998. (if org-odd-levels-only (1+ (/ n 2)) n))
  6999. ;;; Vertical tree motion, cutting and pasting of subtrees
  7000. (defun org-move-subtree-up (&optional arg)
  7001. "Move the current subtree up past ARG headlines of the same level."
  7002. (interactive "p")
  7003. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7004. (defun org-move-subtree-down (&optional arg)
  7005. "Move the current subtree down past ARG headlines of the same level."
  7006. (interactive "p")
  7007. (setq arg (prefix-numeric-value arg))
  7008. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7009. 'org-get-last-sibling))
  7010. (ins-point (make-marker))
  7011. (cnt (abs arg))
  7012. (col (current-column))
  7013. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7014. ;; Select the tree
  7015. (org-back-to-heading)
  7016. (setq beg0 (point))
  7017. (save-excursion
  7018. (setq ne-beg (org-back-over-empty-lines))
  7019. (setq beg (point)))
  7020. (save-match-data
  7021. (save-excursion (outline-end-of-heading)
  7022. (setq folded (outline-invisible-p)))
  7023. (outline-end-of-subtree))
  7024. (outline-next-heading)
  7025. (setq ne-end (org-back-over-empty-lines))
  7026. (setq end (point))
  7027. (goto-char beg0)
  7028. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7029. ;; include less whitespace
  7030. (save-excursion
  7031. (goto-char beg)
  7032. (forward-line (- ne-beg ne-end))
  7033. (setq beg (point))))
  7034. ;; Find insertion point, with error handling
  7035. (while (> cnt 0)
  7036. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7037. (progn (goto-char beg0)
  7038. (error "Cannot move past superior level or buffer limit")))
  7039. (setq cnt (1- cnt)))
  7040. (if (> arg 0)
  7041. ;; Moving forward - still need to move over subtree
  7042. (progn (org-end-of-subtree t t)
  7043. (save-excursion
  7044. (org-back-over-empty-lines)
  7045. (or (bolp) (newline)))))
  7046. (setq ne-ins (org-back-over-empty-lines))
  7047. (move-marker ins-point (point))
  7048. (setq txt (buffer-substring beg end))
  7049. (org-save-markers-in-region beg end)
  7050. (delete-region beg end)
  7051. (org-remove-empty-overlays-at beg)
  7052. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7053. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7054. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7055. (let ((bbb (point)))
  7056. (insert-before-markers txt)
  7057. (org-reinstall-markers-in-region bbb)
  7058. (move-marker ins-point bbb))
  7059. (or (bolp) (insert "\n"))
  7060. (setq ins-end (point))
  7061. (goto-char ins-point)
  7062. (org-skip-whitespace)
  7063. (when (and (< arg 0)
  7064. (org-first-sibling-p)
  7065. (> ne-ins ne-beg))
  7066. ;; Move whitespace back to beginning
  7067. (save-excursion
  7068. (goto-char ins-end)
  7069. (let ((kill-whole-line t))
  7070. (kill-line (- ne-ins ne-beg)) (point)))
  7071. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7072. (move-marker ins-point nil)
  7073. (if folded
  7074. (hide-subtree)
  7075. (org-show-entry)
  7076. (show-children)
  7077. (org-cycle-hide-drawers 'children))
  7078. (org-clean-visibility-after-subtree-move)
  7079. ;; move back to the initial column we were at
  7080. (move-to-column col)))
  7081. (defvar org-subtree-clip ""
  7082. "Clipboard for cut and paste of subtrees.
  7083. This is actually only a copy of the kill, because we use the normal kill
  7084. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7085. (defvar org-subtree-clip-folded nil
  7086. "Was the last copied subtree folded?
  7087. This is used to fold the tree back after pasting.")
  7088. (defun org-cut-subtree (&optional n)
  7089. "Cut the current subtree into the clipboard.
  7090. With prefix arg N, cut this many sequential subtrees.
  7091. This is a short-hand for marking the subtree and then cutting it."
  7092. (interactive "p")
  7093. (org-copy-subtree n 'cut))
  7094. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7095. "Cut the current subtree into the clipboard.
  7096. With prefix arg N, cut this many sequential subtrees.
  7097. This is a short-hand for marking the subtree and then copying it.
  7098. If CUT is non-nil, actually cut the subtree.
  7099. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7100. of some markers in the region, even if CUT is non-nil. This is
  7101. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7102. (interactive "p")
  7103. (let (beg end folded (beg0 (point)))
  7104. (if (org-called-interactively-p 'any)
  7105. (org-back-to-heading nil) ; take what looks like a subtree
  7106. (org-back-to-heading t)) ; take what is really there
  7107. (setq beg (point))
  7108. (skip-chars-forward " \t\r\n")
  7109. (save-match-data
  7110. (if nosubtrees
  7111. (outline-next-heading)
  7112. (save-excursion (outline-end-of-heading)
  7113. (setq folded (outline-invisible-p)))
  7114. (condition-case nil
  7115. (org-forward-heading-same-level (1- n) t)
  7116. (error nil))
  7117. (org-end-of-subtree t t)))
  7118. (setq end (point))
  7119. (goto-char beg0)
  7120. (when (> end beg)
  7121. (setq org-subtree-clip-folded folded)
  7122. (when (or cut force-store-markers)
  7123. (org-save-markers-in-region beg end))
  7124. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7125. (setq org-subtree-clip (current-kill 0))
  7126. (message "%s: Subtree(s) with %d characters"
  7127. (if cut "Cut" "Copied")
  7128. (length org-subtree-clip)))))
  7129. (defun org-paste-subtree (&optional level tree for-yank)
  7130. "Paste the clipboard as a subtree, with modification of headline level.
  7131. The entire subtree is promoted or demoted in order to match a new headline
  7132. level.
  7133. If the cursor is at the beginning of a headline, the same level as
  7134. that headline is used to paste the tree.
  7135. If not, the new level is derived from the *visible* headings
  7136. before and after the insertion point, and taken to be the inferior headline
  7137. level of the two. So if the previous visible heading is level 3 and the
  7138. next is level 4 (or vice versa), level 4 will be used for insertion.
  7139. This makes sure that the subtree remains an independent subtree and does
  7140. not swallow low level entries.
  7141. You can also force a different level, either by using a numeric prefix
  7142. argument, or by inserting the heading marker by hand. For example, if the
  7143. cursor is after \"*****\", then the tree will be shifted to level 5.
  7144. If optional TREE is given, use this text instead of the kill ring.
  7145. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7146. move back over whitespace before inserting, and move point to the end of
  7147. the inserted text when done."
  7148. (interactive "P")
  7149. (setq tree (or tree (and kill-ring (current-kill 0))))
  7150. (unless (org-kill-is-subtree-p tree)
  7151. (error "%s"
  7152. (substitute-command-keys
  7153. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7154. (org-with-limited-levels
  7155. (let* ((visp (not (outline-invisible-p)))
  7156. (txt tree)
  7157. (^re_ "\\(\\*+\\)[ \t]*")
  7158. (old-level (if (string-match org-outline-regexp-bol txt)
  7159. (- (match-end 0) (match-beginning 0) 1)
  7160. -1))
  7161. (force-level (cond (level (prefix-numeric-value level))
  7162. ((and (looking-at "[ \t]*$")
  7163. (string-match
  7164. "^\\*+$" (buffer-substring
  7165. (point-at-bol) (point))))
  7166. (- (match-end 1) (match-beginning 1)))
  7167. ((and (bolp)
  7168. (looking-at org-outline-regexp))
  7169. (- (match-end 0) (point) 1))))
  7170. (previous-level (save-excursion
  7171. (condition-case nil
  7172. (progn
  7173. (outline-previous-visible-heading 1)
  7174. (if (looking-at ^re_)
  7175. (- (match-end 0) (match-beginning 0) 1)
  7176. 1))
  7177. (error 1))))
  7178. (next-level (save-excursion
  7179. (condition-case nil
  7180. (progn
  7181. (or (looking-at org-outline-regexp)
  7182. (outline-next-visible-heading 1))
  7183. (if (looking-at ^re_)
  7184. (- (match-end 0) (match-beginning 0) 1)
  7185. 1))
  7186. (error 1))))
  7187. (new-level (or force-level (max previous-level next-level)))
  7188. (shift (if (or (= old-level -1)
  7189. (= new-level -1)
  7190. (= old-level new-level))
  7191. 0
  7192. (- new-level old-level)))
  7193. (delta (if (> shift 0) -1 1))
  7194. (func (if (> shift 0) 'org-demote 'org-promote))
  7195. (org-odd-levels-only nil)
  7196. beg end newend)
  7197. ;; Remove the forced level indicator
  7198. (if force-level
  7199. (delete-region (point-at-bol) (point)))
  7200. ;; Paste
  7201. (beginning-of-line (if (bolp) 1 2))
  7202. (setq beg (point))
  7203. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7204. (insert-before-markers txt)
  7205. (unless (string-match "\n\\'" txt) (insert "\n"))
  7206. (setq newend (point))
  7207. (org-reinstall-markers-in-region beg)
  7208. (setq end (point))
  7209. (goto-char beg)
  7210. (skip-chars-forward " \t\n\r")
  7211. (setq beg (point))
  7212. (if (and (outline-invisible-p) visp)
  7213. (save-excursion (outline-show-heading)))
  7214. ;; Shift if necessary
  7215. (unless (= shift 0)
  7216. (save-restriction
  7217. (narrow-to-region beg end)
  7218. (while (not (= shift 0))
  7219. (org-map-region func (point-min) (point-max))
  7220. (setq shift (+ delta shift)))
  7221. (goto-char (point-min))
  7222. (setq newend (point-max))))
  7223. (when (or (org-called-interactively-p 'interactive) for-yank)
  7224. (message "Clipboard pasted as level %d subtree" new-level))
  7225. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7226. kill-ring
  7227. (eq org-subtree-clip (current-kill 0))
  7228. org-subtree-clip-folded)
  7229. ;; The tree was folded before it was killed/copied
  7230. (hide-subtree))
  7231. (and for-yank (goto-char newend)))))
  7232. (defun org-kill-is-subtree-p (&optional txt)
  7233. "Check if the current kill is an outline subtree, or a set of trees.
  7234. Returns nil if kill does not start with a headline, or if the first
  7235. headline level is not the largest headline level in the tree.
  7236. So this will actually accept several entries of equal levels as well,
  7237. which is OK for `org-paste-subtree'.
  7238. If optional TXT is given, check this string instead of the current kill."
  7239. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7240. (re (org-get-limited-outline-regexp))
  7241. (^re (concat "^" re))
  7242. (start-level (and kill
  7243. (string-match
  7244. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7245. kill)
  7246. (- (match-end 2) (match-beginning 2) 1)))
  7247. (start (1+ (or (match-beginning 2) -1))))
  7248. (if (not start-level)
  7249. (progn
  7250. nil) ;; does not even start with a heading
  7251. (catch 'exit
  7252. (while (setq start (string-match ^re kill (1+ start)))
  7253. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7254. (throw 'exit nil)))
  7255. t))))
  7256. (defvar org-markers-to-move nil
  7257. "Markers that should be moved with a cut-and-paste operation.
  7258. Those markers are stored together with their positions relative to
  7259. the start of the region.")
  7260. (defun org-save-markers-in-region (beg end)
  7261. "Check markers in region.
  7262. If these markers are between BEG and END, record their position relative
  7263. to BEG, so that after moving the block of text, we can put the markers back
  7264. into place.
  7265. This function gets called just before an entry or tree gets cut from the
  7266. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7267. called immediately, to move the markers with the entries."
  7268. (setq org-markers-to-move nil)
  7269. (when (featurep 'org-clock)
  7270. (org-clock-save-markers-for-cut-and-paste beg end))
  7271. (when (featurep 'org-agenda)
  7272. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7273. (defun org-check-and-save-marker (marker beg end)
  7274. "Check if MARKER is between BEG and END.
  7275. If yes, remember the marker and the distance to BEG."
  7276. (when (and (marker-buffer marker)
  7277. (equal (marker-buffer marker) (current-buffer)))
  7278. (if (and (>= marker beg) (< marker end))
  7279. (push (cons marker (- marker beg)) org-markers-to-move))))
  7280. (defun org-reinstall-markers-in-region (beg)
  7281. "Move all remembered markers to their position relative to BEG."
  7282. (mapc (lambda (x)
  7283. (move-marker (car x) (+ beg (cdr x))))
  7284. org-markers-to-move)
  7285. (setq org-markers-to-move nil))
  7286. (defun org-narrow-to-subtree ()
  7287. "Narrow buffer to the current subtree."
  7288. (interactive)
  7289. (save-excursion
  7290. (save-match-data
  7291. (org-with-limited-levels
  7292. (narrow-to-region
  7293. (progn (org-back-to-heading t) (point))
  7294. (progn (org-end-of-subtree t t)
  7295. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7296. (point)))))))
  7297. (defun org-narrow-to-block ()
  7298. "Narrow buffer to the current block."
  7299. (interactive)
  7300. (let* ((case-fold-search t)
  7301. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7302. "^[ \t]*#\\+end_.*")))
  7303. (if blockp
  7304. (narrow-to-region (car blockp) (cdr blockp))
  7305. (error "Not in a block"))))
  7306. (eval-when-compile
  7307. (defvar org-property-drawer-re))
  7308. (defvar org-property-start-re) ;; defined below
  7309. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7310. "Clone the task (subtree) at point N times.
  7311. The clones will be inserted as siblings.
  7312. In interactive use, the user will be prompted for the number of
  7313. clones to be produced, and for a time SHIFT, which may be a
  7314. repeater as used in time stamps, for example `+3d'.
  7315. When a valid repeater is given and the entry contains any time
  7316. stamps, the clones will become a sequence in time, with time
  7317. stamps in the subtree shifted for each clone produced. If SHIFT
  7318. is nil or the empty string, time stamps will be left alone. The
  7319. ID property of the original subtree is removed.
  7320. If the original subtree did contain time stamps with a repeater,
  7321. the following will happen:
  7322. - the repeater will be removed in each clone
  7323. - an additional clone will be produced, with the current, unshifted
  7324. date(s) in the entry.
  7325. - the original entry will be placed *after* all the clones, with
  7326. repeater intact.
  7327. - the start days in the repeater in the original entry will be shifted
  7328. to past the last clone.
  7329. In this way you can spell out a number of instances of a repeating task,
  7330. and still retain the repeater to cover future instances of the task."
  7331. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7332. (let (beg end template task idprop
  7333. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7334. (drawer-re org-drawer-regexp))
  7335. (if (not (and (integerp n) (> n 0)))
  7336. (error "Invalid number of replications %s" n))
  7337. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7338. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7339. shift)))
  7340. (error "Invalid shift specification %s" shift))
  7341. (when doshift
  7342. (setq shift-n (string-to-number (match-string 1 shift))
  7343. shift-what (cdr (assoc (match-string 2 shift)
  7344. '(("d" . day) ("w" . week)
  7345. ("m" . month) ("y" . year))))))
  7346. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7347. (setq nmin 1 nmax n)
  7348. (org-back-to-heading t)
  7349. (setq beg (point))
  7350. (setq idprop (org-entry-get nil "ID"))
  7351. (org-end-of-subtree t t)
  7352. (or (bolp) (insert "\n"))
  7353. (setq end (point))
  7354. (setq template (buffer-substring beg end))
  7355. (when (and doshift
  7356. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7357. (delete-region beg end)
  7358. (setq end beg)
  7359. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7360. (goto-char end)
  7361. (loop for n from nmin to nmax do
  7362. ;; prepare clone
  7363. (with-temp-buffer
  7364. (insert template)
  7365. (org-mode)
  7366. (goto-char (point-min))
  7367. (org-show-subtree)
  7368. (and idprop (if org-clone-delete-id
  7369. (org-entry-delete nil "ID")
  7370. (org-id-get-create t)))
  7371. (unless (= n 0)
  7372. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7373. (kill-whole-line))
  7374. (goto-char (point-min))
  7375. (while (re-search-forward drawer-re nil t)
  7376. (mapc (lambda (d)
  7377. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7378. (goto-char (point-min))
  7379. (when doshift
  7380. (while (re-search-forward org-ts-regexp-both nil t)
  7381. (org-timestamp-change (* n shift-n) shift-what))
  7382. (unless (= n n-no-remove)
  7383. (goto-char (point-min))
  7384. (while (re-search-forward org-ts-regexp nil t)
  7385. (save-excursion
  7386. (goto-char (match-beginning 0))
  7387. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7388. (delete-region (match-beginning 1) (match-end 1)))))))
  7389. (setq task (buffer-string)))
  7390. (insert task))
  7391. (goto-char beg)))
  7392. ;;; Outline Sorting
  7393. (defun org-sort (with-case)
  7394. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7395. Optional argument WITH-CASE means sort case-sensitively."
  7396. (interactive "P")
  7397. (cond
  7398. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7399. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7400. (t
  7401. (org-call-with-arg 'org-sort-entries with-case))))
  7402. (defun org-sort-remove-invisible (s)
  7403. (remove-text-properties 0 (length s) org-rm-props s)
  7404. (while (string-match org-bracket-link-regexp s)
  7405. (setq s (replace-match (if (match-end 2)
  7406. (match-string 3 s)
  7407. (match-string 1 s)) t t s)))
  7408. s)
  7409. (defvar org-priority-regexp) ; defined later in the file
  7410. (defvar org-after-sorting-entries-or-items-hook nil
  7411. "Hook that is run after a bunch of entries or items have been sorted.
  7412. When children are sorted, the cursor is in the parent line when this
  7413. hook gets called. When a region or a plain list is sorted, the cursor
  7414. will be in the first entry of the sorted region/list.")
  7415. (defun org-sort-entries
  7416. (&optional with-case sorting-type getkey-func compare-func property)
  7417. "Sort entries on a certain level of an outline tree.
  7418. If there is an active region, the entries in the region are sorted.
  7419. Else, if the cursor is before the first entry, sort the top-level items.
  7420. Else, the children of the entry at point are sorted.
  7421. Sorting can be alphabetically, numerically, by date/time as given by
  7422. a time stamp, by a property or by priority.
  7423. The command prompts for the sorting type unless it has been given to the
  7424. function through the SORTING-TYPE argument, which needs to be a character,
  7425. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7426. precise meaning of each character:
  7427. n Numerically, by converting the beginning of the entry/item to a number.
  7428. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7429. o By order of TODO keywords.
  7430. t By date/time, either the first active time stamp in the entry, or, if
  7431. none exist, by the first inactive one.
  7432. s By the scheduled date/time.
  7433. d By deadline date/time.
  7434. c By creation time, which is assumed to be the first inactive time stamp
  7435. at the beginning of a line.
  7436. p By priority according to the cookie.
  7437. r By the value of a property.
  7438. Capital letters will reverse the sort order.
  7439. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7440. called with point at the beginning of the record. It must return either
  7441. a string or a number that should serve as the sorting key for that record.
  7442. Comparing entries ignores case by default. However, with an optional argument
  7443. WITH-CASE, the sorting considers case as well."
  7444. (interactive "P")
  7445. (let ((case-func (if with-case 'identity 'downcase))
  7446. (cmstr
  7447. ;; The clock marker is lost when using `sort-subr', let's
  7448. ;; store the clocking string.
  7449. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7450. (save-excursion
  7451. (goto-char org-clock-marker)
  7452. (looking-back "^.*") (match-string-no-properties 0))))
  7453. start beg end stars re re2
  7454. txt what tmp)
  7455. ;; Find beginning and end of region to sort
  7456. (cond
  7457. ((org-region-active-p)
  7458. ;; we will sort the region
  7459. (setq end (region-end)
  7460. what "region")
  7461. (goto-char (region-beginning))
  7462. (if (not (org-at-heading-p)) (outline-next-heading))
  7463. (setq start (point)))
  7464. ((or (org-at-heading-p)
  7465. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7466. ;; we will sort the children of the current headline
  7467. (org-back-to-heading)
  7468. (setq start (point)
  7469. end (progn (org-end-of-subtree t t)
  7470. (or (bolp) (insert "\n"))
  7471. (org-back-over-empty-lines)
  7472. (point))
  7473. what "children")
  7474. (goto-char start)
  7475. (show-subtree)
  7476. (outline-next-heading))
  7477. (t
  7478. ;; we will sort the top-level entries in this file
  7479. (goto-char (point-min))
  7480. (or (org-at-heading-p) (outline-next-heading))
  7481. (setq start (point))
  7482. (goto-char (point-max))
  7483. (beginning-of-line 1)
  7484. (when (looking-at ".*?\\S-")
  7485. ;; File ends in a non-white line
  7486. (end-of-line 1)
  7487. (insert "\n"))
  7488. (setq end (point-max))
  7489. (setq what "top-level")
  7490. (goto-char start)
  7491. (show-all)))
  7492. (setq beg (point))
  7493. (if (>= beg end) (error "Nothing to sort"))
  7494. (looking-at "\\(\\*+\\)")
  7495. (setq stars (match-string 1)
  7496. re (concat "^" (regexp-quote stars) " +")
  7497. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7498. txt (buffer-substring beg end))
  7499. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7500. (if (and (not (equal stars "*")) (string-match re2 txt))
  7501. (error "Region to sort contains a level above the first entry"))
  7502. (unless sorting-type
  7503. (message
  7504. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7505. [t]ime [s]cheduled [d]eadline [c]reated
  7506. A/N/P/R/O/F/T/S/D/C means reversed:"
  7507. what)
  7508. (setq sorting-type (read-char-exclusive))
  7509. (unless getkey-func
  7510. (and (= (downcase sorting-type) ?f)
  7511. (setq getkey-func
  7512. (org-icompleting-read "Sort using function: "
  7513. obarray 'fboundp t nil nil))
  7514. (setq getkey-func (intern getkey-func))))
  7515. (and (= (downcase sorting-type) ?r)
  7516. (not property)
  7517. (setq property
  7518. (org-icompleting-read "Property: "
  7519. (mapcar 'list (org-buffer-property-keys t))
  7520. nil t))))
  7521. (message "Sorting entries...")
  7522. (save-restriction
  7523. (narrow-to-region start end)
  7524. (let ((dcst (downcase sorting-type))
  7525. (case-fold-search nil)
  7526. (now (current-time)))
  7527. (sort-subr
  7528. (/= dcst sorting-type)
  7529. ;; This function moves to the beginning character of the "record" to
  7530. ;; be sorted.
  7531. (lambda nil
  7532. (if (re-search-forward re nil t)
  7533. (goto-char (match-beginning 0))
  7534. (goto-char (point-max))))
  7535. ;; This function moves to the last character of the "record" being
  7536. ;; sorted.
  7537. (lambda nil
  7538. (save-match-data
  7539. (condition-case nil
  7540. (outline-forward-same-level 1)
  7541. (error
  7542. (goto-char (point-max))))))
  7543. ;; This function returns the value that gets sorted against.
  7544. (lambda nil
  7545. (cond
  7546. ((= dcst ?n)
  7547. (if (looking-at org-complex-heading-regexp)
  7548. (string-to-number (match-string 4))
  7549. nil))
  7550. ((= dcst ?a)
  7551. (if (looking-at org-complex-heading-regexp)
  7552. (funcall case-func (match-string 4))
  7553. nil))
  7554. ((= dcst ?t)
  7555. (let ((end (save-excursion (outline-next-heading) (point))))
  7556. (if (or (re-search-forward org-ts-regexp end t)
  7557. (re-search-forward org-ts-regexp-both end t))
  7558. (org-time-string-to-seconds (match-string 0))
  7559. (org-float-time now))))
  7560. ((= dcst ?c)
  7561. (let ((end (save-excursion (outline-next-heading) (point))))
  7562. (if (re-search-forward
  7563. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7564. end t)
  7565. (org-time-string-to-seconds (match-string 0))
  7566. (org-float-time now))))
  7567. ((= dcst ?s)
  7568. (let ((end (save-excursion (outline-next-heading) (point))))
  7569. (if (re-search-forward org-scheduled-time-regexp end t)
  7570. (org-time-string-to-seconds (match-string 1))
  7571. (org-float-time now))))
  7572. ((= dcst ?d)
  7573. (let ((end (save-excursion (outline-next-heading) (point))))
  7574. (if (re-search-forward org-deadline-time-regexp end t)
  7575. (org-time-string-to-seconds (match-string 1))
  7576. (org-float-time now))))
  7577. ((= dcst ?p)
  7578. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7579. (string-to-char (match-string 2))
  7580. org-default-priority))
  7581. ((= dcst ?r)
  7582. (or (org-entry-get nil property) ""))
  7583. ((= dcst ?o)
  7584. (if (looking-at org-complex-heading-regexp)
  7585. (- 9999 (length (member (match-string 2)
  7586. org-todo-keywords-1)))))
  7587. ((= dcst ?f)
  7588. (if getkey-func
  7589. (progn
  7590. (setq tmp (funcall getkey-func))
  7591. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7592. tmp)
  7593. (error "Invalid key function `%s'" getkey-func)))
  7594. (t (error "Invalid sorting type `%c'" sorting-type))))
  7595. nil
  7596. (cond
  7597. ((= dcst ?a) 'string<)
  7598. ((= dcst ?f) compare-func)
  7599. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7600. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7601. ;; Reset the clock marker if needed
  7602. (when cmstr
  7603. (save-excursion
  7604. (goto-char start)
  7605. (search-forward cmstr nil t)
  7606. (move-marker org-clock-marker (point))))
  7607. (message "Sorting entries...done")))
  7608. (defun org-do-sort (table what &optional with-case sorting-type)
  7609. "Sort TABLE of WHAT according to SORTING-TYPE.
  7610. The user will be prompted for the SORTING-TYPE if the call to this
  7611. function does not specify it. WHAT is only for the prompt, to indicate
  7612. what is being sorted. The sorting key will be extracted from
  7613. the car of the elements of the table.
  7614. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7615. (unless sorting-type
  7616. (message
  7617. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7618. what)
  7619. (setq sorting-type (read-char-exclusive)))
  7620. (let ((dcst (downcase sorting-type))
  7621. extractfun comparefun)
  7622. ;; Define the appropriate functions
  7623. (cond
  7624. ((= dcst ?n)
  7625. (setq extractfun 'string-to-number
  7626. comparefun (if (= dcst sorting-type) '< '>)))
  7627. ((= dcst ?a)
  7628. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7629. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7630. comparefun (if (= dcst sorting-type)
  7631. 'string<
  7632. (lambda (a b) (and (not (string< a b))
  7633. (not (string= a b)))))))
  7634. ((= dcst ?t)
  7635. (setq extractfun
  7636. (lambda (x)
  7637. (if (or (string-match org-ts-regexp x)
  7638. (string-match org-ts-regexp-both x))
  7639. (org-float-time
  7640. (org-time-string-to-time (match-string 0 x)))
  7641. 0))
  7642. comparefun (if (= dcst sorting-type) '< '>)))
  7643. (t (error "Invalid sorting type `%c'" sorting-type)))
  7644. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7645. table)
  7646. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7647. ;;; The orgstruct minor mode
  7648. ;; Define a minor mode which can be used in other modes in order to
  7649. ;; integrate the org-mode structure editing commands.
  7650. ;; This is really a hack, because the org-mode structure commands use
  7651. ;; keys which normally belong to the major mode. Here is how it
  7652. ;; works: The minor mode defines all the keys necessary to operate the
  7653. ;; structure commands, but wraps the commands into a function which
  7654. ;; tests if the cursor is currently at a headline or a plain list
  7655. ;; item. If that is the case, the structure command is used,
  7656. ;; temporarily setting many Org-mode variables like regular
  7657. ;; expressions for filling etc. However, when any of those keys is
  7658. ;; used at a different location, function uses `key-binding' to look
  7659. ;; up if the key has an associated command in another currently active
  7660. ;; keymap (minor modes, major mode, global), and executes that
  7661. ;; command. There might be problems if any of the keys is otherwise
  7662. ;; used as a prefix key.
  7663. (defcustom orgstruct-heading-prefix-regexp ""
  7664. "Regexp that matches the custom prefix of Org headlines in
  7665. orgstruct(++)-mode."
  7666. :group 'org
  7667. :type 'string)
  7668. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7669. (defcustom orgstruct-setup-hook nil
  7670. "Hook run after orgstruct-mode-map is filled."
  7671. :group 'org
  7672. :type 'hook)
  7673. (defvar orgstruct-initialized nil)
  7674. (defvar org-local-vars nil
  7675. "List of local variables, for use by `orgstruct-mode'.")
  7676. ;;;###autoload
  7677. (define-minor-mode orgstruct-mode
  7678. "Toggle the minor mode `orgstruct-mode'.
  7679. This mode is for using Org-mode structure commands in other
  7680. modes. The following keys behave as if Org-mode were active, if
  7681. the cursor is on a headline, or on a plain list item (both as
  7682. defined by Org-mode)."
  7683. nil " OrgStruct" (make-sparse-keymap)
  7684. (when orgstruct-mode
  7685. (org-load-modules-maybe)
  7686. (unless orgstruct-initialized
  7687. (orgstruct-setup)
  7688. (setq orgstruct-initialized t))))
  7689. ;;;###autoload
  7690. (defun turn-on-orgstruct ()
  7691. "Unconditionally turn on `orgstruct-mode'."
  7692. (orgstruct-mode 1))
  7693. (defvar org-fb-vars nil)
  7694. (make-variable-buffer-local 'org-fb-vars)
  7695. (defun orgstruct++-mode (&optional arg)
  7696. "Toggle `orgstruct-mode', the enhanced version of it.
  7697. In addition to setting orgstruct-mode, this also exports all
  7698. indentation and autofilling variables from org-mode into the
  7699. buffer. It will also recognize item context in multiline items."
  7700. (interactive "P")
  7701. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7702. (if (< arg 1)
  7703. (progn (orgstruct-mode -1)
  7704. (mapc (lambda(v)
  7705. (org-set-local (car v)
  7706. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7707. org-fb-vars))
  7708. (orgstruct-mode 1)
  7709. (setq org-fb-vars nil)
  7710. (let (var val)
  7711. (mapc
  7712. (lambda (x)
  7713. (when (string-match
  7714. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7715. (symbol-name (car x)))
  7716. (setq var (car x) val (nth 1 x))
  7717. (push (list var `(quote ,(eval var))) org-fb-vars)
  7718. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7719. org-local-vars)
  7720. (org-set-local 'orgstruct-is-++ t))))
  7721. (defvar orgstruct-is-++ nil
  7722. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7723. (make-variable-buffer-local 'orgstruct-is-++)
  7724. ;;;###autoload
  7725. (defun turn-on-orgstruct++ ()
  7726. "Unconditionally turn on `orgstruct++-mode'."
  7727. (orgstruct++-mode 1))
  7728. (defun orgstruct-error ()
  7729. "Error when there is no default binding for a structure key."
  7730. (interactive)
  7731. (error "This key has no function outside structure elements"))
  7732. (defun orgstruct-setup ()
  7733. "Setup orgstruct keymap."
  7734. (dolist (f
  7735. '("org-meta"
  7736. "org-shiftmeta"
  7737. org-shifttab
  7738. org-backward-element
  7739. org-backward-heading-same-level
  7740. org-ctrl-c-ret
  7741. org-cycle
  7742. org-forward-heading-same-level
  7743. org-insert-heading
  7744. org-insert-heading-respect-content
  7745. org-kill-note-or-show-branches
  7746. org-mark-subtree
  7747. org-narrow-to-subtree
  7748. org-promote-subtree
  7749. org-reveal
  7750. org-show-subtree
  7751. org-sort
  7752. org-up-element
  7753. outline-demote
  7754. outline-next-visible-heading
  7755. outline-previous-visible-heading
  7756. outline-promote
  7757. outline-up-heading
  7758. show-children))
  7759. (dolist (f (if (stringp f)
  7760. (let ((flist))
  7761. (dolist (postfix
  7762. '("-return" "tab" "left" "right" "up" "down")
  7763. flist)
  7764. (let ((f (intern (concat f postfix))))
  7765. (when (fboundp f)
  7766. (push f flist)))))
  7767. (list f)))
  7768. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7769. (where-is-internal f outline-mode-map)))
  7770. ;; TODO use local-function-key-map
  7771. (dolist (rep '(("<tab>" . "TAB")
  7772. ("<ret>" . "RET")
  7773. ("<esc>" . "ESC")
  7774. ("<del>" . "DEL")))
  7775. (setq binding (read-kbd-macro (replace-regexp-in-string
  7776. (regexp-quote (car rep))
  7777. (cdr rep)
  7778. (key-description binding)))))
  7779. (let ((key (lookup-key orgstruct-mode-map binding)))
  7780. (when (or (not key) (numberp key))
  7781. (condition-case nil
  7782. (org-defkey orgstruct-mode-map
  7783. binding
  7784. (orgstruct-make-binding f binding))
  7785. (error nil)))))))
  7786. (run-hooks 'orgstruct-setup-hook))
  7787. (defun orgstruct-make-binding (fun key)
  7788. "Create a function for binding in the structure minor mode.
  7789. FUN is the command to call inside a table. KEY is the key that
  7790. should be checked in for a command to execute outside of tables."
  7791. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7792. (let ((nname name)
  7793. (i 0))
  7794. (while (fboundp (intern nname))
  7795. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7796. (setq name (intern nname)))
  7797. (eval
  7798. `(defun ,name (arg)
  7799. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7800. "Outside of structure, run the binding of `"
  7801. (key-description key) "'.")
  7802. (interactive "p")
  7803. (unless
  7804. (let* ((org-heading-regexp
  7805. (concat "^"
  7806. orgstruct-heading-prefix-regexp
  7807. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7808. (org-outline-regexp
  7809. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7810. (org-outline-regexp-bol
  7811. (concat "^" org-outline-regexp))
  7812. (outline-regexp org-outline-regexp)
  7813. (outline-heading-end-regexp "\n")
  7814. (outline-level 'outline-level)
  7815. (outline-heading-alist))
  7816. (when (org-context-p 'headline 'item
  7817. ,(when (memq fun '(org-insert-heading))
  7818. '(when orgstruct-is-++
  7819. 'item-body)))
  7820. (org-run-like-in-org-mode ',fun)
  7821. t))
  7822. (let* ((orgstruct-mode)
  7823. (binding (key-binding ,key)))
  7824. (if (keymapp binding)
  7825. (set-temporary-overlay-map binding)
  7826. (call-interactively
  7827. (or binding 'orgstruct-error)))))))
  7828. name))
  7829. (defun org-contextualize-keys (alist contexts)
  7830. "Return valid elements in ALIST depending on CONTEXTS.
  7831. `org-agenda-custom-commands' or `org-capture-templates' are the
  7832. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7833. or `org-capture-templates-contexts' are the associated contexts
  7834. definitions."
  7835. (let ((contexts
  7836. ;; normalize contexts
  7837. (mapcar
  7838. (lambda(c) (cond ((listp (cadr c))
  7839. (list (car c) (car c) (cadr c)))
  7840. ((string= "" (cadr c))
  7841. (list (car c) (car c) (caddr c)))
  7842. (t c))) contexts))
  7843. (a alist) c r s)
  7844. ;; loop over all commands or templates
  7845. (while (setq c (pop a))
  7846. (let (vrules repl)
  7847. (cond
  7848. ((not (assoc (car c) contexts))
  7849. (push c r))
  7850. ((and (assoc (car c) contexts)
  7851. (setq vrules (org-contextualize-validate-key
  7852. (car c) contexts)))
  7853. (mapc (lambda (vr)
  7854. (when (not (equal (car vr) (cadr vr)))
  7855. (setq repl vr))) vrules)
  7856. (if (not repl) (push c r)
  7857. (push (cadr repl) s)
  7858. (push
  7859. (cons (car c)
  7860. (cdr (or (assoc (cadr repl) alist)
  7861. (error "Undefined key `%s' as contextual replacement for `%s'"
  7862. (cadr repl) (car c)))))
  7863. r))))))
  7864. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7865. (delq
  7866. nil
  7867. (delete-dups
  7868. (mapcar (lambda (x)
  7869. (let ((tpl (car x)))
  7870. (when (not (delq
  7871. nil
  7872. (mapcar (lambda(y)
  7873. (equal y tpl)) s))) x)))
  7874. (reverse r))))))
  7875. (defun org-contextualize-validate-key (key contexts)
  7876. "Check CONTEXTS for agenda or capture KEY."
  7877. (let (r rr res)
  7878. (while (setq r (pop contexts))
  7879. (mapc
  7880. (lambda (rr)
  7881. (when
  7882. (and (equal key (car r))
  7883. (if (functionp rr) (funcall rr)
  7884. (or (and (eq (car rr) 'in-file)
  7885. (buffer-file-name)
  7886. (string-match (cdr rr) (buffer-file-name)))
  7887. (and (eq (car rr) 'in-mode)
  7888. (string-match (cdr rr) (symbol-name major-mode)))
  7889. (and (eq (car rr) 'in-buffer)
  7890. (string-match (cdr rr) (buffer-name)))
  7891. (when (and (eq (car rr) 'not-in-file)
  7892. (buffer-file-name))
  7893. (not (string-match (cdr rr) (buffer-file-name))))
  7894. (when (eq (car rr) 'not-in-mode)
  7895. (not (string-match (cdr rr) (symbol-name major-mode))))
  7896. (when (eq (car rr) 'not-in-buffer)
  7897. (not (string-match (cdr rr) (buffer-name)))))))
  7898. (push r res)))
  7899. (car (last r))))
  7900. (delete-dups (delq nil res))))
  7901. (defun org-context-p (&rest contexts)
  7902. "Check if local context is any of CONTEXTS.
  7903. Possible values in the list of contexts are `table', `headline', and `item'."
  7904. (let ((pos (point)))
  7905. (goto-char (point-at-bol))
  7906. (prog1 (or (and (memq 'table contexts)
  7907. (looking-at "[ \t]*|"))
  7908. (and (memq 'headline contexts)
  7909. (looking-at org-outline-regexp))
  7910. (and (memq 'item contexts)
  7911. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7912. (and (memq 'item-body contexts)
  7913. (org-in-item-p)))
  7914. (goto-char pos))))
  7915. (defun org-get-local-variables ()
  7916. "Return a list of all local variables in an Org mode buffer."
  7917. (let (varlist)
  7918. (with-current-buffer (get-buffer-create "*Org tmp*")
  7919. (erase-buffer)
  7920. (org-mode)
  7921. (setq varlist (buffer-local-variables)))
  7922. (kill-buffer "*Org tmp*")
  7923. (delq nil
  7924. (mapcar
  7925. (lambda (x)
  7926. (setq x
  7927. (if (symbolp x)
  7928. (list x)
  7929. (list (car x) (cdr x))))
  7930. (if (and (not (get (car x) 'org-state))
  7931. (string-match
  7932. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7933. (symbol-name (car x))))
  7934. x nil))
  7935. varlist))))
  7936. (defun org-clone-local-variables (from-buffer &optional regexp)
  7937. "Clone local variables from FROM-BUFFER.
  7938. Optional argument REGEXP selects variables to clone."
  7939. (mapc
  7940. (lambda (pair)
  7941. (and (symbolp (car pair))
  7942. (or (null regexp)
  7943. (string-match regexp (symbol-name (car pair))))
  7944. (set (make-local-variable (car pair))
  7945. (cdr pair))))
  7946. (buffer-local-variables from-buffer)))
  7947. ;;;###autoload
  7948. (defun org-run-like-in-org-mode (cmd)
  7949. "Run a command, pretending that the current buffer is in Org-mode.
  7950. This will temporarily bind local variables that are typically bound in
  7951. Org-mode to the values they have in Org-mode, and then interactively
  7952. call CMD."
  7953. (org-load-modules-maybe)
  7954. (unless org-local-vars
  7955. (setq org-local-vars (org-get-local-variables)))
  7956. (let (binds)
  7957. (dolist (var org-local-vars)
  7958. (when (or (not (boundp (car var)))
  7959. (eq (symbol-value (car var))
  7960. (default-value (car var))))
  7961. (push (list (car var) `(quote ,(cadr var))) binds)))
  7962. (eval `(let ,binds
  7963. (call-interactively (quote ,cmd))))))
  7964. ;;;; Archiving
  7965. (defun org-get-category (&optional pos force-refresh)
  7966. "Get the category applying to position POS."
  7967. (save-match-data
  7968. (if force-refresh (org-refresh-category-properties))
  7969. (let ((pos (or pos (point))))
  7970. (or (get-text-property pos 'org-category)
  7971. (progn (org-refresh-category-properties)
  7972. (get-text-property pos 'org-category))))))
  7973. (defun org-refresh-category-properties ()
  7974. "Refresh category text properties in the buffer."
  7975. (let ((case-fold-search t)
  7976. (inhibit-read-only t)
  7977. (def-cat (cond
  7978. ((null org-category)
  7979. (if buffer-file-name
  7980. (file-name-sans-extension
  7981. (file-name-nondirectory buffer-file-name))
  7982. "???"))
  7983. ((symbolp org-category) (symbol-name org-category))
  7984. (t org-category)))
  7985. beg end cat pos optionp)
  7986. (org-unmodified
  7987. (save-excursion
  7988. (save-restriction
  7989. (widen)
  7990. (goto-char (point-min))
  7991. (put-text-property (point) (point-max) 'org-category def-cat)
  7992. (while (re-search-forward
  7993. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7994. (setq pos (match-end 0)
  7995. optionp (equal (char-after (match-beginning 0)) ?#)
  7996. cat (org-trim (match-string 2)))
  7997. (if optionp
  7998. (setq beg (point-at-bol) end (point-max))
  7999. (org-back-to-heading t)
  8000. (setq beg (point) end (org-end-of-subtree t t)))
  8001. (put-text-property beg end 'org-category cat)
  8002. (put-text-property beg end 'org-category-position beg)
  8003. (goto-char pos)))))))
  8004. (defun org-refresh-properties (dprop tprop)
  8005. "Refresh buffer text properties.
  8006. DPROP is the drawer property and TPROP is the corresponding text
  8007. property to set."
  8008. (let ((case-fold-search t)
  8009. (inhibit-read-only t) p)
  8010. (org-unmodified
  8011. (save-excursion
  8012. (save-restriction
  8013. (widen)
  8014. (goto-char (point-min))
  8015. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8016. (setq p (org-match-string-no-properties 1))
  8017. (save-excursion
  8018. (org-back-to-heading t)
  8019. (put-text-property
  8020. (point-at-bol) (point-at-eol) tprop p))))))))
  8021. ;;;; Link Stuff
  8022. ;;; Link abbreviations
  8023. (defun org-link-expand-abbrev (link)
  8024. "Apply replacements as defined in `org-link-abbrev-alist'."
  8025. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8026. (let* ((key (match-string 1 link))
  8027. (as (or (assoc key org-link-abbrev-alist-local)
  8028. (assoc key org-link-abbrev-alist)))
  8029. (tag (and (match-end 2) (match-string 3 link)))
  8030. rpl)
  8031. (if (not as)
  8032. link
  8033. (setq rpl (cdr as))
  8034. (cond
  8035. ((symbolp rpl) (funcall rpl tag))
  8036. ((string-match "%(\\([^)]+\\))" rpl)
  8037. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8038. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8039. ((string-match "%h" rpl)
  8040. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8041. (t (concat rpl tag)))))
  8042. link))
  8043. ;;; Storing and inserting links
  8044. (defvar org-insert-link-history nil
  8045. "Minibuffer history for links inserted with `org-insert-link'.")
  8046. (defvar org-stored-links nil
  8047. "Contains the links stored with `org-store-link'.")
  8048. (defvar org-store-link-plist nil
  8049. "Plist with info about the most recently link created with `org-store-link'.")
  8050. (defvar org-link-protocols nil
  8051. "Link protocols added to Org-mode using `org-add-link-type'.")
  8052. (defvar org-store-link-functions nil
  8053. "List of functions that are called to create and store a link.
  8054. Each function will be called in turn until one returns a non-nil
  8055. value. Each function should check if it is responsible for creating
  8056. this link (for example by looking at the major mode).
  8057. If not, it must exit and return nil.
  8058. If yes, it should return a non-nil value after a calling
  8059. `org-store-link-props' with a list of properties and values.
  8060. Special properties are:
  8061. :type The link prefix, like \"http\". This must be given.
  8062. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8063. This is obligatory as well.
  8064. :description Optional default description for the second pair
  8065. of brackets in an Org-mode link. The user can still change
  8066. this when inserting this link into an Org-mode buffer.
  8067. In addition to these, any additional properties can be specified
  8068. and then used in capture templates.")
  8069. (defun org-add-link-type (type &optional follow export)
  8070. "Add TYPE to the list of `org-link-types'.
  8071. Re-compute all regular expressions depending on `org-link-types'
  8072. FOLLOW and EXPORT are two functions.
  8073. FOLLOW should take the link path as the single argument and do whatever
  8074. is necessary to follow the link, for example find a file or display
  8075. a mail message.
  8076. EXPORT should format the link path for export to one of the export formats.
  8077. It should be a function accepting three arguments:
  8078. path the path of the link, the text after the prefix (like \"http:\")
  8079. desc the description of the link, if any, or a description added by
  8080. org-export-normalize-links if there is none
  8081. format the export format, a symbol like `html' or `latex' or `ascii'..
  8082. The function may use the FORMAT information to return different values
  8083. depending on the format. The return value will be put literally into
  8084. the exported file. If the return value is nil, this means Org should
  8085. do what it normally does with links which do not have EXPORT defined.
  8086. Org-mode has a built-in default for exporting links. If you are happy with
  8087. this default, there is no need to define an export function for the link
  8088. type. For a simple example of an export function, see `org-bbdb.el'."
  8089. (add-to-list 'org-link-types type t)
  8090. (org-make-link-regexps)
  8091. (if (assoc type org-link-protocols)
  8092. (setcdr (assoc type org-link-protocols) (list follow export))
  8093. (push (list type follow export) org-link-protocols)))
  8094. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8095. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8096. ;;;###autoload
  8097. (defun org-store-link (arg)
  8098. "\\<org-mode-map>Store an org-link to the current location.
  8099. This link is added to `org-stored-links' and can later be inserted
  8100. into an org-buffer with \\[org-insert-link].
  8101. For some link types, a prefix arg is interpreted.
  8102. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8103. For file links, arg negates `org-context-in-file-links'.
  8104. A double prefix arg force skipping storing functions that are not
  8105. part of Org's core."
  8106. (interactive "P")
  8107. (org-load-modules-maybe)
  8108. (setq org-store-link-plist nil) ; reset
  8109. (org-with-limited-levels
  8110. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8111. (cond
  8112. ((and (not (equal arg '(16)))
  8113. (setq sfuns
  8114. (delq
  8115. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8116. org-store-link-functions))
  8117. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8118. (or (and (cdr sfuns)
  8119. (funcall (intern
  8120. (completing-read "Which function for creating the link? "
  8121. sfunsn t (car sfunsn)))))
  8122. (funcall (caar sfuns)))
  8123. (setq link (plist-get org-store-link-plist :link)
  8124. desc (or (plist-get org-store-link-plist :description) link))))
  8125. ((org-src-edit-buffer-p)
  8126. (let (label gc)
  8127. (while (or (not label)
  8128. (save-excursion
  8129. (save-restriction
  8130. (widen)
  8131. (goto-char (point-min))
  8132. (re-search-forward
  8133. (regexp-quote (format org-coderef-label-format label))
  8134. nil t))))
  8135. (when label (message "Label exists already") (sit-for 2))
  8136. (setq label (read-string "Code line label: " label)))
  8137. (end-of-line 1)
  8138. (setq link (format org-coderef-label-format label))
  8139. (setq gc (- 79 (length link)))
  8140. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8141. (insert link)
  8142. (setq link (concat "(" label ")") desc nil)))
  8143. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8144. ;; We are in the agenda, link to referenced location
  8145. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8146. (get-text-property (point) 'org-marker))))
  8147. (when m
  8148. (org-with-point-at m
  8149. (setq agenda-link
  8150. (if (org-called-interactively-p 'any)
  8151. (call-interactively 'org-store-link)
  8152. (org-store-link nil)))))))
  8153. ((eq major-mode 'calendar-mode)
  8154. (let ((cd (calendar-cursor-to-date)))
  8155. (setq link
  8156. (format-time-string
  8157. (car org-time-stamp-formats)
  8158. (apply 'encode-time
  8159. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8160. nil nil nil))))
  8161. (org-store-link-props :type "calendar" :date cd)))
  8162. ((eq major-mode 'help-mode)
  8163. (setq link (concat "help:" (save-excursion
  8164. (goto-char (point-min))
  8165. (looking-at "^[^ ]+")
  8166. (match-string 0))))
  8167. (org-store-link-props :type "help"))
  8168. ((eq major-mode 'w3-mode)
  8169. (setq cpltxt (if (and (buffer-name)
  8170. (not (string-match "Untitled" (buffer-name))))
  8171. (buffer-name)
  8172. (url-view-url t))
  8173. link (url-view-url t))
  8174. (org-store-link-props :type "w3" :url (url-view-url t)))
  8175. ((setq search (run-hook-with-args-until-success
  8176. 'org-create-file-search-functions))
  8177. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8178. "::" search))
  8179. (setq cpltxt (or description link)))
  8180. ((eq major-mode 'image-mode)
  8181. (setq cpltxt (concat "file:"
  8182. (abbreviate-file-name buffer-file-name))
  8183. link cpltxt)
  8184. (org-store-link-props :type "image" :file buffer-file-name))
  8185. ((eq major-mode 'dired-mode)
  8186. ;; link to the file in the current line
  8187. (let ((file (dired-get-filename nil t)))
  8188. (setq file (if file
  8189. (abbreviate-file-name
  8190. (expand-file-name (dired-get-filename nil t)))
  8191. ;; otherwise, no file so use current directory.
  8192. default-directory))
  8193. (setq cpltxt (concat "file:" file)
  8194. link cpltxt)))
  8195. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8196. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8197. (cond
  8198. ((org-in-regexp "<<\\(.*?\\)>>")
  8199. (setq cpltxt
  8200. (concat "file:"
  8201. (abbreviate-file-name
  8202. (buffer-file-name (buffer-base-buffer)))
  8203. "::" (match-string 1))
  8204. link cpltxt))
  8205. ((and (featurep 'org-id)
  8206. (or (eq org-id-link-to-org-use-id t)
  8207. (and (org-called-interactively-p 'any)
  8208. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8209. (and (eq org-id-link-to-org-use-id
  8210. 'create-if-interactive-and-no-custom-id)
  8211. (not custom-id))))
  8212. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8213. ;; We can make a link using the ID.
  8214. (setq link (condition-case nil
  8215. (prog1 (org-id-store-link)
  8216. (setq desc (plist-get org-store-link-plist :description)))
  8217. (error
  8218. ;; probably before first headline, link to file only
  8219. (concat "file:"
  8220. (abbreviate-file-name
  8221. (buffer-file-name (buffer-base-buffer))))))))
  8222. (t
  8223. ;; Just link to current headline
  8224. (setq cpltxt (concat "file:"
  8225. (abbreviate-file-name
  8226. (buffer-file-name (buffer-base-buffer)))))
  8227. ;; Add a context search string
  8228. (when (org-xor org-context-in-file-links arg)
  8229. (let* ((ee (org-element-at-point))
  8230. (et (org-element-type ee))
  8231. (ev (plist-get (cadr ee) :value)))
  8232. (setq txt (cond
  8233. ((org-at-heading-p) nil)
  8234. ((eq et 'keyword) ev)
  8235. ((org-region-active-p)
  8236. (buffer-substring (region-beginning) (region-end)))))
  8237. (when (or (null txt) (string-match "\\S-" txt))
  8238. (setq cpltxt
  8239. (concat cpltxt "::"
  8240. (condition-case nil
  8241. (org-make-org-heading-search-string txt)
  8242. (error "")))
  8243. desc (or (and (eq et 'keyword) ev)
  8244. (nth 4 (ignore-errors (org-heading-components)))
  8245. "NONE")))))
  8246. (if (string-match "::\\'" cpltxt)
  8247. (setq cpltxt (substring cpltxt 0 -2)))
  8248. (setq link cpltxt))))
  8249. ((buffer-file-name (buffer-base-buffer))
  8250. ;; Just link to this file here.
  8251. (setq cpltxt (concat "file:"
  8252. (abbreviate-file-name
  8253. (buffer-file-name (buffer-base-buffer)))))
  8254. ;; Add a context string
  8255. (when (org-xor org-context-in-file-links arg)
  8256. (setq txt (if (org-region-active-p)
  8257. (buffer-substring (region-beginning) (region-end))
  8258. (buffer-substring (point-at-bol) (point-at-eol))))
  8259. ;; Only use search option if there is some text.
  8260. (when (string-match "\\S-" txt)
  8261. (setq cpltxt
  8262. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8263. desc "NONE")))
  8264. (setq link cpltxt))
  8265. ((org-called-interactively-p 'interactive)
  8266. (user-error "No method for storing a link from this buffer"))
  8267. (t (setq link nil)))
  8268. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8269. (setq link (or link cpltxt)
  8270. desc (or desc cpltxt))
  8271. (cond ((equal desc "NONE") (setq desc nil))
  8272. ((string-match org-bracket-link-regexp desc)
  8273. (setq desc (replace-regexp-in-string
  8274. org-bracket-link-regexp
  8275. (concat "\\3" (if (equal (length (match-string 0 desc))
  8276. (length desc)) "*" "")) desc))))
  8277. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8278. (progn
  8279. (setq org-stored-links
  8280. (cons (list link desc) org-stored-links))
  8281. (message "Stored: %s" (or desc link))
  8282. (when custom-id
  8283. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8284. "::#" custom-id))
  8285. (setq org-stored-links
  8286. (cons (list link desc) org-stored-links))))
  8287. (or agenda-link (and link (org-make-link-string link desc)))))))
  8288. (defun org-store-link-props (&rest plist)
  8289. "Store link properties, extract names and addresses."
  8290. (let (x adr)
  8291. (when (setq x (plist-get plist :from))
  8292. (setq adr (mail-extract-address-components x))
  8293. (setq plist (plist-put plist :fromname (car adr)))
  8294. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8295. (when (setq x (plist-get plist :to))
  8296. (setq adr (mail-extract-address-components x))
  8297. (setq plist (plist-put plist :toname (car adr)))
  8298. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8299. (let ((from (plist-get plist :from))
  8300. (to (plist-get plist :to)))
  8301. (when (and from to org-from-is-user-regexp)
  8302. (setq plist
  8303. (plist-put plist :fromto
  8304. (if (string-match org-from-is-user-regexp from)
  8305. (concat "to %t")
  8306. (concat "from %f"))))))
  8307. (setq org-store-link-plist plist))
  8308. (defun org-add-link-props (&rest plist)
  8309. "Add these properties to the link property list."
  8310. (let (key value)
  8311. (while plist
  8312. (setq key (pop plist) value (pop plist))
  8313. (setq org-store-link-plist
  8314. (plist-put org-store-link-plist key value)))))
  8315. (defun org-email-link-description (&optional fmt)
  8316. "Return the description part of an email link.
  8317. This takes information from `org-store-link-plist' and formats it
  8318. according to FMT (default from `org-email-link-description-format')."
  8319. (setq fmt (or fmt org-email-link-description-format))
  8320. (let* ((p org-store-link-plist)
  8321. (to (plist-get p :toaddress))
  8322. (from (plist-get p :fromaddress))
  8323. (table
  8324. (list
  8325. (cons "%c" (plist-get p :fromto))
  8326. (cons "%F" (plist-get p :from))
  8327. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8328. (cons "%T" (plist-get p :to))
  8329. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8330. (cons "%s" (plist-get p :subject))
  8331. (cons "%d" (plist-get p :date))
  8332. (cons "%m" (plist-get p :message-id)))))
  8333. (when (string-match "%c" fmt)
  8334. ;; Check if the user wrote this message
  8335. (if (and org-from-is-user-regexp from to
  8336. (save-match-data (string-match org-from-is-user-regexp from)))
  8337. (setq fmt (replace-match "to %t" t t fmt))
  8338. (setq fmt (replace-match "from %f" t t fmt))))
  8339. (org-replace-escapes fmt table)))
  8340. (defun org-make-org-heading-search-string (&optional string)
  8341. "Make search string for the current headline or STRING."
  8342. (let ((s (or string
  8343. (and (derived-mode-p 'org-mode)
  8344. (save-excursion
  8345. (org-back-to-heading t)
  8346. (org-element-property :raw-value (org-element-at-point))))))
  8347. (lines org-context-in-file-links))
  8348. (or string (setq s (concat "*" s))) ; Add * for headlines
  8349. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8350. (when (and string (integerp lines) (> lines 0))
  8351. (let ((slines (org-split-string s "\n")))
  8352. (when (< lines (length slines))
  8353. (setq s (mapconcat
  8354. 'identity
  8355. (reverse (nthcdr (- (length slines) lines)
  8356. (reverse slines))) "\n")))))
  8357. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8358. (defun org-make-link-string (link &optional description)
  8359. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8360. (unless (string-match "\\S-" link)
  8361. (error "Empty link"))
  8362. (when (and description
  8363. (stringp description)
  8364. (not (string-match "\\S-" description)))
  8365. (setq description nil))
  8366. (when (stringp description)
  8367. ;; Remove brackets from the description, they are fatal.
  8368. (while (string-match "\\[" description)
  8369. (setq description (replace-match "{" t t description)))
  8370. (while (string-match "\\]" description)
  8371. (setq description (replace-match "}" t t description))))
  8372. (when (equal link description)
  8373. ;; No description needed, it is identical
  8374. (setq description nil))
  8375. (when (and (not description)
  8376. (not (string-match (org-image-file-name-regexp) link))
  8377. (not (equal link (org-link-escape link))))
  8378. (setq description (org-extract-attributes link)))
  8379. (setq link
  8380. (cond ((string-match (org-image-file-name-regexp) link) link)
  8381. ((string-match org-link-types-re link)
  8382. (concat (match-string 1 link)
  8383. (org-link-escape (substring link (match-end 1)))))
  8384. (t (org-link-escape link))))
  8385. (concat "[[" link "]"
  8386. (if description (concat "[" description "]") "")
  8387. "]"))
  8388. (defconst org-link-escape-chars
  8389. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8390. "List of characters that should be escaped in link.
  8391. This is the list that is used for internal purposes.")
  8392. (defconst org-link-escape-chars-browser
  8393. '(?\ )
  8394. "List of escapes for characters that are problematic in links.
  8395. This is the list that is used before handing over to the browser.")
  8396. (defun org-link-escape (text &optional table merge)
  8397. "Return percent escaped representation of TEXT.
  8398. TEXT is a string with the text to escape.
  8399. Optional argument TABLE is a list with characters that should be
  8400. escaped. When nil, `org-link-escape-chars' is used.
  8401. If optional argument MERGE is set, merge TABLE into
  8402. `org-link-escape-chars'."
  8403. (cond
  8404. ((and table merge)
  8405. (mapc (lambda (defchr)
  8406. (unless (member defchr table)
  8407. (setq table (cons defchr table)))) org-link-escape-chars))
  8408. ((null table)
  8409. (setq table org-link-escape-chars)))
  8410. (mapconcat
  8411. (lambda (char)
  8412. (if (or (member char table)
  8413. (and (or (< char 32) (= char 37) (> char 126))
  8414. org-url-hexify-p))
  8415. (mapconcat (lambda (sequence-element)
  8416. (format "%%%.2X" sequence-element))
  8417. (or (encode-coding-char char 'utf-8)
  8418. (error "Unable to percent escape character: %s"
  8419. (char-to-string char))) "")
  8420. (char-to-string char))) text ""))
  8421. (defun org-link-unescape (str)
  8422. "Unhex hexified Unicode strings as returned from the JavaScript function
  8423. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8424. (unless (and (null str) (string= "" str))
  8425. (let ((pos 0) (case-fold-search t) unhexed)
  8426. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8427. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8428. (setq str (replace-match unhexed t t str))
  8429. (setq pos (+ pos (length unhexed))))))
  8430. str)
  8431. (defun org-link-unescape-compound (hex)
  8432. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8433. Note: this function also decodes single byte encodings like
  8434. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8435. (save-match-data
  8436. (let* ((bytes (cdr (split-string hex "%")))
  8437. (ret "")
  8438. (eat 0)
  8439. (sum 0))
  8440. (while bytes
  8441. (let* ((val (string-to-number (pop bytes) 16))
  8442. (shift-xor
  8443. (if (= 0 eat)
  8444. (cond
  8445. ((>= val 252) (cons 6 252))
  8446. ((>= val 248) (cons 5 248))
  8447. ((>= val 240) (cons 4 240))
  8448. ((>= val 224) (cons 3 224))
  8449. ((>= val 192) (cons 2 192))
  8450. (t (cons 0 0)))
  8451. (cons 6 128))))
  8452. (if (>= val 192) (setq eat (car shift-xor)))
  8453. (setq val (logxor val (cdr shift-xor)))
  8454. (setq sum (+ (lsh sum (car shift-xor)) val))
  8455. (if (> eat 0) (setq eat (- eat 1)))
  8456. (cond
  8457. ((= 0 eat) ;multi byte
  8458. (setq ret (concat ret (org-char-to-string sum)))
  8459. (setq sum 0))
  8460. ((not bytes) ; single byte(s)
  8461. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8462. )) ;; end (while bytes
  8463. ret )))
  8464. (defun org-link-unescape-single-byte-sequence (hex)
  8465. "Unhexify hex-encoded single byte character sequences."
  8466. (mapconcat (lambda (byte)
  8467. (char-to-string (string-to-number byte 16)))
  8468. (cdr (split-string hex "%")) ""))
  8469. (defun org-xor (a b)
  8470. "Exclusive or."
  8471. (if a (not b) b))
  8472. (defun org-fixup-message-id-for-http (s)
  8473. "Replace special characters in a message id, so it can be used in an http query."
  8474. (when (string-match "%" s)
  8475. (setq s (mapconcat (lambda (c)
  8476. (if (eq c ?%)
  8477. "%25"
  8478. (char-to-string c)))
  8479. s "")))
  8480. (while (string-match "<" s)
  8481. (setq s (replace-match "%3C" t t s)))
  8482. (while (string-match ">" s)
  8483. (setq s (replace-match "%3E" t t s)))
  8484. (while (string-match "@" s)
  8485. (setq s (replace-match "%40" t t s)))
  8486. s)
  8487. (defun org-link-prettify (link)
  8488. "Return a human-readable representation of LINK.
  8489. The car of LINK must be a raw link the cdr of LINK must be either
  8490. a link description or nil."
  8491. (let ((desc (or (cadr link) "<no description>")))
  8492. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8493. "<" (car link) ">")))
  8494. ;;;###autoload
  8495. (defun org-insert-link-global ()
  8496. "Insert a link like Org-mode does.
  8497. This command can be called in any mode to insert a link in Org-mode syntax."
  8498. (interactive)
  8499. (org-load-modules-maybe)
  8500. (org-run-like-in-org-mode 'org-insert-link))
  8501. (defun org-insert-all-links (&optional keep)
  8502. "Insert all links in `org-stored-links'."
  8503. (interactive "P")
  8504. (let ((links (copy-sequence org-stored-links)) l)
  8505. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8506. (insert "- ")
  8507. (org-insert-link nil (car l) (cadr l))
  8508. (insert "\n"))))
  8509. (defun org-link-fontify-links-to-this-file ()
  8510. "Fontify links to the current file in `org-stored-links'."
  8511. (let ((f (buffer-file-name)) a b)
  8512. (setq a (mapcar (lambda(l)
  8513. (let ((ll (car l)))
  8514. (when (and (string-match "^file:\\(.+\\)::" ll)
  8515. (equal f (expand-file-name (match-string 1 ll))))
  8516. ll)))
  8517. org-stored-links))
  8518. (when (featurep 'org-id)
  8519. (setq b (mapcar (lambda(l)
  8520. (let ((ll (car l)))
  8521. (when (and (string-match "^id:\\(.+\\)$" ll)
  8522. (equal f (expand-file-name
  8523. (or (org-id-find-id-file
  8524. (match-string 1 ll)) ""))))
  8525. ll)))
  8526. org-stored-links)))
  8527. (mapcar (lambda(l)
  8528. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8529. (delq nil (append a b)))))
  8530. (defvar org-link-links-in-this-file nil)
  8531. (defun org-insert-link (&optional complete-file link-location default-description)
  8532. "Insert a link. At the prompt, enter the link.
  8533. Completion can be used to insert any of the link protocol prefixes like
  8534. http or ftp in use.
  8535. The history can be used to select a link previously stored with
  8536. `org-store-link'. When the empty string is entered (i.e. if you just
  8537. press RET at the prompt), the link defaults to the most recently
  8538. stored link. As SPC triggers completion in the minibuffer, you need to
  8539. use M-SPC or C-q SPC to force the insertion of a space character.
  8540. You will also be prompted for a description, and if one is given, it will
  8541. be displayed in the buffer instead of the link.
  8542. If there is already a link at point, this command will allow you to edit link
  8543. and description parts.
  8544. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8545. be selected using completion. The path to the file will be relative to the
  8546. current directory if the file is in the current directory or a subdirectory.
  8547. Otherwise, the link will be the absolute path as completed in the minibuffer
  8548. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8549. option `org-link-file-path-type'.
  8550. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8551. the current directory or below.
  8552. With three \\[universal-argument] prefixes, negate the meaning of
  8553. `org-keep-stored-link-after-insertion'.
  8554. If `org-make-link-description-function' is non-nil, this function will be
  8555. called with the link target, and the result will be the default
  8556. link description.
  8557. If the LINK-LOCATION parameter is non-nil, this value will be
  8558. used as the link location instead of reading one interactively.
  8559. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8560. be used as the default description."
  8561. (interactive "P")
  8562. (let* ((wcf (current-window-configuration))
  8563. (origbuf (current-buffer))
  8564. (region (if (org-region-active-p)
  8565. (buffer-substring (region-beginning) (region-end))))
  8566. (remove (and region (list (region-beginning) (region-end))))
  8567. (desc region)
  8568. tmphist ; byte-compile incorrectly complains about this
  8569. (link link-location)
  8570. (abbrevs org-link-abbrev-alist-local)
  8571. entry file all-prefixes auto-desc)
  8572. (cond
  8573. (link-location) ; specified by arg, just use it.
  8574. ((org-in-regexp org-bracket-link-regexp 1)
  8575. ;; We do have a link at point, and we are going to edit it.
  8576. (setq remove (list (match-beginning 0) (match-end 0)))
  8577. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8578. (setq link (read-string "Link: "
  8579. (org-link-unescape
  8580. (org-match-string-no-properties 1)))))
  8581. ((or (org-in-regexp org-angle-link-re)
  8582. (org-in-regexp org-plain-link-re))
  8583. ;; Convert to bracket link
  8584. (setq remove (list (match-beginning 0) (match-end 0))
  8585. link (read-string "Link: "
  8586. (org-remove-angle-brackets (match-string 0)))))
  8587. ((member complete-file '((4) (16)))
  8588. ;; Completing read for file names.
  8589. (setq link (org-file-complete-link complete-file)))
  8590. (t
  8591. ;; Read link, with completion for stored links.
  8592. (org-link-fontify-links-to-this-file)
  8593. (org-switch-to-buffer-other-window "*Org Links*")
  8594. (with-current-buffer "*Org Links*"
  8595. (erase-buffer)
  8596. (insert "Insert a link.
  8597. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8598. (when org-stored-links
  8599. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8600. (insert (mapconcat 'org-link-prettify
  8601. (reverse org-stored-links) "\n")))
  8602. (goto-char (point-min)))
  8603. (let ((cw (selected-window)))
  8604. (select-window (get-buffer-window "*Org Links*" 'visible))
  8605. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8606. (unless (pos-visible-in-window-p (point-max))
  8607. (org-fit-window-to-buffer))
  8608. (and (window-live-p cw) (select-window cw)))
  8609. ;; Fake a link history, containing the stored links.
  8610. (setq tmphist (append (mapcar 'car org-stored-links)
  8611. org-insert-link-history))
  8612. (setq all-prefixes (append (mapcar 'car abbrevs)
  8613. (mapcar 'car org-link-abbrev-alist)
  8614. org-link-types))
  8615. (unwind-protect
  8616. (progn
  8617. (setq link
  8618. (org-completing-read
  8619. "Link: "
  8620. (append
  8621. (mapcar (lambda (x) (concat x ":"))
  8622. all-prefixes)
  8623. (mapcar 'car org-stored-links))
  8624. nil nil nil
  8625. 'tmphist
  8626. (caar org-stored-links)))
  8627. (if (not (string-match "\\S-" link))
  8628. (error "No link selected"))
  8629. (mapc (lambda(l)
  8630. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8631. org-stored-links)
  8632. (if (or (member link all-prefixes)
  8633. (and (equal ":" (substring link -1))
  8634. (member (substring link 0 -1) all-prefixes)
  8635. (setq link (substring link 0 -1))))
  8636. (setq link (with-current-buffer origbuf
  8637. (org-link-try-special-completion link)))))
  8638. (set-window-configuration wcf)
  8639. (kill-buffer "*Org Links*"))
  8640. (setq entry (assoc link org-stored-links))
  8641. (or entry (push link org-insert-link-history))
  8642. (setq desc (or desc (nth 1 entry)))))
  8643. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8644. (not org-keep-stored-link-after-insertion))
  8645. (setq org-stored-links (delq (assoc link org-stored-links)
  8646. org-stored-links)))
  8647. (if (string-match org-plain-link-re link)
  8648. ;; URL-like link, normalize the use of angular brackets.
  8649. (setq link (org-remove-angle-brackets link)))
  8650. ;; Check if we are linking to the current file with a search
  8651. ;; option If yes, simplify the link by using only the search
  8652. ;; option.
  8653. (when (and buffer-file-name
  8654. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8655. (let* ((path (match-string 1 link))
  8656. (case-fold-search nil)
  8657. (search (match-string 2 link)))
  8658. (save-match-data
  8659. (if (equal (file-truename buffer-file-name) (file-truename path))
  8660. ;; We are linking to this same file, with a search option
  8661. (setq link search)))))
  8662. ;; Check if we can/should use a relative path. If yes, simplify the link
  8663. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8664. (let* ((type (match-string 1 link))
  8665. (path (match-string 2 link))
  8666. (origpath path)
  8667. (case-fold-search nil))
  8668. (cond
  8669. ((or (eq org-link-file-path-type 'absolute)
  8670. (equal complete-file '(16)))
  8671. (setq path (abbreviate-file-name (expand-file-name path))))
  8672. ((eq org-link-file-path-type 'noabbrev)
  8673. (setq path (expand-file-name path)))
  8674. ((eq org-link-file-path-type 'relative)
  8675. (setq path (file-relative-name path)))
  8676. (t
  8677. (save-match-data
  8678. (if (string-match (concat "^" (regexp-quote
  8679. (expand-file-name
  8680. (file-name-as-directory
  8681. default-directory))))
  8682. (expand-file-name path))
  8683. ;; We are linking a file with relative path name.
  8684. (setq path (substring (expand-file-name path)
  8685. (match-end 0)))
  8686. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8687. (setq link (concat type path))
  8688. (if (equal desc origpath)
  8689. (setq desc path))))
  8690. (if org-make-link-description-function
  8691. (setq desc
  8692. (or (condition-case nil
  8693. (funcall org-make-link-description-function link desc)
  8694. (error (progn (message "Can't get link description from `%s'"
  8695. (symbol-name org-make-link-description-function))
  8696. (sit-for 2) nil)))
  8697. (read-string "Description: " default-description)))
  8698. (if default-description (setq desc default-description)
  8699. (setq desc (or (and auto-desc desc)
  8700. (read-string "Description: " desc)))))
  8701. (unless (string-match "\\S-" desc) (setq desc nil))
  8702. (if remove (apply 'delete-region remove))
  8703. (insert (org-make-link-string link desc))))
  8704. (defun org-link-try-special-completion (type)
  8705. "If there is completion support for link type TYPE, offer it."
  8706. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8707. (if (functionp fun)
  8708. (funcall fun)
  8709. (read-string "Link (no completion support): " (concat type ":")))))
  8710. (defun org-file-complete-link (&optional arg)
  8711. "Create a file link using completion."
  8712. (let (file link)
  8713. (setq file (org-iread-file-name "File: "))
  8714. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8715. (pwd1 (file-name-as-directory (abbreviate-file-name
  8716. (expand-file-name ".")))))
  8717. (cond
  8718. ((equal arg '(16))
  8719. (setq link (concat
  8720. "file:"
  8721. (abbreviate-file-name (expand-file-name file)))))
  8722. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8723. (setq link (concat "file:" (match-string 1 file))))
  8724. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8725. (expand-file-name file))
  8726. (setq link (concat
  8727. "file:" (match-string 1 (expand-file-name file)))))
  8728. (t (setq link (concat "file:" file)))))
  8729. link))
  8730. (defun org-iread-file-name (&rest args)
  8731. "Read-file-name using `ido-mode' speedup if available.
  8732. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8733. See `read-file-name' for a description of parameters."
  8734. (org-without-partial-completion
  8735. (if (and org-completion-use-ido
  8736. (fboundp 'ido-read-file-name)
  8737. (boundp 'ido-mode) ido-mode
  8738. (listp (second args)))
  8739. (let ((ido-enter-matching-directory nil))
  8740. (apply 'ido-read-file-name args))
  8741. (apply 'read-file-name args))))
  8742. (defun org-completing-read (&rest args)
  8743. "Completing-read with SPACE being a normal character."
  8744. (let ((enable-recursive-minibuffers t)
  8745. (minibuffer-local-completion-map
  8746. (copy-keymap minibuffer-local-completion-map)))
  8747. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8748. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8749. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8750. (apply 'org-icompleting-read args)))
  8751. (defun org-completing-read-no-i (&rest args)
  8752. (let (org-completion-use-ido org-completion-use-iswitchb)
  8753. (apply 'org-completing-read args)))
  8754. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8755. "Use iswitch as a completing-read replacement to choose from choices.
  8756. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8757. from."
  8758. (let* ((iswitchb-use-virtual-buffers nil)
  8759. (iswitchb-make-buflist-hook
  8760. (lambda ()
  8761. (setq iswitchb-temp-buflist choices))))
  8762. (iswitchb-read-buffer prompt)))
  8763. (defun org-icompleting-read (&rest args)
  8764. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8765. (org-without-partial-completion
  8766. (if (and org-completion-use-ido
  8767. (fboundp 'ido-completing-read)
  8768. (boundp 'ido-mode) ido-mode
  8769. (listp (second args)))
  8770. (let ((ido-enter-matching-directory nil))
  8771. (apply 'ido-completing-read (concat (car args))
  8772. (if (consp (car (nth 1 args)))
  8773. (mapcar 'car (nth 1 args))
  8774. (nth 1 args))
  8775. (cddr args)))
  8776. (if (and org-completion-use-iswitchb
  8777. (boundp 'iswitchb-mode) iswitchb-mode
  8778. (listp (second args)))
  8779. (apply 'org-iswitchb-completing-read (concat (car args))
  8780. (if (consp (car (nth 1 args)))
  8781. (mapcar 'car (nth 1 args))
  8782. (nth 1 args))
  8783. (cddr args))
  8784. (apply 'completing-read args)))))
  8785. (defun org-extract-attributes (s)
  8786. "Extract the attributes cookie from a string and set as text property."
  8787. (let (a attr (start 0) key value)
  8788. (save-match-data
  8789. (when (string-match "{{\\([^}]+\\)}}$" s)
  8790. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8791. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8792. (setq key (match-string 1 a) value (match-string 2 a)
  8793. start (match-end 0)
  8794. attr (plist-put attr (intern key) value))))
  8795. (org-add-props s nil 'org-attr attr))
  8796. s))
  8797. (defun org-extract-attributes-from-string (tag)
  8798. (let (key value attr)
  8799. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8800. (setq key (match-string 1 tag) value (match-string 2 tag)
  8801. tag (replace-match "" t t tag)
  8802. attr (plist-put attr (intern key) value)))
  8803. (cons tag attr)))
  8804. (defun org-attributes-to-string (plist)
  8805. "Format a property list into an HTML attribute list."
  8806. (let ((s "") key value)
  8807. (while plist
  8808. (setq key (pop plist) value (pop plist))
  8809. (and value
  8810. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8811. s))
  8812. ;;; Opening/following a link
  8813. (defvar org-link-search-failed nil)
  8814. (defvar org-open-link-functions nil
  8815. "Hook for functions finding a plain text link.
  8816. These functions must take a single argument, the link content.
  8817. They will be called for links that look like [[link text][description]]
  8818. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8819. like a filename (e.g. \"./blue.png\").
  8820. These functions will be called *before* Org attempts to resolve the
  8821. link by doing text searches in the current buffer - so if you want a
  8822. link \"[[target]]\" to still find \"<<target>>\", your function should
  8823. handle this as a special case.
  8824. When the function does handle the link, it must return a non-nil value.
  8825. If it decides that it is not responsible for this link, it must return
  8826. nil to indicate that that Org-mode can continue with other options
  8827. like exact and fuzzy text search.")
  8828. (defun org-next-link ()
  8829. "Move forward to the next link.
  8830. If the link is in hidden text, expose it."
  8831. (interactive)
  8832. (when (and org-link-search-failed (eq this-command last-command))
  8833. (goto-char (point-min))
  8834. (message "Link search wrapped back to beginning of buffer"))
  8835. (setq org-link-search-failed nil)
  8836. (let* ((pos (point))
  8837. (ct (org-context))
  8838. (a (assoc :link ct)))
  8839. (if a (goto-char (nth 2 a)))
  8840. (if (re-search-forward org-any-link-re nil t)
  8841. (progn
  8842. (goto-char (match-beginning 0))
  8843. (if (outline-invisible-p) (org-show-context)))
  8844. (goto-char pos)
  8845. (setq org-link-search-failed t)
  8846. (error "No further link found"))))
  8847. (defun org-previous-link ()
  8848. "Move backward to the previous link.
  8849. If the link is in hidden text, expose it."
  8850. (interactive)
  8851. (when (and org-link-search-failed (eq this-command last-command))
  8852. (goto-char (point-max))
  8853. (message "Link search wrapped back to end of buffer"))
  8854. (setq org-link-search-failed nil)
  8855. (let* ((pos (point))
  8856. (ct (org-context))
  8857. (a (assoc :link ct)))
  8858. (if a (goto-char (nth 1 a)))
  8859. (if (re-search-backward org-any-link-re nil t)
  8860. (progn
  8861. (goto-char (match-beginning 0))
  8862. (if (outline-invisible-p) (org-show-context)))
  8863. (goto-char pos)
  8864. (setq org-link-search-failed t)
  8865. (error "No further link found"))))
  8866. (defun org-translate-link (s)
  8867. "Translate a link string if a translation function has been defined."
  8868. (if (and org-link-translation-function
  8869. (fboundp org-link-translation-function)
  8870. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8871. (progn
  8872. (setq s (funcall org-link-translation-function
  8873. (match-string 1 s) (match-string 2 s)))
  8874. (concat (car s) ":" (cdr s)))
  8875. s))
  8876. (defun org-translate-link-from-planner (type path)
  8877. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8878. This is still an experimental function, your mileage may vary."
  8879. (cond
  8880. ((member type '("http" "https" "news" "ftp"))
  8881. ;; standard Internet links are the same.
  8882. nil)
  8883. ((and (equal type "irc") (string-match "^//" path))
  8884. ;; Planner has two / at the beginning of an irc link, we have 1.
  8885. ;; We should have zero, actually....
  8886. (setq path (substring path 1)))
  8887. ((and (equal type "lisp") (string-match "^/" path))
  8888. ;; Planner has a slash, we do not.
  8889. (setq type "elisp" path (substring path 1)))
  8890. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8891. ;; A typical message link. Planner has the id after the final slash,
  8892. ;; we separate it with a hash mark
  8893. (setq path (concat (match-string 1 path) "#"
  8894. (org-remove-angle-brackets (match-string 2 path)))))
  8895. )
  8896. (cons type path))
  8897. (defun org-find-file-at-mouse (ev)
  8898. "Open file link or URL at mouse."
  8899. (interactive "e")
  8900. (mouse-set-point ev)
  8901. (org-open-at-point 'in-emacs))
  8902. (defun org-open-at-mouse (ev)
  8903. "Open file link or URL at mouse.
  8904. See the docstring of `org-open-file' for details."
  8905. (interactive "e")
  8906. (mouse-set-point ev)
  8907. (if (eq major-mode 'org-agenda-mode)
  8908. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8909. (org-open-at-point))
  8910. (defvar org-window-config-before-follow-link nil
  8911. "The window configuration before following a link.
  8912. This is saved in case the need arises to restore it.")
  8913. (defvar org-open-link-marker (make-marker)
  8914. "Marker pointing to the location where `org-open-at-point; was called.")
  8915. ;;;###autoload
  8916. (defun org-open-at-point-global ()
  8917. "Follow a link like Org-mode does.
  8918. This command can be called in any mode to follow a link that has
  8919. Org-mode syntax."
  8920. (interactive)
  8921. (org-run-like-in-org-mode 'org-open-at-point))
  8922. ;;;###autoload
  8923. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8924. "Open a link in the string S, as if it was in Org-mode."
  8925. (interactive "sLink: \nP")
  8926. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8927. (with-temp-buffer
  8928. (let ((org-inhibit-startup (not reference-buffer)))
  8929. (org-mode)
  8930. (insert s)
  8931. (goto-char (point-min))
  8932. (when reference-buffer
  8933. (setq org-link-abbrev-alist-local
  8934. (with-current-buffer reference-buffer
  8935. org-link-abbrev-alist-local)))
  8936. (org-open-at-point arg reference-buffer)))))
  8937. (defvar org-open-at-point-functions nil
  8938. "Hook that is run when following a link at point.
  8939. Functions in this hook must return t if they identify and follow
  8940. a link at point. If they don't find anything interesting at point,
  8941. they must return nil.")
  8942. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8943. (defun org-open-at-point (&optional arg reference-buffer)
  8944. "Open link at or after point.
  8945. If there is no link at point, this function will search forward up to
  8946. the end of the current line.
  8947. Normally, files will be opened by an appropriate application. If the
  8948. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8949. With a double prefix argument, try to open outside of Emacs, in the
  8950. application the system uses for this file type."
  8951. (interactive "P")
  8952. ;; if in a code block, then open the block's results
  8953. (unless (call-interactively #'org-babel-open-src-block-result)
  8954. (org-load-modules-maybe)
  8955. (move-marker org-open-link-marker (point))
  8956. (setq org-window-config-before-follow-link (current-window-configuration))
  8957. (org-remove-occur-highlights nil nil t)
  8958. (cond
  8959. ((and (org-at-heading-p)
  8960. (not (org-at-timestamp-p t))
  8961. (not (org-in-regexp
  8962. (concat org-plain-link-re "\\|"
  8963. org-bracket-link-regexp "\\|"
  8964. org-angle-link-re "\\|"
  8965. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8966. (not (get-text-property (point) 'org-linked-text)))
  8967. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8968. (lk0 (car lkall))
  8969. (lk (if (stringp lk0) (list lk0) lk0))
  8970. (lkend (cdr lkall)))
  8971. (mapcar (lambda(l)
  8972. (search-forward l nil lkend)
  8973. (goto-char (match-beginning 0))
  8974. (org-open-at-point))
  8975. lk))
  8976. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8977. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8978. ((and (org-at-timestamp-p t)
  8979. (not (org-in-regexp org-bracket-link-regexp)))
  8980. (org-follow-timestamp-link))
  8981. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8982. (not (org-in-regexp org-any-link-re)))
  8983. (org-footnote-action))
  8984. (t
  8985. (let (type path link line search (pos (point)))
  8986. (catch 'match
  8987. (save-excursion
  8988. (skip-chars-forward "^]\n\r")
  8989. (when (org-in-regexp org-bracket-link-regexp 1)
  8990. (setq link (org-extract-attributes
  8991. (org-link-unescape (org-match-string-no-properties 1))))
  8992. (while (string-match " *\n *" link)
  8993. (setq link (replace-match " " t t link)))
  8994. (setq link (org-link-expand-abbrev link))
  8995. (cond
  8996. ((or (file-name-absolute-p link)
  8997. (string-match "^\\.\\.?/" link))
  8998. (setq type "file" path link))
  8999. ((string-match org-link-re-with-space3 link)
  9000. (setq type (match-string 1 link) path (match-string 2 link)))
  9001. ((string-match "^help:+\\(.+\\)" link)
  9002. (setq type "help" path (match-string 1 link)))
  9003. (t (setq type "thisfile" path link)))
  9004. (throw 'match t)))
  9005. (when (get-text-property (point) 'org-linked-text)
  9006. (setq type "thisfile"
  9007. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9008. (1+ (point)) (point))
  9009. path (buffer-substring
  9010. (or (previous-single-property-change pos 'org-linked-text)
  9011. (point-min))
  9012. (or (next-single-property-change pos 'org-linked-text)
  9013. (point-max))))
  9014. (throw 'match t))
  9015. (save-excursion
  9016. (when (or (org-in-regexp org-angle-link-re)
  9017. (let ((match (org-in-regexp org-plain-link-re)))
  9018. ;; Check a plain link is not within a bracket link
  9019. (and match
  9020. (save-excursion
  9021. (progn
  9022. (goto-char (car match))
  9023. (not (org-in-regexp org-bracket-link-regexp))))))
  9024. (let ((line_ending (save-excursion (end-of-line) (point))))
  9025. ;; We are in a line before a plain or bracket link
  9026. (or (re-search-forward org-plain-link-re line_ending t)
  9027. (re-search-forward org-bracket-link-regexp line_ending t))))
  9028. (setq type (match-string 1)
  9029. path (org-link-unescape (match-string 2)))
  9030. (throw 'match t)))
  9031. (save-excursion
  9032. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9033. (setq type "tags"
  9034. path (match-string 1))
  9035. (while (string-match ":" path)
  9036. (setq path (replace-match "+" t t path)))
  9037. (throw 'match t)))
  9038. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9039. (setq type "tree-match"
  9040. path (match-string 1))
  9041. (throw 'match t)))
  9042. (unless path
  9043. (user-error "No link found"))
  9044. ;; switch back to reference buffer
  9045. ;; needed when if called in a temporary buffer through
  9046. ;; org-open-link-from-string
  9047. (with-current-buffer (or reference-buffer (current-buffer))
  9048. ;; Remove any trailing spaces in path
  9049. (if (string-match " +\\'" path)
  9050. (setq path (replace-match "" t t path)))
  9051. (if (and org-link-translation-function
  9052. (fboundp org-link-translation-function))
  9053. ;; Check if we need to translate the link
  9054. (let ((tmp (funcall org-link-translation-function type path)))
  9055. (setq type (car tmp) path (cdr tmp))))
  9056. (cond
  9057. ((assoc type org-link-protocols)
  9058. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9059. ((equal type "help")
  9060. (let ((f-or-v (intern path)))
  9061. (cond ((fboundp f-or-v)
  9062. (describe-function f-or-v))
  9063. ((boundp f-or-v)
  9064. (describe-variable f-or-v))
  9065. (t (error "Not a known function or variable")))))
  9066. ((equal type "mailto")
  9067. (let ((cmd (car org-link-mailto-program))
  9068. (args (cdr org-link-mailto-program)) args1
  9069. (address path) (subject "") a)
  9070. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9071. (setq address (match-string 1 path)
  9072. subject (org-link-escape (match-string 2 path))))
  9073. (while args
  9074. (cond
  9075. ((not (stringp (car args))) (push (pop args) args1))
  9076. (t (setq a (pop args))
  9077. (if (string-match "%a" a)
  9078. (setq a (replace-match address t t a)))
  9079. (if (string-match "%s" a)
  9080. (setq a (replace-match subject t t a)))
  9081. (push a args1))))
  9082. (apply cmd (nreverse args1))))
  9083. ((member type '("http" "https" "ftp" "news"))
  9084. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9085. (org-link-escape
  9086. path org-link-escape-chars-browser)
  9087. path))))
  9088. ((string= type "doi")
  9089. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9090. (org-link-escape
  9091. path org-link-escape-chars-browser)
  9092. path))))
  9093. ((member type '("message"))
  9094. (browse-url (concat type ":" path)))
  9095. ((string= type "tags")
  9096. (org-tags-view arg path))
  9097. ((string= type "tree-match")
  9098. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9099. ((string= type "file")
  9100. (if (string-match "::\\([0-9]+\\)\\'" path)
  9101. (setq line (string-to-number (match-string 1 path))
  9102. path (substring path 0 (match-beginning 0)))
  9103. (if (string-match "::\\(.+\\)\\'" path)
  9104. (setq search (match-string 1 path)
  9105. path (substring path 0 (match-beginning 0)))))
  9106. (if (string-match "[*?{]" (file-name-nondirectory path))
  9107. (dired path)
  9108. (org-open-file path arg line search)))
  9109. ((string= type "shell")
  9110. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9111. (cmd path))
  9112. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9113. (string-match org-confirm-shell-link-not-regexp cmd))
  9114. (not org-confirm-shell-link-function)
  9115. (funcall org-confirm-shell-link-function
  9116. (format "Execute \"%s\" in shell? "
  9117. (org-add-props cmd nil
  9118. 'face 'org-warning))))
  9119. (progn
  9120. (message "Executing %s" cmd)
  9121. (shell-command cmd buf)
  9122. (if (featurep 'midnight)
  9123. (setq clean-buffer-list-kill-buffer-names
  9124. (cons buf clean-buffer-list-kill-buffer-names))))
  9125. (error "Abort"))))
  9126. ((string= type "elisp")
  9127. (let ((cmd path))
  9128. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9129. (string-match org-confirm-elisp-link-not-regexp cmd))
  9130. (not org-confirm-elisp-link-function)
  9131. (funcall org-confirm-elisp-link-function
  9132. (format "Execute \"%s\" as elisp? "
  9133. (org-add-props cmd nil
  9134. 'face 'org-warning))))
  9135. (message "%s => %s" cmd
  9136. (if (equal (string-to-char cmd) ?\()
  9137. (eval (read cmd))
  9138. (call-interactively (read cmd))))
  9139. (error "Abort"))))
  9140. ((and (string= type "thisfile")
  9141. (run-hook-with-args-until-success
  9142. 'org-open-link-functions path)))
  9143. ((string= type "thisfile")
  9144. (if arg
  9145. (switch-to-buffer-other-window
  9146. (org-get-buffer-for-internal-link (current-buffer)))
  9147. (org-mark-ring-push))
  9148. (let ((cmd `(org-link-search
  9149. ,path
  9150. ,(cond ((equal arg '(4)) ''occur)
  9151. ((equal arg '(16)) ''org-occur))
  9152. ,pos)))
  9153. (condition-case nil (let ((org-link-search-inhibit-query t))
  9154. (eval cmd))
  9155. (error (progn (widen) (eval cmd))))))
  9156. (t (browse-url-at-point)))))))
  9157. (move-marker org-open-link-marker nil)
  9158. (run-hook-with-args 'org-follow-link-hook)))
  9159. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9160. "Offer links in the current entry and return the selected link.
  9161. If there is only one link, return it.
  9162. If NTH is an integer, return the NTH link found.
  9163. If ZERO is a string, check also this string for a link, and if
  9164. there is one, return it."
  9165. (with-current-buffer buffer
  9166. (save-excursion
  9167. (save-restriction
  9168. (widen)
  9169. (goto-char marker)
  9170. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9171. "\\(" org-angle-link-re "\\)\\|"
  9172. "\\(" org-plain-link-re "\\)"))
  9173. (cnt ?0)
  9174. (in-emacs (if (integerp nth) nil nth))
  9175. have-zero end links link c)
  9176. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9177. (push (match-string 0 zero) links)
  9178. (setq cnt (1- cnt) have-zero t))
  9179. (save-excursion
  9180. (org-back-to-heading t)
  9181. (setq end (save-excursion (outline-next-heading) (point)))
  9182. (while (re-search-forward re end t)
  9183. (push (match-string 0) links))
  9184. (setq links (org-uniquify (reverse links))))
  9185. (cond
  9186. ((null links)
  9187. (message "No links"))
  9188. ((equal (length links) 1)
  9189. (setq link (car links)))
  9190. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9191. (setq link (nth (if have-zero nth (1- nth)) links)))
  9192. (t ; we have to select a link
  9193. (save-excursion
  9194. (save-window-excursion
  9195. (delete-other-windows)
  9196. (with-output-to-temp-buffer "*Select Link*"
  9197. (mapc (lambda (l)
  9198. (if (not (string-match org-bracket-link-regexp l))
  9199. (princ (format "[%c] %s\n" (incf cnt)
  9200. (org-remove-angle-brackets l)))
  9201. (if (match-end 3)
  9202. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9203. (match-string 3 l) (match-string 1 l)))
  9204. (princ (format "[%c] %s\n" (incf cnt)
  9205. (match-string 1 l))))))
  9206. links))
  9207. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9208. (message "Select link to open, RET to open all:")
  9209. (setq c (read-char-exclusive))
  9210. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9211. (when (equal c ?q) (error "Abort"))
  9212. (if (equal c ?\C-m)
  9213. (setq link links)
  9214. (setq nth (- c ?0))
  9215. (if have-zero (setq nth (1+ nth)))
  9216. (unless (and (integerp nth) (>= (length links) nth))
  9217. (error "Invalid link selection"))
  9218. (setq link (nth (1- nth) links)))))
  9219. (cons link end))))))
  9220. ;; Add special file links that specify the way of opening
  9221. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9222. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9223. (defun org-open-file-with-system (path)
  9224. "Open file at PATH using the system way of opening it."
  9225. (org-open-file path 'system))
  9226. (defun org-open-file-with-emacs (path)
  9227. "Open file at PATH in Emacs."
  9228. (org-open-file path 'emacs))
  9229. ;;; File search
  9230. (defvar org-create-file-search-functions nil
  9231. "List of functions to construct the right search string for a file link.
  9232. These functions are called in turn with point at the location to
  9233. which the link should point.
  9234. A function in the hook should first test if it would like to
  9235. handle this file type, for example by checking the `major-mode'
  9236. or the file extension. If it decides not to handle this file, it
  9237. should just return nil to give other functions a chance. If it
  9238. does handle the file, it must return the search string to be used
  9239. when following the link. The search string will be part of the
  9240. file link, given after a double colon, and `org-open-at-point'
  9241. will automatically search for it. If special measures must be
  9242. taken to make the search successful, another function should be
  9243. added to the companion hook `org-execute-file-search-functions',
  9244. which see.
  9245. A function in this hook may also use `setq' to set the variable
  9246. `description' to provide a suggestion for the descriptive text to
  9247. be used for this link when it gets inserted into an Org-mode
  9248. buffer with \\[org-insert-link].")
  9249. (defvar org-execute-file-search-functions nil
  9250. "List of functions to execute a file search triggered by a link.
  9251. Functions added to this hook must accept a single argument, the
  9252. search string that was part of the file link, the part after the
  9253. double colon. The function must first check if it would like to
  9254. handle this search, for example by checking the `major-mode' or
  9255. the file extension. If it decides not to handle this search, it
  9256. should just return nil to give other functions a chance. If it
  9257. does handle the search, it must return a non-nil value to keep
  9258. other functions from trying.
  9259. Each function can access the current prefix argument through the
  9260. variable `current-prefix-argument'. Note that a single prefix is
  9261. used to force opening a link in Emacs, so it may be good to only
  9262. use a numeric or double prefix to guide the search function.
  9263. In case this is needed, a function in this hook can also restore
  9264. the window configuration before `org-open-at-point' was called using:
  9265. (set-window-configuration org-window-config-before-follow-link)")
  9266. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9267. (defun org-link-search (s &optional type avoid-pos stealth)
  9268. "Search for a link search option.
  9269. If S is surrounded by forward slashes, it is interpreted as a
  9270. regular expression. In org-mode files, this will create an `org-occur'
  9271. sparse tree. In ordinary files, `occur' will be used to list matches.
  9272. If the current buffer is in `dired-mode', grep will be used to search
  9273. in all files. If AVOID-POS is given, ignore matches near that position.
  9274. When optional argument STEALTH is non-nil, do not modify
  9275. visibility around point, thus ignoring
  9276. `org-show-hierarchy-above', `org-show-following-heading' and
  9277. `org-show-siblings' variables."
  9278. (let ((case-fold-search t)
  9279. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9280. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9281. (append '(("") (" ") ("\t") ("\n"))
  9282. org-emphasis-alist)
  9283. "\\|") "\\)"))
  9284. (pos (point))
  9285. (pre nil) (post nil)
  9286. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9287. (cond
  9288. ;; First check if there are any special search functions
  9289. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9290. ;; Now try the builtin stuff
  9291. ((and (equal (string-to-char s0) ?#)
  9292. (> (length s0) 1)
  9293. (save-excursion
  9294. (goto-char (point-min))
  9295. (and
  9296. (re-search-forward
  9297. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9298. (setq type 'dedicated
  9299. pos (match-beginning 0))))
  9300. ;; There is an exact target for this
  9301. (goto-char pos)
  9302. (org-back-to-heading t)))
  9303. ((save-excursion
  9304. (goto-char (point-min))
  9305. (and
  9306. (re-search-forward
  9307. (concat "<<" (regexp-quote s0) ">>") nil t)
  9308. (setq type 'dedicated
  9309. pos (match-beginning 0))))
  9310. ;; There is an exact target for this
  9311. (goto-char pos))
  9312. ((save-excursion
  9313. (goto-char (point-min))
  9314. (and
  9315. (re-search-forward
  9316. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9317. (setq type 'dedicated pos (match-beginning 0))))
  9318. ;; Found an invisible target.
  9319. (goto-char pos))
  9320. ((save-excursion
  9321. (goto-char (point-min))
  9322. (and
  9323. (re-search-forward
  9324. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9325. (setq type 'dedicated pos (match-beginning 0))))
  9326. ;; Found an element with a matching #+name affiliated keyword.
  9327. (goto-char pos))
  9328. ((and (string-match "^(\\(.*\\))$" s0)
  9329. (save-excursion
  9330. (goto-char (point-min))
  9331. (and
  9332. (re-search-forward
  9333. (concat "[^[]" (regexp-quote
  9334. (format org-coderef-label-format
  9335. (match-string 1 s0))))
  9336. nil t)
  9337. (setq type 'dedicated
  9338. pos (1+ (match-beginning 0))))))
  9339. ;; There is a coderef target for this
  9340. (goto-char pos))
  9341. ((string-match "^/\\(.*\\)/$" s)
  9342. ;; A regular expression
  9343. (cond
  9344. ((derived-mode-p 'org-mode)
  9345. (org-occur (match-string 1 s)))
  9346. (t (org-do-occur (match-string 1 s)))))
  9347. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9348. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9349. (goto-char (point-min))
  9350. (cond
  9351. ((let (case-fold-search)
  9352. (re-search-forward (format org-complex-heading-regexp-format
  9353. (regexp-quote s))
  9354. nil t))
  9355. ;; OK, found a match
  9356. (setq type 'dedicated)
  9357. (goto-char (match-beginning 0)))
  9358. ((and (not org-link-search-inhibit-query)
  9359. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9360. (y-or-n-p "No match - create this as a new heading? "))
  9361. (goto-char (point-max))
  9362. (or (bolp) (newline))
  9363. (insert "* " s "\n")
  9364. (beginning-of-line 0))
  9365. (t
  9366. (goto-char pos)
  9367. (error "No match"))))
  9368. (t
  9369. ;; A normal search string
  9370. (when (equal (string-to-char s) ?*)
  9371. ;; Anchor on headlines, post may include tags.
  9372. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9373. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9374. s (substring s 1)))
  9375. (remove-text-properties
  9376. 0 (length s)
  9377. '(face nil mouse-face nil keymap nil fontified nil) s)
  9378. ;; Make a series of regular expressions to find a match
  9379. (setq words (org-split-string s "[ \n\r\t]+")
  9380. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9381. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9382. "\\)" markers)
  9383. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9384. re2a (concat "[ \t\r\n]" re2a_)
  9385. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9386. re4 (concat "[^a-zA-Z_]" re4_)
  9387. re1 (concat pre re2 post)
  9388. re3 (concat pre (if pre re4_ re4) post)
  9389. re5 (concat pre ".*" re4)
  9390. re2 (concat pre re2)
  9391. re2a (concat pre (if pre re2a_ re2a))
  9392. re4 (concat pre (if pre re4_ re4))
  9393. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9394. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9395. re5 "\\)"
  9396. ))
  9397. (cond
  9398. ((eq type 'org-occur) (org-occur reall))
  9399. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9400. (t (goto-char (point-min))
  9401. (setq type 'fuzzy)
  9402. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9403. (org-search-not-self 1 re1 nil t)
  9404. (org-search-not-self 1 re2 nil t)
  9405. (org-search-not-self 1 re2a nil t)
  9406. (org-search-not-self 1 re3 nil t)
  9407. (org-search-not-self 1 re4 nil t)
  9408. (org-search-not-self 1 re5 nil t)
  9409. )
  9410. (goto-char (match-beginning 1))
  9411. (goto-char pos)
  9412. (error "No match"))))))
  9413. (and (derived-mode-p 'org-mode)
  9414. (not stealth)
  9415. (org-show-context 'link-search))
  9416. type))
  9417. (defun org-search-not-self (group &rest args)
  9418. "Execute `re-search-forward', but only accept matches that do not
  9419. enclose the position of `org-open-link-marker'."
  9420. (let ((m org-open-link-marker))
  9421. (catch 'exit
  9422. (while (apply 're-search-forward args)
  9423. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9424. (goto-char (match-end group))
  9425. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9426. (> (match-beginning 0) (marker-position m))
  9427. (< (match-end 0) (marker-position m)))
  9428. (save-match-data
  9429. (or (not (org-in-regexp
  9430. org-bracket-link-analytic-regexp 1))
  9431. (not (match-end 4)) ; no description
  9432. (and (<= (match-beginning 4) (point))
  9433. (>= (match-end 4) (point))))))
  9434. (throw 'exit (point))))))))
  9435. (defun org-get-buffer-for-internal-link (buffer)
  9436. "Return a buffer to be used for displaying the link target of internal links."
  9437. (cond
  9438. ((not org-display-internal-link-with-indirect-buffer)
  9439. buffer)
  9440. ((string-match "(Clone)$" (buffer-name buffer))
  9441. (message "Buffer is already a clone, not making another one")
  9442. ;; we also do not modify visibility in this case
  9443. buffer)
  9444. (t ; make a new indirect buffer for displaying the link
  9445. (let* ((bn (buffer-name buffer))
  9446. (ibn (concat bn "(Clone)"))
  9447. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9448. (with-current-buffer ib (org-overview))
  9449. ib))))
  9450. (defun org-do-occur (regexp &optional cleanup)
  9451. "Call the Emacs command `occur'.
  9452. If CLEANUP is non-nil, remove the printout of the regular expression
  9453. in the *Occur* buffer. This is useful if the regex is long and not useful
  9454. to read."
  9455. (occur regexp)
  9456. (when cleanup
  9457. (let ((cwin (selected-window)) win beg end)
  9458. (when (setq win (get-buffer-window "*Occur*"))
  9459. (select-window win))
  9460. (goto-char (point-min))
  9461. (when (re-search-forward "match[a-z]+" nil t)
  9462. (setq beg (match-end 0))
  9463. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9464. (setq end (1- (match-beginning 0)))))
  9465. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9466. (goto-char (point-min))
  9467. (select-window cwin))))
  9468. ;;; The mark ring for links jumps
  9469. (defvar org-mark-ring nil
  9470. "Mark ring for positions before jumps in Org-mode.")
  9471. (defvar org-mark-ring-last-goto nil
  9472. "Last position in the mark ring used to go back.")
  9473. ;; Fill and close the ring
  9474. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9475. (loop for i from 1 to org-mark-ring-length do
  9476. (push (make-marker) org-mark-ring))
  9477. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9478. org-mark-ring)
  9479. (defun org-mark-ring-push (&optional pos buffer)
  9480. "Put the current position or POS into the mark ring and rotate it."
  9481. (interactive)
  9482. (setq pos (or pos (point)))
  9483. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9484. (move-marker (car org-mark-ring)
  9485. (or pos (point))
  9486. (or buffer (current-buffer)))
  9487. (message "%s"
  9488. (substitute-command-keys
  9489. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9490. (defun org-mark-ring-goto (&optional n)
  9491. "Jump to the previous position in the mark ring.
  9492. With prefix arg N, jump back that many stored positions. When
  9493. called several times in succession, walk through the entire ring.
  9494. Org-mode commands jumping to a different position in the current file,
  9495. or to another Org-mode file, automatically push the old position
  9496. onto the ring."
  9497. (interactive "p")
  9498. (let (p m)
  9499. (if (eq last-command this-command)
  9500. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9501. (setq p org-mark-ring))
  9502. (setq org-mark-ring-last-goto p)
  9503. (setq m (car p))
  9504. (org-pop-to-buffer-same-window (marker-buffer m))
  9505. (goto-char m)
  9506. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9507. (defun org-remove-angle-brackets (s)
  9508. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9509. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9510. s)
  9511. (defun org-add-angle-brackets (s)
  9512. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9513. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9514. s)
  9515. (defun org-remove-double-quotes (s)
  9516. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9517. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9518. s)
  9519. ;;; Following specific links
  9520. (defun org-follow-timestamp-link ()
  9521. "Open an agenda view for the time-stamp date/range at point."
  9522. (cond
  9523. ((org-at-date-range-p t)
  9524. (let ((org-agenda-start-on-weekday)
  9525. (t1 (match-string 1))
  9526. (t2 (match-string 2)) tt1 tt2)
  9527. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9528. tt2 (time-to-days (org-time-string-to-time t2)))
  9529. (let ((org-agenda-buffer-tmp-name
  9530. (format "*Org Agenda(a:%s)"
  9531. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9532. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9533. ((org-at-timestamp-p t)
  9534. (let ((org-agenda-buffer-tmp-name
  9535. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9536. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9537. (substring (match-string 1) 0 10)))
  9538. 1)))
  9539. (t (error "This should not happen"))))
  9540. ;;; Following file links
  9541. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9542. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9543. (declare-function mailcap-mime-info
  9544. "mailcap" (string &optional request no-decode))
  9545. (defvar org-wait nil)
  9546. (defun org-open-file (path &optional in-emacs line search)
  9547. "Open the file at PATH.
  9548. First, this expands any special file name abbreviations. Then the
  9549. configuration variable `org-file-apps' is checked if it contains an
  9550. entry for this file type, and if yes, the corresponding command is launched.
  9551. If no application is found, Emacs simply visits the file.
  9552. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9553. With a double \\[universal-argument] \\[universal-argument] \
  9554. prefix arg, Org tries to avoid opening in Emacs
  9555. and to use an external application to visit the file.
  9556. Optional LINE specifies a line to go to, optional SEARCH a string
  9557. to search for. If LINE or SEARCH is given, the file will be
  9558. opened in Emacs, unless an entry from org-file-apps that makes
  9559. use of groups in a regexp matches.
  9560. If you want to change the way frames are used when following a
  9561. link, please customize `org-link-frame-setup'.
  9562. If the file does not exist, an error is thrown."
  9563. (let* ((file (if (equal path "")
  9564. buffer-file-name
  9565. (substitute-in-file-name (expand-file-name path))))
  9566. (file-apps (append org-file-apps (org-default-apps)))
  9567. (apps (org-remove-if
  9568. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9569. (apps-dlink (org-remove-if-not
  9570. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9571. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9572. (dirp (if remp nil (file-directory-p file)))
  9573. (file (if (and dirp org-open-directory-means-index-dot-org)
  9574. (concat (file-name-as-directory file) "index.org")
  9575. file))
  9576. (a-m-a-p (assq 'auto-mode apps))
  9577. (dfile (downcase file))
  9578. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9579. (link (cond ((and (eq line nil)
  9580. (eq search nil))
  9581. file)
  9582. (line
  9583. (concat file "::" (number-to-string line)))
  9584. (search
  9585. (concat file "::" search))))
  9586. (dlink (downcase link))
  9587. (old-buffer (current-buffer))
  9588. (old-pos (point))
  9589. (old-mode major-mode)
  9590. ext cmd link-match-data)
  9591. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9592. (setq ext (match-string 1 dfile))
  9593. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9594. (setq ext (match-string 1 dfile))))
  9595. (cond
  9596. ((member in-emacs '((16) system))
  9597. (setq cmd (cdr (assoc 'system apps))))
  9598. (in-emacs (setq cmd 'emacs))
  9599. (t
  9600. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9601. (and dirp (cdr (assoc 'directory apps)))
  9602. ; first, try matching against apps-dlink
  9603. ; if we get a match here, store the match data for later
  9604. (let ((match (assoc-default dlink apps-dlink
  9605. 'string-match)))
  9606. (if match
  9607. (progn (setq link-match-data (match-data))
  9608. match)
  9609. (progn (setq in-emacs (or in-emacs line search))
  9610. nil))) ; if we have no match in apps-dlink,
  9611. ; always open the file in emacs if line or search
  9612. ; is given (for backwards compatibility)
  9613. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9614. 'string-match)
  9615. (cdr (assoc ext apps))
  9616. (cdr (assoc t apps))))))
  9617. (when (eq cmd 'system)
  9618. (setq cmd (cdr (assoc 'system apps))))
  9619. (when (eq cmd 'default)
  9620. (setq cmd (cdr (assoc t apps))))
  9621. (when (eq cmd 'mailcap)
  9622. (require 'mailcap)
  9623. (mailcap-parse-mailcaps)
  9624. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9625. (command (mailcap-mime-info mime-type)))
  9626. (if (stringp command)
  9627. (setq cmd command)
  9628. (setq cmd 'emacs))))
  9629. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9630. (not (file-exists-p file))
  9631. (not org-open-non-existing-files))
  9632. (error "No such file: %s" file))
  9633. (cond
  9634. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9635. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9636. (while (string-match "['\"]%s['\"]" cmd)
  9637. (setq cmd (replace-match "%s" t t cmd)))
  9638. (while (string-match "%s" cmd)
  9639. (setq cmd (replace-match
  9640. (save-match-data
  9641. (shell-quote-argument
  9642. (convert-standard-filename file)))
  9643. t t cmd)))
  9644. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9645. (save-match-data
  9646. (let ((match-index 1)
  9647. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9648. (set-match-data link-match-data)
  9649. (while (<= match-index number-of-groups)
  9650. (let ((regex (concat "%" (number-to-string match-index)))
  9651. (replace-with (match-string match-index dlink)))
  9652. (while (string-match regex cmd)
  9653. (setq cmd (replace-match replace-with t t cmd))))
  9654. (setq match-index (+ match-index 1)))))
  9655. (save-window-excursion
  9656. (message "Running %s...done" cmd)
  9657. (start-process-shell-command cmd nil cmd)
  9658. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9659. ((or (stringp cmd)
  9660. (eq cmd 'emacs))
  9661. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9662. (widen)
  9663. (if line (org-goto-line line)
  9664. (if search (org-link-search search))))
  9665. ((consp cmd)
  9666. (let ((file (convert-standard-filename file)))
  9667. (save-match-data
  9668. (set-match-data link-match-data)
  9669. (eval cmd))))
  9670. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9671. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9672. (or (not (equal old-buffer (current-buffer)))
  9673. (not (equal old-pos (point))))
  9674. (org-mark-ring-push old-pos old-buffer))))
  9675. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9676. "This function returns non-nil if `entry' uses a regular
  9677. expression which should be matched against the whole link by
  9678. org-open-file.
  9679. It assumes that is the case when the entry uses a regular
  9680. expression which has at least one grouping construct and the
  9681. action is either a lisp form or a command string containing
  9682. '%1', i.e. using at least one subexpression match as a
  9683. parameter."
  9684. (let ((selector (car entry))
  9685. (action (cdr entry)))
  9686. (if (stringp selector)
  9687. (and (> (regexp-opt-depth selector) 0)
  9688. (or (and (stringp action)
  9689. (string-match "%[0-9]" action))
  9690. (consp action)))
  9691. nil)))
  9692. (defun org-default-apps ()
  9693. "Return the default applications for this operating system."
  9694. (cond
  9695. ((eq system-type 'darwin)
  9696. org-file-apps-defaults-macosx)
  9697. ((eq system-type 'windows-nt)
  9698. org-file-apps-defaults-windowsnt)
  9699. (t org-file-apps-defaults-gnu)))
  9700. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9701. "Convert extensions to regular expressions in the cars of LIST.
  9702. Also, weed out any non-string entries, because the return value is used
  9703. only for regexp matching.
  9704. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9705. point to the symbol `emacs', indicating that the file should
  9706. be opened in Emacs."
  9707. (append
  9708. (delq nil
  9709. (mapcar (lambda (x)
  9710. (if (not (stringp (car x)))
  9711. nil
  9712. (if (string-match "\\W" (car x))
  9713. x
  9714. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9715. list))
  9716. (if add-auto-mode
  9717. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9718. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9719. (defun org-file-remote-p (file)
  9720. "Test whether FILE specifies a location on a remote system.
  9721. Return non-nil if the location is indeed remote.
  9722. For example, the filename \"/user@host:/foo\" specifies a location
  9723. on the system \"/user@host:\"."
  9724. (cond ((fboundp 'file-remote-p)
  9725. (file-remote-p file))
  9726. ((fboundp 'tramp-handle-file-remote-p)
  9727. (tramp-handle-file-remote-p file))
  9728. ((and (boundp 'ange-ftp-name-format)
  9729. (string-match (car ange-ftp-name-format) file))
  9730. t)))
  9731. ;;;; Refiling
  9732. (defun org-get-org-file ()
  9733. "Read a filename, with default directory `org-directory'."
  9734. (let ((default (or org-default-notes-file remember-data-file)))
  9735. (read-file-name (format "File name [%s]: " default)
  9736. (file-name-as-directory org-directory)
  9737. default)))
  9738. (defun org-notes-order-reversed-p ()
  9739. "Check if the current file should receive notes in reversed order."
  9740. (cond
  9741. ((not org-reverse-note-order) nil)
  9742. ((eq t org-reverse-note-order) t)
  9743. ((not (listp org-reverse-note-order)) nil)
  9744. (t (catch 'exit
  9745. (let ((all org-reverse-note-order)
  9746. entry)
  9747. (while (setq entry (pop all))
  9748. (if (string-match (car entry) buffer-file-name)
  9749. (throw 'exit (cdr entry))))
  9750. nil)))))
  9751. (defvar org-refile-target-table nil
  9752. "The list of refile targets, created by `org-refile'.")
  9753. (defvar org-agenda-new-buffers nil
  9754. "Buffers created to visit agenda files.")
  9755. (defvar org-refile-cache nil
  9756. "Cache for refile targets.")
  9757. (defvar org-refile-markers nil
  9758. "All the markers used for caching refile locations.")
  9759. (defun org-refile-marker (pos)
  9760. "Get a new refile marker, but only if caching is in use."
  9761. (if (not org-refile-use-cache)
  9762. pos
  9763. (let ((m (make-marker)))
  9764. (move-marker m pos)
  9765. (push m org-refile-markers)
  9766. m)))
  9767. (defun org-refile-cache-clear ()
  9768. "Clear the refile cache and disable all the markers."
  9769. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9770. (setq org-refile-markers nil)
  9771. (setq org-refile-cache nil)
  9772. (message "Refile cache has been cleared"))
  9773. (defun org-refile-cache-check-set (set)
  9774. "Check if all the markers in the cache still have live buffers."
  9775. (let (marker)
  9776. (catch 'exit
  9777. (while (and set (setq marker (nth 3 (pop set))))
  9778. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9779. (when (and marker (null (marker-buffer marker)))
  9780. (message "not found") (sit-for 3)
  9781. (throw 'exit nil)))
  9782. t)))
  9783. (defun org-refile-cache-put (set &rest identifiers)
  9784. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9785. (let* ((key (sha1 (prin1-to-string identifiers)))
  9786. (entry (assoc key org-refile-cache)))
  9787. (if entry
  9788. (setcdr entry set)
  9789. (push (cons key set) org-refile-cache))))
  9790. (defun org-refile-cache-get (&rest identifiers)
  9791. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9792. (cond
  9793. ((not org-refile-cache) nil)
  9794. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9795. (t
  9796. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9797. org-refile-cache))))
  9798. (and set (org-refile-cache-check-set set) set)))))
  9799. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9800. "Produce a table with refile targets."
  9801. (let ((case-fold-search nil)
  9802. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9803. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9804. targets tgs txt re files f desc descre fast-path-p level pos0)
  9805. (message "Getting targets...")
  9806. (with-current-buffer (or default-buffer (current-buffer))
  9807. (while (setq entry (pop entries))
  9808. (setq files (car entry) desc (cdr entry))
  9809. (setq fast-path-p nil)
  9810. (cond
  9811. ((null files) (setq files (list (current-buffer))))
  9812. ((eq files 'org-agenda-files)
  9813. (setq files (org-agenda-files 'unrestricted)))
  9814. ((and (symbolp files) (fboundp files))
  9815. (setq files (funcall files)))
  9816. ((and (symbolp files) (boundp files))
  9817. (setq files (symbol-value files))))
  9818. (if (stringp files) (setq files (list files)))
  9819. (cond
  9820. ((eq (car desc) :tag)
  9821. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9822. ((eq (car desc) :todo)
  9823. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9824. ((eq (car desc) :regexp)
  9825. (setq descre (cdr desc)))
  9826. ((eq (car desc) :level)
  9827. (setq descre (concat "^\\*\\{" (number-to-string
  9828. (if org-odd-levels-only
  9829. (1- (* 2 (cdr desc)))
  9830. (cdr desc)))
  9831. "\\}[ \t]")))
  9832. ((eq (car desc) :maxlevel)
  9833. (setq fast-path-p t)
  9834. (setq descre (concat "^\\*\\{1," (number-to-string
  9835. (if org-odd-levels-only
  9836. (1- (* 2 (cdr desc)))
  9837. (cdr desc)))
  9838. "\\}[ \t]")))
  9839. (t (error "Bad refiling target description %s" desc)))
  9840. (while (setq f (pop files))
  9841. (with-current-buffer
  9842. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9843. (or
  9844. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9845. (progn
  9846. (if (bufferp f) (setq f (buffer-file-name
  9847. (buffer-base-buffer f))))
  9848. (setq f (and f (expand-file-name f)))
  9849. (if (eq org-refile-use-outline-path 'file)
  9850. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9851. (save-excursion
  9852. (save-restriction
  9853. (widen)
  9854. (goto-char (point-min))
  9855. (while (re-search-forward descre nil t)
  9856. (goto-char (setq pos0 (point-at-bol)))
  9857. (catch 'next
  9858. (when org-refile-target-verify-function
  9859. (save-match-data
  9860. (or (funcall org-refile-target-verify-function)
  9861. (throw 'next t))))
  9862. (when (and (looking-at org-complex-heading-regexp)
  9863. (not (member (match-string 4) excluded-entries))
  9864. (match-string 4))
  9865. (setq level (org-reduced-level
  9866. (- (match-end 1) (match-beginning 1)))
  9867. txt (org-link-display-format (match-string 4))
  9868. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9869. re (format org-complex-heading-regexp-format
  9870. (regexp-quote (match-string 4))))
  9871. (when org-refile-use-outline-path
  9872. (setq txt (mapconcat
  9873. 'org-protect-slash
  9874. (append
  9875. (if (eq org-refile-use-outline-path
  9876. 'file)
  9877. (list (file-name-nondirectory
  9878. (buffer-file-name
  9879. (buffer-base-buffer))))
  9880. (if (eq org-refile-use-outline-path
  9881. 'full-file-path)
  9882. (list (buffer-file-name
  9883. (buffer-base-buffer)))))
  9884. (org-get-outline-path fast-path-p
  9885. level txt)
  9886. (list txt))
  9887. "/")))
  9888. (push (list txt f re (org-refile-marker (point)))
  9889. tgs)))
  9890. (when (= (point) pos0)
  9891. ;; verification function has not moved point
  9892. (goto-char (point-at-eol))))))))
  9893. (when org-refile-use-cache
  9894. (org-refile-cache-put tgs (buffer-file-name) descre))
  9895. (setq targets (append tgs targets))
  9896. ))))
  9897. (message "Getting targets...done")
  9898. (nreverse targets)))
  9899. (defun org-protect-slash (s)
  9900. (while (string-match "/" s)
  9901. (setq s (replace-match "\\" t t s)))
  9902. s)
  9903. (defvar org-olpa (make-vector 20 nil))
  9904. (defun org-get-outline-path (&optional fastp level heading)
  9905. "Return the outline path to the current entry, as a list.
  9906. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9907. routine which makes outline path derivations for an entire file,
  9908. avoiding backtracing. Refile target collection makes use of that."
  9909. (if fastp
  9910. (progn
  9911. (if (> level 19)
  9912. (error "Outline path failure, more than 19 levels"))
  9913. (loop for i from level upto 19 do
  9914. (aset org-olpa i nil))
  9915. (prog1
  9916. (delq nil (append org-olpa nil))
  9917. (aset org-olpa level heading)))
  9918. (let (rtn case-fold-search)
  9919. (save-excursion
  9920. (save-restriction
  9921. (widen)
  9922. (while (org-up-heading-safe)
  9923. (when (looking-at org-complex-heading-regexp)
  9924. (push (org-match-string-no-properties 4) rtn)))
  9925. rtn)))))
  9926. (defun org-format-outline-path (path &optional width prefix separator)
  9927. "Format the outline path PATH for display.
  9928. WIDTH is the maximum number of characters that is available.
  9929. PREFIX is a prefix to be included in the returned string,
  9930. such as the file name.
  9931. SEPARATOR is inserted between the different parts of the path,
  9932. the default is \"/\"."
  9933. (setq width (or width 79))
  9934. (if prefix (setq width (- width (length prefix))))
  9935. (if (not path)
  9936. (or prefix "")
  9937. (let* ((nsteps (length path))
  9938. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9939. (maxwidth (if (<= total-width width)
  9940. 10000 ;; everything fits
  9941. ;; we need to shorten the level headings
  9942. (/ (- width nsteps) nsteps)))
  9943. (org-odd-levels-only nil)
  9944. (n 0)
  9945. (total (1+ (length prefix))))
  9946. (setq maxwidth (max maxwidth 10))
  9947. (concat prefix
  9948. (if prefix (or separator "/"))
  9949. (mapconcat
  9950. (lambda (h)
  9951. (setq n (1+ n))
  9952. (if (and (= n nsteps) (< maxwidth 10000))
  9953. (setq maxwidth (- total-width total)))
  9954. (if (< (length h) maxwidth)
  9955. (progn (setq total (+ total (length h) 1)) h)
  9956. (setq h (substring h 0 (- maxwidth 2))
  9957. total (+ total maxwidth 1))
  9958. (if (string-match "[ \t]+\\'" h)
  9959. (setq h (substring h 0 (match-beginning 0))))
  9960. (setq h (concat h "..")))
  9961. (org-add-props h nil 'face
  9962. (nth (% (1- n) org-n-level-faces)
  9963. org-level-faces))
  9964. h)
  9965. path (or separator "/"))))))
  9966. (defun org-display-outline-path (&optional file current separator just-return-string)
  9967. "Display the current outline path in the echo area.
  9968. If FILE is non-nil, prepend the output with the file name.
  9969. If CURRENT is non-nil, append the current heading to the output.
  9970. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9971. the different parts of the path and defaults to \"/\".
  9972. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9973. (interactive "P")
  9974. (let* (case-fold-search
  9975. message-log-max ; Don't populate the *Messages* buffer
  9976. (bfn (buffer-file-name (buffer-base-buffer)))
  9977. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9978. res)
  9979. (if current (setq path (append path
  9980. (save-excursion
  9981. (org-back-to-heading t)
  9982. (if (looking-at org-complex-heading-regexp)
  9983. (list (match-string 4)))))))
  9984. (setq res
  9985. (org-format-outline-path
  9986. path
  9987. (1- (frame-width))
  9988. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9989. separator))
  9990. (if just-return-string
  9991. (org-no-properties res)
  9992. (message "%s" res))))
  9993. (defvar org-refile-history nil
  9994. "History for refiling operations.")
  9995. (defvar org-after-refile-insert-hook nil
  9996. "Hook run after `org-refile' has inserted its stuff at the new location.
  9997. Note that this is still *before* the stuff will be removed from
  9998. the *old* location.")
  9999. (defvar org-capture-last-stored-marker)
  10000. (defvar org-refile-keep nil
  10001. "Non-nil means `org-refile' will copy instead of refile.")
  10002. (defun org-copy ()
  10003. "Like `org-refile', but copy."
  10004. (interactive)
  10005. (let ((org-refile-keep t))
  10006. (funcall 'org-refile nil nil nil "Copy")))
  10007. (defun org-refile (&optional goto default-buffer rfloc msg)
  10008. "Move the entry or entries at point to another heading.
  10009. The list of target headings is compiled using the information in
  10010. `org-refile-targets', which see.
  10011. At the target location, the entry is filed as a subitem of the target
  10012. heading. Depending on `org-reverse-note-order', the new subitem will
  10013. either be the first or the last subitem.
  10014. If there is an active region, all entries in that region will be moved.
  10015. However, the region must fulfill the requirement that the first heading
  10016. is the first one sets the top-level of the moved text - at most siblings
  10017. below it are allowed.
  10018. With prefix arg GOTO, the command will only visit the target location
  10019. and not actually move anything.
  10020. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10021. go to the location where the last refiling operation has put the subtree.
  10022. With a prefix argument of `2', refile to the running clock.
  10023. RFLOC can be a refile location obtained in a different way.
  10024. MSG is a string to replace \"Refile\" in the default prompt with
  10025. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10026. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10027. If you are using target caching (see `org-refile-use-cache'),
  10028. you have to clear the target cache in order to find new targets.
  10029. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10030. prefix argument (`C-u C-u C-u C-c C-w')."
  10031. (interactive "P")
  10032. (if (member goto '(0 (64)))
  10033. (org-refile-cache-clear)
  10034. (let* ((actionmsg (or msg "Refile"))
  10035. (cbuf (current-buffer))
  10036. (regionp (org-region-active-p))
  10037. (region-start (and regionp (region-beginning)))
  10038. (region-end (and regionp (region-end)))
  10039. (region-length (and regionp (- region-end region-start)))
  10040. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10041. pos it nbuf file re level reversed)
  10042. (setq last-command nil)
  10043. (when regionp
  10044. (goto-char region-start)
  10045. (or (bolp) (goto-char (point-at-bol)))
  10046. (setq region-start (point))
  10047. (unless (or (org-kill-is-subtree-p
  10048. (buffer-substring region-start region-end))
  10049. (prog1 org-refile-active-region-within-subtree
  10050. (org-toggle-heading)))
  10051. (error "The region is not a (sequence of) subtree(s)")))
  10052. (if (equal goto '(16))
  10053. (org-refile-goto-last-stored)
  10054. (when (or
  10055. (and (equal goto 2)
  10056. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10057. (prog1
  10058. (setq it (list (or org-clock-heading "running clock")
  10059. (buffer-file-name
  10060. (marker-buffer org-clock-hd-marker))
  10061. ""
  10062. (marker-position org-clock-hd-marker)))
  10063. (setq goto nil)))
  10064. (setq it (or rfloc
  10065. (let (heading-text)
  10066. (save-excursion
  10067. (unless goto
  10068. (org-back-to-heading t)
  10069. (setq heading-text
  10070. (nth 4 (org-heading-components))))
  10071. (org-refile-get-location
  10072. (cond (goto "Goto")
  10073. (regionp (concat actionmsg " region to"))
  10074. (t (concat actionmsg " subtree \""
  10075. heading-text "\" to")))
  10076. default-buffer
  10077. (and (not (equal '(4) goto))
  10078. org-refile-allow-creating-parent-nodes)
  10079. goto))))))
  10080. (setq file (nth 1 it)
  10081. re (nth 2 it)
  10082. pos (nth 3 it))
  10083. (if (and (not goto)
  10084. pos
  10085. (equal (buffer-file-name) file)
  10086. (if regionp
  10087. (and (>= pos region-start)
  10088. (<= pos region-end))
  10089. (and (>= pos (point))
  10090. (< pos (save-excursion
  10091. (org-end-of-subtree t t))))))
  10092. (error "Cannot refile to position inside the tree or region"))
  10093. (setq nbuf (or (find-buffer-visiting file)
  10094. (find-file-noselect file)))
  10095. (if goto
  10096. (progn
  10097. (org-pop-to-buffer-same-window nbuf)
  10098. (goto-char pos)
  10099. (org-show-context 'org-goto))
  10100. (if regionp
  10101. (progn
  10102. (org-kill-new (buffer-substring region-start region-end))
  10103. (org-save-markers-in-region region-start region-end))
  10104. (org-copy-subtree 1 nil t))
  10105. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10106. (find-file-noselect file)))
  10107. (setq reversed (org-notes-order-reversed-p))
  10108. (save-excursion
  10109. (save-restriction
  10110. (widen)
  10111. (if pos
  10112. (progn
  10113. (goto-char pos)
  10114. (looking-at org-outline-regexp)
  10115. (setq level (org-get-valid-level (funcall outline-level) 1))
  10116. (goto-char
  10117. (if reversed
  10118. (or (outline-next-heading) (point-max))
  10119. (or (save-excursion (org-get-next-sibling))
  10120. (org-end-of-subtree t t)
  10121. (point-max)))))
  10122. (setq level 1)
  10123. (if (not reversed)
  10124. (goto-char (point-max))
  10125. (goto-char (point-min))
  10126. (or (outline-next-heading) (goto-char (point-max)))))
  10127. (if (not (bolp)) (newline))
  10128. (org-paste-subtree level)
  10129. (when org-log-refile
  10130. (org-add-log-setup 'refile nil nil 'findpos
  10131. org-log-refile)
  10132. (unless (eq org-log-refile 'note)
  10133. (save-excursion (org-add-log-note))))
  10134. (and org-auto-align-tags
  10135. (let ((org-loop-over-headlines-in-active-region nil))
  10136. (org-set-tags nil t)))
  10137. (with-demoted-errors
  10138. (bookmark-set "org-refile-last-stored"))
  10139. ;; If we are refiling for capture, make sure that the
  10140. ;; last-capture pointers point here
  10141. (when (org-bound-and-true-p org-refile-for-capture)
  10142. (with-demoted-errors
  10143. (bookmark-set "org-capture-last-stored-marker"))
  10144. (move-marker org-capture-last-stored-marker (point)))
  10145. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10146. (run-hooks 'org-after-refile-insert-hook))))
  10147. (unless org-refile-keep
  10148. (if regionp
  10149. (delete-region (point) (+ (point) region-length))
  10150. (org-cut-subtree)))
  10151. (when (featurep 'org-inlinetask)
  10152. (org-inlinetask-remove-END-maybe))
  10153. (setq org-markers-to-move nil)
  10154. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10155. (defun org-refile-goto-last-stored ()
  10156. "Go to the location where the last refile was stored."
  10157. (interactive)
  10158. (bookmark-jump "org-refile-last-stored")
  10159. (message "This is the location of the last refile"))
  10160. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10161. no-exclude)
  10162. "Prompt the user for a refile location, using PROMPT.
  10163. PROMPT should not be suffixed with a colon and a space, because
  10164. this function appends the default value from
  10165. `org-refile-history' automatically, if that is not empty.
  10166. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10167. this is used for the GOTO interface."
  10168. (let ((org-refile-targets org-refile-targets)
  10169. (org-refile-use-outline-path org-refile-use-outline-path)
  10170. excluded-entries)
  10171. (when (and (derived-mode-p 'org-mode)
  10172. (not org-refile-use-cache)
  10173. (not no-exclude))
  10174. (org-map-tree
  10175. (lambda()
  10176. (setq excluded-entries
  10177. (append excluded-entries (list (org-get-heading t t)))))))
  10178. (setq org-refile-target-table
  10179. (org-refile-get-targets default-buffer excluded-entries)))
  10180. (unless org-refile-target-table
  10181. (error "No refile targets"))
  10182. (let* ((cbuf (current-buffer))
  10183. (partial-completion-mode nil)
  10184. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10185. (cfunc (if (and org-refile-use-outline-path
  10186. org-outline-path-complete-in-steps)
  10187. 'org-olpath-completing-read
  10188. 'org-icompleting-read))
  10189. (extra (if org-refile-use-outline-path "/" ""))
  10190. (cbnex (concat (buffer-name) extra))
  10191. (filename (and cfn (expand-file-name cfn)))
  10192. (tbl (mapcar
  10193. (lambda (x)
  10194. (if (and (not (member org-refile-use-outline-path
  10195. '(file full-file-path)))
  10196. (not (equal filename (nth 1 x))))
  10197. (cons (concat (car x) extra " ("
  10198. (file-name-nondirectory (nth 1 x)) ")")
  10199. (cdr x))
  10200. (cons (concat (car x) extra) (cdr x))))
  10201. org-refile-target-table))
  10202. (completion-ignore-case t)
  10203. cdef
  10204. (prompt (concat prompt
  10205. (or (and (car org-refile-history)
  10206. (concat " (default " (car org-refile-history) ")"))
  10207. (and (assoc cbnex tbl) (setq cdef cbnex)
  10208. (concat " (default " cbnex ")"))) ": "))
  10209. pa answ parent-target child parent old-hist)
  10210. (setq old-hist org-refile-history)
  10211. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10212. nil 'org-refile-history (or cdef (car org-refile-history))))
  10213. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10214. (org-refile-check-position pa)
  10215. (if pa
  10216. (progn
  10217. (when (or (not org-refile-history)
  10218. (not (eq old-hist org-refile-history))
  10219. (not (equal (car pa) (car org-refile-history))))
  10220. (setq org-refile-history
  10221. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10222. org-refile-history
  10223. (cdr org-refile-history))))
  10224. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10225. (pop org-refile-history)))
  10226. pa)
  10227. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10228. (progn
  10229. (setq parent (match-string 1 answ)
  10230. child (match-string 2 answ))
  10231. (setq parent-target (or (assoc parent tbl)
  10232. (assoc (concat parent "/") tbl)))
  10233. (when (and parent-target
  10234. (or (eq new-nodes t)
  10235. (and (eq new-nodes 'confirm)
  10236. (y-or-n-p (format "Create new node \"%s\"? "
  10237. child)))))
  10238. (org-refile-new-child parent-target child)))
  10239. (error "Invalid target location")))))
  10240. (declare-function org-string-nw-p "org-macs" (s))
  10241. (defun org-refile-check-position (refile-pointer)
  10242. "Check if the refile pointer matches the headline to which it points."
  10243. (let* ((file (nth 1 refile-pointer))
  10244. (re (nth 2 refile-pointer))
  10245. (pos (nth 3 refile-pointer))
  10246. buffer)
  10247. (if (and (not (markerp pos)) (not file))
  10248. (error "Please save the buffer to a file before refiling")
  10249. (when (org-string-nw-p re)
  10250. (setq buffer (if (markerp pos)
  10251. (marker-buffer pos)
  10252. (or (find-buffer-visiting file)
  10253. (find-file-noselect file))))
  10254. (with-current-buffer buffer
  10255. (save-excursion
  10256. (save-restriction
  10257. (widen)
  10258. (goto-char pos)
  10259. (beginning-of-line 1)
  10260. (unless (org-looking-at-p re)
  10261. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10262. (defun org-refile-new-child (parent-target child)
  10263. "Use refile target PARENT-TARGET to add new CHILD below it."
  10264. (unless parent-target
  10265. (error "Cannot find parent for new node"))
  10266. (let ((file (nth 1 parent-target))
  10267. (pos (nth 3 parent-target))
  10268. level)
  10269. (with-current-buffer (or (find-buffer-visiting file)
  10270. (find-file-noselect file))
  10271. (save-excursion
  10272. (save-restriction
  10273. (widen)
  10274. (if pos
  10275. (goto-char pos)
  10276. (goto-char (point-max))
  10277. (if (not (bolp)) (newline)))
  10278. (when (looking-at org-outline-regexp)
  10279. (setq level (funcall outline-level))
  10280. (org-end-of-subtree t t))
  10281. (org-back-over-empty-lines)
  10282. (insert "\n" (make-string
  10283. (if pos (org-get-valid-level level 1) 1) ?*)
  10284. " " child "\n")
  10285. (beginning-of-line 0)
  10286. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10287. (defun org-olpath-completing-read (prompt collection &rest args)
  10288. "Read an outline path like a file name."
  10289. (let ((thetable collection)
  10290. (org-completion-use-ido nil) ; does not work with ido.
  10291. (org-completion-use-iswitchb nil)) ; or iswitchb
  10292. (apply
  10293. 'org-icompleting-read prompt
  10294. (lambda (string predicate &optional flag)
  10295. (let (rtn r f (l (length string)))
  10296. (cond
  10297. ((eq flag nil)
  10298. ;; try completion
  10299. (try-completion string thetable))
  10300. ((eq flag t)
  10301. ;; all-completions
  10302. (setq rtn (all-completions string thetable predicate))
  10303. (mapcar
  10304. (lambda (x)
  10305. (setq r (substring x l))
  10306. (if (string-match " ([^)]*)$" x)
  10307. (setq f (match-string 0 x))
  10308. (setq f ""))
  10309. (if (string-match "/" r)
  10310. (concat string (substring r 0 (match-end 0)) f)
  10311. x))
  10312. rtn))
  10313. ((eq flag 'lambda)
  10314. ;; exact match?
  10315. (assoc string thetable)))))
  10316. args)))
  10317. ;;;; Dynamic blocks
  10318. (defun org-find-dblock (name)
  10319. "Find the first dynamic block with name NAME in the buffer.
  10320. If not found, stay at current position and return nil."
  10321. (let ((case-fold-search t) pos)
  10322. (save-excursion
  10323. (goto-char (point-min))
  10324. (setq pos (and (re-search-forward
  10325. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10326. (match-beginning 0))))
  10327. (if pos (goto-char pos))
  10328. pos))
  10329. (defconst org-dblock-start-re
  10330. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10331. "Matches the start line of a dynamic block, with parameters.")
  10332. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10333. "Matches the end of a dynamic block.")
  10334. (defun org-create-dblock (plist)
  10335. "Create a dynamic block section, with parameters taken from PLIST.
  10336. PLIST must contain a :name entry which is used as name of the block."
  10337. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10338. (end-of-line 1)
  10339. (newline))
  10340. (let ((col (current-column))
  10341. (name (plist-get plist :name)))
  10342. (insert "#+BEGIN: " name)
  10343. (while plist
  10344. (if (eq (car plist) :name)
  10345. (setq plist (cddr plist))
  10346. (insert " " (prin1-to-string (pop plist)))))
  10347. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10348. (beginning-of-line -2)))
  10349. (defun org-prepare-dblock ()
  10350. "Prepare dynamic block for refresh.
  10351. This empties the block, puts the cursor at the insert position and returns
  10352. the property list including an extra property :name with the block name."
  10353. (unless (looking-at org-dblock-start-re)
  10354. (error "Not at a dynamic block"))
  10355. (let* ((begdel (1+ (match-end 0)))
  10356. (name (org-no-properties (match-string 1)))
  10357. (params (append (list :name name)
  10358. (read (concat "(" (match-string 3) ")")))))
  10359. (save-excursion
  10360. (beginning-of-line 1)
  10361. (skip-chars-forward " \t")
  10362. (setq params (plist-put params :indentation-column (current-column))))
  10363. (unless (re-search-forward org-dblock-end-re nil t)
  10364. (error "Dynamic block not terminated"))
  10365. (setq params
  10366. (append params
  10367. (list :content (buffer-substring
  10368. begdel (match-beginning 0)))))
  10369. (delete-region begdel (match-beginning 0))
  10370. (goto-char begdel)
  10371. (open-line 1)
  10372. params))
  10373. (defun org-map-dblocks (&optional command)
  10374. "Apply COMMAND to all dynamic blocks in the current buffer.
  10375. If COMMAND is not given, use `org-update-dblock'."
  10376. (let ((cmd (or command 'org-update-dblock)))
  10377. (save-excursion
  10378. (goto-char (point-min))
  10379. (while (re-search-forward org-dblock-start-re nil t)
  10380. (goto-char (match-beginning 0))
  10381. (save-excursion
  10382. (condition-case nil
  10383. (funcall cmd)
  10384. (error (message "Error during update of dynamic block"))))
  10385. (unless (re-search-forward org-dblock-end-re nil t)
  10386. (error "Dynamic block not terminated"))))))
  10387. (defun org-dblock-update (&optional arg)
  10388. "User command for updating dynamic blocks.
  10389. Update the dynamic block at point. With prefix ARG, update all dynamic
  10390. blocks in the buffer."
  10391. (interactive "P")
  10392. (if arg
  10393. (org-update-all-dblocks)
  10394. (or (looking-at org-dblock-start-re)
  10395. (org-beginning-of-dblock))
  10396. (org-update-dblock)))
  10397. (defun org-update-dblock ()
  10398. "Update the dynamic block at point.
  10399. This means to empty the block, parse for parameters and then call
  10400. the correct writing function."
  10401. (interactive)
  10402. (save-window-excursion
  10403. (let* ((pos (point))
  10404. (line (org-current-line))
  10405. (params (org-prepare-dblock))
  10406. (name (plist-get params :name))
  10407. (indent (plist-get params :indentation-column))
  10408. (cmd (intern (concat "org-dblock-write:" name))))
  10409. (message "Updating dynamic block `%s' at line %d..." name line)
  10410. (funcall cmd params)
  10411. (message "Updating dynamic block `%s' at line %d...done" name line)
  10412. (goto-char pos)
  10413. (when (and indent (> indent 0))
  10414. (setq indent (make-string indent ?\ ))
  10415. (save-excursion
  10416. (org-beginning-of-dblock)
  10417. (forward-line 1)
  10418. (while (not (looking-at org-dblock-end-re))
  10419. (insert indent)
  10420. (beginning-of-line 2))
  10421. (when (looking-at org-dblock-end-re)
  10422. (and (looking-at "[ \t]+")
  10423. (replace-match ""))
  10424. (insert indent)))))))
  10425. (defun org-beginning-of-dblock ()
  10426. "Find the beginning of the dynamic block at point.
  10427. Error if there is no such block at point."
  10428. (let ((pos (point))
  10429. beg)
  10430. (end-of-line 1)
  10431. (if (and (re-search-backward org-dblock-start-re nil t)
  10432. (setq beg (match-beginning 0))
  10433. (re-search-forward org-dblock-end-re nil t)
  10434. (> (match-end 0) pos))
  10435. (goto-char beg)
  10436. (goto-char pos)
  10437. (error "Not in a dynamic block"))))
  10438. (defun org-update-all-dblocks ()
  10439. "Update all dynamic blocks in the buffer.
  10440. This function can be used in a hook."
  10441. (interactive)
  10442. (when (derived-mode-p 'org-mode)
  10443. (org-map-dblocks 'org-update-dblock)))
  10444. ;;;; Completion
  10445. (defun org-get-export-keywords ()
  10446. "Return a list of all currently understood export keywords.
  10447. Export keywords include options, block names, attributes and
  10448. keywords relative to each registered export back-end."
  10449. (delq nil
  10450. (let (keywords)
  10451. (mapc
  10452. (lambda (back-end)
  10453. (let ((props (cdr back-end)))
  10454. ;; Back-end name (for keywords, like #+LATEX:)
  10455. (push (upcase (symbol-name (car back-end))) keywords)
  10456. ;; Back-end options.
  10457. (mapc (lambda (option) (push (cadr option) keywords))
  10458. (plist-get (cdr back-end) :options-alist))))
  10459. (org-bound-and-true-p org-export-registered-backends))
  10460. keywords)))
  10461. (defconst org-options-keywords
  10462. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10463. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10464. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10465. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10466. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10467. (defcustom org-structure-template-alist
  10468. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10469. "<src lang=\"?\">\n\n</src>")
  10470. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10471. "<example>\n?\n</example>")
  10472. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10473. "<quote>\n?\n</quote>")
  10474. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10475. "<verse>\n?\n</verse>")
  10476. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10477. "<verbatim>\n?\n</verbatim>")
  10478. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10479. "<center>\n?\n</center>")
  10480. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10481. "<literal style=\"latex\">\n?\n</literal>")
  10482. ("L" "#+LaTeX: "
  10483. "<literal style=\"latex\">?</literal>")
  10484. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10485. "<literal style=\"html\">\n?\n</literal>")
  10486. ("H" "#+HTML: "
  10487. "<literal style=\"html\">?</literal>")
  10488. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10489. ("A" "#+ASCII: ")
  10490. ("i" "#+INDEX: ?"
  10491. "#+INDEX: ?")
  10492. ("I" "#+INCLUDE: %file ?"
  10493. "<include file=%file markup=\"?\">"))
  10494. "Structure completion elements.
  10495. This is a list of abbreviation keys and values. The value gets inserted
  10496. if you type `<' followed by the key and then press the completion key,
  10497. usually `M-TAB'. %file will be replaced by a file name after prompting
  10498. for the file using completion. The cursor will be placed at the position
  10499. of the `?` in the template.
  10500. There are two templates for each key, the first uses the original Org syntax,
  10501. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10502. the default when the /org-mtags.el/ module has been loaded. See also the
  10503. variable `org-mtags-prefer-muse-templates'."
  10504. :group 'org-completion
  10505. :type '(repeat
  10506. (string :tag "Key")
  10507. (string :tag "Template")
  10508. (string :tag "Muse Template")))
  10509. (defun org-try-structure-completion ()
  10510. "Try to complete a structure template before point.
  10511. This looks for strings like \"<e\" on an otherwise empty line and
  10512. expands them."
  10513. (let ((l (buffer-substring (point-at-bol) (point)))
  10514. a)
  10515. (when (and (looking-at "[ \t]*$")
  10516. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10517. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10518. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10519. (match-beginning 1)) a)
  10520. t)))
  10521. (defun org-complete-expand-structure-template (start cell)
  10522. "Expand a structure template."
  10523. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10524. (rpl (nth (if musep 2 1) cell))
  10525. (ind ""))
  10526. (delete-region start (point))
  10527. (when (string-match "\\`#\\+" rpl)
  10528. (cond
  10529. ((bolp))
  10530. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10531. (setq ind (buffer-substring (point-at-bol) (point))))
  10532. (t (newline))))
  10533. (setq start (point))
  10534. (if (string-match "%file" rpl)
  10535. (setq rpl (replace-match
  10536. (concat
  10537. "\""
  10538. (save-match-data
  10539. (abbreviate-file-name (read-file-name "Include file: ")))
  10540. "\"")
  10541. t t rpl)))
  10542. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10543. (concat "\n" ind)))
  10544. (insert rpl)
  10545. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10546. ;;;; TODO, DEADLINE, Comments
  10547. (defun org-toggle-comment ()
  10548. "Change the COMMENT state of an entry."
  10549. (interactive)
  10550. (save-excursion
  10551. (org-back-to-heading)
  10552. (let (case-fold-search)
  10553. (cond
  10554. ((looking-at (format org-heading-keyword-regexp-format
  10555. org-comment-string))
  10556. (goto-char (match-end 1))
  10557. (looking-at (concat " +" org-comment-string))
  10558. (replace-match "" t t)
  10559. (when (eolp) (insert " ")))
  10560. ((looking-at org-outline-regexp)
  10561. (goto-char (match-end 0))
  10562. (insert org-comment-string " "))))))
  10563. (defvar org-last-todo-state-is-todo nil
  10564. "This is non-nil when the last TODO state change led to a TODO state.
  10565. If the last change removed the TODO tag or switched to DONE, then
  10566. this is nil.")
  10567. (defvar org-setting-tags nil) ; dynamically skipped
  10568. (defvar org-todo-setup-filter-hook nil
  10569. "Hook for functions that pre-filter todo specs.
  10570. Each function takes a todo spec and returns either nil or the spec
  10571. transformed into canonical form." )
  10572. (defvar org-todo-get-default-hook nil
  10573. "Hook for functions that get a default item for todo.
  10574. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10575. nil or a string to be used for the todo mark." )
  10576. (defvar org-agenda-headline-snapshot-before-repeat)
  10577. (defun org-current-effective-time ()
  10578. "Return current time adjusted for `org-extend-today-until' variable."
  10579. (let* ((ct (org-current-time))
  10580. (dct (decode-time ct))
  10581. (ct1
  10582. (cond
  10583. (org-use-last-clock-out-time-as-effective-time
  10584. (or (org-clock-get-last-clock-out-time) ct))
  10585. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10586. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10587. (t ct))))
  10588. ct1))
  10589. (defun org-todo-yesterday (&optional arg)
  10590. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10591. (interactive "P")
  10592. (if (eq major-mode 'org-agenda-mode)
  10593. (apply 'org-agenda-todo-yesterday arg)
  10594. (let* ((hour (third (decode-time
  10595. (org-current-time))))
  10596. (org-extend-today-until (1+ hour)))
  10597. (org-todo arg))))
  10598. (defvar org-block-entry-blocking ""
  10599. "First entry preventing the TODO state change.")
  10600. (defun org-todo (&optional arg)
  10601. "Change the TODO state of an item.
  10602. The state of an item is given by a keyword at the start of the heading,
  10603. like
  10604. *** TODO Write paper
  10605. *** DONE Call mom
  10606. The different keywords are specified in the variable `org-todo-keywords'.
  10607. By default the available states are \"TODO\" and \"DONE\".
  10608. So for this example: when the item starts with TODO, it is changed to DONE.
  10609. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10610. DONE are present, add TODO at the beginning of the heading.
  10611. With \\[universal-argument] prefix arg, use completion to determine the new \
  10612. state.
  10613. With numeric prefix arg, switch to that state.
  10614. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10615. keywords (nextset).
  10616. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10617. With a numeric prefix arg of 0, inhibit note taking for the change.
  10618. For calling through lisp, arg is also interpreted in the following way:
  10619. 'none -> empty state
  10620. \"\"(empty string) -> switch to empty state
  10621. 'done -> switch to DONE
  10622. 'nextset -> switch to the next set of keywords
  10623. 'previousset -> switch to the previous set of keywords
  10624. \"WAITING\" -> switch to the specified keyword, but only if it
  10625. really is a member of `org-todo-keywords'."
  10626. (interactive "P")
  10627. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10628. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10629. 'region-start-level 'region))
  10630. org-loop-over-headlines-in-active-region)
  10631. (org-map-entries
  10632. `(org-todo ,arg)
  10633. org-loop-over-headlines-in-active-region
  10634. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10635. (if (equal arg '(16)) (setq arg 'nextset))
  10636. (let ((org-blocker-hook org-blocker-hook)
  10637. commentp
  10638. case-fold-search)
  10639. (when (equal arg '(64))
  10640. (setq arg nil org-blocker-hook nil))
  10641. (when (and org-blocker-hook
  10642. (or org-inhibit-blocking
  10643. (org-entry-get nil "NOBLOCKING")))
  10644. (setq org-blocker-hook nil))
  10645. (save-excursion
  10646. (catch 'exit
  10647. (org-back-to-heading t)
  10648. (when (looking-at (concat "^\\*+ " org-comment-string))
  10649. (org-toggle-comment)
  10650. (setq commentp t))
  10651. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10652. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10653. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10654. (let* ((match-data (match-data))
  10655. (startpos (point-at-bol))
  10656. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10657. (org-log-done org-log-done)
  10658. (org-log-repeat org-log-repeat)
  10659. (org-todo-log-states org-todo-log-states)
  10660. (org-inhibit-logging
  10661. (if (equal arg 0)
  10662. (progn (setq arg nil) 'note) org-inhibit-logging))
  10663. (this (match-string 1))
  10664. (hl-pos (match-beginning 0))
  10665. (head (org-get-todo-sequence-head this))
  10666. (ass (assoc head org-todo-kwd-alist))
  10667. (interpret (nth 1 ass))
  10668. (done-word (nth 3 ass))
  10669. (final-done-word (nth 4 ass))
  10670. (org-last-state (or this ""))
  10671. (completion-ignore-case t)
  10672. (member (member this org-todo-keywords-1))
  10673. (tail (cdr member))
  10674. (org-state (cond
  10675. ((and org-todo-key-trigger
  10676. (or (and (equal arg '(4))
  10677. (eq org-use-fast-todo-selection 'prefix))
  10678. (and (not arg) org-use-fast-todo-selection
  10679. (not (eq org-use-fast-todo-selection
  10680. 'prefix)))))
  10681. ;; Use fast selection
  10682. (org-fast-todo-selection))
  10683. ((and (equal arg '(4))
  10684. (or (not org-use-fast-todo-selection)
  10685. (not org-todo-key-trigger)))
  10686. ;; Read a state with completion
  10687. (org-icompleting-read
  10688. "State: " (mapcar (lambda(x) (list x))
  10689. org-todo-keywords-1)
  10690. nil t))
  10691. ((eq arg 'right)
  10692. (if this
  10693. (if tail (car tail) nil)
  10694. (car org-todo-keywords-1)))
  10695. ((eq arg 'left)
  10696. (if (equal member org-todo-keywords-1)
  10697. nil
  10698. (if this
  10699. (nth (- (length org-todo-keywords-1)
  10700. (length tail) 2)
  10701. org-todo-keywords-1)
  10702. (org-last org-todo-keywords-1))))
  10703. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10704. (setq arg nil))) ; hack to fall back to cycling
  10705. (arg
  10706. ;; user or caller requests a specific state
  10707. (cond
  10708. ((equal arg "") nil)
  10709. ((eq arg 'none) nil)
  10710. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10711. ((eq arg 'nextset)
  10712. (or (car (cdr (member head org-todo-heads)))
  10713. (car org-todo-heads)))
  10714. ((eq arg 'previousset)
  10715. (let ((org-todo-heads (reverse org-todo-heads)))
  10716. (or (car (cdr (member head org-todo-heads)))
  10717. (car org-todo-heads))))
  10718. ((car (member arg org-todo-keywords-1)))
  10719. ((stringp arg)
  10720. (error "State `%s' not valid in this file" arg))
  10721. ((nth (1- (prefix-numeric-value arg))
  10722. org-todo-keywords-1))))
  10723. ((null member) (or head (car org-todo-keywords-1)))
  10724. ((equal this final-done-word) nil) ;; -> make empty
  10725. ((null tail) nil) ;; -> first entry
  10726. ((memq interpret '(type priority))
  10727. (if (eq this-command last-command)
  10728. (car tail)
  10729. (if (> (length tail) 0)
  10730. (or done-word (car org-done-keywords))
  10731. nil)))
  10732. (t
  10733. (car tail))))
  10734. (org-state (or
  10735. (run-hook-with-args-until-success
  10736. 'org-todo-get-default-hook org-state org-last-state)
  10737. org-state))
  10738. (next (if org-state (concat " " org-state " ") " "))
  10739. (change-plist (list :type 'todo-state-change :from this :to org-state
  10740. :position startpos))
  10741. dolog now-done-p)
  10742. (when org-blocker-hook
  10743. (setq org-last-todo-state-is-todo
  10744. (not (member this org-done-keywords)))
  10745. (unless (save-excursion
  10746. (save-match-data
  10747. (org-with-wide-buffer
  10748. (run-hook-with-args-until-failure
  10749. 'org-blocker-hook change-plist))))
  10750. (if (org-called-interactively-p 'interactive)
  10751. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10752. this org-state org-block-entry-blocking)
  10753. ;; fail silently
  10754. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10755. this org-state org-block-entry-blocking)
  10756. (throw 'exit nil))))
  10757. (store-match-data match-data)
  10758. (replace-match next t t)
  10759. (unless (pos-visible-in-window-p hl-pos)
  10760. (message "TODO state changed to %s" (org-trim next)))
  10761. (unless head
  10762. (setq head (org-get-todo-sequence-head org-state)
  10763. ass (assoc head org-todo-kwd-alist)
  10764. interpret (nth 1 ass)
  10765. done-word (nth 3 ass)
  10766. final-done-word (nth 4 ass)))
  10767. (when (memq arg '(nextset previousset))
  10768. (message "Keyword-Set %d/%d: %s"
  10769. (- (length org-todo-sets) -1
  10770. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10771. (length org-todo-sets)
  10772. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10773. (setq org-last-todo-state-is-todo
  10774. (not (member org-state org-done-keywords)))
  10775. (setq now-done-p (and (member org-state org-done-keywords)
  10776. (not (member this org-done-keywords))))
  10777. (and logging (org-local-logging logging))
  10778. (when (and (or org-todo-log-states org-log-done)
  10779. (not (eq org-inhibit-logging t))
  10780. (not (memq arg '(nextset previousset))))
  10781. ;; we need to look at recording a time and note
  10782. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10783. (nth 2 (assoc this org-todo-log-states))))
  10784. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10785. (setq dolog 'time))
  10786. (when (and org-state
  10787. (member org-state org-not-done-keywords)
  10788. (not (member this org-not-done-keywords)))
  10789. ;; This is now a todo state and was not one before
  10790. ;; If there was a CLOSED time stamp, get rid of it.
  10791. (org-add-planning-info nil nil 'closed))
  10792. (when (and now-done-p org-log-done)
  10793. ;; It is now done, and it was not done before
  10794. (org-add-planning-info 'closed (org-current-effective-time))
  10795. (if (and (not dolog) (eq 'note org-log-done))
  10796. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10797. (when (and org-state dolog)
  10798. ;; This is a non-nil state, and we need to log it
  10799. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10800. ;; Fixup tag positioning
  10801. (org-todo-trigger-tag-changes org-state)
  10802. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10803. (when org-provide-todo-statistics
  10804. (org-update-parent-todo-statistics))
  10805. (run-hooks 'org-after-todo-state-change-hook)
  10806. (if (and arg (not (member org-state org-done-keywords)))
  10807. (setq head (org-get-todo-sequence-head org-state)))
  10808. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10809. ;; Do we need to trigger a repeat?
  10810. (when now-done-p
  10811. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10812. ;; This is for the agenda, take a snapshot of the headline.
  10813. (save-match-data
  10814. (setq org-agenda-headline-snapshot-before-repeat
  10815. (org-get-heading))))
  10816. (org-auto-repeat-maybe org-state))
  10817. ;; Fixup cursor location if close to the keyword
  10818. (if (and (outline-on-heading-p)
  10819. (not (bolp))
  10820. (save-excursion (beginning-of-line 1)
  10821. (looking-at org-todo-line-regexp))
  10822. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10823. (progn
  10824. (goto-char (or (match-end 2) (match-end 1)))
  10825. (and (looking-at " ") (just-one-space))))
  10826. (when org-trigger-hook
  10827. (save-excursion
  10828. (run-hook-with-args 'org-trigger-hook change-plist)))
  10829. (when commentp (org-toggle-comment))))))))
  10830. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10831. "Block turning an entry into a TODO, using the hierarchy.
  10832. This checks whether the current task should be blocked from state
  10833. changes. Such blocking occurs when:
  10834. 1. The task has children which are not all in a completed state.
  10835. 2. A task has a parent with the property :ORDERED:, and there
  10836. are siblings prior to the current task with incomplete
  10837. status.
  10838. 3. The parent of the task is blocked because it has siblings that should
  10839. be done first, or is child of a block grandparent TODO entry."
  10840. (if (not org-enforce-todo-dependencies)
  10841. t ; if locally turned off don't block
  10842. (catch 'dont-block
  10843. ;; If this is not a todo state change, or if this entry is already DONE,
  10844. ;; do not block
  10845. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10846. (member (plist-get change-plist :from)
  10847. (cons 'done org-done-keywords))
  10848. (member (plist-get change-plist :to)
  10849. (cons 'todo org-not-done-keywords))
  10850. (not (plist-get change-plist :to)))
  10851. (throw 'dont-block t))
  10852. ;; If this task has children, and any are undone, it's blocked
  10853. (save-excursion
  10854. (org-back-to-heading t)
  10855. (let ((this-level (funcall outline-level)))
  10856. (outline-next-heading)
  10857. (let ((child-level (funcall outline-level)))
  10858. (while (and (not (eobp))
  10859. (> child-level this-level))
  10860. ;; this todo has children, check whether they are all
  10861. ;; completed
  10862. (if (and (not (org-entry-is-done-p))
  10863. (org-entry-is-todo-p))
  10864. (progn (setq org-block-entry-blocking (org-get-heading))
  10865. (throw 'dont-block nil)))
  10866. (outline-next-heading)
  10867. (setq child-level (funcall outline-level))))))
  10868. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10869. ;; any previous siblings are undone, it's blocked
  10870. (save-excursion
  10871. (org-back-to-heading t)
  10872. (let* ((pos (point))
  10873. (parent-pos (and (org-up-heading-safe) (point))))
  10874. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10875. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10876. (forward-line 1)
  10877. (re-search-forward org-not-done-heading-regexp pos t))
  10878. (setq org-block-entry-blocking (match-string 0))
  10879. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10880. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10881. (while t
  10882. (goto-char parent-pos)
  10883. (if (not (looking-at org-not-done-heading-regexp))
  10884. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10885. (setq pos (point))
  10886. (setq parent-pos (and (org-up-heading-safe) (point)))
  10887. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10888. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10889. (forward-line 1)
  10890. (re-search-forward org-not-done-heading-regexp pos t)
  10891. (setq org-block-entry-blocking (org-get-heading)))
  10892. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10893. (defcustom org-track-ordered-property-with-tag nil
  10894. "Should the ORDERED property also be shown as a tag?
  10895. The ORDERED property decides if an entry should require subtasks to be
  10896. completed in sequence. Since a property is not very visible, setting
  10897. this option means that toggling the ORDERED property with the command
  10898. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10899. not relevant for the behavior, but it makes things more visible.
  10900. Note that toggling the tag with tags commands will not change the property
  10901. and therefore not influence behavior!
  10902. This can be t, meaning the tag ORDERED should be used, It can also be a
  10903. string to select a different tag for this task."
  10904. :group 'org-todo
  10905. :type '(choice
  10906. (const :tag "No tracking" nil)
  10907. (const :tag "Track with ORDERED tag" t)
  10908. (string :tag "Use other tag")))
  10909. (defun org-toggle-ordered-property ()
  10910. "Toggle the ORDERED property of the current entry.
  10911. For better visibility, you can track the value of this property with a tag.
  10912. See variable `org-track-ordered-property-with-tag'."
  10913. (interactive)
  10914. (let* ((t1 org-track-ordered-property-with-tag)
  10915. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10916. (save-excursion
  10917. (org-back-to-heading)
  10918. (if (org-entry-get nil "ORDERED")
  10919. (progn
  10920. (org-delete-property "ORDERED")
  10921. (and tag (org-toggle-tag tag 'off))
  10922. (message "Subtasks can be completed in arbitrary order"))
  10923. (org-entry-put nil "ORDERED" "t")
  10924. (and tag (org-toggle-tag tag 'on))
  10925. (message "Subtasks must be completed in sequence")))))
  10926. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10927. (defun org-block-todo-from-checkboxes (change-plist)
  10928. "Block turning an entry into a TODO, using checkboxes.
  10929. This checks whether the current task should be blocked from state
  10930. changes because there are unchecked boxes in this entry."
  10931. (if (not org-enforce-todo-checkbox-dependencies)
  10932. t ; if locally turned off don't block
  10933. (catch 'dont-block
  10934. ;; If this is not a todo state change, or if this entry is already DONE,
  10935. ;; do not block
  10936. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10937. (member (plist-get change-plist :from)
  10938. (cons 'done org-done-keywords))
  10939. (member (plist-get change-plist :to)
  10940. (cons 'todo org-not-done-keywords))
  10941. (not (plist-get change-plist :to)))
  10942. (throw 'dont-block t))
  10943. ;; If this task has checkboxes that are not checked, it's blocked
  10944. (save-excursion
  10945. (org-back-to-heading t)
  10946. (let ((beg (point)) end)
  10947. (outline-next-heading)
  10948. (setq end (point))
  10949. (goto-char beg)
  10950. (if (org-list-search-forward
  10951. (concat (org-item-beginning-re)
  10952. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10953. "\\[[- ]\\]")
  10954. end t)
  10955. (progn
  10956. (if (boundp 'org-blocked-by-checkboxes)
  10957. (setq org-blocked-by-checkboxes t))
  10958. (throw 'dont-block nil)))))
  10959. t))) ; do not block
  10960. (defun org-entry-blocked-p ()
  10961. "Is the current entry blocked?"
  10962. (org-with-buffer-modified-unmodified
  10963. (if (org-entry-get nil "NOBLOCKING")
  10964. nil ;; Never block this entry
  10965. (not
  10966. (run-hook-with-args-until-failure
  10967. 'org-blocker-hook
  10968. (list :type 'todo-state-change
  10969. :position (point)
  10970. :from 'todo
  10971. :to 'done))))))
  10972. (defun org-update-statistics-cookies (all)
  10973. "Update the statistics cookie, either from TODO or from checkboxes.
  10974. This should be called with the cursor in a line with a statistics cookie."
  10975. (interactive "P")
  10976. (if all
  10977. (progn
  10978. (org-update-checkbox-count 'all)
  10979. (org-map-entries 'org-update-parent-todo-statistics))
  10980. (if (not (org-at-heading-p))
  10981. (org-update-checkbox-count)
  10982. (let ((pos (point-marker))
  10983. end l1 l2)
  10984. (ignore-errors (org-back-to-heading t))
  10985. (if (not (org-at-heading-p))
  10986. (org-update-checkbox-count)
  10987. (setq l1 (org-outline-level))
  10988. (setq end (save-excursion
  10989. (outline-next-heading)
  10990. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10991. (point)))
  10992. (if (and (save-excursion
  10993. (re-search-forward
  10994. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10995. (not (save-excursion (re-search-forward
  10996. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10997. (org-update-checkbox-count)
  10998. (if (and l2 (> l2 l1))
  10999. (progn
  11000. (goto-char end)
  11001. (org-update-parent-todo-statistics))
  11002. (goto-char pos)
  11003. (beginning-of-line 1)
  11004. (while (re-search-forward
  11005. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11006. (point-at-eol) t)
  11007. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11008. (goto-char pos)
  11009. (move-marker pos nil)))))
  11010. (defvar org-entry-property-inherited-from) ;; defined below
  11011. (defun org-update-parent-todo-statistics ()
  11012. "Update any statistics cookie in the parent of the current headline.
  11013. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11014. the entire subtree and this will travel up the hierarchy and update
  11015. statistics everywhere."
  11016. (let* ((prop (save-excursion (org-up-heading-safe)
  11017. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11018. (recursive (or (not org-hierarchical-todo-statistics)
  11019. (and prop (string-match "\\<recursive\\>" prop))))
  11020. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11021. 0))
  11022. (first t)
  11023. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11024. level ltoggle l1 new ndel
  11025. (cnt-all 0) (cnt-done 0) is-percent kwd
  11026. checkbox-beg ov ovs ove cookie-present)
  11027. (catch 'exit
  11028. (save-excursion
  11029. (beginning-of-line 1)
  11030. (setq ltoggle (funcall outline-level))
  11031. ;; Three situations are to consider:
  11032. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11033. ;; to the top-level ancestor on the headline;
  11034. ;; 2. If parent has "recursive" property, repeat up to the
  11035. ;; headline setting that property, taking inheritance into
  11036. ;; account;
  11037. ;; 3. Else, move up to direct parent and proceed only once.
  11038. (while (and (setq level (org-up-heading-safe))
  11039. (or recursive first)
  11040. (>= (point) lim))
  11041. (setq first nil cookie-present nil)
  11042. (unless (and level
  11043. (not (string-match
  11044. "\\<checkbox\\>"
  11045. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11046. "")))))
  11047. (throw 'exit nil))
  11048. (while (re-search-forward box-re (point-at-eol) t)
  11049. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11050. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11051. (save-match-data
  11052. (unless (outline-next-heading) (throw 'exit nil))
  11053. (while (and (looking-at org-complex-heading-regexp)
  11054. (> (setq l1 (length (match-string 1))) level))
  11055. (setq kwd (and (or recursive (= l1 ltoggle))
  11056. (match-string 2)))
  11057. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11058. (and (listp org-provide-todo-statistics)
  11059. (or (member kwd org-provide-todo-statistics)
  11060. (member kwd org-done-keywords))))
  11061. (setq cnt-all (1+ cnt-all))
  11062. (if (eq org-provide-todo-statistics t)
  11063. (and kwd (setq cnt-all (1+ cnt-all)))))
  11064. (and (member kwd org-done-keywords)
  11065. (setq cnt-done (1+ cnt-done)))
  11066. (outline-next-heading)))
  11067. (setq new
  11068. (if is-percent
  11069. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11070. (format "[%d/%d]" cnt-done cnt-all))
  11071. ndel (- (match-end 0) checkbox-beg))
  11072. ;; handle overlays when updating cookie from column view
  11073. (when (setq ov (car (overlays-at checkbox-beg)))
  11074. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11075. (delete-overlay ov))
  11076. (goto-char checkbox-beg)
  11077. (insert new)
  11078. (delete-region (point) (+ (point) ndel))
  11079. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11080. (when ov (move-overlay ov ovs ove)))
  11081. (when cookie-present
  11082. (run-hook-with-args 'org-after-todo-statistics-hook
  11083. cnt-done (- cnt-all cnt-done))))))
  11084. (run-hooks 'org-todo-statistics-hook)))
  11085. (defvar org-after-todo-statistics-hook nil
  11086. "Hook that is called after a TODO statistics cookie has been updated.
  11087. Each function is called with two arguments: the number of not-done entries
  11088. and the number of done entries.
  11089. For example, the following function, when added to this hook, will switch
  11090. an entry to DONE when all children are done, and back to TODO when new
  11091. entries are set to a TODO status. Note that this hook is only called
  11092. when there is a statistics cookie in the headline!
  11093. (defun org-summary-todo (n-done n-not-done)
  11094. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11095. (let (org-log-done org-log-states) ; turn off logging
  11096. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11097. ")
  11098. (defvar org-todo-statistics-hook nil
  11099. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11100. This hook runs even if there is no statistics cookie present, in which case
  11101. `org-after-todo-statistics-hook' would not run.")
  11102. (defun org-todo-trigger-tag-changes (state)
  11103. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11104. (let ((l org-todo-state-tags-triggers)
  11105. changes)
  11106. (when (or (not state) (equal state ""))
  11107. (setq changes (append changes (cdr (assoc "" l)))))
  11108. (when (and (stringp state) (> (length state) 0))
  11109. (setq changes (append changes (cdr (assoc state l)))))
  11110. (when (member state org-not-done-keywords)
  11111. (setq changes (append changes (cdr (assoc 'todo l)))))
  11112. (when (member state org-done-keywords)
  11113. (setq changes (append changes (cdr (assoc 'done l)))))
  11114. (dolist (c changes)
  11115. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11116. (defun org-local-logging (value)
  11117. "Get logging settings from a property VALUE."
  11118. (let* (words w a)
  11119. ;; directly set the variables, they are already local.
  11120. (setq org-log-done nil
  11121. org-log-repeat nil
  11122. org-todo-log-states nil)
  11123. (setq words (org-split-string value))
  11124. (while (setq w (pop words))
  11125. (cond
  11126. ((setq a (assoc w org-startup-options))
  11127. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11128. (set (nth 1 a) (nth 2 a))))
  11129. ((setq a (org-extract-log-state-settings w))
  11130. (and (member (car a) org-todo-keywords-1)
  11131. (push a org-todo-log-states)))))))
  11132. (defun org-get-todo-sequence-head (kwd)
  11133. "Return the head of the TODO sequence to which KWD belongs.
  11134. If KWD is not set, check if there is a text property remembering the
  11135. right sequence."
  11136. (let (p)
  11137. (cond
  11138. ((not kwd)
  11139. (or (get-text-property (point-at-bol) 'org-todo-head)
  11140. (progn
  11141. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11142. nil (point-at-eol)))
  11143. (get-text-property p 'org-todo-head))))
  11144. ((not (member kwd org-todo-keywords-1))
  11145. (car org-todo-keywords-1))
  11146. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11147. (defun org-fast-todo-selection ()
  11148. "Fast TODO keyword selection with single keys.
  11149. Returns the new TODO keyword, or nil if no state change should occur."
  11150. (let* ((fulltable org-todo-key-alist)
  11151. (done-keywords org-done-keywords) ;; needed for the faces.
  11152. (maxlen (apply 'max (mapcar
  11153. (lambda (x)
  11154. (if (stringp (car x)) (string-width (car x)) 0))
  11155. fulltable)))
  11156. (expert nil)
  11157. (fwidth (+ maxlen 3 1 3))
  11158. (ncol (/ (- (window-width) 4) fwidth))
  11159. tg cnt e c tbl
  11160. groups ingroup)
  11161. (save-excursion
  11162. (save-window-excursion
  11163. (if expert
  11164. (set-buffer (get-buffer-create " *Org todo*"))
  11165. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11166. (erase-buffer)
  11167. (org-set-local 'org-done-keywords done-keywords)
  11168. (setq tbl fulltable cnt 0)
  11169. (while (setq e (pop tbl))
  11170. (cond
  11171. ((equal e '(:startgroup))
  11172. (push '() groups) (setq ingroup t)
  11173. (when (not (= cnt 0))
  11174. (setq cnt 0)
  11175. (insert "\n"))
  11176. (insert "{ "))
  11177. ((equal e '(:endgroup))
  11178. (setq ingroup nil cnt 0)
  11179. (insert "}\n"))
  11180. ((equal e '(:newline))
  11181. (when (not (= cnt 0))
  11182. (setq cnt 0)
  11183. (insert "\n")
  11184. (setq e (car tbl))
  11185. (while (equal (car tbl) '(:newline))
  11186. (insert "\n")
  11187. (setq tbl (cdr tbl)))))
  11188. (t
  11189. (setq tg (car e) c (cdr e))
  11190. (if ingroup (push tg (car groups)))
  11191. (setq tg (org-add-props tg nil 'face
  11192. (org-get-todo-face tg)))
  11193. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11194. (insert "[" c "] " tg (make-string
  11195. (- fwidth 4 (length tg)) ?\ ))
  11196. (when (= (setq cnt (1+ cnt)) ncol)
  11197. (insert "\n")
  11198. (if ingroup (insert " "))
  11199. (setq cnt 0)))))
  11200. (insert "\n")
  11201. (goto-char (point-min))
  11202. (if (not expert) (org-fit-window-to-buffer))
  11203. (message "[a-z..]:Set [SPC]:clear")
  11204. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11205. (cond
  11206. ((or (= c ?\C-g)
  11207. (and (= c ?q) (not (rassoc c fulltable))))
  11208. (setq quit-flag t))
  11209. ((= c ?\ ) nil)
  11210. ((setq e (rassoc c fulltable) tg (car e))
  11211. tg)
  11212. (t (setq quit-flag t)))))))
  11213. (defun org-entry-is-todo-p ()
  11214. (member (org-get-todo-state) org-not-done-keywords))
  11215. (defun org-entry-is-done-p ()
  11216. (member (org-get-todo-state) org-done-keywords))
  11217. (defun org-get-todo-state ()
  11218. (save-excursion
  11219. (org-back-to-heading t)
  11220. (and (looking-at org-todo-line-regexp)
  11221. (match-end 2)
  11222. (match-string 2))))
  11223. (defun org-at-date-range-p (&optional inactive-ok)
  11224. "Is the cursor inside a date range?"
  11225. (interactive)
  11226. (save-excursion
  11227. (catch 'exit
  11228. (let ((pos (point)))
  11229. (skip-chars-backward "^[<\r\n")
  11230. (skip-chars-backward "<[")
  11231. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11232. (>= (match-end 0) pos)
  11233. (throw 'exit t))
  11234. (skip-chars-backward "^<[\r\n")
  11235. (skip-chars-backward "<[")
  11236. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11237. (>= (match-end 0) pos)
  11238. (throw 'exit t)))
  11239. nil)))
  11240. (defun org-get-repeat (&optional tagline)
  11241. "Check if there is a deadline/schedule with repeater in this entry."
  11242. (save-match-data
  11243. (save-excursion
  11244. (org-back-to-heading t)
  11245. (and (re-search-forward (if tagline
  11246. (concat tagline "\\s-*" org-repeat-re)
  11247. org-repeat-re)
  11248. (org-entry-end-position) t)
  11249. (match-string-no-properties 1)))))
  11250. (defvar org-last-changed-timestamp)
  11251. (defvar org-last-inserted-timestamp)
  11252. (defvar org-log-post-message)
  11253. (defvar org-log-note-purpose)
  11254. (defvar org-log-note-how)
  11255. (defvar org-log-note-extra)
  11256. (defun org-auto-repeat-maybe (done-word)
  11257. "Check if the current headline contains a repeated deadline/schedule.
  11258. If yes, set TODO state back to what it was and change the base date
  11259. of repeating deadline/scheduled time stamps to new date.
  11260. This function is run automatically after each state change to a DONE state."
  11261. ;; last-state is dynamically scoped into this function
  11262. (let* ((repeat (org-get-repeat))
  11263. (aa (assoc org-last-state org-todo-kwd-alist))
  11264. (interpret (nth 1 aa))
  11265. (head (nth 2 aa))
  11266. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11267. (msg "Entry repeats: ")
  11268. (org-log-done nil)
  11269. (org-todo-log-states nil)
  11270. re type n what ts time to-state)
  11271. (when repeat
  11272. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11273. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11274. org-todo-repeat-to-state))
  11275. (unless (and to-state (member to-state org-todo-keywords-1))
  11276. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11277. (org-todo to-state)
  11278. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11279. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11280. (org-time-stamp-format t t))))
  11281. (when org-log-repeat
  11282. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11283. (memq 'org-add-log-note post-command-hook))
  11284. ;; OK, we are already setup for some record
  11285. (if (eq org-log-repeat 'note)
  11286. ;; make sure we take a note, not only a time stamp
  11287. (setq org-log-note-how 'note))
  11288. ;; Set up for taking a record
  11289. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11290. org-last-state
  11291. 'findpos org-log-repeat)))
  11292. (org-back-to-heading t)
  11293. (org-add-planning-info nil nil 'closed)
  11294. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11295. org-deadline-time-regexp "\\)\\|\\("
  11296. org-ts-regexp "\\)"))
  11297. (while (re-search-forward
  11298. re (save-excursion (outline-next-heading) (point)) t)
  11299. (setq type (if (match-end 1) org-scheduled-string
  11300. (if (match-end 3) org-deadline-string "Plain:"))
  11301. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11302. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11303. (setq n (string-to-number (match-string 2 ts))
  11304. what (match-string 3 ts))
  11305. (if (equal what "w") (setq n (* n 7) what "d"))
  11306. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11307. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11308. ;; Preparation, see if we need to modify the start date for the change
  11309. (when (match-end 1)
  11310. (setq time (save-match-data (org-time-string-to-time ts)))
  11311. (cond
  11312. ((equal (match-string 1 ts) ".")
  11313. ;; Shift starting date to today
  11314. (org-timestamp-change
  11315. (- (org-today) (time-to-days time))
  11316. 'day))
  11317. ((equal (match-string 1 ts) "+")
  11318. (let ((nshiftmax 10) (nshift 0))
  11319. (while (or (= nshift 0)
  11320. (<= (time-to-days time)
  11321. (time-to-days (current-time))))
  11322. (when (= (incf nshift) nshiftmax)
  11323. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11324. (error "Abort")))
  11325. (org-timestamp-change n (cdr (assoc what whata)))
  11326. (org-at-timestamp-p t)
  11327. (setq ts (match-string 1))
  11328. (setq time (save-match-data (org-time-string-to-time ts)))))
  11329. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11330. ;; rematch, so that we have everything in place for the real shift
  11331. (org-at-timestamp-p t)
  11332. (setq ts (match-string 1))
  11333. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11334. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11335. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11336. (setq org-log-post-message msg)
  11337. (message "%s" msg))))
  11338. (defun org-show-todo-tree (arg)
  11339. "Make a compact tree which shows all headlines marked with TODO.
  11340. The tree will show the lines where the regexp matches, and all higher
  11341. headlines above the match.
  11342. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11343. With a numeric prefix N, construct a sparse tree for the Nth element
  11344. of `org-todo-keywords-1'."
  11345. (interactive "P")
  11346. (let ((case-fold-search nil)
  11347. (kwd-re
  11348. (cond ((null arg) org-not-done-regexp)
  11349. ((equal arg '(4))
  11350. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11351. (mapcar 'list org-todo-keywords-1))))
  11352. (concat "\\("
  11353. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11354. "\\)\\>")))
  11355. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11356. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11357. org-todo-keywords-1)))
  11358. (t (error "Invalid prefix argument: %s" arg)))))
  11359. (message "%d TODO entries found"
  11360. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11361. (defun org-deadline (&optional arg time)
  11362. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11363. With one universal prefix argument, remove any deadline from the item.
  11364. With two universal prefix arguments, prompt for a warning delay.
  11365. With argument TIME, set the deadline at the corresponding date. TIME
  11366. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11367. (interactive "P")
  11368. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11369. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11370. 'region-start-level 'region))
  11371. org-loop-over-headlines-in-active-region)
  11372. (org-map-entries
  11373. `(org-deadline ',arg ,time)
  11374. org-loop-over-headlines-in-active-region
  11375. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11376. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11377. (repeater (and old-date
  11378. (string-match
  11379. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11380. old-date)
  11381. (match-string 1 old-date))))
  11382. (cond
  11383. ((equal arg '(4))
  11384. (when (and old-date org-log-redeadline)
  11385. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11386. org-log-redeadline))
  11387. (org-remove-timestamp-with-keyword org-deadline-string)
  11388. (message "Item no longer has a deadline."))
  11389. ((equal arg '(16))
  11390. (save-excursion
  11391. (if (re-search-forward
  11392. org-deadline-time-regexp
  11393. (save-excursion (outline-next-heading) (point)) t)
  11394. (let* ((rpl0 (match-string 1))
  11395. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11396. (replace-match
  11397. (concat org-deadline-string
  11398. " <" rpl
  11399. (format " -%dd" (abs
  11400. (- (time-to-days
  11401. (save-match-data
  11402. (org-read-date nil t nil "Warn starting from")))
  11403. (time-to-days nil))))
  11404. ">") t t))
  11405. (user-error "No deadline information to update"))))
  11406. (t
  11407. (org-add-planning-info 'deadline time 'closed)
  11408. (when (and old-date org-log-redeadline
  11409. (not (equal old-date
  11410. (substring org-last-inserted-timestamp 1 -1))))
  11411. (org-add-log-setup 'redeadline nil old-date 'findpos
  11412. org-log-redeadline))
  11413. (when repeater
  11414. (save-excursion
  11415. (org-back-to-heading t)
  11416. (when (re-search-forward (concat org-deadline-string " "
  11417. org-last-inserted-timestamp)
  11418. (save-excursion
  11419. (outline-next-heading) (point)) t)
  11420. (goto-char (1- (match-end 0)))
  11421. (insert " " repeater)
  11422. (setq org-last-inserted-timestamp
  11423. (concat (substring org-last-inserted-timestamp 0 -1)
  11424. " " repeater
  11425. (substring org-last-inserted-timestamp -1))))))
  11426. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11427. (defun org-schedule (&optional arg time)
  11428. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11429. With one universal prefix argument, remove any scheduling date from the item.
  11430. With two universal prefix arguments, prompt for a delay cookie.
  11431. With argument TIME, scheduled at the corresponding date. TIME can
  11432. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11433. (interactive "P")
  11434. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11435. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11436. 'region-start-level 'region))
  11437. org-loop-over-headlines-in-active-region)
  11438. (org-map-entries
  11439. `(org-schedule ',arg ,time)
  11440. org-loop-over-headlines-in-active-region
  11441. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11442. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11443. (repeater (and old-date
  11444. (string-match
  11445. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11446. old-date)
  11447. (match-string 1 old-date))))
  11448. (cond
  11449. ((equal arg '(4))
  11450. (progn
  11451. (when (and old-date org-log-reschedule)
  11452. (org-add-log-setup 'delschedule nil old-date 'findpos
  11453. org-log-reschedule))
  11454. (org-remove-timestamp-with-keyword org-scheduled-string)
  11455. (message "Item is no longer scheduled.")))
  11456. ((equal arg '(16))
  11457. (save-excursion
  11458. (if (re-search-forward
  11459. org-scheduled-time-regexp
  11460. (save-excursion (outline-next-heading) (point)) t)
  11461. (let* ((rpl0 (match-string 1))
  11462. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11463. (replace-match
  11464. (concat org-scheduled-string
  11465. " <" rpl
  11466. (format " -%dd" (abs
  11467. (- (time-to-days
  11468. (save-match-data
  11469. (org-read-date nil t nil "Delay until")))
  11470. (time-to-days nil))))
  11471. ">") t t))
  11472. (user-error "No scheduled information to update"))))
  11473. (t
  11474. (org-add-planning-info 'scheduled time 'closed)
  11475. (when (and old-date org-log-reschedule
  11476. (not (equal old-date
  11477. (substring org-last-inserted-timestamp 1 -1))))
  11478. (org-add-log-setup 'reschedule nil old-date 'findpos
  11479. org-log-reschedule))
  11480. (when repeater
  11481. (save-excursion
  11482. (org-back-to-heading t)
  11483. (when (re-search-forward (concat org-scheduled-string " "
  11484. org-last-inserted-timestamp)
  11485. (save-excursion
  11486. (outline-next-heading) (point)) t)
  11487. (goto-char (1- (match-end 0)))
  11488. (insert " " repeater)
  11489. (setq org-last-inserted-timestamp
  11490. (concat (substring org-last-inserted-timestamp 0 -1)
  11491. " " repeater
  11492. (substring org-last-inserted-timestamp -1))))))
  11493. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11494. (defun org-get-scheduled-time (pom &optional inherit)
  11495. "Get the scheduled time as a time tuple, of a format suitable
  11496. for calling org-schedule with, or if there is no scheduling,
  11497. returns nil."
  11498. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11499. (when time
  11500. (apply 'encode-time (org-parse-time-string time)))))
  11501. (defun org-get-deadline-time (pom &optional inherit)
  11502. "Get the deadline as a time tuple, of a format suitable for
  11503. calling org-deadline with, or if there is no scheduling, returns
  11504. nil."
  11505. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11506. (when time
  11507. (apply 'encode-time (org-parse-time-string time)))))
  11508. (defun org-remove-timestamp-with-keyword (keyword)
  11509. "Remove all time stamps with KEYWORD in the current entry."
  11510. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11511. beg)
  11512. (save-excursion
  11513. (org-back-to-heading t)
  11514. (setq beg (point))
  11515. (outline-next-heading)
  11516. (while (re-search-backward re beg t)
  11517. (replace-match "")
  11518. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11519. (equal (char-before) ?\ ))
  11520. (backward-delete-char 1)
  11521. (if (string-match "^[ \t]*$" (buffer-substring
  11522. (point-at-bol) (point-at-eol)))
  11523. (delete-region (point-at-bol)
  11524. (min (point-max) (1+ (point-at-eol))))))))))
  11525. (defun org-add-planning-info (what &optional time &rest remove)
  11526. "Insert new timestamp with keyword in the line directly after the headline.
  11527. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11528. If non is given, the user is prompted for a date.
  11529. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11530. be removed."
  11531. (interactive)
  11532. (let (org-time-was-given org-end-time-was-given ts
  11533. end default-time default-input)
  11534. (catch 'exit
  11535. (when (and (memq what '(scheduled deadline))
  11536. (or (not time)
  11537. (and (stringp time)
  11538. (string-match "^[-+]+[0-9]" time))))
  11539. ;; Try to get a default date/time from existing timestamp
  11540. (save-excursion
  11541. (org-back-to-heading t)
  11542. (setq end (save-excursion (outline-next-heading) (point)))
  11543. (when (re-search-forward (if (eq what 'scheduled)
  11544. org-scheduled-time-regexp
  11545. org-deadline-time-regexp)
  11546. end t)
  11547. (setq ts (match-string 1)
  11548. default-time
  11549. (apply 'encode-time (org-parse-time-string ts))
  11550. default-input (and ts (org-get-compact-tod ts))))))
  11551. (when what
  11552. (setq time
  11553. (if (stringp time)
  11554. ;; This is a string (relative or absolute), set proper date
  11555. (apply 'encode-time
  11556. (org-read-date-analyze
  11557. time default-time (decode-time default-time)))
  11558. ;; If necessary, get the time from the user
  11559. (or time (org-read-date nil 'to-time nil nil
  11560. default-time default-input)))))
  11561. (when (and org-insert-labeled-timestamps-at-point
  11562. (member what '(scheduled deadline)))
  11563. (insert
  11564. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11565. (org-insert-time-stamp time org-time-was-given
  11566. nil nil nil (list org-end-time-was-given))
  11567. (setq what nil))
  11568. (save-excursion
  11569. (save-restriction
  11570. (let (col list elt ts buffer-invisibility-spec)
  11571. (org-back-to-heading t)
  11572. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11573. (goto-char (match-end 1))
  11574. (setq col (current-column))
  11575. (goto-char (match-end 0))
  11576. (if (eobp) (insert "\n") (forward-char 1))
  11577. (when (and (not what)
  11578. (not (looking-at
  11579. (concat "[ \t]*"
  11580. org-keyword-time-not-clock-regexp))))
  11581. ;; Nothing to add, nothing to remove...... :-)
  11582. (throw 'exit nil))
  11583. (if (and (not (looking-at org-outline-regexp))
  11584. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11585. "[^\r\n]*"))
  11586. (not (equal (match-string 1) org-clock-string)))
  11587. (narrow-to-region (match-beginning 0) (match-end 0))
  11588. (insert-before-markers "\n")
  11589. (backward-char 1)
  11590. (narrow-to-region (point) (point))
  11591. (and org-adapt-indentation (org-indent-to-column col)))
  11592. ;; Check if we have to remove something.
  11593. (setq list (cons what remove))
  11594. (while list
  11595. (setq elt (pop list))
  11596. (when (or (and (eq elt 'scheduled)
  11597. (re-search-forward org-scheduled-time-regexp nil t))
  11598. (and (eq elt 'deadline)
  11599. (re-search-forward org-deadline-time-regexp nil t))
  11600. (and (eq elt 'closed)
  11601. (re-search-forward org-closed-time-regexp nil t)))
  11602. (replace-match "")
  11603. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11604. (and (looking-at "[ \t]+") (replace-match ""))
  11605. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11606. (when what
  11607. (insert
  11608. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11609. (cond ((eq what 'scheduled) org-scheduled-string)
  11610. ((eq what 'deadline) org-deadline-string)
  11611. ((eq what 'closed) org-closed-string))
  11612. " ")
  11613. (setq ts (org-insert-time-stamp
  11614. time
  11615. (or org-time-was-given
  11616. (and (eq what 'closed) org-log-done-with-time))
  11617. (eq what 'closed)
  11618. nil nil (list org-end-time-was-given)))
  11619. (insert
  11620. (if (not (or (bolp) (eq (char-before) ?\ )
  11621. (memq (char-after) '(32 10))
  11622. (eobp))) " " ""))
  11623. (end-of-line 1))
  11624. (goto-char (point-min))
  11625. (widen)
  11626. (if (and (looking-at "[ \t]*\n")
  11627. (equal (char-before) ?\n))
  11628. (delete-region (1- (point)) (point-at-eol)))
  11629. ts))))))
  11630. (defvar org-log-note-marker (make-marker))
  11631. (defvar org-log-note-purpose nil)
  11632. (defvar org-log-note-state nil)
  11633. (defvar org-log-note-previous-state nil)
  11634. (defvar org-log-note-how nil)
  11635. (defvar org-log-note-extra nil)
  11636. (defvar org-log-note-window-configuration nil)
  11637. (defvar org-log-note-return-to (make-marker))
  11638. (defvar org-log-note-effective-time nil
  11639. "Remembered current time so that dynamically scoped
  11640. `org-extend-today-until' affects tha timestamps in state change
  11641. log")
  11642. (defvar org-log-post-message nil
  11643. "Message to be displayed after a log note has been stored.
  11644. The auto-repeater uses this.")
  11645. (defun org-add-note ()
  11646. "Add a note to the current entry.
  11647. This is done in the same way as adding a state change note."
  11648. (interactive)
  11649. (org-add-log-setup 'note nil nil 'findpos nil))
  11650. (defvar org-property-end-re)
  11651. (defun org-add-log-setup (&optional purpose state prev-state
  11652. findpos how extra)
  11653. "Set up the post command hook to take a note.
  11654. If this is about to TODO state change, the new state is expected in STATE.
  11655. When FINDPOS is non-nil, find the correct position for the note in
  11656. the current entry. If not, assume that it can be inserted at point.
  11657. HOW is an indicator what kind of note should be created.
  11658. EXTRA is additional text that will be inserted into the notes buffer."
  11659. (let* ((org-log-into-drawer (org-log-into-drawer))
  11660. (drawer (cond ((stringp org-log-into-drawer)
  11661. org-log-into-drawer)
  11662. (org-log-into-drawer "LOGBOOK"))))
  11663. (save-restriction
  11664. (save-excursion
  11665. (when findpos
  11666. (org-back-to-heading t)
  11667. (narrow-to-region (point) (save-excursion
  11668. (outline-next-heading) (point)))
  11669. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11670. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11671. "[^\r\n]*\\)?"))
  11672. (goto-char (match-end 0))
  11673. (cond
  11674. (drawer
  11675. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11676. nil t)
  11677. (progn
  11678. (goto-char (match-end 0))
  11679. (or org-log-states-order-reversed
  11680. (and (re-search-forward org-property-end-re nil t)
  11681. (goto-char (1- (match-beginning 0))))))
  11682. (insert "\n:" drawer ":\n:END:")
  11683. (beginning-of-line 0)
  11684. (org-indent-line)
  11685. (beginning-of-line 2)
  11686. (org-indent-line)
  11687. (end-of-line 0)))
  11688. ((and org-log-state-notes-insert-after-drawers
  11689. (save-excursion
  11690. (forward-line) (looking-at org-drawer-regexp)))
  11691. (forward-line)
  11692. (while (looking-at org-drawer-regexp)
  11693. (goto-char (match-end 0))
  11694. (re-search-forward org-property-end-re (point-max) t)
  11695. (forward-line))
  11696. (forward-line -1)))
  11697. (unless org-log-states-order-reversed
  11698. (and (= (char-after) ?\n) (forward-char 1))
  11699. (org-skip-over-state-notes)
  11700. (skip-chars-backward " \t\n\r")))
  11701. (move-marker org-log-note-marker (point))
  11702. (setq org-log-note-purpose purpose
  11703. org-log-note-state state
  11704. org-log-note-previous-state prev-state
  11705. org-log-note-how how
  11706. org-log-note-extra extra
  11707. org-log-note-effective-time (org-current-effective-time))
  11708. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11709. (defun org-skip-over-state-notes ()
  11710. "Skip past the list of State notes in an entry."
  11711. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11712. (when (ignore-errors (goto-char (org-in-item-p)))
  11713. (let* ((struct (org-list-struct))
  11714. (prevs (org-list-prevs-alist struct)))
  11715. (while (looking-at "[ \t]*- State")
  11716. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11717. (org-list-get-item-end (point) struct)))))))
  11718. (defun org-add-log-note (&optional purpose)
  11719. "Pop up a window for taking a note, and add this note later at point."
  11720. (remove-hook 'post-command-hook 'org-add-log-note)
  11721. (setq org-log-note-window-configuration (current-window-configuration))
  11722. (delete-other-windows)
  11723. (move-marker org-log-note-return-to (point))
  11724. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11725. (goto-char org-log-note-marker)
  11726. (org-switch-to-buffer-other-window "*Org Note*")
  11727. (erase-buffer)
  11728. (if (memq org-log-note-how '(time state))
  11729. (let (current-prefix-arg) (org-store-log-note))
  11730. (let ((org-inhibit-startup t)) (org-mode))
  11731. (insert (format "# Insert note for %s.
  11732. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11733. (cond
  11734. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11735. ((eq org-log-note-purpose 'done) "closed todo item")
  11736. ((eq org-log-note-purpose 'state)
  11737. (format "state change from \"%s\" to \"%s\""
  11738. (or org-log-note-previous-state "")
  11739. (or org-log-note-state "")))
  11740. ((eq org-log-note-purpose 'reschedule)
  11741. "rescheduling")
  11742. ((eq org-log-note-purpose 'delschedule)
  11743. "no longer scheduled")
  11744. ((eq org-log-note-purpose 'redeadline)
  11745. "changing deadline")
  11746. ((eq org-log-note-purpose 'deldeadline)
  11747. "removing deadline")
  11748. ((eq org-log-note-purpose 'refile)
  11749. "refiling")
  11750. ((eq org-log-note-purpose 'note)
  11751. "this entry")
  11752. (t (error "This should not happen")))))
  11753. (if org-log-note-extra (insert org-log-note-extra))
  11754. (org-set-local 'org-finish-function 'org-store-log-note)
  11755. (run-hooks 'org-log-buffer-setup-hook)))
  11756. (defvar org-note-abort nil) ; dynamically scoped
  11757. (defun org-store-log-note ()
  11758. "Finish taking a log note, and insert it to where it belongs."
  11759. (let ((txt (buffer-string)))
  11760. (kill-buffer (current-buffer))
  11761. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11762. lines ind bul)
  11763. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11764. (setq txt (replace-match "" t t txt)))
  11765. (if (string-match "\\s-+\\'" txt)
  11766. (setq txt (replace-match "" t t txt)))
  11767. (setq lines (org-split-string txt "\n"))
  11768. (when (and note (string-match "\\S-" note))
  11769. (setq note
  11770. (org-replace-escapes
  11771. note
  11772. (list (cons "%u" (user-login-name))
  11773. (cons "%U" user-full-name)
  11774. (cons "%t" (format-time-string
  11775. (org-time-stamp-format 'long 'inactive)
  11776. org-log-note-effective-time))
  11777. (cons "%T" (format-time-string
  11778. (org-time-stamp-format 'long nil)
  11779. org-log-note-effective-time))
  11780. (cons "%d" (format-time-string
  11781. (org-time-stamp-format nil 'inactive)
  11782. org-log-note-effective-time))
  11783. (cons "%D" (format-time-string
  11784. (org-time-stamp-format nil nil)
  11785. org-log-note-effective-time))
  11786. (cons "%s" (if org-log-note-state
  11787. (concat "\"" org-log-note-state "\"")
  11788. ""))
  11789. (cons "%S" (if org-log-note-previous-state
  11790. (concat "\"" org-log-note-previous-state "\"")
  11791. "\"\"")))))
  11792. (if lines (setq note (concat note " \\\\")))
  11793. (push note lines))
  11794. (when (or current-prefix-arg org-note-abort)
  11795. (when org-log-into-drawer
  11796. (org-remove-empty-drawer-at
  11797. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11798. org-log-note-marker))
  11799. (setq lines nil))
  11800. (when lines
  11801. (with-current-buffer (marker-buffer org-log-note-marker)
  11802. (save-excursion
  11803. (goto-char org-log-note-marker)
  11804. (move-marker org-log-note-marker nil)
  11805. (end-of-line 1)
  11806. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11807. (setq ind (save-excursion
  11808. (if (ignore-errors (goto-char (org-in-item-p)))
  11809. (let ((struct (org-list-struct)))
  11810. (org-list-get-ind
  11811. (org-list-get-top-point struct) struct))
  11812. (skip-chars-backward " \r\t\n")
  11813. (cond
  11814. ((and (org-at-heading-p)
  11815. org-adapt-indentation)
  11816. (1+ (org-current-level)))
  11817. ((org-at-heading-p) 0)
  11818. (t (org-get-indentation))))))
  11819. (setq bul (org-list-bullet-string "-"))
  11820. (org-indent-line-to ind)
  11821. (insert bul (pop lines))
  11822. (let ((ind-body (+ (length bul) ind)))
  11823. (while lines
  11824. (insert "\n")
  11825. (org-indent-line-to ind-body)
  11826. (insert (pop lines))))
  11827. (message "Note stored")
  11828. (org-back-to-heading t)
  11829. (org-cycle-hide-drawers 'children))))))
  11830. (set-window-configuration org-log-note-window-configuration)
  11831. (with-current-buffer (marker-buffer org-log-note-return-to)
  11832. (goto-char org-log-note-return-to))
  11833. (move-marker org-log-note-return-to nil)
  11834. (and org-log-post-message (message "%s" org-log-post-message)))
  11835. (defun org-remove-empty-drawer-at (drawer pos)
  11836. "Remove an empty drawer DRAWER at position POS.
  11837. POS may also be a marker."
  11838. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11839. (save-excursion
  11840. (save-restriction
  11841. (widen)
  11842. (goto-char pos)
  11843. (if (org-in-regexp
  11844. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11845. (replace-match ""))))))
  11846. (defvar org-ts-type nil)
  11847. (defun org-sparse-tree (&optional arg type)
  11848. "Create a sparse tree, prompt for the details.
  11849. This command can create sparse trees. You first need to select the type
  11850. of match used to create the tree:
  11851. t Show all TODO entries.
  11852. T Show entries with a specific TODO keyword.
  11853. m Show entries selected by a tags/property match.
  11854. p Enter a property name and its value (both with completion on existing
  11855. names/values) and show entries with that property.
  11856. r Show entries matching a regular expression (`/' can be used as well).
  11857. b Show deadlines and scheduled items before a date.
  11858. a Show deadlines and scheduled items after a date.
  11859. d Show deadlines due within `org-deadline-warning-days'.
  11860. D Show deadlines and scheduled items between a date range."
  11861. (interactive "P")
  11862. (let (ans kwd value ts-type)
  11863. (setq type (or type org-sparse-tree-default-date-type))
  11864. (setq org-ts-type type)
  11865. (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"
  11866. (cond ((eq type 'all) "all timestamps")
  11867. ((eq type 'scheduled) "only scheduled")
  11868. ((eq type 'deadline) "only deadline")
  11869. ((eq type 'active) "only active timestamps")
  11870. ((eq type 'inactive) "only inactive timestamps")
  11871. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11872. (t "scheduled/deadline")))
  11873. (setq ans (read-char-exclusive))
  11874. (cond
  11875. ((equal ans ?c)
  11876. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11877. ((equal ans ?d)
  11878. (call-interactively 'org-check-deadlines))
  11879. ((equal ans ?b)
  11880. (call-interactively 'org-check-before-date))
  11881. ((equal ans ?a)
  11882. (call-interactively 'org-check-after-date))
  11883. ((equal ans ?D)
  11884. (call-interactively 'org-check-dates-range))
  11885. ((equal ans ?t)
  11886. (call-interactively 'org-show-todo-tree))
  11887. ((equal ans ?T)
  11888. (org-show-todo-tree '(4)))
  11889. ((member ans '(?T ?m))
  11890. (call-interactively 'org-match-sparse-tree))
  11891. ((member ans '(?p ?P))
  11892. (setq kwd (org-icompleting-read "Property: "
  11893. (mapcar 'list (org-buffer-property-keys))))
  11894. (setq value (org-icompleting-read "Value: "
  11895. (mapcar 'list (org-property-values kwd))))
  11896. (unless (string-match "\\`{.*}\\'" value)
  11897. (setq value (concat "\"" value "\"")))
  11898. (org-match-sparse-tree arg (concat kwd "=" value)))
  11899. ((member ans '(?r ?R ?/))
  11900. (call-interactively 'org-occur))
  11901. (t (error "No such sparse tree command \"%c\"" ans)))))
  11902. (defvar org-occur-highlights nil
  11903. "List of overlays used for occur matches.")
  11904. (make-variable-buffer-local 'org-occur-highlights)
  11905. (defvar org-occur-parameters nil
  11906. "Parameters of the active org-occur calls.
  11907. This is a list, each call to org-occur pushes as cons cell,
  11908. containing the regular expression and the callback, onto the list.
  11909. The list can contain several entries if `org-occur' has been called
  11910. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11911. will only contain one set of parameters. When the highlights are
  11912. removed (for example with `C-c C-c', or with the next edit (depending
  11913. on `org-remove-highlights-with-change'), this variable is emptied
  11914. as well.")
  11915. (make-variable-buffer-local 'org-occur-parameters)
  11916. (defun org-occur (regexp &optional keep-previous callback)
  11917. "Make a compact tree which shows all matches of REGEXP.
  11918. The tree will show the lines where the regexp matches, and all higher
  11919. headlines above the match. It will also show the heading after the match,
  11920. to make sure editing the matching entry is easy.
  11921. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11922. call to `org-occur' will be kept, to allow stacking of calls to this
  11923. command.
  11924. If CALLBACK is non-nil, it is a function which is called to confirm
  11925. that the match should indeed be shown."
  11926. (interactive "sRegexp: \nP")
  11927. (when (equal regexp "")
  11928. (error "Regexp cannot be empty"))
  11929. (unless keep-previous
  11930. (org-remove-occur-highlights nil nil t))
  11931. (push (cons regexp callback) org-occur-parameters)
  11932. (let ((cnt 0))
  11933. (save-excursion
  11934. (goto-char (point-min))
  11935. (if (or (not keep-previous) ; do not want to keep
  11936. (not org-occur-highlights)) ; no previous matches
  11937. ;; hide everything
  11938. (org-overview))
  11939. (while (re-search-forward regexp nil t)
  11940. (when (or (not callback)
  11941. (save-match-data (funcall callback)))
  11942. (setq cnt (1+ cnt))
  11943. (when org-highlight-sparse-tree-matches
  11944. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11945. (org-show-context 'occur-tree))))
  11946. (when org-remove-highlights-with-change
  11947. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11948. nil 'local))
  11949. (unless org-sparse-tree-open-archived-trees
  11950. (org-hide-archived-subtrees (point-min) (point-max)))
  11951. (run-hooks 'org-occur-hook)
  11952. (if (org-called-interactively-p 'interactive)
  11953. (message "%d match(es) for regexp %s" cnt regexp))
  11954. cnt))
  11955. (defun org-occur-next-match (&optional n reset)
  11956. "Function for `next-error-function' to find sparse tree matches.
  11957. N is the number of matches to move, when negative move backwards.
  11958. RESET is entirely ignored - this function always goes back to the
  11959. starting point when no match is found."
  11960. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11961. (search-func (if (< n 0)
  11962. 'previous-single-char-property-change
  11963. 'next-single-char-property-change))
  11964. (n (abs n))
  11965. (pos (point))
  11966. p1)
  11967. (catch 'exit
  11968. (while (setq p1 (funcall search-func (point) 'org-type))
  11969. (when (equal p1 limit)
  11970. (goto-char pos)
  11971. (error "No more matches"))
  11972. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11973. (setq n (1- n))
  11974. (when (= n 0)
  11975. (goto-char p1)
  11976. (throw 'exit (point))))
  11977. (goto-char p1))
  11978. (goto-char p1)
  11979. (error "No more matches"))))
  11980. (defun org-show-context (&optional key)
  11981. "Make sure point and context are visible.
  11982. How much context is shown depends upon the variables
  11983. `org-show-hierarchy-above', `org-show-following-heading',
  11984. `org-show-entry-below' and `org-show-siblings'."
  11985. (let ((heading-p (org-at-heading-p t))
  11986. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11987. (following-p (org-get-alist-option org-show-following-heading key))
  11988. (entry-p (org-get-alist-option org-show-entry-below key))
  11989. (siblings-p (org-get-alist-option org-show-siblings key)))
  11990. (catch 'exit
  11991. ;; Show heading or entry text
  11992. (if (and heading-p (not entry-p))
  11993. (org-flag-heading nil) ; only show the heading
  11994. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11995. (org-show-hidden-entry))) ; show entire entry
  11996. (when following-p
  11997. ;; Show next sibling, or heading below text
  11998. (save-excursion
  11999. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12000. (org-flag-heading nil))))
  12001. (when siblings-p (org-show-siblings))
  12002. (when hierarchy-p
  12003. ;; show all higher headings, possibly with siblings
  12004. (save-excursion
  12005. (while (and (condition-case nil
  12006. (progn (org-up-heading-all 1) t)
  12007. (error nil))
  12008. (not (bobp)))
  12009. (org-flag-heading nil)
  12010. (when siblings-p (org-show-siblings))))))))
  12011. (defvar org-reveal-start-hook nil
  12012. "Hook run before revealing a location.")
  12013. (defun org-reveal (&optional siblings)
  12014. "Show current entry, hierarchy above it, and the following headline.
  12015. This can be used to show a consistent set of context around locations
  12016. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12017. not t for the search context.
  12018. With optional argument SIBLINGS, on each level of the hierarchy all
  12019. siblings are shown. This repairs the tree structure to what it would
  12020. look like when opened with hierarchical calls to `org-cycle'.
  12021. With double optional argument \\[universal-argument] \\[universal-argument], \
  12022. go to the parent and show the
  12023. entire tree."
  12024. (interactive "P")
  12025. (run-hooks 'org-reveal-start-hook)
  12026. (let ((org-show-hierarchy-above t)
  12027. (org-show-following-heading t)
  12028. (org-show-siblings (if siblings t org-show-siblings)))
  12029. (org-show-context nil))
  12030. (when (equal siblings '(16))
  12031. (save-excursion
  12032. (when (org-up-heading-safe)
  12033. (org-show-subtree)
  12034. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12035. (defun org-highlight-new-match (beg end)
  12036. "Highlight from BEG to END and mark the highlight is an occur headline."
  12037. (let ((ov (make-overlay beg end)))
  12038. (overlay-put ov 'face 'secondary-selection)
  12039. (overlay-put ov 'org-type 'org-occur)
  12040. (push ov org-occur-highlights)))
  12041. (defun org-remove-occur-highlights (&optional beg end noremove)
  12042. "Remove the occur highlights from the buffer.
  12043. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12044. from the `before-change-functions' in the current buffer."
  12045. (interactive)
  12046. (unless org-inhibit-highlight-removal
  12047. (mapc 'delete-overlay org-occur-highlights)
  12048. (setq org-occur-highlights nil)
  12049. (setq org-occur-parameters nil)
  12050. (unless noremove
  12051. (remove-hook 'before-change-functions
  12052. 'org-remove-occur-highlights 'local))))
  12053. ;;;; Priorities
  12054. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12055. "Regular expression matching the priority indicator.")
  12056. (defvar org-remove-priority-next-time nil)
  12057. (defun org-priority-up ()
  12058. "Increase the priority of the current item."
  12059. (interactive)
  12060. (org-priority 'up))
  12061. (defun org-priority-down ()
  12062. "Decrease the priority of the current item."
  12063. (interactive)
  12064. (org-priority 'down))
  12065. (defun org-priority (&optional action show)
  12066. "Change the priority of an item.
  12067. ACTION can be `set', `up', `down', or a character."
  12068. (interactive "P")
  12069. (if (equal action '(4))
  12070. (org-show-priority)
  12071. (unless org-enable-priority-commands
  12072. (error "Priority commands are disabled"))
  12073. (setq action (or action 'set))
  12074. (let (current new news have remove)
  12075. (save-excursion
  12076. (org-back-to-heading t)
  12077. (if (looking-at org-priority-regexp)
  12078. (setq current (string-to-char (match-string 2))
  12079. have t))
  12080. (cond
  12081. ((eq action 'remove)
  12082. (setq remove t new ?\ ))
  12083. ((or (eq action 'set)
  12084. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12085. (if (not (eq action 'set))
  12086. (setq new action)
  12087. (message "Priority %c-%c, SPC to remove: "
  12088. org-highest-priority org-lowest-priority)
  12089. (save-match-data
  12090. (setq new (read-char-exclusive))))
  12091. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12092. (= (upcase org-lowest-priority) org-lowest-priority))
  12093. (setq new (upcase new)))
  12094. (cond ((equal new ?\ ) (setq remove t))
  12095. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12096. (error "Priority must be between `%c' and `%c'"
  12097. org-highest-priority org-lowest-priority))))
  12098. ((eq action 'up)
  12099. (setq new (if have
  12100. (1- current) ; normal cycling
  12101. ;; last priority was empty
  12102. (if (eq last-command this-command)
  12103. org-lowest-priority ; wrap around empty to lowest
  12104. ;; default
  12105. (if org-priority-start-cycle-with-default
  12106. org-default-priority
  12107. (1- org-default-priority))))))
  12108. ((eq action 'down)
  12109. (setq new (if have
  12110. (1+ current) ; normal cycling
  12111. ;; last priority was empty
  12112. (if (eq last-command this-command)
  12113. org-highest-priority ; wrap around empty to highest
  12114. ;; default
  12115. (if org-priority-start-cycle-with-default
  12116. org-default-priority
  12117. (1+ org-default-priority))))))
  12118. (t (error "Invalid action")))
  12119. (if (or (< (upcase new) org-highest-priority)
  12120. (> (upcase new) org-lowest-priority))
  12121. (if (and (memq action '(up down))
  12122. (not have) (not (eq last-command this-command)))
  12123. ;; `new' is from default priority
  12124. (error
  12125. "The default can not be set, see `org-default-priority' why")
  12126. ;; normal cycling: `new' is beyond highest/lowest priority
  12127. ;; and is wrapped around to the empty priority
  12128. (setq remove t)))
  12129. (setq news (format "%c" new))
  12130. (if have
  12131. (if remove
  12132. (replace-match "" t t nil 1)
  12133. (replace-match news t t nil 2))
  12134. (if remove
  12135. (error "No priority cookie found in line")
  12136. (let ((case-fold-search nil))
  12137. (looking-at org-todo-line-regexp))
  12138. (if (match-end 2)
  12139. (progn
  12140. (goto-char (match-end 2))
  12141. (insert " [#" news "]"))
  12142. (goto-char (match-beginning 3))
  12143. (insert "[#" news "] "))))
  12144. (org-preserve-lc (org-set-tags nil 'align)))
  12145. (if remove
  12146. (message "Priority removed")
  12147. (message "Priority of current item set to %s" news)))))
  12148. (defun org-show-priority ()
  12149. "Show the priority of the current item.
  12150. This priority is composed of the main priority given with the [#A] cookies,
  12151. and by additional input from the age of a schedules or deadline entry."
  12152. (interactive)
  12153. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12154. (org-get-at-bol 'priority)
  12155. (save-excursion
  12156. (save-match-data
  12157. (beginning-of-line)
  12158. (and (looking-at org-heading-regexp)
  12159. (org-get-priority (match-string 0))))))))
  12160. (message "Priority is %d" (if pri pri -1000))))
  12161. (defun org-get-priority (s)
  12162. "Find priority cookie and return priority."
  12163. (save-match-data
  12164. (if (functionp org-get-priority-function)
  12165. (funcall org-get-priority-function)
  12166. (if (not (string-match org-priority-regexp s))
  12167. (* 1000 (- org-lowest-priority org-default-priority))
  12168. (* 1000 (- org-lowest-priority
  12169. (string-to-char (match-string 2 s))))))))
  12170. ;;;; Tags
  12171. (defvar org-agenda-archives-mode)
  12172. (defvar org-map-continue-from nil
  12173. "Position from where mapping should continue.
  12174. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12175. (defvar org-scanner-tags nil
  12176. "The current tag list while the tags scanner is running.")
  12177. (defvar org-trust-scanner-tags nil
  12178. "Should `org-get-tags-at' use the tags for the scanner.
  12179. This is for internal dynamical scoping only.
  12180. When this is non-nil, the function `org-get-tags-at' will return the value
  12181. of `org-scanner-tags' instead of building the list by itself. This
  12182. can lead to large speed-ups when the tags scanner is used in a file with
  12183. many entries, and when the list of tags is retrieved, for example to
  12184. obtain a list of properties. Building the tags list for each entry in such
  12185. a file becomes an N^2 operation - but with this variable set, it scales
  12186. as N.")
  12187. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12188. "Scan headline tags with inheritance and produce output ACTION.
  12189. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12190. or `agenda' to produce an entry list for an agenda view. It can also be
  12191. a Lisp form or a function that should be called at each matched headline, in
  12192. this case the return value is a list of all return values from these calls.
  12193. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12194. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12195. only lines with a not-done TODO keyword are included in the output.
  12196. This should be the same variable that was scoped into
  12197. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12198. START-LEVEL can be a string with asterisks, reducing the scope to
  12199. headlines matching this string."
  12200. (require 'org-agenda)
  12201. (let* ((re (concat "^"
  12202. (if start-level
  12203. ;; Get the correct level to match
  12204. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12205. org-outline-regexp)
  12206. " *\\(\\<\\("
  12207. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12208. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12209. (props (list 'face 'default
  12210. 'done-face 'org-agenda-done
  12211. 'undone-face 'default
  12212. 'mouse-face 'highlight
  12213. 'org-not-done-regexp org-not-done-regexp
  12214. 'org-todo-regexp org-todo-regexp
  12215. 'org-complex-heading-regexp org-complex-heading-regexp
  12216. 'help-echo
  12217. (format "mouse-2 or RET jump to org file %s"
  12218. (abbreviate-file-name
  12219. (or (buffer-file-name (buffer-base-buffer))
  12220. (buffer-name (buffer-base-buffer)))))))
  12221. (case-fold-search nil)
  12222. (org-map-continue-from nil)
  12223. lspos tags tags-list
  12224. (tags-alist (list (cons 0 org-file-tags)))
  12225. (llast 0) rtn rtn1 level category i txt
  12226. todo marker entry priority)
  12227. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12228. (setq action (list 'lambda nil action)))
  12229. (save-excursion
  12230. (goto-char (point-min))
  12231. (when (eq action 'sparse-tree)
  12232. (org-overview)
  12233. (org-remove-occur-highlights))
  12234. (while (re-search-forward re nil t)
  12235. (setq org-map-continue-from nil)
  12236. (catch :skip
  12237. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12238. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12239. (goto-char (setq lspos (match-beginning 0)))
  12240. (setq level (org-reduced-level (org-outline-level))
  12241. category (org-get-category))
  12242. (setq i llast llast level)
  12243. ;; remove tag lists from same and sublevels
  12244. (while (>= i level)
  12245. (when (setq entry (assoc i tags-alist))
  12246. (setq tags-alist (delete entry tags-alist)))
  12247. (setq i (1- i)))
  12248. ;; add the next tags
  12249. (when tags
  12250. (setq tags (org-split-string tags ":")
  12251. tags-alist
  12252. (cons (cons level tags) tags-alist)))
  12253. ;; compile tags for current headline
  12254. (setq tags-list
  12255. (if org-use-tag-inheritance
  12256. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12257. tags)
  12258. org-scanner-tags tags-list)
  12259. (when org-use-tag-inheritance
  12260. (setcdr (car tags-alist)
  12261. (mapcar (lambda (x)
  12262. (setq x (copy-sequence x))
  12263. (org-add-prop-inherited x))
  12264. (cdar tags-alist))))
  12265. (when (and tags org-use-tag-inheritance
  12266. (or (not (eq t org-use-tag-inheritance))
  12267. org-tags-exclude-from-inheritance))
  12268. ;; selective inheritance, remove uninherited ones
  12269. (setcdr (car tags-alist)
  12270. (org-remove-uninherited-tags (cdar tags-alist))))
  12271. (when (and
  12272. ;; eval matcher only when the todo condition is OK
  12273. (and (or (not todo-only) (member todo org-not-done-keywords))
  12274. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12275. (eval matcher)))
  12276. ;; Call the skipper, but return t if it does not skip,
  12277. ;; so that the `and' form continues evaluating
  12278. (progn
  12279. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12280. t)
  12281. ;; Check if timestamps are deselecting this entry
  12282. (or (not todo-only)
  12283. (and (member todo org-not-done-keywords)
  12284. (or (not org-agenda-tags-todo-honor-ignore-options)
  12285. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12286. ;; select this headline
  12287. (cond
  12288. ((eq action 'sparse-tree)
  12289. (and org-highlight-sparse-tree-matches
  12290. (org-get-heading) (match-end 0)
  12291. (org-highlight-new-match
  12292. (match-beginning 1) (match-end 1)))
  12293. (org-show-context 'tags-tree))
  12294. ((eq action 'agenda)
  12295. (setq txt (org-agenda-format-item
  12296. ""
  12297. (concat
  12298. (if (eq org-tags-match-list-sublevels 'indented)
  12299. (make-string (1- level) ?.) "")
  12300. (org-get-heading))
  12301. level category
  12302. tags-list)
  12303. priority (org-get-priority txt))
  12304. (goto-char lspos)
  12305. (setq marker (org-agenda-new-marker))
  12306. (org-add-props txt props
  12307. 'org-marker marker 'org-hd-marker marker 'org-category category
  12308. 'todo-state todo
  12309. 'priority priority 'type "tagsmatch")
  12310. (push txt rtn))
  12311. ((functionp action)
  12312. (setq org-map-continue-from nil)
  12313. (save-excursion
  12314. (setq rtn1 (funcall action))
  12315. (push rtn1 rtn)))
  12316. (t (error "Invalid action")))
  12317. ;; if we are to skip sublevels, jump to end of subtree
  12318. (unless org-tags-match-list-sublevels
  12319. (org-end-of-subtree t)
  12320. (backward-char 1))))
  12321. ;; Get the correct position from where to continue
  12322. (if org-map-continue-from
  12323. (goto-char org-map-continue-from)
  12324. (and (= (point) lspos) (end-of-line 1)))))
  12325. (when (and (eq action 'sparse-tree)
  12326. (not org-sparse-tree-open-archived-trees))
  12327. (org-hide-archived-subtrees (point-min) (point-max)))
  12328. (nreverse rtn)))
  12329. (defun org-remove-uninherited-tags (tags)
  12330. "Remove all tags that are not inherited from the list TAGS."
  12331. (cond
  12332. ((eq org-use-tag-inheritance t)
  12333. (if org-tags-exclude-from-inheritance
  12334. (org-delete-all org-tags-exclude-from-inheritance tags)
  12335. tags))
  12336. ((not org-use-tag-inheritance) nil)
  12337. ((stringp org-use-tag-inheritance)
  12338. (delq nil (mapcar
  12339. (lambda (x)
  12340. (if (and (string-match org-use-tag-inheritance x)
  12341. (not (member x org-tags-exclude-from-inheritance)))
  12342. x nil))
  12343. tags)))
  12344. ((listp org-use-tag-inheritance)
  12345. (delq nil (mapcar
  12346. (lambda (x)
  12347. (if (member x org-use-tag-inheritance) x nil))
  12348. tags)))))
  12349. (defun org-match-sparse-tree (&optional todo-only match)
  12350. "Create a sparse tree according to tags string MATCH.
  12351. MATCH can contain positive and negative selection of tags, like
  12352. \"+WORK+URGENT-WITHBOSS\".
  12353. If optional argument TODO-ONLY is non-nil, only select lines that are
  12354. also TODO lines."
  12355. (interactive "P")
  12356. (org-agenda-prepare-buffers (list (current-buffer)))
  12357. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12358. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12359. (defvar org-cached-props nil)
  12360. (defun org-cached-entry-get (pom property)
  12361. (if (or (eq t org-use-property-inheritance)
  12362. (and (stringp org-use-property-inheritance)
  12363. (string-match org-use-property-inheritance property))
  12364. (and (listp org-use-property-inheritance)
  12365. (member property org-use-property-inheritance)))
  12366. ;; Caching is not possible, check it directly
  12367. (org-entry-get pom property 'inherit)
  12368. ;; Get all properties, so that we can do complicated checks easily
  12369. (cdr (assoc property (or org-cached-props
  12370. (setq org-cached-props
  12371. (org-entry-properties pom)))))))
  12372. (defun org-global-tags-completion-table (&optional files)
  12373. "Return the list of all tags in all agenda buffer/files.
  12374. Optional FILES argument is a list of files which can be used
  12375. instead of the agenda files."
  12376. (save-excursion
  12377. (org-uniquify
  12378. (delq nil
  12379. (apply 'append
  12380. (mapcar
  12381. (lambda (file)
  12382. (set-buffer (find-file-noselect file))
  12383. (append (org-get-buffer-tags)
  12384. (mapcar (lambda (x) (if (stringp (car-safe x))
  12385. (list (car-safe x)) nil))
  12386. org-tag-alist)))
  12387. (if (and files (car files))
  12388. files
  12389. (org-agenda-files))))))))
  12390. (defun org-make-tags-matcher (match)
  12391. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12392. The variable `todo-only' is scoped dynamically into this function.
  12393. It will be set to t if the matcher restricts matching to TODO entries,
  12394. otherwise will not be touched.
  12395. Returns a cons of the selection string MATCH and the constructed
  12396. lisp form implementing the matcher. The matcher is to be evaluated
  12397. at an Org entry, with point on the headline, and returns t if the
  12398. entry matches the selection string MATCH. The returned lisp form
  12399. references two variables with information about the entry, which
  12400. must be bound around the form's evaluation: todo, the TODO keyword
  12401. at the entry (or nil of none); and tags-list, the list of all tags
  12402. at the entry including inherited ones. Additionally, the category
  12403. of the entry (if any) must be specified as the text property
  12404. 'org-category on the headline.
  12405. See also `org-scan-tags'.
  12406. "
  12407. (declare (special todo-only))
  12408. (unless (boundp 'todo-only)
  12409. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12410. (unless match
  12411. ;; Get a new match request, with completion
  12412. (let ((org-last-tags-completion-table
  12413. (org-global-tags-completion-table)))
  12414. (setq match (org-completing-read-no-i
  12415. "Match: " 'org-tags-completion-function nil nil nil
  12416. 'org-tags-history))))
  12417. ;; Parse the string and create a lisp form
  12418. (let ((match0 match)
  12419. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12420. minus tag mm
  12421. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12422. orterms term orlist re-p str-p level-p level-op time-p
  12423. prop-p pn pv po gv rest)
  12424. (if (string-match "/+" match)
  12425. ;; match contains also a todo-matching request
  12426. (progn
  12427. (setq tagsmatch (substring match 0 (match-beginning 0))
  12428. todomatch (substring match (match-end 0)))
  12429. (if (string-match "^!" todomatch)
  12430. (setq todo-only t todomatch (substring todomatch 1)))
  12431. (if (string-match "^\\s-*$" todomatch)
  12432. (setq todomatch nil)))
  12433. ;; only matching tags
  12434. (setq tagsmatch match todomatch nil))
  12435. ;; Make the tags matcher
  12436. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12437. (setq tagsmatcher t)
  12438. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12439. (while (setq term (pop orterms))
  12440. (while (and (equal (substring term -1) "\\") orterms)
  12441. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12442. (while (string-match re term)
  12443. (setq rest (substring term (match-end 0))
  12444. minus (and (match-end 1)
  12445. (equal (match-string 1 term) "-"))
  12446. tag (save-match-data (replace-regexp-in-string
  12447. "\\\\-" "-"
  12448. (match-string 2 term)))
  12449. re-p (equal (string-to-char tag) ?{)
  12450. level-p (match-end 4)
  12451. prop-p (match-end 5)
  12452. mm (cond
  12453. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12454. (level-p
  12455. (setq level-op (org-op-to-function (match-string 3 term)))
  12456. `(,level-op level ,(string-to-number
  12457. (match-string 4 term))))
  12458. (prop-p
  12459. (setq pn (match-string 5 term)
  12460. po (match-string 6 term)
  12461. pv (match-string 7 term)
  12462. re-p (equal (string-to-char pv) ?{)
  12463. str-p (equal (string-to-char pv) ?\")
  12464. time-p (save-match-data
  12465. (string-match "^\"[[<].*[]>]\"$" pv))
  12466. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12467. (if time-p (setq pv (org-matcher-time pv)))
  12468. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12469. (cond
  12470. ((equal pn "CATEGORY")
  12471. (setq gv '(get-text-property (point) 'org-category)))
  12472. ((equal pn "TODO")
  12473. (setq gv 'todo))
  12474. (t
  12475. (setq gv `(org-cached-entry-get nil ,pn))))
  12476. (if re-p
  12477. (if (eq po 'org<>)
  12478. `(not (string-match ,pv (or ,gv "")))
  12479. `(string-match ,pv (or ,gv "")))
  12480. (if str-p
  12481. `(,po (or ,gv "") ,pv)
  12482. `(,po (string-to-number (or ,gv ""))
  12483. ,(string-to-number pv) ))))
  12484. (t `(member ,tag tags-list)))
  12485. mm (if minus (list 'not mm) mm)
  12486. term rest)
  12487. (push mm tagsmatcher))
  12488. (push (if (> (length tagsmatcher) 1)
  12489. (cons 'and tagsmatcher)
  12490. (car tagsmatcher))
  12491. orlist)
  12492. (setq tagsmatcher nil))
  12493. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12494. (setq tagsmatcher
  12495. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12496. ;; Make the todo matcher
  12497. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12498. (setq todomatcher t)
  12499. (setq orterms (org-split-string todomatch "|") orlist nil)
  12500. (while (setq term (pop orterms))
  12501. (while (string-match re term)
  12502. (setq minus (and (match-end 1)
  12503. (equal (match-string 1 term) "-"))
  12504. kwd (match-string 2 term)
  12505. re-p (equal (string-to-char kwd) ?{)
  12506. term (substring term (match-end 0))
  12507. mm (if re-p
  12508. `(string-match ,(substring kwd 1 -1) todo)
  12509. (list 'equal 'todo kwd))
  12510. mm (if minus (list 'not mm) mm))
  12511. (push mm todomatcher))
  12512. (push (if (> (length todomatcher) 1)
  12513. (cons 'and todomatcher)
  12514. (car todomatcher))
  12515. orlist)
  12516. (setq todomatcher nil))
  12517. (setq todomatcher (if (> (length orlist) 1)
  12518. (cons 'or orlist) (car orlist))))
  12519. ;; Return the string and lisp forms of the matcher
  12520. (setq matcher (if todomatcher
  12521. (list 'and tagsmatcher todomatcher)
  12522. tagsmatcher))
  12523. (when todo-only
  12524. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12525. matcher)))
  12526. (cons match0 matcher)))
  12527. (defun org-op-to-function (op &optional stringp)
  12528. "Turn an operator into the appropriate function."
  12529. (setq op
  12530. (cond
  12531. ((equal op "<" ) '(< string< org-time<))
  12532. ((equal op ">" ) '(> org-string> org-time>))
  12533. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12534. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12535. ((member op '("=" "==")) '(= string= org-time=))
  12536. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12537. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12538. (defun org<> (a b) (not (= a b)))
  12539. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12540. (defun org-string>= (a b) (not (string< a b)))
  12541. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12542. (defun org-string<> (a b) (not (string= a b)))
  12543. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12544. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12545. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12546. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12547. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12548. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12549. (defun org-2ft (s)
  12550. "Convert S to a floating point time.
  12551. If S is already a number, just return it. If it is a string, parse
  12552. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12553. (cond
  12554. ((numberp s) s)
  12555. ((stringp s)
  12556. (condition-case nil
  12557. (float-time (apply 'encode-time (org-parse-time-string s)))
  12558. (error 0.)))
  12559. (t 0.)))
  12560. (defun org-time-today ()
  12561. "Time in seconds today at 0:00.
  12562. Returns the float number of seconds since the beginning of the
  12563. epoch to the beginning of today (00:00)."
  12564. (float-time (apply 'encode-time
  12565. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12566. (defun org-matcher-time (s)
  12567. "Interpret a time comparison value."
  12568. (save-match-data
  12569. (cond
  12570. ((string= s "<now>") (float-time))
  12571. ((string= s "<today>") (org-time-today))
  12572. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12573. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12574. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12575. (+ (org-time-today)
  12576. (* (string-to-number (match-string 1 s))
  12577. (cdr (assoc (match-string 2 s)
  12578. '(("d" . 86400.0) ("w" . 604800.0)
  12579. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12580. (t (org-2ft s)))))
  12581. (defun org-match-any-p (re list)
  12582. "Does re match any element of list?"
  12583. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12584. (delq nil list))
  12585. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12586. (defvar org-tags-overlay (make-overlay 1 1))
  12587. (org-detach-overlay org-tags-overlay)
  12588. (defun org-get-local-tags-at (&optional pos)
  12589. "Get a list of tags defined in the current headline."
  12590. (org-get-tags-at pos 'local))
  12591. (defun org-get-local-tags ()
  12592. "Get a list of tags defined in the current headline."
  12593. (org-get-tags-at nil 'local))
  12594. (defun org-get-tags-at (&optional pos local)
  12595. "Get a list of all headline tags applicable at POS.
  12596. POS defaults to point. If tags are inherited, the list contains
  12597. the targets in the same sequence as the headlines appear, i.e.
  12598. the tags of the current headline come last.
  12599. When LOCAL is non-nil, only return tags from the current headline,
  12600. ignore inherited ones."
  12601. (interactive)
  12602. (if (and org-trust-scanner-tags
  12603. (or (not pos) (equal pos (point)))
  12604. (not local))
  12605. org-scanner-tags
  12606. (let (tags ltags lastpos parent)
  12607. (save-excursion
  12608. (save-restriction
  12609. (widen)
  12610. (goto-char (or pos (point)))
  12611. (save-match-data
  12612. (catch 'done
  12613. (condition-case nil
  12614. (progn
  12615. (org-back-to-heading t)
  12616. (while (not (equal lastpos (point)))
  12617. (setq lastpos (point))
  12618. (when (looking-at
  12619. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12620. (setq ltags (org-split-string
  12621. (org-match-string-no-properties 1) ":"))
  12622. (when parent
  12623. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12624. (setq tags (append
  12625. (if parent
  12626. (org-remove-uninherited-tags ltags)
  12627. ltags)
  12628. tags)))
  12629. (or org-use-tag-inheritance (throw 'done t))
  12630. (if local (throw 'done t))
  12631. (or (org-up-heading-safe) (error nil))
  12632. (setq parent t)))
  12633. (error nil)))))
  12634. (if local
  12635. tags
  12636. (reverse (delete-dups
  12637. (reverse (append
  12638. (org-remove-uninherited-tags
  12639. org-file-tags) tags)))))))))
  12640. (defun org-add-prop-inherited (s)
  12641. (add-text-properties 0 (length s) '(inherited t) s)
  12642. s)
  12643. (defun org-toggle-tag (tag &optional onoff)
  12644. "Toggle the tag TAG for the current line.
  12645. If ONOFF is `on' or `off', don't toggle but set to this state."
  12646. (let (res current)
  12647. (save-excursion
  12648. (org-back-to-heading t)
  12649. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12650. (point-at-eol) t)
  12651. (progn
  12652. (setq current (match-string 1))
  12653. (replace-match ""))
  12654. (setq current ""))
  12655. (setq current (nreverse (org-split-string current ":")))
  12656. (cond
  12657. ((eq onoff 'on)
  12658. (setq res t)
  12659. (or (member tag current) (push tag current)))
  12660. ((eq onoff 'off)
  12661. (or (not (member tag current)) (setq current (delete tag current))))
  12662. (t (if (member tag current)
  12663. (setq current (delete tag current))
  12664. (setq res t)
  12665. (push tag current))))
  12666. (end-of-line 1)
  12667. (if current
  12668. (progn
  12669. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12670. (org-set-tags nil t))
  12671. (delete-horizontal-space))
  12672. (run-hooks 'org-after-tags-change-hook))
  12673. res))
  12674. (defun org-align-tags-here (to-col)
  12675. ;; Assumes that this is a headline
  12676. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12677. (beginning-of-line 1)
  12678. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12679. (< pos (match-beginning 2)))
  12680. (progn
  12681. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12682. (goto-char (match-beginning 1))
  12683. (insert " ")
  12684. (delete-region (point) (1+ (match-beginning 2)))
  12685. (setq ncol (max (current-column)
  12686. (1+ col)
  12687. (if (> to-col 0)
  12688. to-col
  12689. (- (abs to-col) tags-l))))
  12690. (setq p (point))
  12691. (insert (make-string (- ncol (current-column)) ?\ ))
  12692. (setq ncol (current-column))
  12693. (when indent-tabs-mode (tabify p (point-at-eol)))
  12694. (org-move-to-column (min ncol col) t))
  12695. (goto-char pos))))
  12696. (defun org-set-tags-command (&optional arg just-align)
  12697. "Call the set-tags command for the current entry."
  12698. (interactive "P")
  12699. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12700. (org-set-tags arg just-align)
  12701. (save-excursion
  12702. (org-back-to-heading t)
  12703. (org-set-tags arg just-align))))
  12704. (defun org-set-tags-to (data)
  12705. "Set the tags of the current entry to DATA, replacing the current tags.
  12706. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12707. If DATA is nil or the empty string, any tags will be removed."
  12708. (interactive "sTags: ")
  12709. (setq data
  12710. (cond
  12711. ((eq data nil) "")
  12712. ((equal data "") "")
  12713. ((stringp data)
  12714. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12715. ":"))
  12716. ((listp data)
  12717. (concat ":" (mapconcat 'identity data ":") ":"))))
  12718. (when data
  12719. (save-excursion
  12720. (org-back-to-heading t)
  12721. (when (looking-at org-complex-heading-regexp)
  12722. (if (match-end 5)
  12723. (progn
  12724. (goto-char (match-beginning 5))
  12725. (insert data)
  12726. (delete-region (point) (point-at-eol))
  12727. (org-set-tags nil 'align))
  12728. (goto-char (point-at-eol))
  12729. (insert " " data)
  12730. (org-set-tags nil 'align)))
  12731. (beginning-of-line 1)
  12732. (if (looking-at ".*?\\([ \t]+\\)$")
  12733. (delete-region (match-beginning 1) (match-end 1))))))
  12734. (defun org-align-all-tags ()
  12735. "Align the tags i all headings."
  12736. (interactive)
  12737. (save-excursion
  12738. (or (ignore-errors (org-back-to-heading t))
  12739. (outline-next-heading))
  12740. (if (org-at-heading-p)
  12741. (org-set-tags t)
  12742. (message "No headings"))))
  12743. (defvar org-indent-indentation-per-level)
  12744. (defun org-set-tags (&optional arg just-align)
  12745. "Set the tags for the current headline.
  12746. With prefix ARG, realign all tags in headings in the current buffer."
  12747. (interactive "P")
  12748. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12749. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12750. 'region-start-level 'region))
  12751. org-loop-over-headlines-in-active-region)
  12752. (org-map-entries
  12753. ;; We don't use ARG and JUST-ALIGN here these args are not
  12754. ;; useful when looping over headlines
  12755. `(org-set-tags)
  12756. org-loop-over-headlines-in-active-region
  12757. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12758. (let* ((re org-outline-regexp-bol)
  12759. (current (unless arg (org-get-tags-string)))
  12760. (col (current-column))
  12761. (org-setting-tags t)
  12762. table current-tags inherited-tags ; computed below when needed
  12763. tags p0 c0 c1 rpl di tc level)
  12764. (if arg
  12765. (save-excursion
  12766. (goto-char (point-min))
  12767. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12768. (while (re-search-forward re nil t)
  12769. (org-set-tags nil t)
  12770. (end-of-line 1)))
  12771. (message "All tags realigned to column %d" org-tags-column))
  12772. (if just-align
  12773. (setq tags current)
  12774. ;; Get a new set of tags from the user
  12775. (save-excursion
  12776. (setq table (append org-tag-persistent-alist
  12777. (or org-tag-alist (org-get-buffer-tags))
  12778. (and
  12779. org-complete-tags-always-offer-all-agenda-tags
  12780. (org-global-tags-completion-table
  12781. (org-agenda-files))))
  12782. org-last-tags-completion-table table
  12783. current-tags (org-split-string current ":")
  12784. inherited-tags (nreverse
  12785. (nthcdr (length current-tags)
  12786. (nreverse (org-get-tags-at))))
  12787. tags
  12788. (if (or (eq t org-use-fast-tag-selection)
  12789. (and org-use-fast-tag-selection
  12790. (delq nil (mapcar 'cdr table))))
  12791. (org-fast-tag-selection
  12792. current-tags inherited-tags table
  12793. (if org-fast-tag-selection-include-todo
  12794. org-todo-key-alist))
  12795. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12796. (org-trim
  12797. (org-icompleting-read "Tags: "
  12798. 'org-tags-completion-function
  12799. nil nil current 'org-tags-history))))))
  12800. (while (string-match "[-+&]+" tags)
  12801. ;; No boolean logic, just a list
  12802. (setq tags (replace-match ":" t t tags))))
  12803. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12804. (if org-tags-sort-function
  12805. (setq tags (mapconcat 'identity
  12806. (sort (org-split-string
  12807. tags (org-re "[^[:alnum:]_@#%]+"))
  12808. org-tags-sort-function) ":")))
  12809. (if (string-match "\\`[\t ]*\\'" tags)
  12810. (setq tags "")
  12811. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12812. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12813. ;; Insert new tags at the correct column
  12814. (beginning-of-line 1)
  12815. (setq level (or (and (looking-at org-outline-regexp)
  12816. (- (match-end 0) (point) 1))
  12817. 1))
  12818. (cond
  12819. ((and (equal current "") (equal tags "")))
  12820. ((re-search-forward
  12821. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12822. (point-at-eol) t)
  12823. (if (equal tags "")
  12824. (setq rpl "")
  12825. (goto-char (match-beginning 0))
  12826. (setq c0 (current-column)
  12827. ;; compute offset for the case of org-indent-mode active
  12828. di (if org-indent-mode
  12829. (* (1- org-indent-indentation-per-level) (1- level))
  12830. 0)
  12831. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12832. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12833. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12834. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12835. (replace-match rpl t t)
  12836. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12837. tags)
  12838. (t (error "Tags alignment failed")))
  12839. (org-move-to-column col)
  12840. (unless just-align
  12841. (run-hooks 'org-after-tags-change-hook))))))
  12842. (defun org-change-tag-in-region (beg end tag off)
  12843. "Add or remove TAG for each entry in the region.
  12844. This works in the agenda, and also in an org-mode buffer."
  12845. (interactive
  12846. (list (region-beginning) (region-end)
  12847. (let ((org-last-tags-completion-table
  12848. (if (derived-mode-p 'org-mode)
  12849. (org-get-buffer-tags)
  12850. (org-global-tags-completion-table))))
  12851. (org-icompleting-read
  12852. "Tag: " 'org-tags-completion-function nil nil nil
  12853. 'org-tags-history))
  12854. (progn
  12855. (message "[s]et or [r]emove? ")
  12856. (equal (read-char-exclusive) ?r))))
  12857. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12858. (let ((agendap (equal major-mode 'org-agenda-mode))
  12859. l1 l2 m buf pos newhead (cnt 0))
  12860. (goto-char end)
  12861. (setq l2 (1- (org-current-line)))
  12862. (goto-char beg)
  12863. (setq l1 (org-current-line))
  12864. (loop for l from l1 to l2 do
  12865. (org-goto-line l)
  12866. (setq m (get-text-property (point) 'org-hd-marker))
  12867. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12868. (and agendap m))
  12869. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12870. pos (if agendap m (point)))
  12871. (with-current-buffer buf
  12872. (save-excursion
  12873. (save-restriction
  12874. (goto-char pos)
  12875. (setq cnt (1+ cnt))
  12876. (org-toggle-tag tag (if off 'off 'on))
  12877. (setq newhead (org-get-heading)))))
  12878. (and agendap (org-agenda-change-all-lines newhead m))))
  12879. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12880. (defun org-tags-completion-function (string predicate &optional flag)
  12881. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12882. (confirm (lambda (x) (stringp (car x)))))
  12883. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12884. (setq s1 (match-string 1 string)
  12885. s2 (match-string 2 string))
  12886. (setq s1 "" s2 string))
  12887. (cond
  12888. ((eq flag nil)
  12889. ;; try completion
  12890. (setq rtn (try-completion s2 ctable confirm))
  12891. (if (stringp rtn)
  12892. (setq rtn
  12893. (concat s1 s2 (substring rtn (length s2))
  12894. (if (and org-add-colon-after-tag-completion
  12895. (assoc rtn ctable))
  12896. ":" ""))))
  12897. rtn)
  12898. ((eq flag t)
  12899. ;; all-completions
  12900. (all-completions s2 ctable confirm)
  12901. )
  12902. ((eq flag 'lambda)
  12903. ;; exact match?
  12904. (assoc s2 ctable)))
  12905. ))
  12906. (defun org-fast-tag-insert (kwd tags face &optional end)
  12907. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12908. (insert (format "%-12s" (concat kwd ":"))
  12909. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12910. (or end "")))
  12911. (defun org-fast-tag-show-exit (flag)
  12912. (save-excursion
  12913. (org-goto-line 3)
  12914. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12915. (replace-match ""))
  12916. (when flag
  12917. (end-of-line 1)
  12918. (org-move-to-column (- (window-width) 19) t)
  12919. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12920. (defun org-set-current-tags-overlay (current prefix)
  12921. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12922. (if (featurep 'xemacs)
  12923. (org-overlay-display org-tags-overlay (concat prefix s)
  12924. 'secondary-selection)
  12925. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12926. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12927. (defvar org-last-tag-selection-key nil)
  12928. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12929. "Fast tag selection with single keys.
  12930. CURRENT is the current list of tags in the headline, INHERITED is the
  12931. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12932. possibly with grouping information. TODO-TABLE is a similar table with
  12933. TODO keywords, should these have keys assigned to them.
  12934. If the keys are nil, a-z are automatically assigned.
  12935. Returns the new tags string, or nil to not change the current settings."
  12936. (let* ((fulltable (append table todo-table))
  12937. (maxlen (apply 'max (mapcar
  12938. (lambda (x)
  12939. (if (stringp (car x)) (string-width (car x)) 0))
  12940. fulltable)))
  12941. (buf (current-buffer))
  12942. (expert (eq org-fast-tag-selection-single-key 'expert))
  12943. (buffer-tags nil)
  12944. (fwidth (+ maxlen 3 1 3))
  12945. (ncol (/ (- (window-width) 4) fwidth))
  12946. (i-face 'org-done)
  12947. (c-face 'org-todo)
  12948. tg cnt e c char c1 c2 ntable tbl rtn
  12949. ov-start ov-end ov-prefix
  12950. (exit-after-next org-fast-tag-selection-single-key)
  12951. (done-keywords org-done-keywords)
  12952. groups ingroup)
  12953. (save-excursion
  12954. (beginning-of-line 1)
  12955. (if (looking-at
  12956. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12957. (setq ov-start (match-beginning 1)
  12958. ov-end (match-end 1)
  12959. ov-prefix "")
  12960. (setq ov-start (1- (point-at-eol))
  12961. ov-end (1+ ov-start))
  12962. (skip-chars-forward "^\n\r")
  12963. (setq ov-prefix
  12964. (concat
  12965. (buffer-substring (1- (point)) (point))
  12966. (if (> (current-column) org-tags-column)
  12967. " "
  12968. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12969. (move-overlay org-tags-overlay ov-start ov-end)
  12970. (save-window-excursion
  12971. (if expert
  12972. (set-buffer (get-buffer-create " *Org tags*"))
  12973. (delete-other-windows)
  12974. (split-window-vertically)
  12975. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12976. (erase-buffer)
  12977. (org-set-local 'org-done-keywords done-keywords)
  12978. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12979. (org-fast-tag-insert "Current" current c-face "\n\n")
  12980. (org-fast-tag-show-exit exit-after-next)
  12981. (org-set-current-tags-overlay current ov-prefix)
  12982. (setq tbl fulltable char ?a cnt 0)
  12983. (while (setq e (pop tbl))
  12984. (cond
  12985. ((equal (car e) :startgroup)
  12986. (push '() groups) (setq ingroup t)
  12987. (when (not (= cnt 0))
  12988. (setq cnt 0)
  12989. (insert "\n"))
  12990. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12991. ((equal (car e) :endgroup)
  12992. (setq ingroup nil cnt 0)
  12993. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12994. ((equal e '(:newline))
  12995. (when (not (= cnt 0))
  12996. (setq cnt 0)
  12997. (insert "\n")
  12998. (setq e (car tbl))
  12999. (while (equal (car tbl) '(:newline))
  13000. (insert "\n")
  13001. (setq tbl (cdr tbl)))))
  13002. (t
  13003. (setq tg (copy-sequence (car e)) c2 nil)
  13004. (if (cdr e)
  13005. (setq c (cdr e))
  13006. ;; automatically assign a character.
  13007. (setq c1 (string-to-char
  13008. (downcase (substring
  13009. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13010. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13011. (while (or (rassoc char ntable) (rassoc char table))
  13012. (setq char (1+ char)))
  13013. (setq c2 c1))
  13014. (setq c (or c2 char)))
  13015. (if ingroup (push tg (car groups)))
  13016. (setq tg (org-add-props tg nil 'face
  13017. (cond
  13018. ((not (assoc tg table))
  13019. (org-get-todo-face tg))
  13020. ((member tg current) c-face)
  13021. ((member tg inherited) i-face))))
  13022. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13023. (insert "[" c "] " tg (make-string
  13024. (- fwidth 4 (length tg)) ?\ ))
  13025. (push (cons tg c) ntable)
  13026. (when (= (setq cnt (1+ cnt)) ncol)
  13027. (insert "\n")
  13028. (if ingroup (insert " "))
  13029. (setq cnt 0)))))
  13030. (setq ntable (nreverse ntable))
  13031. (insert "\n")
  13032. (goto-char (point-min))
  13033. (if (not expert) (org-fit-window-to-buffer))
  13034. (setq rtn
  13035. (catch 'exit
  13036. (while t
  13037. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13038. (if (not groups) "no " "")
  13039. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13040. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13041. (setq org-last-tag-selection-key c)
  13042. (cond
  13043. ((= c ?\r) (throw 'exit t))
  13044. ((= c ?!)
  13045. (setq groups (not groups))
  13046. (goto-char (point-min))
  13047. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13048. ((= c ?\C-c)
  13049. (if (not expert)
  13050. (org-fast-tag-show-exit
  13051. (setq exit-after-next (not exit-after-next)))
  13052. (setq expert nil)
  13053. (delete-other-windows)
  13054. (set-window-buffer (split-window-vertically) " *Org tags*")
  13055. (org-switch-to-buffer-other-window " *Org tags*")
  13056. (org-fit-window-to-buffer)))
  13057. ((or (= c ?\C-g)
  13058. (and (= c ?q) (not (rassoc c ntable))))
  13059. (org-detach-overlay org-tags-overlay)
  13060. (setq quit-flag t))
  13061. ((= c ?\ )
  13062. (setq current nil)
  13063. (if exit-after-next (setq exit-after-next 'now)))
  13064. ((= c ?\t)
  13065. (condition-case nil
  13066. (setq tg (org-icompleting-read
  13067. "Tag: "
  13068. (or buffer-tags
  13069. (with-current-buffer buf
  13070. (org-get-buffer-tags)))))
  13071. (quit (setq tg "")))
  13072. (when (string-match "\\S-" tg)
  13073. (add-to-list 'buffer-tags (list tg))
  13074. (if (member tg current)
  13075. (setq current (delete tg current))
  13076. (push tg current)))
  13077. (if exit-after-next (setq exit-after-next 'now)))
  13078. ((setq e (rassoc c todo-table) tg (car e))
  13079. (with-current-buffer buf
  13080. (save-excursion (org-todo tg)))
  13081. (if exit-after-next (setq exit-after-next 'now)))
  13082. ((setq e (rassoc c ntable) tg (car e))
  13083. (if (member tg current)
  13084. (setq current (delete tg current))
  13085. (loop for g in groups do
  13086. (if (member tg g)
  13087. (mapc (lambda (x)
  13088. (setq current (delete x current)))
  13089. g)))
  13090. (push tg current))
  13091. (if exit-after-next (setq exit-after-next 'now))))
  13092. ;; Create a sorted list
  13093. (setq current
  13094. (sort current
  13095. (lambda (a b)
  13096. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13097. (if (eq exit-after-next 'now) (throw 'exit t))
  13098. (goto-char (point-min))
  13099. (beginning-of-line 2)
  13100. (delete-region (point) (point-at-eol))
  13101. (org-fast-tag-insert "Current" current c-face)
  13102. (org-set-current-tags-overlay current ov-prefix)
  13103. (while (re-search-forward
  13104. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13105. (setq tg (match-string 1))
  13106. (add-text-properties
  13107. (match-beginning 1) (match-end 1)
  13108. (list 'face
  13109. (cond
  13110. ((member tg current) c-face)
  13111. ((member tg inherited) i-face)
  13112. (t (get-text-property (match-beginning 1) 'face))))))
  13113. (goto-char (point-min)))))
  13114. (org-detach-overlay org-tags-overlay)
  13115. (if rtn
  13116. (mapconcat 'identity current ":")
  13117. nil))))
  13118. (defun org-get-tags-string ()
  13119. "Get the TAGS string in the current headline."
  13120. (unless (org-at-heading-p t)
  13121. (error "Not on a heading"))
  13122. (save-excursion
  13123. (beginning-of-line 1)
  13124. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13125. (org-match-string-no-properties 1)
  13126. "")))
  13127. (defun org-get-tags ()
  13128. "Get the list of tags specified in the current headline."
  13129. (org-split-string (org-get-tags-string) ":"))
  13130. (defun org-get-buffer-tags ()
  13131. "Get a table of all tags used in the buffer, for completion."
  13132. (let (tags)
  13133. (save-excursion
  13134. (goto-char (point-min))
  13135. (while (re-search-forward
  13136. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13137. (when (equal (char-after (point-at-bol 0)) ?*)
  13138. (mapc (lambda (x) (add-to-list 'tags x))
  13139. (org-split-string (org-match-string-no-properties 1) ":")))))
  13140. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13141. (mapcar 'list tags)))
  13142. ;;;; The mapping API
  13143. (defun org-map-entries (func &optional match scope &rest skip)
  13144. "Call FUNC at each headline selected by MATCH in SCOPE.
  13145. FUNC is a function or a lisp form. The function will be called without
  13146. arguments, with the cursor positioned at the beginning of the headline.
  13147. The return values of all calls to the function will be collected and
  13148. returned as a list.
  13149. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13150. does not need to preserve point. After evaluation, the cursor will be
  13151. moved to the end of the line (presumably of the headline of the
  13152. processed entry) and search continues from there. Under some
  13153. circumstances, this may not produce the wanted results. For example,
  13154. if you have removed (e.g. archived) the current (sub)tree it could
  13155. mean that the next entry will be skipped entirely. In such cases, you
  13156. can specify the position from where search should continue by making
  13157. FUNC set the variable `org-map-continue-from' to the desired buffer
  13158. position.
  13159. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13160. Only headlines that are matched by this query will be considered during
  13161. the iteration. When MATCH is nil or t, all headlines will be
  13162. visited by the iteration.
  13163. SCOPE determines the scope of this command. It can be any of:
  13164. nil The current buffer, respecting the restriction if any
  13165. tree The subtree started with the entry at point
  13166. region The entries within the active region, if any
  13167. region-start-level
  13168. The entries within the active region, but only those at
  13169. the same level than the first one.
  13170. file The current buffer, without restriction
  13171. file-with-archives
  13172. The current buffer, and any archives associated with it
  13173. agenda All agenda files
  13174. agenda-with-archives
  13175. All agenda files with any archive files associated with them
  13176. \(file1 file2 ...)
  13177. If this is a list, all files in the list will be scanned
  13178. The remaining args are treated as settings for the skipping facilities of
  13179. the scanner. The following items can be given here:
  13180. archive skip trees with the archive tag.
  13181. comment skip trees with the COMMENT keyword
  13182. function or Emacs Lisp form:
  13183. will be used as value for `org-agenda-skip-function', so whenever
  13184. the function returns t, FUNC will not be called for that
  13185. entry and search will continue from the point where the
  13186. function leaves it.
  13187. If your function needs to retrieve the tags including inherited tags
  13188. at the *current* entry, you can use the value of the variable
  13189. `org-scanner-tags' which will be much faster than getting the value
  13190. with `org-get-tags-at'. If your function gets properties with
  13191. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13192. to t around the call to `org-entry-properties' to get the same speedup.
  13193. Note that if your function moves around to retrieve tags and properties at
  13194. a *different* entry, you cannot use these techniques."
  13195. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13196. (not (org-region-active-p)))
  13197. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13198. (org-agenda-skip-archived-trees (memq 'archive skip))
  13199. (org-agenda-skip-comment-trees (memq 'comment skip))
  13200. (org-agenda-skip-function
  13201. (car (org-delete-all '(comment archive) skip)))
  13202. (org-tags-match-list-sublevels t)
  13203. (start-level (eq scope 'region-start-level))
  13204. matcher file res
  13205. org-todo-keywords-for-agenda
  13206. org-done-keywords-for-agenda
  13207. org-todo-keyword-alist-for-agenda
  13208. org-drawers-for-agenda
  13209. org-tag-alist-for-agenda
  13210. todo-only)
  13211. (cond
  13212. ((eq match t) (setq matcher t))
  13213. ((eq match nil) (setq matcher t))
  13214. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13215. (save-excursion
  13216. (save-restriction
  13217. (cond ((eq scope 'tree)
  13218. (org-back-to-heading t)
  13219. (org-narrow-to-subtree)
  13220. (setq scope nil))
  13221. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13222. (org-region-active-p))
  13223. ;; If needed, set start-level to a string like "2"
  13224. (when start-level
  13225. (save-excursion
  13226. (goto-char (region-beginning))
  13227. (unless (org-at-heading-p) (outline-next-heading))
  13228. (setq start-level (org-current-level))))
  13229. (narrow-to-region (region-beginning)
  13230. (save-excursion
  13231. (goto-char (region-end))
  13232. (unless (and (bolp) (org-at-heading-p))
  13233. (outline-next-heading))
  13234. (point)))
  13235. (setq scope nil)))
  13236. (if (not scope)
  13237. (progn
  13238. (org-agenda-prepare-buffers
  13239. (list (buffer-file-name (current-buffer))))
  13240. (setq res (org-scan-tags func matcher todo-only start-level)))
  13241. ;; Get the right scope
  13242. (cond
  13243. ((and scope (listp scope) (symbolp (car scope)))
  13244. (setq scope (eval scope)))
  13245. ((eq scope 'agenda)
  13246. (setq scope (org-agenda-files t)))
  13247. ((eq scope 'agenda-with-archives)
  13248. (setq scope (org-agenda-files t))
  13249. (setq scope (org-add-archive-files scope)))
  13250. ((eq scope 'file)
  13251. (setq scope (list (buffer-file-name))))
  13252. ((eq scope 'file-with-archives)
  13253. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13254. (org-agenda-prepare-buffers scope)
  13255. (while (setq file (pop scope))
  13256. (with-current-buffer (org-find-base-buffer-visiting file)
  13257. (save-excursion
  13258. (save-restriction
  13259. (widen)
  13260. (goto-char (point-min))
  13261. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13262. res)))
  13263. ;;;; Properties
  13264. ;;; Setting and retrieving properties
  13265. (defconst org-special-properties
  13266. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13267. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13268. "The special properties valid in Org-mode.
  13269. These are properties that are not defined in the property drawer,
  13270. but in some other way.")
  13271. (defconst org-default-properties
  13272. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13273. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13274. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13275. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13276. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13277. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13278. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13279. "Some properties that are used by Org-mode for various purposes.
  13280. Being in this list makes sure that they are offered for completion.")
  13281. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13282. "Regular expression matching the first line of a property drawer.")
  13283. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13284. "Regular expression matching the last line of a property drawer.")
  13285. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13286. "Regular expression matching the first line of a property drawer.")
  13287. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13288. "Regular expression matching the first line of a property drawer.")
  13289. (defconst org-property-drawer-re
  13290. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13291. org-property-end-re "\\)\n?")
  13292. "Matches an entire property drawer.")
  13293. (defconst org-clock-drawer-re
  13294. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13295. org-property-end-re "\\)\n?")
  13296. "Matches an entire clock drawer.")
  13297. (defsubst org-re-property (property)
  13298. "Return a regexp matching a PROPERTY line.
  13299. Match group 1 will be set to the value."
  13300. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13301. (defsubst org-re-property-keyword (property)
  13302. "Return a regexp matching a PROPERTY line, possibly with no
  13303. value for the property."
  13304. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13305. (defun org-property-action ()
  13306. "Do an action on properties."
  13307. (interactive)
  13308. (let (c)
  13309. (org-at-property-p)
  13310. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13311. (setq c (read-char-exclusive))
  13312. (cond
  13313. ((equal c ?s)
  13314. (call-interactively 'org-set-property))
  13315. ((equal c ?d)
  13316. (call-interactively 'org-delete-property))
  13317. ((equal c ?D)
  13318. (call-interactively 'org-delete-property-globally))
  13319. ((equal c ?c)
  13320. (call-interactively 'org-compute-property-at-point))
  13321. (t (error "No such property action %c" c)))))
  13322. (defun org-inc-effort ()
  13323. "Increment the value of the effort property in the current entry."
  13324. (interactive)
  13325. (org-set-effort nil t))
  13326. (defun org-set-effort (&optional value increment)
  13327. "Set the effort property of the current entry.
  13328. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13329. 10th allowed value.
  13330. When INCREMENT is non-nil, set the property to the next allowed value."
  13331. (interactive "P")
  13332. (if (equal value 0) (setq value 10))
  13333. (let* ((completion-ignore-case t)
  13334. (prop org-effort-property)
  13335. (cur (org-entry-get nil prop))
  13336. (allowed (org-property-get-allowed-values nil prop 'table))
  13337. (existing (mapcar 'list (org-property-values prop)))
  13338. rpl
  13339. (val (cond
  13340. ((stringp value) value)
  13341. ((and allowed (integerp value))
  13342. (or (car (nth (1- value) allowed))
  13343. (car (org-last allowed))))
  13344. ((and allowed increment)
  13345. (or (caadr (member (list cur) allowed))
  13346. (error "Allowed effort values are not set")))
  13347. (allowed
  13348. (message "Select 1-9,0, [RET%s]: %s"
  13349. (if cur (concat "=" cur) "")
  13350. (mapconcat 'car allowed " "))
  13351. (setq rpl (read-char-exclusive))
  13352. (if (equal rpl ?\r)
  13353. cur
  13354. (setq rpl (- rpl ?0))
  13355. (if (equal rpl 0) (setq rpl 10))
  13356. (if (and (> rpl 0) (<= rpl (length allowed)))
  13357. (car (nth (1- rpl) allowed))
  13358. (org-completing-read "Effort: " allowed nil))))
  13359. (t
  13360. (let (org-completion-use-ido org-completion-use-iswitchb)
  13361. (org-completing-read
  13362. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13363. (concat "[" cur "]") "")
  13364. ": ")
  13365. existing nil nil "" nil cur))))))
  13366. (unless (equal (org-entry-get nil prop) val)
  13367. (org-entry-put nil prop val))
  13368. (save-excursion
  13369. (org-back-to-heading t)
  13370. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13371. (message "%s is now %s" prop val)))
  13372. (defun org-at-property-p ()
  13373. "Is cursor inside a property drawer?"
  13374. (save-excursion
  13375. (beginning-of-line 1)
  13376. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13377. (save-match-data ;; Used by calling procedures
  13378. (let ((p (point))
  13379. (range (unless (org-before-first-heading-p)
  13380. (org-get-property-block))))
  13381. (and range (<= (car range) p) (< p (cdr range))))))))
  13382. (defun org-get-property-block (&optional beg end force)
  13383. "Return the (beg . end) range of the body of the property drawer.
  13384. BEG and END are the beginning and end of the current subtree, or of
  13385. the part before the first headline. If they are not given, they will
  13386. be found. If the drawer does not exist and FORCE is non-nil, create
  13387. the drawer."
  13388. (catch 'exit
  13389. (save-excursion
  13390. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13391. (progn (org-back-to-heading t) (point))))
  13392. (end (or end (and (not (outline-next-heading)) (point-max))
  13393. (point))))
  13394. (goto-char beg)
  13395. (if (re-search-forward org-property-start-re end t)
  13396. (setq beg (1+ (match-end 0)))
  13397. (if force
  13398. (save-excursion
  13399. (org-insert-property-drawer)
  13400. (setq end (progn (outline-next-heading) (point))))
  13401. (throw 'exit nil))
  13402. (goto-char beg)
  13403. (if (re-search-forward org-property-start-re end t)
  13404. (setq beg (1+ (match-end 0)))))
  13405. (if (re-search-forward org-property-end-re end t)
  13406. (setq end (match-beginning 0))
  13407. (or force (throw 'exit nil))
  13408. (goto-char beg)
  13409. (setq end beg)
  13410. (org-indent-line)
  13411. (insert ":END:\n"))
  13412. (cons beg end)))))
  13413. (defun org-entry-properties (&optional pom which specific)
  13414. "Get all properties of the entry at point-or-marker POM.
  13415. This includes the TODO keyword, the tags, time strings for deadline,
  13416. scheduled, and clocking, and any additional properties defined in the
  13417. entry. The return value is an alist, keys may occur multiple times
  13418. if the property key was used several times.
  13419. POM may also be nil, in which case the current entry is used.
  13420. If WHICH is nil or `all', get all properties. If WHICH is
  13421. `special' or `standard', only get that subclass. If WHICH
  13422. is a string only get exactly this property. SPECIFIC can be a string, the
  13423. specific property we are interested in. Specifying it can speed
  13424. things up because then unnecessary parsing is avoided."
  13425. (setq which (or which 'all))
  13426. (org-with-point-at pom
  13427. (let ((clockstr (substring org-clock-string 0 -1))
  13428. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13429. (case-fold-search nil)
  13430. beg end range props sum-props key key1 value string clocksum clocksumt)
  13431. (save-excursion
  13432. (when (condition-case nil
  13433. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13434. (error nil))
  13435. (setq beg (point))
  13436. (setq sum-props (get-text-property (point) 'org-summaries))
  13437. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13438. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13439. (outline-next-heading)
  13440. (setq end (point))
  13441. (when (memq which '(all special))
  13442. ;; Get the special properties, like TODO and tags
  13443. (goto-char beg)
  13444. (when (and (or (not specific) (string= specific "TODO"))
  13445. (looking-at org-todo-line-regexp) (match-end 2))
  13446. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13447. (when (and (or (not specific) (string= specific "PRIORITY"))
  13448. (looking-at org-priority-regexp))
  13449. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13450. (when (or (not specific) (string= specific "FILE"))
  13451. (push (cons "FILE" buffer-file-name) props))
  13452. (when (and (or (not specific) (string= specific "TAGS"))
  13453. (setq value (org-get-tags-string))
  13454. (string-match "\\S-" value))
  13455. (push (cons "TAGS" value) props))
  13456. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13457. (setq value (org-get-tags-at)))
  13458. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13459. ":"))
  13460. props))
  13461. (when (or (not specific) (string= specific "BLOCKED"))
  13462. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13463. (when (or (not specific)
  13464. (member specific
  13465. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13466. "TIMESTAMP" "TIMESTAMP_IA")))
  13467. (catch 'match
  13468. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13469. (setq key (if (match-end 1)
  13470. (substring (org-match-string-no-properties 1)
  13471. 0 -1))
  13472. string (if (equal key clockstr)
  13473. (org-trim
  13474. (buffer-substring-no-properties
  13475. (match-beginning 3) (goto-char
  13476. (point-at-eol))))
  13477. (substring (org-match-string-no-properties 3)
  13478. 1 -1)))
  13479. ;; Get the correct property name from the key. This is
  13480. ;; necessary if the user has configured time keywords.
  13481. (setq key1 (concat key ":"))
  13482. (cond
  13483. ((not key)
  13484. (setq key
  13485. (if (= (char-after (match-beginning 3)) ?\[)
  13486. "TIMESTAMP_IA" "TIMESTAMP")))
  13487. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13488. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13489. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13490. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13491. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13492. (progn
  13493. (push (cons key string) props)
  13494. ;; no need to search further if match is found
  13495. (throw 'match t))
  13496. (when (or (equal key "CLOCK") (not (assoc key props)))
  13497. (push (cons key string) props)))))))
  13498. (when (memq which '(all standard))
  13499. ;; Get the standard properties, like :PROP: ...
  13500. (setq range (org-get-property-block beg end))
  13501. (when range
  13502. (goto-char (car range))
  13503. (while (re-search-forward
  13504. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13505. (cdr range) t)
  13506. (setq key (org-match-string-no-properties 1)
  13507. value (org-trim (or (org-match-string-no-properties 2) "")))
  13508. (unless (member key excluded)
  13509. (push (cons key (or value "")) props)))))
  13510. (if clocksum
  13511. (push (cons "CLOCKSUM"
  13512. (org-columns-number-to-string (/ (float clocksum) 60.)
  13513. 'add_times))
  13514. props))
  13515. (if clocksumt
  13516. (push (cons "CLOCKSUM_T"
  13517. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13518. 'add_times))
  13519. props))
  13520. (unless (assoc "CATEGORY" props)
  13521. (push (cons "CATEGORY" (org-get-category)) props))
  13522. (append sum-props (nreverse props)))))))
  13523. (defun org-entry-get (pom property &optional inherit literal-nil)
  13524. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13525. If INHERIT is non-nil and the entry does not have the property,
  13526. then also check higher levels of the hierarchy.
  13527. If INHERIT is the symbol `selective', use inheritance only if the setting
  13528. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13529. If the property is present but empty, the return value is the empty string.
  13530. If the property is not present at all, nil is returned.
  13531. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13532. do not interpret it as the list atom nil. This is used for inheritance
  13533. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13534. (org-with-point-at pom
  13535. (if (and inherit (if (eq inherit 'selective)
  13536. (org-property-inherit-p property)
  13537. t))
  13538. (org-entry-get-with-inheritance property literal-nil)
  13539. (if (member property org-special-properties)
  13540. ;; We need a special property. Use `org-entry-properties' to
  13541. ;; retrieve it, but specify the wanted property
  13542. (cdr (assoc property (org-entry-properties nil 'special property)))
  13543. (let ((range (org-get-property-block)))
  13544. (when (and range (not (eq (car range) (cdr range))))
  13545. (let* ((props (list (or (assoc property org-file-properties)
  13546. (assoc property org-global-properties)
  13547. (assoc property org-global-properties-fixed))))
  13548. (ap (lambda (key)
  13549. (when (re-search-forward
  13550. (org-re-property key) (cdr range) t)
  13551. (setq props
  13552. (org-update-property-plist
  13553. key
  13554. (if (match-end 1)
  13555. (org-match-string-no-properties 1) "")
  13556. props)))))
  13557. val)
  13558. (goto-char (car range))
  13559. (funcall ap property)
  13560. (goto-char (car range))
  13561. (while (funcall ap (concat property "+")))
  13562. (setq val (cdr (assoc property props)))
  13563. (when val (if literal-nil val (org-not-nil val))))))))))
  13564. (defun org-property-or-variable-value (var &optional inherit)
  13565. "Check if there is a property fixing the value of VAR.
  13566. If yes, return this value. If not, return the current value of the variable."
  13567. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13568. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13569. (read prop)
  13570. (symbol-value var))))
  13571. (defun org-entry-delete (pom property)
  13572. "Delete the property PROPERTY from entry at point-or-marker POM."
  13573. (org-with-point-at pom
  13574. (if (member property org-special-properties)
  13575. nil ; cannot delete these properties.
  13576. (let ((range (org-get-property-block)))
  13577. (if (and range
  13578. (goto-char (car range))
  13579. (re-search-forward
  13580. (org-re-property property)
  13581. (cdr range) t))
  13582. (progn
  13583. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13584. t)
  13585. nil)))))
  13586. ;; Multi-values properties are properties that contain multiple values
  13587. ;; These values are assumed to be single words, separated by whitespace.
  13588. (defun org-entry-add-to-multivalued-property (pom property value)
  13589. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13590. (let* ((old (org-entry-get pom property))
  13591. (values (and old (org-split-string old "[ \t]"))))
  13592. (setq value (org-entry-protect-space value))
  13593. (unless (member value values)
  13594. (setq values (cons value values))
  13595. (org-entry-put pom property
  13596. (mapconcat 'identity values " ")))))
  13597. (defun org-entry-remove-from-multivalued-property (pom property value)
  13598. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13599. (let* ((old (org-entry-get pom property))
  13600. (values (and old (org-split-string old "[ \t]"))))
  13601. (setq value (org-entry-protect-space value))
  13602. (when (member value values)
  13603. (setq values (delete value values))
  13604. (org-entry-put pom property
  13605. (mapconcat 'identity values " ")))))
  13606. (defun org-entry-member-in-multivalued-property (pom property value)
  13607. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13608. (let* ((old (org-entry-get pom property))
  13609. (values (and old (org-split-string old "[ \t]"))))
  13610. (setq value (org-entry-protect-space value))
  13611. (member value values)))
  13612. (defun org-entry-get-multivalued-property (pom property)
  13613. "Return a list of values in a multivalued property."
  13614. (let* ((value (org-entry-get pom property))
  13615. (values (and value (org-split-string value "[ \t]"))))
  13616. (mapcar 'org-entry-restore-space values)))
  13617. (defun org-entry-put-multivalued-property (pom property &rest values)
  13618. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13619. VALUES should be a list of strings. Spaces will be protected."
  13620. (org-entry-put pom property
  13621. (mapconcat 'org-entry-protect-space values " "))
  13622. (let* ((value (org-entry-get pom property))
  13623. (values (and value (org-split-string value "[ \t]"))))
  13624. (mapcar 'org-entry-restore-space values)))
  13625. (defun org-entry-protect-space (s)
  13626. "Protect spaces and newline in string S."
  13627. (while (string-match " " s)
  13628. (setq s (replace-match "%20" t t s)))
  13629. (while (string-match "\n" s)
  13630. (setq s (replace-match "%0A" t t s)))
  13631. s)
  13632. (defun org-entry-restore-space (s)
  13633. "Restore spaces and newline in string S."
  13634. (while (string-match "%20" s)
  13635. (setq s (replace-match " " t t s)))
  13636. (while (string-match "%0A" s)
  13637. (setq s (replace-match "\n" t t s)))
  13638. s)
  13639. (defvar org-entry-property-inherited-from (make-marker)
  13640. "Marker pointing to the entry from where a property was inherited.
  13641. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13642. location of the entry where the inheritance search matched. If there was
  13643. no match, the marker will point nowhere.
  13644. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13645. is set.")
  13646. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13647. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13648. The search will stop at the first ancestor which has the property defined.
  13649. If the value found is \"nil\", return nil to show that the property
  13650. should be considered as undefined (this is the meaning of nil here).
  13651. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13652. (move-marker org-entry-property-inherited-from nil)
  13653. (let (tmp)
  13654. (save-excursion
  13655. (save-restriction
  13656. (widen)
  13657. (catch 'ex
  13658. (while t
  13659. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13660. (or (ignore-errors (org-back-to-heading t))
  13661. (goto-char (point-min)))
  13662. (move-marker org-entry-property-inherited-from (point))
  13663. (throw 'ex tmp))
  13664. (or (ignore-errors (org-up-heading-safe))
  13665. (throw 'ex nil))))))
  13666. (setq tmp (or tmp
  13667. (cdr (assoc property org-file-properties))
  13668. (cdr (assoc property org-global-properties))
  13669. (cdr (assoc property org-global-properties-fixed))))
  13670. (if literal-nil tmp (org-not-nil tmp))))
  13671. (defvar org-property-changed-functions nil
  13672. "Hook called when the value of a property has changed.
  13673. Each hook function should accept two arguments, the name of the property
  13674. and the new value.")
  13675. (defun org-entry-put (pom property value)
  13676. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13677. (org-with-point-at pom
  13678. (org-back-to-heading t)
  13679. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13680. range)
  13681. (cond
  13682. ((equal property "TODO")
  13683. (when (and (stringp value) (string-match "\\S-" value)
  13684. (not (member value org-todo-keywords-1)))
  13685. (error "\"%s\" is not a valid TODO state" value))
  13686. (if (or (not value)
  13687. (not (string-match "\\S-" value)))
  13688. (setq value 'none))
  13689. (org-todo value)
  13690. (org-set-tags nil 'align))
  13691. ((equal property "PRIORITY")
  13692. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13693. (string-to-char value) ?\ ))
  13694. (org-set-tags nil 'align))
  13695. ((equal property "CLOCKSUM")
  13696. (if (not (re-search-forward
  13697. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13698. (error "Cannot find a clock log")
  13699. (goto-char (- (match-end 1) 2))
  13700. (cond
  13701. ((eq value 'earlier) (org-timestamp-down))
  13702. ((eq value 'later) (org-timestamp-up)))
  13703. (org-clock-sum-current-item)))
  13704. ((equal property "SCHEDULED")
  13705. (if (re-search-forward org-scheduled-time-regexp end t)
  13706. (cond
  13707. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13708. ((eq value 'later) (org-timestamp-change 1 'day))
  13709. (t (call-interactively 'org-schedule)))
  13710. (call-interactively 'org-schedule)))
  13711. ((equal property "DEADLINE")
  13712. (if (re-search-forward org-deadline-time-regexp end t)
  13713. (cond
  13714. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13715. ((eq value 'later) (org-timestamp-change 1 'day))
  13716. (t (call-interactively 'org-deadline)))
  13717. (call-interactively 'org-deadline)))
  13718. ((member property org-special-properties)
  13719. (error "The %s property can not yet be set with `org-entry-put'"
  13720. property))
  13721. (t ; a non-special property
  13722. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13723. (setq range (org-get-property-block beg end 'force))
  13724. (goto-char (car range))
  13725. (if (re-search-forward
  13726. (org-re-property-keyword property) (cdr range) t)
  13727. (progn
  13728. (delete-region (match-beginning 0) (match-end 0))
  13729. (goto-char (match-beginning 0)))
  13730. (goto-char (cdr range))
  13731. (insert "\n")
  13732. (backward-char 1)
  13733. (org-indent-line))
  13734. (insert ":" property ":")
  13735. (and value (insert " " value))
  13736. (org-indent-line)))))
  13737. (run-hook-with-args 'org-property-changed-functions property value)))
  13738. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13739. "Get all property keys in the current buffer.
  13740. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13741. like tags and TODO state.
  13742. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13743. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13744. and others.
  13745. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13746. formats in the current buffer."
  13747. (let (rtn range cfmt s p)
  13748. (save-excursion
  13749. (save-restriction
  13750. (widen)
  13751. (goto-char (point-min))
  13752. (while (re-search-forward org-property-start-re nil t)
  13753. (setq range (org-get-property-block))
  13754. (goto-char (car range))
  13755. (while (re-search-forward
  13756. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13757. (cdr range) t)
  13758. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13759. (outline-next-heading))))
  13760. (when include-specials
  13761. (setq rtn (append org-special-properties rtn)))
  13762. (when include-defaults
  13763. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13764. (add-to-list 'rtn org-effort-property))
  13765. (when include-columns
  13766. (save-excursion
  13767. (save-restriction
  13768. (widen)
  13769. (goto-char (point-min))
  13770. (while (re-search-forward
  13771. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13772. nil t)
  13773. (setq cfmt (match-string 2) s 0)
  13774. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13775. cfmt s)
  13776. (setq s (match-end 0)
  13777. p (match-string 1 cfmt))
  13778. (unless (or (equal p "ITEM")
  13779. (member p org-special-properties))
  13780. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13781. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13782. (defun org-property-values (key)
  13783. "Return a list of all values of property KEY in the current buffer."
  13784. (save-excursion
  13785. (save-restriction
  13786. (widen)
  13787. (goto-char (point-min))
  13788. (let ((re (org-re-property key))
  13789. values)
  13790. (while (re-search-forward re nil t)
  13791. (add-to-list 'values (org-trim (match-string 1))))
  13792. (delete "" values)))))
  13793. (defun org-insert-property-drawer ()
  13794. "Insert a property drawer into the current entry."
  13795. (org-back-to-heading t)
  13796. (looking-at org-outline-regexp)
  13797. (let ((indent (if org-adapt-indentation
  13798. (- (match-end 0) (match-beginning 0))
  13799. 0))
  13800. (beg (point))
  13801. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13802. end hiddenp)
  13803. (outline-next-heading)
  13804. (setq end (point))
  13805. (goto-char beg)
  13806. (while (re-search-forward re end t))
  13807. (setq hiddenp (outline-invisible-p))
  13808. (end-of-line 1)
  13809. (and (equal (char-after) ?\n) (forward-char 1))
  13810. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13811. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13812. ;; just skip this line
  13813. (beginning-of-line 2)
  13814. ;; Drawer start, find the end
  13815. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13816. (beginning-of-line 1)))
  13817. (org-skip-over-state-notes)
  13818. (skip-chars-backward " \t\n\r")
  13819. (if (eq (char-before) ?*) (forward-char 1))
  13820. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13821. (beginning-of-line 0)
  13822. (org-indent-to-column indent)
  13823. (beginning-of-line 2)
  13824. (org-indent-to-column indent)
  13825. (beginning-of-line 0)
  13826. (if hiddenp
  13827. (save-excursion
  13828. (org-back-to-heading t)
  13829. (hide-entry))
  13830. (org-flag-drawer t))))
  13831. (defun org-insert-drawer (&optional arg drawer)
  13832. "Insert a drawer at point.
  13833. Optional argument DRAWER, when non-nil, is a string representing
  13834. drawer's name. Otherwise, the user is prompted for a name.
  13835. If a region is active, insert the drawer around that region
  13836. instead.
  13837. Point is left between drawer's boundaries."
  13838. (interactive "P")
  13839. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13840. "LOGBOOK"))
  13841. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13842. ;; internally by Org. They are meant to be inserted
  13843. ;; automatically.
  13844. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13845. ;; Remove system drawers from list. Note: For some reason,
  13846. ;; `org-completing-read' ignores the predicate while
  13847. ;; `completing-read' handles it fine.
  13848. (drawer (if arg "PROPERTIES"
  13849. (or drawer
  13850. (completing-read
  13851. "Drawer: " org-drawers
  13852. (lambda (d) (not (member d system-drawers))))))))
  13853. (cond
  13854. ;; With C-u, fall back on `org-insert-property-drawer'
  13855. (arg (org-insert-property-drawer))
  13856. ;; With an active region, insert a drawer at point.
  13857. ((not (org-region-active-p))
  13858. (progn
  13859. (unless (bolp) (insert "\n"))
  13860. (insert (format ":%s:\n\n:END:\n" drawer))
  13861. (forward-line -2)))
  13862. ;; Otherwise, insert the drawer at point
  13863. (t
  13864. (let ((rbeg (region-beginning))
  13865. (rend (copy-marker (region-end))))
  13866. (unwind-protect
  13867. (progn
  13868. (goto-char rbeg)
  13869. (beginning-of-line)
  13870. (when (save-excursion
  13871. (re-search-forward org-outline-regexp-bol rend t))
  13872. (error "Drawers cannot contain headlines"))
  13873. ;; Position point at the beginning of the first
  13874. ;; non-blank line in region. Insert drawer's opening
  13875. ;; there, then indent it.
  13876. (org-skip-whitespace)
  13877. (beginning-of-line)
  13878. (insert ":" drawer ":\n")
  13879. (forward-line -1)
  13880. (indent-for-tab-command)
  13881. ;; Move point to the beginning of the first blank line
  13882. ;; after the last non-blank line in region. Insert
  13883. ;; drawer's closing, then indent it.
  13884. (goto-char rend)
  13885. (skip-chars-backward " \r\t\n")
  13886. (insert "\n:END:")
  13887. (deactivate-mark t)
  13888. (indent-for-tab-command)
  13889. (unless (eolp) (insert "\n")))
  13890. ;; Clear marker, whatever the outcome of insertion is.
  13891. (set-marker rend nil)))))))
  13892. (defvar org-property-set-functions-alist nil
  13893. "Property set function alist.
  13894. Each entry should have the following format:
  13895. (PROPERTY . READ-FUNCTION)
  13896. The read function will be called with the same argument as
  13897. `org-completing-read'.")
  13898. (defun org-set-property-function (property)
  13899. "Get the function that should be used to set PROPERTY.
  13900. This is computed according to `org-property-set-functions-alist'."
  13901. (or (cdr (assoc property org-property-set-functions-alist))
  13902. 'org-completing-read))
  13903. (defun org-read-property-value (property)
  13904. "Read PROPERTY value from user."
  13905. (let* ((completion-ignore-case t)
  13906. (allowed (org-property-get-allowed-values nil property 'table))
  13907. (cur (org-entry-get nil property))
  13908. (prompt (concat property " value"
  13909. (if (and cur (string-match "\\S-" cur))
  13910. (concat " [" cur "]") "") ": "))
  13911. (set-function (org-set-property-function property))
  13912. (val (if allowed
  13913. (funcall set-function prompt allowed nil
  13914. (not (get-text-property 0 'org-unrestricted
  13915. (caar allowed))))
  13916. (let (org-completion-use-ido org-completion-use-iswitchb)
  13917. (funcall set-function prompt
  13918. (mapcar 'list (org-property-values property))
  13919. nil nil "" nil cur)))))
  13920. (if (equal val "")
  13921. cur
  13922. val)))
  13923. (defvar org-last-set-property nil)
  13924. (defvar org-last-set-property-value nil)
  13925. (defun org-read-property-name ()
  13926. "Read a property name."
  13927. (let* ((completion-ignore-case t)
  13928. (keys (org-buffer-property-keys nil t t))
  13929. (default-prop (or (save-excursion
  13930. (save-match-data
  13931. (beginning-of-line)
  13932. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13933. (null (string= (match-string 1) "END"))
  13934. (match-string 1))))
  13935. org-last-set-property))
  13936. (property (org-icompleting-read
  13937. (concat "Property"
  13938. (if default-prop (concat " [" default-prop "]") "")
  13939. ": ")
  13940. (mapcar 'list keys)
  13941. nil nil nil nil
  13942. default-prop)))
  13943. (if (member property keys)
  13944. property
  13945. (or (cdr (assoc (downcase property)
  13946. (mapcar (lambda (x) (cons (downcase x) x))
  13947. keys)))
  13948. property))))
  13949. (defun org-set-property-and-value (use-last)
  13950. "Allow to set [PROPERTY]: [value] direction from prompt.
  13951. When use-default, don't even ask, just use the last
  13952. \"[PROPERTY]: [value]\" string from the history."
  13953. (interactive "P")
  13954. (let* ((completion-ignore-case t)
  13955. (pv (or (and use-last org-last-set-property-value)
  13956. (org-completing-read
  13957. "Enter a \"[Property]: [value]\" pair: "
  13958. nil nil nil nil nil
  13959. org-last-set-property-value)))
  13960. prop val)
  13961. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13962. (setq prop (match-string 1 pv)
  13963. val (match-string 2 pv))
  13964. (org-set-property prop val))))
  13965. (defun org-set-property (property value)
  13966. "In the current entry, set PROPERTY to VALUE.
  13967. When called interactively, this will prompt for a property name, offering
  13968. completion on existing and default properties. And then it will prompt
  13969. for a value, offering completion either on allowed values (via an inherited
  13970. xxx_ALL property) or on existing values in other instances of this property
  13971. in the current file."
  13972. (interactive (list nil nil))
  13973. (let* ((property (or property (org-read-property-name)))
  13974. (value (or value (org-read-property-value property)))
  13975. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13976. (setq org-last-set-property property)
  13977. (setq org-last-set-property-value (concat property ": " value))
  13978. ;; Possibly postprocess the inserted value:
  13979. (when fn (setq value (funcall fn value)))
  13980. (unless (equal (org-entry-get nil property) value)
  13981. (org-entry-put nil property value))))
  13982. (defun org-delete-property (property)
  13983. "In the current entry, delete PROPERTY."
  13984. (interactive
  13985. (let* ((completion-ignore-case t)
  13986. (prop (org-icompleting-read "Property: "
  13987. (org-entry-properties nil 'standard))))
  13988. (list prop)))
  13989. (message "Property %s %s" property
  13990. (if (org-entry-delete nil property)
  13991. "deleted"
  13992. "was not present in the entry")))
  13993. (defun org-delete-property-globally (property)
  13994. "Remove PROPERTY globally, from all entries."
  13995. (interactive
  13996. (let* ((completion-ignore-case t)
  13997. (prop (org-icompleting-read
  13998. "Globally remove property: "
  13999. (mapcar 'list (org-buffer-property-keys)))))
  14000. (list prop)))
  14001. (save-excursion
  14002. (save-restriction
  14003. (widen)
  14004. (goto-char (point-min))
  14005. (let ((cnt 0))
  14006. (while (re-search-forward
  14007. (org-re-property property)
  14008. nil t)
  14009. (setq cnt (1+ cnt))
  14010. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14011. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14012. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14013. (defun org-compute-property-at-point ()
  14014. "Compute the property at point.
  14015. This looks for an enclosing column format, extracts the operator and
  14016. then applies it to the property in the column format's scope."
  14017. (interactive)
  14018. (unless (org-at-property-p)
  14019. (error "Not at a property"))
  14020. (let ((prop (org-match-string-no-properties 2)))
  14021. (org-columns-get-format-and-top-level)
  14022. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14023. (error "No operator defined for property %s" prop))
  14024. (org-columns-compute prop)))
  14025. (defvar org-property-allowed-value-functions nil
  14026. "Hook for functions supplying allowed values for a specific property.
  14027. The functions must take a single argument, the name of the property, and
  14028. return a flat list of allowed values. If \":ETC\" is one of
  14029. the values, this means that these values are intended as defaults for
  14030. completion, but that other values should be allowed too.
  14031. The functions must return nil if they are not responsible for this
  14032. property.")
  14033. (defun org-property-get-allowed-values (pom property &optional table)
  14034. "Get allowed values for the property PROPERTY.
  14035. When TABLE is non-nil, return an alist that can directly be used for
  14036. completion."
  14037. (let (vals)
  14038. (cond
  14039. ((equal property "TODO")
  14040. (setq vals (org-with-point-at pom
  14041. (append org-todo-keywords-1 '("")))))
  14042. ((equal property "PRIORITY")
  14043. (let ((n org-lowest-priority))
  14044. (while (>= n org-highest-priority)
  14045. (push (char-to-string n) vals)
  14046. (setq n (1- n)))))
  14047. ((member property org-special-properties))
  14048. ((setq vals (run-hook-with-args-until-success
  14049. 'org-property-allowed-value-functions property)))
  14050. (t
  14051. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14052. (when (and vals (string-match "\\S-" vals))
  14053. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14054. (setq vals (mapcar (lambda (x)
  14055. (cond ((stringp x) x)
  14056. ((numberp x) (number-to-string x))
  14057. ((symbolp x) (symbol-name x))
  14058. (t "???")))
  14059. vals)))))
  14060. (when (member ":ETC" vals)
  14061. (setq vals (remove ":ETC" vals))
  14062. (org-add-props (car vals) '(org-unrestricted t)))
  14063. (if table (mapcar 'list vals) vals)))
  14064. (defun org-property-previous-allowed-value (&optional previous)
  14065. "Switch to the next allowed value for this property."
  14066. (interactive)
  14067. (org-property-next-allowed-value t))
  14068. (defun org-property-next-allowed-value (&optional previous)
  14069. "Switch to the next allowed value for this property."
  14070. (interactive)
  14071. (unless (org-at-property-p)
  14072. (error "Not at a property"))
  14073. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14074. (key (match-string 2))
  14075. (value (match-string 3))
  14076. (allowed (or (org-property-get-allowed-values (point) key)
  14077. (and (member value '("[ ]" "[-]" "[X]"))
  14078. '("[ ]" "[X]"))))
  14079. nval)
  14080. (unless allowed
  14081. (error "Allowed values for this property have not been defined"))
  14082. (if previous (setq allowed (reverse allowed)))
  14083. (if (member value allowed)
  14084. (setq nval (car (cdr (member value allowed)))))
  14085. (setq nval (or nval (car allowed)))
  14086. (if (equal nval value)
  14087. (error "Only one allowed value for this property"))
  14088. (org-at-property-p)
  14089. (replace-match (concat " :" key ": " nval) t t)
  14090. (org-indent-line)
  14091. (beginning-of-line 1)
  14092. (skip-chars-forward " \t")
  14093. (when (equal prop org-effort-property)
  14094. (save-excursion
  14095. (org-back-to-heading t)
  14096. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  14097. (run-hook-with-args 'org-property-changed-functions key nval)))
  14098. (defun org-find-olp (path &optional this-buffer)
  14099. "Return a marker pointing to the entry at outline path OLP.
  14100. If anything goes wrong, throw an error.
  14101. You can wrap this call to catch the error like this:
  14102. (condition-case msg
  14103. (org-mobile-locate-entry (match-string 4))
  14104. (error (nth 1 msg)))
  14105. The return value will then be either a string with the error message,
  14106. or a marker if everything is OK.
  14107. If THIS-BUFFER is set, the outline path does not contain a file,
  14108. only headings."
  14109. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14110. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14111. (level 1)
  14112. (lmin 1)
  14113. (lmax 1)
  14114. limit re end found pos heading cnt flevel)
  14115. (unless buffer (error "File not found :%s" file))
  14116. (with-current-buffer buffer
  14117. (save-excursion
  14118. (save-restriction
  14119. (widen)
  14120. (setq limit (point-max))
  14121. (goto-char (point-min))
  14122. (while (setq heading (pop path))
  14123. (setq re (format org-complex-heading-regexp-format
  14124. (regexp-quote heading)))
  14125. (setq cnt 0 pos (point))
  14126. (while (re-search-forward re end t)
  14127. (setq level (- (match-end 1) (match-beginning 1)))
  14128. (if (and (>= level lmin) (<= level lmax))
  14129. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14130. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14131. lmax heading))
  14132. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14133. lmax heading))
  14134. (goto-char found)
  14135. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14136. (setq end (save-excursion (org-end-of-subtree t t))))
  14137. (when (org-at-heading-p)
  14138. (point-marker)))))))
  14139. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14140. "Find node HEADING in BUFFER.
  14141. Return a marker to the heading if it was found, or nil if not.
  14142. If POS-ONLY is set, return just the position instead of a marker.
  14143. The heading text must match exact, but it may have a TODO keyword,
  14144. a priority cookie and tags in the standard locations."
  14145. (with-current-buffer (or buffer (current-buffer))
  14146. (save-excursion
  14147. (save-restriction
  14148. (widen)
  14149. (goto-char (point-min))
  14150. (let (case-fold-search)
  14151. (if (re-search-forward
  14152. (format org-complex-heading-regexp-format
  14153. (regexp-quote heading)) nil t)
  14154. (if pos-only
  14155. (match-beginning 0)
  14156. (move-marker (make-marker) (match-beginning 0)))))))))
  14157. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14158. "Find Org node headline HEADING in all .org files in directory DIR.
  14159. When the target headline is found, return a marker to this location."
  14160. (let ((files (directory-files (or dir default-directory)
  14161. nil "\\`[^.#].*\\.org\\'"))
  14162. file visiting m buffer)
  14163. (catch 'found
  14164. (while (setq file (pop files))
  14165. (message "trying %s" file)
  14166. (setq visiting (org-find-base-buffer-visiting file))
  14167. (setq buffer (or visiting (find-file-noselect file)))
  14168. (setq m (org-find-exact-headline-in-buffer
  14169. heading buffer))
  14170. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14171. (and m (throw 'found m))))))
  14172. (defun org-find-entry-with-id (ident)
  14173. "Locate the entry that contains the ID property with exact value IDENT.
  14174. IDENT can be a string, a symbol or a number, this function will search for
  14175. the string representation of it.
  14176. Return the position where this entry starts, or nil if there is no such entry."
  14177. (interactive "sID: ")
  14178. (let ((id (cond
  14179. ((stringp ident) ident)
  14180. ((symbol-name ident) (symbol-name ident))
  14181. ((numberp ident) (number-to-string ident))
  14182. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14183. (case-fold-search nil))
  14184. (save-excursion
  14185. (save-restriction
  14186. (widen)
  14187. (goto-char (point-min))
  14188. (when (re-search-forward
  14189. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14190. nil t)
  14191. (org-back-to-heading t)
  14192. (point))))))
  14193. ;;;; Timestamps
  14194. (defvar org-last-changed-timestamp nil)
  14195. (defvar org-last-inserted-timestamp nil
  14196. "The last time stamp inserted with `org-insert-time-stamp'.")
  14197. (defvar org-time-was-given) ; dynamically scoped parameter
  14198. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14199. (defvar org-ts-what) ; dynamically scoped parameter
  14200. (defun org-time-stamp (arg &optional inactive)
  14201. "Prompt for a date/time and insert a time stamp.
  14202. If the user specifies a time like HH:MM or if this command is
  14203. called with at least one prefix argument, the time stamp contains
  14204. the date and the time. Otherwise, only the date is be included.
  14205. All parts of a date not specified by the user is filled in from
  14206. the current date/time. So if you just press return without
  14207. typing anything, the time stamp will represent the current
  14208. date/time.
  14209. If there is already a timestamp at the cursor, it will be
  14210. modified.
  14211. With two universal prefix arguments, insert an active timestamp
  14212. with the current time without prompting the user."
  14213. (interactive "P")
  14214. (let* ((ts nil)
  14215. (default-time
  14216. ;; Default time is either today, or, when entering a range,
  14217. ;; the range start.
  14218. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14219. (save-excursion
  14220. (re-search-backward
  14221. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14222. (- (point) 20) t)))
  14223. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14224. (current-time)))
  14225. (default-input (and ts (org-get-compact-tod ts)))
  14226. (repeater (save-excursion
  14227. (save-match-data
  14228. (beginning-of-line)
  14229. (when (re-search-forward
  14230. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14231. (save-excursion (progn (end-of-line) (point))) t)
  14232. (match-string 0)))))
  14233. org-time-was-given org-end-time-was-given time)
  14234. (cond
  14235. ((and (org-at-timestamp-p t)
  14236. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14237. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14238. (insert "--")
  14239. (setq time (let ((this-command this-command))
  14240. (org-read-date arg 'totime nil nil
  14241. default-time default-input inactive)))
  14242. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14243. ((org-at-timestamp-p t)
  14244. (setq time (let ((this-command this-command))
  14245. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14246. (when (org-at-timestamp-p t) ; just to get the match data
  14247. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14248. (replace-match "")
  14249. (setq org-last-changed-timestamp
  14250. (org-insert-time-stamp
  14251. time (or org-time-was-given arg)
  14252. inactive nil nil (list org-end-time-was-given)))
  14253. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14254. (setq org-last-changed-timestamp
  14255. (concat (substring org-last-inserted-timestamp 0 -1)
  14256. " " repeater ">"))))
  14257. (message "Timestamp updated"))
  14258. ((equal arg '(16))
  14259. (org-insert-time-stamp (current-time) t))
  14260. (t
  14261. (setq time (let ((this-command this-command))
  14262. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14263. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14264. nil nil (list org-end-time-was-given))))))
  14265. ;; FIXME: can we use this for something else, like computing time differences?
  14266. (defun org-get-compact-tod (s)
  14267. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14268. (let* ((t1 (match-string 1 s))
  14269. (h1 (string-to-number (match-string 2 s)))
  14270. (m1 (string-to-number (match-string 3 s)))
  14271. (t2 (and (match-end 4) (match-string 5 s)))
  14272. (h2 (and t2 (string-to-number (match-string 6 s))))
  14273. (m2 (and t2 (string-to-number (match-string 7 s))))
  14274. dh dm)
  14275. (if (not t2)
  14276. t1
  14277. (setq dh (- h2 h1) dm (- m2 m1))
  14278. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14279. (concat t1 "+" (number-to-string dh)
  14280. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14281. (defun org-time-stamp-inactive (&optional arg)
  14282. "Insert an inactive time stamp.
  14283. An inactive time stamp is enclosed in square brackets instead of angle
  14284. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14285. does not link to the calendar and cannot be changed with the S-cursor keys.
  14286. So these are more for recording a certain time/date."
  14287. (interactive "P")
  14288. (org-time-stamp arg 'inactive))
  14289. (defvar org-date-ovl (make-overlay 1 1))
  14290. (overlay-put org-date-ovl 'face 'org-date-selected)
  14291. (org-detach-overlay org-date-ovl)
  14292. (defvar org-ans1) ; dynamically scoped parameter
  14293. (defvar org-ans2) ; dynamically scoped parameter
  14294. (defvar org-plain-time-of-day-regexp) ; defined below
  14295. (defvar org-overriding-default-time nil) ; dynamically scoped
  14296. (defvar org-read-date-overlay nil)
  14297. (defvar org-dcst nil) ; dynamically scoped
  14298. (defvar org-read-date-history nil)
  14299. (defvar org-read-date-final-answer nil)
  14300. (defvar org-read-date-analyze-futurep nil)
  14301. (defvar org-read-date-analyze-forced-year nil)
  14302. (defvar org-read-date-inactive)
  14303. (defvar org-read-date-minibuffer-local-map
  14304. (let ((map (make-sparse-keymap)))
  14305. (set-keymap-parent map minibuffer-local-map)
  14306. (org-defkey map (kbd ".")
  14307. (lambda () (interactive)
  14308. (org-eval-in-calendar '(calendar-goto-today))))
  14309. (org-defkey map [(meta shift left)]
  14310. (lambda () (interactive)
  14311. (org-eval-in-calendar '(calendar-backward-month 1))))
  14312. (org-defkey map [(meta shift right)]
  14313. (lambda () (interactive)
  14314. (org-eval-in-calendar '(calendar-forward-month 1))))
  14315. (org-defkey map [(meta shift up)]
  14316. (lambda () (interactive)
  14317. (org-eval-in-calendar '(calendar-backward-year 1))))
  14318. (org-defkey map [(meta shift down)]
  14319. (lambda () (interactive)
  14320. (org-eval-in-calendar '(calendar-forward-year 1))))
  14321. (org-defkey map [?\e (shift left)]
  14322. (lambda () (interactive)
  14323. (org-eval-in-calendar '(calendar-backward-month 1))))
  14324. (org-defkey map [?\e (shift right)]
  14325. (lambda () (interactive)
  14326. (org-eval-in-calendar '(calendar-forward-month 1))))
  14327. (org-defkey map [?\e (shift up)]
  14328. (lambda () (interactive)
  14329. (org-eval-in-calendar '(calendar-backward-year 1))))
  14330. (org-defkey map [?\e (shift down)]
  14331. (lambda () (interactive)
  14332. (org-eval-in-calendar '(calendar-forward-year 1))))
  14333. (org-defkey map [(shift up)]
  14334. (lambda () (interactive)
  14335. (org-eval-in-calendar '(calendar-backward-week 1))))
  14336. (org-defkey map [(shift down)]
  14337. (lambda () (interactive)
  14338. (org-eval-in-calendar '(calendar-forward-week 1))))
  14339. (org-defkey map [(shift left)]
  14340. (lambda () (interactive)
  14341. (org-eval-in-calendar '(calendar-backward-day 1))))
  14342. (org-defkey map [(shift right)]
  14343. (lambda () (interactive)
  14344. (org-eval-in-calendar '(calendar-forward-day 1))))
  14345. (org-defkey map "!"
  14346. (lambda () (interactive)
  14347. (org-eval-in-calendar '(diary-view-entries))
  14348. (message "")))
  14349. (org-defkey map ">"
  14350. (lambda () (interactive)
  14351. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14352. (org-defkey map "<"
  14353. (lambda () (interactive)
  14354. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14355. (org-defkey map "\C-v"
  14356. (lambda () (interactive)
  14357. (org-eval-in-calendar
  14358. '(calendar-scroll-left-three-months 1))))
  14359. (org-defkey map "\M-v"
  14360. (lambda () (interactive)
  14361. (org-eval-in-calendar
  14362. '(calendar-scroll-right-three-months 1))))
  14363. map)
  14364. "Keymap for minibuffer commands when using `org-read-date'.")
  14365. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14366. default-time default-input inactive)
  14367. "Read a date, possibly a time, and make things smooth for the user.
  14368. The prompt will suggest to enter an ISO date, but you can also enter anything
  14369. which will at least partially be understood by `parse-time-string'.
  14370. Unrecognized parts of the date will default to the current day, month, year,
  14371. hour and minute. If this command is called to replace a timestamp at point,
  14372. or to enter the second timestamp of a range, the default time is taken
  14373. from the existing stamp. Furthermore, the command prefers the future,
  14374. so if you are giving a date where the year is not given, and the day-month
  14375. combination is already past in the current year, it will assume you
  14376. mean next year. For details, see the manual. A few examples:
  14377. 3-2-5 --> 2003-02-05
  14378. feb 15 --> currentyear-02-15
  14379. 2/15 --> currentyear-02-15
  14380. sep 12 9 --> 2009-09-12
  14381. 12:45 --> today 12:45
  14382. 22 sept 0:34 --> currentyear-09-22 0:34
  14383. 12 --> currentyear-currentmonth-12
  14384. Fri --> nearest Friday (today or later)
  14385. etc.
  14386. Furthermore you can specify a relative date by giving, as the *first* thing
  14387. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14388. change in days weeks, months, years.
  14389. With a single plus or minus, the date is relative to today. With a double
  14390. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14391. +4d --> four days from today
  14392. +4 --> same as above
  14393. +2w --> two weeks from today
  14394. ++5 --> five days from default date
  14395. The function understands only English month and weekday abbreviations.
  14396. While prompting, a calendar is popped up - you can also select the
  14397. date with the mouse (button 1). The calendar shows a period of three
  14398. months. To scroll it to other months, use the keys `>' and `<'.
  14399. If you don't like the calendar, turn it off with
  14400. \(setq org-read-date-popup-calendar nil)
  14401. With optional argument TO-TIME, the date will immediately be converted
  14402. to an internal time.
  14403. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14404. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14405. still enter a time, and this function will inform the calling routine
  14406. about this change. The calling routine may then choose to change the
  14407. format used to insert the time stamp into the buffer to include the time.
  14408. With optional argument FROM-STRING, read from this string instead from
  14409. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14410. the time/date that is used for everything that is not specified by the
  14411. user."
  14412. (require 'parse-time)
  14413. (let* ((org-time-stamp-rounding-minutes
  14414. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14415. (org-dcst org-display-custom-times)
  14416. (ct (org-current-time))
  14417. (org-def (or org-overriding-default-time default-time ct))
  14418. (org-defdecode (decode-time org-def))
  14419. (dummy (progn
  14420. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14421. (setcar (nthcdr 2 org-defdecode) -1)
  14422. (setcar (nthcdr 1 org-defdecode) 59)
  14423. (setq org-def (apply 'encode-time org-defdecode)
  14424. org-defdecode (decode-time org-def)))))
  14425. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14426. (calendar-frame-setup nil)
  14427. (calendar-setup nil)
  14428. (calendar-move-hook nil)
  14429. (calendar-view-diary-initially-flag nil)
  14430. (calendar-view-holidays-initially-flag nil)
  14431. (timestr (format-time-string
  14432. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14433. (prompt (concat (if prompt (concat prompt " ") "")
  14434. (format "Date+time [%s]: " timestr)))
  14435. ans (org-ans0 "") org-ans1 org-ans2 final)
  14436. (cond
  14437. (from-string (setq ans from-string))
  14438. (org-read-date-popup-calendar
  14439. (save-excursion
  14440. (save-window-excursion
  14441. (calendar)
  14442. (org-eval-in-calendar '(setq cursor-type nil) t)
  14443. (unwind-protect
  14444. (progn
  14445. (calendar-forward-day (- (time-to-days org-def)
  14446. (calendar-absolute-from-gregorian
  14447. (calendar-current-date))))
  14448. (org-eval-in-calendar nil t)
  14449. (let* ((old-map (current-local-map))
  14450. (map (copy-keymap calendar-mode-map))
  14451. (minibuffer-local-map
  14452. (copy-keymap org-read-date-minibuffer-local-map)))
  14453. (org-defkey map (kbd "RET") 'org-calendar-select)
  14454. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14455. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14456. (unwind-protect
  14457. (progn
  14458. (use-local-map map)
  14459. (setq org-read-date-inactive inactive)
  14460. (add-hook 'post-command-hook 'org-read-date-display)
  14461. (setq org-ans0 (read-string prompt default-input
  14462. 'org-read-date-history nil))
  14463. ;; org-ans0: from prompt
  14464. ;; org-ans1: from mouse click
  14465. ;; org-ans2: from calendar motion
  14466. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14467. (remove-hook 'post-command-hook 'org-read-date-display)
  14468. (use-local-map old-map)
  14469. (when org-read-date-overlay
  14470. (delete-overlay org-read-date-overlay)
  14471. (setq org-read-date-overlay nil)))))
  14472. (bury-buffer "*Calendar*")))))
  14473. (t ; Naked prompt only
  14474. (unwind-protect
  14475. (setq ans (read-string prompt default-input
  14476. 'org-read-date-history timestr))
  14477. (when org-read-date-overlay
  14478. (delete-overlay org-read-date-overlay)
  14479. (setq org-read-date-overlay nil)))))
  14480. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14481. (when org-read-date-analyze-forced-year
  14482. (message "Year was forced into %s"
  14483. (if org-read-date-force-compatible-dates
  14484. "compatible range (1970-2037)"
  14485. "range representable on this machine"))
  14486. (ding))
  14487. ;; One round trip to get rid of 34th of August and stuff like that....
  14488. (setq final (decode-time (apply 'encode-time final)))
  14489. (setq org-read-date-final-answer ans)
  14490. (if to-time
  14491. (apply 'encode-time final)
  14492. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14493. (format "%04d-%02d-%02d %02d:%02d"
  14494. (nth 5 final) (nth 4 final) (nth 3 final)
  14495. (nth 2 final) (nth 1 final))
  14496. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14497. (defvar org-def)
  14498. (defvar org-defdecode)
  14499. (defvar org-with-time)
  14500. (defun org-read-date-display ()
  14501. "Display the current date prompt interpretation in the minibuffer."
  14502. (when org-read-date-display-live
  14503. (when org-read-date-overlay
  14504. (delete-overlay org-read-date-overlay))
  14505. (when (minibufferp (current-buffer))
  14506. (save-excursion
  14507. (end-of-line 1)
  14508. (while (not (equal (buffer-substring
  14509. (max (point-min) (- (point) 4)) (point))
  14510. " "))
  14511. (insert " ")))
  14512. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14513. " " (or org-ans1 org-ans2)))
  14514. (org-end-time-was-given nil)
  14515. (f (org-read-date-analyze ans org-def org-defdecode))
  14516. (fmts (if org-dcst
  14517. org-time-stamp-custom-formats
  14518. org-time-stamp-formats))
  14519. (fmt (if (or org-with-time
  14520. (and (boundp 'org-time-was-given) org-time-was-given))
  14521. (cdr fmts)
  14522. (car fmts)))
  14523. (txt (format-time-string fmt (apply 'encode-time f)))
  14524. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14525. (txt (concat "=> " txt)))
  14526. (when (and org-end-time-was-given
  14527. (string-match org-plain-time-of-day-regexp txt))
  14528. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14529. org-end-time-was-given
  14530. (substring txt (match-end 0)))))
  14531. (when org-read-date-analyze-futurep
  14532. (setq txt (concat txt " (=>F)")))
  14533. (setq org-read-date-overlay
  14534. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14535. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14536. (defun org-read-date-analyze (ans org-def org-defdecode)
  14537. "Analyze the combined answer of the date prompt."
  14538. ;; FIXME: cleanup and comment
  14539. (let ((nowdecode (decode-time (current-time)))
  14540. delta deltan deltaw deltadef year month day
  14541. hour minute second wday pm h2 m2 tl wday1
  14542. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14543. (setq org-read-date-analyze-futurep nil
  14544. org-read-date-analyze-forced-year nil)
  14545. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14546. (setq ans "+0"))
  14547. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14548. (setq ans (replace-match "" t t ans)
  14549. deltan (car delta)
  14550. deltaw (nth 1 delta)
  14551. deltadef (nth 2 delta)))
  14552. ;; Check if there is an iso week date in there. If yes, store the
  14553. ;; info and postpone interpreting it until the rest of the parsing
  14554. ;; is done.
  14555. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14556. (setq iso-year (if (match-end 1)
  14557. (org-small-year-to-year
  14558. (string-to-number (match-string 1 ans))))
  14559. iso-weekday (if (match-end 3)
  14560. (string-to-number (match-string 3 ans)))
  14561. iso-week (string-to-number (match-string 2 ans)))
  14562. (setq ans (replace-match "" t t ans)))
  14563. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14564. (when (string-match
  14565. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14566. (setq year (if (match-end 2)
  14567. (string-to-number (match-string 2 ans))
  14568. (progn (setq kill-year t)
  14569. (string-to-number (format-time-string "%Y"))))
  14570. month (string-to-number (match-string 3 ans))
  14571. day (string-to-number (match-string 4 ans)))
  14572. (if (< year 100) (setq year (+ 2000 year)))
  14573. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14574. t nil ans)))
  14575. ;; Help matching dotted european dates
  14576. (when (string-match
  14577. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14578. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14579. (setq kill-year t)
  14580. (string-to-number (format-time-string "%Y")))
  14581. day (string-to-number (match-string 1 ans))
  14582. month (string-to-number (match-string 2 ans))
  14583. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14584. t nil ans)))
  14585. ;; Help matching american dates, like 5/30 or 5/30/7
  14586. (when (string-match
  14587. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14588. (setq year (if (match-end 4)
  14589. (string-to-number (match-string 4 ans))
  14590. (progn (setq kill-year t)
  14591. (string-to-number (format-time-string "%Y"))))
  14592. month (string-to-number (match-string 1 ans))
  14593. day (string-to-number (match-string 2 ans)))
  14594. (if (< year 100) (setq year (+ 2000 year)))
  14595. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14596. t nil ans)))
  14597. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14598. ;; If there is a time with am/pm, and *no* time without it, we convert
  14599. ;; so that matching will be successful.
  14600. (loop for i from 1 to 2 do ; twice, for end time as well
  14601. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14602. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14603. (setq hour (string-to-number (match-string 1 ans))
  14604. minute (if (match-end 3)
  14605. (string-to-number (match-string 3 ans))
  14606. 0)
  14607. pm (equal ?p
  14608. (string-to-char (downcase (match-string 4 ans)))))
  14609. (if (and (= hour 12) (not pm))
  14610. (setq hour 0)
  14611. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14612. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14613. t t ans))))
  14614. ;; Check if a time range is given as a duration
  14615. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14616. (setq hour (string-to-number (match-string 1 ans))
  14617. h2 (+ hour (string-to-number (match-string 3 ans)))
  14618. minute (string-to-number (match-string 2 ans))
  14619. m2 (+ minute (if (match-end 5) (string-to-number
  14620. (match-string 5 ans))0)))
  14621. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14622. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14623. t t ans)))
  14624. ;; Check if there is a time range
  14625. (when (boundp 'org-end-time-was-given)
  14626. (setq org-time-was-given nil)
  14627. (when (and (string-match org-plain-time-of-day-regexp ans)
  14628. (match-end 8))
  14629. (setq org-end-time-was-given (match-string 8 ans))
  14630. (setq ans (concat (substring ans 0 (match-beginning 7))
  14631. (substring ans (match-end 7))))))
  14632. (setq tl (parse-time-string ans)
  14633. day (or (nth 3 tl) (nth 3 org-defdecode))
  14634. month (or (nth 4 tl)
  14635. (if (and org-read-date-prefer-future
  14636. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14637. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14638. (nth 4 org-defdecode)))
  14639. year (or (and (not kill-year) (nth 5 tl))
  14640. (if (and org-read-date-prefer-future
  14641. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14642. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14643. (nth 5 org-defdecode)))
  14644. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14645. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14646. second (or (nth 0 tl) 0)
  14647. wday (nth 6 tl))
  14648. (when (and (eq org-read-date-prefer-future 'time)
  14649. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14650. (equal day (nth 3 nowdecode))
  14651. (equal month (nth 4 nowdecode))
  14652. (equal year (nth 5 nowdecode))
  14653. (nth 2 tl)
  14654. (or (< (nth 2 tl) (nth 2 nowdecode))
  14655. (and (= (nth 2 tl) (nth 2 nowdecode))
  14656. (nth 1 tl)
  14657. (< (nth 1 tl) (nth 1 nowdecode)))))
  14658. (setq day (1+ day)
  14659. futurep t))
  14660. ;; Special date definitions below
  14661. (cond
  14662. (iso-week
  14663. ;; There was an iso week
  14664. (require 'cal-iso)
  14665. (setq futurep nil)
  14666. (setq year (or iso-year year)
  14667. day (or iso-weekday wday 1)
  14668. wday nil ; to make sure that the trigger below does not match
  14669. iso-date (calendar-gregorian-from-absolute
  14670. (calendar-absolute-from-iso
  14671. (list iso-week day year))))
  14672. ; FIXME: Should we also push ISO weeks into the future?
  14673. ; (when (and org-read-date-prefer-future
  14674. ; (not iso-year)
  14675. ; (< (calendar-absolute-from-gregorian iso-date)
  14676. ; (time-to-days (current-time))))
  14677. ; (setq year (1+ year)
  14678. ; iso-date (calendar-gregorian-from-absolute
  14679. ; (calendar-absolute-from-iso
  14680. ; (list iso-week day year)))))
  14681. (setq month (car iso-date)
  14682. year (nth 2 iso-date)
  14683. day (nth 1 iso-date)))
  14684. (deltan
  14685. (setq futurep nil)
  14686. (unless deltadef
  14687. (let ((now (decode-time (current-time))))
  14688. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14689. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14690. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14691. ((equal deltaw "m") (setq month (+ month deltan)))
  14692. ((equal deltaw "y") (setq year (+ year deltan)))))
  14693. ((and wday (not (nth 3 tl)))
  14694. ;; Weekday was given, but no day, so pick that day in the week
  14695. ;; on or after the derived date.
  14696. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14697. (unless (equal wday wday1)
  14698. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14699. (if (and (boundp 'org-time-was-given)
  14700. (nth 2 tl))
  14701. (setq org-time-was-given t))
  14702. (if (< year 100) (setq year (+ 2000 year)))
  14703. ;; Check of the date is representable
  14704. (if org-read-date-force-compatible-dates
  14705. (progn
  14706. (if (< year 1970)
  14707. (setq year 1970 org-read-date-analyze-forced-year t))
  14708. (if (> year 2037)
  14709. (setq year 2037 org-read-date-analyze-forced-year t)))
  14710. (condition-case nil
  14711. (ignore (encode-time second minute hour day month year))
  14712. (error
  14713. (setq year (nth 5 org-defdecode))
  14714. (setq org-read-date-analyze-forced-year t))))
  14715. (setq org-read-date-analyze-futurep futurep)
  14716. (list second minute hour day month year)))
  14717. (defvar parse-time-weekdays)
  14718. (defun org-read-date-get-relative (s today default)
  14719. "Check string S for special relative date string.
  14720. TODAY and DEFAULT are internal times, for today and for a default.
  14721. Return shift list (N what def-flag)
  14722. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14723. N is the number of WHATs to shift.
  14724. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14725. the DEFAULT date rather than TODAY."
  14726. (require 'parse-time)
  14727. (when (and
  14728. (string-match
  14729. (concat
  14730. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14731. "\\([0-9]+\\)?"
  14732. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14733. "\\([ \t]\\|$\\)") s)
  14734. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14735. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14736. (string-to-char (substring (match-string 1 s) -1))
  14737. ?+))
  14738. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14739. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14740. (what (if (match-end 3) (match-string 3 s) "d"))
  14741. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14742. (date (if rel default today))
  14743. (wday (nth 6 (decode-time date)))
  14744. delta)
  14745. (if wday1
  14746. (progn
  14747. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14748. (if (= dir ?-) (setq delta (- delta 7)))
  14749. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14750. (list delta "d" rel))
  14751. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14752. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14753. "Turn a user-specified date into the internal representation.
  14754. The internal representation needed by the calendar is (month day year).
  14755. This is a wrapper to handle the brain-dead convention in calendar that
  14756. user function argument order change dependent on argument order."
  14757. (if (boundp 'calendar-date-style)
  14758. (cond
  14759. ((eq calendar-date-style 'american)
  14760. (list arg1 arg2 arg3))
  14761. ((eq calendar-date-style 'european)
  14762. (list arg2 arg1 arg3))
  14763. ((eq calendar-date-style 'iso)
  14764. (list arg2 arg3 arg1)))
  14765. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14766. (if (org-bound-and-true-p european-calendar-style)
  14767. (list arg2 arg1 arg3)
  14768. (list arg1 arg2 arg3)))))
  14769. (defun org-eval-in-calendar (form &optional keepdate)
  14770. "Eval FORM in the calendar window and return to current window.
  14771. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14772. otherwise stick to the current value of `org-ans2'."
  14773. (let ((sf (selected-frame))
  14774. (sw (selected-window)))
  14775. (select-window (get-buffer-window "*Calendar*" t))
  14776. (eval form)
  14777. (when (and (not keepdate) (calendar-cursor-to-date))
  14778. (let* ((date (calendar-cursor-to-date))
  14779. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14780. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14781. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14782. (select-window sw)
  14783. (org-select-frame-set-input-focus sf)))
  14784. (defun org-calendar-select ()
  14785. "Return to `org-read-date' with the date currently selected.
  14786. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14787. (interactive)
  14788. (when (calendar-cursor-to-date)
  14789. (let* ((date (calendar-cursor-to-date))
  14790. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14791. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14792. (if (active-minibuffer-window) (exit-minibuffer))))
  14793. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14794. "Insert a date stamp for the date given by the internal TIME.
  14795. WITH-HM means use the stamp format that includes the time of the day.
  14796. INACTIVE means use square brackets instead of angular ones, so that the
  14797. stamp will not contribute to the agenda.
  14798. PRE and POST are optional strings to be inserted before and after the
  14799. stamp.
  14800. The command returns the inserted time stamp."
  14801. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14802. stamp)
  14803. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14804. (insert-before-markers (or pre ""))
  14805. (when (listp extra)
  14806. (setq extra (car extra))
  14807. (if (and (stringp extra)
  14808. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14809. (setq extra (format "-%02d:%02d"
  14810. (string-to-number (match-string 1 extra))
  14811. (string-to-number (match-string 2 extra))))
  14812. (setq extra nil)))
  14813. (when extra
  14814. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14815. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14816. (insert-before-markers (or post ""))
  14817. (setq org-last-inserted-timestamp stamp)))
  14818. (defun org-toggle-time-stamp-overlays ()
  14819. "Toggle the use of custom time stamp formats."
  14820. (interactive)
  14821. (setq org-display-custom-times (not org-display-custom-times))
  14822. (unless org-display-custom-times
  14823. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14824. (while (setq p (next-single-property-change p 'display))
  14825. (if (and (get-text-property p 'display)
  14826. (eq (get-text-property p 'face) 'org-date))
  14827. (remove-text-properties
  14828. p (setq p (next-single-property-change p 'display))
  14829. '(display t))))
  14830. (set-buffer-modified-p bmp)))
  14831. (if (featurep 'xemacs)
  14832. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14833. (org-restart-font-lock)
  14834. (setq org-table-may-need-update t)
  14835. (if org-display-custom-times
  14836. (message "Time stamps are overlaid with custom format")
  14837. (message "Time stamp overlays removed")))
  14838. (defun org-display-custom-time (beg end)
  14839. "Overlay modified time stamp format over timestamp between BEG and END."
  14840. (let* ((ts (buffer-substring beg end))
  14841. t1 w1 with-hm tf time str w2 (off 0))
  14842. (save-match-data
  14843. (setq t1 (org-parse-time-string ts t))
  14844. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14845. (setq off (- (match-end 0) (match-beginning 0)))))
  14846. (setq end (- end off))
  14847. (setq w1 (- end beg)
  14848. with-hm (and (nth 1 t1) (nth 2 t1))
  14849. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14850. time (org-fix-decoded-time t1)
  14851. str (org-add-props
  14852. (format-time-string
  14853. (substring tf 1 -1) (apply 'encode-time time))
  14854. nil 'mouse-face 'highlight)
  14855. w2 (length str))
  14856. (if (not (= w2 w1))
  14857. (add-text-properties (1+ beg) (+ 2 beg)
  14858. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14859. (if (featurep 'xemacs)
  14860. (progn
  14861. (put-text-property beg end 'invisible t)
  14862. (put-text-property beg end 'end-glyph (make-glyph str)))
  14863. (put-text-property beg end 'display str))))
  14864. (defun org-translate-time (string)
  14865. "Translate all timestamps in STRING to custom format.
  14866. But do this only if the variable `org-display-custom-times' is set."
  14867. (when org-display-custom-times
  14868. (save-match-data
  14869. (let* ((start 0)
  14870. (re org-ts-regexp-both)
  14871. t1 with-hm inactive tf time str beg end)
  14872. (while (setq start (string-match re string start))
  14873. (setq beg (match-beginning 0)
  14874. end (match-end 0)
  14875. t1 (save-match-data
  14876. (org-parse-time-string (substring string beg end) t))
  14877. with-hm (and (nth 1 t1) (nth 2 t1))
  14878. inactive (equal (substring string beg (1+ beg)) "[")
  14879. tf (funcall (if with-hm 'cdr 'car)
  14880. org-time-stamp-custom-formats)
  14881. time (org-fix-decoded-time t1)
  14882. str (format-time-string
  14883. (concat
  14884. (if inactive "[" "<") (substring tf 1 -1)
  14885. (if inactive "]" ">"))
  14886. (apply 'encode-time time))
  14887. string (replace-match str t t string)
  14888. start (+ start (length str)))))))
  14889. string)
  14890. (defun org-fix-decoded-time (time)
  14891. "Set 0 instead of nil for the first 6 elements of time.
  14892. Don't touch the rest."
  14893. (let ((n 0))
  14894. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14895. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14896. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14897. "Difference between TIMESTAMP-STRING and now in days.
  14898. If SECONDS is non-nil, return the difference in seconds."
  14899. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14900. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14901. (funcall fdiff (current-time)))))
  14902. (defun org-deadline-close (timestamp-string &optional ndays)
  14903. "Is the time in TIMESTAMP-STRING close to the current date?"
  14904. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14905. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14906. (not (org-entry-is-done-p))))
  14907. (defun org-get-wdays (ts &optional delay zero-delay)
  14908. "Get the deadline lead time appropriate for timestring TS.
  14909. When DELAY is non-nil, get the delay time for scheduled items
  14910. instead of the deadline lead time. When ZERO-DELAY is non-nil
  14911. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  14912. don't try to find the delay cookie in the scheduled timestamp."
  14913. (let ((tv (if delay org-scheduled-delay-days
  14914. org-deadline-warning-days)))
  14915. (cond
  14916. ((or (and delay (< tv 0))
  14917. (and delay zero-delay (<= tv 0))
  14918. (and (not delay) (<= tv 0)))
  14919. ;; Enforce this value no matter what
  14920. (- tv))
  14921. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14922. ;; lead time is specified.
  14923. (floor (* (string-to-number (match-string 1 ts))
  14924. (cdr (assoc (match-string 2 ts)
  14925. '(("d" . 1) ("w" . 7)
  14926. ("m" . 30.4) ("y" . 365.25)
  14927. ("h" . 0.041667)))))))
  14928. ;; go for the default.
  14929. (t tv))))
  14930. (defun org-calendar-select-mouse (ev)
  14931. "Return to `org-read-date' with the date currently selected.
  14932. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14933. (interactive "e")
  14934. (mouse-set-point ev)
  14935. (when (calendar-cursor-to-date)
  14936. (let* ((date (calendar-cursor-to-date))
  14937. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14938. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14939. (if (active-minibuffer-window) (exit-minibuffer))))
  14940. (defun org-check-deadlines (ndays)
  14941. "Check if there are any deadlines due or past due.
  14942. A deadline is considered due if it happens within `org-deadline-warning-days'
  14943. days from today's date. If the deadline appears in an entry marked DONE,
  14944. it is not shown. The prefix arg NDAYS can be used to test that many
  14945. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14946. (interactive "P")
  14947. (let* ((org-warn-days
  14948. (cond
  14949. ((equal ndays '(4)) 100000)
  14950. (ndays (prefix-numeric-value ndays))
  14951. (t (abs org-deadline-warning-days))))
  14952. (case-fold-search nil)
  14953. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14954. (callback
  14955. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14956. (message "%d deadlines past-due or due within %d days"
  14957. (org-occur regexp nil callback)
  14958. org-warn-days)))
  14959. (defsubst org-re-timestamp (type)
  14960. "Return a regexp for timestamp TYPE.
  14961. Allowed values for TYPE are:
  14962. all: all timestamps
  14963. active: only active timestamps (<...>)
  14964. inactive: only inactive timestamps ([...])
  14965. scheduled: only scheduled timestamps
  14966. deadline: only deadline timestamps
  14967. When TYPE is nil, fall back on returning a regexp that matches
  14968. both scheduled and deadline timestamps."
  14969. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14970. ((eq type 'active) org-ts-regexp)
  14971. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14972. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14973. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14974. ((eq type 'scheduled-or-deadline)
  14975. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14976. (defun org-check-before-date (date)
  14977. "Check if there are deadlines or scheduled entries before DATE."
  14978. (interactive (list (org-read-date)))
  14979. (let ((case-fold-search nil)
  14980. (regexp (org-re-timestamp org-ts-type))
  14981. (callback
  14982. (lambda () (time-less-p
  14983. (org-time-string-to-time (match-string 1))
  14984. (org-time-string-to-time date)))))
  14985. (message "%d entries before %s"
  14986. (org-occur regexp nil callback) date)))
  14987. (defun org-check-after-date (date)
  14988. "Check if there are deadlines or scheduled entries after DATE."
  14989. (interactive (list (org-read-date)))
  14990. (let ((case-fold-search nil)
  14991. (regexp (org-re-timestamp org-ts-type))
  14992. (callback
  14993. (lambda () (not
  14994. (time-less-p
  14995. (org-time-string-to-time (match-string 1))
  14996. (org-time-string-to-time date))))))
  14997. (message "%d entries after %s"
  14998. (org-occur regexp nil callback) date)))
  14999. (defun org-check-dates-range (start-date end-date)
  15000. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15001. (interactive (list (org-read-date nil nil nil "Range starts")
  15002. (org-read-date nil nil nil "Range end")))
  15003. (let ((case-fold-search nil)
  15004. (regexp (org-re-timestamp org-ts-type))
  15005. (callback
  15006. (lambda ()
  15007. (let ((match (match-string 1)))
  15008. (and
  15009. (not (time-less-p
  15010. (org-time-string-to-time match)
  15011. (org-time-string-to-time start-date)))
  15012. (time-less-p
  15013. (org-time-string-to-time match)
  15014. (org-time-string-to-time end-date)))))))
  15015. (message "%d entries between %s and %s"
  15016. (org-occur regexp nil callback) start-date end-date)))
  15017. (defun org-evaluate-time-range (&optional to-buffer)
  15018. "Evaluate a time range by computing the difference between start and end.
  15019. Normally the result is just printed in the echo area, but with prefix arg
  15020. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15021. If the time range is actually in a table, the result is inserted into the
  15022. next column.
  15023. For time difference computation, a year is assumed to be exactly 365
  15024. days in order to avoid rounding problems."
  15025. (interactive "P")
  15026. (or
  15027. (org-clock-update-time-maybe)
  15028. (save-excursion
  15029. (unless (org-at-date-range-p t)
  15030. (goto-char (point-at-bol))
  15031. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15032. (if (not (org-at-date-range-p t))
  15033. (error "Not at a time-stamp range, and none found in current line")))
  15034. (let* ((ts1 (match-string 1))
  15035. (ts2 (match-string 2))
  15036. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15037. (match-end (match-end 0))
  15038. (time1 (org-time-string-to-time ts1))
  15039. (time2 (org-time-string-to-time ts2))
  15040. (t1 (org-float-time time1))
  15041. (t2 (org-float-time time2))
  15042. (diff (abs (- t2 t1)))
  15043. (negative (< (- t2 t1) 0))
  15044. ;; (ys (floor (* 365 24 60 60)))
  15045. (ds (* 24 60 60))
  15046. (hs (* 60 60))
  15047. (fy "%dy %dd %02d:%02d")
  15048. (fy1 "%dy %dd")
  15049. (fd "%dd %02d:%02d")
  15050. (fd1 "%dd")
  15051. (fh "%02d:%02d")
  15052. y d h m align)
  15053. (if havetime
  15054. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15055. y 0
  15056. d (floor (/ diff ds)) diff (mod diff ds)
  15057. h (floor (/ diff hs)) diff (mod diff hs)
  15058. m (floor (/ diff 60)))
  15059. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15060. y 0
  15061. d (floor (+ (/ diff ds) 0.5))
  15062. h 0 m 0))
  15063. (if (not to-buffer)
  15064. (message "%s" (org-make-tdiff-string y d h m))
  15065. (if (org-at-table-p)
  15066. (progn
  15067. (goto-char match-end)
  15068. (setq align t)
  15069. (and (looking-at " *|") (goto-char (match-end 0))))
  15070. (goto-char match-end))
  15071. (if (looking-at
  15072. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15073. (replace-match ""))
  15074. (if negative (insert " -"))
  15075. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15076. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15077. (insert " " (format fh h m))))
  15078. (if align (org-table-align))
  15079. (message "Time difference inserted")))))
  15080. (defun org-make-tdiff-string (y d h m)
  15081. (let ((fmt "")
  15082. (l nil))
  15083. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15084. l (push y l)))
  15085. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15086. l (push d l)))
  15087. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15088. l (push h l)))
  15089. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15090. l (push m l)))
  15091. (apply 'format fmt (nreverse l))))
  15092. (defun org-time-string-to-time (s &optional buffer pos)
  15093. "Convert a timestamp string into internal time."
  15094. (condition-case errdata
  15095. (apply 'encode-time (org-parse-time-string s))
  15096. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15097. s (if (not (and buffer pos))
  15098. ""
  15099. (format " at %d in buffer `%s'" pos buffer))
  15100. (cdr errdata)))))
  15101. (defun org-time-string-to-seconds (s)
  15102. "Convert a timestamp string to a number of seconds."
  15103. (org-float-time (org-time-string-to-time s)))
  15104. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15105. "Convert a time stamp to an absolute day number.
  15106. If there is a specifier for a cyclic time stamp, get the closest
  15107. date to DAYNR.
  15108. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15109. The variable date is bound by the calendar when this is called."
  15110. (cond
  15111. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15112. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15113. daynr
  15114. (+ daynr 1000)))
  15115. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15116. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15117. (time-to-days (current-time))) (match-string 0 s)
  15118. prefer show-all))
  15119. (t (time-to-days
  15120. (condition-case errdata
  15121. (apply 'encode-time (org-parse-time-string s))
  15122. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15123. s (if (not (and buffer pos))
  15124. ""
  15125. (format " at %d in buffer `%s'" pos buffer))
  15126. (cdr errdata))))))))
  15127. (defun org-days-to-iso-week (days)
  15128. "Return the iso week number."
  15129. (require 'cal-iso)
  15130. (car (calendar-iso-from-absolute days)))
  15131. (defun org-small-year-to-year (year)
  15132. "Convert 2-digit years into 4-digit years.
  15133. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15134. The year 2000 cannot be abbreviated. Any year larger than 99
  15135. is returned unchanged."
  15136. (if (< year 38)
  15137. (setq year (+ 2000 year))
  15138. (if (< year 100)
  15139. (setq year (+ 1900 year))))
  15140. year)
  15141. (defun org-time-from-absolute (d)
  15142. "Return the time corresponding to date D.
  15143. D may be an absolute day number, or a calendar-type list (month day year)."
  15144. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15145. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15146. (defun org-calendar-holiday ()
  15147. "List of holidays, for Diary display in Org-mode."
  15148. (require 'holidays)
  15149. (let ((hl (funcall
  15150. (if (fboundp 'calendar-check-holidays)
  15151. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15152. (if hl (mapconcat 'identity hl "; "))))
  15153. (defun org-diary-sexp-entry (sexp entry date)
  15154. "Process a SEXP diary ENTRY for DATE."
  15155. (require 'diary-lib)
  15156. (let ((result (if calendar-debug-sexp
  15157. (let ((stack-trace-on-error t))
  15158. (eval (car (read-from-string sexp))))
  15159. (condition-case nil
  15160. (eval (car (read-from-string sexp)))
  15161. (error
  15162. (beep)
  15163. (message "Bad sexp at line %d in %s: %s"
  15164. (org-current-line)
  15165. (buffer-file-name) sexp)
  15166. (sleep-for 2))))))
  15167. (cond ((stringp result) (split-string result "; "))
  15168. ((and (consp result)
  15169. (not (consp (cdr result)))
  15170. (stringp (cdr result))) (cdr result))
  15171. ((and (consp result)
  15172. (stringp (car result))) result)
  15173. (result entry))))
  15174. (defun org-diary-to-ical-string (frombuf)
  15175. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15176. This uses the icalendar.el library."
  15177. (let* ((tmpdir (if (featurep 'xemacs)
  15178. (temp-directory)
  15179. temporary-file-directory))
  15180. (tmpfile (make-temp-name
  15181. (expand-file-name "orgics" tmpdir)))
  15182. buf rtn b e)
  15183. (with-current-buffer frombuf
  15184. (icalendar-export-region (point-min) (point-max) tmpfile)
  15185. (setq buf (find-buffer-visiting tmpfile))
  15186. (set-buffer buf)
  15187. (goto-char (point-min))
  15188. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15189. (setq b (match-beginning 0)))
  15190. (goto-char (point-max))
  15191. (if (re-search-backward "^END:VEVENT" nil t)
  15192. (setq e (match-end 0)))
  15193. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15194. (kill-buffer buf)
  15195. (delete-file tmpfile)
  15196. rtn))
  15197. (defun org-closest-date (start current change prefer show-all)
  15198. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15199. When PREFER is `past', return a date that is either CURRENT or past.
  15200. When PREFER is `future', return a date that is either CURRENT or future.
  15201. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15202. ;; Make the proper lists from the dates
  15203. (catch 'exit
  15204. (let ((a1 '(("h" . hour)
  15205. ("d" . day)
  15206. ("w" . week)
  15207. ("m" . month)
  15208. ("y" . year)))
  15209. (shour (nth 2 (org-parse-time-string start)))
  15210. dn dw sday cday n1 n2 n0
  15211. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15212. (setq start (org-date-to-gregorian start)
  15213. current (org-date-to-gregorian
  15214. (if show-all
  15215. current
  15216. (time-to-days (current-time))))
  15217. sday (calendar-absolute-from-gregorian start)
  15218. cday (calendar-absolute-from-gregorian current))
  15219. (if (<= cday sday) (throw 'exit sday))
  15220. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15221. (setq dn (string-to-number (match-string 1 change))
  15222. dw (cdr (assoc (match-string 2 change) a1)))
  15223. (error "Invalid change specifier: %s" change))
  15224. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15225. (cond
  15226. ((eq dw 'hour)
  15227. (let ((missing-hours
  15228. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15229. dn)))
  15230. (setq n1 (if (zerop missing-hours) cday
  15231. (- cday (1+ (floor (/ missing-hours 24)))))
  15232. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15233. ((eq dw 'day)
  15234. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15235. n2 (+ n1 dn)))
  15236. ((eq dw 'year)
  15237. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15238. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15239. (setq date1 (list m d y1)
  15240. n1 (calendar-absolute-from-gregorian date1)
  15241. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15242. n2 (calendar-absolute-from-gregorian date2)))
  15243. ((eq dw 'month)
  15244. ;; approx number of month between the two dates
  15245. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15246. ;; How often does dn fit in there?
  15247. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15248. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15249. m (+ m nm)
  15250. ny (floor (/ m 12))
  15251. y (+ y ny)
  15252. m (- m (* ny 12)))
  15253. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15254. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15255. (setq m2 (+ m dn) y2 y)
  15256. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15257. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15258. (while (<= n2 cday)
  15259. (setq n1 n2 m m2 y y2)
  15260. (setq m2 (+ m dn) y2 y)
  15261. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15262. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15263. ;; Make sure n1 is the earlier date
  15264. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15265. (if show-all
  15266. (cond
  15267. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15268. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15269. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15270. (cond
  15271. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15272. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15273. (t (if (= cday n1) n1 n2)))))))
  15274. (defun org-date-to-gregorian (date)
  15275. "Turn any specification of DATE into a Gregorian date for the calendar."
  15276. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15277. ((and (listp date) (= (length date) 3)) date)
  15278. ((stringp date)
  15279. (setq date (org-parse-time-string date))
  15280. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15281. ((listp date)
  15282. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15283. (defun org-parse-time-string (s &optional nodefault)
  15284. "Parse the standard Org-mode time string.
  15285. This should be a lot faster than the normal `parse-time-string'.
  15286. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15287. hour and minute fields will be nil if not given."
  15288. (cond ((string-match org-ts-regexp0 s)
  15289. (list 0
  15290. (if (or (match-beginning 8) (not nodefault))
  15291. (string-to-number (or (match-string 8 s) "0")))
  15292. (if (or (match-beginning 7) (not nodefault))
  15293. (string-to-number (or (match-string 7 s) "0")))
  15294. (string-to-number (match-string 4 s))
  15295. (string-to-number (match-string 3 s))
  15296. (string-to-number (match-string 2 s))
  15297. nil nil nil))
  15298. ((string-match "^<[^>]+>$" s)
  15299. (decode-time (seconds-to-time (org-matcher-time s))))
  15300. (t (error "Not a standard Org-mode time string: %s" s))))
  15301. (defun org-timestamp-up (&optional arg)
  15302. "Increase the date item at the cursor by one.
  15303. If the cursor is on the year, change the year. If it is on the month,
  15304. the day or the time, change that.
  15305. With prefix ARG, change by that many units."
  15306. (interactive "p")
  15307. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15308. (defun org-timestamp-down (&optional arg)
  15309. "Decrease the date item at the cursor by one.
  15310. If the cursor is on the year, change the year. If it is on the month,
  15311. the day or the time, change that.
  15312. With prefix ARG, change by that many units."
  15313. (interactive "p")
  15314. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15315. (defun org-timestamp-up-day (&optional arg)
  15316. "Increase the date in the time stamp by one day.
  15317. With prefix ARG, change that many days."
  15318. (interactive "p")
  15319. (if (and (not (org-at-timestamp-p t))
  15320. (org-at-heading-p))
  15321. (org-todo 'up)
  15322. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15323. (defun org-timestamp-down-day (&optional arg)
  15324. "Decrease the date in the time stamp by one day.
  15325. With prefix ARG, change that many days."
  15326. (interactive "p")
  15327. (if (and (not (org-at-timestamp-p t))
  15328. (org-at-heading-p))
  15329. (org-todo 'down)
  15330. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15331. (defun org-at-timestamp-p (&optional inactive-ok)
  15332. "Determine if the cursor is in or at a timestamp."
  15333. (interactive)
  15334. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15335. (pos (point))
  15336. (ans (or (looking-at tsr)
  15337. (save-excursion
  15338. (skip-chars-backward "^[<\n\r\t")
  15339. (if (> (point) (point-min)) (backward-char 1))
  15340. (and (looking-at tsr)
  15341. (> (- (match-end 0) pos) -1))))))
  15342. (and ans
  15343. (boundp 'org-ts-what)
  15344. (setq org-ts-what
  15345. (cond
  15346. ((= pos (match-beginning 0)) 'bracket)
  15347. ;; Point is considered to be "on the bracket" whether
  15348. ;; it's really on it or right after it.
  15349. ((= pos (1- (match-end 0))) 'bracket)
  15350. ((= pos (match-end 0)) 'after)
  15351. ((org-pos-in-match-range pos 2) 'year)
  15352. ((org-pos-in-match-range pos 3) 'month)
  15353. ((org-pos-in-match-range pos 7) 'hour)
  15354. ((org-pos-in-match-range pos 8) 'minute)
  15355. ((or (org-pos-in-match-range pos 4)
  15356. (org-pos-in-match-range pos 5)) 'day)
  15357. ((and (> pos (or (match-end 8) (match-end 5)))
  15358. (< pos (match-end 0)))
  15359. (- pos (or (match-end 8) (match-end 5))))
  15360. (t 'day))))
  15361. ans))
  15362. (defun org-toggle-timestamp-type ()
  15363. "Toggle the type (<active> or [inactive]) of a time stamp."
  15364. (interactive)
  15365. (when (org-at-timestamp-p t)
  15366. (let ((beg (match-beginning 0)) (end (match-end 0))
  15367. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15368. (save-excursion
  15369. (goto-char beg)
  15370. (while (re-search-forward "[][<>]" end t)
  15371. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15372. t t)))
  15373. (message "Timestamp is now %sactive"
  15374. (if (equal (char-after beg) ?<) "" "in")))))
  15375. (defvar org-clock-history) ; defined in org-clock.el
  15376. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15377. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15378. "Change the date in the time stamp at point.
  15379. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15380. `year', `minute', `second'. If WHAT is not given, the cursor position
  15381. in the timestamp determines what will be changed.
  15382. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15383. (let ((origin (point)) origin-cat
  15384. with-hm inactive
  15385. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15386. org-ts-what
  15387. extra rem
  15388. ts time time0 fixnext clrgx)
  15389. (if (not (org-at-timestamp-p t))
  15390. (error "Not at a timestamp"))
  15391. (if (and (not what) (eq org-ts-what 'bracket))
  15392. (org-toggle-timestamp-type)
  15393. ;; Point isn't on brackets. Remember the part of the time-stamp
  15394. ;; the point was in. Indeed, size of time-stamps may change,
  15395. ;; but point must be kept in the same category nonetheless.
  15396. (setq origin-cat org-ts-what)
  15397. (if (and (not what) (not (eq org-ts-what 'day))
  15398. org-display-custom-times
  15399. (get-text-property (point) 'display)
  15400. (not (get-text-property (1- (point)) 'display)))
  15401. (setq org-ts-what 'day))
  15402. (setq org-ts-what (or what org-ts-what)
  15403. inactive (= (char-after (match-beginning 0)) ?\[)
  15404. ts (match-string 0))
  15405. (replace-match "")
  15406. (when (string-match
  15407. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15408. ts)
  15409. (setq extra (match-string 1 ts))
  15410. (if suppress-tmp-delay
  15411. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15412. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15413. (setq with-hm t))
  15414. (setq time0 (org-parse-time-string ts))
  15415. (when (and updown
  15416. (eq org-ts-what 'minute)
  15417. (not current-prefix-arg))
  15418. ;; This looks like s-up and s-down. Change by one rounding step.
  15419. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15420. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15421. (setcar (cdr time0) (+ (nth 1 time0)
  15422. (if (> n 0) (- rem) (- dm rem))))))
  15423. (setq time
  15424. (encode-time (or (car time0) 0)
  15425. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15426. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15427. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15428. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15429. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15430. (nthcdr 6 time0)))
  15431. (when (and (member org-ts-what '(hour minute))
  15432. extra
  15433. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15434. (setq extra (org-modify-ts-extra
  15435. extra
  15436. (if (eq org-ts-what 'hour) 2 5)
  15437. n dm)))
  15438. (when (integerp org-ts-what)
  15439. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15440. (if (eq what 'calendar)
  15441. (let ((cal-date (org-get-date-from-calendar)))
  15442. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15443. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15444. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15445. (setcar time0 (or (car time0) 0))
  15446. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15447. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15448. (setq time (apply 'encode-time time0))))
  15449. ;; Insert the new time-stamp, and ensure point stays in the same
  15450. ;; category as before (i.e. not after the last position in that
  15451. ;; category).
  15452. (let ((pos (point)))
  15453. ;; Stay before inserted string. `save-excursion' is of no use.
  15454. (setq org-last-changed-timestamp
  15455. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15456. (goto-char pos))
  15457. (save-match-data
  15458. (looking-at org-ts-regexp3)
  15459. (goto-char (cond
  15460. ;; `day' category ends before `hour' if any, or at
  15461. ;; the end of the day name.
  15462. ((eq origin-cat 'day)
  15463. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15464. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15465. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15466. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15467. ;; `year' and `month' have both fixed size: point
  15468. ;; couldn't have moved into another part.
  15469. (t origin))))
  15470. ;; Update clock if on a CLOCK line.
  15471. (org-clock-update-time-maybe)
  15472. ;; Maybe adjust the closest clock in `org-clock-history'
  15473. (when org-clock-adjust-closest
  15474. (if (not (and (org-at-clock-log-p)
  15475. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15476. org-clock-history))))))
  15477. (message "No clock to adjust")
  15478. (cond ((save-excursion ; fix previous clock?
  15479. (re-search-backward org-ts-regexp0 nil t)
  15480. (org-looking-back (concat org-clock-string " \\[")))
  15481. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15482. ((save-excursion ; fix next clock?
  15483. (re-search-backward org-ts-regexp0 nil t)
  15484. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15485. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15486. (save-window-excursion
  15487. ;; Find closest clock to point, adjust the previous/next one in history
  15488. (let* ((p (save-excursion (org-back-to-heading t)))
  15489. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15490. (clfixnth
  15491. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15492. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15493. (if (not clfixpos)
  15494. (message "No clock to adjust")
  15495. (save-excursion
  15496. (org-goto-marker-or-bmk clfixpos)
  15497. (org-show-subtree)
  15498. (when (re-search-forward clrgx nil t)
  15499. (goto-char (match-beginning 1))
  15500. (let (org-clock-adjust-closest)
  15501. (org-timestamp-change n org-ts-what updown))
  15502. (message "Clock adjusted in %s for heading: %s"
  15503. (file-name-nondirectory (buffer-file-name))
  15504. (org-get-heading t t)))))))))
  15505. ;; Try to recenter the calendar window, if any.
  15506. (if (and org-calendar-follow-timestamp-change
  15507. (get-buffer-window "*Calendar*" t)
  15508. (memq org-ts-what '(day month year)))
  15509. (org-recenter-calendar (time-to-days time))))))
  15510. (defun org-modify-ts-extra (s pos n dm)
  15511. "Change the different parts of the lead-time and repeat fields in timestamp."
  15512. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15513. ng h m new rem)
  15514. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15515. (cond
  15516. ((or (org-pos-in-match-range pos 2)
  15517. (org-pos-in-match-range pos 3))
  15518. (setq m (string-to-number (match-string 3 s))
  15519. h (string-to-number (match-string 2 s)))
  15520. (if (org-pos-in-match-range pos 2)
  15521. (setq h (+ h n))
  15522. (setq n (* dm (org-no-warnings (signum n))))
  15523. (when (not (= 0 (setq rem (% m dm))))
  15524. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15525. (setq m (+ m n)))
  15526. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15527. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15528. (setq h (min 24 (max 0 h)))
  15529. (setq ng 1 new (format "-%02d:%02d" h m)))
  15530. ((org-pos-in-match-range pos 6)
  15531. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15532. ((org-pos-in-match-range pos 5)
  15533. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15534. ((org-pos-in-match-range pos 9)
  15535. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15536. ((org-pos-in-match-range pos 8)
  15537. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15538. (when ng
  15539. (setq s (concat
  15540. (substring s 0 (match-beginning ng))
  15541. new
  15542. (substring s (match-end ng))))))
  15543. s))
  15544. (defun org-recenter-calendar (date)
  15545. "If the calendar is visible, recenter it to DATE."
  15546. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15547. (when cwin
  15548. (let ((calendar-move-hook nil))
  15549. (with-selected-window cwin
  15550. (calendar-goto-date (if (listp date) date
  15551. (calendar-gregorian-from-absolute date))))))))
  15552. (defun org-goto-calendar (&optional arg)
  15553. "Go to the Emacs calendar at the current date.
  15554. If there is a time stamp in the current line, go to that date.
  15555. A prefix ARG can be used to force the current date."
  15556. (interactive "P")
  15557. (let ((tsr org-ts-regexp) diff
  15558. (calendar-move-hook nil)
  15559. (calendar-view-holidays-initially-flag nil)
  15560. (calendar-view-diary-initially-flag nil))
  15561. (if (or (org-at-timestamp-p)
  15562. (save-excursion
  15563. (beginning-of-line 1)
  15564. (looking-at (concat ".*" tsr))))
  15565. (let ((d1 (time-to-days (current-time)))
  15566. (d2 (time-to-days
  15567. (org-time-string-to-time (match-string 1)))))
  15568. (setq diff (- d2 d1))))
  15569. (calendar)
  15570. (calendar-goto-today)
  15571. (if (and diff (not arg)) (calendar-forward-day diff))))
  15572. (defun org-get-date-from-calendar ()
  15573. "Return a list (month day year) of date at point in calendar."
  15574. (with-current-buffer "*Calendar*"
  15575. (save-match-data
  15576. (calendar-cursor-to-date))))
  15577. (defun org-date-from-calendar ()
  15578. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15579. If there is already a time stamp at the cursor position, update it."
  15580. (interactive)
  15581. (if (org-at-timestamp-p t)
  15582. (org-timestamp-change 0 'calendar)
  15583. (let ((cal-date (org-get-date-from-calendar)))
  15584. (org-insert-time-stamp
  15585. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15586. (defcustom org-effort-durations
  15587. `(("h" . 60)
  15588. ("d" . ,(* 60 8))
  15589. ("w" . ,(* 60 8 5))
  15590. ("m" . ,(* 60 8 5 4))
  15591. ("y" . ,(* 60 8 5 40)))
  15592. "Conversion factor to minutes for an effort modifier.
  15593. Each entry has the form (MODIFIER . MINUTES).
  15594. In an effort string, a number followed by MODIFIER is multiplied
  15595. by the specified number of MINUTES to obtain an effort in
  15596. minutes.
  15597. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15598. effort string \"2hours\" is equivalent to 120 minutes."
  15599. :group 'org-agenda
  15600. :version "24.1"
  15601. :type '(alist :key-type (string :tag "Modifier")
  15602. :value-type (number :tag "Minutes")))
  15603. (defun org-minutes-to-clocksum-string (m)
  15604. "Format number of minutes as a clocksum string.
  15605. The format is determined by `org-time-clocksum-format',
  15606. `org-time-clocksum-use-fractional' and
  15607. `org-time-clocksum-fractional-format' and
  15608. `org-time-clocksum-use-effort-durations'."
  15609. (let ((clocksum "") h d w mo y fmt n)
  15610. (setq h (if org-time-clocksum-use-effort-durations
  15611. (cdr (assoc "h" org-effort-durations)) 60)
  15612. d (if org-time-clocksum-use-effort-durations
  15613. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15614. w (if org-time-clocksum-use-effort-durations
  15615. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15616. mo (if org-time-clocksum-use-effort-durations
  15617. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15618. y (if org-time-clocksum-use-effort-durations
  15619. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15620. ;; fractional format
  15621. (if org-time-clocksum-use-fractional
  15622. (cond
  15623. ;; single format string
  15624. ((stringp org-time-clocksum-fractional-format)
  15625. (format org-time-clocksum-fractional-format (/ m (float h))))
  15626. ;; choice of fractional formats for different time units
  15627. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15628. (> (/ (truncate m) (* y d h)) 0))
  15629. (format fmt (/ m (* y d (float h)))))
  15630. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15631. (> (/ (truncate m) (* mo d h)) 0))
  15632. (format fmt (/ m (* mo d (float h)))))
  15633. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15634. (> (/ (truncate m) (* w d h)) 0))
  15635. (format fmt (/ m (* w d (float h)))))
  15636. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15637. (> (/ (truncate m) (* d h)) 0))
  15638. (format fmt (/ m (* d (float h)))))
  15639. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15640. (> (/ (truncate m) h) 0))
  15641. (format fmt (/ m (float h))))
  15642. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15643. (format fmt m))
  15644. ;; fall back to smallest time unit with a format
  15645. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15646. (format fmt (/ m (float h))))
  15647. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15648. (format fmt (/ m (* d (float h)))))
  15649. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15650. (format fmt (/ m (* w d (float h)))))
  15651. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15652. (format fmt (/ m (* mo d (float h)))))
  15653. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15654. (format fmt (/ m (* y d (float h))))))
  15655. ;; standard (non-fractional) format, with single format string
  15656. (if (stringp org-time-clocksum-format)
  15657. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15658. ;; separate formats components
  15659. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15660. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15661. (plist-get org-time-clocksum-format :require-years))
  15662. (setq clocksum (concat clocksum (format fmt n))
  15663. m (- m (* n y d h))))
  15664. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15665. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15666. (plist-get org-time-clocksum-format :require-months))
  15667. (setq clocksum (concat clocksum (format fmt n))
  15668. m (- m (* n mo d h))))
  15669. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15670. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15671. (plist-get org-time-clocksum-format :require-weeks))
  15672. (setq clocksum (concat clocksum (format fmt n))
  15673. m (- m (* n w d h))))
  15674. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15675. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15676. (plist-get org-time-clocksum-format :require-days))
  15677. (setq clocksum (concat clocksum (format fmt n))
  15678. m (- m (* n d h))))
  15679. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15680. (or (> (setq n (/ (truncate m) h)) 0)
  15681. (plist-get org-time-clocksum-format :require-hours))
  15682. (setq clocksum (concat clocksum (format fmt n))
  15683. m (- m (* n h))))
  15684. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15685. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15686. (setq clocksum (concat clocksum (format fmt m))))
  15687. ;; return formatted time duration
  15688. clocksum))))
  15689. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15690. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15691. "Org mode version 8.0")
  15692. (defun org-hours-to-clocksum-string (n)
  15693. (org-minutes-to-clocksum-string (* n 60)))
  15694. (defun org-hh:mm-string-to-minutes (s)
  15695. "Convert a string H:MM to a number of minutes.
  15696. If the string is just a number, interpret it as minutes.
  15697. In fact, the first hh:mm or number in the string will be taken,
  15698. there can be extra stuff in the string.
  15699. If no number is found, the return value is 0."
  15700. (cond
  15701. ((integerp s) s)
  15702. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15703. (+ (* (string-to-number (match-string 1 s)) 60)
  15704. (string-to-number (match-string 2 s))))
  15705. ((string-match "\\([0-9]+\\)" s)
  15706. (string-to-number (match-string 1 s)))
  15707. (t 0)))
  15708. (defcustom org-image-actual-width t
  15709. "Should we use the actual width of images when inlining them?
  15710. When set to `t', always use the image width.
  15711. When set to a number, use imagemagick (when available) to set
  15712. the image's width to this value.
  15713. When set to a number in a list, try to get the width from the
  15714. #+ATTR.* keyword if it matches a width specification like
  15715. width=\"[0-9]+\", and fall back on that number if none is found.
  15716. When set to nil, try to get the width from an #+ATTR.* keyword
  15717. and fall back on the original width if none is found.
  15718. This requires Emacs >= 24.1, build with imagemagick support."
  15719. :group 'org-appearance
  15720. :version "24.3"
  15721. :type '(choice
  15722. (const :tag "Use the image width" t)
  15723. (integer :tag "Use a number of pixels")
  15724. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15725. (const :tag "Use #+ATTR* or don't resize" nil)))
  15726. (defcustom org-agenda-inhibit-startup t
  15727. "Inhibit startup when preparing agenda buffers.
  15728. When this variable is `t' (the default), the initialization of
  15729. the Org agenda buffers is inhibited: e.g. the visibility state
  15730. is not set, the tables are not re-aligned, etc."
  15731. :type 'boolean
  15732. :version "24.3"
  15733. :group 'org-agenda)
  15734. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15735. "Convert a duration string S to minutes.
  15736. A bare number is interpreted as minutes, modifiers can be set by
  15737. customizing `org-effort-durations' (which see).
  15738. Entries containing a colon are interpreted as H:MM by
  15739. `org-hh:mm-string-to-minutes'."
  15740. (let ((result 0)
  15741. (re (concat "\\([0-9.]+\\) *\\("
  15742. (regexp-opt (mapcar 'car org-effort-durations))
  15743. "\\)")))
  15744. (while (string-match re s)
  15745. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15746. (string-to-number (match-string 1 s))))
  15747. (setq s (replace-match "" nil t s)))
  15748. (setq result (floor result))
  15749. (incf result (org-hh:mm-string-to-minutes s))
  15750. (if output-to-string (number-to-string result) result)))
  15751. ;;;; Files
  15752. (defun org-save-all-org-buffers ()
  15753. "Save all Org-mode buffers without user confirmation."
  15754. (interactive)
  15755. (message "Saving all Org-mode buffers...")
  15756. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15757. (when (featurep 'org-id) (org-id-locations-save))
  15758. (message "Saving all Org-mode buffers... done"))
  15759. (defun org-revert-all-org-buffers ()
  15760. "Revert all Org-mode buffers.
  15761. Prompt for confirmation when there are unsaved changes.
  15762. Be sure you know what you are doing before letting this function
  15763. overwrite your changes.
  15764. This function is useful in a setup where one tracks org files
  15765. with a version control system, to revert on one machine after pulling
  15766. changes from another. I believe the procedure must be like this:
  15767. 1. M-x org-save-all-org-buffers
  15768. 2. Pull changes from the other machine, resolve conflicts
  15769. 3. M-x org-revert-all-org-buffers"
  15770. (interactive)
  15771. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15772. (error "Abort"))
  15773. (save-excursion
  15774. (save-window-excursion
  15775. (mapc
  15776. (lambda (b)
  15777. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15778. (with-current-buffer b buffer-file-name))
  15779. (org-pop-to-buffer-same-window b)
  15780. (revert-buffer t 'no-confirm)))
  15781. (buffer-list))
  15782. (when (and (featurep 'org-id) org-id-track-globally)
  15783. (org-id-locations-load)))))
  15784. ;;;; Agenda files
  15785. ;;;###autoload
  15786. (defun org-switchb (&optional arg)
  15787. "Switch between Org buffers.
  15788. With one prefix argument, restrict available buffers to files.
  15789. With two prefix arguments, restrict available buffers to agenda files.
  15790. Defaults to `iswitchb' for buffer name completion.
  15791. Set `org-completion-use-ido' to make it use ido instead."
  15792. (interactive "P")
  15793. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15794. ((equal arg '(16)) (org-buffer-list 'agenda))
  15795. (t (org-buffer-list))))
  15796. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15797. (org-completion-use-ido org-completion-use-ido))
  15798. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15799. (setq org-completion-use-iswitchb t))
  15800. (org-pop-to-buffer-same-window
  15801. (org-icompleting-read "Org buffer: "
  15802. (mapcar 'list (mapcar 'buffer-name blist))
  15803. nil t))))
  15804. ;;; Define some older names previously used for this functionality
  15805. ;;;###autoload
  15806. (defalias 'org-ido-switchb 'org-switchb)
  15807. ;;;###autoload
  15808. (defalias 'org-iswitchb 'org-switchb)
  15809. (defun org-buffer-list (&optional predicate exclude-tmp)
  15810. "Return a list of Org buffers.
  15811. PREDICATE can be `export', `files' or `agenda'.
  15812. export restrict the list to Export buffers.
  15813. files restrict the list to buffers visiting Org files.
  15814. agenda restrict the list to buffers visiting agenda files.
  15815. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15816. (let* ((bfn nil)
  15817. (agenda-files (and (eq predicate 'agenda)
  15818. (mapcar 'file-truename (org-agenda-files t))))
  15819. (filter
  15820. (cond
  15821. ((eq predicate 'files)
  15822. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15823. ((eq predicate 'export)
  15824. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15825. ((eq predicate 'agenda)
  15826. (lambda (b)
  15827. (with-current-buffer b
  15828. (and (derived-mode-p 'org-mode)
  15829. (setq bfn (buffer-file-name b))
  15830. (member (file-truename bfn) agenda-files)))))
  15831. (t (lambda (b) (with-current-buffer b
  15832. (or (derived-mode-p 'org-mode)
  15833. (string-match "\*Org .*Export"
  15834. (buffer-name b)))))))))
  15835. (delq nil
  15836. (mapcar
  15837. (lambda(b)
  15838. (if (and (funcall filter b)
  15839. (or (not exclude-tmp)
  15840. (not (string-match "tmp" (buffer-name b)))))
  15841. b
  15842. nil))
  15843. (buffer-list)))))
  15844. (defun org-agenda-files (&optional unrestricted archives)
  15845. "Get the list of agenda files.
  15846. Optional UNRESTRICTED means return the full list even if a restriction
  15847. is currently in place.
  15848. When ARCHIVES is t, include all archive files that are really being
  15849. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15850. `org-agenda-archives-mode' is t."
  15851. (let ((files
  15852. (cond
  15853. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15854. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15855. ((listp org-agenda-files) org-agenda-files)
  15856. (t (error "Invalid value of `org-agenda-files'")))))
  15857. (setq files (apply 'append
  15858. (mapcar (lambda (f)
  15859. (if (file-directory-p f)
  15860. (directory-files
  15861. f t org-agenda-file-regexp)
  15862. (list f)))
  15863. files)))
  15864. (when org-agenda-skip-unavailable-files
  15865. (setq files (delq nil
  15866. (mapcar (function
  15867. (lambda (file)
  15868. (and (file-readable-p file) file)))
  15869. files))))
  15870. (when (or (eq archives t)
  15871. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15872. (setq files (org-add-archive-files files)))
  15873. files))
  15874. (defun org-agenda-file-p (&optional file)
  15875. "Return non-nil, if FILE is an agenda file.
  15876. If FILE is omitted, use the file associated with the current
  15877. buffer."
  15878. (member (or file (buffer-file-name))
  15879. (org-agenda-files t)))
  15880. (defun org-edit-agenda-file-list ()
  15881. "Edit the list of agenda files.
  15882. Depending on setup, this either uses customize to edit the variable
  15883. `org-agenda-files', or it visits the file that is holding the list. In the
  15884. latter case, the buffer is set up in a way that saving it automatically kills
  15885. the buffer and restores the previous window configuration."
  15886. (interactive)
  15887. (if (stringp org-agenda-files)
  15888. (let ((cw (current-window-configuration)))
  15889. (find-file org-agenda-files)
  15890. (org-set-local 'org-window-configuration cw)
  15891. (org-add-hook 'after-save-hook
  15892. (lambda ()
  15893. (set-window-configuration
  15894. (prog1 org-window-configuration
  15895. (kill-buffer (current-buffer))))
  15896. (org-install-agenda-files-menu)
  15897. (message "New agenda file list installed"))
  15898. nil 'local)
  15899. (message "%s" (substitute-command-keys
  15900. "Edit list and finish with \\[save-buffer]")))
  15901. (customize-variable 'org-agenda-files)))
  15902. (defun org-store-new-agenda-file-list (list)
  15903. "Set new value for the agenda file list and save it correctly."
  15904. (if (stringp org-agenda-files)
  15905. (let ((fe (org-read-agenda-file-list t)) b u)
  15906. (while (setq b (find-buffer-visiting org-agenda-files))
  15907. (kill-buffer b))
  15908. (with-temp-file org-agenda-files
  15909. (insert
  15910. (mapconcat
  15911. (lambda (f) ;; Keep un-expanded entries.
  15912. (if (setq u (assoc f fe))
  15913. (cdr u)
  15914. f))
  15915. list "\n")
  15916. "\n")))
  15917. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15918. (org-insert-mode-line-in-empty-file nil))
  15919. (setq org-agenda-files list)
  15920. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15921. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15922. "Read the list of agenda files from a file.
  15923. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15924. filenames, used by `org-store-new-agenda-file-list' to write back
  15925. un-expanded file names."
  15926. (when (file-directory-p org-agenda-files)
  15927. (error "`org-agenda-files' cannot be a single directory"))
  15928. (when (stringp org-agenda-files)
  15929. (with-temp-buffer
  15930. (insert-file-contents org-agenda-files)
  15931. (mapcar
  15932. (lambda (f)
  15933. (let ((e (expand-file-name (substitute-in-file-name f)
  15934. org-directory)))
  15935. (if pair-with-expansion
  15936. (cons e f)
  15937. e)))
  15938. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15939. ;;;###autoload
  15940. (defun org-cycle-agenda-files ()
  15941. "Cycle through the files in `org-agenda-files'.
  15942. If the current buffer visits an agenda file, find the next one in the list.
  15943. If the current buffer does not, find the first agenda file."
  15944. (interactive)
  15945. (let* ((fs (org-agenda-files t))
  15946. (files (append fs (list (car fs))))
  15947. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15948. file)
  15949. (unless files (error "No agenda files"))
  15950. (catch 'exit
  15951. (while (setq file (pop files))
  15952. (if (equal (file-truename file) tcf)
  15953. (when (car files)
  15954. (find-file (car files))
  15955. (throw 'exit t))))
  15956. (find-file (car fs)))
  15957. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15958. (defun org-agenda-file-to-front (&optional to-end)
  15959. "Move/add the current file to the top of the agenda file list.
  15960. If the file is not present in the list, it is added to the front. If it is
  15961. present, it is moved there. With optional argument TO-END, add/move to the
  15962. end of the list."
  15963. (interactive "P")
  15964. (let ((org-agenda-skip-unavailable-files nil)
  15965. (file-alist (mapcar (lambda (x)
  15966. (cons (file-truename x) x))
  15967. (org-agenda-files t)))
  15968. (ctf (file-truename
  15969. (or buffer-file-name
  15970. (error "Please save the current buffer to a file"))))
  15971. x had)
  15972. (setq x (assoc ctf file-alist) had x)
  15973. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15974. (if to-end
  15975. (setq file-alist (append (delq x file-alist) (list x)))
  15976. (setq file-alist (cons x (delq x file-alist))))
  15977. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15978. (org-install-agenda-files-menu)
  15979. (message "File %s to %s of agenda file list"
  15980. (if had "moved" "added") (if to-end "end" "front"))))
  15981. (defun org-remove-file (&optional file)
  15982. "Remove current file from the list of files in variable `org-agenda-files'.
  15983. These are the files which are being checked for agenda entries.
  15984. Optional argument FILE means use this file instead of the current."
  15985. (interactive)
  15986. (let* ((org-agenda-skip-unavailable-files nil)
  15987. (file (or file buffer-file-name
  15988. (error "Current buffer does not visit a file")))
  15989. (true-file (file-truename file))
  15990. (afile (abbreviate-file-name file))
  15991. (files (delq nil (mapcar
  15992. (lambda (x)
  15993. (if (equal true-file
  15994. (file-truename x))
  15995. nil x))
  15996. (org-agenda-files t)))))
  15997. (if (not (= (length files) (length (org-agenda-files t))))
  15998. (progn
  15999. (org-store-new-agenda-file-list files)
  16000. (org-install-agenda-files-menu)
  16001. (message "Removed file: %s" afile))
  16002. (message "File was not in list: %s (not removed)" afile))))
  16003. (defun org-file-menu-entry (file)
  16004. (vector file (list 'find-file file) t))
  16005. (defun org-check-agenda-file (file)
  16006. "Make sure FILE exists. If not, ask user what to do."
  16007. (when (not (file-exists-p file))
  16008. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16009. (abbreviate-file-name file))
  16010. (let ((r (downcase (read-char-exclusive))))
  16011. (cond
  16012. ((equal r ?r)
  16013. (org-remove-file file)
  16014. (throw 'nextfile t))
  16015. (t (error "Abort"))))))
  16016. (defun org-get-agenda-file-buffer (file)
  16017. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16018. it to the list of buffers which might be released later."
  16019. (let ((buf (org-find-base-buffer-visiting file)))
  16020. (if buf
  16021. buf ; just return it
  16022. ;; Make a new buffer and remember it
  16023. (setq buf (find-file-noselect file))
  16024. (if buf (push buf org-agenda-new-buffers))
  16025. buf)))
  16026. (defun org-release-buffers (blist)
  16027. "Release all buffers in list, asking the user for confirmation when needed.
  16028. When a buffer is unmodified, it is just killed. When modified, it is saved
  16029. \(if the user agrees) and then killed."
  16030. (let (buf file)
  16031. (while (setq buf (pop blist))
  16032. (setq file (buffer-file-name buf))
  16033. (when (and (buffer-modified-p buf)
  16034. file
  16035. (y-or-n-p (format "Save file %s? " file)))
  16036. (with-current-buffer buf (save-buffer)))
  16037. (kill-buffer buf))))
  16038. (defun org-agenda-prepare-buffers (files)
  16039. "Create buffers for all agenda files, protect archived trees and comments."
  16040. (interactive)
  16041. (let ((pa '(:org-archived t))
  16042. (pc '(:org-comment t))
  16043. (pall '(:org-archived t :org-comment t))
  16044. (inhibit-read-only t)
  16045. (org-inhibit-startup org-agenda-inhibit-startup)
  16046. (rea (concat ":" org-archive-tag ":"))
  16047. file re)
  16048. (save-excursion
  16049. (save-restriction
  16050. (while (setq file (pop files))
  16051. (catch 'nextfile
  16052. (if (bufferp file)
  16053. (set-buffer file)
  16054. (org-check-agenda-file file)
  16055. (set-buffer (org-get-agenda-file-buffer file)))
  16056. (widen)
  16057. (org-unmodified
  16058. (org-refresh-category-properties)
  16059. (org-refresh-properties org-effort-property 'org-effort)
  16060. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16061. (setq org-todo-keywords-for-agenda
  16062. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16063. (setq org-done-keywords-for-agenda
  16064. (append org-done-keywords-for-agenda org-done-keywords))
  16065. (setq org-todo-keyword-alist-for-agenda
  16066. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16067. (setq org-drawers-for-agenda
  16068. (append org-drawers-for-agenda org-drawers))
  16069. (setq org-tag-alist-for-agenda
  16070. (append org-tag-alist-for-agenda org-tag-alist))
  16071. (save-excursion
  16072. (remove-text-properties (point-min) (point-max) pall)
  16073. (when org-agenda-skip-archived-trees
  16074. (goto-char (point-min))
  16075. (while (re-search-forward rea nil t)
  16076. (if (org-at-heading-p t)
  16077. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16078. (goto-char (point-min))
  16079. (setq re (format org-heading-keyword-regexp-format
  16080. org-comment-string))
  16081. (while (re-search-forward re nil t)
  16082. (add-text-properties
  16083. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16084. (setq org-todo-keywords-for-agenda
  16085. (org-uniquify org-todo-keywords-for-agenda))
  16086. (setq org-todo-keyword-alist-for-agenda
  16087. (org-uniquify org-todo-keyword-alist-for-agenda)
  16088. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16089. ;;;; CDLaTeX minor mode
  16090. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16091. "Keymap for the minor `org-cdlatex-mode'.")
  16092. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16093. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16094. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16095. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16096. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16097. (defvar org-cdlatex-texmathp-advice-is-done nil
  16098. "Flag remembering if we have applied the advice to texmathp already.")
  16099. (define-minor-mode org-cdlatex-mode
  16100. "Toggle the minor `org-cdlatex-mode'.
  16101. This mode supports entering LaTeX environment and math in LaTeX fragments
  16102. in Org-mode.
  16103. \\{org-cdlatex-mode-map}"
  16104. nil " OCDL" nil
  16105. (when org-cdlatex-mode
  16106. (require 'cdlatex)
  16107. (run-hooks 'cdlatex-mode-hook)
  16108. (cdlatex-compute-tables))
  16109. (unless org-cdlatex-texmathp-advice-is-done
  16110. (setq org-cdlatex-texmathp-advice-is-done t)
  16111. (defadvice texmathp (around org-math-always-on activate)
  16112. "Always return t in org-mode buffers.
  16113. This is because we want to insert math symbols without dollars even outside
  16114. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16115. an embedded LaTeX fragment, let texmathp do its job.
  16116. \\[org-cdlatex-mode-map]"
  16117. (interactive)
  16118. (let (p)
  16119. (cond
  16120. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16121. ((eq this-command 'cdlatex-math-symbol)
  16122. (setq ad-return-value t
  16123. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16124. (t
  16125. (let ((p (org-inside-LaTeX-fragment-p)))
  16126. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16127. (setq ad-return-value t
  16128. texmathp-why '("Org-mode embedded math" . 0))
  16129. (if p ad-do-it)))))))))
  16130. (defun turn-on-org-cdlatex ()
  16131. "Unconditionally turn on `org-cdlatex-mode'."
  16132. (org-cdlatex-mode 1))
  16133. (defun org-try-cdlatex-tab ()
  16134. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16135. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16136. - inside a LaTeX fragment, or
  16137. - after the first word in a line, where an abbreviation expansion could
  16138. insert a LaTeX environment."
  16139. (when org-cdlatex-mode
  16140. (cond
  16141. ;; Before any word on the line: No expansion possible.
  16142. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16143. ;; Just after first word on the line: Expand it. Make sure it
  16144. ;; cannot happen on headlines, though.
  16145. ((save-excursion
  16146. (skip-chars-backward "a-zA-Z0-9*")
  16147. (skip-chars-backward " \t")
  16148. (and (bolp) (not (org-at-heading-p))))
  16149. (cdlatex-tab) t)
  16150. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16151. (defun org-cdlatex-underscore-caret (&optional arg)
  16152. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16153. Revert to the normal definition outside of these fragments."
  16154. (interactive "P")
  16155. (if (org-inside-LaTeX-fragment-p)
  16156. (call-interactively 'cdlatex-sub-superscript)
  16157. (let (org-cdlatex-mode)
  16158. (call-interactively (key-binding (vector last-input-event))))))
  16159. (defun org-cdlatex-math-modify (&optional arg)
  16160. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16161. Revert to the normal definition outside of these fragments."
  16162. (interactive "P")
  16163. (if (org-inside-LaTeX-fragment-p)
  16164. (call-interactively 'cdlatex-math-modify)
  16165. (let (org-cdlatex-mode)
  16166. (call-interactively (key-binding (vector last-input-event))))))
  16167. ;;;; LaTeX fragments
  16168. (defvar org-latex-regexps
  16169. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16170. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16171. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16172. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16173. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16174. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16175. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16176. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16177. "Regular expressions for matching embedded LaTeX.")
  16178. (defun org-inside-LaTeX-fragment-p ()
  16179. "Test if point is inside a LaTeX fragment.
  16180. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16181. sequence appearing also before point.
  16182. Even though the matchers for math are configurable, this function assumes
  16183. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16184. delimiters are skipped when they have been removed by customization.
  16185. The return value is nil, or a cons cell with the delimiter and the
  16186. position of this delimiter.
  16187. This function does a reasonably good job, but can locally be fooled by
  16188. for example currency specifications. For example it will assume being in
  16189. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16190. fragments that are properly closed, but during editing, we have to live
  16191. with the uncertainty caused by missing closing delimiters. This function
  16192. looks only before point, not after."
  16193. (catch 'exit
  16194. (let ((pos (point))
  16195. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16196. (lim (progn
  16197. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16198. (point)))
  16199. dd-on str (start 0) m re)
  16200. (goto-char pos)
  16201. (when dodollar
  16202. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16203. re (nth 1 (assoc "$" org-latex-regexps)))
  16204. (while (string-match re str start)
  16205. (cond
  16206. ((= (match-end 0) (length str))
  16207. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16208. ((= (match-end 0) (- (length str) 5))
  16209. (throw 'exit nil))
  16210. (t (setq start (match-end 0))))))
  16211. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16212. (goto-char pos)
  16213. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16214. (and (match-beginning 2) (throw 'exit nil))
  16215. ;; count $$
  16216. (while (re-search-backward "\\$\\$" lim t)
  16217. (setq dd-on (not dd-on)))
  16218. (goto-char pos)
  16219. (if dd-on (cons "$$" m))))))
  16220. (defun org-inside-latex-macro-p ()
  16221. "Is point inside a LaTeX macro or its arguments?"
  16222. (save-match-data
  16223. (org-in-regexp
  16224. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16225. (defvar org-latex-fragment-image-overlays nil
  16226. "List of overlays carrying the images of latex fragments.")
  16227. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16228. (defun org-remove-latex-fragment-image-overlays ()
  16229. "Remove all overlays with LaTeX fragment images in current buffer."
  16230. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16231. (setq org-latex-fragment-image-overlays nil))
  16232. (defun org-preview-latex-fragment (&optional subtree)
  16233. "Preview the LaTeX fragment at point, or all locally or globally.
  16234. If the cursor is in a LaTeX fragment, create the image and overlay
  16235. it over the source code. If there is no fragment at point, display
  16236. all fragments in the current text, from one headline to the next. With
  16237. prefix SUBTREE, display all fragments in the current subtree. With a
  16238. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16239. the cursor is before the first headline,
  16240. display all fragments in the buffer.
  16241. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16242. (interactive "P")
  16243. (unless buffer-file-name
  16244. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16245. (org-remove-latex-fragment-image-overlays)
  16246. (save-excursion
  16247. (save-restriction
  16248. (let (beg end at msg)
  16249. (cond
  16250. ((or (equal subtree '(16))
  16251. (not (save-excursion
  16252. (re-search-backward org-outline-regexp-bol nil t))))
  16253. (setq beg (point-min) end (point-max)
  16254. msg "Creating images for buffer...%s"))
  16255. ((equal subtree '(4))
  16256. (org-back-to-heading)
  16257. (setq beg (point) end (org-end-of-subtree t)
  16258. msg "Creating images for subtree...%s"))
  16259. (t
  16260. (if (setq at (org-inside-LaTeX-fragment-p))
  16261. (goto-char (max (point-min) (- (cdr at) 2)))
  16262. (org-back-to-heading))
  16263. (setq beg (point) end (progn (outline-next-heading) (point))
  16264. msg (if at "Creating image...%s"
  16265. "Creating images for entry...%s"))))
  16266. (message msg "")
  16267. (narrow-to-region beg end)
  16268. (goto-char beg)
  16269. (org-format-latex
  16270. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16271. (file-name-nondirectory
  16272. buffer-file-name)))
  16273. default-directory 'overlays msg at 'forbuffer
  16274. org-latex-create-formula-image-program)
  16275. (message msg "done. Use `C-c C-c' to remove images.")))))
  16276. (defun org-format-latex (prefix &optional dir overlays msg at
  16277. forbuffer processing-type)
  16278. "Replace LaTeX fragments with links to an image, and produce images.
  16279. Some of the options can be changed using the variable
  16280. `org-format-latex-options'."
  16281. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16282. (let* ((prefixnodir (file-name-nondirectory prefix))
  16283. (absprefix (expand-file-name prefix dir))
  16284. (todir (file-name-directory absprefix))
  16285. (opt org-format-latex-options)
  16286. (optnew org-format-latex-options)
  16287. (matchers (plist-get opt :matchers))
  16288. (re-list org-latex-regexps)
  16289. (cnt 0) txt hash link beg end re e checkdir
  16290. string
  16291. m n block-type block linkfile movefile ov)
  16292. ;; Check the different regular expressions
  16293. (while (setq e (pop re-list))
  16294. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16295. block (if block-type "\n\n" ""))
  16296. (when (member m matchers)
  16297. (goto-char (point-min))
  16298. (while (re-search-forward re nil t)
  16299. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16300. (or (not overlays)
  16301. (not (eq (get-char-property (match-beginning n)
  16302. 'org-overlay-type)
  16303. 'org-latex-overlay))))
  16304. (cond
  16305. ((eq processing-type 'verbatim))
  16306. ((eq processing-type 'mathjax)
  16307. ;; Prepare for MathJax processing.
  16308. (setq string (match-string n))
  16309. (when (member m '("$" "$1"))
  16310. (save-excursion
  16311. (delete-region (match-beginning n) (match-end n))
  16312. (goto-char (match-beginning n))
  16313. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16314. ((or (eq processing-type 'dvipng)
  16315. (eq processing-type 'imagemagick))
  16316. ;; Process to an image.
  16317. (setq txt (match-string n)
  16318. beg (match-beginning n) end (match-end n)
  16319. cnt (1+ cnt))
  16320. (let ((face (face-at-point))
  16321. (fg (plist-get opt :foreground))
  16322. (bg (plist-get opt :background))
  16323. ;; Ensure full list is printed.
  16324. print-length print-level)
  16325. (when forbuffer
  16326. ;; Get the colors from the face at point.
  16327. (goto-char beg)
  16328. (when (eq fg 'auto)
  16329. (setq fg (face-attribute face :foreground nil 'default)))
  16330. (when (eq bg 'auto)
  16331. (setq bg (face-attribute face :background nil 'default)))
  16332. (setq optnew (copy-sequence opt))
  16333. (plist-put optnew :foreground fg)
  16334. (plist-put optnew :background bg))
  16335. (setq hash (sha1 (prin1-to-string
  16336. (list org-format-latex-header
  16337. org-latex-default-packages-alist
  16338. org-latex-packages-alist
  16339. org-format-latex-options
  16340. forbuffer txt fg bg)))
  16341. linkfile (format "%s_%s.png" prefix hash)
  16342. movefile (format "%s_%s.png" absprefix hash)))
  16343. (setq link (concat block "[[file:" linkfile "]]" block))
  16344. (if msg (message msg cnt))
  16345. (goto-char beg)
  16346. (unless checkdir ; Ensure the directory exists.
  16347. (setq checkdir t)
  16348. (or (file-directory-p todir) (make-directory todir t)))
  16349. (unless (file-exists-p movefile)
  16350. (org-create-formula-image
  16351. txt movefile optnew forbuffer processing-type))
  16352. (if overlays
  16353. (progn
  16354. (mapc (lambda (o)
  16355. (if (eq (overlay-get o 'org-overlay-type)
  16356. 'org-latex-overlay)
  16357. (delete-overlay o)))
  16358. (overlays-in beg end))
  16359. (setq ov (make-overlay beg end))
  16360. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16361. (if (featurep 'xemacs)
  16362. (progn
  16363. (overlay-put ov 'invisible t)
  16364. (overlay-put
  16365. ov 'end-glyph
  16366. (make-glyph (vector 'png :file movefile))))
  16367. (overlay-put
  16368. ov 'display
  16369. (list 'image :type 'png :file movefile :ascent 'center)))
  16370. (push ov org-latex-fragment-image-overlays)
  16371. (goto-char end))
  16372. (delete-region beg end)
  16373. (insert (org-add-props link
  16374. (list 'org-latex-src
  16375. (replace-regexp-in-string
  16376. "\"" "" txt)
  16377. 'org-latex-src-embed-type
  16378. (if block-type 'paragraph 'character))))))
  16379. ((eq processing-type 'mathml)
  16380. ;; Process to MathML
  16381. (unless (save-match-data (org-format-latex-mathml-available-p))
  16382. (error "LaTeX to MathML converter not configured"))
  16383. (setq txt (match-string n)
  16384. beg (match-beginning n) end (match-end n)
  16385. cnt (1+ cnt))
  16386. (if msg (message msg cnt))
  16387. (goto-char beg)
  16388. (delete-region beg end)
  16389. (insert (org-format-latex-as-mathml
  16390. txt block-type prefix dir)))
  16391. (t
  16392. (error "Unknown conversion type %s for latex fragments"
  16393. processing-type)))))))))
  16394. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16395. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16396. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16397. sucessful, return the portion between \"<math...> </math>\"
  16398. elements otherwise return nil. When MATHML-FILE is specified,
  16399. write the results in to that file. When invoked as an
  16400. interactive command, prompt for LATEX-FRAG, with initial value
  16401. set to the current active region and echo the results for user
  16402. inspection."
  16403. (interactive (list (let ((frag (when (org-region-active-p)
  16404. (buffer-substring-no-properties
  16405. (region-beginning) (region-end)))))
  16406. (read-string "LaTeX Fragment: " frag nil frag))))
  16407. (unless latex-frag (error "Invalid latex-frag"))
  16408. (let* ((tmp-in-file (file-relative-name
  16409. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16410. (ignore (write-region latex-frag nil tmp-in-file))
  16411. (tmp-out-file (file-relative-name
  16412. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16413. (cmd (format-spec
  16414. org-latex-to-mathml-convert-command
  16415. `((?j . ,(shell-quote-argument
  16416. (expand-file-name org-latex-to-mathml-jar-file)))
  16417. (?I . ,(shell-quote-argument tmp-in-file))
  16418. (?o . ,(shell-quote-argument tmp-out-file)))))
  16419. mathml shell-command-output)
  16420. (when (org-called-interactively-p 'any)
  16421. (unless (org-format-latex-mathml-available-p)
  16422. (error "LaTeX to MathML converter not configured")))
  16423. (message "Running %s" cmd)
  16424. (setq shell-command-output (shell-command-to-string cmd))
  16425. (setq mathml
  16426. (when (file-readable-p tmp-out-file)
  16427. (with-current-buffer (find-file-noselect tmp-out-file t)
  16428. (goto-char (point-min))
  16429. (when (re-search-forward
  16430. (concat
  16431. (regexp-quote
  16432. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16433. "\\(.\\|\n\\)*"
  16434. (regexp-quote "</math>")) nil t)
  16435. (prog1 (match-string 0) (kill-buffer))))))
  16436. (cond
  16437. (mathml
  16438. (setq mathml
  16439. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16440. (when mathml-file
  16441. (write-region mathml nil mathml-file))
  16442. (when (org-called-interactively-p 'any)
  16443. (message mathml)))
  16444. ((message "LaTeX to MathML conversion failed")
  16445. (message shell-command-output)))
  16446. (delete-file tmp-in-file)
  16447. (when (file-exists-p tmp-out-file)
  16448. (delete-file tmp-out-file))
  16449. mathml))
  16450. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16451. prefix &optional dir)
  16452. "Use `org-create-math-formula' but check local cache first."
  16453. (let* ((absprefix (expand-file-name prefix dir))
  16454. (print-length nil) (print-level nil)
  16455. (formula-id (concat
  16456. "formula-"
  16457. (sha1
  16458. (prin1-to-string
  16459. (list latex-frag
  16460. org-latex-to-mathml-convert-command)))))
  16461. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16462. (formula-cache-dir (file-name-directory formula-cache)))
  16463. (unless (file-directory-p formula-cache-dir)
  16464. (make-directory formula-cache-dir t))
  16465. (unless (file-exists-p formula-cache)
  16466. (org-create-math-formula latex-frag formula-cache))
  16467. (if (file-exists-p formula-cache)
  16468. ;; Successful conversion. Return the link to MathML file.
  16469. (org-add-props
  16470. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16471. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16472. 'org-latex-src-embed-type (if latex-frag-type
  16473. 'paragraph 'character)))
  16474. ;; Failed conversion. Return the LaTeX fragment verbatim
  16475. latex-frag)))
  16476. (defun org-create-formula-image (string tofile options buffer &optional type)
  16477. "Create an image from LaTeX source using dvipng or convert.
  16478. This function calls either `org-create-formula-image-with-dvipng'
  16479. or `org-create-formula-image-with-imagemagick' depending on the
  16480. value of `org-latex-create-formula-image-program' or on the value
  16481. of the optional TYPE variable.
  16482. Note: ultimately these two function should be combined as they
  16483. share a good deal of logic."
  16484. (org-check-external-command
  16485. "latex" "needed to convert LaTeX fragments to images")
  16486. (funcall
  16487. (case (or type org-latex-create-formula-image-program)
  16488. ('dvipng
  16489. (org-check-external-command
  16490. "dvipng" "needed to convert LaTeX fragments to images")
  16491. #'org-create-formula-image-with-dvipng)
  16492. ('imagemagick
  16493. (org-check-external-command
  16494. "convert" "you need to install imagemagick")
  16495. #'org-create-formula-image-with-imagemagick)
  16496. (t (error
  16497. "invalid value of `org-latex-create-formula-image-program'")))
  16498. string tofile options buffer))
  16499. ;; This function borrows from Ganesh Swami's latex2png.el
  16500. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16501. "This calls dvipng."
  16502. (let* ((tmpdir (if (featurep 'xemacs)
  16503. (temp-directory)
  16504. temporary-file-directory))
  16505. (texfilebase (make-temp-name
  16506. (expand-file-name "orgtex" tmpdir)))
  16507. (texfile (concat texfilebase ".tex"))
  16508. (dvifile (concat texfilebase ".dvi"))
  16509. (pngfile (concat texfilebase ".png"))
  16510. (fnh (if (featurep 'xemacs)
  16511. (font-height (face-font 'default))
  16512. (face-attribute 'default :height nil)))
  16513. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16514. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16515. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16516. "Black"))
  16517. (bg (or (plist-get options (if buffer :background :html-background))
  16518. "Transparent")))
  16519. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16520. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16521. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16522. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16523. (with-temp-file texfile
  16524. (require 'ox-latex)
  16525. (insert (org-latex-guess-inputenc
  16526. (org-splice-latex-header
  16527. org-format-latex-header
  16528. org-latex-default-packages-alist
  16529. org-latex-packages-alist t)))
  16530. (insert "\n\\begin{document}\n" string "\n\\end{document}\n"))
  16531. (let ((dir default-directory))
  16532. (condition-case nil
  16533. (progn
  16534. (cd tmpdir)
  16535. (call-process "latex" nil nil nil texfile))
  16536. (error nil))
  16537. (cd dir))
  16538. (if (not (file-exists-p dvifile))
  16539. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16540. (condition-case nil
  16541. (if (featurep 'xemacs)
  16542. (call-process "dvipng" nil nil nil
  16543. "-fg" fg "-bg" bg
  16544. "-T" "tight"
  16545. "-o" pngfile
  16546. dvifile)
  16547. (call-process "dvipng" nil nil nil
  16548. "-fg" fg "-bg" bg
  16549. "-D" dpi
  16550. ;;"-x" scale "-y" scale
  16551. "-T" "tight"
  16552. "-o" pngfile
  16553. dvifile))
  16554. (error nil))
  16555. (if (not (file-exists-p pngfile))
  16556. (if org-format-latex-signal-error
  16557. (error "Failed to create png file from %s" texfile)
  16558. (message "Failed to create png file from %s" texfile)
  16559. nil)
  16560. ;; Use the requested file name and clean up
  16561. (copy-file pngfile tofile 'replace)
  16562. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16563. (if (file-exists-p (concat texfilebase e))
  16564. (delete-file (concat texfilebase e))))
  16565. pngfile))))
  16566. (defvar org-latex-pdf-process) ; From ox-latex.el
  16567. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16568. "This calls convert, which is included into imagemagick."
  16569. (let* ((tmpdir (if (featurep 'xemacs)
  16570. (temp-directory)
  16571. temporary-file-directory))
  16572. (texfilebase (make-temp-name
  16573. (expand-file-name "orgtex" tmpdir)))
  16574. (texfile (concat texfilebase ".tex"))
  16575. (pdffile (concat texfilebase ".pdf"))
  16576. (pngfile (concat texfilebase ".png"))
  16577. (fnh (if (featurep 'xemacs)
  16578. (font-height (face-font 'default))
  16579. (face-attribute 'default :height nil)))
  16580. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16581. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16582. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16583. "black"))
  16584. (bg (or (plist-get options (if buffer :background :html-background))
  16585. "white")))
  16586. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16587. (setq fg (org-latex-color-format fg)))
  16588. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16589. (setq bg (org-latex-color-format
  16590. (if (string= bg "Transparent") "white" bg))))
  16591. (with-temp-file texfile
  16592. (require 'ox-latex)
  16593. (insert (org-latex-guess-inputenc
  16594. (org-splice-latex-header
  16595. org-format-latex-header
  16596. org-latex-default-packages-alist
  16597. org-latex-packages-alist t)))
  16598. (insert "\n\\begin{document}\n"
  16599. "\\definecolor{fg}{rgb}{" fg "}\n"
  16600. "\\definecolor{bg}{rgb}{" bg "}\n"
  16601. "\n\\pagecolor{bg}\n"
  16602. "\n{\\color{fg}\n"
  16603. string
  16604. "\n}\n"
  16605. "\n\\end{document}\n"))
  16606. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16607. (condition-case nil
  16608. (progn
  16609. (cd tmpdir)
  16610. (setq cmds org-latex-pdf-process)
  16611. (while cmds
  16612. (setq latex-frags-cmds (pop cmds))
  16613. (if (listp latex-frags-cmds)
  16614. (setq cmds nil)
  16615. (setq latex-frags-cmds (list (car org-latex-pdf-process)))))
  16616. (while latex-frags-cmds
  16617. (setq cmd (pop latex-frags-cmds))
  16618. (while (string-match "%b" cmd)
  16619. (setq cmd (replace-match
  16620. (save-match-data
  16621. (shell-quote-argument texfile))
  16622. t t cmd)))
  16623. (while (string-match "%f" cmd)
  16624. (setq cmd (replace-match
  16625. (save-match-data
  16626. (shell-quote-argument
  16627. (file-name-nondirectory texfile)))
  16628. t t cmd)))
  16629. (while (string-match "%o" cmd)
  16630. (setq cmd (replace-match
  16631. (save-match-data
  16632. (shell-quote-argument
  16633. (file-name-directory texfile)))
  16634. t t cmd)))
  16635. (setq cmd (split-string cmd))
  16636. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16637. (error nil))
  16638. (cd dir))
  16639. (if (not (file-exists-p pdffile))
  16640. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16641. (condition-case nil
  16642. (if (featurep 'xemacs)
  16643. (call-process "convert" nil nil nil
  16644. "-density" "96"
  16645. "-trim"
  16646. "-antialias"
  16647. pdffile
  16648. "-quality" "100"
  16649. ;; "-sharpen" "0x1.0"
  16650. pngfile)
  16651. (call-process "convert" nil nil nil
  16652. "-density" dpi
  16653. "-trim"
  16654. "-antialias"
  16655. pdffile
  16656. "-quality" "100"
  16657. ;; "-sharpen" "0x1.0"
  16658. pngfile))
  16659. (error nil))
  16660. (if (not (file-exists-p pngfile))
  16661. (if org-format-latex-signal-error
  16662. (error "Failed to create png file from %s" texfile)
  16663. (message "Failed to create png file from %s" texfile)
  16664. nil)
  16665. ;; Use the requested file name and clean up
  16666. (copy-file pngfile tofile 'replace)
  16667. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16668. (if (file-exists-p (concat texfilebase e))
  16669. (delete-file (concat texfilebase e))))
  16670. pngfile))))
  16671. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16672. "Fill a LaTeX header template TPL.
  16673. In the template, the following place holders will be recognized:
  16674. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16675. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16676. [PACKAGES] \\usepackage statements for PKG
  16677. [NO-PACKAGES] do not include PKG
  16678. [EXTRA] the string EXTRA
  16679. [NO-EXTRA] do not include EXTRA
  16680. For backward compatibility, if both the positive and the negative place
  16681. holder is missing, the positive one (without the \"NO-\") will be
  16682. assumed to be present at the end of the template.
  16683. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16684. EXTRA is a string.
  16685. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16686. (let (rpl (end ""))
  16687. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16688. (setq rpl (if (or (match-end 1) (not def-pkg))
  16689. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16690. tpl (replace-match rpl t t tpl))
  16691. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16692. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16693. (setq rpl (if (or (match-end 1) (not pkg))
  16694. "" (org-latex-packages-to-string pkg snippets-p t))
  16695. tpl (replace-match rpl t t tpl))
  16696. (if pkg (setq end
  16697. (concat end "\n"
  16698. (org-latex-packages-to-string pkg snippets-p)))))
  16699. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16700. (setq rpl (if (or (match-end 1) (not extra))
  16701. "" (concat extra "\n"))
  16702. tpl (replace-match rpl t t tpl))
  16703. (if (and extra (string-match "\\S-" extra))
  16704. (setq end (concat end "\n" extra))))
  16705. (if (string-match "\\S-" end)
  16706. (concat tpl "\n" end)
  16707. tpl)))
  16708. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16709. "Turn an alist of packages into a string with the \\usepackage macros."
  16710. (setq pkg (mapconcat (lambda(p)
  16711. (cond
  16712. ((stringp p) p)
  16713. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16714. (format "%% Package %s omitted" (cadr p)))
  16715. ((equal "" (car p))
  16716. (format "\\usepackage{%s}" (cadr p)))
  16717. (t
  16718. (format "\\usepackage[%s]{%s}"
  16719. (car p) (cadr p)))))
  16720. pkg
  16721. "\n"))
  16722. (if newline (concat pkg "\n") pkg))
  16723. (defun org-dvipng-color (attr)
  16724. "Return a RGB color specification for dvipng."
  16725. (apply 'format "rgb %s %s %s"
  16726. (mapcar 'org-normalize-color
  16727. (if (featurep 'xemacs)
  16728. (color-rgb-components
  16729. (face-property 'default
  16730. (cond ((eq attr :foreground) 'foreground)
  16731. ((eq attr :background) 'background))))
  16732. (color-values (face-attribute 'default attr nil))))))
  16733. (defun org-dvipng-color-format (color-name)
  16734. "Convert COLOR-NAME to a RGB color value for dvipng."
  16735. (apply 'format "rgb %s %s %s"
  16736. (mapcar 'org-normalize-color
  16737. (color-values color-name))))
  16738. (defun org-latex-color (attr)
  16739. "Return a RGB color for the LaTeX color package."
  16740. (apply 'format "%s,%s,%s"
  16741. (mapcar 'org-normalize-color
  16742. (if (featurep 'xemacs)
  16743. (color-rgb-components
  16744. (face-property 'default
  16745. (cond ((eq attr :foreground) 'foreground)
  16746. ((eq attr :background) 'background))))
  16747. (color-values (face-attribute 'default attr nil))))))
  16748. (defun org-latex-color-format (color-name)
  16749. "Convert COLOR-NAME to a RGB color value."
  16750. (apply 'format "%s,%s,%s"
  16751. (mapcar 'org-normalize-color
  16752. (color-values color-name))))
  16753. (defun org-normalize-color (value)
  16754. "Return string to be used as color value for an RGB component."
  16755. (format "%g" (/ value 65535.0)))
  16756. ;; Image display
  16757. (defvar org-inline-image-overlays nil)
  16758. (make-variable-buffer-local 'org-inline-image-overlays)
  16759. (defun org-toggle-inline-images (&optional include-linked)
  16760. "Toggle the display of inline images.
  16761. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16762. (interactive "P")
  16763. (if org-inline-image-overlays
  16764. (progn
  16765. (org-remove-inline-images)
  16766. (message "Inline image display turned off"))
  16767. (org-display-inline-images include-linked)
  16768. (if (and (org-called-interactively-p)
  16769. org-inline-image-overlays)
  16770. (message "%d images displayed inline"
  16771. (length org-inline-image-overlays))
  16772. (message "No images to display inline"))))
  16773. (defun org-redisplay-inline-images ()
  16774. "Refresh the display of inline images."
  16775. (interactive)
  16776. (if (not org-inline-image-overlays)
  16777. (org-toggle-inline-images)
  16778. (org-toggle-inline-images)
  16779. (org-toggle-inline-images)))
  16780. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16781. "Display inline images.
  16782. Normally only links without a description part are inlined, because this
  16783. is how it will work for export. When INCLUDE-LINKED is set, also links
  16784. with a description part will be inlined. This can be nice for a quick
  16785. look at those images, but it does not reflect what exported files will look
  16786. like.
  16787. When REFRESH is set, refresh existing images between BEG and END.
  16788. This will create new image displays only if necessary.
  16789. BEG and END default to the buffer boundaries."
  16790. (interactive "P")
  16791. (unless refresh
  16792. (org-remove-inline-images)
  16793. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16794. (save-excursion
  16795. (save-restriction
  16796. (widen)
  16797. (setq beg (or beg (point-min)) end (or end (point-max)))
  16798. (goto-char beg)
  16799. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16800. (substring (org-image-file-name-regexp) 0 -2)
  16801. "\\)\\]" (if include-linked "" "\\]")))
  16802. old file ov img type attrwidth width)
  16803. (while (re-search-forward re end t)
  16804. (setq old (get-char-property-and-overlay (match-beginning 1)
  16805. 'org-image-overlay)
  16806. file (expand-file-name
  16807. (concat (or (match-string 3) "") (match-string 4))))
  16808. (when (image-type-available-p 'imagemagick)
  16809. (setq attrwidth (if (or (listp org-image-actual-width)
  16810. (null org-image-actual-width))
  16811. (save-excursion
  16812. (save-match-data
  16813. (when (re-search-backward
  16814. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16815. (save-excursion
  16816. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16817. (string-to-number (match-string 1))))))
  16818. width (cond ((eq org-image-actual-width t) nil)
  16819. ((null org-image-actual-width) attrwidth)
  16820. ((numberp org-image-actual-width)
  16821. org-image-actual-width)
  16822. ((listp org-image-actual-width)
  16823. (or attrwidth (car org-image-actual-width))))
  16824. type (if width 'imagemagick)))
  16825. (when (file-exists-p file)
  16826. (if (and (car-safe old) refresh)
  16827. (image-refresh (overlay-get (cdr old) 'display))
  16828. (setq img (save-match-data (create-image file type nil :width width)))
  16829. (when img
  16830. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16831. (overlay-put ov 'display img)
  16832. (overlay-put ov 'face 'default)
  16833. (overlay-put ov 'org-image-overlay t)
  16834. (overlay-put ov 'modification-hooks
  16835. (list 'org-display-inline-remove-overlay))
  16836. (push ov org-inline-image-overlays)))))))))
  16837. (define-obsolete-function-alias
  16838. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16839. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16840. "Remove inline-display overlay if a corresponding region is modified."
  16841. (let ((inhibit-modification-hooks t))
  16842. (when (and ov after)
  16843. (delete ov org-inline-image-overlays)
  16844. (delete-overlay ov))))
  16845. (defun org-remove-inline-images ()
  16846. "Remove inline display of images."
  16847. (interactive)
  16848. (mapc 'delete-overlay org-inline-image-overlays)
  16849. (setq org-inline-image-overlays nil))
  16850. ;;;; Key bindings
  16851. ;; Outline functions from `outline-mode-prefix-map'
  16852. ;; that can be remapped in Org:
  16853. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16854. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16855. (define-key org-mode-map [remap outline-forward-same-level]
  16856. 'org-forward-heading-same-level)
  16857. (define-key org-mode-map [remap outline-backward-same-level]
  16858. 'org-backward-heading-same-level)
  16859. (define-key org-mode-map [remap show-branches]
  16860. 'org-kill-note-or-show-branches)
  16861. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16862. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16863. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16864. ;; Outline functions from `outline-mode-prefix-map' that can not
  16865. ;; be remapped in Org:
  16866. ;;
  16867. ;; - the column "key binding" shows whether the Outline function is still
  16868. ;; available in Org mode on the same key that it has been bound to in
  16869. ;; Outline mode:
  16870. ;; - "overridden": key used for a different functionality in Org mode
  16871. ;; - else: key still bound to the same Outline function in Org mode
  16872. ;;
  16873. ;; | Outline function | key binding | Org replacement |
  16874. ;; |------------------------------------+-------------+-----------------------|
  16875. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16876. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16877. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16878. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16879. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16880. ;; | `show-entry' | overridden | no replacement |
  16881. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16882. ;; | `show-branches' | `C-c C-k' | still same function |
  16883. ;; | `show-subtree' | overridden | visibility cycling |
  16884. ;; | `show-all' | overridden | no replacement |
  16885. ;; | `hide-subtree' | overridden | visibility cycling |
  16886. ;; | `hide-body' | overridden | no replacement |
  16887. ;; | `hide-entry' | overridden | visibility cycling |
  16888. ;; | `hide-leaves' | overridden | no replacement |
  16889. ;; | `hide-sublevels' | overridden | no replacement |
  16890. ;; | `hide-other' | overridden | no replacement |
  16891. ;; Make `C-c C-x' a prefix key
  16892. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16893. ;; TAB key with modifiers
  16894. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16895. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16896. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16897. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16898. ;; The following line is necessary under Suse GNU/Linux
  16899. (unless (featurep 'xemacs)
  16900. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16901. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16902. (define-key org-mode-map [backtab] 'org-shifttab)
  16903. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16904. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16905. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16906. ;; Cursor keys with modifiers
  16907. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16908. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16909. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16910. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16911. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16912. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16913. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16914. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16915. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16916. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16917. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16918. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16919. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16920. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16921. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16922. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16923. ;; Babel keys
  16924. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16925. (mapc (lambda (pair)
  16926. (define-key org-babel-map (car pair) (cdr pair)))
  16927. org-babel-key-bindings)
  16928. ;;; Extra keys for tty access.
  16929. ;; We only set them when really needed because otherwise the
  16930. ;; menus don't show the simple keys
  16931. (when (or org-use-extra-keys
  16932. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16933. (not window-system))
  16934. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16935. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16936. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16937. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16938. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16939. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16940. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16941. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16942. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16943. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16944. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16945. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16946. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16947. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16948. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16949. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16950. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16951. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16952. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16953. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16954. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16955. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16956. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16957. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16958. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16959. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16960. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16961. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16962. ;; All the other keys
  16963. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16964. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16965. (if (boundp 'narrow-map)
  16966. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16967. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16968. (if (boundp 'narrow-map)
  16969. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16970. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16971. (if (boundp 'narrow-map)
  16972. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16973. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16974. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16975. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16976. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16977. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16978. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16979. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16980. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16981. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  16982. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  16983. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16984. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16985. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16986. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16987. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16988. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16989. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16990. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16991. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16992. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16993. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16994. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16995. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16996. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16997. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16998. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16999. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17000. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17001. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17002. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17003. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17004. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17005. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17006. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17007. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17008. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17009. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17010. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17011. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17012. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17013. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17014. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17015. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17016. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17017. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17018. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17019. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17020. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17021. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17022. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17023. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17024. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17025. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17026. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17027. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17028. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17029. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17030. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17031. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17032. (org-defkey org-mode-map "\C-m" 'org-return)
  17033. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17034. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17035. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17036. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17037. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17038. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17039. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17040. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17041. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17042. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17043. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17044. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17045. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17046. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17047. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17048. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17049. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17050. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17051. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17052. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17053. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17054. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17055. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17056. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17057. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17058. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17059. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17060. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17061. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17062. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17063. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17064. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17065. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17066. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17067. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17068. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17069. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17070. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17071. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17072. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17073. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17074. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17075. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17076. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17077. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17078. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17079. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17080. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17081. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17082. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17083. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17084. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17085. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17086. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17087. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17088. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17089. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17090. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17091. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17092. (when (featurep 'xemacs)
  17093. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17094. (defconst org-speed-commands-default
  17095. '(
  17096. ("Outline Navigation")
  17097. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17098. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17099. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17100. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17101. ("F" . org-next-block)
  17102. ("B" . org-previous-block)
  17103. ("u" . (org-speed-move-safe 'outline-up-heading))
  17104. ("j" . org-goto)
  17105. ("g" . (org-refile t))
  17106. ("Outline Visibility")
  17107. ("c" . org-cycle)
  17108. ("C" . org-shifttab)
  17109. (" " . org-display-outline-path)
  17110. ("=" . org-columns)
  17111. ("Outline Structure Editing")
  17112. ("U" . org-shiftmetaup)
  17113. ("D" . org-shiftmetadown)
  17114. ("r" . org-metaright)
  17115. ("l" . org-metaleft)
  17116. ("R" . org-shiftmetaright)
  17117. ("L" . org-shiftmetaleft)
  17118. ("i" . (progn (forward-char 1) (call-interactively
  17119. 'org-insert-heading-respect-content)))
  17120. ("^" . org-sort)
  17121. ("w" . org-refile)
  17122. ("a" . org-archive-subtree-default-with-confirmation)
  17123. ("." . org-mark-subtree) ;; FIXME Better use @ (see C-c @) here?
  17124. ("#" . org-toggle-comment)
  17125. ("Clock Commands")
  17126. ("I" . org-clock-in)
  17127. ("O" . org-clock-out)
  17128. ("Meta Data Editing")
  17129. ("t" . org-todo)
  17130. ("," . (org-priority))
  17131. ("0" . (org-priority ?\ ))
  17132. ("1" . (org-priority ?A))
  17133. ("2" . (org-priority ?B))
  17134. ("3" . (org-priority ?C))
  17135. (":" . org-set-tags-command)
  17136. ("e" . org-set-effort)
  17137. ("E" . org-inc-effort)
  17138. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17139. (org-entry-put (point) "APPT_WARNTIME" m)))
  17140. ("Agenda Views etc")
  17141. ("v" . org-agenda)
  17142. ("/" . org-sparse-tree)
  17143. ("Misc")
  17144. ("o" . org-open-at-point)
  17145. ("?" . org-speed-command-help)
  17146. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17147. (">" . (org-agenda-remove-restriction-lock))
  17148. )
  17149. "The default speed commands.")
  17150. (defun org-print-speed-command (e)
  17151. (if (> (length (car e)) 1)
  17152. (progn
  17153. (princ "\n")
  17154. (princ (car e))
  17155. (princ "\n")
  17156. (princ (make-string (length (car e)) ?-))
  17157. (princ "\n"))
  17158. (princ (car e))
  17159. (princ " ")
  17160. (if (symbolp (cdr e))
  17161. (princ (symbol-name (cdr e)))
  17162. (prin1 (cdr e)))
  17163. (princ "\n")))
  17164. (defun org-speed-command-help ()
  17165. "Show the available speed commands."
  17166. (interactive)
  17167. (if (not org-use-speed-commands)
  17168. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17169. (with-output-to-temp-buffer "*Help*"
  17170. (princ "User-defined Speed commands\n===========================\n")
  17171. (mapc 'org-print-speed-command org-speed-commands-user)
  17172. (princ "\n")
  17173. (princ "Built-in Speed commands\n=======================\n")
  17174. (mapc 'org-print-speed-command org-speed-commands-default))
  17175. (with-current-buffer "*Help*"
  17176. (setq truncate-lines t))))
  17177. (defun org-speed-move-safe (cmd)
  17178. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17179. If not, return to the original position and throw an error."
  17180. (interactive)
  17181. (let ((pos (point)))
  17182. (call-interactively cmd)
  17183. (unless (and (bolp) (org-at-heading-p))
  17184. (goto-char pos)
  17185. (error "Boundary reached while executing %s" cmd))))
  17186. (defvar org-self-insert-command-undo-counter 0)
  17187. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17188. (defvar org-speed-command nil)
  17189. (define-obsolete-function-alias
  17190. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17191. (defun org-speed-command-activate (keys)
  17192. "Hook for activating single-letter speed commands.
  17193. `org-speed-commands-default' specifies a minimal command set.
  17194. Use `org-speed-commands-user' for further customization."
  17195. (when (or (and (bolp) (looking-at org-outline-regexp))
  17196. (and (functionp org-use-speed-commands)
  17197. (funcall org-use-speed-commands)))
  17198. (cdr (assoc keys (append org-speed-commands-user
  17199. org-speed-commands-default)))))
  17200. (define-obsolete-function-alias
  17201. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17202. (defun org-babel-speed-command-activate (keys)
  17203. "Hook for activating single-letter code block commands."
  17204. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17205. (cdr (assoc keys org-babel-key-bindings))))
  17206. (defcustom org-speed-command-hook
  17207. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17208. "Hook for activating speed commands at strategic locations.
  17209. Hook functions are called in sequence until a valid handler is
  17210. found.
  17211. Each hook takes a single argument, a user-pressed command key
  17212. which is also a `self-insert-command' from the global map.
  17213. Within the hook, examine the cursor position and the command key
  17214. and return nil or a valid handler as appropriate. Handler could
  17215. be one of an interactive command, a function, or a form.
  17216. Set `org-use-speed-commands' to non-nil value to enable this
  17217. hook. The default setting is `org-speed-command-activate'."
  17218. :group 'org-structure
  17219. :version "24.1"
  17220. :type 'hook)
  17221. (defun org-self-insert-command (N)
  17222. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17223. If the cursor is in a table looking at whitespace, the whitespace is
  17224. overwritten, and the table is not marked as requiring realignment."
  17225. (interactive "p")
  17226. (org-check-before-invisible-edit 'insert)
  17227. (cond
  17228. ((and org-use-speed-commands
  17229. (setq org-speed-command
  17230. (run-hook-with-args-until-success
  17231. 'org-speed-command-hook (this-command-keys))))
  17232. (cond
  17233. ((commandp org-speed-command)
  17234. (setq this-command org-speed-command)
  17235. (call-interactively org-speed-command))
  17236. ((functionp org-speed-command)
  17237. (funcall org-speed-command))
  17238. ((and org-speed-command (listp org-speed-command))
  17239. (eval org-speed-command))
  17240. (t (let (org-use-speed-commands)
  17241. (call-interactively 'org-self-insert-command)))))
  17242. ((and
  17243. (org-table-p)
  17244. (progn
  17245. ;; check if we blank the field, and if that triggers align
  17246. (and (featurep 'org-table) org-table-auto-blank-field
  17247. (member last-command
  17248. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17249. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17250. ;; got extra space, this field does not determine column width
  17251. (let (org-table-may-need-update) (org-table-blank-field))
  17252. ;; no extra space, this field may determine column width
  17253. (org-table-blank-field)))
  17254. t)
  17255. (eq N 1)
  17256. (looking-at "[^|\n]* |"))
  17257. (let (org-table-may-need-update)
  17258. (goto-char (1- (match-end 0)))
  17259. (backward-delete-char 1)
  17260. (goto-char (match-beginning 0))
  17261. (self-insert-command N)))
  17262. (t
  17263. (setq org-table-may-need-update t)
  17264. (self-insert-command N)
  17265. (org-fix-tags-on-the-fly)
  17266. (if org-self-insert-cluster-for-undo
  17267. (if (not (eq last-command 'org-self-insert-command))
  17268. (setq org-self-insert-command-undo-counter 1)
  17269. (if (>= org-self-insert-command-undo-counter 20)
  17270. (setq org-self-insert-command-undo-counter 1)
  17271. (and (> org-self-insert-command-undo-counter 0)
  17272. buffer-undo-list (listp buffer-undo-list)
  17273. (not (cadr buffer-undo-list)) ; remove nil entry
  17274. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17275. (setq org-self-insert-command-undo-counter
  17276. (1+ org-self-insert-command-undo-counter))))))))
  17277. (defun org-check-before-invisible-edit (kind)
  17278. "Check is editing if kind KIND would be dangerous with invisible text around.
  17279. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17280. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17281. (if (and org-catch-invisible-edits
  17282. (or (not (boundp 'visible-mode)) (not visible-mode))
  17283. (or (get-char-property (point) 'invisible)
  17284. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17285. ;; OK, we need to take a closer look
  17286. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17287. (invisible-before-point (if (bobp) nil (get-char-property
  17288. (1- (point)) 'invisible)))
  17289. (border-and-ok-direction
  17290. (or
  17291. ;; Check if we are acting predictably before invisible text
  17292. (and invisible-at-point (not invisible-before-point)
  17293. (memq kind '(insert delete-backward)))
  17294. ;; Check if we are acting predictably after invisible text
  17295. ;; This works not well, and I have turned it off. It seems
  17296. ;; better to always show and stop after invisible text.
  17297. ;; (and (not invisible-at-point) invisible-before-point
  17298. ;; (memq kind '(insert delete)))
  17299. )))
  17300. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17301. (memq invisible-before-point '(outline org-hide-block t)))
  17302. (if (eq org-catch-invisible-edits 'error)
  17303. (error "Editing in invisible areas is prohibited - make visible first"))
  17304. (if (and org-custom-properties-overlays
  17305. (y-or-n-p "Display invisible properties in this buffer? "))
  17306. (org-toggle-custom-properties-visibility)
  17307. ;; Make the area visible
  17308. (save-excursion
  17309. (if invisible-before-point
  17310. (goto-char (previous-single-char-property-change
  17311. (point) 'invisible)))
  17312. (org-cycle))
  17313. (cond
  17314. ((eq org-catch-invisible-edits 'show)
  17315. ;; That's it, we do the edit after showing
  17316. (message
  17317. "Unfolding invisible region around point before editing")
  17318. (sit-for 1))
  17319. ((and (eq org-catch-invisible-edits 'smart)
  17320. border-and-ok-direction)
  17321. (message "Unfolding invisible region around point before editing"))
  17322. (t
  17323. ;; Don't do the edit, make the user repeat it in full visibility
  17324. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17325. (defun org-fix-tags-on-the-fly ()
  17326. (when (and (equal (char-after (point-at-bol)) ?*)
  17327. (org-at-heading-p))
  17328. (org-align-tags-here org-tags-column)))
  17329. (defun org-delete-backward-char (N)
  17330. "Like `delete-backward-char', insert whitespace at field end in tables.
  17331. When deleting backwards, in tables this function will insert whitespace in
  17332. front of the next \"|\" separator, to keep the table aligned. The table will
  17333. still be marked for re-alignment if the field did fill the entire column,
  17334. because, in this case the deletion might narrow the column."
  17335. (interactive "p")
  17336. (save-match-data
  17337. (org-check-before-invisible-edit 'delete-backward)
  17338. (if (and (org-table-p)
  17339. (eq N 1)
  17340. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17341. (looking-at ".*?|"))
  17342. (let ((pos (point))
  17343. (noalign (looking-at "[^|\n\r]* |"))
  17344. (c org-table-may-need-update))
  17345. (backward-delete-char N)
  17346. (if (not overwrite-mode)
  17347. (progn
  17348. (skip-chars-forward "^|")
  17349. (insert " ")
  17350. (goto-char (1- pos))))
  17351. ;; noalign: if there were two spaces at the end, this field
  17352. ;; does not determine the width of the column.
  17353. (if noalign (setq org-table-may-need-update c)))
  17354. (backward-delete-char N)
  17355. (org-fix-tags-on-the-fly))))
  17356. (defun org-delete-char (N)
  17357. "Like `delete-char', but insert whitespace at field end in tables.
  17358. When deleting characters, in tables this function will insert whitespace in
  17359. front of the next \"|\" separator, to keep the table aligned. The table will
  17360. still be marked for re-alignment if the field did fill the entire column,
  17361. because, in this case the deletion might narrow the column."
  17362. (interactive "p")
  17363. (save-match-data
  17364. (org-check-before-invisible-edit 'delete)
  17365. (if (and (org-table-p)
  17366. (not (bolp))
  17367. (not (= (char-after) ?|))
  17368. (eq N 1))
  17369. (if (looking-at ".*?|")
  17370. (let ((pos (point))
  17371. (noalign (looking-at "[^|\n\r]* |"))
  17372. (c org-table-may-need-update))
  17373. (replace-match (concat
  17374. (substring (match-string 0) 1 -1)
  17375. " |"))
  17376. (goto-char pos)
  17377. ;; noalign: if there were two spaces at the end, this field
  17378. ;; does not determine the width of the column.
  17379. (if noalign (setq org-table-may-need-update c)))
  17380. (delete-char N))
  17381. (delete-char N)
  17382. (org-fix-tags-on-the-fly))))
  17383. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17384. (put 'org-self-insert-command 'delete-selection t)
  17385. (put 'orgtbl-self-insert-command 'delete-selection t)
  17386. (put 'org-delete-char 'delete-selection 'supersede)
  17387. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17388. (put 'org-yank 'delete-selection 'yank)
  17389. ;; Make `flyspell-mode' delay after some commands
  17390. (put 'org-self-insert-command 'flyspell-delayed t)
  17391. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17392. (put 'org-delete-char 'flyspell-delayed t)
  17393. (put 'org-delete-backward-char 'flyspell-delayed t)
  17394. ;; Make pabbrev-mode expand after org-mode commands
  17395. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17396. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17397. ;; How to do this: Measure non-white length of current string
  17398. ;; If equal to column width, we should realign.
  17399. (defun org-remap (map &rest commands)
  17400. "In MAP, remap the functions given in COMMANDS.
  17401. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17402. (let (new old)
  17403. (while commands
  17404. (setq old (pop commands) new (pop commands))
  17405. (if (fboundp 'command-remapping)
  17406. (org-defkey map (vector 'remap old) new)
  17407. (substitute-key-definition old new map global-map)))))
  17408. (when (eq org-enable-table-editor 'optimized)
  17409. ;; If the user wants maximum table support, we need to hijack
  17410. ;; some standard editing functions
  17411. (org-remap org-mode-map
  17412. 'self-insert-command 'org-self-insert-command
  17413. 'delete-char 'org-delete-char
  17414. 'delete-backward-char 'org-delete-backward-char)
  17415. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17416. (defvar org-ctrl-c-ctrl-c-hook nil
  17417. "Hook for functions attaching themselves to `C-c C-c'.
  17418. This can be used to add additional functionality to the C-c C-c
  17419. key which executes context-dependent commands. This hook is run
  17420. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17421. run after the last test.
  17422. Each function will be called with no arguments. The function
  17423. must check if the context is appropriate for it to act. If yes,
  17424. it should do its thing and then return a non-nil value. If the
  17425. context is wrong, just do nothing and return nil.")
  17426. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17427. "Hook for functions attaching themselves to `C-c C-c'.
  17428. This can be used to add additional functionality to the C-c C-c
  17429. key which executes context-dependent commands. This hook is run
  17430. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17431. before the first test.
  17432. Each function will be called with no arguments. The function
  17433. must check if the context is appropriate for it to act. If yes,
  17434. it should do its thing and then return a non-nil value. If the
  17435. context is wrong, just do nothing and return nil.")
  17436. (defvar org-tab-first-hook nil
  17437. "Hook for functions to attach themselves to TAB.
  17438. See `org-ctrl-c-ctrl-c-hook' for more information.
  17439. This hook runs as the first action when TAB is pressed, even before
  17440. `org-cycle' messes around with the `outline-regexp' to cater for
  17441. inline tasks and plain list item folding.
  17442. If any function in this hook returns t, any other actions that
  17443. would have been caused by TAB (such as table field motion or visibility
  17444. cycling) will not occur.")
  17445. (defvar org-tab-after-check-for-table-hook nil
  17446. "Hook for functions to attach themselves to TAB.
  17447. See `org-ctrl-c-ctrl-c-hook' for more information.
  17448. This hook runs after it has been established that the cursor is not in a
  17449. table, but before checking if the cursor is in a headline or if global cycling
  17450. should be done.
  17451. If any function in this hook returns t, not other actions like visibility
  17452. cycling will be done.")
  17453. (defvar org-tab-after-check-for-cycling-hook nil
  17454. "Hook for functions to attach themselves to TAB.
  17455. See `org-ctrl-c-ctrl-c-hook' for more information.
  17456. This hook runs after it has been established that not table field motion and
  17457. not visibility should be done because of current context. This is probably
  17458. the place where a package like yasnippets can hook in.")
  17459. (defvar org-tab-before-tab-emulation-hook nil
  17460. "Hook for functions to attach themselves to TAB.
  17461. See `org-ctrl-c-ctrl-c-hook' for more information.
  17462. This hook runs after every other options for TAB have been exhausted, but
  17463. before indentation and \t insertion takes place.")
  17464. (defvar org-metaleft-hook nil
  17465. "Hook for functions attaching themselves to `M-left'.
  17466. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17467. (defvar org-metaright-hook nil
  17468. "Hook for functions attaching themselves to `M-right'.
  17469. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17470. (defvar org-metaup-hook nil
  17471. "Hook for functions attaching themselves to `M-up'.
  17472. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17473. (defvar org-metadown-hook nil
  17474. "Hook for functions attaching themselves to `M-down'.
  17475. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17476. (defvar org-shiftmetaleft-hook nil
  17477. "Hook for functions attaching themselves to `M-S-left'.
  17478. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17479. (defvar org-shiftmetaright-hook nil
  17480. "Hook for functions attaching themselves to `M-S-right'.
  17481. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17482. (defvar org-shiftmetaup-hook nil
  17483. "Hook for functions attaching themselves to `M-S-up'.
  17484. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17485. (defvar org-shiftmetadown-hook nil
  17486. "Hook for functions attaching themselves to `M-S-down'.
  17487. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17488. (defvar org-metareturn-hook nil
  17489. "Hook for functions attaching themselves to `M-RET'.
  17490. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17491. (defvar org-shiftup-hook nil
  17492. "Hook for functions attaching themselves to `S-up'.
  17493. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17494. (defvar org-shiftup-final-hook nil
  17495. "Hook for functions attaching themselves to `S-up'.
  17496. This one runs after all other options except shift-select have been excluded.
  17497. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17498. (defvar org-shiftdown-hook nil
  17499. "Hook for functions attaching themselves to `S-down'.
  17500. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17501. (defvar org-shiftdown-final-hook nil
  17502. "Hook for functions attaching themselves to `S-down'.
  17503. This one runs after all other options except shift-select have been excluded.
  17504. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17505. (defvar org-shiftleft-hook nil
  17506. "Hook for functions attaching themselves to `S-left'.
  17507. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17508. (defvar org-shiftleft-final-hook nil
  17509. "Hook for functions attaching themselves to `S-left'.
  17510. This one runs after all other options except shift-select have been excluded.
  17511. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17512. (defvar org-shiftright-hook nil
  17513. "Hook for functions attaching themselves to `S-right'.
  17514. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17515. (defvar org-shiftright-final-hook nil
  17516. "Hook for functions attaching themselves to `S-right'.
  17517. This one runs after all other options except shift-select have been excluded.
  17518. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17519. (defun org-modifier-cursor-error ()
  17520. "Throw an error, a modified cursor command was applied in wrong context."
  17521. (error "This command is active in special context like tables, headlines or items"))
  17522. (defun org-shiftselect-error ()
  17523. "Throw an error because Shift-Cursor command was applied in wrong context."
  17524. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17525. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17526. (error "This command works only in special context like headlines or timestamps")))
  17527. (defun org-call-for-shift-select (cmd)
  17528. (let ((this-command-keys-shift-translated t))
  17529. (call-interactively cmd)))
  17530. (defun org-shifttab (&optional arg)
  17531. "Global visibility cycling or move to previous table field.
  17532. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17533. on context.
  17534. See the individual commands for more information."
  17535. (interactive "P")
  17536. (cond
  17537. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17538. ((integerp arg)
  17539. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17540. (message "Content view to level: %d" arg)
  17541. (org-content (prefix-numeric-value arg2))
  17542. (setq org-cycle-global-status 'overview)))
  17543. (t (call-interactively 'org-global-cycle))))
  17544. (defun org-shiftmetaleft ()
  17545. "Promote subtree or delete table column.
  17546. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17547. `org-table-delete-column', depending on context. See the
  17548. individual commands for more information."
  17549. (interactive)
  17550. (cond
  17551. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17552. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17553. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17554. ((if (not (org-region-active-p)) (org-at-item-p)
  17555. (save-excursion (goto-char (region-beginning))
  17556. (org-at-item-p)))
  17557. (call-interactively 'org-outdent-item-tree))
  17558. (t (org-modifier-cursor-error))))
  17559. (defun org-shiftmetaright ()
  17560. "Demote subtree or insert table column.
  17561. Calls `org-demote-subtree', `org-indent-item-tree', or
  17562. `org-table-insert-column', depending on context. See the
  17563. individual commands for more information."
  17564. (interactive)
  17565. (cond
  17566. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17567. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17568. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17569. ((if (not (org-region-active-p)) (org-at-item-p)
  17570. (save-excursion (goto-char (region-beginning))
  17571. (org-at-item-p)))
  17572. (call-interactively 'org-indent-item-tree))
  17573. (t (org-modifier-cursor-error))))
  17574. (defun org-shiftmetaup (&optional arg)
  17575. "Move subtree up or kill table row.
  17576. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17577. `org-move-item-up' or `org-timestamp-up', depending on context.
  17578. See the individual commands for more information."
  17579. (interactive "P")
  17580. (cond
  17581. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17582. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17583. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17584. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17585. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17586. (call-interactively 'org-timestamp-up)))
  17587. (t (call-interactively 'org-drag-line-backward))))
  17588. (defun org-shiftmetadown (&optional arg)
  17589. "Move subtree down or insert table row.
  17590. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17591. `org-move-item-down' or `org-timestamp-up', depending on context.
  17592. See the individual commands for more information."
  17593. (interactive "P")
  17594. (cond
  17595. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17596. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17597. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17598. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17599. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17600. (call-interactively 'org-timestamp-down)))
  17601. (t (call-interactively 'org-drag-line-forward))))
  17602. (defsubst org-hidden-tree-error ()
  17603. (error
  17604. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17605. (defun org-metaleft (&optional arg)
  17606. "Promote heading or move table column to left.
  17607. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17608. With no specific context, calls the Emacs default `backward-word'.
  17609. See the individual commands for more information."
  17610. (interactive "P")
  17611. (cond
  17612. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17613. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17614. ((org-with-limited-levels
  17615. (or (org-at-heading-p)
  17616. (and (org-region-active-p)
  17617. (save-excursion
  17618. (goto-char (region-beginning))
  17619. (org-at-heading-p)))))
  17620. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17621. (call-interactively 'org-do-promote))
  17622. ;; At an inline task.
  17623. ((org-at-heading-p)
  17624. (call-interactively 'org-inlinetask-promote))
  17625. ((or (org-at-item-p)
  17626. (and (org-region-active-p)
  17627. (save-excursion
  17628. (goto-char (region-beginning))
  17629. (org-at-item-p))))
  17630. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17631. (call-interactively 'org-outdent-item))
  17632. (t (call-interactively 'backward-word))))
  17633. (defun org-metaright (&optional arg)
  17634. "Demote a subtree, a list item or move table column to right.
  17635. In front of a drawer or a block keyword, indent it correctly.
  17636. With no specific context, calls the Emacs default `forward-word'.
  17637. See the individual commands for more information."
  17638. (interactive "P")
  17639. (cond
  17640. ((run-hook-with-args-until-success 'org-metaright-hook))
  17641. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17642. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17643. ((org-at-block-p) (call-interactively 'org-indent-block))
  17644. ((org-with-limited-levels
  17645. (or (org-at-heading-p)
  17646. (and (org-region-active-p)
  17647. (save-excursion
  17648. (goto-char (region-beginning))
  17649. (org-at-heading-p)))))
  17650. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17651. (call-interactively 'org-do-demote))
  17652. ;; At an inline task.
  17653. ((org-at-heading-p)
  17654. (call-interactively 'org-inlinetask-demote))
  17655. ((or (org-at-item-p)
  17656. (and (org-region-active-p)
  17657. (save-excursion
  17658. (goto-char (region-beginning))
  17659. (org-at-item-p))))
  17660. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17661. (call-interactively 'org-indent-item))
  17662. (t (call-interactively 'forward-word))))
  17663. (defun org-check-for-hidden (what)
  17664. "Check if there are hidden headlines/items in the current visual line.
  17665. WHAT can be either `headlines' or `items'. If the current line is
  17666. an outline or item heading and it has a folded subtree below it,
  17667. this function returns t, nil otherwise."
  17668. (let ((re (cond
  17669. ((eq what 'headlines) org-outline-regexp-bol)
  17670. ((eq what 'items) (org-item-beginning-re))
  17671. (t (error "This should not happen"))))
  17672. beg end)
  17673. (save-excursion
  17674. (catch 'exit
  17675. (unless (org-region-active-p)
  17676. (setq beg (point-at-bol))
  17677. (beginning-of-line 2)
  17678. (while (and (not (eobp)) ;; this is like `next-line'
  17679. (get-char-property (1- (point)) 'invisible))
  17680. (beginning-of-line 2))
  17681. (setq end (point))
  17682. (goto-char beg)
  17683. (goto-char (point-at-eol))
  17684. (setq end (max end (point)))
  17685. (while (re-search-forward re end t)
  17686. (if (get-char-property (match-beginning 0) 'invisible)
  17687. (throw 'exit t))))
  17688. nil))))
  17689. (defun org-metaup (&optional arg)
  17690. "Move subtree up or move table row up.
  17691. Calls `org-move-subtree-up' or `org-table-move-row' or
  17692. `org-move-item-up', depending on context. See the individual commands
  17693. for more information."
  17694. (interactive "P")
  17695. (cond
  17696. ((run-hook-with-args-until-success 'org-metaup-hook))
  17697. ((org-region-active-p)
  17698. (let* ((a (min (region-beginning) (region-end)))
  17699. (b (1- (max (region-beginning) (region-end))))
  17700. (c (save-excursion (goto-char a)
  17701. (move-beginning-of-line 0)))
  17702. (d (save-excursion (goto-char a)
  17703. (move-end-of-line 0) (point))))
  17704. (transpose-regions a b c d)
  17705. (goto-char c)))
  17706. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17707. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17708. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17709. (t (org-drag-element-backward))))
  17710. (defun org-metadown (&optional arg)
  17711. "Move subtree down or move table row down.
  17712. Calls `org-move-subtree-down' or `org-table-move-row' or
  17713. `org-move-item-down', depending on context. See the individual
  17714. commands for more information."
  17715. (interactive "P")
  17716. (cond
  17717. ((run-hook-with-args-until-success 'org-metadown-hook))
  17718. ((org-region-active-p)
  17719. (let* ((a (min (region-beginning) (region-end)))
  17720. (b (max (region-beginning) (region-end)))
  17721. (c (save-excursion (goto-char b)
  17722. (move-beginning-of-line 1)))
  17723. (d (save-excursion (goto-char b)
  17724. (move-end-of-line 1) (1+ (point)))))
  17725. (transpose-regions a b c d)
  17726. (goto-char d)))
  17727. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17728. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17729. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17730. (t (org-drag-element-forward))))
  17731. (defun org-shiftup (&optional arg)
  17732. "Increase item in timestamp or increase priority of current headline.
  17733. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17734. depending on context. See the individual commands for more information."
  17735. (interactive "P")
  17736. (cond
  17737. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17738. ((and org-support-shift-select (org-region-active-p))
  17739. (org-call-for-shift-select 'previous-line))
  17740. ((org-at-timestamp-p t)
  17741. (call-interactively (if org-edit-timestamp-down-means-later
  17742. 'org-timestamp-down 'org-timestamp-up)))
  17743. ((and (not (eq org-support-shift-select 'always))
  17744. org-enable-priority-commands
  17745. (org-at-heading-p))
  17746. (call-interactively 'org-priority-up))
  17747. ((and (not org-support-shift-select) (org-at-item-p))
  17748. (call-interactively 'org-previous-item))
  17749. ((org-clocktable-try-shift 'up arg))
  17750. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17751. (org-support-shift-select
  17752. (org-call-for-shift-select 'previous-line))
  17753. (t (org-shiftselect-error))))
  17754. (defun org-shiftdown (&optional arg)
  17755. "Decrease item in timestamp or decrease priority of current headline.
  17756. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17757. depending on context. See the individual commands for more information."
  17758. (interactive "P")
  17759. (cond
  17760. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17761. ((and org-support-shift-select (org-region-active-p))
  17762. (org-call-for-shift-select 'next-line))
  17763. ((org-at-timestamp-p t)
  17764. (call-interactively (if org-edit-timestamp-down-means-later
  17765. 'org-timestamp-up 'org-timestamp-down)))
  17766. ((and (not (eq org-support-shift-select 'always))
  17767. org-enable-priority-commands
  17768. (org-at-heading-p))
  17769. (call-interactively 'org-priority-down))
  17770. ((and (not org-support-shift-select) (org-at-item-p))
  17771. (call-interactively 'org-next-item))
  17772. ((org-clocktable-try-shift 'down arg))
  17773. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17774. (org-support-shift-select
  17775. (org-call-for-shift-select 'next-line))
  17776. (t (org-shiftselect-error))))
  17777. (defun org-shiftright (&optional arg)
  17778. "Cycle the thing at point or in the current line, depending on context.
  17779. Depending on context, this does one of the following:
  17780. - switch a timestamp at point one day into the future
  17781. - on a headline, switch to the next TODO keyword.
  17782. - on an item, switch entire list to the next bullet type
  17783. - on a property line, switch to the next allowed value
  17784. - on a clocktable definition line, move time block into the future"
  17785. (interactive "P")
  17786. (cond
  17787. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17788. ((and org-support-shift-select (org-region-active-p))
  17789. (org-call-for-shift-select 'forward-char))
  17790. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17791. ((and (not (eq org-support-shift-select 'always))
  17792. (org-at-heading-p))
  17793. (let ((org-inhibit-logging
  17794. (not org-treat-S-cursor-todo-selection-as-state-change))
  17795. (org-inhibit-blocking
  17796. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17797. (org-call-with-arg 'org-todo 'right)))
  17798. ((or (and org-support-shift-select
  17799. (not (eq org-support-shift-select 'always))
  17800. (org-at-item-bullet-p))
  17801. (and (not org-support-shift-select) (org-at-item-p)))
  17802. (org-call-with-arg 'org-cycle-list-bullet nil))
  17803. ((and (not (eq org-support-shift-select 'always))
  17804. (org-at-property-p))
  17805. (call-interactively 'org-property-next-allowed-value))
  17806. ((org-clocktable-try-shift 'right arg))
  17807. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17808. (org-support-shift-select
  17809. (org-call-for-shift-select 'forward-char))
  17810. (t (org-shiftselect-error))))
  17811. (defun org-shiftleft (&optional arg)
  17812. "Cycle the thing at point or in the current line, depending on context.
  17813. Depending on context, this does one of the following:
  17814. - switch a timestamp at point one day into the past
  17815. - on a headline, switch to the previous TODO keyword.
  17816. - on an item, switch entire list to the previous bullet type
  17817. - on a property line, switch to the previous allowed value
  17818. - on a clocktable definition line, move time block into the past"
  17819. (interactive "P")
  17820. (cond
  17821. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17822. ((and org-support-shift-select (org-region-active-p))
  17823. (org-call-for-shift-select 'backward-char))
  17824. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17825. ((and (not (eq org-support-shift-select 'always))
  17826. (org-at-heading-p))
  17827. (let ((org-inhibit-logging
  17828. (not org-treat-S-cursor-todo-selection-as-state-change))
  17829. (org-inhibit-blocking
  17830. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17831. (org-call-with-arg 'org-todo 'left)))
  17832. ((or (and org-support-shift-select
  17833. (not (eq org-support-shift-select 'always))
  17834. (org-at-item-bullet-p))
  17835. (and (not org-support-shift-select) (org-at-item-p)))
  17836. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17837. ((and (not (eq org-support-shift-select 'always))
  17838. (org-at-property-p))
  17839. (call-interactively 'org-property-previous-allowed-value))
  17840. ((org-clocktable-try-shift 'left arg))
  17841. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17842. (org-support-shift-select
  17843. (org-call-for-shift-select 'backward-char))
  17844. (t (org-shiftselect-error))))
  17845. (defun org-shiftcontrolright ()
  17846. "Switch to next TODO set."
  17847. (interactive)
  17848. (cond
  17849. ((and org-support-shift-select (org-region-active-p))
  17850. (org-call-for-shift-select 'forward-word))
  17851. ((and (not (eq org-support-shift-select 'always))
  17852. (org-at-heading-p))
  17853. (org-call-with-arg 'org-todo 'nextset))
  17854. (org-support-shift-select
  17855. (org-call-for-shift-select 'forward-word))
  17856. (t (org-shiftselect-error))))
  17857. (defun org-shiftcontrolleft ()
  17858. "Switch to previous TODO set."
  17859. (interactive)
  17860. (cond
  17861. ((and org-support-shift-select (org-region-active-p))
  17862. (org-call-for-shift-select 'backward-word))
  17863. ((and (not (eq org-support-shift-select 'always))
  17864. (org-at-heading-p))
  17865. (org-call-with-arg 'org-todo 'previousset))
  17866. (org-support-shift-select
  17867. (org-call-for-shift-select 'backward-word))
  17868. (t (org-shiftselect-error))))
  17869. (defun org-shiftcontrolup (&optional n)
  17870. "Change timestamps synchronously up in CLOCK log lines.
  17871. Optional argument N tells to change by that many units."
  17872. (interactive "P")
  17873. (cond ((and (not org-support-shift-select)
  17874. (org-at-clock-log-p)
  17875. (org-at-timestamp-p t))
  17876. (org-clock-timestamps-up n))
  17877. (t (org-shiftselect-error))))
  17878. (defun org-shiftcontroldown (&optional n)
  17879. "Change timestamps synchronously down in CLOCK log lines.
  17880. Optional argument N tells to change by that many units."
  17881. (interactive "P")
  17882. (cond ((and (not org-support-shift-select)
  17883. (org-at-clock-log-p)
  17884. (org-at-timestamp-p t))
  17885. (org-clock-timestamps-down n))
  17886. (t (org-shiftselect-error))))
  17887. (defun org-ctrl-c-ret ()
  17888. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17889. (interactive)
  17890. (cond
  17891. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17892. (t (call-interactively 'org-insert-heading))))
  17893. (defun org-find-visible ()
  17894. (let ((s (point)))
  17895. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17896. (get-char-property s 'invisible)))
  17897. s))
  17898. (defun org-find-invisible ()
  17899. (let ((s (point)))
  17900. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17901. (not (get-char-property s 'invisible))))
  17902. s))
  17903. (defun org-copy-visible (beg end)
  17904. "Copy the visible parts of the region."
  17905. (interactive "r")
  17906. (let (snippets s)
  17907. (save-excursion
  17908. (save-restriction
  17909. (narrow-to-region beg end)
  17910. (setq s (goto-char (point-min)))
  17911. (while (not (= (point) (point-max)))
  17912. (goto-char (org-find-invisible))
  17913. (push (buffer-substring s (point)) snippets)
  17914. (setq s (goto-char (org-find-visible))))))
  17915. (kill-new (apply 'concat (nreverse snippets)))))
  17916. (defun org-copy-special ()
  17917. "Copy region in table or copy current subtree.
  17918. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17919. See the individual commands for more information."
  17920. (interactive)
  17921. (call-interactively
  17922. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17923. (defun org-cut-special ()
  17924. "Cut region in table or cut current subtree.
  17925. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17926. See the individual commands for more information."
  17927. (interactive)
  17928. (call-interactively
  17929. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17930. (defun org-paste-special (arg)
  17931. "Paste rectangular region into table, or past subtree relative to level.
  17932. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17933. See the individual commands for more information."
  17934. (interactive "P")
  17935. (if (org-at-table-p)
  17936. (org-table-paste-rectangle)
  17937. (org-paste-subtree arg)))
  17938. (defsubst org-in-fixed-width-region-p ()
  17939. "Is point in a fixed-width region?"
  17940. (save-match-data
  17941. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17942. (defun org-edit-special (&optional arg)
  17943. "Call a special editor for the element at point.
  17944. When at a table, call the formula editor with `org-table-edit-formulas'.
  17945. When in a source code block, call `org-edit-src-code'.
  17946. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17947. When at an #+INCLUDE keyword, visit the included file.
  17948. On a link, call `ffap' to visit the link at point.
  17949. Otherwise, return a user error."
  17950. (interactive)
  17951. (let ((element (org-element-at-point)))
  17952. (assert (not buffer-read-only) nil
  17953. "Buffer is read-only: %s" (buffer-name))
  17954. (case (org-element-type element)
  17955. (src-block
  17956. (if (not arg) (org-edit-src-code)
  17957. (let* ((info (org-babel-get-src-block-info))
  17958. (lang (nth 0 info))
  17959. (params (nth 2 info))
  17960. (session (cdr (assq :session params))))
  17961. (if (not session) (org-edit-src-code)
  17962. ;; At a src-block with a session and function called with
  17963. ;; an ARG: switch to the buffer related to the inferior
  17964. ;; process.
  17965. (funcall (intern (concat "org-babel-prep-session:" lang))
  17966. session params)))))
  17967. (keyword
  17968. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  17969. (find-file
  17970. (org-remove-double-quotes
  17971. (car (org-split-string (org-element-property :value element)))))
  17972. (user-error "No special environment to edit here")))
  17973. (table
  17974. (if (eq (org-element-property :type element) 'table.el)
  17975. (org-edit-src-code)
  17976. (call-interactively 'org-table-edit-formulas)))
  17977. ;; Only Org tables contain `table-row' type elements.
  17978. (table-row (call-interactively 'org-table-edit-formulas))
  17979. ((example-block export-block) (org-edit-src-code))
  17980. (fixed-width (org-edit-fixed-width-region))
  17981. (otherwise
  17982. ;; No notable element at point. Though, we may be at a link,
  17983. ;; which is an object. Thus, scan deeper.
  17984. (if (eq (org-element-type (org-element-context element)) 'link)
  17985. (call-interactively 'ffap)
  17986. (user-error "No special environment to edit here"))))))
  17987. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17988. (defun org-ctrl-c-ctrl-c (&optional arg)
  17989. "Set tags in headline, or update according to changed information at point.
  17990. This command does many different things, depending on context:
  17991. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17992. this is what we do.
  17993. - If the cursor is on a statistics cookie, update it.
  17994. - If the cursor is in a headline, prompt for tags and insert them
  17995. into the current line, aligned to `org-tags-column'. When called
  17996. with prefix arg, realign all tags in the current buffer.
  17997. - If the cursor is in one of the special #+KEYWORD lines, this
  17998. triggers scanning the buffer for these lines and updating the
  17999. information.
  18000. - If the cursor is inside a table, realign the table. This command
  18001. works even if the automatic table editor has been turned off.
  18002. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18003. the entire table.
  18004. - If the cursor is at a footnote reference or definition, jump to
  18005. the corresponding definition or references, respectively.
  18006. - If the cursor is a the beginning of a dynamic block, update it.
  18007. - If the current buffer is a capture buffer, close note and file it.
  18008. - If the cursor is on a <<<target>>>, update radio targets and
  18009. corresponding links in this buffer.
  18010. - If the cursor is on a numbered item in a plain list, renumber the
  18011. ordered list.
  18012. - If the cursor is on a checkbox, toggle it.
  18013. - If the cursor is on a code block, evaluate it. The variable
  18014. `org-confirm-babel-evaluate' can be used to control prompting
  18015. before code block evaluation, by default every code block
  18016. evaluation requires confirmation. Code block evaluation can be
  18017. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18018. (interactive "P")
  18019. (cond
  18020. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18021. org-occur-highlights
  18022. org-latex-fragment-image-overlays)
  18023. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18024. (org-remove-occur-highlights)
  18025. (org-remove-latex-fragment-image-overlays)
  18026. (message "Temporary highlights/overlays removed from current buffer"))
  18027. ((and (local-variable-p 'org-finish-function (current-buffer))
  18028. (fboundp org-finish-function))
  18029. (funcall org-finish-function))
  18030. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18031. (t
  18032. (let* ((context (org-element-context)) (type (org-element-type context)))
  18033. ;; Test if point is within a blank line.
  18034. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18035. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18036. (user-error "C-c C-c can do nothing useful at this location"))
  18037. ;; For convenience: at the first line of a paragraph on the
  18038. ;; same line as an item, apply function on that item instead.
  18039. (when (eq type 'paragraph)
  18040. (let ((parent (org-element-property :parent context)))
  18041. (when (and (eq (org-element-type parent) 'item)
  18042. (= (point-at-bol) (org-element-property :begin parent)))
  18043. (setq context parent type 'item))))
  18044. ;; Act according to type of element or object at point.
  18045. (case type
  18046. (clock (org-clock-update-time-maybe))
  18047. (dynamic-block
  18048. (save-excursion
  18049. (goto-char (org-element-property :post-affiliated context))
  18050. (org-update-dblock)))
  18051. (footnote-definition
  18052. (goto-char (org-element-property :post-affiliated context))
  18053. (call-interactively 'org-footnote-action))
  18054. (footnote-reference (call-interactively 'org-footnote-action))
  18055. ((headline inlinetask)
  18056. (save-excursion (goto-char (org-element-property :begin context))
  18057. (call-interactively 'org-set-tags)))
  18058. (item
  18059. ;; At an item: a double C-u set checkbox to "[-]"
  18060. ;; unconditionally, whereas a single one will toggle its
  18061. ;; presence. Without an universal argument, if the item
  18062. ;; has a checkbox, toggle it. Otherwise repair the list.
  18063. (let* ((box (org-element-property :checkbox context))
  18064. (struct (org-element-property :structure context))
  18065. (old-struct (copy-tree struct))
  18066. (parents (org-list-parents-alist struct))
  18067. (prevs (org-list-prevs-alist struct))
  18068. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18069. (org-list-set-checkbox
  18070. (org-element-property :begin context) struct
  18071. (cond ((equal arg '(16)) "[-]")
  18072. ((and (not box) (equal arg '(4))) "[ ]")
  18073. ((or (not box) (equal arg '(4))) nil)
  18074. ((eq box 'on) "[ ]")
  18075. (t "[X]")))
  18076. ;; Mimic `org-list-write-struct' but with grabbing
  18077. ;; a return value from `org-list-struct-fix-box'.
  18078. (org-list-struct-fix-ind struct parents 2)
  18079. (org-list-struct-fix-item-end struct)
  18080. (org-list-struct-fix-bul struct prevs)
  18081. (org-list-struct-fix-ind struct parents)
  18082. (let ((block-item
  18083. (org-list-struct-fix-box struct parents prevs orderedp)))
  18084. (if (and box (equal struct old-struct))
  18085. (if (equal arg '(16))
  18086. (message "Checkboxes already reset")
  18087. (user-error "Cannot toggle this checkbox: %s"
  18088. (if (eq box 'on)
  18089. "all subitems checked"
  18090. "unchecked subitems")))
  18091. (org-list-struct-apply-struct struct old-struct)
  18092. (org-update-checkbox-count-maybe))
  18093. (when block-item
  18094. (message "Checkboxes were removed due to empty box at line %d"
  18095. (org-current-line block-item))))))
  18096. (keyword
  18097. (let ((org-inhibit-startup-visibility-stuff t)
  18098. (org-startup-align-all-tables nil))
  18099. (when (boundp 'org-table-coordinate-overlays)
  18100. (mapc 'delete-overlay org-table-coordinate-overlays)
  18101. (setq org-table-coordinate-overlays nil))
  18102. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18103. (message "Local setup has been refreshed"))
  18104. (plain-list
  18105. ;; At a plain list, with a double C-u argument, set
  18106. ;; checkboxes of each item to "[-]", whereas a single one
  18107. ;; will toggle their presence according to the state of the
  18108. ;; first item in the list. Without an argument, repair the
  18109. ;; list.
  18110. (let* ((begin (org-element-property :contents-begin context))
  18111. (struct (org-element-property :structure context))
  18112. (old-struct (copy-tree struct))
  18113. (first-box (save-excursion
  18114. (goto-char begin)
  18115. (looking-at org-list-full-item-re)
  18116. (match-string-no-properties 3)))
  18117. (new-box (cond ((equal arg '(16)) "[-]")
  18118. ((equal arg '(4)) (unless first-box "[ ]"))
  18119. ((equal first-box "[X]") "[ ]")
  18120. (t "[X]"))))
  18121. (cond
  18122. (arg
  18123. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18124. (org-list-get-all-items
  18125. begin struct (org-list-prevs-alist struct))))
  18126. ((and first-box (eq (point) begin))
  18127. ;; For convenience, when point is at bol on the first
  18128. ;; item of the list and no argument is provided, simply
  18129. ;; toggle checkbox of that item, if any.
  18130. (org-list-set-checkbox begin struct new-box)))
  18131. (org-list-write-struct
  18132. struct (org-list-parents-alist struct) old-struct)
  18133. (org-update-checkbox-count-maybe)
  18134. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18135. ((property-drawer node-property)
  18136. (call-interactively 'org-property-action))
  18137. ((radio-target target)
  18138. (call-interactively 'org-update-radio-target-regexp))
  18139. (statistics-cookie
  18140. (call-interactively 'org-update-statistics-cookies))
  18141. ((table table-cell table-row)
  18142. ;; At a table, recalculate every field and align it. Also
  18143. ;; send the table if necessary. If the table has
  18144. ;; a `table.el' type, just give up. At a table row or
  18145. ;; cell, maybe recalculate line but always align table.
  18146. (if (eq (org-element-property :type context) 'table.el)
  18147. (message "Use C-c ' to edit table.el tables")
  18148. (let ((org-enable-table-editor t))
  18149. (if (or (eq type 'table)
  18150. ;; Check if point is at a TBLFM line.
  18151. (and (eq type 'table-row)
  18152. (= (point) (org-element-property :end context))))
  18153. (save-excursion
  18154. (goto-char (org-element-property :contents-begin context))
  18155. (org-call-with-arg 'org-table-recalculate (or arg t))
  18156. (orgtbl-send-table 'maybe))
  18157. (org-table-maybe-eval-formula)
  18158. (cond (arg (call-interactively 'org-table-recalculate))
  18159. ((org-table-maybe-recalculate-line))
  18160. (t (org-table-align)))))))
  18161. (timestamp (org-timestamp-change 0 'day))
  18162. (otherwise
  18163. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18164. (user-error
  18165. "C-c C-c can do nothing useful at this location")))))))))
  18166. (defun org-mode-restart ()
  18167. "Restart Org-mode, to scan again for special lines.
  18168. Also updates the keyword regular expressions."
  18169. (interactive)
  18170. (org-mode)
  18171. (message "Org-mode restarted"))
  18172. (defun org-kill-note-or-show-branches ()
  18173. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18174. (interactive)
  18175. (if (not org-finish-function)
  18176. (progn
  18177. (hide-subtree)
  18178. (call-interactively 'show-branches))
  18179. (let ((org-note-abort t))
  18180. (funcall org-finish-function))))
  18181. (defun org-return (&optional indent)
  18182. "Goto next table row or insert a newline.
  18183. Calls `org-table-next-row' or `newline', depending on context.
  18184. See the individual commands for more information."
  18185. (interactive)
  18186. (let (org-ts-what)
  18187. (cond
  18188. ((or (bobp) (org-in-src-block-p))
  18189. (if indent (newline-and-indent) (newline)))
  18190. ((org-at-table-p)
  18191. (org-table-justify-field-maybe)
  18192. (call-interactively 'org-table-next-row))
  18193. ;; when `newline-and-indent' is called within a list, make sure
  18194. ;; text moved stays inside the item.
  18195. ((and (org-in-item-p) indent)
  18196. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18197. (progn
  18198. (save-match-data (newline))
  18199. (org-indent-line-to (length (match-string 0))))
  18200. (let ((ind (org-get-indentation)))
  18201. (newline)
  18202. (if (org-looking-back org-list-end-re)
  18203. (org-indent-line)
  18204. (org-indent-line-to ind)))))
  18205. ((and org-return-follows-link
  18206. (org-at-timestamp-p t)
  18207. (not (eq org-ts-what 'after)))
  18208. (org-follow-timestamp-link))
  18209. ((and org-return-follows-link
  18210. (let ((tprop (get-text-property (point) 'face)))
  18211. (or (eq tprop 'org-link)
  18212. (and (listp tprop) (memq 'org-link tprop)))))
  18213. (call-interactively 'org-open-at-point))
  18214. ((and (org-at-heading-p)
  18215. (looking-at
  18216. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18217. (org-show-entry)
  18218. (end-of-line 1)
  18219. (newline))
  18220. (t (if indent (newline-and-indent) (newline))))))
  18221. (defun org-return-indent ()
  18222. "Goto next table row or insert a newline and indent.
  18223. Calls `org-table-next-row' or `newline-and-indent', depending on
  18224. context. See the individual commands for more information."
  18225. (interactive)
  18226. (org-return t))
  18227. (defun org-ctrl-c-star ()
  18228. "Compute table, or change heading status of lines.
  18229. Calls `org-table-recalculate' or `org-toggle-heading',
  18230. depending on context."
  18231. (interactive)
  18232. (cond
  18233. ((org-at-table-p)
  18234. (call-interactively 'org-table-recalculate))
  18235. (t
  18236. ;; Convert all lines in region to list items
  18237. (call-interactively 'org-toggle-heading))))
  18238. (defun org-ctrl-c-minus ()
  18239. "Insert separator line in table or modify bullet status of line.
  18240. Also turns a plain line or a region of lines into list items.
  18241. Calls `org-table-insert-hline', `org-toggle-item', or
  18242. `org-cycle-list-bullet', depending on context."
  18243. (interactive)
  18244. (cond
  18245. ((org-at-table-p)
  18246. (call-interactively 'org-table-insert-hline))
  18247. ((org-region-active-p)
  18248. (call-interactively 'org-toggle-item))
  18249. ((org-in-item-p)
  18250. (call-interactively 'org-cycle-list-bullet))
  18251. (t
  18252. (call-interactively 'org-toggle-item))))
  18253. (defun org-toggle-item (arg)
  18254. "Convert headings or normal lines to items, items to normal lines.
  18255. If there is no active region, only the current line is considered.
  18256. If the first non blank line in the region is an headline, convert
  18257. all headlines to items, shifting text accordingly.
  18258. If it is an item, convert all items to normal lines.
  18259. If it is normal text, change region into an item. With a prefix
  18260. argument ARG, change each line in region into an item."
  18261. (interactive "P")
  18262. (let ((shift-text
  18263. (function
  18264. ;; Shift text in current section to IND, from point to END.
  18265. ;; The function leaves point to END line.
  18266. (lambda (ind end)
  18267. (let ((min-i 1000) (end (copy-marker end)))
  18268. ;; First determine the minimum indentation (MIN-I) of
  18269. ;; the text.
  18270. (save-excursion
  18271. (catch 'exit
  18272. (while (< (point) end)
  18273. (let ((i (org-get-indentation)))
  18274. (cond
  18275. ;; Skip blank lines and inline tasks.
  18276. ((looking-at "^[ \t]*$"))
  18277. ((looking-at org-outline-regexp-bol))
  18278. ;; We can't find less than 0 indentation.
  18279. ((zerop i) (throw 'exit (setq min-i 0)))
  18280. ((< i min-i) (setq min-i i))))
  18281. (forward-line))))
  18282. ;; Then indent each line so that a line indented to
  18283. ;; MIN-I becomes indented to IND. Ignore blank lines
  18284. ;; and inline tasks in the process.
  18285. (let ((delta (- ind min-i)))
  18286. (while (< (point) end)
  18287. (unless (or (looking-at "^[ \t]*$")
  18288. (looking-at org-outline-regexp-bol))
  18289. (org-indent-line-to (+ (org-get-indentation) delta)))
  18290. (forward-line)))))))
  18291. (skip-blanks
  18292. (function
  18293. ;; Return beginning of first non-blank line, starting from
  18294. ;; line at POS.
  18295. (lambda (pos)
  18296. (save-excursion
  18297. (goto-char pos)
  18298. (skip-chars-forward " \r\t\n")
  18299. (point-at-bol)))))
  18300. beg end)
  18301. ;; Determine boundaries of changes.
  18302. (if (org-region-active-p)
  18303. (setq beg (funcall skip-blanks (region-beginning))
  18304. end (copy-marker (region-end)))
  18305. (setq beg (funcall skip-blanks (point-at-bol))
  18306. end (copy-marker (point-at-eol))))
  18307. ;; Depending on the starting line, choose an action on the text
  18308. ;; between BEG and END.
  18309. (org-with-limited-levels
  18310. (save-excursion
  18311. (goto-char beg)
  18312. (cond
  18313. ;; Case 1. Start at an item: de-itemize. Note that it only
  18314. ;; happens when a region is active: `org-ctrl-c-minus'
  18315. ;; would call `org-cycle-list-bullet' otherwise.
  18316. ((org-at-item-p)
  18317. (while (< (point) end)
  18318. (when (org-at-item-p)
  18319. (skip-chars-forward " \t")
  18320. (delete-region (point) (match-end 0)))
  18321. (forward-line)))
  18322. ;; Case 2. Start at an heading: convert to items.
  18323. ((org-at-heading-p)
  18324. (let* ((bul (org-list-bullet-string "-"))
  18325. (bul-len (length bul))
  18326. ;; Indentation of the first heading. It should be
  18327. ;; relative to the indentation of its parent, if any.
  18328. (start-ind (save-excursion
  18329. (cond
  18330. ((not org-adapt-indentation) 0)
  18331. ((not (outline-previous-heading)) 0)
  18332. (t (length (match-string 0))))))
  18333. ;; Level of first heading. Further headings will be
  18334. ;; compared to it to determine hierarchy in the list.
  18335. (ref-level (org-reduced-level (org-outline-level))))
  18336. (while (< (point) end)
  18337. (let* ((level (org-reduced-level (org-outline-level)))
  18338. (delta (max 0 (- level ref-level))))
  18339. ;; If current headline is less indented than the first
  18340. ;; one, set it as reference, in order to preserve
  18341. ;; subtrees.
  18342. (when (< level ref-level) (setq ref-level level))
  18343. (replace-match bul t t)
  18344. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18345. ;; Ensure all text down to END (or SECTION-END) belongs
  18346. ;; to the newly created item.
  18347. (let ((section-end (save-excursion
  18348. (or (outline-next-heading) (point)))))
  18349. (forward-line)
  18350. (funcall shift-text
  18351. (+ start-ind (* (1+ delta) bul-len))
  18352. (min end section-end)))))))
  18353. ;; Case 3. Normal line with ARG: turn each non-item line into
  18354. ;; an item.
  18355. (arg
  18356. (while (< (point) end)
  18357. (unless (or (org-at-heading-p) (org-at-item-p))
  18358. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18359. (replace-match
  18360. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18361. (forward-line)))
  18362. ;; Case 4. Normal line without ARG: make the first line of
  18363. ;; region an item, and shift indentation of others
  18364. ;; lines to set them as item's body.
  18365. (t (let* ((bul (org-list-bullet-string "-"))
  18366. (bul-len (length bul))
  18367. (ref-ind (org-get-indentation)))
  18368. (skip-chars-forward " \t")
  18369. (insert bul)
  18370. (forward-line)
  18371. (while (< (point) end)
  18372. ;; Ensure that lines less indented than first one
  18373. ;; still get included in item body.
  18374. (funcall shift-text
  18375. (+ ref-ind bul-len)
  18376. (min end (save-excursion (or (outline-next-heading)
  18377. (point)))))
  18378. (forward-line)))))))))
  18379. (defun org-toggle-heading (&optional nstars)
  18380. "Convert headings to normal text, or items or text to headings.
  18381. If there is no active region, only the current line is considered.
  18382. With a \\[universal-argument] prefix, convert the whole list at
  18383. point into heading.
  18384. In a region:
  18385. - If the first non blank line is an headline, remove the stars
  18386. from all headlines in the region.
  18387. - If it is a normal line turn each and every normal line (i.e. not an
  18388. heading or an item) in the region into a heading.
  18389. - If it is a plain list item, turn all plain list items into headings.
  18390. When converting a line into a heading, the number of stars is chosen
  18391. such that the lines become children of the current entry. However,
  18392. when a prefix argument is given, its value determines the number of
  18393. stars to add."
  18394. (interactive "P")
  18395. (let ((skip-blanks
  18396. (function
  18397. ;; Return beginning of first non-blank line, starting from
  18398. ;; line at POS.
  18399. (lambda (pos)
  18400. (save-excursion
  18401. (goto-char pos)
  18402. (while (org-at-comment-p) (forward-line))
  18403. (skip-chars-forward " \r\t\n")
  18404. (point-at-bol)))))
  18405. beg end toggled)
  18406. ;; Determine boundaries of changes. If a universal prefix has
  18407. ;; been given, put the list in a region. If region ends at a bol,
  18408. ;; do not consider the last line to be in the region.
  18409. (when (and current-prefix-arg (org-at-item-p))
  18410. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18411. (org-mark-element))
  18412. (if (org-region-active-p)
  18413. (setq beg (funcall skip-blanks (region-beginning))
  18414. end (copy-marker (save-excursion
  18415. (goto-char (region-end))
  18416. (if (bolp) (point) (point-at-eol)))))
  18417. (setq beg (funcall skip-blanks (point-at-bol))
  18418. end (copy-marker (point-at-eol))))
  18419. ;; Ensure inline tasks don't count as headings.
  18420. (org-with-limited-levels
  18421. (save-excursion
  18422. (goto-char beg)
  18423. (cond
  18424. ;; Case 1. Started at an heading: de-star headings.
  18425. ((org-at-heading-p)
  18426. (while (< (point) end)
  18427. (when (org-at-heading-p t)
  18428. (looking-at org-outline-regexp) (replace-match "")
  18429. (setq toggled t))
  18430. (forward-line)))
  18431. ;; Case 2. Started at an item: change items into headlines.
  18432. ;; One star will be added by `org-list-to-subtree'.
  18433. ((org-at-item-p)
  18434. (let* ((stars (make-string
  18435. (if nstars
  18436. ;; subtract the star that will be added again by
  18437. ;; `org-list-to-subtree'
  18438. (1- (prefix-numeric-value current-prefix-arg))
  18439. (or (org-current-level) 0))
  18440. ?*))
  18441. (add-stars
  18442. (cond (nstars "") ; stars from prefix only
  18443. ((equal stars "") "") ; before first heading
  18444. (org-odd-levels-only "*") ; inside heading, odd
  18445. (t "")))) ; inside heading, oddeven
  18446. (while (< (point) end)
  18447. (when (org-at-item-p)
  18448. ;; Pay attention to cases when region ends before list.
  18449. (let* ((struct (org-list-struct))
  18450. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18451. (save-restriction
  18452. (narrow-to-region (point) list-end)
  18453. (insert
  18454. (org-list-to-subtree
  18455. (org-list-parse-list t)
  18456. '(:istart (concat stars add-stars (funcall get-stars depth))
  18457. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18458. (setq toggled t))
  18459. (forward-line))))
  18460. ;; Case 3. Started at normal text: make every line an heading,
  18461. ;; skipping headlines and items.
  18462. (t (let* ((stars (make-string
  18463. (if nstars
  18464. (prefix-numeric-value current-prefix-arg)
  18465. (or (org-current-level) 0))
  18466. ?*))
  18467. (add-stars
  18468. (cond (nstars "") ; stars from prefix only
  18469. ((equal stars "") "*") ; before first heading
  18470. (org-odd-levels-only "**") ; inside heading, odd
  18471. (t "*"))) ; inside heading, oddeven
  18472. (rpl (concat stars add-stars " ")))
  18473. (while (< (point) end)
  18474. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18475. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18476. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18477. (forward-line)))))))
  18478. (unless toggled (message "Cannot toggle heading from here"))))
  18479. (defun org-meta-return (&optional arg)
  18480. "Insert a new heading or wrap a region in a table.
  18481. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18482. See the individual commands for more information."
  18483. (interactive "P")
  18484. (cond
  18485. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18486. ((or (org-at-drawer-p) (org-at-property-p))
  18487. (newline-and-indent))
  18488. ((org-at-table-p)
  18489. (call-interactively 'org-table-wrap-region))
  18490. (t (call-interactively 'org-insert-heading))))
  18491. ;;; Menu entries
  18492. (defsubst org-in-subtree-not-table-p ()
  18493. "Are we in a subtree and not in a table?"
  18494. (and (not (org-before-first-heading-p))
  18495. (not (org-at-table-p))))
  18496. ;; Define the Org-mode menus
  18497. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18498. '("Tbl"
  18499. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18500. ["Next Field" org-cycle (org-at-table-p)]
  18501. ["Previous Field" org-shifttab (org-at-table-p)]
  18502. ["Next Row" org-return (org-at-table-p)]
  18503. "--"
  18504. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18505. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18506. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18507. "--"
  18508. ("Column"
  18509. ["Move Column Left" org-metaleft (org-at-table-p)]
  18510. ["Move Column Right" org-metaright (org-at-table-p)]
  18511. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18512. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18513. ("Row"
  18514. ["Move Row Up" org-metaup (org-at-table-p)]
  18515. ["Move Row Down" org-metadown (org-at-table-p)]
  18516. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18517. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18518. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18519. "--"
  18520. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18521. ("Rectangle"
  18522. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18523. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18524. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18525. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18526. "--"
  18527. ("Calculate"
  18528. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18529. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18530. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18531. "--"
  18532. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18533. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18534. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18535. "--"
  18536. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18537. "--"
  18538. ["Sum Column/Rectangle" org-table-sum
  18539. (or (org-at-table-p) (org-region-active-p))]
  18540. ["Which Column?" org-table-current-column (org-at-table-p)])
  18541. ["Debug Formulas"
  18542. org-table-toggle-formula-debugger
  18543. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18544. ["Show Col/Row Numbers"
  18545. org-table-toggle-coordinate-overlays
  18546. :style toggle
  18547. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18548. "--"
  18549. ["Create" org-table-create (and (not (org-at-table-p))
  18550. org-enable-table-editor)]
  18551. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18552. ["Import from File" org-table-import (not (org-at-table-p))]
  18553. ["Export to File" org-table-export (org-at-table-p)]
  18554. "--"
  18555. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18556. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18557. '("Org"
  18558. ("Show/Hide"
  18559. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18560. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18561. ["Sparse Tree..." org-sparse-tree t]
  18562. ["Reveal Context" org-reveal t]
  18563. ["Show All" show-all t]
  18564. "--"
  18565. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18566. "--"
  18567. ["New Heading" org-insert-heading t]
  18568. ("Navigate Headings"
  18569. ["Up" outline-up-heading t]
  18570. ["Next" outline-next-visible-heading t]
  18571. ["Previous" outline-previous-visible-heading t]
  18572. ["Next Same Level" outline-forward-same-level t]
  18573. ["Previous Same Level" outline-backward-same-level t]
  18574. "--"
  18575. ["Jump" org-goto t])
  18576. ("Edit Structure"
  18577. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18578. "--"
  18579. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18580. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18581. "--"
  18582. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18583. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18584. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18585. "--"
  18586. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18587. "--"
  18588. ["Copy visible text" org-copy-visible t]
  18589. "--"
  18590. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18591. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18592. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18593. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18594. "--"
  18595. ["Sort Region/Children" org-sort t]
  18596. "--"
  18597. ["Convert to odd levels" org-convert-to-odd-levels t]
  18598. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18599. ("Editing"
  18600. ["Emphasis..." org-emphasize t]
  18601. ["Edit Source Example" org-edit-special t]
  18602. "--"
  18603. ["Footnote new/jump" org-footnote-action t]
  18604. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18605. ("Archive"
  18606. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18607. "--"
  18608. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18609. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18610. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18611. )
  18612. "--"
  18613. ("Hyperlinks"
  18614. ["Store Link (Global)" org-store-link t]
  18615. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18616. ["Insert Link" org-insert-link t]
  18617. ["Follow Link" org-open-at-point t]
  18618. "--"
  18619. ["Next link" org-next-link t]
  18620. ["Previous link" org-previous-link t]
  18621. "--"
  18622. ["Descriptive Links"
  18623. org-toggle-link-display
  18624. :style radio
  18625. :selected org-descriptive-links
  18626. ]
  18627. ["Literal Links"
  18628. org-toggle-link-display
  18629. :style radio
  18630. :selected (not org-descriptive-links)])
  18631. "--"
  18632. ("TODO Lists"
  18633. ["TODO/DONE/-" org-todo t]
  18634. ("Select keyword"
  18635. ["Next keyword" org-shiftright (org-at-heading-p)]
  18636. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18637. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18638. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18639. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18640. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18641. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18642. "--"
  18643. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18644. :selected org-enforce-todo-dependencies :style toggle :active t]
  18645. "Settings for tree at point"
  18646. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18647. :selected (org-entry-get nil "ORDERED")
  18648. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18649. ["Do Children parallel" org-toggle-ordered-property :style radio
  18650. :selected (not (org-entry-get nil "ORDERED"))
  18651. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18652. "--"
  18653. ["Set Priority" org-priority t]
  18654. ["Priority Up" org-shiftup t]
  18655. ["Priority Down" org-shiftdown t]
  18656. "--"
  18657. ["Get news from all feeds" org-feed-update-all t]
  18658. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18659. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18660. ("TAGS and Properties"
  18661. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18662. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18663. "--"
  18664. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18665. ["Column view of properties" org-columns t]
  18666. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18667. ("Dates and Scheduling"
  18668. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18669. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18670. ("Change Date"
  18671. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18672. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18673. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18674. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18675. ["Compute Time Range" org-evaluate-time-range t]
  18676. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18677. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18678. "--"
  18679. ["Custom time format" org-toggle-time-stamp-overlays
  18680. :style radio :selected org-display-custom-times]
  18681. "--"
  18682. ["Goto Calendar" org-goto-calendar t]
  18683. ["Date from Calendar" org-date-from-calendar t]
  18684. "--"
  18685. ["Start/Restart Timer" org-timer-start t]
  18686. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18687. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18688. ["Insert Timer String" org-timer t]
  18689. ["Insert Timer Item" org-timer-item t])
  18690. ("Logging work"
  18691. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18692. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18693. ["Clock out" org-clock-out t]
  18694. ["Clock cancel" org-clock-cancel t]
  18695. "--"
  18696. ["Mark as default task" org-clock-mark-default-task t]
  18697. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18698. ["Goto running clock" org-clock-goto t]
  18699. "--"
  18700. ["Display times" org-clock-display t]
  18701. ["Create clock table" org-clock-report t]
  18702. "--"
  18703. ["Record DONE time"
  18704. (progn (setq org-log-done (not org-log-done))
  18705. (message "Switching to %s will %s record a timestamp"
  18706. (car org-done-keywords)
  18707. (if org-log-done "automatically" "not")))
  18708. :style toggle :selected org-log-done])
  18709. "--"
  18710. ["Agenda Command..." org-agenda t]
  18711. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18712. ("File List for Agenda")
  18713. ("Special views current file"
  18714. ["TODO Tree" org-show-todo-tree t]
  18715. ["Check Deadlines" org-check-deadlines t]
  18716. ["Timeline" org-timeline t]
  18717. ["Tags/Property tree" org-match-sparse-tree t])
  18718. "--"
  18719. ["Export/Publish..." org-export-dispatch t]
  18720. ("LaTeX"
  18721. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18722. :selected org-cdlatex-mode]
  18723. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18724. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18725. ["Modify math symbol" org-cdlatex-math-modify
  18726. (org-inside-LaTeX-fragment-p)]
  18727. ["Insert citation" org-reftex-citation t]
  18728. "--"
  18729. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18730. "--"
  18731. ("MobileOrg"
  18732. ["Push Files and Views" org-mobile-push t]
  18733. ["Get Captured and Flagged" org-mobile-pull t]
  18734. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18735. "--"
  18736. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18737. "--"
  18738. ("Documentation"
  18739. ["Show Version" org-version t]
  18740. ["Info Documentation" org-info t])
  18741. ("Customize"
  18742. ["Browse Org Group" org-customize t]
  18743. "--"
  18744. ["Expand This Menu" org-create-customize-menu
  18745. (fboundp 'customize-menu-create)])
  18746. ["Send bug report" org-submit-bug-report t]
  18747. "--"
  18748. ("Refresh/Reload"
  18749. ["Refresh setup current buffer" org-mode-restart t]
  18750. ["Reload Org (after update)" org-reload t]
  18751. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18752. ))
  18753. (defun org-info (&optional node)
  18754. "Read documentation for Org-mode in the info system.
  18755. With optional NODE, go directly to that node."
  18756. (interactive)
  18757. (info (format "(org)%s" (or node ""))))
  18758. ;;;###autoload
  18759. (defun org-submit-bug-report ()
  18760. "Submit a bug report on Org-mode via mail.
  18761. Don't hesitate to report any problems or inaccurate documentation.
  18762. If you don't have setup sending mail from (X)Emacs, please copy the
  18763. output buffer into your mail program, as it gives us important
  18764. information about your Org-mode version and configuration."
  18765. (interactive)
  18766. (require 'reporter)
  18767. (org-load-modules-maybe)
  18768. (org-require-autoloaded-modules)
  18769. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18770. (reporter-submit-bug-report
  18771. "emacs-orgmode@gnu.org"
  18772. (org-version nil 'full)
  18773. (let (list)
  18774. (save-window-excursion
  18775. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18776. (delete-other-windows)
  18777. (erase-buffer)
  18778. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18779. We would like to add your full Org-mode and Outline configuration to the
  18780. bug report. This greatly simplifies the work of the maintainer and
  18781. other experts on the mailing list.
  18782. HOWEVER, some variables you have customized may contain private
  18783. information. The names of customers, colleagues, or friends, might
  18784. appear in the form of file names, tags, todo states, or search strings.
  18785. If you answer yes to the prompt, you might want to check and remove
  18786. such private information before sending the email.")
  18787. (add-text-properties (point-min) (point-max) '(face org-warning))
  18788. (when (yes-or-no-p "Include your Org-mode configuration ")
  18789. (mapatoms
  18790. (lambda (v)
  18791. (and (boundp v)
  18792. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18793. (or (and (symbol-value v)
  18794. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18795. (and
  18796. (get v 'custom-type) (get v 'standard-value)
  18797. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18798. (push v list)))))
  18799. (kill-buffer (get-buffer "*Warn about privacy*"))
  18800. list))
  18801. nil nil
  18802. "Remember to cover the basics, that is, what you expected to happen and
  18803. what in fact did happen. You don't know how to make a good report? See
  18804. http://orgmode.org/manual/Feedback.html#Feedback
  18805. Your bug report will be posted to the Org-mode mailing list.
  18806. ------------------------------------------------------------------------")
  18807. (save-excursion
  18808. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18809. (replace-match "\\1Bug: \\3 [\\2]")))))
  18810. (defun org-install-agenda-files-menu ()
  18811. (let ((bl (buffer-list)))
  18812. (save-excursion
  18813. (while bl
  18814. (set-buffer (pop bl))
  18815. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18816. (when (derived-mode-p 'org-mode)
  18817. (easy-menu-change
  18818. '("Org") "File List for Agenda"
  18819. (append
  18820. (list
  18821. ["Edit File List" (org-edit-agenda-file-list) t]
  18822. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18823. ["Remove Current File from List" org-remove-file t]
  18824. ["Cycle through agenda files" org-cycle-agenda-files t]
  18825. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18826. "--")
  18827. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18828. ;;;; Documentation
  18829. (defun org-require-autoloaded-modules ()
  18830. (interactive)
  18831. (mapc 'require
  18832. '(org-agenda org-archive org-attach org-clock org-colview org-id
  18833. org-remember org-table org-timer)))
  18834. ;;;###autoload
  18835. (defun org-reload (&optional uncompiled)
  18836. "Reload all org lisp files.
  18837. With prefix arg UNCOMPILED, load the uncompiled versions."
  18838. (interactive "P")
  18839. (require 'loadhist)
  18840. (let* ((org-dir (org-find-library-dir "org"))
  18841. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18842. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  18843. (remove-re (mapconcat 'identity
  18844. (mapcar (lambda (f) (concat "^" f "$"))
  18845. (list (if (featurep 'xemacs)
  18846. "org-colview"
  18847. "org-colview-xemacs")
  18848. "org" "org-loaddefs" "org-version"))
  18849. "\\|"))
  18850. (feats (delete-dups
  18851. (mapcar 'file-name-sans-extension
  18852. (mapcar 'file-name-nondirectory
  18853. (delq nil
  18854. (mapcar 'feature-file
  18855. features))))))
  18856. (lfeat (append
  18857. (sort
  18858. (setq feats
  18859. (delq nil (mapcar
  18860. (lambda (f)
  18861. (if (and (string-match feature-re f)
  18862. (not (string-match remove-re f)))
  18863. f nil))
  18864. feats)))
  18865. 'string-lessp)
  18866. (list "org-version" "org")))
  18867. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18868. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18869. load-uncore load-misses)
  18870. (setq load-misses
  18871. (delq 't
  18872. (mapcar (lambda (f)
  18873. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18874. (and (string= org-dir contrib-dir)
  18875. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18876. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18877. (add-to-list 'load-uncore f 'append)
  18878. 't)
  18879. f))
  18880. lfeat)))
  18881. (if load-uncore
  18882. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18883. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18884. (if load-misses
  18885. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18886. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18887. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18888. ;;;###autoload
  18889. (defun org-customize ()
  18890. "Call the customize function with org as argument."
  18891. (interactive)
  18892. (org-load-modules-maybe)
  18893. (org-require-autoloaded-modules)
  18894. (customize-browse 'org))
  18895. (defun org-create-customize-menu ()
  18896. "Create a full customization menu for Org-mode, insert it into the menu."
  18897. (interactive)
  18898. (org-load-modules-maybe)
  18899. (org-require-autoloaded-modules)
  18900. (if (fboundp 'customize-menu-create)
  18901. (progn
  18902. (easy-menu-change
  18903. '("Org") "Customize"
  18904. `(["Browse Org group" org-customize t]
  18905. "--"
  18906. ,(customize-menu-create 'org)
  18907. ["Set" Custom-set t]
  18908. ["Save" Custom-save t]
  18909. ["Reset to Current" Custom-reset-current t]
  18910. ["Reset to Saved" Custom-reset-saved t]
  18911. ["Reset to Standard Settings" Custom-reset-standard t]))
  18912. (message "\"Org\"-menu now contains full customization menu"))
  18913. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18914. ;;;; Miscellaneous stuff
  18915. ;;; Generally useful functions
  18916. (defun org-get-at-bol (property)
  18917. "Get text property PROPERTY at beginning of line."
  18918. (get-text-property (point-at-bol) property))
  18919. (defun org-find-text-property-in-string (prop s)
  18920. "Return the first non-nil value of property PROP in string S."
  18921. (or (get-text-property 0 prop s)
  18922. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18923. prop s)))
  18924. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18925. "Display the given MESSAGE as a warning."
  18926. (if (fboundp 'display-warning)
  18927. (display-warning 'org message
  18928. (if (featurep 'xemacs) 'warning :warning))
  18929. (let ((buf (get-buffer-create "*Org warnings*")))
  18930. (with-current-buffer buf
  18931. (goto-char (point-max))
  18932. (insert "Warning (Org): " message)
  18933. (unless (bolp)
  18934. (newline)))
  18935. (display-buffer buf)
  18936. (sit-for 0))))
  18937. (defun org-eval (form)
  18938. "Eval FORM and return result."
  18939. (condition-case error
  18940. (eval form)
  18941. (error (format "%%![Error: %s]" error))))
  18942. (defun org-in-clocktable-p ()
  18943. "Check if the cursor is in a clocktable."
  18944. (let ((pos (point)) start)
  18945. (save-excursion
  18946. (end-of-line 1)
  18947. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18948. (setq start (match-beginning 0))
  18949. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18950. (>= (match-end 0) pos)
  18951. start))))
  18952. (defun org-in-commented-line ()
  18953. "Is point in a line starting with `#'?"
  18954. (equal (char-after (point-at-bol)) ?#))
  18955. (defun org-in-indented-comment-line ()
  18956. "Is point in a line starting with `#' after some white space?"
  18957. (save-excursion
  18958. (save-match-data
  18959. (goto-char (point-at-bol))
  18960. (looking-at "[ \t]*#"))))
  18961. (defun org-in-verbatim-emphasis ()
  18962. (save-match-data
  18963. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18964. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18965. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18966. (if (and marker (marker-buffer marker)
  18967. (buffer-live-p (marker-buffer marker)))
  18968. (progn
  18969. (org-pop-to-buffer-same-window (marker-buffer marker))
  18970. (if (or (> marker (point-max)) (< marker (point-min)))
  18971. (widen))
  18972. (goto-char marker)
  18973. (org-show-context 'org-goto))
  18974. (if bookmark
  18975. (bookmark-jump bookmark)
  18976. (error "Cannot find location"))))
  18977. (defun org-quote-csv-field (s)
  18978. "Quote field for inclusion in CSV material."
  18979. (if (string-match "[\",]" s)
  18980. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18981. s))
  18982. (defun org-force-self-insert (N)
  18983. "Needed to enforce self-insert under remapping."
  18984. (interactive "p")
  18985. (self-insert-command N))
  18986. (defun org-string-width (s)
  18987. "Compute width of string, ignoring invisible characters.
  18988. This ignores character with invisibility property `org-link', and also
  18989. characters with property `org-cwidth', because these will become invisible
  18990. upon the next fontification round."
  18991. (let (b l)
  18992. (when (or (eq t buffer-invisibility-spec)
  18993. (assq 'org-link buffer-invisibility-spec))
  18994. (while (setq b (text-property-any 0 (length s)
  18995. 'invisible 'org-link s))
  18996. (setq s (concat (substring s 0 b)
  18997. (substring s (or (next-single-property-change
  18998. b 'invisible s) (length s)))))))
  18999. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19000. (setq s (concat (substring s 0 b)
  19001. (substring s (or (next-single-property-change
  19002. b 'org-cwidth s) (length s))))))
  19003. (setq l (string-width s) b -1)
  19004. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19005. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19006. l))
  19007. (defun org-shorten-string (s maxlength)
  19008. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19009. If the string is shorter or has length MAXLENGTH, just return the
  19010. original string. If it is longer, the functions finds a space in the
  19011. string, breaks this string off at that locations and adds three dots
  19012. as ellipsis. Including the ellipsis, the string will not be longer
  19013. than MAXLENGTH. If finding a good breaking point in the string does
  19014. not work, the string is just chopped off in the middle of a word
  19015. if necessary."
  19016. (if (<= (length s) maxlength)
  19017. s
  19018. (let* ((n (max (- maxlength 4) 1))
  19019. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19020. (if (string-match re s)
  19021. (concat (match-string 1 s) "...")
  19022. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19023. (defun org-get-indentation (&optional line)
  19024. "Get the indentation of the current line, interpreting tabs.
  19025. When LINE is given, assume it represents a line and compute its indentation."
  19026. (if line
  19027. (if (string-match "^ *" (org-remove-tabs line))
  19028. (match-end 0))
  19029. (save-excursion
  19030. (beginning-of-line 1)
  19031. (skip-chars-forward " \t")
  19032. (current-column))))
  19033. (defun org-get-string-indentation (s)
  19034. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19035. (let ((n -1) (i 0) (w tab-width) c)
  19036. (catch 'exit
  19037. (while (< (setq n (1+ n)) (length s))
  19038. (setq c (aref s n))
  19039. (cond ((= c ?\ ) (setq i (1+ i)))
  19040. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19041. (t (throw 'exit t)))))
  19042. i))
  19043. (defun org-remove-tabs (s &optional width)
  19044. "Replace tabulators in S with spaces.
  19045. Assumes that s is a single line, starting in column 0."
  19046. (setq width (or width tab-width))
  19047. (while (string-match "\t" s)
  19048. (setq s (replace-match
  19049. (make-string
  19050. (- (* width (/ (+ (match-beginning 0) width) width))
  19051. (match-beginning 0)) ?\ )
  19052. t t s)))
  19053. s)
  19054. (defun org-fix-indentation (line ind)
  19055. "Fix indentation in LINE.
  19056. IND is a cons cell with target and minimum indentation.
  19057. If the current indentation in LINE is smaller than the minimum,
  19058. leave it alone. If it is larger than ind, set it to the target."
  19059. (let* ((l (org-remove-tabs line))
  19060. (i (org-get-indentation l))
  19061. (i1 (car ind)) (i2 (cdr ind)))
  19062. (if (>= i i2) (setq l (substring line i2)))
  19063. (if (> i1 0)
  19064. (concat (make-string i1 ?\ ) l)
  19065. l)))
  19066. (defun org-remove-indentation (code &optional n)
  19067. "Remove the maximum common indentation from the lines in CODE.
  19068. N may optionally be the number of spaces to remove."
  19069. (with-temp-buffer
  19070. (insert code)
  19071. (org-do-remove-indentation n)
  19072. (buffer-string)))
  19073. (defun org-do-remove-indentation (&optional n)
  19074. "Remove the maximum common indentation from the buffer."
  19075. (untabify (point-min) (point-max))
  19076. (let ((min 10000) re)
  19077. (if n
  19078. (setq min n)
  19079. (goto-char (point-min))
  19080. (while (re-search-forward "^ *[^ \n]" nil t)
  19081. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19082. (unless (or (= min 0) (= min 10000))
  19083. (setq re (format "^ \\{%d\\}" min))
  19084. (goto-char (point-min))
  19085. (while (re-search-forward re nil t)
  19086. (replace-match "")
  19087. (end-of-line 1))
  19088. min)))
  19089. (defun org-fill-template (template alist)
  19090. "Find each %key of ALIST in TEMPLATE and replace it."
  19091. (let ((case-fold-search nil)
  19092. entry key value)
  19093. (setq alist (sort (copy-sequence alist)
  19094. (lambda (a b) (< (length (car a)) (length (car b))))))
  19095. (while (setq entry (pop alist))
  19096. (setq template
  19097. (replace-regexp-in-string
  19098. (concat "%" (regexp-quote (car entry)))
  19099. (or (cdr entry) "") template t t)))
  19100. template))
  19101. (defun org-base-buffer (buffer)
  19102. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19103. (if (not buffer)
  19104. buffer
  19105. (or (buffer-base-buffer buffer)
  19106. buffer)))
  19107. (defun org-trim (s)
  19108. "Remove whitespace at beginning and end of string."
  19109. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19110. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19111. s)
  19112. (defun org-wrap (string &optional width lines)
  19113. "Wrap string to either a number of lines, or a width in characters.
  19114. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19115. that costs. If there is a word longer than WIDTH, the text is actually
  19116. wrapped to the length of that word.
  19117. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19118. many lines, whatever width that takes.
  19119. The return value is a list of lines, without newlines at the end."
  19120. (let* ((words (org-split-string string "[ \t\n]+"))
  19121. (maxword (apply 'max (mapcar 'org-string-width words)))
  19122. w ll)
  19123. (cond (width
  19124. (org-do-wrap words (max maxword width)))
  19125. (lines
  19126. (setq w maxword)
  19127. (setq ll (org-do-wrap words maxword))
  19128. (if (<= (length ll) lines)
  19129. ll
  19130. (setq ll words)
  19131. (while (> (length ll) lines)
  19132. (setq w (1+ w))
  19133. (setq ll (org-do-wrap words w)))
  19134. ll))
  19135. (t (error "Cannot wrap this")))))
  19136. (defun org-do-wrap (words width)
  19137. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19138. (let (lines line)
  19139. (while words
  19140. (setq line (pop words))
  19141. (while (and words (< (+ (length line) (length (car words))) width))
  19142. (setq line (concat line " " (pop words))))
  19143. (setq lines (push line lines)))
  19144. (nreverse lines)))
  19145. (defun org-split-string (string &optional separators)
  19146. "Splits STRING into substrings at SEPARATORS.
  19147. No empty strings are returned if there are matches at the beginning
  19148. and end of string."
  19149. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19150. (start 0)
  19151. notfirst
  19152. (list nil))
  19153. (while (and (string-match rexp string
  19154. (if (and notfirst
  19155. (= start (match-beginning 0))
  19156. (< start (length string)))
  19157. (1+ start) start))
  19158. (< (match-beginning 0) (length string)))
  19159. (setq notfirst t)
  19160. (or (eq (match-beginning 0) 0)
  19161. (and (eq (match-beginning 0) (match-end 0))
  19162. (eq (match-beginning 0) start))
  19163. (setq list
  19164. (cons (substring string start (match-beginning 0))
  19165. list)))
  19166. (setq start (match-end 0)))
  19167. (or (eq start (length string))
  19168. (setq list
  19169. (cons (substring string start)
  19170. list)))
  19171. (nreverse list)))
  19172. (defun org-quote-vert (s)
  19173. "Replace \"|\" with \"\\vert\"."
  19174. (while (string-match "|" s)
  19175. (setq s (replace-match "\\vert" t t s)))
  19176. s)
  19177. (defun org-uuidgen-p (s)
  19178. "Is S an ID created by UUIDGEN?"
  19179. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19180. (defun org-in-src-block-p (&optional inside)
  19181. "Whether point is in a code source block.
  19182. When INSIDE is non-nil, don't consider we are within a src block
  19183. when point is at #+BEGIN_SRC or #+END_SRC."
  19184. (let ((case-fold-search t) ov)
  19185. (or (and (setq ov (overlays-at (point)))
  19186. (memq 'org-block-background
  19187. (overlay-properties (car ov))))
  19188. (and (not inside)
  19189. (save-match-data
  19190. (save-excursion
  19191. (beginning-of-line)
  19192. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19193. (defun org-context ()
  19194. "Return a list of contexts of the current cursor position.
  19195. If several contexts apply, all are returned.
  19196. Each context entry is a list with a symbol naming the context, and
  19197. two positions indicating start and end of the context. Possible
  19198. contexts are:
  19199. :headline anywhere in a headline
  19200. :headline-stars on the leading stars in a headline
  19201. :todo-keyword on a TODO keyword (including DONE) in a headline
  19202. :tags on the TAGS in a headline
  19203. :priority on the priority cookie in a headline
  19204. :item on the first line of a plain list item
  19205. :item-bullet on the bullet/number of a plain list item
  19206. :checkbox on the checkbox in a plain list item
  19207. :table in an org-mode table
  19208. :table-special on a special filed in a table
  19209. :table-table in a table.el table
  19210. :clocktable in a clocktable
  19211. :src-block in a source block
  19212. :link on a hyperlink
  19213. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19214. :target on a <<target>>
  19215. :radio-target on a <<<radio-target>>>
  19216. :latex-fragment on a LaTeX fragment
  19217. :latex-preview on a LaTeX fragment with overlaid preview image
  19218. This function expects the position to be visible because it uses font-lock
  19219. faces as a help to recognize the following contexts: :table-special, :link,
  19220. and :keyword."
  19221. (let* ((f (get-text-property (point) 'face))
  19222. (faces (if (listp f) f (list f)))
  19223. (case-fold-search t)
  19224. (p (point)) clist o)
  19225. ;; First the large context
  19226. (cond
  19227. ((org-at-heading-p t)
  19228. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19229. (when (progn
  19230. (beginning-of-line 1)
  19231. (looking-at org-todo-line-tags-regexp))
  19232. (push (org-point-in-group p 1 :headline-stars) clist)
  19233. (push (org-point-in-group p 2 :todo-keyword) clist)
  19234. (push (org-point-in-group p 4 :tags) clist))
  19235. (goto-char p)
  19236. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19237. (if (looking-at "\\[#[A-Z0-9]\\]")
  19238. (push (org-point-in-group p 0 :priority) clist)))
  19239. ((org-at-item-p)
  19240. (push (org-point-in-group p 2 :item-bullet) clist)
  19241. (push (list :item (point-at-bol)
  19242. (save-excursion (org-end-of-item) (point)))
  19243. clist)
  19244. (and (org-at-item-checkbox-p)
  19245. (push (org-point-in-group p 0 :checkbox) clist)))
  19246. ((org-at-table-p)
  19247. (push (list :table (org-table-begin) (org-table-end)) clist)
  19248. (if (memq 'org-formula faces)
  19249. (push (list :table-special
  19250. (previous-single-property-change p 'face)
  19251. (next-single-property-change p 'face)) clist)))
  19252. ((org-at-table-p 'any)
  19253. (push (list :table-table) clist)))
  19254. (goto-char p)
  19255. (let ((case-fold-search t))
  19256. ;; New the "medium" contexts: clocktables, source blocks
  19257. (cond ((org-in-clocktable-p)
  19258. (push (list :clocktable
  19259. (and (or (looking-at "#\\+BEGIN: clocktable")
  19260. (search-backward "#+BEGIN: clocktable" nil t))
  19261. (match-beginning 0))
  19262. (and (re-search-forward "#\\+END:?" nil t)
  19263. (match-end 0))) clist))
  19264. ((org-in-src-block-p)
  19265. (push (list :src-block
  19266. (and (or (looking-at "#\\+BEGIN_SRC")
  19267. (search-backward "#+BEGIN_SRC" nil t))
  19268. (match-beginning 0))
  19269. (and (search-forward "#+END_SRC" nil t)
  19270. (match-beginning 0))) clist))))
  19271. (goto-char p)
  19272. ;; Now the small context
  19273. (cond
  19274. ((org-at-timestamp-p)
  19275. (push (org-point-in-group p 0 :timestamp) clist))
  19276. ((memq 'org-link faces)
  19277. (push (list :link
  19278. (previous-single-property-change p 'face)
  19279. (next-single-property-change p 'face)) clist))
  19280. ((memq 'org-special-keyword faces)
  19281. (push (list :keyword
  19282. (previous-single-property-change p 'face)
  19283. (next-single-property-change p 'face)) clist))
  19284. ((org-at-target-p)
  19285. (push (org-point-in-group p 0 :target) clist)
  19286. (goto-char (1- (match-beginning 0)))
  19287. (if (looking-at org-radio-target-regexp)
  19288. (push (org-point-in-group p 0 :radio-target) clist))
  19289. (goto-char p))
  19290. ((setq o (car (delq nil
  19291. (mapcar
  19292. (lambda (x)
  19293. (if (memq x org-latex-fragment-image-overlays) x))
  19294. (overlays-at (point))))))
  19295. (push (list :latex-fragment
  19296. (overlay-start o) (overlay-end o)) clist)
  19297. (push (list :latex-preview
  19298. (overlay-start o) (overlay-end o)) clist))
  19299. ((org-inside-LaTeX-fragment-p)
  19300. ;; FIXME: positions wrong.
  19301. (push (list :latex-fragment (point) (point)) clist)))
  19302. (setq clist (nreverse (delq nil clist)))
  19303. clist))
  19304. ;; FIXME: Compare with at-regexp-p Do we need both?
  19305. (defun org-in-regexp (re &optional nlines visually)
  19306. "Check if point is inside a match of regexp.
  19307. Normally only the current line is checked, but you can include NLINES extra
  19308. lines both before and after point into the search.
  19309. If VISUALLY is set, require that the cursor is not after the match but
  19310. really on, so that the block visually is on the match."
  19311. (catch 'exit
  19312. (let ((pos (point))
  19313. (eol (point-at-eol (+ 1 (or nlines 0))))
  19314. (inc (if visually 1 0)))
  19315. (save-excursion
  19316. (beginning-of-line (- 1 (or nlines 0)))
  19317. (while (re-search-forward re eol t)
  19318. (if (and (<= (match-beginning 0) pos)
  19319. (>= (+ inc (match-end 0)) pos))
  19320. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19321. (defun org-at-regexp-p (regexp)
  19322. "Is point inside a match of REGEXP in the current line?"
  19323. (catch 'exit
  19324. (save-excursion
  19325. (let ((pos (point)) (end (point-at-eol)))
  19326. (beginning-of-line 1)
  19327. (while (re-search-forward regexp end t)
  19328. (if (and (<= (match-beginning 0) pos)
  19329. (>= (match-end 0) pos))
  19330. (throw 'exit t)))
  19331. nil))))
  19332. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19333. "Non-nil when point is between matches of START-RE and END-RE.
  19334. Also return a non-nil value when point is on one of the matches.
  19335. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19336. buffer positions. Default values are the positions of headlines
  19337. surrounding the point.
  19338. The functions returns a cons cell whose car (resp. cdr) is the
  19339. position before START-RE (resp. after END-RE)."
  19340. (save-match-data
  19341. (let ((pos (point))
  19342. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19343. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19344. beg end)
  19345. (save-excursion
  19346. ;; Point is on a block when on START-RE or if START-RE can be
  19347. ;; found before it...
  19348. (and (or (org-at-regexp-p start-re)
  19349. (re-search-backward start-re limit-up t))
  19350. (setq beg (match-beginning 0))
  19351. ;; ... and END-RE after it...
  19352. (goto-char (match-end 0))
  19353. (re-search-forward end-re limit-down t)
  19354. (> (setq end (match-end 0)) pos)
  19355. ;; ... without another START-RE in-between.
  19356. (goto-char (match-beginning 0))
  19357. (not (re-search-backward start-re (1+ beg) t))
  19358. ;; Return value.
  19359. (cons beg end))))))
  19360. (defun org-in-block-p (names)
  19361. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19362. NAMES is a list of strings containing names of blocks.
  19363. Return first block name matched, or nil. Beware that in case of
  19364. nested blocks, the returned name may not belong to the closest
  19365. block from point."
  19366. (save-match-data
  19367. (catch 'exit
  19368. (let ((case-fold-search t)
  19369. (lim-up (save-excursion (outline-previous-heading)))
  19370. (lim-down (save-excursion (outline-next-heading))))
  19371. (mapc (lambda (name)
  19372. (let ((n (regexp-quote name)))
  19373. (when (org-between-regexps-p
  19374. (concat "^[ \t]*#\\+begin_" n)
  19375. (concat "^[ \t]*#\\+end_" n)
  19376. lim-up lim-down)
  19377. (throw 'exit n))))
  19378. names))
  19379. nil)))
  19380. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19381. "Call `multi-occur' with buffers for all agenda files."
  19382. (interactive "sOrg-files matching: \np")
  19383. (let* ((files (org-agenda-files))
  19384. (tnames (mapcar 'file-truename files))
  19385. (extra org-agenda-text-search-extra-files)
  19386. f)
  19387. (when (eq (car extra) 'agenda-archives)
  19388. (setq extra (cdr extra))
  19389. (setq files (org-add-archive-files files)))
  19390. (while (setq f (pop extra))
  19391. (unless (member (file-truename f) tnames)
  19392. (add-to-list 'files f 'append)
  19393. (add-to-list 'tnames (file-truename f) 'append)))
  19394. (multi-occur
  19395. (mapcar (lambda (x)
  19396. (with-current-buffer
  19397. (or (get-file-buffer x) (find-file-noselect x))
  19398. (widen)
  19399. (current-buffer)))
  19400. files)
  19401. regexp)))
  19402. (if (boundp 'occur-mode-find-occurrence-hook)
  19403. ;; Emacs 23
  19404. (add-hook 'occur-mode-find-occurrence-hook
  19405. (lambda ()
  19406. (when (derived-mode-p 'org-mode)
  19407. (org-reveal))))
  19408. ;; Emacs 22
  19409. (defadvice occur-mode-goto-occurrence
  19410. (after org-occur-reveal activate)
  19411. (and (derived-mode-p 'org-mode) (org-reveal)))
  19412. (defadvice occur-mode-goto-occurrence-other-window
  19413. (after org-occur-reveal activate)
  19414. (and (derived-mode-p 'org-mode) (org-reveal)))
  19415. (defadvice occur-mode-display-occurrence
  19416. (after org-occur-reveal activate)
  19417. (when (derived-mode-p 'org-mode)
  19418. (let ((pos (occur-mode-find-occurrence)))
  19419. (with-current-buffer (marker-buffer pos)
  19420. (save-excursion
  19421. (goto-char pos)
  19422. (org-reveal)))))))
  19423. (defun org-occur-link-in-agenda-files ()
  19424. "Create a link and search for it in the agendas.
  19425. The link is not stored in `org-stored-links', it is just created
  19426. for the search purpose."
  19427. (interactive)
  19428. (let ((link (condition-case nil
  19429. (org-store-link nil)
  19430. (error "Unable to create a link to here"))))
  19431. (org-occur-in-agenda-files (regexp-quote link))))
  19432. (defun org-reverse-string (string)
  19433. "Return the reverse of STRING."
  19434. (apply 'string (reverse (string-to-list string))))
  19435. (defun org-uniquify (list)
  19436. "Remove duplicate elements from LIST."
  19437. (let (res)
  19438. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19439. res))
  19440. (defun org-delete-all (elts list)
  19441. "Remove all elements in ELTS from LIST."
  19442. (while elts
  19443. (setq list (delete (pop elts) list)))
  19444. list)
  19445. (defun org-count (cl-item cl-seq)
  19446. "Count the number of occurrences of ITEM in SEQ.
  19447. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19448. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19449. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19450. (while (< cl-start cl-end)
  19451. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19452. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19453. (setq cl-start (1+ cl-start)))
  19454. cl-count))
  19455. (defun org-remove-if (predicate seq)
  19456. "Remove everything from SEQ that fulfills PREDICATE."
  19457. (let (res e)
  19458. (while seq
  19459. (setq e (pop seq))
  19460. (if (not (funcall predicate e)) (push e res)))
  19461. (nreverse res)))
  19462. (defun org-remove-if-not (predicate seq)
  19463. "Remove everything from SEQ that does not fulfill PREDICATE."
  19464. (let (res e)
  19465. (while seq
  19466. (setq e (pop seq))
  19467. (if (funcall predicate e) (push e res)))
  19468. (nreverse res)))
  19469. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19470. "Reduce two-argument FUNCTION across SEQ.
  19471. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19472. \":initial-value\" removed."
  19473. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19474. (cadr (memq :initial-value cl-keys)))
  19475. (cl-seq (pop cl-seq))
  19476. (t (funcall cl-func)))))
  19477. (while cl-seq
  19478. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19479. cl-accum))
  19480. (defun org-back-over-empty-lines ()
  19481. "Move backwards over whitespace, to the beginning of the first empty line.
  19482. Returns the number of empty lines passed."
  19483. (let ((pos (point)))
  19484. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19485. (skip-chars-backward " \t\n\r")
  19486. (unless (eobp)
  19487. (forward-line -1)))
  19488. (beginning-of-line 2)
  19489. (goto-char (min (point) pos))
  19490. (count-lines (point) pos)))
  19491. (defun org-skip-whitespace ()
  19492. (skip-chars-forward " \t\n\r"))
  19493. (defun org-point-in-group (point group &optional context)
  19494. "Check if POINT is in match-group GROUP.
  19495. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19496. match. If the match group does not exist or point is not inside it,
  19497. return nil."
  19498. (and (match-beginning group)
  19499. (>= point (match-beginning group))
  19500. (<= point (match-end group))
  19501. (if context
  19502. (list context (match-beginning group) (match-end group))
  19503. t)))
  19504. (defun org-switch-to-buffer-other-window (&rest args)
  19505. "Switch to buffer in a second window on the current frame.
  19506. In particular, do not allow pop-up frames.
  19507. Returns the newly created buffer."
  19508. (org-no-popups
  19509. (apply 'switch-to-buffer-other-window args)))
  19510. (defun org-combine-plists (&rest plists)
  19511. "Create a single property list from all plists in PLISTS.
  19512. The process starts by copying the first list, and then setting properties
  19513. from the other lists. Settings in the last list are the most significant
  19514. ones and overrule settings in the other lists."
  19515. (let ((rtn (copy-sequence (pop plists)))
  19516. p v ls)
  19517. (while plists
  19518. (setq ls (pop plists))
  19519. (while ls
  19520. (setq p (pop ls) v (pop ls))
  19521. (setq rtn (plist-put rtn p v))))
  19522. rtn))
  19523. (defun org-replace-escapes (string table)
  19524. "Replace %-escapes in STRING with values in TABLE.
  19525. TABLE is an association list with keys like \"%a\" and string values.
  19526. The sequences in STRING may contain normal field width and padding information,
  19527. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19528. so values can contain further %-escapes if they are define later in TABLE."
  19529. (let ((tbl (copy-alist table))
  19530. (case-fold-search nil)
  19531. (pchg 0)
  19532. e re rpl)
  19533. (while (setq e (pop tbl))
  19534. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19535. (when (and (cdr e) (string-match re (cdr e)))
  19536. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19537. (safe "SREF"))
  19538. (add-text-properties 0 3 (list 'sref sref) safe)
  19539. (setcdr e (replace-match safe t t (cdr e)))))
  19540. (while (string-match re string)
  19541. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19542. (cdr e)))
  19543. (setq string (replace-match rpl t t string))))
  19544. (while (setq pchg (next-property-change pchg string))
  19545. (let ((sref (get-text-property pchg 'sref string)))
  19546. (when (and sref (string-match "SREF" string pchg))
  19547. (setq string (replace-match sref t t string)))))
  19548. string))
  19549. (defun org-sublist (list start end)
  19550. "Return a section of LIST, from START to END.
  19551. Counting starts at 1."
  19552. (let (rtn (c start))
  19553. (setq list (nthcdr (1- start) list))
  19554. (while (and list (<= c end))
  19555. (push (pop list) rtn)
  19556. (setq c (1+ c)))
  19557. (nreverse rtn)))
  19558. (defun org-find-base-buffer-visiting (file)
  19559. "Like `find-buffer-visiting' but always return the base buffer and
  19560. not an indirect buffer."
  19561. (let ((buf (or (get-file-buffer file)
  19562. (find-buffer-visiting file))))
  19563. (if buf
  19564. (or (buffer-base-buffer buf) buf)
  19565. nil)))
  19566. (defun org-image-file-name-regexp (&optional extensions)
  19567. "Return regexp matching the file names of images.
  19568. If EXTENSIONS is given, only match these."
  19569. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19570. (image-file-name-regexp)
  19571. (let ((image-file-name-extensions
  19572. (or extensions
  19573. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19574. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19575. (concat "\\."
  19576. (regexp-opt (nconc (mapcar 'upcase
  19577. image-file-name-extensions)
  19578. image-file-name-extensions)
  19579. t)
  19580. "\\'"))))
  19581. (defun org-file-image-p (file &optional extensions)
  19582. "Return non-nil if FILE is an image."
  19583. (save-match-data
  19584. (string-match (org-image-file-name-regexp extensions) file)))
  19585. (defun org-get-cursor-date (&optional with-time)
  19586. "Return the date at cursor in as a time.
  19587. This works in the calendar and in the agenda, anywhere else it just
  19588. returns the current time.
  19589. If WITH-TIME is non-nil, returns the time of the event at point (in
  19590. the agenda) or the current time of the day."
  19591. (let (date day defd tp tm hod mod)
  19592. (when with-time
  19593. (setq tp (get-text-property (point) 'time))
  19594. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19595. (setq hod (string-to-number (match-string 1 tp))
  19596. mod (string-to-number (match-string 2 tp))))
  19597. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19598. mod (nth 1 (decode-time (current-time))))))
  19599. (cond
  19600. ((eq major-mode 'calendar-mode)
  19601. (setq date (calendar-cursor-to-date)
  19602. defd (encode-time 0 (or mod 0) (or hod 0)
  19603. (nth 1 date) (nth 0 date) (nth 2 date))))
  19604. ((eq major-mode 'org-agenda-mode)
  19605. (setq day (get-text-property (point) 'day))
  19606. (if day
  19607. (setq date (calendar-gregorian-from-absolute day)
  19608. defd (encode-time 0 (or mod 0) (or hod 0)
  19609. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19610. (or defd (current-time))))
  19611. (defun org-mark-subtree (&optional up)
  19612. "Mark the current subtree.
  19613. This puts point at the start of the current subtree, and mark at
  19614. the end. If a numeric prefix UP is given, move up into the
  19615. hierarchy of headlines by UP levels before marking the subtree."
  19616. (interactive "P")
  19617. (org-with-limited-levels
  19618. (cond ((org-at-heading-p) (beginning-of-line))
  19619. ((org-before-first-heading-p) (error "Not in a subtree"))
  19620. (t (outline-previous-visible-heading 1))))
  19621. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19622. (if (org-called-interactively-p 'any)
  19623. (call-interactively 'org-mark-element)
  19624. (org-mark-element)))
  19625. ;;; Indentation
  19626. (defun org-indent-line ()
  19627. "Indent line depending on context."
  19628. (interactive)
  19629. (let* ((pos (point))
  19630. (itemp (org-at-item-p))
  19631. (case-fold-search t)
  19632. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19633. (inline-task-p (and (featurep 'org-inlinetask)
  19634. (org-inlinetask-in-task-p)))
  19635. (inline-re (and inline-task-p
  19636. (org-inlinetask-outline-regexp)))
  19637. column)
  19638. (if (and orgstruct-is-++ (eq pos (point)))
  19639. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19640. (indent-according-to-mode))
  19641. (beginning-of-line 1)
  19642. (cond
  19643. ;; Headings
  19644. ((looking-at org-outline-regexp) (setq column 0))
  19645. ;; Footnote definition
  19646. ((looking-at org-footnote-definition-re) (setq column 0))
  19647. ;; Literal examples
  19648. ((looking-at "[ \t]*:\\( \\|$\\)")
  19649. (setq column (org-get-indentation))) ; do nothing
  19650. ;; Lists
  19651. ((ignore-errors (goto-char (org-in-item-p)))
  19652. (setq column (if itemp
  19653. (org-get-indentation)
  19654. (org-list-item-body-column (point))))
  19655. (goto-char pos))
  19656. ;; Drawers
  19657. ((and (looking-at "[ \t]*:END:")
  19658. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19659. (save-excursion
  19660. (goto-char (1- (match-beginning 1)))
  19661. (setq column (current-column))))
  19662. ;; Special blocks
  19663. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19664. (save-excursion
  19665. (re-search-backward
  19666. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19667. (setq column (org-get-indentation (match-string 0))))
  19668. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19669. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19670. (save-excursion
  19671. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19672. (setq column
  19673. (cond ((equal (downcase (match-string 1)) "src")
  19674. ;; src blocks: let `org-edit-src-exit' handle them
  19675. (org-get-indentation))
  19676. ((equal (downcase (match-string 1)) "example")
  19677. (max (org-get-indentation)
  19678. (org-get-indentation (match-string 0))))
  19679. (t
  19680. (org-get-indentation (match-string 0))))))
  19681. ;; This line has nothing special, look at the previous relevant
  19682. ;; line to compute indentation
  19683. (t
  19684. (beginning-of-line 0)
  19685. (while (and (not (bobp))
  19686. (not (looking-at org-table-line-regexp))
  19687. (not (looking-at org-drawer-regexp))
  19688. ;; When point started in an inline task, do not move
  19689. ;; above task starting line.
  19690. (not (and inline-task-p (looking-at inline-re)))
  19691. ;; Skip drawers, blocks, empty lines, verbatim,
  19692. ;; comments, tables, footnotes definitions, lists,
  19693. ;; inline tasks.
  19694. (or (and (looking-at "[ \t]*:END:")
  19695. (re-search-backward org-drawer-regexp nil t))
  19696. (and (looking-at "[ \t]*#\\+end_")
  19697. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19698. (looking-at "[ \t]*[\n:#|]")
  19699. (looking-at org-footnote-definition-re)
  19700. (and (ignore-errors (goto-char (org-in-item-p)))
  19701. (goto-char
  19702. (org-list-get-top-point (org-list-struct))))
  19703. (and (not inline-task-p)
  19704. (featurep 'org-inlinetask)
  19705. (org-inlinetask-in-task-p)
  19706. (or (org-inlinetask-goto-beginning) t))))
  19707. (beginning-of-line 0))
  19708. (cond
  19709. ;; There was an heading above.
  19710. ((looking-at "\\*+[ \t]+")
  19711. (if (not org-adapt-indentation)
  19712. (setq column 0)
  19713. (goto-char (match-end 0))
  19714. (setq column (current-column))))
  19715. ;; A drawer had started and is unfinished
  19716. ((looking-at org-drawer-regexp)
  19717. (goto-char (1- (match-beginning 1)))
  19718. (setq column (current-column)))
  19719. ;; Else, nothing noticeable found: get indentation and go on.
  19720. (t (setq column (org-get-indentation))))))
  19721. ;; Now apply indentation and move cursor accordingly
  19722. (goto-char pos)
  19723. (if (<= (current-column) (current-indentation))
  19724. (org-indent-line-to column)
  19725. (save-excursion (org-indent-line-to column)))
  19726. ;; Special polishing for properties, see `org-property-format'
  19727. (setq column (current-column))
  19728. (beginning-of-line 1)
  19729. (if (looking-at
  19730. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19731. (replace-match (concat (match-string 1)
  19732. (format org-property-format
  19733. (match-string 2) (match-string 3)))
  19734. t t))
  19735. (org-move-to-column column))))
  19736. (defun org-indent-drawer ()
  19737. "Indent the drawer at point."
  19738. (interactive)
  19739. (let ((p (point))
  19740. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19741. (match-end 0)))
  19742. (folded
  19743. (save-excursion
  19744. (end-of-line)
  19745. (when (overlays-at (point))
  19746. (member 'invisible (overlay-properties
  19747. (car (overlays-at (point)))))))))
  19748. (when folded (org-cycle))
  19749. (indent-for-tab-command)
  19750. (while (and (move-beginning-of-line 2) (< (point) e))
  19751. (indent-for-tab-command))
  19752. (goto-char p)
  19753. (when folded (org-cycle)))
  19754. (message "Drawer at point indented"))
  19755. (defun org-indent-block ()
  19756. "Indent the block at point."
  19757. (interactive)
  19758. (let ((p (point))
  19759. (case-fold-search t)
  19760. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19761. (match-end 0)))
  19762. (folded
  19763. (save-excursion
  19764. (end-of-line)
  19765. (when (overlays-at (point))
  19766. (member 'invisible (overlay-properties
  19767. (car (overlays-at (point)))))))))
  19768. (when folded (org-cycle))
  19769. (indent-for-tab-command)
  19770. (while (and (move-beginning-of-line 2) (< (point) e))
  19771. (indent-for-tab-command))
  19772. (goto-char p)
  19773. (when folded (org-cycle)))
  19774. (message "Block at point indented"))
  19775. (defun org-indent-region (start end)
  19776. "Indent region."
  19777. (interactive "r")
  19778. (save-excursion
  19779. (let ((line-end (org-current-line end)))
  19780. (goto-char start)
  19781. (while (< (org-current-line) line-end)
  19782. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19783. (t (call-interactively 'org-indent-line)))
  19784. (move-beginning-of-line 2)))))
  19785. ;;; Filling
  19786. ;; We use our own fill-paragraph and auto-fill functions.
  19787. ;; `org-fill-paragraph' relies on adaptive filling and context
  19788. ;; checking. Appropriate `fill-prefix' is computed with
  19789. ;; `org-adaptive-fill-function'.
  19790. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19791. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19792. ;; `org-adaptive-fill-function' value. Internally,
  19793. ;; `org-comment-line-break-function' breaks the line.
  19794. ;; `org-setup-filling' installs filling and auto-filling related
  19795. ;; variables during `org-mode' initialization.
  19796. (defun org-setup-filling ()
  19797. (interactive)
  19798. ;; Prevent auto-fill from inserting unwanted new items.
  19799. (when (boundp 'fill-nobreak-predicate)
  19800. (org-set-local
  19801. 'fill-nobreak-predicate
  19802. (org-uniquify
  19803. (append fill-nobreak-predicate
  19804. '(org-fill-paragraph-separate-nobreak-p
  19805. org-fill-line-break-nobreak-p
  19806. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19807. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19808. (org-set-local 'auto-fill-inhibit-regexp nil)
  19809. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19810. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19811. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19812. (defvar org-element-paragraph-separate) ; org-element.el
  19813. (defun org-fill-paragraph-separate-nobreak-p ()
  19814. "Non-nil when a line break at point would insert a new item."
  19815. (looking-at (substring org-element-paragraph-separate 1)))
  19816. (defun org-fill-line-break-nobreak-p ()
  19817. "Non-nil when a line break at point would create an Org line break."
  19818. (save-excursion
  19819. (skip-chars-backward "[ \t]")
  19820. (skip-chars-backward "\\\\")
  19821. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19822. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19823. "Non-nil when a line break at point would insert a new item."
  19824. (and (org-at-timestamp-p t)
  19825. (not (looking-at org-ts-regexp-both))))
  19826. (declare-function message-in-body-p "message" ())
  19827. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19828. (defun org-adaptive-fill-function ()
  19829. "Compute a fill prefix for the current line.
  19830. Return fill prefix, as a string, or nil if current line isn't
  19831. meant to be filled."
  19832. (let (prefix)
  19833. (catch 'exit
  19834. (when (derived-mode-p 'message-mode)
  19835. (save-excursion
  19836. (beginning-of-line)
  19837. (cond ((or (not (message-in-body-p))
  19838. (looking-at orgtbl-line-start-regexp))
  19839. (throw 'exit nil))
  19840. ((looking-at message-cite-prefix-regexp)
  19841. (throw 'exit (match-string-no-properties 0)))
  19842. ((looking-at org-outline-regexp)
  19843. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19844. (org-with-wide-buffer
  19845. (let* ((p (line-beginning-position))
  19846. (element (save-excursion (beginning-of-line)
  19847. (org-element-at-point)))
  19848. (type (org-element-type element))
  19849. (post-affiliated (org-element-property :post-affiliated element)))
  19850. (unless (and post-affiliated (< p post-affiliated))
  19851. (case type
  19852. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19853. (footnote-definition "")
  19854. ((item plain-list)
  19855. (make-string (org-list-item-body-column
  19856. (or post-affiliated
  19857. (org-element-property :begin element)))
  19858. ? ))
  19859. (paragraph
  19860. ;; Fill prefix is usually the same as the current line,
  19861. ;; except if the paragraph is at the beginning of an item.
  19862. (let ((parent (org-element-property :parent element)))
  19863. (cond ((eq (org-element-type parent) 'item)
  19864. (make-string (org-list-item-body-column
  19865. (org-element-property :begin parent))
  19866. ? ))
  19867. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19868. (match-string 0))
  19869. (t ""))))
  19870. (comment-block
  19871. ;; Only fill contents if P is within block boundaries.
  19872. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19873. (forward-line)
  19874. (point)))
  19875. (cend (save-excursion
  19876. (goto-char (org-element-property :end element))
  19877. (skip-chars-backward " \r\t\n")
  19878. (line-beginning-position))))
  19879. (when (and (>= p cbeg) (< p cend))
  19880. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19881. (match-string 0)
  19882. "")))))))))))
  19883. (declare-function message-goto-body "message" ())
  19884. (defvar message-cite-prefix-regexp) ; From message.el
  19885. (defvar org-element-all-objects) ; From org-element.el
  19886. (defun org-fill-paragraph (&optional justify)
  19887. "Fill element at point, when applicable.
  19888. This function only applies to comment blocks, comments, example
  19889. blocks and paragraphs. Also, as a special case, re-align table
  19890. when point is at one.
  19891. If JUSTIFY is non-nil (interactively, with prefix argument),
  19892. justify as well. If `sentence-end-double-space' is non-nil, then
  19893. period followed by one space does not end a sentence, so don't
  19894. break a line there. The variable `fill-column' controls the
  19895. width for filling.
  19896. For convenience, when point is at a plain list, an item or
  19897. a footnote definition, try to fill the first paragraph within."
  19898. (interactive)
  19899. (if (and (derived-mode-p 'message-mode)
  19900. (or (not (message-in-body-p))
  19901. (save-excursion (move-beginning-of-line 1)
  19902. (looking-at message-cite-prefix-regexp))))
  19903. ;; First ensure filling is correct in message-mode.
  19904. (let ((fill-paragraph-function
  19905. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19906. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19907. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19908. (paragraph-separate
  19909. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19910. (fill-paragraph nil))
  19911. (save-excursion
  19912. ;; Move to end of line in order to get the first paragraph
  19913. ;; within a plain list or a footnote definition.
  19914. (end-of-line)
  19915. (let ((element (org-element-at-point)))
  19916. ;; First check if point is in a blank line at the beginning of
  19917. ;; the buffer. In that case, ignore filling.
  19918. (if (< (point) (org-element-property :begin element)) t
  19919. (case (org-element-type element)
  19920. ;; Use major mode filling function is src blocks.
  19921. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19922. ;; Align Org tables, leave table.el tables as-is.
  19923. (table-row (org-table-align) t)
  19924. (table
  19925. (when (eq (org-element-property :type element) 'org)
  19926. (org-table-align))
  19927. t)
  19928. (paragraph
  19929. ;; Paragraphs may contain `line-break' type objects.
  19930. (let ((beg (max (point-min)
  19931. (org-element-property :contents-begin element)))
  19932. (end (min (point-max)
  19933. (org-element-property :contents-end element))))
  19934. ;; Do nothing if point is at an affiliated keyword.
  19935. (if (< (point) beg) t
  19936. (when (derived-mode-p 'message-mode)
  19937. ;; In `message-mode', do not fill following
  19938. ;; citation in current paragraph nor text before
  19939. ;; message body.
  19940. (let ((body-start (save-excursion (message-goto-body))))
  19941. (when body-start (setq beg (max body-start beg))))
  19942. (when (save-excursion
  19943. (re-search-forward
  19944. (concat "^" message-cite-prefix-regexp) end t))
  19945. (setq end (match-beginning 0))))
  19946. ;; Fill paragraph, taking line breaks into
  19947. ;; consideration. For that, slice the paragraph
  19948. ;; using line breaks as separators, and fill the
  19949. ;; parts in reverse order to avoid messing with
  19950. ;; markers.
  19951. (save-excursion
  19952. (goto-char end)
  19953. (mapc
  19954. (lambda (pos)
  19955. (fill-region-as-paragraph pos (point) justify)
  19956. (goto-char pos))
  19957. ;; Find the list of ending positions for line
  19958. ;; breaks in the current paragraph. Add paragraph
  19959. ;; beginning to include first slice.
  19960. (nreverse
  19961. (cons
  19962. beg
  19963. (org-element-map
  19964. (org-element--parse-objects
  19965. beg end nil org-element-all-objects)
  19966. 'line-break
  19967. (lambda (lb) (org-element-property :end lb)))))))
  19968. t)))
  19969. ;; Contents of `comment-block' type elements should be
  19970. ;; filled as plain text, but only if point is within block
  19971. ;; markers.
  19972. (comment-block
  19973. (let* ((case-fold-search t)
  19974. (beg (save-excursion
  19975. (goto-char (org-element-property :begin element))
  19976. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19977. (forward-line)
  19978. (point)))
  19979. (end (save-excursion
  19980. (goto-char (org-element-property :end element))
  19981. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19982. (line-beginning-position))))
  19983. (when (and (>= (point) beg) (< (point) end))
  19984. (fill-region-as-paragraph
  19985. (save-excursion
  19986. (end-of-line)
  19987. (re-search-backward "^[ \t]*$" beg 'move)
  19988. (line-beginning-position))
  19989. (save-excursion
  19990. (beginning-of-line)
  19991. (re-search-forward "^[ \t]*$" end 'move)
  19992. (line-beginning-position))
  19993. justify)))
  19994. t)
  19995. ;; Fill comments.
  19996. (comment (fill-comment-paragraph justify))
  19997. ;; Ignore every other element.
  19998. (otherwise t)))))))
  19999. (defun org-auto-fill-function ()
  20000. "Auto-fill function."
  20001. ;; Check if auto-filling is meaningful.
  20002. (let ((fc (current-fill-column)))
  20003. (when (and fc (> (current-column) fc))
  20004. (let* ((fill-prefix (org-adaptive-fill-function))
  20005. ;; Enforce empty fill prefix, if required. Otherwise, it
  20006. ;; will be computed again.
  20007. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20008. (when fill-prefix (do-auto-fill))))))
  20009. (defun org-comment-line-break-function (&optional soft)
  20010. "Break line at point and indent, continuing comment if within one.
  20011. The inserted newline is marked hard if variable
  20012. `use-hard-newlines' is true, unless optional argument SOFT is
  20013. non-nil."
  20014. (if soft (insert-and-inherit ?\n) (newline 1))
  20015. (save-excursion (forward-char -1) (delete-horizontal-space))
  20016. (delete-horizontal-space)
  20017. (indent-to-left-margin)
  20018. (insert-before-markers-and-inherit fill-prefix))
  20019. ;;; Comments
  20020. ;; Org comments syntax is quite complex. It requires the entire line
  20021. ;; to be just a comment. Also, even with the right syntax at the
  20022. ;; beginning of line, some some elements (i.e. verse-block or
  20023. ;; example-block) don't accept comments. Usual Emacs comment commands
  20024. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20025. ;; Org still relies on `comment-dwim', but cannot trust
  20026. ;; `comment-only-p'. So, `comment-region-function' and
  20027. ;; `uncomment-region-function' both point
  20028. ;; to`org-comment-or-uncomment-region'. Eventually,
  20029. ;; `org-insert-comment' takes care of insertion of comments at the
  20030. ;; beginning of line.
  20031. ;; `org-setup-comments-handling' install comments related variables
  20032. ;; during `org-mode' initialization.
  20033. (defun org-setup-comments-handling ()
  20034. (interactive)
  20035. (org-set-local 'comment-use-syntax nil)
  20036. (org-set-local 'comment-start "# ")
  20037. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20038. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20039. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20040. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20041. (defun org-insert-comment ()
  20042. "Insert an empty comment above current line.
  20043. If the line is empty, insert comment at its beginning."
  20044. (beginning-of-line)
  20045. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20046. (org-indent-line)
  20047. (insert "# "))
  20048. (defvar comment-empty-lines) ; From newcomment.el.
  20049. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20050. "Comment or uncomment each non-blank line in the region.
  20051. Uncomment each non-blank line between BEG and END if it only
  20052. contains commented lines. Otherwise, comment them."
  20053. (save-restriction
  20054. ;; Restrict region
  20055. (narrow-to-region (save-excursion (goto-char beg)
  20056. (skip-chars-forward " \r\t\n" end)
  20057. (line-beginning-position))
  20058. (save-excursion (goto-char end)
  20059. (skip-chars-backward " \r\t\n" beg)
  20060. (line-end-position)))
  20061. (let ((uncommentp
  20062. ;; UNCOMMENTP is non-nil when every non blank line between
  20063. ;; BEG and END is a comment.
  20064. (save-excursion
  20065. (goto-char (point-min))
  20066. (while (and (not (eobp))
  20067. (let ((element (org-element-at-point)))
  20068. (and (eq (org-element-type element) 'comment)
  20069. (goto-char (min (point-max)
  20070. (org-element-property
  20071. :end element)))))))
  20072. (eobp))))
  20073. (if uncommentp
  20074. ;; Only blank lines and comments in region: uncomment it.
  20075. (save-excursion
  20076. (goto-char (point-min))
  20077. (while (not (eobp))
  20078. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20079. (replace-match "" nil nil nil 1))
  20080. (forward-line)))
  20081. ;; Comment each line in region.
  20082. (let ((min-indent (point-max)))
  20083. ;; First find the minimum indentation across all lines.
  20084. (save-excursion
  20085. (goto-char (point-min))
  20086. (while (and (not (eobp)) (not (zerop min-indent)))
  20087. (unless (looking-at "[ \t]*$")
  20088. (setq min-indent (min min-indent (current-indentation))))
  20089. (forward-line)))
  20090. ;; Then loop over all lines.
  20091. (save-excursion
  20092. (goto-char (point-min))
  20093. (while (not (eobp))
  20094. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20095. (org-move-to-column min-indent t)
  20096. (insert comment-start))
  20097. (forward-line))))))))
  20098. ;;; Planning
  20099. ;; This section contains tools to operate on timestamp objects, as
  20100. ;; returned by, e.g. `org-element-context'.
  20101. (defun org-timestamp-has-time-p (timestamp)
  20102. "Non-nil when TIMESTAMP has a time specified."
  20103. (org-element-property :hour-start timestamp))
  20104. (defun org-timestamp-format (timestamp format &optional end utc)
  20105. "Format a TIMESTAMP element into a string.
  20106. FORMAT is a format specifier to be passed to
  20107. `format-time-string'.
  20108. When optional argument END is non-nil, use end of date-range or
  20109. time-range, if possible.
  20110. When optional argument UTC is non-nil, time will be expressed as
  20111. Universal Time."
  20112. (format-time-string
  20113. format
  20114. (apply 'encode-time
  20115. (cons 0
  20116. (mapcar
  20117. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20118. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20119. '(:minute-start :hour-start :day-start :month-start
  20120. :year-start)))))
  20121. utc))
  20122. (defun org-timestamp-split-range (timestamp &optional end)
  20123. "Extract a timestamp object from a date or time range.
  20124. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20125. the end of the range. Otherwise, extract its start.
  20126. Return a new timestamp object sharing the same parent as
  20127. TIMESTAMP."
  20128. (let ((type (org-element-property :type timestamp)))
  20129. (if (memq type '(active inactive diary)) timestamp
  20130. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20131. ;; Set new type.
  20132. (org-element-put-property
  20133. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20134. ;; Copy start properties over end properties if END is
  20135. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20136. (let ((p-alist '((:minute-start . :minute-end)
  20137. (:hour-start . :hour-end)
  20138. (:day-start . :day-end)
  20139. (:month-start . :month-end)
  20140. (:year-start . :year-end))))
  20141. (dolist (p-cell p-alist)
  20142. (org-element-put-property
  20143. split-ts
  20144. (funcall (if end 'car 'cdr) p-cell)
  20145. (org-element-property
  20146. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20147. ;; Eventually refresh `:raw-value'.
  20148. (org-element-put-property split-ts :raw-value nil)
  20149. (org-element-put-property
  20150. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20151. (defun org-timestamp-translate (timestamp &optional boundary)
  20152. "Apply `org-translate-time' on a TIMESTAMP object.
  20153. When optional argument BOUNDARY is non-nil, it is either the
  20154. symbol `start' or `end'. In this case, only translate the
  20155. starting or ending part of TIMESTAMP if it is a date or time
  20156. range. Otherwise, translate both parts."
  20157. (if (and (not boundary)
  20158. (memq (org-element-property :type timestamp)
  20159. '(active-range inactive-range)))
  20160. (concat
  20161. (org-translate-time
  20162. (org-element-property :raw-value
  20163. (org-timestamp-split-range timestamp)))
  20164. "--"
  20165. (org-translate-time
  20166. (org-element-property :raw-value
  20167. (org-timestamp-split-range timestamp t))))
  20168. (org-translate-time
  20169. (org-element-property
  20170. :raw-value
  20171. (if (not boundary) timestamp
  20172. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20173. ;;; Other stuff.
  20174. (defun org-toggle-fixed-width-section (arg)
  20175. "Toggle the fixed-width export.
  20176. If there is no active region, the QUOTE keyword at the current headline is
  20177. inserted or removed. When present, it causes the text between this headline
  20178. and the next to be exported as fixed-width text, and unmodified.
  20179. If there is an active region, this command adds or removes a colon as the
  20180. first character of this line. If the first character of a line is a colon,
  20181. this line is also exported in fixed-width font."
  20182. (interactive "P")
  20183. (let* ((cc 0)
  20184. (regionp (org-region-active-p))
  20185. (beg (if regionp (region-beginning) (point)))
  20186. (end (if regionp (region-end)))
  20187. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20188. (case-fold-search nil)
  20189. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20190. off)
  20191. (if regionp
  20192. (save-excursion
  20193. (goto-char beg)
  20194. (setq cc (current-column))
  20195. (beginning-of-line 1)
  20196. (setq off (looking-at re))
  20197. (while (> nlines 0)
  20198. (setq nlines (1- nlines))
  20199. (beginning-of-line 1)
  20200. (cond
  20201. (arg
  20202. (org-move-to-column cc t)
  20203. (insert ": \n")
  20204. (forward-line -1))
  20205. ((and off (looking-at re))
  20206. (replace-match "" t t nil 1))
  20207. ((not off) (org-move-to-column cc t) (insert ": ")))
  20208. (forward-line 1)))
  20209. (save-excursion
  20210. (org-back-to-heading)
  20211. (cond
  20212. ((looking-at (format org-heading-keyword-regexp-format
  20213. org-quote-string))
  20214. (goto-char (match-end 1))
  20215. (looking-at (concat " +" org-quote-string))
  20216. (replace-match "" t t)
  20217. (when (eolp) (insert " ")))
  20218. ((looking-at org-outline-regexp)
  20219. (goto-char (match-end 0))
  20220. (insert org-quote-string " ")))))))
  20221. (defun org-reftex-citation ()
  20222. "Use reftex-citation to insert a citation into the buffer.
  20223. This looks for a line like
  20224. #+BIBLIOGRAPHY: foo plain option:-d
  20225. and derives from it that foo.bib is the bibliography file relevant
  20226. for this document. It then installs the necessary environment for RefTeX
  20227. to work in this buffer and calls `reftex-citation' to insert a citation
  20228. into the buffer.
  20229. Export of such citations to both LaTeX and HTML is handled by the contributed
  20230. package org-exp-bibtex by Taru Karttunen."
  20231. (interactive)
  20232. (let ((reftex-docstruct-symbol 'rds)
  20233. (reftex-cite-format "\\cite{%l}")
  20234. rds bib)
  20235. (save-excursion
  20236. (save-restriction
  20237. (widen)
  20238. (let ((case-fold-search t)
  20239. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20240. (if (not (save-excursion
  20241. (or (re-search-forward re nil t)
  20242. (re-search-backward re nil t))))
  20243. (error "No bibliography defined in file")
  20244. (setq bib (concat (match-string 1) ".bib")
  20245. rds (list (list 'bib bib)))))))
  20246. (call-interactively 'reftex-citation)))
  20247. ;;;; Functions extending outline functionality
  20248. (defun org-beginning-of-line (&optional arg)
  20249. "Go to the beginning of the current line. If that is invisible, continue
  20250. to a visible line beginning. This makes the function of C-a more intuitive.
  20251. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20252. first attempt, and only move to after the tags when the cursor is already
  20253. beyond the end of the headline."
  20254. (interactive "P")
  20255. (let ((pos (point))
  20256. (special (if (consp org-special-ctrl-a/e)
  20257. (car org-special-ctrl-a/e)
  20258. org-special-ctrl-a/e))
  20259. refpos)
  20260. (if (org-bound-and-true-p visual-line-mode)
  20261. (beginning-of-visual-line 1)
  20262. (beginning-of-line 1))
  20263. (if (and arg (fboundp 'move-beginning-of-line))
  20264. (call-interactively 'move-beginning-of-line)
  20265. (if (bobp)
  20266. nil
  20267. (backward-char 1)
  20268. (if (org-truely-invisible-p)
  20269. (while (and (not (bobp)) (org-truely-invisible-p))
  20270. (backward-char 1)
  20271. (beginning-of-line 1))
  20272. (forward-char 1))))
  20273. (when special
  20274. (cond
  20275. ((and (looking-at org-complex-heading-regexp)
  20276. (= (char-after (match-end 1)) ?\ ))
  20277. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20278. (point-at-eol)))
  20279. (goto-char
  20280. (if (eq special t)
  20281. (cond ((> pos refpos) refpos)
  20282. ((= pos (point)) refpos)
  20283. (t (point)))
  20284. (cond ((> pos (point)) (point))
  20285. ((not (eq last-command this-command)) (point))
  20286. (t refpos)))))
  20287. ((org-at-item-p)
  20288. ;; Being at an item and not looking at an the item means point
  20289. ;; was previously moved to beginning of a visual line, which
  20290. ;; doesn't contain the item. Therefore, do nothing special,
  20291. ;; just stay here.
  20292. (when (looking-at org-list-full-item-re)
  20293. ;; Set special position at first white space character after
  20294. ;; bullet, and check-box, if any.
  20295. (let ((after-bullet
  20296. (let ((box (match-end 3)))
  20297. (if (not box) (match-end 1)
  20298. (let ((after (char-after box)))
  20299. (if (and after (= after ? )) (1+ box) box))))))
  20300. ;; Special case: Move point to special position when
  20301. ;; currently after it or at beginning of line.
  20302. (if (eq special t)
  20303. (when (or (> pos after-bullet) (= (point) pos))
  20304. (goto-char after-bullet))
  20305. ;; Reversed case: Move point to special position when
  20306. ;; point was already at beginning of line and command is
  20307. ;; repeated.
  20308. (when (and (= (point) pos) (eq last-command this-command))
  20309. (goto-char after-bullet))))))))
  20310. (org-no-warnings
  20311. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20312. (defun org-end-of-line (&optional arg)
  20313. "Go to the end of the line.
  20314. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20315. tags on the first attempt, and only move to after the tags when
  20316. the cursor is already beyond the end of the headline."
  20317. (interactive "P")
  20318. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20319. org-special-ctrl-a/e))
  20320. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20321. 'end-of-visual-line)
  20322. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20323. (t 'end-of-line))))
  20324. (if (or (not special) arg) (call-interactively move-fun)
  20325. (let* ((element (save-excursion (beginning-of-line)
  20326. (org-element-at-point)))
  20327. (type (org-element-type element)))
  20328. (cond
  20329. ((memq type '(headline inlinetask))
  20330. (let ((pos (point)))
  20331. (beginning-of-line 1)
  20332. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20333. (if (eq special t)
  20334. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20335. (goto-char (match-beginning 1))
  20336. (goto-char (match-end 0)))
  20337. (if (or (< pos (match-end 0))
  20338. (not (eq this-command last-command)))
  20339. (goto-char (match-end 0))
  20340. (goto-char (match-beginning 1))))
  20341. (call-interactively move-fun))))
  20342. ((org-element-property :hiddenp element)
  20343. ;; If element is hidden, `move-end-of-line' would put point
  20344. ;; after it. Use `end-of-line' to stay on current line.
  20345. (call-interactively 'end-of-line))
  20346. (t (call-interactively move-fun)))))
  20347. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20348. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20349. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20350. (defun org-backward-sentence (&optional arg)
  20351. "Go to beginning of sentence, or beginning of table field.
  20352. This will call `backward-sentence' or `org-table-beginning-of-field',
  20353. depending on context."
  20354. (interactive "P")
  20355. (cond
  20356. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20357. (t (call-interactively 'backward-sentence))))
  20358. (defun org-forward-sentence (&optional arg)
  20359. "Go to end of sentence, or end of table field.
  20360. This will call `forward-sentence' or `org-table-end-of-field',
  20361. depending on context."
  20362. (interactive "P")
  20363. (cond
  20364. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20365. (t (call-interactively 'forward-sentence))))
  20366. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20367. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20368. (defun org-kill-line (&optional arg)
  20369. "Kill line, to tags or end of line."
  20370. (interactive "P")
  20371. (cond
  20372. ((or (not org-special-ctrl-k)
  20373. (bolp)
  20374. (not (org-at-heading-p)))
  20375. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20376. org-ctrl-k-protect-subtree)
  20377. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20378. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20379. (error "C-k aborted - would kill hidden subtree")))
  20380. (call-interactively
  20381. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20382. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20383. (kill-region (point) (match-beginning 1))
  20384. (org-set-tags nil t))
  20385. (t (kill-region (point) (point-at-eol)))))
  20386. (define-key org-mode-map "\C-k" 'org-kill-line)
  20387. (defun org-yank (&optional arg)
  20388. "Yank. If the kill is a subtree, treat it specially.
  20389. This command will look at the current kill and check if is a single
  20390. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20391. cursor is at the beginning of a line or after the stars of a currently
  20392. empty headline, then the yank is handled specially. How exactly depends
  20393. on the value of the following variables, both set by default.
  20394. org-yank-folded-subtrees
  20395. When set, the subtree(s) will be folded after insertion, but only
  20396. if doing so would now swallow text after the yanked text.
  20397. org-yank-adjusted-subtrees
  20398. When set, the subtree will be promoted or demoted in order to
  20399. fit into the local outline tree structure, which means that the level
  20400. will be adjusted so that it becomes the smaller one of the two
  20401. *visible* surrounding headings.
  20402. Any prefix to this command will cause `yank' to be called directly with
  20403. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20404. will just
  20405. plainly yank the text as it is.
  20406. \[1] The test checks if the first non-white line is a heading
  20407. and if there are no other headings with fewer stars."
  20408. (interactive "P")
  20409. (org-yank-generic 'yank arg))
  20410. (defun org-yank-generic (command arg)
  20411. "Perform some yank-like command.
  20412. This function implements the behavior described in the `org-yank'
  20413. documentation. However, it has been generalized to work for any
  20414. interactive command with similar behavior."
  20415. ;; pretend to be command COMMAND
  20416. (setq this-command command)
  20417. (if arg
  20418. (call-interactively command)
  20419. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20420. (and (org-kill-is-subtree-p)
  20421. (or (bolp)
  20422. (and (looking-at "[ \t]*$")
  20423. (string-match
  20424. "\\`\\*+\\'"
  20425. (buffer-substring (point-at-bol) (point)))))))
  20426. swallowp)
  20427. (cond
  20428. ((and subtreep org-yank-folded-subtrees)
  20429. (let ((beg (point))
  20430. end)
  20431. (if (and subtreep org-yank-adjusted-subtrees)
  20432. (org-paste-subtree nil nil 'for-yank)
  20433. (call-interactively command))
  20434. (setq end (point))
  20435. (goto-char beg)
  20436. (when (and (bolp) subtreep
  20437. (not (setq swallowp
  20438. (org-yank-folding-would-swallow-text beg end))))
  20439. (org-with-limited-levels
  20440. (or (looking-at org-outline-regexp)
  20441. (re-search-forward org-outline-regexp-bol end t))
  20442. (while (and (< (point) end) (looking-at org-outline-regexp))
  20443. (hide-subtree)
  20444. (org-cycle-show-empty-lines 'folded)
  20445. (condition-case nil
  20446. (outline-forward-same-level 1)
  20447. (error (goto-char end))))))
  20448. (when swallowp
  20449. (message
  20450. "Inserted text not folded because that would swallow text"))
  20451. (goto-char end)
  20452. (skip-chars-forward " \t\n\r")
  20453. (beginning-of-line 1)
  20454. (push-mark beg 'nomsg)))
  20455. ((and subtreep org-yank-adjusted-subtrees)
  20456. (let ((beg (point-at-bol)))
  20457. (org-paste-subtree nil nil 'for-yank)
  20458. (push-mark beg 'nomsg)))
  20459. (t
  20460. (call-interactively command))))))
  20461. (defun org-yank-folding-would-swallow-text (beg end)
  20462. "Would hide-subtree at BEG swallow any text after END?"
  20463. (let (level)
  20464. (org-with-limited-levels
  20465. (save-excursion
  20466. (goto-char beg)
  20467. (when (or (looking-at org-outline-regexp)
  20468. (re-search-forward org-outline-regexp-bol end t))
  20469. (setq level (org-outline-level)))
  20470. (goto-char end)
  20471. (skip-chars-forward " \t\r\n\v\f")
  20472. (if (or (eobp)
  20473. (and (bolp) (looking-at org-outline-regexp)
  20474. (<= (org-outline-level) level)))
  20475. nil ; Nothing would be swallowed
  20476. t))))) ; something would swallow
  20477. (define-key org-mode-map "\C-y" 'org-yank)
  20478. (defun org-truely-invisible-p ()
  20479. "Check if point is at a character currently not visible.
  20480. This version does not only check the character property, but also
  20481. `visible-mode'."
  20482. ;; Early versions of noutline don't have `outline-invisible-p'.
  20483. (if (org-bound-and-true-p visible-mode)
  20484. nil
  20485. (outline-invisible-p)))
  20486. (defun org-invisible-p2 ()
  20487. "Check if point is at a character currently not visible."
  20488. (save-excursion
  20489. (if (and (eolp) (not (bobp))) (backward-char 1))
  20490. ;; Early versions of noutline don't have `outline-invisible-p'.
  20491. (outline-invisible-p)))
  20492. (defun org-back-to-heading (&optional invisible-ok)
  20493. "Call `outline-back-to-heading', but provide a better error message."
  20494. (condition-case nil
  20495. (outline-back-to-heading invisible-ok)
  20496. (error (error "Before first headline at position %d in buffer %s"
  20497. (point) (current-buffer)))))
  20498. (defun org-before-first-heading-p ()
  20499. "Before first heading?"
  20500. (save-excursion
  20501. (end-of-line)
  20502. (null (re-search-backward org-outline-regexp-bol nil t))))
  20503. (defun org-at-heading-p (&optional ignored)
  20504. (outline-on-heading-p t))
  20505. ;; Compatibility alias with Org versions < 7.8.03
  20506. (defalias 'org-on-heading-p 'org-at-heading-p)
  20507. (defun org-at-comment-p nil
  20508. "Is cursor in a line starting with a # character?"
  20509. (save-excursion
  20510. (beginning-of-line)
  20511. (looking-at "^#")))
  20512. (defun org-at-drawer-p nil
  20513. "Is cursor at a drawer keyword?"
  20514. (save-excursion
  20515. (move-beginning-of-line 1)
  20516. (looking-at org-drawer-regexp)))
  20517. (defun org-at-block-p nil
  20518. "Is cursor at a block keyword?"
  20519. (save-excursion
  20520. (move-beginning-of-line 1)
  20521. (looking-at org-block-regexp)))
  20522. (defun org-point-at-end-of-empty-headline ()
  20523. "If point is at the end of an empty headline, return t, else nil.
  20524. If the heading only contains a TODO keyword, it is still still considered
  20525. empty."
  20526. (and (looking-at "[ \t]*$")
  20527. (when org-todo-line-regexp
  20528. (save-excursion
  20529. (beginning-of-line 1)
  20530. (let ((case-fold-search nil))
  20531. (looking-at org-todo-line-regexp)
  20532. (string= (match-string 3) ""))))))
  20533. (defun org-at-heading-or-item-p ()
  20534. (or (org-at-heading-p) (org-at-item-p)))
  20535. (defun org-at-target-p ()
  20536. (or (org-in-regexp org-radio-target-regexp)
  20537. (org-in-regexp org-target-regexp)))
  20538. ;; Compatibility alias with Org versions < 7.8.03
  20539. (defalias 'org-on-target-p 'org-at-target-p)
  20540. (defun org-up-heading-all (arg)
  20541. "Move to the heading line of which the present line is a subheading.
  20542. This function considers both visible and invisible heading lines.
  20543. With argument, move up ARG levels."
  20544. (if (fboundp 'outline-up-heading-all)
  20545. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20546. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20547. (defun org-up-heading-safe ()
  20548. "Move to the heading line of which the present line is a subheading.
  20549. This version will not throw an error. It will return the level of the
  20550. headline found, or nil if no higher level is found.
  20551. Also, this function will be a lot faster than `outline-up-heading',
  20552. because it relies on stars being the outline starters. This can really
  20553. make a significant difference in outlines with very many siblings."
  20554. (let (start-level re)
  20555. (org-back-to-heading t)
  20556. (setq start-level (funcall outline-level))
  20557. (if (equal start-level 1)
  20558. nil
  20559. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20560. (if (re-search-backward re nil t)
  20561. (funcall outline-level)))))
  20562. (defun org-first-sibling-p ()
  20563. "Is this heading the first child of its parents?"
  20564. (interactive)
  20565. (let ((re org-outline-regexp-bol)
  20566. level l)
  20567. (unless (org-at-heading-p t)
  20568. (error "Not at a heading"))
  20569. (setq level (funcall outline-level))
  20570. (save-excursion
  20571. (if (not (re-search-backward re nil t))
  20572. t
  20573. (setq l (funcall outline-level))
  20574. (< l level)))))
  20575. (defun org-goto-sibling (&optional previous)
  20576. "Goto the next sibling, even if it is invisible.
  20577. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20578. when a sibling was found. When none is found, return nil and don't
  20579. move point."
  20580. (let ((fun (if previous 're-search-backward 're-search-forward))
  20581. (pos (point))
  20582. (re org-outline-regexp-bol)
  20583. level l)
  20584. (when (condition-case nil (org-back-to-heading t) (error nil))
  20585. (setq level (funcall outline-level))
  20586. (catch 'exit
  20587. (or previous (forward-char 1))
  20588. (while (funcall fun re nil t)
  20589. (setq l (funcall outline-level))
  20590. (when (< l level) (goto-char pos) (throw 'exit nil))
  20591. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20592. (goto-char pos)
  20593. nil))))
  20594. (defun org-show-siblings ()
  20595. "Show all siblings of the current headline."
  20596. (save-excursion
  20597. (while (org-goto-sibling) (org-flag-heading nil)))
  20598. (save-excursion
  20599. (while (org-goto-sibling 'previous)
  20600. (org-flag-heading nil))))
  20601. (defun org-goto-first-child ()
  20602. "Goto the first child, even if it is invisible.
  20603. Return t when a child was found. Otherwise don't move point and
  20604. return nil."
  20605. (let (level (pos (point)) (re org-outline-regexp-bol))
  20606. (when (condition-case nil (org-back-to-heading t) (error nil))
  20607. (setq level (outline-level))
  20608. (forward-char 1)
  20609. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20610. (progn (goto-char (match-beginning 0)) t)
  20611. (goto-char pos) nil))))
  20612. (defun org-show-hidden-entry ()
  20613. "Show an entry where even the heading is hidden."
  20614. (save-excursion
  20615. (org-show-entry)))
  20616. (defun org-flag-heading (flag &optional entry)
  20617. "Flag the current heading. FLAG non-nil means make invisible.
  20618. When ENTRY is non-nil, show the entire entry."
  20619. (save-excursion
  20620. (org-back-to-heading t)
  20621. ;; Check if we should show the entire entry
  20622. (if entry
  20623. (progn
  20624. (org-show-entry)
  20625. (save-excursion
  20626. (and (outline-next-heading)
  20627. (org-flag-heading nil))))
  20628. (outline-flag-region (max (point-min) (1- (point)))
  20629. (save-excursion (outline-end-of-heading) (point))
  20630. flag))))
  20631. (defun org-get-next-sibling ()
  20632. "Move to next heading of the same level, and return point.
  20633. If there is no such heading, return nil.
  20634. This is like outline-next-sibling, but invisible headings are ok."
  20635. (let ((level (funcall outline-level)))
  20636. (outline-next-heading)
  20637. (while (and (not (eobp)) (> (funcall outline-level) level))
  20638. (outline-next-heading))
  20639. (if (or (eobp) (< (funcall outline-level) level))
  20640. nil
  20641. (point))))
  20642. (defun org-get-last-sibling ()
  20643. "Move to previous heading of the same level, and return point.
  20644. If there is no such heading, return nil."
  20645. (let ((opoint (point))
  20646. (level (funcall outline-level)))
  20647. (outline-previous-heading)
  20648. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20649. (while (and (> (funcall outline-level) level)
  20650. (not (bobp)))
  20651. (outline-previous-heading))
  20652. (if (< (funcall outline-level) level)
  20653. nil
  20654. (point)))))
  20655. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20656. "Goto to the end of a subtree."
  20657. ;; This contains an exact copy of the original function, but it uses
  20658. ;; `org-back-to-heading', to make it work also in invisible
  20659. ;; trees. And is uses an invisible-ok argument.
  20660. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20661. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20662. ;; with many children and grandchildren etc, this can be much faster
  20663. ;; than the outline version.
  20664. (org-back-to-heading invisible-ok)
  20665. (let ((first t)
  20666. (level (funcall outline-level)))
  20667. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20668. ;; A true heading (not a plain list item), in Org-mode
  20669. ;; This means we can easily find the end by looking
  20670. ;; only for the right number of stars. Using a regexp to do
  20671. ;; this is so much faster than using a Lisp loop.
  20672. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20673. (forward-char 1)
  20674. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20675. ;; something else, do it the slow way
  20676. (while (and (not (eobp))
  20677. (or first (> (funcall outline-level) level)))
  20678. (setq first nil)
  20679. (outline-next-heading)))
  20680. (unless to-heading
  20681. (if (memq (preceding-char) '(?\n ?\^M))
  20682. (progn
  20683. ;; Go to end of line before heading
  20684. (forward-char -1)
  20685. (if (memq (preceding-char) '(?\n ?\^M))
  20686. ;; leave blank line before heading
  20687. (forward-char -1))))))
  20688. (point))
  20689. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20690. "Use Org version in org-mode, for dramatic speed-up."
  20691. (if (derived-mode-p 'org-mode)
  20692. (progn
  20693. (org-end-of-subtree nil t)
  20694. (unless (eobp) (backward-char 1)))
  20695. ad-do-it))
  20696. (defun org-end-of-meta-data-and-drawers ()
  20697. "Jump to the first text after meta data and drawers in the current entry.
  20698. This will move over empty lines, lines with planning time stamps,
  20699. clocking lines, and drawers."
  20700. (org-back-to-heading t)
  20701. (let ((end (save-excursion (outline-next-heading) (point)))
  20702. (re (concat "\\(" org-drawer-regexp "\\)"
  20703. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20704. (forward-line 1)
  20705. (while (re-search-forward re end t)
  20706. (if (not (match-end 1))
  20707. ;; empty or planning line
  20708. (forward-line 1)
  20709. ;; a drawer, find the end
  20710. (re-search-forward "^[ \t]*:END:" end 'move)
  20711. (forward-line 1)))
  20712. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20713. (point)))
  20714. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20715. "Move forward to the ARG'th subheading at same level as this one.
  20716. Stop at the first and last subheadings of a superior heading.
  20717. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20718. non-nil it will also look at invisible ones."
  20719. (interactive "p")
  20720. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20721. (if (and arg (< arg 0))
  20722. (goto-char (point-min))
  20723. (outline-next-heading))
  20724. (org-at-heading-p)
  20725. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20726. (f (if (and arg (< arg 0))
  20727. 're-search-backward
  20728. 're-search-forward))
  20729. (count (if arg (abs arg) 1))
  20730. (result (point)))
  20731. (forward-char (if (and arg (< arg 0)) -1 1))
  20732. (while (and (> count 0)
  20733. (funcall f org-outline-regexp-bol nil 'move))
  20734. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20735. (cond ((< l level) (setq count 0))
  20736. ((and (= l level)
  20737. (or invisible-ok
  20738. (progn
  20739. (goto-char (line-beginning-position))
  20740. (not (outline-invisible-p)))))
  20741. (setq count (1- count))
  20742. (when (eq l level)
  20743. (setq result (point)))))))
  20744. (goto-char result))
  20745. (beginning-of-line 1)))
  20746. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20747. "Move backward to the ARG'th subheading at same level as this one.
  20748. Stop at the first and last subheadings of a superior heading."
  20749. (interactive "p")
  20750. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20751. (defun org-next-block (arg &optional backward block-regexp)
  20752. "Jump to the next block.
  20753. With a prefix argument ARG, jump forward ARG many source blocks.
  20754. When BACKWARD is non-nil, jump to the previous block.
  20755. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20756. (interactive "p")
  20757. (let ((re (or block-regexp org-block-regexp))
  20758. (re-search-fn (or (and backward 're-search-backward)
  20759. 're-search-forward)))
  20760. (if (looking-at re) (forward-char 1))
  20761. (condition-case nil
  20762. (funcall re-search-fn re nil nil arg)
  20763. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20764. (goto-char (match-beginning 0)) (org-show-context)))
  20765. (defun org-previous-block (arg &optional block-regexp)
  20766. "Jump to the previous block.
  20767. With a prefix argument ARG, jump backward ARG many source blocks.
  20768. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20769. (interactive "p")
  20770. (org-next-block arg t block-regexp))
  20771. (defun org-forward-element ()
  20772. "Move forward by one element.
  20773. Move to the next element at the same level, when possible."
  20774. (interactive)
  20775. (cond ((eobp) (error "Cannot move further down"))
  20776. ((org-with-limited-levels (org-at-heading-p))
  20777. (let ((origin (point)))
  20778. (goto-char (org-end-of-subtree nil t))
  20779. (unless (org-with-limited-levels (org-at-heading-p))
  20780. (goto-char origin)
  20781. (error "Cannot move further down"))))
  20782. (t
  20783. (let* ((elem (org-element-at-point))
  20784. (end (org-element-property :end elem))
  20785. (parent (org-element-property :parent elem)))
  20786. (if (and parent (= (org-element-property :contents-end parent) end))
  20787. (goto-char (org-element-property :end parent))
  20788. (goto-char end))))))
  20789. (defun org-backward-element ()
  20790. "Move backward by one element.
  20791. Move to the previous element at the same level, when possible."
  20792. (interactive)
  20793. (cond ((bobp) (error "Cannot move further up"))
  20794. ((org-with-limited-levels (org-at-heading-p))
  20795. ;; At an headline, move to the previous one, if any, or stay
  20796. ;; here.
  20797. (let ((origin (point)))
  20798. (org-with-limited-levels (org-backward-heading-same-level 1))
  20799. ;; When current headline has no sibling above, move to its
  20800. ;; parent.
  20801. (when (= (point) origin)
  20802. (or (org-with-limited-levels (org-up-heading-safe))
  20803. (progn (goto-char origin)
  20804. (error "Cannot move further up"))))))
  20805. (t
  20806. (let* ((trail (org-element-at-point 'keep-trail))
  20807. (elem (car trail))
  20808. (prev-elem (nth 1 trail))
  20809. (beg (org-element-property :begin elem)))
  20810. (cond
  20811. ;; Move to beginning of current element if point isn't
  20812. ;; there already.
  20813. ((/= (point) beg) (goto-char beg))
  20814. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20815. ((org-before-first-heading-p) (goto-char (point-min)))
  20816. (t (org-back-to-heading)))))))
  20817. (defun org-up-element ()
  20818. "Move to upper element."
  20819. (interactive)
  20820. (if (org-with-limited-levels (org-at-heading-p))
  20821. (unless (org-up-heading-safe) (error "No surrounding element"))
  20822. (let* ((elem (org-element-at-point))
  20823. (parent (org-element-property :parent elem)))
  20824. (if parent (goto-char (org-element-property :begin parent))
  20825. (if (org-with-limited-levels (org-before-first-heading-p))
  20826. (error "No surrounding element")
  20827. (org-with-limited-levels (org-back-to-heading)))))))
  20828. (defvar org-element-greater-elements)
  20829. (defun org-down-element ()
  20830. "Move to inner element."
  20831. (interactive)
  20832. (let ((element (org-element-at-point)))
  20833. (cond
  20834. ((memq (org-element-type element) '(plain-list table))
  20835. (goto-char (org-element-property :contents-begin element))
  20836. (forward-char))
  20837. ((memq (org-element-type element) org-element-greater-elements)
  20838. ;; If contents are hidden, first disclose them.
  20839. (when (org-element-property :hiddenp element) (org-cycle))
  20840. (goto-char (or (org-element-property :contents-begin element)
  20841. (error "No content for this element"))))
  20842. (t (error "No inner element")))))
  20843. (defun org-drag-element-backward ()
  20844. "Move backward element at point."
  20845. (interactive)
  20846. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20847. (let* ((trail (org-element-at-point 'keep-trail))
  20848. (elem (car trail))
  20849. (prev-elem (nth 1 trail)))
  20850. ;; Error out if no previous element or previous element is
  20851. ;; a parent of the current one.
  20852. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20853. (error "Cannot drag element backward")
  20854. (let ((pos (point)))
  20855. (org-element-swap-A-B prev-elem elem)
  20856. (goto-char (+ (org-element-property :begin prev-elem)
  20857. (- pos (org-element-property :begin elem)))))))))
  20858. (defun org-drag-element-forward ()
  20859. "Move forward element at point."
  20860. (interactive)
  20861. (let* ((pos (point))
  20862. (elem (org-element-at-point)))
  20863. (when (= (point-max) (org-element-property :end elem))
  20864. (error "Cannot drag element forward"))
  20865. (goto-char (org-element-property :end elem))
  20866. (let ((next-elem (org-element-at-point)))
  20867. (when (or (org-element-nested-p elem next-elem)
  20868. (and (eq (org-element-type next-elem) 'headline)
  20869. (not (eq (org-element-type elem) 'headline))))
  20870. (goto-char pos)
  20871. (error "Cannot drag element forward"))
  20872. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20873. ;; body's length (without final blanks) and by the length of
  20874. ;; blanks between ELEM and NEXT-ELEM.
  20875. (let ((size-next (- (save-excursion
  20876. (goto-char (org-element-property :end next-elem))
  20877. (skip-chars-backward " \r\t\n")
  20878. (forward-line)
  20879. ;; Small correction if buffer doesn't end
  20880. ;; with a newline character.
  20881. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20882. (org-element-property :begin next-elem)))
  20883. (size-blank (- (org-element-property :end elem)
  20884. (save-excursion
  20885. (goto-char (org-element-property :end elem))
  20886. (skip-chars-backward " \r\t\n")
  20887. (forward-line)
  20888. (point)))))
  20889. (org-element-swap-A-B elem next-elem)
  20890. (goto-char (+ pos size-next size-blank))))))
  20891. (defun org-drag-line-forward (arg)
  20892. "Drag the line at point ARG lines forward."
  20893. (interactive "p")
  20894. (dotimes (n (abs arg))
  20895. (let ((c (current-column)))
  20896. (if (< 0 arg)
  20897. (progn
  20898. (beginning-of-line 2)
  20899. (transpose-lines 1)
  20900. (beginning-of-line 0))
  20901. (transpose-lines 1)
  20902. (beginning-of-line -1))
  20903. (org-move-to-column c))))
  20904. (defun org-drag-line-backward (arg)
  20905. "Drag the line at point ARG lines backward."
  20906. (interactive "p")
  20907. (org-drag-line-forward (- arg)))
  20908. (defun org-mark-element ()
  20909. "Put point at beginning of this element, mark at end.
  20910. Interactively, if this command is repeated or (in Transient Mark
  20911. mode) if the mark is active, it marks the next element after the
  20912. ones already marked."
  20913. (interactive)
  20914. (let (deactivate-mark)
  20915. (if (and (org-called-interactively-p 'any)
  20916. (or (and (eq last-command this-command) (mark t))
  20917. (and transient-mark-mode mark-active)))
  20918. (set-mark
  20919. (save-excursion
  20920. (goto-char (mark))
  20921. (goto-char (org-element-property :end (org-element-at-point)))))
  20922. (let ((element (org-element-at-point)))
  20923. (end-of-line)
  20924. (push-mark (org-element-property :end element) t t)
  20925. (goto-char (org-element-property :begin element))))))
  20926. (defun org-narrow-to-element ()
  20927. "Narrow buffer to current element."
  20928. (interactive)
  20929. (let ((elem (org-element-at-point)))
  20930. (cond
  20931. ((eq (car elem) 'headline)
  20932. (narrow-to-region
  20933. (org-element-property :begin elem)
  20934. (org-element-property :end elem)))
  20935. ((memq (car elem) org-element-greater-elements)
  20936. (narrow-to-region
  20937. (org-element-property :contents-begin elem)
  20938. (org-element-property :contents-end elem)))
  20939. (t
  20940. (narrow-to-region
  20941. (org-element-property :begin elem)
  20942. (org-element-property :end elem))))))
  20943. (defun org-transpose-element ()
  20944. "Transpose current and previous elements, keeping blank lines between.
  20945. Point is moved after both elements."
  20946. (interactive)
  20947. (org-skip-whitespace)
  20948. (let ((end (org-element-property :end (org-element-at-point))))
  20949. (org-drag-element-backward)
  20950. (goto-char end)))
  20951. (defun org-unindent-buffer ()
  20952. "Un-indent the visible part of the buffer.
  20953. Relative indentation (between items, inside blocks, etc.) isn't
  20954. modified."
  20955. (interactive)
  20956. (unless (eq major-mode 'org-mode)
  20957. (error "Cannot un-indent a buffer not in Org mode"))
  20958. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20959. unindent-tree ; For byte-compiler.
  20960. (unindent-tree
  20961. (function
  20962. (lambda (contents)
  20963. (mapc
  20964. (lambda (element)
  20965. (if (memq (org-element-type element) '(headline section))
  20966. (funcall unindent-tree (org-element-contents element))
  20967. (save-excursion
  20968. (save-restriction
  20969. (narrow-to-region
  20970. (org-element-property :begin element)
  20971. (org-element-property :end element))
  20972. (org-do-remove-indentation)))))
  20973. (reverse contents))))))
  20974. (funcall unindent-tree (org-element-contents parse-tree))))
  20975. (defun org-show-subtree ()
  20976. "Show everything after this heading at deeper levels."
  20977. (interactive)
  20978. (outline-flag-region
  20979. (point)
  20980. (save-excursion
  20981. (org-end-of-subtree t t))
  20982. nil))
  20983. (defun org-show-entry ()
  20984. "Show the body directly following this heading.
  20985. Show the heading too, if it is currently invisible."
  20986. (interactive)
  20987. (save-excursion
  20988. (condition-case nil
  20989. (progn
  20990. (org-back-to-heading t)
  20991. (outline-flag-region
  20992. (max (point-min) (1- (point)))
  20993. (save-excursion
  20994. (if (re-search-forward
  20995. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20996. (match-beginning 1)
  20997. (point-max)))
  20998. nil)
  20999. (org-cycle-hide-drawers 'children))
  21000. (error nil))))
  21001. (defun org-make-options-regexp (kwds &optional extra)
  21002. "Make a regular expression for keyword lines."
  21003. (concat
  21004. "^#\\+\\("
  21005. (mapconcat 'regexp-quote kwds "\\|")
  21006. (if extra (concat "\\|" extra))
  21007. "\\):[ \t]*\\(.*\\)"))
  21008. ;; Make isearch reveal the necessary context
  21009. (defun org-isearch-end ()
  21010. "Reveal context after isearch exits."
  21011. (when isearch-success ; only if search was successful
  21012. (if (featurep 'xemacs)
  21013. ;; Under XEmacs, the hook is run in the correct place,
  21014. ;; we directly show the context.
  21015. (org-show-context 'isearch)
  21016. ;; In Emacs the hook runs *before* restoring the overlays.
  21017. ;; So we have to use a one-time post-command-hook to do this.
  21018. ;; (Emacs 22 has a special variable, see function `org-mode')
  21019. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21020. isearch-mode-end-hook-quit)
  21021. ;; Only when the isearch was not quitted.
  21022. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21023. 'append 'local)))))
  21024. (defun org-isearch-post-command ()
  21025. "Remove self from hook, and show context."
  21026. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21027. (org-show-context 'isearch))
  21028. ;;;; Integration with and fixes for other packages
  21029. ;;; Imenu support
  21030. (defvar org-imenu-markers nil
  21031. "All markers currently used by Imenu.")
  21032. (make-variable-buffer-local 'org-imenu-markers)
  21033. (defun org-imenu-new-marker (&optional pos)
  21034. "Return a new marker for use by Imenu, and remember the marker."
  21035. (let ((m (make-marker)))
  21036. (move-marker m (or pos (point)))
  21037. (push m org-imenu-markers)
  21038. m))
  21039. (defun org-imenu-get-tree ()
  21040. "Produce the index for Imenu."
  21041. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21042. (setq org-imenu-markers nil)
  21043. (let* ((n org-imenu-depth)
  21044. (re (concat "^" (org-get-limited-outline-regexp)))
  21045. (subs (make-vector (1+ n) nil))
  21046. (last-level 0)
  21047. m level head)
  21048. (save-excursion
  21049. (save-restriction
  21050. (widen)
  21051. (goto-char (point-max))
  21052. (while (re-search-backward re nil t)
  21053. (setq level (org-reduced-level (funcall outline-level)))
  21054. (when (and (<= level n)
  21055. (looking-at org-complex-heading-regexp))
  21056. (setq head (org-link-display-format
  21057. (org-match-string-no-properties 4))
  21058. m (org-imenu-new-marker))
  21059. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21060. (if (>= level last-level)
  21061. (push (cons head m) (aref subs level))
  21062. (push (cons head (aref subs (1+ level))) (aref subs level))
  21063. (loop for i from (1+ level) to n do (aset subs i nil)))
  21064. (setq last-level level)))))
  21065. (aref subs 1)))
  21066. (eval-after-load "imenu"
  21067. '(progn
  21068. (add-hook 'imenu-after-jump-hook
  21069. (lambda ()
  21070. (if (derived-mode-p 'org-mode)
  21071. (org-show-context 'org-goto))))))
  21072. (defun org-link-display-format (link)
  21073. "Replace a link with either the description, or the link target
  21074. if no description is present"
  21075. (save-match-data
  21076. (if (string-match org-bracket-link-analytic-regexp link)
  21077. (replace-match (if (match-end 5)
  21078. (match-string 5 link)
  21079. (concat (match-string 1 link)
  21080. (match-string 3 link)))
  21081. nil t link)
  21082. link)))
  21083. (defun org-toggle-link-display ()
  21084. "Toggle the literal or descriptive display of links."
  21085. (interactive)
  21086. (if org-descriptive-links
  21087. (progn (org-remove-from-invisibility-spec '(org-link))
  21088. (org-restart-font-lock)
  21089. (setq org-descriptive-links nil))
  21090. (progn (add-to-invisibility-spec '(org-link))
  21091. (org-restart-font-lock)
  21092. (setq org-descriptive-links t))))
  21093. ;; Speedbar support
  21094. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21095. "Overlay marking the agenda restriction line in speedbar.")
  21096. (overlay-put org-speedbar-restriction-lock-overlay
  21097. 'face 'org-agenda-restriction-lock)
  21098. (overlay-put org-speedbar-restriction-lock-overlay
  21099. 'help-echo "Agendas are currently limited to this item.")
  21100. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21101. (defun org-speedbar-set-agenda-restriction ()
  21102. "Restrict future agenda commands to the location at point in speedbar.
  21103. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21104. (interactive)
  21105. (require 'org-agenda)
  21106. (let (p m tp np dir txt)
  21107. (cond
  21108. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21109. 'org-imenu t))
  21110. (setq m (get-text-property p 'org-imenu-marker))
  21111. (with-current-buffer (marker-buffer m)
  21112. (goto-char m)
  21113. (org-agenda-set-restriction-lock 'subtree)))
  21114. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21115. 'speedbar-function 'speedbar-find-file))
  21116. (setq tp (previous-single-property-change
  21117. (1+ p) 'speedbar-function)
  21118. np (next-single-property-change
  21119. tp 'speedbar-function)
  21120. dir (speedbar-line-directory)
  21121. txt (buffer-substring-no-properties (or tp (point-min))
  21122. (or np (point-max))))
  21123. (with-current-buffer (find-file-noselect
  21124. (let ((default-directory dir))
  21125. (expand-file-name txt)))
  21126. (unless (derived-mode-p 'org-mode)
  21127. (error "Cannot restrict to non-Org-mode file"))
  21128. (org-agenda-set-restriction-lock 'file)))
  21129. (t (error "Don't know how to restrict Org-mode's agenda")))
  21130. (move-overlay org-speedbar-restriction-lock-overlay
  21131. (point-at-bol) (point-at-eol))
  21132. (setq current-prefix-arg nil)
  21133. (org-agenda-maybe-redo)))
  21134. (eval-after-load "speedbar"
  21135. '(progn
  21136. (speedbar-add-supported-extension ".org")
  21137. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21138. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21139. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21140. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21141. (add-hook 'speedbar-visiting-tag-hook
  21142. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21143. ;;; Fixes and Hacks for problems with other packages
  21144. ;; Make flyspell not check words in links, to not mess up our keymap
  21145. (defvar org-element-affiliated-keywords) ; From org-element.el
  21146. (defvar org-element-block-name-alist) ; From org-element.el
  21147. (defun org-mode-flyspell-verify ()
  21148. "Don't let flyspell put overlays at active buttons, or on
  21149. {todo,all-time,additional-option-like}-keywords."
  21150. (let ((pos (max (1- (point)) (point-min)))
  21151. (word (thing-at-point 'word)))
  21152. (and (not (get-text-property pos 'keymap))
  21153. (not (get-text-property pos 'org-no-flyspell))
  21154. (not (member word org-todo-keywords-1))
  21155. (not (member word org-all-time-keywords))
  21156. (not (member word org-options-keywords))
  21157. (not (member word (mapcar 'car org-startup-options)))
  21158. (not (member-ignore-case word org-element-affiliated-keywords))
  21159. (not (member-ignore-case word (org-get-export-keywords)))
  21160. (not (member-ignore-case
  21161. word (mapcar 'car org-element-block-name-alist)))
  21162. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21163. (defun org-remove-flyspell-overlays-in (beg end)
  21164. "Remove flyspell overlays in region."
  21165. (and (org-bound-and-true-p flyspell-mode)
  21166. (fboundp 'flyspell-delete-region-overlays)
  21167. (flyspell-delete-region-overlays beg end))
  21168. (add-text-properties beg end '(org-no-flyspell t)))
  21169. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21170. (eval-after-load "bookmark"
  21171. '(if (boundp 'bookmark-after-jump-hook)
  21172. ;; We can use the hook
  21173. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21174. ;; Hook not available, use advice
  21175. (defadvice bookmark-jump (after org-make-visible activate)
  21176. "Make the position visible."
  21177. (org-bookmark-jump-unhide))))
  21178. ;; Make sure saveplace shows the location if it was hidden
  21179. (eval-after-load "saveplace"
  21180. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21181. "Make the position visible."
  21182. (org-bookmark-jump-unhide)))
  21183. ;; Make sure ecb shows the location if it was hidden
  21184. (eval-after-load "ecb"
  21185. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21186. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21187. (if (derived-mode-p 'org-mode)
  21188. (org-show-context))))
  21189. (defun org-bookmark-jump-unhide ()
  21190. "Unhide the current position, to show the bookmark location."
  21191. (and (derived-mode-p 'org-mode)
  21192. (or (outline-invisible-p)
  21193. (save-excursion (goto-char (max (point-min) (1- (point))))
  21194. (outline-invisible-p)))
  21195. (org-show-context 'bookmark-jump)))
  21196. ;; Make session.el ignore our circular variable
  21197. (eval-after-load "session"
  21198. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21199. ;;;; Experimental code
  21200. (defun org-closed-in-range ()
  21201. "Sparse tree of items closed in a certain time range.
  21202. Still experimental, may disappear in the future."
  21203. (interactive)
  21204. ;; Get the time interval from the user.
  21205. (let* ((time1 (org-float-time
  21206. (org-read-date nil 'to-time nil "Starting date: ")))
  21207. (time2 (org-float-time
  21208. (org-read-date nil 'to-time nil "End date:")))
  21209. ;; callback function
  21210. (callback (lambda ()
  21211. (let ((time
  21212. (org-float-time
  21213. (apply 'encode-time
  21214. (org-parse-time-string
  21215. (match-string 1))))))
  21216. ;; check if time in interval
  21217. (and (>= time time1) (<= time time2))))))
  21218. ;; make tree, check each match with the callback
  21219. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21220. ;;;; Finish up
  21221. (provide 'org)
  21222. (run-hooks 'org-load-hook)
  21223. ;;; org.el ends here