org.el 873 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096
  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" "STARTUP" "ARCHIVE" "FILETAGS"
  4129. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4130. "SETUPFILE" "OPTIONS")
  4131. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4132. (splitre "[ \t]+")
  4133. (scripts org-use-sub-superscripts)
  4134. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4135. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4136. (start 0))
  4137. (save-excursion
  4138. (save-restriction
  4139. (widen)
  4140. (goto-char (point-min))
  4141. (while (or (and ext-setup-or-nil
  4142. (string-match re ext-setup-or-nil start)
  4143. (setq start (match-end 0)))
  4144. (and (setq ext-setup-or-nil nil start 0)
  4145. (re-search-forward re nil t)))
  4146. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4147. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4148. (if (stringp value) (setq value (org-trim value)))
  4149. (cond
  4150. ((equal key "CATEGORY")
  4151. (setq cat value))
  4152. ((member key '("SEQ_TODO" "TODO"))
  4153. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4154. ((equal key "TYP_TODO")
  4155. (push (cons 'type (org-split-string value splitre)) kwds))
  4156. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4157. ;; general TODO-like setup
  4158. (push (cons (intern (downcase (match-string 1 key)))
  4159. (org-split-string value splitre)) kwds))
  4160. ((equal key "TAGS")
  4161. (setq tags (append tags (if tags '("\\n") nil)
  4162. (org-split-string value splitre))))
  4163. ((equal key "COLUMNS")
  4164. (org-set-local 'org-columns-default-format value))
  4165. ((equal key "LINK")
  4166. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4167. (push (cons (match-string 1 value)
  4168. (org-trim (match-string 2 value)))
  4169. links)))
  4170. ((equal key "PRIORITIES")
  4171. (setq prio (org-split-string value " +")))
  4172. ((equal key "PROPERTY")
  4173. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4174. (setq props (org-update-property-plist (match-string 1 value)
  4175. (match-string 2 value)
  4176. props))))
  4177. ((equal key "FILETAGS")
  4178. (when (string-match "\\S-" value)
  4179. (setq ftags
  4180. (append
  4181. ftags
  4182. (apply 'append
  4183. (mapcar (lambda (x) (org-split-string x ":"))
  4184. (org-split-string value)))))))
  4185. ((equal key "DRAWERS")
  4186. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4187. ((equal key "CONSTANTS")
  4188. (setq const (append const (org-split-string value splitre))))
  4189. ((equal key "STARTUP")
  4190. (let ((opts (org-split-string value splitre))
  4191. l var val)
  4192. (while (setq l (pop opts))
  4193. (when (setq l (assoc l org-startup-options))
  4194. (setq var (nth 1 l) val (nth 2 l))
  4195. (if (not (nth 3 l))
  4196. (set (make-local-variable var) val)
  4197. (if (not (listp (symbol-value var)))
  4198. (set (make-local-variable var) nil))
  4199. (set (make-local-variable var) (symbol-value var))
  4200. (add-to-list var val))))))
  4201. ((equal key "ARCHIVE")
  4202. (setq arch value)
  4203. (remove-text-properties 0 (length arch)
  4204. '(face t fontified t) arch))
  4205. ((equal key "OPTIONS")
  4206. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4207. (setq scripts (read (match-string 2 value)))))
  4208. ((equal key "SETUPFILE")
  4209. (setq setup-contents (org-file-contents
  4210. (expand-file-name
  4211. (org-remove-double-quotes value))
  4212. 'noerror))
  4213. (if (not ext-setup-or-nil)
  4214. (setq ext-setup-or-nil setup-contents start 0)
  4215. (setq ext-setup-or-nil
  4216. (concat (substring ext-setup-or-nil 0 start)
  4217. "\n" setup-contents "\n"
  4218. (substring ext-setup-or-nil start)))))))
  4219. ;; search for property blocks
  4220. (goto-char (point-min))
  4221. (while (re-search-forward org-block-regexp nil t)
  4222. (when (equal "PROPERTY" (upcase (match-string 1)))
  4223. (setq value (replace-regexp-in-string
  4224. "[\n\r]" " " (match-string 4)))
  4225. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4226. (setq props (org-update-property-plist (match-string 1 value)
  4227. (match-string 2 value)
  4228. props)))))))
  4229. (org-set-local 'org-use-sub-superscripts scripts)
  4230. (when cat
  4231. (org-set-local 'org-category (intern cat))
  4232. (push (cons "CATEGORY" cat) props))
  4233. (when prio
  4234. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4235. (setq prio (mapcar 'string-to-char prio))
  4236. (org-set-local 'org-highest-priority (nth 0 prio))
  4237. (org-set-local 'org-lowest-priority (nth 1 prio))
  4238. (org-set-local 'org-default-priority (nth 2 prio)))
  4239. (and props (org-set-local 'org-file-properties (nreverse props)))
  4240. (and ftags (org-set-local 'org-file-tags
  4241. (mapcar 'org-add-prop-inherited ftags)))
  4242. (and drawers (org-set-local 'org-drawers drawers))
  4243. (and arch (org-set-local 'org-archive-location arch))
  4244. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4245. ;; Process the TODO keywords
  4246. (unless kwds
  4247. ;; Use the global values as if they had been given locally.
  4248. (setq kwds (default-value 'org-todo-keywords))
  4249. (if (stringp (car kwds))
  4250. (setq kwds (list (cons org-todo-interpretation
  4251. (default-value 'org-todo-keywords)))))
  4252. (setq kwds (reverse kwds)))
  4253. (setq kwds (nreverse kwds))
  4254. (let (inter kws kw)
  4255. (while (setq kws (pop kwds))
  4256. (let ((kws (or
  4257. (run-hook-with-args-until-success
  4258. 'org-todo-setup-filter-hook kws)
  4259. kws)))
  4260. (setq inter (pop kws) sep (member "|" kws)
  4261. kws0 (delete "|" (copy-sequence kws))
  4262. kwsa nil
  4263. kws1 (mapcar
  4264. (lambda (x)
  4265. ;; 1 2
  4266. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4267. (progn
  4268. (setq kw (match-string 1 x)
  4269. key (and (match-end 2) (match-string 2 x))
  4270. log (org-extract-log-state-settings x))
  4271. (push (cons kw (and key (string-to-char key))) kwsa)
  4272. (and log (push log org-todo-log-states))
  4273. kw)
  4274. (error "Invalid TODO keyword %s" x)))
  4275. kws0)
  4276. kwsa (if kwsa (append '((:startgroup))
  4277. (nreverse kwsa)
  4278. '((:endgroup))))
  4279. hw (car kws1)
  4280. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4281. tail (list inter hw (car dws) (org-last dws))))
  4282. (add-to-list 'org-todo-heads hw 'append)
  4283. (push kws1 org-todo-sets)
  4284. (setq org-done-keywords (append org-done-keywords dws nil))
  4285. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4286. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4287. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4288. (setq org-todo-sets (nreverse org-todo-sets)
  4289. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4290. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4291. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4292. ;; Process the constants
  4293. (when const
  4294. (let (e cst)
  4295. (while (setq e (pop const))
  4296. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4297. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4298. (setq org-table-formula-constants-local cst)))
  4299. ;; Process the tags.
  4300. (when tags
  4301. (let (e tgs)
  4302. (while (setq e (pop tags))
  4303. (cond
  4304. ((equal e "{") (push '(:startgroup) tgs))
  4305. ((equal e "}") (push '(:endgroup) tgs))
  4306. ((equal e "\\n") (push '(:newline) tgs))
  4307. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4308. (push (cons (match-string 1 e)
  4309. (string-to-char (match-string 2 e)))
  4310. tgs))
  4311. (t (push (list e) tgs))))
  4312. (org-set-local 'org-tag-alist nil)
  4313. (while (setq e (pop tgs))
  4314. (or (and (stringp (car e))
  4315. (assoc (car e) org-tag-alist))
  4316. (push e org-tag-alist)))))
  4317. ;; Compute the regular expressions and other local variables.
  4318. ;; Using `org-outline-regexp-bol' would complicate them much,
  4319. ;; because of the fixed white space at the end of that string.
  4320. (if (not org-done-keywords)
  4321. (setq org-done-keywords (and org-todo-keywords-1
  4322. (list (org-last org-todo-keywords-1)))))
  4323. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4324. (length org-scheduled-string)
  4325. (length org-clock-string)
  4326. (length org-closed-string)))
  4327. org-drawer-regexp
  4328. (concat "^[ \t]*:\\("
  4329. (mapconcat 'regexp-quote org-drawers "\\|")
  4330. "\\):[ \t]*$")
  4331. org-not-done-keywords
  4332. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4333. org-todo-regexp
  4334. (concat "\\("
  4335. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4336. "\\)")
  4337. org-not-done-regexp
  4338. (concat "\\("
  4339. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4340. "\\)")
  4341. org-not-done-heading-regexp
  4342. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4343. org-todo-line-regexp
  4344. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4345. org-complex-heading-regexp
  4346. (concat "^\\(\\*+\\)"
  4347. "\\(?: +" org-todo-regexp "\\)?"
  4348. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4349. "\\(?: +\\(.*?\\)\\)??"
  4350. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4351. "[ \t]*$")
  4352. org-complex-heading-regexp-format
  4353. (concat "^\\(\\*+\\)"
  4354. "\\(?: +" org-todo-regexp "\\)?"
  4355. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4356. "\\(?: +"
  4357. ;; Stats cookies can be stuck to body.
  4358. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4359. "\\(%s\\)"
  4360. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4361. "\\)"
  4362. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4363. "[ \t]*$")
  4364. org-todo-line-tags-regexp
  4365. (concat "^\\(\\*+\\)"
  4366. "\\(?: +" org-todo-regexp "\\)?"
  4367. "\\(?: +\\(.*?\\)\\)??"
  4368. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4369. "[ \t]*$")
  4370. org-deadline-regexp (concat "\\<" org-deadline-string)
  4371. org-deadline-time-regexp
  4372. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4373. org-deadline-line-regexp
  4374. (concat "\\<\\(" org-deadline-string "\\).*")
  4375. org-scheduled-regexp
  4376. (concat "\\<" org-scheduled-string)
  4377. org-scheduled-time-regexp
  4378. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4379. org-closed-time-regexp
  4380. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4381. org-keyword-time-regexp
  4382. (concat "\\<\\(" org-scheduled-string
  4383. "\\|" org-deadline-string
  4384. "\\|" org-closed-string
  4385. "\\|" org-clock-string "\\)"
  4386. " *[[<]\\([^]>]+\\)[]>]")
  4387. org-keyword-time-not-clock-regexp
  4388. (concat "\\<\\(" org-scheduled-string
  4389. "\\|" org-deadline-string
  4390. "\\|" org-closed-string
  4391. "\\)"
  4392. " *[[<]\\([^]>]+\\)[]>]")
  4393. org-maybe-keyword-time-regexp
  4394. (concat "\\(\\<\\(" org-scheduled-string
  4395. "\\|" org-deadline-string
  4396. "\\|" org-closed-string
  4397. "\\|" org-clock-string "\\)\\)?"
  4398. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4399. org-all-time-keywords
  4400. (mapcar (lambda (w) (substring w 0 -1))
  4401. (list org-scheduled-string org-deadline-string
  4402. org-clock-string org-closed-string)))
  4403. (org-set-font-lock-defaults))))
  4404. (defun org-file-contents (file &optional noerror)
  4405. "Return the contents of FILE, as a string."
  4406. (if (or (not file)
  4407. (not (file-readable-p file)))
  4408. (if noerror
  4409. (progn
  4410. (message "Cannot read file \"%s\"" file)
  4411. (ding) (sit-for 2)
  4412. "")
  4413. (error "Cannot read file \"%s\"" file))
  4414. (with-temp-buffer
  4415. (insert-file-contents file)
  4416. (buffer-string))))
  4417. (defun org-extract-log-state-settings (x)
  4418. "Extract the log state setting from a TODO keyword string.
  4419. This will extract info from a string like \"WAIT(w@/!)\"."
  4420. (let (kw key log1 log2)
  4421. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4422. (setq kw (match-string 1 x)
  4423. key (and (match-end 2) (match-string 2 x))
  4424. log1 (and (match-end 3) (match-string 3 x))
  4425. log2 (and (match-end 4) (match-string 4 x)))
  4426. (and (or log1 log2)
  4427. (list kw
  4428. (and log1 (if (equal log1 "!") 'time 'note))
  4429. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4430. (defun org-remove-keyword-keys (list)
  4431. "Remove a pair of parenthesis at the end of each string in LIST."
  4432. (mapcar (lambda (x)
  4433. (if (string-match "(.*)$" x)
  4434. (substring x 0 (match-beginning 0))
  4435. x))
  4436. list))
  4437. (defun org-assign-fast-keys (alist)
  4438. "Assign fast keys to a keyword-key alist.
  4439. Respect keys that are already there."
  4440. (let (new e (alt ?0))
  4441. (while (setq e (pop alist))
  4442. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4443. (cdr e)) ;; Key already assigned.
  4444. (push e new)
  4445. (let ((clist (string-to-list (downcase (car e))))
  4446. (used (append new alist)))
  4447. (when (= (car clist) ?@)
  4448. (pop clist))
  4449. (while (and clist (rassoc (car clist) used))
  4450. (pop clist))
  4451. (unless clist
  4452. (while (rassoc alt used)
  4453. (incf alt)))
  4454. (push (cons (car e) (or (car clist) alt)) new))))
  4455. (nreverse new)))
  4456. ;;; Some variables used in various places
  4457. (defvar org-window-configuration nil
  4458. "Used in various places to store a window configuration.")
  4459. (defvar org-selected-window nil
  4460. "Used in various places to store a window configuration.")
  4461. (defvar org-finish-function nil
  4462. "Function to be called when `C-c C-c' is used.
  4463. This is for getting out of special buffers like capture.")
  4464. ;; FIXME: Occasionally check by commenting these, to make sure
  4465. ;; no other functions uses these, forgetting to let-bind them.
  4466. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4467. (defvar org-last-state)
  4468. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4469. ;; Defined somewhere in this file, but used before definition.
  4470. (defvar org-entities) ;; defined in org-entities.el
  4471. (defvar org-struct-menu)
  4472. (defvar org-org-menu)
  4473. (defvar org-tbl-menu)
  4474. ;;;; Define the Org-mode
  4475. ;; We use a before-change function to check if a table might need
  4476. ;; an update.
  4477. (defvar org-table-may-need-update t
  4478. "Indicates that a table might need an update.
  4479. This variable is set by `org-before-change-function'.
  4480. `org-table-align' sets it back to nil.")
  4481. (defun org-before-change-function (beg end)
  4482. "Every change indicates that a table might need an update."
  4483. (setq org-table-may-need-update t))
  4484. (defvar org-mode-map)
  4485. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4486. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4487. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4488. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4489. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4490. (defvar org-table-buffer-is-an nil)
  4491. (defvar bidi-paragraph-direction)
  4492. (defvar buffer-face-mode-face)
  4493. (require 'outline)
  4494. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4495. (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"))
  4496. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4497. ;; Other stuff we need.
  4498. (require 'time-date)
  4499. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4500. (require 'easymenu)
  4501. (require 'overlay)
  4502. ;; (require 'org-macs) moved higher up in the file before it is first used
  4503. (require 'org-entities)
  4504. ;; (require 'org-compat) moved higher up in the file before it is first used
  4505. (require 'org-faces)
  4506. (require 'org-list)
  4507. (require 'org-pcomplete)
  4508. (require 'org-src)
  4509. (require 'org-footnote)
  4510. (require 'org-macro)
  4511. ;; babel
  4512. (require 'ob)
  4513. ;;;###autoload
  4514. (define-derived-mode org-mode outline-mode "Org"
  4515. "Outline-based notes management and organizer, alias
  4516. \"Carsten's outline-mode for keeping track of everything.\"
  4517. Org-mode develops organizational tasks around a NOTES file which
  4518. contains information about projects as plain text. Org-mode is
  4519. implemented on top of outline-mode, which is ideal to keep the content
  4520. of large files well structured. It supports ToDo items, deadlines and
  4521. time stamps, which magically appear in the diary listing of the Emacs
  4522. calendar. Tables are easily created with a built-in table editor.
  4523. Plain text URL-like links connect to websites, emails (VM), Usenet
  4524. messages (Gnus), BBDB entries, and any files related to the project.
  4525. For printing and sharing of notes, an Org-mode file (or a part of it)
  4526. can be exported as a structured ASCII or HTML file.
  4527. The following commands are available:
  4528. \\{org-mode-map}"
  4529. ;; Get rid of Outline menus, they are not needed
  4530. ;; Need to do this here because define-derived-mode sets up
  4531. ;; the keymap so late. Still, it is a waste to call this each time
  4532. ;; we switch another buffer into org-mode.
  4533. (if (featurep 'xemacs)
  4534. (when (boundp 'outline-mode-menu-heading)
  4535. ;; Assume this is Greg's port, it uses easymenu
  4536. (easy-menu-remove outline-mode-menu-heading)
  4537. (easy-menu-remove outline-mode-menu-show)
  4538. (easy-menu-remove outline-mode-menu-hide))
  4539. (define-key org-mode-map [menu-bar headings] 'undefined)
  4540. (define-key org-mode-map [menu-bar hide] 'undefined)
  4541. (define-key org-mode-map [menu-bar show] 'undefined))
  4542. (org-load-modules-maybe)
  4543. (easy-menu-add org-org-menu)
  4544. (easy-menu-add org-tbl-menu)
  4545. (org-install-agenda-files-menu)
  4546. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4547. (add-to-invisibility-spec '(org-cwidth))
  4548. (add-to-invisibility-spec '(org-hide-block . t))
  4549. (when (featurep 'xemacs)
  4550. (org-set-local 'line-move-ignore-invisible t))
  4551. (org-set-local 'outline-regexp org-outline-regexp)
  4552. (org-set-local 'outline-level 'org-outline-level)
  4553. (setq bidi-paragraph-direction 'left-to-right)
  4554. (when (and org-ellipsis
  4555. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4556. (fboundp 'make-glyph-code))
  4557. (unless org-display-table
  4558. (setq org-display-table (make-display-table)))
  4559. (set-display-table-slot
  4560. org-display-table 4
  4561. (vconcat (mapcar
  4562. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4563. org-ellipsis)))
  4564. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4565. (setq buffer-display-table org-display-table))
  4566. (org-set-regexps-and-options)
  4567. (when (and org-tag-faces (not org-tags-special-faces-re))
  4568. ;; tag faces set outside customize.... force initialization.
  4569. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4570. ;; Calc embedded
  4571. (org-set-local 'calc-embedded-open-mode "# ")
  4572. (mapc (lambda(c) (modify-syntax-entry (string-to-char (car c)) "w p"))
  4573. org-emphasis-alist)
  4574. (modify-syntax-entry ?< "(")
  4575. (modify-syntax-entry ?> ")")
  4576. (modify-syntax-entry ?{ "(")
  4577. (modify-syntax-entry ?} ")")
  4578. (modify-syntax-entry ?@ "w")
  4579. (modify-syntax-entry ?\" "\"")
  4580. (if org-startup-truncated (setq truncate-lines t))
  4581. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4582. (org-set-local 'font-lock-unfontify-region-function
  4583. 'org-unfontify-region)
  4584. ;; Activate before-change-function
  4585. (org-set-local 'org-table-may-need-update t)
  4586. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4587. 'local)
  4588. ;; Check for running clock before killing a buffer
  4589. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4590. ;; Initialize macros templates.
  4591. (org-macro-initialize-templates)
  4592. ;; Initialize radio targets.
  4593. (org-update-radio-target-regexp)
  4594. ;; Indentation.
  4595. (org-set-local 'indent-line-function 'org-indent-line)
  4596. (org-set-local 'indent-region-function 'org-indent-region)
  4597. ;; Filling and auto-filling.
  4598. (org-setup-filling)
  4599. ;; Comments.
  4600. (org-setup-comments-handling)
  4601. ;; Beginning/end of defun
  4602. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4603. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4604. ;; Next error for sparse trees
  4605. (org-set-local 'next-error-function 'org-occur-next-match)
  4606. ;; Make sure dependence stuff works reliably, even for users who set it
  4607. ;; too late :-(
  4608. (if org-enforce-todo-dependencies
  4609. (add-hook 'org-blocker-hook
  4610. 'org-block-todo-from-children-or-siblings-or-parent)
  4611. (remove-hook 'org-blocker-hook
  4612. 'org-block-todo-from-children-or-siblings-or-parent))
  4613. (if org-enforce-todo-checkbox-dependencies
  4614. (add-hook 'org-blocker-hook
  4615. 'org-block-todo-from-checkboxes)
  4616. (remove-hook 'org-blocker-hook
  4617. 'org-block-todo-from-checkboxes))
  4618. ;; Align options lines
  4619. (org-set-local
  4620. 'align-mode-rules-list
  4621. '((org-in-buffer-settings
  4622. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4623. (modes . '(org-mode)))))
  4624. ;; Imenu
  4625. (org-set-local 'imenu-create-index-function
  4626. 'org-imenu-get-tree)
  4627. ;; Make isearch reveal context
  4628. (if (or (featurep 'xemacs)
  4629. (not (boundp 'outline-isearch-open-invisible-function)))
  4630. ;; Emacs 21 and XEmacs make use of the hook
  4631. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4632. ;; Emacs 22 deals with this through a special variable
  4633. (org-set-local 'outline-isearch-open-invisible-function
  4634. (lambda (&rest ignore) (org-show-context 'isearch))))
  4635. ;; Setup the pcomplete hooks
  4636. (set (make-local-variable 'pcomplete-command-completion-function)
  4637. 'org-pcomplete-initial)
  4638. (set (make-local-variable 'pcomplete-command-name-function)
  4639. 'org-command-at-point)
  4640. (set (make-local-variable 'pcomplete-default-completion-function)
  4641. 'ignore)
  4642. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4643. 'org-parse-arguments)
  4644. (set (make-local-variable 'pcomplete-termination-string) "")
  4645. (when (>= emacs-major-version 23)
  4646. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4647. ;; If empty file that did not turn on org-mode automatically, make it to.
  4648. (if (and org-insert-mode-line-in-empty-file
  4649. (org-called-interactively-p 'any)
  4650. (= (point-min) (point-max)))
  4651. (insert "# -*- mode: org -*-\n\n"))
  4652. (unless org-inhibit-startup
  4653. (org-unmodified
  4654. (and org-startup-with-beamer-mode (org-beamer-mode))
  4655. (when org-startup-align-all-tables
  4656. (org-table-map-tables 'org-table-align 'quietly))
  4657. (when org-startup-with-inline-images
  4658. (org-display-inline-images))
  4659. (when org-startup-with-latex-preview
  4660. (org-preview-latex-fragment))
  4661. (unless org-inhibit-startup-visibility-stuff
  4662. (org-set-startup-visibility))))
  4663. ;; Try to set org-hide correctly
  4664. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4665. (when (fboundp 'abbrev-table-put)
  4666. (abbrev-table-put org-mode-abbrev-table
  4667. :parents (list text-mode-abbrev-table)))
  4668. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4669. (defun org-find-invisible-foreground ()
  4670. (let ((candidates (remove
  4671. "unspecified-bg"
  4672. (nconc
  4673. (list (face-background 'default)
  4674. (face-background 'org-default))
  4675. (mapcar
  4676. (lambda (alist)
  4677. (when (boundp alist)
  4678. (cdr (assoc 'background-color (symbol-value alist)))))
  4679. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4680. (list (face-foreground 'org-hide))))))
  4681. (car (remove nil candidates))))
  4682. (defun org-current-time (&optional rounding-minutes)
  4683. "Current time, possibly rounded to ROUNDING-MINUTES.
  4684. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4685. `org-time-stamp-rounding-minutes'."
  4686. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4687. (car org-time-stamp-rounding-minutes)))
  4688. (time (decode-time)))
  4689. (if (> r 1)
  4690. (apply 'encode-time
  4691. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4692. (nthcdr 2 time)))
  4693. (current-time))))
  4694. (defun org-today ()
  4695. "Return today date, considering `org-extend-today-until'."
  4696. (time-to-days
  4697. (time-subtract (current-time)
  4698. (list 0 (* 3600 org-extend-today-until) 0))))
  4699. ;;;; Font-Lock stuff, including the activators
  4700. (defvar org-mouse-map (make-sparse-keymap))
  4701. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4702. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4703. (when org-mouse-1-follows-link
  4704. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4705. (when org-tab-follows-link
  4706. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4707. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4708. (require 'font-lock)
  4709. (defconst org-non-link-chars "]\t\n\r<>")
  4710. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4711. "shell" "elisp" "doi" "message"))
  4712. (defvar org-link-types-re nil
  4713. "Matches a link that has a url-like prefix like \"http:\"")
  4714. (defvar org-link-re-with-space nil
  4715. "Matches a link with spaces, optional angular brackets around it.")
  4716. (defvar org-link-re-with-space2 nil
  4717. "Matches a link with spaces, optional angular brackets around it.")
  4718. (defvar org-link-re-with-space3 nil
  4719. "Matches a link with spaces, only for internal part in bracket links.")
  4720. (defvar org-angle-link-re nil
  4721. "Matches link with angular brackets, spaces are allowed.")
  4722. (defvar org-plain-link-re nil
  4723. "Matches plain link, without spaces.")
  4724. (defvar org-bracket-link-regexp nil
  4725. "Matches a link in double brackets.")
  4726. (defvar org-bracket-link-analytic-regexp nil
  4727. "Regular expression used to analyze links.
  4728. Here is what the match groups contain after a match:
  4729. 1: http:
  4730. 2: http
  4731. 3: path
  4732. 4: [desc]
  4733. 5: desc")
  4734. (defvar org-bracket-link-analytic-regexp++ nil
  4735. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4736. (defvar org-any-link-re nil
  4737. "Regular expression matching any link.")
  4738. (defconst org-match-sexp-depth 3
  4739. "Number of stacked braces for sub/superscript matching.")
  4740. (defun org-create-multibrace-regexp (left right n)
  4741. "Create a regular expression which will match a balanced sexp.
  4742. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4743. as single character strings.
  4744. The regexp returned will match the entire expression including the
  4745. delimiters. It will also define a single group which contains the
  4746. match except for the outermost delimiters. The maximum depth of
  4747. stacked delimiters is N. Escaping delimiters is not possible."
  4748. (let* ((nothing (concat "[^" left right "]*?"))
  4749. (or "\\|")
  4750. (re nothing)
  4751. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4752. (while (> n 1)
  4753. (setq n (1- n)
  4754. re (concat re or next)
  4755. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4756. (concat left "\\(" re "\\)" right)))
  4757. (defvar org-match-substring-regexp
  4758. (concat
  4759. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4760. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4761. "\\|"
  4762. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4763. "\\|"
  4764. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4765. "The regular expression matching a sub- or superscript.")
  4766. (defvar org-match-substring-with-braces-regexp
  4767. (concat
  4768. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4769. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4770. "\\)")
  4771. "The regular expression matching a sub- or superscript, forcing braces.")
  4772. (defun org-make-link-regexps ()
  4773. "Update the link regular expressions.
  4774. This should be called after the variable `org-link-types' has changed."
  4775. (setq org-link-types-re
  4776. (concat
  4777. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4778. org-link-re-with-space
  4779. (concat
  4780. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4781. "\\([^" org-non-link-chars " ]"
  4782. "[^" org-non-link-chars "]*"
  4783. "[^" org-non-link-chars " ]\\)>?")
  4784. org-link-re-with-space2
  4785. (concat
  4786. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4787. "\\([^" org-non-link-chars " ]"
  4788. "[^\t\n\r]*"
  4789. "[^" org-non-link-chars " ]\\)>?")
  4790. org-link-re-with-space3
  4791. (concat
  4792. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4793. "\\([^" org-non-link-chars " ]"
  4794. "[^\t\n\r]*\\)")
  4795. org-angle-link-re
  4796. (concat
  4797. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4798. "\\([^" org-non-link-chars " ]"
  4799. "[^" org-non-link-chars "]*"
  4800. "\\)>")
  4801. org-plain-link-re
  4802. (concat
  4803. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4804. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4805. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4806. org-bracket-link-regexp
  4807. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4808. org-bracket-link-analytic-regexp
  4809. (concat
  4810. "\\[\\["
  4811. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4812. "\\([^]]+\\)"
  4813. "\\]"
  4814. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4815. "\\]")
  4816. org-bracket-link-analytic-regexp++
  4817. (concat
  4818. "\\[\\["
  4819. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4820. "\\([^]]+\\)"
  4821. "\\]"
  4822. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4823. "\\]")
  4824. org-any-link-re
  4825. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4826. org-angle-link-re "\\)\\|\\("
  4827. org-plain-link-re "\\)")))
  4828. (org-make-link-regexps)
  4829. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4830. "Regular expression for fast time stamp matching.")
  4831. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4832. "Regular expression for fast time stamp matching.")
  4833. (defconst org-ts-regexp0
  4834. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4835. "Regular expression matching time strings for analysis.
  4836. This one does not require the space after the date, so it can be used
  4837. on a string that terminates immediately after the date.")
  4838. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4839. "Regular expression matching time strings for analysis.")
  4840. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4841. "Regular expression matching time stamps, with groups.")
  4842. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4843. "Regular expression matching time stamps (also [..]), with groups.")
  4844. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4845. "Regular expression matching a time stamp range.")
  4846. (defconst org-tr-regexp-both
  4847. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4848. "Regular expression matching a time stamp range.")
  4849. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4850. org-ts-regexp "\\)?")
  4851. "Regular expression matching a time stamp or time stamp range.")
  4852. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4853. org-ts-regexp-both "\\)?")
  4854. "Regular expression matching a time stamp or time stamp range.
  4855. The time stamps may be either active or inactive.")
  4856. (defvar org-emph-face nil)
  4857. (defun org-do-emphasis-faces (limit)
  4858. "Run through the buffer and add overlays to emphasized strings."
  4859. (let (rtn a)
  4860. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4861. (if (not (= (char-after (match-beginning 3))
  4862. (char-after (match-beginning 4))))
  4863. (progn
  4864. (setq rtn t)
  4865. (setq a (assoc (match-string 3) org-emphasis-alist))
  4866. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4867. 'face
  4868. (nth 1 a))
  4869. (and (nth 4 a)
  4870. (org-remove-flyspell-overlays-in
  4871. (match-beginning 0) (match-end 0)))
  4872. (add-text-properties (match-beginning 2) (match-end 2)
  4873. '(font-lock-multiline t org-emphasis t))
  4874. (when org-hide-emphasis-markers
  4875. (add-text-properties (match-end 4) (match-beginning 5)
  4876. '(invisible org-link))
  4877. (add-text-properties (match-beginning 3) (match-end 3)
  4878. '(invisible org-link)))))
  4879. (backward-char 1))
  4880. rtn))
  4881. (defun org-emphasize (&optional char)
  4882. "Insert or change an emphasis, i.e. a font like bold or italic.
  4883. If there is an active region, change that region to a new emphasis.
  4884. If there is no region, just insert the marker characters and position
  4885. the cursor between them.
  4886. CHAR should be either the marker character, or the first character of the
  4887. HTML tag associated with that emphasis. If CHAR is a space, the means
  4888. to remove the emphasis of the selected region.
  4889. If char is not given (for example in an interactive call) it
  4890. will be prompted for."
  4891. (interactive)
  4892. (let ((eal org-emphasis-alist) e det
  4893. (erc org-emphasis-regexp-components)
  4894. (prompt "")
  4895. (string "") beg end move tag c s)
  4896. (if (org-region-active-p)
  4897. (setq beg (region-beginning) end (region-end)
  4898. string (buffer-substring beg end))
  4899. (setq move t))
  4900. (while (setq e (pop eal))
  4901. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4902. c (aref tag 0))
  4903. (push (cons c (string-to-char (car e))) det)
  4904. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4905. (substring tag 1)))))
  4906. (setq det (nreverse det))
  4907. (unless char
  4908. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4909. (setq char (read-char-exclusive)))
  4910. (setq char (or (cdr (assoc char det)) char))
  4911. (if (equal char ?\ )
  4912. (setq s "" move nil)
  4913. (unless (assoc (char-to-string char) org-emphasis-alist)
  4914. (error "No such emphasis marker: \"%c\"" char))
  4915. (setq s (char-to-string char)))
  4916. (while (and (> (length string) 1)
  4917. (equal (substring string 0 1) (substring string -1))
  4918. (assoc (substring string 0 1) org-emphasis-alist))
  4919. (setq string (substring string 1 -1)))
  4920. (setq string (concat s string s))
  4921. (if beg (delete-region beg end))
  4922. (unless (or (bolp)
  4923. (string-match (concat "[" (nth 0 erc) "\n]")
  4924. (char-to-string (char-before (point)))))
  4925. (insert " "))
  4926. (unless (or (eobp)
  4927. (string-match (concat "[" (nth 1 erc) "\n]")
  4928. (char-to-string (char-after (point)))))
  4929. (insert " ") (backward-char 1))
  4930. (insert string)
  4931. (and move (backward-char 1))))
  4932. (defconst org-nonsticky-props
  4933. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4934. (defsubst org-rear-nonsticky-at (pos)
  4935. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4936. (defun org-activate-plain-links (limit)
  4937. "Run through the buffer and add overlays to links."
  4938. (catch 'exit
  4939. (let (f hl)
  4940. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4941. (not (org-in-src-block-p)))
  4942. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4943. (setq f (get-text-property (match-beginning 0) 'face))
  4944. (setq hl (org-match-string-no-properties 0))
  4945. (if (or (eq f 'org-tag)
  4946. (and (listp f) (memq 'org-tag f)))
  4947. nil
  4948. (add-text-properties (match-beginning 0) (match-end 0)
  4949. (list 'mouse-face 'highlight
  4950. 'face 'org-link
  4951. 'htmlize-link `(:uri ,hl)
  4952. 'keymap org-mouse-map))
  4953. (org-rear-nonsticky-at (match-end 0)))
  4954. t))))
  4955. (defun org-activate-code (limit)
  4956. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4957. (progn
  4958. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4959. (remove-text-properties (match-beginning 0) (match-end 0)
  4960. '(display t invisible t intangible t))
  4961. t)))
  4962. (defcustom org-src-fontify-natively nil
  4963. "When non-nil, fontify code in code blocks."
  4964. :type 'boolean
  4965. :version "24.1"
  4966. :group 'org-appearance
  4967. :group 'org-babel)
  4968. (defcustom org-allow-promoting-top-level-subtree nil
  4969. "When non-nil, allow promoting a top level subtree.
  4970. The leading star of the top level headline will be replaced
  4971. by a #."
  4972. :type 'boolean
  4973. :version "24.1"
  4974. :group 'org-appearance)
  4975. (defun org-fontify-meta-lines-and-blocks (limit)
  4976. (condition-case nil
  4977. (org-fontify-meta-lines-and-blocks-1 limit)
  4978. (error (message "org-mode fontification error"))))
  4979. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4980. "Fontify #+ lines and blocks, in the correct ways."
  4981. (let ((case-fold-search t))
  4982. (if (re-search-forward
  4983. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4984. limit t)
  4985. (let ((beg (match-beginning 0))
  4986. (block-start (match-end 0))
  4987. (block-end nil)
  4988. (lang (match-string 7))
  4989. (beg1 (line-beginning-position 2))
  4990. (dc1 (downcase (match-string 2)))
  4991. (dc3 (downcase (match-string 3)))
  4992. end end1 quoting block-type ovl)
  4993. (cond
  4994. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  4995. ;; a single line of backend-specific content
  4996. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4997. (remove-text-properties (match-beginning 0) (match-end 0)
  4998. '(display t invisible t intangible t))
  4999. (add-text-properties (match-beginning 1) (match-end 3)
  5000. '(font-lock-fontified t face org-meta-line))
  5001. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5002. '(font-lock-fontified t face org-block))
  5003. ; for backend-specific code
  5004. t)
  5005. ((and (match-end 4) (equal dc3 "+begin"))
  5006. ;; Truly a block
  5007. (setq block-type (downcase (match-string 5))
  5008. quoting (member block-type org-protecting-blocks))
  5009. (when (re-search-forward
  5010. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5011. nil t) ;; on purpose, we look further than LIMIT
  5012. (setq end (min (point-max) (match-end 0))
  5013. end1 (min (point-max) (1- (match-beginning 0))))
  5014. (setq block-end (match-beginning 0))
  5015. (when quoting
  5016. (remove-text-properties beg end
  5017. '(display t invisible t intangible t)))
  5018. (add-text-properties
  5019. beg end
  5020. '(font-lock-fontified t font-lock-multiline t))
  5021. (add-text-properties beg beg1 '(face org-meta-line))
  5022. (add-text-properties end1 (min (point-max) (1+ end))
  5023. '(face org-meta-line)) ; for end_src
  5024. (cond
  5025. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5026. (org-src-font-lock-fontify-block lang block-start block-end)
  5027. ;; remove old background overlays
  5028. (mapc (lambda (ov)
  5029. (if (eq (overlay-get ov 'face) 'org-block-background)
  5030. (delete-overlay ov)))
  5031. (overlays-at (/ (+ beg1 block-end) 2)))
  5032. ;; add a background overlay
  5033. (setq ovl (make-overlay beg1 block-end))
  5034. (overlay-put ovl 'face 'org-block-background)
  5035. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5036. (quoting
  5037. (add-text-properties beg1 (min (point-max) (1+ end1))
  5038. '(face org-block))) ; end of source block
  5039. ((not org-fontify-quote-and-verse-blocks))
  5040. ((string= block-type "quote")
  5041. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5042. ((string= block-type "verse")
  5043. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5044. (add-text-properties beg beg1 '(face org-block-begin-line))
  5045. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5046. '(face org-block-end-line))
  5047. t))
  5048. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5049. (add-text-properties
  5050. beg (match-end 3)
  5051. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5052. '(font-lock-fontified t invisible t)
  5053. '(font-lock-fontified t face org-document-info-keyword)))
  5054. (add-text-properties
  5055. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5056. (if (string-equal dc1 "+title:")
  5057. '(font-lock-fontified t face org-document-title)
  5058. '(font-lock-fontified t face org-document-info))))
  5059. ((or (equal dc1 "+results")
  5060. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5061. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5062. "+call:" "+header:" "+headers:" "+name:"))
  5063. (and (match-end 4) (equal dc3 "+attr")))
  5064. (add-text-properties
  5065. beg (match-end 0)
  5066. '(font-lock-fontified t face org-meta-line))
  5067. t)
  5068. ((member dc3 '(" " ""))
  5069. (add-text-properties
  5070. beg (match-end 0)
  5071. '(font-lock-fontified t face font-lock-comment-face)))
  5072. ((not (member (char-after beg) '(?\ ?\t)))
  5073. ;; just any other in-buffer setting, but not indented
  5074. (add-text-properties
  5075. beg (match-end 0)
  5076. '(font-lock-fontified t face org-meta-line))
  5077. t)
  5078. (t nil))))))
  5079. (defun org-activate-angle-links (limit)
  5080. "Run through the buffer and add overlays to links."
  5081. (if (and (re-search-forward org-angle-link-re limit t)
  5082. (not (org-in-src-block-p)))
  5083. (progn
  5084. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5085. (add-text-properties (match-beginning 0) (match-end 0)
  5086. (list 'mouse-face 'highlight
  5087. 'keymap org-mouse-map))
  5088. (org-rear-nonsticky-at (match-end 0))
  5089. t)))
  5090. (defun org-activate-footnote-links (limit)
  5091. "Run through the buffer and add overlays to footnotes."
  5092. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5093. (when fn
  5094. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5095. (org-remove-flyspell-overlays-in beg end)
  5096. (add-text-properties beg end
  5097. (list 'mouse-face 'highlight
  5098. 'keymap org-mouse-map
  5099. 'help-echo
  5100. (if (= (point-at-bol) beg)
  5101. "Footnote definition"
  5102. "Footnote reference")
  5103. 'font-lock-fontified t
  5104. 'font-lock-multiline t
  5105. 'face 'org-footnote))))))
  5106. (defun org-activate-bracket-links (limit)
  5107. "Run through the buffer and add overlays to bracketed links."
  5108. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5109. (not (org-in-src-block-p)))
  5110. (let* ((hl (org-match-string-no-properties 1))
  5111. (help (concat "LINK: " hl))
  5112. ;; FIXME: Above we should remove the escapes. But that
  5113. ;; requires another match, protecting match data, a lot
  5114. ;; of overhead for font-lock.
  5115. (ip (org-maybe-intangible
  5116. (list 'invisible 'org-link
  5117. 'keymap org-mouse-map 'mouse-face 'highlight
  5118. 'font-lock-multiline t 'help-echo help
  5119. 'htmlize-link `(:uri ,hl))))
  5120. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5121. 'font-lock-multiline t 'help-echo help
  5122. 'htmlize-link `(:uri ,hl))))
  5123. ;; We need to remove the invisible property here. Table narrowing
  5124. ;; may have made some of this invisible.
  5125. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5126. (remove-text-properties (match-beginning 0) (match-end 0)
  5127. '(invisible nil))
  5128. (if (match-end 3)
  5129. (progn
  5130. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5131. (org-rear-nonsticky-at (match-beginning 3))
  5132. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5133. (org-rear-nonsticky-at (match-end 3))
  5134. (add-text-properties (match-end 3) (match-end 0) ip)
  5135. (org-rear-nonsticky-at (match-end 0)))
  5136. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5137. (org-rear-nonsticky-at (match-beginning 1))
  5138. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5139. (org-rear-nonsticky-at (match-end 1))
  5140. (add-text-properties (match-end 1) (match-end 0) ip)
  5141. (org-rear-nonsticky-at (match-end 0)))
  5142. t)))
  5143. (defun org-activate-dates (limit)
  5144. "Run through the buffer and add overlays to dates."
  5145. (if (re-search-forward org-tsr-regexp-both limit t)
  5146. (progn
  5147. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5148. (add-text-properties (match-beginning 0) (match-end 0)
  5149. (list 'mouse-face 'highlight
  5150. 'keymap org-mouse-map))
  5151. (org-rear-nonsticky-at (match-end 0))
  5152. (when org-display-custom-times
  5153. (if (match-end 3)
  5154. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5155. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5156. t)))
  5157. (defvar org-target-link-regexp nil
  5158. "Regular expression matching radio targets in plain text.")
  5159. (make-variable-buffer-local 'org-target-link-regexp)
  5160. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5161. "Regular expression matching a link target.")
  5162. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5163. "Regular expression matching a radio target.")
  5164. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5165. "Regular expression matching any target.")
  5166. (defun org-activate-target-links (limit)
  5167. "Run through the buffer and add overlays to target matches."
  5168. (when org-target-link-regexp
  5169. (let ((case-fold-search t))
  5170. (if (re-search-forward org-target-link-regexp limit t)
  5171. (progn
  5172. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5173. (add-text-properties (match-beginning 0) (match-end 0)
  5174. (list 'mouse-face 'highlight
  5175. 'keymap org-mouse-map
  5176. 'help-echo "Radio target link"
  5177. 'org-linked-text t))
  5178. (org-rear-nonsticky-at (match-end 0))
  5179. t)))))
  5180. (defun org-update-radio-target-regexp ()
  5181. "Find all radio targets in this file and update the regular expression."
  5182. (interactive)
  5183. (when (memq 'radio org-activate-links)
  5184. (setq org-target-link-regexp
  5185. (org-make-target-link-regexp (org-all-targets 'radio)))
  5186. (org-restart-font-lock)))
  5187. (defun org-hide-wide-columns (limit)
  5188. (let (s e)
  5189. (setq s (text-property-any (point) (or limit (point-max))
  5190. 'org-cwidth t))
  5191. (when s
  5192. (setq e (next-single-property-change s 'org-cwidth))
  5193. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5194. (goto-char e)
  5195. t)))
  5196. (defvar org-match-substring-regexp)
  5197. (defvar org-match-substring-with-braces-regexp)
  5198. (defun org-restart-font-lock ()
  5199. "Restart `font-lock-mode', to force refontification."
  5200. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5201. (font-lock-mode -1)
  5202. (font-lock-mode 1)))
  5203. (defun org-all-targets (&optional radio)
  5204. "Return a list of all targets in this file.
  5205. When optional argument RADIO is non-nil, only find radio
  5206. targets."
  5207. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5208. (save-excursion
  5209. (goto-char (point-min))
  5210. (while (re-search-forward re nil t)
  5211. ;; Make sure point is really within the object.
  5212. (backward-char)
  5213. (let ((obj (org-element-context)))
  5214. (when (memq (org-element-type obj) '(radio-target target))
  5215. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5216. rtn)))
  5217. (defun org-make-target-link-regexp (targets)
  5218. "Make regular expression matching all strings in TARGETS.
  5219. The regular expression finds the targets also if there is a line break
  5220. between words."
  5221. (and targets
  5222. (concat
  5223. "\\<\\("
  5224. (mapconcat
  5225. (lambda (x)
  5226. (setq x (regexp-quote x))
  5227. (while (string-match " +" x)
  5228. (setq x (replace-match "\\s-+" t t x)))
  5229. x)
  5230. targets
  5231. "\\|")
  5232. "\\)\\>")))
  5233. (defun org-activate-tags (limit)
  5234. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5235. (progn
  5236. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5237. (add-text-properties (match-beginning 1) (match-end 1)
  5238. (list 'mouse-face 'highlight
  5239. 'keymap org-mouse-map))
  5240. (org-rear-nonsticky-at (match-end 1))
  5241. t)))
  5242. (defun org-outline-level ()
  5243. "Compute the outline level of the heading at point.
  5244. If this is called at a normal headline, the level is the number of stars.
  5245. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5246. (save-excursion
  5247. (if (not (condition-case nil
  5248. (org-back-to-heading t)
  5249. (error nil)))
  5250. 0
  5251. (looking-at org-outline-regexp)
  5252. (1- (- (match-end 0) (match-beginning 0))))))
  5253. (defvar org-font-lock-keywords nil)
  5254. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5255. "Regular expression matching a property line.")
  5256. (defvar org-font-lock-hook nil
  5257. "Functions to be called for special font lock stuff.")
  5258. (defvar org-font-lock-set-keywords-hook nil
  5259. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5260. This is called after `org-font-lock-extra-keywords' is defined, but before
  5261. it is installed to be used by font lock. This can be useful if something
  5262. needs to be inserted at a specific position in the font-lock sequence.")
  5263. (defun org-font-lock-hook (limit)
  5264. "Run `org-font-lock-hook' within LIMIT."
  5265. (run-hook-with-args 'org-font-lock-hook limit))
  5266. (defun org-set-font-lock-defaults ()
  5267. "Set font lock defaults for the current buffer."
  5268. (let* ((em org-fontify-emphasized-text)
  5269. (lk org-activate-links)
  5270. (org-font-lock-extra-keywords
  5271. (list
  5272. ;; Call the hook
  5273. '(org-font-lock-hook)
  5274. ;; Headlines
  5275. `(,(if org-fontify-whole-heading-line
  5276. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5277. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5278. (1 (org-get-level-face 1))
  5279. (2 (org-get-level-face 2))
  5280. (3 (org-get-level-face 3)))
  5281. ;; Table lines
  5282. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5283. (1 'org-table t))
  5284. ;; Table internals
  5285. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5286. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5287. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5288. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5289. ;; Drawers
  5290. (list org-drawer-regexp '(0 'org-special-keyword t))
  5291. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5292. ;; Properties
  5293. (list org-property-re
  5294. '(1 'org-special-keyword t)
  5295. '(3 'org-property-value t))
  5296. ;; Links
  5297. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5298. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5299. (if (memq 'plain lk) '(org-activate-plain-links))
  5300. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5301. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5302. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5303. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5304. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5305. '(org-hide-wide-columns (0 nil append))
  5306. ;; TODO keyword
  5307. (list (format org-heading-keyword-regexp-format
  5308. org-todo-regexp)
  5309. '(2 (org-get-todo-face 2) t))
  5310. ;; DONE
  5311. (if org-fontify-done-headline
  5312. (list (format org-heading-keyword-regexp-format
  5313. (concat
  5314. "\\(?:"
  5315. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5316. "\\)"))
  5317. '(2 'org-headline-done t))
  5318. nil)
  5319. ;; Priorities
  5320. '(org-font-lock-add-priority-faces)
  5321. ;; Tags
  5322. '(org-font-lock-add-tag-faces)
  5323. ;; Special keywords
  5324. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5325. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5326. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5327. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5328. ;; Emphasis
  5329. (if em
  5330. (if (featurep 'xemacs)
  5331. '(org-do-emphasis-faces (0 nil append))
  5332. '(org-do-emphasis-faces)))
  5333. ;; Checkboxes
  5334. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5335. 1 'org-checkbox prepend)
  5336. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5337. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5338. (0 (org-get-checkbox-statistics-face) t)))
  5339. ;; Description list items
  5340. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5341. 1 'org-list-dt prepend)
  5342. ;; ARCHIVEd headings
  5343. (list (concat
  5344. org-outline-regexp-bol
  5345. "\\(.*:" org-archive-tag ":.*\\)")
  5346. '(1 'org-archived prepend))
  5347. ;; Specials
  5348. '(org-fontify-entities)
  5349. '(org-raise-scripts)
  5350. ;; Code
  5351. '(org-activate-code (1 'org-code t))
  5352. ;; COMMENT
  5353. (list (format org-heading-keyword-regexp-format
  5354. (concat "\\("
  5355. org-comment-string "\\|" org-quote-string
  5356. "\\)"))
  5357. '(2 'org-special-keyword t))
  5358. ;; Blocks and meta lines
  5359. '(org-fontify-meta-lines-and-blocks)
  5360. )))
  5361. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5362. (run-hooks 'org-font-lock-set-keywords-hook)
  5363. ;; Now set the full font-lock-keywords
  5364. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5365. (org-set-local 'font-lock-defaults
  5366. '(org-font-lock-keywords t nil nil backward-paragraph))
  5367. (kill-local-variable 'font-lock-keywords) nil))
  5368. (defun org-toggle-pretty-entities ()
  5369. "Toggle the composition display of entities as UTF8 characters."
  5370. (interactive)
  5371. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5372. (org-restart-font-lock)
  5373. (if org-pretty-entities
  5374. (message "Entities are displayed as UTF8 characters")
  5375. (save-restriction
  5376. (widen)
  5377. (org-decompose-region (point-min) (point-max))
  5378. (message "Entities are displayed plain"))))
  5379. (defvar org-custom-properties-overlays nil
  5380. "List of overlays used for custom properties.")
  5381. (make-variable-buffer-local 'org-custom-properties-overlays)
  5382. (defun org-toggle-custom-properties-visibility ()
  5383. "Display or hide properties in `org-custom-properties'."
  5384. (interactive)
  5385. (if org-custom-properties-overlays
  5386. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5387. (setq org-custom-properties-overlays nil))
  5388. (unless (not org-custom-properties)
  5389. (save-excursion
  5390. (save-restriction
  5391. (widen)
  5392. (goto-char (point-min))
  5393. (while (re-search-forward org-property-re nil t)
  5394. (mapc (lambda(p)
  5395. (when (equal p (substring (match-string 1) 1 -1))
  5396. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5397. (overlay-put o 'invisible t)
  5398. (overlay-put o 'org-custom-property t)
  5399. (push o org-custom-properties-overlays))))
  5400. org-custom-properties)))))))
  5401. (defun org-fontify-entities (limit)
  5402. "Find an entity to fontify."
  5403. (let (ee)
  5404. (when org-pretty-entities
  5405. (catch 'match
  5406. (while (re-search-forward
  5407. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5408. limit t)
  5409. (if (and (not (org-in-indented-comment-line))
  5410. (setq ee (org-entity-get (match-string 1)))
  5411. (= (length (nth 6 ee)) 1))
  5412. (let*
  5413. ((end (if (equal (match-string 2) "{}")
  5414. (match-end 2)
  5415. (match-end 1))))
  5416. (add-text-properties
  5417. (match-beginning 0) end
  5418. (list 'font-lock-fontified t))
  5419. (compose-region (match-beginning 0) end
  5420. (nth 6 ee) nil)
  5421. (backward-char 1)
  5422. (throw 'match t))))
  5423. nil))))
  5424. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5425. "Fontify string S like in Org-mode."
  5426. (with-temp-buffer
  5427. (insert s)
  5428. (let ((org-odd-levels-only odd-levels))
  5429. (org-mode)
  5430. (font-lock-fontify-buffer)
  5431. (buffer-string))))
  5432. (defvar org-m nil)
  5433. (defvar org-l nil)
  5434. (defvar org-f nil)
  5435. (defun org-get-level-face (n)
  5436. "Get the right face for match N in font-lock matching of headlines."
  5437. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5438. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5439. (if org-cycle-level-faces
  5440. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5441. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5442. (cond
  5443. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5444. ((eq n 2) org-f)
  5445. (t (if org-level-color-stars-only nil org-f))))
  5446. (defun org-get-todo-face (kwd)
  5447. "Get the right face for a TODO keyword KWD.
  5448. If KWD is a number, get the corresponding match group."
  5449. (if (numberp kwd) (setq kwd (match-string kwd)))
  5450. (or (org-face-from-face-or-color
  5451. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5452. (and (member kwd org-done-keywords) 'org-done)
  5453. 'org-todo))
  5454. (defun org-face-from-face-or-color (context inherit face-or-color)
  5455. "Create a face list that inherits INHERIT, but sets the foreground color.
  5456. When FACE-OR-COLOR is not a string, just return it."
  5457. (if (stringp face-or-color)
  5458. (list :inherit inherit
  5459. (cdr (assoc context org-faces-easy-properties))
  5460. face-or-color)
  5461. face-or-color))
  5462. (defun org-font-lock-add-tag-faces (limit)
  5463. "Add the special tag faces."
  5464. (when (and org-tag-faces org-tags-special-faces-re)
  5465. (while (re-search-forward org-tags-special-faces-re limit t)
  5466. (add-text-properties (match-beginning 1) (match-end 1)
  5467. (list 'face (org-get-tag-face 1)
  5468. 'font-lock-fontified t))
  5469. (backward-char 1))))
  5470. (defun org-font-lock-add-priority-faces (limit)
  5471. "Add the special priority faces."
  5472. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5473. (when (save-match-data (org-at-heading-p))
  5474. (add-text-properties
  5475. (match-beginning 0) (match-end 0)
  5476. (list 'face (or (org-face-from-face-or-color
  5477. 'priority 'org-priority
  5478. (cdr (assoc (char-after (match-beginning 1))
  5479. org-priority-faces)))
  5480. 'org-priority)
  5481. 'font-lock-fontified t)))))
  5482. (defun org-get-tag-face (kwd)
  5483. "Get the right face for a TODO keyword KWD.
  5484. If KWD is a number, get the corresponding match group."
  5485. (if (numberp kwd) (setq kwd (match-string kwd)))
  5486. (or (org-face-from-face-or-color
  5487. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5488. 'org-tag))
  5489. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5490. "Remove fontification and activation overlays from links."
  5491. (font-lock-default-unfontify-region beg end)
  5492. (let* ((buffer-undo-list t)
  5493. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5494. (inhibit-modification-hooks t)
  5495. deactivate-mark buffer-file-name buffer-file-truename)
  5496. (org-decompose-region beg end)
  5497. (remove-text-properties beg end
  5498. '(mouse-face t keymap t org-linked-text t
  5499. invisible t intangible t
  5500. org-no-flyspell t org-emphasis t))
  5501. (org-remove-font-lock-display-properties beg end)))
  5502. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5503. ((raise 0.3) (height 0.7))
  5504. ((raise -0.5))
  5505. ((raise 0.5)))
  5506. "Display properties for showing superscripts and subscripts.")
  5507. (defun org-remove-font-lock-display-properties (beg end)
  5508. "Remove specific display properties that have been added by font lock.
  5509. The will remove the raise properties that are used to show superscripts
  5510. and subscripts."
  5511. (let (next prop)
  5512. (while (< beg end)
  5513. (setq next (next-single-property-change beg 'display nil end)
  5514. prop (get-text-property beg 'display))
  5515. (if (member prop org-script-display)
  5516. (put-text-property beg next 'display nil))
  5517. (setq beg next))))
  5518. (defun org-raise-scripts (limit)
  5519. "Add raise properties to sub/superscripts."
  5520. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5521. (if (re-search-forward
  5522. (if (eq org-use-sub-superscripts t)
  5523. org-match-substring-regexp
  5524. org-match-substring-with-braces-regexp)
  5525. limit t)
  5526. (let* ((pos (point)) table-p comment-p
  5527. (mpos (match-beginning 3))
  5528. (emph-p (get-text-property mpos 'org-emphasis))
  5529. (link-p (get-text-property mpos 'mouse-face))
  5530. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5531. (goto-char (point-at-bol))
  5532. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5533. comment-p (org-looking-at-p "[ \t]*#"))
  5534. (goto-char pos)
  5535. ;; FIXME: Should we go back one character here, for a_b^c
  5536. ;; (goto-char (1- pos)) ;????????????????????
  5537. (if (or comment-p emph-p link-p keyw-p)
  5538. t
  5539. (put-text-property (match-beginning 3) (match-end 0)
  5540. 'display
  5541. (if (equal (char-after (match-beginning 2)) ?^)
  5542. (nth (if table-p 3 1) org-script-display)
  5543. (nth (if table-p 2 0) org-script-display)))
  5544. (add-text-properties (match-beginning 2) (match-end 2)
  5545. (list 'invisible t
  5546. 'org-dwidth t 'org-dwidth-n 1))
  5547. (if (and (eq (char-after (match-beginning 3)) ?{)
  5548. (eq (char-before (match-end 3)) ?}))
  5549. (progn
  5550. (add-text-properties
  5551. (match-beginning 3) (1+ (match-beginning 3))
  5552. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5553. (add-text-properties
  5554. (1- (match-end 3)) (match-end 3)
  5555. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5556. t)))))
  5557. ;;;; Visibility cycling, including org-goto and indirect buffer
  5558. ;;; Cycling
  5559. (defvar org-cycle-global-status nil)
  5560. (make-variable-buffer-local 'org-cycle-global-status)
  5561. (put 'org-cycle-global-status 'org-state t)
  5562. (defvar org-cycle-subtree-status nil)
  5563. (make-variable-buffer-local 'org-cycle-subtree-status)
  5564. (put 'org-cycle-subtree-status 'org-state t)
  5565. (defvar org-inlinetask-min-level)
  5566. ;;;###autoload
  5567. (defun org-cycle (&optional arg)
  5568. "TAB-action and visibility cycling for Org-mode.
  5569. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5570. is outline visibility cycling, but it also invokes other actions
  5571. in special contexts.
  5572. - When this function is called with a prefix argument, rotate the entire
  5573. buffer through 3 states (global cycling)
  5574. 1. OVERVIEW: Show only top-level headlines.
  5575. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5576. 3. SHOW ALL: Show everything.
  5577. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5578. determined by the variable `org-startup-folded', and by any VISIBILITY
  5579. properties in the buffer.
  5580. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5581. including any drawers.
  5582. - When inside a table, re-align the table and move to the next field.
  5583. - When point is at the beginning of a headline, rotate the subtree started
  5584. by this line through 3 different states (local cycling)
  5585. 1. FOLDED: Only the main headline is shown.
  5586. 2. CHILDREN: The main headline and the direct children are shown.
  5587. From this state, you can move to one of the children
  5588. and zoom in further.
  5589. 3. SUBTREE: Show the entire subtree, including body text.
  5590. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5591. - When point is at the beginning of an empty headline and the variable
  5592. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5593. of the headline by demoting and promoting it to likely levels. This
  5594. speeds up creation document structure by pressing TAB once or several
  5595. times right after creating a new headline.
  5596. - When there is a numeric prefix, go up to a heading with level ARG, do
  5597. a `show-subtree' and return to the previous cursor position. If ARG
  5598. is negative, go up that many levels.
  5599. - When point is not at the beginning of a headline, execute the global
  5600. binding for TAB, which is re-indenting the line. See the option
  5601. `org-cycle-emulate-tab' for details.
  5602. - Special case: if point is at the beginning of the buffer and there is
  5603. no headline in line 1, this function will act as if called with prefix arg
  5604. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5605. But only if also the variable `org-cycle-global-at-bob' is t."
  5606. (interactive "P")
  5607. (org-load-modules-maybe)
  5608. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5609. (and org-cycle-level-after-item/entry-creation
  5610. (or (org-cycle-level)
  5611. (org-cycle-item-indentation))))
  5612. (let* (message-log-max ; Don't populate the *Messages* buffer
  5613. (limit-level
  5614. (or org-cycle-max-level
  5615. (and (boundp 'org-inlinetask-min-level)
  5616. org-inlinetask-min-level
  5617. (1- org-inlinetask-min-level))))
  5618. (nstars (and limit-level
  5619. (if org-odd-levels-only
  5620. (and limit-level (1- (* limit-level 2)))
  5621. limit-level)))
  5622. (org-outline-regexp
  5623. (if (not (derived-mode-p 'org-mode))
  5624. outline-regexp
  5625. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5626. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5627. (not (looking-at org-outline-regexp))))
  5628. (org-cycle-hook
  5629. (if bob-special
  5630. (delq 'org-optimize-window-after-visibility-change
  5631. (copy-sequence org-cycle-hook))
  5632. org-cycle-hook))
  5633. (pos (point)))
  5634. (if (or bob-special (equal arg '(4)))
  5635. ;; special case: use global cycling
  5636. (setq arg t))
  5637. (cond
  5638. ((equal arg '(16))
  5639. (setq last-command 'dummy)
  5640. (org-set-startup-visibility)
  5641. (message "Startup visibility, plus VISIBILITY properties"))
  5642. ((equal arg '(64))
  5643. (show-all)
  5644. (message "Entire buffer visible, including drawers"))
  5645. ;; Table: enter it or move to the next field.
  5646. ((org-at-table-p 'any)
  5647. (if (org-at-table.el-p)
  5648. (message "Use C-c ' to edit table.el tables")
  5649. (if arg (org-table-edit-field t)
  5650. (org-table-justify-field-maybe)
  5651. (call-interactively 'org-table-next-field))))
  5652. ((run-hook-with-args-until-success
  5653. 'org-tab-after-check-for-table-hook))
  5654. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5655. ((eq arg t) (org-cycle-internal-global))
  5656. ;; Drawers: delegate to `org-flag-drawer'.
  5657. ((and org-drawers org-drawer-regexp
  5658. (save-excursion
  5659. (beginning-of-line 1)
  5660. (looking-at org-drawer-regexp)))
  5661. (org-flag-drawer ; toggle block visibility
  5662. (not (get-char-property (match-end 0) 'invisible))))
  5663. ;; Show-subtree, ARG levels up from here.
  5664. ((integerp arg)
  5665. (save-excursion
  5666. (org-back-to-heading)
  5667. (outline-up-heading (if (< arg 0) (- arg)
  5668. (- (funcall outline-level) arg)))
  5669. (org-show-subtree)))
  5670. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5671. ((and (featurep 'org-inlinetask)
  5672. (org-inlinetask-at-task-p)
  5673. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5674. (org-inlinetask-toggle-visibility))
  5675. ((org-try-cdlatex-tab))
  5676. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5677. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5678. (save-excursion (beginning-of-line 1)
  5679. (looking-at org-outline-regexp)))
  5680. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5681. (org-cycle-internal-local))
  5682. ;; From there: TAB emulation and template completion.
  5683. (buffer-read-only (org-back-to-heading))
  5684. ((run-hook-with-args-until-success
  5685. 'org-tab-after-check-for-cycling-hook))
  5686. ((org-try-structure-completion))
  5687. ((run-hook-with-args-until-success
  5688. 'org-tab-before-tab-emulation-hook))
  5689. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5690. (or (not (bolp))
  5691. (not (looking-at org-outline-regexp))))
  5692. (call-interactively (global-key-binding "\t")))
  5693. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5694. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5695. (or (and (eq org-cycle-emulate-tab 'white)
  5696. (= (match-end 0) (point-at-eol)))
  5697. (and (eq org-cycle-emulate-tab 'whitestart)
  5698. (>= (match-end 0) pos))))
  5699. t
  5700. (eq org-cycle-emulate-tab t))
  5701. (call-interactively (global-key-binding "\t")))
  5702. (t (save-excursion
  5703. (org-back-to-heading)
  5704. (org-cycle)))))))
  5705. (defun org-cycle-internal-global ()
  5706. "Do the global cycling action."
  5707. ;; Hack to avoid display of messages for .org attachments in Gnus
  5708. (let (message-log-max ; Don't populate the *Messages* buffer
  5709. (ga (string-match "\\*fontification" (buffer-name))))
  5710. (cond
  5711. ((and (eq last-command this-command)
  5712. (eq org-cycle-global-status 'overview))
  5713. ;; We just created the overview - now do table of contents
  5714. ;; This can be slow in very large buffers, so indicate action
  5715. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5716. (unless ga (message "CONTENTS..."))
  5717. (org-content)
  5718. (unless ga (message "CONTENTS...done"))
  5719. (setq org-cycle-global-status 'contents)
  5720. (run-hook-with-args 'org-cycle-hook 'contents))
  5721. ((and (eq last-command this-command)
  5722. (eq org-cycle-global-status 'contents))
  5723. ;; We just showed the table of contents - now show everything
  5724. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5725. (show-all)
  5726. (unless ga (message "SHOW ALL"))
  5727. (setq org-cycle-global-status 'all)
  5728. (run-hook-with-args 'org-cycle-hook 'all))
  5729. (t
  5730. ;; Default action: go to overview
  5731. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5732. (org-overview)
  5733. (unless ga (message "OVERVIEW"))
  5734. (setq org-cycle-global-status 'overview)
  5735. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5736. (defun org-cycle-internal-local ()
  5737. "Do the local cycling action."
  5738. (let (message-log-max ; Don't populate the *Messages* buffer
  5739. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5740. ;; First, determine end of headline (EOH), end of subtree or item
  5741. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5742. (save-excursion
  5743. (if (org-at-item-p)
  5744. (progn
  5745. (beginning-of-line)
  5746. (setq struct (org-list-struct))
  5747. (setq eoh (point-at-eol))
  5748. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5749. (setq has-children (org-list-has-child-p (point) struct)))
  5750. (org-back-to-heading)
  5751. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5752. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5753. (setq has-children
  5754. (or (save-excursion
  5755. (let ((level (funcall outline-level)))
  5756. (outline-next-heading)
  5757. (and (org-at-heading-p t)
  5758. (> (funcall outline-level) level))))
  5759. (save-excursion
  5760. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5761. ;; Determine end invisible part of buffer (EOL)
  5762. (beginning-of-line 2)
  5763. ;; XEmacs doesn't have `next-single-char-property-change'
  5764. (if (featurep 'xemacs)
  5765. (while (and (not (eobp)) ;; this is like `next-line'
  5766. (get-char-property (1- (point)) 'invisible))
  5767. (beginning-of-line 2))
  5768. (while (and (not (eobp)) ;; this is like `next-line'
  5769. (get-char-property (1- (point)) 'invisible))
  5770. (goto-char (next-single-char-property-change (point) 'invisible))
  5771. (and (eolp) (beginning-of-line 2))))
  5772. (setq eol (point)))
  5773. ;; Find out what to do next and set `this-command'
  5774. (cond
  5775. ((= eos eoh)
  5776. ;; Nothing is hidden behind this heading
  5777. (unless (org-before-first-heading-p)
  5778. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5779. (message "EMPTY ENTRY")
  5780. (setq org-cycle-subtree-status nil)
  5781. (save-excursion
  5782. (goto-char eos)
  5783. (outline-next-heading)
  5784. (if (outline-invisible-p) (org-flag-heading nil))))
  5785. ((and (or (>= eol eos)
  5786. (not (string-match "\\S-" (buffer-substring eol eos))))
  5787. (or has-children
  5788. (not (setq children-skipped
  5789. org-cycle-skip-children-state-if-no-children))))
  5790. ;; Entire subtree is hidden in one line: children view
  5791. (unless (org-before-first-heading-p)
  5792. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5793. (if (org-at-item-p)
  5794. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5795. (org-show-entry)
  5796. (org-with-limited-levels (show-children))
  5797. ;; FIXME: This slows down the func way too much.
  5798. ;; How keep drawers hidden in subtree anyway?
  5799. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5800. ;; (org-cycle-hide-drawers 'subtree))
  5801. ;; Fold every list in subtree to top-level items.
  5802. (when (eq org-cycle-include-plain-lists 'integrate)
  5803. (save-excursion
  5804. (org-back-to-heading)
  5805. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5806. (beginning-of-line 1)
  5807. (let* ((struct (org-list-struct))
  5808. (prevs (org-list-prevs-alist struct))
  5809. (end (org-list-get-bottom-point struct)))
  5810. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5811. (org-list-get-all-items (point) struct prevs))
  5812. (goto-char end))))))
  5813. (message "CHILDREN")
  5814. (save-excursion
  5815. (goto-char eos)
  5816. (outline-next-heading)
  5817. (if (outline-invisible-p) (org-flag-heading nil)))
  5818. (setq org-cycle-subtree-status 'children)
  5819. (unless (org-before-first-heading-p)
  5820. (run-hook-with-args 'org-cycle-hook 'children)))
  5821. ((or children-skipped
  5822. (and (eq last-command this-command)
  5823. (eq org-cycle-subtree-status 'children)))
  5824. ;; We just showed the children, or no children are there,
  5825. ;; now show everything.
  5826. (unless (org-before-first-heading-p)
  5827. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5828. (outline-flag-region eoh eos nil)
  5829. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5830. (setq org-cycle-subtree-status 'subtree)
  5831. (unless (org-before-first-heading-p)
  5832. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5833. (t
  5834. ;; Default action: hide the subtree.
  5835. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5836. (outline-flag-region eoh eos t)
  5837. (message "FOLDED")
  5838. (setq org-cycle-subtree-status 'folded)
  5839. (unless (org-before-first-heading-p)
  5840. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5841. ;;;###autoload
  5842. (defun org-global-cycle (&optional arg)
  5843. "Cycle the global visibility. For details see `org-cycle'.
  5844. With \\[universal-argument] prefix arg, switch to startup visibility.
  5845. With a numeric prefix, show all headlines up to that level."
  5846. (interactive "P")
  5847. (let ((org-cycle-include-plain-lists
  5848. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5849. (cond
  5850. ((integerp arg)
  5851. (show-all)
  5852. (hide-sublevels arg)
  5853. (setq org-cycle-global-status 'contents))
  5854. ((equal arg '(4))
  5855. (org-set-startup-visibility)
  5856. (message "Startup visibility, plus VISIBILITY properties."))
  5857. (t
  5858. (org-cycle '(4))))))
  5859. (defun org-set-startup-visibility ()
  5860. "Set the visibility required by startup options and properties."
  5861. (cond
  5862. ((eq org-startup-folded t)
  5863. (org-cycle '(4)))
  5864. ((eq org-startup-folded 'content)
  5865. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5866. (org-cycle '(4)) (org-cycle '(4)))))
  5867. (unless (eq org-startup-folded 'showeverything)
  5868. (if org-hide-block-startup (org-hide-block-all))
  5869. (org-set-visibility-according-to-property 'no-cleanup)
  5870. (org-cycle-hide-archived-subtrees 'all)
  5871. (org-cycle-hide-drawers 'all)
  5872. (org-cycle-show-empty-lines t)))
  5873. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5874. "Switch subtree visibilities according to :VISIBILITY: property."
  5875. (interactive)
  5876. (let (org-show-entry-below state)
  5877. (save-excursion
  5878. (goto-char (point-min))
  5879. (while (re-search-forward
  5880. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5881. nil t)
  5882. (setq state (match-string 1))
  5883. (save-excursion
  5884. (org-back-to-heading t)
  5885. (hide-subtree)
  5886. (org-reveal)
  5887. (cond
  5888. ((equal state '("fold" "folded"))
  5889. (hide-subtree))
  5890. ((equal state "children")
  5891. (org-show-hidden-entry)
  5892. (show-children))
  5893. ((equal state "content")
  5894. (save-excursion
  5895. (save-restriction
  5896. (org-narrow-to-subtree)
  5897. (org-content))))
  5898. ((member state '("all" "showall"))
  5899. (show-subtree)))))
  5900. (unless no-cleanup
  5901. (org-cycle-hide-archived-subtrees 'all)
  5902. (org-cycle-hide-drawers 'all)
  5903. (org-cycle-show-empty-lines 'all)))))
  5904. ;; This function uses outline-regexp instead of the more fundamental
  5905. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5906. ;; buffers, where outline-regexp is needed.
  5907. (defun org-overview ()
  5908. "Switch to overview mode, showing only top-level headlines.
  5909. Really, this shows all headlines with level equal or greater than the level
  5910. of the first headline in the buffer. This is important, because if the
  5911. first headline is not level one, then (hide-sublevels 1) gives confusing
  5912. results."
  5913. (interactive)
  5914. (let ((level (save-excursion
  5915. (goto-char (point-min))
  5916. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5917. (progn
  5918. (goto-char (match-beginning 0))
  5919. (funcall outline-level))))))
  5920. (and level (hide-sublevels level))))
  5921. (defun org-content (&optional arg)
  5922. "Show all headlines in the buffer, like a table of contents.
  5923. With numerical argument N, show content up to level N."
  5924. (interactive "P")
  5925. (save-excursion
  5926. ;; Visit all headings and show their offspring
  5927. (and (integerp arg) (org-overview))
  5928. (goto-char (point-max))
  5929. (catch 'exit
  5930. (while (and (progn (condition-case nil
  5931. (outline-previous-visible-heading 1)
  5932. (error (goto-char (point-min))))
  5933. t)
  5934. (looking-at org-outline-regexp))
  5935. (if (integerp arg)
  5936. (show-children (1- arg))
  5937. (show-branches))
  5938. (if (bobp) (throw 'exit nil))))))
  5939. (defun org-optimize-window-after-visibility-change (state)
  5940. "Adjust the window after a change in outline visibility.
  5941. This function is the default value of the hook `org-cycle-hook'."
  5942. (when (get-buffer-window (current-buffer))
  5943. (cond
  5944. ((eq state 'content) nil)
  5945. ((eq state 'all) nil)
  5946. ((eq state 'folded) nil)
  5947. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5948. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5949. (defun org-remove-empty-overlays-at (pos)
  5950. "Remove outline overlays that do not contain non-white stuff."
  5951. (mapc
  5952. (lambda (o)
  5953. (and (eq 'outline (overlay-get o 'invisible))
  5954. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5955. (overlay-end o))))
  5956. (delete-overlay o)))
  5957. (overlays-at pos)))
  5958. (defun org-clean-visibility-after-subtree-move ()
  5959. "Fix visibility issues after moving a subtree."
  5960. ;; First, find a reasonable region to look at:
  5961. ;; Start two siblings above, end three below
  5962. (let* ((beg (save-excursion
  5963. (and (org-get-last-sibling)
  5964. (org-get-last-sibling))
  5965. (point)))
  5966. (end (save-excursion
  5967. (and (org-get-next-sibling)
  5968. (org-get-next-sibling)
  5969. (org-get-next-sibling))
  5970. (if (org-at-heading-p)
  5971. (point-at-eol)
  5972. (point))))
  5973. (level (looking-at "\\*+"))
  5974. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5975. (save-excursion
  5976. (save-restriction
  5977. (narrow-to-region beg end)
  5978. (when re
  5979. ;; Properly fold already folded siblings
  5980. (goto-char (point-min))
  5981. (while (re-search-forward re nil t)
  5982. (if (and (not (outline-invisible-p))
  5983. (save-excursion
  5984. (goto-char (point-at-eol)) (outline-invisible-p)))
  5985. (hide-entry))))
  5986. (org-cycle-show-empty-lines 'overview)
  5987. (org-cycle-hide-drawers 'overview)))))
  5988. (defun org-cycle-show-empty-lines (state)
  5989. "Show empty lines above all visible headlines.
  5990. The region to be covered depends on STATE when called through
  5991. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5992. entire buffer covered. Note that an empty line is only shown if there
  5993. are at least `org-cycle-separator-lines' empty lines before the headline."
  5994. (when (not (= org-cycle-separator-lines 0))
  5995. (save-excursion
  5996. (let* ((n (abs org-cycle-separator-lines))
  5997. (re (cond
  5998. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5999. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6000. (t (let ((ns (number-to-string (- n 2))))
  6001. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6002. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6003. beg end b e)
  6004. (cond
  6005. ((memq state '(overview contents t))
  6006. (setq beg (point-min) end (point-max)))
  6007. ((memq state '(children folded))
  6008. (setq beg (point) end (progn (org-end-of-subtree t t)
  6009. (beginning-of-line 2)
  6010. (point)))))
  6011. (when beg
  6012. (goto-char beg)
  6013. (while (re-search-forward re end t)
  6014. (unless (get-char-property (match-end 1) 'invisible)
  6015. (setq e (match-end 1))
  6016. (if (< org-cycle-separator-lines 0)
  6017. (setq b (save-excursion
  6018. (goto-char (match-beginning 0))
  6019. (org-back-over-empty-lines)
  6020. (if (save-excursion
  6021. (goto-char (max (point-min) (1- (point))))
  6022. (org-at-heading-p))
  6023. (1- (point))
  6024. (point))))
  6025. (setq b (match-beginning 1)))
  6026. (outline-flag-region b e nil)))))))
  6027. ;; Never hide empty lines at the end of the file.
  6028. (save-excursion
  6029. (goto-char (point-max))
  6030. (outline-previous-heading)
  6031. (outline-end-of-heading)
  6032. (if (and (looking-at "[ \t\n]+")
  6033. (= (match-end 0) (point-max)))
  6034. (outline-flag-region (point) (match-end 0) nil))))
  6035. (defun org-show-empty-lines-in-parent ()
  6036. "Move to the parent and re-show empty lines before visible headlines."
  6037. (save-excursion
  6038. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6039. (org-cycle-show-empty-lines context))))
  6040. (defun org-files-list ()
  6041. "Return `org-agenda-files' list, plus all open org-mode files.
  6042. This is useful for operations that need to scan all of a user's
  6043. open and agenda-wise Org files."
  6044. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6045. (dolist (buf (buffer-list))
  6046. (with-current-buffer buf
  6047. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6048. (let ((file (expand-file-name (buffer-file-name))))
  6049. (unless (member file files)
  6050. (push file files))))))
  6051. files))
  6052. (defsubst org-entry-beginning-position ()
  6053. "Return the beginning position of the current entry."
  6054. (save-excursion (outline-back-to-heading t) (point)))
  6055. (defsubst org-entry-end-position ()
  6056. "Return the end position of the current entry."
  6057. (save-excursion (outline-next-heading) (point)))
  6058. (defun org-cycle-hide-drawers (state)
  6059. "Re-hide all drawers after a visibility state change."
  6060. (when (and (derived-mode-p 'org-mode)
  6061. (not (memq state '(overview folded contents))))
  6062. (save-excursion
  6063. (let* ((globalp (memq state '(contents all)))
  6064. (beg (if globalp (point-min) (point)))
  6065. (end (if globalp (point-max)
  6066. (if (eq state 'children)
  6067. (save-excursion (outline-next-heading) (point))
  6068. (org-end-of-subtree t)))))
  6069. (goto-char beg)
  6070. (while (re-search-forward org-drawer-regexp end t)
  6071. (org-flag-drawer t))))))
  6072. (defun org-cycle-hide-inline-tasks (state)
  6073. "Re-hide inline task when switching to 'contents visibility state."
  6074. (when (and (eq state 'contents)
  6075. (boundp 'org-inlinetask-min-level)
  6076. org-inlinetask-min-level)
  6077. (hide-sublevels (1- org-inlinetask-min-level))))
  6078. (defun org-flag-drawer (flag)
  6079. "When FLAG is non-nil, hide the drawer we are within.
  6080. Otherwise make it visible."
  6081. (save-excursion
  6082. (beginning-of-line 1)
  6083. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6084. (let ((b (match-end 0)))
  6085. (if (re-search-forward
  6086. "^[ \t]*:END:"
  6087. (save-excursion (outline-next-heading) (point)) t)
  6088. (outline-flag-region b (point-at-eol) flag)
  6089. (error ":END: line missing at position %s" b))))))
  6090. (defun org-subtree-end-visible-p ()
  6091. "Is the end of the current subtree visible?"
  6092. (pos-visible-in-window-p
  6093. (save-excursion (org-end-of-subtree t) (point))))
  6094. (defun org-first-headline-recenter (&optional N)
  6095. "Move cursor to the first headline and recenter the headline.
  6096. Optional argument N means put the headline into the Nth line of the window."
  6097. (goto-char (point-min))
  6098. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6099. (beginning-of-line)
  6100. (recenter (prefix-numeric-value N))))
  6101. ;;; Saving and restoring visibility
  6102. (defun org-outline-overlay-data (&optional use-markers)
  6103. "Return a list of the locations of all outline overlays.
  6104. These are overlays with the `invisible' property value `outline'.
  6105. The return value is a list of cons cells, with start and stop
  6106. positions for each overlay.
  6107. If USE-MARKERS is set, return the positions as markers."
  6108. (let (beg end)
  6109. (save-excursion
  6110. (save-restriction
  6111. (widen)
  6112. (delq nil
  6113. (mapcar (lambda (o)
  6114. (when (eq (overlay-get o 'invisible) 'outline)
  6115. (setq beg (overlay-start o)
  6116. end (overlay-end o))
  6117. (and beg end (> end beg)
  6118. (if use-markers
  6119. (cons (move-marker (make-marker) beg)
  6120. (move-marker (make-marker) end))
  6121. (cons beg end)))))
  6122. (overlays-in (point-min) (point-max))))))))
  6123. (defun org-set-outline-overlay-data (data)
  6124. "Create visibility overlays for all positions in DATA.
  6125. DATA should have been made by `org-outline-overlay-data'."
  6126. (let (o)
  6127. (save-excursion
  6128. (save-restriction
  6129. (widen)
  6130. (show-all)
  6131. (mapc (lambda (c)
  6132. (outline-flag-region (car c) (cdr c) t))
  6133. data)))))
  6134. ;;; Folding of blocks
  6135. (defvar org-hide-block-overlays nil
  6136. "Overlays hiding blocks.")
  6137. (make-variable-buffer-local 'org-hide-block-overlays)
  6138. (defun org-block-map (function &optional start end)
  6139. "Call FUNCTION at the head of all source blocks in the current buffer.
  6140. Optional arguments START and END can be used to limit the range."
  6141. (let ((start (or start (point-min)))
  6142. (end (or end (point-max))))
  6143. (save-excursion
  6144. (goto-char start)
  6145. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6146. (save-excursion
  6147. (save-match-data
  6148. (goto-char (match-beginning 0))
  6149. (funcall function)))))))
  6150. (defun org-hide-block-toggle-all ()
  6151. "Toggle the visibility of all blocks in the current buffer."
  6152. (org-block-map #'org-hide-block-toggle))
  6153. (defun org-hide-block-all ()
  6154. "Fold all blocks in the current buffer."
  6155. (interactive)
  6156. (org-show-block-all)
  6157. (org-block-map #'org-hide-block-toggle-maybe))
  6158. (defun org-show-block-all ()
  6159. "Unfold all blocks in the current buffer."
  6160. (interactive)
  6161. (mapc 'delete-overlay org-hide-block-overlays)
  6162. (setq org-hide-block-overlays nil))
  6163. (defun org-hide-block-toggle-maybe ()
  6164. "Toggle visibility of block at point."
  6165. (interactive)
  6166. (let ((case-fold-search t))
  6167. (if (save-excursion
  6168. (beginning-of-line 1)
  6169. (looking-at org-block-regexp))
  6170. (progn (org-hide-block-toggle)
  6171. t) ;; to signal that we took action
  6172. nil))) ;; to signal that we did not
  6173. (defun org-hide-block-toggle (&optional force)
  6174. "Toggle the visibility of the current block."
  6175. (interactive)
  6176. (save-excursion
  6177. (beginning-of-line)
  6178. (if (re-search-forward org-block-regexp nil t)
  6179. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6180. (end (match-end 0)) ;; end of entire body
  6181. ov)
  6182. (if (memq t (mapcar (lambda (overlay)
  6183. (eq (overlay-get overlay 'invisible)
  6184. 'org-hide-block))
  6185. (overlays-at start)))
  6186. (if (or (not force) (eq force 'off))
  6187. (mapc (lambda (ov)
  6188. (when (member ov org-hide-block-overlays)
  6189. (setq org-hide-block-overlays
  6190. (delq ov org-hide-block-overlays)))
  6191. (when (eq (overlay-get ov 'invisible)
  6192. 'org-hide-block)
  6193. (delete-overlay ov)))
  6194. (overlays-at start)))
  6195. (setq ov (make-overlay start end))
  6196. (overlay-put ov 'invisible 'org-hide-block)
  6197. ;; make the block accessible to isearch
  6198. (overlay-put
  6199. ov 'isearch-open-invisible
  6200. (lambda (ov)
  6201. (when (member ov org-hide-block-overlays)
  6202. (setq org-hide-block-overlays
  6203. (delq ov org-hide-block-overlays)))
  6204. (when (eq (overlay-get ov 'invisible)
  6205. 'org-hide-block)
  6206. (delete-overlay ov))))
  6207. (push ov org-hide-block-overlays)))
  6208. (error "Not looking at a source block"))))
  6209. ;; org-tab-after-check-for-cycling-hook
  6210. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6211. ;; Remove overlays when changing major mode
  6212. (add-hook 'org-mode-hook
  6213. (lambda () (org-add-hook 'change-major-mode-hook
  6214. 'org-show-block-all 'append 'local)))
  6215. ;;; Org-goto
  6216. (defvar org-goto-window-configuration nil)
  6217. (defvar org-goto-marker nil)
  6218. (defvar org-goto-map)
  6219. (defun org-goto-map ()
  6220. "Set the keymap `org-goto'."
  6221. (setq org-goto-map
  6222. (let ((map (make-sparse-keymap)))
  6223. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6224. mouse-drag-region universal-argument org-occur))
  6225. cmd)
  6226. (while (setq cmd (pop cmds))
  6227. (substitute-key-definition cmd cmd map global-map)))
  6228. (suppress-keymap map)
  6229. (org-defkey map "\C-m" 'org-goto-ret)
  6230. (org-defkey map [(return)] 'org-goto-ret)
  6231. (org-defkey map [(left)] 'org-goto-left)
  6232. (org-defkey map [(right)] 'org-goto-right)
  6233. (org-defkey map [(control ?g)] 'org-goto-quit)
  6234. (org-defkey map "\C-i" 'org-cycle)
  6235. (org-defkey map [(tab)] 'org-cycle)
  6236. (org-defkey map [(down)] 'outline-next-visible-heading)
  6237. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6238. (if org-goto-auto-isearch
  6239. (if (fboundp 'define-key-after)
  6240. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6241. nil)
  6242. (org-defkey map "q" 'org-goto-quit)
  6243. (org-defkey map "n" 'outline-next-visible-heading)
  6244. (org-defkey map "p" 'outline-previous-visible-heading)
  6245. (org-defkey map "f" 'outline-forward-same-level)
  6246. (org-defkey map "b" 'outline-backward-same-level)
  6247. (org-defkey map "u" 'outline-up-heading))
  6248. (org-defkey map "/" 'org-occur)
  6249. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6250. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6251. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6252. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6253. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6254. map)))
  6255. (defconst org-goto-help
  6256. "Browse buffer copy, to find location or copy text.%s
  6257. RET=jump to location C-g=quit and return to previous location
  6258. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6259. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6260. ;; FIXME: Docstring does not mention both interfaces
  6261. (defun org-goto (&optional alternative-interface)
  6262. "Look up a different location in the current file, keeping current visibility.
  6263. When you want look-up or go to a different location in a
  6264. document, the fastest way is often to fold the entire buffer and
  6265. then dive into the tree. This method has the disadvantage, that
  6266. the previous location will be folded, which may not be what you
  6267. want.
  6268. This command works around this by showing a copy of the current
  6269. buffer in an indirect buffer, in overview mode. You can dive
  6270. into the tree in that copy, use org-occur and incremental search
  6271. to find a location. When pressing RET or `Q', the command
  6272. returns to the original buffer in which the visibility is still
  6273. unchanged. After RET it will also jump to the location selected
  6274. in the indirect buffer and expose the headline hierarchy above.
  6275. With a prefix argument, use the alternative interface: e.g. if
  6276. `org-goto-interface' is 'outline use 'outline-path-completion."
  6277. (interactive "P")
  6278. (org-goto-map)
  6279. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6280. (org-refile-use-outline-path t)
  6281. (org-refile-target-verify-function nil)
  6282. (interface
  6283. (if (not alternative-interface)
  6284. org-goto-interface
  6285. (if (eq org-goto-interface 'outline)
  6286. 'outline-path-completion
  6287. 'outline)))
  6288. (org-goto-start-pos (point))
  6289. (selected-point
  6290. (if (eq interface 'outline)
  6291. (car (org-get-location (current-buffer) org-goto-help))
  6292. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6293. (org-refile-check-position pa)
  6294. (nth 3 pa)))))
  6295. (if selected-point
  6296. (progn
  6297. (org-mark-ring-push org-goto-start-pos)
  6298. (goto-char selected-point)
  6299. (if (or (outline-invisible-p) (org-invisible-p2))
  6300. (org-show-context 'org-goto)))
  6301. (message "Quit"))))
  6302. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6303. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6304. (defvar org-goto-local-auto-isearch-map) ; defined below
  6305. (defun org-get-location (buf help)
  6306. "Let the user select a location in the Org-mode buffer BUF.
  6307. This function uses a recursive edit. It returns the selected position
  6308. or nil."
  6309. (org-no-popups
  6310. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6311. (isearch-hide-immediately nil)
  6312. (isearch-search-fun-function
  6313. (lambda () 'org-goto-local-search-headings))
  6314. (org-goto-selected-point org-goto-exit-command))
  6315. (save-excursion
  6316. (save-window-excursion
  6317. (delete-other-windows)
  6318. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6319. (org-pop-to-buffer-same-window
  6320. (condition-case nil
  6321. (make-indirect-buffer (current-buffer) "*org-goto*")
  6322. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6323. (with-output-to-temp-buffer "*Org Help*"
  6324. (princ (format help (if org-goto-auto-isearch
  6325. " Just type for auto-isearch."
  6326. " n/p/f/b/u to navigate, q to quit."))))
  6327. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6328. (setq buffer-read-only nil)
  6329. (let ((org-startup-truncated t)
  6330. (org-startup-folded nil)
  6331. (org-startup-align-all-tables nil))
  6332. (org-mode)
  6333. (org-overview))
  6334. (setq buffer-read-only t)
  6335. (if (and (boundp 'org-goto-start-pos)
  6336. (integer-or-marker-p org-goto-start-pos))
  6337. (let ((org-show-hierarchy-above t)
  6338. (org-show-siblings t)
  6339. (org-show-following-heading t))
  6340. (goto-char org-goto-start-pos)
  6341. (and (outline-invisible-p) (org-show-context)))
  6342. (goto-char (point-min)))
  6343. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6344. (message "Select location and press RET")
  6345. (use-local-map org-goto-map)
  6346. (recursive-edit)))
  6347. (kill-buffer "*org-goto*")
  6348. (cons org-goto-selected-point org-goto-exit-command))))
  6349. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6350. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6351. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6352. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6353. (defun org-goto-local-search-headings (string bound noerror)
  6354. "Search and make sure that any matches are in headlines."
  6355. (catch 'return
  6356. (while (if isearch-forward
  6357. (search-forward string bound noerror)
  6358. (search-backward string bound noerror))
  6359. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6360. (and (member :headline context)
  6361. (not (member :tags context))))
  6362. (throw 'return (point))))))
  6363. (defun org-goto-local-auto-isearch ()
  6364. "Start isearch."
  6365. (interactive)
  6366. (goto-char (point-min))
  6367. (let ((keys (this-command-keys)))
  6368. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6369. (isearch-mode t)
  6370. (isearch-process-search-char (string-to-char keys)))))
  6371. (defun org-goto-ret (&optional arg)
  6372. "Finish `org-goto' by going to the new location."
  6373. (interactive "P")
  6374. (setq org-goto-selected-point (point)
  6375. org-goto-exit-command 'return)
  6376. (throw 'exit nil))
  6377. (defun org-goto-left ()
  6378. "Finish `org-goto' by going to the new location."
  6379. (interactive)
  6380. (if (org-at-heading-p)
  6381. (progn
  6382. (beginning-of-line 1)
  6383. (setq org-goto-selected-point (point)
  6384. org-goto-exit-command 'left)
  6385. (throw 'exit nil))
  6386. (error "Not on a heading")))
  6387. (defun org-goto-right ()
  6388. "Finish `org-goto' by going to the new location."
  6389. (interactive)
  6390. (if (org-at-heading-p)
  6391. (progn
  6392. (setq org-goto-selected-point (point)
  6393. org-goto-exit-command 'right)
  6394. (throw 'exit nil))
  6395. (error "Not on a heading")))
  6396. (defun org-goto-quit ()
  6397. "Finish `org-goto' without cursor motion."
  6398. (interactive)
  6399. (setq org-goto-selected-point nil)
  6400. (setq org-goto-exit-command 'quit)
  6401. (throw 'exit nil))
  6402. ;;; Indirect buffer display of subtrees
  6403. (defvar org-indirect-dedicated-frame nil
  6404. "This is the frame being used for indirect tree display.")
  6405. (defvar org-last-indirect-buffer nil)
  6406. (defun org-tree-to-indirect-buffer (&optional arg)
  6407. "Create indirect buffer and narrow it to current subtree.
  6408. With a numerical prefix ARG, go up to this level and then take that tree.
  6409. If ARG is negative, go up that many levels.
  6410. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6411. indirect buffer previously made with this command, to avoid proliferation of
  6412. indirect buffers. However, when you call the command with a \
  6413. \\[universal-argument] prefix, or
  6414. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6415. is kept so that you can work with several indirect buffers at the same time.
  6416. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6417. \\[universal-argument] prefix also
  6418. requests that a new frame be made for the new buffer, so that the dedicated
  6419. frame is not changed."
  6420. (interactive "P")
  6421. (let ((cbuf (current-buffer))
  6422. (cwin (selected-window))
  6423. (pos (point))
  6424. beg end level heading ibuf)
  6425. (save-excursion
  6426. (org-back-to-heading t)
  6427. (when (numberp arg)
  6428. (setq level (org-outline-level))
  6429. (if (< arg 0) (setq arg (+ level arg)))
  6430. (while (> (setq level (org-outline-level)) arg)
  6431. (org-up-heading-safe)))
  6432. (setq beg (point)
  6433. heading (org-get-heading))
  6434. (org-end-of-subtree t t)
  6435. (if (org-at-heading-p) (backward-char 1))
  6436. (setq end (point)))
  6437. (if (and (buffer-live-p org-last-indirect-buffer)
  6438. (not (eq org-indirect-buffer-display 'new-frame))
  6439. (not arg))
  6440. (kill-buffer org-last-indirect-buffer))
  6441. (setq ibuf (org-get-indirect-buffer cbuf)
  6442. org-last-indirect-buffer ibuf)
  6443. (cond
  6444. ((or (eq org-indirect-buffer-display 'new-frame)
  6445. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6446. (select-frame (make-frame))
  6447. (delete-other-windows)
  6448. (org-pop-to-buffer-same-window ibuf)
  6449. (org-set-frame-title heading))
  6450. ((eq org-indirect-buffer-display 'dedicated-frame)
  6451. (raise-frame
  6452. (select-frame (or (and org-indirect-dedicated-frame
  6453. (frame-live-p org-indirect-dedicated-frame)
  6454. org-indirect-dedicated-frame)
  6455. (setq org-indirect-dedicated-frame (make-frame)))))
  6456. (delete-other-windows)
  6457. (org-pop-to-buffer-same-window ibuf)
  6458. (org-set-frame-title (concat "Indirect: " heading)))
  6459. ((eq org-indirect-buffer-display 'current-window)
  6460. (org-pop-to-buffer-same-window ibuf))
  6461. ((eq org-indirect-buffer-display 'other-window)
  6462. (pop-to-buffer ibuf))
  6463. (t (error "Invalid value")))
  6464. (if (featurep 'xemacs)
  6465. (save-excursion (org-mode) (turn-on-font-lock)))
  6466. (narrow-to-region beg end)
  6467. (show-all)
  6468. (goto-char pos)
  6469. (run-hook-with-args 'org-cycle-hook 'all)
  6470. (and (window-live-p cwin) (select-window cwin))))
  6471. (defun org-get-indirect-buffer (&optional buffer)
  6472. (setq buffer (or buffer (current-buffer)))
  6473. (let ((n 1) (base (buffer-name buffer)) bname)
  6474. (while (buffer-live-p
  6475. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6476. (setq n (1+ n)))
  6477. (condition-case nil
  6478. (make-indirect-buffer buffer bname 'clone)
  6479. (error (make-indirect-buffer buffer bname)))))
  6480. (defun org-set-frame-title (title)
  6481. "Set the title of the current frame to the string TITLE."
  6482. ;; FIXME: how to name a single frame in XEmacs???
  6483. (unless (featurep 'xemacs)
  6484. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6485. ;;;; Structure editing
  6486. ;;; Inserting headlines
  6487. (defun org-previous-line-empty-p ()
  6488. (save-excursion
  6489. (and (not (bobp))
  6490. (or (beginning-of-line 0) t)
  6491. (save-match-data
  6492. (looking-at "[ \t]*$")))))
  6493. (defun org-insert-heading (&optional force-heading invisible-ok)
  6494. "Insert a new heading or item with same depth at point.
  6495. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6496. If point is at the beginning of a headline, insert a sibling before the
  6497. current headline. If point is not at the beginning, split the line,
  6498. create the new headline with the text in the current line after point
  6499. \(but see also the variable `org-M-RET-may-split-line').
  6500. With a double prefix arg, force the heading to be inserted at the
  6501. end of the parent subtree.
  6502. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6503. This is important for non-interactive uses of the command."
  6504. (interactive "P")
  6505. (if (or (= (buffer-size) 0)
  6506. (and (not (save-excursion
  6507. (and (ignore-errors (org-back-to-heading invisible-ok))
  6508. (org-at-heading-p))))
  6509. (or force-heading (not (org-in-item-p)))))
  6510. (progn
  6511. (insert "\n* ")
  6512. (run-hooks 'org-insert-heading-hook))
  6513. (when (or force-heading (not (org-insert-item)))
  6514. (let* ((empty-line-p nil)
  6515. (level nil)
  6516. (on-heading (org-at-heading-p))
  6517. (head (save-excursion
  6518. (condition-case nil
  6519. (progn
  6520. (org-back-to-heading invisible-ok)
  6521. (when (and (not on-heading)
  6522. (featurep 'org-inlinetask)
  6523. (integerp org-inlinetask-min-level)
  6524. (>= (length (match-string 0))
  6525. org-inlinetask-min-level))
  6526. ;; Find a heading level before the inline task
  6527. (while (and (setq level (org-up-heading-safe))
  6528. (>= level org-inlinetask-min-level)))
  6529. (if (org-at-heading-p)
  6530. (org-back-to-heading invisible-ok)
  6531. (error "This should not happen")))
  6532. (setq empty-line-p (org-previous-line-empty-p))
  6533. (match-string 0))
  6534. (error "*"))))
  6535. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6536. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6537. pos hide-previous previous-pos)
  6538. (cond
  6539. ((and (org-at-heading-p) (bolp)
  6540. (or (bobp)
  6541. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6542. ;; insert before the current line
  6543. (open-line (if blank 2 1)))
  6544. ((and (bolp)
  6545. (not org-insert-heading-respect-content)
  6546. (or (bobp)
  6547. (save-excursion
  6548. (backward-char 1) (not (outline-invisible-p)))))
  6549. ;; insert right here
  6550. nil)
  6551. (t
  6552. ;; somewhere in the line
  6553. (save-excursion
  6554. (setq previous-pos (point-at-bol))
  6555. (end-of-line)
  6556. (setq hide-previous (outline-invisible-p)))
  6557. (and org-insert-heading-respect-content (org-show-subtree))
  6558. (let ((split
  6559. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6560. (save-excursion
  6561. (let ((p (point)))
  6562. (goto-char (point-at-bol))
  6563. (and (looking-at org-complex-heading-regexp)
  6564. (match-beginning 4)
  6565. (> p (match-beginning 4)))))))
  6566. tags pos)
  6567. (cond
  6568. (org-insert-heading-respect-content
  6569. (if (not (equal force-heading '(16)))
  6570. (org-end-of-subtree nil t)
  6571. (org-up-heading-safe)
  6572. (org-end-of-subtree nil t))
  6573. (when (featurep 'org-inlinetask)
  6574. (while (and (not (eobp))
  6575. (looking-at "\\(\\*+\\)[ \t]+")
  6576. (>= (length (match-string 1))
  6577. org-inlinetask-min-level))
  6578. (org-end-of-subtree nil t)))
  6579. (or (bolp) (newline))
  6580. (or (org-previous-line-empty-p)
  6581. (and blank (newline)))
  6582. (open-line 1))
  6583. ((org-at-heading-p)
  6584. (when hide-previous
  6585. (show-children)
  6586. (org-show-entry))
  6587. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6588. (setq tags (and (match-end 2) (match-string 2)))
  6589. (and (match-end 1)
  6590. (delete-region (match-beginning 1) (match-end 1)))
  6591. (setq pos (point-at-bol))
  6592. (or split (end-of-line 1))
  6593. (delete-horizontal-space)
  6594. (if (string-match "\\`\\*+\\'"
  6595. (buffer-substring (point-at-bol) (point)))
  6596. (insert " "))
  6597. (newline (if blank 2 1))
  6598. (when tags
  6599. (save-excursion
  6600. (goto-char pos)
  6601. (end-of-line 1)
  6602. (insert " " tags)
  6603. (org-set-tags nil 'align))))
  6604. (t
  6605. (or split (end-of-line 1))
  6606. (newline (if blank 2 1)))))))
  6607. (insert head) (just-one-space)
  6608. (setq pos (point))
  6609. (end-of-line 1)
  6610. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6611. (when (and org-insert-heading-respect-content hide-previous)
  6612. (save-excursion
  6613. (goto-char previous-pos)
  6614. (hide-subtree)))
  6615. (run-hooks 'org-insert-heading-hook)))))
  6616. (defun org-get-heading (&optional no-tags no-todo)
  6617. "Return the heading of the current entry, without the stars.
  6618. When NO-TAGS is non-nil, don't include tags.
  6619. When NO-TODO is non-nil, don't include TODO keywords."
  6620. (save-excursion
  6621. (org-back-to-heading t)
  6622. (cond
  6623. ((and no-tags no-todo)
  6624. (looking-at org-complex-heading-regexp)
  6625. (match-string 4))
  6626. (no-tags
  6627. (looking-at (concat org-outline-regexp
  6628. "\\(.*?\\)"
  6629. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6630. (match-string 1))
  6631. (no-todo
  6632. (looking-at org-todo-line-regexp)
  6633. (match-string 3))
  6634. (t (looking-at org-heading-regexp)
  6635. (match-string 2)))))
  6636. (defvar orgstruct-mode) ; defined below
  6637. (defun org-heading-components ()
  6638. "Return the components of the current heading.
  6639. This is a list with the following elements:
  6640. - the level as an integer
  6641. - the reduced level, different if `org-odd-levels-only' is set.
  6642. - the TODO keyword, or nil
  6643. - the priority character, like ?A, or nil if no priority is given
  6644. - the headline text itself, or the tags string if no headline text
  6645. - the tags string, or nil."
  6646. (save-excursion
  6647. (org-back-to-heading t)
  6648. (if (let (case-fold-search)
  6649. (looking-at
  6650. (if orgstruct-mode
  6651. org-heading-regexp
  6652. org-complex-heading-regexp)))
  6653. (if orgstruct-mode
  6654. (list (length (match-string 1))
  6655. (org-reduced-level (length (match-string 1)))
  6656. nil
  6657. nil
  6658. (match-string 2)
  6659. nil)
  6660. (list (length (match-string 1))
  6661. (org-reduced-level (length (match-string 1)))
  6662. (org-match-string-no-properties 2)
  6663. (and (match-end 3) (aref (match-string 3) 2))
  6664. (org-match-string-no-properties 4)
  6665. (org-match-string-no-properties 5))))))
  6666. (defun org-get-entry ()
  6667. "Get the entry text, after heading, entire subtree."
  6668. (save-excursion
  6669. (org-back-to-heading t)
  6670. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6671. (defun org-insert-heading-after-current ()
  6672. "Insert a new heading with same level as current, after current subtree."
  6673. (interactive)
  6674. (org-back-to-heading)
  6675. (org-insert-heading)
  6676. (org-move-subtree-down)
  6677. (end-of-line 1))
  6678. (defun org-insert-heading-respect-content (invisible-ok)
  6679. "Insert heading with `org-insert-heading-respect-content' set to t."
  6680. (interactive "P")
  6681. (let ((org-insert-heading-respect-content t))
  6682. (org-insert-heading t invisible-ok)))
  6683. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6684. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6685. (interactive "P")
  6686. (let ((org-insert-heading-respect-content t))
  6687. (org-insert-todo-heading force-state t)))
  6688. (defun org-insert-todo-heading (arg &optional force-heading)
  6689. "Insert a new heading with the same level and TODO state as current heading.
  6690. If the heading has no TODO state, or if the state is DONE, use the first
  6691. state (TODO by default). Also one prefix arg, force first state. With two
  6692. prefix args, force inserting at the end of the parent subtree."
  6693. (interactive "P")
  6694. (when (or force-heading (not (org-insert-item 'checkbox)))
  6695. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6696. force-heading))
  6697. (save-excursion
  6698. (org-back-to-heading)
  6699. (outline-previous-heading)
  6700. (looking-at org-todo-line-regexp))
  6701. (let*
  6702. ((new-mark-x
  6703. (if (or arg
  6704. (not (match-beginning 2))
  6705. (member (match-string 2) org-done-keywords))
  6706. (car org-todo-keywords-1)
  6707. (match-string 2)))
  6708. (new-mark
  6709. (or
  6710. (run-hook-with-args-until-success
  6711. 'org-todo-get-default-hook new-mark-x nil)
  6712. new-mark-x)))
  6713. (beginning-of-line 1)
  6714. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6715. (if org-treat-insert-todo-heading-as-state-change
  6716. (org-todo new-mark)
  6717. (insert new-mark " "))))
  6718. (when org-provide-todo-statistics
  6719. (org-update-parent-todo-statistics))))
  6720. (defun org-insert-subheading (arg)
  6721. "Insert a new subheading and demote it.
  6722. Works for outline headings and for plain lists alike."
  6723. (interactive "P")
  6724. (org-insert-heading arg)
  6725. (cond
  6726. ((org-at-heading-p) (org-do-demote))
  6727. ((org-at-item-p) (org-indent-item))))
  6728. (defun org-insert-todo-subheading (arg)
  6729. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6730. Works for outline headings and for plain lists alike."
  6731. (interactive "P")
  6732. (org-insert-todo-heading arg)
  6733. (cond
  6734. ((org-at-heading-p) (org-do-demote))
  6735. ((org-at-item-p) (org-indent-item))))
  6736. ;;; Promotion and Demotion
  6737. (defvar org-after-demote-entry-hook nil
  6738. "Hook run after an entry has been demoted.
  6739. The cursor will be at the beginning of the entry.
  6740. When a subtree is being demoted, the hook will be called for each node.")
  6741. (defvar org-after-promote-entry-hook nil
  6742. "Hook run after an entry has been promoted.
  6743. The cursor will be at the beginning of the entry.
  6744. When a subtree is being promoted, the hook will be called for each node.")
  6745. (defun org-promote-subtree ()
  6746. "Promote the entire subtree.
  6747. See also `org-promote'."
  6748. (interactive)
  6749. (save-excursion
  6750. (org-with-limited-levels (org-map-tree 'org-promote)))
  6751. (org-fix-position-after-promote))
  6752. (defun org-demote-subtree ()
  6753. "Demote the entire subtree. See `org-demote'.
  6754. See also `org-promote'."
  6755. (interactive)
  6756. (save-excursion
  6757. (org-with-limited-levels (org-map-tree 'org-demote)))
  6758. (org-fix-position-after-promote))
  6759. (defun org-do-promote ()
  6760. "Promote the current heading higher up the tree.
  6761. If the region is active in `transient-mark-mode', promote all headings
  6762. in the region."
  6763. (interactive)
  6764. (save-excursion
  6765. (if (org-region-active-p)
  6766. (org-map-region 'org-promote (region-beginning) (region-end))
  6767. (org-promote)))
  6768. (org-fix-position-after-promote))
  6769. (defun org-do-demote ()
  6770. "Demote the current heading lower down the tree.
  6771. If the region is active in `transient-mark-mode', demote all headings
  6772. in the region."
  6773. (interactive)
  6774. (save-excursion
  6775. (if (org-region-active-p)
  6776. (org-map-region 'org-demote (region-beginning) (region-end))
  6777. (org-demote)))
  6778. (org-fix-position-after-promote))
  6779. (defun org-fix-position-after-promote ()
  6780. "Make sure that after pro/demotion cursor position is right."
  6781. (let ((pos (point)))
  6782. (when (save-excursion
  6783. (beginning-of-line 1)
  6784. (looking-at org-todo-line-regexp)
  6785. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6786. (cond ((eobp) (insert " "))
  6787. ((eolp) (insert " "))
  6788. ((equal (char-after) ?\ ) (forward-char 1))))))
  6789. (defun org-current-level ()
  6790. "Return the level of the current entry, or nil if before the first headline.
  6791. The level is the number of stars at the beginning of the headline."
  6792. (save-excursion
  6793. (org-with-limited-levels
  6794. (if (ignore-errors (org-back-to-heading t))
  6795. (funcall outline-level)))))
  6796. (defun org-get-previous-line-level ()
  6797. "Return the outline depth of the last headline before the current line.
  6798. Returns 0 for the first headline in the buffer, and nil if before the
  6799. first headline."
  6800. (let ((current-level (org-current-level))
  6801. (prev-level (when (> (line-number-at-pos) 1)
  6802. (save-excursion
  6803. (beginning-of-line 0)
  6804. (org-current-level)))))
  6805. (cond ((null current-level) nil) ; Before first headline
  6806. ((null prev-level) 0) ; At first headline
  6807. (prev-level))))
  6808. (defun org-reduced-level (l)
  6809. "Compute the effective level of a heading.
  6810. This takes into account the setting of `org-odd-levels-only'."
  6811. (cond
  6812. ((zerop l) 0)
  6813. (org-odd-levels-only (1+ (floor (/ l 2))))
  6814. (t l)))
  6815. (defun org-level-increment ()
  6816. "Return the number of stars that will be added or removed at a
  6817. time to headlines when structure editing, based on the value of
  6818. `org-odd-levels-only'."
  6819. (if org-odd-levels-only 2 1))
  6820. (defun org-get-valid-level (level &optional change)
  6821. "Rectify a level change under the influence of `org-odd-levels-only'
  6822. LEVEL is a current level, CHANGE is by how much the level should be
  6823. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6824. even level numbers will become the next higher odd number."
  6825. (if org-odd-levels-only
  6826. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6827. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6828. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6829. (max 1 (+ level (or change 0)))))
  6830. (if (boundp 'define-obsolete-function-alias)
  6831. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6832. (define-obsolete-function-alias 'org-get-legal-level
  6833. 'org-get-valid-level)
  6834. (define-obsolete-function-alias 'org-get-legal-level
  6835. 'org-get-valid-level "23.1")))
  6836. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6837. ;; ̀org-with-limited-levels'
  6838. (defun org-promote ()
  6839. "Promote the current heading higher up the tree.
  6840. If the region is active in `transient-mark-mode', promote all headings
  6841. in the region."
  6842. (org-back-to-heading t)
  6843. (let* ((level (save-match-data (funcall outline-level)))
  6844. (after-change-functions (remove 'flyspell-after-change-function
  6845. after-change-functions))
  6846. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6847. (diff (abs (- level (length up-head) -1))))
  6848. (cond ((and (= level 1) org-called-with-limited-levels
  6849. org-allow-promoting-top-level-subtree)
  6850. (replace-match "# " nil t))
  6851. ((= level 1)
  6852. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6853. (t (replace-match up-head nil t)))
  6854. ;; Fixup tag positioning
  6855. (unless (= level 1)
  6856. (and org-auto-align-tags (org-set-tags nil t))
  6857. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6858. (run-hooks 'org-after-promote-entry-hook)))
  6859. (defun org-demote ()
  6860. "Demote the current heading lower down the tree.
  6861. If the region is active in `transient-mark-mode', demote all headings
  6862. in the region."
  6863. (org-back-to-heading t)
  6864. (let* ((level (save-match-data (funcall outline-level)))
  6865. (after-change-functions (remove 'flyspell-after-change-function
  6866. after-change-functions))
  6867. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6868. (diff (abs (- level (length down-head) -1))))
  6869. (replace-match down-head nil t)
  6870. ;; Fixup tag positioning
  6871. (and org-auto-align-tags (org-set-tags nil t))
  6872. (if org-adapt-indentation (org-fixup-indentation diff))
  6873. (run-hooks 'org-after-demote-entry-hook)))
  6874. (defun org-cycle-level ()
  6875. "Cycle the level of an empty headline through possible states.
  6876. This goes first to child, then to parent, level, then up the hierarchy.
  6877. After top level, it switches back to sibling level."
  6878. (interactive)
  6879. (let ((org-adapt-indentation nil))
  6880. (when (org-point-at-end-of-empty-headline)
  6881. (setq this-command 'org-cycle-level) ; Only needed for caching
  6882. (let ((cur-level (org-current-level))
  6883. (prev-level (org-get-previous-line-level)))
  6884. (cond
  6885. ;; If first headline in file, promote to top-level.
  6886. ((= prev-level 0)
  6887. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6888. do (org-do-promote)))
  6889. ;; If same level as prev, demote one.
  6890. ((= prev-level cur-level)
  6891. (org-do-demote))
  6892. ;; If parent is top-level, promote to top level if not already.
  6893. ((= prev-level 1)
  6894. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6895. do (org-do-promote)))
  6896. ;; If top-level, return to prev-level.
  6897. ((= cur-level 1)
  6898. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6899. do (org-do-demote)))
  6900. ;; If less than prev-level, promote one.
  6901. ((< cur-level prev-level)
  6902. (org-do-promote))
  6903. ;; If deeper than prev-level, promote until higher than
  6904. ;; prev-level.
  6905. ((> cur-level prev-level)
  6906. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6907. do (org-do-promote))))
  6908. t))))
  6909. (defun org-map-tree (fun)
  6910. "Call FUN for every heading underneath the current one."
  6911. (org-back-to-heading)
  6912. (let ((level (funcall outline-level)))
  6913. (save-excursion
  6914. (funcall fun)
  6915. (while (and (progn
  6916. (outline-next-heading)
  6917. (> (funcall outline-level) level))
  6918. (not (eobp)))
  6919. (funcall fun)))))
  6920. (defun org-map-region (fun beg end)
  6921. "Call FUN for every heading between BEG and END."
  6922. (let ((org-ignore-region t))
  6923. (save-excursion
  6924. (setq end (copy-marker end))
  6925. (goto-char beg)
  6926. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6927. (< (point) end))
  6928. (funcall fun))
  6929. (while (and (progn
  6930. (outline-next-heading)
  6931. (< (point) end))
  6932. (not (eobp)))
  6933. (funcall fun)))))
  6934. (defvar org-property-end-re) ; silence byte-compiler
  6935. (defun org-fixup-indentation (diff)
  6936. "Change the indentation in the current entry by DIFF.
  6937. However, if any line in the current entry has no indentation, or if it
  6938. would end up with no indentation after the change, nothing at all is done."
  6939. (save-excursion
  6940. (let ((end (save-excursion (outline-next-heading)
  6941. (point-marker)))
  6942. (prohibit (if (> diff 0)
  6943. "^\\S-"
  6944. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6945. col)
  6946. (unless (save-excursion (end-of-line 1)
  6947. (re-search-forward prohibit end t))
  6948. (while (and (< (point) end)
  6949. (re-search-forward "^[ \t]+" end t))
  6950. (goto-char (match-end 0))
  6951. (setq col (current-column))
  6952. (if (< diff 0) (replace-match ""))
  6953. (org-indent-to-column (+ diff col))))
  6954. (move-marker end nil))))
  6955. (defun org-convert-to-odd-levels ()
  6956. "Convert an org-mode file with all levels allowed to one with odd levels.
  6957. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6958. level 5 etc."
  6959. (interactive)
  6960. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6961. (let ((outline-level 'org-outline-level)
  6962. (org-odd-levels-only nil) n)
  6963. (save-excursion
  6964. (goto-char (point-min))
  6965. (while (re-search-forward "^\\*\\*+ " nil t)
  6966. (setq n (- (length (match-string 0)) 2))
  6967. (while (>= (setq n (1- n)) 0)
  6968. (org-demote))
  6969. (end-of-line 1))))))
  6970. (defun org-convert-to-oddeven-levels ()
  6971. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6972. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6973. file contains a section with an even level, conversion would
  6974. destroy the structure of the file. An error is signaled in this
  6975. case."
  6976. (interactive)
  6977. (goto-char (point-min))
  6978. ;; First check if there are no even levels
  6979. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6980. (org-show-context t)
  6981. (error "Not all levels are odd in this file. Conversion not possible"))
  6982. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6983. (let ((outline-regexp org-outline-regexp)
  6984. (outline-level 'org-outline-level)
  6985. (org-odd-levels-only nil) n)
  6986. (save-excursion
  6987. (goto-char (point-min))
  6988. (while (re-search-forward "^\\*\\*+ " nil t)
  6989. (setq n (/ (1- (length (match-string 0))) 2))
  6990. (while (>= (setq n (1- n)) 0)
  6991. (org-promote))
  6992. (end-of-line 1))))))
  6993. (defun org-tr-level (n)
  6994. "Make N odd if required."
  6995. (if org-odd-levels-only (1+ (/ n 2)) n))
  6996. ;;; Vertical tree motion, cutting and pasting of subtrees
  6997. (defun org-move-subtree-up (&optional arg)
  6998. "Move the current subtree up past ARG headlines of the same level."
  6999. (interactive "p")
  7000. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7001. (defun org-move-subtree-down (&optional arg)
  7002. "Move the current subtree down past ARG headlines of the same level."
  7003. (interactive "p")
  7004. (setq arg (prefix-numeric-value arg))
  7005. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7006. 'org-get-last-sibling))
  7007. (ins-point (make-marker))
  7008. (cnt (abs arg))
  7009. (col (current-column))
  7010. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7011. ;; Select the tree
  7012. (org-back-to-heading)
  7013. (setq beg0 (point))
  7014. (save-excursion
  7015. (setq ne-beg (org-back-over-empty-lines))
  7016. (setq beg (point)))
  7017. (save-match-data
  7018. (save-excursion (outline-end-of-heading)
  7019. (setq folded (outline-invisible-p)))
  7020. (outline-end-of-subtree))
  7021. (outline-next-heading)
  7022. (setq ne-end (org-back-over-empty-lines))
  7023. (setq end (point))
  7024. (goto-char beg0)
  7025. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7026. ;; include less whitespace
  7027. (save-excursion
  7028. (goto-char beg)
  7029. (forward-line (- ne-beg ne-end))
  7030. (setq beg (point))))
  7031. ;; Find insertion point, with error handling
  7032. (while (> cnt 0)
  7033. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7034. (progn (goto-char beg0)
  7035. (error "Cannot move past superior level or buffer limit")))
  7036. (setq cnt (1- cnt)))
  7037. (if (> arg 0)
  7038. ;; Moving forward - still need to move over subtree
  7039. (progn (org-end-of-subtree t t)
  7040. (save-excursion
  7041. (org-back-over-empty-lines)
  7042. (or (bolp) (newline)))))
  7043. (setq ne-ins (org-back-over-empty-lines))
  7044. (move-marker ins-point (point))
  7045. (setq txt (buffer-substring beg end))
  7046. (org-save-markers-in-region beg end)
  7047. (delete-region beg end)
  7048. (org-remove-empty-overlays-at beg)
  7049. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7050. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7051. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7052. (let ((bbb (point)))
  7053. (insert-before-markers txt)
  7054. (org-reinstall-markers-in-region bbb)
  7055. (move-marker ins-point bbb))
  7056. (or (bolp) (insert "\n"))
  7057. (setq ins-end (point))
  7058. (goto-char ins-point)
  7059. (org-skip-whitespace)
  7060. (when (and (< arg 0)
  7061. (org-first-sibling-p)
  7062. (> ne-ins ne-beg))
  7063. ;; Move whitespace back to beginning
  7064. (save-excursion
  7065. (goto-char ins-end)
  7066. (let ((kill-whole-line t))
  7067. (kill-line (- ne-ins ne-beg)) (point)))
  7068. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7069. (move-marker ins-point nil)
  7070. (if folded
  7071. (hide-subtree)
  7072. (org-show-entry)
  7073. (show-children)
  7074. (org-cycle-hide-drawers 'children))
  7075. (org-clean-visibility-after-subtree-move)
  7076. ;; move back to the initial column we were at
  7077. (move-to-column col)))
  7078. (defvar org-subtree-clip ""
  7079. "Clipboard for cut and paste of subtrees.
  7080. This is actually only a copy of the kill, because we use the normal kill
  7081. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7082. (defvar org-subtree-clip-folded nil
  7083. "Was the last copied subtree folded?
  7084. This is used to fold the tree back after pasting.")
  7085. (defun org-cut-subtree (&optional n)
  7086. "Cut the current subtree into the clipboard.
  7087. With prefix arg N, cut this many sequential subtrees.
  7088. This is a short-hand for marking the subtree and then cutting it."
  7089. (interactive "p")
  7090. (org-copy-subtree n 'cut))
  7091. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7092. "Cut the current subtree into the clipboard.
  7093. With prefix arg N, cut this many sequential subtrees.
  7094. This is a short-hand for marking the subtree and then copying it.
  7095. If CUT is non-nil, actually cut the subtree.
  7096. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7097. of some markers in the region, even if CUT is non-nil. This is
  7098. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7099. (interactive "p")
  7100. (let (beg end folded (beg0 (point)))
  7101. (if (org-called-interactively-p 'any)
  7102. (org-back-to-heading nil) ; take what looks like a subtree
  7103. (org-back-to-heading t)) ; take what is really there
  7104. (setq beg (point))
  7105. (skip-chars-forward " \t\r\n")
  7106. (save-match-data
  7107. (if nosubtrees
  7108. (outline-next-heading)
  7109. (save-excursion (outline-end-of-heading)
  7110. (setq folded (outline-invisible-p)))
  7111. (condition-case nil
  7112. (org-forward-heading-same-level (1- n) t)
  7113. (error nil))
  7114. (org-end-of-subtree t t)))
  7115. (setq end (point))
  7116. (goto-char beg0)
  7117. (when (> end beg)
  7118. (setq org-subtree-clip-folded folded)
  7119. (when (or cut force-store-markers)
  7120. (org-save-markers-in-region beg end))
  7121. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7122. (setq org-subtree-clip (current-kill 0))
  7123. (message "%s: Subtree(s) with %d characters"
  7124. (if cut "Cut" "Copied")
  7125. (length org-subtree-clip)))))
  7126. (defun org-paste-subtree (&optional level tree for-yank)
  7127. "Paste the clipboard as a subtree, with modification of headline level.
  7128. The entire subtree is promoted or demoted in order to match a new headline
  7129. level.
  7130. If the cursor is at the beginning of a headline, the same level as
  7131. that headline is used to paste the tree.
  7132. If not, the new level is derived from the *visible* headings
  7133. before and after the insertion point, and taken to be the inferior headline
  7134. level of the two. So if the previous visible heading is level 3 and the
  7135. next is level 4 (or vice versa), level 4 will be used for insertion.
  7136. This makes sure that the subtree remains an independent subtree and does
  7137. not swallow low level entries.
  7138. You can also force a different level, either by using a numeric prefix
  7139. argument, or by inserting the heading marker by hand. For example, if the
  7140. cursor is after \"*****\", then the tree will be shifted to level 5.
  7141. If optional TREE is given, use this text instead of the kill ring.
  7142. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7143. move back over whitespace before inserting, and move point to the end of
  7144. the inserted text when done."
  7145. (interactive "P")
  7146. (setq tree (or tree (and kill-ring (current-kill 0))))
  7147. (unless (org-kill-is-subtree-p tree)
  7148. (error "%s"
  7149. (substitute-command-keys
  7150. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7151. (org-with-limited-levels
  7152. (let* ((visp (not (outline-invisible-p)))
  7153. (txt tree)
  7154. (^re_ "\\(\\*+\\)[ \t]*")
  7155. (old-level (if (string-match org-outline-regexp-bol txt)
  7156. (- (match-end 0) (match-beginning 0) 1)
  7157. -1))
  7158. (force-level (cond (level (prefix-numeric-value level))
  7159. ((and (looking-at "[ \t]*$")
  7160. (string-match
  7161. "^\\*+$" (buffer-substring
  7162. (point-at-bol) (point))))
  7163. (- (match-end 1) (match-beginning 1)))
  7164. ((and (bolp)
  7165. (looking-at org-outline-regexp))
  7166. (- (match-end 0) (point) 1))))
  7167. (previous-level (save-excursion
  7168. (condition-case nil
  7169. (progn
  7170. (outline-previous-visible-heading 1)
  7171. (if (looking-at ^re_)
  7172. (- (match-end 0) (match-beginning 0) 1)
  7173. 1))
  7174. (error 1))))
  7175. (next-level (save-excursion
  7176. (condition-case nil
  7177. (progn
  7178. (or (looking-at org-outline-regexp)
  7179. (outline-next-visible-heading 1))
  7180. (if (looking-at ^re_)
  7181. (- (match-end 0) (match-beginning 0) 1)
  7182. 1))
  7183. (error 1))))
  7184. (new-level (or force-level (max previous-level next-level)))
  7185. (shift (if (or (= old-level -1)
  7186. (= new-level -1)
  7187. (= old-level new-level))
  7188. 0
  7189. (- new-level old-level)))
  7190. (delta (if (> shift 0) -1 1))
  7191. (func (if (> shift 0) 'org-demote 'org-promote))
  7192. (org-odd-levels-only nil)
  7193. beg end newend)
  7194. ;; Remove the forced level indicator
  7195. (if force-level
  7196. (delete-region (point-at-bol) (point)))
  7197. ;; Paste
  7198. (beginning-of-line (if (bolp) 1 2))
  7199. (setq beg (point))
  7200. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7201. (insert-before-markers txt)
  7202. (unless (string-match "\n\\'" txt) (insert "\n"))
  7203. (setq newend (point))
  7204. (org-reinstall-markers-in-region beg)
  7205. (setq end (point))
  7206. (goto-char beg)
  7207. (skip-chars-forward " \t\n\r")
  7208. (setq beg (point))
  7209. (if (and (outline-invisible-p) visp)
  7210. (save-excursion (outline-show-heading)))
  7211. ;; Shift if necessary
  7212. (unless (= shift 0)
  7213. (save-restriction
  7214. (narrow-to-region beg end)
  7215. (while (not (= shift 0))
  7216. (org-map-region func (point-min) (point-max))
  7217. (setq shift (+ delta shift)))
  7218. (goto-char (point-min))
  7219. (setq newend (point-max))))
  7220. (when (or (org-called-interactively-p 'interactive) for-yank)
  7221. (message "Clipboard pasted as level %d subtree" new-level))
  7222. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7223. kill-ring
  7224. (eq org-subtree-clip (current-kill 0))
  7225. org-subtree-clip-folded)
  7226. ;; The tree was folded before it was killed/copied
  7227. (hide-subtree))
  7228. (and for-yank (goto-char newend)))))
  7229. (defun org-kill-is-subtree-p (&optional txt)
  7230. "Check if the current kill is an outline subtree, or a set of trees.
  7231. Returns nil if kill does not start with a headline, or if the first
  7232. headline level is not the largest headline level in the tree.
  7233. So this will actually accept several entries of equal levels as well,
  7234. which is OK for `org-paste-subtree'.
  7235. If optional TXT is given, check this string instead of the current kill."
  7236. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7237. (re (org-get-limited-outline-regexp))
  7238. (^re (concat "^" re))
  7239. (start-level (and kill
  7240. (string-match
  7241. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7242. kill)
  7243. (- (match-end 2) (match-beginning 2) 1)))
  7244. (start (1+ (or (match-beginning 2) -1))))
  7245. (if (not start-level)
  7246. (progn
  7247. nil) ;; does not even start with a heading
  7248. (catch 'exit
  7249. (while (setq start (string-match ^re kill (1+ start)))
  7250. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7251. (throw 'exit nil)))
  7252. t))))
  7253. (defvar org-markers-to-move nil
  7254. "Markers that should be moved with a cut-and-paste operation.
  7255. Those markers are stored together with their positions relative to
  7256. the start of the region.")
  7257. (defun org-save-markers-in-region (beg end)
  7258. "Check markers in region.
  7259. If these markers are between BEG and END, record their position relative
  7260. to BEG, so that after moving the block of text, we can put the markers back
  7261. into place.
  7262. This function gets called just before an entry or tree gets cut from the
  7263. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7264. called immediately, to move the markers with the entries."
  7265. (setq org-markers-to-move nil)
  7266. (when (featurep 'org-clock)
  7267. (org-clock-save-markers-for-cut-and-paste beg end))
  7268. (when (featurep 'org-agenda)
  7269. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7270. (defun org-check-and-save-marker (marker beg end)
  7271. "Check if MARKER is between BEG and END.
  7272. If yes, remember the marker and the distance to BEG."
  7273. (when (and (marker-buffer marker)
  7274. (equal (marker-buffer marker) (current-buffer)))
  7275. (if (and (>= marker beg) (< marker end))
  7276. (push (cons marker (- marker beg)) org-markers-to-move))))
  7277. (defun org-reinstall-markers-in-region (beg)
  7278. "Move all remembered markers to their position relative to BEG."
  7279. (mapc (lambda (x)
  7280. (move-marker (car x) (+ beg (cdr x))))
  7281. org-markers-to-move)
  7282. (setq org-markers-to-move nil))
  7283. (defun org-narrow-to-subtree ()
  7284. "Narrow buffer to the current subtree."
  7285. (interactive)
  7286. (save-excursion
  7287. (save-match-data
  7288. (org-with-limited-levels
  7289. (narrow-to-region
  7290. (progn (org-back-to-heading t) (point))
  7291. (progn (org-end-of-subtree t t)
  7292. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7293. (point)))))))
  7294. (defun org-narrow-to-block ()
  7295. "Narrow buffer to the current block."
  7296. (interactive)
  7297. (let* ((case-fold-search t)
  7298. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7299. "^[ \t]*#\\+end_.*")))
  7300. (if blockp
  7301. (narrow-to-region (car blockp) (cdr blockp))
  7302. (error "Not in a block"))))
  7303. (eval-when-compile
  7304. (defvar org-property-drawer-re))
  7305. (defvar org-property-start-re) ;; defined below
  7306. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7307. "Clone the task (subtree) at point N times.
  7308. The clones will be inserted as siblings.
  7309. In interactive use, the user will be prompted for the number of
  7310. clones to be produced, and for a time SHIFT, which may be a
  7311. repeater as used in time stamps, for example `+3d'.
  7312. When a valid repeater is given and the entry contains any time
  7313. stamps, the clones will become a sequence in time, with time
  7314. stamps in the subtree shifted for each clone produced. If SHIFT
  7315. is nil or the empty string, time stamps will be left alone. The
  7316. ID property of the original subtree is removed.
  7317. If the original subtree did contain time stamps with a repeater,
  7318. the following will happen:
  7319. - the repeater will be removed in each clone
  7320. - an additional clone will be produced, with the current, unshifted
  7321. date(s) in the entry.
  7322. - the original entry will be placed *after* all the clones, with
  7323. repeater intact.
  7324. - the start days in the repeater in the original entry will be shifted
  7325. to past the last clone.
  7326. In this way you can spell out a number of instances of a repeating task,
  7327. and still retain the repeater to cover future instances of the task."
  7328. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7329. (let (beg end template task idprop
  7330. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7331. (drawer-re org-drawer-regexp))
  7332. (if (not (and (integerp n) (> n 0)))
  7333. (error "Invalid number of replications %s" n))
  7334. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7335. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7336. shift)))
  7337. (error "Invalid shift specification %s" shift))
  7338. (when doshift
  7339. (setq shift-n (string-to-number (match-string 1 shift))
  7340. shift-what (cdr (assoc (match-string 2 shift)
  7341. '(("d" . day) ("w" . week)
  7342. ("m" . month) ("y" . year))))))
  7343. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7344. (setq nmin 1 nmax n)
  7345. (org-back-to-heading t)
  7346. (setq beg (point))
  7347. (setq idprop (org-entry-get nil "ID"))
  7348. (org-end-of-subtree t t)
  7349. (or (bolp) (insert "\n"))
  7350. (setq end (point))
  7351. (setq template (buffer-substring beg end))
  7352. (when (and doshift
  7353. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7354. (delete-region beg end)
  7355. (setq end beg)
  7356. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7357. (goto-char end)
  7358. (loop for n from nmin to nmax do
  7359. ;; prepare clone
  7360. (with-temp-buffer
  7361. (insert template)
  7362. (org-mode)
  7363. (goto-char (point-min))
  7364. (org-show-subtree)
  7365. (and idprop (if org-clone-delete-id
  7366. (org-entry-delete nil "ID")
  7367. (org-id-get-create t)))
  7368. (unless (= n 0)
  7369. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7370. (kill-whole-line))
  7371. (goto-char (point-min))
  7372. (while (re-search-forward drawer-re nil t)
  7373. (mapc (lambda (d)
  7374. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7375. (goto-char (point-min))
  7376. (when doshift
  7377. (while (re-search-forward org-ts-regexp-both nil t)
  7378. (org-timestamp-change (* n shift-n) shift-what))
  7379. (unless (= n n-no-remove)
  7380. (goto-char (point-min))
  7381. (while (re-search-forward org-ts-regexp nil t)
  7382. (save-excursion
  7383. (goto-char (match-beginning 0))
  7384. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7385. (delete-region (match-beginning 1) (match-end 1)))))))
  7386. (setq task (buffer-string)))
  7387. (insert task))
  7388. (goto-char beg)))
  7389. ;;; Outline Sorting
  7390. (defun org-sort (with-case)
  7391. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7392. Optional argument WITH-CASE means sort case-sensitively."
  7393. (interactive "P")
  7394. (cond
  7395. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7396. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7397. (t
  7398. (org-call-with-arg 'org-sort-entries with-case))))
  7399. (defun org-sort-remove-invisible (s)
  7400. (remove-text-properties 0 (length s) org-rm-props s)
  7401. (while (string-match org-bracket-link-regexp s)
  7402. (setq s (replace-match (if (match-end 2)
  7403. (match-string 3 s)
  7404. (match-string 1 s)) t t s)))
  7405. s)
  7406. (defvar org-priority-regexp) ; defined later in the file
  7407. (defvar org-after-sorting-entries-or-items-hook nil
  7408. "Hook that is run after a bunch of entries or items have been sorted.
  7409. When children are sorted, the cursor is in the parent line when this
  7410. hook gets called. When a region or a plain list is sorted, the cursor
  7411. will be in the first entry of the sorted region/list.")
  7412. (defun org-sort-entries
  7413. (&optional with-case sorting-type getkey-func compare-func property)
  7414. "Sort entries on a certain level of an outline tree.
  7415. If there is an active region, the entries in the region are sorted.
  7416. Else, if the cursor is before the first entry, sort the top-level items.
  7417. Else, the children of the entry at point are sorted.
  7418. Sorting can be alphabetically, numerically, by date/time as given by
  7419. a time stamp, by a property or by priority.
  7420. The command prompts for the sorting type unless it has been given to the
  7421. function through the SORTING-TYPE argument, which needs to be a character,
  7422. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7423. precise meaning of each character:
  7424. n Numerically, by converting the beginning of the entry/item to a number.
  7425. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7426. o By order of TODO keywords.
  7427. t By date/time, either the first active time stamp in the entry, or, if
  7428. none exist, by the first inactive one.
  7429. s By the scheduled date/time.
  7430. d By deadline date/time.
  7431. c By creation time, which is assumed to be the first inactive time stamp
  7432. at the beginning of a line.
  7433. p By priority according to the cookie.
  7434. r By the value of a property.
  7435. Capital letters will reverse the sort order.
  7436. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7437. called with point at the beginning of the record. It must return either
  7438. a string or a number that should serve as the sorting key for that record.
  7439. Comparing entries ignores case by default. However, with an optional argument
  7440. WITH-CASE, the sorting considers case as well."
  7441. (interactive "P")
  7442. (let ((case-func (if with-case 'identity 'downcase))
  7443. (cmstr
  7444. ;; The clock marker is lost when using `sort-subr', let's
  7445. ;; store the clocking string.
  7446. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7447. (save-excursion
  7448. (goto-char org-clock-marker)
  7449. (looking-back "^.*") (match-string-no-properties 0))))
  7450. start beg end stars re re2
  7451. txt what tmp)
  7452. ;; Find beginning and end of region to sort
  7453. (cond
  7454. ((org-region-active-p)
  7455. ;; we will sort the region
  7456. (setq end (region-end)
  7457. what "region")
  7458. (goto-char (region-beginning))
  7459. (if (not (org-at-heading-p)) (outline-next-heading))
  7460. (setq start (point)))
  7461. ((or (org-at-heading-p)
  7462. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7463. ;; we will sort the children of the current headline
  7464. (org-back-to-heading)
  7465. (setq start (point)
  7466. end (progn (org-end-of-subtree t t)
  7467. (or (bolp) (insert "\n"))
  7468. (org-back-over-empty-lines)
  7469. (point))
  7470. what "children")
  7471. (goto-char start)
  7472. (show-subtree)
  7473. (outline-next-heading))
  7474. (t
  7475. ;; we will sort the top-level entries in this file
  7476. (goto-char (point-min))
  7477. (or (org-at-heading-p) (outline-next-heading))
  7478. (setq start (point))
  7479. (goto-char (point-max))
  7480. (beginning-of-line 1)
  7481. (when (looking-at ".*?\\S-")
  7482. ;; File ends in a non-white line
  7483. (end-of-line 1)
  7484. (insert "\n"))
  7485. (setq end (point-max))
  7486. (setq what "top-level")
  7487. (goto-char start)
  7488. (show-all)))
  7489. (setq beg (point))
  7490. (if (>= beg end) (error "Nothing to sort"))
  7491. (looking-at "\\(\\*+\\)")
  7492. (setq stars (match-string 1)
  7493. re (concat "^" (regexp-quote stars) " +")
  7494. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7495. txt (buffer-substring beg end))
  7496. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7497. (if (and (not (equal stars "*")) (string-match re2 txt))
  7498. (error "Region to sort contains a level above the first entry"))
  7499. (unless sorting-type
  7500. (message
  7501. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7502. [t]ime [s]cheduled [d]eadline [c]reated
  7503. A/N/P/R/O/F/T/S/D/C means reversed:"
  7504. what)
  7505. (setq sorting-type (read-char-exclusive))
  7506. (unless getkey-func
  7507. (and (= (downcase sorting-type) ?f)
  7508. (setq getkey-func
  7509. (org-icompleting-read "Sort using function: "
  7510. obarray 'fboundp t nil nil))
  7511. (setq getkey-func (intern getkey-func))))
  7512. (and (= (downcase sorting-type) ?r)
  7513. (not property)
  7514. (setq property
  7515. (org-icompleting-read "Property: "
  7516. (mapcar 'list (org-buffer-property-keys t))
  7517. nil t))))
  7518. (message "Sorting entries...")
  7519. (save-restriction
  7520. (narrow-to-region start end)
  7521. (let ((dcst (downcase sorting-type))
  7522. (case-fold-search nil)
  7523. (now (current-time)))
  7524. (sort-subr
  7525. (/= dcst sorting-type)
  7526. ;; This function moves to the beginning character of the "record" to
  7527. ;; be sorted.
  7528. (lambda nil
  7529. (if (re-search-forward re nil t)
  7530. (goto-char (match-beginning 0))
  7531. (goto-char (point-max))))
  7532. ;; This function moves to the last character of the "record" being
  7533. ;; sorted.
  7534. (lambda nil
  7535. (save-match-data
  7536. (condition-case nil
  7537. (outline-forward-same-level 1)
  7538. (error
  7539. (goto-char (point-max))))))
  7540. ;; This function returns the value that gets sorted against.
  7541. (lambda nil
  7542. (cond
  7543. ((= dcst ?n)
  7544. (if (looking-at org-complex-heading-regexp)
  7545. (string-to-number (match-string 4))
  7546. nil))
  7547. ((= dcst ?a)
  7548. (if (looking-at org-complex-heading-regexp)
  7549. (funcall case-func (match-string 4))
  7550. nil))
  7551. ((= dcst ?t)
  7552. (let ((end (save-excursion (outline-next-heading) (point))))
  7553. (if (or (re-search-forward org-ts-regexp end t)
  7554. (re-search-forward org-ts-regexp-both end t))
  7555. (org-time-string-to-seconds (match-string 0))
  7556. (org-float-time now))))
  7557. ((= dcst ?c)
  7558. (let ((end (save-excursion (outline-next-heading) (point))))
  7559. (if (re-search-forward
  7560. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7561. end t)
  7562. (org-time-string-to-seconds (match-string 0))
  7563. (org-float-time now))))
  7564. ((= dcst ?s)
  7565. (let ((end (save-excursion (outline-next-heading) (point))))
  7566. (if (re-search-forward org-scheduled-time-regexp end t)
  7567. (org-time-string-to-seconds (match-string 1))
  7568. (org-float-time now))))
  7569. ((= dcst ?d)
  7570. (let ((end (save-excursion (outline-next-heading) (point))))
  7571. (if (re-search-forward org-deadline-time-regexp end t)
  7572. (org-time-string-to-seconds (match-string 1))
  7573. (org-float-time now))))
  7574. ((= dcst ?p)
  7575. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7576. (string-to-char (match-string 2))
  7577. org-default-priority))
  7578. ((= dcst ?r)
  7579. (or (org-entry-get nil property) ""))
  7580. ((= dcst ?o)
  7581. (if (looking-at org-complex-heading-regexp)
  7582. (- 9999 (length (member (match-string 2)
  7583. org-todo-keywords-1)))))
  7584. ((= dcst ?f)
  7585. (if getkey-func
  7586. (progn
  7587. (setq tmp (funcall getkey-func))
  7588. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7589. tmp)
  7590. (error "Invalid key function `%s'" getkey-func)))
  7591. (t (error "Invalid sorting type `%c'" sorting-type))))
  7592. nil
  7593. (cond
  7594. ((= dcst ?a) 'string<)
  7595. ((= dcst ?f) compare-func)
  7596. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7597. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7598. ;; Reset the clock marker if needed
  7599. (when cmstr
  7600. (save-excursion
  7601. (goto-char start)
  7602. (search-forward cmstr nil t)
  7603. (move-marker org-clock-marker (point))))
  7604. (message "Sorting entries...done")))
  7605. (defun org-do-sort (table what &optional with-case sorting-type)
  7606. "Sort TABLE of WHAT according to SORTING-TYPE.
  7607. The user will be prompted for the SORTING-TYPE if the call to this
  7608. function does not specify it. WHAT is only for the prompt, to indicate
  7609. what is being sorted. The sorting key will be extracted from
  7610. the car of the elements of the table.
  7611. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7612. (unless sorting-type
  7613. (message
  7614. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7615. what)
  7616. (setq sorting-type (read-char-exclusive)))
  7617. (let ((dcst (downcase sorting-type))
  7618. extractfun comparefun)
  7619. ;; Define the appropriate functions
  7620. (cond
  7621. ((= dcst ?n)
  7622. (setq extractfun 'string-to-number
  7623. comparefun (if (= dcst sorting-type) '< '>)))
  7624. ((= dcst ?a)
  7625. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7626. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7627. comparefun (if (= dcst sorting-type)
  7628. 'string<
  7629. (lambda (a b) (and (not (string< a b))
  7630. (not (string= a b)))))))
  7631. ((= dcst ?t)
  7632. (setq extractfun
  7633. (lambda (x)
  7634. (if (or (string-match org-ts-regexp x)
  7635. (string-match org-ts-regexp-both x))
  7636. (org-float-time
  7637. (org-time-string-to-time (match-string 0 x)))
  7638. 0))
  7639. comparefun (if (= dcst sorting-type) '< '>)))
  7640. (t (error "Invalid sorting type `%c'" sorting-type)))
  7641. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7642. table)
  7643. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7644. ;;; The orgstruct minor mode
  7645. ;; Define a minor mode which can be used in other modes in order to
  7646. ;; integrate the org-mode structure editing commands.
  7647. ;; This is really a hack, because the org-mode structure commands use
  7648. ;; keys which normally belong to the major mode. Here is how it
  7649. ;; works: The minor mode defines all the keys necessary to operate the
  7650. ;; structure commands, but wraps the commands into a function which
  7651. ;; tests if the cursor is currently at a headline or a plain list
  7652. ;; item. If that is the case, the structure command is used,
  7653. ;; temporarily setting many Org-mode variables like regular
  7654. ;; expressions for filling etc. However, when any of those keys is
  7655. ;; used at a different location, function uses `key-binding' to look
  7656. ;; up if the key has an associated command in another currently active
  7657. ;; keymap (minor modes, major mode, global), and executes that
  7658. ;; command. There might be problems if any of the keys is otherwise
  7659. ;; used as a prefix key.
  7660. (defcustom orgstruct-heading-prefix-regexp ""
  7661. "Regexp that matches the custom prefix of Org headlines in
  7662. orgstruct(++)-mode."
  7663. :group 'org
  7664. :type 'string)
  7665. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7666. (defcustom orgstruct-setup-hook nil
  7667. "Hook run after orgstruct-mode-map is filled."
  7668. :group 'org
  7669. :type 'hook)
  7670. (defvar orgstruct-initialized nil)
  7671. (defvar org-local-vars nil
  7672. "List of local variables, for use by `orgstruct-mode'.")
  7673. ;;;###autoload
  7674. (define-minor-mode orgstruct-mode
  7675. "Toggle the minor mode `orgstruct-mode'.
  7676. This mode is for using Org-mode structure commands in other
  7677. modes. The following keys behave as if Org-mode were active, if
  7678. the cursor is on a headline, or on a plain list item (both as
  7679. defined by Org-mode)."
  7680. nil " OrgStruct" (make-sparse-keymap)
  7681. (when orgstruct-mode
  7682. (org-load-modules-maybe)
  7683. (unless orgstruct-initialized
  7684. (orgstruct-setup)
  7685. (setq orgstruct-initialized t))))
  7686. ;;;###autoload
  7687. (defun turn-on-orgstruct ()
  7688. "Unconditionally turn on `orgstruct-mode'."
  7689. (orgstruct-mode 1))
  7690. (defvar org-fb-vars nil)
  7691. (make-variable-buffer-local 'org-fb-vars)
  7692. (defun orgstruct++-mode (&optional arg)
  7693. "Toggle `orgstruct-mode', the enhanced version of it.
  7694. In addition to setting orgstruct-mode, this also exports all
  7695. indentation and autofilling variables from org-mode into the
  7696. buffer. It will also recognize item context in multiline items."
  7697. (interactive "P")
  7698. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7699. (if (< arg 1)
  7700. (progn (orgstruct-mode -1)
  7701. (mapc (lambda(v)
  7702. (org-set-local (car v)
  7703. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7704. org-fb-vars))
  7705. (orgstruct-mode 1)
  7706. (setq org-fb-vars nil)
  7707. (let (var val)
  7708. (mapc
  7709. (lambda (x)
  7710. (when (string-match
  7711. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7712. (symbol-name (car x)))
  7713. (setq var (car x) val (nth 1 x))
  7714. (push (list var `(quote ,(eval var))) org-fb-vars)
  7715. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7716. org-local-vars)
  7717. (org-set-local 'orgstruct-is-++ t))))
  7718. (defvar orgstruct-is-++ nil
  7719. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7720. (make-variable-buffer-local 'orgstruct-is-++)
  7721. ;;;###autoload
  7722. (defun turn-on-orgstruct++ ()
  7723. "Unconditionally turn on `orgstruct++-mode'."
  7724. (orgstruct++-mode 1))
  7725. (defun orgstruct-error ()
  7726. "Error when there is no default binding for a structure key."
  7727. (interactive)
  7728. (error "This key has no function outside structure elements"))
  7729. (defun orgstruct-setup ()
  7730. "Setup orgstruct keymap."
  7731. (dolist (f
  7732. '("org-meta"
  7733. "org-shiftmeta"
  7734. org-shifttab
  7735. org-backward-element
  7736. org-backward-heading-same-level
  7737. org-ctrl-c-ret
  7738. org-cycle
  7739. org-forward-heading-same-level
  7740. org-insert-heading
  7741. org-insert-heading-respect-content
  7742. org-kill-note-or-show-branches
  7743. org-mark-subtree
  7744. org-narrow-to-subtree
  7745. org-promote-subtree
  7746. org-reveal
  7747. org-show-subtree
  7748. org-sort
  7749. org-up-element
  7750. outline-demote
  7751. outline-next-visible-heading
  7752. outline-previous-visible-heading
  7753. outline-promote
  7754. outline-up-heading
  7755. show-children))
  7756. (dolist (f (if (stringp f)
  7757. (let ((flist))
  7758. (dolist (postfix
  7759. '("-return" "tab" "left" "right" "up" "down")
  7760. flist)
  7761. (let ((f (intern (concat f postfix))))
  7762. (when (fboundp f)
  7763. (push f flist)))))
  7764. (list f)))
  7765. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7766. (where-is-internal f outline-mode-map)))
  7767. ;; TODO use local-function-key-map
  7768. (dolist (rep '(("<tab>" . "TAB")
  7769. ("<ret>" . "RET")
  7770. ("<esc>" . "ESC")
  7771. ("<del>" . "DEL")))
  7772. (setq binding (read-kbd-macro (replace-regexp-in-string
  7773. (regexp-quote (car rep))
  7774. (cdr rep)
  7775. (key-description binding)))))
  7776. (let ((key (lookup-key orgstruct-mode-map binding)))
  7777. (when (or (not key) (numberp key))
  7778. (condition-case nil
  7779. (org-defkey orgstruct-mode-map
  7780. binding
  7781. (orgstruct-make-binding f binding))
  7782. (error nil)))))))
  7783. (run-hooks 'orgstruct-setup-hook))
  7784. (defun orgstruct-make-binding (fun key)
  7785. "Create a function for binding in the structure minor mode.
  7786. FUN is the command to call inside a table. KEY is the key that
  7787. should be checked in for a command to execute outside of tables."
  7788. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7789. (let ((nname name)
  7790. (i 0))
  7791. (while (fboundp (intern nname))
  7792. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7793. (setq name (intern nname)))
  7794. (eval
  7795. `(defun ,name (arg)
  7796. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7797. "Outside of structure, run the binding of `"
  7798. (key-description key) "'.")
  7799. (interactive "p")
  7800. (unless
  7801. (let* ((org-heading-regexp
  7802. (concat "^"
  7803. orgstruct-heading-prefix-regexp
  7804. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7805. (org-outline-regexp
  7806. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7807. (org-outline-regexp-bol
  7808. (concat "^" org-outline-regexp))
  7809. (outline-regexp org-outline-regexp)
  7810. (outline-heading-end-regexp "\n")
  7811. (outline-level 'outline-level)
  7812. (outline-heading-alist))
  7813. (when (org-context-p 'headline 'item
  7814. ,(when (memq fun '(org-insert-heading))
  7815. '(when orgstruct-is-++
  7816. 'item-body)))
  7817. (org-run-like-in-org-mode ',fun)
  7818. t))
  7819. (let* ((orgstruct-mode)
  7820. (binding (key-binding ,key)))
  7821. (if (keymapp binding)
  7822. (set-temporary-overlay-map binding)
  7823. (call-interactively
  7824. (or binding 'orgstruct-error)))))))
  7825. name))
  7826. (defun org-contextualize-keys (alist contexts)
  7827. "Return valid elements in ALIST depending on CONTEXTS.
  7828. `org-agenda-custom-commands' or `org-capture-templates' are the
  7829. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7830. or `org-capture-templates-contexts' are the associated contexts
  7831. definitions."
  7832. (let ((contexts
  7833. ;; normalize contexts
  7834. (mapcar
  7835. (lambda(c) (cond ((listp (cadr c))
  7836. (list (car c) (car c) (cadr c)))
  7837. ((string= "" (cadr c))
  7838. (list (car c) (car c) (caddr c)))
  7839. (t c))) contexts))
  7840. (a alist) c r s)
  7841. ;; loop over all commands or templates
  7842. (while (setq c (pop a))
  7843. (let (vrules repl)
  7844. (cond
  7845. ((not (assoc (car c) contexts))
  7846. (push c r))
  7847. ((and (assoc (car c) contexts)
  7848. (setq vrules (org-contextualize-validate-key
  7849. (car c) contexts)))
  7850. (mapc (lambda (vr)
  7851. (when (not (equal (car vr) (cadr vr)))
  7852. (setq repl vr))) vrules)
  7853. (if (not repl) (push c r)
  7854. (push (cadr repl) s)
  7855. (push
  7856. (cons (car c)
  7857. (cdr (or (assoc (cadr repl) alist)
  7858. (error "Undefined key `%s' as contextual replacement for `%s'"
  7859. (cadr repl) (car c)))))
  7860. r))))))
  7861. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7862. (delq
  7863. nil
  7864. (delete-dups
  7865. (mapcar (lambda (x)
  7866. (let ((tpl (car x)))
  7867. (when (not (delq
  7868. nil
  7869. (mapcar (lambda(y)
  7870. (equal y tpl)) s))) x)))
  7871. (reverse r))))))
  7872. (defun org-contextualize-validate-key (key contexts)
  7873. "Check CONTEXTS for agenda or capture KEY."
  7874. (let (r rr res)
  7875. (while (setq r (pop contexts))
  7876. (mapc
  7877. (lambda (rr)
  7878. (when
  7879. (and (equal key (car r))
  7880. (if (functionp rr) (funcall rr)
  7881. (or (and (eq (car rr) 'in-file)
  7882. (buffer-file-name)
  7883. (string-match (cdr rr) (buffer-file-name)))
  7884. (and (eq (car rr) 'in-mode)
  7885. (string-match (cdr rr) (symbol-name major-mode)))
  7886. (and (eq (car rr) 'in-buffer)
  7887. (string-match (cdr rr) (buffer-name)))
  7888. (when (and (eq (car rr) 'not-in-file)
  7889. (buffer-file-name))
  7890. (not (string-match (cdr rr) (buffer-file-name))))
  7891. (when (eq (car rr) 'not-in-mode)
  7892. (not (string-match (cdr rr) (symbol-name major-mode))))
  7893. (when (eq (car rr) 'not-in-buffer)
  7894. (not (string-match (cdr rr) (buffer-name)))))))
  7895. (push r res)))
  7896. (car (last r))))
  7897. (delete-dups (delq nil res))))
  7898. (defun org-context-p (&rest contexts)
  7899. "Check if local context is any of CONTEXTS.
  7900. Possible values in the list of contexts are `table', `headline', and `item'."
  7901. (let ((pos (point)))
  7902. (goto-char (point-at-bol))
  7903. (prog1 (or (and (memq 'table contexts)
  7904. (looking-at "[ \t]*|"))
  7905. (and (memq 'headline contexts)
  7906. (looking-at org-outline-regexp))
  7907. (and (memq 'item contexts)
  7908. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7909. (and (memq 'item-body contexts)
  7910. (org-in-item-p)))
  7911. (goto-char pos))))
  7912. (defun org-get-local-variables ()
  7913. "Return a list of all local variables in an Org mode buffer."
  7914. (let (varlist)
  7915. (with-current-buffer (get-buffer-create "*Org tmp*")
  7916. (erase-buffer)
  7917. (org-mode)
  7918. (setq varlist (buffer-local-variables)))
  7919. (kill-buffer "*Org tmp*")
  7920. (delq nil
  7921. (mapcar
  7922. (lambda (x)
  7923. (setq x
  7924. (if (symbolp x)
  7925. (list x)
  7926. (list (car x) (cdr x))))
  7927. (if (and (not (get (car x) 'org-state))
  7928. (string-match
  7929. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7930. (symbol-name (car x))))
  7931. x nil))
  7932. varlist))))
  7933. (defun org-clone-local-variables (from-buffer &optional regexp)
  7934. "Clone local variables from FROM-BUFFER.
  7935. Optional argument REGEXP selects variables to clone."
  7936. (mapc
  7937. (lambda (pair)
  7938. (and (symbolp (car pair))
  7939. (or (null regexp)
  7940. (string-match regexp (symbol-name (car pair))))
  7941. (set (make-local-variable (car pair))
  7942. (cdr pair))))
  7943. (buffer-local-variables from-buffer)))
  7944. ;;;###autoload
  7945. (defun org-run-like-in-org-mode (cmd)
  7946. "Run a command, pretending that the current buffer is in Org-mode.
  7947. This will temporarily bind local variables that are typically bound in
  7948. Org-mode to the values they have in Org-mode, and then interactively
  7949. call CMD."
  7950. (org-load-modules-maybe)
  7951. (unless org-local-vars
  7952. (setq org-local-vars (org-get-local-variables)))
  7953. (let (binds)
  7954. (dolist (var org-local-vars)
  7955. (when (or (not (boundp (car var)))
  7956. (eq (symbol-value (car var))
  7957. (default-value (car var))))
  7958. (push (list (car var) `(quote ,(cadr var))) binds)))
  7959. (eval `(let ,binds
  7960. (call-interactively (quote ,cmd))))))
  7961. ;;;; Archiving
  7962. (defun org-get-category (&optional pos force-refresh)
  7963. "Get the category applying to position POS."
  7964. (save-match-data
  7965. (if force-refresh (org-refresh-category-properties))
  7966. (let ((pos (or pos (point))))
  7967. (or (get-text-property pos 'org-category)
  7968. (progn (org-refresh-category-properties)
  7969. (get-text-property pos 'org-category))))))
  7970. (defun org-refresh-category-properties ()
  7971. "Refresh category text properties in the buffer."
  7972. (let ((case-fold-search t)
  7973. (inhibit-read-only t)
  7974. (def-cat (cond
  7975. ((null org-category)
  7976. (if buffer-file-name
  7977. (file-name-sans-extension
  7978. (file-name-nondirectory buffer-file-name))
  7979. "???"))
  7980. ((symbolp org-category) (symbol-name org-category))
  7981. (t org-category)))
  7982. beg end cat pos optionp)
  7983. (org-unmodified
  7984. (save-excursion
  7985. (save-restriction
  7986. (widen)
  7987. (goto-char (point-min))
  7988. (put-text-property (point) (point-max) 'org-category def-cat)
  7989. (while (re-search-forward
  7990. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7991. (setq pos (match-end 0)
  7992. optionp (equal (char-after (match-beginning 0)) ?#)
  7993. cat (org-trim (match-string 2)))
  7994. (if optionp
  7995. (setq beg (point-at-bol) end (point-max))
  7996. (org-back-to-heading t)
  7997. (setq beg (point) end (org-end-of-subtree t t)))
  7998. (put-text-property beg end 'org-category cat)
  7999. (put-text-property beg end 'org-category-position beg)
  8000. (goto-char pos)))))))
  8001. (defun org-refresh-properties (dprop tprop)
  8002. "Refresh buffer text properties.
  8003. DPROP is the drawer property and TPROP is the corresponding text
  8004. property to set."
  8005. (let ((case-fold-search t)
  8006. (inhibit-read-only t) p)
  8007. (org-unmodified
  8008. (save-excursion
  8009. (save-restriction
  8010. (widen)
  8011. (goto-char (point-min))
  8012. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8013. (setq p (org-match-string-no-properties 1))
  8014. (save-excursion
  8015. (org-back-to-heading t)
  8016. (put-text-property
  8017. (point-at-bol) (point-at-eol) tprop p))))))))
  8018. ;;;; Link Stuff
  8019. ;;; Link abbreviations
  8020. (defun org-link-expand-abbrev (link)
  8021. "Apply replacements as defined in `org-link-abbrev-alist'."
  8022. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8023. (let* ((key (match-string 1 link))
  8024. (as (or (assoc key org-link-abbrev-alist-local)
  8025. (assoc key org-link-abbrev-alist)))
  8026. (tag (and (match-end 2) (match-string 3 link)))
  8027. rpl)
  8028. (if (not as)
  8029. link
  8030. (setq rpl (cdr as))
  8031. (cond
  8032. ((symbolp rpl) (funcall rpl tag))
  8033. ((string-match "%(\\([^)]+\\))" rpl)
  8034. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8035. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8036. ((string-match "%h" rpl)
  8037. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8038. (t (concat rpl tag)))))
  8039. link))
  8040. ;;; Storing and inserting links
  8041. (defvar org-insert-link-history nil
  8042. "Minibuffer history for links inserted with `org-insert-link'.")
  8043. (defvar org-stored-links nil
  8044. "Contains the links stored with `org-store-link'.")
  8045. (defvar org-store-link-plist nil
  8046. "Plist with info about the most recently link created with `org-store-link'.")
  8047. (defvar org-link-protocols nil
  8048. "Link protocols added to Org-mode using `org-add-link-type'.")
  8049. (defvar org-store-link-functions nil
  8050. "List of functions that are called to create and store a link.
  8051. Each function will be called in turn until one returns a non-nil
  8052. value. Each function should check if it is responsible for creating
  8053. this link (for example by looking at the major mode).
  8054. If not, it must exit and return nil.
  8055. If yes, it should return a non-nil value after a calling
  8056. `org-store-link-props' with a list of properties and values.
  8057. Special properties are:
  8058. :type The link prefix, like \"http\". This must be given.
  8059. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8060. This is obligatory as well.
  8061. :description Optional default description for the second pair
  8062. of brackets in an Org-mode link. The user can still change
  8063. this when inserting this link into an Org-mode buffer.
  8064. In addition to these, any additional properties can be specified
  8065. and then used in capture templates.")
  8066. (defun org-add-link-type (type &optional follow export)
  8067. "Add TYPE to the list of `org-link-types'.
  8068. Re-compute all regular expressions depending on `org-link-types'
  8069. FOLLOW and EXPORT are two functions.
  8070. FOLLOW should take the link path as the single argument and do whatever
  8071. is necessary to follow the link, for example find a file or display
  8072. a mail message.
  8073. EXPORT should format the link path for export to one of the export formats.
  8074. It should be a function accepting three arguments:
  8075. path the path of the link, the text after the prefix (like \"http:\")
  8076. desc the description of the link, if any, or a description added by
  8077. org-export-normalize-links if there is none
  8078. format the export format, a symbol like `html' or `latex' or `ascii'..
  8079. The function may use the FORMAT information to return different values
  8080. depending on the format. The return value will be put literally into
  8081. the exported file. If the return value is nil, this means Org should
  8082. do what it normally does with links which do not have EXPORT defined.
  8083. Org-mode has a built-in default for exporting links. If you are happy with
  8084. this default, there is no need to define an export function for the link
  8085. type. For a simple example of an export function, see `org-bbdb.el'."
  8086. (add-to-list 'org-link-types type t)
  8087. (org-make-link-regexps)
  8088. (if (assoc type org-link-protocols)
  8089. (setcdr (assoc type org-link-protocols) (list follow export))
  8090. (push (list type follow export) org-link-protocols)))
  8091. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8092. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8093. ;;;###autoload
  8094. (defun org-store-link (arg)
  8095. "\\<org-mode-map>Store an org-link to the current location.
  8096. This link is added to `org-stored-links' and can later be inserted
  8097. into an org-buffer with \\[org-insert-link].
  8098. For some link types, a prefix arg is interpreted.
  8099. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8100. For file links, arg negates `org-context-in-file-links'.
  8101. A double prefix arg force skipping storing functions that are not
  8102. part of Org's core."
  8103. (interactive "P")
  8104. (org-load-modules-maybe)
  8105. (setq org-store-link-plist nil) ; reset
  8106. (org-with-limited-levels
  8107. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8108. (cond
  8109. ((and (not (equal arg '(16)))
  8110. (setq sfuns
  8111. (delq
  8112. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8113. org-store-link-functions))
  8114. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8115. (or (and (cdr sfuns)
  8116. (funcall (intern
  8117. (completing-read "Which function for creating the link? "
  8118. sfunsn t (car sfunsn)))))
  8119. (funcall (caar sfuns)))
  8120. (setq link (plist-get org-store-link-plist :link)
  8121. desc (or (plist-get org-store-link-plist :description) link))))
  8122. ((org-src-edit-buffer-p)
  8123. (let (label gc)
  8124. (while (or (not label)
  8125. (save-excursion
  8126. (save-restriction
  8127. (widen)
  8128. (goto-char (point-min))
  8129. (re-search-forward
  8130. (regexp-quote (format org-coderef-label-format label))
  8131. nil t))))
  8132. (when label (message "Label exists already") (sit-for 2))
  8133. (setq label (read-string "Code line label: " label)))
  8134. (end-of-line 1)
  8135. (setq link (format org-coderef-label-format label))
  8136. (setq gc (- 79 (length link)))
  8137. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8138. (insert link)
  8139. (setq link (concat "(" label ")") desc nil)))
  8140. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8141. ;; We are in the agenda, link to referenced location
  8142. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8143. (get-text-property (point) 'org-marker))))
  8144. (when m
  8145. (org-with-point-at m
  8146. (setq agenda-link
  8147. (if (org-called-interactively-p 'any)
  8148. (call-interactively 'org-store-link)
  8149. (org-store-link nil)))))))
  8150. ((eq major-mode 'calendar-mode)
  8151. (let ((cd (calendar-cursor-to-date)))
  8152. (setq link
  8153. (format-time-string
  8154. (car org-time-stamp-formats)
  8155. (apply 'encode-time
  8156. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8157. nil nil nil))))
  8158. (org-store-link-props :type "calendar" :date cd)))
  8159. ((eq major-mode 'help-mode)
  8160. (setq link (concat "help:" (save-excursion
  8161. (goto-char (point-min))
  8162. (looking-at "^[^ ]+")
  8163. (match-string 0))))
  8164. (org-store-link-props :type "help"))
  8165. ((eq major-mode 'w3-mode)
  8166. (setq cpltxt (if (and (buffer-name)
  8167. (not (string-match "Untitled" (buffer-name))))
  8168. (buffer-name)
  8169. (url-view-url t))
  8170. link (url-view-url t))
  8171. (org-store-link-props :type "w3" :url (url-view-url t)))
  8172. ((setq search (run-hook-with-args-until-success
  8173. 'org-create-file-search-functions))
  8174. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8175. "::" search))
  8176. (setq cpltxt (or description link)))
  8177. ((eq major-mode 'image-mode)
  8178. (setq cpltxt (concat "file:"
  8179. (abbreviate-file-name buffer-file-name))
  8180. link cpltxt)
  8181. (org-store-link-props :type "image" :file buffer-file-name))
  8182. ((eq major-mode 'dired-mode)
  8183. ;; link to the file in the current line
  8184. (let ((file (dired-get-filename nil t)))
  8185. (setq file (if file
  8186. (abbreviate-file-name
  8187. (expand-file-name (dired-get-filename nil t)))
  8188. ;; otherwise, no file so use current directory.
  8189. default-directory))
  8190. (setq cpltxt (concat "file:" file)
  8191. link cpltxt)))
  8192. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8193. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8194. (cond
  8195. ((org-in-regexp "<<\\(.*?\\)>>")
  8196. (setq cpltxt
  8197. (concat "file:"
  8198. (abbreviate-file-name
  8199. (buffer-file-name (buffer-base-buffer)))
  8200. "::" (match-string 1))
  8201. link cpltxt))
  8202. ((and (featurep 'org-id)
  8203. (or (eq org-id-link-to-org-use-id t)
  8204. (and (org-called-interactively-p 'any)
  8205. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8206. (and (eq org-id-link-to-org-use-id
  8207. 'create-if-interactive-and-no-custom-id)
  8208. (not custom-id))))
  8209. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8210. ;; We can make a link using the ID.
  8211. (setq link (condition-case nil
  8212. (prog1 (org-id-store-link)
  8213. (setq desc (plist-get org-store-link-plist :description)))
  8214. (error
  8215. ;; probably before first headline, link to file only
  8216. (concat "file:"
  8217. (abbreviate-file-name
  8218. (buffer-file-name (buffer-base-buffer))))))))
  8219. (t
  8220. ;; Just link to current headline
  8221. (setq cpltxt (concat "file:"
  8222. (abbreviate-file-name
  8223. (buffer-file-name (buffer-base-buffer)))))
  8224. ;; Add a context search string
  8225. (when (org-xor org-context-in-file-links arg)
  8226. (let* ((ee (org-element-at-point))
  8227. (et (org-element-type ee))
  8228. (ev (plist-get (cadr ee) :value)))
  8229. (setq txt (cond
  8230. ((org-at-heading-p) nil)
  8231. ((eq et 'keyword) ev)
  8232. ((org-region-active-p)
  8233. (buffer-substring (region-beginning) (region-end)))))
  8234. (when (or (null txt) (string-match "\\S-" txt))
  8235. (setq cpltxt
  8236. (concat cpltxt "::"
  8237. (condition-case nil
  8238. (org-make-org-heading-search-string txt)
  8239. (error "")))
  8240. desc (or (and (eq et 'keyword) ev)
  8241. (nth 4 (ignore-errors (org-heading-components)))
  8242. "NONE")))))
  8243. (if (string-match "::\\'" cpltxt)
  8244. (setq cpltxt (substring cpltxt 0 -2)))
  8245. (setq link cpltxt))))
  8246. ((buffer-file-name (buffer-base-buffer))
  8247. ;; Just link to this file here.
  8248. (setq cpltxt (concat "file:"
  8249. (abbreviate-file-name
  8250. (buffer-file-name (buffer-base-buffer)))))
  8251. ;; Add a context string
  8252. (when (org-xor org-context-in-file-links arg)
  8253. (setq txt (if (org-region-active-p)
  8254. (buffer-substring (region-beginning) (region-end))
  8255. (buffer-substring (point-at-bol) (point-at-eol))))
  8256. ;; Only use search option if there is some text.
  8257. (when (string-match "\\S-" txt)
  8258. (setq cpltxt
  8259. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8260. desc "NONE")))
  8261. (setq link cpltxt))
  8262. ((org-called-interactively-p 'interactive)
  8263. (user-error "No method for storing a link from this buffer"))
  8264. (t (setq link nil)))
  8265. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8266. (setq link (or link cpltxt)
  8267. desc (or desc cpltxt))
  8268. (cond ((equal desc "NONE") (setq desc nil))
  8269. ((string-match org-bracket-link-regexp desc)
  8270. (setq desc (replace-regexp-in-string
  8271. org-bracket-link-regexp
  8272. (concat "\\3" (if (equal (length (match-string 0 desc))
  8273. (length desc)) "*" "")) desc))))
  8274. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8275. (progn
  8276. (setq org-stored-links
  8277. (cons (list link desc) org-stored-links))
  8278. (message "Stored: %s" (or desc link))
  8279. (when custom-id
  8280. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8281. "::#" custom-id))
  8282. (setq org-stored-links
  8283. (cons (list link desc) org-stored-links))))
  8284. (or agenda-link (and link (org-make-link-string link desc)))))))
  8285. (defun org-store-link-props (&rest plist)
  8286. "Store link properties, extract names and addresses."
  8287. (let (x adr)
  8288. (when (setq x (plist-get plist :from))
  8289. (setq adr (mail-extract-address-components x))
  8290. (setq plist (plist-put plist :fromname (car adr)))
  8291. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8292. (when (setq x (plist-get plist :to))
  8293. (setq adr (mail-extract-address-components x))
  8294. (setq plist (plist-put plist :toname (car adr)))
  8295. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8296. (let ((from (plist-get plist :from))
  8297. (to (plist-get plist :to)))
  8298. (when (and from to org-from-is-user-regexp)
  8299. (setq plist
  8300. (plist-put plist :fromto
  8301. (if (string-match org-from-is-user-regexp from)
  8302. (concat "to %t")
  8303. (concat "from %f"))))))
  8304. (setq org-store-link-plist plist))
  8305. (defun org-add-link-props (&rest plist)
  8306. "Add these properties to the link property list."
  8307. (let (key value)
  8308. (while plist
  8309. (setq key (pop plist) value (pop plist))
  8310. (setq org-store-link-plist
  8311. (plist-put org-store-link-plist key value)))))
  8312. (defun org-email-link-description (&optional fmt)
  8313. "Return the description part of an email link.
  8314. This takes information from `org-store-link-plist' and formats it
  8315. according to FMT (default from `org-email-link-description-format')."
  8316. (setq fmt (or fmt org-email-link-description-format))
  8317. (let* ((p org-store-link-plist)
  8318. (to (plist-get p :toaddress))
  8319. (from (plist-get p :fromaddress))
  8320. (table
  8321. (list
  8322. (cons "%c" (plist-get p :fromto))
  8323. (cons "%F" (plist-get p :from))
  8324. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8325. (cons "%T" (plist-get p :to))
  8326. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8327. (cons "%s" (plist-get p :subject))
  8328. (cons "%d" (plist-get p :date))
  8329. (cons "%m" (plist-get p :message-id)))))
  8330. (when (string-match "%c" fmt)
  8331. ;; Check if the user wrote this message
  8332. (if (and org-from-is-user-regexp from to
  8333. (save-match-data (string-match org-from-is-user-regexp from)))
  8334. (setq fmt (replace-match "to %t" t t fmt))
  8335. (setq fmt (replace-match "from %f" t t fmt))))
  8336. (org-replace-escapes fmt table)))
  8337. (defun org-make-org-heading-search-string (&optional string)
  8338. "Make search string for the current headline or STRING."
  8339. (let ((s (or string
  8340. (and (derived-mode-p 'org-mode)
  8341. (save-excursion
  8342. (org-back-to-heading t)
  8343. (org-element-property :raw-value (org-element-at-point))))))
  8344. (lines org-context-in-file-links))
  8345. (or string (setq s (concat "*" s))) ; Add * for headlines
  8346. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8347. (when (and string (integerp lines) (> lines 0))
  8348. (let ((slines (org-split-string s "\n")))
  8349. (when (< lines (length slines))
  8350. (setq s (mapconcat
  8351. 'identity
  8352. (reverse (nthcdr (- (length slines) lines)
  8353. (reverse slines))) "\n")))))
  8354. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8355. (defun org-make-link-string (link &optional description)
  8356. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8357. (unless (string-match "\\S-" link)
  8358. (error "Empty link"))
  8359. (when (and description
  8360. (stringp description)
  8361. (not (string-match "\\S-" description)))
  8362. (setq description nil))
  8363. (when (stringp description)
  8364. ;; Remove brackets from the description, they are fatal.
  8365. (while (string-match "\\[" description)
  8366. (setq description (replace-match "{" t t description)))
  8367. (while (string-match "\\]" description)
  8368. (setq description (replace-match "}" t t description))))
  8369. (when (equal link description)
  8370. ;; No description needed, it is identical
  8371. (setq description nil))
  8372. (when (and (not description)
  8373. (not (string-match (org-image-file-name-regexp) link))
  8374. (not (equal link (org-link-escape link))))
  8375. (setq description (org-extract-attributes link)))
  8376. (setq link
  8377. (cond ((string-match (org-image-file-name-regexp) link) link)
  8378. ((string-match org-link-types-re link)
  8379. (concat (match-string 1 link)
  8380. (org-link-escape (substring link (match-end 1)))))
  8381. (t (org-link-escape link))))
  8382. (concat "[[" link "]"
  8383. (if description (concat "[" description "]") "")
  8384. "]"))
  8385. (defconst org-link-escape-chars
  8386. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8387. "List of characters that should be escaped in link.
  8388. This is the list that is used for internal purposes.")
  8389. (defconst org-link-escape-chars-browser
  8390. '(?\ )
  8391. "List of escapes for characters that are problematic in links.
  8392. This is the list that is used before handing over to the browser.")
  8393. (defun org-link-escape (text &optional table merge)
  8394. "Return percent escaped representation of TEXT.
  8395. TEXT is a string with the text to escape.
  8396. Optional argument TABLE is a list with characters that should be
  8397. escaped. When nil, `org-link-escape-chars' is used.
  8398. If optional argument MERGE is set, merge TABLE into
  8399. `org-link-escape-chars'."
  8400. (cond
  8401. ((and table merge)
  8402. (mapc (lambda (defchr)
  8403. (unless (member defchr table)
  8404. (setq table (cons defchr table)))) org-link-escape-chars))
  8405. ((null table)
  8406. (setq table org-link-escape-chars)))
  8407. (mapconcat
  8408. (lambda (char)
  8409. (if (or (member char table)
  8410. (and (or (< char 32) (= char 37) (> char 126))
  8411. org-url-hexify-p))
  8412. (mapconcat (lambda (sequence-element)
  8413. (format "%%%.2X" sequence-element))
  8414. (or (encode-coding-char char 'utf-8)
  8415. (error "Unable to percent escape character: %s"
  8416. (char-to-string char))) "")
  8417. (char-to-string char))) text ""))
  8418. (defun org-link-unescape (str)
  8419. "Unhex hexified Unicode strings as returned from the JavaScript function
  8420. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8421. (unless (and (null str) (string= "" str))
  8422. (let ((pos 0) (case-fold-search t) unhexed)
  8423. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8424. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8425. (setq str (replace-match unhexed t t str))
  8426. (setq pos (+ pos (length unhexed))))))
  8427. str)
  8428. (defun org-link-unescape-compound (hex)
  8429. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8430. Note: this function also decodes single byte encodings like
  8431. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8432. (save-match-data
  8433. (let* ((bytes (cdr (split-string hex "%")))
  8434. (ret "")
  8435. (eat 0)
  8436. (sum 0))
  8437. (while bytes
  8438. (let* ((val (string-to-number (pop bytes) 16))
  8439. (shift-xor
  8440. (if (= 0 eat)
  8441. (cond
  8442. ((>= val 252) (cons 6 252))
  8443. ((>= val 248) (cons 5 248))
  8444. ((>= val 240) (cons 4 240))
  8445. ((>= val 224) (cons 3 224))
  8446. ((>= val 192) (cons 2 192))
  8447. (t (cons 0 0)))
  8448. (cons 6 128))))
  8449. (if (>= val 192) (setq eat (car shift-xor)))
  8450. (setq val (logxor val (cdr shift-xor)))
  8451. (setq sum (+ (lsh sum (car shift-xor)) val))
  8452. (if (> eat 0) (setq eat (- eat 1)))
  8453. (cond
  8454. ((= 0 eat) ;multi byte
  8455. (setq ret (concat ret (org-char-to-string sum)))
  8456. (setq sum 0))
  8457. ((not bytes) ; single byte(s)
  8458. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8459. )) ;; end (while bytes
  8460. ret )))
  8461. (defun org-link-unescape-single-byte-sequence (hex)
  8462. "Unhexify hex-encoded single byte character sequences."
  8463. (mapconcat (lambda (byte)
  8464. (char-to-string (string-to-number byte 16)))
  8465. (cdr (split-string hex "%")) ""))
  8466. (defun org-xor (a b)
  8467. "Exclusive or."
  8468. (if a (not b) b))
  8469. (defun org-fixup-message-id-for-http (s)
  8470. "Replace special characters in a message id, so it can be used in an http query."
  8471. (when (string-match "%" s)
  8472. (setq s (mapconcat (lambda (c)
  8473. (if (eq c ?%)
  8474. "%25"
  8475. (char-to-string c)))
  8476. s "")))
  8477. (while (string-match "<" s)
  8478. (setq s (replace-match "%3C" t t s)))
  8479. (while (string-match ">" s)
  8480. (setq s (replace-match "%3E" t t s)))
  8481. (while (string-match "@" s)
  8482. (setq s (replace-match "%40" t t s)))
  8483. s)
  8484. (defun org-link-prettify (link)
  8485. "Return a human-readable representation of LINK.
  8486. The car of LINK must be a raw link the cdr of LINK must be either
  8487. a link description or nil."
  8488. (let ((desc (or (cadr link) "<no description>")))
  8489. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8490. "<" (car link) ">")))
  8491. ;;;###autoload
  8492. (defun org-insert-link-global ()
  8493. "Insert a link like Org-mode does.
  8494. This command can be called in any mode to insert a link in Org-mode syntax."
  8495. (interactive)
  8496. (org-load-modules-maybe)
  8497. (org-run-like-in-org-mode 'org-insert-link))
  8498. (defun org-insert-all-links (&optional keep)
  8499. "Insert all links in `org-stored-links'."
  8500. (interactive "P")
  8501. (let ((links (copy-sequence org-stored-links)) l)
  8502. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8503. (insert "- ")
  8504. (org-insert-link nil (car l) (cadr l))
  8505. (insert "\n"))))
  8506. (defun org-link-fontify-links-to-this-file ()
  8507. "Fontify links to the current file in `org-stored-links'."
  8508. (let ((f (buffer-file-name)) a b)
  8509. (setq a (mapcar (lambda(l)
  8510. (let ((ll (car l)))
  8511. (when (and (string-match "^file:\\(.+\\)::" ll)
  8512. (equal f (expand-file-name (match-string 1 ll))))
  8513. ll)))
  8514. org-stored-links))
  8515. (when (featurep 'org-id)
  8516. (setq b (mapcar (lambda(l)
  8517. (let ((ll (car l)))
  8518. (when (and (string-match "^id:\\(.+\\)$" ll)
  8519. (equal f (expand-file-name
  8520. (or (org-id-find-id-file
  8521. (match-string 1 ll)) ""))))
  8522. ll)))
  8523. org-stored-links)))
  8524. (mapcar (lambda(l)
  8525. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8526. (delq nil (append a b)))))
  8527. (defvar org-link-links-in-this-file nil)
  8528. (defun org-insert-link (&optional complete-file link-location default-description)
  8529. "Insert a link. At the prompt, enter the link.
  8530. Completion can be used to insert any of the link protocol prefixes like
  8531. http or ftp in use.
  8532. The history can be used to select a link previously stored with
  8533. `org-store-link'. When the empty string is entered (i.e. if you just
  8534. press RET at the prompt), the link defaults to the most recently
  8535. stored link. As SPC triggers completion in the minibuffer, you need to
  8536. use M-SPC or C-q SPC to force the insertion of a space character.
  8537. You will also be prompted for a description, and if one is given, it will
  8538. be displayed in the buffer instead of the link.
  8539. If there is already a link at point, this command will allow you to edit link
  8540. and description parts.
  8541. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8542. be selected using completion. The path to the file will be relative to the
  8543. current directory if the file is in the current directory or a subdirectory.
  8544. Otherwise, the link will be the absolute path as completed in the minibuffer
  8545. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8546. option `org-link-file-path-type'.
  8547. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8548. the current directory or below.
  8549. With three \\[universal-argument] prefixes, negate the meaning of
  8550. `org-keep-stored-link-after-insertion'.
  8551. If `org-make-link-description-function' is non-nil, this function will be
  8552. called with the link target, and the result will be the default
  8553. link description.
  8554. If the LINK-LOCATION parameter is non-nil, this value will be
  8555. used as the link location instead of reading one interactively.
  8556. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8557. be used as the default description."
  8558. (interactive "P")
  8559. (let* ((wcf (current-window-configuration))
  8560. (origbuf (current-buffer))
  8561. (region (if (org-region-active-p)
  8562. (buffer-substring (region-beginning) (region-end))))
  8563. (remove (and region (list (region-beginning) (region-end))))
  8564. (desc region)
  8565. tmphist ; byte-compile incorrectly complains about this
  8566. (link link-location)
  8567. (abbrevs org-link-abbrev-alist-local)
  8568. entry file all-prefixes auto-desc)
  8569. (cond
  8570. (link-location) ; specified by arg, just use it.
  8571. ((org-in-regexp org-bracket-link-regexp 1)
  8572. ;; We do have a link at point, and we are going to edit it.
  8573. (setq remove (list (match-beginning 0) (match-end 0)))
  8574. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8575. (setq link (read-string "Link: "
  8576. (org-link-unescape
  8577. (org-match-string-no-properties 1)))))
  8578. ((or (org-in-regexp org-angle-link-re)
  8579. (org-in-regexp org-plain-link-re))
  8580. ;; Convert to bracket link
  8581. (setq remove (list (match-beginning 0) (match-end 0))
  8582. link (read-string "Link: "
  8583. (org-remove-angle-brackets (match-string 0)))))
  8584. ((member complete-file '((4) (16)))
  8585. ;; Completing read for file names.
  8586. (setq link (org-file-complete-link complete-file)))
  8587. (t
  8588. ;; Read link, with completion for stored links.
  8589. (org-link-fontify-links-to-this-file)
  8590. (org-switch-to-buffer-other-window "*Org Links*")
  8591. (with-current-buffer "*Org Links*"
  8592. (erase-buffer)
  8593. (insert "Insert a link.
  8594. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8595. (when org-stored-links
  8596. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8597. (insert (mapconcat 'org-link-prettify
  8598. (reverse org-stored-links) "\n")))
  8599. (goto-char (point-min)))
  8600. (let ((cw (selected-window)))
  8601. (select-window (get-buffer-window "*Org Links*" 'visible))
  8602. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8603. (unless (pos-visible-in-window-p (point-max))
  8604. (org-fit-window-to-buffer))
  8605. (and (window-live-p cw) (select-window cw)))
  8606. ;; Fake a link history, containing the stored links.
  8607. (setq tmphist (append (mapcar 'car org-stored-links)
  8608. org-insert-link-history))
  8609. (setq all-prefixes (append (mapcar 'car abbrevs)
  8610. (mapcar 'car org-link-abbrev-alist)
  8611. org-link-types))
  8612. (unwind-protect
  8613. (progn
  8614. (setq link
  8615. (org-completing-read
  8616. "Link: "
  8617. (append
  8618. (mapcar (lambda (x) (concat x ":"))
  8619. all-prefixes)
  8620. (mapcar 'car org-stored-links))
  8621. nil nil nil
  8622. 'tmphist
  8623. (caar org-stored-links)))
  8624. (if (not (string-match "\\S-" link))
  8625. (error "No link selected"))
  8626. (mapc (lambda(l)
  8627. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8628. org-stored-links)
  8629. (if (or (member link all-prefixes)
  8630. (and (equal ":" (substring link -1))
  8631. (member (substring link 0 -1) all-prefixes)
  8632. (setq link (substring link 0 -1))))
  8633. (setq link (with-current-buffer origbuf
  8634. (org-link-try-special-completion link)))))
  8635. (set-window-configuration wcf)
  8636. (kill-buffer "*Org Links*"))
  8637. (setq entry (assoc link org-stored-links))
  8638. (or entry (push link org-insert-link-history))
  8639. (setq desc (or desc (nth 1 entry)))))
  8640. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8641. (not org-keep-stored-link-after-insertion))
  8642. (setq org-stored-links (delq (assoc link org-stored-links)
  8643. org-stored-links)))
  8644. (if (string-match org-plain-link-re link)
  8645. ;; URL-like link, normalize the use of angular brackets.
  8646. (setq link (org-remove-angle-brackets link)))
  8647. ;; Check if we are linking to the current file with a search
  8648. ;; option If yes, simplify the link by using only the search
  8649. ;; option.
  8650. (when (and buffer-file-name
  8651. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8652. (let* ((path (match-string 1 link))
  8653. (case-fold-search nil)
  8654. (search (match-string 2 link)))
  8655. (save-match-data
  8656. (if (equal (file-truename buffer-file-name) (file-truename path))
  8657. ;; We are linking to this same file, with a search option
  8658. (setq link search)))))
  8659. ;; Check if we can/should use a relative path. If yes, simplify the link
  8660. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8661. (let* ((type (match-string 1 link))
  8662. (path (match-string 2 link))
  8663. (origpath path)
  8664. (case-fold-search nil))
  8665. (cond
  8666. ((or (eq org-link-file-path-type 'absolute)
  8667. (equal complete-file '(16)))
  8668. (setq path (abbreviate-file-name (expand-file-name path))))
  8669. ((eq org-link-file-path-type 'noabbrev)
  8670. (setq path (expand-file-name path)))
  8671. ((eq org-link-file-path-type 'relative)
  8672. (setq path (file-relative-name path)))
  8673. (t
  8674. (save-match-data
  8675. (if (string-match (concat "^" (regexp-quote
  8676. (expand-file-name
  8677. (file-name-as-directory
  8678. default-directory))))
  8679. (expand-file-name path))
  8680. ;; We are linking a file with relative path name.
  8681. (setq path (substring (expand-file-name path)
  8682. (match-end 0)))
  8683. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8684. (setq link (concat type path))
  8685. (if (equal desc origpath)
  8686. (setq desc path))))
  8687. (if org-make-link-description-function
  8688. (setq desc
  8689. (or (condition-case nil
  8690. (funcall org-make-link-description-function link desc)
  8691. (error (progn (message "Can't get link description from `%s'"
  8692. (symbol-name org-make-link-description-function))
  8693. (sit-for 2) nil)))
  8694. (read-string "Description: " default-description)))
  8695. (if default-description (setq desc default-description)
  8696. (setq desc (or (and auto-desc desc)
  8697. (read-string "Description: " desc)))))
  8698. (unless (string-match "\\S-" desc) (setq desc nil))
  8699. (if remove (apply 'delete-region remove))
  8700. (insert (org-make-link-string link desc))))
  8701. (defun org-link-try-special-completion (type)
  8702. "If there is completion support for link type TYPE, offer it."
  8703. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8704. (if (functionp fun)
  8705. (funcall fun)
  8706. (read-string "Link (no completion support): " (concat type ":")))))
  8707. (defun org-file-complete-link (&optional arg)
  8708. "Create a file link using completion."
  8709. (let (file link)
  8710. (setq file (org-iread-file-name "File: "))
  8711. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8712. (pwd1 (file-name-as-directory (abbreviate-file-name
  8713. (expand-file-name ".")))))
  8714. (cond
  8715. ((equal arg '(16))
  8716. (setq link (concat
  8717. "file:"
  8718. (abbreviate-file-name (expand-file-name file)))))
  8719. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8720. (setq link (concat "file:" (match-string 1 file))))
  8721. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8722. (expand-file-name file))
  8723. (setq link (concat
  8724. "file:" (match-string 1 (expand-file-name file)))))
  8725. (t (setq link (concat "file:" file)))))
  8726. link))
  8727. (defun org-iread-file-name (&rest args)
  8728. "Read-file-name using `ido-mode' speedup if available.
  8729. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8730. See `read-file-name' for a description of parameters."
  8731. (org-without-partial-completion
  8732. (if (and org-completion-use-ido
  8733. (fboundp 'ido-read-file-name)
  8734. (boundp 'ido-mode) ido-mode
  8735. (listp (second args)))
  8736. (let ((ido-enter-matching-directory nil))
  8737. (apply 'ido-read-file-name args))
  8738. (apply 'read-file-name args))))
  8739. (defun org-completing-read (&rest args)
  8740. "Completing-read with SPACE being a normal character."
  8741. (let ((enable-recursive-minibuffers t)
  8742. (minibuffer-local-completion-map
  8743. (copy-keymap minibuffer-local-completion-map)))
  8744. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8745. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8746. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8747. (apply 'org-icompleting-read args)))
  8748. (defun org-completing-read-no-i (&rest args)
  8749. (let (org-completion-use-ido org-completion-use-iswitchb)
  8750. (apply 'org-completing-read args)))
  8751. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8752. "Use iswitch as a completing-read replacement to choose from choices.
  8753. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8754. from."
  8755. (let* ((iswitchb-use-virtual-buffers nil)
  8756. (iswitchb-make-buflist-hook
  8757. (lambda ()
  8758. (setq iswitchb-temp-buflist choices))))
  8759. (iswitchb-read-buffer prompt)))
  8760. (defun org-icompleting-read (&rest args)
  8761. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8762. (org-without-partial-completion
  8763. (if (and org-completion-use-ido
  8764. (fboundp 'ido-completing-read)
  8765. (boundp 'ido-mode) ido-mode
  8766. (listp (second args)))
  8767. (let ((ido-enter-matching-directory nil))
  8768. (apply 'ido-completing-read (concat (car args))
  8769. (if (consp (car (nth 1 args)))
  8770. (mapcar 'car (nth 1 args))
  8771. (nth 1 args))
  8772. (cddr args)))
  8773. (if (and org-completion-use-iswitchb
  8774. (boundp 'iswitchb-mode) iswitchb-mode
  8775. (listp (second args)))
  8776. (apply 'org-iswitchb-completing-read (concat (car args))
  8777. (if (consp (car (nth 1 args)))
  8778. (mapcar 'car (nth 1 args))
  8779. (nth 1 args))
  8780. (cddr args))
  8781. (apply 'completing-read args)))))
  8782. (defun org-extract-attributes (s)
  8783. "Extract the attributes cookie from a string and set as text property."
  8784. (let (a attr (start 0) key value)
  8785. (save-match-data
  8786. (when (string-match "{{\\([^}]+\\)}}$" s)
  8787. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8788. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8789. (setq key (match-string 1 a) value (match-string 2 a)
  8790. start (match-end 0)
  8791. attr (plist-put attr (intern key) value))))
  8792. (org-add-props s nil 'org-attr attr))
  8793. s))
  8794. (defun org-extract-attributes-from-string (tag)
  8795. (let (key value attr)
  8796. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8797. (setq key (match-string 1 tag) value (match-string 2 tag)
  8798. tag (replace-match "" t t tag)
  8799. attr (plist-put attr (intern key) value)))
  8800. (cons tag attr)))
  8801. (defun org-attributes-to-string (plist)
  8802. "Format a property list into an HTML attribute list."
  8803. (let ((s "") key value)
  8804. (while plist
  8805. (setq key (pop plist) value (pop plist))
  8806. (and value
  8807. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8808. s))
  8809. ;;; Opening/following a link
  8810. (defvar org-link-search-failed nil)
  8811. (defvar org-open-link-functions nil
  8812. "Hook for functions finding a plain text link.
  8813. These functions must take a single argument, the link content.
  8814. They will be called for links that look like [[link text][description]]
  8815. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8816. like a filename (e.g. \"./blue.png\").
  8817. These functions will be called *before* Org attempts to resolve the
  8818. link by doing text searches in the current buffer - so if you want a
  8819. link \"[[target]]\" to still find \"<<target>>\", your function should
  8820. handle this as a special case.
  8821. When the function does handle the link, it must return a non-nil value.
  8822. If it decides that it is not responsible for this link, it must return
  8823. nil to indicate that that Org-mode can continue with other options
  8824. like exact and fuzzy text search.")
  8825. (defun org-next-link ()
  8826. "Move forward to the next link.
  8827. If the link is in hidden text, expose it."
  8828. (interactive)
  8829. (when (and org-link-search-failed (eq this-command last-command))
  8830. (goto-char (point-min))
  8831. (message "Link search wrapped back to beginning of buffer"))
  8832. (setq org-link-search-failed nil)
  8833. (let* ((pos (point))
  8834. (ct (org-context))
  8835. (a (assoc :link ct)))
  8836. (if a (goto-char (nth 2 a)))
  8837. (if (re-search-forward org-any-link-re nil t)
  8838. (progn
  8839. (goto-char (match-beginning 0))
  8840. (if (outline-invisible-p) (org-show-context)))
  8841. (goto-char pos)
  8842. (setq org-link-search-failed t)
  8843. (error "No further link found"))))
  8844. (defun org-previous-link ()
  8845. "Move backward to the previous link.
  8846. If the link is in hidden text, expose it."
  8847. (interactive)
  8848. (when (and org-link-search-failed (eq this-command last-command))
  8849. (goto-char (point-max))
  8850. (message "Link search wrapped back to end of buffer"))
  8851. (setq org-link-search-failed nil)
  8852. (let* ((pos (point))
  8853. (ct (org-context))
  8854. (a (assoc :link ct)))
  8855. (if a (goto-char (nth 1 a)))
  8856. (if (re-search-backward org-any-link-re nil t)
  8857. (progn
  8858. (goto-char (match-beginning 0))
  8859. (if (outline-invisible-p) (org-show-context)))
  8860. (goto-char pos)
  8861. (setq org-link-search-failed t)
  8862. (error "No further link found"))))
  8863. (defun org-translate-link (s)
  8864. "Translate a link string if a translation function has been defined."
  8865. (if (and org-link-translation-function
  8866. (fboundp org-link-translation-function)
  8867. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8868. (progn
  8869. (setq s (funcall org-link-translation-function
  8870. (match-string 1 s) (match-string 2 s)))
  8871. (concat (car s) ":" (cdr s)))
  8872. s))
  8873. (defun org-translate-link-from-planner (type path)
  8874. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8875. This is still an experimental function, your mileage may vary."
  8876. (cond
  8877. ((member type '("http" "https" "news" "ftp"))
  8878. ;; standard Internet links are the same.
  8879. nil)
  8880. ((and (equal type "irc") (string-match "^//" path))
  8881. ;; Planner has two / at the beginning of an irc link, we have 1.
  8882. ;; We should have zero, actually....
  8883. (setq path (substring path 1)))
  8884. ((and (equal type "lisp") (string-match "^/" path))
  8885. ;; Planner has a slash, we do not.
  8886. (setq type "elisp" path (substring path 1)))
  8887. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8888. ;; A typical message link. Planner has the id after the final slash,
  8889. ;; we separate it with a hash mark
  8890. (setq path (concat (match-string 1 path) "#"
  8891. (org-remove-angle-brackets (match-string 2 path)))))
  8892. )
  8893. (cons type path))
  8894. (defun org-find-file-at-mouse (ev)
  8895. "Open file link or URL at mouse."
  8896. (interactive "e")
  8897. (mouse-set-point ev)
  8898. (org-open-at-point 'in-emacs))
  8899. (defun org-open-at-mouse (ev)
  8900. "Open file link or URL at mouse.
  8901. See the docstring of `org-open-file' for details."
  8902. (interactive "e")
  8903. (mouse-set-point ev)
  8904. (if (eq major-mode 'org-agenda-mode)
  8905. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8906. (org-open-at-point))
  8907. (defvar org-window-config-before-follow-link nil
  8908. "The window configuration before following a link.
  8909. This is saved in case the need arises to restore it.")
  8910. (defvar org-open-link-marker (make-marker)
  8911. "Marker pointing to the location where `org-open-at-point; was called.")
  8912. ;;;###autoload
  8913. (defun org-open-at-point-global ()
  8914. "Follow a link like Org-mode does.
  8915. This command can be called in any mode to follow a link that has
  8916. Org-mode syntax."
  8917. (interactive)
  8918. (org-run-like-in-org-mode 'org-open-at-point))
  8919. ;;;###autoload
  8920. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8921. "Open a link in the string S, as if it was in Org-mode."
  8922. (interactive "sLink: \nP")
  8923. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8924. (with-temp-buffer
  8925. (let ((org-inhibit-startup (not reference-buffer)))
  8926. (org-mode)
  8927. (insert s)
  8928. (goto-char (point-min))
  8929. (when reference-buffer
  8930. (setq org-link-abbrev-alist-local
  8931. (with-current-buffer reference-buffer
  8932. org-link-abbrev-alist-local)))
  8933. (org-open-at-point arg reference-buffer)))))
  8934. (defvar org-open-at-point-functions nil
  8935. "Hook that is run when following a link at point.
  8936. Functions in this hook must return t if they identify and follow
  8937. a link at point. If they don't find anything interesting at point,
  8938. they must return nil.")
  8939. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8940. (defun org-open-at-point (&optional arg reference-buffer)
  8941. "Open link at or after point.
  8942. If there is no link at point, this function will search forward up to
  8943. the end of the current line.
  8944. Normally, files will be opened by an appropriate application. If the
  8945. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8946. With a double prefix argument, try to open outside of Emacs, in the
  8947. application the system uses for this file type."
  8948. (interactive "P")
  8949. ;; if in a code block, then open the block's results
  8950. (unless (call-interactively #'org-babel-open-src-block-result)
  8951. (org-load-modules-maybe)
  8952. (move-marker org-open-link-marker (point))
  8953. (setq org-window-config-before-follow-link (current-window-configuration))
  8954. (org-remove-occur-highlights nil nil t)
  8955. (cond
  8956. ((and (org-at-heading-p)
  8957. (not (org-at-timestamp-p t))
  8958. (not (org-in-regexp
  8959. (concat org-plain-link-re "\\|"
  8960. org-bracket-link-regexp "\\|"
  8961. org-angle-link-re "\\|"
  8962. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8963. (not (get-text-property (point) 'org-linked-text)))
  8964. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8965. (lk0 (car lkall))
  8966. (lk (if (stringp lk0) (list lk0) lk0))
  8967. (lkend (cdr lkall)))
  8968. (mapcar (lambda(l)
  8969. (search-forward l nil lkend)
  8970. (goto-char (match-beginning 0))
  8971. (org-open-at-point))
  8972. lk))
  8973. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8974. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8975. ((and (org-at-timestamp-p t)
  8976. (not (org-in-regexp org-bracket-link-regexp)))
  8977. (org-follow-timestamp-link))
  8978. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8979. (not (org-in-regexp org-any-link-re)))
  8980. (org-footnote-action))
  8981. (t
  8982. (let (type path link line search (pos (point)))
  8983. (catch 'match
  8984. (save-excursion
  8985. (skip-chars-forward "^]\n\r")
  8986. (when (org-in-regexp org-bracket-link-regexp 1)
  8987. (setq link (org-extract-attributes
  8988. (org-link-unescape (org-match-string-no-properties 1))))
  8989. (while (string-match " *\n *" link)
  8990. (setq link (replace-match " " t t link)))
  8991. (setq link (org-link-expand-abbrev link))
  8992. (cond
  8993. ((or (file-name-absolute-p link)
  8994. (string-match "^\\.\\.?/" link))
  8995. (setq type "file" path link))
  8996. ((string-match org-link-re-with-space3 link)
  8997. (setq type (match-string 1 link) path (match-string 2 link)))
  8998. ((string-match "^help:+\\(.+\\)" link)
  8999. (setq type "help" path (match-string 1 link)))
  9000. (t (setq type "thisfile" path link)))
  9001. (throw 'match t)))
  9002. (when (get-text-property (point) 'org-linked-text)
  9003. (setq type "thisfile"
  9004. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9005. (1+ (point)) (point))
  9006. path (buffer-substring
  9007. (or (previous-single-property-change pos 'org-linked-text)
  9008. (point-min))
  9009. (or (next-single-property-change pos 'org-linked-text)
  9010. (point-max))))
  9011. (throw 'match t))
  9012. (save-excursion
  9013. (when (or (org-in-regexp org-angle-link-re)
  9014. (let ((match (org-in-regexp org-plain-link-re)))
  9015. ;; Check a plain link is not within a bracket link
  9016. (and match
  9017. (save-excursion
  9018. (progn
  9019. (goto-char (car match))
  9020. (not (org-in-regexp org-bracket-link-regexp))))))
  9021. (let ((line_ending (save-excursion (end-of-line) (point))))
  9022. ;; We are in a line before a plain or bracket link
  9023. (or (re-search-forward org-plain-link-re line_ending t)
  9024. (re-search-forward org-bracket-link-regexp line_ending t))))
  9025. (setq type (match-string 1)
  9026. path (org-link-unescape (match-string 2)))
  9027. (throw 'match t)))
  9028. (save-excursion
  9029. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9030. (setq type "tags"
  9031. path (match-string 1))
  9032. (while (string-match ":" path)
  9033. (setq path (replace-match "+" t t path)))
  9034. (throw 'match t)))
  9035. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9036. (setq type "tree-match"
  9037. path (match-string 1))
  9038. (throw 'match t)))
  9039. (unless path
  9040. (user-error "No link found"))
  9041. ;; switch back to reference buffer
  9042. ;; needed when if called in a temporary buffer through
  9043. ;; org-open-link-from-string
  9044. (with-current-buffer (or reference-buffer (current-buffer))
  9045. ;; Remove any trailing spaces in path
  9046. (if (string-match " +\\'" path)
  9047. (setq path (replace-match "" t t path)))
  9048. (if (and org-link-translation-function
  9049. (fboundp org-link-translation-function))
  9050. ;; Check if we need to translate the link
  9051. (let ((tmp (funcall org-link-translation-function type path)))
  9052. (setq type (car tmp) path (cdr tmp))))
  9053. (cond
  9054. ((assoc type org-link-protocols)
  9055. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9056. ((equal type "help")
  9057. (let ((f-or-v (intern path)))
  9058. (cond ((fboundp f-or-v)
  9059. (describe-function f-or-v))
  9060. ((boundp f-or-v)
  9061. (describe-variable f-or-v))
  9062. (t (error "Not a known function or variable")))))
  9063. ((equal type "mailto")
  9064. (let ((cmd (car org-link-mailto-program))
  9065. (args (cdr org-link-mailto-program)) args1
  9066. (address path) (subject "") a)
  9067. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9068. (setq address (match-string 1 path)
  9069. subject (org-link-escape (match-string 2 path))))
  9070. (while args
  9071. (cond
  9072. ((not (stringp (car args))) (push (pop args) args1))
  9073. (t (setq a (pop args))
  9074. (if (string-match "%a" a)
  9075. (setq a (replace-match address t t a)))
  9076. (if (string-match "%s" a)
  9077. (setq a (replace-match subject t t a)))
  9078. (push a args1))))
  9079. (apply cmd (nreverse args1))))
  9080. ((member type '("http" "https" "ftp" "news"))
  9081. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9082. (org-link-escape
  9083. path org-link-escape-chars-browser)
  9084. path))))
  9085. ((string= type "doi")
  9086. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9087. (org-link-escape
  9088. path org-link-escape-chars-browser)
  9089. path))))
  9090. ((member type '("message"))
  9091. (browse-url (concat type ":" path)))
  9092. ((string= type "tags")
  9093. (org-tags-view arg path))
  9094. ((string= type "tree-match")
  9095. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9096. ((string= type "file")
  9097. (if (string-match "::\\([0-9]+\\)\\'" path)
  9098. (setq line (string-to-number (match-string 1 path))
  9099. path (substring path 0 (match-beginning 0)))
  9100. (if (string-match "::\\(.+\\)\\'" path)
  9101. (setq search (match-string 1 path)
  9102. path (substring path 0 (match-beginning 0)))))
  9103. (if (string-match "[*?{]" (file-name-nondirectory path))
  9104. (dired path)
  9105. (org-open-file path arg line search)))
  9106. ((string= type "shell")
  9107. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9108. (cmd path))
  9109. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9110. (string-match org-confirm-shell-link-not-regexp cmd))
  9111. (not org-confirm-shell-link-function)
  9112. (funcall org-confirm-shell-link-function
  9113. (format "Execute \"%s\" in shell? "
  9114. (org-add-props cmd nil
  9115. 'face 'org-warning))))
  9116. (progn
  9117. (message "Executing %s" cmd)
  9118. (shell-command cmd buf)
  9119. (if (featurep 'midnight)
  9120. (setq clean-buffer-list-kill-buffer-names
  9121. (cons buf clean-buffer-list-kill-buffer-names))))
  9122. (error "Abort"))))
  9123. ((string= type "elisp")
  9124. (let ((cmd path))
  9125. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9126. (string-match org-confirm-elisp-link-not-regexp cmd))
  9127. (not org-confirm-elisp-link-function)
  9128. (funcall org-confirm-elisp-link-function
  9129. (format "Execute \"%s\" as elisp? "
  9130. (org-add-props cmd nil
  9131. 'face 'org-warning))))
  9132. (message "%s => %s" cmd
  9133. (if (equal (string-to-char cmd) ?\()
  9134. (eval (read cmd))
  9135. (call-interactively (read cmd))))
  9136. (error "Abort"))))
  9137. ((and (string= type "thisfile")
  9138. (run-hook-with-args-until-success
  9139. 'org-open-link-functions path)))
  9140. ((string= type "thisfile")
  9141. (if arg
  9142. (switch-to-buffer-other-window
  9143. (org-get-buffer-for-internal-link (current-buffer)))
  9144. (org-mark-ring-push))
  9145. (let ((cmd `(org-link-search
  9146. ,path
  9147. ,(cond ((equal arg '(4)) ''occur)
  9148. ((equal arg '(16)) ''org-occur))
  9149. ,pos)))
  9150. (condition-case nil (let ((org-link-search-inhibit-query t))
  9151. (eval cmd))
  9152. (error (progn (widen) (eval cmd))))))
  9153. (t (browse-url-at-point)))))))
  9154. (move-marker org-open-link-marker nil)
  9155. (run-hook-with-args 'org-follow-link-hook)))
  9156. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9157. "Offer links in the current entry and return the selected link.
  9158. If there is only one link, return it.
  9159. If NTH is an integer, return the NTH link found.
  9160. If ZERO is a string, check also this string for a link, and if
  9161. there is one, return it."
  9162. (with-current-buffer buffer
  9163. (save-excursion
  9164. (save-restriction
  9165. (widen)
  9166. (goto-char marker)
  9167. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9168. "\\(" org-angle-link-re "\\)\\|"
  9169. "\\(" org-plain-link-re "\\)"))
  9170. (cnt ?0)
  9171. (in-emacs (if (integerp nth) nil nth))
  9172. have-zero end links link c)
  9173. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9174. (push (match-string 0 zero) links)
  9175. (setq cnt (1- cnt) have-zero t))
  9176. (save-excursion
  9177. (org-back-to-heading t)
  9178. (setq end (save-excursion (outline-next-heading) (point)))
  9179. (while (re-search-forward re end t)
  9180. (push (match-string 0) links))
  9181. (setq links (org-uniquify (reverse links))))
  9182. (cond
  9183. ((null links)
  9184. (message "No links"))
  9185. ((equal (length links) 1)
  9186. (setq link (car links)))
  9187. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9188. (setq link (nth (if have-zero nth (1- nth)) links)))
  9189. (t ; we have to select a link
  9190. (save-excursion
  9191. (save-window-excursion
  9192. (delete-other-windows)
  9193. (with-output-to-temp-buffer "*Select Link*"
  9194. (mapc (lambda (l)
  9195. (if (not (string-match org-bracket-link-regexp l))
  9196. (princ (format "[%c] %s\n" (incf cnt)
  9197. (org-remove-angle-brackets l)))
  9198. (if (match-end 3)
  9199. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9200. (match-string 3 l) (match-string 1 l)))
  9201. (princ (format "[%c] %s\n" (incf cnt)
  9202. (match-string 1 l))))))
  9203. links))
  9204. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9205. (message "Select link to open, RET to open all:")
  9206. (setq c (read-char-exclusive))
  9207. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9208. (when (equal c ?q) (error "Abort"))
  9209. (if (equal c ?\C-m)
  9210. (setq link links)
  9211. (setq nth (- c ?0))
  9212. (if have-zero (setq nth (1+ nth)))
  9213. (unless (and (integerp nth) (>= (length links) nth))
  9214. (error "Invalid link selection"))
  9215. (setq link (nth (1- nth) links)))))
  9216. (cons link end))))))
  9217. ;; Add special file links that specify the way of opening
  9218. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9219. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9220. (defun org-open-file-with-system (path)
  9221. "Open file at PATH using the system way of opening it."
  9222. (org-open-file path 'system))
  9223. (defun org-open-file-with-emacs (path)
  9224. "Open file at PATH in Emacs."
  9225. (org-open-file path 'emacs))
  9226. ;;; File search
  9227. (defvar org-create-file-search-functions nil
  9228. "List of functions to construct the right search string for a file link.
  9229. These functions are called in turn with point at the location to
  9230. which the link should point.
  9231. A function in the hook should first test if it would like to
  9232. handle this file type, for example by checking the `major-mode'
  9233. or the file extension. If it decides not to handle this file, it
  9234. should just return nil to give other functions a chance. If it
  9235. does handle the file, it must return the search string to be used
  9236. when following the link. The search string will be part of the
  9237. file link, given after a double colon, and `org-open-at-point'
  9238. will automatically search for it. If special measures must be
  9239. taken to make the search successful, another function should be
  9240. added to the companion hook `org-execute-file-search-functions',
  9241. which see.
  9242. A function in this hook may also use `setq' to set the variable
  9243. `description' to provide a suggestion for the descriptive text to
  9244. be used for this link when it gets inserted into an Org-mode
  9245. buffer with \\[org-insert-link].")
  9246. (defvar org-execute-file-search-functions nil
  9247. "List of functions to execute a file search triggered by a link.
  9248. Functions added to this hook must accept a single argument, the
  9249. search string that was part of the file link, the part after the
  9250. double colon. The function must first check if it would like to
  9251. handle this search, for example by checking the `major-mode' or
  9252. the file extension. If it decides not to handle this search, it
  9253. should just return nil to give other functions a chance. If it
  9254. does handle the search, it must return a non-nil value to keep
  9255. other functions from trying.
  9256. Each function can access the current prefix argument through the
  9257. variable `current-prefix-argument'. Note that a single prefix is
  9258. used to force opening a link in Emacs, so it may be good to only
  9259. use a numeric or double prefix to guide the search function.
  9260. In case this is needed, a function in this hook can also restore
  9261. the window configuration before `org-open-at-point' was called using:
  9262. (set-window-configuration org-window-config-before-follow-link)")
  9263. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9264. (defun org-link-search (s &optional type avoid-pos stealth)
  9265. "Search for a link search option.
  9266. If S is surrounded by forward slashes, it is interpreted as a
  9267. regular expression. In org-mode files, this will create an `org-occur'
  9268. sparse tree. In ordinary files, `occur' will be used to list matches.
  9269. If the current buffer is in `dired-mode', grep will be used to search
  9270. in all files. If AVOID-POS is given, ignore matches near that position.
  9271. When optional argument STEALTH is non-nil, do not modify
  9272. visibility around point, thus ignoring
  9273. `org-show-hierarchy-above', `org-show-following-heading' and
  9274. `org-show-siblings' variables."
  9275. (let ((case-fold-search t)
  9276. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9277. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9278. (append '(("") (" ") ("\t") ("\n"))
  9279. org-emphasis-alist)
  9280. "\\|") "\\)"))
  9281. (pos (point))
  9282. (pre nil) (post nil)
  9283. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9284. (cond
  9285. ;; First check if there are any special search functions
  9286. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9287. ;; Now try the builtin stuff
  9288. ((and (equal (string-to-char s0) ?#)
  9289. (> (length s0) 1)
  9290. (save-excursion
  9291. (goto-char (point-min))
  9292. (and
  9293. (re-search-forward
  9294. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9295. (setq type 'dedicated
  9296. pos (match-beginning 0))))
  9297. ;; There is an exact target for this
  9298. (goto-char pos)
  9299. (org-back-to-heading t)))
  9300. ((save-excursion
  9301. (goto-char (point-min))
  9302. (and
  9303. (re-search-forward
  9304. (concat "<<" (regexp-quote s0) ">>") nil t)
  9305. (setq type 'dedicated
  9306. pos (match-beginning 0))))
  9307. ;; There is an exact target for this
  9308. (goto-char pos))
  9309. ((save-excursion
  9310. (goto-char (point-min))
  9311. (and
  9312. (re-search-forward
  9313. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9314. (setq type 'dedicated pos (match-beginning 0))))
  9315. ;; Found an invisible target.
  9316. (goto-char pos))
  9317. ((save-excursion
  9318. (goto-char (point-min))
  9319. (and
  9320. (re-search-forward
  9321. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9322. (setq type 'dedicated pos (match-beginning 0))))
  9323. ;; Found an element with a matching #+name affiliated keyword.
  9324. (goto-char pos))
  9325. ((and (string-match "^(\\(.*\\))$" s0)
  9326. (save-excursion
  9327. (goto-char (point-min))
  9328. (and
  9329. (re-search-forward
  9330. (concat "[^[]" (regexp-quote
  9331. (format org-coderef-label-format
  9332. (match-string 1 s0))))
  9333. nil t)
  9334. (setq type 'dedicated
  9335. pos (1+ (match-beginning 0))))))
  9336. ;; There is a coderef target for this
  9337. (goto-char pos))
  9338. ((string-match "^/\\(.*\\)/$" s)
  9339. ;; A regular expression
  9340. (cond
  9341. ((derived-mode-p 'org-mode)
  9342. (org-occur (match-string 1 s)))
  9343. (t (org-do-occur (match-string 1 s)))))
  9344. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9345. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9346. (goto-char (point-min))
  9347. (cond
  9348. ((let (case-fold-search)
  9349. (re-search-forward (format org-complex-heading-regexp-format
  9350. (regexp-quote s))
  9351. nil t))
  9352. ;; OK, found a match
  9353. (setq type 'dedicated)
  9354. (goto-char (match-beginning 0)))
  9355. ((and (not org-link-search-inhibit-query)
  9356. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9357. (y-or-n-p "No match - create this as a new heading? "))
  9358. (goto-char (point-max))
  9359. (or (bolp) (newline))
  9360. (insert "* " s "\n")
  9361. (beginning-of-line 0))
  9362. (t
  9363. (goto-char pos)
  9364. (error "No match"))))
  9365. (t
  9366. ;; A normal search string
  9367. (when (equal (string-to-char s) ?*)
  9368. ;; Anchor on headlines, post may include tags.
  9369. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9370. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9371. s (substring s 1)))
  9372. (remove-text-properties
  9373. 0 (length s)
  9374. '(face nil mouse-face nil keymap nil fontified nil) s)
  9375. ;; Make a series of regular expressions to find a match
  9376. (setq words (org-split-string s "[ \n\r\t]+")
  9377. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9378. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9379. "\\)" markers)
  9380. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9381. re2a (concat "[ \t\r\n]" re2a_)
  9382. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9383. re4 (concat "[^a-zA-Z_]" re4_)
  9384. re1 (concat pre re2 post)
  9385. re3 (concat pre (if pre re4_ re4) post)
  9386. re5 (concat pre ".*" re4)
  9387. re2 (concat pre re2)
  9388. re2a (concat pre (if pre re2a_ re2a))
  9389. re4 (concat pre (if pre re4_ re4))
  9390. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9391. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9392. re5 "\\)"
  9393. ))
  9394. (cond
  9395. ((eq type 'org-occur) (org-occur reall))
  9396. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9397. (t (goto-char (point-min))
  9398. (setq type 'fuzzy)
  9399. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9400. (org-search-not-self 1 re1 nil t)
  9401. (org-search-not-self 1 re2 nil t)
  9402. (org-search-not-self 1 re2a nil t)
  9403. (org-search-not-self 1 re3 nil t)
  9404. (org-search-not-self 1 re4 nil t)
  9405. (org-search-not-self 1 re5 nil t)
  9406. )
  9407. (goto-char (match-beginning 1))
  9408. (goto-char pos)
  9409. (error "No match"))))))
  9410. (and (derived-mode-p 'org-mode)
  9411. (not stealth)
  9412. (org-show-context 'link-search))
  9413. type))
  9414. (defun org-search-not-self (group &rest args)
  9415. "Execute `re-search-forward', but only accept matches that do not
  9416. enclose the position of `org-open-link-marker'."
  9417. (let ((m org-open-link-marker))
  9418. (catch 'exit
  9419. (while (apply 're-search-forward args)
  9420. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9421. (goto-char (match-end group))
  9422. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9423. (> (match-beginning 0) (marker-position m))
  9424. (< (match-end 0) (marker-position m)))
  9425. (save-match-data
  9426. (or (not (org-in-regexp
  9427. org-bracket-link-analytic-regexp 1))
  9428. (not (match-end 4)) ; no description
  9429. (and (<= (match-beginning 4) (point))
  9430. (>= (match-end 4) (point))))))
  9431. (throw 'exit (point))))))))
  9432. (defun org-get-buffer-for-internal-link (buffer)
  9433. "Return a buffer to be used for displaying the link target of internal links."
  9434. (cond
  9435. ((not org-display-internal-link-with-indirect-buffer)
  9436. buffer)
  9437. ((string-match "(Clone)$" (buffer-name buffer))
  9438. (message "Buffer is already a clone, not making another one")
  9439. ;; we also do not modify visibility in this case
  9440. buffer)
  9441. (t ; make a new indirect buffer for displaying the link
  9442. (let* ((bn (buffer-name buffer))
  9443. (ibn (concat bn "(Clone)"))
  9444. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9445. (with-current-buffer ib (org-overview))
  9446. ib))))
  9447. (defun org-do-occur (regexp &optional cleanup)
  9448. "Call the Emacs command `occur'.
  9449. If CLEANUP is non-nil, remove the printout of the regular expression
  9450. in the *Occur* buffer. This is useful if the regex is long and not useful
  9451. to read."
  9452. (occur regexp)
  9453. (when cleanup
  9454. (let ((cwin (selected-window)) win beg end)
  9455. (when (setq win (get-buffer-window "*Occur*"))
  9456. (select-window win))
  9457. (goto-char (point-min))
  9458. (when (re-search-forward "match[a-z]+" nil t)
  9459. (setq beg (match-end 0))
  9460. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9461. (setq end (1- (match-beginning 0)))))
  9462. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9463. (goto-char (point-min))
  9464. (select-window cwin))))
  9465. ;;; The mark ring for links jumps
  9466. (defvar org-mark-ring nil
  9467. "Mark ring for positions before jumps in Org-mode.")
  9468. (defvar org-mark-ring-last-goto nil
  9469. "Last position in the mark ring used to go back.")
  9470. ;; Fill and close the ring
  9471. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9472. (loop for i from 1 to org-mark-ring-length do
  9473. (push (make-marker) org-mark-ring))
  9474. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9475. org-mark-ring)
  9476. (defun org-mark-ring-push (&optional pos buffer)
  9477. "Put the current position or POS into the mark ring and rotate it."
  9478. (interactive)
  9479. (setq pos (or pos (point)))
  9480. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9481. (move-marker (car org-mark-ring)
  9482. (or pos (point))
  9483. (or buffer (current-buffer)))
  9484. (message "%s"
  9485. (substitute-command-keys
  9486. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9487. (defun org-mark-ring-goto (&optional n)
  9488. "Jump to the previous position in the mark ring.
  9489. With prefix arg N, jump back that many stored positions. When
  9490. called several times in succession, walk through the entire ring.
  9491. Org-mode commands jumping to a different position in the current file,
  9492. or to another Org-mode file, automatically push the old position
  9493. onto the ring."
  9494. (interactive "p")
  9495. (let (p m)
  9496. (if (eq last-command this-command)
  9497. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9498. (setq p org-mark-ring))
  9499. (setq org-mark-ring-last-goto p)
  9500. (setq m (car p))
  9501. (org-pop-to-buffer-same-window (marker-buffer m))
  9502. (goto-char m)
  9503. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9504. (defun org-remove-angle-brackets (s)
  9505. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9506. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9507. s)
  9508. (defun org-add-angle-brackets (s)
  9509. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9510. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9511. s)
  9512. (defun org-remove-double-quotes (s)
  9513. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9514. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9515. s)
  9516. ;;; Following specific links
  9517. (defun org-follow-timestamp-link ()
  9518. "Open an agenda view for the time-stamp date/range at point."
  9519. (cond
  9520. ((org-at-date-range-p t)
  9521. (let ((org-agenda-start-on-weekday)
  9522. (t1 (match-string 1))
  9523. (t2 (match-string 2)) tt1 tt2)
  9524. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9525. tt2 (time-to-days (org-time-string-to-time t2)))
  9526. (let ((org-agenda-buffer-tmp-name
  9527. (format "*Org Agenda(a:%s)"
  9528. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9529. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9530. ((org-at-timestamp-p t)
  9531. (let ((org-agenda-buffer-tmp-name
  9532. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9533. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9534. (substring (match-string 1) 0 10)))
  9535. 1)))
  9536. (t (error "This should not happen"))))
  9537. ;;; Following file links
  9538. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9539. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9540. (declare-function mailcap-mime-info
  9541. "mailcap" (string &optional request no-decode))
  9542. (defvar org-wait nil)
  9543. (defun org-open-file (path &optional in-emacs line search)
  9544. "Open the file at PATH.
  9545. First, this expands any special file name abbreviations. Then the
  9546. configuration variable `org-file-apps' is checked if it contains an
  9547. entry for this file type, and if yes, the corresponding command is launched.
  9548. If no application is found, Emacs simply visits the file.
  9549. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9550. With a double \\[universal-argument] \\[universal-argument] \
  9551. prefix arg, Org tries to avoid opening in Emacs
  9552. and to use an external application to visit the file.
  9553. Optional LINE specifies a line to go to, optional SEARCH a string
  9554. to search for. If LINE or SEARCH is given, the file will be
  9555. opened in Emacs, unless an entry from org-file-apps that makes
  9556. use of groups in a regexp matches.
  9557. If you want to change the way frames are used when following a
  9558. link, please customize `org-link-frame-setup'.
  9559. If the file does not exist, an error is thrown."
  9560. (let* ((file (if (equal path "")
  9561. buffer-file-name
  9562. (substitute-in-file-name (expand-file-name path))))
  9563. (file-apps (append org-file-apps (org-default-apps)))
  9564. (apps (org-remove-if
  9565. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9566. (apps-dlink (org-remove-if-not
  9567. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9568. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9569. (dirp (if remp nil (file-directory-p file)))
  9570. (file (if (and dirp org-open-directory-means-index-dot-org)
  9571. (concat (file-name-as-directory file) "index.org")
  9572. file))
  9573. (a-m-a-p (assq 'auto-mode apps))
  9574. (dfile (downcase file))
  9575. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9576. (link (cond ((and (eq line nil)
  9577. (eq search nil))
  9578. file)
  9579. (line
  9580. (concat file "::" (number-to-string line)))
  9581. (search
  9582. (concat file "::" search))))
  9583. (dlink (downcase link))
  9584. (old-buffer (current-buffer))
  9585. (old-pos (point))
  9586. (old-mode major-mode)
  9587. ext cmd link-match-data)
  9588. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9589. (setq ext (match-string 1 dfile))
  9590. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9591. (setq ext (match-string 1 dfile))))
  9592. (cond
  9593. ((member in-emacs '((16) system))
  9594. (setq cmd (cdr (assoc 'system apps))))
  9595. (in-emacs (setq cmd 'emacs))
  9596. (t
  9597. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9598. (and dirp (cdr (assoc 'directory apps)))
  9599. ; first, try matching against apps-dlink
  9600. ; if we get a match here, store the match data for later
  9601. (let ((match (assoc-default dlink apps-dlink
  9602. 'string-match)))
  9603. (if match
  9604. (progn (setq link-match-data (match-data))
  9605. match)
  9606. (progn (setq in-emacs (or in-emacs line search))
  9607. nil))) ; if we have no match in apps-dlink,
  9608. ; always open the file in emacs if line or search
  9609. ; is given (for backwards compatibility)
  9610. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9611. 'string-match)
  9612. (cdr (assoc ext apps))
  9613. (cdr (assoc t apps))))))
  9614. (when (eq cmd 'system)
  9615. (setq cmd (cdr (assoc 'system apps))))
  9616. (when (eq cmd 'default)
  9617. (setq cmd (cdr (assoc t apps))))
  9618. (when (eq cmd 'mailcap)
  9619. (require 'mailcap)
  9620. (mailcap-parse-mailcaps)
  9621. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9622. (command (mailcap-mime-info mime-type)))
  9623. (if (stringp command)
  9624. (setq cmd command)
  9625. (setq cmd 'emacs))))
  9626. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9627. (not (file-exists-p file))
  9628. (not org-open-non-existing-files))
  9629. (error "No such file: %s" file))
  9630. (cond
  9631. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9632. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9633. (while (string-match "['\"]%s['\"]" cmd)
  9634. (setq cmd (replace-match "%s" t t cmd)))
  9635. (while (string-match "%s" cmd)
  9636. (setq cmd (replace-match
  9637. (save-match-data
  9638. (shell-quote-argument
  9639. (convert-standard-filename file)))
  9640. t t cmd)))
  9641. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9642. (save-match-data
  9643. (let ((match-index 1)
  9644. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9645. (set-match-data link-match-data)
  9646. (while (<= match-index number-of-groups)
  9647. (let ((regex (concat "%" (number-to-string match-index)))
  9648. (replace-with (match-string match-index dlink)))
  9649. (while (string-match regex cmd)
  9650. (setq cmd (replace-match replace-with t t cmd))))
  9651. (setq match-index (+ match-index 1)))))
  9652. (save-window-excursion
  9653. (message "Running %s...done" cmd)
  9654. (start-process-shell-command cmd nil cmd)
  9655. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9656. ((or (stringp cmd)
  9657. (eq cmd 'emacs))
  9658. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9659. (widen)
  9660. (if line (org-goto-line line)
  9661. (if search (org-link-search search))))
  9662. ((consp cmd)
  9663. (let ((file (convert-standard-filename file)))
  9664. (save-match-data
  9665. (set-match-data link-match-data)
  9666. (eval cmd))))
  9667. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9668. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9669. (or (not (equal old-buffer (current-buffer)))
  9670. (not (equal old-pos (point))))
  9671. (org-mark-ring-push old-pos old-buffer))))
  9672. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9673. "This function returns non-nil if `entry' uses a regular
  9674. expression which should be matched against the whole link by
  9675. org-open-file.
  9676. It assumes that is the case when the entry uses a regular
  9677. expression which has at least one grouping construct and the
  9678. action is either a lisp form or a command string containing
  9679. '%1', i.e. using at least one subexpression match as a
  9680. parameter."
  9681. (let ((selector (car entry))
  9682. (action (cdr entry)))
  9683. (if (stringp selector)
  9684. (and (> (regexp-opt-depth selector) 0)
  9685. (or (and (stringp action)
  9686. (string-match "%[0-9]" action))
  9687. (consp action)))
  9688. nil)))
  9689. (defun org-default-apps ()
  9690. "Return the default applications for this operating system."
  9691. (cond
  9692. ((eq system-type 'darwin)
  9693. org-file-apps-defaults-macosx)
  9694. ((eq system-type 'windows-nt)
  9695. org-file-apps-defaults-windowsnt)
  9696. (t org-file-apps-defaults-gnu)))
  9697. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9698. "Convert extensions to regular expressions in the cars of LIST.
  9699. Also, weed out any non-string entries, because the return value is used
  9700. only for regexp matching.
  9701. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9702. point to the symbol `emacs', indicating that the file should
  9703. be opened in Emacs."
  9704. (append
  9705. (delq nil
  9706. (mapcar (lambda (x)
  9707. (if (not (stringp (car x)))
  9708. nil
  9709. (if (string-match "\\W" (car x))
  9710. x
  9711. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9712. list))
  9713. (if add-auto-mode
  9714. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9715. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9716. (defun org-file-remote-p (file)
  9717. "Test whether FILE specifies a location on a remote system.
  9718. Return non-nil if the location is indeed remote.
  9719. For example, the filename \"/user@host:/foo\" specifies a location
  9720. on the system \"/user@host:\"."
  9721. (cond ((fboundp 'file-remote-p)
  9722. (file-remote-p file))
  9723. ((fboundp 'tramp-handle-file-remote-p)
  9724. (tramp-handle-file-remote-p file))
  9725. ((and (boundp 'ange-ftp-name-format)
  9726. (string-match (car ange-ftp-name-format) file))
  9727. t)))
  9728. ;;;; Refiling
  9729. (defun org-get-org-file ()
  9730. "Read a filename, with default directory `org-directory'."
  9731. (let ((default (or org-default-notes-file remember-data-file)))
  9732. (read-file-name (format "File name [%s]: " default)
  9733. (file-name-as-directory org-directory)
  9734. default)))
  9735. (defun org-notes-order-reversed-p ()
  9736. "Check if the current file should receive notes in reversed order."
  9737. (cond
  9738. ((not org-reverse-note-order) nil)
  9739. ((eq t org-reverse-note-order) t)
  9740. ((not (listp org-reverse-note-order)) nil)
  9741. (t (catch 'exit
  9742. (let ((all org-reverse-note-order)
  9743. entry)
  9744. (while (setq entry (pop all))
  9745. (if (string-match (car entry) buffer-file-name)
  9746. (throw 'exit (cdr entry))))
  9747. nil)))))
  9748. (defvar org-refile-target-table nil
  9749. "The list of refile targets, created by `org-refile'.")
  9750. (defvar org-agenda-new-buffers nil
  9751. "Buffers created to visit agenda files.")
  9752. (defvar org-refile-cache nil
  9753. "Cache for refile targets.")
  9754. (defvar org-refile-markers nil
  9755. "All the markers used for caching refile locations.")
  9756. (defun org-refile-marker (pos)
  9757. "Get a new refile marker, but only if caching is in use."
  9758. (if (not org-refile-use-cache)
  9759. pos
  9760. (let ((m (make-marker)))
  9761. (move-marker m pos)
  9762. (push m org-refile-markers)
  9763. m)))
  9764. (defun org-refile-cache-clear ()
  9765. "Clear the refile cache and disable all the markers."
  9766. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9767. (setq org-refile-markers nil)
  9768. (setq org-refile-cache nil)
  9769. (message "Refile cache has been cleared"))
  9770. (defun org-refile-cache-check-set (set)
  9771. "Check if all the markers in the cache still have live buffers."
  9772. (let (marker)
  9773. (catch 'exit
  9774. (while (and set (setq marker (nth 3 (pop set))))
  9775. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9776. (when (and marker (null (marker-buffer marker)))
  9777. (message "not found") (sit-for 3)
  9778. (throw 'exit nil)))
  9779. t)))
  9780. (defun org-refile-cache-put (set &rest identifiers)
  9781. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9782. (let* ((key (sha1 (prin1-to-string identifiers)))
  9783. (entry (assoc key org-refile-cache)))
  9784. (if entry
  9785. (setcdr entry set)
  9786. (push (cons key set) org-refile-cache))))
  9787. (defun org-refile-cache-get (&rest identifiers)
  9788. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9789. (cond
  9790. ((not org-refile-cache) nil)
  9791. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9792. (t
  9793. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9794. org-refile-cache))))
  9795. (and set (org-refile-cache-check-set set) set)))))
  9796. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9797. "Produce a table with refile targets."
  9798. (let ((case-fold-search nil)
  9799. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9800. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9801. targets tgs txt re files f desc descre fast-path-p level pos0)
  9802. (message "Getting targets...")
  9803. (with-current-buffer (or default-buffer (current-buffer))
  9804. (while (setq entry (pop entries))
  9805. (setq files (car entry) desc (cdr entry))
  9806. (setq fast-path-p nil)
  9807. (cond
  9808. ((null files) (setq files (list (current-buffer))))
  9809. ((eq files 'org-agenda-files)
  9810. (setq files (org-agenda-files 'unrestricted)))
  9811. ((and (symbolp files) (fboundp files))
  9812. (setq files (funcall files)))
  9813. ((and (symbolp files) (boundp files))
  9814. (setq files (symbol-value files))))
  9815. (if (stringp files) (setq files (list files)))
  9816. (cond
  9817. ((eq (car desc) :tag)
  9818. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9819. ((eq (car desc) :todo)
  9820. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9821. ((eq (car desc) :regexp)
  9822. (setq descre (cdr desc)))
  9823. ((eq (car desc) :level)
  9824. (setq descre (concat "^\\*\\{" (number-to-string
  9825. (if org-odd-levels-only
  9826. (1- (* 2 (cdr desc)))
  9827. (cdr desc)))
  9828. "\\}[ \t]")))
  9829. ((eq (car desc) :maxlevel)
  9830. (setq fast-path-p t)
  9831. (setq descre (concat "^\\*\\{1," (number-to-string
  9832. (if org-odd-levels-only
  9833. (1- (* 2 (cdr desc)))
  9834. (cdr desc)))
  9835. "\\}[ \t]")))
  9836. (t (error "Bad refiling target description %s" desc)))
  9837. (while (setq f (pop files))
  9838. (with-current-buffer
  9839. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9840. (or
  9841. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9842. (progn
  9843. (if (bufferp f) (setq f (buffer-file-name
  9844. (buffer-base-buffer f))))
  9845. (setq f (and f (expand-file-name f)))
  9846. (if (eq org-refile-use-outline-path 'file)
  9847. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9848. (save-excursion
  9849. (save-restriction
  9850. (widen)
  9851. (goto-char (point-min))
  9852. (while (re-search-forward descre nil t)
  9853. (goto-char (setq pos0 (point-at-bol)))
  9854. (catch 'next
  9855. (when org-refile-target-verify-function
  9856. (save-match-data
  9857. (or (funcall org-refile-target-verify-function)
  9858. (throw 'next t))))
  9859. (when (and (looking-at org-complex-heading-regexp)
  9860. (not (member (match-string 4) excluded-entries))
  9861. (match-string 4))
  9862. (setq level (org-reduced-level
  9863. (- (match-end 1) (match-beginning 1)))
  9864. txt (org-link-display-format (match-string 4))
  9865. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9866. re (format org-complex-heading-regexp-format
  9867. (regexp-quote (match-string 4))))
  9868. (when org-refile-use-outline-path
  9869. (setq txt (mapconcat
  9870. 'org-protect-slash
  9871. (append
  9872. (if (eq org-refile-use-outline-path
  9873. 'file)
  9874. (list (file-name-nondirectory
  9875. (buffer-file-name
  9876. (buffer-base-buffer))))
  9877. (if (eq org-refile-use-outline-path
  9878. 'full-file-path)
  9879. (list (buffer-file-name
  9880. (buffer-base-buffer)))))
  9881. (org-get-outline-path fast-path-p
  9882. level txt)
  9883. (list txt))
  9884. "/")))
  9885. (push (list txt f re (org-refile-marker (point)))
  9886. tgs)))
  9887. (when (= (point) pos0)
  9888. ;; verification function has not moved point
  9889. (goto-char (point-at-eol))))))))
  9890. (when org-refile-use-cache
  9891. (org-refile-cache-put tgs (buffer-file-name) descre))
  9892. (setq targets (append tgs targets))
  9893. ))))
  9894. (message "Getting targets...done")
  9895. (nreverse targets)))
  9896. (defun org-protect-slash (s)
  9897. (while (string-match "/" s)
  9898. (setq s (replace-match "\\" t t s)))
  9899. s)
  9900. (defvar org-olpa (make-vector 20 nil))
  9901. (defun org-get-outline-path (&optional fastp level heading)
  9902. "Return the outline path to the current entry, as a list.
  9903. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9904. routine which makes outline path derivations for an entire file,
  9905. avoiding backtracing. Refile target collection makes use of that."
  9906. (if fastp
  9907. (progn
  9908. (if (> level 19)
  9909. (error "Outline path failure, more than 19 levels"))
  9910. (loop for i from level upto 19 do
  9911. (aset org-olpa i nil))
  9912. (prog1
  9913. (delq nil (append org-olpa nil))
  9914. (aset org-olpa level heading)))
  9915. (let (rtn case-fold-search)
  9916. (save-excursion
  9917. (save-restriction
  9918. (widen)
  9919. (while (org-up-heading-safe)
  9920. (when (looking-at org-complex-heading-regexp)
  9921. (push (org-match-string-no-properties 4) rtn)))
  9922. rtn)))))
  9923. (defun org-format-outline-path (path &optional width prefix separator)
  9924. "Format the outline path PATH for display.
  9925. WIDTH is the maximum number of characters that is available.
  9926. PREFIX is a prefix to be included in the returned string,
  9927. such as the file name.
  9928. SEPARATOR is inserted between the different parts of the path,
  9929. the default is \"/\"."
  9930. (setq width (or width 79))
  9931. (if prefix (setq width (- width (length prefix))))
  9932. (if (not path)
  9933. (or prefix "")
  9934. (let* ((nsteps (length path))
  9935. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9936. (maxwidth (if (<= total-width width)
  9937. 10000 ;; everything fits
  9938. ;; we need to shorten the level headings
  9939. (/ (- width nsteps) nsteps)))
  9940. (org-odd-levels-only nil)
  9941. (n 0)
  9942. (total (1+ (length prefix))))
  9943. (setq maxwidth (max maxwidth 10))
  9944. (concat prefix
  9945. (if prefix (or separator "/"))
  9946. (mapconcat
  9947. (lambda (h)
  9948. (setq n (1+ n))
  9949. (if (and (= n nsteps) (< maxwidth 10000))
  9950. (setq maxwidth (- total-width total)))
  9951. (if (< (length h) maxwidth)
  9952. (progn (setq total (+ total (length h) 1)) h)
  9953. (setq h (substring h 0 (- maxwidth 2))
  9954. total (+ total maxwidth 1))
  9955. (if (string-match "[ \t]+\\'" h)
  9956. (setq h (substring h 0 (match-beginning 0))))
  9957. (setq h (concat h "..")))
  9958. (org-add-props h nil 'face
  9959. (nth (% (1- n) org-n-level-faces)
  9960. org-level-faces))
  9961. h)
  9962. path (or separator "/"))))))
  9963. (defun org-display-outline-path (&optional file current separator just-return-string)
  9964. "Display the current outline path in the echo area.
  9965. If FILE is non-nil, prepend the output with the file name.
  9966. If CURRENT is non-nil, append the current heading to the output.
  9967. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9968. the different parts of the path and defaults to \"/\".
  9969. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9970. (interactive "P")
  9971. (let* (case-fold-search
  9972. message-log-max ; Don't populate the *Messages* buffer
  9973. (bfn (buffer-file-name (buffer-base-buffer)))
  9974. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9975. res)
  9976. (if current (setq path (append path
  9977. (save-excursion
  9978. (org-back-to-heading t)
  9979. (if (looking-at org-complex-heading-regexp)
  9980. (list (match-string 4)))))))
  9981. (setq res
  9982. (org-format-outline-path
  9983. path
  9984. (1- (frame-width))
  9985. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9986. separator))
  9987. (if just-return-string
  9988. (org-no-properties res)
  9989. (message "%s" res))))
  9990. (defvar org-refile-history nil
  9991. "History for refiling operations.")
  9992. (defvar org-after-refile-insert-hook nil
  9993. "Hook run after `org-refile' has inserted its stuff at the new location.
  9994. Note that this is still *before* the stuff will be removed from
  9995. the *old* location.")
  9996. (defvar org-capture-last-stored-marker)
  9997. (defvar org-refile-keep nil
  9998. "Non-nil means `org-refile' will copy instead of refile.")
  9999. (defun org-copy ()
  10000. "Like `org-refile', but copy."
  10001. (interactive)
  10002. (let ((org-refile-keep t))
  10003. (funcall 'org-refile nil nil nil "Copy")))
  10004. (defun org-refile (&optional goto default-buffer rfloc msg)
  10005. "Move the entry or entries at point to another heading.
  10006. The list of target headings is compiled using the information in
  10007. `org-refile-targets', which see.
  10008. At the target location, the entry is filed as a subitem of the target
  10009. heading. Depending on `org-reverse-note-order', the new subitem will
  10010. either be the first or the last subitem.
  10011. If there is an active region, all entries in that region will be moved.
  10012. However, the region must fulfill the requirement that the first heading
  10013. is the first one sets the top-level of the moved text - at most siblings
  10014. below it are allowed.
  10015. With prefix arg GOTO, the command will only visit the target location
  10016. and not actually move anything.
  10017. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10018. go to the location where the last refiling operation has put the subtree.
  10019. With a prefix argument of `2', refile to the running clock.
  10020. RFLOC can be a refile location obtained in a different way.
  10021. MSG is a string to replace \"Refile\" in the default prompt with
  10022. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10023. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10024. If you are using target caching (see `org-refile-use-cache'),
  10025. you have to clear the target cache in order to find new targets.
  10026. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10027. prefix argument (`C-u C-u C-u C-c C-w')."
  10028. (interactive "P")
  10029. (if (member goto '(0 (64)))
  10030. (org-refile-cache-clear)
  10031. (let* ((actionmsg (or msg "Refile"))
  10032. (cbuf (current-buffer))
  10033. (regionp (org-region-active-p))
  10034. (region-start (and regionp (region-beginning)))
  10035. (region-end (and regionp (region-end)))
  10036. (region-length (and regionp (- region-end region-start)))
  10037. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10038. pos it nbuf file re level reversed)
  10039. (setq last-command nil)
  10040. (when regionp
  10041. (goto-char region-start)
  10042. (or (bolp) (goto-char (point-at-bol)))
  10043. (setq region-start (point))
  10044. (unless (or (org-kill-is-subtree-p
  10045. (buffer-substring region-start region-end))
  10046. (prog1 org-refile-active-region-within-subtree
  10047. (org-toggle-heading)))
  10048. (error "The region is not a (sequence of) subtree(s)")))
  10049. (if (equal goto '(16))
  10050. (org-refile-goto-last-stored)
  10051. (when (or
  10052. (and (equal goto 2)
  10053. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10054. (prog1
  10055. (setq it (list (or org-clock-heading "running clock")
  10056. (buffer-file-name
  10057. (marker-buffer org-clock-hd-marker))
  10058. ""
  10059. (marker-position org-clock-hd-marker)))
  10060. (setq goto nil)))
  10061. (setq it (or rfloc
  10062. (let (heading-text)
  10063. (save-excursion
  10064. (unless goto
  10065. (org-back-to-heading t)
  10066. (setq heading-text
  10067. (nth 4 (org-heading-components))))
  10068. (org-refile-get-location
  10069. (cond (goto "Goto")
  10070. (regionp (concat actionmsg " region to"))
  10071. (t (concat actionmsg " subtree \""
  10072. heading-text "\" to")))
  10073. default-buffer
  10074. (and (not (equal '(4) goto))
  10075. org-refile-allow-creating-parent-nodes)
  10076. goto))))))
  10077. (setq file (nth 1 it)
  10078. re (nth 2 it)
  10079. pos (nth 3 it))
  10080. (if (and (not goto)
  10081. pos
  10082. (equal (buffer-file-name) file)
  10083. (if regionp
  10084. (and (>= pos region-start)
  10085. (<= pos region-end))
  10086. (and (>= pos (point))
  10087. (< pos (save-excursion
  10088. (org-end-of-subtree t t))))))
  10089. (error "Cannot refile to position inside the tree or region"))
  10090. (setq nbuf (or (find-buffer-visiting file)
  10091. (find-file-noselect file)))
  10092. (if goto
  10093. (progn
  10094. (org-pop-to-buffer-same-window nbuf)
  10095. (goto-char pos)
  10096. (org-show-context 'org-goto))
  10097. (if regionp
  10098. (progn
  10099. (org-kill-new (buffer-substring region-start region-end))
  10100. (org-save-markers-in-region region-start region-end))
  10101. (org-copy-subtree 1 nil t))
  10102. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10103. (find-file-noselect file)))
  10104. (setq reversed (org-notes-order-reversed-p))
  10105. (save-excursion
  10106. (save-restriction
  10107. (widen)
  10108. (if pos
  10109. (progn
  10110. (goto-char pos)
  10111. (looking-at org-outline-regexp)
  10112. (setq level (org-get-valid-level (funcall outline-level) 1))
  10113. (goto-char
  10114. (if reversed
  10115. (or (outline-next-heading) (point-max))
  10116. (or (save-excursion (org-get-next-sibling))
  10117. (org-end-of-subtree t t)
  10118. (point-max)))))
  10119. (setq level 1)
  10120. (if (not reversed)
  10121. (goto-char (point-max))
  10122. (goto-char (point-min))
  10123. (or (outline-next-heading) (goto-char (point-max)))))
  10124. (if (not (bolp)) (newline))
  10125. (org-paste-subtree level)
  10126. (when org-log-refile
  10127. (org-add-log-setup 'refile nil nil 'findpos
  10128. org-log-refile)
  10129. (unless (eq org-log-refile 'note)
  10130. (save-excursion (org-add-log-note))))
  10131. (and org-auto-align-tags
  10132. (let ((org-loop-over-headlines-in-active-region nil))
  10133. (org-set-tags nil t)))
  10134. (with-demoted-errors
  10135. (bookmark-set "org-refile-last-stored"))
  10136. ;; If we are refiling for capture, make sure that the
  10137. ;; last-capture pointers point here
  10138. (when (org-bound-and-true-p org-refile-for-capture)
  10139. (with-demoted-errors
  10140. (bookmark-set "org-capture-last-stored-marker"))
  10141. (move-marker org-capture-last-stored-marker (point)))
  10142. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10143. (run-hooks 'org-after-refile-insert-hook))))
  10144. (unless org-refile-keep
  10145. (if regionp
  10146. (delete-region (point) (+ (point) region-length))
  10147. (org-cut-subtree)))
  10148. (when (featurep 'org-inlinetask)
  10149. (org-inlinetask-remove-END-maybe))
  10150. (setq org-markers-to-move nil)
  10151. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10152. (defun org-refile-goto-last-stored ()
  10153. "Go to the location where the last refile was stored."
  10154. (interactive)
  10155. (bookmark-jump "org-refile-last-stored")
  10156. (message "This is the location of the last refile"))
  10157. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10158. no-exclude)
  10159. "Prompt the user for a refile location, using PROMPT.
  10160. PROMPT should not be suffixed with a colon and a space, because
  10161. this function appends the default value from
  10162. `org-refile-history' automatically, if that is not empty.
  10163. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10164. this is used for the GOTO interface."
  10165. (let ((org-refile-targets org-refile-targets)
  10166. (org-refile-use-outline-path org-refile-use-outline-path)
  10167. excluded-entries)
  10168. (when (and (derived-mode-p 'org-mode)
  10169. (not org-refile-use-cache)
  10170. (not no-exclude))
  10171. (org-map-tree
  10172. (lambda()
  10173. (setq excluded-entries
  10174. (append excluded-entries (list (org-get-heading t t)))))))
  10175. (setq org-refile-target-table
  10176. (org-refile-get-targets default-buffer excluded-entries)))
  10177. (unless org-refile-target-table
  10178. (error "No refile targets"))
  10179. (let* ((cbuf (current-buffer))
  10180. (partial-completion-mode nil)
  10181. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10182. (cfunc (if (and org-refile-use-outline-path
  10183. org-outline-path-complete-in-steps)
  10184. 'org-olpath-completing-read
  10185. 'org-icompleting-read))
  10186. (extra (if org-refile-use-outline-path "/" ""))
  10187. (cbnex (concat (buffer-name) extra))
  10188. (filename (and cfn (expand-file-name cfn)))
  10189. (tbl (mapcar
  10190. (lambda (x)
  10191. (if (and (not (member org-refile-use-outline-path
  10192. '(file full-file-path)))
  10193. (not (equal filename (nth 1 x))))
  10194. (cons (concat (car x) extra " ("
  10195. (file-name-nondirectory (nth 1 x)) ")")
  10196. (cdr x))
  10197. (cons (concat (car x) extra) (cdr x))))
  10198. org-refile-target-table))
  10199. (completion-ignore-case t)
  10200. cdef
  10201. (prompt (concat prompt
  10202. (or (and (car org-refile-history)
  10203. (concat " (default " (car org-refile-history) ")"))
  10204. (and (assoc cbnex tbl) (setq cdef cbnex)
  10205. (concat " (default " cbnex ")"))) ": "))
  10206. pa answ parent-target child parent old-hist)
  10207. (setq old-hist org-refile-history)
  10208. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10209. nil 'org-refile-history (or cdef (car org-refile-history))))
  10210. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10211. (org-refile-check-position pa)
  10212. (if pa
  10213. (progn
  10214. (when (or (not org-refile-history)
  10215. (not (eq old-hist org-refile-history))
  10216. (not (equal (car pa) (car org-refile-history))))
  10217. (setq org-refile-history
  10218. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10219. org-refile-history
  10220. (cdr org-refile-history))))
  10221. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10222. (pop org-refile-history)))
  10223. pa)
  10224. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10225. (progn
  10226. (setq parent (match-string 1 answ)
  10227. child (match-string 2 answ))
  10228. (setq parent-target (or (assoc parent tbl)
  10229. (assoc (concat parent "/") tbl)))
  10230. (when (and parent-target
  10231. (or (eq new-nodes t)
  10232. (and (eq new-nodes 'confirm)
  10233. (y-or-n-p (format "Create new node \"%s\"? "
  10234. child)))))
  10235. (org-refile-new-child parent-target child)))
  10236. (error "Invalid target location")))))
  10237. (declare-function org-string-nw-p "org-macs" (s))
  10238. (defun org-refile-check-position (refile-pointer)
  10239. "Check if the refile pointer matches the headline to which it points."
  10240. (let* ((file (nth 1 refile-pointer))
  10241. (re (nth 2 refile-pointer))
  10242. (pos (nth 3 refile-pointer))
  10243. buffer)
  10244. (if (and (not (markerp pos)) (not file))
  10245. (error "Please save the buffer to a file before refiling")
  10246. (when (org-string-nw-p re)
  10247. (setq buffer (if (markerp pos)
  10248. (marker-buffer pos)
  10249. (or (find-buffer-visiting file)
  10250. (find-file-noselect file))))
  10251. (with-current-buffer buffer
  10252. (save-excursion
  10253. (save-restriction
  10254. (widen)
  10255. (goto-char pos)
  10256. (beginning-of-line 1)
  10257. (unless (org-looking-at-p re)
  10258. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10259. (defun org-refile-new-child (parent-target child)
  10260. "Use refile target PARENT-TARGET to add new CHILD below it."
  10261. (unless parent-target
  10262. (error "Cannot find parent for new node"))
  10263. (let ((file (nth 1 parent-target))
  10264. (pos (nth 3 parent-target))
  10265. level)
  10266. (with-current-buffer (or (find-buffer-visiting file)
  10267. (find-file-noselect file))
  10268. (save-excursion
  10269. (save-restriction
  10270. (widen)
  10271. (if pos
  10272. (goto-char pos)
  10273. (goto-char (point-max))
  10274. (if (not (bolp)) (newline)))
  10275. (when (looking-at org-outline-regexp)
  10276. (setq level (funcall outline-level))
  10277. (org-end-of-subtree t t))
  10278. (org-back-over-empty-lines)
  10279. (insert "\n" (make-string
  10280. (if pos (org-get-valid-level level 1) 1) ?*)
  10281. " " child "\n")
  10282. (beginning-of-line 0)
  10283. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10284. (defun org-olpath-completing-read (prompt collection &rest args)
  10285. "Read an outline path like a file name."
  10286. (let ((thetable collection)
  10287. (org-completion-use-ido nil) ; does not work with ido.
  10288. (org-completion-use-iswitchb nil)) ; or iswitchb
  10289. (apply
  10290. 'org-icompleting-read prompt
  10291. (lambda (string predicate &optional flag)
  10292. (let (rtn r f (l (length string)))
  10293. (cond
  10294. ((eq flag nil)
  10295. ;; try completion
  10296. (try-completion string thetable))
  10297. ((eq flag t)
  10298. ;; all-completions
  10299. (setq rtn (all-completions string thetable predicate))
  10300. (mapcar
  10301. (lambda (x)
  10302. (setq r (substring x l))
  10303. (if (string-match " ([^)]*)$" x)
  10304. (setq f (match-string 0 x))
  10305. (setq f ""))
  10306. (if (string-match "/" r)
  10307. (concat string (substring r 0 (match-end 0)) f)
  10308. x))
  10309. rtn))
  10310. ((eq flag 'lambda)
  10311. ;; exact match?
  10312. (assoc string thetable)))))
  10313. args)))
  10314. ;;;; Dynamic blocks
  10315. (defun org-find-dblock (name)
  10316. "Find the first dynamic block with name NAME in the buffer.
  10317. If not found, stay at current position and return nil."
  10318. (let ((case-fold-search t) pos)
  10319. (save-excursion
  10320. (goto-char (point-min))
  10321. (setq pos (and (re-search-forward
  10322. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10323. (match-beginning 0))))
  10324. (if pos (goto-char pos))
  10325. pos))
  10326. (defconst org-dblock-start-re
  10327. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10328. "Matches the start line of a dynamic block, with parameters.")
  10329. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10330. "Matches the end of a dynamic block.")
  10331. (defun org-create-dblock (plist)
  10332. "Create a dynamic block section, with parameters taken from PLIST.
  10333. PLIST must contain a :name entry which is used as name of the block."
  10334. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10335. (end-of-line 1)
  10336. (newline))
  10337. (let ((col (current-column))
  10338. (name (plist-get plist :name)))
  10339. (insert "#+BEGIN: " name)
  10340. (while plist
  10341. (if (eq (car plist) :name)
  10342. (setq plist (cddr plist))
  10343. (insert " " (prin1-to-string (pop plist)))))
  10344. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10345. (beginning-of-line -2)))
  10346. (defun org-prepare-dblock ()
  10347. "Prepare dynamic block for refresh.
  10348. This empties the block, puts the cursor at the insert position and returns
  10349. the property list including an extra property :name with the block name."
  10350. (unless (looking-at org-dblock-start-re)
  10351. (error "Not at a dynamic block"))
  10352. (let* ((begdel (1+ (match-end 0)))
  10353. (name (org-no-properties (match-string 1)))
  10354. (params (append (list :name name)
  10355. (read (concat "(" (match-string 3) ")")))))
  10356. (save-excursion
  10357. (beginning-of-line 1)
  10358. (skip-chars-forward " \t")
  10359. (setq params (plist-put params :indentation-column (current-column))))
  10360. (unless (re-search-forward org-dblock-end-re nil t)
  10361. (error "Dynamic block not terminated"))
  10362. (setq params
  10363. (append params
  10364. (list :content (buffer-substring
  10365. begdel (match-beginning 0)))))
  10366. (delete-region begdel (match-beginning 0))
  10367. (goto-char begdel)
  10368. (open-line 1)
  10369. params))
  10370. (defun org-map-dblocks (&optional command)
  10371. "Apply COMMAND to all dynamic blocks in the current buffer.
  10372. If COMMAND is not given, use `org-update-dblock'."
  10373. (let ((cmd (or command 'org-update-dblock)))
  10374. (save-excursion
  10375. (goto-char (point-min))
  10376. (while (re-search-forward org-dblock-start-re nil t)
  10377. (goto-char (match-beginning 0))
  10378. (save-excursion
  10379. (condition-case nil
  10380. (funcall cmd)
  10381. (error (message "Error during update of dynamic block"))))
  10382. (unless (re-search-forward org-dblock-end-re nil t)
  10383. (error "Dynamic block not terminated"))))))
  10384. (defun org-dblock-update (&optional arg)
  10385. "User command for updating dynamic blocks.
  10386. Update the dynamic block at point. With prefix ARG, update all dynamic
  10387. blocks in the buffer."
  10388. (interactive "P")
  10389. (if arg
  10390. (org-update-all-dblocks)
  10391. (or (looking-at org-dblock-start-re)
  10392. (org-beginning-of-dblock))
  10393. (org-update-dblock)))
  10394. (defun org-update-dblock ()
  10395. "Update the dynamic block at point.
  10396. This means to empty the block, parse for parameters and then call
  10397. the correct writing function."
  10398. (interactive)
  10399. (save-window-excursion
  10400. (let* ((pos (point))
  10401. (line (org-current-line))
  10402. (params (org-prepare-dblock))
  10403. (name (plist-get params :name))
  10404. (indent (plist-get params :indentation-column))
  10405. (cmd (intern (concat "org-dblock-write:" name))))
  10406. (message "Updating dynamic block `%s' at line %d..." name line)
  10407. (funcall cmd params)
  10408. (message "Updating dynamic block `%s' at line %d...done" name line)
  10409. (goto-char pos)
  10410. (when (and indent (> indent 0))
  10411. (setq indent (make-string indent ?\ ))
  10412. (save-excursion
  10413. (org-beginning-of-dblock)
  10414. (forward-line 1)
  10415. (while (not (looking-at org-dblock-end-re))
  10416. (insert indent)
  10417. (beginning-of-line 2))
  10418. (when (looking-at org-dblock-end-re)
  10419. (and (looking-at "[ \t]+")
  10420. (replace-match ""))
  10421. (insert indent)))))))
  10422. (defun org-beginning-of-dblock ()
  10423. "Find the beginning of the dynamic block at point.
  10424. Error if there is no such block at point."
  10425. (let ((pos (point))
  10426. beg)
  10427. (end-of-line 1)
  10428. (if (and (re-search-backward org-dblock-start-re nil t)
  10429. (setq beg (match-beginning 0))
  10430. (re-search-forward org-dblock-end-re nil t)
  10431. (> (match-end 0) pos))
  10432. (goto-char beg)
  10433. (goto-char pos)
  10434. (error "Not in a dynamic block"))))
  10435. (defun org-update-all-dblocks ()
  10436. "Update all dynamic blocks in the buffer.
  10437. This function can be used in a hook."
  10438. (interactive)
  10439. (when (derived-mode-p 'org-mode)
  10440. (org-map-dblocks 'org-update-dblock)))
  10441. ;;;; Completion
  10442. (defun org-get-export-keywords ()
  10443. "Return a list of all currently understood export keywords.
  10444. Export keywords include options, block names, attributes and
  10445. keywords relative to each registered export back-end."
  10446. (delq nil
  10447. (let (keywords)
  10448. (mapc
  10449. (lambda (back-end)
  10450. (let ((props (cdr back-end)))
  10451. ;; Back-end name (for keywords, like #+LATEX:)
  10452. (push (upcase (symbol-name (car back-end))) keywords)
  10453. ;; Back-end options.
  10454. (mapc (lambda (option) (push (cadr option) keywords))
  10455. (plist-get (cdr back-end) :options-alist))))
  10456. (org-bound-and-true-p org-export-registered-backends))
  10457. keywords)))
  10458. (defconst org-options-keywords
  10459. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10460. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10461. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10462. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10463. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10464. (defcustom org-structure-template-alist
  10465. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10466. "<src lang=\"?\">\n\n</src>")
  10467. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10468. "<example>\n?\n</example>")
  10469. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10470. "<quote>\n?\n</quote>")
  10471. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10472. "<verse>\n?\n</verse>")
  10473. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10474. "<verbatim>\n?\n</verbatim>")
  10475. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10476. "<center>\n?\n</center>")
  10477. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10478. "<literal style=\"latex\">\n?\n</literal>")
  10479. ("L" "#+LaTeX: "
  10480. "<literal style=\"latex\">?</literal>")
  10481. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10482. "<literal style=\"html\">\n?\n</literal>")
  10483. ("H" "#+HTML: "
  10484. "<literal style=\"html\">?</literal>")
  10485. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10486. ("A" "#+ASCII: ")
  10487. ("i" "#+INDEX: ?"
  10488. "#+INDEX: ?")
  10489. ("I" "#+INCLUDE: %file ?"
  10490. "<include file=%file markup=\"?\">"))
  10491. "Structure completion elements.
  10492. This is a list of abbreviation keys and values. The value gets inserted
  10493. if you type `<' followed by the key and then press the completion key,
  10494. usually `M-TAB'. %file will be replaced by a file name after prompting
  10495. for the file using completion. The cursor will be placed at the position
  10496. of the `?` in the template.
  10497. There are two templates for each key, the first uses the original Org syntax,
  10498. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10499. the default when the /org-mtags.el/ module has been loaded. See also the
  10500. variable `org-mtags-prefer-muse-templates'."
  10501. :group 'org-completion
  10502. :type '(repeat
  10503. (string :tag "Key")
  10504. (string :tag "Template")
  10505. (string :tag "Muse Template")))
  10506. (defun org-try-structure-completion ()
  10507. "Try to complete a structure template before point.
  10508. This looks for strings like \"<e\" on an otherwise empty line and
  10509. expands them."
  10510. (let ((l (buffer-substring (point-at-bol) (point)))
  10511. a)
  10512. (when (and (looking-at "[ \t]*$")
  10513. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10514. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10515. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10516. (match-beginning 1)) a)
  10517. t)))
  10518. (defun org-complete-expand-structure-template (start cell)
  10519. "Expand a structure template."
  10520. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10521. (rpl (nth (if musep 2 1) cell))
  10522. (ind ""))
  10523. (delete-region start (point))
  10524. (when (string-match "\\`#\\+" rpl)
  10525. (cond
  10526. ((bolp))
  10527. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10528. (setq ind (buffer-substring (point-at-bol) (point))))
  10529. (t (newline))))
  10530. (setq start (point))
  10531. (if (string-match "%file" rpl)
  10532. (setq rpl (replace-match
  10533. (concat
  10534. "\""
  10535. (save-match-data
  10536. (abbreviate-file-name (read-file-name "Include file: ")))
  10537. "\"")
  10538. t t rpl)))
  10539. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10540. (concat "\n" ind)))
  10541. (insert rpl)
  10542. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10543. ;;;; TODO, DEADLINE, Comments
  10544. (defun org-toggle-comment ()
  10545. "Change the COMMENT state of an entry."
  10546. (interactive)
  10547. (save-excursion
  10548. (org-back-to-heading)
  10549. (let (case-fold-search)
  10550. (cond
  10551. ((looking-at (format org-heading-keyword-regexp-format
  10552. org-comment-string))
  10553. (goto-char (match-end 1))
  10554. (looking-at (concat " +" org-comment-string))
  10555. (replace-match "" t t)
  10556. (when (eolp) (insert " ")))
  10557. ((looking-at org-outline-regexp)
  10558. (goto-char (match-end 0))
  10559. (insert org-comment-string " "))))))
  10560. (defvar org-last-todo-state-is-todo nil
  10561. "This is non-nil when the last TODO state change led to a TODO state.
  10562. If the last change removed the TODO tag or switched to DONE, then
  10563. this is nil.")
  10564. (defvar org-setting-tags nil) ; dynamically skipped
  10565. (defvar org-todo-setup-filter-hook nil
  10566. "Hook for functions that pre-filter todo specs.
  10567. Each function takes a todo spec and returns either nil or the spec
  10568. transformed into canonical form." )
  10569. (defvar org-todo-get-default-hook nil
  10570. "Hook for functions that get a default item for todo.
  10571. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10572. nil or a string to be used for the todo mark." )
  10573. (defvar org-agenda-headline-snapshot-before-repeat)
  10574. (defun org-current-effective-time ()
  10575. "Return current time adjusted for `org-extend-today-until' variable."
  10576. (let* ((ct (org-current-time))
  10577. (dct (decode-time ct))
  10578. (ct1
  10579. (cond
  10580. (org-use-last-clock-out-time-as-effective-time
  10581. (or (org-clock-get-last-clock-out-time) ct))
  10582. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10583. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10584. (t ct))))
  10585. ct1))
  10586. (defun org-todo-yesterday (&optional arg)
  10587. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10588. (interactive "P")
  10589. (if (eq major-mode 'org-agenda-mode)
  10590. (apply 'org-agenda-todo-yesterday arg)
  10591. (let* ((hour (third (decode-time
  10592. (org-current-time))))
  10593. (org-extend-today-until (1+ hour)))
  10594. (org-todo arg))))
  10595. (defvar org-block-entry-blocking ""
  10596. "First entry preventing the TODO state change.")
  10597. (defun org-todo (&optional arg)
  10598. "Change the TODO state of an item.
  10599. The state of an item is given by a keyword at the start of the heading,
  10600. like
  10601. *** TODO Write paper
  10602. *** DONE Call mom
  10603. The different keywords are specified in the variable `org-todo-keywords'.
  10604. By default the available states are \"TODO\" and \"DONE\".
  10605. So for this example: when the item starts with TODO, it is changed to DONE.
  10606. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10607. DONE are present, add TODO at the beginning of the heading.
  10608. With \\[universal-argument] prefix arg, use completion to determine the new \
  10609. state.
  10610. With numeric prefix arg, switch to that state.
  10611. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10612. keywords (nextset).
  10613. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10614. With a numeric prefix arg of 0, inhibit note taking for the change.
  10615. For calling through lisp, arg is also interpreted in the following way:
  10616. 'none -> empty state
  10617. \"\"(empty string) -> switch to empty state
  10618. 'done -> switch to DONE
  10619. 'nextset -> switch to the next set of keywords
  10620. 'previousset -> switch to the previous set of keywords
  10621. \"WAITING\" -> switch to the specified keyword, but only if it
  10622. really is a member of `org-todo-keywords'."
  10623. (interactive "P")
  10624. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10625. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10626. 'region-start-level 'region))
  10627. org-loop-over-headlines-in-active-region)
  10628. (org-map-entries
  10629. `(org-todo ,arg)
  10630. org-loop-over-headlines-in-active-region
  10631. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10632. (if (equal arg '(16)) (setq arg 'nextset))
  10633. (let ((org-blocker-hook org-blocker-hook)
  10634. commentp
  10635. case-fold-search)
  10636. (when (equal arg '(64))
  10637. (setq arg nil org-blocker-hook nil))
  10638. (when (and org-blocker-hook
  10639. (or org-inhibit-blocking
  10640. (org-entry-get nil "NOBLOCKING")))
  10641. (setq org-blocker-hook nil))
  10642. (save-excursion
  10643. (catch 'exit
  10644. (org-back-to-heading t)
  10645. (when (looking-at (concat "^\\*+ " org-comment-string))
  10646. (org-toggle-comment)
  10647. (setq commentp t))
  10648. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10649. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10650. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10651. (let* ((match-data (match-data))
  10652. (startpos (point-at-bol))
  10653. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10654. (org-log-done org-log-done)
  10655. (org-log-repeat org-log-repeat)
  10656. (org-todo-log-states org-todo-log-states)
  10657. (org-inhibit-logging
  10658. (if (equal arg 0)
  10659. (progn (setq arg nil) 'note) org-inhibit-logging))
  10660. (this (match-string 1))
  10661. (hl-pos (match-beginning 0))
  10662. (head (org-get-todo-sequence-head this))
  10663. (ass (assoc head org-todo-kwd-alist))
  10664. (interpret (nth 1 ass))
  10665. (done-word (nth 3 ass))
  10666. (final-done-word (nth 4 ass))
  10667. (org-last-state (or this ""))
  10668. (completion-ignore-case t)
  10669. (member (member this org-todo-keywords-1))
  10670. (tail (cdr member))
  10671. (org-state (cond
  10672. ((and org-todo-key-trigger
  10673. (or (and (equal arg '(4))
  10674. (eq org-use-fast-todo-selection 'prefix))
  10675. (and (not arg) org-use-fast-todo-selection
  10676. (not (eq org-use-fast-todo-selection
  10677. 'prefix)))))
  10678. ;; Use fast selection
  10679. (org-fast-todo-selection))
  10680. ((and (equal arg '(4))
  10681. (or (not org-use-fast-todo-selection)
  10682. (not org-todo-key-trigger)))
  10683. ;; Read a state with completion
  10684. (org-icompleting-read
  10685. "State: " (mapcar (lambda(x) (list x))
  10686. org-todo-keywords-1)
  10687. nil t))
  10688. ((eq arg 'right)
  10689. (if this
  10690. (if tail (car tail) nil)
  10691. (car org-todo-keywords-1)))
  10692. ((eq arg 'left)
  10693. (if (equal member org-todo-keywords-1)
  10694. nil
  10695. (if this
  10696. (nth (- (length org-todo-keywords-1)
  10697. (length tail) 2)
  10698. org-todo-keywords-1)
  10699. (org-last org-todo-keywords-1))))
  10700. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10701. (setq arg nil))) ; hack to fall back to cycling
  10702. (arg
  10703. ;; user or caller requests a specific state
  10704. (cond
  10705. ((equal arg "") nil)
  10706. ((eq arg 'none) nil)
  10707. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10708. ((eq arg 'nextset)
  10709. (or (car (cdr (member head org-todo-heads)))
  10710. (car org-todo-heads)))
  10711. ((eq arg 'previousset)
  10712. (let ((org-todo-heads (reverse org-todo-heads)))
  10713. (or (car (cdr (member head org-todo-heads)))
  10714. (car org-todo-heads))))
  10715. ((car (member arg org-todo-keywords-1)))
  10716. ((stringp arg)
  10717. (error "State `%s' not valid in this file" arg))
  10718. ((nth (1- (prefix-numeric-value arg))
  10719. org-todo-keywords-1))))
  10720. ((null member) (or head (car org-todo-keywords-1)))
  10721. ((equal this final-done-word) nil) ;; -> make empty
  10722. ((null tail) nil) ;; -> first entry
  10723. ((memq interpret '(type priority))
  10724. (if (eq this-command last-command)
  10725. (car tail)
  10726. (if (> (length tail) 0)
  10727. (or done-word (car org-done-keywords))
  10728. nil)))
  10729. (t
  10730. (car tail))))
  10731. (org-state (or
  10732. (run-hook-with-args-until-success
  10733. 'org-todo-get-default-hook org-state org-last-state)
  10734. org-state))
  10735. (next (if org-state (concat " " org-state " ") " "))
  10736. (change-plist (list :type 'todo-state-change :from this :to org-state
  10737. :position startpos))
  10738. dolog now-done-p)
  10739. (when org-blocker-hook
  10740. (setq org-last-todo-state-is-todo
  10741. (not (member this org-done-keywords)))
  10742. (unless (save-excursion
  10743. (save-match-data
  10744. (org-with-wide-buffer
  10745. (run-hook-with-args-until-failure
  10746. 'org-blocker-hook change-plist))))
  10747. (if (org-called-interactively-p 'interactive)
  10748. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10749. this org-state org-block-entry-blocking)
  10750. ;; fail silently
  10751. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10752. this org-state org-block-entry-blocking)
  10753. (throw 'exit nil))))
  10754. (store-match-data match-data)
  10755. (replace-match next t t)
  10756. (unless (pos-visible-in-window-p hl-pos)
  10757. (message "TODO state changed to %s" (org-trim next)))
  10758. (unless head
  10759. (setq head (org-get-todo-sequence-head org-state)
  10760. ass (assoc head org-todo-kwd-alist)
  10761. interpret (nth 1 ass)
  10762. done-word (nth 3 ass)
  10763. final-done-word (nth 4 ass)))
  10764. (when (memq arg '(nextset previousset))
  10765. (message "Keyword-Set %d/%d: %s"
  10766. (- (length org-todo-sets) -1
  10767. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10768. (length org-todo-sets)
  10769. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10770. (setq org-last-todo-state-is-todo
  10771. (not (member org-state org-done-keywords)))
  10772. (setq now-done-p (and (member org-state org-done-keywords)
  10773. (not (member this org-done-keywords))))
  10774. (and logging (org-local-logging logging))
  10775. (when (and (or org-todo-log-states org-log-done)
  10776. (not (eq org-inhibit-logging t))
  10777. (not (memq arg '(nextset previousset))))
  10778. ;; we need to look at recording a time and note
  10779. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10780. (nth 2 (assoc this org-todo-log-states))))
  10781. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10782. (setq dolog 'time))
  10783. (when (and org-state
  10784. (member org-state org-not-done-keywords)
  10785. (not (member this org-not-done-keywords)))
  10786. ;; This is now a todo state and was not one before
  10787. ;; If there was a CLOSED time stamp, get rid of it.
  10788. (org-add-planning-info nil nil 'closed))
  10789. (when (and now-done-p org-log-done)
  10790. ;; It is now done, and it was not done before
  10791. (org-add-planning-info 'closed (org-current-effective-time))
  10792. (if (and (not dolog) (eq 'note org-log-done))
  10793. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10794. (when (and org-state dolog)
  10795. ;; This is a non-nil state, and we need to log it
  10796. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10797. ;; Fixup tag positioning
  10798. (org-todo-trigger-tag-changes org-state)
  10799. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10800. (when org-provide-todo-statistics
  10801. (org-update-parent-todo-statistics))
  10802. (run-hooks 'org-after-todo-state-change-hook)
  10803. (if (and arg (not (member org-state org-done-keywords)))
  10804. (setq head (org-get-todo-sequence-head org-state)))
  10805. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10806. ;; Do we need to trigger a repeat?
  10807. (when now-done-p
  10808. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10809. ;; This is for the agenda, take a snapshot of the headline.
  10810. (save-match-data
  10811. (setq org-agenda-headline-snapshot-before-repeat
  10812. (org-get-heading))))
  10813. (org-auto-repeat-maybe org-state))
  10814. ;; Fixup cursor location if close to the keyword
  10815. (if (and (outline-on-heading-p)
  10816. (not (bolp))
  10817. (save-excursion (beginning-of-line 1)
  10818. (looking-at org-todo-line-regexp))
  10819. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10820. (progn
  10821. (goto-char (or (match-end 2) (match-end 1)))
  10822. (and (looking-at " ") (just-one-space))))
  10823. (when org-trigger-hook
  10824. (save-excursion
  10825. (run-hook-with-args 'org-trigger-hook change-plist)))
  10826. (when commentp (org-toggle-comment))))))))
  10827. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10828. "Block turning an entry into a TODO, using the hierarchy.
  10829. This checks whether the current task should be blocked from state
  10830. changes. Such blocking occurs when:
  10831. 1. The task has children which are not all in a completed state.
  10832. 2. A task has a parent with the property :ORDERED:, and there
  10833. are siblings prior to the current task with incomplete
  10834. status.
  10835. 3. The parent of the task is blocked because it has siblings that should
  10836. be done first, or is child of a block grandparent TODO entry."
  10837. (if (not org-enforce-todo-dependencies)
  10838. t ; if locally turned off don't block
  10839. (catch 'dont-block
  10840. ;; If this is not a todo state change, or if this entry is already DONE,
  10841. ;; do not block
  10842. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10843. (member (plist-get change-plist :from)
  10844. (cons 'done org-done-keywords))
  10845. (member (plist-get change-plist :to)
  10846. (cons 'todo org-not-done-keywords))
  10847. (not (plist-get change-plist :to)))
  10848. (throw 'dont-block t))
  10849. ;; If this task has children, and any are undone, it's blocked
  10850. (save-excursion
  10851. (org-back-to-heading t)
  10852. (let ((this-level (funcall outline-level)))
  10853. (outline-next-heading)
  10854. (let ((child-level (funcall outline-level)))
  10855. (while (and (not (eobp))
  10856. (> child-level this-level))
  10857. ;; this todo has children, check whether they are all
  10858. ;; completed
  10859. (if (and (not (org-entry-is-done-p))
  10860. (org-entry-is-todo-p))
  10861. (progn (setq org-block-entry-blocking (org-get-heading))
  10862. (throw 'dont-block nil)))
  10863. (outline-next-heading)
  10864. (setq child-level (funcall outline-level))))))
  10865. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10866. ;; any previous siblings are undone, it's blocked
  10867. (save-excursion
  10868. (org-back-to-heading t)
  10869. (let* ((pos (point))
  10870. (parent-pos (and (org-up-heading-safe) (point))))
  10871. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10872. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10873. (forward-line 1)
  10874. (re-search-forward org-not-done-heading-regexp pos t))
  10875. (setq org-block-entry-blocking (match-string 0))
  10876. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10877. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10878. (while t
  10879. (goto-char parent-pos)
  10880. (if (not (looking-at org-not-done-heading-regexp))
  10881. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10882. (setq pos (point))
  10883. (setq parent-pos (and (org-up-heading-safe) (point)))
  10884. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10885. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10886. (forward-line 1)
  10887. (re-search-forward org-not-done-heading-regexp pos t)
  10888. (setq org-block-entry-blocking (org-get-heading)))
  10889. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10890. (defcustom org-track-ordered-property-with-tag nil
  10891. "Should the ORDERED property also be shown as a tag?
  10892. The ORDERED property decides if an entry should require subtasks to be
  10893. completed in sequence. Since a property is not very visible, setting
  10894. this option means that toggling the ORDERED property with the command
  10895. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10896. not relevant for the behavior, but it makes things more visible.
  10897. Note that toggling the tag with tags commands will not change the property
  10898. and therefore not influence behavior!
  10899. This can be t, meaning the tag ORDERED should be used, It can also be a
  10900. string to select a different tag for this task."
  10901. :group 'org-todo
  10902. :type '(choice
  10903. (const :tag "No tracking" nil)
  10904. (const :tag "Track with ORDERED tag" t)
  10905. (string :tag "Use other tag")))
  10906. (defun org-toggle-ordered-property ()
  10907. "Toggle the ORDERED property of the current entry.
  10908. For better visibility, you can track the value of this property with a tag.
  10909. See variable `org-track-ordered-property-with-tag'."
  10910. (interactive)
  10911. (let* ((t1 org-track-ordered-property-with-tag)
  10912. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10913. (save-excursion
  10914. (org-back-to-heading)
  10915. (if (org-entry-get nil "ORDERED")
  10916. (progn
  10917. (org-delete-property "ORDERED")
  10918. (and tag (org-toggle-tag tag 'off))
  10919. (message "Subtasks can be completed in arbitrary order"))
  10920. (org-entry-put nil "ORDERED" "t")
  10921. (and tag (org-toggle-tag tag 'on))
  10922. (message "Subtasks must be completed in sequence")))))
  10923. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10924. (defun org-block-todo-from-checkboxes (change-plist)
  10925. "Block turning an entry into a TODO, using checkboxes.
  10926. This checks whether the current task should be blocked from state
  10927. changes because there are unchecked boxes in this entry."
  10928. (if (not org-enforce-todo-checkbox-dependencies)
  10929. t ; if locally turned off don't block
  10930. (catch 'dont-block
  10931. ;; If this is not a todo state change, or if this entry is already DONE,
  10932. ;; do not block
  10933. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10934. (member (plist-get change-plist :from)
  10935. (cons 'done org-done-keywords))
  10936. (member (plist-get change-plist :to)
  10937. (cons 'todo org-not-done-keywords))
  10938. (not (plist-get change-plist :to)))
  10939. (throw 'dont-block t))
  10940. ;; If this task has checkboxes that are not checked, it's blocked
  10941. (save-excursion
  10942. (org-back-to-heading t)
  10943. (let ((beg (point)) end)
  10944. (outline-next-heading)
  10945. (setq end (point))
  10946. (goto-char beg)
  10947. (if (org-list-search-forward
  10948. (concat (org-item-beginning-re)
  10949. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10950. "\\[[- ]\\]")
  10951. end t)
  10952. (progn
  10953. (if (boundp 'org-blocked-by-checkboxes)
  10954. (setq org-blocked-by-checkboxes t))
  10955. (throw 'dont-block nil)))))
  10956. t))) ; do not block
  10957. (defun org-entry-blocked-p ()
  10958. "Is the current entry blocked?"
  10959. (org-with-buffer-modified-unmodified
  10960. (if (org-entry-get nil "NOBLOCKING")
  10961. nil ;; Never block this entry
  10962. (not
  10963. (run-hook-with-args-until-failure
  10964. 'org-blocker-hook
  10965. (list :type 'todo-state-change
  10966. :position (point)
  10967. :from 'todo
  10968. :to 'done))))))
  10969. (defun org-update-statistics-cookies (all)
  10970. "Update the statistics cookie, either from TODO or from checkboxes.
  10971. This should be called with the cursor in a line with a statistics cookie."
  10972. (interactive "P")
  10973. (if all
  10974. (progn
  10975. (org-update-checkbox-count 'all)
  10976. (org-map-entries 'org-update-parent-todo-statistics))
  10977. (if (not (org-at-heading-p))
  10978. (org-update-checkbox-count)
  10979. (let ((pos (point-marker))
  10980. end l1 l2)
  10981. (ignore-errors (org-back-to-heading t))
  10982. (if (not (org-at-heading-p))
  10983. (org-update-checkbox-count)
  10984. (setq l1 (org-outline-level))
  10985. (setq end (save-excursion
  10986. (outline-next-heading)
  10987. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10988. (point)))
  10989. (if (and (save-excursion
  10990. (re-search-forward
  10991. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10992. (not (save-excursion (re-search-forward
  10993. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10994. (org-update-checkbox-count)
  10995. (if (and l2 (> l2 l1))
  10996. (progn
  10997. (goto-char end)
  10998. (org-update-parent-todo-statistics))
  10999. (goto-char pos)
  11000. (beginning-of-line 1)
  11001. (while (re-search-forward
  11002. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11003. (point-at-eol) t)
  11004. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11005. (goto-char pos)
  11006. (move-marker pos nil)))))
  11007. (defvar org-entry-property-inherited-from) ;; defined below
  11008. (defun org-update-parent-todo-statistics ()
  11009. "Update any statistics cookie in the parent of the current headline.
  11010. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11011. the entire subtree and this will travel up the hierarchy and update
  11012. statistics everywhere."
  11013. (let* ((prop (save-excursion (org-up-heading-safe)
  11014. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11015. (recursive (or (not org-hierarchical-todo-statistics)
  11016. (and prop (string-match "\\<recursive\\>" prop))))
  11017. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11018. 0))
  11019. (first t)
  11020. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11021. level ltoggle l1 new ndel
  11022. (cnt-all 0) (cnt-done 0) is-percent kwd
  11023. checkbox-beg ov ovs ove cookie-present)
  11024. (catch 'exit
  11025. (save-excursion
  11026. (beginning-of-line 1)
  11027. (setq ltoggle (funcall outline-level))
  11028. ;; Three situations are to consider:
  11029. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11030. ;; to the top-level ancestor on the headline;
  11031. ;; 2. If parent has "recursive" property, repeat up to the
  11032. ;; headline setting that property, taking inheritance into
  11033. ;; account;
  11034. ;; 3. Else, move up to direct parent and proceed only once.
  11035. (while (and (setq level (org-up-heading-safe))
  11036. (or recursive first)
  11037. (>= (point) lim))
  11038. (setq first nil cookie-present nil)
  11039. (unless (and level
  11040. (not (string-match
  11041. "\\<checkbox\\>"
  11042. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11043. "")))))
  11044. (throw 'exit nil))
  11045. (while (re-search-forward box-re (point-at-eol) t)
  11046. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11047. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11048. (save-match-data
  11049. (unless (outline-next-heading) (throw 'exit nil))
  11050. (while (and (looking-at org-complex-heading-regexp)
  11051. (> (setq l1 (length (match-string 1))) level))
  11052. (setq kwd (and (or recursive (= l1 ltoggle))
  11053. (match-string 2)))
  11054. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11055. (and (listp org-provide-todo-statistics)
  11056. (or (member kwd org-provide-todo-statistics)
  11057. (member kwd org-done-keywords))))
  11058. (setq cnt-all (1+ cnt-all))
  11059. (if (eq org-provide-todo-statistics t)
  11060. (and kwd (setq cnt-all (1+ cnt-all)))))
  11061. (and (member kwd org-done-keywords)
  11062. (setq cnt-done (1+ cnt-done)))
  11063. (outline-next-heading)))
  11064. (setq new
  11065. (if is-percent
  11066. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11067. (format "[%d/%d]" cnt-done cnt-all))
  11068. ndel (- (match-end 0) checkbox-beg))
  11069. ;; handle overlays when updating cookie from column view
  11070. (when (setq ov (car (overlays-at checkbox-beg)))
  11071. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11072. (delete-overlay ov))
  11073. (goto-char checkbox-beg)
  11074. (insert new)
  11075. (delete-region (point) (+ (point) ndel))
  11076. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11077. (when ov (move-overlay ov ovs ove)))
  11078. (when cookie-present
  11079. (run-hook-with-args 'org-after-todo-statistics-hook
  11080. cnt-done (- cnt-all cnt-done))))))
  11081. (run-hooks 'org-todo-statistics-hook)))
  11082. (defvar org-after-todo-statistics-hook nil
  11083. "Hook that is called after a TODO statistics cookie has been updated.
  11084. Each function is called with two arguments: the number of not-done entries
  11085. and the number of done entries.
  11086. For example, the following function, when added to this hook, will switch
  11087. an entry to DONE when all children are done, and back to TODO when new
  11088. entries are set to a TODO status. Note that this hook is only called
  11089. when there is a statistics cookie in the headline!
  11090. (defun org-summary-todo (n-done n-not-done)
  11091. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11092. (let (org-log-done org-log-states) ; turn off logging
  11093. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11094. ")
  11095. (defvar org-todo-statistics-hook nil
  11096. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11097. This hook runs even if there is no statistics cookie present, in which case
  11098. `org-after-todo-statistics-hook' would not run.")
  11099. (defun org-todo-trigger-tag-changes (state)
  11100. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11101. (let ((l org-todo-state-tags-triggers)
  11102. changes)
  11103. (when (or (not state) (equal state ""))
  11104. (setq changes (append changes (cdr (assoc "" l)))))
  11105. (when (and (stringp state) (> (length state) 0))
  11106. (setq changes (append changes (cdr (assoc state l)))))
  11107. (when (member state org-not-done-keywords)
  11108. (setq changes (append changes (cdr (assoc 'todo l)))))
  11109. (when (member state org-done-keywords)
  11110. (setq changes (append changes (cdr (assoc 'done l)))))
  11111. (dolist (c changes)
  11112. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11113. (defun org-local-logging (value)
  11114. "Get logging settings from a property VALUE."
  11115. (let* (words w a)
  11116. ;; directly set the variables, they are already local.
  11117. (setq org-log-done nil
  11118. org-log-repeat nil
  11119. org-todo-log-states nil)
  11120. (setq words (org-split-string value))
  11121. (while (setq w (pop words))
  11122. (cond
  11123. ((setq a (assoc w org-startup-options))
  11124. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11125. (set (nth 1 a) (nth 2 a))))
  11126. ((setq a (org-extract-log-state-settings w))
  11127. (and (member (car a) org-todo-keywords-1)
  11128. (push a org-todo-log-states)))))))
  11129. (defun org-get-todo-sequence-head (kwd)
  11130. "Return the head of the TODO sequence to which KWD belongs.
  11131. If KWD is not set, check if there is a text property remembering the
  11132. right sequence."
  11133. (let (p)
  11134. (cond
  11135. ((not kwd)
  11136. (or (get-text-property (point-at-bol) 'org-todo-head)
  11137. (progn
  11138. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11139. nil (point-at-eol)))
  11140. (get-text-property p 'org-todo-head))))
  11141. ((not (member kwd org-todo-keywords-1))
  11142. (car org-todo-keywords-1))
  11143. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11144. (defun org-fast-todo-selection ()
  11145. "Fast TODO keyword selection with single keys.
  11146. Returns the new TODO keyword, or nil if no state change should occur."
  11147. (let* ((fulltable org-todo-key-alist)
  11148. (done-keywords org-done-keywords) ;; needed for the faces.
  11149. (maxlen (apply 'max (mapcar
  11150. (lambda (x)
  11151. (if (stringp (car x)) (string-width (car x)) 0))
  11152. fulltable)))
  11153. (expert nil)
  11154. (fwidth (+ maxlen 3 1 3))
  11155. (ncol (/ (- (window-width) 4) fwidth))
  11156. tg cnt e c tbl
  11157. groups ingroup)
  11158. (save-excursion
  11159. (save-window-excursion
  11160. (if expert
  11161. (set-buffer (get-buffer-create " *Org todo*"))
  11162. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11163. (erase-buffer)
  11164. (org-set-local 'org-done-keywords done-keywords)
  11165. (setq tbl fulltable cnt 0)
  11166. (while (setq e (pop tbl))
  11167. (cond
  11168. ((equal e '(:startgroup))
  11169. (push '() groups) (setq ingroup t)
  11170. (when (not (= cnt 0))
  11171. (setq cnt 0)
  11172. (insert "\n"))
  11173. (insert "{ "))
  11174. ((equal e '(:endgroup))
  11175. (setq ingroup nil cnt 0)
  11176. (insert "}\n"))
  11177. ((equal e '(:newline))
  11178. (when (not (= cnt 0))
  11179. (setq cnt 0)
  11180. (insert "\n")
  11181. (setq e (car tbl))
  11182. (while (equal (car tbl) '(:newline))
  11183. (insert "\n")
  11184. (setq tbl (cdr tbl)))))
  11185. (t
  11186. (setq tg (car e) c (cdr e))
  11187. (if ingroup (push tg (car groups)))
  11188. (setq tg (org-add-props tg nil 'face
  11189. (org-get-todo-face tg)))
  11190. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11191. (insert "[" c "] " tg (make-string
  11192. (- fwidth 4 (length tg)) ?\ ))
  11193. (when (= (setq cnt (1+ cnt)) ncol)
  11194. (insert "\n")
  11195. (if ingroup (insert " "))
  11196. (setq cnt 0)))))
  11197. (insert "\n")
  11198. (goto-char (point-min))
  11199. (if (not expert) (org-fit-window-to-buffer))
  11200. (message "[a-z..]:Set [SPC]:clear")
  11201. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11202. (cond
  11203. ((or (= c ?\C-g)
  11204. (and (= c ?q) (not (rassoc c fulltable))))
  11205. (setq quit-flag t))
  11206. ((= c ?\ ) nil)
  11207. ((setq e (rassoc c fulltable) tg (car e))
  11208. tg)
  11209. (t (setq quit-flag t)))))))
  11210. (defun org-entry-is-todo-p ()
  11211. (member (org-get-todo-state) org-not-done-keywords))
  11212. (defun org-entry-is-done-p ()
  11213. (member (org-get-todo-state) org-done-keywords))
  11214. (defun org-get-todo-state ()
  11215. (save-excursion
  11216. (org-back-to-heading t)
  11217. (and (looking-at org-todo-line-regexp)
  11218. (match-end 2)
  11219. (match-string 2))))
  11220. (defun org-at-date-range-p (&optional inactive-ok)
  11221. "Is the cursor inside a date range?"
  11222. (interactive)
  11223. (save-excursion
  11224. (catch 'exit
  11225. (let ((pos (point)))
  11226. (skip-chars-backward "^[<\r\n")
  11227. (skip-chars-backward "<[")
  11228. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11229. (>= (match-end 0) pos)
  11230. (throw 'exit t))
  11231. (skip-chars-backward "^<[\r\n")
  11232. (skip-chars-backward "<[")
  11233. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11234. (>= (match-end 0) pos)
  11235. (throw 'exit t)))
  11236. nil)))
  11237. (defun org-get-repeat (&optional tagline)
  11238. "Check if there is a deadline/schedule with repeater in this entry."
  11239. (save-match-data
  11240. (save-excursion
  11241. (org-back-to-heading t)
  11242. (and (re-search-forward (if tagline
  11243. (concat tagline "\\s-*" org-repeat-re)
  11244. org-repeat-re)
  11245. (org-entry-end-position) t)
  11246. (match-string-no-properties 1)))))
  11247. (defvar org-last-changed-timestamp)
  11248. (defvar org-last-inserted-timestamp)
  11249. (defvar org-log-post-message)
  11250. (defvar org-log-note-purpose)
  11251. (defvar org-log-note-how)
  11252. (defvar org-log-note-extra)
  11253. (defun org-auto-repeat-maybe (done-word)
  11254. "Check if the current headline contains a repeated deadline/schedule.
  11255. If yes, set TODO state back to what it was and change the base date
  11256. of repeating deadline/scheduled time stamps to new date.
  11257. This function is run automatically after each state change to a DONE state."
  11258. ;; last-state is dynamically scoped into this function
  11259. (let* ((repeat (org-get-repeat))
  11260. (aa (assoc org-last-state org-todo-kwd-alist))
  11261. (interpret (nth 1 aa))
  11262. (head (nth 2 aa))
  11263. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11264. (msg "Entry repeats: ")
  11265. (org-log-done nil)
  11266. (org-todo-log-states nil)
  11267. re type n what ts time to-state)
  11268. (when repeat
  11269. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11270. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11271. org-todo-repeat-to-state))
  11272. (unless (and to-state (member to-state org-todo-keywords-1))
  11273. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11274. (org-todo to-state)
  11275. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11276. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11277. (org-time-stamp-format t t))))
  11278. (when org-log-repeat
  11279. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11280. (memq 'org-add-log-note post-command-hook))
  11281. ;; OK, we are already setup for some record
  11282. (if (eq org-log-repeat 'note)
  11283. ;; make sure we take a note, not only a time stamp
  11284. (setq org-log-note-how 'note))
  11285. ;; Set up for taking a record
  11286. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11287. org-last-state
  11288. 'findpos org-log-repeat)))
  11289. (org-back-to-heading t)
  11290. (org-add-planning-info nil nil 'closed)
  11291. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11292. org-deadline-time-regexp "\\)\\|\\("
  11293. org-ts-regexp "\\)"))
  11294. (while (re-search-forward
  11295. re (save-excursion (outline-next-heading) (point)) t)
  11296. (setq type (if (match-end 1) org-scheduled-string
  11297. (if (match-end 3) org-deadline-string "Plain:"))
  11298. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11299. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11300. (setq n (string-to-number (match-string 2 ts))
  11301. what (match-string 3 ts))
  11302. (if (equal what "w") (setq n (* n 7) what "d"))
  11303. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11304. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11305. ;; Preparation, see if we need to modify the start date for the change
  11306. (when (match-end 1)
  11307. (setq time (save-match-data (org-time-string-to-time ts)))
  11308. (cond
  11309. ((equal (match-string 1 ts) ".")
  11310. ;; Shift starting date to today
  11311. (org-timestamp-change
  11312. (- (org-today) (time-to-days time))
  11313. 'day))
  11314. ((equal (match-string 1 ts) "+")
  11315. (let ((nshiftmax 10) (nshift 0))
  11316. (while (or (= nshift 0)
  11317. (<= (time-to-days time)
  11318. (time-to-days (current-time))))
  11319. (when (= (incf nshift) nshiftmax)
  11320. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11321. (error "Abort")))
  11322. (org-timestamp-change n (cdr (assoc what whata)))
  11323. (org-at-timestamp-p t)
  11324. (setq ts (match-string 1))
  11325. (setq time (save-match-data (org-time-string-to-time ts)))))
  11326. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11327. ;; rematch, so that we have everything in place for the real shift
  11328. (org-at-timestamp-p t)
  11329. (setq ts (match-string 1))
  11330. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11331. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11332. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11333. (setq org-log-post-message msg)
  11334. (message "%s" msg))))
  11335. (defun org-show-todo-tree (arg)
  11336. "Make a compact tree which shows all headlines marked with TODO.
  11337. The tree will show the lines where the regexp matches, and all higher
  11338. headlines above the match.
  11339. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11340. With a numeric prefix N, construct a sparse tree for the Nth element
  11341. of `org-todo-keywords-1'."
  11342. (interactive "P")
  11343. (let ((case-fold-search nil)
  11344. (kwd-re
  11345. (cond ((null arg) org-not-done-regexp)
  11346. ((equal arg '(4))
  11347. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11348. (mapcar 'list org-todo-keywords-1))))
  11349. (concat "\\("
  11350. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11351. "\\)\\>")))
  11352. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11353. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11354. org-todo-keywords-1)))
  11355. (t (error "Invalid prefix argument: %s" arg)))))
  11356. (message "%d TODO entries found"
  11357. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11358. (defun org-deadline (&optional arg time)
  11359. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11360. With one universal prefix argument, remove any deadline from the item.
  11361. With two universal prefix arguments, prompt for a warning delay.
  11362. With argument TIME, set the deadline at the corresponding date. TIME
  11363. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11364. (interactive "P")
  11365. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11366. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11367. 'region-start-level 'region))
  11368. org-loop-over-headlines-in-active-region)
  11369. (org-map-entries
  11370. `(org-deadline ',arg ,time)
  11371. org-loop-over-headlines-in-active-region
  11372. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11373. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11374. (repeater (and old-date
  11375. (string-match
  11376. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11377. old-date)
  11378. (match-string 1 old-date))))
  11379. (cond
  11380. ((equal arg '(4))
  11381. (when (and old-date org-log-redeadline)
  11382. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11383. org-log-redeadline))
  11384. (org-remove-timestamp-with-keyword org-deadline-string)
  11385. (message "Item no longer has a deadline."))
  11386. ((equal arg '(16))
  11387. (save-excursion
  11388. (if (re-search-forward
  11389. org-deadline-time-regexp
  11390. (save-excursion (outline-next-heading) (point)) t)
  11391. (let* ((rpl0 (match-string 1))
  11392. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11393. (replace-match
  11394. (concat org-deadline-string
  11395. " <" rpl
  11396. (format " -%dd" (abs
  11397. (- (time-to-days
  11398. (save-match-data
  11399. (org-read-date nil t nil "Warn starting from")))
  11400. (time-to-days nil))))
  11401. ">") t t))
  11402. (user-error "No deadline information to update"))))
  11403. (t
  11404. (org-add-planning-info 'deadline time 'closed)
  11405. (when (and old-date org-log-redeadline
  11406. (not (equal old-date
  11407. (substring org-last-inserted-timestamp 1 -1))))
  11408. (org-add-log-setup 'redeadline nil old-date 'findpos
  11409. org-log-redeadline))
  11410. (when repeater
  11411. (save-excursion
  11412. (org-back-to-heading t)
  11413. (when (re-search-forward (concat org-deadline-string " "
  11414. org-last-inserted-timestamp)
  11415. (save-excursion
  11416. (outline-next-heading) (point)) t)
  11417. (goto-char (1- (match-end 0)))
  11418. (insert " " repeater)
  11419. (setq org-last-inserted-timestamp
  11420. (concat (substring org-last-inserted-timestamp 0 -1)
  11421. " " repeater
  11422. (substring org-last-inserted-timestamp -1))))))
  11423. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11424. (defun org-schedule (&optional arg time)
  11425. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11426. With one universal prefix argument, remove any scheduling date from the item.
  11427. With two universal prefix arguments, prompt for a delay cookie.
  11428. With argument TIME, scheduled at the corresponding date. TIME can
  11429. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11430. (interactive "P")
  11431. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11432. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11433. 'region-start-level 'region))
  11434. org-loop-over-headlines-in-active-region)
  11435. (org-map-entries
  11436. `(org-schedule ',arg ,time)
  11437. org-loop-over-headlines-in-active-region
  11438. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11439. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11440. (repeater (and old-date
  11441. (string-match
  11442. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11443. old-date)
  11444. (match-string 1 old-date))))
  11445. (cond
  11446. ((equal arg '(4))
  11447. (progn
  11448. (when (and old-date org-log-reschedule)
  11449. (org-add-log-setup 'delschedule nil old-date 'findpos
  11450. org-log-reschedule))
  11451. (org-remove-timestamp-with-keyword org-scheduled-string)
  11452. (message "Item is no longer scheduled.")))
  11453. ((equal arg '(16))
  11454. (save-excursion
  11455. (if (re-search-forward
  11456. org-scheduled-time-regexp
  11457. (save-excursion (outline-next-heading) (point)) t)
  11458. (let* ((rpl0 (match-string 1))
  11459. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11460. (replace-match
  11461. (concat org-scheduled-string
  11462. " <" rpl
  11463. (format " -%dd" (abs
  11464. (- (time-to-days
  11465. (save-match-data
  11466. (org-read-date nil t nil "Delay until")))
  11467. (time-to-days nil))))
  11468. ">") t t))
  11469. (user-error "No scheduled information to update"))))
  11470. (t
  11471. (org-add-planning-info 'scheduled time 'closed)
  11472. (when (and old-date org-log-reschedule
  11473. (not (equal old-date
  11474. (substring org-last-inserted-timestamp 1 -1))))
  11475. (org-add-log-setup 'reschedule nil old-date 'findpos
  11476. org-log-reschedule))
  11477. (when repeater
  11478. (save-excursion
  11479. (org-back-to-heading t)
  11480. (when (re-search-forward (concat org-scheduled-string " "
  11481. org-last-inserted-timestamp)
  11482. (save-excursion
  11483. (outline-next-heading) (point)) t)
  11484. (goto-char (1- (match-end 0)))
  11485. (insert " " repeater)
  11486. (setq org-last-inserted-timestamp
  11487. (concat (substring org-last-inserted-timestamp 0 -1)
  11488. " " repeater
  11489. (substring org-last-inserted-timestamp -1))))))
  11490. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11491. (defun org-get-scheduled-time (pom &optional inherit)
  11492. "Get the scheduled time as a time tuple, of a format suitable
  11493. for calling org-schedule with, or if there is no scheduling,
  11494. returns nil."
  11495. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11496. (when time
  11497. (apply 'encode-time (org-parse-time-string time)))))
  11498. (defun org-get-deadline-time (pom &optional inherit)
  11499. "Get the deadline as a time tuple, of a format suitable for
  11500. calling org-deadline with, or if there is no scheduling, returns
  11501. nil."
  11502. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11503. (when time
  11504. (apply 'encode-time (org-parse-time-string time)))))
  11505. (defun org-remove-timestamp-with-keyword (keyword)
  11506. "Remove all time stamps with KEYWORD in the current entry."
  11507. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11508. beg)
  11509. (save-excursion
  11510. (org-back-to-heading t)
  11511. (setq beg (point))
  11512. (outline-next-heading)
  11513. (while (re-search-backward re beg t)
  11514. (replace-match "")
  11515. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11516. (equal (char-before) ?\ ))
  11517. (backward-delete-char 1)
  11518. (if (string-match "^[ \t]*$" (buffer-substring
  11519. (point-at-bol) (point-at-eol)))
  11520. (delete-region (point-at-bol)
  11521. (min (point-max) (1+ (point-at-eol))))))))))
  11522. (defun org-add-planning-info (what &optional time &rest remove)
  11523. "Insert new timestamp with keyword in the line directly after the headline.
  11524. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11525. If non is given, the user is prompted for a date.
  11526. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11527. be removed."
  11528. (interactive)
  11529. (let (org-time-was-given org-end-time-was-given ts
  11530. end default-time default-input)
  11531. (catch 'exit
  11532. (when (and (memq what '(scheduled deadline))
  11533. (or (not time)
  11534. (and (stringp time)
  11535. (string-match "^[-+]+[0-9]" time))))
  11536. ;; Try to get a default date/time from existing timestamp
  11537. (save-excursion
  11538. (org-back-to-heading t)
  11539. (setq end (save-excursion (outline-next-heading) (point)))
  11540. (when (re-search-forward (if (eq what 'scheduled)
  11541. org-scheduled-time-regexp
  11542. org-deadline-time-regexp)
  11543. end t)
  11544. (setq ts (match-string 1)
  11545. default-time
  11546. (apply 'encode-time (org-parse-time-string ts))
  11547. default-input (and ts (org-get-compact-tod ts))))))
  11548. (when what
  11549. (setq time
  11550. (if (stringp time)
  11551. ;; This is a string (relative or absolute), set proper date
  11552. (apply 'encode-time
  11553. (org-read-date-analyze
  11554. time default-time (decode-time default-time)))
  11555. ;; If necessary, get the time from the user
  11556. (or time (org-read-date nil 'to-time nil nil
  11557. default-time default-input)))))
  11558. (when (and org-insert-labeled-timestamps-at-point
  11559. (member what '(scheduled deadline)))
  11560. (insert
  11561. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11562. (org-insert-time-stamp time org-time-was-given
  11563. nil nil nil (list org-end-time-was-given))
  11564. (setq what nil))
  11565. (save-excursion
  11566. (save-restriction
  11567. (let (col list elt ts buffer-invisibility-spec)
  11568. (org-back-to-heading t)
  11569. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11570. (goto-char (match-end 1))
  11571. (setq col (current-column))
  11572. (goto-char (match-end 0))
  11573. (if (eobp) (insert "\n") (forward-char 1))
  11574. (when (and (not what)
  11575. (not (looking-at
  11576. (concat "[ \t]*"
  11577. org-keyword-time-not-clock-regexp))))
  11578. ;; Nothing to add, nothing to remove...... :-)
  11579. (throw 'exit nil))
  11580. (if (and (not (looking-at org-outline-regexp))
  11581. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11582. "[^\r\n]*"))
  11583. (not (equal (match-string 1) org-clock-string)))
  11584. (narrow-to-region (match-beginning 0) (match-end 0))
  11585. (insert-before-markers "\n")
  11586. (backward-char 1)
  11587. (narrow-to-region (point) (point))
  11588. (and org-adapt-indentation (org-indent-to-column col)))
  11589. ;; Check if we have to remove something.
  11590. (setq list (cons what remove))
  11591. (while list
  11592. (setq elt (pop list))
  11593. (when (or (and (eq elt 'scheduled)
  11594. (re-search-forward org-scheduled-time-regexp nil t))
  11595. (and (eq elt 'deadline)
  11596. (re-search-forward org-deadline-time-regexp nil t))
  11597. (and (eq elt 'closed)
  11598. (re-search-forward org-closed-time-regexp nil t)))
  11599. (replace-match "")
  11600. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11601. (and (looking-at "[ \t]+") (replace-match ""))
  11602. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11603. (when what
  11604. (insert
  11605. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11606. (cond ((eq what 'scheduled) org-scheduled-string)
  11607. ((eq what 'deadline) org-deadline-string)
  11608. ((eq what 'closed) org-closed-string))
  11609. " ")
  11610. (setq ts (org-insert-time-stamp
  11611. time
  11612. (or org-time-was-given
  11613. (and (eq what 'closed) org-log-done-with-time))
  11614. (eq what 'closed)
  11615. nil nil (list org-end-time-was-given)))
  11616. (insert
  11617. (if (not (or (bolp) (eq (char-before) ?\ )
  11618. (memq (char-after) '(32 10))
  11619. (eobp))) " " ""))
  11620. (end-of-line 1))
  11621. (goto-char (point-min))
  11622. (widen)
  11623. (if (and (looking-at "[ \t]*\n")
  11624. (equal (char-before) ?\n))
  11625. (delete-region (1- (point)) (point-at-eol)))
  11626. ts))))))
  11627. (defvar org-log-note-marker (make-marker))
  11628. (defvar org-log-note-purpose nil)
  11629. (defvar org-log-note-state nil)
  11630. (defvar org-log-note-previous-state nil)
  11631. (defvar org-log-note-how nil)
  11632. (defvar org-log-note-extra nil)
  11633. (defvar org-log-note-window-configuration nil)
  11634. (defvar org-log-note-return-to (make-marker))
  11635. (defvar org-log-note-effective-time nil
  11636. "Remembered current time so that dynamically scoped
  11637. `org-extend-today-until' affects tha timestamps in state change
  11638. log")
  11639. (defvar org-log-post-message nil
  11640. "Message to be displayed after a log note has been stored.
  11641. The auto-repeater uses this.")
  11642. (defun org-add-note ()
  11643. "Add a note to the current entry.
  11644. This is done in the same way as adding a state change note."
  11645. (interactive)
  11646. (org-add-log-setup 'note nil nil 'findpos nil))
  11647. (defvar org-property-end-re)
  11648. (defun org-add-log-setup (&optional purpose state prev-state
  11649. findpos how extra)
  11650. "Set up the post command hook to take a note.
  11651. If this is about to TODO state change, the new state is expected in STATE.
  11652. When FINDPOS is non-nil, find the correct position for the note in
  11653. the current entry. If not, assume that it can be inserted at point.
  11654. HOW is an indicator what kind of note should be created.
  11655. EXTRA is additional text that will be inserted into the notes buffer."
  11656. (let* ((org-log-into-drawer (org-log-into-drawer))
  11657. (drawer (cond ((stringp org-log-into-drawer)
  11658. org-log-into-drawer)
  11659. (org-log-into-drawer "LOGBOOK"))))
  11660. (save-restriction
  11661. (save-excursion
  11662. (when findpos
  11663. (org-back-to-heading t)
  11664. (narrow-to-region (point) (save-excursion
  11665. (outline-next-heading) (point)))
  11666. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11667. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11668. "[^\r\n]*\\)?"))
  11669. (goto-char (match-end 0))
  11670. (cond
  11671. (drawer
  11672. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11673. nil t)
  11674. (progn
  11675. (goto-char (match-end 0))
  11676. (or org-log-states-order-reversed
  11677. (and (re-search-forward org-property-end-re nil t)
  11678. (goto-char (1- (match-beginning 0))))))
  11679. (insert "\n:" drawer ":\n:END:")
  11680. (beginning-of-line 0)
  11681. (org-indent-line)
  11682. (beginning-of-line 2)
  11683. (org-indent-line)
  11684. (end-of-line 0)))
  11685. ((and org-log-state-notes-insert-after-drawers
  11686. (save-excursion
  11687. (forward-line) (looking-at org-drawer-regexp)))
  11688. (forward-line)
  11689. (while (looking-at org-drawer-regexp)
  11690. (goto-char (match-end 0))
  11691. (re-search-forward org-property-end-re (point-max) t)
  11692. (forward-line))
  11693. (forward-line -1)))
  11694. (unless org-log-states-order-reversed
  11695. (and (= (char-after) ?\n) (forward-char 1))
  11696. (org-skip-over-state-notes)
  11697. (skip-chars-backward " \t\n\r")))
  11698. (move-marker org-log-note-marker (point))
  11699. (setq org-log-note-purpose purpose
  11700. org-log-note-state state
  11701. org-log-note-previous-state prev-state
  11702. org-log-note-how how
  11703. org-log-note-extra extra
  11704. org-log-note-effective-time (org-current-effective-time))
  11705. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11706. (defun org-skip-over-state-notes ()
  11707. "Skip past the list of State notes in an entry."
  11708. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11709. (when (ignore-errors (goto-char (org-in-item-p)))
  11710. (let* ((struct (org-list-struct))
  11711. (prevs (org-list-prevs-alist struct)))
  11712. (while (looking-at "[ \t]*- State")
  11713. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11714. (org-list-get-item-end (point) struct)))))))
  11715. (defun org-add-log-note (&optional purpose)
  11716. "Pop up a window for taking a note, and add this note later at point."
  11717. (remove-hook 'post-command-hook 'org-add-log-note)
  11718. (setq org-log-note-window-configuration (current-window-configuration))
  11719. (delete-other-windows)
  11720. (move-marker org-log-note-return-to (point))
  11721. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11722. (goto-char org-log-note-marker)
  11723. (org-switch-to-buffer-other-window "*Org Note*")
  11724. (erase-buffer)
  11725. (if (memq org-log-note-how '(time state))
  11726. (let (current-prefix-arg) (org-store-log-note))
  11727. (let ((org-inhibit-startup t)) (org-mode))
  11728. (insert (format "# Insert note for %s.
  11729. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11730. (cond
  11731. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11732. ((eq org-log-note-purpose 'done) "closed todo item")
  11733. ((eq org-log-note-purpose 'state)
  11734. (format "state change from \"%s\" to \"%s\""
  11735. (or org-log-note-previous-state "")
  11736. (or org-log-note-state "")))
  11737. ((eq org-log-note-purpose 'reschedule)
  11738. "rescheduling")
  11739. ((eq org-log-note-purpose 'delschedule)
  11740. "no longer scheduled")
  11741. ((eq org-log-note-purpose 'redeadline)
  11742. "changing deadline")
  11743. ((eq org-log-note-purpose 'deldeadline)
  11744. "removing deadline")
  11745. ((eq org-log-note-purpose 'refile)
  11746. "refiling")
  11747. ((eq org-log-note-purpose 'note)
  11748. "this entry")
  11749. (t (error "This should not happen")))))
  11750. (if org-log-note-extra (insert org-log-note-extra))
  11751. (org-set-local 'org-finish-function 'org-store-log-note)
  11752. (run-hooks 'org-log-buffer-setup-hook)))
  11753. (defvar org-note-abort nil) ; dynamically scoped
  11754. (defun org-store-log-note ()
  11755. "Finish taking a log note, and insert it to where it belongs."
  11756. (let ((txt (buffer-string)))
  11757. (kill-buffer (current-buffer))
  11758. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11759. lines ind bul)
  11760. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11761. (setq txt (replace-match "" t t txt)))
  11762. (if (string-match "\\s-+\\'" txt)
  11763. (setq txt (replace-match "" t t txt)))
  11764. (setq lines (org-split-string txt "\n"))
  11765. (when (and note (string-match "\\S-" note))
  11766. (setq note
  11767. (org-replace-escapes
  11768. note
  11769. (list (cons "%u" (user-login-name))
  11770. (cons "%U" user-full-name)
  11771. (cons "%t" (format-time-string
  11772. (org-time-stamp-format 'long 'inactive)
  11773. org-log-note-effective-time))
  11774. (cons "%T" (format-time-string
  11775. (org-time-stamp-format 'long nil)
  11776. org-log-note-effective-time))
  11777. (cons "%d" (format-time-string
  11778. (org-time-stamp-format nil 'inactive)
  11779. org-log-note-effective-time))
  11780. (cons "%D" (format-time-string
  11781. (org-time-stamp-format nil nil)
  11782. org-log-note-effective-time))
  11783. (cons "%s" (if org-log-note-state
  11784. (concat "\"" org-log-note-state "\"")
  11785. ""))
  11786. (cons "%S" (if org-log-note-previous-state
  11787. (concat "\"" org-log-note-previous-state "\"")
  11788. "\"\"")))))
  11789. (if lines (setq note (concat note " \\\\")))
  11790. (push note lines))
  11791. (when (or current-prefix-arg org-note-abort)
  11792. (when org-log-into-drawer
  11793. (org-remove-empty-drawer-at
  11794. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11795. org-log-note-marker))
  11796. (setq lines nil))
  11797. (when lines
  11798. (with-current-buffer (marker-buffer org-log-note-marker)
  11799. (save-excursion
  11800. (goto-char org-log-note-marker)
  11801. (move-marker org-log-note-marker nil)
  11802. (end-of-line 1)
  11803. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11804. (setq ind (save-excursion
  11805. (if (ignore-errors (goto-char (org-in-item-p)))
  11806. (let ((struct (org-list-struct)))
  11807. (org-list-get-ind
  11808. (org-list-get-top-point struct) struct))
  11809. (skip-chars-backward " \r\t\n")
  11810. (cond
  11811. ((and (org-at-heading-p)
  11812. org-adapt-indentation)
  11813. (1+ (org-current-level)))
  11814. ((org-at-heading-p) 0)
  11815. (t (org-get-indentation))))))
  11816. (setq bul (org-list-bullet-string "-"))
  11817. (org-indent-line-to ind)
  11818. (insert bul (pop lines))
  11819. (let ((ind-body (+ (length bul) ind)))
  11820. (while lines
  11821. (insert "\n")
  11822. (org-indent-line-to ind-body)
  11823. (insert (pop lines))))
  11824. (message "Note stored")
  11825. (org-back-to-heading t)
  11826. (org-cycle-hide-drawers 'children))))))
  11827. (set-window-configuration org-log-note-window-configuration)
  11828. (with-current-buffer (marker-buffer org-log-note-return-to)
  11829. (goto-char org-log-note-return-to))
  11830. (move-marker org-log-note-return-to nil)
  11831. (and org-log-post-message (message "%s" org-log-post-message)))
  11832. (defun org-remove-empty-drawer-at (drawer pos)
  11833. "Remove an empty drawer DRAWER at position POS.
  11834. POS may also be a marker."
  11835. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11836. (save-excursion
  11837. (save-restriction
  11838. (widen)
  11839. (goto-char pos)
  11840. (if (org-in-regexp
  11841. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11842. (replace-match ""))))))
  11843. (defvar org-ts-type nil)
  11844. (defun org-sparse-tree (&optional arg type)
  11845. "Create a sparse tree, prompt for the details.
  11846. This command can create sparse trees. You first need to select the type
  11847. of match used to create the tree:
  11848. t Show all TODO entries.
  11849. T Show entries with a specific TODO keyword.
  11850. m Show entries selected by a tags/property match.
  11851. p Enter a property name and its value (both with completion on existing
  11852. names/values) and show entries with that property.
  11853. r Show entries matching a regular expression (`/' can be used as well).
  11854. b Show deadlines and scheduled items before a date.
  11855. a Show deadlines and scheduled items after a date.
  11856. d Show deadlines due within `org-deadline-warning-days'.
  11857. D Show deadlines and scheduled items between a date range."
  11858. (interactive "P")
  11859. (let (ans kwd value ts-type)
  11860. (setq type (or type org-sparse-tree-default-date-type))
  11861. (setq org-ts-type type)
  11862. (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"
  11863. (cond ((eq type 'all) "all timestamps")
  11864. ((eq type 'scheduled) "only scheduled")
  11865. ((eq type 'deadline) "only deadline")
  11866. ((eq type 'active) "only active timestamps")
  11867. ((eq type 'inactive) "only inactive timestamps")
  11868. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11869. (t "scheduled/deadline")))
  11870. (setq ans (read-char-exclusive))
  11871. (cond
  11872. ((equal ans ?c)
  11873. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11874. ((equal ans ?d)
  11875. (call-interactively 'org-check-deadlines))
  11876. ((equal ans ?b)
  11877. (call-interactively 'org-check-before-date))
  11878. ((equal ans ?a)
  11879. (call-interactively 'org-check-after-date))
  11880. ((equal ans ?D)
  11881. (call-interactively 'org-check-dates-range))
  11882. ((equal ans ?t)
  11883. (call-interactively 'org-show-todo-tree))
  11884. ((equal ans ?T)
  11885. (org-show-todo-tree '(4)))
  11886. ((member ans '(?T ?m))
  11887. (call-interactively 'org-match-sparse-tree))
  11888. ((member ans '(?p ?P))
  11889. (setq kwd (org-icompleting-read "Property: "
  11890. (mapcar 'list (org-buffer-property-keys))))
  11891. (setq value (org-icompleting-read "Value: "
  11892. (mapcar 'list (org-property-values kwd))))
  11893. (unless (string-match "\\`{.*}\\'" value)
  11894. (setq value (concat "\"" value "\"")))
  11895. (org-match-sparse-tree arg (concat kwd "=" value)))
  11896. ((member ans '(?r ?R ?/))
  11897. (call-interactively 'org-occur))
  11898. (t (error "No such sparse tree command \"%c\"" ans)))))
  11899. (defvar org-occur-highlights nil
  11900. "List of overlays used for occur matches.")
  11901. (make-variable-buffer-local 'org-occur-highlights)
  11902. (defvar org-occur-parameters nil
  11903. "Parameters of the active org-occur calls.
  11904. This is a list, each call to org-occur pushes as cons cell,
  11905. containing the regular expression and the callback, onto the list.
  11906. The list can contain several entries if `org-occur' has been called
  11907. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11908. will only contain one set of parameters. When the highlights are
  11909. removed (for example with `C-c C-c', or with the next edit (depending
  11910. on `org-remove-highlights-with-change'), this variable is emptied
  11911. as well.")
  11912. (make-variable-buffer-local 'org-occur-parameters)
  11913. (defun org-occur (regexp &optional keep-previous callback)
  11914. "Make a compact tree which shows all matches of REGEXP.
  11915. The tree will show the lines where the regexp matches, and all higher
  11916. headlines above the match. It will also show the heading after the match,
  11917. to make sure editing the matching entry is easy.
  11918. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11919. call to `org-occur' will be kept, to allow stacking of calls to this
  11920. command.
  11921. If CALLBACK is non-nil, it is a function which is called to confirm
  11922. that the match should indeed be shown."
  11923. (interactive "sRegexp: \nP")
  11924. (when (equal regexp "")
  11925. (error "Regexp cannot be empty"))
  11926. (unless keep-previous
  11927. (org-remove-occur-highlights nil nil t))
  11928. (push (cons regexp callback) org-occur-parameters)
  11929. (let ((cnt 0))
  11930. (save-excursion
  11931. (goto-char (point-min))
  11932. (if (or (not keep-previous) ; do not want to keep
  11933. (not org-occur-highlights)) ; no previous matches
  11934. ;; hide everything
  11935. (org-overview))
  11936. (while (re-search-forward regexp nil t)
  11937. (when (or (not callback)
  11938. (save-match-data (funcall callback)))
  11939. (setq cnt (1+ cnt))
  11940. (when org-highlight-sparse-tree-matches
  11941. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11942. (org-show-context 'occur-tree))))
  11943. (when org-remove-highlights-with-change
  11944. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11945. nil 'local))
  11946. (unless org-sparse-tree-open-archived-trees
  11947. (org-hide-archived-subtrees (point-min) (point-max)))
  11948. (run-hooks 'org-occur-hook)
  11949. (if (org-called-interactively-p 'interactive)
  11950. (message "%d match(es) for regexp %s" cnt regexp))
  11951. cnt))
  11952. (defun org-occur-next-match (&optional n reset)
  11953. "Function for `next-error-function' to find sparse tree matches.
  11954. N is the number of matches to move, when negative move backwards.
  11955. RESET is entirely ignored - this function always goes back to the
  11956. starting point when no match is found."
  11957. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11958. (search-func (if (< n 0)
  11959. 'previous-single-char-property-change
  11960. 'next-single-char-property-change))
  11961. (n (abs n))
  11962. (pos (point))
  11963. p1)
  11964. (catch 'exit
  11965. (while (setq p1 (funcall search-func (point) 'org-type))
  11966. (when (equal p1 limit)
  11967. (goto-char pos)
  11968. (error "No more matches"))
  11969. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11970. (setq n (1- n))
  11971. (when (= n 0)
  11972. (goto-char p1)
  11973. (throw 'exit (point))))
  11974. (goto-char p1))
  11975. (goto-char p1)
  11976. (error "No more matches"))))
  11977. (defun org-show-context (&optional key)
  11978. "Make sure point and context are visible.
  11979. How much context is shown depends upon the variables
  11980. `org-show-hierarchy-above', `org-show-following-heading',
  11981. `org-show-entry-below' and `org-show-siblings'."
  11982. (let ((heading-p (org-at-heading-p t))
  11983. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11984. (following-p (org-get-alist-option org-show-following-heading key))
  11985. (entry-p (org-get-alist-option org-show-entry-below key))
  11986. (siblings-p (org-get-alist-option org-show-siblings key)))
  11987. (catch 'exit
  11988. ;; Show heading or entry text
  11989. (if (and heading-p (not entry-p))
  11990. (org-flag-heading nil) ; only show the heading
  11991. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11992. (org-show-hidden-entry))) ; show entire entry
  11993. (when following-p
  11994. ;; Show next sibling, or heading below text
  11995. (save-excursion
  11996. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11997. (org-flag-heading nil))))
  11998. (when siblings-p (org-show-siblings))
  11999. (when hierarchy-p
  12000. ;; show all higher headings, possibly with siblings
  12001. (save-excursion
  12002. (while (and (condition-case nil
  12003. (progn (org-up-heading-all 1) t)
  12004. (error nil))
  12005. (not (bobp)))
  12006. (org-flag-heading nil)
  12007. (when siblings-p (org-show-siblings))))))))
  12008. (defvar org-reveal-start-hook nil
  12009. "Hook run before revealing a location.")
  12010. (defun org-reveal (&optional siblings)
  12011. "Show current entry, hierarchy above it, and the following headline.
  12012. This can be used to show a consistent set of context around locations
  12013. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12014. not t for the search context.
  12015. With optional argument SIBLINGS, on each level of the hierarchy all
  12016. siblings are shown. This repairs the tree structure to what it would
  12017. look like when opened with hierarchical calls to `org-cycle'.
  12018. With double optional argument \\[universal-argument] \\[universal-argument], \
  12019. go to the parent and show the
  12020. entire tree."
  12021. (interactive "P")
  12022. (run-hooks 'org-reveal-start-hook)
  12023. (let ((org-show-hierarchy-above t)
  12024. (org-show-following-heading t)
  12025. (org-show-siblings (if siblings t org-show-siblings)))
  12026. (org-show-context nil))
  12027. (when (equal siblings '(16))
  12028. (save-excursion
  12029. (when (org-up-heading-safe)
  12030. (org-show-subtree)
  12031. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12032. (defun org-highlight-new-match (beg end)
  12033. "Highlight from BEG to END and mark the highlight is an occur headline."
  12034. (let ((ov (make-overlay beg end)))
  12035. (overlay-put ov 'face 'secondary-selection)
  12036. (overlay-put ov 'org-type 'org-occur)
  12037. (push ov org-occur-highlights)))
  12038. (defun org-remove-occur-highlights (&optional beg end noremove)
  12039. "Remove the occur highlights from the buffer.
  12040. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12041. from the `before-change-functions' in the current buffer."
  12042. (interactive)
  12043. (unless org-inhibit-highlight-removal
  12044. (mapc 'delete-overlay org-occur-highlights)
  12045. (setq org-occur-highlights nil)
  12046. (setq org-occur-parameters nil)
  12047. (unless noremove
  12048. (remove-hook 'before-change-functions
  12049. 'org-remove-occur-highlights 'local))))
  12050. ;;;; Priorities
  12051. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12052. "Regular expression matching the priority indicator.")
  12053. (defvar org-remove-priority-next-time nil)
  12054. (defun org-priority-up ()
  12055. "Increase the priority of the current item."
  12056. (interactive)
  12057. (org-priority 'up))
  12058. (defun org-priority-down ()
  12059. "Decrease the priority of the current item."
  12060. (interactive)
  12061. (org-priority 'down))
  12062. (defun org-priority (&optional action show)
  12063. "Change the priority of an item.
  12064. ACTION can be `set', `up', `down', or a character."
  12065. (interactive "P")
  12066. (if (equal action '(4))
  12067. (org-show-priority)
  12068. (unless org-enable-priority-commands
  12069. (error "Priority commands are disabled"))
  12070. (setq action (or action 'set))
  12071. (let (current new news have remove)
  12072. (save-excursion
  12073. (org-back-to-heading t)
  12074. (if (looking-at org-priority-regexp)
  12075. (setq current (string-to-char (match-string 2))
  12076. have t))
  12077. (cond
  12078. ((eq action 'remove)
  12079. (setq remove t new ?\ ))
  12080. ((or (eq action 'set)
  12081. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12082. (if (not (eq action 'set))
  12083. (setq new action)
  12084. (message "Priority %c-%c, SPC to remove: "
  12085. org-highest-priority org-lowest-priority)
  12086. (save-match-data
  12087. (setq new (read-char-exclusive))))
  12088. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12089. (= (upcase org-lowest-priority) org-lowest-priority))
  12090. (setq new (upcase new)))
  12091. (cond ((equal new ?\ ) (setq remove t))
  12092. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12093. (error "Priority must be between `%c' and `%c'"
  12094. org-highest-priority org-lowest-priority))))
  12095. ((eq action 'up)
  12096. (setq new (if have
  12097. (1- current) ; normal cycling
  12098. ;; last priority was empty
  12099. (if (eq last-command this-command)
  12100. org-lowest-priority ; wrap around empty to lowest
  12101. ;; default
  12102. (if org-priority-start-cycle-with-default
  12103. org-default-priority
  12104. (1- org-default-priority))))))
  12105. ((eq action 'down)
  12106. (setq new (if have
  12107. (1+ current) ; normal cycling
  12108. ;; last priority was empty
  12109. (if (eq last-command this-command)
  12110. org-highest-priority ; wrap around empty to highest
  12111. ;; default
  12112. (if org-priority-start-cycle-with-default
  12113. org-default-priority
  12114. (1+ org-default-priority))))))
  12115. (t (error "Invalid action")))
  12116. (if (or (< (upcase new) org-highest-priority)
  12117. (> (upcase new) org-lowest-priority))
  12118. (if (and (memq action '(up down))
  12119. (not have) (not (eq last-command this-command)))
  12120. ;; `new' is from default priority
  12121. (error
  12122. "The default can not be set, see `org-default-priority' why")
  12123. ;; normal cycling: `new' is beyond highest/lowest priority
  12124. ;; and is wrapped around to the empty priority
  12125. (setq remove t)))
  12126. (setq news (format "%c" new))
  12127. (if have
  12128. (if remove
  12129. (replace-match "" t t nil 1)
  12130. (replace-match news t t nil 2))
  12131. (if remove
  12132. (error "No priority cookie found in line")
  12133. (let ((case-fold-search nil))
  12134. (looking-at org-todo-line-regexp))
  12135. (if (match-end 2)
  12136. (progn
  12137. (goto-char (match-end 2))
  12138. (insert " [#" news "]"))
  12139. (goto-char (match-beginning 3))
  12140. (insert "[#" news "] "))))
  12141. (org-preserve-lc (org-set-tags nil 'align)))
  12142. (if remove
  12143. (message "Priority removed")
  12144. (message "Priority of current item set to %s" news)))))
  12145. (defun org-show-priority ()
  12146. "Show the priority of the current item.
  12147. This priority is composed of the main priority given with the [#A] cookies,
  12148. and by additional input from the age of a schedules or deadline entry."
  12149. (interactive)
  12150. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12151. (org-get-at-bol 'priority)
  12152. (save-excursion
  12153. (save-match-data
  12154. (beginning-of-line)
  12155. (and (looking-at org-heading-regexp)
  12156. (org-get-priority (match-string 0))))))))
  12157. (message "Priority is %d" (if pri pri -1000))))
  12158. (defun org-get-priority (s)
  12159. "Find priority cookie and return priority."
  12160. (save-match-data
  12161. (if (functionp org-get-priority-function)
  12162. (funcall org-get-priority-function)
  12163. (if (not (string-match org-priority-regexp s))
  12164. (* 1000 (- org-lowest-priority org-default-priority))
  12165. (* 1000 (- org-lowest-priority
  12166. (string-to-char (match-string 2 s))))))))
  12167. ;;;; Tags
  12168. (defvar org-agenda-archives-mode)
  12169. (defvar org-map-continue-from nil
  12170. "Position from where mapping should continue.
  12171. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12172. (defvar org-scanner-tags nil
  12173. "The current tag list while the tags scanner is running.")
  12174. (defvar org-trust-scanner-tags nil
  12175. "Should `org-get-tags-at' use the tags for the scanner.
  12176. This is for internal dynamical scoping only.
  12177. When this is non-nil, the function `org-get-tags-at' will return the value
  12178. of `org-scanner-tags' instead of building the list by itself. This
  12179. can lead to large speed-ups when the tags scanner is used in a file with
  12180. many entries, and when the list of tags is retrieved, for example to
  12181. obtain a list of properties. Building the tags list for each entry in such
  12182. a file becomes an N^2 operation - but with this variable set, it scales
  12183. as N.")
  12184. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12185. "Scan headline tags with inheritance and produce output ACTION.
  12186. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12187. or `agenda' to produce an entry list for an agenda view. It can also be
  12188. a Lisp form or a function that should be called at each matched headline, in
  12189. this case the return value is a list of all return values from these calls.
  12190. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12191. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12192. only lines with a not-done TODO keyword are included in the output.
  12193. This should be the same variable that was scoped into
  12194. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12195. START-LEVEL can be a string with asterisks, reducing the scope to
  12196. headlines matching this string."
  12197. (require 'org-agenda)
  12198. (let* ((re (concat "^"
  12199. (if start-level
  12200. ;; Get the correct level to match
  12201. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12202. org-outline-regexp)
  12203. " *\\(\\<\\("
  12204. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12205. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12206. (props (list 'face 'default
  12207. 'done-face 'org-agenda-done
  12208. 'undone-face 'default
  12209. 'mouse-face 'highlight
  12210. 'org-not-done-regexp org-not-done-regexp
  12211. 'org-todo-regexp org-todo-regexp
  12212. 'org-complex-heading-regexp org-complex-heading-regexp
  12213. 'help-echo
  12214. (format "mouse-2 or RET jump to org file %s"
  12215. (abbreviate-file-name
  12216. (or (buffer-file-name (buffer-base-buffer))
  12217. (buffer-name (buffer-base-buffer)))))))
  12218. (case-fold-search nil)
  12219. (org-map-continue-from nil)
  12220. lspos tags tags-list
  12221. (tags-alist (list (cons 0 org-file-tags)))
  12222. (llast 0) rtn rtn1 level category i txt
  12223. todo marker entry priority)
  12224. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12225. (setq action (list 'lambda nil action)))
  12226. (save-excursion
  12227. (goto-char (point-min))
  12228. (when (eq action 'sparse-tree)
  12229. (org-overview)
  12230. (org-remove-occur-highlights))
  12231. (while (re-search-forward re nil t)
  12232. (setq org-map-continue-from nil)
  12233. (catch :skip
  12234. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12235. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12236. (goto-char (setq lspos (match-beginning 0)))
  12237. (setq level (org-reduced-level (org-outline-level))
  12238. category (org-get-category))
  12239. (setq i llast llast level)
  12240. ;; remove tag lists from same and sublevels
  12241. (while (>= i level)
  12242. (when (setq entry (assoc i tags-alist))
  12243. (setq tags-alist (delete entry tags-alist)))
  12244. (setq i (1- i)))
  12245. ;; add the next tags
  12246. (when tags
  12247. (setq tags (org-split-string tags ":")
  12248. tags-alist
  12249. (cons (cons level tags) tags-alist)))
  12250. ;; compile tags for current headline
  12251. (setq tags-list
  12252. (if org-use-tag-inheritance
  12253. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12254. tags)
  12255. org-scanner-tags tags-list)
  12256. (when org-use-tag-inheritance
  12257. (setcdr (car tags-alist)
  12258. (mapcar (lambda (x)
  12259. (setq x (copy-sequence x))
  12260. (org-add-prop-inherited x))
  12261. (cdar tags-alist))))
  12262. (when (and tags org-use-tag-inheritance
  12263. (or (not (eq t org-use-tag-inheritance))
  12264. org-tags-exclude-from-inheritance))
  12265. ;; selective inheritance, remove uninherited ones
  12266. (setcdr (car tags-alist)
  12267. (org-remove-uninherited-tags (cdar tags-alist))))
  12268. (when (and
  12269. ;; eval matcher only when the todo condition is OK
  12270. (and (or (not todo-only) (member todo org-not-done-keywords))
  12271. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12272. (eval matcher)))
  12273. ;; Call the skipper, but return t if it does not skip,
  12274. ;; so that the `and' form continues evaluating
  12275. (progn
  12276. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12277. t)
  12278. ;; Check if timestamps are deselecting this entry
  12279. (or (not todo-only)
  12280. (and (member todo org-not-done-keywords)
  12281. (or (not org-agenda-tags-todo-honor-ignore-options)
  12282. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12283. ;; select this headline
  12284. (cond
  12285. ((eq action 'sparse-tree)
  12286. (and org-highlight-sparse-tree-matches
  12287. (org-get-heading) (match-end 0)
  12288. (org-highlight-new-match
  12289. (match-beginning 1) (match-end 1)))
  12290. (org-show-context 'tags-tree))
  12291. ((eq action 'agenda)
  12292. (setq txt (org-agenda-format-item
  12293. ""
  12294. (concat
  12295. (if (eq org-tags-match-list-sublevels 'indented)
  12296. (make-string (1- level) ?.) "")
  12297. (org-get-heading))
  12298. level category
  12299. tags-list)
  12300. priority (org-get-priority txt))
  12301. (goto-char lspos)
  12302. (setq marker (org-agenda-new-marker))
  12303. (org-add-props txt props
  12304. 'org-marker marker 'org-hd-marker marker 'org-category category
  12305. 'todo-state todo
  12306. 'priority priority 'type "tagsmatch")
  12307. (push txt rtn))
  12308. ((functionp action)
  12309. (setq org-map-continue-from nil)
  12310. (save-excursion
  12311. (setq rtn1 (funcall action))
  12312. (push rtn1 rtn)))
  12313. (t (error "Invalid action")))
  12314. ;; if we are to skip sublevels, jump to end of subtree
  12315. (unless org-tags-match-list-sublevels
  12316. (org-end-of-subtree t)
  12317. (backward-char 1))))
  12318. ;; Get the correct position from where to continue
  12319. (if org-map-continue-from
  12320. (goto-char org-map-continue-from)
  12321. (and (= (point) lspos) (end-of-line 1)))))
  12322. (when (and (eq action 'sparse-tree)
  12323. (not org-sparse-tree-open-archived-trees))
  12324. (org-hide-archived-subtrees (point-min) (point-max)))
  12325. (nreverse rtn)))
  12326. (defun org-remove-uninherited-tags (tags)
  12327. "Remove all tags that are not inherited from the list TAGS."
  12328. (cond
  12329. ((eq org-use-tag-inheritance t)
  12330. (if org-tags-exclude-from-inheritance
  12331. (org-delete-all org-tags-exclude-from-inheritance tags)
  12332. tags))
  12333. ((not org-use-tag-inheritance) nil)
  12334. ((stringp org-use-tag-inheritance)
  12335. (delq nil (mapcar
  12336. (lambda (x)
  12337. (if (and (string-match org-use-tag-inheritance x)
  12338. (not (member x org-tags-exclude-from-inheritance)))
  12339. x nil))
  12340. tags)))
  12341. ((listp org-use-tag-inheritance)
  12342. (delq nil (mapcar
  12343. (lambda (x)
  12344. (if (member x org-use-tag-inheritance) x nil))
  12345. tags)))))
  12346. (defun org-match-sparse-tree (&optional todo-only match)
  12347. "Create a sparse tree according to tags string MATCH.
  12348. MATCH can contain positive and negative selection of tags, like
  12349. \"+WORK+URGENT-WITHBOSS\".
  12350. If optional argument TODO-ONLY is non-nil, only select lines that are
  12351. also TODO lines."
  12352. (interactive "P")
  12353. (org-agenda-prepare-buffers (list (current-buffer)))
  12354. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12355. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12356. (defvar org-cached-props nil)
  12357. (defun org-cached-entry-get (pom property)
  12358. (if (or (eq t org-use-property-inheritance)
  12359. (and (stringp org-use-property-inheritance)
  12360. (string-match org-use-property-inheritance property))
  12361. (and (listp org-use-property-inheritance)
  12362. (member property org-use-property-inheritance)))
  12363. ;; Caching is not possible, check it directly
  12364. (org-entry-get pom property 'inherit)
  12365. ;; Get all properties, so that we can do complicated checks easily
  12366. (cdr (assoc property (or org-cached-props
  12367. (setq org-cached-props
  12368. (org-entry-properties pom)))))))
  12369. (defun org-global-tags-completion-table (&optional files)
  12370. "Return the list of all tags in all agenda buffer/files.
  12371. Optional FILES argument is a list of files which can be used
  12372. instead of the agenda files."
  12373. (save-excursion
  12374. (org-uniquify
  12375. (delq nil
  12376. (apply 'append
  12377. (mapcar
  12378. (lambda (file)
  12379. (set-buffer (find-file-noselect file))
  12380. (append (org-get-buffer-tags)
  12381. (mapcar (lambda (x) (if (stringp (car-safe x))
  12382. (list (car-safe x)) nil))
  12383. org-tag-alist)))
  12384. (if (and files (car files))
  12385. files
  12386. (org-agenda-files))))))))
  12387. (defun org-make-tags-matcher (match)
  12388. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12389. The variable `todo-only' is scoped dynamically into this function.
  12390. It will be set to t if the matcher restricts matching to TODO entries,
  12391. otherwise will not be touched.
  12392. Returns a cons of the selection string MATCH and the constructed
  12393. lisp form implementing the matcher. The matcher is to be evaluated
  12394. at an Org entry, with point on the headline, and returns t if the
  12395. entry matches the selection string MATCH. The returned lisp form
  12396. references two variables with information about the entry, which
  12397. must be bound around the form's evaluation: todo, the TODO keyword
  12398. at the entry (or nil of none); and tags-list, the list of all tags
  12399. at the entry including inherited ones. Additionally, the category
  12400. of the entry (if any) must be specified as the text property
  12401. 'org-category on the headline.
  12402. See also `org-scan-tags'.
  12403. "
  12404. (declare (special todo-only))
  12405. (unless (boundp 'todo-only)
  12406. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12407. (unless match
  12408. ;; Get a new match request, with completion
  12409. (let ((org-last-tags-completion-table
  12410. (org-global-tags-completion-table)))
  12411. (setq match (org-completing-read-no-i
  12412. "Match: " 'org-tags-completion-function nil nil nil
  12413. 'org-tags-history))))
  12414. ;; Parse the string and create a lisp form
  12415. (let ((match0 match)
  12416. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12417. minus tag mm
  12418. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12419. orterms term orlist re-p str-p level-p level-op time-p
  12420. prop-p pn pv po gv rest)
  12421. (if (string-match "/+" match)
  12422. ;; match contains also a todo-matching request
  12423. (progn
  12424. (setq tagsmatch (substring match 0 (match-beginning 0))
  12425. todomatch (substring match (match-end 0)))
  12426. (if (string-match "^!" todomatch)
  12427. (setq todo-only t todomatch (substring todomatch 1)))
  12428. (if (string-match "^\\s-*$" todomatch)
  12429. (setq todomatch nil)))
  12430. ;; only matching tags
  12431. (setq tagsmatch match todomatch nil))
  12432. ;; Make the tags matcher
  12433. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12434. (setq tagsmatcher t)
  12435. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12436. (while (setq term (pop orterms))
  12437. (while (and (equal (substring term -1) "\\") orterms)
  12438. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12439. (while (string-match re term)
  12440. (setq rest (substring term (match-end 0))
  12441. minus (and (match-end 1)
  12442. (equal (match-string 1 term) "-"))
  12443. tag (save-match-data (replace-regexp-in-string
  12444. "\\\\-" "-"
  12445. (match-string 2 term)))
  12446. re-p (equal (string-to-char tag) ?{)
  12447. level-p (match-end 4)
  12448. prop-p (match-end 5)
  12449. mm (cond
  12450. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12451. (level-p
  12452. (setq level-op (org-op-to-function (match-string 3 term)))
  12453. `(,level-op level ,(string-to-number
  12454. (match-string 4 term))))
  12455. (prop-p
  12456. (setq pn (match-string 5 term)
  12457. po (match-string 6 term)
  12458. pv (match-string 7 term)
  12459. re-p (equal (string-to-char pv) ?{)
  12460. str-p (equal (string-to-char pv) ?\")
  12461. time-p (save-match-data
  12462. (string-match "^\"[[<].*[]>]\"$" pv))
  12463. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12464. (if time-p (setq pv (org-matcher-time pv)))
  12465. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12466. (cond
  12467. ((equal pn "CATEGORY")
  12468. (setq gv '(get-text-property (point) 'org-category)))
  12469. ((equal pn "TODO")
  12470. (setq gv 'todo))
  12471. (t
  12472. (setq gv `(org-cached-entry-get nil ,pn))))
  12473. (if re-p
  12474. (if (eq po 'org<>)
  12475. `(not (string-match ,pv (or ,gv "")))
  12476. `(string-match ,pv (or ,gv "")))
  12477. (if str-p
  12478. `(,po (or ,gv "") ,pv)
  12479. `(,po (string-to-number (or ,gv ""))
  12480. ,(string-to-number pv) ))))
  12481. (t `(member ,tag tags-list)))
  12482. mm (if minus (list 'not mm) mm)
  12483. term rest)
  12484. (push mm tagsmatcher))
  12485. (push (if (> (length tagsmatcher) 1)
  12486. (cons 'and tagsmatcher)
  12487. (car tagsmatcher))
  12488. orlist)
  12489. (setq tagsmatcher nil))
  12490. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12491. (setq tagsmatcher
  12492. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12493. ;; Make the todo matcher
  12494. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12495. (setq todomatcher t)
  12496. (setq orterms (org-split-string todomatch "|") orlist nil)
  12497. (while (setq term (pop orterms))
  12498. (while (string-match re term)
  12499. (setq minus (and (match-end 1)
  12500. (equal (match-string 1 term) "-"))
  12501. kwd (match-string 2 term)
  12502. re-p (equal (string-to-char kwd) ?{)
  12503. term (substring term (match-end 0))
  12504. mm (if re-p
  12505. `(string-match ,(substring kwd 1 -1) todo)
  12506. (list 'equal 'todo kwd))
  12507. mm (if minus (list 'not mm) mm))
  12508. (push mm todomatcher))
  12509. (push (if (> (length todomatcher) 1)
  12510. (cons 'and todomatcher)
  12511. (car todomatcher))
  12512. orlist)
  12513. (setq todomatcher nil))
  12514. (setq todomatcher (if (> (length orlist) 1)
  12515. (cons 'or orlist) (car orlist))))
  12516. ;; Return the string and lisp forms of the matcher
  12517. (setq matcher (if todomatcher
  12518. (list 'and tagsmatcher todomatcher)
  12519. tagsmatcher))
  12520. (when todo-only
  12521. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12522. matcher)))
  12523. (cons match0 matcher)))
  12524. (defun org-op-to-function (op &optional stringp)
  12525. "Turn an operator into the appropriate function."
  12526. (setq op
  12527. (cond
  12528. ((equal op "<" ) '(< string< org-time<))
  12529. ((equal op ">" ) '(> org-string> org-time>))
  12530. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12531. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12532. ((member op '("=" "==")) '(= string= org-time=))
  12533. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12534. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12535. (defun org<> (a b) (not (= a b)))
  12536. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12537. (defun org-string>= (a b) (not (string< a b)))
  12538. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12539. (defun org-string<> (a b) (not (string= a b)))
  12540. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12541. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12542. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= 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) (org<> a b)))
  12546. (defun org-2ft (s)
  12547. "Convert S to a floating point time.
  12548. If S is already a number, just return it. If it is a string, parse
  12549. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12550. (cond
  12551. ((numberp s) s)
  12552. ((stringp s)
  12553. (condition-case nil
  12554. (float-time (apply 'encode-time (org-parse-time-string s)))
  12555. (error 0.)))
  12556. (t 0.)))
  12557. (defun org-time-today ()
  12558. "Time in seconds today at 0:00.
  12559. Returns the float number of seconds since the beginning of the
  12560. epoch to the beginning of today (00:00)."
  12561. (float-time (apply 'encode-time
  12562. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12563. (defun org-matcher-time (s)
  12564. "Interpret a time comparison value."
  12565. (save-match-data
  12566. (cond
  12567. ((string= s "<now>") (float-time))
  12568. ((string= s "<today>") (org-time-today))
  12569. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12570. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12571. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12572. (+ (org-time-today)
  12573. (* (string-to-number (match-string 1 s))
  12574. (cdr (assoc (match-string 2 s)
  12575. '(("d" . 86400.0) ("w" . 604800.0)
  12576. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12577. (t (org-2ft s)))))
  12578. (defun org-match-any-p (re list)
  12579. "Does re match any element of list?"
  12580. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12581. (delq nil list))
  12582. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12583. (defvar org-tags-overlay (make-overlay 1 1))
  12584. (org-detach-overlay org-tags-overlay)
  12585. (defun org-get-local-tags-at (&optional pos)
  12586. "Get a list of tags defined in the current headline."
  12587. (org-get-tags-at pos 'local))
  12588. (defun org-get-local-tags ()
  12589. "Get a list of tags defined in the current headline."
  12590. (org-get-tags-at nil 'local))
  12591. (defun org-get-tags-at (&optional pos local)
  12592. "Get a list of all headline tags applicable at POS.
  12593. POS defaults to point. If tags are inherited, the list contains
  12594. the targets in the same sequence as the headlines appear, i.e.
  12595. the tags of the current headline come last.
  12596. When LOCAL is non-nil, only return tags from the current headline,
  12597. ignore inherited ones."
  12598. (interactive)
  12599. (if (and org-trust-scanner-tags
  12600. (or (not pos) (equal pos (point)))
  12601. (not local))
  12602. org-scanner-tags
  12603. (let (tags ltags lastpos parent)
  12604. (save-excursion
  12605. (save-restriction
  12606. (widen)
  12607. (goto-char (or pos (point)))
  12608. (save-match-data
  12609. (catch 'done
  12610. (condition-case nil
  12611. (progn
  12612. (org-back-to-heading t)
  12613. (while (not (equal lastpos (point)))
  12614. (setq lastpos (point))
  12615. (when (looking-at
  12616. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12617. (setq ltags (org-split-string
  12618. (org-match-string-no-properties 1) ":"))
  12619. (when parent
  12620. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12621. (setq tags (append
  12622. (if parent
  12623. (org-remove-uninherited-tags ltags)
  12624. ltags)
  12625. tags)))
  12626. (or org-use-tag-inheritance (throw 'done t))
  12627. (if local (throw 'done t))
  12628. (or (org-up-heading-safe) (error nil))
  12629. (setq parent t)))
  12630. (error nil)))))
  12631. (if local
  12632. tags
  12633. (reverse (delete-dups
  12634. (reverse (append
  12635. (org-remove-uninherited-tags
  12636. org-file-tags) tags)))))))))
  12637. (defun org-add-prop-inherited (s)
  12638. (add-text-properties 0 (length s) '(inherited t) s)
  12639. s)
  12640. (defun org-toggle-tag (tag &optional onoff)
  12641. "Toggle the tag TAG for the current line.
  12642. If ONOFF is `on' or `off', don't toggle but set to this state."
  12643. (let (res current)
  12644. (save-excursion
  12645. (org-back-to-heading t)
  12646. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12647. (point-at-eol) t)
  12648. (progn
  12649. (setq current (match-string 1))
  12650. (replace-match ""))
  12651. (setq current ""))
  12652. (setq current (nreverse (org-split-string current ":")))
  12653. (cond
  12654. ((eq onoff 'on)
  12655. (setq res t)
  12656. (or (member tag current) (push tag current)))
  12657. ((eq onoff 'off)
  12658. (or (not (member tag current)) (setq current (delete tag current))))
  12659. (t (if (member tag current)
  12660. (setq current (delete tag current))
  12661. (setq res t)
  12662. (push tag current))))
  12663. (end-of-line 1)
  12664. (if current
  12665. (progn
  12666. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12667. (org-set-tags nil t))
  12668. (delete-horizontal-space))
  12669. (run-hooks 'org-after-tags-change-hook))
  12670. res))
  12671. (defun org-align-tags-here (to-col)
  12672. ;; Assumes that this is a headline
  12673. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12674. (beginning-of-line 1)
  12675. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12676. (< pos (match-beginning 2)))
  12677. (progn
  12678. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12679. (goto-char (match-beginning 1))
  12680. (insert " ")
  12681. (delete-region (point) (1+ (match-beginning 2)))
  12682. (setq ncol (max (current-column)
  12683. (1+ col)
  12684. (if (> to-col 0)
  12685. to-col
  12686. (- (abs to-col) tags-l))))
  12687. (setq p (point))
  12688. (insert (make-string (- ncol (current-column)) ?\ ))
  12689. (setq ncol (current-column))
  12690. (when indent-tabs-mode (tabify p (point-at-eol)))
  12691. (org-move-to-column (min ncol col) t))
  12692. (goto-char pos))))
  12693. (defun org-set-tags-command (&optional arg just-align)
  12694. "Call the set-tags command for the current entry."
  12695. (interactive "P")
  12696. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12697. (org-set-tags arg just-align)
  12698. (save-excursion
  12699. (org-back-to-heading t)
  12700. (org-set-tags arg just-align))))
  12701. (defun org-set-tags-to (data)
  12702. "Set the tags of the current entry to DATA, replacing the current tags.
  12703. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12704. If DATA is nil or the empty string, any tags will be removed."
  12705. (interactive "sTags: ")
  12706. (setq data
  12707. (cond
  12708. ((eq data nil) "")
  12709. ((equal data "") "")
  12710. ((stringp data)
  12711. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12712. ":"))
  12713. ((listp data)
  12714. (concat ":" (mapconcat 'identity data ":") ":"))))
  12715. (when data
  12716. (save-excursion
  12717. (org-back-to-heading t)
  12718. (when (looking-at org-complex-heading-regexp)
  12719. (if (match-end 5)
  12720. (progn
  12721. (goto-char (match-beginning 5))
  12722. (insert data)
  12723. (delete-region (point) (point-at-eol))
  12724. (org-set-tags nil 'align))
  12725. (goto-char (point-at-eol))
  12726. (insert " " data)
  12727. (org-set-tags nil 'align)))
  12728. (beginning-of-line 1)
  12729. (if (looking-at ".*?\\([ \t]+\\)$")
  12730. (delete-region (match-beginning 1) (match-end 1))))))
  12731. (defun org-align-all-tags ()
  12732. "Align the tags i all headings."
  12733. (interactive)
  12734. (save-excursion
  12735. (or (ignore-errors (org-back-to-heading t))
  12736. (outline-next-heading))
  12737. (if (org-at-heading-p)
  12738. (org-set-tags t)
  12739. (message "No headings"))))
  12740. (defvar org-indent-indentation-per-level)
  12741. (defun org-set-tags (&optional arg just-align)
  12742. "Set the tags for the current headline.
  12743. With prefix ARG, realign all tags in headings in the current buffer."
  12744. (interactive "P")
  12745. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12746. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12747. 'region-start-level 'region))
  12748. org-loop-over-headlines-in-active-region)
  12749. (org-map-entries
  12750. ;; We don't use ARG and JUST-ALIGN here these args are not
  12751. ;; useful when looping over headlines
  12752. `(org-set-tags)
  12753. org-loop-over-headlines-in-active-region
  12754. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12755. (let* ((re org-outline-regexp-bol)
  12756. (current (unless arg (org-get-tags-string)))
  12757. (col (current-column))
  12758. (org-setting-tags t)
  12759. table current-tags inherited-tags ; computed below when needed
  12760. tags p0 c0 c1 rpl di tc level)
  12761. (if arg
  12762. (save-excursion
  12763. (goto-char (point-min))
  12764. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12765. (while (re-search-forward re nil t)
  12766. (org-set-tags nil t)
  12767. (end-of-line 1)))
  12768. (message "All tags realigned to column %d" org-tags-column))
  12769. (if just-align
  12770. (setq tags current)
  12771. ;; Get a new set of tags from the user
  12772. (save-excursion
  12773. (setq table (append org-tag-persistent-alist
  12774. (or org-tag-alist (org-get-buffer-tags))
  12775. (and
  12776. org-complete-tags-always-offer-all-agenda-tags
  12777. (org-global-tags-completion-table
  12778. (org-agenda-files))))
  12779. org-last-tags-completion-table table
  12780. current-tags (org-split-string current ":")
  12781. inherited-tags (nreverse
  12782. (nthcdr (length current-tags)
  12783. (nreverse (org-get-tags-at))))
  12784. tags
  12785. (if (or (eq t org-use-fast-tag-selection)
  12786. (and org-use-fast-tag-selection
  12787. (delq nil (mapcar 'cdr table))))
  12788. (org-fast-tag-selection
  12789. current-tags inherited-tags table
  12790. (if org-fast-tag-selection-include-todo
  12791. org-todo-key-alist))
  12792. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12793. (org-trim
  12794. (org-icompleting-read "Tags: "
  12795. 'org-tags-completion-function
  12796. nil nil current 'org-tags-history))))))
  12797. (while (string-match "[-+&]+" tags)
  12798. ;; No boolean logic, just a list
  12799. (setq tags (replace-match ":" t t tags))))
  12800. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12801. (if org-tags-sort-function
  12802. (setq tags (mapconcat 'identity
  12803. (sort (org-split-string
  12804. tags (org-re "[^[:alnum:]_@#%]+"))
  12805. org-tags-sort-function) ":")))
  12806. (if (string-match "\\`[\t ]*\\'" tags)
  12807. (setq tags "")
  12808. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12809. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12810. ;; Insert new tags at the correct column
  12811. (beginning-of-line 1)
  12812. (setq level (or (and (looking-at org-outline-regexp)
  12813. (- (match-end 0) (point) 1))
  12814. 1))
  12815. (cond
  12816. ((and (equal current "") (equal tags "")))
  12817. ((re-search-forward
  12818. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12819. (point-at-eol) t)
  12820. (if (equal tags "")
  12821. (setq rpl "")
  12822. (goto-char (match-beginning 0))
  12823. (setq c0 (current-column)
  12824. ;; compute offset for the case of org-indent-mode active
  12825. di (if org-indent-mode
  12826. (* (1- org-indent-indentation-per-level) (1- level))
  12827. 0)
  12828. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12829. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12830. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12831. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12832. (replace-match rpl t t)
  12833. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12834. tags)
  12835. (t (error "Tags alignment failed")))
  12836. (org-move-to-column col)
  12837. (unless just-align
  12838. (run-hooks 'org-after-tags-change-hook))))))
  12839. (defun org-change-tag-in-region (beg end tag off)
  12840. "Add or remove TAG for each entry in the region.
  12841. This works in the agenda, and also in an org-mode buffer."
  12842. (interactive
  12843. (list (region-beginning) (region-end)
  12844. (let ((org-last-tags-completion-table
  12845. (if (derived-mode-p 'org-mode)
  12846. (org-get-buffer-tags)
  12847. (org-global-tags-completion-table))))
  12848. (org-icompleting-read
  12849. "Tag: " 'org-tags-completion-function nil nil nil
  12850. 'org-tags-history))
  12851. (progn
  12852. (message "[s]et or [r]emove? ")
  12853. (equal (read-char-exclusive) ?r))))
  12854. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12855. (let ((agendap (equal major-mode 'org-agenda-mode))
  12856. l1 l2 m buf pos newhead (cnt 0))
  12857. (goto-char end)
  12858. (setq l2 (1- (org-current-line)))
  12859. (goto-char beg)
  12860. (setq l1 (org-current-line))
  12861. (loop for l from l1 to l2 do
  12862. (org-goto-line l)
  12863. (setq m (get-text-property (point) 'org-hd-marker))
  12864. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12865. (and agendap m))
  12866. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12867. pos (if agendap m (point)))
  12868. (with-current-buffer buf
  12869. (save-excursion
  12870. (save-restriction
  12871. (goto-char pos)
  12872. (setq cnt (1+ cnt))
  12873. (org-toggle-tag tag (if off 'off 'on))
  12874. (setq newhead (org-get-heading)))))
  12875. (and agendap (org-agenda-change-all-lines newhead m))))
  12876. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12877. (defun org-tags-completion-function (string predicate &optional flag)
  12878. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12879. (confirm (lambda (x) (stringp (car x)))))
  12880. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12881. (setq s1 (match-string 1 string)
  12882. s2 (match-string 2 string))
  12883. (setq s1 "" s2 string))
  12884. (cond
  12885. ((eq flag nil)
  12886. ;; try completion
  12887. (setq rtn (try-completion s2 ctable confirm))
  12888. (if (stringp rtn)
  12889. (setq rtn
  12890. (concat s1 s2 (substring rtn (length s2))
  12891. (if (and org-add-colon-after-tag-completion
  12892. (assoc rtn ctable))
  12893. ":" ""))))
  12894. rtn)
  12895. ((eq flag t)
  12896. ;; all-completions
  12897. (all-completions s2 ctable confirm)
  12898. )
  12899. ((eq flag 'lambda)
  12900. ;; exact match?
  12901. (assoc s2 ctable)))
  12902. ))
  12903. (defun org-fast-tag-insert (kwd tags face &optional end)
  12904. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12905. (insert (format "%-12s" (concat kwd ":"))
  12906. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12907. (or end "")))
  12908. (defun org-fast-tag-show-exit (flag)
  12909. (save-excursion
  12910. (org-goto-line 3)
  12911. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12912. (replace-match ""))
  12913. (when flag
  12914. (end-of-line 1)
  12915. (org-move-to-column (- (window-width) 19) t)
  12916. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12917. (defun org-set-current-tags-overlay (current prefix)
  12918. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12919. (if (featurep 'xemacs)
  12920. (org-overlay-display org-tags-overlay (concat prefix s)
  12921. 'secondary-selection)
  12922. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12923. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12924. (defvar org-last-tag-selection-key nil)
  12925. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12926. "Fast tag selection with single keys.
  12927. CURRENT is the current list of tags in the headline, INHERITED is the
  12928. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12929. possibly with grouping information. TODO-TABLE is a similar table with
  12930. TODO keywords, should these have keys assigned to them.
  12931. If the keys are nil, a-z are automatically assigned.
  12932. Returns the new tags string, or nil to not change the current settings."
  12933. (let* ((fulltable (append table todo-table))
  12934. (maxlen (apply 'max (mapcar
  12935. (lambda (x)
  12936. (if (stringp (car x)) (string-width (car x)) 0))
  12937. fulltable)))
  12938. (buf (current-buffer))
  12939. (expert (eq org-fast-tag-selection-single-key 'expert))
  12940. (buffer-tags nil)
  12941. (fwidth (+ maxlen 3 1 3))
  12942. (ncol (/ (- (window-width) 4) fwidth))
  12943. (i-face 'org-done)
  12944. (c-face 'org-todo)
  12945. tg cnt e c char c1 c2 ntable tbl rtn
  12946. ov-start ov-end ov-prefix
  12947. (exit-after-next org-fast-tag-selection-single-key)
  12948. (done-keywords org-done-keywords)
  12949. groups ingroup)
  12950. (save-excursion
  12951. (beginning-of-line 1)
  12952. (if (looking-at
  12953. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12954. (setq ov-start (match-beginning 1)
  12955. ov-end (match-end 1)
  12956. ov-prefix "")
  12957. (setq ov-start (1- (point-at-eol))
  12958. ov-end (1+ ov-start))
  12959. (skip-chars-forward "^\n\r")
  12960. (setq ov-prefix
  12961. (concat
  12962. (buffer-substring (1- (point)) (point))
  12963. (if (> (current-column) org-tags-column)
  12964. " "
  12965. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12966. (move-overlay org-tags-overlay ov-start ov-end)
  12967. (save-window-excursion
  12968. (if expert
  12969. (set-buffer (get-buffer-create " *Org tags*"))
  12970. (delete-other-windows)
  12971. (split-window-vertically)
  12972. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12973. (erase-buffer)
  12974. (org-set-local 'org-done-keywords done-keywords)
  12975. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12976. (org-fast-tag-insert "Current" current c-face "\n\n")
  12977. (org-fast-tag-show-exit exit-after-next)
  12978. (org-set-current-tags-overlay current ov-prefix)
  12979. (setq tbl fulltable char ?a cnt 0)
  12980. (while (setq e (pop tbl))
  12981. (cond
  12982. ((equal (car e) :startgroup)
  12983. (push '() groups) (setq ingroup t)
  12984. (when (not (= cnt 0))
  12985. (setq cnt 0)
  12986. (insert "\n"))
  12987. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12988. ((equal (car e) :endgroup)
  12989. (setq ingroup nil cnt 0)
  12990. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12991. ((equal e '(:newline))
  12992. (when (not (= cnt 0))
  12993. (setq cnt 0)
  12994. (insert "\n")
  12995. (setq e (car tbl))
  12996. (while (equal (car tbl) '(:newline))
  12997. (insert "\n")
  12998. (setq tbl (cdr tbl)))))
  12999. (t
  13000. (setq tg (copy-sequence (car e)) c2 nil)
  13001. (if (cdr e)
  13002. (setq c (cdr e))
  13003. ;; automatically assign a character.
  13004. (setq c1 (string-to-char
  13005. (downcase (substring
  13006. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13007. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13008. (while (or (rassoc char ntable) (rassoc char table))
  13009. (setq char (1+ char)))
  13010. (setq c2 c1))
  13011. (setq c (or c2 char)))
  13012. (if ingroup (push tg (car groups)))
  13013. (setq tg (org-add-props tg nil 'face
  13014. (cond
  13015. ((not (assoc tg table))
  13016. (org-get-todo-face tg))
  13017. ((member tg current) c-face)
  13018. ((member tg inherited) i-face))))
  13019. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13020. (insert "[" c "] " tg (make-string
  13021. (- fwidth 4 (length tg)) ?\ ))
  13022. (push (cons tg c) ntable)
  13023. (when (= (setq cnt (1+ cnt)) ncol)
  13024. (insert "\n")
  13025. (if ingroup (insert " "))
  13026. (setq cnt 0)))))
  13027. (setq ntable (nreverse ntable))
  13028. (insert "\n")
  13029. (goto-char (point-min))
  13030. (if (not expert) (org-fit-window-to-buffer))
  13031. (setq rtn
  13032. (catch 'exit
  13033. (while t
  13034. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13035. (if (not groups) "no " "")
  13036. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13037. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13038. (setq org-last-tag-selection-key c)
  13039. (cond
  13040. ((= c ?\r) (throw 'exit t))
  13041. ((= c ?!)
  13042. (setq groups (not groups))
  13043. (goto-char (point-min))
  13044. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13045. ((= c ?\C-c)
  13046. (if (not expert)
  13047. (org-fast-tag-show-exit
  13048. (setq exit-after-next (not exit-after-next)))
  13049. (setq expert nil)
  13050. (delete-other-windows)
  13051. (set-window-buffer (split-window-vertically) " *Org tags*")
  13052. (org-switch-to-buffer-other-window " *Org tags*")
  13053. (org-fit-window-to-buffer)))
  13054. ((or (= c ?\C-g)
  13055. (and (= c ?q) (not (rassoc c ntable))))
  13056. (org-detach-overlay org-tags-overlay)
  13057. (setq quit-flag t))
  13058. ((= c ?\ )
  13059. (setq current nil)
  13060. (if exit-after-next (setq exit-after-next 'now)))
  13061. ((= c ?\t)
  13062. (condition-case nil
  13063. (setq tg (org-icompleting-read
  13064. "Tag: "
  13065. (or buffer-tags
  13066. (with-current-buffer buf
  13067. (org-get-buffer-tags)))))
  13068. (quit (setq tg "")))
  13069. (when (string-match "\\S-" tg)
  13070. (add-to-list 'buffer-tags (list tg))
  13071. (if (member tg current)
  13072. (setq current (delete tg current))
  13073. (push tg current)))
  13074. (if exit-after-next (setq exit-after-next 'now)))
  13075. ((setq e (rassoc c todo-table) tg (car e))
  13076. (with-current-buffer buf
  13077. (save-excursion (org-todo tg)))
  13078. (if exit-after-next (setq exit-after-next 'now)))
  13079. ((setq e (rassoc c ntable) tg (car e))
  13080. (if (member tg current)
  13081. (setq current (delete tg current))
  13082. (loop for g in groups do
  13083. (if (member tg g)
  13084. (mapc (lambda (x)
  13085. (setq current (delete x current)))
  13086. g)))
  13087. (push tg current))
  13088. (if exit-after-next (setq exit-after-next 'now))))
  13089. ;; Create a sorted list
  13090. (setq current
  13091. (sort current
  13092. (lambda (a b)
  13093. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13094. (if (eq exit-after-next 'now) (throw 'exit t))
  13095. (goto-char (point-min))
  13096. (beginning-of-line 2)
  13097. (delete-region (point) (point-at-eol))
  13098. (org-fast-tag-insert "Current" current c-face)
  13099. (org-set-current-tags-overlay current ov-prefix)
  13100. (while (re-search-forward
  13101. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13102. (setq tg (match-string 1))
  13103. (add-text-properties
  13104. (match-beginning 1) (match-end 1)
  13105. (list 'face
  13106. (cond
  13107. ((member tg current) c-face)
  13108. ((member tg inherited) i-face)
  13109. (t (get-text-property (match-beginning 1) 'face))))))
  13110. (goto-char (point-min)))))
  13111. (org-detach-overlay org-tags-overlay)
  13112. (if rtn
  13113. (mapconcat 'identity current ":")
  13114. nil))))
  13115. (defun org-get-tags-string ()
  13116. "Get the TAGS string in the current headline."
  13117. (unless (org-at-heading-p t)
  13118. (error "Not on a heading"))
  13119. (save-excursion
  13120. (beginning-of-line 1)
  13121. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13122. (org-match-string-no-properties 1)
  13123. "")))
  13124. (defun org-get-tags ()
  13125. "Get the list of tags specified in the current headline."
  13126. (org-split-string (org-get-tags-string) ":"))
  13127. (defun org-get-buffer-tags ()
  13128. "Get a table of all tags used in the buffer, for completion."
  13129. (let (tags)
  13130. (save-excursion
  13131. (goto-char (point-min))
  13132. (while (re-search-forward
  13133. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13134. (when (equal (char-after (point-at-bol 0)) ?*)
  13135. (mapc (lambda (x) (add-to-list 'tags x))
  13136. (org-split-string (org-match-string-no-properties 1) ":")))))
  13137. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13138. (mapcar 'list tags)))
  13139. ;;;; The mapping API
  13140. (defun org-map-entries (func &optional match scope &rest skip)
  13141. "Call FUNC at each headline selected by MATCH in SCOPE.
  13142. FUNC is a function or a lisp form. The function will be called without
  13143. arguments, with the cursor positioned at the beginning of the headline.
  13144. The return values of all calls to the function will be collected and
  13145. returned as a list.
  13146. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13147. does not need to preserve point. After evaluation, the cursor will be
  13148. moved to the end of the line (presumably of the headline of the
  13149. processed entry) and search continues from there. Under some
  13150. circumstances, this may not produce the wanted results. For example,
  13151. if you have removed (e.g. archived) the current (sub)tree it could
  13152. mean that the next entry will be skipped entirely. In such cases, you
  13153. can specify the position from where search should continue by making
  13154. FUNC set the variable `org-map-continue-from' to the desired buffer
  13155. position.
  13156. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13157. Only headlines that are matched by this query will be considered during
  13158. the iteration. When MATCH is nil or t, all headlines will be
  13159. visited by the iteration.
  13160. SCOPE determines the scope of this command. It can be any of:
  13161. nil The current buffer, respecting the restriction if any
  13162. tree The subtree started with the entry at point
  13163. region The entries within the active region, if any
  13164. region-start-level
  13165. The entries within the active region, but only those at
  13166. the same level than the first one.
  13167. file The current buffer, without restriction
  13168. file-with-archives
  13169. The current buffer, and any archives associated with it
  13170. agenda All agenda files
  13171. agenda-with-archives
  13172. All agenda files with any archive files associated with them
  13173. \(file1 file2 ...)
  13174. If this is a list, all files in the list will be scanned
  13175. The remaining args are treated as settings for the skipping facilities of
  13176. the scanner. The following items can be given here:
  13177. archive skip trees with the archive tag.
  13178. comment skip trees with the COMMENT keyword
  13179. function or Emacs Lisp form:
  13180. will be used as value for `org-agenda-skip-function', so whenever
  13181. the function returns t, FUNC will not be called for that
  13182. entry and search will continue from the point where the
  13183. function leaves it.
  13184. If your function needs to retrieve the tags including inherited tags
  13185. at the *current* entry, you can use the value of the variable
  13186. `org-scanner-tags' which will be much faster than getting the value
  13187. with `org-get-tags-at'. If your function gets properties with
  13188. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13189. to t around the call to `org-entry-properties' to get the same speedup.
  13190. Note that if your function moves around to retrieve tags and properties at
  13191. a *different* entry, you cannot use these techniques."
  13192. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13193. (not (org-region-active-p)))
  13194. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13195. (org-agenda-skip-archived-trees (memq 'archive skip))
  13196. (org-agenda-skip-comment-trees (memq 'comment skip))
  13197. (org-agenda-skip-function
  13198. (car (org-delete-all '(comment archive) skip)))
  13199. (org-tags-match-list-sublevels t)
  13200. (start-level (eq scope 'region-start-level))
  13201. matcher file res
  13202. org-todo-keywords-for-agenda
  13203. org-done-keywords-for-agenda
  13204. org-todo-keyword-alist-for-agenda
  13205. org-drawers-for-agenda
  13206. org-tag-alist-for-agenda
  13207. todo-only)
  13208. (cond
  13209. ((eq match t) (setq matcher t))
  13210. ((eq match nil) (setq matcher t))
  13211. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13212. (save-excursion
  13213. (save-restriction
  13214. (cond ((eq scope 'tree)
  13215. (org-back-to-heading t)
  13216. (org-narrow-to-subtree)
  13217. (setq scope nil))
  13218. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13219. (org-region-active-p))
  13220. ;; If needed, set start-level to a string like "2"
  13221. (when start-level
  13222. (save-excursion
  13223. (goto-char (region-beginning))
  13224. (unless (org-at-heading-p) (outline-next-heading))
  13225. (setq start-level (org-current-level))))
  13226. (narrow-to-region (region-beginning)
  13227. (save-excursion
  13228. (goto-char (region-end))
  13229. (unless (and (bolp) (org-at-heading-p))
  13230. (outline-next-heading))
  13231. (point)))
  13232. (setq scope nil)))
  13233. (if (not scope)
  13234. (progn
  13235. (org-agenda-prepare-buffers
  13236. (list (buffer-file-name (current-buffer))))
  13237. (setq res (org-scan-tags func matcher todo-only start-level)))
  13238. ;; Get the right scope
  13239. (cond
  13240. ((and scope (listp scope) (symbolp (car scope)))
  13241. (setq scope (eval scope)))
  13242. ((eq scope 'agenda)
  13243. (setq scope (org-agenda-files t)))
  13244. ((eq scope 'agenda-with-archives)
  13245. (setq scope (org-agenda-files t))
  13246. (setq scope (org-add-archive-files scope)))
  13247. ((eq scope 'file)
  13248. (setq scope (list (buffer-file-name))))
  13249. ((eq scope 'file-with-archives)
  13250. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13251. (org-agenda-prepare-buffers scope)
  13252. (while (setq file (pop scope))
  13253. (with-current-buffer (org-find-base-buffer-visiting file)
  13254. (save-excursion
  13255. (save-restriction
  13256. (widen)
  13257. (goto-char (point-min))
  13258. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13259. res)))
  13260. ;;;; Properties
  13261. ;;; Setting and retrieving properties
  13262. (defconst org-special-properties
  13263. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13264. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13265. "The special properties valid in Org-mode.
  13266. These are properties that are not defined in the property drawer,
  13267. but in some other way.")
  13268. (defconst org-default-properties
  13269. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13270. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13271. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13272. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13273. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13274. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13275. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13276. "Some properties that are used by Org-mode for various purposes.
  13277. Being in this list makes sure that they are offered for completion.")
  13278. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13279. "Regular expression matching the first line of a property drawer.")
  13280. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13281. "Regular expression matching the last line of a property drawer.")
  13282. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13283. "Regular expression matching the first line of a property drawer.")
  13284. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13285. "Regular expression matching the first line of a property drawer.")
  13286. (defconst org-property-drawer-re
  13287. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13288. org-property-end-re "\\)\n?")
  13289. "Matches an entire property drawer.")
  13290. (defconst org-clock-drawer-re
  13291. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13292. org-property-end-re "\\)\n?")
  13293. "Matches an entire clock drawer.")
  13294. (defsubst org-re-property (property)
  13295. "Return a regexp matching a PROPERTY line.
  13296. Match group 1 will be set to the value."
  13297. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13298. (defsubst org-re-property-keyword (property)
  13299. "Return a regexp matching a PROPERTY line, possibly with no
  13300. value for the property."
  13301. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13302. (defun org-property-action ()
  13303. "Do an action on properties."
  13304. (interactive)
  13305. (let (c)
  13306. (org-at-property-p)
  13307. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13308. (setq c (read-char-exclusive))
  13309. (cond
  13310. ((equal c ?s)
  13311. (call-interactively 'org-set-property))
  13312. ((equal c ?d)
  13313. (call-interactively 'org-delete-property))
  13314. ((equal c ?D)
  13315. (call-interactively 'org-delete-property-globally))
  13316. ((equal c ?c)
  13317. (call-interactively 'org-compute-property-at-point))
  13318. (t (error "No such property action %c" c)))))
  13319. (defun org-inc-effort ()
  13320. "Increment the value of the effort property in the current entry."
  13321. (interactive)
  13322. (org-set-effort nil t))
  13323. (defun org-set-effort (&optional value increment)
  13324. "Set the effort property of the current entry.
  13325. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13326. 10th allowed value.
  13327. When INCREMENT is non-nil, set the property to the next allowed value."
  13328. (interactive "P")
  13329. (if (equal value 0) (setq value 10))
  13330. (let* ((completion-ignore-case t)
  13331. (prop org-effort-property)
  13332. (cur (org-entry-get nil prop))
  13333. (allowed (org-property-get-allowed-values nil prop 'table))
  13334. (existing (mapcar 'list (org-property-values prop)))
  13335. rpl
  13336. (val (cond
  13337. ((stringp value) value)
  13338. ((and allowed (integerp value))
  13339. (or (car (nth (1- value) allowed))
  13340. (car (org-last allowed))))
  13341. ((and allowed increment)
  13342. (or (caadr (member (list cur) allowed))
  13343. (error "Allowed effort values are not set")))
  13344. (allowed
  13345. (message "Select 1-9,0, [RET%s]: %s"
  13346. (if cur (concat "=" cur) "")
  13347. (mapconcat 'car allowed " "))
  13348. (setq rpl (read-char-exclusive))
  13349. (if (equal rpl ?\r)
  13350. cur
  13351. (setq rpl (- rpl ?0))
  13352. (if (equal rpl 0) (setq rpl 10))
  13353. (if (and (> rpl 0) (<= rpl (length allowed)))
  13354. (car (nth (1- rpl) allowed))
  13355. (org-completing-read "Effort: " allowed nil))))
  13356. (t
  13357. (let (org-completion-use-ido org-completion-use-iswitchb)
  13358. (org-completing-read
  13359. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13360. (concat "[" cur "]") "")
  13361. ": ")
  13362. existing nil nil "" nil cur))))))
  13363. (unless (equal (org-entry-get nil prop) val)
  13364. (org-entry-put nil prop val))
  13365. (save-excursion
  13366. (org-back-to-heading t)
  13367. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13368. (message "%s is now %s" prop val)))
  13369. (defun org-at-property-p ()
  13370. "Is cursor inside a property drawer?"
  13371. (save-excursion
  13372. (beginning-of-line 1)
  13373. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13374. (save-match-data ;; Used by calling procedures
  13375. (let ((p (point))
  13376. (range (unless (org-before-first-heading-p)
  13377. (org-get-property-block))))
  13378. (and range (<= (car range) p) (< p (cdr range))))))))
  13379. (defun org-get-property-block (&optional beg end force)
  13380. "Return the (beg . end) range of the body of the property drawer.
  13381. BEG and END are the beginning and end of the current subtree, or of
  13382. the part before the first headline. If they are not given, they will
  13383. be found. If the drawer does not exist and FORCE is non-nil, create
  13384. the drawer."
  13385. (catch 'exit
  13386. (save-excursion
  13387. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13388. (progn (org-back-to-heading t) (point))))
  13389. (end (or end (and (not (outline-next-heading)) (point-max))
  13390. (point))))
  13391. (goto-char beg)
  13392. (if (re-search-forward org-property-start-re end t)
  13393. (setq beg (1+ (match-end 0)))
  13394. (if force
  13395. (save-excursion
  13396. (org-insert-property-drawer)
  13397. (setq end (progn (outline-next-heading) (point))))
  13398. (throw 'exit nil))
  13399. (goto-char beg)
  13400. (if (re-search-forward org-property-start-re end t)
  13401. (setq beg (1+ (match-end 0)))))
  13402. (if (re-search-forward org-property-end-re end t)
  13403. (setq end (match-beginning 0))
  13404. (or force (throw 'exit nil))
  13405. (goto-char beg)
  13406. (setq end beg)
  13407. (org-indent-line)
  13408. (insert ":END:\n"))
  13409. (cons beg end)))))
  13410. (defun org-entry-properties (&optional pom which specific)
  13411. "Get all properties of the entry at point-or-marker POM.
  13412. This includes the TODO keyword, the tags, time strings for deadline,
  13413. scheduled, and clocking, and any additional properties defined in the
  13414. entry. The return value is an alist, keys may occur multiple times
  13415. if the property key was used several times.
  13416. POM may also be nil, in which case the current entry is used.
  13417. If WHICH is nil or `all', get all properties. If WHICH is
  13418. `special' or `standard', only get that subclass. If WHICH
  13419. is a string only get exactly this property. SPECIFIC can be a string, the
  13420. specific property we are interested in. Specifying it can speed
  13421. things up because then unnecessary parsing is avoided."
  13422. (setq which (or which 'all))
  13423. (org-with-point-at pom
  13424. (let ((clockstr (substring org-clock-string 0 -1))
  13425. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13426. (case-fold-search nil)
  13427. beg end range props sum-props key key1 value string clocksum clocksumt)
  13428. (save-excursion
  13429. (when (condition-case nil
  13430. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13431. (error nil))
  13432. (setq beg (point))
  13433. (setq sum-props (get-text-property (point) 'org-summaries))
  13434. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13435. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13436. (outline-next-heading)
  13437. (setq end (point))
  13438. (when (memq which '(all special))
  13439. ;; Get the special properties, like TODO and tags
  13440. (goto-char beg)
  13441. (when (and (or (not specific) (string= specific "TODO"))
  13442. (looking-at org-todo-line-regexp) (match-end 2))
  13443. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13444. (when (and (or (not specific) (string= specific "PRIORITY"))
  13445. (looking-at org-priority-regexp))
  13446. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13447. (when (or (not specific) (string= specific "FILE"))
  13448. (push (cons "FILE" buffer-file-name) props))
  13449. (when (and (or (not specific) (string= specific "TAGS"))
  13450. (setq value (org-get-tags-string))
  13451. (string-match "\\S-" value))
  13452. (push (cons "TAGS" value) props))
  13453. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13454. (setq value (org-get-tags-at)))
  13455. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13456. ":"))
  13457. props))
  13458. (when (or (not specific) (string= specific "BLOCKED"))
  13459. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13460. (when (or (not specific)
  13461. (member specific
  13462. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13463. "TIMESTAMP" "TIMESTAMP_IA")))
  13464. (catch 'match
  13465. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13466. (setq key (if (match-end 1)
  13467. (substring (org-match-string-no-properties 1)
  13468. 0 -1))
  13469. string (if (equal key clockstr)
  13470. (org-trim
  13471. (buffer-substring-no-properties
  13472. (match-beginning 3) (goto-char
  13473. (point-at-eol))))
  13474. (substring (org-match-string-no-properties 3)
  13475. 1 -1)))
  13476. ;; Get the correct property name from the key. This is
  13477. ;; necessary if the user has configured time keywords.
  13478. (setq key1 (concat key ":"))
  13479. (cond
  13480. ((not key)
  13481. (setq key
  13482. (if (= (char-after (match-beginning 3)) ?\[)
  13483. "TIMESTAMP_IA" "TIMESTAMP")))
  13484. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13485. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13486. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13487. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13488. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13489. (progn
  13490. (push (cons key string) props)
  13491. ;; no need to search further if match is found
  13492. (throw 'match t))
  13493. (when (or (equal key "CLOCK") (not (assoc key props)))
  13494. (push (cons key string) props)))))))
  13495. (when (memq which '(all standard))
  13496. ;; Get the standard properties, like :PROP: ...
  13497. (setq range (org-get-property-block beg end))
  13498. (when range
  13499. (goto-char (car range))
  13500. (while (re-search-forward
  13501. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13502. (cdr range) t)
  13503. (setq key (org-match-string-no-properties 1)
  13504. value (org-trim (or (org-match-string-no-properties 2) "")))
  13505. (unless (member key excluded)
  13506. (push (cons key (or value "")) props)))))
  13507. (if clocksum
  13508. (push (cons "CLOCKSUM"
  13509. (org-columns-number-to-string (/ (float clocksum) 60.)
  13510. 'add_times))
  13511. props))
  13512. (if clocksumt
  13513. (push (cons "CLOCKSUM_T"
  13514. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13515. 'add_times))
  13516. props))
  13517. (unless (assoc "CATEGORY" props)
  13518. (push (cons "CATEGORY" (org-get-category)) props))
  13519. (append sum-props (nreverse props)))))))
  13520. (defun org-entry-get (pom property &optional inherit literal-nil)
  13521. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13522. If INHERIT is non-nil and the entry does not have the property,
  13523. then also check higher levels of the hierarchy.
  13524. If INHERIT is the symbol `selective', use inheritance only if the setting
  13525. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13526. If the property is present but empty, the return value is the empty string.
  13527. If the property is not present at all, nil is returned.
  13528. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13529. do not interpret it as the list atom nil. This is used for inheritance
  13530. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13531. (org-with-point-at pom
  13532. (if (and inherit (if (eq inherit 'selective)
  13533. (org-property-inherit-p property)
  13534. t))
  13535. (org-entry-get-with-inheritance property literal-nil)
  13536. (if (member property org-special-properties)
  13537. ;; We need a special property. Use `org-entry-properties' to
  13538. ;; retrieve it, but specify the wanted property
  13539. (cdr (assoc property (org-entry-properties nil 'special property)))
  13540. (let ((range (org-get-property-block)))
  13541. (when (and range (not (eq (car range) (cdr range))))
  13542. (let* ((props (list (or (assoc property org-file-properties)
  13543. (assoc property org-global-properties)
  13544. (assoc property org-global-properties-fixed))))
  13545. (ap (lambda (key)
  13546. (when (re-search-forward
  13547. (org-re-property key) (cdr range) t)
  13548. (setq props
  13549. (org-update-property-plist
  13550. key
  13551. (if (match-end 1)
  13552. (org-match-string-no-properties 1) "")
  13553. props)))))
  13554. val)
  13555. (goto-char (car range))
  13556. (funcall ap property)
  13557. (goto-char (car range))
  13558. (while (funcall ap (concat property "+")))
  13559. (setq val (cdr (assoc property props)))
  13560. (when val (if literal-nil val (org-not-nil val))))))))))
  13561. (defun org-property-or-variable-value (var &optional inherit)
  13562. "Check if there is a property fixing the value of VAR.
  13563. If yes, return this value. If not, return the current value of the variable."
  13564. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13565. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13566. (read prop)
  13567. (symbol-value var))))
  13568. (defun org-entry-delete (pom property)
  13569. "Delete the property PROPERTY from entry at point-or-marker POM."
  13570. (org-with-point-at pom
  13571. (if (member property org-special-properties)
  13572. nil ; cannot delete these properties.
  13573. (let ((range (org-get-property-block)))
  13574. (if (and range
  13575. (goto-char (car range))
  13576. (re-search-forward
  13577. (org-re-property property)
  13578. (cdr range) t))
  13579. (progn
  13580. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13581. t)
  13582. nil)))))
  13583. ;; Multi-values properties are properties that contain multiple values
  13584. ;; These values are assumed to be single words, separated by whitespace.
  13585. (defun org-entry-add-to-multivalued-property (pom property value)
  13586. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13587. (let* ((old (org-entry-get pom property))
  13588. (values (and old (org-split-string old "[ \t]"))))
  13589. (setq value (org-entry-protect-space value))
  13590. (unless (member value values)
  13591. (setq values (cons value values))
  13592. (org-entry-put pom property
  13593. (mapconcat 'identity values " ")))))
  13594. (defun org-entry-remove-from-multivalued-property (pom property value)
  13595. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13596. (let* ((old (org-entry-get pom property))
  13597. (values (and old (org-split-string old "[ \t]"))))
  13598. (setq value (org-entry-protect-space value))
  13599. (when (member value values)
  13600. (setq values (delete value values))
  13601. (org-entry-put pom property
  13602. (mapconcat 'identity values " ")))))
  13603. (defun org-entry-member-in-multivalued-property (pom property value)
  13604. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13605. (let* ((old (org-entry-get pom property))
  13606. (values (and old (org-split-string old "[ \t]"))))
  13607. (setq value (org-entry-protect-space value))
  13608. (member value values)))
  13609. (defun org-entry-get-multivalued-property (pom property)
  13610. "Return a list of values in a multivalued property."
  13611. (let* ((value (org-entry-get pom property))
  13612. (values (and value (org-split-string value "[ \t]"))))
  13613. (mapcar 'org-entry-restore-space values)))
  13614. (defun org-entry-put-multivalued-property (pom property &rest values)
  13615. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13616. VALUES should be a list of strings. Spaces will be protected."
  13617. (org-entry-put pom property
  13618. (mapconcat 'org-entry-protect-space values " "))
  13619. (let* ((value (org-entry-get pom property))
  13620. (values (and value (org-split-string value "[ \t]"))))
  13621. (mapcar 'org-entry-restore-space values)))
  13622. (defun org-entry-protect-space (s)
  13623. "Protect spaces and newline in string S."
  13624. (while (string-match " " s)
  13625. (setq s (replace-match "%20" t t s)))
  13626. (while (string-match "\n" s)
  13627. (setq s (replace-match "%0A" t t s)))
  13628. s)
  13629. (defun org-entry-restore-space (s)
  13630. "Restore spaces and newline in string S."
  13631. (while (string-match "%20" s)
  13632. (setq s (replace-match " " t t s)))
  13633. (while (string-match "%0A" s)
  13634. (setq s (replace-match "\n" t t s)))
  13635. s)
  13636. (defvar org-entry-property-inherited-from (make-marker)
  13637. "Marker pointing to the entry from where a property was inherited.
  13638. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13639. location of the entry where the inheritance search matched. If there was
  13640. no match, the marker will point nowhere.
  13641. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13642. is set.")
  13643. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13644. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13645. The search will stop at the first ancestor which has the property defined.
  13646. If the value found is \"nil\", return nil to show that the property
  13647. should be considered as undefined (this is the meaning of nil here).
  13648. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13649. (move-marker org-entry-property-inherited-from nil)
  13650. (let (tmp)
  13651. (save-excursion
  13652. (save-restriction
  13653. (widen)
  13654. (catch 'ex
  13655. (while t
  13656. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13657. (or (ignore-errors (org-back-to-heading t))
  13658. (goto-char (point-min)))
  13659. (move-marker org-entry-property-inherited-from (point))
  13660. (throw 'ex tmp))
  13661. (or (ignore-errors (org-up-heading-safe))
  13662. (throw 'ex nil))))))
  13663. (setq tmp (or tmp
  13664. (cdr (assoc property org-file-properties))
  13665. (cdr (assoc property org-global-properties))
  13666. (cdr (assoc property org-global-properties-fixed))))
  13667. (if literal-nil tmp (org-not-nil tmp))))
  13668. (defvar org-property-changed-functions nil
  13669. "Hook called when the value of a property has changed.
  13670. Each hook function should accept two arguments, the name of the property
  13671. and the new value.")
  13672. (defun org-entry-put (pom property value)
  13673. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13674. (org-with-point-at pom
  13675. (org-back-to-heading t)
  13676. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13677. range)
  13678. (cond
  13679. ((equal property "TODO")
  13680. (when (and (stringp value) (string-match "\\S-" value)
  13681. (not (member value org-todo-keywords-1)))
  13682. (error "\"%s\" is not a valid TODO state" value))
  13683. (if (or (not value)
  13684. (not (string-match "\\S-" value)))
  13685. (setq value 'none))
  13686. (org-todo value)
  13687. (org-set-tags nil 'align))
  13688. ((equal property "PRIORITY")
  13689. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13690. (string-to-char value) ?\ ))
  13691. (org-set-tags nil 'align))
  13692. ((equal property "CLOCKSUM")
  13693. (if (not (re-search-forward
  13694. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13695. (error "Cannot find a clock log")
  13696. (goto-char (- (match-end 1) 2))
  13697. (cond
  13698. ((eq value 'earlier) (org-timestamp-down))
  13699. ((eq value 'later) (org-timestamp-up)))
  13700. (org-clock-sum-current-item)))
  13701. ((equal property "SCHEDULED")
  13702. (if (re-search-forward org-scheduled-time-regexp end t)
  13703. (cond
  13704. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13705. ((eq value 'later) (org-timestamp-change 1 'day))
  13706. (t (call-interactively 'org-schedule)))
  13707. (call-interactively 'org-schedule)))
  13708. ((equal property "DEADLINE")
  13709. (if (re-search-forward org-deadline-time-regexp end t)
  13710. (cond
  13711. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13712. ((eq value 'later) (org-timestamp-change 1 'day))
  13713. (t (call-interactively 'org-deadline)))
  13714. (call-interactively 'org-deadline)))
  13715. ((member property org-special-properties)
  13716. (error "The %s property can not yet be set with `org-entry-put'"
  13717. property))
  13718. (t ; a non-special property
  13719. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13720. (setq range (org-get-property-block beg end 'force))
  13721. (goto-char (car range))
  13722. (if (re-search-forward
  13723. (org-re-property-keyword property) (cdr range) t)
  13724. (progn
  13725. (delete-region (match-beginning 0) (match-end 0))
  13726. (goto-char (match-beginning 0)))
  13727. (goto-char (cdr range))
  13728. (insert "\n")
  13729. (backward-char 1)
  13730. (org-indent-line))
  13731. (insert ":" property ":")
  13732. (and value (insert " " value))
  13733. (org-indent-line)))))
  13734. (run-hook-with-args 'org-property-changed-functions property value)))
  13735. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13736. "Get all property keys in the current buffer.
  13737. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13738. like tags and TODO state.
  13739. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13740. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13741. and others.
  13742. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13743. formats in the current buffer."
  13744. (let (rtn range cfmt s p)
  13745. (save-excursion
  13746. (save-restriction
  13747. (widen)
  13748. (goto-char (point-min))
  13749. (while (re-search-forward org-property-start-re nil t)
  13750. (setq range (org-get-property-block))
  13751. (goto-char (car range))
  13752. (while (re-search-forward
  13753. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13754. (cdr range) t)
  13755. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13756. (outline-next-heading))))
  13757. (when include-specials
  13758. (setq rtn (append org-special-properties rtn)))
  13759. (when include-defaults
  13760. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13761. (add-to-list 'rtn org-effort-property))
  13762. (when include-columns
  13763. (save-excursion
  13764. (save-restriction
  13765. (widen)
  13766. (goto-char (point-min))
  13767. (while (re-search-forward
  13768. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13769. nil t)
  13770. (setq cfmt (match-string 2) s 0)
  13771. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13772. cfmt s)
  13773. (setq s (match-end 0)
  13774. p (match-string 1 cfmt))
  13775. (unless (or (equal p "ITEM")
  13776. (member p org-special-properties))
  13777. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13778. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13779. (defun org-property-values (key)
  13780. "Return a list of all values of property KEY in the current buffer."
  13781. (save-excursion
  13782. (save-restriction
  13783. (widen)
  13784. (goto-char (point-min))
  13785. (let ((re (org-re-property key))
  13786. values)
  13787. (while (re-search-forward re nil t)
  13788. (add-to-list 'values (org-trim (match-string 1))))
  13789. (delete "" values)))))
  13790. (defun org-insert-property-drawer ()
  13791. "Insert a property drawer into the current entry."
  13792. (org-back-to-heading t)
  13793. (looking-at org-outline-regexp)
  13794. (let ((indent (if org-adapt-indentation
  13795. (- (match-end 0) (match-beginning 0))
  13796. 0))
  13797. (beg (point))
  13798. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13799. end hiddenp)
  13800. (outline-next-heading)
  13801. (setq end (point))
  13802. (goto-char beg)
  13803. (while (re-search-forward re end t))
  13804. (setq hiddenp (outline-invisible-p))
  13805. (end-of-line 1)
  13806. (and (equal (char-after) ?\n) (forward-char 1))
  13807. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13808. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13809. ;; just skip this line
  13810. (beginning-of-line 2)
  13811. ;; Drawer start, find the end
  13812. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13813. (beginning-of-line 1)))
  13814. (org-skip-over-state-notes)
  13815. (skip-chars-backward " \t\n\r")
  13816. (if (eq (char-before) ?*) (forward-char 1))
  13817. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13818. (beginning-of-line 0)
  13819. (org-indent-to-column indent)
  13820. (beginning-of-line 2)
  13821. (org-indent-to-column indent)
  13822. (beginning-of-line 0)
  13823. (if hiddenp
  13824. (save-excursion
  13825. (org-back-to-heading t)
  13826. (hide-entry))
  13827. (org-flag-drawer t))))
  13828. (defun org-insert-drawer (&optional arg drawer)
  13829. "Insert a drawer at point.
  13830. Optional argument DRAWER, when non-nil, is a string representing
  13831. drawer's name. Otherwise, the user is prompted for a name.
  13832. If a region is active, insert the drawer around that region
  13833. instead.
  13834. Point is left between drawer's boundaries."
  13835. (interactive "P")
  13836. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13837. "LOGBOOK"))
  13838. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13839. ;; internally by Org. They are meant to be inserted
  13840. ;; automatically.
  13841. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13842. ;; Remove system drawers from list. Note: For some reason,
  13843. ;; `org-completing-read' ignores the predicate while
  13844. ;; `completing-read' handles it fine.
  13845. (drawer (if arg "PROPERTIES"
  13846. (or drawer
  13847. (completing-read
  13848. "Drawer: " org-drawers
  13849. (lambda (d) (not (member d system-drawers))))))))
  13850. (cond
  13851. ;; With C-u, fall back on `org-insert-property-drawer'
  13852. (arg (org-insert-property-drawer))
  13853. ;; With an active region, insert a drawer at point.
  13854. ((not (org-region-active-p))
  13855. (progn
  13856. (unless (bolp) (insert "\n"))
  13857. (insert (format ":%s:\n\n:END:\n" drawer))
  13858. (forward-line -2)))
  13859. ;; Otherwise, insert the drawer at point
  13860. (t
  13861. (let ((rbeg (region-beginning))
  13862. (rend (copy-marker (region-end))))
  13863. (unwind-protect
  13864. (progn
  13865. (goto-char rbeg)
  13866. (beginning-of-line)
  13867. (when (save-excursion
  13868. (re-search-forward org-outline-regexp-bol rend t))
  13869. (error "Drawers cannot contain headlines"))
  13870. ;; Position point at the beginning of the first
  13871. ;; non-blank line in region. Insert drawer's opening
  13872. ;; there, then indent it.
  13873. (org-skip-whitespace)
  13874. (beginning-of-line)
  13875. (insert ":" drawer ":\n")
  13876. (forward-line -1)
  13877. (indent-for-tab-command)
  13878. ;; Move point to the beginning of the first blank line
  13879. ;; after the last non-blank line in region. Insert
  13880. ;; drawer's closing, then indent it.
  13881. (goto-char rend)
  13882. (skip-chars-backward " \r\t\n")
  13883. (insert "\n:END:")
  13884. (deactivate-mark t)
  13885. (indent-for-tab-command)
  13886. (unless (eolp) (insert "\n")))
  13887. ;; Clear marker, whatever the outcome of insertion is.
  13888. (set-marker rend nil)))))))
  13889. (defvar org-property-set-functions-alist nil
  13890. "Property set function alist.
  13891. Each entry should have the following format:
  13892. (PROPERTY . READ-FUNCTION)
  13893. The read function will be called with the same argument as
  13894. `org-completing-read'.")
  13895. (defun org-set-property-function (property)
  13896. "Get the function that should be used to set PROPERTY.
  13897. This is computed according to `org-property-set-functions-alist'."
  13898. (or (cdr (assoc property org-property-set-functions-alist))
  13899. 'org-completing-read))
  13900. (defun org-read-property-value (property)
  13901. "Read PROPERTY value from user."
  13902. (let* ((completion-ignore-case t)
  13903. (allowed (org-property-get-allowed-values nil property 'table))
  13904. (cur (org-entry-get nil property))
  13905. (prompt (concat property " value"
  13906. (if (and cur (string-match "\\S-" cur))
  13907. (concat " [" cur "]") "") ": "))
  13908. (set-function (org-set-property-function property))
  13909. (val (if allowed
  13910. (funcall set-function prompt allowed nil
  13911. (not (get-text-property 0 'org-unrestricted
  13912. (caar allowed))))
  13913. (let (org-completion-use-ido org-completion-use-iswitchb)
  13914. (funcall set-function prompt
  13915. (mapcar 'list (org-property-values property))
  13916. nil nil "" nil cur)))))
  13917. (if (equal val "")
  13918. cur
  13919. val)))
  13920. (defvar org-last-set-property nil)
  13921. (defvar org-last-set-property-value nil)
  13922. (defun org-read-property-name ()
  13923. "Read a property name."
  13924. (let* ((completion-ignore-case t)
  13925. (keys (org-buffer-property-keys nil t t))
  13926. (default-prop (or (save-excursion
  13927. (save-match-data
  13928. (beginning-of-line)
  13929. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13930. (null (string= (match-string 1) "END"))
  13931. (match-string 1))))
  13932. org-last-set-property))
  13933. (property (org-icompleting-read
  13934. (concat "Property"
  13935. (if default-prop (concat " [" default-prop "]") "")
  13936. ": ")
  13937. (mapcar 'list keys)
  13938. nil nil nil nil
  13939. default-prop)))
  13940. (if (member property keys)
  13941. property
  13942. (or (cdr (assoc (downcase property)
  13943. (mapcar (lambda (x) (cons (downcase x) x))
  13944. keys)))
  13945. property))))
  13946. (defun org-set-property-and-value (use-last)
  13947. "Allow to set [PROPERTY]: [value] direction from prompt.
  13948. When use-default, don't even ask, just use the last
  13949. \"[PROPERTY]: [value]\" string from the history."
  13950. (interactive "P")
  13951. (let* ((completion-ignore-case t)
  13952. (pv (or (and use-last org-last-set-property-value)
  13953. (org-completing-read
  13954. "Enter a \"[Property]: [value]\" pair: "
  13955. nil nil nil nil nil
  13956. org-last-set-property-value)))
  13957. prop val)
  13958. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13959. (setq prop (match-string 1 pv)
  13960. val (match-string 2 pv))
  13961. (org-set-property prop val))))
  13962. (defun org-set-property (property value)
  13963. "In the current entry, set PROPERTY to VALUE.
  13964. When called interactively, this will prompt for a property name, offering
  13965. completion on existing and default properties. And then it will prompt
  13966. for a value, offering completion either on allowed values (via an inherited
  13967. xxx_ALL property) or on existing values in other instances of this property
  13968. in the current file."
  13969. (interactive (list nil nil))
  13970. (let* ((property (or property (org-read-property-name)))
  13971. (value (or value (org-read-property-value property)))
  13972. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13973. (setq org-last-set-property property)
  13974. (setq org-last-set-property-value (concat property ": " value))
  13975. ;; Possibly postprocess the inserted value:
  13976. (when fn (setq value (funcall fn value)))
  13977. (unless (equal (org-entry-get nil property) value)
  13978. (org-entry-put nil property value))))
  13979. (defun org-delete-property (property)
  13980. "In the current entry, delete PROPERTY."
  13981. (interactive
  13982. (let* ((completion-ignore-case t)
  13983. (prop (org-icompleting-read "Property: "
  13984. (org-entry-properties nil 'standard))))
  13985. (list prop)))
  13986. (message "Property %s %s" property
  13987. (if (org-entry-delete nil property)
  13988. "deleted"
  13989. "was not present in the entry")))
  13990. (defun org-delete-property-globally (property)
  13991. "Remove PROPERTY globally, from all entries."
  13992. (interactive
  13993. (let* ((completion-ignore-case t)
  13994. (prop (org-icompleting-read
  13995. "Globally remove property: "
  13996. (mapcar 'list (org-buffer-property-keys)))))
  13997. (list prop)))
  13998. (save-excursion
  13999. (save-restriction
  14000. (widen)
  14001. (goto-char (point-min))
  14002. (let ((cnt 0))
  14003. (while (re-search-forward
  14004. (org-re-property property)
  14005. nil t)
  14006. (setq cnt (1+ cnt))
  14007. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14008. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14009. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14010. (defun org-compute-property-at-point ()
  14011. "Compute the property at point.
  14012. This looks for an enclosing column format, extracts the operator and
  14013. then applies it to the property in the column format's scope."
  14014. (interactive)
  14015. (unless (org-at-property-p)
  14016. (error "Not at a property"))
  14017. (let ((prop (org-match-string-no-properties 2)))
  14018. (org-columns-get-format-and-top-level)
  14019. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14020. (error "No operator defined for property %s" prop))
  14021. (org-columns-compute prop)))
  14022. (defvar org-property-allowed-value-functions nil
  14023. "Hook for functions supplying allowed values for a specific property.
  14024. The functions must take a single argument, the name of the property, and
  14025. return a flat list of allowed values. If \":ETC\" is one of
  14026. the values, this means that these values are intended as defaults for
  14027. completion, but that other values should be allowed too.
  14028. The functions must return nil if they are not responsible for this
  14029. property.")
  14030. (defun org-property-get-allowed-values (pom property &optional table)
  14031. "Get allowed values for the property PROPERTY.
  14032. When TABLE is non-nil, return an alist that can directly be used for
  14033. completion."
  14034. (let (vals)
  14035. (cond
  14036. ((equal property "TODO")
  14037. (setq vals (org-with-point-at pom
  14038. (append org-todo-keywords-1 '("")))))
  14039. ((equal property "PRIORITY")
  14040. (let ((n org-lowest-priority))
  14041. (while (>= n org-highest-priority)
  14042. (push (char-to-string n) vals)
  14043. (setq n (1- n)))))
  14044. ((member property org-special-properties))
  14045. ((setq vals (run-hook-with-args-until-success
  14046. 'org-property-allowed-value-functions property)))
  14047. (t
  14048. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14049. (when (and vals (string-match "\\S-" vals))
  14050. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14051. (setq vals (mapcar (lambda (x)
  14052. (cond ((stringp x) x)
  14053. ((numberp x) (number-to-string x))
  14054. ((symbolp x) (symbol-name x))
  14055. (t "???")))
  14056. vals)))))
  14057. (when (member ":ETC" vals)
  14058. (setq vals (remove ":ETC" vals))
  14059. (org-add-props (car vals) '(org-unrestricted t)))
  14060. (if table (mapcar 'list vals) vals)))
  14061. (defun org-property-previous-allowed-value (&optional previous)
  14062. "Switch to the next allowed value for this property."
  14063. (interactive)
  14064. (org-property-next-allowed-value t))
  14065. (defun org-property-next-allowed-value (&optional previous)
  14066. "Switch to the next allowed value for this property."
  14067. (interactive)
  14068. (unless (org-at-property-p)
  14069. (error "Not at a property"))
  14070. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14071. (key (match-string 2))
  14072. (value (match-string 3))
  14073. (allowed (or (org-property-get-allowed-values (point) key)
  14074. (and (member value '("[ ]" "[-]" "[X]"))
  14075. '("[ ]" "[X]"))))
  14076. nval)
  14077. (unless allowed
  14078. (error "Allowed values for this property have not been defined"))
  14079. (if previous (setq allowed (reverse allowed)))
  14080. (if (member value allowed)
  14081. (setq nval (car (cdr (member value allowed)))))
  14082. (setq nval (or nval (car allowed)))
  14083. (if (equal nval value)
  14084. (error "Only one allowed value for this property"))
  14085. (org-at-property-p)
  14086. (replace-match (concat " :" key ": " nval) t t)
  14087. (org-indent-line)
  14088. (beginning-of-line 1)
  14089. (skip-chars-forward " \t")
  14090. (when (equal prop org-effort-property)
  14091. (save-excursion
  14092. (org-back-to-heading t)
  14093. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  14094. (run-hook-with-args 'org-property-changed-functions key nval)))
  14095. (defun org-find-olp (path &optional this-buffer)
  14096. "Return a marker pointing to the entry at outline path OLP.
  14097. If anything goes wrong, throw an error.
  14098. You can wrap this call to catch the error like this:
  14099. (condition-case msg
  14100. (org-mobile-locate-entry (match-string 4))
  14101. (error (nth 1 msg)))
  14102. The return value will then be either a string with the error message,
  14103. or a marker if everything is OK.
  14104. If THIS-BUFFER is set, the outline path does not contain a file,
  14105. only headings."
  14106. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14107. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14108. (level 1)
  14109. (lmin 1)
  14110. (lmax 1)
  14111. limit re end found pos heading cnt flevel)
  14112. (unless buffer (error "File not found :%s" file))
  14113. (with-current-buffer buffer
  14114. (save-excursion
  14115. (save-restriction
  14116. (widen)
  14117. (setq limit (point-max))
  14118. (goto-char (point-min))
  14119. (while (setq heading (pop path))
  14120. (setq re (format org-complex-heading-regexp-format
  14121. (regexp-quote heading)))
  14122. (setq cnt 0 pos (point))
  14123. (while (re-search-forward re end t)
  14124. (setq level (- (match-end 1) (match-beginning 1)))
  14125. (if (and (>= level lmin) (<= level lmax))
  14126. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14127. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14128. lmax heading))
  14129. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14130. lmax heading))
  14131. (goto-char found)
  14132. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14133. (setq end (save-excursion (org-end-of-subtree t t))))
  14134. (when (org-at-heading-p)
  14135. (point-marker)))))))
  14136. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14137. "Find node HEADING in BUFFER.
  14138. Return a marker to the heading if it was found, or nil if not.
  14139. If POS-ONLY is set, return just the position instead of a marker.
  14140. The heading text must match exact, but it may have a TODO keyword,
  14141. a priority cookie and tags in the standard locations."
  14142. (with-current-buffer (or buffer (current-buffer))
  14143. (save-excursion
  14144. (save-restriction
  14145. (widen)
  14146. (goto-char (point-min))
  14147. (let (case-fold-search)
  14148. (if (re-search-forward
  14149. (format org-complex-heading-regexp-format
  14150. (regexp-quote heading)) nil t)
  14151. (if pos-only
  14152. (match-beginning 0)
  14153. (move-marker (make-marker) (match-beginning 0)))))))))
  14154. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14155. "Find Org node headline HEADING in all .org files in directory DIR.
  14156. When the target headline is found, return a marker to this location."
  14157. (let ((files (directory-files (or dir default-directory)
  14158. nil "\\`[^.#].*\\.org\\'"))
  14159. file visiting m buffer)
  14160. (catch 'found
  14161. (while (setq file (pop files))
  14162. (message "trying %s" file)
  14163. (setq visiting (org-find-base-buffer-visiting file))
  14164. (setq buffer (or visiting (find-file-noselect file)))
  14165. (setq m (org-find-exact-headline-in-buffer
  14166. heading buffer))
  14167. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14168. (and m (throw 'found m))))))
  14169. (defun org-find-entry-with-id (ident)
  14170. "Locate the entry that contains the ID property with exact value IDENT.
  14171. IDENT can be a string, a symbol or a number, this function will search for
  14172. the string representation of it.
  14173. Return the position where this entry starts, or nil if there is no such entry."
  14174. (interactive "sID: ")
  14175. (let ((id (cond
  14176. ((stringp ident) ident)
  14177. ((symbol-name ident) (symbol-name ident))
  14178. ((numberp ident) (number-to-string ident))
  14179. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14180. (case-fold-search nil))
  14181. (save-excursion
  14182. (save-restriction
  14183. (widen)
  14184. (goto-char (point-min))
  14185. (when (re-search-forward
  14186. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14187. nil t)
  14188. (org-back-to-heading t)
  14189. (point))))))
  14190. ;;;; Timestamps
  14191. (defvar org-last-changed-timestamp nil)
  14192. (defvar org-last-inserted-timestamp nil
  14193. "The last time stamp inserted with `org-insert-time-stamp'.")
  14194. (defvar org-time-was-given) ; dynamically scoped parameter
  14195. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14196. (defvar org-ts-what) ; dynamically scoped parameter
  14197. (defun org-time-stamp (arg &optional inactive)
  14198. "Prompt for a date/time and insert a time stamp.
  14199. If the user specifies a time like HH:MM or if this command is
  14200. called with at least one prefix argument, the time stamp contains
  14201. the date and the time. Otherwise, only the date is be included.
  14202. All parts of a date not specified by the user is filled in from
  14203. the current date/time. So if you just press return without
  14204. typing anything, the time stamp will represent the current
  14205. date/time.
  14206. If there is already a timestamp at the cursor, it will be
  14207. modified.
  14208. With two universal prefix arguments, insert an active timestamp
  14209. with the current time without prompting the user."
  14210. (interactive "P")
  14211. (let* ((ts nil)
  14212. (default-time
  14213. ;; Default time is either today, or, when entering a range,
  14214. ;; the range start.
  14215. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14216. (save-excursion
  14217. (re-search-backward
  14218. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14219. (- (point) 20) t)))
  14220. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14221. (current-time)))
  14222. (default-input (and ts (org-get-compact-tod ts)))
  14223. (repeater (save-excursion
  14224. (save-match-data
  14225. (beginning-of-line)
  14226. (when (re-search-forward
  14227. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14228. (save-excursion (progn (end-of-line) (point))) t)
  14229. (match-string 0)))))
  14230. org-time-was-given org-end-time-was-given time)
  14231. (cond
  14232. ((and (org-at-timestamp-p t)
  14233. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14234. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14235. (insert "--")
  14236. (setq time (let ((this-command this-command))
  14237. (org-read-date arg 'totime nil nil
  14238. default-time default-input inactive)))
  14239. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14240. ((org-at-timestamp-p t)
  14241. (setq time (let ((this-command this-command))
  14242. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14243. (when (org-at-timestamp-p t) ; just to get the match data
  14244. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14245. (replace-match "")
  14246. (setq org-last-changed-timestamp
  14247. (org-insert-time-stamp
  14248. time (or org-time-was-given arg)
  14249. inactive nil nil (list org-end-time-was-given)))
  14250. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14251. (setq org-last-changed-timestamp
  14252. (concat (substring org-last-inserted-timestamp 0 -1)
  14253. " " repeater ">"))))
  14254. (message "Timestamp updated"))
  14255. ((equal arg '(16))
  14256. (org-insert-time-stamp (current-time) t))
  14257. (t
  14258. (setq time (let ((this-command this-command))
  14259. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14260. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14261. nil nil (list org-end-time-was-given))))))
  14262. ;; FIXME: can we use this for something else, like computing time differences?
  14263. (defun org-get-compact-tod (s)
  14264. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14265. (let* ((t1 (match-string 1 s))
  14266. (h1 (string-to-number (match-string 2 s)))
  14267. (m1 (string-to-number (match-string 3 s)))
  14268. (t2 (and (match-end 4) (match-string 5 s)))
  14269. (h2 (and t2 (string-to-number (match-string 6 s))))
  14270. (m2 (and t2 (string-to-number (match-string 7 s))))
  14271. dh dm)
  14272. (if (not t2)
  14273. t1
  14274. (setq dh (- h2 h1) dm (- m2 m1))
  14275. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14276. (concat t1 "+" (number-to-string dh)
  14277. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14278. (defun org-time-stamp-inactive (&optional arg)
  14279. "Insert an inactive time stamp.
  14280. An inactive time stamp is enclosed in square brackets instead of angle
  14281. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14282. does not link to the calendar and cannot be changed with the S-cursor keys.
  14283. So these are more for recording a certain time/date."
  14284. (interactive "P")
  14285. (org-time-stamp arg 'inactive))
  14286. (defvar org-date-ovl (make-overlay 1 1))
  14287. (overlay-put org-date-ovl 'face 'org-date-selected)
  14288. (org-detach-overlay org-date-ovl)
  14289. (defvar org-ans1) ; dynamically scoped parameter
  14290. (defvar org-ans2) ; dynamically scoped parameter
  14291. (defvar org-plain-time-of-day-regexp) ; defined below
  14292. (defvar org-overriding-default-time nil) ; dynamically scoped
  14293. (defvar org-read-date-overlay nil)
  14294. (defvar org-dcst nil) ; dynamically scoped
  14295. (defvar org-read-date-history nil)
  14296. (defvar org-read-date-final-answer nil)
  14297. (defvar org-read-date-analyze-futurep nil)
  14298. (defvar org-read-date-analyze-forced-year nil)
  14299. (defvar org-read-date-inactive)
  14300. (defvar org-read-date-minibuffer-local-map
  14301. (let ((map (make-sparse-keymap)))
  14302. (set-keymap-parent map minibuffer-local-map)
  14303. (org-defkey map (kbd ".")
  14304. (lambda () (interactive)
  14305. (org-eval-in-calendar '(calendar-goto-today))))
  14306. (org-defkey map [(meta shift left)]
  14307. (lambda () (interactive)
  14308. (org-eval-in-calendar '(calendar-backward-month 1))))
  14309. (org-defkey map [(meta shift right)]
  14310. (lambda () (interactive)
  14311. (org-eval-in-calendar '(calendar-forward-month 1))))
  14312. (org-defkey map [(meta shift up)]
  14313. (lambda () (interactive)
  14314. (org-eval-in-calendar '(calendar-backward-year 1))))
  14315. (org-defkey map [(meta shift down)]
  14316. (lambda () (interactive)
  14317. (org-eval-in-calendar '(calendar-forward-year 1))))
  14318. (org-defkey map [?\e (shift left)]
  14319. (lambda () (interactive)
  14320. (org-eval-in-calendar '(calendar-backward-month 1))))
  14321. (org-defkey map [?\e (shift right)]
  14322. (lambda () (interactive)
  14323. (org-eval-in-calendar '(calendar-forward-month 1))))
  14324. (org-defkey map [?\e (shift up)]
  14325. (lambda () (interactive)
  14326. (org-eval-in-calendar '(calendar-backward-year 1))))
  14327. (org-defkey map [?\e (shift down)]
  14328. (lambda () (interactive)
  14329. (org-eval-in-calendar '(calendar-forward-year 1))))
  14330. (org-defkey map [(shift up)]
  14331. (lambda () (interactive)
  14332. (org-eval-in-calendar '(calendar-backward-week 1))))
  14333. (org-defkey map [(shift down)]
  14334. (lambda () (interactive)
  14335. (org-eval-in-calendar '(calendar-forward-week 1))))
  14336. (org-defkey map [(shift left)]
  14337. (lambda () (interactive)
  14338. (org-eval-in-calendar '(calendar-backward-day 1))))
  14339. (org-defkey map [(shift right)]
  14340. (lambda () (interactive)
  14341. (org-eval-in-calendar '(calendar-forward-day 1))))
  14342. (org-defkey map "!"
  14343. (lambda () (interactive)
  14344. (org-eval-in-calendar '(diary-view-entries))
  14345. (message "")))
  14346. (org-defkey map ">"
  14347. (lambda () (interactive)
  14348. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14349. (org-defkey map "<"
  14350. (lambda () (interactive)
  14351. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14352. (org-defkey map "\C-v"
  14353. (lambda () (interactive)
  14354. (org-eval-in-calendar
  14355. '(calendar-scroll-left-three-months 1))))
  14356. (org-defkey map "\M-v"
  14357. (lambda () (interactive)
  14358. (org-eval-in-calendar
  14359. '(calendar-scroll-right-three-months 1))))
  14360. map)
  14361. "Keymap for minibuffer commands when using `org-read-date'.")
  14362. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14363. default-time default-input inactive)
  14364. "Read a date, possibly a time, and make things smooth for the user.
  14365. The prompt will suggest to enter an ISO date, but you can also enter anything
  14366. which will at least partially be understood by `parse-time-string'.
  14367. Unrecognized parts of the date will default to the current day, month, year,
  14368. hour and minute. If this command is called to replace a timestamp at point,
  14369. or to enter the second timestamp of a range, the default time is taken
  14370. from the existing stamp. Furthermore, the command prefers the future,
  14371. so if you are giving a date where the year is not given, and the day-month
  14372. combination is already past in the current year, it will assume you
  14373. mean next year. For details, see the manual. A few examples:
  14374. 3-2-5 --> 2003-02-05
  14375. feb 15 --> currentyear-02-15
  14376. 2/15 --> currentyear-02-15
  14377. sep 12 9 --> 2009-09-12
  14378. 12:45 --> today 12:45
  14379. 22 sept 0:34 --> currentyear-09-22 0:34
  14380. 12 --> currentyear-currentmonth-12
  14381. Fri --> nearest Friday (today or later)
  14382. etc.
  14383. Furthermore you can specify a relative date by giving, as the *first* thing
  14384. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14385. change in days weeks, months, years.
  14386. With a single plus or minus, the date is relative to today. With a double
  14387. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14388. +4d --> four days from today
  14389. +4 --> same as above
  14390. +2w --> two weeks from today
  14391. ++5 --> five days from default date
  14392. The function understands only English month and weekday abbreviations.
  14393. While prompting, a calendar is popped up - you can also select the
  14394. date with the mouse (button 1). The calendar shows a period of three
  14395. months. To scroll it to other months, use the keys `>' and `<'.
  14396. If you don't like the calendar, turn it off with
  14397. \(setq org-read-date-popup-calendar nil)
  14398. With optional argument TO-TIME, the date will immediately be converted
  14399. to an internal time.
  14400. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14401. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14402. still enter a time, and this function will inform the calling routine
  14403. about this change. The calling routine may then choose to change the
  14404. format used to insert the time stamp into the buffer to include the time.
  14405. With optional argument FROM-STRING, read from this string instead from
  14406. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14407. the time/date that is used for everything that is not specified by the
  14408. user."
  14409. (require 'parse-time)
  14410. (let* ((org-time-stamp-rounding-minutes
  14411. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14412. (org-dcst org-display-custom-times)
  14413. (ct (org-current-time))
  14414. (org-def (or org-overriding-default-time default-time ct))
  14415. (org-defdecode (decode-time org-def))
  14416. (dummy (progn
  14417. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14418. (setcar (nthcdr 2 org-defdecode) -1)
  14419. (setcar (nthcdr 1 org-defdecode) 59)
  14420. (setq org-def (apply 'encode-time org-defdecode)
  14421. org-defdecode (decode-time org-def)))))
  14422. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14423. (calendar-frame-setup nil)
  14424. (calendar-setup nil)
  14425. (calendar-move-hook nil)
  14426. (calendar-view-diary-initially-flag nil)
  14427. (calendar-view-holidays-initially-flag nil)
  14428. (timestr (format-time-string
  14429. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14430. (prompt (concat (if prompt (concat prompt " ") "")
  14431. (format "Date+time [%s]: " timestr)))
  14432. ans (org-ans0 "") org-ans1 org-ans2 final)
  14433. (cond
  14434. (from-string (setq ans from-string))
  14435. (org-read-date-popup-calendar
  14436. (save-excursion
  14437. (save-window-excursion
  14438. (calendar)
  14439. (org-eval-in-calendar '(setq cursor-type nil) t)
  14440. (unwind-protect
  14441. (progn
  14442. (calendar-forward-day (- (time-to-days org-def)
  14443. (calendar-absolute-from-gregorian
  14444. (calendar-current-date))))
  14445. (org-eval-in-calendar nil t)
  14446. (let* ((old-map (current-local-map))
  14447. (map (copy-keymap calendar-mode-map))
  14448. (minibuffer-local-map
  14449. (copy-keymap org-read-date-minibuffer-local-map)))
  14450. (org-defkey map (kbd "RET") 'org-calendar-select)
  14451. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14452. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14453. (unwind-protect
  14454. (progn
  14455. (use-local-map map)
  14456. (setq org-read-date-inactive inactive)
  14457. (add-hook 'post-command-hook 'org-read-date-display)
  14458. (setq org-ans0 (read-string prompt default-input
  14459. 'org-read-date-history nil))
  14460. ;; org-ans0: from prompt
  14461. ;; org-ans1: from mouse click
  14462. ;; org-ans2: from calendar motion
  14463. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14464. (remove-hook 'post-command-hook 'org-read-date-display)
  14465. (use-local-map old-map)
  14466. (when org-read-date-overlay
  14467. (delete-overlay org-read-date-overlay)
  14468. (setq org-read-date-overlay nil)))))
  14469. (bury-buffer "*Calendar*")))))
  14470. (t ; Naked prompt only
  14471. (unwind-protect
  14472. (setq ans (read-string prompt default-input
  14473. 'org-read-date-history timestr))
  14474. (when org-read-date-overlay
  14475. (delete-overlay org-read-date-overlay)
  14476. (setq org-read-date-overlay nil)))))
  14477. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14478. (when org-read-date-analyze-forced-year
  14479. (message "Year was forced into %s"
  14480. (if org-read-date-force-compatible-dates
  14481. "compatible range (1970-2037)"
  14482. "range representable on this machine"))
  14483. (ding))
  14484. ;; One round trip to get rid of 34th of August and stuff like that....
  14485. (setq final (decode-time (apply 'encode-time final)))
  14486. (setq org-read-date-final-answer ans)
  14487. (if to-time
  14488. (apply 'encode-time final)
  14489. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14490. (format "%04d-%02d-%02d %02d:%02d"
  14491. (nth 5 final) (nth 4 final) (nth 3 final)
  14492. (nth 2 final) (nth 1 final))
  14493. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14494. (defvar org-def)
  14495. (defvar org-defdecode)
  14496. (defvar org-with-time)
  14497. (defun org-read-date-display ()
  14498. "Display the current date prompt interpretation in the minibuffer."
  14499. (when org-read-date-display-live
  14500. (when org-read-date-overlay
  14501. (delete-overlay org-read-date-overlay))
  14502. (when (minibufferp (current-buffer))
  14503. (save-excursion
  14504. (end-of-line 1)
  14505. (while (not (equal (buffer-substring
  14506. (max (point-min) (- (point) 4)) (point))
  14507. " "))
  14508. (insert " ")))
  14509. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14510. " " (or org-ans1 org-ans2)))
  14511. (org-end-time-was-given nil)
  14512. (f (org-read-date-analyze ans org-def org-defdecode))
  14513. (fmts (if org-dcst
  14514. org-time-stamp-custom-formats
  14515. org-time-stamp-formats))
  14516. (fmt (if (or org-with-time
  14517. (and (boundp 'org-time-was-given) org-time-was-given))
  14518. (cdr fmts)
  14519. (car fmts)))
  14520. (txt (format-time-string fmt (apply 'encode-time f)))
  14521. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14522. (txt (concat "=> " txt)))
  14523. (when (and org-end-time-was-given
  14524. (string-match org-plain-time-of-day-regexp txt))
  14525. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14526. org-end-time-was-given
  14527. (substring txt (match-end 0)))))
  14528. (when org-read-date-analyze-futurep
  14529. (setq txt (concat txt " (=>F)")))
  14530. (setq org-read-date-overlay
  14531. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14532. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14533. (defun org-read-date-analyze (ans org-def org-defdecode)
  14534. "Analyze the combined answer of the date prompt."
  14535. ;; FIXME: cleanup and comment
  14536. (let ((nowdecode (decode-time (current-time)))
  14537. delta deltan deltaw deltadef year month day
  14538. hour minute second wday pm h2 m2 tl wday1
  14539. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14540. (setq org-read-date-analyze-futurep nil
  14541. org-read-date-analyze-forced-year nil)
  14542. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14543. (setq ans "+0"))
  14544. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14545. (setq ans (replace-match "" t t ans)
  14546. deltan (car delta)
  14547. deltaw (nth 1 delta)
  14548. deltadef (nth 2 delta)))
  14549. ;; Check if there is an iso week date in there. If yes, store the
  14550. ;; info and postpone interpreting it until the rest of the parsing
  14551. ;; is done.
  14552. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14553. (setq iso-year (if (match-end 1)
  14554. (org-small-year-to-year
  14555. (string-to-number (match-string 1 ans))))
  14556. iso-weekday (if (match-end 3)
  14557. (string-to-number (match-string 3 ans)))
  14558. iso-week (string-to-number (match-string 2 ans)))
  14559. (setq ans (replace-match "" t t ans)))
  14560. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14561. (when (string-match
  14562. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14563. (setq year (if (match-end 2)
  14564. (string-to-number (match-string 2 ans))
  14565. (progn (setq kill-year t)
  14566. (string-to-number (format-time-string "%Y"))))
  14567. month (string-to-number (match-string 3 ans))
  14568. day (string-to-number (match-string 4 ans)))
  14569. (if (< year 100) (setq year (+ 2000 year)))
  14570. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14571. t nil ans)))
  14572. ;; Help matching dotted european dates
  14573. (when (string-match
  14574. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14575. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14576. (setq kill-year t)
  14577. (string-to-number (format-time-string "%Y")))
  14578. day (string-to-number (match-string 1 ans))
  14579. month (string-to-number (match-string 2 ans))
  14580. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14581. t nil ans)))
  14582. ;; Help matching american dates, like 5/30 or 5/30/7
  14583. (when (string-match
  14584. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14585. (setq year (if (match-end 4)
  14586. (string-to-number (match-string 4 ans))
  14587. (progn (setq kill-year t)
  14588. (string-to-number (format-time-string "%Y"))))
  14589. month (string-to-number (match-string 1 ans))
  14590. day (string-to-number (match-string 2 ans)))
  14591. (if (< year 100) (setq year (+ 2000 year)))
  14592. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14593. t nil ans)))
  14594. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14595. ;; If there is a time with am/pm, and *no* time without it, we convert
  14596. ;; so that matching will be successful.
  14597. (loop for i from 1 to 2 do ; twice, for end time as well
  14598. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14599. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14600. (setq hour (string-to-number (match-string 1 ans))
  14601. minute (if (match-end 3)
  14602. (string-to-number (match-string 3 ans))
  14603. 0)
  14604. pm (equal ?p
  14605. (string-to-char (downcase (match-string 4 ans)))))
  14606. (if (and (= hour 12) (not pm))
  14607. (setq hour 0)
  14608. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14609. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14610. t t ans))))
  14611. ;; Check if a time range is given as a duration
  14612. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14613. (setq hour (string-to-number (match-string 1 ans))
  14614. h2 (+ hour (string-to-number (match-string 3 ans)))
  14615. minute (string-to-number (match-string 2 ans))
  14616. m2 (+ minute (if (match-end 5) (string-to-number
  14617. (match-string 5 ans))0)))
  14618. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14619. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14620. t t ans)))
  14621. ;; Check if there is a time range
  14622. (when (boundp 'org-end-time-was-given)
  14623. (setq org-time-was-given nil)
  14624. (when (and (string-match org-plain-time-of-day-regexp ans)
  14625. (match-end 8))
  14626. (setq org-end-time-was-given (match-string 8 ans))
  14627. (setq ans (concat (substring ans 0 (match-beginning 7))
  14628. (substring ans (match-end 7))))))
  14629. (setq tl (parse-time-string ans)
  14630. day (or (nth 3 tl) (nth 3 org-defdecode))
  14631. month (or (nth 4 tl)
  14632. (if (and org-read-date-prefer-future
  14633. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14634. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14635. (nth 4 org-defdecode)))
  14636. year (or (and (not kill-year) (nth 5 tl))
  14637. (if (and org-read-date-prefer-future
  14638. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14639. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14640. (nth 5 org-defdecode)))
  14641. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14642. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14643. second (or (nth 0 tl) 0)
  14644. wday (nth 6 tl))
  14645. (when (and (eq org-read-date-prefer-future 'time)
  14646. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14647. (equal day (nth 3 nowdecode))
  14648. (equal month (nth 4 nowdecode))
  14649. (equal year (nth 5 nowdecode))
  14650. (nth 2 tl)
  14651. (or (< (nth 2 tl) (nth 2 nowdecode))
  14652. (and (= (nth 2 tl) (nth 2 nowdecode))
  14653. (nth 1 tl)
  14654. (< (nth 1 tl) (nth 1 nowdecode)))))
  14655. (setq day (1+ day)
  14656. futurep t))
  14657. ;; Special date definitions below
  14658. (cond
  14659. (iso-week
  14660. ;; There was an iso week
  14661. (require 'cal-iso)
  14662. (setq futurep nil)
  14663. (setq year (or iso-year year)
  14664. day (or iso-weekday wday 1)
  14665. wday nil ; to make sure that the trigger below does not match
  14666. iso-date (calendar-gregorian-from-absolute
  14667. (calendar-absolute-from-iso
  14668. (list iso-week day year))))
  14669. ; FIXME: Should we also push ISO weeks into the future?
  14670. ; (when (and org-read-date-prefer-future
  14671. ; (not iso-year)
  14672. ; (< (calendar-absolute-from-gregorian iso-date)
  14673. ; (time-to-days (current-time))))
  14674. ; (setq year (1+ year)
  14675. ; iso-date (calendar-gregorian-from-absolute
  14676. ; (calendar-absolute-from-iso
  14677. ; (list iso-week day year)))))
  14678. (setq month (car iso-date)
  14679. year (nth 2 iso-date)
  14680. day (nth 1 iso-date)))
  14681. (deltan
  14682. (setq futurep nil)
  14683. (unless deltadef
  14684. (let ((now (decode-time (current-time))))
  14685. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14686. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14687. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14688. ((equal deltaw "m") (setq month (+ month deltan)))
  14689. ((equal deltaw "y") (setq year (+ year deltan)))))
  14690. ((and wday (not (nth 3 tl)))
  14691. ;; Weekday was given, but no day, so pick that day in the week
  14692. ;; on or after the derived date.
  14693. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14694. (unless (equal wday wday1)
  14695. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14696. (if (and (boundp 'org-time-was-given)
  14697. (nth 2 tl))
  14698. (setq org-time-was-given t))
  14699. (if (< year 100) (setq year (+ 2000 year)))
  14700. ;; Check of the date is representable
  14701. (if org-read-date-force-compatible-dates
  14702. (progn
  14703. (if (< year 1970)
  14704. (setq year 1970 org-read-date-analyze-forced-year t))
  14705. (if (> year 2037)
  14706. (setq year 2037 org-read-date-analyze-forced-year t)))
  14707. (condition-case nil
  14708. (ignore (encode-time second minute hour day month year))
  14709. (error
  14710. (setq year (nth 5 org-defdecode))
  14711. (setq org-read-date-analyze-forced-year t))))
  14712. (setq org-read-date-analyze-futurep futurep)
  14713. (list second minute hour day month year)))
  14714. (defvar parse-time-weekdays)
  14715. (defun org-read-date-get-relative (s today default)
  14716. "Check string S for special relative date string.
  14717. TODAY and DEFAULT are internal times, for today and for a default.
  14718. Return shift list (N what def-flag)
  14719. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14720. N is the number of WHATs to shift.
  14721. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14722. the DEFAULT date rather than TODAY."
  14723. (require 'parse-time)
  14724. (when (and
  14725. (string-match
  14726. (concat
  14727. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14728. "\\([0-9]+\\)?"
  14729. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14730. "\\([ \t]\\|$\\)") s)
  14731. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14732. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14733. (string-to-char (substring (match-string 1 s) -1))
  14734. ?+))
  14735. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14736. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14737. (what (if (match-end 3) (match-string 3 s) "d"))
  14738. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14739. (date (if rel default today))
  14740. (wday (nth 6 (decode-time date)))
  14741. delta)
  14742. (if wday1
  14743. (progn
  14744. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14745. (if (= dir ?-) (setq delta (- delta 7)))
  14746. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14747. (list delta "d" rel))
  14748. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14749. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14750. "Turn a user-specified date into the internal representation.
  14751. The internal representation needed by the calendar is (month day year).
  14752. This is a wrapper to handle the brain-dead convention in calendar that
  14753. user function argument order change dependent on argument order."
  14754. (if (boundp 'calendar-date-style)
  14755. (cond
  14756. ((eq calendar-date-style 'american)
  14757. (list arg1 arg2 arg3))
  14758. ((eq calendar-date-style 'european)
  14759. (list arg2 arg1 arg3))
  14760. ((eq calendar-date-style 'iso)
  14761. (list arg2 arg3 arg1)))
  14762. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14763. (if (org-bound-and-true-p european-calendar-style)
  14764. (list arg2 arg1 arg3)
  14765. (list arg1 arg2 arg3)))))
  14766. (defun org-eval-in-calendar (form &optional keepdate)
  14767. "Eval FORM in the calendar window and return to current window.
  14768. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14769. otherwise stick to the current value of `org-ans2'."
  14770. (let ((sf (selected-frame))
  14771. (sw (selected-window)))
  14772. (select-window (get-buffer-window "*Calendar*" t))
  14773. (eval form)
  14774. (when (and (not keepdate) (calendar-cursor-to-date))
  14775. (let* ((date (calendar-cursor-to-date))
  14776. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14777. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14778. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14779. (select-window sw)
  14780. (org-select-frame-set-input-focus sf)))
  14781. (defun org-calendar-select ()
  14782. "Return to `org-read-date' with the date currently selected.
  14783. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14784. (interactive)
  14785. (when (calendar-cursor-to-date)
  14786. (let* ((date (calendar-cursor-to-date))
  14787. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14788. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14789. (if (active-minibuffer-window) (exit-minibuffer))))
  14790. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14791. "Insert a date stamp for the date given by the internal TIME.
  14792. WITH-HM means use the stamp format that includes the time of the day.
  14793. INACTIVE means use square brackets instead of angular ones, so that the
  14794. stamp will not contribute to the agenda.
  14795. PRE and POST are optional strings to be inserted before and after the
  14796. stamp.
  14797. The command returns the inserted time stamp."
  14798. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14799. stamp)
  14800. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14801. (insert-before-markers (or pre ""))
  14802. (when (listp extra)
  14803. (setq extra (car extra))
  14804. (if (and (stringp extra)
  14805. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14806. (setq extra (format "-%02d:%02d"
  14807. (string-to-number (match-string 1 extra))
  14808. (string-to-number (match-string 2 extra))))
  14809. (setq extra nil)))
  14810. (when extra
  14811. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14812. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14813. (insert-before-markers (or post ""))
  14814. (setq org-last-inserted-timestamp stamp)))
  14815. (defun org-toggle-time-stamp-overlays ()
  14816. "Toggle the use of custom time stamp formats."
  14817. (interactive)
  14818. (setq org-display-custom-times (not org-display-custom-times))
  14819. (unless org-display-custom-times
  14820. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14821. (while (setq p (next-single-property-change p 'display))
  14822. (if (and (get-text-property p 'display)
  14823. (eq (get-text-property p 'face) 'org-date))
  14824. (remove-text-properties
  14825. p (setq p (next-single-property-change p 'display))
  14826. '(display t))))
  14827. (set-buffer-modified-p bmp)))
  14828. (if (featurep 'xemacs)
  14829. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14830. (org-restart-font-lock)
  14831. (setq org-table-may-need-update t)
  14832. (if org-display-custom-times
  14833. (message "Time stamps are overlaid with custom format")
  14834. (message "Time stamp overlays removed")))
  14835. (defun org-display-custom-time (beg end)
  14836. "Overlay modified time stamp format over timestamp between BEG and END."
  14837. (let* ((ts (buffer-substring beg end))
  14838. t1 w1 with-hm tf time str w2 (off 0))
  14839. (save-match-data
  14840. (setq t1 (org-parse-time-string ts t))
  14841. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14842. (setq off (- (match-end 0) (match-beginning 0)))))
  14843. (setq end (- end off))
  14844. (setq w1 (- end beg)
  14845. with-hm (and (nth 1 t1) (nth 2 t1))
  14846. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14847. time (org-fix-decoded-time t1)
  14848. str (org-add-props
  14849. (format-time-string
  14850. (substring tf 1 -1) (apply 'encode-time time))
  14851. nil 'mouse-face 'highlight)
  14852. w2 (length str))
  14853. (if (not (= w2 w1))
  14854. (add-text-properties (1+ beg) (+ 2 beg)
  14855. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14856. (if (featurep 'xemacs)
  14857. (progn
  14858. (put-text-property beg end 'invisible t)
  14859. (put-text-property beg end 'end-glyph (make-glyph str)))
  14860. (put-text-property beg end 'display str))))
  14861. (defun org-translate-time (string)
  14862. "Translate all timestamps in STRING to custom format.
  14863. But do this only if the variable `org-display-custom-times' is set."
  14864. (when org-display-custom-times
  14865. (save-match-data
  14866. (let* ((start 0)
  14867. (re org-ts-regexp-both)
  14868. t1 with-hm inactive tf time str beg end)
  14869. (while (setq start (string-match re string start))
  14870. (setq beg (match-beginning 0)
  14871. end (match-end 0)
  14872. t1 (save-match-data
  14873. (org-parse-time-string (substring string beg end) t))
  14874. with-hm (and (nth 1 t1) (nth 2 t1))
  14875. inactive (equal (substring string beg (1+ beg)) "[")
  14876. tf (funcall (if with-hm 'cdr 'car)
  14877. org-time-stamp-custom-formats)
  14878. time (org-fix-decoded-time t1)
  14879. str (format-time-string
  14880. (concat
  14881. (if inactive "[" "<") (substring tf 1 -1)
  14882. (if inactive "]" ">"))
  14883. (apply 'encode-time time))
  14884. string (replace-match str t t string)
  14885. start (+ start (length str)))))))
  14886. string)
  14887. (defun org-fix-decoded-time (time)
  14888. "Set 0 instead of nil for the first 6 elements of time.
  14889. Don't touch the rest."
  14890. (let ((n 0))
  14891. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14892. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14893. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14894. "Difference between TIMESTAMP-STRING and now in days.
  14895. If SECONDS is non-nil, return the difference in seconds."
  14896. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14897. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14898. (funcall fdiff (current-time)))))
  14899. (defun org-deadline-close (timestamp-string &optional ndays)
  14900. "Is the time in TIMESTAMP-STRING close to the current date?"
  14901. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14902. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14903. (not (org-entry-is-done-p))))
  14904. (defun org-get-wdays (ts &optional delay zero-delay)
  14905. "Get the deadline lead time appropriate for timestring TS.
  14906. When DELAY is non-nil, get the delay time for scheduled items
  14907. instead of the deadline lead time. When ZERO-DELAY is non-nil
  14908. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  14909. don't try to find the delay cookie in the scheduled timestamp."
  14910. (let ((tv (if delay org-scheduled-delay-days
  14911. org-deadline-warning-days)))
  14912. (cond
  14913. ((or (and delay (< tv 0))
  14914. (and delay zero-delay (<= tv 0))
  14915. (and (not delay) (<= tv 0)))
  14916. ;; Enforce this value no matter what
  14917. (- tv))
  14918. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14919. ;; lead time is specified.
  14920. (floor (* (string-to-number (match-string 1 ts))
  14921. (cdr (assoc (match-string 2 ts)
  14922. '(("d" . 1) ("w" . 7)
  14923. ("m" . 30.4) ("y" . 365.25)
  14924. ("h" . 0.041667)))))))
  14925. ;; go for the default.
  14926. (t tv))))
  14927. (defun org-calendar-select-mouse (ev)
  14928. "Return to `org-read-date' with the date currently selected.
  14929. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14930. (interactive "e")
  14931. (mouse-set-point ev)
  14932. (when (calendar-cursor-to-date)
  14933. (let* ((date (calendar-cursor-to-date))
  14934. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14935. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14936. (if (active-minibuffer-window) (exit-minibuffer))))
  14937. (defun org-check-deadlines (ndays)
  14938. "Check if there are any deadlines due or past due.
  14939. A deadline is considered due if it happens within `org-deadline-warning-days'
  14940. days from today's date. If the deadline appears in an entry marked DONE,
  14941. it is not shown. The prefix arg NDAYS can be used to test that many
  14942. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14943. (interactive "P")
  14944. (let* ((org-warn-days
  14945. (cond
  14946. ((equal ndays '(4)) 100000)
  14947. (ndays (prefix-numeric-value ndays))
  14948. (t (abs org-deadline-warning-days))))
  14949. (case-fold-search nil)
  14950. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14951. (callback
  14952. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14953. (message "%d deadlines past-due or due within %d days"
  14954. (org-occur regexp nil callback)
  14955. org-warn-days)))
  14956. (defsubst org-re-timestamp (type)
  14957. "Return a regexp for timestamp TYPE.
  14958. Allowed values for TYPE are:
  14959. all: all timestamps
  14960. active: only active timestamps (<...>)
  14961. inactive: only inactive timestamps ([...])
  14962. scheduled: only scheduled timestamps
  14963. deadline: only deadline timestamps
  14964. When TYPE is nil, fall back on returning a regexp that matches
  14965. both scheduled and deadline timestamps."
  14966. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14967. ((eq type 'active) org-ts-regexp)
  14968. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14969. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14970. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14971. ((eq type 'scheduled-or-deadline)
  14972. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14973. (defun org-check-before-date (date)
  14974. "Check if there are deadlines or scheduled entries before DATE."
  14975. (interactive (list (org-read-date)))
  14976. (let ((case-fold-search nil)
  14977. (regexp (org-re-timestamp org-ts-type))
  14978. (callback
  14979. (lambda () (time-less-p
  14980. (org-time-string-to-time (match-string 1))
  14981. (org-time-string-to-time date)))))
  14982. (message "%d entries before %s"
  14983. (org-occur regexp nil callback) date)))
  14984. (defun org-check-after-date (date)
  14985. "Check if there are deadlines or scheduled entries after DATE."
  14986. (interactive (list (org-read-date)))
  14987. (let ((case-fold-search nil)
  14988. (regexp (org-re-timestamp org-ts-type))
  14989. (callback
  14990. (lambda () (not
  14991. (time-less-p
  14992. (org-time-string-to-time (match-string 1))
  14993. (org-time-string-to-time date))))))
  14994. (message "%d entries after %s"
  14995. (org-occur regexp nil callback) date)))
  14996. (defun org-check-dates-range (start-date end-date)
  14997. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14998. (interactive (list (org-read-date nil nil nil "Range starts")
  14999. (org-read-date nil nil nil "Range end")))
  15000. (let ((case-fold-search nil)
  15001. (regexp (org-re-timestamp org-ts-type))
  15002. (callback
  15003. (lambda ()
  15004. (let ((match (match-string 1)))
  15005. (and
  15006. (not (time-less-p
  15007. (org-time-string-to-time match)
  15008. (org-time-string-to-time start-date)))
  15009. (time-less-p
  15010. (org-time-string-to-time match)
  15011. (org-time-string-to-time end-date)))))))
  15012. (message "%d entries between %s and %s"
  15013. (org-occur regexp nil callback) start-date end-date)))
  15014. (defun org-evaluate-time-range (&optional to-buffer)
  15015. "Evaluate a time range by computing the difference between start and end.
  15016. Normally the result is just printed in the echo area, but with prefix arg
  15017. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15018. If the time range is actually in a table, the result is inserted into the
  15019. next column.
  15020. For time difference computation, a year is assumed to be exactly 365
  15021. days in order to avoid rounding problems."
  15022. (interactive "P")
  15023. (or
  15024. (org-clock-update-time-maybe)
  15025. (save-excursion
  15026. (unless (org-at-date-range-p t)
  15027. (goto-char (point-at-bol))
  15028. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15029. (if (not (org-at-date-range-p t))
  15030. (error "Not at a time-stamp range, and none found in current line")))
  15031. (let* ((ts1 (match-string 1))
  15032. (ts2 (match-string 2))
  15033. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15034. (match-end (match-end 0))
  15035. (time1 (org-time-string-to-time ts1))
  15036. (time2 (org-time-string-to-time ts2))
  15037. (t1 (org-float-time time1))
  15038. (t2 (org-float-time time2))
  15039. (diff (abs (- t2 t1)))
  15040. (negative (< (- t2 t1) 0))
  15041. ;; (ys (floor (* 365 24 60 60)))
  15042. (ds (* 24 60 60))
  15043. (hs (* 60 60))
  15044. (fy "%dy %dd %02d:%02d")
  15045. (fy1 "%dy %dd")
  15046. (fd "%dd %02d:%02d")
  15047. (fd1 "%dd")
  15048. (fh "%02d:%02d")
  15049. y d h m align)
  15050. (if havetime
  15051. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15052. y 0
  15053. d (floor (/ diff ds)) diff (mod diff ds)
  15054. h (floor (/ diff hs)) diff (mod diff hs)
  15055. m (floor (/ diff 60)))
  15056. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15057. y 0
  15058. d (floor (+ (/ diff ds) 0.5))
  15059. h 0 m 0))
  15060. (if (not to-buffer)
  15061. (message "%s" (org-make-tdiff-string y d h m))
  15062. (if (org-at-table-p)
  15063. (progn
  15064. (goto-char match-end)
  15065. (setq align t)
  15066. (and (looking-at " *|") (goto-char (match-end 0))))
  15067. (goto-char match-end))
  15068. (if (looking-at
  15069. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15070. (replace-match ""))
  15071. (if negative (insert " -"))
  15072. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15073. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15074. (insert " " (format fh h m))))
  15075. (if align (org-table-align))
  15076. (message "Time difference inserted")))))
  15077. (defun org-make-tdiff-string (y d h m)
  15078. (let ((fmt "")
  15079. (l nil))
  15080. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15081. l (push y l)))
  15082. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15083. l (push d l)))
  15084. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15085. l (push h l)))
  15086. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15087. l (push m l)))
  15088. (apply 'format fmt (nreverse l))))
  15089. (defun org-time-string-to-time (s &optional buffer pos)
  15090. "Convert a timestamp string into internal time."
  15091. (condition-case errdata
  15092. (apply 'encode-time (org-parse-time-string s))
  15093. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15094. s (if (not (and buffer pos))
  15095. ""
  15096. (format " at %d in buffer `%s'" pos buffer))
  15097. (cdr errdata)))))
  15098. (defun org-time-string-to-seconds (s)
  15099. "Convert a timestamp string to a number of seconds."
  15100. (org-float-time (org-time-string-to-time s)))
  15101. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15102. "Convert a time stamp to an absolute day number.
  15103. If there is a specifier for a cyclic time stamp, get the closest
  15104. date to DAYNR.
  15105. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15106. The variable date is bound by the calendar when this is called."
  15107. (cond
  15108. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15109. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15110. daynr
  15111. (+ daynr 1000)))
  15112. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15113. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15114. (time-to-days (current-time))) (match-string 0 s)
  15115. prefer show-all))
  15116. (t (time-to-days
  15117. (condition-case errdata
  15118. (apply 'encode-time (org-parse-time-string s))
  15119. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15120. s (if (not (and buffer pos))
  15121. ""
  15122. (format " at %d in buffer `%s'" pos buffer))
  15123. (cdr errdata))))))))
  15124. (defun org-days-to-iso-week (days)
  15125. "Return the iso week number."
  15126. (require 'cal-iso)
  15127. (car (calendar-iso-from-absolute days)))
  15128. (defun org-small-year-to-year (year)
  15129. "Convert 2-digit years into 4-digit years.
  15130. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15131. The year 2000 cannot be abbreviated. Any year larger than 99
  15132. is returned unchanged."
  15133. (if (< year 38)
  15134. (setq year (+ 2000 year))
  15135. (if (< year 100)
  15136. (setq year (+ 1900 year))))
  15137. year)
  15138. (defun org-time-from-absolute (d)
  15139. "Return the time corresponding to date D.
  15140. D may be an absolute day number, or a calendar-type list (month day year)."
  15141. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15142. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15143. (defun org-calendar-holiday ()
  15144. "List of holidays, for Diary display in Org-mode."
  15145. (require 'holidays)
  15146. (let ((hl (funcall
  15147. (if (fboundp 'calendar-check-holidays)
  15148. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15149. (if hl (mapconcat 'identity hl "; "))))
  15150. (defun org-diary-sexp-entry (sexp entry date)
  15151. "Process a SEXP diary ENTRY for DATE."
  15152. (require 'diary-lib)
  15153. (let ((result (if calendar-debug-sexp
  15154. (let ((stack-trace-on-error t))
  15155. (eval (car (read-from-string sexp))))
  15156. (condition-case nil
  15157. (eval (car (read-from-string sexp)))
  15158. (error
  15159. (beep)
  15160. (message "Bad sexp at line %d in %s: %s"
  15161. (org-current-line)
  15162. (buffer-file-name) sexp)
  15163. (sleep-for 2))))))
  15164. (cond ((stringp result) (split-string result "; "))
  15165. ((and (consp result)
  15166. (not (consp (cdr result)))
  15167. (stringp (cdr result))) (cdr result))
  15168. ((and (consp result)
  15169. (stringp (car result))) result)
  15170. (result entry))))
  15171. (defun org-diary-to-ical-string (frombuf)
  15172. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15173. This uses the icalendar.el library."
  15174. (let* ((tmpdir (if (featurep 'xemacs)
  15175. (temp-directory)
  15176. temporary-file-directory))
  15177. (tmpfile (make-temp-name
  15178. (expand-file-name "orgics" tmpdir)))
  15179. buf rtn b e)
  15180. (with-current-buffer frombuf
  15181. (icalendar-export-region (point-min) (point-max) tmpfile)
  15182. (setq buf (find-buffer-visiting tmpfile))
  15183. (set-buffer buf)
  15184. (goto-char (point-min))
  15185. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15186. (setq b (match-beginning 0)))
  15187. (goto-char (point-max))
  15188. (if (re-search-backward "^END:VEVENT" nil t)
  15189. (setq e (match-end 0)))
  15190. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15191. (kill-buffer buf)
  15192. (delete-file tmpfile)
  15193. rtn))
  15194. (defun org-closest-date (start current change prefer show-all)
  15195. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15196. When PREFER is `past', return a date that is either CURRENT or past.
  15197. When PREFER is `future', return a date that is either CURRENT or future.
  15198. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15199. ;; Make the proper lists from the dates
  15200. (catch 'exit
  15201. (let ((a1 '(("h" . hour)
  15202. ("d" . day)
  15203. ("w" . week)
  15204. ("m" . month)
  15205. ("y" . year)))
  15206. (shour (nth 2 (org-parse-time-string start)))
  15207. dn dw sday cday n1 n2 n0
  15208. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15209. (setq start (org-date-to-gregorian start)
  15210. current (org-date-to-gregorian
  15211. (if show-all
  15212. current
  15213. (time-to-days (current-time))))
  15214. sday (calendar-absolute-from-gregorian start)
  15215. cday (calendar-absolute-from-gregorian current))
  15216. (if (<= cday sday) (throw 'exit sday))
  15217. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15218. (setq dn (string-to-number (match-string 1 change))
  15219. dw (cdr (assoc (match-string 2 change) a1)))
  15220. (error "Invalid change specifier: %s" change))
  15221. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15222. (cond
  15223. ((eq dw 'hour)
  15224. (let ((missing-hours
  15225. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15226. dn)))
  15227. (setq n1 (if (zerop missing-hours) cday
  15228. (- cday (1+ (floor (/ missing-hours 24)))))
  15229. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15230. ((eq dw 'day)
  15231. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15232. n2 (+ n1 dn)))
  15233. ((eq dw 'year)
  15234. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15235. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15236. (setq date1 (list m d y1)
  15237. n1 (calendar-absolute-from-gregorian date1)
  15238. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15239. n2 (calendar-absolute-from-gregorian date2)))
  15240. ((eq dw 'month)
  15241. ;; approx number of month between the two dates
  15242. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15243. ;; How often does dn fit in there?
  15244. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15245. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15246. m (+ m nm)
  15247. ny (floor (/ m 12))
  15248. y (+ y ny)
  15249. m (- m (* ny 12)))
  15250. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15251. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15252. (setq m2 (+ m dn) y2 y)
  15253. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15254. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15255. (while (<= n2 cday)
  15256. (setq n1 n2 m m2 y y2)
  15257. (setq m2 (+ m dn) y2 y)
  15258. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15259. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15260. ;; Make sure n1 is the earlier date
  15261. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15262. (if show-all
  15263. (cond
  15264. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15265. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15266. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15267. (cond
  15268. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15269. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15270. (t (if (= cday n1) n1 n2)))))))
  15271. (defun org-date-to-gregorian (date)
  15272. "Turn any specification of DATE into a Gregorian date for the calendar."
  15273. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15274. ((and (listp date) (= (length date) 3)) date)
  15275. ((stringp date)
  15276. (setq date (org-parse-time-string date))
  15277. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15278. ((listp date)
  15279. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15280. (defun org-parse-time-string (s &optional nodefault)
  15281. "Parse the standard Org-mode time string.
  15282. This should be a lot faster than the normal `parse-time-string'.
  15283. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15284. hour and minute fields will be nil if not given."
  15285. (cond ((string-match org-ts-regexp0 s)
  15286. (list 0
  15287. (if (or (match-beginning 8) (not nodefault))
  15288. (string-to-number (or (match-string 8 s) "0")))
  15289. (if (or (match-beginning 7) (not nodefault))
  15290. (string-to-number (or (match-string 7 s) "0")))
  15291. (string-to-number (match-string 4 s))
  15292. (string-to-number (match-string 3 s))
  15293. (string-to-number (match-string 2 s))
  15294. nil nil nil))
  15295. ((string-match "^<[^>]+>$" s)
  15296. (decode-time (seconds-to-time (org-matcher-time s))))
  15297. (t (error "Not a standard Org-mode time string: %s" s))))
  15298. (defun org-timestamp-up (&optional arg)
  15299. "Increase the date item at the cursor by one.
  15300. If the cursor is on the year, change the year. If it is on the month,
  15301. the day or the time, change that.
  15302. With prefix ARG, change by that many units."
  15303. (interactive "p")
  15304. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15305. (defun org-timestamp-down (&optional arg)
  15306. "Decrease the date item at the cursor by one.
  15307. If the cursor is on the year, change the year. If it is on the month,
  15308. the day or the time, change that.
  15309. With prefix ARG, change by that many units."
  15310. (interactive "p")
  15311. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15312. (defun org-timestamp-up-day (&optional arg)
  15313. "Increase the date in the time stamp by one day.
  15314. With prefix ARG, change that many days."
  15315. (interactive "p")
  15316. (if (and (not (org-at-timestamp-p t))
  15317. (org-at-heading-p))
  15318. (org-todo 'up)
  15319. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15320. (defun org-timestamp-down-day (&optional arg)
  15321. "Decrease the date in the time stamp by one day.
  15322. With prefix ARG, change that many days."
  15323. (interactive "p")
  15324. (if (and (not (org-at-timestamp-p t))
  15325. (org-at-heading-p))
  15326. (org-todo 'down)
  15327. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15328. (defun org-at-timestamp-p (&optional inactive-ok)
  15329. "Determine if the cursor is in or at a timestamp."
  15330. (interactive)
  15331. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15332. (pos (point))
  15333. (ans (or (looking-at tsr)
  15334. (save-excursion
  15335. (skip-chars-backward "^[<\n\r\t")
  15336. (if (> (point) (point-min)) (backward-char 1))
  15337. (and (looking-at tsr)
  15338. (> (- (match-end 0) pos) -1))))))
  15339. (and ans
  15340. (boundp 'org-ts-what)
  15341. (setq org-ts-what
  15342. (cond
  15343. ((= pos (match-beginning 0)) 'bracket)
  15344. ;; Point is considered to be "on the bracket" whether
  15345. ;; it's really on it or right after it.
  15346. ((= pos (1- (match-end 0))) 'bracket)
  15347. ((= pos (match-end 0)) 'after)
  15348. ((org-pos-in-match-range pos 2) 'year)
  15349. ((org-pos-in-match-range pos 3) 'month)
  15350. ((org-pos-in-match-range pos 7) 'hour)
  15351. ((org-pos-in-match-range pos 8) 'minute)
  15352. ((or (org-pos-in-match-range pos 4)
  15353. (org-pos-in-match-range pos 5)) 'day)
  15354. ((and (> pos (or (match-end 8) (match-end 5)))
  15355. (< pos (match-end 0)))
  15356. (- pos (or (match-end 8) (match-end 5))))
  15357. (t 'day))))
  15358. ans))
  15359. (defun org-toggle-timestamp-type ()
  15360. "Toggle the type (<active> or [inactive]) of a time stamp."
  15361. (interactive)
  15362. (when (org-at-timestamp-p t)
  15363. (let ((beg (match-beginning 0)) (end (match-end 0))
  15364. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15365. (save-excursion
  15366. (goto-char beg)
  15367. (while (re-search-forward "[][<>]" end t)
  15368. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15369. t t)))
  15370. (message "Timestamp is now %sactive"
  15371. (if (equal (char-after beg) ?<) "" "in")))))
  15372. (defvar org-clock-history) ; defined in org-clock.el
  15373. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15374. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15375. "Change the date in the time stamp at point.
  15376. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15377. `year', `minute', `second'. If WHAT is not given, the cursor position
  15378. in the timestamp determines what will be changed.
  15379. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15380. (let ((origin (point)) origin-cat
  15381. with-hm inactive
  15382. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15383. org-ts-what
  15384. extra rem
  15385. ts time time0 fixnext clrgx)
  15386. (if (not (org-at-timestamp-p t))
  15387. (error "Not at a timestamp"))
  15388. (if (and (not what) (eq org-ts-what 'bracket))
  15389. (org-toggle-timestamp-type)
  15390. ;; Point isn't on brackets. Remember the part of the time-stamp
  15391. ;; the point was in. Indeed, size of time-stamps may change,
  15392. ;; but point must be kept in the same category nonetheless.
  15393. (setq origin-cat org-ts-what)
  15394. (if (and (not what) (not (eq org-ts-what 'day))
  15395. org-display-custom-times
  15396. (get-text-property (point) 'display)
  15397. (not (get-text-property (1- (point)) 'display)))
  15398. (setq org-ts-what 'day))
  15399. (setq org-ts-what (or what org-ts-what)
  15400. inactive (= (char-after (match-beginning 0)) ?\[)
  15401. ts (match-string 0))
  15402. (replace-match "")
  15403. (when (string-match
  15404. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15405. ts)
  15406. (setq extra (match-string 1 ts))
  15407. (if suppress-tmp-delay
  15408. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15409. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15410. (setq with-hm t))
  15411. (setq time0 (org-parse-time-string ts))
  15412. (when (and updown
  15413. (eq org-ts-what 'minute)
  15414. (not current-prefix-arg))
  15415. ;; This looks like s-up and s-down. Change by one rounding step.
  15416. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15417. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15418. (setcar (cdr time0) (+ (nth 1 time0)
  15419. (if (> n 0) (- rem) (- dm rem))))))
  15420. (setq time
  15421. (encode-time (or (car time0) 0)
  15422. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15423. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15424. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15425. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15426. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15427. (nthcdr 6 time0)))
  15428. (when (and (member org-ts-what '(hour minute))
  15429. extra
  15430. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15431. (setq extra (org-modify-ts-extra
  15432. extra
  15433. (if (eq org-ts-what 'hour) 2 5)
  15434. n dm)))
  15435. (when (integerp org-ts-what)
  15436. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15437. (if (eq what 'calendar)
  15438. (let ((cal-date (org-get-date-from-calendar)))
  15439. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15440. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15441. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15442. (setcar time0 (or (car time0) 0))
  15443. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15444. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15445. (setq time (apply 'encode-time time0))))
  15446. ;; Insert the new time-stamp, and ensure point stays in the same
  15447. ;; category as before (i.e. not after the last position in that
  15448. ;; category).
  15449. (let ((pos (point)))
  15450. ;; Stay before inserted string. `save-excursion' is of no use.
  15451. (setq org-last-changed-timestamp
  15452. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15453. (goto-char pos))
  15454. (save-match-data
  15455. (looking-at org-ts-regexp3)
  15456. (goto-char (cond
  15457. ;; `day' category ends before `hour' if any, or at
  15458. ;; the end of the day name.
  15459. ((eq origin-cat 'day)
  15460. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15461. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15462. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15463. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15464. ;; `year' and `month' have both fixed size: point
  15465. ;; couldn't have moved into another part.
  15466. (t origin))))
  15467. ;; Update clock if on a CLOCK line.
  15468. (org-clock-update-time-maybe)
  15469. ;; Maybe adjust the closest clock in `org-clock-history'
  15470. (when org-clock-adjust-closest
  15471. (if (not (and (org-at-clock-log-p)
  15472. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15473. org-clock-history))))))
  15474. (message "No clock to adjust")
  15475. (cond ((save-excursion ; fix previous clock?
  15476. (re-search-backward org-ts-regexp0 nil t)
  15477. (org-looking-back (concat org-clock-string " \\[")))
  15478. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15479. ((save-excursion ; fix next clock?
  15480. (re-search-backward org-ts-regexp0 nil t)
  15481. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15482. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15483. (save-window-excursion
  15484. ;; Find closest clock to point, adjust the previous/next one in history
  15485. (let* ((p (save-excursion (org-back-to-heading t)))
  15486. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15487. (clfixnth
  15488. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15489. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15490. (if (not clfixpos)
  15491. (message "No clock to adjust")
  15492. (save-excursion
  15493. (org-goto-marker-or-bmk clfixpos)
  15494. (org-show-subtree)
  15495. (when (re-search-forward clrgx nil t)
  15496. (goto-char (match-beginning 1))
  15497. (let (org-clock-adjust-closest)
  15498. (org-timestamp-change n org-ts-what updown))
  15499. (message "Clock adjusted in %s for heading: %s"
  15500. (file-name-nondirectory (buffer-file-name))
  15501. (org-get-heading t t)))))))))
  15502. ;; Try to recenter the calendar window, if any.
  15503. (if (and org-calendar-follow-timestamp-change
  15504. (get-buffer-window "*Calendar*" t)
  15505. (memq org-ts-what '(day month year)))
  15506. (org-recenter-calendar (time-to-days time))))))
  15507. (defun org-modify-ts-extra (s pos n dm)
  15508. "Change the different parts of the lead-time and repeat fields in timestamp."
  15509. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15510. ng h m new rem)
  15511. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15512. (cond
  15513. ((or (org-pos-in-match-range pos 2)
  15514. (org-pos-in-match-range pos 3))
  15515. (setq m (string-to-number (match-string 3 s))
  15516. h (string-to-number (match-string 2 s)))
  15517. (if (org-pos-in-match-range pos 2)
  15518. (setq h (+ h n))
  15519. (setq n (* dm (org-no-warnings (signum n))))
  15520. (when (not (= 0 (setq rem (% m dm))))
  15521. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15522. (setq m (+ m n)))
  15523. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15524. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15525. (setq h (min 24 (max 0 h)))
  15526. (setq ng 1 new (format "-%02d:%02d" h m)))
  15527. ((org-pos-in-match-range pos 6)
  15528. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15529. ((org-pos-in-match-range pos 5)
  15530. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15531. ((org-pos-in-match-range pos 9)
  15532. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15533. ((org-pos-in-match-range pos 8)
  15534. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15535. (when ng
  15536. (setq s (concat
  15537. (substring s 0 (match-beginning ng))
  15538. new
  15539. (substring s (match-end ng))))))
  15540. s))
  15541. (defun org-recenter-calendar (date)
  15542. "If the calendar is visible, recenter it to DATE."
  15543. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15544. (when cwin
  15545. (let ((calendar-move-hook nil))
  15546. (with-selected-window cwin
  15547. (calendar-goto-date (if (listp date) date
  15548. (calendar-gregorian-from-absolute date))))))))
  15549. (defun org-goto-calendar (&optional arg)
  15550. "Go to the Emacs calendar at the current date.
  15551. If there is a time stamp in the current line, go to that date.
  15552. A prefix ARG can be used to force the current date."
  15553. (interactive "P")
  15554. (let ((tsr org-ts-regexp) diff
  15555. (calendar-move-hook nil)
  15556. (calendar-view-holidays-initially-flag nil)
  15557. (calendar-view-diary-initially-flag nil))
  15558. (if (or (org-at-timestamp-p)
  15559. (save-excursion
  15560. (beginning-of-line 1)
  15561. (looking-at (concat ".*" tsr))))
  15562. (let ((d1 (time-to-days (current-time)))
  15563. (d2 (time-to-days
  15564. (org-time-string-to-time (match-string 1)))))
  15565. (setq diff (- d2 d1))))
  15566. (calendar)
  15567. (calendar-goto-today)
  15568. (if (and diff (not arg)) (calendar-forward-day diff))))
  15569. (defun org-get-date-from-calendar ()
  15570. "Return a list (month day year) of date at point in calendar."
  15571. (with-current-buffer "*Calendar*"
  15572. (save-match-data
  15573. (calendar-cursor-to-date))))
  15574. (defun org-date-from-calendar ()
  15575. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15576. If there is already a time stamp at the cursor position, update it."
  15577. (interactive)
  15578. (if (org-at-timestamp-p t)
  15579. (org-timestamp-change 0 'calendar)
  15580. (let ((cal-date (org-get-date-from-calendar)))
  15581. (org-insert-time-stamp
  15582. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15583. (defcustom org-effort-durations
  15584. `(("h" . 60)
  15585. ("d" . ,(* 60 8))
  15586. ("w" . ,(* 60 8 5))
  15587. ("m" . ,(* 60 8 5 4))
  15588. ("y" . ,(* 60 8 5 40)))
  15589. "Conversion factor to minutes for an effort modifier.
  15590. Each entry has the form (MODIFIER . MINUTES).
  15591. In an effort string, a number followed by MODIFIER is multiplied
  15592. by the specified number of MINUTES to obtain an effort in
  15593. minutes.
  15594. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15595. effort string \"2hours\" is equivalent to 120 minutes."
  15596. :group 'org-agenda
  15597. :version "24.1"
  15598. :type '(alist :key-type (string :tag "Modifier")
  15599. :value-type (number :tag "Minutes")))
  15600. (defun org-minutes-to-clocksum-string (m)
  15601. "Format number of minutes as a clocksum string.
  15602. The format is determined by `org-time-clocksum-format',
  15603. `org-time-clocksum-use-fractional' and
  15604. `org-time-clocksum-fractional-format' and
  15605. `org-time-clocksum-use-effort-durations'."
  15606. (let ((clocksum "") h d w mo y fmt n)
  15607. (setq h (if org-time-clocksum-use-effort-durations
  15608. (cdr (assoc "h" org-effort-durations)) 60)
  15609. d (if org-time-clocksum-use-effort-durations
  15610. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15611. w (if org-time-clocksum-use-effort-durations
  15612. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15613. mo (if org-time-clocksum-use-effort-durations
  15614. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15615. y (if org-time-clocksum-use-effort-durations
  15616. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15617. ;; fractional format
  15618. (if org-time-clocksum-use-fractional
  15619. (cond
  15620. ;; single format string
  15621. ((stringp org-time-clocksum-fractional-format)
  15622. (format org-time-clocksum-fractional-format (/ m (float h))))
  15623. ;; choice of fractional formats for different time units
  15624. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15625. (> (/ (truncate m) (* y d h)) 0))
  15626. (format fmt (/ m (* y d (float h)))))
  15627. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15628. (> (/ (truncate m) (* mo d h)) 0))
  15629. (format fmt (/ m (* mo d (float h)))))
  15630. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15631. (> (/ (truncate m) (* w d h)) 0))
  15632. (format fmt (/ m (* w d (float h)))))
  15633. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15634. (> (/ (truncate m) (* d h)) 0))
  15635. (format fmt (/ m (* d (float h)))))
  15636. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15637. (> (/ (truncate m) h) 0))
  15638. (format fmt (/ m (float h))))
  15639. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15640. (format fmt m))
  15641. ;; fall back to smallest time unit with a format
  15642. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15643. (format fmt (/ m (float h))))
  15644. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15645. (format fmt (/ m (* d (float h)))))
  15646. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15647. (format fmt (/ m (* w d (float h)))))
  15648. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15649. (format fmt (/ m (* mo d (float h)))))
  15650. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15651. (format fmt (/ m (* y d (float h))))))
  15652. ;; standard (non-fractional) format, with single format string
  15653. (if (stringp org-time-clocksum-format)
  15654. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15655. ;; separate formats components
  15656. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15657. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15658. (plist-get org-time-clocksum-format :require-years))
  15659. (setq clocksum (concat clocksum (format fmt n))
  15660. m (- m (* n y d h))))
  15661. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15662. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15663. (plist-get org-time-clocksum-format :require-months))
  15664. (setq clocksum (concat clocksum (format fmt n))
  15665. m (- m (* n mo d h))))
  15666. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15667. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15668. (plist-get org-time-clocksum-format :require-weeks))
  15669. (setq clocksum (concat clocksum (format fmt n))
  15670. m (- m (* n w d h))))
  15671. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15672. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15673. (plist-get org-time-clocksum-format :require-days))
  15674. (setq clocksum (concat clocksum (format fmt n))
  15675. m (- m (* n d h))))
  15676. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15677. (or (> (setq n (/ (truncate m) h)) 0)
  15678. (plist-get org-time-clocksum-format :require-hours))
  15679. (setq clocksum (concat clocksum (format fmt n))
  15680. m (- m (* n h))))
  15681. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15682. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15683. (setq clocksum (concat clocksum (format fmt m))))
  15684. ;; return formatted time duration
  15685. clocksum))))
  15686. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15687. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15688. "Org mode version 8.0")
  15689. (defun org-hours-to-clocksum-string (n)
  15690. (org-minutes-to-clocksum-string (* n 60)))
  15691. (defun org-hh:mm-string-to-minutes (s)
  15692. "Convert a string H:MM to a number of minutes.
  15693. If the string is just a number, interpret it as minutes.
  15694. In fact, the first hh:mm or number in the string will be taken,
  15695. there can be extra stuff in the string.
  15696. If no number is found, the return value is 0."
  15697. (cond
  15698. ((integerp s) s)
  15699. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15700. (+ (* (string-to-number (match-string 1 s)) 60)
  15701. (string-to-number (match-string 2 s))))
  15702. ((string-match "\\([0-9]+\\)" s)
  15703. (string-to-number (match-string 1 s)))
  15704. (t 0)))
  15705. (defcustom org-image-actual-width t
  15706. "Should we use the actual width of images when inlining them?
  15707. When set to `t', always use the image width.
  15708. When set to a number, use imagemagick (when available) to set
  15709. the image's width to this value.
  15710. When set to a number in a list, try to get the width from the
  15711. #+ATTR.* keyword if it matches a width specification like
  15712. width=\"[0-9]+\", and fall back on that number if none is found.
  15713. When set to nil, try to get the width from an #+ATTR.* keyword
  15714. and fall back on the original width if none is found.
  15715. This requires Emacs >= 24.1, build with imagemagick support."
  15716. :group 'org-appearance
  15717. :version "24.3"
  15718. :type '(choice
  15719. (const :tag "Use the image width" t)
  15720. (integer :tag "Use a number of pixels")
  15721. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15722. (const :tag "Use #+ATTR* or don't resize" nil)))
  15723. (defcustom org-agenda-inhibit-startup t
  15724. "Inhibit startup when preparing agenda buffers.
  15725. When this variable is `t' (the default), the initialization of
  15726. the Org agenda buffers is inhibited: e.g. the visibility state
  15727. is not set, the tables are not re-aligned, etc."
  15728. :type 'boolean
  15729. :version "24.3"
  15730. :group 'org-agenda)
  15731. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15732. "Convert a duration string S to minutes.
  15733. A bare number is interpreted as minutes, modifiers can be set by
  15734. customizing `org-effort-durations' (which see).
  15735. Entries containing a colon are interpreted as H:MM by
  15736. `org-hh:mm-string-to-minutes'."
  15737. (let ((result 0)
  15738. (re (concat "\\([0-9.]+\\) *\\("
  15739. (regexp-opt (mapcar 'car org-effort-durations))
  15740. "\\)")))
  15741. (while (string-match re s)
  15742. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15743. (string-to-number (match-string 1 s))))
  15744. (setq s (replace-match "" nil t s)))
  15745. (setq result (floor result))
  15746. (incf result (org-hh:mm-string-to-minutes s))
  15747. (if output-to-string (number-to-string result) result)))
  15748. ;;;; Files
  15749. (defun org-save-all-org-buffers ()
  15750. "Save all Org-mode buffers without user confirmation."
  15751. (interactive)
  15752. (message "Saving all Org-mode buffers...")
  15753. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15754. (when (featurep 'org-id) (org-id-locations-save))
  15755. (message "Saving all Org-mode buffers... done"))
  15756. (defun org-revert-all-org-buffers ()
  15757. "Revert all Org-mode buffers.
  15758. Prompt for confirmation when there are unsaved changes.
  15759. Be sure you know what you are doing before letting this function
  15760. overwrite your changes.
  15761. This function is useful in a setup where one tracks org files
  15762. with a version control system, to revert on one machine after pulling
  15763. changes from another. I believe the procedure must be like this:
  15764. 1. M-x org-save-all-org-buffers
  15765. 2. Pull changes from the other machine, resolve conflicts
  15766. 3. M-x org-revert-all-org-buffers"
  15767. (interactive)
  15768. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15769. (error "Abort"))
  15770. (save-excursion
  15771. (save-window-excursion
  15772. (mapc
  15773. (lambda (b)
  15774. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15775. (with-current-buffer b buffer-file-name))
  15776. (org-pop-to-buffer-same-window b)
  15777. (revert-buffer t 'no-confirm)))
  15778. (buffer-list))
  15779. (when (and (featurep 'org-id) org-id-track-globally)
  15780. (org-id-locations-load)))))
  15781. ;;;; Agenda files
  15782. ;;;###autoload
  15783. (defun org-switchb (&optional arg)
  15784. "Switch between Org buffers.
  15785. With one prefix argument, restrict available buffers to files.
  15786. With two prefix arguments, restrict available buffers to agenda files.
  15787. Defaults to `iswitchb' for buffer name completion.
  15788. Set `org-completion-use-ido' to make it use ido instead."
  15789. (interactive "P")
  15790. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15791. ((equal arg '(16)) (org-buffer-list 'agenda))
  15792. (t (org-buffer-list))))
  15793. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15794. (org-completion-use-ido org-completion-use-ido))
  15795. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15796. (setq org-completion-use-iswitchb t))
  15797. (org-pop-to-buffer-same-window
  15798. (org-icompleting-read "Org buffer: "
  15799. (mapcar 'list (mapcar 'buffer-name blist))
  15800. nil t))))
  15801. ;;; Define some older names previously used for this functionality
  15802. ;;;###autoload
  15803. (defalias 'org-ido-switchb 'org-switchb)
  15804. ;;;###autoload
  15805. (defalias 'org-iswitchb 'org-switchb)
  15806. (defun org-buffer-list (&optional predicate exclude-tmp)
  15807. "Return a list of Org buffers.
  15808. PREDICATE can be `export', `files' or `agenda'.
  15809. export restrict the list to Export buffers.
  15810. files restrict the list to buffers visiting Org files.
  15811. agenda restrict the list to buffers visiting agenda files.
  15812. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15813. (let* ((bfn nil)
  15814. (agenda-files (and (eq predicate 'agenda)
  15815. (mapcar 'file-truename (org-agenda-files t))))
  15816. (filter
  15817. (cond
  15818. ((eq predicate 'files)
  15819. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15820. ((eq predicate 'export)
  15821. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15822. ((eq predicate 'agenda)
  15823. (lambda (b)
  15824. (with-current-buffer b
  15825. (and (derived-mode-p 'org-mode)
  15826. (setq bfn (buffer-file-name b))
  15827. (member (file-truename bfn) agenda-files)))))
  15828. (t (lambda (b) (with-current-buffer b
  15829. (or (derived-mode-p 'org-mode)
  15830. (string-match "\*Org .*Export"
  15831. (buffer-name b)))))))))
  15832. (delq nil
  15833. (mapcar
  15834. (lambda(b)
  15835. (if (and (funcall filter b)
  15836. (or (not exclude-tmp)
  15837. (not (string-match "tmp" (buffer-name b)))))
  15838. b
  15839. nil))
  15840. (buffer-list)))))
  15841. (defun org-agenda-files (&optional unrestricted archives)
  15842. "Get the list of agenda files.
  15843. Optional UNRESTRICTED means return the full list even if a restriction
  15844. is currently in place.
  15845. When ARCHIVES is t, include all archive files that are really being
  15846. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15847. `org-agenda-archives-mode' is t."
  15848. (let ((files
  15849. (cond
  15850. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15851. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15852. ((listp org-agenda-files) org-agenda-files)
  15853. (t (error "Invalid value of `org-agenda-files'")))))
  15854. (setq files (apply 'append
  15855. (mapcar (lambda (f)
  15856. (if (file-directory-p f)
  15857. (directory-files
  15858. f t org-agenda-file-regexp)
  15859. (list f)))
  15860. files)))
  15861. (when org-agenda-skip-unavailable-files
  15862. (setq files (delq nil
  15863. (mapcar (function
  15864. (lambda (file)
  15865. (and (file-readable-p file) file)))
  15866. files))))
  15867. (when (or (eq archives t)
  15868. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15869. (setq files (org-add-archive-files files)))
  15870. files))
  15871. (defun org-agenda-file-p (&optional file)
  15872. "Return non-nil, if FILE is an agenda file.
  15873. If FILE is omitted, use the file associated with the current
  15874. buffer."
  15875. (member (or file (buffer-file-name))
  15876. (org-agenda-files t)))
  15877. (defun org-edit-agenda-file-list ()
  15878. "Edit the list of agenda files.
  15879. Depending on setup, this either uses customize to edit the variable
  15880. `org-agenda-files', or it visits the file that is holding the list. In the
  15881. latter case, the buffer is set up in a way that saving it automatically kills
  15882. the buffer and restores the previous window configuration."
  15883. (interactive)
  15884. (if (stringp org-agenda-files)
  15885. (let ((cw (current-window-configuration)))
  15886. (find-file org-agenda-files)
  15887. (org-set-local 'org-window-configuration cw)
  15888. (org-add-hook 'after-save-hook
  15889. (lambda ()
  15890. (set-window-configuration
  15891. (prog1 org-window-configuration
  15892. (kill-buffer (current-buffer))))
  15893. (org-install-agenda-files-menu)
  15894. (message "New agenda file list installed"))
  15895. nil 'local)
  15896. (message "%s" (substitute-command-keys
  15897. "Edit list and finish with \\[save-buffer]")))
  15898. (customize-variable 'org-agenda-files)))
  15899. (defun org-store-new-agenda-file-list (list)
  15900. "Set new value for the agenda file list and save it correctly."
  15901. (if (stringp org-agenda-files)
  15902. (let ((fe (org-read-agenda-file-list t)) b u)
  15903. (while (setq b (find-buffer-visiting org-agenda-files))
  15904. (kill-buffer b))
  15905. (with-temp-file org-agenda-files
  15906. (insert
  15907. (mapconcat
  15908. (lambda (f) ;; Keep un-expanded entries.
  15909. (if (setq u (assoc f fe))
  15910. (cdr u)
  15911. f))
  15912. list "\n")
  15913. "\n")))
  15914. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15915. (org-insert-mode-line-in-empty-file nil))
  15916. (setq org-agenda-files list)
  15917. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15918. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15919. "Read the list of agenda files from a file.
  15920. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15921. filenames, used by `org-store-new-agenda-file-list' to write back
  15922. un-expanded file names."
  15923. (when (file-directory-p org-agenda-files)
  15924. (error "`org-agenda-files' cannot be a single directory"))
  15925. (when (stringp org-agenda-files)
  15926. (with-temp-buffer
  15927. (insert-file-contents org-agenda-files)
  15928. (mapcar
  15929. (lambda (f)
  15930. (let ((e (expand-file-name (substitute-in-file-name f)
  15931. org-directory)))
  15932. (if pair-with-expansion
  15933. (cons e f)
  15934. e)))
  15935. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15936. ;;;###autoload
  15937. (defun org-cycle-agenda-files ()
  15938. "Cycle through the files in `org-agenda-files'.
  15939. If the current buffer visits an agenda file, find the next one in the list.
  15940. If the current buffer does not, find the first agenda file."
  15941. (interactive)
  15942. (let* ((fs (org-agenda-files t))
  15943. (files (append fs (list (car fs))))
  15944. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15945. file)
  15946. (unless files (error "No agenda files"))
  15947. (catch 'exit
  15948. (while (setq file (pop files))
  15949. (if (equal (file-truename file) tcf)
  15950. (when (car files)
  15951. (find-file (car files))
  15952. (throw 'exit t))))
  15953. (find-file (car fs)))
  15954. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15955. (defun org-agenda-file-to-front (&optional to-end)
  15956. "Move/add the current file to the top of the agenda file list.
  15957. If the file is not present in the list, it is added to the front. If it is
  15958. present, it is moved there. With optional argument TO-END, add/move to the
  15959. end of the list."
  15960. (interactive "P")
  15961. (let ((org-agenda-skip-unavailable-files nil)
  15962. (file-alist (mapcar (lambda (x)
  15963. (cons (file-truename x) x))
  15964. (org-agenda-files t)))
  15965. (ctf (file-truename
  15966. (or buffer-file-name
  15967. (error "Please save the current buffer to a file"))))
  15968. x had)
  15969. (setq x (assoc ctf file-alist) had x)
  15970. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15971. (if to-end
  15972. (setq file-alist (append (delq x file-alist) (list x)))
  15973. (setq file-alist (cons x (delq x file-alist))))
  15974. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15975. (org-install-agenda-files-menu)
  15976. (message "File %s to %s of agenda file list"
  15977. (if had "moved" "added") (if to-end "end" "front"))))
  15978. (defun org-remove-file (&optional file)
  15979. "Remove current file from the list of files in variable `org-agenda-files'.
  15980. These are the files which are being checked for agenda entries.
  15981. Optional argument FILE means use this file instead of the current."
  15982. (interactive)
  15983. (let* ((org-agenda-skip-unavailable-files nil)
  15984. (file (or file buffer-file-name
  15985. (error "Current buffer does not visit a file")))
  15986. (true-file (file-truename file))
  15987. (afile (abbreviate-file-name file))
  15988. (files (delq nil (mapcar
  15989. (lambda (x)
  15990. (if (equal true-file
  15991. (file-truename x))
  15992. nil x))
  15993. (org-agenda-files t)))))
  15994. (if (not (= (length files) (length (org-agenda-files t))))
  15995. (progn
  15996. (org-store-new-agenda-file-list files)
  15997. (org-install-agenda-files-menu)
  15998. (message "Removed file: %s" afile))
  15999. (message "File was not in list: %s (not removed)" afile))))
  16000. (defun org-file-menu-entry (file)
  16001. (vector file (list 'find-file file) t))
  16002. (defun org-check-agenda-file (file)
  16003. "Make sure FILE exists. If not, ask user what to do."
  16004. (when (not (file-exists-p file))
  16005. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16006. (abbreviate-file-name file))
  16007. (let ((r (downcase (read-char-exclusive))))
  16008. (cond
  16009. ((equal r ?r)
  16010. (org-remove-file file)
  16011. (throw 'nextfile t))
  16012. (t (error "Abort"))))))
  16013. (defun org-get-agenda-file-buffer (file)
  16014. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16015. it to the list of buffers which might be released later."
  16016. (let ((buf (org-find-base-buffer-visiting file)))
  16017. (if buf
  16018. buf ; just return it
  16019. ;; Make a new buffer and remember it
  16020. (setq buf (find-file-noselect file))
  16021. (if buf (push buf org-agenda-new-buffers))
  16022. buf)))
  16023. (defun org-release-buffers (blist)
  16024. "Release all buffers in list, asking the user for confirmation when needed.
  16025. When a buffer is unmodified, it is just killed. When modified, it is saved
  16026. \(if the user agrees) and then killed."
  16027. (let (buf file)
  16028. (while (setq buf (pop blist))
  16029. (setq file (buffer-file-name buf))
  16030. (when (and (buffer-modified-p buf)
  16031. file
  16032. (y-or-n-p (format "Save file %s? " file)))
  16033. (with-current-buffer buf (save-buffer)))
  16034. (kill-buffer buf))))
  16035. (defun org-agenda-prepare-buffers (files)
  16036. "Create buffers for all agenda files, protect archived trees and comments."
  16037. (interactive)
  16038. (let ((pa '(:org-archived t))
  16039. (pc '(:org-comment t))
  16040. (pall '(:org-archived t :org-comment t))
  16041. (inhibit-read-only t)
  16042. (org-inhibit-startup org-agenda-inhibit-startup)
  16043. (rea (concat ":" org-archive-tag ":"))
  16044. file re)
  16045. (save-excursion
  16046. (save-restriction
  16047. (while (setq file (pop files))
  16048. (catch 'nextfile
  16049. (if (bufferp file)
  16050. (set-buffer file)
  16051. (org-check-agenda-file file)
  16052. (set-buffer (org-get-agenda-file-buffer file)))
  16053. (widen)
  16054. (org-unmodified
  16055. (org-refresh-category-properties)
  16056. (org-refresh-properties org-effort-property 'org-effort)
  16057. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16058. (setq org-todo-keywords-for-agenda
  16059. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16060. (setq org-done-keywords-for-agenda
  16061. (append org-done-keywords-for-agenda org-done-keywords))
  16062. (setq org-todo-keyword-alist-for-agenda
  16063. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16064. (setq org-drawers-for-agenda
  16065. (append org-drawers-for-agenda org-drawers))
  16066. (setq org-tag-alist-for-agenda
  16067. (append org-tag-alist-for-agenda org-tag-alist))
  16068. (save-excursion
  16069. (remove-text-properties (point-min) (point-max) pall)
  16070. (when org-agenda-skip-archived-trees
  16071. (goto-char (point-min))
  16072. (while (re-search-forward rea nil t)
  16073. (if (org-at-heading-p t)
  16074. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16075. (goto-char (point-min))
  16076. (setq re (format org-heading-keyword-regexp-format
  16077. org-comment-string))
  16078. (while (re-search-forward re nil t)
  16079. (add-text-properties
  16080. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16081. (setq org-todo-keywords-for-agenda
  16082. (org-uniquify org-todo-keywords-for-agenda))
  16083. (setq org-todo-keyword-alist-for-agenda
  16084. (org-uniquify org-todo-keyword-alist-for-agenda)
  16085. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16086. ;;;; CDLaTeX minor mode
  16087. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16088. "Keymap for the minor `org-cdlatex-mode'.")
  16089. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16090. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16091. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16092. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16093. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16094. (defvar org-cdlatex-texmathp-advice-is-done nil
  16095. "Flag remembering if we have applied the advice to texmathp already.")
  16096. (define-minor-mode org-cdlatex-mode
  16097. "Toggle the minor `org-cdlatex-mode'.
  16098. This mode supports entering LaTeX environment and math in LaTeX fragments
  16099. in Org-mode.
  16100. \\{org-cdlatex-mode-map}"
  16101. nil " OCDL" nil
  16102. (when org-cdlatex-mode
  16103. (require 'cdlatex)
  16104. (run-hooks 'cdlatex-mode-hook)
  16105. (cdlatex-compute-tables))
  16106. (unless org-cdlatex-texmathp-advice-is-done
  16107. (setq org-cdlatex-texmathp-advice-is-done t)
  16108. (defadvice texmathp (around org-math-always-on activate)
  16109. "Always return t in org-mode buffers.
  16110. This is because we want to insert math symbols without dollars even outside
  16111. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16112. an embedded LaTeX fragment, let texmathp do its job.
  16113. \\[org-cdlatex-mode-map]"
  16114. (interactive)
  16115. (let (p)
  16116. (cond
  16117. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16118. ((eq this-command 'cdlatex-math-symbol)
  16119. (setq ad-return-value t
  16120. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16121. (t
  16122. (let ((p (org-inside-LaTeX-fragment-p)))
  16123. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16124. (setq ad-return-value t
  16125. texmathp-why '("Org-mode embedded math" . 0))
  16126. (if p ad-do-it)))))))))
  16127. (defun turn-on-org-cdlatex ()
  16128. "Unconditionally turn on `org-cdlatex-mode'."
  16129. (org-cdlatex-mode 1))
  16130. (defun org-try-cdlatex-tab ()
  16131. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16132. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16133. - inside a LaTeX fragment, or
  16134. - after the first word in a line, where an abbreviation expansion could
  16135. insert a LaTeX environment."
  16136. (when org-cdlatex-mode
  16137. (cond
  16138. ;; Before any word on the line: No expansion possible.
  16139. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16140. ;; Just after first word on the line: Expand it. Make sure it
  16141. ;; cannot happen on headlines, though.
  16142. ((save-excursion
  16143. (skip-chars-backward "a-zA-Z0-9*")
  16144. (skip-chars-backward " \t")
  16145. (and (bolp) (not (org-at-heading-p))))
  16146. (cdlatex-tab) t)
  16147. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16148. (defun org-cdlatex-underscore-caret (&optional arg)
  16149. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16150. Revert to the normal definition outside of these fragments."
  16151. (interactive "P")
  16152. (if (org-inside-LaTeX-fragment-p)
  16153. (call-interactively 'cdlatex-sub-superscript)
  16154. (let (org-cdlatex-mode)
  16155. (call-interactively (key-binding (vector last-input-event))))))
  16156. (defun org-cdlatex-math-modify (&optional arg)
  16157. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16158. Revert to the normal definition outside of these fragments."
  16159. (interactive "P")
  16160. (if (org-inside-LaTeX-fragment-p)
  16161. (call-interactively 'cdlatex-math-modify)
  16162. (let (org-cdlatex-mode)
  16163. (call-interactively (key-binding (vector last-input-event))))))
  16164. ;;;; LaTeX fragments
  16165. (defvar org-latex-regexps
  16166. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16167. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16168. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16169. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16170. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16171. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16172. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16173. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16174. "Regular expressions for matching embedded LaTeX.")
  16175. (defun org-inside-LaTeX-fragment-p ()
  16176. "Test if point is inside a LaTeX fragment.
  16177. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16178. sequence appearing also before point.
  16179. Even though the matchers for math are configurable, this function assumes
  16180. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16181. delimiters are skipped when they have been removed by customization.
  16182. The return value is nil, or a cons cell with the delimiter and the
  16183. position of this delimiter.
  16184. This function does a reasonably good job, but can locally be fooled by
  16185. for example currency specifications. For example it will assume being in
  16186. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16187. fragments that are properly closed, but during editing, we have to live
  16188. with the uncertainty caused by missing closing delimiters. This function
  16189. looks only before point, not after."
  16190. (catch 'exit
  16191. (let ((pos (point))
  16192. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16193. (lim (progn
  16194. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16195. (point)))
  16196. dd-on str (start 0) m re)
  16197. (goto-char pos)
  16198. (when dodollar
  16199. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16200. re (nth 1 (assoc "$" org-latex-regexps)))
  16201. (while (string-match re str start)
  16202. (cond
  16203. ((= (match-end 0) (length str))
  16204. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16205. ((= (match-end 0) (- (length str) 5))
  16206. (throw 'exit nil))
  16207. (t (setq start (match-end 0))))))
  16208. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16209. (goto-char pos)
  16210. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16211. (and (match-beginning 2) (throw 'exit nil))
  16212. ;; count $$
  16213. (while (re-search-backward "\\$\\$" lim t)
  16214. (setq dd-on (not dd-on)))
  16215. (goto-char pos)
  16216. (if dd-on (cons "$$" m))))))
  16217. (defun org-inside-latex-macro-p ()
  16218. "Is point inside a LaTeX macro or its arguments?"
  16219. (save-match-data
  16220. (org-in-regexp
  16221. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16222. (defvar org-latex-fragment-image-overlays nil
  16223. "List of overlays carrying the images of latex fragments.")
  16224. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16225. (defun org-remove-latex-fragment-image-overlays ()
  16226. "Remove all overlays with LaTeX fragment images in current buffer."
  16227. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16228. (setq org-latex-fragment-image-overlays nil))
  16229. (defun org-preview-latex-fragment (&optional subtree)
  16230. "Preview the LaTeX fragment at point, or all locally or globally.
  16231. If the cursor is in a LaTeX fragment, create the image and overlay
  16232. it over the source code. If there is no fragment at point, display
  16233. all fragments in the current text, from one headline to the next. With
  16234. prefix SUBTREE, display all fragments in the current subtree. With a
  16235. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16236. the cursor is before the first headline,
  16237. display all fragments in the buffer.
  16238. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16239. (interactive "P")
  16240. (unless buffer-file-name
  16241. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16242. (org-remove-latex-fragment-image-overlays)
  16243. (save-excursion
  16244. (save-restriction
  16245. (let (beg end at msg)
  16246. (cond
  16247. ((or (equal subtree '(16))
  16248. (not (save-excursion
  16249. (re-search-backward org-outline-regexp-bol nil t))))
  16250. (setq beg (point-min) end (point-max)
  16251. msg "Creating images for buffer...%s"))
  16252. ((equal subtree '(4))
  16253. (org-back-to-heading)
  16254. (setq beg (point) end (org-end-of-subtree t)
  16255. msg "Creating images for subtree...%s"))
  16256. (t
  16257. (if (setq at (org-inside-LaTeX-fragment-p))
  16258. (goto-char (max (point-min) (- (cdr at) 2)))
  16259. (org-back-to-heading))
  16260. (setq beg (point) end (progn (outline-next-heading) (point))
  16261. msg (if at "Creating image...%s"
  16262. "Creating images for entry...%s"))))
  16263. (message msg "")
  16264. (narrow-to-region beg end)
  16265. (goto-char beg)
  16266. (org-format-latex
  16267. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16268. (file-name-nondirectory
  16269. buffer-file-name)))
  16270. default-directory 'overlays msg at 'forbuffer
  16271. org-latex-create-formula-image-program)
  16272. (message msg "done. Use `C-c C-c' to remove images.")))))
  16273. (defun org-format-latex (prefix &optional dir overlays msg at
  16274. forbuffer processing-type)
  16275. "Replace LaTeX fragments with links to an image, and produce images.
  16276. Some of the options can be changed using the variable
  16277. `org-format-latex-options'."
  16278. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16279. (let* ((prefixnodir (file-name-nondirectory prefix))
  16280. (absprefix (expand-file-name prefix dir))
  16281. (todir (file-name-directory absprefix))
  16282. (opt org-format-latex-options)
  16283. (optnew org-format-latex-options)
  16284. (matchers (plist-get opt :matchers))
  16285. (re-list org-latex-regexps)
  16286. (cnt 0) txt hash link beg end re e checkdir
  16287. string
  16288. m n block-type block linkfile movefile ov)
  16289. ;; Check the different regular expressions
  16290. (while (setq e (pop re-list))
  16291. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16292. block (if block-type "\n\n" ""))
  16293. (when (member m matchers)
  16294. (goto-char (point-min))
  16295. (while (re-search-forward re nil t)
  16296. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16297. (or (not overlays)
  16298. (not (eq (get-char-property (match-beginning n)
  16299. 'org-overlay-type)
  16300. 'org-latex-overlay))))
  16301. (cond
  16302. ((eq processing-type 'verbatim))
  16303. ((eq processing-type 'mathjax)
  16304. ;; Prepare for MathJax processing.
  16305. (setq string (match-string n))
  16306. (when (member m '("$" "$1"))
  16307. (save-excursion
  16308. (delete-region (match-beginning n) (match-end n))
  16309. (goto-char (match-beginning n))
  16310. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16311. ((or (eq processing-type 'dvipng)
  16312. (eq processing-type 'imagemagick))
  16313. ;; Process to an image.
  16314. (setq txt (match-string n)
  16315. beg (match-beginning n) end (match-end n)
  16316. cnt (1+ cnt))
  16317. (let ((face (face-at-point))
  16318. (fg (plist-get opt :foreground))
  16319. (bg (plist-get opt :background))
  16320. ;; Ensure full list is printed.
  16321. print-length print-level)
  16322. (when forbuffer
  16323. ;; Get the colors from the face at point.
  16324. (goto-char beg)
  16325. (when (eq fg 'auto)
  16326. (setq fg (face-attribute face :foreground nil 'default)))
  16327. (when (eq bg 'auto)
  16328. (setq bg (face-attribute face :background nil 'default)))
  16329. (setq optnew (copy-sequence opt))
  16330. (plist-put optnew :foreground fg)
  16331. (plist-put optnew :background bg))
  16332. (setq hash (sha1 (prin1-to-string
  16333. (list org-format-latex-header
  16334. org-latex-default-packages-alist
  16335. org-latex-packages-alist
  16336. org-format-latex-options
  16337. forbuffer txt fg bg)))
  16338. linkfile (format "%s_%s.png" prefix hash)
  16339. movefile (format "%s_%s.png" absprefix hash)))
  16340. (setq link (concat block "[[file:" linkfile "]]" block))
  16341. (if msg (message msg cnt))
  16342. (goto-char beg)
  16343. (unless checkdir ; Ensure the directory exists.
  16344. (setq checkdir t)
  16345. (or (file-directory-p todir) (make-directory todir t)))
  16346. (unless (file-exists-p movefile)
  16347. (org-create-formula-image
  16348. txt movefile optnew forbuffer processing-type))
  16349. (if overlays
  16350. (progn
  16351. (mapc (lambda (o)
  16352. (if (eq (overlay-get o 'org-overlay-type)
  16353. 'org-latex-overlay)
  16354. (delete-overlay o)))
  16355. (overlays-in beg end))
  16356. (setq ov (make-overlay beg end))
  16357. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16358. (if (featurep 'xemacs)
  16359. (progn
  16360. (overlay-put ov 'invisible t)
  16361. (overlay-put
  16362. ov 'end-glyph
  16363. (make-glyph (vector 'png :file movefile))))
  16364. (overlay-put
  16365. ov 'display
  16366. (list 'image :type 'png :file movefile :ascent 'center)))
  16367. (push ov org-latex-fragment-image-overlays)
  16368. (goto-char end))
  16369. (delete-region beg end)
  16370. (insert (org-add-props link
  16371. (list 'org-latex-src
  16372. (replace-regexp-in-string
  16373. "\"" "" txt)
  16374. 'org-latex-src-embed-type
  16375. (if block-type 'paragraph 'character))))))
  16376. ((eq processing-type 'mathml)
  16377. ;; Process to MathML
  16378. (unless (save-match-data (org-format-latex-mathml-available-p))
  16379. (error "LaTeX to MathML converter not configured"))
  16380. (setq txt (match-string n)
  16381. beg (match-beginning n) end (match-end n)
  16382. cnt (1+ cnt))
  16383. (if msg (message msg cnt))
  16384. (goto-char beg)
  16385. (delete-region beg end)
  16386. (insert (org-format-latex-as-mathml
  16387. txt block-type prefix dir)))
  16388. (t
  16389. (error "Unknown conversion type %s for latex fragments"
  16390. processing-type)))))))))
  16391. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16392. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16393. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16394. sucessful, return the portion between \"<math...> </math>\"
  16395. elements otherwise return nil. When MATHML-FILE is specified,
  16396. write the results in to that file. When invoked as an
  16397. interactive command, prompt for LATEX-FRAG, with initial value
  16398. set to the current active region and echo the results for user
  16399. inspection."
  16400. (interactive (list (let ((frag (when (org-region-active-p)
  16401. (buffer-substring-no-properties
  16402. (region-beginning) (region-end)))))
  16403. (read-string "LaTeX Fragment: " frag nil frag))))
  16404. (unless latex-frag (error "Invalid latex-frag"))
  16405. (let* ((tmp-in-file (file-relative-name
  16406. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16407. (ignore (write-region latex-frag nil tmp-in-file))
  16408. (tmp-out-file (file-relative-name
  16409. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16410. (cmd (format-spec
  16411. org-latex-to-mathml-convert-command
  16412. `((?j . ,(shell-quote-argument
  16413. (expand-file-name org-latex-to-mathml-jar-file)))
  16414. (?I . ,(shell-quote-argument tmp-in-file))
  16415. (?o . ,(shell-quote-argument tmp-out-file)))))
  16416. mathml shell-command-output)
  16417. (when (org-called-interactively-p 'any)
  16418. (unless (org-format-latex-mathml-available-p)
  16419. (error "LaTeX to MathML converter not configured")))
  16420. (message "Running %s" cmd)
  16421. (setq shell-command-output (shell-command-to-string cmd))
  16422. (setq mathml
  16423. (when (file-readable-p tmp-out-file)
  16424. (with-current-buffer (find-file-noselect tmp-out-file t)
  16425. (goto-char (point-min))
  16426. (when (re-search-forward
  16427. (concat
  16428. (regexp-quote
  16429. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16430. "\\(.\\|\n\\)*"
  16431. (regexp-quote "</math>")) nil t)
  16432. (prog1 (match-string 0) (kill-buffer))))))
  16433. (cond
  16434. (mathml
  16435. (setq mathml
  16436. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16437. (when mathml-file
  16438. (write-region mathml nil mathml-file))
  16439. (when (org-called-interactively-p 'any)
  16440. (message mathml)))
  16441. ((message "LaTeX to MathML conversion failed")
  16442. (message shell-command-output)))
  16443. (delete-file tmp-in-file)
  16444. (when (file-exists-p tmp-out-file)
  16445. (delete-file tmp-out-file))
  16446. mathml))
  16447. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16448. prefix &optional dir)
  16449. "Use `org-create-math-formula' but check local cache first."
  16450. (let* ((absprefix (expand-file-name prefix dir))
  16451. (print-length nil) (print-level nil)
  16452. (formula-id (concat
  16453. "formula-"
  16454. (sha1
  16455. (prin1-to-string
  16456. (list latex-frag
  16457. org-latex-to-mathml-convert-command)))))
  16458. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16459. (formula-cache-dir (file-name-directory formula-cache)))
  16460. (unless (file-directory-p formula-cache-dir)
  16461. (make-directory formula-cache-dir t))
  16462. (unless (file-exists-p formula-cache)
  16463. (org-create-math-formula latex-frag formula-cache))
  16464. (if (file-exists-p formula-cache)
  16465. ;; Successful conversion. Return the link to MathML file.
  16466. (org-add-props
  16467. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16468. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16469. 'org-latex-src-embed-type (if latex-frag-type
  16470. 'paragraph 'character)))
  16471. ;; Failed conversion. Return the LaTeX fragment verbatim
  16472. latex-frag)))
  16473. (defun org-create-formula-image (string tofile options buffer &optional type)
  16474. "Create an image from LaTeX source using dvipng or convert.
  16475. This function calls either `org-create-formula-image-with-dvipng'
  16476. or `org-create-formula-image-with-imagemagick' depending on the
  16477. value of `org-latex-create-formula-image-program' or on the value
  16478. of the optional TYPE variable.
  16479. Note: ultimately these two function should be combined as they
  16480. share a good deal of logic."
  16481. (org-check-external-command
  16482. "latex" "needed to convert LaTeX fragments to images")
  16483. (funcall
  16484. (case (or type org-latex-create-formula-image-program)
  16485. ('dvipng
  16486. (org-check-external-command
  16487. "dvipng" "needed to convert LaTeX fragments to images")
  16488. #'org-create-formula-image-with-dvipng)
  16489. ('imagemagick
  16490. (org-check-external-command
  16491. "convert" "you need to install imagemagick")
  16492. #'org-create-formula-image-with-imagemagick)
  16493. (t (error
  16494. "invalid value of `org-latex-create-formula-image-program'")))
  16495. string tofile options buffer))
  16496. ;; This function borrows from Ganesh Swami's latex2png.el
  16497. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16498. "This calls dvipng."
  16499. (let* ((tmpdir (if (featurep 'xemacs)
  16500. (temp-directory)
  16501. temporary-file-directory))
  16502. (texfilebase (make-temp-name
  16503. (expand-file-name "orgtex" tmpdir)))
  16504. (texfile (concat texfilebase ".tex"))
  16505. (dvifile (concat texfilebase ".dvi"))
  16506. (pngfile (concat texfilebase ".png"))
  16507. (fnh (if (featurep 'xemacs)
  16508. (font-height (face-font 'default))
  16509. (face-attribute 'default :height nil)))
  16510. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16511. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16512. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16513. "Black"))
  16514. (bg (or (plist-get options (if buffer :background :html-background))
  16515. "Transparent")))
  16516. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16517. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16518. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16519. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16520. (with-temp-file texfile
  16521. (require 'ox-latex)
  16522. (insert (org-latex-guess-inputenc
  16523. (org-splice-latex-header
  16524. org-format-latex-header
  16525. org-latex-default-packages-alist
  16526. org-latex-packages-alist t)))
  16527. (insert "\n\\begin{document}\n" string "\n\\end{document}\n"))
  16528. (let ((dir default-directory))
  16529. (condition-case nil
  16530. (progn
  16531. (cd tmpdir)
  16532. (call-process "latex" nil nil nil texfile))
  16533. (error nil))
  16534. (cd dir))
  16535. (if (not (file-exists-p dvifile))
  16536. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16537. (condition-case nil
  16538. (if (featurep 'xemacs)
  16539. (call-process "dvipng" nil nil nil
  16540. "-fg" fg "-bg" bg
  16541. "-T" "tight"
  16542. "-o" pngfile
  16543. dvifile)
  16544. (call-process "dvipng" nil nil nil
  16545. "-fg" fg "-bg" bg
  16546. "-D" dpi
  16547. ;;"-x" scale "-y" scale
  16548. "-T" "tight"
  16549. "-o" pngfile
  16550. dvifile))
  16551. (error nil))
  16552. (if (not (file-exists-p pngfile))
  16553. (if org-format-latex-signal-error
  16554. (error "Failed to create png file from %s" texfile)
  16555. (message "Failed to create png file from %s" texfile)
  16556. nil)
  16557. ;; Use the requested file name and clean up
  16558. (copy-file pngfile tofile 'replace)
  16559. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16560. (if (file-exists-p (concat texfilebase e))
  16561. (delete-file (concat texfilebase e))))
  16562. pngfile))))
  16563. (defvar org-latex-pdf-process) ; From ox-latex.el
  16564. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16565. "This calls convert, which is included into imagemagick."
  16566. (let* ((tmpdir (if (featurep 'xemacs)
  16567. (temp-directory)
  16568. temporary-file-directory))
  16569. (texfilebase (make-temp-name
  16570. (expand-file-name "orgtex" tmpdir)))
  16571. (texfile (concat texfilebase ".tex"))
  16572. (pdffile (concat texfilebase ".pdf"))
  16573. (pngfile (concat texfilebase ".png"))
  16574. (fnh (if (featurep 'xemacs)
  16575. (font-height (face-font 'default))
  16576. (face-attribute 'default :height nil)))
  16577. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16578. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16579. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16580. "black"))
  16581. (bg (or (plist-get options (if buffer :background :html-background))
  16582. "white")))
  16583. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16584. (setq fg (org-latex-color-format fg)))
  16585. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16586. (setq bg (org-latex-color-format
  16587. (if (string= bg "Transparent") "white" bg))))
  16588. (with-temp-file texfile
  16589. (require 'ox-latex)
  16590. (insert (org-latex-guess-inputenc
  16591. (org-splice-latex-header
  16592. org-format-latex-header
  16593. org-latex-default-packages-alist
  16594. org-latex-packages-alist t)))
  16595. (insert "\n\\begin{document}\n"
  16596. "\\definecolor{fg}{rgb}{" fg "}\n"
  16597. "\\definecolor{bg}{rgb}{" bg "}\n"
  16598. "\n\\pagecolor{bg}\n"
  16599. "\n{\\color{fg}\n"
  16600. string
  16601. "\n}\n"
  16602. "\n\\end{document}\n"))
  16603. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16604. (condition-case nil
  16605. (progn
  16606. (cd tmpdir)
  16607. (setq cmds org-latex-pdf-process)
  16608. (while cmds
  16609. (setq latex-frags-cmds (pop cmds))
  16610. (if (listp latex-frags-cmds)
  16611. (setq cmds nil)
  16612. (setq latex-frags-cmds (list (car org-latex-pdf-process)))))
  16613. (while latex-frags-cmds
  16614. (setq cmd (pop latex-frags-cmds))
  16615. (while (string-match "%b" cmd)
  16616. (setq cmd (replace-match
  16617. (save-match-data
  16618. (shell-quote-argument texfile))
  16619. t t cmd)))
  16620. (while (string-match "%f" cmd)
  16621. (setq cmd (replace-match
  16622. (save-match-data
  16623. (shell-quote-argument
  16624. (file-name-nondirectory texfile)))
  16625. t t cmd)))
  16626. (while (string-match "%o" cmd)
  16627. (setq cmd (replace-match
  16628. (save-match-data
  16629. (shell-quote-argument
  16630. (file-name-directory texfile)))
  16631. t t cmd)))
  16632. (setq cmd (split-string cmd))
  16633. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16634. (error nil))
  16635. (cd dir))
  16636. (if (not (file-exists-p pdffile))
  16637. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16638. (condition-case nil
  16639. (if (featurep 'xemacs)
  16640. (call-process "convert" nil nil nil
  16641. "-density" "96"
  16642. "-trim"
  16643. "-antialias"
  16644. pdffile
  16645. "-quality" "100"
  16646. ;; "-sharpen" "0x1.0"
  16647. pngfile)
  16648. (call-process "convert" nil nil nil
  16649. "-density" dpi
  16650. "-trim"
  16651. "-antialias"
  16652. pdffile
  16653. "-quality" "100"
  16654. ;; "-sharpen" "0x1.0"
  16655. pngfile))
  16656. (error nil))
  16657. (if (not (file-exists-p pngfile))
  16658. (if org-format-latex-signal-error
  16659. (error "Failed to create png file from %s" texfile)
  16660. (message "Failed to create png file from %s" texfile)
  16661. nil)
  16662. ;; Use the requested file name and clean up
  16663. (copy-file pngfile tofile 'replace)
  16664. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16665. (if (file-exists-p (concat texfilebase e))
  16666. (delete-file (concat texfilebase e))))
  16667. pngfile))))
  16668. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16669. "Fill a LaTeX header template TPL.
  16670. In the template, the following place holders will be recognized:
  16671. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16672. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16673. [PACKAGES] \\usepackage statements for PKG
  16674. [NO-PACKAGES] do not include PKG
  16675. [EXTRA] the string EXTRA
  16676. [NO-EXTRA] do not include EXTRA
  16677. For backward compatibility, if both the positive and the negative place
  16678. holder is missing, the positive one (without the \"NO-\") will be
  16679. assumed to be present at the end of the template.
  16680. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16681. EXTRA is a string.
  16682. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16683. (let (rpl (end ""))
  16684. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16685. (setq rpl (if (or (match-end 1) (not def-pkg))
  16686. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16687. tpl (replace-match rpl t t tpl))
  16688. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16689. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16690. (setq rpl (if (or (match-end 1) (not pkg))
  16691. "" (org-latex-packages-to-string pkg snippets-p t))
  16692. tpl (replace-match rpl t t tpl))
  16693. (if pkg (setq end
  16694. (concat end "\n"
  16695. (org-latex-packages-to-string pkg snippets-p)))))
  16696. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16697. (setq rpl (if (or (match-end 1) (not extra))
  16698. "" (concat extra "\n"))
  16699. tpl (replace-match rpl t t tpl))
  16700. (if (and extra (string-match "\\S-" extra))
  16701. (setq end (concat end "\n" extra))))
  16702. (if (string-match "\\S-" end)
  16703. (concat tpl "\n" end)
  16704. tpl)))
  16705. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16706. "Turn an alist of packages into a string with the \\usepackage macros."
  16707. (setq pkg (mapconcat (lambda(p)
  16708. (cond
  16709. ((stringp p) p)
  16710. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16711. (format "%% Package %s omitted" (cadr p)))
  16712. ((equal "" (car p))
  16713. (format "\\usepackage{%s}" (cadr p)))
  16714. (t
  16715. (format "\\usepackage[%s]{%s}"
  16716. (car p) (cadr p)))))
  16717. pkg
  16718. "\n"))
  16719. (if newline (concat pkg "\n") pkg))
  16720. (defun org-dvipng-color (attr)
  16721. "Return a RGB color specification for dvipng."
  16722. (apply 'format "rgb %s %s %s"
  16723. (mapcar 'org-normalize-color
  16724. (if (featurep 'xemacs)
  16725. (color-rgb-components
  16726. (face-property 'default
  16727. (cond ((eq attr :foreground) 'foreground)
  16728. ((eq attr :background) 'background))))
  16729. (color-values (face-attribute 'default attr nil))))))
  16730. (defun org-dvipng-color-format (color-name)
  16731. "Convert COLOR-NAME to a RGB color value for dvipng."
  16732. (apply 'format "rgb %s %s %s"
  16733. (mapcar 'org-normalize-color
  16734. (color-values color-name))))
  16735. (defun org-latex-color (attr)
  16736. "Return a RGB color for the LaTeX color package."
  16737. (apply 'format "%s,%s,%s"
  16738. (mapcar 'org-normalize-color
  16739. (if (featurep 'xemacs)
  16740. (color-rgb-components
  16741. (face-property 'default
  16742. (cond ((eq attr :foreground) 'foreground)
  16743. ((eq attr :background) 'background))))
  16744. (color-values (face-attribute 'default attr nil))))))
  16745. (defun org-latex-color-format (color-name)
  16746. "Convert COLOR-NAME to a RGB color value."
  16747. (apply 'format "%s,%s,%s"
  16748. (mapcar 'org-normalize-color
  16749. (color-values color-name))))
  16750. (defun org-normalize-color (value)
  16751. "Return string to be used as color value for an RGB component."
  16752. (format "%g" (/ value 65535.0)))
  16753. ;; Image display
  16754. (defvar org-inline-image-overlays nil)
  16755. (make-variable-buffer-local 'org-inline-image-overlays)
  16756. (defun org-toggle-inline-images (&optional include-linked)
  16757. "Toggle the display of inline images.
  16758. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16759. (interactive "P")
  16760. (if org-inline-image-overlays
  16761. (progn
  16762. (org-remove-inline-images)
  16763. (message "Inline image display turned off"))
  16764. (org-display-inline-images include-linked)
  16765. (if (and (org-called-interactively-p)
  16766. org-inline-image-overlays)
  16767. (message "%d images displayed inline"
  16768. (length org-inline-image-overlays))
  16769. (message "No images to display inline"))))
  16770. (defun org-redisplay-inline-images ()
  16771. "Refresh the display of inline images."
  16772. (interactive)
  16773. (if (not org-inline-image-overlays)
  16774. (org-toggle-inline-images)
  16775. (org-toggle-inline-images)
  16776. (org-toggle-inline-images)))
  16777. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16778. "Display inline images.
  16779. Normally only links without a description part are inlined, because this
  16780. is how it will work for export. When INCLUDE-LINKED is set, also links
  16781. with a description part will be inlined. This can be nice for a quick
  16782. look at those images, but it does not reflect what exported files will look
  16783. like.
  16784. When REFRESH is set, refresh existing images between BEG and END.
  16785. This will create new image displays only if necessary.
  16786. BEG and END default to the buffer boundaries."
  16787. (interactive "P")
  16788. (unless refresh
  16789. (org-remove-inline-images)
  16790. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16791. (save-excursion
  16792. (save-restriction
  16793. (widen)
  16794. (setq beg (or beg (point-min)) end (or end (point-max)))
  16795. (goto-char beg)
  16796. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16797. (substring (org-image-file-name-regexp) 0 -2)
  16798. "\\)\\]" (if include-linked "" "\\]")))
  16799. old file ov img type attrwidth width)
  16800. (while (re-search-forward re end t)
  16801. (setq old (get-char-property-and-overlay (match-beginning 1)
  16802. 'org-image-overlay)
  16803. file (expand-file-name
  16804. (concat (or (match-string 3) "") (match-string 4))))
  16805. (when (image-type-available-p 'imagemagick)
  16806. (setq attrwidth (if (or (listp org-image-actual-width)
  16807. (null org-image-actual-width))
  16808. (save-excursion
  16809. (save-match-data
  16810. (when (re-search-backward
  16811. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16812. (save-excursion
  16813. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16814. (string-to-number (match-string 1))))))
  16815. width (cond ((eq org-image-actual-width t) nil)
  16816. ((null org-image-actual-width) attrwidth)
  16817. ((numberp org-image-actual-width)
  16818. org-image-actual-width)
  16819. ((listp org-image-actual-width)
  16820. (or attrwidth (car org-image-actual-width))))
  16821. type (if width 'imagemagick)))
  16822. (when (file-exists-p file)
  16823. (if (and (car-safe old) refresh)
  16824. (image-refresh (overlay-get (cdr old) 'display))
  16825. (setq img (save-match-data (create-image file type nil :width width)))
  16826. (when img
  16827. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16828. (overlay-put ov 'display img)
  16829. (overlay-put ov 'face 'default)
  16830. (overlay-put ov 'org-image-overlay t)
  16831. (overlay-put ov 'modification-hooks
  16832. (list 'org-display-inline-remove-overlay))
  16833. (push ov org-inline-image-overlays)))))))))
  16834. (define-obsolete-function-alias
  16835. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16836. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16837. "Remove inline-display overlay if a corresponding region is modified."
  16838. (let ((inhibit-modification-hooks t))
  16839. (when (and ov after)
  16840. (delete ov org-inline-image-overlays)
  16841. (delete-overlay ov))))
  16842. (defun org-remove-inline-images ()
  16843. "Remove inline display of images."
  16844. (interactive)
  16845. (mapc 'delete-overlay org-inline-image-overlays)
  16846. (setq org-inline-image-overlays nil))
  16847. ;;;; Key bindings
  16848. ;; Outline functions from `outline-mode-prefix-map'
  16849. ;; that can be remapped in Org:
  16850. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16851. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16852. (define-key org-mode-map [remap outline-forward-same-level]
  16853. 'org-forward-heading-same-level)
  16854. (define-key org-mode-map [remap outline-backward-same-level]
  16855. 'org-backward-heading-same-level)
  16856. (define-key org-mode-map [remap show-branches]
  16857. 'org-kill-note-or-show-branches)
  16858. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16859. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16860. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16861. ;; Outline functions from `outline-mode-prefix-map' that can not
  16862. ;; be remapped in Org:
  16863. ;;
  16864. ;; - the column "key binding" shows whether the Outline function is still
  16865. ;; available in Org mode on the same key that it has been bound to in
  16866. ;; Outline mode:
  16867. ;; - "overridden": key used for a different functionality in Org mode
  16868. ;; - else: key still bound to the same Outline function in Org mode
  16869. ;;
  16870. ;; | Outline function | key binding | Org replacement |
  16871. ;; |------------------------------------+-------------+-----------------------|
  16872. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16873. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16874. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16875. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16876. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16877. ;; | `show-entry' | overridden | no replacement |
  16878. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16879. ;; | `show-branches' | `C-c C-k' | still same function |
  16880. ;; | `show-subtree' | overridden | visibility cycling |
  16881. ;; | `show-all' | overridden | no replacement |
  16882. ;; | `hide-subtree' | overridden | visibility cycling |
  16883. ;; | `hide-body' | overridden | no replacement |
  16884. ;; | `hide-entry' | overridden | visibility cycling |
  16885. ;; | `hide-leaves' | overridden | no replacement |
  16886. ;; | `hide-sublevels' | overridden | no replacement |
  16887. ;; | `hide-other' | overridden | no replacement |
  16888. ;; Make `C-c C-x' a prefix key
  16889. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16890. ;; TAB key with modifiers
  16891. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16892. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16893. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16894. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16895. ;; The following line is necessary under Suse GNU/Linux
  16896. (unless (featurep 'xemacs)
  16897. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16898. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16899. (define-key org-mode-map [backtab] 'org-shifttab)
  16900. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16901. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16902. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16903. ;; Cursor keys with modifiers
  16904. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16905. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16906. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16907. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16908. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16909. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16910. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16911. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16912. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16913. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16914. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16915. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16916. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16917. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16918. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16919. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16920. ;; Babel keys
  16921. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16922. (mapc (lambda (pair)
  16923. (define-key org-babel-map (car pair) (cdr pair)))
  16924. org-babel-key-bindings)
  16925. ;;; Extra keys for tty access.
  16926. ;; We only set them when really needed because otherwise the
  16927. ;; menus don't show the simple keys
  16928. (when (or org-use-extra-keys
  16929. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16930. (not window-system))
  16931. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16932. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16933. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16934. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16935. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16936. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16937. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16938. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16939. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16940. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16941. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16942. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16943. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16944. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16945. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16946. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16947. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16948. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16949. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16950. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16951. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16952. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16953. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16954. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16955. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16956. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16957. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16958. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16959. ;; All the other keys
  16960. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16961. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16962. (if (boundp 'narrow-map)
  16963. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16964. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16965. (if (boundp 'narrow-map)
  16966. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16967. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16968. (if (boundp 'narrow-map)
  16969. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16970. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16971. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16972. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16973. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16974. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16975. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16976. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16977. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16978. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  16979. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  16980. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16981. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16982. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16983. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16984. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16985. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16986. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16987. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16988. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16989. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16990. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16991. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16992. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16993. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16994. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16995. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16996. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16997. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16998. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16999. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17000. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17001. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17002. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17003. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17004. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17005. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17006. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17007. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17008. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17009. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17010. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17011. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17012. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17013. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17014. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17015. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17016. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17017. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17018. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17019. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17020. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17021. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17022. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17023. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17024. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17025. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17026. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17027. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17028. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17029. (org-defkey org-mode-map "\C-m" 'org-return)
  17030. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17031. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17032. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17033. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17034. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17035. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17036. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17037. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17038. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17039. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17040. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17041. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17042. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17043. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17044. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17045. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17046. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17047. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17048. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17049. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17050. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17051. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17052. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17053. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17054. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17055. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17056. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17057. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17058. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17059. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17060. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17061. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17062. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17063. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17064. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17065. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17066. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17067. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17068. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17069. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17070. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17071. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17072. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17073. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17074. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17075. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17076. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17077. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17078. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17079. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17080. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17081. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17082. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17083. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17084. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17085. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17086. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17087. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17088. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17089. (when (featurep 'xemacs)
  17090. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17091. (defconst org-speed-commands-default
  17092. '(
  17093. ("Outline Navigation")
  17094. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17095. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17096. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17097. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17098. ("F" . org-next-block)
  17099. ("B" . org-previous-block)
  17100. ("u" . (org-speed-move-safe 'outline-up-heading))
  17101. ("j" . org-goto)
  17102. ("g" . (org-refile t))
  17103. ("Outline Visibility")
  17104. ("c" . org-cycle)
  17105. ("C" . org-shifttab)
  17106. (" " . org-display-outline-path)
  17107. ("=" . org-columns)
  17108. ("Outline Structure Editing")
  17109. ("U" . org-shiftmetaup)
  17110. ("D" . org-shiftmetadown)
  17111. ("r" . org-metaright)
  17112. ("l" . org-metaleft)
  17113. ("R" . org-shiftmetaright)
  17114. ("L" . org-shiftmetaleft)
  17115. ("i" . (progn (forward-char 1) (call-interactively
  17116. 'org-insert-heading-respect-content)))
  17117. ("^" . org-sort)
  17118. ("w" . org-refile)
  17119. ("a" . org-archive-subtree-default-with-confirmation)
  17120. ("." . org-mark-subtree) ;; FIXME Better use @ (see C-c @) here?
  17121. ("#" . org-toggle-comment)
  17122. ("Clock Commands")
  17123. ("I" . org-clock-in)
  17124. ("O" . org-clock-out)
  17125. ("Meta Data Editing")
  17126. ("t" . org-todo)
  17127. ("," . (org-priority))
  17128. ("0" . (org-priority ?\ ))
  17129. ("1" . (org-priority ?A))
  17130. ("2" . (org-priority ?B))
  17131. ("3" . (org-priority ?C))
  17132. (":" . org-set-tags-command)
  17133. ("e" . org-set-effort)
  17134. ("E" . org-inc-effort)
  17135. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17136. (org-entry-put (point) "APPT_WARNTIME" m)))
  17137. ("Agenda Views etc")
  17138. ("v" . org-agenda)
  17139. ("/" . org-sparse-tree)
  17140. ("Misc")
  17141. ("o" . org-open-at-point)
  17142. ("?" . org-speed-command-help)
  17143. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17144. (">" . (org-agenda-remove-restriction-lock))
  17145. )
  17146. "The default speed commands.")
  17147. (defun org-print-speed-command (e)
  17148. (if (> (length (car e)) 1)
  17149. (progn
  17150. (princ "\n")
  17151. (princ (car e))
  17152. (princ "\n")
  17153. (princ (make-string (length (car e)) ?-))
  17154. (princ "\n"))
  17155. (princ (car e))
  17156. (princ " ")
  17157. (if (symbolp (cdr e))
  17158. (princ (symbol-name (cdr e)))
  17159. (prin1 (cdr e)))
  17160. (princ "\n")))
  17161. (defun org-speed-command-help ()
  17162. "Show the available speed commands."
  17163. (interactive)
  17164. (if (not org-use-speed-commands)
  17165. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17166. (with-output-to-temp-buffer "*Help*"
  17167. (princ "User-defined Speed commands\n===========================\n")
  17168. (mapc 'org-print-speed-command org-speed-commands-user)
  17169. (princ "\n")
  17170. (princ "Built-in Speed commands\n=======================\n")
  17171. (mapc 'org-print-speed-command org-speed-commands-default))
  17172. (with-current-buffer "*Help*"
  17173. (setq truncate-lines t))))
  17174. (defun org-speed-move-safe (cmd)
  17175. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17176. If not, return to the original position and throw an error."
  17177. (interactive)
  17178. (let ((pos (point)))
  17179. (call-interactively cmd)
  17180. (unless (and (bolp) (org-at-heading-p))
  17181. (goto-char pos)
  17182. (error "Boundary reached while executing %s" cmd))))
  17183. (defvar org-self-insert-command-undo-counter 0)
  17184. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17185. (defvar org-speed-command nil)
  17186. (define-obsolete-function-alias
  17187. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17188. (defun org-speed-command-activate (keys)
  17189. "Hook for activating single-letter speed commands.
  17190. `org-speed-commands-default' specifies a minimal command set.
  17191. Use `org-speed-commands-user' for further customization."
  17192. (when (or (and (bolp) (looking-at org-outline-regexp))
  17193. (and (functionp org-use-speed-commands)
  17194. (funcall org-use-speed-commands)))
  17195. (cdr (assoc keys (append org-speed-commands-user
  17196. org-speed-commands-default)))))
  17197. (define-obsolete-function-alias
  17198. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17199. (defun org-babel-speed-command-activate (keys)
  17200. "Hook for activating single-letter code block commands."
  17201. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17202. (cdr (assoc keys org-babel-key-bindings))))
  17203. (defcustom org-speed-command-hook
  17204. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17205. "Hook for activating speed commands at strategic locations.
  17206. Hook functions are called in sequence until a valid handler is
  17207. found.
  17208. Each hook takes a single argument, a user-pressed command key
  17209. which is also a `self-insert-command' from the global map.
  17210. Within the hook, examine the cursor position and the command key
  17211. and return nil or a valid handler as appropriate. Handler could
  17212. be one of an interactive command, a function, or a form.
  17213. Set `org-use-speed-commands' to non-nil value to enable this
  17214. hook. The default setting is `org-speed-command-activate'."
  17215. :group 'org-structure
  17216. :version "24.1"
  17217. :type 'hook)
  17218. (defun org-self-insert-command (N)
  17219. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17220. If the cursor is in a table looking at whitespace, the whitespace is
  17221. overwritten, and the table is not marked as requiring realignment."
  17222. (interactive "p")
  17223. (org-check-before-invisible-edit 'insert)
  17224. (cond
  17225. ((and org-use-speed-commands
  17226. (setq org-speed-command
  17227. (run-hook-with-args-until-success
  17228. 'org-speed-command-hook (this-command-keys))))
  17229. (cond
  17230. ((commandp org-speed-command)
  17231. (setq this-command org-speed-command)
  17232. (call-interactively org-speed-command))
  17233. ((functionp org-speed-command)
  17234. (funcall org-speed-command))
  17235. ((and org-speed-command (listp org-speed-command))
  17236. (eval org-speed-command))
  17237. (t (let (org-use-speed-commands)
  17238. (call-interactively 'org-self-insert-command)))))
  17239. ((and
  17240. (org-table-p)
  17241. (progn
  17242. ;; check if we blank the field, and if that triggers align
  17243. (and (featurep 'org-table) org-table-auto-blank-field
  17244. (member last-command
  17245. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17246. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17247. ;; got extra space, this field does not determine column width
  17248. (let (org-table-may-need-update) (org-table-blank-field))
  17249. ;; no extra space, this field may determine column width
  17250. (org-table-blank-field)))
  17251. t)
  17252. (eq N 1)
  17253. (looking-at "[^|\n]* |"))
  17254. (let (org-table-may-need-update)
  17255. (goto-char (1- (match-end 0)))
  17256. (backward-delete-char 1)
  17257. (goto-char (match-beginning 0))
  17258. (self-insert-command N)))
  17259. (t
  17260. (setq org-table-may-need-update t)
  17261. (self-insert-command N)
  17262. (org-fix-tags-on-the-fly)
  17263. (if org-self-insert-cluster-for-undo
  17264. (if (not (eq last-command 'org-self-insert-command))
  17265. (setq org-self-insert-command-undo-counter 1)
  17266. (if (>= org-self-insert-command-undo-counter 20)
  17267. (setq org-self-insert-command-undo-counter 1)
  17268. (and (> org-self-insert-command-undo-counter 0)
  17269. buffer-undo-list (listp buffer-undo-list)
  17270. (not (cadr buffer-undo-list)) ; remove nil entry
  17271. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17272. (setq org-self-insert-command-undo-counter
  17273. (1+ org-self-insert-command-undo-counter))))))))
  17274. (defun org-check-before-invisible-edit (kind)
  17275. "Check is editing if kind KIND would be dangerous with invisible text around.
  17276. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17277. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17278. (if (and org-catch-invisible-edits
  17279. (or (not (boundp 'visible-mode)) (not visible-mode))
  17280. (or (get-char-property (point) 'invisible)
  17281. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17282. ;; OK, we need to take a closer look
  17283. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17284. (invisible-before-point (if (bobp) nil (get-char-property
  17285. (1- (point)) 'invisible)))
  17286. (border-and-ok-direction
  17287. (or
  17288. ;; Check if we are acting predictably before invisible text
  17289. (and invisible-at-point (not invisible-before-point)
  17290. (memq kind '(insert delete-backward)))
  17291. ;; Check if we are acting predictably after invisible text
  17292. ;; This works not well, and I have turned it off. It seems
  17293. ;; better to always show and stop after invisible text.
  17294. ;; (and (not invisible-at-point) invisible-before-point
  17295. ;; (memq kind '(insert delete)))
  17296. )))
  17297. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17298. (memq invisible-before-point '(outline org-hide-block t)))
  17299. (if (eq org-catch-invisible-edits 'error)
  17300. (error "Editing in invisible areas is prohibited - make visible first"))
  17301. (if (and org-custom-properties-overlays
  17302. (y-or-n-p "Display invisible properties in this buffer? "))
  17303. (org-toggle-custom-properties-visibility)
  17304. ;; Make the area visible
  17305. (save-excursion
  17306. (if invisible-before-point
  17307. (goto-char (previous-single-char-property-change
  17308. (point) 'invisible)))
  17309. (org-cycle))
  17310. (cond
  17311. ((eq org-catch-invisible-edits 'show)
  17312. ;; That's it, we do the edit after showing
  17313. (message
  17314. "Unfolding invisible region around point before editing")
  17315. (sit-for 1))
  17316. ((and (eq org-catch-invisible-edits 'smart)
  17317. border-and-ok-direction)
  17318. (message "Unfolding invisible region around point before editing"))
  17319. (t
  17320. ;; Don't do the edit, make the user repeat it in full visibility
  17321. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17322. (defun org-fix-tags-on-the-fly ()
  17323. (when (and (equal (char-after (point-at-bol)) ?*)
  17324. (org-at-heading-p))
  17325. (org-align-tags-here org-tags-column)))
  17326. (defun org-delete-backward-char (N)
  17327. "Like `delete-backward-char', insert whitespace at field end in tables.
  17328. When deleting backwards, in tables this function will insert whitespace in
  17329. front of the next \"|\" separator, to keep the table aligned. The table will
  17330. still be marked for re-alignment if the field did fill the entire column,
  17331. because, in this case the deletion might narrow the column."
  17332. (interactive "p")
  17333. (save-match-data
  17334. (org-check-before-invisible-edit 'delete-backward)
  17335. (if (and (org-table-p)
  17336. (eq N 1)
  17337. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17338. (looking-at ".*?|"))
  17339. (let ((pos (point))
  17340. (noalign (looking-at "[^|\n\r]* |"))
  17341. (c org-table-may-need-update))
  17342. (backward-delete-char N)
  17343. (if (not overwrite-mode)
  17344. (progn
  17345. (skip-chars-forward "^|")
  17346. (insert " ")
  17347. (goto-char (1- pos))))
  17348. ;; noalign: if there were two spaces at the end, this field
  17349. ;; does not determine the width of the column.
  17350. (if noalign (setq org-table-may-need-update c)))
  17351. (backward-delete-char N)
  17352. (org-fix-tags-on-the-fly))))
  17353. (defun org-delete-char (N)
  17354. "Like `delete-char', but insert whitespace at field end in tables.
  17355. When deleting characters, in tables this function will insert whitespace in
  17356. front of the next \"|\" separator, to keep the table aligned. The table will
  17357. still be marked for re-alignment if the field did fill the entire column,
  17358. because, in this case the deletion might narrow the column."
  17359. (interactive "p")
  17360. (save-match-data
  17361. (org-check-before-invisible-edit 'delete)
  17362. (if (and (org-table-p)
  17363. (not (bolp))
  17364. (not (= (char-after) ?|))
  17365. (eq N 1))
  17366. (if (looking-at ".*?|")
  17367. (let ((pos (point))
  17368. (noalign (looking-at "[^|\n\r]* |"))
  17369. (c org-table-may-need-update))
  17370. (replace-match (concat
  17371. (substring (match-string 0) 1 -1)
  17372. " |"))
  17373. (goto-char pos)
  17374. ;; noalign: if there were two spaces at the end, this field
  17375. ;; does not determine the width of the column.
  17376. (if noalign (setq org-table-may-need-update c)))
  17377. (delete-char N))
  17378. (delete-char N)
  17379. (org-fix-tags-on-the-fly))))
  17380. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17381. (put 'org-self-insert-command 'delete-selection t)
  17382. (put 'orgtbl-self-insert-command 'delete-selection t)
  17383. (put 'org-delete-char 'delete-selection 'supersede)
  17384. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17385. (put 'org-yank 'delete-selection 'yank)
  17386. ;; Make `flyspell-mode' delay after some commands
  17387. (put 'org-self-insert-command 'flyspell-delayed t)
  17388. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17389. (put 'org-delete-char 'flyspell-delayed t)
  17390. (put 'org-delete-backward-char 'flyspell-delayed t)
  17391. ;; Make pabbrev-mode expand after org-mode commands
  17392. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17393. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17394. ;; How to do this: Measure non-white length of current string
  17395. ;; If equal to column width, we should realign.
  17396. (defun org-remap (map &rest commands)
  17397. "In MAP, remap the functions given in COMMANDS.
  17398. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17399. (let (new old)
  17400. (while commands
  17401. (setq old (pop commands) new (pop commands))
  17402. (if (fboundp 'command-remapping)
  17403. (org-defkey map (vector 'remap old) new)
  17404. (substitute-key-definition old new map global-map)))))
  17405. (when (eq org-enable-table-editor 'optimized)
  17406. ;; If the user wants maximum table support, we need to hijack
  17407. ;; some standard editing functions
  17408. (org-remap org-mode-map
  17409. 'self-insert-command 'org-self-insert-command
  17410. 'delete-char 'org-delete-char
  17411. 'delete-backward-char 'org-delete-backward-char)
  17412. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17413. (defvar org-ctrl-c-ctrl-c-hook nil
  17414. "Hook for functions attaching themselves to `C-c C-c'.
  17415. This can be used to add additional functionality to the C-c C-c
  17416. key which executes context-dependent commands. This hook is run
  17417. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17418. run after the last test.
  17419. Each function will be called with no arguments. The function
  17420. must check if the context is appropriate for it to act. If yes,
  17421. it should do its thing and then return a non-nil value. If the
  17422. context is wrong, just do nothing and return nil.")
  17423. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17424. "Hook for functions attaching themselves to `C-c C-c'.
  17425. This can be used to add additional functionality to the C-c C-c
  17426. key which executes context-dependent commands. This hook is run
  17427. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17428. before the first test.
  17429. Each function will be called with no arguments. The function
  17430. must check if the context is appropriate for it to act. If yes,
  17431. it should do its thing and then return a non-nil value. If the
  17432. context is wrong, just do nothing and return nil.")
  17433. (defvar org-tab-first-hook nil
  17434. "Hook for functions to attach themselves to TAB.
  17435. See `org-ctrl-c-ctrl-c-hook' for more information.
  17436. This hook runs as the first action when TAB is pressed, even before
  17437. `org-cycle' messes around with the `outline-regexp' to cater for
  17438. inline tasks and plain list item folding.
  17439. If any function in this hook returns t, any other actions that
  17440. would have been caused by TAB (such as table field motion or visibility
  17441. cycling) will not occur.")
  17442. (defvar org-tab-after-check-for-table-hook nil
  17443. "Hook for functions to attach themselves to TAB.
  17444. See `org-ctrl-c-ctrl-c-hook' for more information.
  17445. This hook runs after it has been established that the cursor is not in a
  17446. table, but before checking if the cursor is in a headline or if global cycling
  17447. should be done.
  17448. If any function in this hook returns t, not other actions like visibility
  17449. cycling will be done.")
  17450. (defvar org-tab-after-check-for-cycling-hook nil
  17451. "Hook for functions to attach themselves to TAB.
  17452. See `org-ctrl-c-ctrl-c-hook' for more information.
  17453. This hook runs after it has been established that not table field motion and
  17454. not visibility should be done because of current context. This is probably
  17455. the place where a package like yasnippets can hook in.")
  17456. (defvar org-tab-before-tab-emulation-hook nil
  17457. "Hook for functions to attach themselves to TAB.
  17458. See `org-ctrl-c-ctrl-c-hook' for more information.
  17459. This hook runs after every other options for TAB have been exhausted, but
  17460. before indentation and \t insertion takes place.")
  17461. (defvar org-metaleft-hook nil
  17462. "Hook for functions attaching themselves to `M-left'.
  17463. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17464. (defvar org-metaright-hook nil
  17465. "Hook for functions attaching themselves to `M-right'.
  17466. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17467. (defvar org-metaup-hook nil
  17468. "Hook for functions attaching themselves to `M-up'.
  17469. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17470. (defvar org-metadown-hook nil
  17471. "Hook for functions attaching themselves to `M-down'.
  17472. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17473. (defvar org-shiftmetaleft-hook nil
  17474. "Hook for functions attaching themselves to `M-S-left'.
  17475. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17476. (defvar org-shiftmetaright-hook nil
  17477. "Hook for functions attaching themselves to `M-S-right'.
  17478. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17479. (defvar org-shiftmetaup-hook nil
  17480. "Hook for functions attaching themselves to `M-S-up'.
  17481. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17482. (defvar org-shiftmetadown-hook nil
  17483. "Hook for functions attaching themselves to `M-S-down'.
  17484. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17485. (defvar org-metareturn-hook nil
  17486. "Hook for functions attaching themselves to `M-RET'.
  17487. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17488. (defvar org-shiftup-hook nil
  17489. "Hook for functions attaching themselves to `S-up'.
  17490. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17491. (defvar org-shiftup-final-hook nil
  17492. "Hook for functions attaching themselves to `S-up'.
  17493. This one runs after all other options except shift-select have been excluded.
  17494. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17495. (defvar org-shiftdown-hook nil
  17496. "Hook for functions attaching themselves to `S-down'.
  17497. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17498. (defvar org-shiftdown-final-hook nil
  17499. "Hook for functions attaching themselves to `S-down'.
  17500. This one runs after all other options except shift-select have been excluded.
  17501. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17502. (defvar org-shiftleft-hook nil
  17503. "Hook for functions attaching themselves to `S-left'.
  17504. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17505. (defvar org-shiftleft-final-hook nil
  17506. "Hook for functions attaching themselves to `S-left'.
  17507. This one runs after all other options except shift-select have been excluded.
  17508. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17509. (defvar org-shiftright-hook nil
  17510. "Hook for functions attaching themselves to `S-right'.
  17511. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17512. (defvar org-shiftright-final-hook nil
  17513. "Hook for functions attaching themselves to `S-right'.
  17514. This one runs after all other options except shift-select have been excluded.
  17515. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17516. (defun org-modifier-cursor-error ()
  17517. "Throw an error, a modified cursor command was applied in wrong context."
  17518. (error "This command is active in special context like tables, headlines or items"))
  17519. (defun org-shiftselect-error ()
  17520. "Throw an error because Shift-Cursor command was applied in wrong context."
  17521. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17522. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17523. (error "This command works only in special context like headlines or timestamps")))
  17524. (defun org-call-for-shift-select (cmd)
  17525. (let ((this-command-keys-shift-translated t))
  17526. (call-interactively cmd)))
  17527. (defun org-shifttab (&optional arg)
  17528. "Global visibility cycling or move to previous table field.
  17529. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17530. on context.
  17531. See the individual commands for more information."
  17532. (interactive "P")
  17533. (cond
  17534. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17535. ((integerp arg)
  17536. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17537. (message "Content view to level: %d" arg)
  17538. (org-content (prefix-numeric-value arg2))
  17539. (setq org-cycle-global-status 'overview)))
  17540. (t (call-interactively 'org-global-cycle))))
  17541. (defun org-shiftmetaleft ()
  17542. "Promote subtree or delete table column.
  17543. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17544. `org-table-delete-column', depending on context. See the
  17545. individual commands for more information."
  17546. (interactive)
  17547. (cond
  17548. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17549. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17550. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17551. ((if (not (org-region-active-p)) (org-at-item-p)
  17552. (save-excursion (goto-char (region-beginning))
  17553. (org-at-item-p)))
  17554. (call-interactively 'org-outdent-item-tree))
  17555. (t (org-modifier-cursor-error))))
  17556. (defun org-shiftmetaright ()
  17557. "Demote subtree or insert table column.
  17558. Calls `org-demote-subtree', `org-indent-item-tree', or
  17559. `org-table-insert-column', depending on context. See the
  17560. individual commands for more information."
  17561. (interactive)
  17562. (cond
  17563. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17564. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17565. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17566. ((if (not (org-region-active-p)) (org-at-item-p)
  17567. (save-excursion (goto-char (region-beginning))
  17568. (org-at-item-p)))
  17569. (call-interactively 'org-indent-item-tree))
  17570. (t (org-modifier-cursor-error))))
  17571. (defun org-shiftmetaup (&optional arg)
  17572. "Move subtree up or kill table row.
  17573. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17574. `org-move-item-up' or `org-timestamp-up', depending on context.
  17575. See the individual commands for more information."
  17576. (interactive "P")
  17577. (cond
  17578. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17579. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17580. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17581. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17582. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17583. (call-interactively 'org-timestamp-up)))
  17584. (t (call-interactively 'org-drag-line-backward))))
  17585. (defun org-shiftmetadown (&optional arg)
  17586. "Move subtree down or insert table row.
  17587. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17588. `org-move-item-down' or `org-timestamp-up', depending on context.
  17589. See the individual commands for more information."
  17590. (interactive "P")
  17591. (cond
  17592. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17593. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17594. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17595. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17596. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17597. (call-interactively 'org-timestamp-down)))
  17598. (t (call-interactively 'org-drag-line-forward))))
  17599. (defsubst org-hidden-tree-error ()
  17600. (error
  17601. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17602. (defun org-metaleft (&optional arg)
  17603. "Promote heading or move table column to left.
  17604. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17605. With no specific context, calls the Emacs default `backward-word'.
  17606. See the individual commands for more information."
  17607. (interactive "P")
  17608. (cond
  17609. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17610. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17611. ((org-with-limited-levels
  17612. (or (org-at-heading-p)
  17613. (and (org-region-active-p)
  17614. (save-excursion
  17615. (goto-char (region-beginning))
  17616. (org-at-heading-p)))))
  17617. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17618. (call-interactively 'org-do-promote))
  17619. ;; At an inline task.
  17620. ((org-at-heading-p)
  17621. (call-interactively 'org-inlinetask-promote))
  17622. ((or (org-at-item-p)
  17623. (and (org-region-active-p)
  17624. (save-excursion
  17625. (goto-char (region-beginning))
  17626. (org-at-item-p))))
  17627. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17628. (call-interactively 'org-outdent-item))
  17629. (t (call-interactively 'backward-word))))
  17630. (defun org-metaright (&optional arg)
  17631. "Demote a subtree, a list item or move table column to right.
  17632. In front of a drawer or a block keyword, indent it correctly.
  17633. With no specific context, calls the Emacs default `forward-word'.
  17634. See the individual commands for more information."
  17635. (interactive "P")
  17636. (cond
  17637. ((run-hook-with-args-until-success 'org-metaright-hook))
  17638. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17639. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17640. ((org-at-block-p) (call-interactively 'org-indent-block))
  17641. ((org-with-limited-levels
  17642. (or (org-at-heading-p)
  17643. (and (org-region-active-p)
  17644. (save-excursion
  17645. (goto-char (region-beginning))
  17646. (org-at-heading-p)))))
  17647. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17648. (call-interactively 'org-do-demote))
  17649. ;; At an inline task.
  17650. ((org-at-heading-p)
  17651. (call-interactively 'org-inlinetask-demote))
  17652. ((or (org-at-item-p)
  17653. (and (org-region-active-p)
  17654. (save-excursion
  17655. (goto-char (region-beginning))
  17656. (org-at-item-p))))
  17657. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17658. (call-interactively 'org-indent-item))
  17659. (t (call-interactively 'forward-word))))
  17660. (defun org-check-for-hidden (what)
  17661. "Check if there are hidden headlines/items in the current visual line.
  17662. WHAT can be either `headlines' or `items'. If the current line is
  17663. an outline or item heading and it has a folded subtree below it,
  17664. this function returns t, nil otherwise."
  17665. (let ((re (cond
  17666. ((eq what 'headlines) org-outline-regexp-bol)
  17667. ((eq what 'items) (org-item-beginning-re))
  17668. (t (error "This should not happen"))))
  17669. beg end)
  17670. (save-excursion
  17671. (catch 'exit
  17672. (unless (org-region-active-p)
  17673. (setq beg (point-at-bol))
  17674. (beginning-of-line 2)
  17675. (while (and (not (eobp)) ;; this is like `next-line'
  17676. (get-char-property (1- (point)) 'invisible))
  17677. (beginning-of-line 2))
  17678. (setq end (point))
  17679. (goto-char beg)
  17680. (goto-char (point-at-eol))
  17681. (setq end (max end (point)))
  17682. (while (re-search-forward re end t)
  17683. (if (get-char-property (match-beginning 0) 'invisible)
  17684. (throw 'exit t))))
  17685. nil))))
  17686. (defun org-metaup (&optional arg)
  17687. "Move subtree up or move table row up.
  17688. Calls `org-move-subtree-up' or `org-table-move-row' or
  17689. `org-move-item-up', depending on context. See the individual commands
  17690. for more information."
  17691. (interactive "P")
  17692. (cond
  17693. ((run-hook-with-args-until-success 'org-metaup-hook))
  17694. ((org-region-active-p)
  17695. (let* ((a (min (region-beginning) (region-end)))
  17696. (b (1- (max (region-beginning) (region-end))))
  17697. (c (save-excursion (goto-char a)
  17698. (move-beginning-of-line 0)))
  17699. (d (save-excursion (goto-char a)
  17700. (move-end-of-line 0) (point))))
  17701. (transpose-regions a b c d)
  17702. (goto-char c)))
  17703. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17704. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17705. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17706. (t (org-drag-element-backward))))
  17707. (defun org-metadown (&optional arg)
  17708. "Move subtree down or move table row down.
  17709. Calls `org-move-subtree-down' or `org-table-move-row' or
  17710. `org-move-item-down', depending on context. See the individual
  17711. commands for more information."
  17712. (interactive "P")
  17713. (cond
  17714. ((run-hook-with-args-until-success 'org-metadown-hook))
  17715. ((org-region-active-p)
  17716. (let* ((a (min (region-beginning) (region-end)))
  17717. (b (max (region-beginning) (region-end)))
  17718. (c (save-excursion (goto-char b)
  17719. (move-beginning-of-line 1)))
  17720. (d (save-excursion (goto-char b)
  17721. (move-end-of-line 1) (1+ (point)))))
  17722. (transpose-regions a b c d)
  17723. (goto-char d)))
  17724. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17725. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17726. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17727. (t (org-drag-element-forward))))
  17728. (defun org-shiftup (&optional arg)
  17729. "Increase item in timestamp or increase priority of current headline.
  17730. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17731. depending on context. See the individual commands for more information."
  17732. (interactive "P")
  17733. (cond
  17734. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17735. ((and org-support-shift-select (org-region-active-p))
  17736. (org-call-for-shift-select 'previous-line))
  17737. ((org-at-timestamp-p t)
  17738. (call-interactively (if org-edit-timestamp-down-means-later
  17739. 'org-timestamp-down 'org-timestamp-up)))
  17740. ((and (not (eq org-support-shift-select 'always))
  17741. org-enable-priority-commands
  17742. (org-at-heading-p))
  17743. (call-interactively 'org-priority-up))
  17744. ((and (not org-support-shift-select) (org-at-item-p))
  17745. (call-interactively 'org-previous-item))
  17746. ((org-clocktable-try-shift 'up arg))
  17747. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17748. (org-support-shift-select
  17749. (org-call-for-shift-select 'previous-line))
  17750. (t (org-shiftselect-error))))
  17751. (defun org-shiftdown (&optional arg)
  17752. "Decrease item in timestamp or decrease priority of current headline.
  17753. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17754. depending on context. See the individual commands for more information."
  17755. (interactive "P")
  17756. (cond
  17757. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17758. ((and org-support-shift-select (org-region-active-p))
  17759. (org-call-for-shift-select 'next-line))
  17760. ((org-at-timestamp-p t)
  17761. (call-interactively (if org-edit-timestamp-down-means-later
  17762. 'org-timestamp-up 'org-timestamp-down)))
  17763. ((and (not (eq org-support-shift-select 'always))
  17764. org-enable-priority-commands
  17765. (org-at-heading-p))
  17766. (call-interactively 'org-priority-down))
  17767. ((and (not org-support-shift-select) (org-at-item-p))
  17768. (call-interactively 'org-next-item))
  17769. ((org-clocktable-try-shift 'down arg))
  17770. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17771. (org-support-shift-select
  17772. (org-call-for-shift-select 'next-line))
  17773. (t (org-shiftselect-error))))
  17774. (defun org-shiftright (&optional arg)
  17775. "Cycle the thing at point or in the current line, depending on context.
  17776. Depending on context, this does one of the following:
  17777. - switch a timestamp at point one day into the future
  17778. - on a headline, switch to the next TODO keyword.
  17779. - on an item, switch entire list to the next bullet type
  17780. - on a property line, switch to the next allowed value
  17781. - on a clocktable definition line, move time block into the future"
  17782. (interactive "P")
  17783. (cond
  17784. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17785. ((and org-support-shift-select (org-region-active-p))
  17786. (org-call-for-shift-select 'forward-char))
  17787. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17788. ((and (not (eq org-support-shift-select 'always))
  17789. (org-at-heading-p))
  17790. (let ((org-inhibit-logging
  17791. (not org-treat-S-cursor-todo-selection-as-state-change))
  17792. (org-inhibit-blocking
  17793. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17794. (org-call-with-arg 'org-todo 'right)))
  17795. ((or (and org-support-shift-select
  17796. (not (eq org-support-shift-select 'always))
  17797. (org-at-item-bullet-p))
  17798. (and (not org-support-shift-select) (org-at-item-p)))
  17799. (org-call-with-arg 'org-cycle-list-bullet nil))
  17800. ((and (not (eq org-support-shift-select 'always))
  17801. (org-at-property-p))
  17802. (call-interactively 'org-property-next-allowed-value))
  17803. ((org-clocktable-try-shift 'right arg))
  17804. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17805. (org-support-shift-select
  17806. (org-call-for-shift-select 'forward-char))
  17807. (t (org-shiftselect-error))))
  17808. (defun org-shiftleft (&optional arg)
  17809. "Cycle the thing at point or in the current line, depending on context.
  17810. Depending on context, this does one of the following:
  17811. - switch a timestamp at point one day into the past
  17812. - on a headline, switch to the previous TODO keyword.
  17813. - on an item, switch entire list to the previous bullet type
  17814. - on a property line, switch to the previous allowed value
  17815. - on a clocktable definition line, move time block into the past"
  17816. (interactive "P")
  17817. (cond
  17818. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17819. ((and org-support-shift-select (org-region-active-p))
  17820. (org-call-for-shift-select 'backward-char))
  17821. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17822. ((and (not (eq org-support-shift-select 'always))
  17823. (org-at-heading-p))
  17824. (let ((org-inhibit-logging
  17825. (not org-treat-S-cursor-todo-selection-as-state-change))
  17826. (org-inhibit-blocking
  17827. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17828. (org-call-with-arg 'org-todo 'left)))
  17829. ((or (and org-support-shift-select
  17830. (not (eq org-support-shift-select 'always))
  17831. (org-at-item-bullet-p))
  17832. (and (not org-support-shift-select) (org-at-item-p)))
  17833. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17834. ((and (not (eq org-support-shift-select 'always))
  17835. (org-at-property-p))
  17836. (call-interactively 'org-property-previous-allowed-value))
  17837. ((org-clocktable-try-shift 'left arg))
  17838. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17839. (org-support-shift-select
  17840. (org-call-for-shift-select 'backward-char))
  17841. (t (org-shiftselect-error))))
  17842. (defun org-shiftcontrolright ()
  17843. "Switch to next TODO set."
  17844. (interactive)
  17845. (cond
  17846. ((and org-support-shift-select (org-region-active-p))
  17847. (org-call-for-shift-select 'forward-word))
  17848. ((and (not (eq org-support-shift-select 'always))
  17849. (org-at-heading-p))
  17850. (org-call-with-arg 'org-todo 'nextset))
  17851. (org-support-shift-select
  17852. (org-call-for-shift-select 'forward-word))
  17853. (t (org-shiftselect-error))))
  17854. (defun org-shiftcontrolleft ()
  17855. "Switch to previous TODO set."
  17856. (interactive)
  17857. (cond
  17858. ((and org-support-shift-select (org-region-active-p))
  17859. (org-call-for-shift-select 'backward-word))
  17860. ((and (not (eq org-support-shift-select 'always))
  17861. (org-at-heading-p))
  17862. (org-call-with-arg 'org-todo 'previousset))
  17863. (org-support-shift-select
  17864. (org-call-for-shift-select 'backward-word))
  17865. (t (org-shiftselect-error))))
  17866. (defun org-shiftcontrolup (&optional n)
  17867. "Change timestamps synchronously up in CLOCK log lines.
  17868. Optional argument N tells to change by that many units."
  17869. (interactive "P")
  17870. (cond ((and (not org-support-shift-select)
  17871. (org-at-clock-log-p)
  17872. (org-at-timestamp-p t))
  17873. (org-clock-timestamps-up n))
  17874. (t (org-shiftselect-error))))
  17875. (defun org-shiftcontroldown (&optional n)
  17876. "Change timestamps synchronously down in CLOCK log lines.
  17877. Optional argument N tells to change by that many units."
  17878. (interactive "P")
  17879. (cond ((and (not org-support-shift-select)
  17880. (org-at-clock-log-p)
  17881. (org-at-timestamp-p t))
  17882. (org-clock-timestamps-down n))
  17883. (t (org-shiftselect-error))))
  17884. (defun org-ctrl-c-ret ()
  17885. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17886. (interactive)
  17887. (cond
  17888. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17889. (t (call-interactively 'org-insert-heading))))
  17890. (defun org-find-visible ()
  17891. (let ((s (point)))
  17892. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17893. (get-char-property s 'invisible)))
  17894. s))
  17895. (defun org-find-invisible ()
  17896. (let ((s (point)))
  17897. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17898. (not (get-char-property s 'invisible))))
  17899. s))
  17900. (defun org-copy-visible (beg end)
  17901. "Copy the visible parts of the region."
  17902. (interactive "r")
  17903. (let (snippets s)
  17904. (save-excursion
  17905. (save-restriction
  17906. (narrow-to-region beg end)
  17907. (setq s (goto-char (point-min)))
  17908. (while (not (= (point) (point-max)))
  17909. (goto-char (org-find-invisible))
  17910. (push (buffer-substring s (point)) snippets)
  17911. (setq s (goto-char (org-find-visible))))))
  17912. (kill-new (apply 'concat (nreverse snippets)))))
  17913. (defun org-copy-special ()
  17914. "Copy region in table or copy current subtree.
  17915. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17916. See the individual commands for more information."
  17917. (interactive)
  17918. (call-interactively
  17919. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17920. (defun org-cut-special ()
  17921. "Cut region in table or cut current subtree.
  17922. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17923. See the individual commands for more information."
  17924. (interactive)
  17925. (call-interactively
  17926. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17927. (defun org-paste-special (arg)
  17928. "Paste rectangular region into table, or past subtree relative to level.
  17929. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17930. See the individual commands for more information."
  17931. (interactive "P")
  17932. (if (org-at-table-p)
  17933. (org-table-paste-rectangle)
  17934. (org-paste-subtree arg)))
  17935. (defsubst org-in-fixed-width-region-p ()
  17936. "Is point in a fixed-width region?"
  17937. (save-match-data
  17938. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17939. (defun org-edit-special (&optional arg)
  17940. "Call a special editor for the element at point.
  17941. When at a table, call the formula editor with `org-table-edit-formulas'.
  17942. When in a source code block, call `org-edit-src-code'.
  17943. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17944. When at an #+INCLUDE keyword, visit the included file.
  17945. On a link, call `ffap' to visit the link at point.
  17946. Otherwise, return a user error."
  17947. (interactive)
  17948. (let ((element (org-element-at-point)))
  17949. (assert (not buffer-read-only) nil
  17950. "Buffer is read-only: %s" (buffer-name))
  17951. (case (org-element-type element)
  17952. (src-block
  17953. (if (not arg) (org-edit-src-code)
  17954. (let* ((info (org-babel-get-src-block-info))
  17955. (lang (nth 0 info))
  17956. (params (nth 2 info))
  17957. (session (cdr (assq :session params))))
  17958. (if (not session) (org-edit-src-code)
  17959. ;; At a src-block with a session and function called with
  17960. ;; an ARG: switch to the buffer related to the inferior
  17961. ;; process.
  17962. (funcall (intern (concat "org-babel-prep-session:" lang))
  17963. session params)))))
  17964. (keyword
  17965. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  17966. (find-file
  17967. (org-remove-double-quotes
  17968. (car (org-split-string (org-element-property :value element)))))
  17969. (user-error "No special environment to edit here")))
  17970. (table
  17971. (if (eq (org-element-property :type element) 'table.el)
  17972. (org-edit-src-code)
  17973. (call-interactively 'org-table-edit-formulas)))
  17974. ;; Only Org tables contain `table-row' type elements.
  17975. (table-row (call-interactively 'org-table-edit-formulas))
  17976. ((example-block export-block) (org-edit-src-code))
  17977. (fixed-width (org-edit-fixed-width-region))
  17978. (otherwise
  17979. ;; No notable element at point. Though, we may be at a link,
  17980. ;; which is an object. Thus, scan deeper.
  17981. (if (eq (org-element-type (org-element-context element)) 'link)
  17982. (call-interactively 'ffap)
  17983. (user-error "No special environment to edit here"))))))
  17984. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17985. (defun org-ctrl-c-ctrl-c (&optional arg)
  17986. "Set tags in headline, or update according to changed information at point.
  17987. This command does many different things, depending on context:
  17988. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17989. this is what we do.
  17990. - If the cursor is on a statistics cookie, update it.
  17991. - If the cursor is in a headline, prompt for tags and insert them
  17992. into the current line, aligned to `org-tags-column'. When called
  17993. with prefix arg, realign all tags in the current buffer.
  17994. - If the cursor is in one of the special #+KEYWORD lines, this
  17995. triggers scanning the buffer for these lines and updating the
  17996. information.
  17997. - If the cursor is inside a table, realign the table. This command
  17998. works even if the automatic table editor has been turned off.
  17999. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18000. the entire table.
  18001. - If the cursor is at a footnote reference or definition, jump to
  18002. the corresponding definition or references, respectively.
  18003. - If the cursor is a the beginning of a dynamic block, update it.
  18004. - If the current buffer is a capture buffer, close note and file it.
  18005. - If the cursor is on a <<<target>>>, update radio targets and
  18006. corresponding links in this buffer.
  18007. - If the cursor is on a numbered item in a plain list, renumber the
  18008. ordered list.
  18009. - If the cursor is on a checkbox, toggle it.
  18010. - If the cursor is on a code block, evaluate it. The variable
  18011. `org-confirm-babel-evaluate' can be used to control prompting
  18012. before code block evaluation, by default every code block
  18013. evaluation requires confirmation. Code block evaluation can be
  18014. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18015. (interactive "P")
  18016. (cond
  18017. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18018. org-occur-highlights
  18019. org-latex-fragment-image-overlays)
  18020. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18021. (org-remove-occur-highlights)
  18022. (org-remove-latex-fragment-image-overlays)
  18023. (message "Temporary highlights/overlays removed from current buffer"))
  18024. ((and (local-variable-p 'org-finish-function (current-buffer))
  18025. (fboundp org-finish-function))
  18026. (funcall org-finish-function))
  18027. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18028. (t
  18029. (let* ((context (org-element-context)) (type (org-element-type context)))
  18030. ;; Test if point is within a blank line.
  18031. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18032. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18033. (user-error "C-c C-c can do nothing useful at this location"))
  18034. ;; For convenience: at the first line of a paragraph on the
  18035. ;; same line as an item, apply function on that item instead.
  18036. (when (eq type 'paragraph)
  18037. (let ((parent (org-element-property :parent context)))
  18038. (when (and (eq (org-element-type parent) 'item)
  18039. (= (point-at-bol) (org-element-property :begin parent)))
  18040. (setq context parent type 'item))))
  18041. ;; Act according to type of element or object at point.
  18042. (case type
  18043. (clock (org-clock-update-time-maybe))
  18044. (dynamic-block
  18045. (save-excursion
  18046. (goto-char (org-element-property :post-affiliated context))
  18047. (org-update-dblock)))
  18048. (footnote-definition
  18049. (goto-char (org-element-property :post-affiliated context))
  18050. (call-interactively 'org-footnote-action))
  18051. (footnote-reference (call-interactively 'org-footnote-action))
  18052. ((headline inlinetask)
  18053. (save-excursion (goto-char (org-element-property :begin context))
  18054. (call-interactively 'org-set-tags)))
  18055. (item
  18056. ;; At an item: a double C-u set checkbox to "[-]"
  18057. ;; unconditionally, whereas a single one will toggle its
  18058. ;; presence. Without an universal argument, if the item
  18059. ;; has a checkbox, toggle it. Otherwise repair the list.
  18060. (let* ((box (org-element-property :checkbox context))
  18061. (struct (org-element-property :structure context))
  18062. (old-struct (copy-tree struct))
  18063. (parents (org-list-parents-alist struct))
  18064. (prevs (org-list-prevs-alist struct))
  18065. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18066. (org-list-set-checkbox
  18067. (org-element-property :begin context) struct
  18068. (cond ((equal arg '(16)) "[-]")
  18069. ((and (not box) (equal arg '(4))) "[ ]")
  18070. ((or (not box) (equal arg '(4))) nil)
  18071. ((eq box 'on) "[ ]")
  18072. (t "[X]")))
  18073. ;; Mimic `org-list-write-struct' but with grabbing
  18074. ;; a return value from `org-list-struct-fix-box'.
  18075. (org-list-struct-fix-ind struct parents 2)
  18076. (org-list-struct-fix-item-end struct)
  18077. (org-list-struct-fix-bul struct prevs)
  18078. (org-list-struct-fix-ind struct parents)
  18079. (let ((block-item
  18080. (org-list-struct-fix-box struct parents prevs orderedp)))
  18081. (if (and box (equal struct old-struct))
  18082. (if (equal arg '(16))
  18083. (message "Checkboxes already reset")
  18084. (user-error "Cannot toggle this checkbox: %s"
  18085. (if (eq box 'on)
  18086. "all subitems checked"
  18087. "unchecked subitems")))
  18088. (org-list-struct-apply-struct struct old-struct)
  18089. (org-update-checkbox-count-maybe))
  18090. (when block-item
  18091. (message "Checkboxes were removed due to empty box at line %d"
  18092. (org-current-line block-item))))))
  18093. (keyword
  18094. (let ((org-inhibit-startup-visibility-stuff t)
  18095. (org-startup-align-all-tables nil))
  18096. (when (boundp 'org-table-coordinate-overlays)
  18097. (mapc 'delete-overlay org-table-coordinate-overlays)
  18098. (setq org-table-coordinate-overlays nil))
  18099. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18100. (message "Local setup has been refreshed"))
  18101. (plain-list
  18102. ;; At a plain list, with a double C-u argument, set
  18103. ;; checkboxes of each item to "[-]", whereas a single one
  18104. ;; will toggle their presence according to the state of the
  18105. ;; first item in the list. Without an argument, repair the
  18106. ;; list.
  18107. (let* ((begin (org-element-property :contents-begin context))
  18108. (struct (org-element-property :structure context))
  18109. (old-struct (copy-tree struct))
  18110. (first-box (save-excursion
  18111. (goto-char begin)
  18112. (looking-at org-list-full-item-re)
  18113. (match-string-no-properties 3)))
  18114. (new-box (cond ((equal arg '(16)) "[-]")
  18115. ((equal arg '(4)) (unless first-box "[ ]"))
  18116. ((equal first-box "[X]") "[ ]")
  18117. (t "[X]"))))
  18118. (cond
  18119. (arg
  18120. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18121. (org-list-get-all-items
  18122. begin struct (org-list-prevs-alist struct))))
  18123. ((and first-box (eq (point) begin))
  18124. ;; For convenience, when point is at bol on the first
  18125. ;; item of the list and no argument is provided, simply
  18126. ;; toggle checkbox of that item, if any.
  18127. (org-list-set-checkbox begin struct new-box)))
  18128. (org-list-write-struct
  18129. struct (org-list-parents-alist struct) old-struct)
  18130. (org-update-checkbox-count-maybe)
  18131. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18132. ((property-drawer node-property)
  18133. (call-interactively 'org-property-action))
  18134. ((radio-target target)
  18135. (call-interactively 'org-update-radio-target-regexp))
  18136. (statistics-cookie
  18137. (call-interactively 'org-update-statistics-cookies))
  18138. ((table table-cell table-row)
  18139. ;; At a table, recalculate every field and align it. Also
  18140. ;; send the table if necessary. If the table has
  18141. ;; a `table.el' type, just give up. At a table row or
  18142. ;; cell, maybe recalculate line but always align table.
  18143. (if (eq (org-element-property :type context) 'table.el)
  18144. (message "Use C-c ' to edit table.el tables")
  18145. (let ((org-enable-table-editor t))
  18146. (if (or (eq type 'table)
  18147. ;; Check if point is at a TBLFM line.
  18148. (and (eq type 'table-row)
  18149. (= (point) (org-element-property :end context))))
  18150. (save-excursion
  18151. (goto-char (org-element-property :contents-begin context))
  18152. (org-call-with-arg 'org-table-recalculate (or arg t))
  18153. (orgtbl-send-table 'maybe))
  18154. (org-table-maybe-eval-formula)
  18155. (cond (arg (call-interactively 'org-table-recalculate))
  18156. ((org-table-maybe-recalculate-line))
  18157. (t (org-table-align)))))))
  18158. (timestamp (org-timestamp-change 0 'day))
  18159. (otherwise
  18160. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18161. (user-error
  18162. "C-c C-c can do nothing useful at this location")))))))))
  18163. (defun org-mode-restart ()
  18164. "Restart Org-mode, to scan again for special lines.
  18165. Also updates the keyword regular expressions."
  18166. (interactive)
  18167. (org-mode)
  18168. (message "Org-mode restarted"))
  18169. (defun org-kill-note-or-show-branches ()
  18170. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18171. (interactive)
  18172. (if (not org-finish-function)
  18173. (progn
  18174. (hide-subtree)
  18175. (call-interactively 'show-branches))
  18176. (let ((org-note-abort t))
  18177. (funcall org-finish-function))))
  18178. (defun org-return (&optional indent)
  18179. "Goto next table row or insert a newline.
  18180. Calls `org-table-next-row' or `newline', depending on context.
  18181. See the individual commands for more information."
  18182. (interactive)
  18183. (let (org-ts-what)
  18184. (cond
  18185. ((or (bobp) (org-in-src-block-p))
  18186. (if indent (newline-and-indent) (newline)))
  18187. ((org-at-table-p)
  18188. (org-table-justify-field-maybe)
  18189. (call-interactively 'org-table-next-row))
  18190. ;; when `newline-and-indent' is called within a list, make sure
  18191. ;; text moved stays inside the item.
  18192. ((and (org-in-item-p) indent)
  18193. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18194. (progn
  18195. (save-match-data (newline))
  18196. (org-indent-line-to (length (match-string 0))))
  18197. (let ((ind (org-get-indentation)))
  18198. (newline)
  18199. (if (org-looking-back org-list-end-re)
  18200. (org-indent-line)
  18201. (org-indent-line-to ind)))))
  18202. ((and org-return-follows-link
  18203. (org-at-timestamp-p t)
  18204. (not (eq org-ts-what 'after)))
  18205. (org-follow-timestamp-link))
  18206. ((and org-return-follows-link
  18207. (let ((tprop (get-text-property (point) 'face)))
  18208. (or (eq tprop 'org-link)
  18209. (and (listp tprop) (memq 'org-link tprop)))))
  18210. (call-interactively 'org-open-at-point))
  18211. ((and (org-at-heading-p)
  18212. (looking-at
  18213. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18214. (org-show-entry)
  18215. (end-of-line 1)
  18216. (newline))
  18217. (t (if indent (newline-and-indent) (newline))))))
  18218. (defun org-return-indent ()
  18219. "Goto next table row or insert a newline and indent.
  18220. Calls `org-table-next-row' or `newline-and-indent', depending on
  18221. context. See the individual commands for more information."
  18222. (interactive)
  18223. (org-return t))
  18224. (defun org-ctrl-c-star ()
  18225. "Compute table, or change heading status of lines.
  18226. Calls `org-table-recalculate' or `org-toggle-heading',
  18227. depending on context."
  18228. (interactive)
  18229. (cond
  18230. ((org-at-table-p)
  18231. (call-interactively 'org-table-recalculate))
  18232. (t
  18233. ;; Convert all lines in region to list items
  18234. (call-interactively 'org-toggle-heading))))
  18235. (defun org-ctrl-c-minus ()
  18236. "Insert separator line in table or modify bullet status of line.
  18237. Also turns a plain line or a region of lines into list items.
  18238. Calls `org-table-insert-hline', `org-toggle-item', or
  18239. `org-cycle-list-bullet', depending on context."
  18240. (interactive)
  18241. (cond
  18242. ((org-at-table-p)
  18243. (call-interactively 'org-table-insert-hline))
  18244. ((org-region-active-p)
  18245. (call-interactively 'org-toggle-item))
  18246. ((org-in-item-p)
  18247. (call-interactively 'org-cycle-list-bullet))
  18248. (t
  18249. (call-interactively 'org-toggle-item))))
  18250. (defun org-toggle-item (arg)
  18251. "Convert headings or normal lines to items, items to normal lines.
  18252. If there is no active region, only the current line is considered.
  18253. If the first non blank line in the region is an headline, convert
  18254. all headlines to items, shifting text accordingly.
  18255. If it is an item, convert all items to normal lines.
  18256. If it is normal text, change region into an item. With a prefix
  18257. argument ARG, change each line in region into an item."
  18258. (interactive "P")
  18259. (let ((shift-text
  18260. (function
  18261. ;; Shift text in current section to IND, from point to END.
  18262. ;; The function leaves point to END line.
  18263. (lambda (ind end)
  18264. (let ((min-i 1000) (end (copy-marker end)))
  18265. ;; First determine the minimum indentation (MIN-I) of
  18266. ;; the text.
  18267. (save-excursion
  18268. (catch 'exit
  18269. (while (< (point) end)
  18270. (let ((i (org-get-indentation)))
  18271. (cond
  18272. ;; Skip blank lines and inline tasks.
  18273. ((looking-at "^[ \t]*$"))
  18274. ((looking-at org-outline-regexp-bol))
  18275. ;; We can't find less than 0 indentation.
  18276. ((zerop i) (throw 'exit (setq min-i 0)))
  18277. ((< i min-i) (setq min-i i))))
  18278. (forward-line))))
  18279. ;; Then indent each line so that a line indented to
  18280. ;; MIN-I becomes indented to IND. Ignore blank lines
  18281. ;; and inline tasks in the process.
  18282. (let ((delta (- ind min-i)))
  18283. (while (< (point) end)
  18284. (unless (or (looking-at "^[ \t]*$")
  18285. (looking-at org-outline-regexp-bol))
  18286. (org-indent-line-to (+ (org-get-indentation) delta)))
  18287. (forward-line)))))))
  18288. (skip-blanks
  18289. (function
  18290. ;; Return beginning of first non-blank line, starting from
  18291. ;; line at POS.
  18292. (lambda (pos)
  18293. (save-excursion
  18294. (goto-char pos)
  18295. (skip-chars-forward " \r\t\n")
  18296. (point-at-bol)))))
  18297. beg end)
  18298. ;; Determine boundaries of changes.
  18299. (if (org-region-active-p)
  18300. (setq beg (funcall skip-blanks (region-beginning))
  18301. end (copy-marker (region-end)))
  18302. (setq beg (funcall skip-blanks (point-at-bol))
  18303. end (copy-marker (point-at-eol))))
  18304. ;; Depending on the starting line, choose an action on the text
  18305. ;; between BEG and END.
  18306. (org-with-limited-levels
  18307. (save-excursion
  18308. (goto-char beg)
  18309. (cond
  18310. ;; Case 1. Start at an item: de-itemize. Note that it only
  18311. ;; happens when a region is active: `org-ctrl-c-minus'
  18312. ;; would call `org-cycle-list-bullet' otherwise.
  18313. ((org-at-item-p)
  18314. (while (< (point) end)
  18315. (when (org-at-item-p)
  18316. (skip-chars-forward " \t")
  18317. (delete-region (point) (match-end 0)))
  18318. (forward-line)))
  18319. ;; Case 2. Start at an heading: convert to items.
  18320. ((org-at-heading-p)
  18321. (let* ((bul (org-list-bullet-string "-"))
  18322. (bul-len (length bul))
  18323. ;; Indentation of the first heading. It should be
  18324. ;; relative to the indentation of its parent, if any.
  18325. (start-ind (save-excursion
  18326. (cond
  18327. ((not org-adapt-indentation) 0)
  18328. ((not (outline-previous-heading)) 0)
  18329. (t (length (match-string 0))))))
  18330. ;; Level of first heading. Further headings will be
  18331. ;; compared to it to determine hierarchy in the list.
  18332. (ref-level (org-reduced-level (org-outline-level))))
  18333. (while (< (point) end)
  18334. (let* ((level (org-reduced-level (org-outline-level)))
  18335. (delta (max 0 (- level ref-level))))
  18336. ;; If current headline is less indented than the first
  18337. ;; one, set it as reference, in order to preserve
  18338. ;; subtrees.
  18339. (when (< level ref-level) (setq ref-level level))
  18340. (replace-match bul t t)
  18341. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18342. ;; Ensure all text down to END (or SECTION-END) belongs
  18343. ;; to the newly created item.
  18344. (let ((section-end (save-excursion
  18345. (or (outline-next-heading) (point)))))
  18346. (forward-line)
  18347. (funcall shift-text
  18348. (+ start-ind (* (1+ delta) bul-len))
  18349. (min end section-end)))))))
  18350. ;; Case 3. Normal line with ARG: turn each non-item line into
  18351. ;; an item.
  18352. (arg
  18353. (while (< (point) end)
  18354. (unless (or (org-at-heading-p) (org-at-item-p))
  18355. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18356. (replace-match
  18357. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18358. (forward-line)))
  18359. ;; Case 4. Normal line without ARG: make the first line of
  18360. ;; region an item, and shift indentation of others
  18361. ;; lines to set them as item's body.
  18362. (t (let* ((bul (org-list-bullet-string "-"))
  18363. (bul-len (length bul))
  18364. (ref-ind (org-get-indentation)))
  18365. (skip-chars-forward " \t")
  18366. (insert bul)
  18367. (forward-line)
  18368. (while (< (point) end)
  18369. ;; Ensure that lines less indented than first one
  18370. ;; still get included in item body.
  18371. (funcall shift-text
  18372. (+ ref-ind bul-len)
  18373. (min end (save-excursion (or (outline-next-heading)
  18374. (point)))))
  18375. (forward-line)))))))))
  18376. (defun org-toggle-heading (&optional nstars)
  18377. "Convert headings to normal text, or items or text to headings.
  18378. If there is no active region, only the current line is considered.
  18379. With a \\[universal-argument] prefix, convert the whole list at
  18380. point into heading.
  18381. In a region:
  18382. - If the first non blank line is an headline, remove the stars
  18383. from all headlines in the region.
  18384. - If it is a normal line turn each and every normal line (i.e. not an
  18385. heading or an item) in the region into a heading.
  18386. - If it is a plain list item, turn all plain list items into headings.
  18387. When converting a line into a heading, the number of stars is chosen
  18388. such that the lines become children of the current entry. However,
  18389. when a prefix argument is given, its value determines the number of
  18390. stars to add."
  18391. (interactive "P")
  18392. (let ((skip-blanks
  18393. (function
  18394. ;; Return beginning of first non-blank line, starting from
  18395. ;; line at POS.
  18396. (lambda (pos)
  18397. (save-excursion
  18398. (goto-char pos)
  18399. (while (org-at-comment-p) (forward-line))
  18400. (skip-chars-forward " \r\t\n")
  18401. (point-at-bol)))))
  18402. beg end toggled)
  18403. ;; Determine boundaries of changes. If a universal prefix has
  18404. ;; been given, put the list in a region. If region ends at a bol,
  18405. ;; do not consider the last line to be in the region.
  18406. (when (and current-prefix-arg (org-at-item-p))
  18407. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18408. (org-mark-element))
  18409. (if (org-region-active-p)
  18410. (setq beg (funcall skip-blanks (region-beginning))
  18411. end (copy-marker (save-excursion
  18412. (goto-char (region-end))
  18413. (if (bolp) (point) (point-at-eol)))))
  18414. (setq beg (funcall skip-blanks (point-at-bol))
  18415. end (copy-marker (point-at-eol))))
  18416. ;; Ensure inline tasks don't count as headings.
  18417. (org-with-limited-levels
  18418. (save-excursion
  18419. (goto-char beg)
  18420. (cond
  18421. ;; Case 1. Started at an heading: de-star headings.
  18422. ((org-at-heading-p)
  18423. (while (< (point) end)
  18424. (when (org-at-heading-p t)
  18425. (looking-at org-outline-regexp) (replace-match "")
  18426. (setq toggled t))
  18427. (forward-line)))
  18428. ;; Case 2. Started at an item: change items into headlines.
  18429. ;; One star will be added by `org-list-to-subtree'.
  18430. ((org-at-item-p)
  18431. (let* ((stars (make-string
  18432. (if nstars
  18433. ;; subtract the star that will be added again by
  18434. ;; `org-list-to-subtree'
  18435. (1- (prefix-numeric-value current-prefix-arg))
  18436. (or (org-current-level) 0))
  18437. ?*))
  18438. (add-stars
  18439. (cond (nstars "") ; stars from prefix only
  18440. ((equal stars "") "") ; before first heading
  18441. (org-odd-levels-only "*") ; inside heading, odd
  18442. (t "")))) ; inside heading, oddeven
  18443. (while (< (point) end)
  18444. (when (org-at-item-p)
  18445. ;; Pay attention to cases when region ends before list.
  18446. (let* ((struct (org-list-struct))
  18447. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18448. (save-restriction
  18449. (narrow-to-region (point) list-end)
  18450. (insert
  18451. (org-list-to-subtree
  18452. (org-list-parse-list t)
  18453. '(:istart (concat stars add-stars (funcall get-stars depth))
  18454. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18455. (setq toggled t))
  18456. (forward-line))))
  18457. ;; Case 3. Started at normal text: make every line an heading,
  18458. ;; skipping headlines and items.
  18459. (t (let* ((stars (make-string
  18460. (if nstars
  18461. (prefix-numeric-value current-prefix-arg)
  18462. (or (org-current-level) 0))
  18463. ?*))
  18464. (add-stars
  18465. (cond (nstars "") ; stars from prefix only
  18466. ((equal stars "") "*") ; before first heading
  18467. (org-odd-levels-only "**") ; inside heading, odd
  18468. (t "*"))) ; inside heading, oddeven
  18469. (rpl (concat stars add-stars " ")))
  18470. (while (< (point) end)
  18471. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18472. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18473. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18474. (forward-line)))))))
  18475. (unless toggled (message "Cannot toggle heading from here"))))
  18476. (defun org-meta-return (&optional arg)
  18477. "Insert a new heading or wrap a region in a table.
  18478. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18479. See the individual commands for more information."
  18480. (interactive "P")
  18481. (cond
  18482. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18483. ((or (org-at-drawer-p) (org-at-property-p))
  18484. (newline-and-indent))
  18485. ((org-at-table-p)
  18486. (call-interactively 'org-table-wrap-region))
  18487. (t (call-interactively 'org-insert-heading))))
  18488. ;;; Menu entries
  18489. (defsubst org-in-subtree-not-table-p ()
  18490. "Are we in a subtree and not in a table?"
  18491. (and (not (org-before-first-heading-p))
  18492. (not (org-at-table-p))))
  18493. ;; Define the Org-mode menus
  18494. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18495. '("Tbl"
  18496. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18497. ["Next Field" org-cycle (org-at-table-p)]
  18498. ["Previous Field" org-shifttab (org-at-table-p)]
  18499. ["Next Row" org-return (org-at-table-p)]
  18500. "--"
  18501. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18502. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18503. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18504. "--"
  18505. ("Column"
  18506. ["Move Column Left" org-metaleft (org-at-table-p)]
  18507. ["Move Column Right" org-metaright (org-at-table-p)]
  18508. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18509. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18510. ("Row"
  18511. ["Move Row Up" org-metaup (org-at-table-p)]
  18512. ["Move Row Down" org-metadown (org-at-table-p)]
  18513. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18514. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18515. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18516. "--"
  18517. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18518. ("Rectangle"
  18519. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18520. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18521. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18522. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18523. "--"
  18524. ("Calculate"
  18525. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18526. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18527. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18528. "--"
  18529. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18530. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18531. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18532. "--"
  18533. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18534. "--"
  18535. ["Sum Column/Rectangle" org-table-sum
  18536. (or (org-at-table-p) (org-region-active-p))]
  18537. ["Which Column?" org-table-current-column (org-at-table-p)])
  18538. ["Debug Formulas"
  18539. org-table-toggle-formula-debugger
  18540. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18541. ["Show Col/Row Numbers"
  18542. org-table-toggle-coordinate-overlays
  18543. :style toggle
  18544. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18545. "--"
  18546. ["Create" org-table-create (and (not (org-at-table-p))
  18547. org-enable-table-editor)]
  18548. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18549. ["Import from File" org-table-import (not (org-at-table-p))]
  18550. ["Export to File" org-table-export (org-at-table-p)]
  18551. "--"
  18552. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18553. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18554. '("Org"
  18555. ("Show/Hide"
  18556. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18557. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18558. ["Sparse Tree..." org-sparse-tree t]
  18559. ["Reveal Context" org-reveal t]
  18560. ["Show All" show-all t]
  18561. "--"
  18562. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18563. "--"
  18564. ["New Heading" org-insert-heading t]
  18565. ("Navigate Headings"
  18566. ["Up" outline-up-heading t]
  18567. ["Next" outline-next-visible-heading t]
  18568. ["Previous" outline-previous-visible-heading t]
  18569. ["Next Same Level" outline-forward-same-level t]
  18570. ["Previous Same Level" outline-backward-same-level t]
  18571. "--"
  18572. ["Jump" org-goto t])
  18573. ("Edit Structure"
  18574. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18575. "--"
  18576. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18577. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18578. "--"
  18579. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18580. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18581. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18582. "--"
  18583. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18584. "--"
  18585. ["Copy visible text" org-copy-visible t]
  18586. "--"
  18587. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18588. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18589. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18590. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18591. "--"
  18592. ["Sort Region/Children" org-sort t]
  18593. "--"
  18594. ["Convert to odd levels" org-convert-to-odd-levels t]
  18595. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18596. ("Editing"
  18597. ["Emphasis..." org-emphasize t]
  18598. ["Edit Source Example" org-edit-special t]
  18599. "--"
  18600. ["Footnote new/jump" org-footnote-action t]
  18601. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18602. ("Archive"
  18603. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18604. "--"
  18605. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18606. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18607. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18608. )
  18609. "--"
  18610. ("Hyperlinks"
  18611. ["Store Link (Global)" org-store-link t]
  18612. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18613. ["Insert Link" org-insert-link t]
  18614. ["Follow Link" org-open-at-point t]
  18615. "--"
  18616. ["Next link" org-next-link t]
  18617. ["Previous link" org-previous-link t]
  18618. "--"
  18619. ["Descriptive Links"
  18620. org-toggle-link-display
  18621. :style radio
  18622. :selected org-descriptive-links
  18623. ]
  18624. ["Literal Links"
  18625. org-toggle-link-display
  18626. :style radio
  18627. :selected (not org-descriptive-links)])
  18628. "--"
  18629. ("TODO Lists"
  18630. ["TODO/DONE/-" org-todo t]
  18631. ("Select keyword"
  18632. ["Next keyword" org-shiftright (org-at-heading-p)]
  18633. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18634. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18635. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18636. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18637. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18638. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18639. "--"
  18640. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18641. :selected org-enforce-todo-dependencies :style toggle :active t]
  18642. "Settings for tree at point"
  18643. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18644. :selected (org-entry-get nil "ORDERED")
  18645. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18646. ["Do Children parallel" org-toggle-ordered-property :style radio
  18647. :selected (not (org-entry-get nil "ORDERED"))
  18648. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18649. "--"
  18650. ["Set Priority" org-priority t]
  18651. ["Priority Up" org-shiftup t]
  18652. ["Priority Down" org-shiftdown t]
  18653. "--"
  18654. ["Get news from all feeds" org-feed-update-all t]
  18655. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18656. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18657. ("TAGS and Properties"
  18658. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18659. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18660. "--"
  18661. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18662. ["Column view of properties" org-columns t]
  18663. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18664. ("Dates and Scheduling"
  18665. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18666. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18667. ("Change Date"
  18668. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18669. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18670. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18671. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18672. ["Compute Time Range" org-evaluate-time-range t]
  18673. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18674. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18675. "--"
  18676. ["Custom time format" org-toggle-time-stamp-overlays
  18677. :style radio :selected org-display-custom-times]
  18678. "--"
  18679. ["Goto Calendar" org-goto-calendar t]
  18680. ["Date from Calendar" org-date-from-calendar t]
  18681. "--"
  18682. ["Start/Restart Timer" org-timer-start t]
  18683. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18684. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18685. ["Insert Timer String" org-timer t]
  18686. ["Insert Timer Item" org-timer-item t])
  18687. ("Logging work"
  18688. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18689. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18690. ["Clock out" org-clock-out t]
  18691. ["Clock cancel" org-clock-cancel t]
  18692. "--"
  18693. ["Mark as default task" org-clock-mark-default-task t]
  18694. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18695. ["Goto running clock" org-clock-goto t]
  18696. "--"
  18697. ["Display times" org-clock-display t]
  18698. ["Create clock table" org-clock-report t]
  18699. "--"
  18700. ["Record DONE time"
  18701. (progn (setq org-log-done (not org-log-done))
  18702. (message "Switching to %s will %s record a timestamp"
  18703. (car org-done-keywords)
  18704. (if org-log-done "automatically" "not")))
  18705. :style toggle :selected org-log-done])
  18706. "--"
  18707. ["Agenda Command..." org-agenda t]
  18708. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18709. ("File List for Agenda")
  18710. ("Special views current file"
  18711. ["TODO Tree" org-show-todo-tree t]
  18712. ["Check Deadlines" org-check-deadlines t]
  18713. ["Timeline" org-timeline t]
  18714. ["Tags/Property tree" org-match-sparse-tree t])
  18715. "--"
  18716. ["Export/Publish..." org-export-dispatch t]
  18717. ("LaTeX"
  18718. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18719. :selected org-cdlatex-mode]
  18720. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18721. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18722. ["Modify math symbol" org-cdlatex-math-modify
  18723. (org-inside-LaTeX-fragment-p)]
  18724. ["Insert citation" org-reftex-citation t]
  18725. "--"
  18726. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18727. "--"
  18728. ("MobileOrg"
  18729. ["Push Files and Views" org-mobile-push t]
  18730. ["Get Captured and Flagged" org-mobile-pull t]
  18731. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18732. "--"
  18733. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18734. "--"
  18735. ("Documentation"
  18736. ["Show Version" org-version t]
  18737. ["Info Documentation" org-info t])
  18738. ("Customize"
  18739. ["Browse Org Group" org-customize t]
  18740. "--"
  18741. ["Expand This Menu" org-create-customize-menu
  18742. (fboundp 'customize-menu-create)])
  18743. ["Send bug report" org-submit-bug-report t]
  18744. "--"
  18745. ("Refresh/Reload"
  18746. ["Refresh setup current buffer" org-mode-restart t]
  18747. ["Reload Org (after update)" org-reload t]
  18748. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18749. ))
  18750. (defun org-info (&optional node)
  18751. "Read documentation for Org-mode in the info system.
  18752. With optional NODE, go directly to that node."
  18753. (interactive)
  18754. (info (format "(org)%s" (or node ""))))
  18755. ;;;###autoload
  18756. (defun org-submit-bug-report ()
  18757. "Submit a bug report on Org-mode via mail.
  18758. Don't hesitate to report any problems or inaccurate documentation.
  18759. If you don't have setup sending mail from (X)Emacs, please copy the
  18760. output buffer into your mail program, as it gives us important
  18761. information about your Org-mode version and configuration."
  18762. (interactive)
  18763. (require 'reporter)
  18764. (org-load-modules-maybe)
  18765. (org-require-autoloaded-modules)
  18766. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18767. (reporter-submit-bug-report
  18768. "emacs-orgmode@gnu.org"
  18769. (org-version nil 'full)
  18770. (let (list)
  18771. (save-window-excursion
  18772. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18773. (delete-other-windows)
  18774. (erase-buffer)
  18775. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18776. We would like to add your full Org-mode and Outline configuration to the
  18777. bug report. This greatly simplifies the work of the maintainer and
  18778. other experts on the mailing list.
  18779. HOWEVER, some variables you have customized may contain private
  18780. information. The names of customers, colleagues, or friends, might
  18781. appear in the form of file names, tags, todo states, or search strings.
  18782. If you answer yes to the prompt, you might want to check and remove
  18783. such private information before sending the email.")
  18784. (add-text-properties (point-min) (point-max) '(face org-warning))
  18785. (when (yes-or-no-p "Include your Org-mode configuration ")
  18786. (mapatoms
  18787. (lambda (v)
  18788. (and (boundp v)
  18789. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18790. (or (and (symbol-value v)
  18791. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18792. (and
  18793. (get v 'custom-type) (get v 'standard-value)
  18794. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18795. (push v list)))))
  18796. (kill-buffer (get-buffer "*Warn about privacy*"))
  18797. list))
  18798. nil nil
  18799. "Remember to cover the basics, that is, what you expected to happen and
  18800. what in fact did happen. You don't know how to make a good report? See
  18801. http://orgmode.org/manual/Feedback.html#Feedback
  18802. Your bug report will be posted to the Org-mode mailing list.
  18803. ------------------------------------------------------------------------")
  18804. (save-excursion
  18805. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18806. (replace-match "\\1Bug: \\3 [\\2]")))))
  18807. (defun org-install-agenda-files-menu ()
  18808. (let ((bl (buffer-list)))
  18809. (save-excursion
  18810. (while bl
  18811. (set-buffer (pop bl))
  18812. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18813. (when (derived-mode-p 'org-mode)
  18814. (easy-menu-change
  18815. '("Org") "File List for Agenda"
  18816. (append
  18817. (list
  18818. ["Edit File List" (org-edit-agenda-file-list) t]
  18819. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18820. ["Remove Current File from List" org-remove-file t]
  18821. ["Cycle through agenda files" org-cycle-agenda-files t]
  18822. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18823. "--")
  18824. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18825. ;;;; Documentation
  18826. (defun org-require-autoloaded-modules ()
  18827. (interactive)
  18828. (mapc 'require
  18829. '(org-agenda org-archive org-attach org-clock org-colview org-id
  18830. org-remember org-table org-timer)))
  18831. ;;;###autoload
  18832. (defun org-reload (&optional uncompiled)
  18833. "Reload all org lisp files.
  18834. With prefix arg UNCOMPILED, load the uncompiled versions."
  18835. (interactive "P")
  18836. (require 'loadhist)
  18837. (let* ((org-dir (org-find-library-dir "org"))
  18838. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18839. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  18840. (remove-re (mapconcat 'identity
  18841. (mapcar (lambda (f) (concat "^" f "$"))
  18842. (list (if (featurep 'xemacs)
  18843. "org-colview"
  18844. "org-colview-xemacs")
  18845. "org" "org-loaddefs" "org-version"))
  18846. "\\|"))
  18847. (feats (delete-dups
  18848. (mapcar 'file-name-sans-extension
  18849. (mapcar 'file-name-nondirectory
  18850. (delq nil
  18851. (mapcar 'feature-file
  18852. features))))))
  18853. (lfeat (append
  18854. (sort
  18855. (setq feats
  18856. (delq nil (mapcar
  18857. (lambda (f)
  18858. (if (and (string-match feature-re f)
  18859. (not (string-match remove-re f)))
  18860. f nil))
  18861. feats)))
  18862. 'string-lessp)
  18863. (list "org-version" "org")))
  18864. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18865. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18866. load-uncore load-misses)
  18867. (setq load-misses
  18868. (delq 't
  18869. (mapcar (lambda (f)
  18870. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18871. (and (string= org-dir contrib-dir)
  18872. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18873. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18874. (add-to-list 'load-uncore f 'append)
  18875. 't)
  18876. f))
  18877. lfeat)))
  18878. (if load-uncore
  18879. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18880. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18881. (if load-misses
  18882. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18883. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18884. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18885. ;;;###autoload
  18886. (defun org-customize ()
  18887. "Call the customize function with org as argument."
  18888. (interactive)
  18889. (org-load-modules-maybe)
  18890. (org-require-autoloaded-modules)
  18891. (customize-browse 'org))
  18892. (defun org-create-customize-menu ()
  18893. "Create a full customization menu for Org-mode, insert it into the menu."
  18894. (interactive)
  18895. (org-load-modules-maybe)
  18896. (org-require-autoloaded-modules)
  18897. (if (fboundp 'customize-menu-create)
  18898. (progn
  18899. (easy-menu-change
  18900. '("Org") "Customize"
  18901. `(["Browse Org group" org-customize t]
  18902. "--"
  18903. ,(customize-menu-create 'org)
  18904. ["Set" Custom-set t]
  18905. ["Save" Custom-save t]
  18906. ["Reset to Current" Custom-reset-current t]
  18907. ["Reset to Saved" Custom-reset-saved t]
  18908. ["Reset to Standard Settings" Custom-reset-standard t]))
  18909. (message "\"Org\"-menu now contains full customization menu"))
  18910. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18911. ;;;; Miscellaneous stuff
  18912. ;;; Generally useful functions
  18913. (defun org-get-at-bol (property)
  18914. "Get text property PROPERTY at beginning of line."
  18915. (get-text-property (point-at-bol) property))
  18916. (defun org-find-text-property-in-string (prop s)
  18917. "Return the first non-nil value of property PROP in string S."
  18918. (or (get-text-property 0 prop s)
  18919. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18920. prop s)))
  18921. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18922. "Display the given MESSAGE as a warning."
  18923. (if (fboundp 'display-warning)
  18924. (display-warning 'org message
  18925. (if (featurep 'xemacs) 'warning :warning))
  18926. (let ((buf (get-buffer-create "*Org warnings*")))
  18927. (with-current-buffer buf
  18928. (goto-char (point-max))
  18929. (insert "Warning (Org): " message)
  18930. (unless (bolp)
  18931. (newline)))
  18932. (display-buffer buf)
  18933. (sit-for 0))))
  18934. (defun org-eval (form)
  18935. "Eval FORM and return result."
  18936. (condition-case error
  18937. (eval form)
  18938. (error (format "%%![Error: %s]" error))))
  18939. (defun org-in-clocktable-p ()
  18940. "Check if the cursor is in a clocktable."
  18941. (let ((pos (point)) start)
  18942. (save-excursion
  18943. (end-of-line 1)
  18944. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18945. (setq start (match-beginning 0))
  18946. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18947. (>= (match-end 0) pos)
  18948. start))))
  18949. (defun org-in-commented-line ()
  18950. "Is point in a line starting with `#'?"
  18951. (equal (char-after (point-at-bol)) ?#))
  18952. (defun org-in-indented-comment-line ()
  18953. "Is point in a line starting with `#' after some white space?"
  18954. (save-excursion
  18955. (save-match-data
  18956. (goto-char (point-at-bol))
  18957. (looking-at "[ \t]*#"))))
  18958. (defun org-in-verbatim-emphasis ()
  18959. (save-match-data
  18960. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18961. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18962. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18963. (if (and marker (marker-buffer marker)
  18964. (buffer-live-p (marker-buffer marker)))
  18965. (progn
  18966. (org-pop-to-buffer-same-window (marker-buffer marker))
  18967. (if (or (> marker (point-max)) (< marker (point-min)))
  18968. (widen))
  18969. (goto-char marker)
  18970. (org-show-context 'org-goto))
  18971. (if bookmark
  18972. (bookmark-jump bookmark)
  18973. (error "Cannot find location"))))
  18974. (defun org-quote-csv-field (s)
  18975. "Quote field for inclusion in CSV material."
  18976. (if (string-match "[\",]" s)
  18977. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18978. s))
  18979. (defun org-force-self-insert (N)
  18980. "Needed to enforce self-insert under remapping."
  18981. (interactive "p")
  18982. (self-insert-command N))
  18983. (defun org-string-width (s)
  18984. "Compute width of string, ignoring invisible characters.
  18985. This ignores character with invisibility property `org-link', and also
  18986. characters with property `org-cwidth', because these will become invisible
  18987. upon the next fontification round."
  18988. (let (b l)
  18989. (when (or (eq t buffer-invisibility-spec)
  18990. (assq 'org-link buffer-invisibility-spec))
  18991. (while (setq b (text-property-any 0 (length s)
  18992. 'invisible 'org-link s))
  18993. (setq s (concat (substring s 0 b)
  18994. (substring s (or (next-single-property-change
  18995. b 'invisible s) (length s)))))))
  18996. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18997. (setq s (concat (substring s 0 b)
  18998. (substring s (or (next-single-property-change
  18999. b 'org-cwidth s) (length s))))))
  19000. (setq l (string-width s) b -1)
  19001. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19002. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19003. l))
  19004. (defun org-shorten-string (s maxlength)
  19005. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19006. If the string is shorter or has length MAXLENGTH, just return the
  19007. original string. If it is longer, the functions finds a space in the
  19008. string, breaks this string off at that locations and adds three dots
  19009. as ellipsis. Including the ellipsis, the string will not be longer
  19010. than MAXLENGTH. If finding a good breaking point in the string does
  19011. not work, the string is just chopped off in the middle of a word
  19012. if necessary."
  19013. (if (<= (length s) maxlength)
  19014. s
  19015. (let* ((n (max (- maxlength 4) 1))
  19016. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19017. (if (string-match re s)
  19018. (concat (match-string 1 s) "...")
  19019. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19020. (defun org-get-indentation (&optional line)
  19021. "Get the indentation of the current line, interpreting tabs.
  19022. When LINE is given, assume it represents a line and compute its indentation."
  19023. (if line
  19024. (if (string-match "^ *" (org-remove-tabs line))
  19025. (match-end 0))
  19026. (save-excursion
  19027. (beginning-of-line 1)
  19028. (skip-chars-forward " \t")
  19029. (current-column))))
  19030. (defun org-get-string-indentation (s)
  19031. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19032. (let ((n -1) (i 0) (w tab-width) c)
  19033. (catch 'exit
  19034. (while (< (setq n (1+ n)) (length s))
  19035. (setq c (aref s n))
  19036. (cond ((= c ?\ ) (setq i (1+ i)))
  19037. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19038. (t (throw 'exit t)))))
  19039. i))
  19040. (defun org-remove-tabs (s &optional width)
  19041. "Replace tabulators in S with spaces.
  19042. Assumes that s is a single line, starting in column 0."
  19043. (setq width (or width tab-width))
  19044. (while (string-match "\t" s)
  19045. (setq s (replace-match
  19046. (make-string
  19047. (- (* width (/ (+ (match-beginning 0) width) width))
  19048. (match-beginning 0)) ?\ )
  19049. t t s)))
  19050. s)
  19051. (defun org-fix-indentation (line ind)
  19052. "Fix indentation in LINE.
  19053. IND is a cons cell with target and minimum indentation.
  19054. If the current indentation in LINE is smaller than the minimum,
  19055. leave it alone. If it is larger than ind, set it to the target."
  19056. (let* ((l (org-remove-tabs line))
  19057. (i (org-get-indentation l))
  19058. (i1 (car ind)) (i2 (cdr ind)))
  19059. (if (>= i i2) (setq l (substring line i2)))
  19060. (if (> i1 0)
  19061. (concat (make-string i1 ?\ ) l)
  19062. l)))
  19063. (defun org-remove-indentation (code &optional n)
  19064. "Remove the maximum common indentation from the lines in CODE.
  19065. N may optionally be the number of spaces to remove."
  19066. (with-temp-buffer
  19067. (insert code)
  19068. (org-do-remove-indentation n)
  19069. (buffer-string)))
  19070. (defun org-do-remove-indentation (&optional n)
  19071. "Remove the maximum common indentation from the buffer."
  19072. (untabify (point-min) (point-max))
  19073. (let ((min 10000) re)
  19074. (if n
  19075. (setq min n)
  19076. (goto-char (point-min))
  19077. (while (re-search-forward "^ *[^ \n]" nil t)
  19078. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19079. (unless (or (= min 0) (= min 10000))
  19080. (setq re (format "^ \\{%d\\}" min))
  19081. (goto-char (point-min))
  19082. (while (re-search-forward re nil t)
  19083. (replace-match "")
  19084. (end-of-line 1))
  19085. min)))
  19086. (defun org-fill-template (template alist)
  19087. "Find each %key of ALIST in TEMPLATE and replace it."
  19088. (let ((case-fold-search nil)
  19089. entry key value)
  19090. (setq alist (sort (copy-sequence alist)
  19091. (lambda (a b) (< (length (car a)) (length (car b))))))
  19092. (while (setq entry (pop alist))
  19093. (setq template
  19094. (replace-regexp-in-string
  19095. (concat "%" (regexp-quote (car entry)))
  19096. (or (cdr entry) "") template t t)))
  19097. template))
  19098. (defun org-base-buffer (buffer)
  19099. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19100. (if (not buffer)
  19101. buffer
  19102. (or (buffer-base-buffer buffer)
  19103. buffer)))
  19104. (defun org-trim (s)
  19105. "Remove whitespace at beginning and end of string."
  19106. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19107. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19108. s)
  19109. (defun org-wrap (string &optional width lines)
  19110. "Wrap string to either a number of lines, or a width in characters.
  19111. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19112. that costs. If there is a word longer than WIDTH, the text is actually
  19113. wrapped to the length of that word.
  19114. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19115. many lines, whatever width that takes.
  19116. The return value is a list of lines, without newlines at the end."
  19117. (let* ((words (org-split-string string "[ \t\n]+"))
  19118. (maxword (apply 'max (mapcar 'org-string-width words)))
  19119. w ll)
  19120. (cond (width
  19121. (org-do-wrap words (max maxword width)))
  19122. (lines
  19123. (setq w maxword)
  19124. (setq ll (org-do-wrap words maxword))
  19125. (if (<= (length ll) lines)
  19126. ll
  19127. (setq ll words)
  19128. (while (> (length ll) lines)
  19129. (setq w (1+ w))
  19130. (setq ll (org-do-wrap words w)))
  19131. ll))
  19132. (t (error "Cannot wrap this")))))
  19133. (defun org-do-wrap (words width)
  19134. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19135. (let (lines line)
  19136. (while words
  19137. (setq line (pop words))
  19138. (while (and words (< (+ (length line) (length (car words))) width))
  19139. (setq line (concat line " " (pop words))))
  19140. (setq lines (push line lines)))
  19141. (nreverse lines)))
  19142. (defun org-split-string (string &optional separators)
  19143. "Splits STRING into substrings at SEPARATORS.
  19144. No empty strings are returned if there are matches at the beginning
  19145. and end of string."
  19146. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19147. (start 0)
  19148. notfirst
  19149. (list nil))
  19150. (while (and (string-match rexp string
  19151. (if (and notfirst
  19152. (= start (match-beginning 0))
  19153. (< start (length string)))
  19154. (1+ start) start))
  19155. (< (match-beginning 0) (length string)))
  19156. (setq notfirst t)
  19157. (or (eq (match-beginning 0) 0)
  19158. (and (eq (match-beginning 0) (match-end 0))
  19159. (eq (match-beginning 0) start))
  19160. (setq list
  19161. (cons (substring string start (match-beginning 0))
  19162. list)))
  19163. (setq start (match-end 0)))
  19164. (or (eq start (length string))
  19165. (setq list
  19166. (cons (substring string start)
  19167. list)))
  19168. (nreverse list)))
  19169. (defun org-quote-vert (s)
  19170. "Replace \"|\" with \"\\vert\"."
  19171. (while (string-match "|" s)
  19172. (setq s (replace-match "\\vert" t t s)))
  19173. s)
  19174. (defun org-uuidgen-p (s)
  19175. "Is S an ID created by UUIDGEN?"
  19176. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19177. (defun org-in-src-block-p (&optional inside)
  19178. "Whether point is in a code source block.
  19179. When INSIDE is non-nil, don't consider we are within a src block
  19180. when point is at #+BEGIN_SRC or #+END_SRC."
  19181. (let ((case-fold-search t) ov)
  19182. (or (and (setq ov (overlays-at (point)))
  19183. (memq 'org-block-background
  19184. (overlay-properties (car ov))))
  19185. (and (not inside)
  19186. (save-match-data
  19187. (save-excursion
  19188. (beginning-of-line)
  19189. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19190. (defun org-context ()
  19191. "Return a list of contexts of the current cursor position.
  19192. If several contexts apply, all are returned.
  19193. Each context entry is a list with a symbol naming the context, and
  19194. two positions indicating start and end of the context. Possible
  19195. contexts are:
  19196. :headline anywhere in a headline
  19197. :headline-stars on the leading stars in a headline
  19198. :todo-keyword on a TODO keyword (including DONE) in a headline
  19199. :tags on the TAGS in a headline
  19200. :priority on the priority cookie in a headline
  19201. :item on the first line of a plain list item
  19202. :item-bullet on the bullet/number of a plain list item
  19203. :checkbox on the checkbox in a plain list item
  19204. :table in an org-mode table
  19205. :table-special on a special filed in a table
  19206. :table-table in a table.el table
  19207. :clocktable in a clocktable
  19208. :src-block in a source block
  19209. :link on a hyperlink
  19210. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19211. :target on a <<target>>
  19212. :radio-target on a <<<radio-target>>>
  19213. :latex-fragment on a LaTeX fragment
  19214. :latex-preview on a LaTeX fragment with overlaid preview image
  19215. This function expects the position to be visible because it uses font-lock
  19216. faces as a help to recognize the following contexts: :table-special, :link,
  19217. and :keyword."
  19218. (let* ((f (get-text-property (point) 'face))
  19219. (faces (if (listp f) f (list f)))
  19220. (case-fold-search t)
  19221. (p (point)) clist o)
  19222. ;; First the large context
  19223. (cond
  19224. ((org-at-heading-p t)
  19225. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19226. (when (progn
  19227. (beginning-of-line 1)
  19228. (looking-at org-todo-line-tags-regexp))
  19229. (push (org-point-in-group p 1 :headline-stars) clist)
  19230. (push (org-point-in-group p 2 :todo-keyword) clist)
  19231. (push (org-point-in-group p 4 :tags) clist))
  19232. (goto-char p)
  19233. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19234. (if (looking-at "\\[#[A-Z0-9]\\]")
  19235. (push (org-point-in-group p 0 :priority) clist)))
  19236. ((org-at-item-p)
  19237. (push (org-point-in-group p 2 :item-bullet) clist)
  19238. (push (list :item (point-at-bol)
  19239. (save-excursion (org-end-of-item) (point)))
  19240. clist)
  19241. (and (org-at-item-checkbox-p)
  19242. (push (org-point-in-group p 0 :checkbox) clist)))
  19243. ((org-at-table-p)
  19244. (push (list :table (org-table-begin) (org-table-end)) clist)
  19245. (if (memq 'org-formula faces)
  19246. (push (list :table-special
  19247. (previous-single-property-change p 'face)
  19248. (next-single-property-change p 'face)) clist)))
  19249. ((org-at-table-p 'any)
  19250. (push (list :table-table) clist)))
  19251. (goto-char p)
  19252. (let ((case-fold-search t))
  19253. ;; New the "medium" contexts: clocktables, source blocks
  19254. (cond ((org-in-clocktable-p)
  19255. (push (list :clocktable
  19256. (and (or (looking-at "#\\+BEGIN: clocktable")
  19257. (search-backward "#+BEGIN: clocktable" nil t))
  19258. (match-beginning 0))
  19259. (and (re-search-forward "#\\+END:?" nil t)
  19260. (match-end 0))) clist))
  19261. ((org-in-src-block-p)
  19262. (push (list :src-block
  19263. (and (or (looking-at "#\\+BEGIN_SRC")
  19264. (search-backward "#+BEGIN_SRC" nil t))
  19265. (match-beginning 0))
  19266. (and (search-forward "#+END_SRC" nil t)
  19267. (match-beginning 0))) clist))))
  19268. (goto-char p)
  19269. ;; Now the small context
  19270. (cond
  19271. ((org-at-timestamp-p)
  19272. (push (org-point-in-group p 0 :timestamp) clist))
  19273. ((memq 'org-link faces)
  19274. (push (list :link
  19275. (previous-single-property-change p 'face)
  19276. (next-single-property-change p 'face)) clist))
  19277. ((memq 'org-special-keyword faces)
  19278. (push (list :keyword
  19279. (previous-single-property-change p 'face)
  19280. (next-single-property-change p 'face)) clist))
  19281. ((org-at-target-p)
  19282. (push (org-point-in-group p 0 :target) clist)
  19283. (goto-char (1- (match-beginning 0)))
  19284. (if (looking-at org-radio-target-regexp)
  19285. (push (org-point-in-group p 0 :radio-target) clist))
  19286. (goto-char p))
  19287. ((setq o (car (delq nil
  19288. (mapcar
  19289. (lambda (x)
  19290. (if (memq x org-latex-fragment-image-overlays) x))
  19291. (overlays-at (point))))))
  19292. (push (list :latex-fragment
  19293. (overlay-start o) (overlay-end o)) clist)
  19294. (push (list :latex-preview
  19295. (overlay-start o) (overlay-end o)) clist))
  19296. ((org-inside-LaTeX-fragment-p)
  19297. ;; FIXME: positions wrong.
  19298. (push (list :latex-fragment (point) (point)) clist)))
  19299. (setq clist (nreverse (delq nil clist)))
  19300. clist))
  19301. ;; FIXME: Compare with at-regexp-p Do we need both?
  19302. (defun org-in-regexp (re &optional nlines visually)
  19303. "Check if point is inside a match of regexp.
  19304. Normally only the current line is checked, but you can include NLINES extra
  19305. lines both before and after point into the search.
  19306. If VISUALLY is set, require that the cursor is not after the match but
  19307. really on, so that the block visually is on the match."
  19308. (catch 'exit
  19309. (let ((pos (point))
  19310. (eol (point-at-eol (+ 1 (or nlines 0))))
  19311. (inc (if visually 1 0)))
  19312. (save-excursion
  19313. (beginning-of-line (- 1 (or nlines 0)))
  19314. (while (re-search-forward re eol t)
  19315. (if (and (<= (match-beginning 0) pos)
  19316. (>= (+ inc (match-end 0)) pos))
  19317. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19318. (defun org-at-regexp-p (regexp)
  19319. "Is point inside a match of REGEXP in the current line?"
  19320. (catch 'exit
  19321. (save-excursion
  19322. (let ((pos (point)) (end (point-at-eol)))
  19323. (beginning-of-line 1)
  19324. (while (re-search-forward regexp end t)
  19325. (if (and (<= (match-beginning 0) pos)
  19326. (>= (match-end 0) pos))
  19327. (throw 'exit t)))
  19328. nil))))
  19329. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19330. "Non-nil when point is between matches of START-RE and END-RE.
  19331. Also return a non-nil value when point is on one of the matches.
  19332. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19333. buffer positions. Default values are the positions of headlines
  19334. surrounding the point.
  19335. The functions returns a cons cell whose car (resp. cdr) is the
  19336. position before START-RE (resp. after END-RE)."
  19337. (save-match-data
  19338. (let ((pos (point))
  19339. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19340. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19341. beg end)
  19342. (save-excursion
  19343. ;; Point is on a block when on START-RE or if START-RE can be
  19344. ;; found before it...
  19345. (and (or (org-at-regexp-p start-re)
  19346. (re-search-backward start-re limit-up t))
  19347. (setq beg (match-beginning 0))
  19348. ;; ... and END-RE after it...
  19349. (goto-char (match-end 0))
  19350. (re-search-forward end-re limit-down t)
  19351. (> (setq end (match-end 0)) pos)
  19352. ;; ... without another START-RE in-between.
  19353. (goto-char (match-beginning 0))
  19354. (not (re-search-backward start-re (1+ beg) t))
  19355. ;; Return value.
  19356. (cons beg end))))))
  19357. (defun org-in-block-p (names)
  19358. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19359. NAMES is a list of strings containing names of blocks.
  19360. Return first block name matched, or nil. Beware that in case of
  19361. nested blocks, the returned name may not belong to the closest
  19362. block from point."
  19363. (save-match-data
  19364. (catch 'exit
  19365. (let ((case-fold-search t)
  19366. (lim-up (save-excursion (outline-previous-heading)))
  19367. (lim-down (save-excursion (outline-next-heading))))
  19368. (mapc (lambda (name)
  19369. (let ((n (regexp-quote name)))
  19370. (when (org-between-regexps-p
  19371. (concat "^[ \t]*#\\+begin_" n)
  19372. (concat "^[ \t]*#\\+end_" n)
  19373. lim-up lim-down)
  19374. (throw 'exit n))))
  19375. names))
  19376. nil)))
  19377. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19378. "Call `multi-occur' with buffers for all agenda files."
  19379. (interactive "sOrg-files matching: \np")
  19380. (let* ((files (org-agenda-files))
  19381. (tnames (mapcar 'file-truename files))
  19382. (extra org-agenda-text-search-extra-files)
  19383. f)
  19384. (when (eq (car extra) 'agenda-archives)
  19385. (setq extra (cdr extra))
  19386. (setq files (org-add-archive-files files)))
  19387. (while (setq f (pop extra))
  19388. (unless (member (file-truename f) tnames)
  19389. (add-to-list 'files f 'append)
  19390. (add-to-list 'tnames (file-truename f) 'append)))
  19391. (multi-occur
  19392. (mapcar (lambda (x)
  19393. (with-current-buffer
  19394. (or (get-file-buffer x) (find-file-noselect x))
  19395. (widen)
  19396. (current-buffer)))
  19397. files)
  19398. regexp)))
  19399. (if (boundp 'occur-mode-find-occurrence-hook)
  19400. ;; Emacs 23
  19401. (add-hook 'occur-mode-find-occurrence-hook
  19402. (lambda ()
  19403. (when (derived-mode-p 'org-mode)
  19404. (org-reveal))))
  19405. ;; Emacs 22
  19406. (defadvice occur-mode-goto-occurrence
  19407. (after org-occur-reveal activate)
  19408. (and (derived-mode-p 'org-mode) (org-reveal)))
  19409. (defadvice occur-mode-goto-occurrence-other-window
  19410. (after org-occur-reveal activate)
  19411. (and (derived-mode-p 'org-mode) (org-reveal)))
  19412. (defadvice occur-mode-display-occurrence
  19413. (after org-occur-reveal activate)
  19414. (when (derived-mode-p 'org-mode)
  19415. (let ((pos (occur-mode-find-occurrence)))
  19416. (with-current-buffer (marker-buffer pos)
  19417. (save-excursion
  19418. (goto-char pos)
  19419. (org-reveal)))))))
  19420. (defun org-occur-link-in-agenda-files ()
  19421. "Create a link and search for it in the agendas.
  19422. The link is not stored in `org-stored-links', it is just created
  19423. for the search purpose."
  19424. (interactive)
  19425. (let ((link (condition-case nil
  19426. (org-store-link nil)
  19427. (error "Unable to create a link to here"))))
  19428. (org-occur-in-agenda-files (regexp-quote link))))
  19429. (defun org-reverse-string (string)
  19430. "Return the reverse of STRING."
  19431. (apply 'string (reverse (string-to-list string))))
  19432. (defun org-uniquify (list)
  19433. "Remove duplicate elements from LIST."
  19434. (let (res)
  19435. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19436. res))
  19437. (defun org-delete-all (elts list)
  19438. "Remove all elements in ELTS from LIST."
  19439. (while elts
  19440. (setq list (delete (pop elts) list)))
  19441. list)
  19442. (defun org-count (cl-item cl-seq)
  19443. "Count the number of occurrences of ITEM in SEQ.
  19444. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19445. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19446. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19447. (while (< cl-start cl-end)
  19448. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19449. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19450. (setq cl-start (1+ cl-start)))
  19451. cl-count))
  19452. (defun org-remove-if (predicate seq)
  19453. "Remove everything from SEQ that fulfills PREDICATE."
  19454. (let (res e)
  19455. (while seq
  19456. (setq e (pop seq))
  19457. (if (not (funcall predicate e)) (push e res)))
  19458. (nreverse res)))
  19459. (defun org-remove-if-not (predicate seq)
  19460. "Remove everything from SEQ that does not fulfill PREDICATE."
  19461. (let (res e)
  19462. (while seq
  19463. (setq e (pop seq))
  19464. (if (funcall predicate e) (push e res)))
  19465. (nreverse res)))
  19466. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19467. "Reduce two-argument FUNCTION across SEQ.
  19468. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19469. \":initial-value\" removed."
  19470. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19471. (cadr (memq :initial-value cl-keys)))
  19472. (cl-seq (pop cl-seq))
  19473. (t (funcall cl-func)))))
  19474. (while cl-seq
  19475. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19476. cl-accum))
  19477. (defun org-back-over-empty-lines ()
  19478. "Move backwards over whitespace, to the beginning of the first empty line.
  19479. Returns the number of empty lines passed."
  19480. (let ((pos (point)))
  19481. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19482. (skip-chars-backward " \t\n\r")
  19483. (unless (eobp)
  19484. (forward-line -1)))
  19485. (beginning-of-line 2)
  19486. (goto-char (min (point) pos))
  19487. (count-lines (point) pos)))
  19488. (defun org-skip-whitespace ()
  19489. (skip-chars-forward " \t\n\r"))
  19490. (defun org-point-in-group (point group &optional context)
  19491. "Check if POINT is in match-group GROUP.
  19492. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19493. match. If the match group does not exist or point is not inside it,
  19494. return nil."
  19495. (and (match-beginning group)
  19496. (>= point (match-beginning group))
  19497. (<= point (match-end group))
  19498. (if context
  19499. (list context (match-beginning group) (match-end group))
  19500. t)))
  19501. (defun org-switch-to-buffer-other-window (&rest args)
  19502. "Switch to buffer in a second window on the current frame.
  19503. In particular, do not allow pop-up frames.
  19504. Returns the newly created buffer."
  19505. (org-no-popups
  19506. (apply 'switch-to-buffer-other-window args)))
  19507. (defun org-combine-plists (&rest plists)
  19508. "Create a single property list from all plists in PLISTS.
  19509. The process starts by copying the first list, and then setting properties
  19510. from the other lists. Settings in the last list are the most significant
  19511. ones and overrule settings in the other lists."
  19512. (let ((rtn (copy-sequence (pop plists)))
  19513. p v ls)
  19514. (while plists
  19515. (setq ls (pop plists))
  19516. (while ls
  19517. (setq p (pop ls) v (pop ls))
  19518. (setq rtn (plist-put rtn p v))))
  19519. rtn))
  19520. (defun org-replace-escapes (string table)
  19521. "Replace %-escapes in STRING with values in TABLE.
  19522. TABLE is an association list with keys like \"%a\" and string values.
  19523. The sequences in STRING may contain normal field width and padding information,
  19524. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19525. so values can contain further %-escapes if they are define later in TABLE."
  19526. (let ((tbl (copy-alist table))
  19527. (case-fold-search nil)
  19528. (pchg 0)
  19529. e re rpl)
  19530. (while (setq e (pop tbl))
  19531. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19532. (when (and (cdr e) (string-match re (cdr e)))
  19533. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19534. (safe "SREF"))
  19535. (add-text-properties 0 3 (list 'sref sref) safe)
  19536. (setcdr e (replace-match safe t t (cdr e)))))
  19537. (while (string-match re string)
  19538. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19539. (cdr e)))
  19540. (setq string (replace-match rpl t t string))))
  19541. (while (setq pchg (next-property-change pchg string))
  19542. (let ((sref (get-text-property pchg 'sref string)))
  19543. (when (and sref (string-match "SREF" string pchg))
  19544. (setq string (replace-match sref t t string)))))
  19545. string))
  19546. (defun org-sublist (list start end)
  19547. "Return a section of LIST, from START to END.
  19548. Counting starts at 1."
  19549. (let (rtn (c start))
  19550. (setq list (nthcdr (1- start) list))
  19551. (while (and list (<= c end))
  19552. (push (pop list) rtn)
  19553. (setq c (1+ c)))
  19554. (nreverse rtn)))
  19555. (defun org-find-base-buffer-visiting (file)
  19556. "Like `find-buffer-visiting' but always return the base buffer and
  19557. not an indirect buffer."
  19558. (let ((buf (or (get-file-buffer file)
  19559. (find-buffer-visiting file))))
  19560. (if buf
  19561. (or (buffer-base-buffer buf) buf)
  19562. nil)))
  19563. (defun org-image-file-name-regexp (&optional extensions)
  19564. "Return regexp matching the file names of images.
  19565. If EXTENSIONS is given, only match these."
  19566. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19567. (image-file-name-regexp)
  19568. (let ((image-file-name-extensions
  19569. (or extensions
  19570. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19571. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19572. (concat "\\."
  19573. (regexp-opt (nconc (mapcar 'upcase
  19574. image-file-name-extensions)
  19575. image-file-name-extensions)
  19576. t)
  19577. "\\'"))))
  19578. (defun org-file-image-p (file &optional extensions)
  19579. "Return non-nil if FILE is an image."
  19580. (save-match-data
  19581. (string-match (org-image-file-name-regexp extensions) file)))
  19582. (defun org-get-cursor-date (&optional with-time)
  19583. "Return the date at cursor in as a time.
  19584. This works in the calendar and in the agenda, anywhere else it just
  19585. returns the current time.
  19586. If WITH-TIME is non-nil, returns the time of the event at point (in
  19587. the agenda) or the current time of the day."
  19588. (let (date day defd tp tm hod mod)
  19589. (when with-time
  19590. (setq tp (get-text-property (point) 'time))
  19591. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19592. (setq hod (string-to-number (match-string 1 tp))
  19593. mod (string-to-number (match-string 2 tp))))
  19594. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19595. mod (nth 1 (decode-time (current-time))))))
  19596. (cond
  19597. ((eq major-mode 'calendar-mode)
  19598. (setq date (calendar-cursor-to-date)
  19599. defd (encode-time 0 (or mod 0) (or hod 0)
  19600. (nth 1 date) (nth 0 date) (nth 2 date))))
  19601. ((eq major-mode 'org-agenda-mode)
  19602. (setq day (get-text-property (point) 'day))
  19603. (if day
  19604. (setq date (calendar-gregorian-from-absolute day)
  19605. defd (encode-time 0 (or mod 0) (or hod 0)
  19606. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19607. (or defd (current-time))))
  19608. (defun org-mark-subtree (&optional up)
  19609. "Mark the current subtree.
  19610. This puts point at the start of the current subtree, and mark at
  19611. the end. If a numeric prefix UP is given, move up into the
  19612. hierarchy of headlines by UP levels before marking the subtree."
  19613. (interactive "P")
  19614. (org-with-limited-levels
  19615. (cond ((org-at-heading-p) (beginning-of-line))
  19616. ((org-before-first-heading-p) (error "Not in a subtree"))
  19617. (t (outline-previous-visible-heading 1))))
  19618. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19619. (if (org-called-interactively-p 'any)
  19620. (call-interactively 'org-mark-element)
  19621. (org-mark-element)))
  19622. ;;; Indentation
  19623. (defun org-indent-line ()
  19624. "Indent line depending on context."
  19625. (interactive)
  19626. (let* ((pos (point))
  19627. (itemp (org-at-item-p))
  19628. (case-fold-search t)
  19629. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19630. (inline-task-p (and (featurep 'org-inlinetask)
  19631. (org-inlinetask-in-task-p)))
  19632. (inline-re (and inline-task-p
  19633. (org-inlinetask-outline-regexp)))
  19634. column)
  19635. (if (and orgstruct-is-++ (eq pos (point)))
  19636. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19637. (indent-according-to-mode))
  19638. (beginning-of-line 1)
  19639. (cond
  19640. ;; Headings
  19641. ((looking-at org-outline-regexp) (setq column 0))
  19642. ;; Footnote definition
  19643. ((looking-at org-footnote-definition-re) (setq column 0))
  19644. ;; Literal examples
  19645. ((looking-at "[ \t]*:\\( \\|$\\)")
  19646. (setq column (org-get-indentation))) ; do nothing
  19647. ;; Lists
  19648. ((ignore-errors (goto-char (org-in-item-p)))
  19649. (setq column (if itemp
  19650. (org-get-indentation)
  19651. (org-list-item-body-column (point))))
  19652. (goto-char pos))
  19653. ;; Drawers
  19654. ((and (looking-at "[ \t]*:END:")
  19655. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19656. (save-excursion
  19657. (goto-char (1- (match-beginning 1)))
  19658. (setq column (current-column))))
  19659. ;; Special blocks
  19660. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19661. (save-excursion
  19662. (re-search-backward
  19663. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19664. (setq column (org-get-indentation (match-string 0))))
  19665. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19666. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19667. (save-excursion
  19668. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19669. (setq column
  19670. (cond ((equal (downcase (match-string 1)) "src")
  19671. ;; src blocks: let `org-edit-src-exit' handle them
  19672. (org-get-indentation))
  19673. ((equal (downcase (match-string 1)) "example")
  19674. (max (org-get-indentation)
  19675. (org-get-indentation (match-string 0))))
  19676. (t
  19677. (org-get-indentation (match-string 0))))))
  19678. ;; This line has nothing special, look at the previous relevant
  19679. ;; line to compute indentation
  19680. (t
  19681. (beginning-of-line 0)
  19682. (while (and (not (bobp))
  19683. (not (looking-at org-table-line-regexp))
  19684. (not (looking-at org-drawer-regexp))
  19685. ;; When point started in an inline task, do not move
  19686. ;; above task starting line.
  19687. (not (and inline-task-p (looking-at inline-re)))
  19688. ;; Skip drawers, blocks, empty lines, verbatim,
  19689. ;; comments, tables, footnotes definitions, lists,
  19690. ;; inline tasks.
  19691. (or (and (looking-at "[ \t]*:END:")
  19692. (re-search-backward org-drawer-regexp nil t))
  19693. (and (looking-at "[ \t]*#\\+end_")
  19694. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19695. (looking-at "[ \t]*[\n:#|]")
  19696. (looking-at org-footnote-definition-re)
  19697. (and (ignore-errors (goto-char (org-in-item-p)))
  19698. (goto-char
  19699. (org-list-get-top-point (org-list-struct))))
  19700. (and (not inline-task-p)
  19701. (featurep 'org-inlinetask)
  19702. (org-inlinetask-in-task-p)
  19703. (or (org-inlinetask-goto-beginning) t))))
  19704. (beginning-of-line 0))
  19705. (cond
  19706. ;; There was an heading above.
  19707. ((looking-at "\\*+[ \t]+")
  19708. (if (not org-adapt-indentation)
  19709. (setq column 0)
  19710. (goto-char (match-end 0))
  19711. (setq column (current-column))))
  19712. ;; A drawer had started and is unfinished
  19713. ((looking-at org-drawer-regexp)
  19714. (goto-char (1- (match-beginning 1)))
  19715. (setq column (current-column)))
  19716. ;; Else, nothing noticeable found: get indentation and go on.
  19717. (t (setq column (org-get-indentation))))))
  19718. ;; Now apply indentation and move cursor accordingly
  19719. (goto-char pos)
  19720. (if (<= (current-column) (current-indentation))
  19721. (org-indent-line-to column)
  19722. (save-excursion (org-indent-line-to column)))
  19723. ;; Special polishing for properties, see `org-property-format'
  19724. (setq column (current-column))
  19725. (beginning-of-line 1)
  19726. (if (looking-at
  19727. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19728. (replace-match (concat (match-string 1)
  19729. (format org-property-format
  19730. (match-string 2) (match-string 3)))
  19731. t t))
  19732. (org-move-to-column column))))
  19733. (defun org-indent-drawer ()
  19734. "Indent the drawer at point."
  19735. (interactive)
  19736. (let ((p (point))
  19737. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19738. (match-end 0)))
  19739. (folded
  19740. (save-excursion
  19741. (end-of-line)
  19742. (when (overlays-at (point))
  19743. (member 'invisible (overlay-properties
  19744. (car (overlays-at (point)))))))))
  19745. (when folded (org-cycle))
  19746. (indent-for-tab-command)
  19747. (while (and (move-beginning-of-line 2) (< (point) e))
  19748. (indent-for-tab-command))
  19749. (goto-char p)
  19750. (when folded (org-cycle)))
  19751. (message "Drawer at point indented"))
  19752. (defun org-indent-block ()
  19753. "Indent the block at point."
  19754. (interactive)
  19755. (let ((p (point))
  19756. (case-fold-search t)
  19757. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19758. (match-end 0)))
  19759. (folded
  19760. (save-excursion
  19761. (end-of-line)
  19762. (when (overlays-at (point))
  19763. (member 'invisible (overlay-properties
  19764. (car (overlays-at (point)))))))))
  19765. (when folded (org-cycle))
  19766. (indent-for-tab-command)
  19767. (while (and (move-beginning-of-line 2) (< (point) e))
  19768. (indent-for-tab-command))
  19769. (goto-char p)
  19770. (when folded (org-cycle)))
  19771. (message "Block at point indented"))
  19772. (defun org-indent-region (start end)
  19773. "Indent region."
  19774. (interactive "r")
  19775. (save-excursion
  19776. (let ((line-end (org-current-line end)))
  19777. (goto-char start)
  19778. (while (< (org-current-line) line-end)
  19779. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19780. (t (call-interactively 'org-indent-line)))
  19781. (move-beginning-of-line 2)))))
  19782. ;;; Filling
  19783. ;; We use our own fill-paragraph and auto-fill functions.
  19784. ;; `org-fill-paragraph' relies on adaptive filling and context
  19785. ;; checking. Appropriate `fill-prefix' is computed with
  19786. ;; `org-adaptive-fill-function'.
  19787. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19788. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19789. ;; `org-adaptive-fill-function' value. Internally,
  19790. ;; `org-comment-line-break-function' breaks the line.
  19791. ;; `org-setup-filling' installs filling and auto-filling related
  19792. ;; variables during `org-mode' initialization.
  19793. (defun org-setup-filling ()
  19794. (interactive)
  19795. ;; Prevent auto-fill from inserting unwanted new items.
  19796. (when (boundp 'fill-nobreak-predicate)
  19797. (org-set-local
  19798. 'fill-nobreak-predicate
  19799. (org-uniquify
  19800. (append fill-nobreak-predicate
  19801. '(org-fill-paragraph-separate-nobreak-p
  19802. org-fill-line-break-nobreak-p
  19803. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19804. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19805. (org-set-local 'auto-fill-inhibit-regexp nil)
  19806. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19807. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19808. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19809. (defvar org-element-paragraph-separate) ; org-element.el
  19810. (defun org-fill-paragraph-separate-nobreak-p ()
  19811. "Non-nil when a line break at point would insert a new item."
  19812. (looking-at (substring org-element-paragraph-separate 1)))
  19813. (defun org-fill-line-break-nobreak-p ()
  19814. "Non-nil when a line break at point would create an Org line break."
  19815. (save-excursion
  19816. (skip-chars-backward "[ \t]")
  19817. (skip-chars-backward "\\\\")
  19818. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19819. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19820. "Non-nil when a line break at point would insert a new item."
  19821. (and (org-at-timestamp-p t)
  19822. (not (looking-at org-ts-regexp-both))))
  19823. (declare-function message-in-body-p "message" ())
  19824. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19825. (defun org-adaptive-fill-function ()
  19826. "Compute a fill prefix for the current line.
  19827. Return fill prefix, as a string, or nil if current line isn't
  19828. meant to be filled."
  19829. (let (prefix)
  19830. (catch 'exit
  19831. (when (derived-mode-p 'message-mode)
  19832. (save-excursion
  19833. (beginning-of-line)
  19834. (cond ((or (not (message-in-body-p))
  19835. (looking-at orgtbl-line-start-regexp))
  19836. (throw 'exit nil))
  19837. ((looking-at message-cite-prefix-regexp)
  19838. (throw 'exit (match-string-no-properties 0)))
  19839. ((looking-at org-outline-regexp)
  19840. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19841. (org-with-wide-buffer
  19842. (let* ((p (line-beginning-position))
  19843. (element (save-excursion (beginning-of-line)
  19844. (org-element-at-point)))
  19845. (type (org-element-type element))
  19846. (post-affiliated (org-element-property :post-affiliated element)))
  19847. (unless (and post-affiliated (< p post-affiliated))
  19848. (case type
  19849. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19850. (footnote-definition "")
  19851. ((item plain-list)
  19852. (make-string (org-list-item-body-column
  19853. (or post-affiliated
  19854. (org-element-property :begin element)))
  19855. ? ))
  19856. (paragraph
  19857. ;; Fill prefix is usually the same as the current line,
  19858. ;; except if the paragraph is at the beginning of an item.
  19859. (let ((parent (org-element-property :parent element)))
  19860. (cond ((eq (org-element-type parent) 'item)
  19861. (make-string (org-list-item-body-column
  19862. (org-element-property :begin parent))
  19863. ? ))
  19864. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19865. (match-string 0))
  19866. (t ""))))
  19867. (comment-block
  19868. ;; Only fill contents if P is within block boundaries.
  19869. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19870. (forward-line)
  19871. (point)))
  19872. (cend (save-excursion
  19873. (goto-char (org-element-property :end element))
  19874. (skip-chars-backward " \r\t\n")
  19875. (line-beginning-position))))
  19876. (when (and (>= p cbeg) (< p cend))
  19877. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19878. (match-string 0)
  19879. "")))))))))))
  19880. (declare-function message-goto-body "message" ())
  19881. (defvar message-cite-prefix-regexp) ; From message.el
  19882. (defvar org-element-all-objects) ; From org-element.el
  19883. (defun org-fill-paragraph (&optional justify)
  19884. "Fill element at point, when applicable.
  19885. This function only applies to comment blocks, comments, example
  19886. blocks and paragraphs. Also, as a special case, re-align table
  19887. when point is at one.
  19888. If JUSTIFY is non-nil (interactively, with prefix argument),
  19889. justify as well. If `sentence-end-double-space' is non-nil, then
  19890. period followed by one space does not end a sentence, so don't
  19891. break a line there. The variable `fill-column' controls the
  19892. width for filling.
  19893. For convenience, when point is at a plain list, an item or
  19894. a footnote definition, try to fill the first paragraph within."
  19895. (interactive)
  19896. (if (and (derived-mode-p 'message-mode)
  19897. (or (not (message-in-body-p))
  19898. (save-excursion (move-beginning-of-line 1)
  19899. (looking-at message-cite-prefix-regexp))))
  19900. ;; First ensure filling is correct in message-mode.
  19901. (let ((fill-paragraph-function
  19902. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19903. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19904. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19905. (paragraph-separate
  19906. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19907. (fill-paragraph nil))
  19908. (save-excursion
  19909. ;; Move to end of line in order to get the first paragraph
  19910. ;; within a plain list or a footnote definition.
  19911. (end-of-line)
  19912. (let ((element (org-element-at-point)))
  19913. ;; First check if point is in a blank line at the beginning of
  19914. ;; the buffer. In that case, ignore filling.
  19915. (if (< (point) (org-element-property :begin element)) t
  19916. (case (org-element-type element)
  19917. ;; Use major mode filling function is src blocks.
  19918. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19919. ;; Align Org tables, leave table.el tables as-is.
  19920. (table-row (org-table-align) t)
  19921. (table
  19922. (when (eq (org-element-property :type element) 'org)
  19923. (org-table-align))
  19924. t)
  19925. (paragraph
  19926. ;; Paragraphs may contain `line-break' type objects.
  19927. (let ((beg (max (point-min)
  19928. (org-element-property :contents-begin element)))
  19929. (end (min (point-max)
  19930. (org-element-property :contents-end element))))
  19931. ;; Do nothing if point is at an affiliated keyword.
  19932. (if (< (point) beg) t
  19933. (when (derived-mode-p 'message-mode)
  19934. ;; In `message-mode', do not fill following
  19935. ;; citation in current paragraph nor text before
  19936. ;; message body.
  19937. (let ((body-start (save-excursion (message-goto-body))))
  19938. (when body-start (setq beg (max body-start beg))))
  19939. (when (save-excursion
  19940. (re-search-forward
  19941. (concat "^" message-cite-prefix-regexp) end t))
  19942. (setq end (match-beginning 0))))
  19943. ;; Fill paragraph, taking line breaks into
  19944. ;; consideration. For that, slice the paragraph
  19945. ;; using line breaks as separators, and fill the
  19946. ;; parts in reverse order to avoid messing with
  19947. ;; markers.
  19948. (save-excursion
  19949. (goto-char end)
  19950. (mapc
  19951. (lambda (pos)
  19952. (fill-region-as-paragraph pos (point) justify)
  19953. (goto-char pos))
  19954. ;; Find the list of ending positions for line
  19955. ;; breaks in the current paragraph. Add paragraph
  19956. ;; beginning to include first slice.
  19957. (nreverse
  19958. (cons
  19959. beg
  19960. (org-element-map
  19961. (org-element--parse-objects
  19962. beg end nil org-element-all-objects)
  19963. 'line-break
  19964. (lambda (lb) (org-element-property :end lb)))))))
  19965. t)))
  19966. ;; Contents of `comment-block' type elements should be
  19967. ;; filled as plain text, but only if point is within block
  19968. ;; markers.
  19969. (comment-block
  19970. (let* ((case-fold-search t)
  19971. (beg (save-excursion
  19972. (goto-char (org-element-property :begin element))
  19973. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19974. (forward-line)
  19975. (point)))
  19976. (end (save-excursion
  19977. (goto-char (org-element-property :end element))
  19978. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19979. (line-beginning-position))))
  19980. (when (and (>= (point) beg) (< (point) end))
  19981. (fill-region-as-paragraph
  19982. (save-excursion
  19983. (end-of-line)
  19984. (re-search-backward "^[ \t]*$" beg 'move)
  19985. (line-beginning-position))
  19986. (save-excursion
  19987. (beginning-of-line)
  19988. (re-search-forward "^[ \t]*$" end 'move)
  19989. (line-beginning-position))
  19990. justify)))
  19991. t)
  19992. ;; Fill comments.
  19993. (comment (fill-comment-paragraph justify))
  19994. ;; Ignore every other element.
  19995. (otherwise t)))))))
  19996. (defun org-auto-fill-function ()
  19997. "Auto-fill function."
  19998. ;; Check if auto-filling is meaningful.
  19999. (let ((fc (current-fill-column)))
  20000. (when (and fc (> (current-column) fc))
  20001. (let* ((fill-prefix (org-adaptive-fill-function))
  20002. ;; Enforce empty fill prefix, if required. Otherwise, it
  20003. ;; will be computed again.
  20004. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20005. (when fill-prefix (do-auto-fill))))))
  20006. (defun org-comment-line-break-function (&optional soft)
  20007. "Break line at point and indent, continuing comment if within one.
  20008. The inserted newline is marked hard if variable
  20009. `use-hard-newlines' is true, unless optional argument SOFT is
  20010. non-nil."
  20011. (if soft (insert-and-inherit ?\n) (newline 1))
  20012. (save-excursion (forward-char -1) (delete-horizontal-space))
  20013. (delete-horizontal-space)
  20014. (indent-to-left-margin)
  20015. (insert-before-markers-and-inherit fill-prefix))
  20016. ;;; Comments
  20017. ;; Org comments syntax is quite complex. It requires the entire line
  20018. ;; to be just a comment. Also, even with the right syntax at the
  20019. ;; beginning of line, some some elements (i.e. verse-block or
  20020. ;; example-block) don't accept comments. Usual Emacs comment commands
  20021. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20022. ;; Org still relies on `comment-dwim', but cannot trust
  20023. ;; `comment-only-p'. So, `comment-region-function' and
  20024. ;; `uncomment-region-function' both point
  20025. ;; to`org-comment-or-uncomment-region'. Eventually,
  20026. ;; `org-insert-comment' takes care of insertion of comments at the
  20027. ;; beginning of line.
  20028. ;; `org-setup-comments-handling' install comments related variables
  20029. ;; during `org-mode' initialization.
  20030. (defun org-setup-comments-handling ()
  20031. (interactive)
  20032. (org-set-local 'comment-use-syntax nil)
  20033. (org-set-local 'comment-start "# ")
  20034. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20035. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20036. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20037. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20038. (defun org-insert-comment ()
  20039. "Insert an empty comment above current line.
  20040. If the line is empty, insert comment at its beginning."
  20041. (beginning-of-line)
  20042. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20043. (org-indent-line)
  20044. (insert "# "))
  20045. (defvar comment-empty-lines) ; From newcomment.el.
  20046. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20047. "Comment or uncomment each non-blank line in the region.
  20048. Uncomment each non-blank line between BEG and END if it only
  20049. contains commented lines. Otherwise, comment them."
  20050. (save-restriction
  20051. ;; Restrict region
  20052. (narrow-to-region (save-excursion (goto-char beg)
  20053. (skip-chars-forward " \r\t\n" end)
  20054. (line-beginning-position))
  20055. (save-excursion (goto-char end)
  20056. (skip-chars-backward " \r\t\n" beg)
  20057. (line-end-position)))
  20058. (let ((uncommentp
  20059. ;; UNCOMMENTP is non-nil when every non blank line between
  20060. ;; BEG and END is a comment.
  20061. (save-excursion
  20062. (goto-char (point-min))
  20063. (while (and (not (eobp))
  20064. (let ((element (org-element-at-point)))
  20065. (and (eq (org-element-type element) 'comment)
  20066. (goto-char (min (point-max)
  20067. (org-element-property
  20068. :end element)))))))
  20069. (eobp))))
  20070. (if uncommentp
  20071. ;; Only blank lines and comments in region: uncomment it.
  20072. (save-excursion
  20073. (goto-char (point-min))
  20074. (while (not (eobp))
  20075. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20076. (replace-match "" nil nil nil 1))
  20077. (forward-line)))
  20078. ;; Comment each line in region.
  20079. (let ((min-indent (point-max)))
  20080. ;; First find the minimum indentation across all lines.
  20081. (save-excursion
  20082. (goto-char (point-min))
  20083. (while (and (not (eobp)) (not (zerop min-indent)))
  20084. (unless (looking-at "[ \t]*$")
  20085. (setq min-indent (min min-indent (current-indentation))))
  20086. (forward-line)))
  20087. ;; Then loop over all lines.
  20088. (save-excursion
  20089. (goto-char (point-min))
  20090. (while (not (eobp))
  20091. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20092. (org-move-to-column min-indent t)
  20093. (insert comment-start))
  20094. (forward-line))))))))
  20095. ;;; Planning
  20096. ;; This section contains tools to operate on timestamp objects, as
  20097. ;; returned by, e.g. `org-element-context'.
  20098. (defun org-timestamp-has-time-p (timestamp)
  20099. "Non-nil when TIMESTAMP has a time specified."
  20100. (org-element-property :hour-start timestamp))
  20101. (defun org-timestamp-format (timestamp format &optional end utc)
  20102. "Format a TIMESTAMP element into a string.
  20103. FORMAT is a format specifier to be passed to
  20104. `format-time-string'.
  20105. When optional argument END is non-nil, use end of date-range or
  20106. time-range, if possible.
  20107. When optional argument UTC is non-nil, time will be expressed as
  20108. Universal Time."
  20109. (format-time-string
  20110. format
  20111. (apply 'encode-time
  20112. (cons 0
  20113. (mapcar
  20114. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20115. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20116. '(:minute-start :hour-start :day-start :month-start
  20117. :year-start)))))
  20118. utc))
  20119. (defun org-timestamp-split-range (timestamp &optional end)
  20120. "Extract a timestamp object from a date or time range.
  20121. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20122. the end of the range. Otherwise, extract its start.
  20123. Return a new timestamp object sharing the same parent as
  20124. TIMESTAMP."
  20125. (let ((type (org-element-property :type timestamp)))
  20126. (if (memq type '(active inactive diary)) timestamp
  20127. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20128. ;; Set new type.
  20129. (org-element-put-property
  20130. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20131. ;; Copy start properties over end properties if END is
  20132. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20133. (let ((p-alist '((:minute-start . :minute-end)
  20134. (:hour-start . :hour-end)
  20135. (:day-start . :day-end)
  20136. (:month-start . :month-end)
  20137. (:year-start . :year-end))))
  20138. (dolist (p-cell p-alist)
  20139. (org-element-put-property
  20140. split-ts
  20141. (funcall (if end 'car 'cdr) p-cell)
  20142. (org-element-property
  20143. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20144. ;; Eventually refresh `:raw-value'.
  20145. (org-element-put-property split-ts :raw-value nil)
  20146. (org-element-put-property
  20147. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20148. (defun org-timestamp-translate (timestamp &optional boundary)
  20149. "Apply `org-translate-time' on a TIMESTAMP object.
  20150. When optional argument BOUNDARY is non-nil, it is either the
  20151. symbol `start' or `end'. In this case, only translate the
  20152. starting or ending part of TIMESTAMP if it is a date or time
  20153. range. Otherwise, translate both parts."
  20154. (if (and (not boundary)
  20155. (memq (org-element-property :type timestamp)
  20156. '(active-range inactive-range)))
  20157. (concat
  20158. (org-translate-time
  20159. (org-element-property :raw-value
  20160. (org-timestamp-split-range timestamp)))
  20161. "--"
  20162. (org-translate-time
  20163. (org-element-property :raw-value
  20164. (org-timestamp-split-range timestamp t))))
  20165. (org-translate-time
  20166. (org-element-property
  20167. :raw-value
  20168. (if (not boundary) timestamp
  20169. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20170. ;;; Other stuff.
  20171. (defun org-toggle-fixed-width-section (arg)
  20172. "Toggle the fixed-width export.
  20173. If there is no active region, the QUOTE keyword at the current headline is
  20174. inserted or removed. When present, it causes the text between this headline
  20175. and the next to be exported as fixed-width text, and unmodified.
  20176. If there is an active region, this command adds or removes a colon as the
  20177. first character of this line. If the first character of a line is a colon,
  20178. this line is also exported in fixed-width font."
  20179. (interactive "P")
  20180. (let* ((cc 0)
  20181. (regionp (org-region-active-p))
  20182. (beg (if regionp (region-beginning) (point)))
  20183. (end (if regionp (region-end)))
  20184. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20185. (case-fold-search nil)
  20186. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20187. off)
  20188. (if regionp
  20189. (save-excursion
  20190. (goto-char beg)
  20191. (setq cc (current-column))
  20192. (beginning-of-line 1)
  20193. (setq off (looking-at re))
  20194. (while (> nlines 0)
  20195. (setq nlines (1- nlines))
  20196. (beginning-of-line 1)
  20197. (cond
  20198. (arg
  20199. (org-move-to-column cc t)
  20200. (insert ": \n")
  20201. (forward-line -1))
  20202. ((and off (looking-at re))
  20203. (replace-match "" t t nil 1))
  20204. ((not off) (org-move-to-column cc t) (insert ": ")))
  20205. (forward-line 1)))
  20206. (save-excursion
  20207. (org-back-to-heading)
  20208. (cond
  20209. ((looking-at (format org-heading-keyword-regexp-format
  20210. org-quote-string))
  20211. (goto-char (match-end 1))
  20212. (looking-at (concat " +" org-quote-string))
  20213. (replace-match "" t t)
  20214. (when (eolp) (insert " ")))
  20215. ((looking-at org-outline-regexp)
  20216. (goto-char (match-end 0))
  20217. (insert org-quote-string " ")))))))
  20218. (defun org-reftex-citation ()
  20219. "Use reftex-citation to insert a citation into the buffer.
  20220. This looks for a line like
  20221. #+BIBLIOGRAPHY: foo plain option:-d
  20222. and derives from it that foo.bib is the bibliography file relevant
  20223. for this document. It then installs the necessary environment for RefTeX
  20224. to work in this buffer and calls `reftex-citation' to insert a citation
  20225. into the buffer.
  20226. Export of such citations to both LaTeX and HTML is handled by the contributed
  20227. package org-exp-bibtex by Taru Karttunen."
  20228. (interactive)
  20229. (let ((reftex-docstruct-symbol 'rds)
  20230. (reftex-cite-format "\\cite{%l}")
  20231. rds bib)
  20232. (save-excursion
  20233. (save-restriction
  20234. (widen)
  20235. (let ((case-fold-search t)
  20236. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20237. (if (not (save-excursion
  20238. (or (re-search-forward re nil t)
  20239. (re-search-backward re nil t))))
  20240. (error "No bibliography defined in file")
  20241. (setq bib (concat (match-string 1) ".bib")
  20242. rds (list (list 'bib bib)))))))
  20243. (call-interactively 'reftex-citation)))
  20244. ;;;; Functions extending outline functionality
  20245. (defun org-beginning-of-line (&optional arg)
  20246. "Go to the beginning of the current line. If that is invisible, continue
  20247. to a visible line beginning. This makes the function of C-a more intuitive.
  20248. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20249. first attempt, and only move to after the tags when the cursor is already
  20250. beyond the end of the headline."
  20251. (interactive "P")
  20252. (let ((pos (point))
  20253. (special (if (consp org-special-ctrl-a/e)
  20254. (car org-special-ctrl-a/e)
  20255. org-special-ctrl-a/e))
  20256. refpos)
  20257. (if (org-bound-and-true-p visual-line-mode)
  20258. (beginning-of-visual-line 1)
  20259. (beginning-of-line 1))
  20260. (if (and arg (fboundp 'move-beginning-of-line))
  20261. (call-interactively 'move-beginning-of-line)
  20262. (if (bobp)
  20263. nil
  20264. (backward-char 1)
  20265. (if (org-truely-invisible-p)
  20266. (while (and (not (bobp)) (org-truely-invisible-p))
  20267. (backward-char 1)
  20268. (beginning-of-line 1))
  20269. (forward-char 1))))
  20270. (when special
  20271. (cond
  20272. ((and (looking-at org-complex-heading-regexp)
  20273. (= (char-after (match-end 1)) ?\ ))
  20274. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20275. (point-at-eol)))
  20276. (goto-char
  20277. (if (eq special t)
  20278. (cond ((> pos refpos) refpos)
  20279. ((= pos (point)) refpos)
  20280. (t (point)))
  20281. (cond ((> pos (point)) (point))
  20282. ((not (eq last-command this-command)) (point))
  20283. (t refpos)))))
  20284. ((org-at-item-p)
  20285. ;; Being at an item and not looking at an the item means point
  20286. ;; was previously moved to beginning of a visual line, which
  20287. ;; doesn't contain the item. Therefore, do nothing special,
  20288. ;; just stay here.
  20289. (when (looking-at org-list-full-item-re)
  20290. ;; Set special position at first white space character after
  20291. ;; bullet, and check-box, if any.
  20292. (let ((after-bullet
  20293. (let ((box (match-end 3)))
  20294. (if (not box) (match-end 1)
  20295. (let ((after (char-after box)))
  20296. (if (and after (= after ? )) (1+ box) box))))))
  20297. ;; Special case: Move point to special position when
  20298. ;; currently after it or at beginning of line.
  20299. (if (eq special t)
  20300. (when (or (> pos after-bullet) (= (point) pos))
  20301. (goto-char after-bullet))
  20302. ;; Reversed case: Move point to special position when
  20303. ;; point was already at beginning of line and command is
  20304. ;; repeated.
  20305. (when (and (= (point) pos) (eq last-command this-command))
  20306. (goto-char after-bullet))))))))
  20307. (org-no-warnings
  20308. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20309. (defun org-end-of-line (&optional arg)
  20310. "Go to the end of the line.
  20311. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20312. tags on the first attempt, and only move to after the tags when
  20313. the cursor is already beyond the end of the headline."
  20314. (interactive "P")
  20315. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20316. org-special-ctrl-a/e))
  20317. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20318. 'end-of-visual-line)
  20319. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20320. (t 'end-of-line))))
  20321. (if (or (not special) arg) (call-interactively move-fun)
  20322. (let* ((element (save-excursion (beginning-of-line)
  20323. (org-element-at-point)))
  20324. (type (org-element-type element)))
  20325. (cond
  20326. ((memq type '(headline inlinetask))
  20327. (let ((pos (point)))
  20328. (beginning-of-line 1)
  20329. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20330. (if (eq special t)
  20331. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20332. (goto-char (match-beginning 1))
  20333. (goto-char (match-end 0)))
  20334. (if (or (< pos (match-end 0))
  20335. (not (eq this-command last-command)))
  20336. (goto-char (match-end 0))
  20337. (goto-char (match-beginning 1))))
  20338. (call-interactively move-fun))))
  20339. ((org-element-property :hiddenp element)
  20340. ;; If element is hidden, `move-end-of-line' would put point
  20341. ;; after it. Use `end-of-line' to stay on current line.
  20342. (call-interactively 'end-of-line))
  20343. (t (call-interactively move-fun)))))
  20344. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20345. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20346. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20347. (defun org-backward-sentence (&optional arg)
  20348. "Go to beginning of sentence, or beginning of table field.
  20349. This will call `backward-sentence' or `org-table-beginning-of-field',
  20350. depending on context."
  20351. (interactive "P")
  20352. (cond
  20353. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20354. (t (call-interactively 'backward-sentence))))
  20355. (defun org-forward-sentence (&optional arg)
  20356. "Go to end of sentence, or end of table field.
  20357. This will call `forward-sentence' or `org-table-end-of-field',
  20358. depending on context."
  20359. (interactive "P")
  20360. (cond
  20361. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20362. (t (call-interactively 'forward-sentence))))
  20363. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20364. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20365. (defun org-kill-line (&optional arg)
  20366. "Kill line, to tags or end of line."
  20367. (interactive "P")
  20368. (cond
  20369. ((or (not org-special-ctrl-k)
  20370. (bolp)
  20371. (not (org-at-heading-p)))
  20372. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20373. org-ctrl-k-protect-subtree)
  20374. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20375. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20376. (error "C-k aborted - would kill hidden subtree")))
  20377. (call-interactively
  20378. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20379. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20380. (kill-region (point) (match-beginning 1))
  20381. (org-set-tags nil t))
  20382. (t (kill-region (point) (point-at-eol)))))
  20383. (define-key org-mode-map "\C-k" 'org-kill-line)
  20384. (defun org-yank (&optional arg)
  20385. "Yank. If the kill is a subtree, treat it specially.
  20386. This command will look at the current kill and check if is a single
  20387. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20388. cursor is at the beginning of a line or after the stars of a currently
  20389. empty headline, then the yank is handled specially. How exactly depends
  20390. on the value of the following variables, both set by default.
  20391. org-yank-folded-subtrees
  20392. When set, the subtree(s) will be folded after insertion, but only
  20393. if doing so would now swallow text after the yanked text.
  20394. org-yank-adjusted-subtrees
  20395. When set, the subtree will be promoted or demoted in order to
  20396. fit into the local outline tree structure, which means that the level
  20397. will be adjusted so that it becomes the smaller one of the two
  20398. *visible* surrounding headings.
  20399. Any prefix to this command will cause `yank' to be called directly with
  20400. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20401. will just
  20402. plainly yank the text as it is.
  20403. \[1] The test checks if the first non-white line is a heading
  20404. and if there are no other headings with fewer stars."
  20405. (interactive "P")
  20406. (org-yank-generic 'yank arg))
  20407. (defun org-yank-generic (command arg)
  20408. "Perform some yank-like command.
  20409. This function implements the behavior described in the `org-yank'
  20410. documentation. However, it has been generalized to work for any
  20411. interactive command with similar behavior."
  20412. ;; pretend to be command COMMAND
  20413. (setq this-command command)
  20414. (if arg
  20415. (call-interactively command)
  20416. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20417. (and (org-kill-is-subtree-p)
  20418. (or (bolp)
  20419. (and (looking-at "[ \t]*$")
  20420. (string-match
  20421. "\\`\\*+\\'"
  20422. (buffer-substring (point-at-bol) (point)))))))
  20423. swallowp)
  20424. (cond
  20425. ((and subtreep org-yank-folded-subtrees)
  20426. (let ((beg (point))
  20427. end)
  20428. (if (and subtreep org-yank-adjusted-subtrees)
  20429. (org-paste-subtree nil nil 'for-yank)
  20430. (call-interactively command))
  20431. (setq end (point))
  20432. (goto-char beg)
  20433. (when (and (bolp) subtreep
  20434. (not (setq swallowp
  20435. (org-yank-folding-would-swallow-text beg end))))
  20436. (org-with-limited-levels
  20437. (or (looking-at org-outline-regexp)
  20438. (re-search-forward org-outline-regexp-bol end t))
  20439. (while (and (< (point) end) (looking-at org-outline-regexp))
  20440. (hide-subtree)
  20441. (org-cycle-show-empty-lines 'folded)
  20442. (condition-case nil
  20443. (outline-forward-same-level 1)
  20444. (error (goto-char end))))))
  20445. (when swallowp
  20446. (message
  20447. "Inserted text not folded because that would swallow text"))
  20448. (goto-char end)
  20449. (skip-chars-forward " \t\n\r")
  20450. (beginning-of-line 1)
  20451. (push-mark beg 'nomsg)))
  20452. ((and subtreep org-yank-adjusted-subtrees)
  20453. (let ((beg (point-at-bol)))
  20454. (org-paste-subtree nil nil 'for-yank)
  20455. (push-mark beg 'nomsg)))
  20456. (t
  20457. (call-interactively command))))))
  20458. (defun org-yank-folding-would-swallow-text (beg end)
  20459. "Would hide-subtree at BEG swallow any text after END?"
  20460. (let (level)
  20461. (org-with-limited-levels
  20462. (save-excursion
  20463. (goto-char beg)
  20464. (when (or (looking-at org-outline-regexp)
  20465. (re-search-forward org-outline-regexp-bol end t))
  20466. (setq level (org-outline-level)))
  20467. (goto-char end)
  20468. (skip-chars-forward " \t\r\n\v\f")
  20469. (if (or (eobp)
  20470. (and (bolp) (looking-at org-outline-regexp)
  20471. (<= (org-outline-level) level)))
  20472. nil ; Nothing would be swallowed
  20473. t))))) ; something would swallow
  20474. (define-key org-mode-map "\C-y" 'org-yank)
  20475. (defun org-truely-invisible-p ()
  20476. "Check if point is at a character currently not visible.
  20477. This version does not only check the character property, but also
  20478. `visible-mode'."
  20479. ;; Early versions of noutline don't have `outline-invisible-p'.
  20480. (if (org-bound-and-true-p visible-mode)
  20481. nil
  20482. (outline-invisible-p)))
  20483. (defun org-invisible-p2 ()
  20484. "Check if point is at a character currently not visible."
  20485. (save-excursion
  20486. (if (and (eolp) (not (bobp))) (backward-char 1))
  20487. ;; Early versions of noutline don't have `outline-invisible-p'.
  20488. (outline-invisible-p)))
  20489. (defun org-back-to-heading (&optional invisible-ok)
  20490. "Call `outline-back-to-heading', but provide a better error message."
  20491. (condition-case nil
  20492. (outline-back-to-heading invisible-ok)
  20493. (error (error "Before first headline at position %d in buffer %s"
  20494. (point) (current-buffer)))))
  20495. (defun org-before-first-heading-p ()
  20496. "Before first heading?"
  20497. (save-excursion
  20498. (end-of-line)
  20499. (null (re-search-backward org-outline-regexp-bol nil t))))
  20500. (defun org-at-heading-p (&optional ignored)
  20501. (outline-on-heading-p t))
  20502. ;; Compatibility alias with Org versions < 7.8.03
  20503. (defalias 'org-on-heading-p 'org-at-heading-p)
  20504. (defun org-at-comment-p nil
  20505. "Is cursor in a line starting with a # character?"
  20506. (save-excursion
  20507. (beginning-of-line)
  20508. (looking-at "^#")))
  20509. (defun org-at-drawer-p nil
  20510. "Is cursor at a drawer keyword?"
  20511. (save-excursion
  20512. (move-beginning-of-line 1)
  20513. (looking-at org-drawer-regexp)))
  20514. (defun org-at-block-p nil
  20515. "Is cursor at a block keyword?"
  20516. (save-excursion
  20517. (move-beginning-of-line 1)
  20518. (looking-at org-block-regexp)))
  20519. (defun org-point-at-end-of-empty-headline ()
  20520. "If point is at the end of an empty headline, return t, else nil.
  20521. If the heading only contains a TODO keyword, it is still still considered
  20522. empty."
  20523. (and (looking-at "[ \t]*$")
  20524. (when org-todo-line-regexp
  20525. (save-excursion
  20526. (beginning-of-line 1)
  20527. (let ((case-fold-search nil))
  20528. (looking-at org-todo-line-regexp)
  20529. (string= (match-string 3) ""))))))
  20530. (defun org-at-heading-or-item-p ()
  20531. (or (org-at-heading-p) (org-at-item-p)))
  20532. (defun org-at-target-p ()
  20533. (or (org-in-regexp org-radio-target-regexp)
  20534. (org-in-regexp org-target-regexp)))
  20535. ;; Compatibility alias with Org versions < 7.8.03
  20536. (defalias 'org-on-target-p 'org-at-target-p)
  20537. (defun org-up-heading-all (arg)
  20538. "Move to the heading line of which the present line is a subheading.
  20539. This function considers both visible and invisible heading lines.
  20540. With argument, move up ARG levels."
  20541. (if (fboundp 'outline-up-heading-all)
  20542. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20543. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20544. (defun org-up-heading-safe ()
  20545. "Move to the heading line of which the present line is a subheading.
  20546. This version will not throw an error. It will return the level of the
  20547. headline found, or nil if no higher level is found.
  20548. Also, this function will be a lot faster than `outline-up-heading',
  20549. because it relies on stars being the outline starters. This can really
  20550. make a significant difference in outlines with very many siblings."
  20551. (let (start-level re)
  20552. (org-back-to-heading t)
  20553. (setq start-level (funcall outline-level))
  20554. (if (equal start-level 1)
  20555. nil
  20556. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20557. (if (re-search-backward re nil t)
  20558. (funcall outline-level)))))
  20559. (defun org-first-sibling-p ()
  20560. "Is this heading the first child of its parents?"
  20561. (interactive)
  20562. (let ((re org-outline-regexp-bol)
  20563. level l)
  20564. (unless (org-at-heading-p t)
  20565. (error "Not at a heading"))
  20566. (setq level (funcall outline-level))
  20567. (save-excursion
  20568. (if (not (re-search-backward re nil t))
  20569. t
  20570. (setq l (funcall outline-level))
  20571. (< l level)))))
  20572. (defun org-goto-sibling (&optional previous)
  20573. "Goto the next sibling, even if it is invisible.
  20574. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20575. when a sibling was found. When none is found, return nil and don't
  20576. move point."
  20577. (let ((fun (if previous 're-search-backward 're-search-forward))
  20578. (pos (point))
  20579. (re org-outline-regexp-bol)
  20580. level l)
  20581. (when (condition-case nil (org-back-to-heading t) (error nil))
  20582. (setq level (funcall outline-level))
  20583. (catch 'exit
  20584. (or previous (forward-char 1))
  20585. (while (funcall fun re nil t)
  20586. (setq l (funcall outline-level))
  20587. (when (< l level) (goto-char pos) (throw 'exit nil))
  20588. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20589. (goto-char pos)
  20590. nil))))
  20591. (defun org-show-siblings ()
  20592. "Show all siblings of the current headline."
  20593. (save-excursion
  20594. (while (org-goto-sibling) (org-flag-heading nil)))
  20595. (save-excursion
  20596. (while (org-goto-sibling 'previous)
  20597. (org-flag-heading nil))))
  20598. (defun org-goto-first-child ()
  20599. "Goto the first child, even if it is invisible.
  20600. Return t when a child was found. Otherwise don't move point and
  20601. return nil."
  20602. (let (level (pos (point)) (re org-outline-regexp-bol))
  20603. (when (condition-case nil (org-back-to-heading t) (error nil))
  20604. (setq level (outline-level))
  20605. (forward-char 1)
  20606. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20607. (progn (goto-char (match-beginning 0)) t)
  20608. (goto-char pos) nil))))
  20609. (defun org-show-hidden-entry ()
  20610. "Show an entry where even the heading is hidden."
  20611. (save-excursion
  20612. (org-show-entry)))
  20613. (defun org-flag-heading (flag &optional entry)
  20614. "Flag the current heading. FLAG non-nil means make invisible.
  20615. When ENTRY is non-nil, show the entire entry."
  20616. (save-excursion
  20617. (org-back-to-heading t)
  20618. ;; Check if we should show the entire entry
  20619. (if entry
  20620. (progn
  20621. (org-show-entry)
  20622. (save-excursion
  20623. (and (outline-next-heading)
  20624. (org-flag-heading nil))))
  20625. (outline-flag-region (max (point-min) (1- (point)))
  20626. (save-excursion (outline-end-of-heading) (point))
  20627. flag))))
  20628. (defun org-get-next-sibling ()
  20629. "Move to next heading of the same level, and return point.
  20630. If there is no such heading, return nil.
  20631. This is like outline-next-sibling, but invisible headings are ok."
  20632. (let ((level (funcall outline-level)))
  20633. (outline-next-heading)
  20634. (while (and (not (eobp)) (> (funcall outline-level) level))
  20635. (outline-next-heading))
  20636. (if (or (eobp) (< (funcall outline-level) level))
  20637. nil
  20638. (point))))
  20639. (defun org-get-last-sibling ()
  20640. "Move to previous heading of the same level, and return point.
  20641. If there is no such heading, return nil."
  20642. (let ((opoint (point))
  20643. (level (funcall outline-level)))
  20644. (outline-previous-heading)
  20645. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20646. (while (and (> (funcall outline-level) level)
  20647. (not (bobp)))
  20648. (outline-previous-heading))
  20649. (if (< (funcall outline-level) level)
  20650. nil
  20651. (point)))))
  20652. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20653. "Goto to the end of a subtree."
  20654. ;; This contains an exact copy of the original function, but it uses
  20655. ;; `org-back-to-heading', to make it work also in invisible
  20656. ;; trees. And is uses an invisible-ok argument.
  20657. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20658. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20659. ;; with many children and grandchildren etc, this can be much faster
  20660. ;; than the outline version.
  20661. (org-back-to-heading invisible-ok)
  20662. (let ((first t)
  20663. (level (funcall outline-level)))
  20664. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20665. ;; A true heading (not a plain list item), in Org-mode
  20666. ;; This means we can easily find the end by looking
  20667. ;; only for the right number of stars. Using a regexp to do
  20668. ;; this is so much faster than using a Lisp loop.
  20669. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20670. (forward-char 1)
  20671. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20672. ;; something else, do it the slow way
  20673. (while (and (not (eobp))
  20674. (or first (> (funcall outline-level) level)))
  20675. (setq first nil)
  20676. (outline-next-heading)))
  20677. (unless to-heading
  20678. (if (memq (preceding-char) '(?\n ?\^M))
  20679. (progn
  20680. ;; Go to end of line before heading
  20681. (forward-char -1)
  20682. (if (memq (preceding-char) '(?\n ?\^M))
  20683. ;; leave blank line before heading
  20684. (forward-char -1))))))
  20685. (point))
  20686. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20687. "Use Org version in org-mode, for dramatic speed-up."
  20688. (if (derived-mode-p 'org-mode)
  20689. (progn
  20690. (org-end-of-subtree nil t)
  20691. (unless (eobp) (backward-char 1)))
  20692. ad-do-it))
  20693. (defun org-end-of-meta-data-and-drawers ()
  20694. "Jump to the first text after meta data and drawers in the current entry.
  20695. This will move over empty lines, lines with planning time stamps,
  20696. clocking lines, and drawers."
  20697. (org-back-to-heading t)
  20698. (let ((end (save-excursion (outline-next-heading) (point)))
  20699. (re (concat "\\(" org-drawer-regexp "\\)"
  20700. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20701. (forward-line 1)
  20702. (while (re-search-forward re end t)
  20703. (if (not (match-end 1))
  20704. ;; empty or planning line
  20705. (forward-line 1)
  20706. ;; a drawer, find the end
  20707. (re-search-forward "^[ \t]*:END:" end 'move)
  20708. (forward-line 1)))
  20709. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20710. (point)))
  20711. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20712. "Move forward to the ARG'th subheading at same level as this one.
  20713. Stop at the first and last subheadings of a superior heading.
  20714. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20715. non-nil it will also look at invisible ones."
  20716. (interactive "p")
  20717. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20718. (if (and arg (< arg 0))
  20719. (goto-char (point-min))
  20720. (outline-next-heading))
  20721. (org-at-heading-p)
  20722. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20723. (f (if (and arg (< arg 0))
  20724. 're-search-backward
  20725. 're-search-forward))
  20726. (count (if arg (abs arg) 1))
  20727. (result (point)))
  20728. (forward-char (if (and arg (< arg 0)) -1 1))
  20729. (while (and (> count 0)
  20730. (funcall f org-outline-regexp-bol nil 'move))
  20731. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20732. (cond ((< l level) (setq count 0))
  20733. ((and (= l level)
  20734. (or invisible-ok
  20735. (progn
  20736. (goto-char (line-beginning-position))
  20737. (not (outline-invisible-p)))))
  20738. (setq count (1- count))
  20739. (when (eq l level)
  20740. (setq result (point)))))))
  20741. (goto-char result))
  20742. (beginning-of-line 1)))
  20743. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20744. "Move backward to the ARG'th subheading at same level as this one.
  20745. Stop at the first and last subheadings of a superior heading."
  20746. (interactive "p")
  20747. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20748. (defun org-next-block (arg &optional backward block-regexp)
  20749. "Jump to the next block.
  20750. With a prefix argument ARG, jump forward ARG many source blocks.
  20751. When BACKWARD is non-nil, jump to the previous block.
  20752. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20753. (interactive "p")
  20754. (let ((re (or block-regexp org-block-regexp))
  20755. (re-search-fn (or (and backward 're-search-backward)
  20756. 're-search-forward)))
  20757. (if (looking-at re) (forward-char 1))
  20758. (condition-case nil
  20759. (funcall re-search-fn re nil nil arg)
  20760. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20761. (goto-char (match-beginning 0)) (org-show-context)))
  20762. (defun org-previous-block (arg &optional block-regexp)
  20763. "Jump to the previous block.
  20764. With a prefix argument ARG, jump backward ARG many source blocks.
  20765. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20766. (interactive "p")
  20767. (org-next-block arg t block-regexp))
  20768. (defun org-forward-element ()
  20769. "Move forward by one element.
  20770. Move to the next element at the same level, when possible."
  20771. (interactive)
  20772. (cond ((eobp) (error "Cannot move further down"))
  20773. ((org-with-limited-levels (org-at-heading-p))
  20774. (let ((origin (point)))
  20775. (goto-char (org-end-of-subtree nil t))
  20776. (unless (org-with-limited-levels (org-at-heading-p))
  20777. (goto-char origin)
  20778. (error "Cannot move further down"))))
  20779. (t
  20780. (let* ((elem (org-element-at-point))
  20781. (end (org-element-property :end elem))
  20782. (parent (org-element-property :parent elem)))
  20783. (if (and parent (= (org-element-property :contents-end parent) end))
  20784. (goto-char (org-element-property :end parent))
  20785. (goto-char end))))))
  20786. (defun org-backward-element ()
  20787. "Move backward by one element.
  20788. Move to the previous element at the same level, when possible."
  20789. (interactive)
  20790. (cond ((bobp) (error "Cannot move further up"))
  20791. ((org-with-limited-levels (org-at-heading-p))
  20792. ;; At an headline, move to the previous one, if any, or stay
  20793. ;; here.
  20794. (let ((origin (point)))
  20795. (org-with-limited-levels (org-backward-heading-same-level 1))
  20796. ;; When current headline has no sibling above, move to its
  20797. ;; parent.
  20798. (when (= (point) origin)
  20799. (or (org-with-limited-levels (org-up-heading-safe))
  20800. (progn (goto-char origin)
  20801. (error "Cannot move further up"))))))
  20802. (t
  20803. (let* ((trail (org-element-at-point 'keep-trail))
  20804. (elem (car trail))
  20805. (prev-elem (nth 1 trail))
  20806. (beg (org-element-property :begin elem)))
  20807. (cond
  20808. ;; Move to beginning of current element if point isn't
  20809. ;; there already.
  20810. ((/= (point) beg) (goto-char beg))
  20811. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20812. ((org-before-first-heading-p) (goto-char (point-min)))
  20813. (t (org-back-to-heading)))))))
  20814. (defun org-up-element ()
  20815. "Move to upper element."
  20816. (interactive)
  20817. (if (org-with-limited-levels (org-at-heading-p))
  20818. (unless (org-up-heading-safe) (error "No surrounding element"))
  20819. (let* ((elem (org-element-at-point))
  20820. (parent (org-element-property :parent elem)))
  20821. (if parent (goto-char (org-element-property :begin parent))
  20822. (if (org-with-limited-levels (org-before-first-heading-p))
  20823. (error "No surrounding element")
  20824. (org-with-limited-levels (org-back-to-heading)))))))
  20825. (defvar org-element-greater-elements)
  20826. (defun org-down-element ()
  20827. "Move to inner element."
  20828. (interactive)
  20829. (let ((element (org-element-at-point)))
  20830. (cond
  20831. ((memq (org-element-type element) '(plain-list table))
  20832. (goto-char (org-element-property :contents-begin element))
  20833. (forward-char))
  20834. ((memq (org-element-type element) org-element-greater-elements)
  20835. ;; If contents are hidden, first disclose them.
  20836. (when (org-element-property :hiddenp element) (org-cycle))
  20837. (goto-char (or (org-element-property :contents-begin element)
  20838. (error "No content for this element"))))
  20839. (t (error "No inner element")))))
  20840. (defun org-drag-element-backward ()
  20841. "Move backward element at point."
  20842. (interactive)
  20843. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20844. (let* ((trail (org-element-at-point 'keep-trail))
  20845. (elem (car trail))
  20846. (prev-elem (nth 1 trail)))
  20847. ;; Error out if no previous element or previous element is
  20848. ;; a parent of the current one.
  20849. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20850. (error "Cannot drag element backward")
  20851. (let ((pos (point)))
  20852. (org-element-swap-A-B prev-elem elem)
  20853. (goto-char (+ (org-element-property :begin prev-elem)
  20854. (- pos (org-element-property :begin elem)))))))))
  20855. (defun org-drag-element-forward ()
  20856. "Move forward element at point."
  20857. (interactive)
  20858. (let* ((pos (point))
  20859. (elem (org-element-at-point)))
  20860. (when (= (point-max) (org-element-property :end elem))
  20861. (error "Cannot drag element forward"))
  20862. (goto-char (org-element-property :end elem))
  20863. (let ((next-elem (org-element-at-point)))
  20864. (when (or (org-element-nested-p elem next-elem)
  20865. (and (eq (org-element-type next-elem) 'headline)
  20866. (not (eq (org-element-type elem) 'headline))))
  20867. (goto-char pos)
  20868. (error "Cannot drag element forward"))
  20869. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20870. ;; body's length (without final blanks) and by the length of
  20871. ;; blanks between ELEM and NEXT-ELEM.
  20872. (let ((size-next (- (save-excursion
  20873. (goto-char (org-element-property :end next-elem))
  20874. (skip-chars-backward " \r\t\n")
  20875. (forward-line)
  20876. ;; Small correction if buffer doesn't end
  20877. ;; with a newline character.
  20878. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20879. (org-element-property :begin next-elem)))
  20880. (size-blank (- (org-element-property :end elem)
  20881. (save-excursion
  20882. (goto-char (org-element-property :end elem))
  20883. (skip-chars-backward " \r\t\n")
  20884. (forward-line)
  20885. (point)))))
  20886. (org-element-swap-A-B elem next-elem)
  20887. (goto-char (+ pos size-next size-blank))))))
  20888. (defun org-drag-line-forward (arg)
  20889. "Drag the line at point ARG lines forward."
  20890. (interactive "p")
  20891. (dotimes (n (abs arg))
  20892. (let ((c (current-column)))
  20893. (if (< 0 arg)
  20894. (progn
  20895. (beginning-of-line 2)
  20896. (transpose-lines 1)
  20897. (beginning-of-line 0))
  20898. (transpose-lines 1)
  20899. (beginning-of-line -1))
  20900. (org-move-to-column c))))
  20901. (defun org-drag-line-backward (arg)
  20902. "Drag the line at point ARG lines backward."
  20903. (interactive "p")
  20904. (org-drag-line-forward (- arg)))
  20905. (defun org-mark-element ()
  20906. "Put point at beginning of this element, mark at end.
  20907. Interactively, if this command is repeated or (in Transient Mark
  20908. mode) if the mark is active, it marks the next element after the
  20909. ones already marked."
  20910. (interactive)
  20911. (let (deactivate-mark)
  20912. (if (and (org-called-interactively-p 'any)
  20913. (or (and (eq last-command this-command) (mark t))
  20914. (and transient-mark-mode mark-active)))
  20915. (set-mark
  20916. (save-excursion
  20917. (goto-char (mark))
  20918. (goto-char (org-element-property :end (org-element-at-point)))))
  20919. (let ((element (org-element-at-point)))
  20920. (end-of-line)
  20921. (push-mark (org-element-property :end element) t t)
  20922. (goto-char (org-element-property :begin element))))))
  20923. (defun org-narrow-to-element ()
  20924. "Narrow buffer to current element."
  20925. (interactive)
  20926. (let ((elem (org-element-at-point)))
  20927. (cond
  20928. ((eq (car elem) 'headline)
  20929. (narrow-to-region
  20930. (org-element-property :begin elem)
  20931. (org-element-property :end elem)))
  20932. ((memq (car elem) org-element-greater-elements)
  20933. (narrow-to-region
  20934. (org-element-property :contents-begin elem)
  20935. (org-element-property :contents-end elem)))
  20936. (t
  20937. (narrow-to-region
  20938. (org-element-property :begin elem)
  20939. (org-element-property :end elem))))))
  20940. (defun org-transpose-element ()
  20941. "Transpose current and previous elements, keeping blank lines between.
  20942. Point is moved after both elements."
  20943. (interactive)
  20944. (org-skip-whitespace)
  20945. (let ((end (org-element-property :end (org-element-at-point))))
  20946. (org-drag-element-backward)
  20947. (goto-char end)))
  20948. (defun org-unindent-buffer ()
  20949. "Un-indent the visible part of the buffer.
  20950. Relative indentation (between items, inside blocks, etc.) isn't
  20951. modified."
  20952. (interactive)
  20953. (unless (eq major-mode 'org-mode)
  20954. (error "Cannot un-indent a buffer not in Org mode"))
  20955. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20956. unindent-tree ; For byte-compiler.
  20957. (unindent-tree
  20958. (function
  20959. (lambda (contents)
  20960. (mapc
  20961. (lambda (element)
  20962. (if (memq (org-element-type element) '(headline section))
  20963. (funcall unindent-tree (org-element-contents element))
  20964. (save-excursion
  20965. (save-restriction
  20966. (narrow-to-region
  20967. (org-element-property :begin element)
  20968. (org-element-property :end element))
  20969. (org-do-remove-indentation)))))
  20970. (reverse contents))))))
  20971. (funcall unindent-tree (org-element-contents parse-tree))))
  20972. (defun org-show-subtree ()
  20973. "Show everything after this heading at deeper levels."
  20974. (interactive)
  20975. (outline-flag-region
  20976. (point)
  20977. (save-excursion
  20978. (org-end-of-subtree t t))
  20979. nil))
  20980. (defun org-show-entry ()
  20981. "Show the body directly following this heading.
  20982. Show the heading too, if it is currently invisible."
  20983. (interactive)
  20984. (save-excursion
  20985. (condition-case nil
  20986. (progn
  20987. (org-back-to-heading t)
  20988. (outline-flag-region
  20989. (max (point-min) (1- (point)))
  20990. (save-excursion
  20991. (if (re-search-forward
  20992. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20993. (match-beginning 1)
  20994. (point-max)))
  20995. nil)
  20996. (org-cycle-hide-drawers 'children))
  20997. (error nil))))
  20998. (defun org-make-options-regexp (kwds &optional extra)
  20999. "Make a regular expression for keyword lines."
  21000. (concat
  21001. "^#\\+\\("
  21002. (mapconcat 'regexp-quote kwds "\\|")
  21003. (if extra (concat "\\|" extra))
  21004. "\\):[ \t]*\\(.*\\)"))
  21005. ;; Make isearch reveal the necessary context
  21006. (defun org-isearch-end ()
  21007. "Reveal context after isearch exits."
  21008. (when isearch-success ; only if search was successful
  21009. (if (featurep 'xemacs)
  21010. ;; Under XEmacs, the hook is run in the correct place,
  21011. ;; we directly show the context.
  21012. (org-show-context 'isearch)
  21013. ;; In Emacs the hook runs *before* restoring the overlays.
  21014. ;; So we have to use a one-time post-command-hook to do this.
  21015. ;; (Emacs 22 has a special variable, see function `org-mode')
  21016. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21017. isearch-mode-end-hook-quit)
  21018. ;; Only when the isearch was not quitted.
  21019. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21020. 'append 'local)))))
  21021. (defun org-isearch-post-command ()
  21022. "Remove self from hook, and show context."
  21023. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21024. (org-show-context 'isearch))
  21025. ;;;; Integration with and fixes for other packages
  21026. ;;; Imenu support
  21027. (defvar org-imenu-markers nil
  21028. "All markers currently used by Imenu.")
  21029. (make-variable-buffer-local 'org-imenu-markers)
  21030. (defun org-imenu-new-marker (&optional pos)
  21031. "Return a new marker for use by Imenu, and remember the marker."
  21032. (let ((m (make-marker)))
  21033. (move-marker m (or pos (point)))
  21034. (push m org-imenu-markers)
  21035. m))
  21036. (defun org-imenu-get-tree ()
  21037. "Produce the index for Imenu."
  21038. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21039. (setq org-imenu-markers nil)
  21040. (let* ((n org-imenu-depth)
  21041. (re (concat "^" (org-get-limited-outline-regexp)))
  21042. (subs (make-vector (1+ n) nil))
  21043. (last-level 0)
  21044. m level head)
  21045. (save-excursion
  21046. (save-restriction
  21047. (widen)
  21048. (goto-char (point-max))
  21049. (while (re-search-backward re nil t)
  21050. (setq level (org-reduced-level (funcall outline-level)))
  21051. (when (and (<= level n)
  21052. (looking-at org-complex-heading-regexp))
  21053. (setq head (org-link-display-format
  21054. (org-match-string-no-properties 4))
  21055. m (org-imenu-new-marker))
  21056. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21057. (if (>= level last-level)
  21058. (push (cons head m) (aref subs level))
  21059. (push (cons head (aref subs (1+ level))) (aref subs level))
  21060. (loop for i from (1+ level) to n do (aset subs i nil)))
  21061. (setq last-level level)))))
  21062. (aref subs 1)))
  21063. (eval-after-load "imenu"
  21064. '(progn
  21065. (add-hook 'imenu-after-jump-hook
  21066. (lambda ()
  21067. (if (derived-mode-p 'org-mode)
  21068. (org-show-context 'org-goto))))))
  21069. (defun org-link-display-format (link)
  21070. "Replace a link with either the description, or the link target
  21071. if no description is present"
  21072. (save-match-data
  21073. (if (string-match org-bracket-link-analytic-regexp link)
  21074. (replace-match (if (match-end 5)
  21075. (match-string 5 link)
  21076. (concat (match-string 1 link)
  21077. (match-string 3 link)))
  21078. nil t link)
  21079. link)))
  21080. (defun org-toggle-link-display ()
  21081. "Toggle the literal or descriptive display of links."
  21082. (interactive)
  21083. (if org-descriptive-links
  21084. (progn (org-remove-from-invisibility-spec '(org-link))
  21085. (org-restart-font-lock)
  21086. (setq org-descriptive-links nil))
  21087. (progn (add-to-invisibility-spec '(org-link))
  21088. (org-restart-font-lock)
  21089. (setq org-descriptive-links t))))
  21090. ;; Speedbar support
  21091. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21092. "Overlay marking the agenda restriction line in speedbar.")
  21093. (overlay-put org-speedbar-restriction-lock-overlay
  21094. 'face 'org-agenda-restriction-lock)
  21095. (overlay-put org-speedbar-restriction-lock-overlay
  21096. 'help-echo "Agendas are currently limited to this item.")
  21097. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21098. (defun org-speedbar-set-agenda-restriction ()
  21099. "Restrict future agenda commands to the location at point in speedbar.
  21100. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21101. (interactive)
  21102. (require 'org-agenda)
  21103. (let (p m tp np dir txt)
  21104. (cond
  21105. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21106. 'org-imenu t))
  21107. (setq m (get-text-property p 'org-imenu-marker))
  21108. (with-current-buffer (marker-buffer m)
  21109. (goto-char m)
  21110. (org-agenda-set-restriction-lock 'subtree)))
  21111. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21112. 'speedbar-function 'speedbar-find-file))
  21113. (setq tp (previous-single-property-change
  21114. (1+ p) 'speedbar-function)
  21115. np (next-single-property-change
  21116. tp 'speedbar-function)
  21117. dir (speedbar-line-directory)
  21118. txt (buffer-substring-no-properties (or tp (point-min))
  21119. (or np (point-max))))
  21120. (with-current-buffer (find-file-noselect
  21121. (let ((default-directory dir))
  21122. (expand-file-name txt)))
  21123. (unless (derived-mode-p 'org-mode)
  21124. (error "Cannot restrict to non-Org-mode file"))
  21125. (org-agenda-set-restriction-lock 'file)))
  21126. (t (error "Don't know how to restrict Org-mode's agenda")))
  21127. (move-overlay org-speedbar-restriction-lock-overlay
  21128. (point-at-bol) (point-at-eol))
  21129. (setq current-prefix-arg nil)
  21130. (org-agenda-maybe-redo)))
  21131. (eval-after-load "speedbar"
  21132. '(progn
  21133. (speedbar-add-supported-extension ".org")
  21134. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21135. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21136. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21137. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21138. (add-hook 'speedbar-visiting-tag-hook
  21139. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21140. ;;; Fixes and Hacks for problems with other packages
  21141. ;; Make flyspell not check words in links, to not mess up our keymap
  21142. (defvar org-element-affiliated-keywords) ; From org-element.el
  21143. (defvar org-element-block-name-alist) ; From org-element.el
  21144. (defun org-mode-flyspell-verify ()
  21145. "Don't let flyspell put overlays at active buttons, or on
  21146. {todo,all-time,additional-option-like}-keywords."
  21147. (let ((pos (max (1- (point)) (point-min)))
  21148. (word (thing-at-point 'word)))
  21149. (and (not (get-text-property pos 'keymap))
  21150. (not (get-text-property pos 'org-no-flyspell))
  21151. (not (member word org-todo-keywords-1))
  21152. (not (member word org-all-time-keywords))
  21153. (not (member word org-options-keywords))
  21154. (not (member word (mapcar 'car org-startup-options)))
  21155. (not (member-ignore-case word org-element-affiliated-keywords))
  21156. (not (member-ignore-case word (org-get-export-keywords)))
  21157. (not (member-ignore-case
  21158. word (mapcar 'car org-element-block-name-alist)))
  21159. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21160. (defun org-remove-flyspell-overlays-in (beg end)
  21161. "Remove flyspell overlays in region."
  21162. (and (org-bound-and-true-p flyspell-mode)
  21163. (fboundp 'flyspell-delete-region-overlays)
  21164. (flyspell-delete-region-overlays beg end))
  21165. (add-text-properties beg end '(org-no-flyspell t)))
  21166. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21167. (eval-after-load "bookmark"
  21168. '(if (boundp 'bookmark-after-jump-hook)
  21169. ;; We can use the hook
  21170. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21171. ;; Hook not available, use advice
  21172. (defadvice bookmark-jump (after org-make-visible activate)
  21173. "Make the position visible."
  21174. (org-bookmark-jump-unhide))))
  21175. ;; Make sure saveplace shows the location if it was hidden
  21176. (eval-after-load "saveplace"
  21177. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21178. "Make the position visible."
  21179. (org-bookmark-jump-unhide)))
  21180. ;; Make sure ecb shows the location if it was hidden
  21181. (eval-after-load "ecb"
  21182. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21183. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21184. (if (derived-mode-p 'org-mode)
  21185. (org-show-context))))
  21186. (defun org-bookmark-jump-unhide ()
  21187. "Unhide the current position, to show the bookmark location."
  21188. (and (derived-mode-p 'org-mode)
  21189. (or (outline-invisible-p)
  21190. (save-excursion (goto-char (max (point-min) (1- (point))))
  21191. (outline-invisible-p)))
  21192. (org-show-context 'bookmark-jump)))
  21193. ;; Make session.el ignore our circular variable
  21194. (eval-after-load "session"
  21195. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21196. ;;;; Experimental code
  21197. (defun org-closed-in-range ()
  21198. "Sparse tree of items closed in a certain time range.
  21199. Still experimental, may disappear in the future."
  21200. (interactive)
  21201. ;; Get the time interval from the user.
  21202. (let* ((time1 (org-float-time
  21203. (org-read-date nil 'to-time nil "Starting date: ")))
  21204. (time2 (org-float-time
  21205. (org-read-date nil 'to-time nil "End date:")))
  21206. ;; callback function
  21207. (callback (lambda ()
  21208. (let ((time
  21209. (org-float-time
  21210. (apply 'encode-time
  21211. (org-parse-time-string
  21212. (match-string 1))))))
  21213. ;; check if time in interval
  21214. (and (>= time time1) (<= time time2))))))
  21215. ;; make tree, check each match with the callback
  21216. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21217. ;;;; Finish up
  21218. (provide 'org)
  21219. (run-hooks 'org-load-hook)
  21220. ;;; org.el ends here