org.el 867 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961
  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. (require 'org-macs)
  74. (require 'org-compat)
  75. (let ((load-suffixes (list ".el")))
  76. (org-load-noerror-mustsuffix "org-loaddefs"))
  77. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  78. ;; some places -- e.g. see the macro org-with-limited-levels.
  79. ;;
  80. ;; In Org buffers, the value of `outline-regexp' is that of
  81. ;; `org-outline-regexp'. The only function still directly relying on
  82. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  83. ;; job when `orgstruct-mode' is active.
  84. (defvar org-outline-regexp "\\*+ "
  85. "Regexp to match Org headlines.")
  86. (defvar org-outline-regexp-bol "^\\*+ "
  87. "Regexp to match Org headlines.
  88. This is similar to `org-outline-regexp' but additionally makes
  89. sure that we are at the beginning of the line.")
  90. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  91. "Matches an headline, putting stars and text into groups.
  92. Stars are put in group 1 and the trimmed body in group 2.")
  93. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  94. (when (fboundp 'defvaralias)
  95. (unless (boundp 'calendar-view-holidays-initially-flag)
  96. (defvaralias 'calendar-view-holidays-initially-flag
  97. 'view-calendar-holidays-initially))
  98. (unless (boundp 'calendar-view-diary-initially-flag)
  99. (defvaralias 'calendar-view-diary-initially-flag
  100. 'view-diary-entries-initially))
  101. (unless (boundp 'diary-fancy-buffer)
  102. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  103. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  104. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  105. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  106. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  107. (declare-function org-at-clock-log-p "org-clock" ())
  108. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  109. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  110. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  111. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  112. (declare-function orgtbl-mode "org-table" (&optional arg))
  113. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  114. (declare-function org-beamer-mode "org-beamer" ())
  115. (declare-function org-table-edit-field "org-table" (arg))
  116. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  117. (declare-function org-id-get-create "org-id" (&optional force))
  118. (declare-function org-id-find-id-file "org-id" (id))
  119. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  120. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  121. (declare-function org-table-align "org-table" ())
  122. (declare-function org-table-paste-rectangle "org-table" ())
  123. (declare-function org-table-maybe-eval-formula "org-table" ())
  124. (declare-function org-table-maybe-recalculate-line "org-table" ())
  125. (autoload 'org-element-at-point "org-element")
  126. (autoload 'org-element-type "org-element")
  127. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  128. (declare-function org-element-type "org-element" (element))
  129. (declare-function org-element-context "org-element" ())
  130. (declare-function org-element-contents "org-element" (element))
  131. (declare-function org-element-property "org-element" (property element))
  132. (declare-function org-element-map "org-element" (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-swap-A-B "org-element" (elem-a elem-b))
  135. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  136. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  137. ;; load languages based on value of `org-babel-load-languages'
  138. (defvar org-babel-load-languages)
  139. ;;;###autoload
  140. (defun org-babel-do-load-languages (sym value)
  141. "Load the languages defined in `org-babel-load-languages'."
  142. (set-default sym value)
  143. (mapc (lambda (pair)
  144. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  145. (if active
  146. (progn
  147. (require (intern (concat "ob-" lang))))
  148. (progn
  149. (funcall 'fmakunbound
  150. (intern (concat "org-babel-execute:" lang)))
  151. (funcall 'fmakunbound
  152. (intern (concat "org-babel-expand-body:" lang)))))))
  153. org-babel-load-languages))
  154. (defcustom org-babel-load-languages '((emacs-lisp . t))
  155. "Languages which can be evaluated in Org-mode buffers.
  156. This list can be used to load support for any of the languages
  157. below, note that each language will depend on a different set of
  158. system executables and/or Emacs modes. When a language is
  159. \"loaded\", then code blocks in that language can be evaluated
  160. with `org-babel-execute-src-block' bound by default to C-c
  161. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  162. be set to remove code block evaluation from the C-c C-c
  163. keybinding. By default only Emacs Lisp (which has no
  164. requirements) is loaded."
  165. :group 'org-babel
  166. :set 'org-babel-do-load-languages
  167. :version "24.1"
  168. :type '(alist :tag "Babel Languages"
  169. :key-type
  170. (choice
  171. (const :tag "Awk" awk)
  172. (const :tag "C" C)
  173. (const :tag "R" R)
  174. (const :tag "Asymptote" asymptote)
  175. (const :tag "Calc" calc)
  176. (const :tag "Clojure" clojure)
  177. (const :tag "CSS" css)
  178. (const :tag "Ditaa" ditaa)
  179. (const :tag "Dot" dot)
  180. (const :tag "Emacs Lisp" emacs-lisp)
  181. (const :tag "Fortran" fortran)
  182. (const :tag "Gnuplot" gnuplot)
  183. (const :tag "Haskell" haskell)
  184. (const :tag "IO" io)
  185. (const :tag "Java" java)
  186. (const :tag "Javascript" js)
  187. (const :tag "LaTeX" latex)
  188. (const :tag "Ledger" ledger)
  189. (const :tag "Lilypond" lilypond)
  190. (const :tag "Lisp" lisp)
  191. (const :tag "Makefile" makefile)
  192. (const :tag "Maxima" maxima)
  193. (const :tag "Matlab" matlab)
  194. (const :tag "Mscgen" mscgen)
  195. (const :tag "Ocaml" ocaml)
  196. (const :tag "Octave" octave)
  197. (const :tag "Org" org)
  198. (const :tag "Perl" perl)
  199. (const :tag "Pico Lisp" picolisp)
  200. (const :tag "PlantUML" plantuml)
  201. (const :tag "Python" python)
  202. (const :tag "Ruby" ruby)
  203. (const :tag "Sass" sass)
  204. (const :tag "Scala" scala)
  205. (const :tag "Scheme" scheme)
  206. (const :tag "Screen" screen)
  207. (const :tag "Shell Script" sh)
  208. (const :tag "Shen" shen)
  209. (const :tag "Sql" sql)
  210. (const :tag "Sqlite" sqlite))
  211. :value-type (boolean :tag "Activate" :value t)))
  212. ;;;; Customization variables
  213. (defcustom org-clone-delete-id nil
  214. "Remove ID property of clones of a subtree.
  215. When non-nil, clones of a subtree don't inherit the ID property.
  216. Otherwise they inherit the ID property with a new unique
  217. identifier."
  218. :type 'boolean
  219. :version "24.1"
  220. :group 'org-id)
  221. ;;; Version
  222. (org-check-version)
  223. ;;;###autoload
  224. (defun org-version (&optional here full message)
  225. "Show the org-mode version in the echo area.
  226. With prefix argument HERE, insert it at point.
  227. When FULL is non-nil, use a verbose version string.
  228. When MESSAGE is non-nil, display a message with the version."
  229. (interactive "P")
  230. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  231. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  232. (load-suffixes (list ".el"))
  233. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  234. (org-trash (or
  235. (and (fboundp 'org-release) (fboundp 'org-git-version))
  236. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  237. (load-suffixes save-load-suffixes)
  238. (org-version (org-release))
  239. (git-version (org-git-version))
  240. (version (format "Org-mode version %s (%s @ %s)"
  241. org-version
  242. git-version
  243. (if org-install-dir
  244. (if (string= org-dir org-install-dir)
  245. org-install-dir
  246. (concat "mixed installation! " org-install-dir " and " org-dir))
  247. "org-loaddefs.el can not be found!")))
  248. (_version (if full version org-version)))
  249. (if (org-called-interactively-p 'interactive)
  250. (if here
  251. (insert version)
  252. (message version))
  253. (if message (message _version))
  254. _version)))
  255. (defconst org-version (org-version))
  256. ;;; Compatibility constants
  257. ;;; The custom variables
  258. (defgroup org nil
  259. "Outline-based notes management and organizer."
  260. :tag "Org"
  261. :group 'outlines
  262. :group 'calendar)
  263. (defcustom org-mode-hook nil
  264. "Mode hook for Org-mode, run after the mode was turned on."
  265. :group 'org
  266. :type 'hook)
  267. (defcustom org-load-hook nil
  268. "Hook that is run after org.el has been loaded."
  269. :group 'org
  270. :type 'hook)
  271. (defcustom org-log-buffer-setup-hook nil
  272. "Hook that is run after an Org log buffer is created."
  273. :group 'org
  274. :version "24.1"
  275. :type 'hook)
  276. (defvar org-modules) ; defined below
  277. (defvar org-modules-loaded nil
  278. "Have the modules been loaded already?")
  279. (defun org-load-modules-maybe (&optional force)
  280. "Load all extensions listed in `org-modules'."
  281. (when (or force (not org-modules-loaded))
  282. (mapc (lambda (ext)
  283. (condition-case nil (require ext)
  284. (error (message "Problems while trying to load feature `%s'" ext))))
  285. org-modules)
  286. (setq org-modules-loaded t)))
  287. (defun org-set-modules (var value)
  288. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  289. (set var value)
  290. (when (featurep 'org)
  291. (org-load-modules-maybe 'force)))
  292. (when (org-bound-and-true-p org-modules)
  293. (let ((a (member 'org-infojs org-modules)))
  294. (and a (setcar a 'org-jsinfo))))
  295. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  296. "Modules that should always be loaded together with org.el.
  297. If a description starts with <C>, the file is not part of Emacs
  298. and loading it will require that you have downloaded and properly installed
  299. the org-mode distribution.
  300. You can also use this system to load external packages (i.e. neither Org
  301. core modules, nor modules from the CONTRIB directory). Just add symbols
  302. to the end of the list. If the package is called org-xyz.el, then you need
  303. to add the symbol `xyz', and the package must have a call to
  304. (provide 'org-xyz)"
  305. :group 'org
  306. :set 'org-set-modules
  307. :type
  308. '(set :greedy t
  309. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  310. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  311. (const :tag " crypt: Encryption of subtrees" org-crypt)
  312. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  313. (const :tag " docview: Links to doc-view buffers" org-docview)
  314. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  315. (const :tag " id: Global IDs for identifying entries" org-id)
  316. (const :tag " info: Links to Info nodes" org-info)
  317. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  318. (const :tag " habit: Track your consistency with habits" org-habit)
  319. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  320. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  321. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  322. (const :tag " mew Links to Mew folders/messages" org-mew)
  323. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  324. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  325. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  326. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  327. (const :tag " vm: Links to VM folders/messages" org-vm)
  328. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  329. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  330. (const :tag " mouse: Additional mouse support" org-mouse)
  331. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  332. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  333. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  334. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  335. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  336. (const :tag "C collector: Collect properties into tables" org-collector)
  337. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  338. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  339. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  340. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  341. (const :tag "C eval: Include command output as text" org-eval)
  342. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  343. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  344. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  345. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  346. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  347. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  348. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  349. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  350. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  351. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  352. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  353. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  354. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  355. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  356. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  357. (const :tag "C registry: A registry for Org-mode links" org-registry)
  358. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  359. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  360. (const :tag "C secretary: Team management with org-mode" org-secretary)
  361. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  362. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  363. (const :tag "C track: Keep up with Org-mode development" org-track)
  364. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  365. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  366. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  367. (defcustom org-support-shift-select nil
  368. "Non-nil means make shift-cursor commands select text when possible.
  369. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  370. start selecting a region, or enlarge regions started in this way.
  371. In Org-mode, in special contexts, these same keys are used for
  372. other purposes, important enough to compete with shift selection.
  373. Org tries to balance these needs by supporting `shift-select-mode'
  374. outside these special contexts, under control of this variable.
  375. The default of this variable is nil, to avoid confusing behavior. Shifted
  376. cursor keys will then execute Org commands in the following contexts:
  377. - on a headline, changing TODO state (left/right) and priority (up/down)
  378. - on a time stamp, changing the time
  379. - in a plain list item, changing the bullet type
  380. - in a property definition line, switching between allowed values
  381. - in the BEGIN line of a clock table (changing the time block).
  382. Outside these contexts, the commands will throw an error.
  383. When this variable is t and the cursor is not in a special
  384. context, Org-mode will support shift-selection for making and
  385. enlarging regions. To make this more effective, the bullet
  386. cycling will no longer happen anywhere in an item line, but only
  387. if the cursor is exactly on the bullet.
  388. If you set this variable to the symbol `always', then the keys
  389. will not be special in headlines, property lines, and item lines,
  390. to make shift selection work there as well. If this is what you
  391. want, you can use the following alternative commands: `C-c C-t'
  392. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  393. can be used to switch TODO sets, `C-c -' to cycle item bullet
  394. types, and properties can be edited by hand or in column view.
  395. However, when the cursor is on a timestamp, shift-cursor commands
  396. will still edit the time stamp - this is just too good to give up.
  397. XEmacs user should have this variable set to nil, because
  398. `shift-select-mode' is in Emacs 23 or later only."
  399. :group 'org
  400. :type '(choice
  401. (const :tag "Never" nil)
  402. (const :tag "When outside special context" t)
  403. (const :tag "Everywhere except timestamps" always)))
  404. (defcustom org-loop-over-headlines-in-active-region nil
  405. "Shall some commands act upon headlines in the active region?
  406. When set to `t', some commands will be performed in all headlines
  407. within the active region.
  408. When set to `start-level', some commands will be performed in all
  409. headlines within the active region, provided that these headlines
  410. are of the same level than the first one.
  411. When set to a string, those commands will be performed on the
  412. matching headlines within the active region. Such string must be
  413. a tags/property/todo match as it is used in the agenda tags view.
  414. The list of commands is: `org-schedule', `org-deadline',
  415. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  416. `org-archive-to-archive-sibling'. The archiving commands skip
  417. already archived entries."
  418. :type '(choice (const :tag "Don't loop" nil)
  419. (const :tag "All headlines in active region" t)
  420. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  421. (string :tag "Tags/Property/Todo matcher"))
  422. :version "24.1"
  423. :group 'org-todo
  424. :group 'org-archive)
  425. (defgroup org-startup nil
  426. "Options concerning startup of Org-mode."
  427. :tag "Org Startup"
  428. :group 'org)
  429. (defcustom org-startup-folded t
  430. "Non-nil means entering Org-mode will switch to OVERVIEW.
  431. This can also be configured on a per-file basis by adding one of
  432. the following lines anywhere in the buffer:
  433. #+STARTUP: fold (or `overview', this is equivalent)
  434. #+STARTUP: nofold (or `showall', this is equivalent)
  435. #+STARTUP: content
  436. #+STARTUP: showeverything"
  437. :group 'org-startup
  438. :type '(choice
  439. (const :tag "nofold: show all" nil)
  440. (const :tag "fold: overview" t)
  441. (const :tag "content: all headlines" content)
  442. (const :tag "show everything, even drawers" showeverything)))
  443. (defcustom org-startup-truncated t
  444. "Non-nil means entering Org-mode will set `truncate-lines'.
  445. This is useful since some lines containing links can be very long and
  446. uninteresting. Also tables look terrible when wrapped."
  447. :group 'org-startup
  448. :type 'boolean)
  449. (defcustom org-startup-indented nil
  450. "Non-nil means turn on `org-indent-mode' on startup.
  451. This can also be configured on a per-file basis by adding one of
  452. the following lines anywhere in the buffer:
  453. #+STARTUP: indent
  454. #+STARTUP: noindent"
  455. :group 'org-structure
  456. :type '(choice
  457. (const :tag "Not" nil)
  458. (const :tag "Globally (slow on startup in large files)" t)))
  459. (defcustom org-use-sub-superscripts t
  460. "Non-nil means interpret \"_\" and \"^\" for export.
  461. When this option is turned on, you can use TeX-like syntax for sub- and
  462. superscripts. Several characters after \"_\" or \"^\" will be
  463. considered as a single item - so grouping with {} is normally not
  464. needed. For example, the following things will be parsed as single
  465. sub- or superscripts.
  466. 10^24 or 10^tau several digits will be considered 1 item.
  467. 10^-12 or 10^-tau a leading sign with digits or a word
  468. x^2-y^3 will be read as x^2 - y^3, because items are
  469. terminated by almost any nonword/nondigit char.
  470. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  471. Still, ambiguity is possible - so when in doubt use {} to enclose the
  472. sub/superscript. If you set this variable to the symbol `{}',
  473. the braces are *required* in order to trigger interpretations as
  474. sub/superscript. This can be helpful in documents that need \"_\"
  475. frequently in plain text.
  476. Not all export backends support this, but HTML does.
  477. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  478. :group 'org-startup
  479. :group 'org-export-translation
  480. :version "24.1"
  481. :type '(choice
  482. (const :tag "Always interpret" t)
  483. (const :tag "Only with braces" {})
  484. (const :tag "Never interpret" nil)))
  485. (if (fboundp 'defvaralias)
  486. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  487. (defcustom org-startup-with-beamer-mode nil
  488. "Non-nil means turn on `org-beamer-mode' on startup.
  489. This can also be configured on a per-file basis by adding one of
  490. the following lines anywhere in the buffer:
  491. #+STARTUP: beamer"
  492. :group 'org-startup
  493. :version "24.1"
  494. :type 'boolean)
  495. (defcustom org-startup-align-all-tables nil
  496. "Non-nil means align all tables when visiting a file.
  497. This is useful when the column width in tables is forced with <N> cookies
  498. in table fields. Such tables will look correct only after the first re-align.
  499. This can also be configured on a per-file basis by adding one of
  500. the following lines anywhere in the buffer:
  501. #+STARTUP: align
  502. #+STARTUP: noalign"
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-startup-with-inline-images nil
  506. "Non-nil means show inline images when loading a new Org file.
  507. This can also be configured on a per-file basis by adding one of
  508. the following lines anywhere in the buffer:
  509. #+STARTUP: inlineimages
  510. #+STARTUP: noinlineimages"
  511. :group 'org-startup
  512. :version "24.1"
  513. :type 'boolean)
  514. (defcustom org-insert-mode-line-in-empty-file nil
  515. "Non-nil means insert the first line setting Org-mode in empty files.
  516. When the function `org-mode' is called interactively in an empty file, this
  517. normally means that the file name does not automatically trigger Org-mode.
  518. To ensure that the file will always be in Org-mode in the future, a
  519. line enforcing Org-mode will be inserted into the buffer, if this option
  520. has been set."
  521. :group 'org-startup
  522. :type 'boolean)
  523. (defcustom org-replace-disputed-keys nil
  524. "Non-nil means use alternative key bindings for some keys.
  525. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  526. These keys are also used by other packages like shift-selection-mode'
  527. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  528. If you want to use Org-mode together with one of these other modes,
  529. or more generally if you would like to move some Org-mode commands to
  530. other keys, set this variable and configure the keys with the variable
  531. `org-disputed-keys'.
  532. This option is only relevant at load-time of Org-mode, and must be set
  533. *before* org.el is loaded. Changing it requires a restart of Emacs to
  534. become effective."
  535. :group 'org-startup
  536. :type 'boolean)
  537. (defcustom org-use-extra-keys nil
  538. "Non-nil means use extra key sequence definitions for certain commands.
  539. This happens automatically if you run XEmacs or if `window-system'
  540. is nil. This variable lets you do the same manually. You must
  541. set it before loading org.
  542. Example: on Carbon Emacs 22 running graphically, with an external
  543. keyboard on a Powerbook, the default way of setting M-left might
  544. not work for either Alt or ESC. Setting this variable will make
  545. it work for ESC."
  546. :group 'org-startup
  547. :type 'boolean)
  548. (if (fboundp 'defvaralias)
  549. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  550. (defcustom org-disputed-keys
  551. '(([(shift up)] . [(meta p)])
  552. ([(shift down)] . [(meta n)])
  553. ([(shift left)] . [(meta -)])
  554. ([(shift right)] . [(meta +)])
  555. ([(control shift right)] . [(meta shift +)])
  556. ([(control shift left)] . [(meta shift -)]))
  557. "Keys for which Org-mode and other modes compete.
  558. This is an alist, cars are the default keys, second element specifies
  559. the alternative to use when `org-replace-disputed-keys' is t.
  560. Keys can be specified in any syntax supported by `define-key'.
  561. The value of this option takes effect only at Org-mode's startup,
  562. therefore you'll have to restart Emacs to apply it after changing."
  563. :group 'org-startup
  564. :type 'alist)
  565. (defun org-key (key)
  566. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  567. Or return the original if not disputed.
  568. Also apply the translations defined in `org-xemacs-key-equivalents'."
  569. (when org-replace-disputed-keys
  570. (let* ((nkey (key-description key))
  571. (x (org-find-if (lambda (x)
  572. (equal (key-description (car x)) nkey))
  573. org-disputed-keys)))
  574. (setq key (if x (cdr x) key))))
  575. (when (featurep 'xemacs)
  576. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  577. key)
  578. (defun org-find-if (predicate seq)
  579. (catch 'exit
  580. (while seq
  581. (if (funcall predicate (car seq))
  582. (throw 'exit (car seq))
  583. (pop seq)))))
  584. (defun org-defkey (keymap key def)
  585. "Define a key, possibly translated, as returned by `org-key'."
  586. (define-key keymap (org-key key) def))
  587. (defcustom org-ellipsis nil
  588. "The ellipsis to use in the Org-mode outline.
  589. When nil, just use the standard three dots. When a string, use that instead,
  590. When a face, use the standard 3 dots, but with the specified face.
  591. The change affects only Org-mode (which will then use its own display table).
  592. Changing this requires executing `M-x org-mode' in a buffer to become
  593. effective."
  594. :group 'org-startup
  595. :type '(choice (const :tag "Default" nil)
  596. (face :tag "Face" :value org-warning)
  597. (string :tag "String" :value "...#")))
  598. (defvar org-display-table nil
  599. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  600. (defgroup org-keywords nil
  601. "Keywords in Org-mode."
  602. :tag "Org Keywords"
  603. :group 'org)
  604. (defcustom org-deadline-string "DEADLINE:"
  605. "String to mark deadline entries.
  606. A deadline is this string, followed by a time stamp. Should be a word,
  607. terminated by a colon. You can insert a schedule keyword and
  608. a timestamp with \\[org-deadline].
  609. Changes become only effective after restarting Emacs."
  610. :group 'org-keywords
  611. :type 'string)
  612. (defcustom org-scheduled-string "SCHEDULED:"
  613. "String to mark scheduled TODO entries.
  614. A schedule is this string, followed by a time stamp. Should be a word,
  615. terminated by a colon. You can insert a schedule keyword and
  616. a timestamp with \\[org-schedule].
  617. Changes become only effective after restarting Emacs."
  618. :group 'org-keywords
  619. :type 'string)
  620. (defcustom org-closed-string "CLOSED:"
  621. "String used as the prefix for timestamps logging closing a TODO entry."
  622. :group 'org-keywords
  623. :type 'string)
  624. (defcustom org-clock-string "CLOCK:"
  625. "String used as prefix for timestamps clocking work hours on an item."
  626. :group 'org-keywords
  627. :type 'string)
  628. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  629. org-scheduled-string "\\|"
  630. org-deadline-string "\\|"
  631. org-closed-string "\\|"
  632. org-clock-string "\\)")
  633. "Matches a line with planning or clock info.")
  634. (defcustom org-comment-string "COMMENT"
  635. "Entries starting with this keyword will never be exported.
  636. An entry can be toggled between COMMENT and normal with
  637. \\[org-toggle-comment].
  638. Changes become only effective after restarting Emacs."
  639. :group 'org-keywords
  640. :type 'string)
  641. (defcustom org-quote-string "QUOTE"
  642. "Entries starting with this keyword will be exported in fixed-width font.
  643. Quoting applies only to the text in the entry following the headline, and does
  644. not extend beyond the next headline, even if that is lower level.
  645. An entry can be toggled between QUOTE and normal with
  646. \\[org-toggle-fixed-width-section]."
  647. :group 'org-keywords
  648. :type 'string)
  649. (defconst org-repeat-re
  650. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  651. "Regular expression for specifying repeated events.
  652. After a match, group 1 contains the repeat expression.")
  653. (defgroup org-structure nil
  654. "Options concerning the general structure of Org-mode files."
  655. :tag "Org Structure"
  656. :group 'org)
  657. (defgroup org-reveal-location nil
  658. "Options about how to make context of a location visible."
  659. :tag "Org Reveal Location"
  660. :group 'org-structure)
  661. (defconst org-context-choice
  662. '(choice
  663. (const :tag "Always" t)
  664. (const :tag "Never" nil)
  665. (repeat :greedy t :tag "Individual contexts"
  666. (cons
  667. (choice :tag "Context"
  668. (const agenda)
  669. (const org-goto)
  670. (const occur-tree)
  671. (const tags-tree)
  672. (const link-search)
  673. (const mark-goto)
  674. (const bookmark-jump)
  675. (const isearch)
  676. (const default))
  677. (boolean))))
  678. "Contexts for the reveal options.")
  679. (defcustom org-show-hierarchy-above '((default . t))
  680. "Non-nil means show full hierarchy when revealing a location.
  681. Org-mode often shows locations in an org-mode file which might have
  682. been invisible before. When this is set, the hierarchy of headings
  683. above the exposed location is shown.
  684. Turning this off for example for sparse trees makes them very compact.
  685. Instead of t, this can also be an alist specifying this option for different
  686. contexts. Valid contexts are
  687. agenda when exposing an entry from the agenda
  688. org-goto when using the command `org-goto' on key C-c C-j
  689. occur-tree when using the command `org-occur' on key C-c /
  690. tags-tree when constructing a sparse tree based on tags matches
  691. link-search when exposing search matches associated with a link
  692. mark-goto when exposing the jump goal of a mark
  693. bookmark-jump when exposing a bookmark location
  694. isearch when exiting from an incremental search
  695. default default for all contexts not set explicitly"
  696. :group 'org-reveal-location
  697. :type org-context-choice)
  698. (defcustom org-show-following-heading '((default . nil))
  699. "Non-nil means show following heading when revealing a location.
  700. Org-mode often shows locations in an org-mode file which might have
  701. been invisible before. When this is set, the heading following the
  702. match is shown.
  703. Turning this off for example for sparse trees makes them very compact,
  704. but makes it harder to edit the location of the match. In such a case,
  705. use the command \\[org-reveal] to show more context.
  706. Instead of t, this can also be an alist specifying this option for different
  707. contexts. See `org-show-hierarchy-above' for valid contexts."
  708. :group 'org-reveal-location
  709. :type org-context-choice)
  710. (defcustom org-show-siblings '((default . nil) (isearch t))
  711. "Non-nil means show all sibling heading when revealing a location.
  712. Org-mode often shows locations in an org-mode file which might have
  713. been invisible before. When this is set, the sibling of the current entry
  714. heading are all made visible. If `org-show-hierarchy-above' is t,
  715. the same happens on each level of the hierarchy above the current entry.
  716. By default this is on for the isearch context, off for all other contexts.
  717. Turning this off for example for sparse trees makes them very compact,
  718. but makes it harder to edit the location of the match. In such a case,
  719. use the command \\[org-reveal] to show more context.
  720. Instead of t, this can also be an alist specifying this option for different
  721. contexts. See `org-show-hierarchy-above' for valid contexts."
  722. :group 'org-reveal-location
  723. :type org-context-choice)
  724. (defcustom org-show-entry-below '((default . nil))
  725. "Non-nil means show the entry below a headline when revealing a location.
  726. Org-mode often shows locations in an org-mode file which might have
  727. been invisible before. When this is set, the text below the headline that is
  728. exposed is also shown.
  729. By default this is off for all contexts.
  730. Instead of t, this can also be an alist specifying this option for different
  731. contexts. See `org-show-hierarchy-above' for valid contexts."
  732. :group 'org-reveal-location
  733. :type org-context-choice)
  734. (defcustom org-indirect-buffer-display 'other-window
  735. "How should indirect tree buffers be displayed?
  736. This applies to indirect buffers created with the commands
  737. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  738. Valid values are:
  739. current-window Display in the current window
  740. other-window Just display in another window.
  741. dedicated-frame Create one new frame, and re-use it each time.
  742. new-frame Make a new frame each time. Note that in this case
  743. previously-made indirect buffers are kept, and you need to
  744. kill these buffers yourself."
  745. :group 'org-structure
  746. :group 'org-agenda-windows
  747. :type '(choice
  748. (const :tag "In current window" current-window)
  749. (const :tag "In current frame, other window" other-window)
  750. (const :tag "Each time a new frame" new-frame)
  751. (const :tag "One dedicated frame" dedicated-frame)))
  752. (defcustom org-use-speed-commands nil
  753. "Non-nil means activate single letter commands at beginning of a headline.
  754. This may also be a function to test for appropriate locations where speed
  755. commands should be active."
  756. :group 'org-structure
  757. :type '(choice
  758. (const :tag "Never" nil)
  759. (const :tag "At beginning of headline stars" t)
  760. (function)))
  761. (defcustom org-speed-commands-user nil
  762. "Alist of additional speed commands.
  763. This list will be checked before `org-speed-commands-default'
  764. when the variable `org-use-speed-commands' is non-nil
  765. and when the cursor is at the beginning of a headline.
  766. The car if each entry is a string with a single letter, which must
  767. be assigned to `self-insert-command' in the global map.
  768. The cdr is either a command to be called interactively, a function
  769. to be called, or a form to be evaluated.
  770. An entry that is just a list with a single string will be interpreted
  771. as a descriptive headline that will be added when listing the speed
  772. commands in the Help buffer using the `?' speed command."
  773. :group 'org-structure
  774. :type '(repeat :value ("k" . ignore)
  775. (choice :value ("k" . ignore)
  776. (list :tag "Descriptive Headline" (string :tag "Headline"))
  777. (cons :tag "Letter and Command"
  778. (string :tag "Command letter")
  779. (choice
  780. (function)
  781. (sexp))))))
  782. (defgroup org-cycle nil
  783. "Options concerning visibility cycling in Org-mode."
  784. :tag "Org Cycle"
  785. :group 'org-structure)
  786. (defcustom org-cycle-skip-children-state-if-no-children t
  787. "Non-nil means skip CHILDREN state in entries that don't have any."
  788. :group 'org-cycle
  789. :type 'boolean)
  790. (defcustom org-cycle-max-level nil
  791. "Maximum level which should still be subject to visibility cycling.
  792. Levels higher than this will, for cycling, be treated as text, not a headline.
  793. When `org-odd-levels-only' is set, a value of N in this variable actually
  794. means 2N-1 stars as the limiting headline.
  795. When nil, cycle all levels.
  796. Note that the limiting level of cycling is also influenced by
  797. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  798. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  799. than its value."
  800. :group 'org-cycle
  801. :type '(choice
  802. (const :tag "No limit" nil)
  803. (integer :tag "Maximum level")))
  804. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  805. "Names of drawers. Drawers are not opened by cycling on the headline above.
  806. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  807. this:
  808. :DRAWERNAME:
  809. .....
  810. :END:
  811. The drawer \"PROPERTIES\" is special for capturing properties through
  812. the property API.
  813. Drawers can be defined on the per-file basis with a line like:
  814. #+DRAWERS: HIDDEN STATE PROPERTIES"
  815. :group 'org-structure
  816. :group 'org-cycle
  817. :type '(repeat (string :tag "Drawer Name")))
  818. (defcustom org-hide-block-startup nil
  819. "Non-nil means entering Org-mode will fold all blocks.
  820. This can also be set in on a per-file basis with
  821. #+STARTUP: hideblocks
  822. #+STARTUP: showblocks"
  823. :group 'org-startup
  824. :group 'org-cycle
  825. :type 'boolean)
  826. (defcustom org-cycle-global-at-bob nil
  827. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  828. This makes it possible to do global cycling without having to use S-TAB or
  829. \\[universal-argument] TAB. For this special case to work, the first line
  830. of the buffer must not be a headline -- it may be empty or some other text.
  831. When used in this way, `org-cycle-hook' is disabled temporarily to make
  832. sure the cursor stays at the beginning of the buffer. When this option is
  833. nil, don't do anything special at the beginning of the buffer."
  834. :group 'org-cycle
  835. :type 'boolean)
  836. (defcustom org-cycle-level-after-item/entry-creation t
  837. "Non-nil means cycle entry level or item indentation in new empty entries.
  838. When the cursor is at the end of an empty headline, i.e with only stars
  839. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  840. and then all possible ancestor states, before returning to the original state.
  841. This makes data entry extremely fast: M-RET to create a new headline,
  842. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  843. When the cursor is at the end of an empty plain list item, one TAB will
  844. make it a subitem, two or more tabs will back up to make this an item
  845. higher up in the item hierarchy."
  846. :group 'org-cycle
  847. :type 'boolean)
  848. (defcustom org-cycle-emulate-tab t
  849. "Where should `org-cycle' emulate TAB.
  850. nil Never
  851. white Only in completely white lines
  852. whitestart Only at the beginning of lines, before the first non-white char
  853. t Everywhere except in headlines
  854. exc-hl-bol Everywhere except at the start of a headline
  855. If TAB is used in a place where it does not emulate TAB, the current subtree
  856. visibility is cycled."
  857. :group 'org-cycle
  858. :type '(choice (const :tag "Never" nil)
  859. (const :tag "Only in completely white lines" white)
  860. (const :tag "Before first char in a line" whitestart)
  861. (const :tag "Everywhere except in headlines" t)
  862. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  863. ))
  864. (defcustom org-cycle-separator-lines 2
  865. "Number of empty lines needed to keep an empty line between collapsed trees.
  866. If you leave an empty line between the end of a subtree and the following
  867. headline, this empty line is hidden when the subtree is folded.
  868. Org-mode will leave (exactly) one empty line visible if the number of
  869. empty lines is equal or larger to the number given in this variable.
  870. So the default 2 means at least 2 empty lines after the end of a subtree
  871. are needed to produce free space between a collapsed subtree and the
  872. following headline.
  873. If the number is negative, and the number of empty lines is at least -N,
  874. all empty lines are shown.
  875. Special case: when 0, never leave empty lines in collapsed view."
  876. :group 'org-cycle
  877. :type 'integer)
  878. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  879. (defcustom org-pre-cycle-hook nil
  880. "Hook that is run before visibility cycling is happening.
  881. The function(s) in this hook must accept a single argument which indicates
  882. the new state that will be set right after running this hook. The
  883. argument is a symbol. Before a global state change, it can have the values
  884. `overview', `content', or `all'. Before a local state change, it can have
  885. the values `folded', `children', or `subtree'."
  886. :group 'org-cycle
  887. :type 'hook)
  888. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  889. org-cycle-hide-drawers
  890. org-cycle-hide-inline-tasks
  891. org-cycle-show-empty-lines
  892. org-optimize-window-after-visibility-change)
  893. "Hook that is run after `org-cycle' has changed the buffer visibility.
  894. The function(s) in this hook must accept a single argument which indicates
  895. the new state that was set by the most recent `org-cycle' command. The
  896. argument is a symbol. After a global state change, it can have the values
  897. `overview', `contents', or `all'. After a local state change, it can have
  898. the values `folded', `children', or `subtree'."
  899. :group 'org-cycle
  900. :type 'hook)
  901. (defgroup org-edit-structure nil
  902. "Options concerning structure editing in Org-mode."
  903. :tag "Org Edit Structure"
  904. :group 'org-structure)
  905. (defcustom org-odd-levels-only nil
  906. "Non-nil means skip even levels and only use odd levels for the outline.
  907. This has the effect that two stars are being added/taken away in
  908. promotion/demotion commands. It also influences how levels are
  909. handled by the exporters.
  910. Changing it requires restart of `font-lock-mode' to become effective
  911. for fontification also in regions already fontified.
  912. You may also set this on a per-file basis by adding one of the following
  913. lines to the buffer:
  914. #+STARTUP: odd
  915. #+STARTUP: oddeven"
  916. :group 'org-edit-structure
  917. :group 'org-appearance
  918. :type 'boolean)
  919. (defcustom org-adapt-indentation t
  920. "Non-nil means adapt indentation to outline node level.
  921. When this variable is set, Org assumes that you write outlines by
  922. indenting text in each node to align with the headline (after the stars).
  923. The following issues are influenced by this variable:
  924. - When this is set and the *entire* text in an entry is indented, the
  925. indentation is increased by one space in a demotion command, and
  926. decreased by one in a promotion command. If any line in the entry
  927. body starts with text at column 0, indentation is not changed at all.
  928. - Property drawers and planning information is inserted indented when
  929. this variable s set. When nil, they will not be indented.
  930. - TAB indents a line relative to context. The lines below a headline
  931. will be indented when this variable is set.
  932. Note that this is all about true indentation, by adding and removing
  933. space characters. See also `org-indent.el' which does level-dependent
  934. indentation in a virtual way, i.e. at display time in Emacs."
  935. :group 'org-edit-structure
  936. :type 'boolean)
  937. (defcustom org-special-ctrl-a/e nil
  938. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  939. When t, `C-a' will bring back the cursor to the beginning of the
  940. headline text, i.e. after the stars and after a possible TODO
  941. keyword. In an item, this will be the position after bullet and
  942. check-box, if any. When the cursor is already at that position,
  943. another `C-a' will bring it to the beginning of the line.
  944. `C-e' will jump to the end of the headline, ignoring the presence
  945. of tags in the headline. A second `C-e' will then jump to the
  946. true end of the line, after any tags. This also means that, when
  947. this variable is non-nil, `C-e' also will never jump beyond the
  948. end of the heading of a folded section, i.e. not after the
  949. ellipses.
  950. When set to the symbol `reversed', the first `C-a' or `C-e' works
  951. normally, going to the true line boundary first. Only a directly
  952. following, identical keypress will bring the cursor to the
  953. special positions.
  954. This may also be a cons cell where the behavior for `C-a' and
  955. `C-e' is set separately."
  956. :group 'org-edit-structure
  957. :type '(choice
  958. (const :tag "off" nil)
  959. (const :tag "on: after stars/bullet and before tags first" t)
  960. (const :tag "reversed: true line boundary first" reversed)
  961. (cons :tag "Set C-a and C-e separately"
  962. (choice :tag "Special C-a"
  963. (const :tag "off" nil)
  964. (const :tag "on: after stars/bullet first" t)
  965. (const :tag "reversed: before stars/bullet first" reversed))
  966. (choice :tag "Special C-e"
  967. (const :tag "off" nil)
  968. (const :tag "on: before tags first" t)
  969. (const :tag "reversed: after tags first" reversed)))))
  970. (if (fboundp 'defvaralias)
  971. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  972. (defcustom org-special-ctrl-k nil
  973. "Non-nil means `C-k' will behave specially in headlines.
  974. When nil, `C-k' will call the default `kill-line' command.
  975. When t, the following will happen while the cursor is in the headline:
  976. - When the cursor is at the beginning of a headline, kill the entire
  977. line and possible the folded subtree below the line.
  978. - When in the middle of the headline text, kill the headline up to the tags.
  979. - When after the headline text, kill the tags."
  980. :group 'org-edit-structure
  981. :type 'boolean)
  982. (defcustom org-ctrl-k-protect-subtree nil
  983. "Non-nil means, do not delete a hidden subtree with C-k.
  984. When set to the symbol `error', simply throw an error when C-k is
  985. used to kill (part-of) a headline that has hidden text behind it.
  986. Any other non-nil value will result in a query to the user, if it is
  987. OK to kill that hidden subtree. When nil, kill without remorse."
  988. :group 'org-edit-structure
  989. :version "24.1"
  990. :type '(choice
  991. (const :tag "Do not protect hidden subtrees" nil)
  992. (const :tag "Protect hidden subtrees with a security query" t)
  993. (const :tag "Never kill a hidden subtree with C-k" error)))
  994. (defcustom org-catch-invisible-edits nil
  995. "Check if in invisible region before inserting or deleting a character.
  996. Valid values are:
  997. nil Do not check, so just do invisible edits.
  998. error Throw an error and do nothing.
  999. show Make point visible, and do the requested edit.
  1000. show-and-error Make point visible, then throw an error and abort the edit.
  1001. smart Make point visible, and do insertion/deletion if it is
  1002. adjacent to visible text and the change feels predictable.
  1003. Never delete a previously invisible character or add in the
  1004. middle or right after an invisible region. Basically, this
  1005. allows insertion and backward-delete right before ellipses.
  1006. FIXME: maybe in this case we should not even show?"
  1007. :group 'org-edit-structure
  1008. :version "24.1"
  1009. :type '(choice
  1010. (const :tag "Do not check" nil)
  1011. (const :tag "Throw error when trying to edit" error)
  1012. (const :tag "Unhide, but do not do the edit" show-and-error)
  1013. (const :tag "Show invisible part and do the edit" show)
  1014. (const :tag "Be smart and do the right thing" smart)))
  1015. (defcustom org-yank-folded-subtrees t
  1016. "Non-nil means when yanking subtrees, fold them.
  1017. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1018. it starts with a heading and all other headings in it are either children
  1019. or siblings, then fold all the subtrees. However, do this only if no
  1020. text after the yank would be swallowed into a folded tree by this action."
  1021. :group 'org-edit-structure
  1022. :type 'boolean)
  1023. (defcustom org-yank-adjusted-subtrees nil
  1024. "Non-nil means when yanking subtrees, adjust the level.
  1025. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1026. this function for details."
  1027. :group 'org-edit-structure
  1028. :type 'boolean)
  1029. (defcustom org-M-RET-may-split-line '((default . t))
  1030. "Non-nil means M-RET will split the line at the cursor position.
  1031. When nil, it will go to the end of the line before making a
  1032. new line.
  1033. You may also set this option in a different way for different
  1034. contexts. Valid contexts are:
  1035. headline when creating a new headline
  1036. item when creating a new item
  1037. table in a table field
  1038. default the value to be used for all contexts not explicitly
  1039. customized"
  1040. :group 'org-structure
  1041. :group 'org-table
  1042. :type '(choice
  1043. (const :tag "Always" t)
  1044. (const :tag "Never" nil)
  1045. (repeat :greedy t :tag "Individual contexts"
  1046. (cons
  1047. (choice :tag "Context"
  1048. (const headline)
  1049. (const item)
  1050. (const table)
  1051. (const default))
  1052. (boolean)))))
  1053. (defcustom org-insert-heading-respect-content nil
  1054. "Non-nil means insert new headings after the current subtree.
  1055. When nil, the new heading is created directly after the current line.
  1056. The commands \\[org-insert-heading-respect-content] and
  1057. \\[org-insert-todo-heading-respect-content] turn this variable on
  1058. for the duration of the command."
  1059. :group 'org-structure
  1060. :type 'boolean)
  1061. (defcustom org-blank-before-new-entry '((heading . auto)
  1062. (plain-list-item . auto))
  1063. "Should `org-insert-heading' leave a blank line before new heading/item?
  1064. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1065. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1066. which case Org will look at the surrounding headings/items and try to
  1067. make an intelligent decision whether to insert a blank line or not.
  1068. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1069. set, the setting here is ignored and no empty line is inserted, to avoid
  1070. breaking the list structure."
  1071. :group 'org-edit-structure
  1072. :type '(list
  1073. (cons (const heading)
  1074. (choice (const :tag "Never" nil)
  1075. (const :tag "Always" t)
  1076. (const :tag "Auto" auto)))
  1077. (cons (const plain-list-item)
  1078. (choice (const :tag "Never" nil)
  1079. (const :tag "Always" t)
  1080. (const :tag "Auto" auto)))))
  1081. (defcustom org-insert-heading-hook nil
  1082. "Hook being run after inserting a new heading."
  1083. :group 'org-edit-structure
  1084. :type 'hook)
  1085. (defcustom org-enable-fixed-width-editor t
  1086. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1087. This currently only means they are never auto-wrapped.
  1088. When nil, such lines will be treated like ordinary lines.
  1089. See also the QUOTE keyword."
  1090. :group 'org-edit-structure
  1091. :type 'boolean)
  1092. (defcustom org-goto-auto-isearch t
  1093. "Non-nil means typing characters in `org-goto' starts incremental search.
  1094. When nil, you can use these keybindings to navigate the buffer:
  1095. q Quit the org-goto interface
  1096. n Go to the next visible heading
  1097. p Go to the previous visible heading
  1098. f Go one heading forward on same level
  1099. b Go one heading backward on same level
  1100. u Go one heading up"
  1101. :group 'org-edit-structure
  1102. :type 'boolean)
  1103. (defgroup org-sparse-trees nil
  1104. "Options concerning sparse trees in Org-mode."
  1105. :tag "Org Sparse Trees"
  1106. :group 'org-structure)
  1107. (defcustom org-highlight-sparse-tree-matches t
  1108. "Non-nil means highlight all matches that define a sparse tree.
  1109. The highlights will automatically disappear the next time the buffer is
  1110. changed by an edit command."
  1111. :group 'org-sparse-trees
  1112. :type 'boolean)
  1113. (defcustom org-remove-highlights-with-change t
  1114. "Non-nil means any change to the buffer will remove temporary highlights.
  1115. Such highlights are created by `org-occur' and `org-clock-display'.
  1116. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1117. The highlights created by `org-preview-latex-fragment' always need
  1118. `C-c C-c' to be removed."
  1119. :group 'org-sparse-trees
  1120. :group 'org-time
  1121. :type 'boolean)
  1122. (defcustom org-occur-hook '(org-first-headline-recenter)
  1123. "Hook that is run after `org-occur' has constructed a sparse tree.
  1124. This can be used to recenter the window to show as much of the structure
  1125. as possible."
  1126. :group 'org-sparse-trees
  1127. :type 'hook)
  1128. (defgroup org-imenu-and-speedbar nil
  1129. "Options concerning imenu and speedbar in Org-mode."
  1130. :tag "Org Imenu and Speedbar"
  1131. :group 'org-structure)
  1132. (defcustom org-imenu-depth 2
  1133. "The maximum level for Imenu access to Org-mode headlines.
  1134. This also applied for speedbar access."
  1135. :group 'org-imenu-and-speedbar
  1136. :type 'integer)
  1137. (defgroup org-table nil
  1138. "Options concerning tables in Org-mode."
  1139. :tag "Org Table"
  1140. :group 'org)
  1141. (defcustom org-enable-table-editor 'optimized
  1142. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1143. When nil, such lines will be treated like ordinary lines.
  1144. When equal to the symbol `optimized', the table editor will be optimized to
  1145. do the following:
  1146. - Automatic overwrite mode in front of whitespace in table fields.
  1147. This makes the structure of the table stay in tact as long as the edited
  1148. field does not exceed the column width.
  1149. - Minimize the number of realigns. Normally, the table is aligned each time
  1150. TAB or RET are pressed to move to another field. With optimization this
  1151. happens only if changes to a field might have changed the column width.
  1152. Optimization requires replacing the functions `self-insert-command',
  1153. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1154. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1155. very good at guessing when a re-align will be necessary, but you can always
  1156. force one with \\[org-ctrl-c-ctrl-c].
  1157. If you would like to use the optimized version in Org-mode, but the
  1158. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1159. This variable can be used to turn on and off the table editor during a session,
  1160. but in order to toggle optimization, a restart is required.
  1161. See also the variable `org-table-auto-blank-field'."
  1162. :group 'org-table
  1163. :type '(choice
  1164. (const :tag "off" nil)
  1165. (const :tag "on" t)
  1166. (const :tag "on, optimized" optimized)))
  1167. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1168. (version<= emacs-version "24.1"))
  1169. "Non-nil means cluster self-insert commands for undo when possible.
  1170. If this is set, then, like in the Emacs command loop, 20 consecutive
  1171. characters will be undone together.
  1172. This is configurable, because there is some impact on typing performance."
  1173. :group 'org-table
  1174. :type 'boolean)
  1175. (defcustom org-table-tab-recognizes-table.el t
  1176. "Non-nil means TAB will automatically notice a table.el table.
  1177. When it sees such a table, it moves point into it and - if necessary -
  1178. calls `table-recognize-table'."
  1179. :group 'org-table-editing
  1180. :type 'boolean)
  1181. (defgroup org-link nil
  1182. "Options concerning links in Org-mode."
  1183. :tag "Org Link"
  1184. :group 'org)
  1185. (defvar org-link-abbrev-alist-local nil
  1186. "Buffer-local version of `org-link-abbrev-alist', which see.
  1187. The value of this is taken from the #+LINK lines.")
  1188. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1189. (defcustom org-link-abbrev-alist nil
  1190. "Alist of link abbreviations.
  1191. The car of each element is a string, to be replaced at the start of a link.
  1192. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1193. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1194. [[linkkey:tag][description]]
  1195. The 'linkkey' must be a word word, starting with a letter, followed
  1196. by letters, numbers, '-' or '_'.
  1197. If REPLACE is a string, the tag will simply be appended to create the link.
  1198. If the string contains \"%s\", the tag will be inserted there. If the string
  1199. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1200. at that point (see the function `url-hexify-string'). If the string contains
  1201. the specifier \"%(my-function)\", then the custom function `my-function' will
  1202. be invoked: this function takes the tag as its only argument and must return
  1203. a string.
  1204. REPLACE may also be a function that will be called with the tag as the
  1205. only argument to create the link, which should be returned as a string.
  1206. See the manual for examples."
  1207. :group 'org-link
  1208. :type '(repeat
  1209. (cons
  1210. (string :tag "Protocol")
  1211. (choice
  1212. (string :tag "Format")
  1213. (function)))))
  1214. (defcustom org-descriptive-links t
  1215. "Non-nil means Org will display descriptive links.
  1216. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1217. \"Org Website\", hiding the link itself and just displaying its
  1218. description. When set to `nil', Org will display the full links
  1219. literally.
  1220. You can interactively set the value of this variable by calling
  1221. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1222. :group 'org-link
  1223. :type 'boolean)
  1224. (defcustom org-link-file-path-type 'adaptive
  1225. "How the path name in file links should be stored.
  1226. Valid values are:
  1227. relative Relative to the current directory, i.e. the directory of the file
  1228. into which the link is being inserted.
  1229. absolute Absolute path, if possible with ~ for home directory.
  1230. noabbrev Absolute path, no abbreviation of home directory.
  1231. adaptive Use relative path for files in the current directory and sub-
  1232. directories of it. For other files, use an absolute path."
  1233. :group 'org-link
  1234. :type '(choice
  1235. (const relative)
  1236. (const absolute)
  1237. (const noabbrev)
  1238. (const adaptive)))
  1239. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1240. "Types of links that should be activated in Org-mode files.
  1241. This is a list of symbols, each leading to the activation of a certain link
  1242. type. In principle, it does not hurt to turn on most link types - there may
  1243. be a small gain when turning off unused link types. The types are:
  1244. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1245. angle Links in angular brackets that may contain whitespace like
  1246. <bbdb:Carsten Dominik>.
  1247. plain Plain links in normal text, no whitespace, like http://google.com.
  1248. radio Text that is matched by a radio target, see manual for details.
  1249. tag Tag settings in a headline (link to tag search).
  1250. date Time stamps (link to calendar).
  1251. footnote Footnote labels.
  1252. Changing this variable requires a restart of Emacs to become effective."
  1253. :group 'org-link
  1254. :type '(set :greedy t
  1255. (const :tag "Double bracket links" bracket)
  1256. (const :tag "Angular bracket links" angle)
  1257. (const :tag "Plain text links" plain)
  1258. (const :tag "Radio target matches" radio)
  1259. (const :tag "Tags" tag)
  1260. (const :tag "Timestamps" date)
  1261. (const :tag "Footnotes" footnote)))
  1262. (defcustom org-make-link-description-function nil
  1263. "Function to use for generating link descriptions from links.
  1264. When nil, the link location will be used. This function must take
  1265. two parameters: the first one is the link, the second one is the
  1266. description generated by `org-insert-link'. The function should
  1267. return the description to use."
  1268. :group 'org-link
  1269. :type 'function)
  1270. (defgroup org-link-store nil
  1271. "Options concerning storing links in Org-mode."
  1272. :tag "Org Store Link"
  1273. :group 'org-link)
  1274. (defcustom org-url-hexify-p t
  1275. "When non-nil, hexify URL when creating a link."
  1276. :type 'boolean
  1277. :version "24.3"
  1278. :group 'org-link-store)
  1279. (defcustom org-email-link-description-format "Email %c: %.30s"
  1280. "Format of the description part of a link to an email or usenet message.
  1281. The following %-escapes will be replaced by corresponding information:
  1282. %F full \"From\" field
  1283. %f name, taken from \"From\" field, address if no name
  1284. %T full \"To\" field
  1285. %t first name in \"To\" field, address if no name
  1286. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1287. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1288. %s subject
  1289. %d date
  1290. %m message-id.
  1291. You may use normal field width specification between the % and the letter.
  1292. This is for example useful to limit the length of the subject.
  1293. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1294. :group 'org-link-store
  1295. :type 'string)
  1296. (defcustom org-from-is-user-regexp
  1297. (let (r1 r2)
  1298. (when (and user-mail-address (not (string= user-mail-address "")))
  1299. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1300. (when (and user-full-name (not (string= user-full-name "")))
  1301. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1302. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1303. "Regexp matched against the \"From:\" header of an email or usenet message.
  1304. It should match if the message is from the user him/herself."
  1305. :group 'org-link-store
  1306. :type 'regexp)
  1307. (defcustom org-context-in-file-links t
  1308. "Non-nil means file links from `org-store-link' contain context.
  1309. A search string will be added to the file name with :: as separator and
  1310. used to find the context when the link is activated by the command
  1311. `org-open-at-point'. When this option is t, the entire active region
  1312. will be placed in the search string of the file link. If set to a
  1313. positive integer, only the first n lines of context will be stored.
  1314. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1315. negates this setting for the duration of the command."
  1316. :group 'org-link-store
  1317. :type '(choice boolean integer))
  1318. (defcustom org-keep-stored-link-after-insertion nil
  1319. "Non-nil means keep link in list for entire session.
  1320. The command `org-store-link' adds a link pointing to the current
  1321. location to an internal list. These links accumulate during a session.
  1322. The command `org-insert-link' can be used to insert links into any
  1323. Org-mode file (offering completion for all stored links). When this
  1324. option is nil, every link which has been inserted once using \\[org-insert-link]
  1325. will be removed from the list, to make completing the unused links
  1326. more efficient."
  1327. :group 'org-link-store
  1328. :type 'boolean)
  1329. (defgroup org-link-follow nil
  1330. "Options concerning following links in Org-mode."
  1331. :tag "Org Follow Link"
  1332. :group 'org-link)
  1333. (defcustom org-link-translation-function nil
  1334. "Function to translate links with different syntax to Org syntax.
  1335. This can be used to translate links created for example by the Planner
  1336. or emacs-wiki packages to Org syntax.
  1337. The function must accept two parameters, a TYPE containing the link
  1338. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1339. which is everything after the link protocol. It should return a cons
  1340. with possibly modified values of type and path.
  1341. Org contains a function for this, so if you set this variable to
  1342. `org-translate-link-from-planner', you should be able follow many
  1343. links created by planner."
  1344. :group 'org-link-follow
  1345. :type 'function)
  1346. (defcustom org-follow-link-hook nil
  1347. "Hook that is run after a link has been followed."
  1348. :group 'org-link-follow
  1349. :type 'hook)
  1350. (defcustom org-tab-follows-link nil
  1351. "Non-nil means on links TAB will follow the link.
  1352. Needs to be set before org.el is loaded.
  1353. This really should not be used, it does not make sense, and the
  1354. implementation is bad."
  1355. :group 'org-link-follow
  1356. :type 'boolean)
  1357. (defcustom org-return-follows-link nil
  1358. "Non-nil means on links RET will follow the link."
  1359. :group 'org-link-follow
  1360. :type 'boolean)
  1361. (defcustom org-mouse-1-follows-link
  1362. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1363. "Non-nil means mouse-1 on a link will follow the link.
  1364. A longer mouse click will still set point. Does not work on XEmacs.
  1365. Needs to be set before org.el is loaded."
  1366. :group 'org-link-follow
  1367. :type 'boolean)
  1368. (defcustom org-mark-ring-length 4
  1369. "Number of different positions to be recorded in the ring.
  1370. Changing this requires a restart of Emacs to work correctly."
  1371. :group 'org-link-follow
  1372. :type 'integer)
  1373. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1374. "Non-nil means internal links in Org files must exactly match a headline.
  1375. When nil, the link search tries to match a phrase with all words
  1376. in the search text."
  1377. :group 'org-link-follow
  1378. :version "24.1"
  1379. :type '(choice
  1380. (const :tag "Use fuzzy text search" nil)
  1381. (const :tag "Match only exact headline" t)
  1382. (const :tag "Match exact headline or query to create it"
  1383. query-to-create)))
  1384. (defcustom org-link-frame-setup
  1385. '((vm . vm-visit-folder-other-frame)
  1386. (vm-imap . vm-visit-imap-folder-other-frame)
  1387. (gnus . org-gnus-no-new-news)
  1388. (file . find-file-other-window)
  1389. (wl . wl-other-frame))
  1390. "Setup the frame configuration for following links.
  1391. When following a link with Emacs, it may often be useful to display
  1392. this link in another window or frame. This variable can be used to
  1393. set this up for the different types of links.
  1394. For VM, use any of
  1395. `vm-visit-folder'
  1396. `vm-visit-folder-other-window'
  1397. `vm-visit-folder-other-frame'
  1398. For Gnus, use any of
  1399. `gnus'
  1400. `gnus-other-frame'
  1401. `org-gnus-no-new-news'
  1402. For FILE, use any of
  1403. `find-file'
  1404. `find-file-other-window'
  1405. `find-file-other-frame'
  1406. For Wanderlust use any of
  1407. `wl'
  1408. `wl-other-frame'
  1409. For the calendar, use the variable `calendar-setup'.
  1410. For BBDB, it is currently only possible to display the matches in
  1411. another window."
  1412. :group 'org-link-follow
  1413. :type '(list
  1414. (cons (const vm)
  1415. (choice
  1416. (const vm-visit-folder)
  1417. (const vm-visit-folder-other-window)
  1418. (const vm-visit-folder-other-frame)))
  1419. (cons (const gnus)
  1420. (choice
  1421. (const gnus)
  1422. (const gnus-other-frame)
  1423. (const org-gnus-no-new-news)))
  1424. (cons (const file)
  1425. (choice
  1426. (const find-file)
  1427. (const find-file-other-window)
  1428. (const find-file-other-frame)))
  1429. (cons (const wl)
  1430. (choice
  1431. (const wl)
  1432. (const wl-other-frame)))))
  1433. (defcustom org-display-internal-link-with-indirect-buffer nil
  1434. "Non-nil means use indirect buffer to display infile links.
  1435. Activating internal links (from one location in a file to another location
  1436. in the same file) normally just jumps to the location. When the link is
  1437. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1438. is displayed in
  1439. another window. When this option is set, the other window actually displays
  1440. an indirect buffer clone of the current buffer, to avoid any visibility
  1441. changes to the current buffer."
  1442. :group 'org-link-follow
  1443. :type 'boolean)
  1444. (defcustom org-open-non-existing-files nil
  1445. "Non-nil means `org-open-file' will open non-existing files.
  1446. When nil, an error will be generated.
  1447. This variable applies only to external applications because they
  1448. might choke on non-existing files. If the link is to a file that
  1449. will be opened in Emacs, the variable is ignored."
  1450. :group 'org-link-follow
  1451. :type 'boolean)
  1452. (defcustom org-open-directory-means-index-dot-org nil
  1453. "Non-nil means a link to a directory really means to index.org.
  1454. When nil, following a directory link will run dired or open a finder/explorer
  1455. window on that directory."
  1456. :group 'org-link-follow
  1457. :type 'boolean)
  1458. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1459. "Function and arguments to call for following mailto links.
  1460. This is a list with the first element being a Lisp function, and the
  1461. remaining elements being arguments to the function. In string arguments,
  1462. %a will be replaced by the address, and %s will be replaced by the subject
  1463. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1464. :group 'org-link-follow
  1465. :type '(choice
  1466. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1467. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1468. (const :tag "message-mail" (message-mail "%a" "%s"))
  1469. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1470. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1471. "Non-nil means ask for confirmation before executing shell links.
  1472. Shell links can be dangerous: just think about a link
  1473. [[shell:rm -rf ~/*][Google Search]]
  1474. This link would show up in your Org-mode document as \"Google Search\",
  1475. but really it would remove your entire home directory.
  1476. Therefore we advise against setting this variable to nil.
  1477. Just change it to `y-or-n-p' if you want to confirm with a
  1478. single keystroke rather than having to type \"yes\"."
  1479. :group 'org-link-follow
  1480. :type '(choice
  1481. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1482. (const :tag "with y-or-n (faster)" y-or-n-p)
  1483. (const :tag "no confirmation (dangerous)" nil)))
  1484. (put 'org-confirm-shell-link-function
  1485. 'safe-local-variable
  1486. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1487. (defcustom org-confirm-shell-link-not-regexp ""
  1488. "A regexp to skip confirmation for shell links."
  1489. :group 'org-link-follow
  1490. :version "24.1"
  1491. :type 'regexp)
  1492. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1493. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1494. Elisp links can be dangerous: just think about a link
  1495. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1496. This link would show up in your Org-mode document as \"Google Search\",
  1497. but really it would remove your entire home directory.
  1498. Therefore we advise against setting this variable to nil.
  1499. Just change it to `y-or-n-p' if you want to confirm with a
  1500. single keystroke rather than having to type \"yes\"."
  1501. :group 'org-link-follow
  1502. :type '(choice
  1503. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1504. (const :tag "with y-or-n (faster)" y-or-n-p)
  1505. (const :tag "no confirmation (dangerous)" nil)))
  1506. (put 'org-confirm-shell-link-function
  1507. 'safe-local-variable
  1508. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1509. (defcustom org-confirm-elisp-link-not-regexp ""
  1510. "A regexp to skip confirmation for Elisp links."
  1511. :group 'org-link-follow
  1512. :version "24.1"
  1513. :type 'regexp)
  1514. (defconst org-file-apps-defaults-gnu
  1515. '((remote . emacs)
  1516. (system . mailcap)
  1517. (t . mailcap))
  1518. "Default file applications on a UNIX or GNU/Linux system.
  1519. See `org-file-apps'.")
  1520. (defconst org-file-apps-defaults-macosx
  1521. '((remote . emacs)
  1522. (t . "open %s")
  1523. (system . "open %s")
  1524. ("ps.gz" . "gv %s")
  1525. ("eps.gz" . "gv %s")
  1526. ("dvi" . "xdvi %s")
  1527. ("fig" . "xfig %s"))
  1528. "Default file applications on a MacOS X system.
  1529. The system \"open\" is known as a default, but we use X11 applications
  1530. for some files for which the OS does not have a good default.
  1531. See `org-file-apps'.")
  1532. (defconst org-file-apps-defaults-windowsnt
  1533. (list
  1534. '(remote . emacs)
  1535. (cons t
  1536. (list (if (featurep 'xemacs)
  1537. 'mswindows-shell-execute
  1538. 'w32-shell-execute)
  1539. "open" 'file))
  1540. (cons 'system
  1541. (list (if (featurep 'xemacs)
  1542. 'mswindows-shell-execute
  1543. 'w32-shell-execute)
  1544. "open" 'file)))
  1545. "Default file applications on a Windows NT system.
  1546. The system \"open\" is used for most files.
  1547. See `org-file-apps'.")
  1548. (defcustom org-file-apps
  1549. '(
  1550. (auto-mode . emacs)
  1551. ("\\.mm\\'" . default)
  1552. ("\\.x?html?\\'" . default)
  1553. ("\\.pdf\\'" . default)
  1554. )
  1555. "External applications for opening `file:path' items in a document.
  1556. Org-mode uses system defaults for different file types, but
  1557. you can use this variable to set the application for a given file
  1558. extension. The entries in this list are cons cells where the car identifies
  1559. files and the cdr the corresponding command. Possible values for the
  1560. file identifier are
  1561. \"string\" A string as a file identifier can be interpreted in different
  1562. ways, depending on its contents:
  1563. - Alphanumeric characters only:
  1564. Match links with this file extension.
  1565. Example: (\"pdf\" . \"evince %s\")
  1566. to open PDFs with evince.
  1567. - Regular expression: Match links where the
  1568. filename matches the regexp. If you want to
  1569. use groups here, use shy groups.
  1570. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1571. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1572. to open *.html and *.xhtml with firefox.
  1573. - Regular expression which contains (non-shy) groups:
  1574. Match links where the whole link, including \"::\", and
  1575. anything after that, matches the regexp.
  1576. In a custom command string, %1, %2, etc. are replaced with
  1577. the parts of the link that were matched by the groups.
  1578. For backwards compatibility, if a command string is given
  1579. that does not use any of the group matches, this case is
  1580. handled identically to the second one (i.e. match against
  1581. file name only).
  1582. In a custom lisp form, you can access the group matches with
  1583. (match-string n link).
  1584. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1585. to open [[file:document.pdf::5]] with evince at page 5.
  1586. `directory' Matches a directory
  1587. `remote' Matches a remote file, accessible through tramp or efs.
  1588. Remote files most likely should be visited through Emacs
  1589. because external applications cannot handle such paths.
  1590. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1591. so all files Emacs knows how to handle. Using this with
  1592. command `emacs' will open most files in Emacs. Beware that this
  1593. will also open html files inside Emacs, unless you add
  1594. (\"html\" . default) to the list as well.
  1595. t Default for files not matched by any of the other options.
  1596. `system' The system command to open files, like `open' on Windows
  1597. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1598. that will be selected if you call `C-c C-o' with a double
  1599. \\[universal-argument] \\[universal-argument] prefix.
  1600. Possible values for the command are:
  1601. `emacs' The file will be visited by the current Emacs process.
  1602. `default' Use the default application for this file type, which is the
  1603. association for t in the list, most likely in the system-specific
  1604. part.
  1605. This can be used to overrule an unwanted setting in the
  1606. system-specific variable.
  1607. `system' Use the system command for opening files, like \"open\".
  1608. This command is specified by the entry whose car is `system'.
  1609. Most likely, the system-specific version of this variable
  1610. does define this command, but you can overrule/replace it
  1611. here.
  1612. string A command to be executed by a shell; %s will be replaced
  1613. by the path to the file.
  1614. sexp A Lisp form which will be evaluated. The file path will
  1615. be available in the Lisp variable `file'.
  1616. For more examples, see the system specific constants
  1617. `org-file-apps-defaults-macosx'
  1618. `org-file-apps-defaults-windowsnt'
  1619. `org-file-apps-defaults-gnu'."
  1620. :group 'org-link-follow
  1621. :type '(repeat
  1622. (cons (choice :value ""
  1623. (string :tag "Extension")
  1624. (const :tag "System command to open files" system)
  1625. (const :tag "Default for unrecognized files" t)
  1626. (const :tag "Remote file" remote)
  1627. (const :tag "Links to a directory" directory)
  1628. (const :tag "Any files that have Emacs modes"
  1629. auto-mode))
  1630. (choice :value ""
  1631. (const :tag "Visit with Emacs" emacs)
  1632. (const :tag "Use default" default)
  1633. (const :tag "Use the system command" system)
  1634. (string :tag "Command")
  1635. (sexp :tag "Lisp form")))))
  1636. (defcustom org-doi-server-url "http://dx.doi.org/"
  1637. "The URL of the DOI server."
  1638. :type 'string
  1639. :version "24.3"
  1640. :group 'org-link-follow)
  1641. (defgroup org-refile nil
  1642. "Options concerning refiling entries in Org-mode."
  1643. :tag "Org Refile"
  1644. :group 'org)
  1645. (defcustom org-directory "~/org"
  1646. "Directory with org files.
  1647. This is just a default location to look for Org files. There is no need
  1648. at all to put your files into this directory. It is only used in the
  1649. following situations:
  1650. 1. When a capture template specifies a target file that is not an
  1651. absolute path. The path will then be interpreted relative to
  1652. `org-directory'
  1653. 2. When a capture note is filed away in an interactive way (when exiting the
  1654. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1655. with `org-directory' as the default path."
  1656. :group 'org-refile
  1657. :group 'org-remember
  1658. :group 'org-capture
  1659. :type 'directory)
  1660. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1661. "Default target for storing notes.
  1662. Used as a fall back file for org-remember.el and org-capture.el, for
  1663. templates that do not specify a target file."
  1664. :group 'org-refile
  1665. :group 'org-remember
  1666. :group 'org-capture
  1667. :type '(choice
  1668. (const :tag "Default from remember-data-file" nil)
  1669. file))
  1670. (defcustom org-goto-interface 'outline
  1671. "The default interface to be used for `org-goto'.
  1672. Allowed values are:
  1673. outline The interface shows an outline of the relevant file
  1674. and the correct heading is found by moving through
  1675. the outline or by searching with incremental search.
  1676. outline-path-completion Headlines in the current buffer are offered via
  1677. completion. This is the interface also used by
  1678. the refile command."
  1679. :group 'org-refile
  1680. :type '(choice
  1681. (const :tag "Outline" outline)
  1682. (const :tag "Outline-path-completion" outline-path-completion)))
  1683. (defcustom org-goto-max-level 5
  1684. "Maximum target level when running `org-goto' with refile interface."
  1685. :group 'org-refile
  1686. :type 'integer)
  1687. (defcustom org-reverse-note-order nil
  1688. "Non-nil means store new notes at the beginning of a file or entry.
  1689. When nil, new notes will be filed to the end of a file or entry.
  1690. This can also be a list with cons cells of regular expressions that
  1691. are matched against file names, and values."
  1692. :group 'org-remember
  1693. :group 'org-capture
  1694. :group 'org-refile
  1695. :type '(choice
  1696. (const :tag "Reverse always" t)
  1697. (const :tag "Reverse never" nil)
  1698. (repeat :tag "By file name regexp"
  1699. (cons regexp boolean))))
  1700. (defcustom org-log-refile nil
  1701. "Information to record when a task is refiled.
  1702. Possible values are:
  1703. nil Don't add anything
  1704. time Add a time stamp to the task
  1705. note Prompt for a note and add it with template `org-log-note-headings'
  1706. This option can also be set with on a per-file-basis with
  1707. #+STARTUP: nologrefile
  1708. #+STARTUP: logrefile
  1709. #+STARTUP: lognoterefile
  1710. You can have local logging settings for a subtree by setting the LOGGING
  1711. property to one or more of these keywords.
  1712. When bulk-refiling from the agenda, the value `note' is forbidden and
  1713. will temporarily be changed to `time'."
  1714. :group 'org-refile
  1715. :group 'org-progress
  1716. :version "24.1"
  1717. :type '(choice
  1718. (const :tag "No logging" nil)
  1719. (const :tag "Record timestamp" time)
  1720. (const :tag "Record timestamp with note." note)))
  1721. (defcustom org-refile-targets nil
  1722. "Targets for refiling entries with \\[org-refile].
  1723. This is a list of cons cells. Each cell contains:
  1724. - a specification of the files to be considered, either a list of files,
  1725. or a symbol whose function or variable value will be used to retrieve
  1726. a file name or a list of file names. If you use `org-agenda-files' for
  1727. that, all agenda files will be scanned for targets. Nil means consider
  1728. headings in the current buffer.
  1729. - A specification of how to find candidate refile targets. This may be
  1730. any of:
  1731. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1732. This tag has to be present in all target headlines, inheritance will
  1733. not be considered.
  1734. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1735. todo keyword.
  1736. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1737. headlines that are refiling targets.
  1738. - a cons cell (:level . N). Any headline of level N is considered a target.
  1739. Note that, when `org-odd-levels-only' is set, level corresponds to
  1740. order in hierarchy, not to the number of stars.
  1741. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1742. Note that, when `org-odd-levels-only' is set, level corresponds to
  1743. order in hierarchy, not to the number of stars.
  1744. Each element of this list generates a set of possible targets.
  1745. The union of these sets is presented (with completion) to
  1746. the user by `org-refile'.
  1747. You can set the variable `org-refile-target-verify-function' to a function
  1748. to verify each headline found by the simple criteria above.
  1749. When this variable is nil, all top-level headlines in the current buffer
  1750. are used, equivalent to the value `((nil . (:level . 1))'."
  1751. :group 'org-refile
  1752. :type '(repeat
  1753. (cons
  1754. (choice :value org-agenda-files
  1755. (const :tag "All agenda files" org-agenda-files)
  1756. (const :tag "Current buffer" nil)
  1757. (function) (variable) (file))
  1758. (choice :tag "Identify target headline by"
  1759. (cons :tag "Specific tag" (const :value :tag) (string))
  1760. (cons :tag "TODO keyword" (const :value :todo) (string))
  1761. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1762. (cons :tag "Level number" (const :value :level) (integer))
  1763. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1764. (defcustom org-refile-target-verify-function nil
  1765. "Function to verify if the headline at point should be a refile target.
  1766. The function will be called without arguments, with point at the
  1767. beginning of the headline. It should return t and leave point
  1768. where it is if the headline is a valid target for refiling.
  1769. If the target should not be selected, the function must return nil.
  1770. In addition to this, it may move point to a place from where the search
  1771. should be continued. For example, the function may decide that the entire
  1772. subtree of the current entry should be excluded and move point to the end
  1773. of the subtree."
  1774. :group 'org-refile
  1775. :type 'function)
  1776. (defcustom org-refile-use-cache nil
  1777. "Non-nil means cache refile targets to speed up the process.
  1778. The cache for a particular file will be updated automatically when
  1779. the buffer has been killed, or when any of the marker used for flagging
  1780. refile targets no longer points at a live buffer.
  1781. If you have added new entries to a buffer that might themselves be targets,
  1782. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1783. find that easier, `C-u C-u C-u C-c C-w'."
  1784. :group 'org-refile
  1785. :version "24.1"
  1786. :type 'boolean)
  1787. (defcustom org-refile-use-outline-path nil
  1788. "Non-nil means provide refile targets as paths.
  1789. So a level 3 headline will be available as level1/level2/level3.
  1790. When the value is `file', also include the file name (without directory)
  1791. into the path. In this case, you can also stop the completion after
  1792. the file name, to get entries inserted as top level in the file.
  1793. When `full-file-path', include the full file path."
  1794. :group 'org-refile
  1795. :type '(choice
  1796. (const :tag "Not" nil)
  1797. (const :tag "Yes" t)
  1798. (const :tag "Start with file name" file)
  1799. (const :tag "Start with full file path" full-file-path)))
  1800. (defcustom org-outline-path-complete-in-steps t
  1801. "Non-nil means complete the outline path in hierarchical steps.
  1802. When Org-mode uses the refile interface to select an outline path
  1803. \(see variable `org-refile-use-outline-path'), the completion of
  1804. the path can be done is a single go, or if can be done in steps down
  1805. the headline hierarchy. Going in steps is probably the best if you
  1806. do not use a special completion package like `ido' or `icicles'.
  1807. However, when using these packages, going in one step can be very
  1808. fast, while still showing the whole path to the entry."
  1809. :group 'org-refile
  1810. :type 'boolean)
  1811. (defcustom org-refile-allow-creating-parent-nodes nil
  1812. "Non-nil means allow to create new nodes as refile targets.
  1813. New nodes are then created by adding \"/new node name\" to the completion
  1814. of an existing node. When the value of this variable is `confirm',
  1815. new node creation must be confirmed by the user (recommended)
  1816. When nil, the completion must match an existing entry.
  1817. Note that, if the new heading is not seen by the criteria
  1818. listed in `org-refile-targets', multiple instances of the same
  1819. heading would be created by trying again to file under the new
  1820. heading."
  1821. :group 'org-refile
  1822. :type '(choice
  1823. (const :tag "Never" nil)
  1824. (const :tag "Always" t)
  1825. (const :tag "Prompt for confirmation" confirm)))
  1826. (defcustom org-refile-active-region-within-subtree nil
  1827. "Non-nil means also refile active region within a subtree.
  1828. By default `org-refile' doesn't allow refiling regions if they
  1829. don't contain a set of subtrees, but it might be convenient to
  1830. do so sometimes: in that case, the first line of the region is
  1831. converted to a headline before refiling."
  1832. :group 'org-refile
  1833. :version "24.1"
  1834. :type 'boolean)
  1835. (defgroup org-todo nil
  1836. "Options concerning TODO items in Org-mode."
  1837. :tag "Org TODO"
  1838. :group 'org)
  1839. (defgroup org-progress nil
  1840. "Options concerning Progress logging in Org-mode."
  1841. :tag "Org Progress"
  1842. :group 'org-time)
  1843. (defvar org-todo-interpretation-widgets
  1844. '((:tag "Sequence (cycling hits every state)" sequence)
  1845. (:tag "Type (cycling directly to DONE)" type))
  1846. "The available interpretation symbols for customizing `org-todo-keywords'.
  1847. Interested libraries should add to this list.")
  1848. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1849. "List of TODO entry keyword sequences and their interpretation.
  1850. \\<org-mode-map>This is a list of sequences.
  1851. Each sequence starts with a symbol, either `sequence' or `type',
  1852. indicating if the keywords should be interpreted as a sequence of
  1853. action steps, or as different types of TODO items. The first
  1854. keywords are states requiring action - these states will select a headline
  1855. for inclusion into the global TODO list Org-mode produces. If one of
  1856. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1857. signify that no further action is necessary. If \"|\" is not found,
  1858. the last keyword is treated as the only DONE state of the sequence.
  1859. The command \\[org-todo] cycles an entry through these states, and one
  1860. additional state where no keyword is present. For details about this
  1861. cycling, see the manual.
  1862. TODO keywords and interpretation can also be set on a per-file basis with
  1863. the special #+SEQ_TODO and #+TYP_TODO lines.
  1864. Each keyword can optionally specify a character for fast state selection
  1865. \(in combination with the variable `org-use-fast-todo-selection')
  1866. and specifiers for state change logging, using the same syntax that
  1867. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1868. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1869. indicates to record a time stamp each time this state is selected.
  1870. Each keyword may also specify if a timestamp or a note should be
  1871. recorded when entering or leaving the state, by adding additional
  1872. characters in the parenthesis after the keyword. This looks like this:
  1873. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1874. record only the time of the state change. With X and Y being either
  1875. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1876. Y when leaving the state if and only if the *target* state does not
  1877. define X. You may omit any of the fast-selection key or X or /Y,
  1878. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1879. For backward compatibility, this variable may also be just a list
  1880. of keywords. In this case the interpretation (sequence or type) will be
  1881. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1882. :group 'org-todo
  1883. :group 'org-keywords
  1884. :type '(choice
  1885. (repeat :tag "Old syntax, just keywords"
  1886. (string :tag "Keyword"))
  1887. (repeat :tag "New syntax"
  1888. (cons
  1889. (choice
  1890. :tag "Interpretation"
  1891. ;;Quick and dirty way to see
  1892. ;;`org-todo-interpretations'. This takes the
  1893. ;;place of item arguments
  1894. :convert-widget
  1895. (lambda (widget)
  1896. (widget-put widget
  1897. :args (mapcar
  1898. #'(lambda (x)
  1899. (widget-convert
  1900. (cons 'const x)))
  1901. org-todo-interpretation-widgets))
  1902. widget))
  1903. (repeat
  1904. (string :tag "Keyword"))))))
  1905. (defvar org-todo-keywords-1 nil
  1906. "All TODO and DONE keywords active in a buffer.")
  1907. (make-variable-buffer-local 'org-todo-keywords-1)
  1908. (defvar org-todo-keywords-for-agenda nil)
  1909. (defvar org-done-keywords-for-agenda nil)
  1910. (defvar org-drawers-for-agenda nil)
  1911. (defvar org-todo-keyword-alist-for-agenda nil)
  1912. (defvar org-tag-alist-for-agenda nil)
  1913. (defvar org-agenda-contributing-files nil)
  1914. (defvar org-not-done-keywords nil)
  1915. (make-variable-buffer-local 'org-not-done-keywords)
  1916. (defvar org-done-keywords nil)
  1917. (make-variable-buffer-local 'org-done-keywords)
  1918. (defvar org-todo-heads nil)
  1919. (make-variable-buffer-local 'org-todo-heads)
  1920. (defvar org-todo-sets nil)
  1921. (make-variable-buffer-local 'org-todo-sets)
  1922. (defvar org-todo-log-states nil)
  1923. (make-variable-buffer-local 'org-todo-log-states)
  1924. (defvar org-todo-kwd-alist nil)
  1925. (make-variable-buffer-local 'org-todo-kwd-alist)
  1926. (defvar org-todo-key-alist nil)
  1927. (make-variable-buffer-local 'org-todo-key-alist)
  1928. (defvar org-todo-key-trigger nil)
  1929. (make-variable-buffer-local 'org-todo-key-trigger)
  1930. (defcustom org-todo-interpretation 'sequence
  1931. "Controls how TODO keywords are interpreted.
  1932. This variable is in principle obsolete and is only used for
  1933. backward compatibility, if the interpretation of todo keywords is
  1934. not given already in `org-todo-keywords'. See that variable for
  1935. more information."
  1936. :group 'org-todo
  1937. :group 'org-keywords
  1938. :type '(choice (const sequence)
  1939. (const type)))
  1940. (defcustom org-use-fast-todo-selection t
  1941. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1942. This variable describes if and under what circumstances the cycling
  1943. mechanism for TODO keywords will be replaced by a single-key, direct
  1944. selection scheme.
  1945. When nil, fast selection is never used.
  1946. When the symbol `prefix', it will be used when `org-todo' is called
  1947. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1948. `C-u t' in an agenda buffer.
  1949. When t, fast selection is used by default. In this case, the prefix
  1950. argument forces cycling instead.
  1951. In all cases, the special interface is only used if access keys have
  1952. actually been assigned by the user, i.e. if keywords in the configuration
  1953. are followed by a letter in parenthesis, like TODO(t)."
  1954. :group 'org-todo
  1955. :type '(choice
  1956. (const :tag "Never" nil)
  1957. (const :tag "By default" t)
  1958. (const :tag "Only with C-u C-c C-t" prefix)))
  1959. (defcustom org-provide-todo-statistics t
  1960. "Non-nil means update todo statistics after insert and toggle.
  1961. ALL-HEADLINES means update todo statistics by including headlines
  1962. with no TODO keyword as well, counting them as not done.
  1963. A list of TODO keywords means the same, but skip keywords that are
  1964. not in this list.
  1965. When this is set, todo statistics is updated in the parent of the
  1966. current entry each time a todo state is changed."
  1967. :group 'org-todo
  1968. :type '(choice
  1969. (const :tag "Yes, only for TODO entries" t)
  1970. (const :tag "Yes, including all entries" 'all-headlines)
  1971. (repeat :tag "Yes, for TODOs in this list"
  1972. (string :tag "TODO keyword"))
  1973. (other :tag "No TODO statistics" nil)))
  1974. (defcustom org-hierarchical-todo-statistics t
  1975. "Non-nil means TODO statistics covers just direct children.
  1976. When nil, all entries in the subtree are considered.
  1977. This has only an effect if `org-provide-todo-statistics' is set.
  1978. To set this to nil for only a single subtree, use a COOKIE_DATA
  1979. property and include the word \"recursive\" into the value."
  1980. :group 'org-todo
  1981. :type 'boolean)
  1982. (defcustom org-after-todo-state-change-hook nil
  1983. "Hook which is run after the state of a TODO item was changed.
  1984. The new state (a string with a TODO keyword, or nil) is available in the
  1985. Lisp variable `org-state'."
  1986. :group 'org-todo
  1987. :type 'hook)
  1988. (defvar org-blocker-hook nil
  1989. "Hook for functions that are allowed to block a state change.
  1990. Functions in this hook should not modify the buffer.
  1991. Each function gets as its single argument a property list,
  1992. see `org-trigger-hook' for more information about this list.
  1993. If any of the functions in this hook returns nil, the state change
  1994. is blocked.")
  1995. (defvar org-trigger-hook nil
  1996. "Hook for functions that are triggered by a state change.
  1997. Each function gets as its single argument a property list with at
  1998. least the following elements:
  1999. (:type type-of-change :position pos-at-entry-start
  2000. :from old-state :to new-state)
  2001. Depending on the type, more properties may be present.
  2002. This mechanism is currently implemented for:
  2003. TODO state changes
  2004. ------------------
  2005. :type todo-state-change
  2006. :from previous state (keyword as a string), or nil, or a symbol
  2007. 'todo' or 'done', to indicate the general type of state.
  2008. :to new state, like in :from")
  2009. (defcustom org-enforce-todo-dependencies nil
  2010. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2011. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2012. be blocked if any prior sibling is not yet done.
  2013. Finally, if the parent is blocked because of ordered siblings of its own,
  2014. the child will also be blocked."
  2015. :set (lambda (var val)
  2016. (set var val)
  2017. (if val
  2018. (add-hook 'org-blocker-hook
  2019. 'org-block-todo-from-children-or-siblings-or-parent)
  2020. (remove-hook 'org-blocker-hook
  2021. 'org-block-todo-from-children-or-siblings-or-parent)))
  2022. :group 'org-todo
  2023. :type 'boolean)
  2024. (defcustom org-enforce-todo-checkbox-dependencies nil
  2025. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2026. When this is nil, checkboxes have no influence on switching TODO states.
  2027. When non-nil, you first need to check off all check boxes before the TODO
  2028. entry can be switched to DONE.
  2029. This variable needs to be set before org.el is loaded, and you need to
  2030. restart Emacs after a change to make the change effective. The only way
  2031. to change is while Emacs is running is through the customize interface."
  2032. :set (lambda (var val)
  2033. (set var val)
  2034. (if val
  2035. (add-hook 'org-blocker-hook
  2036. 'org-block-todo-from-checkboxes)
  2037. (remove-hook 'org-blocker-hook
  2038. 'org-block-todo-from-checkboxes)))
  2039. :group 'org-todo
  2040. :type 'boolean)
  2041. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2042. "Non-nil means inserting a TODO heading is treated as state change.
  2043. So when the command \\[org-insert-todo-heading] is used, state change
  2044. logging will apply if appropriate. When nil, the new TODO item will
  2045. be inserted directly, and no logging will take place."
  2046. :group 'org-todo
  2047. :type 'boolean)
  2048. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2049. "Non-nil means switching TODO states with S-cursor counts as state change.
  2050. This is the default behavior. However, setting this to nil allows a
  2051. convenient way to select a TODO state and bypass any logging associated
  2052. with that."
  2053. :group 'org-todo
  2054. :type 'boolean)
  2055. (defcustom org-todo-state-tags-triggers nil
  2056. "Tag changes that should be triggered by TODO state changes.
  2057. This is a list. Each entry is
  2058. (state-change (tag . flag) .......)
  2059. State-change can be a string with a state, and empty string to indicate the
  2060. state that has no TODO keyword, or it can be one of the symbols `todo'
  2061. or `done', meaning any not-done or done state, respectively."
  2062. :group 'org-todo
  2063. :group 'org-tags
  2064. :type '(repeat
  2065. (cons (choice :tag "When changing to"
  2066. (const :tag "Not-done state" todo)
  2067. (const :tag "Done state" done)
  2068. (string :tag "State"))
  2069. (repeat
  2070. (cons :tag "Tag action"
  2071. (string :tag "Tag")
  2072. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2073. (defcustom org-log-done nil
  2074. "Information to record when a task moves to the DONE state.
  2075. Possible values are:
  2076. nil Don't add anything, just change the keyword
  2077. time Add a time stamp to the task
  2078. note Prompt for a note and add it with template `org-log-note-headings'
  2079. This option can also be set with on a per-file-basis with
  2080. #+STARTUP: nologdone
  2081. #+STARTUP: logdone
  2082. #+STARTUP: lognotedone
  2083. You can have local logging settings for a subtree by setting the LOGGING
  2084. property to one or more of these keywords."
  2085. :group 'org-todo
  2086. :group 'org-progress
  2087. :type '(choice
  2088. (const :tag "No logging" nil)
  2089. (const :tag "Record CLOSED timestamp" time)
  2090. (const :tag "Record CLOSED timestamp with note." note)))
  2091. ;; Normalize old uses of org-log-done.
  2092. (cond
  2093. ((eq org-log-done t) (setq org-log-done 'time))
  2094. ((and (listp org-log-done) (memq 'done org-log-done))
  2095. (setq org-log-done 'note)))
  2096. (defcustom org-log-reschedule nil
  2097. "Information to record when the scheduling date of a tasks is modified.
  2098. Possible values are:
  2099. nil Don't add anything, just change the date
  2100. time Add a time stamp to the task
  2101. note Prompt for a note and add it with template `org-log-note-headings'
  2102. This option can also be set with on a per-file-basis with
  2103. #+STARTUP: nologreschedule
  2104. #+STARTUP: logreschedule
  2105. #+STARTUP: lognotereschedule"
  2106. :group 'org-todo
  2107. :group 'org-progress
  2108. :type '(choice
  2109. (const :tag "No logging" nil)
  2110. (const :tag "Record timestamp" time)
  2111. (const :tag "Record timestamp with note." note)))
  2112. (defcustom org-log-redeadline nil
  2113. "Information to record when the deadline date of a tasks is modified.
  2114. Possible values are:
  2115. nil Don't add anything, just change the date
  2116. time Add a time stamp to the task
  2117. note Prompt for a note and add it with template `org-log-note-headings'
  2118. This option can also be set with on a per-file-basis with
  2119. #+STARTUP: nologredeadline
  2120. #+STARTUP: logredeadline
  2121. #+STARTUP: lognoteredeadline
  2122. You can have local logging settings for a subtree by setting the LOGGING
  2123. property to one or more of these keywords."
  2124. :group 'org-todo
  2125. :group 'org-progress
  2126. :type '(choice
  2127. (const :tag "No logging" nil)
  2128. (const :tag "Record timestamp" time)
  2129. (const :tag "Record timestamp with note." note)))
  2130. (defcustom org-log-note-clock-out nil
  2131. "Non-nil means record a note when clocking out of an item.
  2132. This can also be configured on a per-file basis by adding one of
  2133. the following lines anywhere in the buffer:
  2134. #+STARTUP: lognoteclock-out
  2135. #+STARTUP: nolognoteclock-out"
  2136. :group 'org-todo
  2137. :group 'org-progress
  2138. :type 'boolean)
  2139. (defcustom org-log-done-with-time t
  2140. "Non-nil means the CLOSED time stamp will contain date and time.
  2141. When nil, only the date will be recorded."
  2142. :group 'org-progress
  2143. :type 'boolean)
  2144. (defcustom org-log-note-headings
  2145. '((done . "CLOSING NOTE %t")
  2146. (state . "State %-12s from %-12S %t")
  2147. (note . "Note taken on %t")
  2148. (reschedule . "Rescheduled from %S on %t")
  2149. (delschedule . "Not scheduled, was %S on %t")
  2150. (redeadline . "New deadline from %S on %t")
  2151. (deldeadline . "Removed deadline, was %S on %t")
  2152. (refile . "Refiled on %t")
  2153. (clock-out . ""))
  2154. "Headings for notes added to entries.
  2155. The value is an alist, with the car being a symbol indicating the note
  2156. context, and the cdr is the heading to be used. The heading may also be the
  2157. empty string.
  2158. %t in the heading will be replaced by a time stamp.
  2159. %T will be an active time stamp instead the default inactive one
  2160. %d will be replaced by a short-format time stamp.
  2161. %D will be replaced by an active short-format time stamp.
  2162. %s will be replaced by the new TODO state, in double quotes.
  2163. %S will be replaced by the old TODO state, in double quotes.
  2164. %u will be replaced by the user name.
  2165. %U will be replaced by the full user name.
  2166. In fact, it is not a good idea to change the `state' entry, because
  2167. agenda log mode depends on the format of these entries."
  2168. :group 'org-todo
  2169. :group 'org-progress
  2170. :type '(list :greedy t
  2171. (cons (const :tag "Heading when closing an item" done) string)
  2172. (cons (const :tag
  2173. "Heading when changing todo state (todo sequence only)"
  2174. state) string)
  2175. (cons (const :tag "Heading when just taking a note" note) string)
  2176. (cons (const :tag "Heading when clocking out" clock-out) string)
  2177. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2178. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2179. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2180. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2181. (cons (const :tag "Heading when refiling" refile) string)))
  2182. (unless (assq 'note org-log-note-headings)
  2183. (push '(note . "%t") org-log-note-headings))
  2184. (defcustom org-log-into-drawer nil
  2185. "Non-nil means insert state change notes and time stamps into a drawer.
  2186. When nil, state changes notes will be inserted after the headline and
  2187. any scheduling and clock lines, but not inside a drawer.
  2188. The value of this variable should be the name of the drawer to use.
  2189. LOGBOOK is proposed as the default drawer for this purpose, you can
  2190. also set this to a string to define the drawer of your choice.
  2191. A value of t is also allowed, representing \"LOGBOOK\".
  2192. A value of t or nil can also be set with on a per-file-basis with
  2193. #+STARTUP: logdrawer
  2194. #+STARTUP: nologdrawer
  2195. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2196. will be ignored.
  2197. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2198. a subtree."
  2199. :group 'org-todo
  2200. :group 'org-progress
  2201. :type '(choice
  2202. (const :tag "Not into a drawer" nil)
  2203. (const :tag "LOGBOOK" t)
  2204. (string :tag "Other")))
  2205. (if (fboundp 'defvaralias)
  2206. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2207. (defun org-log-into-drawer ()
  2208. "Return the value of `org-log-into-drawer', but let properties overrule.
  2209. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2210. used instead of the default value."
  2211. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2212. (cond
  2213. ((not p) org-log-into-drawer)
  2214. ((equal p "nil") nil)
  2215. ((equal p "t") "LOGBOOK")
  2216. (t p))))
  2217. (defcustom org-log-state-notes-insert-after-drawers nil
  2218. "Non-nil means insert state change notes after any drawers in entry.
  2219. Only the drawers that *immediately* follow the headline and the
  2220. deadline/scheduled line are skipped.
  2221. When nil, insert notes right after the heading and perhaps the line
  2222. with deadline/scheduling if present.
  2223. This variable will have no effect if `org-log-into-drawer' is
  2224. set."
  2225. :group 'org-todo
  2226. :group 'org-progress
  2227. :type 'boolean)
  2228. (defcustom org-log-states-order-reversed t
  2229. "Non-nil means the latest state note will be directly after heading.
  2230. When nil, the state change notes will be ordered according to time.
  2231. This option can also be set with on a per-file-basis with
  2232. #+STARTUP: logstatesreversed
  2233. #+STARTUP: nologstatesreversed"
  2234. :group 'org-todo
  2235. :group 'org-progress
  2236. :type 'boolean)
  2237. (defcustom org-todo-repeat-to-state nil
  2238. "The TODO state to which a repeater should return the repeating task.
  2239. By default this is the first task in a TODO sequence, or the previous state
  2240. in a TODO_TYP set. But you can specify another task here.
  2241. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2242. :group 'org-todo
  2243. :version "24.1"
  2244. :type '(choice (const :tag "Head of sequence" nil)
  2245. (string :tag "Specific state")))
  2246. (defcustom org-log-repeat 'time
  2247. "Non-nil means record moving through the DONE state when triggering repeat.
  2248. An auto-repeating task is immediately switched back to TODO when
  2249. marked DONE. If you are not logging state changes (by adding \"@\"
  2250. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2251. record a closing note, there will be no record of the task moving
  2252. through DONE. This variable forces taking a note anyway.
  2253. nil Don't force a record
  2254. time Record a time stamp
  2255. note Prompt for a note and add it with template `org-log-note-headings'
  2256. This option can also be set with on a per-file-basis with
  2257. #+STARTUP: nologrepeat
  2258. #+STARTUP: logrepeat
  2259. #+STARTUP: lognoterepeat
  2260. You can have local logging settings for a subtree by setting the LOGGING
  2261. property to one or more of these keywords."
  2262. :group 'org-todo
  2263. :group 'org-progress
  2264. :type '(choice
  2265. (const :tag "Don't force a record" nil)
  2266. (const :tag "Force recording the DONE state" time)
  2267. (const :tag "Force recording a note with the DONE state" note)))
  2268. (defgroup org-priorities nil
  2269. "Priorities in Org-mode."
  2270. :tag "Org Priorities"
  2271. :group 'org-todo)
  2272. (defcustom org-enable-priority-commands t
  2273. "Non-nil means priority commands are active.
  2274. When nil, these commands will be disabled, so that you never accidentally
  2275. set a priority."
  2276. :group 'org-priorities
  2277. :type 'boolean)
  2278. (defcustom org-highest-priority ?A
  2279. "The highest priority of TODO items. A character like ?A, ?B etc.
  2280. Must have a smaller ASCII number than `org-lowest-priority'."
  2281. :group 'org-priorities
  2282. :type 'character)
  2283. (defcustom org-lowest-priority ?C
  2284. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2285. Must have a larger ASCII number than `org-highest-priority'."
  2286. :group 'org-priorities
  2287. :type 'character)
  2288. (defcustom org-default-priority ?B
  2289. "The default priority of TODO items.
  2290. This is the priority an item gets if no explicit priority is given.
  2291. When starting to cycle on an empty priority the first step in the cycle
  2292. depends on `org-priority-start-cycle-with-default'. The resulting first
  2293. step priority must not exceed the range from `org-highest-priority' to
  2294. `org-lowest-priority' which means that `org-default-priority' has to be
  2295. in this range exclusive or inclusive the range boundaries. Else the
  2296. first step refuses to set the default and the second will fall back
  2297. to (depending on the command used) the highest or lowest priority."
  2298. :group 'org-priorities
  2299. :type 'character)
  2300. (defcustom org-priority-start-cycle-with-default t
  2301. "Non-nil means start with default priority when starting to cycle.
  2302. When this is nil, the first step in the cycle will be (depending on the
  2303. command used) one higher or lower than the default priority.
  2304. See also `org-default-priority'."
  2305. :group 'org-priorities
  2306. :type 'boolean)
  2307. (defcustom org-get-priority-function nil
  2308. "Function to extract the priority from a string.
  2309. The string is normally the headline. If this is nil Org computes the
  2310. priority from the priority cookie like [#A] in the headline. It returns
  2311. an integer, increasing by 1000 for each priority level.
  2312. The user can set a different function here, which should take a string
  2313. as an argument and return the numeric priority."
  2314. :group 'org-priorities
  2315. :version "24.1"
  2316. :type 'function)
  2317. (defgroup org-time nil
  2318. "Options concerning time stamps and deadlines in Org-mode."
  2319. :tag "Org Time"
  2320. :group 'org)
  2321. (defcustom org-insert-labeled-timestamps-at-point nil
  2322. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2323. When nil, these labeled time stamps are forces into the second line of an
  2324. entry, just after the headline. When scheduling from the global TODO list,
  2325. the time stamp will always be forced into the second line."
  2326. :group 'org-time
  2327. :type 'boolean)
  2328. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2329. "Formats for `format-time-string' which are used for time stamps.
  2330. It is not recommended to change this constant.")
  2331. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2332. "Number of minutes to round time stamps to.
  2333. These are two values, the first applies when first creating a time stamp.
  2334. The second applies when changing it with the commands `S-up' and `S-down'.
  2335. When changing the time stamp, this means that it will change in steps
  2336. of N minutes, as given by the second value.
  2337. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2338. numbers should be factors of 60, so for example 5, 10, 15.
  2339. When this is larger than 1, you can still force an exact time stamp by using
  2340. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2341. and by using a prefix arg to `S-up/down' to specify the exact number
  2342. of minutes to shift."
  2343. :group 'org-time
  2344. :get #'(lambda (var) ; Make sure both elements are there
  2345. (if (integerp (default-value var))
  2346. (list (default-value var) 5)
  2347. (default-value var)))
  2348. :type '(list
  2349. (integer :tag "when inserting times")
  2350. (integer :tag "when modifying times")))
  2351. ;; Normalize old customizations of this variable.
  2352. (when (integerp org-time-stamp-rounding-minutes)
  2353. (setq org-time-stamp-rounding-minutes
  2354. (list org-time-stamp-rounding-minutes
  2355. org-time-stamp-rounding-minutes)))
  2356. (defcustom org-display-custom-times nil
  2357. "Non-nil means overlay custom formats over all time stamps.
  2358. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2359. To turn this on on a per-file basis, insert anywhere in the file:
  2360. #+STARTUP: customtime"
  2361. :group 'org-time
  2362. :set 'set-default
  2363. :type 'sexp)
  2364. (make-variable-buffer-local 'org-display-custom-times)
  2365. (defcustom org-time-stamp-custom-formats
  2366. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2367. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2368. These are overlaid over the default ISO format if the variable
  2369. `org-display-custom-times' is set. Time like %H:%M should be at the
  2370. end of the second format. The custom formats are also honored by export
  2371. commands, if custom time display is turned on at the time of export."
  2372. :group 'org-time
  2373. :type 'sexp)
  2374. (defun org-time-stamp-format (&optional long inactive)
  2375. "Get the right format for a time string."
  2376. (let ((f (if long (cdr org-time-stamp-formats)
  2377. (car org-time-stamp-formats))))
  2378. (if inactive
  2379. (concat "[" (substring f 1 -1) "]")
  2380. f)))
  2381. (defcustom org-time-clocksum-format
  2382. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2383. "The format string used when creating CLOCKSUM lines.
  2384. This is also used when Org mode generates a time duration.
  2385. The value can be a single format string containing two
  2386. %-sequences, which will be filled with the number of hours and
  2387. minutes in that order.
  2388. Alternatively, the value can be a plist associating any of the
  2389. keys :years, :months, :weeks, :days, :hours or :minutes with
  2390. format strings. The time duration is formatted using only the
  2391. time components that are needed and concatenating the results.
  2392. If a time unit in absent, it falls back to the next smallest
  2393. unit.
  2394. The keys :require-years, :require-months, :require-days,
  2395. :require-weeks, :require-hours, :require-minutes are also
  2396. meaningful. A non-nil value for these keys indicates that the
  2397. corresponding time component should always be included, even if
  2398. its value is 0.
  2399. For example,
  2400. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2401. :require-minutes t)
  2402. means durations longer than a day will be expressed in days,
  2403. hours and minutes, and durations less than a day will always be
  2404. expressed in hours and minutes (even for durations less than an
  2405. hour).
  2406. The value
  2407. \(:days \"%dd\" :minutes \"%dm\")
  2408. means durations longer than a day will be expressed in days and
  2409. minutes, and durations less than a day will be expressed entirely
  2410. in minutes (even for durations longer than an hour)."
  2411. :group 'org-time
  2412. :group 'org-clock
  2413. :version "24.3"
  2414. :type '(choice (string :tag "Format string")
  2415. (set :tag "Plist"
  2416. (group :inline t (const :tag "Years" :years)
  2417. (string :tag "Format string"))
  2418. (group :inline t
  2419. (const :tag "Always show years" :require-years)
  2420. (const t))
  2421. (group :inline t (const :tag "Months" :months)
  2422. (string :tag "Format string"))
  2423. (group :inline t
  2424. (const :tag "Always show months" :require-months)
  2425. (const t))
  2426. (group :inline t (const :tag "Weeks" :weeks)
  2427. (string :tag "Format string"))
  2428. (group :inline t
  2429. (const :tag "Always show weeks" :require-weeks)
  2430. (const t))
  2431. (group :inline t (const :tag "Days" :days)
  2432. (string :tag "Format string"))
  2433. (group :inline t
  2434. (const :tag "Always show days" :require-days)
  2435. (const t))
  2436. (group :inline t (const :tag "Hours" :hours)
  2437. (string :tag "Format string"))
  2438. (group :inline t
  2439. (const :tag "Always show hours" :require-hours)
  2440. (const t))
  2441. (group :inline t (const :tag "Minutes" :minutes)
  2442. (string :tag "Format string"))
  2443. (group :inline t
  2444. (const :tag "Always show minutes" :require-minutes)
  2445. (const t)))))
  2446. (defcustom org-time-clocksum-use-fractional nil
  2447. "When non-nil, \\[org-clock-display] uses fractional times.
  2448. See `org-time-clocksum-format' for more on time clock formats."
  2449. :group 'org-time
  2450. :group 'org-clock
  2451. :version "24.3"
  2452. :type 'boolean)
  2453. (defcustom org-time-clocksum-use-effort-durations t
  2454. "When non-nil, \\[org-clock-display] uses effort durations.
  2455. E.g. by default, one day is considered to be a 8 hours effort,
  2456. so a task that has been clocked for 16 hours will be displayed
  2457. as during 2 days in the clock display or in the clocktable.
  2458. See `org-effort-durations' on how to set effort durations
  2459. and `org-time-clocksum-format' for more on time clock formats."
  2460. :group 'org-time
  2461. :group 'org-clock
  2462. :version "24.3"
  2463. :type 'boolean)
  2464. (defcustom org-time-clocksum-fractional-format "%.2f"
  2465. "The format string used when creating CLOCKSUM lines,
  2466. or when Org mode generates a time duration, if
  2467. `org-time-clocksum-use-fractional' is enabled.
  2468. The value can be a single format string containing one
  2469. %-sequence, which will be filled with the number of hours as
  2470. a float.
  2471. Alternatively, the value can be a plist associating any of the
  2472. keys :years, :months, :weeks, :days, :hours or :minutes with
  2473. a format string. The time duration is formatted using the
  2474. largest time unit which gives a non-zero integer part. If all
  2475. specified formats have zero integer part, the smallest time unit
  2476. is used."
  2477. :group 'org-time
  2478. :type '(choice (string :tag "Format string")
  2479. (set (group :inline t (const :tag "Years" :years)
  2480. (string :tag "Format string"))
  2481. (group :inline t (const :tag "Months" :months)
  2482. (string :tag "Format string"))
  2483. (group :inline t (const :tag "Weeks" :weeks)
  2484. (string :tag "Format string"))
  2485. (group :inline t (const :tag "Days" :days)
  2486. (string :tag "Format string"))
  2487. (group :inline t (const :tag "Hours" :hours)
  2488. (string :tag "Format string"))
  2489. (group :inline t (const :tag "Minutes" :minutes)
  2490. (string :tag "Format string")))))
  2491. (defcustom org-deadline-warning-days 14
  2492. "No. of days before expiration during which a deadline becomes active.
  2493. This variable governs the display in sparse trees and in the agenda.
  2494. When 0 or negative, it means use this number (the absolute value of it)
  2495. even if a deadline has a different individual lead time specified.
  2496. Custom commands can set this variable in the options section."
  2497. :group 'org-time
  2498. :group 'org-agenda-daily/weekly
  2499. :type 'integer)
  2500. (defcustom org-read-date-prefer-future t
  2501. "Non-nil means assume future for incomplete date input from user.
  2502. This affects the following situations:
  2503. 1. The user gives a month but not a year.
  2504. For example, if it is April and you enter \"feb 2\", this will be read
  2505. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2506. 2. The user gives a day, but no month.
  2507. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2508. read this as the third of *next* month. However, if you enter \"17\",
  2509. it will be considered as *this* month.
  2510. If you set this variable to the symbol `time', then also the following
  2511. will work:
  2512. 3. If the user gives a time.
  2513. If the time is before now, it will be interpreted as tomorrow.
  2514. Currently none of this works for ISO week specifications.
  2515. When this option is nil, the current day, month and year will always be
  2516. used as defaults.
  2517. See also `org-agenda-jump-prefer-future'."
  2518. :group 'org-time
  2519. :type '(choice
  2520. (const :tag "Never" nil)
  2521. (const :tag "Check month and day" t)
  2522. (const :tag "Check month, day, and time" time)))
  2523. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2524. "Should the agenda jump command prefer the future for incomplete dates?
  2525. The default is to do the same as configured in `org-read-date-prefer-future'.
  2526. But you can also set a deviating value here.
  2527. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2528. :group 'org-agenda
  2529. :group 'org-time
  2530. :version "24.1"
  2531. :type '(choice
  2532. (const :tag "Use org-read-date-prefer-future"
  2533. org-read-date-prefer-future)
  2534. (const :tag "Never" nil)
  2535. (const :tag "Always" t)))
  2536. (defcustom org-read-date-force-compatible-dates t
  2537. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2538. Depending on the system Emacs is running on, certain dates cannot
  2539. be represented with the type used internally to represent time.
  2540. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2541. correctly. Some systems allow for earlier dates, some for later,
  2542. some for both. One way to find out it to insert any date into an
  2543. Org buffer, putting the cursor on the year and hitting S-up and
  2544. S-down to test the range.
  2545. When this variable is set to t, the date/time prompt will not let
  2546. you specify dates outside the 1970-2037 range, so it is certain that
  2547. these dates will work in whatever version of Emacs you are
  2548. running, and also that you can move a file from one Emacs implementation
  2549. to another. WHenever Org is forcing the year for you, it will display
  2550. a message and beep.
  2551. When this variable is nil, Org will check if the date is
  2552. representable in the specific Emacs implementation you are using.
  2553. If not, it will force a year, usually the current year, and beep
  2554. to remind you. Currently this setting is not recommended because
  2555. the likelihood that you will open your Org files in an Emacs that
  2556. has limited date range is not negligible.
  2557. A workaround for this problem is to use diary sexp dates for time
  2558. stamps outside of this range."
  2559. :group 'org-time
  2560. :version "24.1"
  2561. :type 'boolean)
  2562. (defcustom org-read-date-display-live t
  2563. "Non-nil means display current interpretation of date prompt live.
  2564. This display will be in an overlay, in the minibuffer."
  2565. :group 'org-time
  2566. :type 'boolean)
  2567. (defcustom org-read-date-popup-calendar t
  2568. "Non-nil means pop up a calendar when prompting for a date.
  2569. In the calendar, the date can be selected with mouse-1. However, the
  2570. minibuffer will also be active, and you can simply enter the date as well.
  2571. When nil, only the minibuffer will be available."
  2572. :group 'org-time
  2573. :type 'boolean)
  2574. (if (fboundp 'defvaralias)
  2575. (defvaralias 'org-popup-calendar-for-date-prompt
  2576. 'org-read-date-popup-calendar))
  2577. (defcustom org-read-date-minibuffer-setup-hook nil
  2578. "Hook to be used to set up keys for the date/time interface.
  2579. Add key definitions to `minibuffer-local-map', which will be a temporary
  2580. copy."
  2581. :group 'org-time
  2582. :type 'hook)
  2583. (defcustom org-extend-today-until 0
  2584. "The hour when your day really ends. Must be an integer.
  2585. This has influence for the following applications:
  2586. - When switching the agenda to \"today\". It it is still earlier than
  2587. the time given here, the day recognized as TODAY is actually yesterday.
  2588. - When a date is read from the user and it is still before the time given
  2589. here, the current date and time will be assumed to be yesterday, 23:59.
  2590. Also, timestamps inserted in capture templates follow this rule.
  2591. IMPORTANT: This is a feature whose implementation is and likely will
  2592. remain incomplete. Really, it is only here because past midnight seems to
  2593. be the favorite working time of John Wiegley :-)"
  2594. :group 'org-time
  2595. :type 'integer)
  2596. (defcustom org-use-effective-time nil
  2597. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2598. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2599. \"effective time\" of any timestamps between midnight and 8am will be
  2600. 23:59 of the previous day."
  2601. :group 'org-time
  2602. :version "24.1"
  2603. :type 'boolean)
  2604. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2605. "When non-nil, use the last clock out time for `org-todo'.
  2606. Note that this option has precedence over the combined use of
  2607. `org-use-effective-time' and `org-extend-today-until'."
  2608. :group 'org-time
  2609. ;; :version "24.3"
  2610. :type 'boolean)
  2611. (defcustom org-edit-timestamp-down-means-later nil
  2612. "Non-nil means S-down will increase the time in a time stamp.
  2613. When nil, S-up will increase."
  2614. :group 'org-time
  2615. :type 'boolean)
  2616. (defcustom org-calendar-follow-timestamp-change t
  2617. "Non-nil means make the calendar window follow timestamp changes.
  2618. When a timestamp is modified and the calendar window is visible, it will be
  2619. moved to the new date."
  2620. :group 'org-time
  2621. :type 'boolean)
  2622. (defgroup org-tags nil
  2623. "Options concerning tags in Org-mode."
  2624. :tag "Org Tags"
  2625. :group 'org)
  2626. (defcustom org-tag-alist nil
  2627. "List of tags allowed in Org-mode files.
  2628. When this list is nil, Org-mode will base TAG input on what is already in the
  2629. buffer.
  2630. The value of this variable is an alist, the car of each entry must be a
  2631. keyword as a string, the cdr may be a character that is used to select
  2632. that tag through the fast-tag-selection interface.
  2633. See the manual for details."
  2634. :group 'org-tags
  2635. :type '(repeat
  2636. (choice
  2637. (cons (string :tag "Tag name")
  2638. (character :tag "Access char"))
  2639. (list :tag "Start radio group"
  2640. (const :startgroup)
  2641. (option (string :tag "Group description")))
  2642. (list :tag "End radio group"
  2643. (const :endgroup)
  2644. (option (string :tag "Group description")))
  2645. (const :tag "New line" (:newline)))))
  2646. (defcustom org-tag-persistent-alist nil
  2647. "List of tags that will always appear in all Org-mode files.
  2648. This is in addition to any in buffer settings or customizations
  2649. of `org-tag-alist'.
  2650. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2651. The value of this variable is an alist, the car of each entry must be a
  2652. keyword as a string, the cdr may be a character that is used to select
  2653. that tag through the fast-tag-selection interface.
  2654. See the manual for details.
  2655. To disable these tags on a per-file basis, insert anywhere in the file:
  2656. #+STARTUP: noptag"
  2657. :group 'org-tags
  2658. :type '(repeat
  2659. (choice
  2660. (cons (string :tag "Tag name")
  2661. (character :tag "Access char"))
  2662. (const :tag "Start radio group" (:startgroup))
  2663. (const :tag "End radio group" (:endgroup))
  2664. (const :tag "New line" (:newline)))))
  2665. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2666. "If non-nil, always offer completion for all tags of all agenda files.
  2667. Instead of customizing this variable directly, you might want to
  2668. set it locally for capture buffers, because there no list of
  2669. tags in that file can be created dynamically (there are none).
  2670. (add-hook 'org-capture-mode-hook
  2671. (lambda ()
  2672. (set (make-local-variable
  2673. 'org-complete-tags-always-offer-all-agenda-tags)
  2674. t)))"
  2675. :group 'org-tags
  2676. :version "24.1"
  2677. :type 'boolean)
  2678. (defvar org-file-tags nil
  2679. "List of tags that can be inherited by all entries in the file.
  2680. The tags will be inherited if the variable `org-use-tag-inheritance'
  2681. says they should be.
  2682. This variable is populated from #+FILETAGS lines.")
  2683. (defcustom org-use-fast-tag-selection 'auto
  2684. "Non-nil means use fast tag selection scheme.
  2685. This is a special interface to select and deselect tags with single keys.
  2686. When nil, fast selection is never used.
  2687. When the symbol `auto', fast selection is used if and only if selection
  2688. characters for tags have been configured, either through the variable
  2689. `org-tag-alist' or through a #+TAGS line in the buffer.
  2690. When t, fast selection is always used and selection keys are assigned
  2691. automatically if necessary."
  2692. :group 'org-tags
  2693. :type '(choice
  2694. (const :tag "Always" t)
  2695. (const :tag "Never" nil)
  2696. (const :tag "When selection characters are configured" 'auto)))
  2697. (defcustom org-fast-tag-selection-single-key nil
  2698. "Non-nil means fast tag selection exits after first change.
  2699. When nil, you have to press RET to exit it.
  2700. During fast tag selection, you can toggle this flag with `C-c'.
  2701. This variable can also have the value `expert'. In this case, the window
  2702. displaying the tags menu is not even shown, until you press C-c again."
  2703. :group 'org-tags
  2704. :type '(choice
  2705. (const :tag "No" nil)
  2706. (const :tag "Yes" t)
  2707. (const :tag "Expert" expert)))
  2708. (defvar org-fast-tag-selection-include-todo nil
  2709. "Non-nil means fast tags selection interface will also offer TODO states.
  2710. This is an undocumented feature, you should not rely on it.")
  2711. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2712. "The column to which tags should be indented in a headline.
  2713. If this number is positive, it specifies the column. If it is negative,
  2714. it means that the tags should be flushright to that column. For example,
  2715. -80 works well for a normal 80 character screen.
  2716. When 0, place tags directly after headline text, with only one space in
  2717. between."
  2718. :group 'org-tags
  2719. :type 'integer)
  2720. (defcustom org-auto-align-tags t
  2721. "Non-nil keeps tags aligned when modifying headlines.
  2722. Some operations (i.e. demoting) change the length of a headline and
  2723. therefore shift the tags around. With this option turned on, after
  2724. each such operation the tags are again aligned to `org-tags-column'."
  2725. :group 'org-tags
  2726. :type 'boolean)
  2727. (defcustom org-use-tag-inheritance t
  2728. "Non-nil means tags in levels apply also for sublevels.
  2729. When nil, only the tags directly given in a specific line apply there.
  2730. This may also be a list of tags that should be inherited, or a regexp that
  2731. matches tags that should be inherited. Additional control is possible
  2732. with the variable `org-tags-exclude-from-inheritance' which gives an
  2733. explicit list of tags to be excluded from inheritance, even if the value of
  2734. `org-use-tag-inheritance' would select it for inheritance.
  2735. If this option is t, a match early-on in a tree can lead to a large
  2736. number of matches in the subtree when constructing the agenda or creating
  2737. a sparse tree. If you only want to see the first match in a tree during
  2738. a search, check out the variable `org-tags-match-list-sublevels'."
  2739. :group 'org-tags
  2740. :type '(choice
  2741. (const :tag "Not" nil)
  2742. (const :tag "Always" t)
  2743. (repeat :tag "Specific tags" (string :tag "Tag"))
  2744. (regexp :tag "Tags matched by regexp")))
  2745. (defcustom org-tags-exclude-from-inheritance nil
  2746. "List of tags that should never be inherited.
  2747. This is a way to exclude a few tags from inheritance. For way to do
  2748. the opposite, to actively allow inheritance for selected tags,
  2749. see the variable `org-use-tag-inheritance'."
  2750. :group 'org-tags
  2751. :type '(repeat (string :tag "Tag")))
  2752. (defun org-tag-inherit-p (tag)
  2753. "Check if TAG is one that should be inherited."
  2754. (cond
  2755. ((member tag org-tags-exclude-from-inheritance) nil)
  2756. ((eq org-use-tag-inheritance t) t)
  2757. ((not org-use-tag-inheritance) nil)
  2758. ((stringp org-use-tag-inheritance)
  2759. (string-match org-use-tag-inheritance tag))
  2760. ((listp org-use-tag-inheritance)
  2761. (member tag org-use-tag-inheritance))
  2762. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2763. (defcustom org-tags-match-list-sublevels t
  2764. "Non-nil means list also sublevels of headlines matching a search.
  2765. This variable applies to tags/property searches, and also to stuck
  2766. projects because this search is based on a tags match as well.
  2767. When set to the symbol `indented', sublevels are indented with
  2768. leading dots.
  2769. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2770. the sublevels of a headline matching a tag search often also match
  2771. the same search. Listing all of them can create very long lists.
  2772. Setting this variable to nil causes subtrees of a match to be skipped.
  2773. This variable is semi-obsolete and probably should always be true. It
  2774. is better to limit inheritance to certain tags using the variables
  2775. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2776. :group 'org-tags
  2777. :type '(choice
  2778. (const :tag "No, don't list them" nil)
  2779. (const :tag "Yes, do list them" t)
  2780. (const :tag "List them, indented with leading dots" indented)))
  2781. (defcustom org-tags-sort-function nil
  2782. "When set, tags are sorted using this function as a comparator."
  2783. :group 'org-tags
  2784. :type '(choice
  2785. (const :tag "No sorting" nil)
  2786. (const :tag "Alphabetical" string<)
  2787. (const :tag "Reverse alphabetical" string>)
  2788. (function :tag "Custom function" nil)))
  2789. (defvar org-tags-history nil
  2790. "History of minibuffer reads for tags.")
  2791. (defvar org-last-tags-completion-table nil
  2792. "The last used completion table for tags.")
  2793. (defvar org-after-tags-change-hook nil
  2794. "Hook that is run after the tags in a line have changed.")
  2795. (defgroup org-properties nil
  2796. "Options concerning properties in Org-mode."
  2797. :tag "Org Properties"
  2798. :group 'org)
  2799. (defcustom org-property-format "%-10s %s"
  2800. "How property key/value pairs should be formatted by `indent-line'.
  2801. When `indent-line' hits a property definition, it will format the line
  2802. according to this format, mainly to make sure that the values are
  2803. lined-up with respect to each other."
  2804. :group 'org-properties
  2805. :type 'string)
  2806. (defcustom org-properties-postprocess-alist nil
  2807. "Alist of properties and functions to adjust inserted values.
  2808. Elements of this alist must be of the form
  2809. ([string] [function])
  2810. where [string] must be a property name and [function] must be a
  2811. lambda expression: this lambda expression must take one argument,
  2812. the value to adjust, and return the new value as a string.
  2813. For example, this element will allow the property \"Remaining\"
  2814. to be updated wrt the relation between the \"Effort\" property
  2815. and the clock summary:
  2816. ((\"Remaining\" (lambda(value)
  2817. (let ((clocksum (org-clock-sum-current-item))
  2818. (effort (org-duration-string-to-minutes
  2819. (org-entry-get (point) \"Effort\"))))
  2820. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2821. :group 'org-properties
  2822. :version "24.1"
  2823. :type '(alist :key-type (string :tag "Property")
  2824. :value-type (function :tag "Function")))
  2825. (defcustom org-use-property-inheritance nil
  2826. "Non-nil means properties apply also for sublevels.
  2827. This setting is chiefly used during property searches. Turning it on can
  2828. cause significant overhead when doing a search, which is why it is not
  2829. on by default.
  2830. When nil, only the properties directly given in the current entry count.
  2831. When t, every property is inherited. The value may also be a list of
  2832. properties that should have inheritance, or a regular expression matching
  2833. properties that should be inherited.
  2834. However, note that some special properties use inheritance under special
  2835. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2836. and the properties ending in \"_ALL\" when they are used as descriptor
  2837. for valid values of a property.
  2838. Note for programmers:
  2839. When querying an entry with `org-entry-get', you can control if inheritance
  2840. should be used. By default, `org-entry-get' looks only at the local
  2841. properties. You can request inheritance by setting the inherit argument
  2842. to t (to force inheritance) or to `selective' (to respect the setting
  2843. in this variable)."
  2844. :group 'org-properties
  2845. :type '(choice
  2846. (const :tag "Not" nil)
  2847. (const :tag "Always" t)
  2848. (repeat :tag "Specific properties" (string :tag "Property"))
  2849. (regexp :tag "Properties matched by regexp")))
  2850. (defun org-property-inherit-p (property)
  2851. "Check if PROPERTY is one that should be inherited."
  2852. (cond
  2853. ((eq org-use-property-inheritance t) t)
  2854. ((not org-use-property-inheritance) nil)
  2855. ((stringp org-use-property-inheritance)
  2856. (string-match org-use-property-inheritance property))
  2857. ((listp org-use-property-inheritance)
  2858. (member property org-use-property-inheritance))
  2859. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2860. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2861. "The default column format, if no other format has been defined.
  2862. This variable can be set on the per-file basis by inserting a line
  2863. #+COLUMNS: %25ITEM ....."
  2864. :group 'org-properties
  2865. :type 'string)
  2866. (defcustom org-columns-ellipses ".."
  2867. "The ellipses to be used when a field in column view is truncated.
  2868. When this is the empty string, as many characters as possible are shown,
  2869. but then there will be no visual indication that the field has been truncated.
  2870. When this is a string of length N, the last N characters of a truncated
  2871. field are replaced by this string. If the column is narrower than the
  2872. ellipses string, only part of the ellipses string will be shown."
  2873. :group 'org-properties
  2874. :type 'string)
  2875. (defcustom org-columns-modify-value-for-display-function nil
  2876. "Function that modifies values for display in column view.
  2877. For example, it can be used to cut out a certain part from a time stamp.
  2878. The function must take 2 arguments:
  2879. column-title The title of the column (*not* the property name)
  2880. value The value that should be modified.
  2881. The function should return the value that should be displayed,
  2882. or nil if the normal value should be used."
  2883. :group 'org-properties
  2884. :type 'function)
  2885. (defcustom org-effort-property "Effort"
  2886. "The property that is being used to keep track of effort estimates.
  2887. Effort estimates given in this property need to have the format H:MM."
  2888. :group 'org-properties
  2889. :group 'org-progress
  2890. :type '(string :tag "Property"))
  2891. (defconst org-global-properties-fixed
  2892. '(("VISIBILITY_ALL" . "folded children content all")
  2893. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2894. "List of property/value pairs that can be inherited by any entry.
  2895. These are fixed values, for the preset properties. The user variable
  2896. that can be used to add to this list is `org-global-properties'.
  2897. The entries in this list are cons cells where the car is a property
  2898. name and cdr is a string with the value. If the value represents
  2899. multiple items like an \"_ALL\" property, separate the items by
  2900. spaces.")
  2901. (defcustom org-global-properties nil
  2902. "List of property/value pairs that can be inherited by any entry.
  2903. This list will be combined with the constant `org-global-properties-fixed'.
  2904. The entries in this list are cons cells where the car is a property
  2905. name and cdr is a string with the value.
  2906. You can set buffer-local values for the same purpose in the variable
  2907. `org-file-properties' this by adding lines like
  2908. #+PROPERTY: NAME VALUE"
  2909. :group 'org-properties
  2910. :type '(repeat
  2911. (cons (string :tag "Property")
  2912. (string :tag "Value"))))
  2913. (defvar org-file-properties nil
  2914. "List of property/value pairs that can be inherited by any entry.
  2915. Valid for the current buffer.
  2916. This variable is populated from #+PROPERTY lines.")
  2917. (make-variable-buffer-local 'org-file-properties)
  2918. (defgroup org-agenda nil
  2919. "Options concerning agenda views in Org-mode."
  2920. :tag "Org Agenda"
  2921. :group 'org)
  2922. (defvar org-category nil
  2923. "Variable used by org files to set a category for agenda display.
  2924. Such files should use a file variable to set it, for example
  2925. # -*- mode: org; org-category: \"ELisp\"
  2926. or contain a special line
  2927. #+CATEGORY: ELisp
  2928. If the file does not specify a category, then file's base name
  2929. is used instead.")
  2930. (make-variable-buffer-local 'org-category)
  2931. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2932. (defcustom org-agenda-files nil
  2933. "The files to be used for agenda display.
  2934. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2935. \\[org-remove-file]. You can also use customize to edit the list.
  2936. If an entry is a directory, all files in that directory that are matched by
  2937. `org-agenda-file-regexp' will be part of the file list.
  2938. If the value of the variable is not a list but a single file name, then
  2939. the list of agenda files is actually stored and maintained in that file, one
  2940. agenda file per line. In this file paths can be given relative to
  2941. `org-directory'. Tilde expansion and environment variable substitution
  2942. are also made."
  2943. :group 'org-agenda
  2944. :type '(choice
  2945. (repeat :tag "List of files and directories" file)
  2946. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2947. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2948. "Regular expression to match files for `org-agenda-files'.
  2949. If any element in the list in that variable contains a directory instead
  2950. of a normal file, all files in that directory that are matched by this
  2951. regular expression will be included."
  2952. :group 'org-agenda
  2953. :type 'regexp)
  2954. (defcustom org-agenda-text-search-extra-files nil
  2955. "List of extra files to be searched by text search commands.
  2956. These files will be search in addition to the agenda files by the
  2957. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2958. Note that these files will only be searched for text search commands,
  2959. not for the other agenda views like todo lists, tag searches or the weekly
  2960. agenda. This variable is intended to list notes and possibly archive files
  2961. that should also be searched by these two commands.
  2962. In fact, if the first element in the list is the symbol `agenda-archives',
  2963. than all archive files of all agenda files will be added to the search
  2964. scope."
  2965. :group 'org-agenda
  2966. :type '(set :greedy t
  2967. (const :tag "Agenda Archives" agenda-archives)
  2968. (repeat :inline t (file))))
  2969. (if (fboundp 'defvaralias)
  2970. (defvaralias 'org-agenda-multi-occur-extra-files
  2971. 'org-agenda-text-search-extra-files))
  2972. (defcustom org-agenda-skip-unavailable-files nil
  2973. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2974. A nil value means to remove them, after a query, from the list."
  2975. :group 'org-agenda
  2976. :type 'boolean)
  2977. (defcustom org-calendar-to-agenda-key [?c]
  2978. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2979. The command `org-calendar-goto-agenda' will be bound to this key. The
  2980. default is the character `c' because then `c' can be used to switch back and
  2981. forth between agenda and calendar."
  2982. :group 'org-agenda
  2983. :type 'sexp)
  2984. (defcustom org-calendar-insert-diary-entry-key [?i]
  2985. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2986. This option is irrelevant until `org-agenda-diary-file' has been configured
  2987. to point to an Org-mode file. When that is the case, the command
  2988. `org-agenda-diary-entry' will be bound to the key given here, by default
  2989. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2990. if you want to continue doing this, you need to change this to a different
  2991. key."
  2992. :group 'org-agenda
  2993. :type 'sexp)
  2994. (defcustom org-agenda-diary-file 'diary-file
  2995. "File to which to add new entries with the `i' key in agenda and calendar.
  2996. When this is the symbol `diary-file', the functionality in the Emacs
  2997. calendar will be used to add entries to the `diary-file'. But when this
  2998. points to a file, `org-agenda-diary-entry' will be used instead."
  2999. :group 'org-agenda
  3000. :type '(choice
  3001. (const :tag "The standard Emacs diary file" diary-file)
  3002. (file :tag "Special Org file diary entries")))
  3003. (eval-after-load "calendar"
  3004. '(progn
  3005. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3006. 'org-calendar-goto-agenda)
  3007. (add-hook 'calendar-mode-hook
  3008. (lambda ()
  3009. (unless (eq org-agenda-diary-file 'diary-file)
  3010. (define-key calendar-mode-map
  3011. org-calendar-insert-diary-entry-key
  3012. 'org-agenda-diary-entry))))))
  3013. (defgroup org-latex nil
  3014. "Options for embedding LaTeX code into Org-mode."
  3015. :tag "Org LaTeX"
  3016. :group 'org)
  3017. (defcustom org-format-latex-options
  3018. '(:foreground default :background default :scale 1.0
  3019. :html-foreground "Black" :html-background "Transparent"
  3020. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3021. "Options for creating images from LaTeX fragments.
  3022. This is a property list with the following properties:
  3023. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3024. `default' means use the foreground of the default face.
  3025. `auto' means use the foreground from the text face.
  3026. :background the background color, or \"Transparent\".
  3027. `default' means use the background of the default face.
  3028. `auto' means use the background from the text face.
  3029. :scale a scaling factor for the size of the images, to get more pixels
  3030. :html-foreground, :html-background, :html-scale
  3031. the same numbers for HTML export.
  3032. :matchers a list indicating which matchers should be used to
  3033. find LaTeX fragments. Valid members of this list are:
  3034. \"begin\" find environments
  3035. \"$1\" find single characters surrounded by $.$
  3036. \"$\" find math expressions surrounded by $...$
  3037. \"$$\" find math expressions surrounded by $$....$$
  3038. \"\\(\" find math expressions surrounded by \\(...\\)
  3039. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3040. :group 'org-latex
  3041. :type 'plist)
  3042. (defcustom org-format-latex-signal-error t
  3043. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3044. When nil, just push out a message."
  3045. :group 'org-latex
  3046. :version "24.1"
  3047. :type 'boolean)
  3048. (defcustom org-latex-to-mathml-jar-file nil
  3049. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3050. Use this to specify additional executable file say a jar file.
  3051. When using MathToWeb as the converter, specify the full-path to
  3052. your mathtoweb.jar file."
  3053. :group 'org-latex
  3054. :version "24.1"
  3055. :type '(choice
  3056. (const :tag "None" nil)
  3057. (file :tag "JAR file" :must-match t)))
  3058. (defcustom org-latex-to-mathml-convert-command nil
  3059. "Command to convert LaTeX fragments to MathML.
  3060. Replace format-specifiers in the command as noted below and use
  3061. `shell-command' to convert LaTeX to MathML.
  3062. %j: Executable file in fully expanded form as specified by
  3063. `org-latex-to-mathml-jar-file'.
  3064. %I: Input LaTeX file in fully expanded form
  3065. %o: Output MathML file
  3066. This command is used by `org-create-math-formula'.
  3067. When using MathToWeb as the converter, set this to
  3068. \"java -jar %j -unicode -force -df %o %I\"."
  3069. :group 'org-latex
  3070. :version "24.1"
  3071. :type '(choice
  3072. (const :tag "None" nil)
  3073. (string :tag "\nShell command")))
  3074. (defcustom org-latex-create-formula-image-program 'dvipng
  3075. "Program to convert LaTeX fragments with.
  3076. dvipng Process the LaTeX fragments to dvi file, then convert
  3077. dvi files to png files using dvipng.
  3078. This will also include processing of non-math environments.
  3079. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3080. to convert pdf files to png files"
  3081. :group 'org-latex
  3082. :version "24.1"
  3083. :type '(choice
  3084. (const :tag "dvipng" dvipng)
  3085. (const :tag "imagemagick" imagemagick)))
  3086. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3087. "Path to store latex preview images.
  3088. A relative path here creates many directories relative to the
  3089. processed org files paths. An absolute path puts all preview
  3090. images at the same place."
  3091. :group 'org-latex
  3092. :version "24.3"
  3093. :type 'string)
  3094. (defun org-format-latex-mathml-available-p ()
  3095. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3096. (save-match-data
  3097. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3098. org-latex-to-mathml-convert-command)
  3099. (let ((executable (car (split-string
  3100. org-latex-to-mathml-convert-command))))
  3101. (when (executable-find executable)
  3102. (if (string-match
  3103. "%j" org-latex-to-mathml-convert-command)
  3104. (file-readable-p org-latex-to-mathml-jar-file)
  3105. t))))))
  3106. (defcustom org-format-latex-header "\\documentclass{article}
  3107. \\usepackage[usenames]{color}
  3108. \\usepackage{amsmath}
  3109. \\usepackage[mathscr]{eucal}
  3110. \\pagestyle{empty} % do not remove
  3111. \[PACKAGES]
  3112. \[DEFAULT-PACKAGES]
  3113. % The settings below are copied from fullpage.sty
  3114. \\setlength{\\textwidth}{\\paperwidth}
  3115. \\addtolength{\\textwidth}{-3cm}
  3116. \\setlength{\\oddsidemargin}{1.5cm}
  3117. \\addtolength{\\oddsidemargin}{-2.54cm}
  3118. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3119. \\setlength{\\textheight}{\\paperheight}
  3120. \\addtolength{\\textheight}{-\\headheight}
  3121. \\addtolength{\\textheight}{-\\headsep}
  3122. \\addtolength{\\textheight}{-\\footskip}
  3123. \\addtolength{\\textheight}{-3cm}
  3124. \\setlength{\\topmargin}{1.5cm}
  3125. \\addtolength{\\topmargin}{-2.54cm}"
  3126. "The document header used for processing LaTeX fragments.
  3127. It is imperative that this header make sure that no page number
  3128. appears on the page. The package defined in the variables
  3129. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3130. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3131. will be appended."
  3132. :group 'org-latex
  3133. :type 'string)
  3134. (defvar org-format-latex-header-extra nil)
  3135. (defun org-set-packages-alist (var val)
  3136. "Set the packages alist and make sure it has 3 elements per entry."
  3137. (set var (mapcar (lambda (x)
  3138. (if (and (consp x) (= (length x) 2))
  3139. (list (car x) (nth 1 x) t)
  3140. x))
  3141. val)))
  3142. (defun org-get-packages-alist (var)
  3143. "Get the packages alist and make sure it has 3 elements per entry."
  3144. (mapcar (lambda (x)
  3145. (if (and (consp x) (= (length x) 2))
  3146. (list (car x) (nth 1 x) t)
  3147. x))
  3148. (default-value var)))
  3149. ;; The following variables are defined here because is it also used
  3150. ;; when formatting latex fragments. Originally it was part of the
  3151. ;; LaTeX exporter, which is why the name includes "export".
  3152. (defcustom org-export-latex-default-packages-alist
  3153. '(("AUTO" "inputenc" t)
  3154. ("T1" "fontenc" t)
  3155. ("" "fixltx2e" nil)
  3156. ("" "graphicx" t)
  3157. ("" "longtable" nil)
  3158. ("" "float" nil)
  3159. ("" "wrapfig" nil)
  3160. ("" "soul" t)
  3161. ("" "textcomp" t)
  3162. ("" "marvosym" t)
  3163. ("" "wasysym" t)
  3164. ("" "latexsym" t)
  3165. ("" "amssymb" t)
  3166. ("" "hyperref" nil)
  3167. "\\tolerance=1000"
  3168. )
  3169. "Alist of default packages to be inserted in the header.
  3170. Change this only if one of the packages here causes an incompatibility
  3171. with another package you are using.
  3172. The packages in this list are needed by one part or another of Org-mode
  3173. to function properly.
  3174. - inputenc, fontenc: for basic font and character selection
  3175. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3176. for interpreting the entities in `org-entities'. You can skip some of these
  3177. packages if you don't use any of the symbols in it.
  3178. - graphicx: for including images
  3179. - float, wrapfig: for figure placement
  3180. - longtable: for long tables
  3181. - hyperref: for cross references
  3182. Therefore you should not modify this variable unless you know what you
  3183. are doing. The one reason to change it anyway is that you might be loading
  3184. some other package that conflicts with one of the default packages.
  3185. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3186. If SNIPPET-FLAG is t, the package also needs to be included when
  3187. compiling LaTeX snippets into images for inclusion into HTML."
  3188. :group 'org-export-latex
  3189. :set 'org-set-packages-alist
  3190. :get 'org-get-packages-alist
  3191. :version "24.1"
  3192. :type '(repeat
  3193. (choice
  3194. (list :tag "options/package pair"
  3195. (string :tag "options")
  3196. (string :tag "package")
  3197. (boolean :tag "Snippet"))
  3198. (string :tag "A line of LaTeX"))))
  3199. (defcustom org-export-latex-packages-alist nil
  3200. "Alist of packages to be inserted in every LaTeX header.
  3201. These will be inserted after `org-export-latex-default-packages-alist'.
  3202. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3203. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3204. turning LaTeX snippets into images for inclusion into HTML.
  3205. Make sure that you only list packages here which:
  3206. - you want in every file
  3207. - do not conflict with the default packages in
  3208. `org-export-latex-default-packages-alist'
  3209. - do not conflict with the setup in `org-format-latex-header'."
  3210. :group 'org-export-latex
  3211. :set 'org-set-packages-alist
  3212. :get 'org-get-packages-alist
  3213. :type '(repeat
  3214. (choice
  3215. (list :tag "options/package pair"
  3216. (string :tag "options")
  3217. (string :tag "package")
  3218. (boolean :tag "Snippet"))
  3219. (string :tag "A line of LaTeX"))))
  3220. (defgroup org-appearance nil
  3221. "Settings for Org-mode appearance."
  3222. :tag "Org Appearance"
  3223. :group 'org)
  3224. (defcustom org-level-color-stars-only nil
  3225. "Non-nil means fontify only the stars in each headline.
  3226. When nil, the entire headline is fontified.
  3227. Changing it requires restart of `font-lock-mode' to become effective
  3228. also in regions already fontified."
  3229. :group 'org-appearance
  3230. :type 'boolean)
  3231. (defcustom org-hide-leading-stars nil
  3232. "Non-nil means hide the first N-1 stars in a headline.
  3233. This works by using the face `org-hide' for these stars. This
  3234. face is white for a light background, and black for a dark
  3235. background. You may have to customize the face `org-hide' to
  3236. make this work.
  3237. Changing it requires restart of `font-lock-mode' to become effective
  3238. also in regions already fontified.
  3239. You may also set this on a per-file basis by adding one of the following
  3240. lines to the buffer:
  3241. #+STARTUP: hidestars
  3242. #+STARTUP: showstars"
  3243. :group 'org-appearance
  3244. :type 'boolean)
  3245. (defcustom org-hidden-keywords nil
  3246. "List of symbols corresponding to keywords to be hidden the org buffer.
  3247. For example, a value '(title) for this list will make the document's title
  3248. appear in the buffer without the initial #+TITLE: keyword."
  3249. :group 'org-appearance
  3250. :version "24.1"
  3251. :type '(set (const :tag "#+AUTHOR" author)
  3252. (const :tag "#+DATE" date)
  3253. (const :tag "#+EMAIL" email)
  3254. (const :tag "#+TITLE" title)))
  3255. (defcustom org-custom-properties nil
  3256. "List of properties (as strings) with a special meaning.
  3257. The default use of these custom properties is to let the user
  3258. hide them with `org-toggle-custom-properties-visibility'."
  3259. :group 'org-properties
  3260. :group 'org-appearance
  3261. :version "24.3"
  3262. :type '(repeat (string :tag "Property Name")))
  3263. (defcustom org-fontify-done-headline nil
  3264. "Non-nil means change the face of a headline if it is marked DONE.
  3265. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3266. When this is non-nil, the headline after the keyword is set to the
  3267. `org-headline-done' as an additional indication."
  3268. :group 'org-appearance
  3269. :type 'boolean)
  3270. (defcustom org-fontify-emphasized-text t
  3271. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3272. Changing this variable requires a restart of Emacs to take effect."
  3273. :group 'org-appearance
  3274. :type 'boolean)
  3275. (defcustom org-fontify-whole-heading-line nil
  3276. "Non-nil means fontify the whole line for headings.
  3277. This is useful when setting a background color for the
  3278. org-level-* faces."
  3279. :group 'org-appearance
  3280. :type 'boolean)
  3281. (defcustom org-highlight-latex-fragments-and-specials nil
  3282. "Non-nil means fontify what is treated specially by the exporters."
  3283. :group 'org-appearance
  3284. :type 'boolean)
  3285. (defcustom org-hide-emphasis-markers nil
  3286. "Non-nil mean font-lock should hide the emphasis marker characters."
  3287. :group 'org-appearance
  3288. :type 'boolean)
  3289. (defcustom org-pretty-entities nil
  3290. "Non-nil means show entities as UTF8 characters.
  3291. When nil, the \\name form remains in the buffer."
  3292. :group 'org-appearance
  3293. :version "24.1"
  3294. :type 'boolean)
  3295. (defcustom org-pretty-entities-include-sub-superscripts t
  3296. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3297. :group 'org-appearance
  3298. :version "24.1"
  3299. :type 'boolean)
  3300. (defvar org-emph-re nil
  3301. "Regular expression for matching emphasis.
  3302. After a match, the match groups contain these elements:
  3303. 0 The match of the full regular expression, including the characters
  3304. before and after the proper match
  3305. 1 The character before the proper match, or empty at beginning of line
  3306. 2 The proper match, including the leading and trailing markers
  3307. 3 The leading marker like * or /, indicating the type of highlighting
  3308. 4 The text between the emphasis markers, not including the markers
  3309. 5 The character after the match, empty at the end of a line")
  3310. (defvar org-verbatim-re nil
  3311. "Regular expression for matching verbatim text.")
  3312. (defvar org-emphasis-regexp-components) ; defined just below
  3313. (defvar org-emphasis-alist) ; defined just below
  3314. (defun org-set-emph-re (var val)
  3315. "Set variable and compute the emphasis regular expression."
  3316. (set var val)
  3317. (when (and (boundp 'org-emphasis-alist)
  3318. (boundp 'org-emphasis-regexp-components)
  3319. org-emphasis-alist org-emphasis-regexp-components)
  3320. (let* ((e org-emphasis-regexp-components)
  3321. (pre (car e))
  3322. (post (nth 1 e))
  3323. (border (nth 2 e))
  3324. (body (nth 3 e))
  3325. (nl (nth 4 e))
  3326. (body1 (concat body "*?"))
  3327. (markers (mapconcat 'car org-emphasis-alist ""))
  3328. (vmarkers (mapconcat
  3329. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3330. org-emphasis-alist "")))
  3331. ;; make sure special characters appear at the right position in the class
  3332. (if (string-match "\\^" markers)
  3333. (setq markers (concat (replace-match "" t t markers) "^")))
  3334. (if (string-match "-" markers)
  3335. (setq markers (concat (replace-match "" t t markers) "-")))
  3336. (if (string-match "\\^" vmarkers)
  3337. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3338. (if (string-match "-" vmarkers)
  3339. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3340. (if (> nl 0)
  3341. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3342. (int-to-string nl) "\\}")))
  3343. ;; Make the regexp
  3344. (setq org-emph-re
  3345. (concat "\\([" pre "]\\|^\\)"
  3346. "\\("
  3347. "\\([" markers "]\\)"
  3348. "\\("
  3349. "[^" border "]\\|"
  3350. "[^" border "]"
  3351. body1
  3352. "[^" border "]"
  3353. "\\)"
  3354. "\\3\\)"
  3355. "\\([" post "]\\|$\\)"))
  3356. (setq org-verbatim-re
  3357. (concat "\\([" pre "]\\|^\\)"
  3358. "\\("
  3359. "\\([" vmarkers "]\\)"
  3360. "\\("
  3361. "[^" border "]\\|"
  3362. "[^" border "]"
  3363. body1
  3364. "[^" border "]"
  3365. "\\)"
  3366. "\\3\\)"
  3367. "\\([" post "]\\|$\\)")))))
  3368. (defcustom org-emphasis-regexp-components
  3369. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3370. "Components used to build the regular expression for emphasis.
  3371. This is a list with five entries. Terminology: In an emphasis string
  3372. like \" *strong word* \", we call the initial space PREMATCH, the final
  3373. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3374. and \"trong wor\" is the body. The different components in this variable
  3375. specify what is allowed/forbidden in each part:
  3376. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3377. post Chars allowed as postmatch. End of line will be allowed too.
  3378. border The chars *forbidden* as border characters.
  3379. body-regexp A regexp like \".\" to match a body character. Don't use
  3380. non-shy groups here, and don't allow newline here.
  3381. newline The maximum number of newlines allowed in an emphasis exp.
  3382. Use customize to modify this, or restart Emacs after changing it."
  3383. :group 'org-appearance
  3384. :set 'org-set-emph-re
  3385. :type '(list
  3386. (sexp :tag "Allowed chars in pre ")
  3387. (sexp :tag "Allowed chars in post ")
  3388. (sexp :tag "Forbidden chars in border ")
  3389. (sexp :tag "Regexp for body ")
  3390. (integer :tag "number of newlines allowed")
  3391. (option (boolean :tag "Please ignore this button"))))
  3392. (defcustom org-emphasis-alist
  3393. `(("*" bold "<b>" "</b>")
  3394. ("/" italic "<i>" "</i>")
  3395. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3396. ("=" org-code "<code>" "</code>" verbatim)
  3397. ("~" org-verbatim "<code>" "</code>" verbatim)
  3398. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3399. "<del>" "</del>")
  3400. )
  3401. "Special syntax for emphasized text.
  3402. Text starting and ending with a special character will be emphasized, for
  3403. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3404. characters, the face to be used by font-lock for highlighting in Org-mode
  3405. Emacs buffers, and the HTML tags to be used for this.
  3406. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3407. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3408. Use customize to modify this, or restart Emacs after changing it."
  3409. :group 'org-appearance
  3410. :set 'org-set-emph-re
  3411. :type '(repeat
  3412. (list
  3413. (string :tag "Marker character")
  3414. (choice
  3415. (face :tag "Font-lock-face")
  3416. (plist :tag "Face property list"))
  3417. (string :tag "HTML start tag")
  3418. (string :tag "HTML end tag")
  3419. (option (const verbatim)))))
  3420. (defvar org-syntax-table
  3421. (let ((st (make-syntax-table)))
  3422. (mapc (lambda(c) (modify-syntax-entry
  3423. (string-to-char (car c)) "w p" st))
  3424. org-emphasis-alist)
  3425. st))
  3426. (defvar org-protecting-blocks
  3427. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3428. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3429. This is needed for font-lock setup.")
  3430. ;;; Miscellaneous options
  3431. (defgroup org-completion nil
  3432. "Completion in Org-mode."
  3433. :tag "Org Completion"
  3434. :group 'org)
  3435. (defcustom org-completion-use-ido nil
  3436. "Non-nil means use ido completion wherever possible.
  3437. Note that `ido-mode' must be active for this variable to be relevant.
  3438. If you decide to turn this variable on, you might well want to turn off
  3439. `org-outline-path-complete-in-steps'.
  3440. See also `org-completion-use-iswitchb'."
  3441. :group 'org-completion
  3442. :type 'boolean)
  3443. (defcustom org-completion-use-iswitchb nil
  3444. "Non-nil means use iswitchb completion wherever possible.
  3445. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3446. If you decide to turn this variable on, you might well want to turn off
  3447. `org-outline-path-complete-in-steps'.
  3448. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3449. :group 'org-completion
  3450. :type 'boolean)
  3451. (defcustom org-completion-fallback-command 'hippie-expand
  3452. "The expansion command called by \\[pcomplete] in normal context.
  3453. Normal means, no org-mode-specific context."
  3454. :group 'org-completion
  3455. :type 'function)
  3456. ;;; Functions and variables from their packages
  3457. ;; Declared here to avoid compiler warnings
  3458. ;; XEmacs only
  3459. (defvar outline-mode-menu-heading)
  3460. (defvar outline-mode-menu-show)
  3461. (defvar outline-mode-menu-hide)
  3462. (defvar zmacs-regions) ; XEmacs regions
  3463. ;; Emacs only
  3464. (defvar mark-active)
  3465. ;; Various packages
  3466. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3467. (declare-function calendar-forward-day "cal-move" (arg))
  3468. (declare-function calendar-goto-date "cal-move" (date))
  3469. (declare-function calendar-goto-today "cal-move" ())
  3470. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3471. (defvar calc-embedded-close-formula)
  3472. (defvar calc-embedded-open-formula)
  3473. (declare-function cdlatex-tab "ext:cdlatex" ())
  3474. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3475. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3476. (defvar font-lock-unfontify-region-function)
  3477. (declare-function iswitchb-read-buffer "iswitchb"
  3478. (prompt &optional default require-match start matches-set))
  3479. (defvar iswitchb-temp-buflist)
  3480. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3481. (defvar org-agenda-tags-todo-honor-ignore-options)
  3482. (declare-function org-agenda-skip "org-agenda" ())
  3483. (declare-function
  3484. org-agenda-format-item "org-agenda"
  3485. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3486. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3487. (declare-function org-agenda-change-all-lines "org-agenda"
  3488. (newhead hdmarker &optional fixface just-this))
  3489. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3490. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3491. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3492. (beg end))
  3493. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3494. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3495. "org-agenda" (&optional end))
  3496. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3497. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3498. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3499. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3500. (declare-function org-indent-mode "org-indent" (&optional arg))
  3501. (declare-function parse-time-string "parse-time" (string))
  3502. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3503. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3504. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3505. (defvar remember-data-file)
  3506. (defvar texmathp-why)
  3507. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3508. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3509. (defvar org-latex-regexps)
  3510. ;;; Autoload and prepare some org modules
  3511. ;; Some table stuff that needs to be defined here, because it is used
  3512. ;; by the functions setting up org-mode or checking for table context.
  3513. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3514. "Detect an org-type or table-type table.")
  3515. (defconst org-table-line-regexp "^[ \t]*|"
  3516. "Detect an org-type table line.")
  3517. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3518. "Detect an org-type table line.")
  3519. (defconst org-table-hline-regexp "^[ \t]*|-"
  3520. "Detect an org-type table hline.")
  3521. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3522. "Detect a table-type table hline.")
  3523. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3524. "Detect the first line outside a table when searching from within it.
  3525. This works for both table types.")
  3526. ;; Autoload the functions in org-table.el that are needed by functions here.
  3527. (eval-and-compile
  3528. (org-autoload "org-table"
  3529. '(org-table-begin org-table-blank-field org-table-end)))
  3530. ;;;###autoload
  3531. (defun turn-on-orgtbl ()
  3532. "Unconditionally turn on `orgtbl-mode'."
  3533. (require 'org-table)
  3534. (orgtbl-mode 1))
  3535. (defun org-at-table-p (&optional table-type)
  3536. "Return t if the cursor is inside an org-type table.
  3537. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3538. (if org-enable-table-editor
  3539. (save-excursion
  3540. (beginning-of-line 1)
  3541. (looking-at (if table-type org-table-any-line-regexp
  3542. org-table-line-regexp)))
  3543. nil))
  3544. (defsubst org-table-p () (org-at-table-p))
  3545. (defun org-at-table.el-p ()
  3546. "Return t if and only if we are at a table.el table."
  3547. (and (org-at-table-p 'any)
  3548. (save-excursion
  3549. (goto-char (org-table-begin 'any))
  3550. (looking-at org-table1-hline-regexp))))
  3551. (defun org-table-recognize-table.el ()
  3552. "If there is a table.el table nearby, recognize it and move into it."
  3553. (if org-table-tab-recognizes-table.el
  3554. (if (org-at-table.el-p)
  3555. (progn
  3556. (beginning-of-line 1)
  3557. (if (looking-at org-table-dataline-regexp)
  3558. nil
  3559. (if (looking-at org-table1-hline-regexp)
  3560. (progn
  3561. (beginning-of-line 2)
  3562. (if (looking-at org-table-any-border-regexp)
  3563. (beginning-of-line -1)))))
  3564. (if (re-search-forward "|" (org-table-end t) t)
  3565. (progn
  3566. (require 'table)
  3567. (if (table--at-cell-p (point))
  3568. t
  3569. (message "recognizing table.el table...")
  3570. (table-recognize-table)
  3571. (message "recognizing table.el table...done")))
  3572. (error "This should not happen"))
  3573. t)
  3574. nil)
  3575. nil))
  3576. (defun org-at-table-hline-p ()
  3577. "Return t if the cursor is inside a hline in a table."
  3578. (if org-enable-table-editor
  3579. (save-excursion
  3580. (beginning-of-line 1)
  3581. (looking-at org-table-hline-regexp))
  3582. nil))
  3583. (defvar org-table-clean-did-remove-column nil)
  3584. (defun org-table-map-tables (function &optional quietly)
  3585. "Apply FUNCTION to the start of all tables in the buffer."
  3586. (save-excursion
  3587. (save-restriction
  3588. (widen)
  3589. (goto-char (point-min))
  3590. (while (re-search-forward org-table-any-line-regexp nil t)
  3591. (unless quietly
  3592. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3593. (beginning-of-line 1)
  3594. (when (and (looking-at org-table-line-regexp)
  3595. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3596. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3597. (save-excursion (funcall function))
  3598. (or (looking-at org-table-line-regexp)
  3599. (forward-char 1)))
  3600. (re-search-forward org-table-any-border-regexp nil 1))))
  3601. (unless quietly (message "Mapping tables: done")))
  3602. ;; Declare and autoload functions from org-exp.el & Co
  3603. (declare-function org-default-export-plist "org-exp")
  3604. (declare-function org-infile-export-plist "org-exp")
  3605. (declare-function org-get-current-options "org-exp")
  3606. ;; Declare and autoload functions from org-agenda.el
  3607. (eval-and-compile
  3608. (org-autoload "org-agenda"
  3609. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3610. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3611. (declare-function org-clock-update-mode-line "org-clock" ())
  3612. (declare-function org-resolve-clocks "org-clock"
  3613. (&optional also-non-dangling-p prompt last-valid))
  3614. (defvar org-clock-start-time)
  3615. (defvar org-clock-marker (make-marker)
  3616. "Marker recording the last clock-in.")
  3617. (defvar org-clock-hd-marker (make-marker)
  3618. "Marker recording the last clock-in, but the headline position.")
  3619. (defvar org-clock-heading ""
  3620. "The heading of the current clock entry.")
  3621. (defun org-clock-is-active ()
  3622. "Return non-nil if clock is currently running.
  3623. The return value is actually the clock marker."
  3624. (marker-buffer org-clock-marker))
  3625. (eval-and-compile
  3626. (org-autoload "org-clock" '(org-clock-remove-overlays
  3627. org-clock-update-time-maybe
  3628. org-clocktable-shift)))
  3629. (defun org-check-running-clock ()
  3630. "Check if the current buffer contains the running clock.
  3631. If yes, offer to stop it and to save the buffer with the changes."
  3632. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3633. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3634. (buffer-name))))
  3635. (org-clock-out)
  3636. (when (y-or-n-p "Save changed buffer?")
  3637. (save-buffer))))
  3638. (defun org-clocktable-try-shift (dir n)
  3639. "Check if this line starts a clock table, if yes, shift the time block."
  3640. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3641. (org-clocktable-shift dir n)))
  3642. ;;;###autoload
  3643. (defun org-clock-persistence-insinuate ()
  3644. "Set up hooks for clock persistence."
  3645. (require 'org-clock)
  3646. (add-hook 'org-mode-hook 'org-clock-load)
  3647. (add-hook 'kill-emacs-hook 'org-clock-save))
  3648. ;; Define the variable already here, to make sure we have it.
  3649. (defvar org-indent-mode nil
  3650. "Non-nil if Org-Indent mode is enabled.
  3651. Use the command `org-indent-mode' to change this variable.")
  3652. ;; Autoload archiving code
  3653. ;; The stuff that is needed for cycling and tags has to be defined here.
  3654. (defgroup org-archive nil
  3655. "Options concerning archiving in Org-mode."
  3656. :tag "Org Archive"
  3657. :group 'org-structure)
  3658. (defcustom org-archive-location "%s_archive::"
  3659. "The location where subtrees should be archived.
  3660. The value of this variable is a string, consisting of two parts,
  3661. separated by a double-colon. The first part is a filename and
  3662. the second part is a headline.
  3663. When the filename is omitted, archiving happens in the same file.
  3664. %s in the filename will be replaced by the current file
  3665. name (without the directory part). Archiving to a different file
  3666. is useful to keep archived entries from contributing to the
  3667. Org-mode Agenda.
  3668. The archived entries will be filed as subtrees of the specified
  3669. headline. When the headline is omitted, the subtrees are simply
  3670. filed away at the end of the file, as top-level entries. Also in
  3671. the heading you can use %s to represent the file name, this can be
  3672. useful when using the same archive for a number of different files.
  3673. Here are a few examples:
  3674. \"%s_archive::\"
  3675. If the current file is Projects.org, archive in file
  3676. Projects.org_archive, as top-level trees. This is the default.
  3677. \"::* Archived Tasks\"
  3678. Archive in the current file, under the top-level headline
  3679. \"* Archived Tasks\".
  3680. \"~/org/archive.org::\"
  3681. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3682. \"~/org/archive.org::* From %s\"
  3683. Archive in file ~/org/archive.org (absolute path), under headlines
  3684. \"From FILENAME\" where file name is the current file name.
  3685. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3686. The \"datetree/\" string is special, signifying to archive
  3687. items to the datetree. Items are placed in either the CLOSED
  3688. date of the item, or the current date if there is no CLOSED date.
  3689. The heading will be a subentry to the current date. There doesn't
  3690. need to be a heading, but there always needs to be a slash after
  3691. datetree. For example, to store archived items directly in the
  3692. datetree, use \"~/org/datetree.org::datetree/\".
  3693. \"basement::** Finished Tasks\"
  3694. Archive in file ./basement (relative path), as level 3 trees
  3695. below the level 2 heading \"** Finished Tasks\".
  3696. You may set this option on a per-file basis by adding to the buffer a
  3697. line like
  3698. #+ARCHIVE: basement::** Finished Tasks
  3699. You may also define it locally for a subtree by setting an ARCHIVE property
  3700. in the entry. If such a property is found in an entry, or anywhere up
  3701. the hierarchy, it will be used."
  3702. :group 'org-archive
  3703. :type 'string)
  3704. (defcustom org-archive-tag "ARCHIVE"
  3705. "The tag that marks a subtree as archived.
  3706. An archived subtree does not open during visibility cycling, and does
  3707. not contribute to the agenda listings.
  3708. After changing this, font-lock must be restarted in the relevant buffers to
  3709. get the proper fontification."
  3710. :group 'org-archive
  3711. :group 'org-keywords
  3712. :type 'string)
  3713. (defcustom org-agenda-skip-archived-trees t
  3714. "Non-nil means the agenda will skip any items located in archived trees.
  3715. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3716. variable is no longer recommended, you should leave it at the value t.
  3717. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3718. :group 'org-archive
  3719. :group 'org-agenda-skip
  3720. :type 'boolean)
  3721. (defcustom org-columns-skip-archived-trees t
  3722. "Non-nil means ignore archived trees when creating column view."
  3723. :group 'org-archive
  3724. :group 'org-properties
  3725. :type 'boolean)
  3726. (defcustom org-cycle-open-archived-trees nil
  3727. "Non-nil means `org-cycle' will open archived trees.
  3728. An archived tree is a tree marked with the tag ARCHIVE.
  3729. When nil, archived trees will stay folded. You can still open them with
  3730. normal outline commands like `show-all', but not with the cycling commands."
  3731. :group 'org-archive
  3732. :group 'org-cycle
  3733. :type 'boolean)
  3734. (defcustom org-sparse-tree-open-archived-trees nil
  3735. "Non-nil means sparse tree construction shows matches in archived trees.
  3736. When nil, matches in these trees are highlighted, but the trees are kept in
  3737. collapsed state."
  3738. :group 'org-archive
  3739. :group 'org-sparse-trees
  3740. :type 'boolean)
  3741. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3742. "The default date type when building a sparse tree.
  3743. When this is nil, a date is a scheduled or a deadline timestamp.
  3744. Otherwise, these types are allowed:
  3745. all: all timestamps
  3746. active: only active timestamps (<...>)
  3747. inactive: only inactive timestamps (<...)
  3748. scheduled: only scheduled timestamps
  3749. deadline: only deadline timestamps"
  3750. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3751. (const :tag "All timestamps" all)
  3752. (const :tag "Only active timestamps" active)
  3753. (const :tag "Only inactive timestamps" inactive)
  3754. (const :tag "Only scheduled timestamps" scheduled)
  3755. (const :tag "Only deadline timestamps" deadline))
  3756. :version "24.3"
  3757. :group 'org-sparse-trees)
  3758. (defun org-cycle-hide-archived-subtrees (state)
  3759. "Re-hide all archived subtrees after a visibility state change."
  3760. (when (and (not org-cycle-open-archived-trees)
  3761. (not (memq state '(overview folded))))
  3762. (save-excursion
  3763. (let* ((globalp (memq state '(contents all)))
  3764. (beg (if globalp (point-min) (point)))
  3765. (end (if globalp (point-max) (org-end-of-subtree t))))
  3766. (org-hide-archived-subtrees beg end)
  3767. (goto-char beg)
  3768. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3769. (message "%s" (substitute-command-keys
  3770. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3771. (defun org-force-cycle-archived ()
  3772. "Cycle subtree even if it is archived."
  3773. (interactive)
  3774. (setq this-command 'org-cycle)
  3775. (let ((org-cycle-open-archived-trees t))
  3776. (call-interactively 'org-cycle)))
  3777. (defun org-hide-archived-subtrees (beg end)
  3778. "Re-hide all archived subtrees after a visibility state change."
  3779. (save-excursion
  3780. (let* ((re (concat ":" org-archive-tag ":")))
  3781. (goto-char beg)
  3782. (while (re-search-forward re end t)
  3783. (when (org-at-heading-p)
  3784. (org-flag-subtree t)
  3785. (org-end-of-subtree t))))))
  3786. (declare-function outline-end-of-heading "outline" ())
  3787. (declare-function outline-flag-region "outline" (from to flag))
  3788. (defun org-flag-subtree (flag)
  3789. (save-excursion
  3790. (org-back-to-heading t)
  3791. (outline-end-of-heading)
  3792. (outline-flag-region (point)
  3793. (progn (org-end-of-subtree t) (point))
  3794. flag)))
  3795. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3796. (eval-and-compile
  3797. (org-autoload "org-archive"
  3798. '(org-add-archive-files)))
  3799. ;; Autoload Column View Code
  3800. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3801. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3802. (declare-function org-columns-compute "org-colview" (property))
  3803. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3804. '(org-columns-number-to-string
  3805. org-columns-get-format-and-top-level
  3806. org-columns-compute
  3807. org-columns-remove-overlays))
  3808. ;; Autoload ID code
  3809. (declare-function org-id-store-link "org-id")
  3810. (declare-function org-id-locations-load "org-id")
  3811. (declare-function org-id-locations-save "org-id")
  3812. (defvar org-id-track-globally)
  3813. (org-autoload "org-id"
  3814. '(org-id-new
  3815. org-id-copy
  3816. org-id-get-with-outline-path-completion
  3817. org-id-get-with-outline-drilling))
  3818. ;;; Variables for pre-computed regular expressions, all buffer local
  3819. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3820. "Matches first line of a hidden block.")
  3821. (make-variable-buffer-local 'org-drawer-regexp)
  3822. (defvar org-todo-regexp nil
  3823. "Matches any of the TODO state keywords.")
  3824. (make-variable-buffer-local 'org-todo-regexp)
  3825. (defvar org-not-done-regexp nil
  3826. "Matches any of the TODO state keywords except the last one.")
  3827. (make-variable-buffer-local 'org-not-done-regexp)
  3828. (defvar org-not-done-heading-regexp nil
  3829. "Matches a TODO headline that is not done.")
  3830. (make-variable-buffer-local 'org-not-done-regexp)
  3831. (defvar org-todo-line-regexp nil
  3832. "Matches a headline and puts TODO state into group 2 if present.")
  3833. (make-variable-buffer-local 'org-todo-line-regexp)
  3834. (defvar org-complex-heading-regexp nil
  3835. "Matches a headline and puts everything into groups:
  3836. group 1: the stars
  3837. group 2: The todo keyword, maybe
  3838. group 3: Priority cookie
  3839. group 4: True headline
  3840. group 5: Tags")
  3841. (make-variable-buffer-local 'org-complex-heading-regexp)
  3842. (defvar org-complex-heading-regexp-format nil
  3843. "Printf format to make regexp to match an exact headline.
  3844. This regexp will match the headline of any node which has the
  3845. exact headline text that is put into the format, but may have any
  3846. TODO state, priority and tags.")
  3847. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3848. (defvar org-todo-line-tags-regexp nil
  3849. "Matches a headline and puts TODO state into group 2 if present.
  3850. Also put tags into group 4 if tags are present.")
  3851. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3852. (defvar org-ds-keyword-length 12
  3853. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3854. (make-variable-buffer-local 'org-ds-keyword-length)
  3855. (defvar org-deadline-regexp nil
  3856. "Matches the DEADLINE keyword.")
  3857. (make-variable-buffer-local 'org-deadline-regexp)
  3858. (defvar org-deadline-time-regexp nil
  3859. "Matches the DEADLINE keyword together with a time stamp.")
  3860. (make-variable-buffer-local 'org-deadline-time-regexp)
  3861. (defvar org-deadline-line-regexp nil
  3862. "Matches the DEADLINE keyword and the rest of the line.")
  3863. (make-variable-buffer-local 'org-deadline-line-regexp)
  3864. (defvar org-scheduled-regexp nil
  3865. "Matches the SCHEDULED keyword.")
  3866. (make-variable-buffer-local 'org-scheduled-regexp)
  3867. (defvar org-scheduled-time-regexp nil
  3868. "Matches the SCHEDULED keyword together with a time stamp.")
  3869. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3870. (defvar org-closed-time-regexp nil
  3871. "Matches the CLOSED keyword together with a time stamp.")
  3872. (make-variable-buffer-local 'org-closed-time-regexp)
  3873. (defvar org-keyword-time-regexp nil
  3874. "Matches any of the 4 keywords, together with the time stamp.")
  3875. (make-variable-buffer-local 'org-keyword-time-regexp)
  3876. (defvar org-keyword-time-not-clock-regexp nil
  3877. "Matches any of the 3 keywords, together with the time stamp.")
  3878. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3879. (defvar org-maybe-keyword-time-regexp nil
  3880. "Matches a timestamp, possibly preceded by a keyword.")
  3881. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3882. (defvar org-all-time-keywords nil
  3883. "List of time keywords.")
  3884. (make-variable-buffer-local 'org-all-time-keywords)
  3885. (defconst org-plain-time-of-day-regexp
  3886. (concat
  3887. "\\(\\<[012]?[0-9]"
  3888. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3889. "\\(--?"
  3890. "\\(\\<[012]?[0-9]"
  3891. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3892. "\\)?")
  3893. "Regular expression to match a plain time or time range.
  3894. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3895. groups carry important information:
  3896. 0 the full match
  3897. 1 the first time, range or not
  3898. 8 the second time, if it is a range.")
  3899. (defconst org-plain-time-extension-regexp
  3900. (concat
  3901. "\\(\\<[012]?[0-9]"
  3902. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3903. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3904. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3905. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3906. groups carry important information:
  3907. 0 the full match
  3908. 7 hours of duration
  3909. 9 minutes of duration")
  3910. (defconst org-stamp-time-of-day-regexp
  3911. (concat
  3912. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3913. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3914. "\\(--?"
  3915. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3916. "Regular expression to match a timestamp time or time range.
  3917. After a match, the following groups carry important information:
  3918. 0 the full match
  3919. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3920. 2 the first time, range or not
  3921. 4 the second time, if it is a range.")
  3922. (defconst org-startup-options
  3923. '(("fold" org-startup-folded t)
  3924. ("overview" org-startup-folded t)
  3925. ("nofold" org-startup-folded nil)
  3926. ("showall" org-startup-folded nil)
  3927. ("showeverything" org-startup-folded showeverything)
  3928. ("content" org-startup-folded content)
  3929. ("indent" org-startup-indented t)
  3930. ("noindent" org-startup-indented nil)
  3931. ("hidestars" org-hide-leading-stars t)
  3932. ("showstars" org-hide-leading-stars nil)
  3933. ("odd" org-odd-levels-only t)
  3934. ("oddeven" org-odd-levels-only nil)
  3935. ("align" org-startup-align-all-tables t)
  3936. ("noalign" org-startup-align-all-tables nil)
  3937. ("inlineimages" org-startup-with-inline-images t)
  3938. ("noinlineimages" org-startup-with-inline-images nil)
  3939. ("customtime" org-display-custom-times t)
  3940. ("logdone" org-log-done time)
  3941. ("lognotedone" org-log-done note)
  3942. ("nologdone" org-log-done nil)
  3943. ("lognoteclock-out" org-log-note-clock-out t)
  3944. ("nolognoteclock-out" org-log-note-clock-out nil)
  3945. ("logrepeat" org-log-repeat state)
  3946. ("lognoterepeat" org-log-repeat note)
  3947. ("logdrawer" org-log-into-drawer t)
  3948. ("nologdrawer" org-log-into-drawer nil)
  3949. ("logstatesreversed" org-log-states-order-reversed t)
  3950. ("nologstatesreversed" org-log-states-order-reversed nil)
  3951. ("nologrepeat" org-log-repeat nil)
  3952. ("logreschedule" org-log-reschedule time)
  3953. ("lognotereschedule" org-log-reschedule note)
  3954. ("nologreschedule" org-log-reschedule nil)
  3955. ("logredeadline" org-log-redeadline time)
  3956. ("lognoteredeadline" org-log-redeadline note)
  3957. ("nologredeadline" org-log-redeadline nil)
  3958. ("logrefile" org-log-refile time)
  3959. ("lognoterefile" org-log-refile note)
  3960. ("nologrefile" org-log-refile nil)
  3961. ("fninline" org-footnote-define-inline t)
  3962. ("nofninline" org-footnote-define-inline nil)
  3963. ("fnlocal" org-footnote-section nil)
  3964. ("fnauto" org-footnote-auto-label t)
  3965. ("fnprompt" org-footnote-auto-label nil)
  3966. ("fnconfirm" org-footnote-auto-label confirm)
  3967. ("fnplain" org-footnote-auto-label plain)
  3968. ("fnadjust" org-footnote-auto-adjust t)
  3969. ("nofnadjust" org-footnote-auto-adjust nil)
  3970. ("constcgs" constants-unit-system cgs)
  3971. ("constSI" constants-unit-system SI)
  3972. ("noptag" org-tag-persistent-alist nil)
  3973. ("hideblocks" org-hide-block-startup t)
  3974. ("nohideblocks" org-hide-block-startup nil)
  3975. ("beamer" org-startup-with-beamer-mode t)
  3976. ("entitiespretty" org-pretty-entities t)
  3977. ("entitiesplain" org-pretty-entities nil))
  3978. "Variable associated with STARTUP options for org-mode.
  3979. Each element is a list of three items: the startup options (as written
  3980. in the #+STARTUP line), the corresponding variable, and the value to set
  3981. this variable to if the option is found. An optional forth element PUSH
  3982. means to push this value onto the list in the variable.")
  3983. (defun org-update-property-plist (key val props)
  3984. "Update PROPS with KEY and VAL."
  3985. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3986. (key (if appending (substring key 0 (- (length key) 1)) key))
  3987. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3988. (previous (cdr (assoc key props))))
  3989. (if appending
  3990. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3991. (cons (cons key val) remainder))))
  3992. (defconst org-block-regexp
  3993. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3994. "Regular expression for hiding blocks.")
  3995. (defconst org-heading-keyword-regexp-format
  3996. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3997. "Printf format for a regexp matching an headline with some keyword.
  3998. This regexp will match the headline of any node which has the
  3999. exact keyword that is put into the format. The keyword isn't in
  4000. any group by default, but the stars and the body are.")
  4001. (defconst org-heading-keyword-maybe-regexp-format
  4002. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4003. "Printf format for a regexp matching an headline, possibly with some keyword.
  4004. This regexp can match any headline with the specified keyword, or
  4005. without a keyword. The keyword isn't in any group by default,
  4006. but the stars and the body are.")
  4007. (defun org-set-regexps-and-options ()
  4008. "Precompute regular expressions for current buffer."
  4009. (when (derived-mode-p 'org-mode)
  4010. (org-set-local 'org-todo-kwd-alist nil)
  4011. (org-set-local 'org-todo-key-alist nil)
  4012. (org-set-local 'org-todo-key-trigger nil)
  4013. (org-set-local 'org-todo-keywords-1 nil)
  4014. (org-set-local 'org-done-keywords nil)
  4015. (org-set-local 'org-todo-heads nil)
  4016. (org-set-local 'org-todo-sets nil)
  4017. (org-set-local 'org-todo-log-states nil)
  4018. (org-set-local 'org-file-properties nil)
  4019. (org-set-local 'org-file-tags nil)
  4020. (let ((re (org-make-options-regexp
  4021. '("CATEGORY" "TODO" "COLUMNS"
  4022. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4023. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4024. "OPTIONS")
  4025. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4026. (splitre "[ \t]+")
  4027. (scripts org-use-sub-superscripts)
  4028. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4029. tail sep kws1 prio props ftags drawers beamer-p
  4030. ext-setup-or-nil setup-contents (start 0))
  4031. (save-excursion
  4032. (save-restriction
  4033. (widen)
  4034. (goto-char (point-min))
  4035. (while (or (and ext-setup-or-nil
  4036. (string-match re ext-setup-or-nil start)
  4037. (setq start (match-end 0)))
  4038. (and (setq ext-setup-or-nil nil start 0)
  4039. (re-search-forward re nil t)))
  4040. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4041. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4042. (if (stringp value) (setq value (org-trim value)))
  4043. (cond
  4044. ((equal key "CATEGORY")
  4045. (setq cat value))
  4046. ((member key '("SEQ_TODO" "TODO"))
  4047. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4048. ((equal key "TYP_TODO")
  4049. (push (cons 'type (org-split-string value splitre)) kwds))
  4050. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4051. ;; general TODO-like setup
  4052. (push (cons (intern (downcase (match-string 1 key)))
  4053. (org-split-string value splitre)) kwds))
  4054. ((equal key "TAGS")
  4055. (setq tags (append tags (if tags '("\\n") nil)
  4056. (org-split-string value splitre))))
  4057. ((equal key "COLUMNS")
  4058. (org-set-local 'org-columns-default-format value))
  4059. ((equal key "LINK")
  4060. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4061. (push (cons (match-string 1 value)
  4062. (org-trim (match-string 2 value)))
  4063. links)))
  4064. ((equal key "PRIORITIES")
  4065. (setq prio (org-split-string value " +")))
  4066. ((equal key "PROPERTY")
  4067. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4068. (setq props (org-update-property-plist (match-string 1 value)
  4069. (match-string 2 value)
  4070. props))))
  4071. ((equal key "FILETAGS")
  4072. (when (string-match "\\S-" value)
  4073. (setq ftags
  4074. (append
  4075. ftags
  4076. (apply 'append
  4077. (mapcar (lambda (x) (org-split-string x ":"))
  4078. (org-split-string value)))))))
  4079. ((equal key "DRAWERS")
  4080. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4081. ((equal key "CONSTANTS")
  4082. (setq const (append const (org-split-string value splitre))))
  4083. ((equal key "STARTUP")
  4084. (let ((opts (org-split-string value splitre))
  4085. l var val)
  4086. (while (setq l (pop opts))
  4087. (when (setq l (assoc l org-startup-options))
  4088. (setq var (nth 1 l) val (nth 2 l))
  4089. (if (not (nth 3 l))
  4090. (set (make-local-variable var) val)
  4091. (if (not (listp (symbol-value var)))
  4092. (set (make-local-variable var) nil))
  4093. (set (make-local-variable var) (symbol-value var))
  4094. (add-to-list var val))))))
  4095. ((equal key "ARCHIVE")
  4096. (setq arch value)
  4097. (remove-text-properties 0 (length arch)
  4098. '(face t fontified t) arch))
  4099. ((equal key "LATEX_CLASS")
  4100. (setq beamer-p (equal value "beamer")))
  4101. ((equal key "OPTIONS")
  4102. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4103. (setq scripts (read (match-string 2 value)))))
  4104. ((equal key "SETUPFILE")
  4105. (setq setup-contents (org-file-contents
  4106. (expand-file-name
  4107. (org-remove-double-quotes value))
  4108. 'noerror))
  4109. (if (not ext-setup-or-nil)
  4110. (setq ext-setup-or-nil setup-contents start 0)
  4111. (setq ext-setup-or-nil
  4112. (concat (substring ext-setup-or-nil 0 start)
  4113. "\n" setup-contents "\n"
  4114. (substring ext-setup-or-nil start)))))))
  4115. ;; search for property blocks
  4116. (goto-char (point-min))
  4117. (while (re-search-forward org-block-regexp nil t)
  4118. (when (equal "PROPERTY" (upcase (match-string 1)))
  4119. (setq value (replace-regexp-in-string
  4120. "[\n\r]" " " (match-string 4)))
  4121. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4122. (setq props (org-update-property-plist (match-string 1 value)
  4123. (match-string 2 value)
  4124. props)))))))
  4125. (org-set-local 'org-use-sub-superscripts scripts)
  4126. (when cat
  4127. (org-set-local 'org-category (intern cat))
  4128. (push (cons "CATEGORY" cat) props))
  4129. (when prio
  4130. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4131. (setq prio (mapcar 'string-to-char prio))
  4132. (org-set-local 'org-highest-priority (nth 0 prio))
  4133. (org-set-local 'org-lowest-priority (nth 1 prio))
  4134. (org-set-local 'org-default-priority (nth 2 prio)))
  4135. (and props (org-set-local 'org-file-properties (nreverse props)))
  4136. (and ftags (org-set-local 'org-file-tags
  4137. (mapcar 'org-add-prop-inherited ftags)))
  4138. (and drawers (org-set-local 'org-drawers drawers))
  4139. (and arch (org-set-local 'org-archive-location arch))
  4140. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4141. ;; Process the TODO keywords
  4142. (unless kwds
  4143. ;; Use the global values as if they had been given locally.
  4144. (setq kwds (default-value 'org-todo-keywords))
  4145. (if (stringp (car kwds))
  4146. (setq kwds (list (cons org-todo-interpretation
  4147. (default-value 'org-todo-keywords)))))
  4148. (setq kwds (reverse kwds)))
  4149. (setq kwds (nreverse kwds))
  4150. (let (inter kws kw)
  4151. (while (setq kws (pop kwds))
  4152. (let ((kws (or
  4153. (run-hook-with-args-until-success
  4154. 'org-todo-setup-filter-hook kws)
  4155. kws)))
  4156. (setq inter (pop kws) sep (member "|" kws)
  4157. kws0 (delete "|" (copy-sequence kws))
  4158. kwsa nil
  4159. kws1 (mapcar
  4160. (lambda (x)
  4161. ;; 1 2
  4162. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4163. (progn
  4164. (setq kw (match-string 1 x)
  4165. key (and (match-end 2) (match-string 2 x))
  4166. log (org-extract-log-state-settings x))
  4167. (push (cons kw (and key (string-to-char key))) kwsa)
  4168. (and log (push log org-todo-log-states))
  4169. kw)
  4170. (error "Invalid TODO keyword %s" x)))
  4171. kws0)
  4172. kwsa (if kwsa (append '((:startgroup))
  4173. (nreverse kwsa)
  4174. '((:endgroup))))
  4175. hw (car kws1)
  4176. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4177. tail (list inter hw (car dws) (org-last dws))))
  4178. (add-to-list 'org-todo-heads hw 'append)
  4179. (push kws1 org-todo-sets)
  4180. (setq org-done-keywords (append org-done-keywords dws nil))
  4181. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4182. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4183. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4184. (setq org-todo-sets (nreverse org-todo-sets)
  4185. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4186. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4187. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4188. ;; Process the constants
  4189. (when const
  4190. (let (e cst)
  4191. (while (setq e (pop const))
  4192. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4193. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4194. (setq org-table-formula-constants-local cst)))
  4195. ;; Process the tags.
  4196. (when tags
  4197. (let (e tgs)
  4198. (while (setq e (pop tags))
  4199. (cond
  4200. ((equal e "{") (push '(:startgroup) tgs))
  4201. ((equal e "}") (push '(:endgroup) tgs))
  4202. ((equal e "\\n") (push '(:newline) tgs))
  4203. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4204. (push (cons (match-string 1 e)
  4205. (string-to-char (match-string 2 e)))
  4206. tgs))
  4207. (t (push (list e) tgs))))
  4208. (org-set-local 'org-tag-alist nil)
  4209. (while (setq e (pop tgs))
  4210. (or (and (stringp (car e))
  4211. (assoc (car e) org-tag-alist))
  4212. (push e org-tag-alist)))))
  4213. ;; Compute the regular expressions and other local variables.
  4214. ;; Using `org-outline-regexp-bol' would complicate them much,
  4215. ;; because of the fixed white space at the end of that string.
  4216. (if (not org-done-keywords)
  4217. (setq org-done-keywords (and org-todo-keywords-1
  4218. (list (org-last org-todo-keywords-1)))))
  4219. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4220. (length org-scheduled-string)
  4221. (length org-clock-string)
  4222. (length org-closed-string)))
  4223. org-drawer-regexp
  4224. (concat "^[ \t]*:\\("
  4225. (mapconcat 'regexp-quote org-drawers "\\|")
  4226. "\\):[ \t]*$")
  4227. org-not-done-keywords
  4228. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4229. org-todo-regexp
  4230. (concat "\\("
  4231. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4232. "\\)")
  4233. org-not-done-regexp
  4234. (concat "\\("
  4235. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4236. "\\)")
  4237. org-not-done-heading-regexp
  4238. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4239. org-todo-line-regexp
  4240. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4241. org-complex-heading-regexp
  4242. (concat "^\\(\\*+\\)"
  4243. "\\(?: +" org-todo-regexp "\\)?"
  4244. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4245. "\\(?: +\\(.*?\\)\\)??"
  4246. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4247. "[ \t]*$")
  4248. org-complex-heading-regexp-format
  4249. (concat "^\\(\\*+\\)"
  4250. "\\(?: +" org-todo-regexp "\\)?"
  4251. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4252. "\\(?: +"
  4253. ;; Stats cookies can be stuck to body.
  4254. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4255. "\\(%s\\)"
  4256. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4257. "\\)"
  4258. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4259. "[ \t]*$")
  4260. org-todo-line-tags-regexp
  4261. (concat "^\\(\\*+\\)"
  4262. "\\(?: +" org-todo-regexp "\\)?"
  4263. "\\(?: +\\(.*?\\)\\)??"
  4264. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4265. "[ \t]*$")
  4266. org-deadline-regexp (concat "\\<" org-deadline-string)
  4267. org-deadline-time-regexp
  4268. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4269. org-deadline-line-regexp
  4270. (concat "\\<\\(" org-deadline-string "\\).*")
  4271. org-scheduled-regexp
  4272. (concat "\\<" org-scheduled-string)
  4273. org-scheduled-time-regexp
  4274. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4275. org-closed-time-regexp
  4276. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4277. org-keyword-time-regexp
  4278. (concat "\\<\\(" org-scheduled-string
  4279. "\\|" org-deadline-string
  4280. "\\|" org-closed-string
  4281. "\\|" org-clock-string "\\)"
  4282. " *[[<]\\([^]>]+\\)[]>]")
  4283. org-keyword-time-not-clock-regexp
  4284. (concat "\\<\\(" org-scheduled-string
  4285. "\\|" org-deadline-string
  4286. "\\|" org-closed-string
  4287. "\\)"
  4288. " *[[<]\\([^]>]+\\)[]>]")
  4289. org-maybe-keyword-time-regexp
  4290. (concat "\\(\\<\\(" org-scheduled-string
  4291. "\\|" org-deadline-string
  4292. "\\|" org-closed-string
  4293. "\\|" org-clock-string "\\)\\)?"
  4294. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4295. org-all-time-keywords
  4296. (mapcar (lambda (w) (substring w 0 -1))
  4297. (list org-scheduled-string org-deadline-string
  4298. org-clock-string org-closed-string))
  4299. )
  4300. (org-compute-latex-and-specials-regexp)
  4301. (org-set-font-lock-defaults))))
  4302. (defun org-file-contents (file &optional noerror)
  4303. "Return the contents of FILE, as a string."
  4304. (if (or (not file)
  4305. (not (file-readable-p file)))
  4306. (if noerror
  4307. (progn
  4308. (message "Cannot read file \"%s\"" file)
  4309. (ding) (sit-for 2)
  4310. "")
  4311. (error "Cannot read file \"%s\"" file))
  4312. (with-temp-buffer
  4313. (insert-file-contents file)
  4314. (buffer-string))))
  4315. (defun org-extract-log-state-settings (x)
  4316. "Extract the log state setting from a TODO keyword string.
  4317. This will extract info from a string like \"WAIT(w@/!)\"."
  4318. (let (kw key log1 log2)
  4319. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4320. (setq kw (match-string 1 x)
  4321. key (and (match-end 2) (match-string 2 x))
  4322. log1 (and (match-end 3) (match-string 3 x))
  4323. log2 (and (match-end 4) (match-string 4 x)))
  4324. (and (or log1 log2)
  4325. (list kw
  4326. (and log1 (if (equal log1 "!") 'time 'note))
  4327. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4328. (defun org-remove-keyword-keys (list)
  4329. "Remove a pair of parenthesis at the end of each string in LIST."
  4330. (mapcar (lambda (x)
  4331. (if (string-match "(.*)$" x)
  4332. (substring x 0 (match-beginning 0))
  4333. x))
  4334. list))
  4335. (defun org-assign-fast-keys (alist)
  4336. "Assign fast keys to a keyword-key alist.
  4337. Respect keys that are already there."
  4338. (let (new e (alt ?0))
  4339. (while (setq e (pop alist))
  4340. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4341. (cdr e)) ;; Key already assigned.
  4342. (push e new)
  4343. (let ((clist (string-to-list (downcase (car e))))
  4344. (used (append new alist)))
  4345. (when (= (car clist) ?@)
  4346. (pop clist))
  4347. (while (and clist (rassoc (car clist) used))
  4348. (pop clist))
  4349. (unless clist
  4350. (while (rassoc alt used)
  4351. (incf alt)))
  4352. (push (cons (car e) (or (car clist) alt)) new))))
  4353. (nreverse new)))
  4354. ;;; Some variables used in various places
  4355. (defvar org-window-configuration nil
  4356. "Used in various places to store a window configuration.")
  4357. (defvar org-selected-window nil
  4358. "Used in various places to store a window configuration.")
  4359. (defvar org-finish-function nil
  4360. "Function to be called when `C-c C-c' is used.
  4361. This is for getting out of special buffers like capture.")
  4362. ;; FIXME: Occasionally check by commenting these, to make sure
  4363. ;; no other functions uses these, forgetting to let-bind them.
  4364. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4365. (defvar org-last-state)
  4366. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4367. ;; Defined somewhere in this file, but used before definition.
  4368. (defvar org-entities) ;; defined in org-entities.el
  4369. (defvar org-struct-menu)
  4370. (defvar org-org-menu)
  4371. (defvar org-tbl-menu)
  4372. ;;;; Define the Org-mode
  4373. ;; We use a before-change function to check if a table might need
  4374. ;; an update.
  4375. (defvar org-table-may-need-update t
  4376. "Indicates that a table might need an update.
  4377. This variable is set by `org-before-change-function'.
  4378. `org-table-align' sets it back to nil.")
  4379. (defun org-before-change-function (beg end)
  4380. "Every change indicates that a table might need an update."
  4381. (setq org-table-may-need-update t))
  4382. (defvar org-mode-map)
  4383. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4384. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4385. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4386. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4387. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4388. (defvar org-table-buffer-is-an nil)
  4389. (defvar bidi-paragraph-direction)
  4390. (defvar buffer-face-mode-face)
  4391. (require 'outline)
  4392. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4393. (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"))
  4394. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4395. ;; Other stuff we need.
  4396. (require 'time-date)
  4397. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4398. (require 'easymenu)
  4399. (require 'overlay)
  4400. ;; (require 'org-macs) moved higher up in the file before it is first used
  4401. (require 'org-entities)
  4402. ;; (require 'org-compat) moved higher up in the file before it is first used
  4403. (require 'org-faces)
  4404. (require 'org-list)
  4405. (require 'org-pcomplete)
  4406. (require 'org-src)
  4407. (require 'org-footnote)
  4408. ;; babel
  4409. (require 'ob)
  4410. ;;;###autoload
  4411. (define-derived-mode org-mode outline-mode "Org"
  4412. "Outline-based notes management and organizer, alias
  4413. \"Carsten's outline-mode for keeping track of everything.\"
  4414. Org-mode develops organizational tasks around a NOTES file which
  4415. contains information about projects as plain text. Org-mode is
  4416. implemented on top of outline-mode, which is ideal to keep the content
  4417. of large files well structured. It supports ToDo items, deadlines and
  4418. time stamps, which magically appear in the diary listing of the Emacs
  4419. calendar. Tables are easily created with a built-in table editor.
  4420. Plain text URL-like links connect to websites, emails (VM), Usenet
  4421. messages (Gnus), BBDB entries, and any files related to the project.
  4422. For printing and sharing of notes, an Org-mode file (or a part of it)
  4423. can be exported as a structured ASCII or HTML file.
  4424. The following commands are available:
  4425. \\{org-mode-map}"
  4426. ;; Get rid of Outline menus, they are not needed
  4427. ;; Need to do this here because define-derived-mode sets up
  4428. ;; the keymap so late. Still, it is a waste to call this each time
  4429. ;; we switch another buffer into org-mode.
  4430. (if (featurep 'xemacs)
  4431. (when (boundp 'outline-mode-menu-heading)
  4432. ;; Assume this is Greg's port, it uses easymenu
  4433. (easy-menu-remove outline-mode-menu-heading)
  4434. (easy-menu-remove outline-mode-menu-show)
  4435. (easy-menu-remove outline-mode-menu-hide))
  4436. (define-key org-mode-map [menu-bar headings] 'undefined)
  4437. (define-key org-mode-map [menu-bar hide] 'undefined)
  4438. (define-key org-mode-map [menu-bar show] 'undefined))
  4439. (org-load-modules-maybe)
  4440. (easy-menu-add org-org-menu)
  4441. (easy-menu-add org-tbl-menu)
  4442. (org-install-agenda-files-menu)
  4443. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4444. (add-to-invisibility-spec '(org-cwidth))
  4445. (add-to-invisibility-spec '(org-hide-block . t))
  4446. (when (featurep 'xemacs)
  4447. (org-set-local 'line-move-ignore-invisible t))
  4448. (org-set-local 'outline-regexp org-outline-regexp)
  4449. (org-set-local 'outline-level 'org-outline-level)
  4450. (setq bidi-paragraph-direction 'left-to-right)
  4451. (when (and org-ellipsis
  4452. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4453. (fboundp 'make-glyph-code))
  4454. (unless org-display-table
  4455. (setq org-display-table (make-display-table)))
  4456. (set-display-table-slot
  4457. org-display-table 4
  4458. (vconcat (mapcar
  4459. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4460. org-ellipsis)))
  4461. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4462. (setq buffer-display-table org-display-table))
  4463. (org-set-regexps-and-options)
  4464. (when (and org-tag-faces (not org-tags-special-faces-re))
  4465. ;; tag faces set outside customize.... force initialization.
  4466. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4467. ;; Calc embedded
  4468. (org-set-local 'calc-embedded-open-mode "# ")
  4469. (modify-syntax-entry ?@ "w")
  4470. (modify-syntax-entry ?\" "\"")
  4471. (if org-startup-truncated (setq truncate-lines t))
  4472. (org-set-local 'font-lock-unfontify-region-function
  4473. 'org-unfontify-region)
  4474. ;; Activate before-change-function
  4475. (org-set-local 'org-table-may-need-update t)
  4476. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4477. 'local)
  4478. ;; Check for running clock before killing a buffer
  4479. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4480. ;; Initialize macros templates.
  4481. (org-macro-initialize-templates)
  4482. ;; Initialize radio targets.
  4483. (org-update-radio-target-regexp)
  4484. ;; Indentation.
  4485. (org-set-local 'indent-line-function 'org-indent-line)
  4486. (org-set-local 'indent-region-function 'org-indent-region)
  4487. ;; Filling and auto-filling.
  4488. (org-setup-filling)
  4489. ;; Comments.
  4490. (org-setup-comments-handling)
  4491. ;; Beginning/end of defun
  4492. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4493. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4494. ;; Next error for sparse trees
  4495. (org-set-local 'next-error-function 'org-occur-next-match)
  4496. ;; Make sure dependence stuff works reliably, even for users who set it
  4497. ;; too late :-(
  4498. (if org-enforce-todo-dependencies
  4499. (add-hook 'org-blocker-hook
  4500. 'org-block-todo-from-children-or-siblings-or-parent)
  4501. (remove-hook 'org-blocker-hook
  4502. 'org-block-todo-from-children-or-siblings-or-parent))
  4503. (if org-enforce-todo-checkbox-dependencies
  4504. (add-hook 'org-blocker-hook
  4505. 'org-block-todo-from-checkboxes)
  4506. (remove-hook 'org-blocker-hook
  4507. 'org-block-todo-from-checkboxes))
  4508. ;; Align options lines
  4509. (org-set-local
  4510. 'align-mode-rules-list
  4511. '((org-in-buffer-settings
  4512. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4513. (modes . '(org-mode)))))
  4514. ;; Imenu
  4515. (org-set-local 'imenu-create-index-function
  4516. 'org-imenu-get-tree)
  4517. ;; Make isearch reveal context
  4518. (if (or (featurep 'xemacs)
  4519. (not (boundp 'outline-isearch-open-invisible-function)))
  4520. ;; Emacs 21 and XEmacs make use of the hook
  4521. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4522. ;; Emacs 22 deals with this through a special variable
  4523. (org-set-local 'outline-isearch-open-invisible-function
  4524. (lambda (&rest ignore) (org-show-context 'isearch))))
  4525. ;; Turn on org-beamer-mode?
  4526. (and org-startup-with-beamer-mode (org-beamer-mode))
  4527. ;; Setup the pcomplete hooks
  4528. (set (make-local-variable 'pcomplete-command-completion-function)
  4529. 'org-pcomplete-initial)
  4530. (set (make-local-variable 'pcomplete-command-name-function)
  4531. 'org-command-at-point)
  4532. (set (make-local-variable 'pcomplete-default-completion-function)
  4533. 'ignore)
  4534. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4535. 'org-parse-arguments)
  4536. (set (make-local-variable 'pcomplete-termination-string) "")
  4537. (when (>= emacs-major-version 23)
  4538. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4539. ;; If empty file that did not turn on org-mode automatically, make it to.
  4540. (if (and org-insert-mode-line-in-empty-file
  4541. (org-called-interactively-p 'any)
  4542. (= (point-min) (point-max)))
  4543. (insert "# -*- mode: org -*-\n\n"))
  4544. (unless org-inhibit-startup
  4545. (when org-startup-align-all-tables
  4546. (let ((bmp (buffer-modified-p)))
  4547. (org-table-map-tables 'org-table-align 'quietly)
  4548. (set-buffer-modified-p bmp)))
  4549. (when org-startup-with-inline-images
  4550. (org-display-inline-images))
  4551. (when org-startup-indented
  4552. (require 'org-indent)
  4553. (org-indent-mode 1))
  4554. (unless org-inhibit-startup-visibility-stuff
  4555. (org-set-startup-visibility)))
  4556. ;; Try to set org-hide correctly
  4557. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4558. (when (fboundp 'abbrev-table-put)
  4559. (abbrev-table-put org-mode-abbrev-table
  4560. :parents (list text-mode-abbrev-table)))
  4561. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4562. (defun org-find-invisible-foreground ()
  4563. (let ((candidates (remove
  4564. "unspecified-bg"
  4565. (nconc
  4566. (list (face-background 'default)
  4567. (face-background 'org-default))
  4568. (mapcar
  4569. (lambda (alist)
  4570. (when (boundp alist)
  4571. (cdr (assoc 'background-color (symbol-value alist)))))
  4572. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4573. (list (face-foreground 'org-hide))))))
  4574. (car (remove nil candidates))))
  4575. (defun org-current-time (&optional rounding-minutes)
  4576. "Current time, possibly rounded to ROUNDING-MINUTES.
  4577. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4578. `org-time-stamp-rounding-minutes'."
  4579. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4580. (car org-time-stamp-rounding-minutes)))
  4581. (time (decode-time)))
  4582. (if (> r 1)
  4583. (apply 'encode-time
  4584. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4585. (nthcdr 2 time)))
  4586. (current-time))))
  4587. (defun org-today ()
  4588. "Return today date, considering `org-extend-today-until'."
  4589. (time-to-days
  4590. (time-subtract (current-time)
  4591. (list 0 (* 3600 org-extend-today-until) 0))))
  4592. ;;;; Font-Lock stuff, including the activators
  4593. (defvar org-mouse-map (make-sparse-keymap))
  4594. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4595. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4596. (when org-mouse-1-follows-link
  4597. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4598. (when org-tab-follows-link
  4599. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4600. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4601. (require 'font-lock)
  4602. (defconst org-non-link-chars "]\t\n\r<>")
  4603. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4604. "shell" "elisp" "doi" "message"))
  4605. (defvar org-link-types-re nil
  4606. "Matches a link that has a url-like prefix like \"http:\"")
  4607. (defvar org-link-re-with-space nil
  4608. "Matches a link with spaces, optional angular brackets around it.")
  4609. (defvar org-link-re-with-space2 nil
  4610. "Matches a link with spaces, optional angular brackets around it.")
  4611. (defvar org-link-re-with-space3 nil
  4612. "Matches a link with spaces, only for internal part in bracket links.")
  4613. (defvar org-angle-link-re nil
  4614. "Matches link with angular brackets, spaces are allowed.")
  4615. (defvar org-plain-link-re nil
  4616. "Matches plain link, without spaces.")
  4617. (defvar org-bracket-link-regexp nil
  4618. "Matches a link in double brackets.")
  4619. (defvar org-bracket-link-analytic-regexp nil
  4620. "Regular expression used to analyze links.
  4621. Here is what the match groups contain after a match:
  4622. 1: http:
  4623. 2: http
  4624. 3: path
  4625. 4: [desc]
  4626. 5: desc")
  4627. (defvar org-bracket-link-analytic-regexp++ nil
  4628. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4629. (defvar org-any-link-re nil
  4630. "Regular expression matching any link.")
  4631. (defcustom org-match-sexp-depth 3
  4632. "Number of stacked braces for sub/superscript matching.
  4633. This has to be set before loading org.el to be effective."
  4634. :group 'org-export-translation ; ??????????????????????????/
  4635. :type 'integer)
  4636. (defun org-create-multibrace-regexp (left right n)
  4637. "Create a regular expression which will match a balanced sexp.
  4638. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4639. as single character strings.
  4640. The regexp returned will match the entire expression including the
  4641. delimiters. It will also define a single group which contains the
  4642. match except for the outermost delimiters. The maximum depth of
  4643. stacked delimiters is N. Escaping delimiters is not possible."
  4644. (let* ((nothing (concat "[^" left right "]*?"))
  4645. (or "\\|")
  4646. (re nothing)
  4647. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4648. (while (> n 1)
  4649. (setq n (1- n)
  4650. re (concat re or next)
  4651. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4652. (concat left "\\(" re "\\)" right)))
  4653. (defvar org-match-substring-regexp
  4654. (concat
  4655. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4656. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4657. "\\|"
  4658. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4659. "\\|"
  4660. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4661. "The regular expression matching a sub- or superscript.")
  4662. (defvar org-match-substring-with-braces-regexp
  4663. (concat
  4664. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4665. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4666. "\\)")
  4667. "The regular expression matching a sub- or superscript, forcing braces.")
  4668. (defun org-make-link-regexps ()
  4669. "Update the link regular expressions.
  4670. This should be called after the variable `org-link-types' has changed."
  4671. (setq org-link-types-re
  4672. (concat
  4673. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4674. org-link-re-with-space
  4675. (concat
  4676. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4677. "\\([^" org-non-link-chars " ]"
  4678. "[^" org-non-link-chars "]*"
  4679. "[^" org-non-link-chars " ]\\)>?")
  4680. org-link-re-with-space2
  4681. (concat
  4682. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4683. "\\([^" org-non-link-chars " ]"
  4684. "[^\t\n\r]*"
  4685. "[^" org-non-link-chars " ]\\)>?")
  4686. org-link-re-with-space3
  4687. (concat
  4688. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4689. "\\([^" org-non-link-chars " ]"
  4690. "[^\t\n\r]*\\)")
  4691. org-angle-link-re
  4692. (concat
  4693. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4694. "\\([^" org-non-link-chars " ]"
  4695. "[^" org-non-link-chars "]*"
  4696. "\\)>")
  4697. org-plain-link-re
  4698. (concat
  4699. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4700. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4701. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4702. org-bracket-link-regexp
  4703. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4704. org-bracket-link-analytic-regexp
  4705. (concat
  4706. "\\[\\["
  4707. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4708. "\\([^]]+\\)"
  4709. "\\]"
  4710. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4711. "\\]")
  4712. org-bracket-link-analytic-regexp++
  4713. (concat
  4714. "\\[\\["
  4715. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4716. "\\([^]]+\\)"
  4717. "\\]"
  4718. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4719. "\\]")
  4720. org-any-link-re
  4721. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4722. org-angle-link-re "\\)\\|\\("
  4723. org-plain-link-re "\\)")))
  4724. (org-make-link-regexps)
  4725. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4726. "Regular expression for fast time stamp matching.")
  4727. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4728. "Regular expression for fast time stamp matching.")
  4729. (defconst org-ts-regexp0
  4730. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4731. "Regular expression matching time strings for analysis.
  4732. This one does not require the space after the date, so it can be used
  4733. on a string that terminates immediately after the date.")
  4734. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4735. "Regular expression matching time strings for analysis.")
  4736. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4737. "Regular expression matching time stamps, with groups.")
  4738. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4739. "Regular expression matching time stamps (also [..]), with groups.")
  4740. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4741. "Regular expression matching a time stamp range.")
  4742. (defconst org-tr-regexp-both
  4743. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4744. "Regular expression matching a time stamp range.")
  4745. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4746. org-ts-regexp "\\)?")
  4747. "Regular expression matching a time stamp or time stamp range.")
  4748. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4749. org-ts-regexp-both "\\)?")
  4750. "Regular expression matching a time stamp or time stamp range.
  4751. The time stamps may be either active or inactive.")
  4752. (defvar org-emph-face nil)
  4753. (defun org-do-emphasis-faces (limit)
  4754. "Run through the buffer and add overlays to emphasized strings."
  4755. (let (rtn a)
  4756. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4757. (if (not (= (char-after (match-beginning 3))
  4758. (char-after (match-beginning 4))))
  4759. (progn
  4760. (setq rtn t)
  4761. (setq a (assoc (match-string 3) org-emphasis-alist))
  4762. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4763. 'face
  4764. (nth 1 a))
  4765. (and (nth 4 a)
  4766. (org-remove-flyspell-overlays-in
  4767. (match-beginning 0) (match-end 0)))
  4768. (add-text-properties (match-beginning 2) (match-end 2)
  4769. '(font-lock-multiline t org-emphasis t))
  4770. (when org-hide-emphasis-markers
  4771. (add-text-properties (match-end 4) (match-beginning 5)
  4772. '(invisible org-link))
  4773. (add-text-properties (match-beginning 3) (match-end 3)
  4774. '(invisible org-link)))))
  4775. (backward-char 1))
  4776. rtn))
  4777. (defun org-emphasize (&optional char)
  4778. "Insert or change an emphasis, i.e. a font like bold or italic.
  4779. If there is an active region, change that region to a new emphasis.
  4780. If there is no region, just insert the marker characters and position
  4781. the cursor between them.
  4782. CHAR should be either the marker character, or the first character of the
  4783. HTML tag associated with that emphasis. If CHAR is a space, the means
  4784. to remove the emphasis of the selected region.
  4785. If char is not given (for example in an interactive call) it
  4786. will be prompted for."
  4787. (interactive)
  4788. (let ((eal org-emphasis-alist) e det
  4789. (erc org-emphasis-regexp-components)
  4790. (prompt "")
  4791. (string "") beg end move tag c s)
  4792. (if (org-region-active-p)
  4793. (setq beg (region-beginning) end (region-end)
  4794. string (buffer-substring beg end))
  4795. (setq move t))
  4796. (while (setq e (pop eal))
  4797. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4798. c (aref tag 0))
  4799. (push (cons c (string-to-char (car e))) det)
  4800. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4801. (substring tag 1)))))
  4802. (setq det (nreverse det))
  4803. (unless char
  4804. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4805. (setq char (read-char-exclusive)))
  4806. (setq char (or (cdr (assoc char det)) char))
  4807. (if (equal char ?\ )
  4808. (setq s "" move nil)
  4809. (unless (assoc (char-to-string char) org-emphasis-alist)
  4810. (error "No such emphasis marker: \"%c\"" char))
  4811. (setq s (char-to-string char)))
  4812. (while (and (> (length string) 1)
  4813. (equal (substring string 0 1) (substring string -1))
  4814. (assoc (substring string 0 1) org-emphasis-alist))
  4815. (setq string (substring string 1 -1)))
  4816. (setq string (concat s string s))
  4817. (if beg (delete-region beg end))
  4818. (unless (or (bolp)
  4819. (string-match (concat "[" (nth 0 erc) "\n]")
  4820. (char-to-string (char-before (point)))))
  4821. (insert " "))
  4822. (unless (or (eobp)
  4823. (string-match (concat "[" (nth 1 erc) "\n]")
  4824. (char-to-string (char-after (point)))))
  4825. (insert " ") (backward-char 1))
  4826. (insert string)
  4827. (and move (backward-char 1))))
  4828. (defconst org-nonsticky-props
  4829. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4830. (defsubst org-rear-nonsticky-at (pos)
  4831. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4832. (defun org-activate-plain-links (limit)
  4833. "Run through the buffer and add overlays to links."
  4834. (catch 'exit
  4835. (let (f hl)
  4836. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4837. (not (org-in-src-block-p)))
  4838. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4839. (setq f (get-text-property (match-beginning 0) 'face))
  4840. (setq hl (org-match-string-no-properties 0))
  4841. (if (or (eq f 'org-tag)
  4842. (and (listp f) (memq 'org-tag f)))
  4843. nil
  4844. (add-text-properties (match-beginning 0) (match-end 0)
  4845. (list 'mouse-face 'highlight
  4846. 'face 'org-link
  4847. 'htmlize-link `(:uri ,hl)
  4848. 'keymap org-mouse-map))
  4849. (org-rear-nonsticky-at (match-end 0)))
  4850. t))))
  4851. (defun org-activate-code (limit)
  4852. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4853. (progn
  4854. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4855. (remove-text-properties (match-beginning 0) (match-end 0)
  4856. '(display t invisible t intangible t))
  4857. t)))
  4858. (defcustom org-src-fontify-natively nil
  4859. "When non-nil, fontify code in code blocks."
  4860. :type 'boolean
  4861. :version "24.1"
  4862. :group 'org-appearance
  4863. :group 'org-babel)
  4864. (defcustom org-allow-promoting-top-level-subtree nil
  4865. "When non-nil, allow promoting a top level subtree.
  4866. The leading star of the top level headline will be replaced
  4867. by a #."
  4868. :type 'boolean
  4869. :version "24.1"
  4870. :group 'org-appearance)
  4871. (defun org-fontify-meta-lines-and-blocks (limit)
  4872. (condition-case nil
  4873. (org-fontify-meta-lines-and-blocks-1 limit)
  4874. (error (message "org-mode fontification error"))))
  4875. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4876. "Fontify #+ lines and blocks, in the correct ways."
  4877. (let ((case-fold-search t))
  4878. (if (re-search-forward
  4879. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4880. limit t)
  4881. (let ((beg (match-beginning 0))
  4882. (block-start (match-end 0))
  4883. (block-end nil)
  4884. (lang (match-string 7))
  4885. (beg1 (line-beginning-position 2))
  4886. (dc1 (downcase (match-string 2)))
  4887. (dc3 (downcase (match-string 3)))
  4888. end end1 quoting block-type ovl)
  4889. (cond
  4890. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4891. ;; a single line of backend-specific content
  4892. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4893. (remove-text-properties (match-beginning 0) (match-end 0)
  4894. '(display t invisible t intangible t))
  4895. (add-text-properties (match-beginning 1) (match-end 3)
  4896. '(font-lock-fontified t face org-meta-line))
  4897. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4898. '(font-lock-fontified t face org-block))
  4899. ; for backend-specific code
  4900. t)
  4901. ((and (match-end 4) (equal dc3 "+begin"))
  4902. ;; Truly a block
  4903. (setq block-type (downcase (match-string 5))
  4904. quoting (member block-type org-protecting-blocks))
  4905. (when (re-search-forward
  4906. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4907. nil t) ;; on purpose, we look further than LIMIT
  4908. (setq end (min (point-max) (match-end 0))
  4909. end1 (min (point-max) (1- (match-beginning 0))))
  4910. (setq block-end (match-beginning 0))
  4911. (when quoting
  4912. (remove-text-properties beg end
  4913. '(display t invisible t intangible t)))
  4914. (add-text-properties
  4915. beg end
  4916. '(font-lock-fontified t font-lock-multiline t))
  4917. (add-text-properties beg beg1 '(face org-meta-line))
  4918. (add-text-properties end1 (min (point-max) (1+ end))
  4919. '(face org-meta-line)) ; for end_src
  4920. (cond
  4921. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4922. (org-src-font-lock-fontify-block lang block-start block-end)
  4923. ;; remove old background overlays
  4924. (mapc (lambda (ov)
  4925. (if (eq (overlay-get ov 'face) 'org-block-background)
  4926. (delete-overlay ov)))
  4927. (overlays-at (/ (+ beg1 block-end) 2)))
  4928. ;; add a background overlay
  4929. (setq ovl (make-overlay beg1 block-end))
  4930. (overlay-put ovl 'face 'org-block-background)
  4931. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4932. (quoting
  4933. (add-text-properties beg1 (min (point-max) (1+ end1))
  4934. '(face org-block))) ; end of source block
  4935. ((not org-fontify-quote-and-verse-blocks))
  4936. ((string= block-type "quote")
  4937. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4938. ((string= block-type "verse")
  4939. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4940. (add-text-properties beg beg1 '(face org-block-begin-line))
  4941. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4942. '(face org-block-end-line))
  4943. t))
  4944. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4945. (add-text-properties
  4946. beg (match-end 3)
  4947. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4948. '(font-lock-fontified t invisible t)
  4949. '(font-lock-fontified t face org-document-info-keyword)))
  4950. (add-text-properties
  4951. (match-beginning 6) (1+ (match-end 6))
  4952. (if (string-equal dc1 "+title:")
  4953. '(font-lock-fontified t face org-document-title)
  4954. '(font-lock-fontified t face org-document-info))))
  4955. ((or (equal dc1 "+results")
  4956. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4957. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4958. "+call:" "+header:" "+headers:" "+name:"))
  4959. (and (match-end 4) (equal dc3 "+attr")))
  4960. (add-text-properties
  4961. beg (match-end 0)
  4962. '(font-lock-fontified t face org-meta-line))
  4963. t)
  4964. ((member dc3 '(" " ""))
  4965. (add-text-properties
  4966. beg (match-end 0)
  4967. '(font-lock-fontified t face font-lock-comment-face)))
  4968. ((not (member (char-after beg) '(?\ ?\t)))
  4969. ;; just any other in-buffer setting, but not indented
  4970. (add-text-properties
  4971. beg (match-end 0)
  4972. '(font-lock-fontified t face org-meta-line))
  4973. t)
  4974. (t nil))))))
  4975. (defun org-activate-angle-links (limit)
  4976. "Run through the buffer and add overlays to links."
  4977. (if (and (re-search-forward org-angle-link-re limit t)
  4978. (not (org-in-src-block-p)))
  4979. (progn
  4980. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4981. (add-text-properties (match-beginning 0) (match-end 0)
  4982. (list 'mouse-face 'highlight
  4983. 'keymap org-mouse-map))
  4984. (org-rear-nonsticky-at (match-end 0))
  4985. t)))
  4986. (defun org-activate-footnote-links (limit)
  4987. "Run through the buffer and add overlays to footnotes."
  4988. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4989. (when fn
  4990. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4991. (org-remove-flyspell-overlays-in beg end)
  4992. (add-text-properties beg end
  4993. (list 'mouse-face 'highlight
  4994. 'keymap org-mouse-map
  4995. 'help-echo
  4996. (if (= (point-at-bol) beg)
  4997. "Footnote definition"
  4998. "Footnote reference")
  4999. 'font-lock-fontified t
  5000. 'font-lock-multiline t
  5001. 'face 'org-footnote))))))
  5002. (defun org-activate-bracket-links (limit)
  5003. "Run through the buffer and add overlays to bracketed links."
  5004. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5005. (not (org-in-src-block-p)))
  5006. (let* ((hl (org-match-string-no-properties 1))
  5007. (help (concat "LINK: " hl))
  5008. ;; FIXME: Above we should remove the escapes. But that
  5009. ;; requires another match, protecting match data, a lot
  5010. ;; of overhead for font-lock.
  5011. (ip (org-maybe-intangible
  5012. (list 'invisible 'org-link
  5013. 'keymap org-mouse-map 'mouse-face 'highlight
  5014. 'font-lock-multiline t 'help-echo help
  5015. 'htmlize-link `(:uri ,hl))))
  5016. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5017. 'font-lock-multiline t 'help-echo help
  5018. 'htmlize-link `(:uri ,hl))))
  5019. ;; We need to remove the invisible property here. Table narrowing
  5020. ;; may have made some of this invisible.
  5021. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5022. (remove-text-properties (match-beginning 0) (match-end 0)
  5023. '(invisible nil))
  5024. (if (match-end 3)
  5025. (progn
  5026. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5027. (org-rear-nonsticky-at (match-beginning 3))
  5028. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5029. (org-rear-nonsticky-at (match-end 3))
  5030. (add-text-properties (match-end 3) (match-end 0) ip)
  5031. (org-rear-nonsticky-at (match-end 0)))
  5032. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5033. (org-rear-nonsticky-at (match-beginning 1))
  5034. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5035. (org-rear-nonsticky-at (match-end 1))
  5036. (add-text-properties (match-end 1) (match-end 0) ip)
  5037. (org-rear-nonsticky-at (match-end 0)))
  5038. t)))
  5039. (defun org-activate-dates (limit)
  5040. "Run through the buffer and add overlays to dates."
  5041. (if (re-search-forward org-tsr-regexp-both limit t)
  5042. (progn
  5043. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5044. (add-text-properties (match-beginning 0) (match-end 0)
  5045. (list 'mouse-face 'highlight
  5046. 'keymap org-mouse-map))
  5047. (org-rear-nonsticky-at (match-end 0))
  5048. (when org-display-custom-times
  5049. (if (match-end 3)
  5050. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5051. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5052. t)))
  5053. (defvar org-target-link-regexp nil
  5054. "Regular expression matching radio targets in plain text.")
  5055. (make-variable-buffer-local 'org-target-link-regexp)
  5056. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5057. "Regular expression matching a link target.")
  5058. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5059. "Regular expression matching a radio target.")
  5060. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5061. "Regular expression matching any target.")
  5062. (defun org-activate-target-links (limit)
  5063. "Run through the buffer and add overlays to target matches."
  5064. (when org-target-link-regexp
  5065. (let ((case-fold-search t))
  5066. (if (re-search-forward org-target-link-regexp limit t)
  5067. (progn
  5068. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5069. (add-text-properties (match-beginning 0) (match-end 0)
  5070. (list 'mouse-face 'highlight
  5071. 'keymap org-mouse-map
  5072. 'help-echo "Radio target link"
  5073. 'org-linked-text t))
  5074. (org-rear-nonsticky-at (match-end 0))
  5075. t)))))
  5076. (defun org-update-radio-target-regexp ()
  5077. "Find all radio targets in this file and update the regular expression."
  5078. (interactive)
  5079. (when (memq 'radio org-activate-links)
  5080. (setq org-target-link-regexp
  5081. (org-make-target-link-regexp (org-all-targets 'radio)))
  5082. (org-restart-font-lock)))
  5083. (defun org-hide-wide-columns (limit)
  5084. (let (s e)
  5085. (setq s (text-property-any (point) (or limit (point-max))
  5086. 'org-cwidth t))
  5087. (when s
  5088. (setq e (next-single-property-change s 'org-cwidth))
  5089. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5090. (goto-char e)
  5091. t)))
  5092. (defvar org-latex-and-specials-regexp nil
  5093. "Regular expression for highlighting export special stuff.")
  5094. (defvar org-match-substring-regexp)
  5095. (defvar org-match-substring-with-braces-regexp)
  5096. ;; This should be with the exporter code, but we also use if for font-locking
  5097. (defconst org-export-html-special-string-regexps
  5098. '(("\\\\-" . "&shy;")
  5099. ("---\\([^-]\\)" . "&mdash;\\1")
  5100. ("--\\([^-]\\)" . "&ndash;\\1")
  5101. ("\\.\\.\\." . "&hellip;"))
  5102. "Regular expressions for special string conversion.")
  5103. (defun org-compute-latex-and-specials-regexp ()
  5104. "Compute regular expression for stuff treated specially by exporters."
  5105. (if (not org-highlight-latex-fragments-and-specials)
  5106. (org-set-local 'org-latex-and-specials-regexp nil)
  5107. (require 'org-exp)
  5108. (let*
  5109. ((matchers (plist-get org-format-latex-options :matchers))
  5110. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5111. org-latex-regexps)))
  5112. (org-export-allow-BIND nil)
  5113. (options (org-combine-plists (org-default-export-plist)
  5114. (org-infile-export-plist)))
  5115. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5116. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5117. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5118. (org-export-html-expand (plist-get options :expand-quoted-html))
  5119. (org-export-with-special-strings (plist-get options :special-strings))
  5120. (re-sub
  5121. (cond
  5122. ((equal org-export-with-sub-superscripts '{})
  5123. (list org-match-substring-with-braces-regexp))
  5124. (org-export-with-sub-superscripts
  5125. (list org-match-substring-regexp))))
  5126. (re-latex
  5127. (if org-export-with-LaTeX-fragments
  5128. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5129. (re-macros
  5130. (if org-export-with-TeX-macros
  5131. (list (concat "\\\\"
  5132. (regexp-opt
  5133. (append
  5134. (delq nil
  5135. (mapcar 'car-safe
  5136. (append org-entities-user
  5137. org-entities)))
  5138. (if (boundp 'org-latex-entities)
  5139. (mapcar (lambda (x)
  5140. (or (car-safe x) x))
  5141. org-latex-entities)
  5142. nil))
  5143. 'words))) ; FIXME
  5144. ))
  5145. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5146. (re-special (if org-export-with-special-strings
  5147. (mapcar (lambda (x) (car x))
  5148. org-export-html-special-string-regexps)))
  5149. (re-rest
  5150. (delq nil
  5151. (list
  5152. (if org-export-html-expand "@<[^>\n]+>")
  5153. ))))
  5154. (org-set-local
  5155. 'org-latex-and-specials-regexp
  5156. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5157. re-rest) "\\|")))))
  5158. (defun org-do-latex-and-special-faces (limit)
  5159. "Run through the buffer and add overlays to links."
  5160. (when org-latex-and-specials-regexp
  5161. (let (rtn d)
  5162. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5163. limit t))
  5164. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5165. 'face))
  5166. '(org-code org-verbatim underline)))
  5167. (progn
  5168. (setq rtn t
  5169. d (cond ((member (char-after (1+ (match-beginning 0)))
  5170. '(?_ ?^)) 1)
  5171. (t 0)))
  5172. (font-lock-prepend-text-property
  5173. (+ d (match-beginning 0)) (match-end 0)
  5174. 'face 'org-latex-and-export-specials)
  5175. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5176. '(font-lock-multiline t)))))
  5177. rtn)))
  5178. (defun org-restart-font-lock ()
  5179. "Restart `font-lock-mode', to force refontification."
  5180. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5181. (font-lock-mode -1)
  5182. (font-lock-mode 1)))
  5183. (defun org-all-targets (&optional radio)
  5184. "Return a list of all targets in this file.
  5185. When optional argument RADIO is non-nil, only find radio
  5186. targets."
  5187. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5188. (save-excursion
  5189. (goto-char (point-min))
  5190. (while (re-search-forward re nil t)
  5191. ;; Make sure point is really within the object.
  5192. (backward-char)
  5193. (let ((obj (org-element-context)))
  5194. (when (memq (org-element-type obj) '(radio-target target))
  5195. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5196. rtn)))
  5197. (defun org-make-target-link-regexp (targets)
  5198. "Make regular expression matching all strings in TARGETS.
  5199. The regular expression finds the targets also if there is a line break
  5200. between words."
  5201. (and targets
  5202. (concat
  5203. "\\<\\("
  5204. (mapconcat
  5205. (lambda (x)
  5206. (setq x (regexp-quote x))
  5207. (while (string-match " +" x)
  5208. (setq x (replace-match "\\s-+" t t x)))
  5209. x)
  5210. targets
  5211. "\\|")
  5212. "\\)\\>")))
  5213. (defun org-activate-tags (limit)
  5214. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5215. (progn
  5216. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5217. (add-text-properties (match-beginning 1) (match-end 1)
  5218. (list 'mouse-face 'highlight
  5219. 'keymap org-mouse-map))
  5220. (org-rear-nonsticky-at (match-end 1))
  5221. t)))
  5222. (defun org-outline-level ()
  5223. "Compute the outline level of the heading at point.
  5224. If this is called at a normal headline, the level is the number of stars.
  5225. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5226. (save-excursion
  5227. (if (not (condition-case nil
  5228. (org-back-to-heading t)
  5229. (error nil)))
  5230. 0
  5231. (looking-at org-outline-regexp)
  5232. (1- (- (match-end 0) (match-beginning 0))))))
  5233. (defvar org-font-lock-keywords nil)
  5234. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5235. "Regular expression matching a property line.")
  5236. (defvar org-font-lock-hook nil
  5237. "Functions to be called for special font lock stuff.")
  5238. (defvar org-font-lock-set-keywords-hook nil
  5239. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5240. This is called after `org-font-lock-extra-keywords' is defined, but before
  5241. it is installed to be used by font lock. This can be useful if something
  5242. needs to be inserted at a specific position in the font-lock sequence.")
  5243. (defun org-font-lock-hook (limit)
  5244. "Run `org-font-lock-hook' within LIMIT."
  5245. (run-hook-with-args 'org-font-lock-hook limit))
  5246. (defun org-set-font-lock-defaults ()
  5247. "Set font lock defaults for the current buffer."
  5248. (let* ((em org-fontify-emphasized-text)
  5249. (lk org-activate-links)
  5250. (org-font-lock-extra-keywords
  5251. (list
  5252. ;; Call the hook
  5253. '(org-font-lock-hook)
  5254. ;; Headlines
  5255. `(,(if org-fontify-whole-heading-line
  5256. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5257. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5258. (1 (org-get-level-face 1))
  5259. (2 (org-get-level-face 2))
  5260. (3 (org-get-level-face 3)))
  5261. ;; Table lines
  5262. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5263. (1 'org-table t))
  5264. ;; Table internals
  5265. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5266. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5267. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5268. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5269. ;; Drawers
  5270. (list org-drawer-regexp '(0 'org-special-keyword t))
  5271. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5272. ;; Properties
  5273. (list org-property-re
  5274. '(1 'org-special-keyword t)
  5275. '(3 'org-property-value t))
  5276. ;; Links
  5277. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5278. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5279. (if (memq 'plain lk) '(org-activate-plain-links))
  5280. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5281. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5282. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5283. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5284. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5285. '(org-hide-wide-columns (0 nil append))
  5286. ;; TODO keyword
  5287. (list (format org-heading-keyword-regexp-format
  5288. org-todo-regexp)
  5289. '(2 (org-get-todo-face 2) t))
  5290. ;; DONE
  5291. (if org-fontify-done-headline
  5292. (list (format org-heading-keyword-regexp-format
  5293. (concat
  5294. "\\(?:"
  5295. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5296. "\\)"))
  5297. '(2 'org-headline-done t))
  5298. nil)
  5299. ;; Priorities
  5300. '(org-font-lock-add-priority-faces)
  5301. ;; Tags
  5302. '(org-font-lock-add-tag-faces)
  5303. ;; Special keywords
  5304. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5305. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5306. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5307. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5308. ;; Emphasis
  5309. (if em
  5310. (if (featurep 'xemacs)
  5311. '(org-do-emphasis-faces (0 nil append))
  5312. '(org-do-emphasis-faces)))
  5313. ;; Checkboxes
  5314. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5315. 1 'org-checkbox prepend)
  5316. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5317. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5318. (0 (org-get-checkbox-statistics-face) t)))
  5319. ;; Description list items
  5320. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5321. 1 'org-list-dt prepend)
  5322. ;; ARCHIVEd headings
  5323. (list (concat
  5324. org-outline-regexp-bol
  5325. "\\(.*:" org-archive-tag ":.*\\)")
  5326. '(1 'org-archived prepend))
  5327. ;; Specials
  5328. '(org-do-latex-and-special-faces)
  5329. '(org-fontify-entities)
  5330. '(org-raise-scripts)
  5331. ;; Code
  5332. '(org-activate-code (1 'org-code t))
  5333. ;; COMMENT
  5334. (list (format org-heading-keyword-regexp-format
  5335. (concat "\\("
  5336. org-comment-string "\\|" org-quote-string
  5337. "\\)"))
  5338. '(2 'org-special-keyword t))
  5339. ;; Blocks and meta lines
  5340. '(org-fontify-meta-lines-and-blocks)
  5341. )))
  5342. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5343. (run-hooks 'org-font-lock-set-keywords-hook)
  5344. ;; Now set the full font-lock-keywords
  5345. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5346. (org-set-local 'font-lock-defaults
  5347. '(org-font-lock-keywords t nil nil backward-paragraph))
  5348. (kill-local-variable 'font-lock-keywords) nil))
  5349. (defun org-toggle-pretty-entities ()
  5350. "Toggle the composition display of entities as UTF8 characters."
  5351. (interactive)
  5352. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5353. (org-restart-font-lock)
  5354. (if org-pretty-entities
  5355. (message "Entities are displayed as UTF8 characters")
  5356. (save-restriction
  5357. (widen)
  5358. (org-decompose-region (point-min) (point-max))
  5359. (message "Entities are displayed plain"))))
  5360. (defvar org-custom-properties-overlays nil
  5361. "List of overlays used for custom properties.")
  5362. (make-variable-buffer-local 'org-custom-properties-overlays)
  5363. (defun org-toggle-custom-properties-visibility ()
  5364. "Display or hide properties in `org-custom-properties'."
  5365. (interactive)
  5366. (if org-custom-properties-overlays
  5367. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5368. (setq org-custom-properties-overlays nil))
  5369. (unless (not org-custom-properties)
  5370. (save-excursion
  5371. (save-restriction
  5372. (widen)
  5373. (goto-char (point-min))
  5374. (while (re-search-forward org-property-re nil t)
  5375. (mapc (lambda(p)
  5376. (when (equal p (substring (match-string 1) 1 -1))
  5377. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5378. (overlay-put o 'invisible t)
  5379. (overlay-put o 'org-custom-property t)
  5380. (push o org-custom-properties-overlays))))
  5381. org-custom-properties)))))))
  5382. (defun org-fontify-entities (limit)
  5383. "Find an entity to fontify."
  5384. (let (ee)
  5385. (when org-pretty-entities
  5386. (catch 'match
  5387. (while (re-search-forward
  5388. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5389. limit t)
  5390. (if (and (not (org-in-indented-comment-line))
  5391. (setq ee (org-entity-get (match-string 1)))
  5392. (= (length (nth 6 ee)) 1))
  5393. (let*
  5394. ((end (if (equal (match-string 2) "{}")
  5395. (match-end 2)
  5396. (match-end 1))))
  5397. (add-text-properties
  5398. (match-beginning 0) end
  5399. (list 'font-lock-fontified t))
  5400. (compose-region (match-beginning 0) end
  5401. (nth 6 ee) nil)
  5402. (backward-char 1)
  5403. (throw 'match t))))
  5404. nil))))
  5405. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5406. "Fontify string S like in Org-mode."
  5407. (with-temp-buffer
  5408. (insert s)
  5409. (let ((org-odd-levels-only odd-levels))
  5410. (org-mode)
  5411. (font-lock-fontify-buffer)
  5412. (buffer-string))))
  5413. (defvar org-m nil)
  5414. (defvar org-l nil)
  5415. (defvar org-f nil)
  5416. (defun org-get-level-face (n)
  5417. "Get the right face for match N in font-lock matching of headlines."
  5418. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5419. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5420. (if org-cycle-level-faces
  5421. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5422. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5423. (cond
  5424. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5425. ((eq n 2) org-f)
  5426. (t (if org-level-color-stars-only nil org-f))))
  5427. (defun org-get-todo-face (kwd)
  5428. "Get the right face for a TODO keyword KWD.
  5429. If KWD is a number, get the corresponding match group."
  5430. (if (numberp kwd) (setq kwd (match-string kwd)))
  5431. (or (org-face-from-face-or-color
  5432. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5433. (and (member kwd org-done-keywords) 'org-done)
  5434. 'org-todo))
  5435. (defun org-face-from-face-or-color (context inherit face-or-color)
  5436. "Create a face list that inherits INHERIT, but sets the foreground color.
  5437. When FACE-OR-COLOR is not a string, just return it."
  5438. (if (stringp face-or-color)
  5439. (list :inherit inherit
  5440. (cdr (assoc context org-faces-easy-properties))
  5441. face-or-color)
  5442. face-or-color))
  5443. (defun org-font-lock-add-tag-faces (limit)
  5444. "Add the special tag faces."
  5445. (when (and org-tag-faces org-tags-special-faces-re)
  5446. (while (re-search-forward org-tags-special-faces-re limit t)
  5447. (add-text-properties (match-beginning 1) (match-end 1)
  5448. (list 'face (org-get-tag-face 1)
  5449. 'font-lock-fontified t))
  5450. (backward-char 1))))
  5451. (defun org-font-lock-add-priority-faces (limit)
  5452. "Add the special priority faces."
  5453. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5454. (when (save-match-data (org-at-heading-p))
  5455. (add-text-properties
  5456. (match-beginning 0) (match-end 0)
  5457. (list 'face (or (org-face-from-face-or-color
  5458. 'priority 'org-special-keyword
  5459. (cdr (assoc (char-after (match-beginning 1))
  5460. org-priority-faces)))
  5461. 'org-special-keyword)
  5462. 'font-lock-fontified t)))))
  5463. (defun org-get-tag-face (kwd)
  5464. "Get the right face for a TODO keyword KWD.
  5465. If KWD is a number, get the corresponding match group."
  5466. (if (numberp kwd) (setq kwd (match-string kwd)))
  5467. (or (org-face-from-face-or-color
  5468. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5469. 'org-tag))
  5470. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5471. "Remove fontification and activation overlays from links."
  5472. (font-lock-default-unfontify-region beg end)
  5473. (let* ((buffer-undo-list t)
  5474. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5475. (inhibit-modification-hooks t)
  5476. deactivate-mark buffer-file-name buffer-file-truename)
  5477. (org-decompose-region beg end)
  5478. (remove-text-properties beg end
  5479. '(mouse-face t keymap t org-linked-text t
  5480. invisible t intangible t
  5481. org-no-flyspell t org-emphasis t))
  5482. (org-remove-font-lock-display-properties beg end)))
  5483. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5484. ((raise 0.3) (height 0.7))
  5485. ((raise -0.5))
  5486. ((raise 0.5)))
  5487. "Display properties for showing superscripts and subscripts.")
  5488. (defun org-remove-font-lock-display-properties (beg end)
  5489. "Remove specific display properties that have been added by font lock.
  5490. The will remove the raise properties that are used to show superscripts
  5491. and subscripts."
  5492. (let (next prop)
  5493. (while (< beg end)
  5494. (setq next (next-single-property-change beg 'display nil end)
  5495. prop (get-text-property beg 'display))
  5496. (if (member prop org-script-display)
  5497. (put-text-property beg next 'display nil))
  5498. (setq beg next))))
  5499. (defun org-raise-scripts (limit)
  5500. "Add raise properties to sub/superscripts."
  5501. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5502. (if (re-search-forward
  5503. (if (eq org-use-sub-superscripts t)
  5504. org-match-substring-regexp
  5505. org-match-substring-with-braces-regexp)
  5506. limit t)
  5507. (let* ((pos (point)) table-p comment-p
  5508. (mpos (match-beginning 3))
  5509. (emph-p (get-text-property mpos 'org-emphasis))
  5510. (link-p (get-text-property mpos 'mouse-face))
  5511. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5512. (goto-char (point-at-bol))
  5513. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5514. comment-p (org-looking-at-p "[ \t]*#"))
  5515. (goto-char pos)
  5516. ;; FIXME: Should we go back one character here, for a_b^c
  5517. ;; (goto-char (1- pos)) ;????????????????????
  5518. (if (or comment-p emph-p link-p keyw-p)
  5519. t
  5520. (put-text-property (match-beginning 3) (match-end 0)
  5521. 'display
  5522. (if (equal (char-after (match-beginning 2)) ?^)
  5523. (nth (if table-p 3 1) org-script-display)
  5524. (nth (if table-p 2 0) org-script-display)))
  5525. (add-text-properties (match-beginning 2) (match-end 2)
  5526. (list 'invisible t
  5527. 'org-dwidth t 'org-dwidth-n 1))
  5528. (if (and (eq (char-after (match-beginning 3)) ?{)
  5529. (eq (char-before (match-end 3)) ?}))
  5530. (progn
  5531. (add-text-properties
  5532. (match-beginning 3) (1+ (match-beginning 3))
  5533. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5534. (add-text-properties
  5535. (1- (match-end 3)) (match-end 3)
  5536. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5537. t)))))
  5538. ;;;; Visibility cycling, including org-goto and indirect buffer
  5539. ;;; Cycling
  5540. (defvar org-cycle-global-status nil)
  5541. (make-variable-buffer-local 'org-cycle-global-status)
  5542. (defvar org-cycle-subtree-status nil)
  5543. (make-variable-buffer-local 'org-cycle-subtree-status)
  5544. (defvar org-inlinetask-min-level)
  5545. ;;;###autoload
  5546. (defun org-cycle (&optional arg)
  5547. "TAB-action and visibility cycling for Org-mode.
  5548. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5549. is outline visibility cycling, but it also invokes other actions
  5550. in special contexts.
  5551. - When this function is called with a prefix argument, rotate the entire
  5552. buffer through 3 states (global cycling)
  5553. 1. OVERVIEW: Show only top-level headlines.
  5554. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5555. 3. SHOW ALL: Show everything.
  5556. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5557. determined by the variable `org-startup-folded', and by any VISIBILITY
  5558. properties in the buffer.
  5559. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5560. including any drawers.
  5561. - When inside a table, re-align the table and move to the next field.
  5562. - When point is at the beginning of a headline, rotate the subtree started
  5563. by this line through 3 different states (local cycling)
  5564. 1. FOLDED: Only the main headline is shown.
  5565. 2. CHILDREN: The main headline and the direct children are shown.
  5566. From this state, you can move to one of the children
  5567. and zoom in further.
  5568. 3. SUBTREE: Show the entire subtree, including body text.
  5569. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5570. - When point is at the beginning of an empty headline and the variable
  5571. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5572. of the headline by demoting and promoting it to likely levels. This
  5573. speeds up creation document structure by pressing TAB once or several
  5574. times right after creating a new headline.
  5575. - When there is a numeric prefix, go up to a heading with level ARG, do
  5576. a `show-subtree' and return to the previous cursor position. If ARG
  5577. is negative, go up that many levels.
  5578. - When point is not at the beginning of a headline, execute the global
  5579. binding for TAB, which is re-indenting the line. See the option
  5580. `org-cycle-emulate-tab' for details.
  5581. - Special case: if point is at the beginning of the buffer and there is
  5582. no headline in line 1, this function will act as if called with prefix arg
  5583. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5584. But only if also the variable `org-cycle-global-at-bob' is t."
  5585. (interactive "P")
  5586. (org-load-modules-maybe)
  5587. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5588. (and org-cycle-level-after-item/entry-creation
  5589. (or (org-cycle-level)
  5590. (org-cycle-item-indentation))))
  5591. (let* (message-log-max ; Don't populate the *Messages* buffer
  5592. (limit-level
  5593. (or org-cycle-max-level
  5594. (and (boundp 'org-inlinetask-min-level)
  5595. org-inlinetask-min-level
  5596. (1- org-inlinetask-min-level))))
  5597. (nstars (and limit-level
  5598. (if org-odd-levels-only
  5599. (and limit-level (1- (* limit-level 2)))
  5600. limit-level)))
  5601. (org-outline-regexp
  5602. (if (not (derived-mode-p 'org-mode))
  5603. outline-regexp
  5604. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5605. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5606. (not (looking-at org-outline-regexp))))
  5607. (org-cycle-hook
  5608. (if bob-special
  5609. (delq 'org-optimize-window-after-visibility-change
  5610. (copy-sequence org-cycle-hook))
  5611. org-cycle-hook))
  5612. (pos (point)))
  5613. (if (or bob-special (equal arg '(4)))
  5614. ;; special case: use global cycling
  5615. (setq arg t))
  5616. (cond
  5617. ((equal arg '(16))
  5618. (setq last-command 'dummy)
  5619. (org-set-startup-visibility)
  5620. (message "Startup visibility, plus VISIBILITY properties"))
  5621. ((equal arg '(64))
  5622. (show-all)
  5623. (message "Entire buffer visible, including drawers"))
  5624. ;; Table: enter it or move to the next field.
  5625. ((org-at-table-p 'any)
  5626. (if (org-at-table.el-p)
  5627. (message "Use C-c ' to edit table.el tables")
  5628. (if arg (org-table-edit-field t)
  5629. (org-table-justify-field-maybe)
  5630. (call-interactively 'org-table-next-field))))
  5631. ((run-hook-with-args-until-success
  5632. 'org-tab-after-check-for-table-hook))
  5633. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5634. ((eq arg t) (org-cycle-internal-global))
  5635. ;; Drawers: delegate to `org-flag-drawer'.
  5636. ((and org-drawers org-drawer-regexp
  5637. (save-excursion
  5638. (beginning-of-line 1)
  5639. (looking-at org-drawer-regexp)))
  5640. (org-flag-drawer ; toggle block visibility
  5641. (not (get-char-property (match-end 0) 'invisible))))
  5642. ;; Show-subtree, ARG levels up from here.
  5643. ((integerp arg)
  5644. (save-excursion
  5645. (org-back-to-heading)
  5646. (outline-up-heading (if (< arg 0) (- arg)
  5647. (- (funcall outline-level) arg)))
  5648. (org-show-subtree)))
  5649. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5650. ((and (featurep 'org-inlinetask)
  5651. (org-inlinetask-at-task-p)
  5652. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5653. (org-inlinetask-toggle-visibility))
  5654. ((org-try-cdlatex-tab))
  5655. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5656. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5657. (save-excursion (beginning-of-line 1)
  5658. (looking-at org-outline-regexp)))
  5659. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5660. (org-cycle-internal-local))
  5661. ;; From there: TAB emulation and template completion.
  5662. (buffer-read-only (org-back-to-heading))
  5663. ((run-hook-with-args-until-success
  5664. 'org-tab-after-check-for-cycling-hook))
  5665. ((org-try-structure-completion))
  5666. ((run-hook-with-args-until-success
  5667. 'org-tab-before-tab-emulation-hook))
  5668. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5669. (or (not (bolp))
  5670. (not (looking-at org-outline-regexp))))
  5671. (call-interactively (global-key-binding "\t")))
  5672. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5673. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5674. (or (and (eq org-cycle-emulate-tab 'white)
  5675. (= (match-end 0) (point-at-eol)))
  5676. (and (eq org-cycle-emulate-tab 'whitestart)
  5677. (>= (match-end 0) pos))))
  5678. t
  5679. (eq org-cycle-emulate-tab t))
  5680. (call-interactively (global-key-binding "\t")))
  5681. (t (save-excursion
  5682. (org-back-to-heading)
  5683. (org-cycle)))))))
  5684. (defun org-cycle-internal-global ()
  5685. "Do the global cycling action."
  5686. ;; Hack to avoid display of messages for .org attachments in Gnus
  5687. (let (message-log-max ; Don't populate the *Messages* buffer
  5688. (ga (string-match "\\*fontification" (buffer-name))))
  5689. (cond
  5690. ((and (eq last-command this-command)
  5691. (eq org-cycle-global-status 'overview))
  5692. ;; We just created the overview - now do table of contents
  5693. ;; This can be slow in very large buffers, so indicate action
  5694. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5695. (unless ga (message "CONTENTS..."))
  5696. (org-content)
  5697. (unless ga (message "CONTENTS...done"))
  5698. (setq org-cycle-global-status 'contents)
  5699. (run-hook-with-args 'org-cycle-hook 'contents))
  5700. ((and (eq last-command this-command)
  5701. (eq org-cycle-global-status 'contents))
  5702. ;; We just showed the table of contents - now show everything
  5703. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5704. (show-all)
  5705. (unless ga (message "SHOW ALL"))
  5706. (setq org-cycle-global-status 'all)
  5707. (run-hook-with-args 'org-cycle-hook 'all))
  5708. (t
  5709. ;; Default action: go to overview
  5710. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5711. (org-overview)
  5712. (unless ga (message "OVERVIEW"))
  5713. (setq org-cycle-global-status 'overview)
  5714. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5715. (defun org-cycle-internal-local ()
  5716. "Do the local cycling action."
  5717. (let (message-log-max ; Don't populate the *Messages* buffer
  5718. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5719. ;; First, determine end of headline (EOH), end of subtree or item
  5720. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5721. (save-excursion
  5722. (if (org-at-item-p)
  5723. (progn
  5724. (beginning-of-line)
  5725. (setq struct (org-list-struct))
  5726. (setq eoh (point-at-eol))
  5727. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5728. (setq has-children (org-list-has-child-p (point) struct)))
  5729. (org-back-to-heading)
  5730. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5731. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5732. (setq has-children
  5733. (or (save-excursion
  5734. (let ((level (funcall outline-level)))
  5735. (outline-next-heading)
  5736. (and (org-at-heading-p t)
  5737. (> (funcall outline-level) level))))
  5738. (save-excursion
  5739. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5740. ;; Determine end invisible part of buffer (EOL)
  5741. (beginning-of-line 2)
  5742. ;; XEmacs doesn't have `next-single-char-property-change'
  5743. (if (featurep 'xemacs)
  5744. (while (and (not (eobp)) ;; this is like `next-line'
  5745. (get-char-property (1- (point)) 'invisible))
  5746. (beginning-of-line 2))
  5747. (while (and (not (eobp)) ;; this is like `next-line'
  5748. (get-char-property (1- (point)) 'invisible))
  5749. (goto-char (next-single-char-property-change (point) 'invisible))
  5750. (and (eolp) (beginning-of-line 2))))
  5751. (setq eol (point)))
  5752. ;; Find out what to do next and set `this-command'
  5753. (cond
  5754. ((= eos eoh)
  5755. ;; Nothing is hidden behind this heading
  5756. (unless (org-before-first-heading-p)
  5757. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5758. (message "EMPTY ENTRY")
  5759. (setq org-cycle-subtree-status nil)
  5760. (save-excursion
  5761. (goto-char eos)
  5762. (outline-next-heading)
  5763. (if (outline-invisible-p) (org-flag-heading nil))))
  5764. ((and (or (>= eol eos)
  5765. (not (string-match "\\S-" (buffer-substring eol eos))))
  5766. (or has-children
  5767. (not (setq children-skipped
  5768. org-cycle-skip-children-state-if-no-children))))
  5769. ;; Entire subtree is hidden in one line: children view
  5770. (unless (org-before-first-heading-p)
  5771. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5772. (if (org-at-item-p)
  5773. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5774. (org-show-entry)
  5775. (org-with-limited-levels (show-children))
  5776. ;; FIXME: This slows down the func way too much.
  5777. ;; How keep drawers hidden in subtree anyway?
  5778. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5779. ;; (org-cycle-hide-drawers 'subtree))
  5780. ;; Fold every list in subtree to top-level items.
  5781. (when (eq org-cycle-include-plain-lists 'integrate)
  5782. (save-excursion
  5783. (org-back-to-heading)
  5784. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5785. (beginning-of-line 1)
  5786. (let* ((struct (org-list-struct))
  5787. (prevs (org-list-prevs-alist struct))
  5788. (end (org-list-get-bottom-point struct)))
  5789. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5790. (org-list-get-all-items (point) struct prevs))
  5791. (goto-char end))))))
  5792. (message "CHILDREN")
  5793. (save-excursion
  5794. (goto-char eos)
  5795. (outline-next-heading)
  5796. (if (outline-invisible-p) (org-flag-heading nil)))
  5797. (setq org-cycle-subtree-status 'children)
  5798. (unless (org-before-first-heading-p)
  5799. (run-hook-with-args 'org-cycle-hook 'children)))
  5800. ((or children-skipped
  5801. (and (eq last-command this-command)
  5802. (eq org-cycle-subtree-status 'children)))
  5803. ;; We just showed the children, or no children are there,
  5804. ;; now show everything.
  5805. (unless (org-before-first-heading-p)
  5806. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5807. (outline-flag-region eoh eos nil)
  5808. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5809. (setq org-cycle-subtree-status 'subtree)
  5810. (unless (org-before-first-heading-p)
  5811. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5812. (t
  5813. ;; Default action: hide the subtree.
  5814. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5815. (outline-flag-region eoh eos t)
  5816. (message "FOLDED")
  5817. (setq org-cycle-subtree-status 'folded)
  5818. (unless (org-before-first-heading-p)
  5819. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5820. ;;;###autoload
  5821. (defun org-global-cycle (&optional arg)
  5822. "Cycle the global visibility. For details see `org-cycle'.
  5823. With \\[universal-argument] prefix arg, switch to startup visibility.
  5824. With a numeric prefix, show all headlines up to that level."
  5825. (interactive "P")
  5826. (let ((org-cycle-include-plain-lists
  5827. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5828. (cond
  5829. ((integerp arg)
  5830. (show-all)
  5831. (hide-sublevels arg)
  5832. (setq org-cycle-global-status 'contents))
  5833. ((equal arg '(4))
  5834. (org-set-startup-visibility)
  5835. (message "Startup visibility, plus VISIBILITY properties."))
  5836. (t
  5837. (org-cycle '(4))))))
  5838. (defun org-set-startup-visibility ()
  5839. "Set the visibility required by startup options and properties."
  5840. (cond
  5841. ((eq org-startup-folded t)
  5842. (org-cycle '(4)))
  5843. ((eq org-startup-folded 'content)
  5844. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5845. (org-cycle '(4)) (org-cycle '(4)))))
  5846. (unless (eq org-startup-folded 'showeverything)
  5847. (if org-hide-block-startup (org-hide-block-all))
  5848. (org-set-visibility-according-to-property 'no-cleanup)
  5849. (org-cycle-hide-archived-subtrees 'all)
  5850. (org-cycle-hide-drawers 'all)
  5851. (org-cycle-show-empty-lines t)))
  5852. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5853. "Switch subtree visibilities according to :VISIBILITY: property."
  5854. (interactive)
  5855. (let (org-show-entry-below state)
  5856. (save-excursion
  5857. (goto-char (point-min))
  5858. (while (re-search-forward
  5859. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5860. nil t)
  5861. (setq state (match-string 1))
  5862. (save-excursion
  5863. (org-back-to-heading t)
  5864. (hide-subtree)
  5865. (org-reveal)
  5866. (cond
  5867. ((equal state '("fold" "folded"))
  5868. (hide-subtree))
  5869. ((equal state "children")
  5870. (org-show-hidden-entry)
  5871. (show-children))
  5872. ((equal state "content")
  5873. (save-excursion
  5874. (save-restriction
  5875. (org-narrow-to-subtree)
  5876. (org-content))))
  5877. ((member state '("all" "showall"))
  5878. (show-subtree)))))
  5879. (unless no-cleanup
  5880. (org-cycle-hide-archived-subtrees 'all)
  5881. (org-cycle-hide-drawers 'all)
  5882. (org-cycle-show-empty-lines 'all)))))
  5883. ;; This function uses outline-regexp instead of the more fundamental
  5884. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5885. ;; buffers, where outline-regexp is needed.
  5886. (defun org-overview ()
  5887. "Switch to overview mode, showing only top-level headlines.
  5888. Really, this shows all headlines with level equal or greater than the level
  5889. of the first headline in the buffer. This is important, because if the
  5890. first headline is not level one, then (hide-sublevels 1) gives confusing
  5891. results."
  5892. (interactive)
  5893. (let ((level (save-excursion
  5894. (goto-char (point-min))
  5895. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5896. (progn
  5897. (goto-char (match-beginning 0))
  5898. (funcall outline-level))))))
  5899. (and level (hide-sublevels level))))
  5900. (defun org-content (&optional arg)
  5901. "Show all headlines in the buffer, like a table of contents.
  5902. With numerical argument N, show content up to level N."
  5903. (interactive "P")
  5904. (save-excursion
  5905. ;; Visit all headings and show their offspring
  5906. (and (integerp arg) (org-overview))
  5907. (goto-char (point-max))
  5908. (catch 'exit
  5909. (while (and (progn (condition-case nil
  5910. (outline-previous-visible-heading 1)
  5911. (error (goto-char (point-min))))
  5912. t)
  5913. (looking-at org-outline-regexp))
  5914. (if (integerp arg)
  5915. (show-children (1- arg))
  5916. (show-branches))
  5917. (if (bobp) (throw 'exit nil))))))
  5918. (defun org-optimize-window-after-visibility-change (state)
  5919. "Adjust the window after a change in outline visibility.
  5920. This function is the default value of the hook `org-cycle-hook'."
  5921. (when (get-buffer-window (current-buffer))
  5922. (cond
  5923. ((eq state 'content) nil)
  5924. ((eq state 'all) nil)
  5925. ((eq state 'folded) nil)
  5926. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5927. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5928. (defun org-remove-empty-overlays-at (pos)
  5929. "Remove outline overlays that do not contain non-white stuff."
  5930. (mapc
  5931. (lambda (o)
  5932. (and (eq 'outline (overlay-get o 'invisible))
  5933. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5934. (overlay-end o))))
  5935. (delete-overlay o)))
  5936. (overlays-at pos)))
  5937. (defun org-clean-visibility-after-subtree-move ()
  5938. "Fix visibility issues after moving a subtree."
  5939. ;; First, find a reasonable region to look at:
  5940. ;; Start two siblings above, end three below
  5941. (let* ((beg (save-excursion
  5942. (and (org-get-last-sibling)
  5943. (org-get-last-sibling))
  5944. (point)))
  5945. (end (save-excursion
  5946. (and (org-get-next-sibling)
  5947. (org-get-next-sibling)
  5948. (org-get-next-sibling))
  5949. (if (org-at-heading-p)
  5950. (point-at-eol)
  5951. (point))))
  5952. (level (looking-at "\\*+"))
  5953. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5954. (save-excursion
  5955. (save-restriction
  5956. (narrow-to-region beg end)
  5957. (when re
  5958. ;; Properly fold already folded siblings
  5959. (goto-char (point-min))
  5960. (while (re-search-forward re nil t)
  5961. (if (and (not (outline-invisible-p))
  5962. (save-excursion
  5963. (goto-char (point-at-eol)) (outline-invisible-p)))
  5964. (hide-entry))))
  5965. (org-cycle-show-empty-lines 'overview)
  5966. (org-cycle-hide-drawers 'overview)))))
  5967. (defun org-cycle-show-empty-lines (state)
  5968. "Show empty lines above all visible headlines.
  5969. The region to be covered depends on STATE when called through
  5970. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5971. entire buffer covered. Note that an empty line is only shown if there
  5972. are at least `org-cycle-separator-lines' empty lines before the headline."
  5973. (when (not (= org-cycle-separator-lines 0))
  5974. (save-excursion
  5975. (let* ((n (abs org-cycle-separator-lines))
  5976. (re (cond
  5977. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5978. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5979. (t (let ((ns (number-to-string (- n 2))))
  5980. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5981. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5982. beg end b e)
  5983. (cond
  5984. ((memq state '(overview contents t))
  5985. (setq beg (point-min) end (point-max)))
  5986. ((memq state '(children folded))
  5987. (setq beg (point) end (progn (org-end-of-subtree t t)
  5988. (beginning-of-line 2)
  5989. (point)))))
  5990. (when beg
  5991. (goto-char beg)
  5992. (while (re-search-forward re end t)
  5993. (unless (get-char-property (match-end 1) 'invisible)
  5994. (setq e (match-end 1))
  5995. (if (< org-cycle-separator-lines 0)
  5996. (setq b (save-excursion
  5997. (goto-char (match-beginning 0))
  5998. (org-back-over-empty-lines)
  5999. (if (save-excursion
  6000. (goto-char (max (point-min) (1- (point))))
  6001. (org-at-heading-p))
  6002. (1- (point))
  6003. (point))))
  6004. (setq b (match-beginning 1)))
  6005. (outline-flag-region b e nil)))))))
  6006. ;; Never hide empty lines at the end of the file.
  6007. (save-excursion
  6008. (goto-char (point-max))
  6009. (outline-previous-heading)
  6010. (outline-end-of-heading)
  6011. (if (and (looking-at "[ \t\n]+")
  6012. (= (match-end 0) (point-max)))
  6013. (outline-flag-region (point) (match-end 0) nil))))
  6014. (defun org-show-empty-lines-in-parent ()
  6015. "Move to the parent and re-show empty lines before visible headlines."
  6016. (save-excursion
  6017. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6018. (org-cycle-show-empty-lines context))))
  6019. (defun org-files-list ()
  6020. "Return `org-agenda-files' list, plus all open org-mode files.
  6021. This is useful for operations that need to scan all of a user's
  6022. open and agenda-wise Org files."
  6023. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6024. (dolist (buf (buffer-list))
  6025. (with-current-buffer buf
  6026. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6027. (let ((file (expand-file-name (buffer-file-name))))
  6028. (unless (member file files)
  6029. (push file files))))))
  6030. files))
  6031. (defsubst org-entry-beginning-position ()
  6032. "Return the beginning position of the current entry."
  6033. (save-excursion (outline-back-to-heading t) (point)))
  6034. (defsubst org-entry-end-position ()
  6035. "Return the end position of the current entry."
  6036. (save-excursion (outline-next-heading) (point)))
  6037. (defun org-cycle-hide-drawers (state)
  6038. "Re-hide all drawers after a visibility state change."
  6039. (when (and (derived-mode-p 'org-mode)
  6040. (not (memq state '(overview folded contents))))
  6041. (save-excursion
  6042. (let* ((globalp (memq state '(contents all)))
  6043. (beg (if globalp (point-min) (point)))
  6044. (end (if globalp (point-max)
  6045. (if (eq state 'children)
  6046. (save-excursion (outline-next-heading) (point))
  6047. (org-end-of-subtree t)))))
  6048. (goto-char beg)
  6049. (while (re-search-forward org-drawer-regexp end t)
  6050. (org-flag-drawer t))))))
  6051. (defun org-cycle-hide-inline-tasks (state)
  6052. "Re-hide inline task when switching to 'contents visibility state."
  6053. (when (and (eq state 'contents)
  6054. (boundp 'org-inlinetask-min-level)
  6055. org-inlinetask-min-level)
  6056. (hide-sublevels (1- org-inlinetask-min-level))))
  6057. (defun org-flag-drawer (flag)
  6058. "When FLAG is non-nil, hide the drawer we are within.
  6059. Otherwise make it visible."
  6060. (save-excursion
  6061. (beginning-of-line 1)
  6062. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6063. (let ((b (match-end 0)))
  6064. (if (re-search-forward
  6065. "^[ \t]*:END:"
  6066. (save-excursion (outline-next-heading) (point)) t)
  6067. (outline-flag-region b (point-at-eol) flag)
  6068. (error ":END: line missing at position %s" b))))))
  6069. (defun org-subtree-end-visible-p ()
  6070. "Is the end of the current subtree visible?"
  6071. (pos-visible-in-window-p
  6072. (save-excursion (org-end-of-subtree t) (point))))
  6073. (defun org-first-headline-recenter (&optional N)
  6074. "Move cursor to the first headline and recenter the headline.
  6075. Optional argument N means put the headline into the Nth line of the window."
  6076. (goto-char (point-min))
  6077. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6078. (beginning-of-line)
  6079. (recenter (prefix-numeric-value N))))
  6080. ;;; Saving and restoring visibility
  6081. (defun org-outline-overlay-data (&optional use-markers)
  6082. "Return a list of the locations of all outline overlays.
  6083. These are overlays with the `invisible' property value `outline'.
  6084. The return value is a list of cons cells, with start and stop
  6085. positions for each overlay.
  6086. If USE-MARKERS is set, return the positions as markers."
  6087. (let (beg end)
  6088. (save-excursion
  6089. (save-restriction
  6090. (widen)
  6091. (delq nil
  6092. (mapcar (lambda (o)
  6093. (when (eq (overlay-get o 'invisible) 'outline)
  6094. (setq beg (overlay-start o)
  6095. end (overlay-end o))
  6096. (and beg end (> end beg)
  6097. (if use-markers
  6098. (cons (move-marker (make-marker) beg)
  6099. (move-marker (make-marker) end))
  6100. (cons beg end)))))
  6101. (overlays-in (point-min) (point-max))))))))
  6102. (defun org-set-outline-overlay-data (data)
  6103. "Create visibility overlays for all positions in DATA.
  6104. DATA should have been made by `org-outline-overlay-data'."
  6105. (let (o)
  6106. (save-excursion
  6107. (save-restriction
  6108. (widen)
  6109. (show-all)
  6110. (mapc (lambda (c)
  6111. (outline-flag-region (car c) (cdr c) t))
  6112. data)))))
  6113. ;;; Folding of blocks
  6114. (defvar org-hide-block-overlays nil
  6115. "Overlays hiding blocks.")
  6116. (make-variable-buffer-local 'org-hide-block-overlays)
  6117. (defun org-block-map (function &optional start end)
  6118. "Call FUNCTION at the head of all source blocks in the current buffer.
  6119. Optional arguments START and END can be used to limit the range."
  6120. (let ((start (or start (point-min)))
  6121. (end (or end (point-max))))
  6122. (save-excursion
  6123. (goto-char start)
  6124. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6125. (save-excursion
  6126. (save-match-data
  6127. (goto-char (match-beginning 0))
  6128. (funcall function)))))))
  6129. (defun org-hide-block-toggle-all ()
  6130. "Toggle the visibility of all blocks in the current buffer."
  6131. (org-block-map #'org-hide-block-toggle))
  6132. (defun org-hide-block-all ()
  6133. "Fold all blocks in the current buffer."
  6134. (interactive)
  6135. (org-show-block-all)
  6136. (org-block-map #'org-hide-block-toggle-maybe))
  6137. (defun org-show-block-all ()
  6138. "Unfold all blocks in the current buffer."
  6139. (interactive)
  6140. (mapc 'delete-overlay org-hide-block-overlays)
  6141. (setq org-hide-block-overlays nil))
  6142. (defun org-hide-block-toggle-maybe ()
  6143. "Toggle visibility of block at point."
  6144. (interactive)
  6145. (let ((case-fold-search t))
  6146. (if (save-excursion
  6147. (beginning-of-line 1)
  6148. (looking-at org-block-regexp))
  6149. (progn (org-hide-block-toggle)
  6150. t) ;; to signal that we took action
  6151. nil))) ;; to signal that we did not
  6152. (defun org-hide-block-toggle (&optional force)
  6153. "Toggle the visibility of the current block."
  6154. (interactive)
  6155. (save-excursion
  6156. (beginning-of-line)
  6157. (if (re-search-forward org-block-regexp nil t)
  6158. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6159. (end (match-end 0)) ;; end of entire body
  6160. ov)
  6161. (if (memq t (mapcar (lambda (overlay)
  6162. (eq (overlay-get overlay 'invisible)
  6163. 'org-hide-block))
  6164. (overlays-at start)))
  6165. (if (or (not force) (eq force 'off))
  6166. (mapc (lambda (ov)
  6167. (when (member ov org-hide-block-overlays)
  6168. (setq org-hide-block-overlays
  6169. (delq ov org-hide-block-overlays)))
  6170. (when (eq (overlay-get ov 'invisible)
  6171. 'org-hide-block)
  6172. (delete-overlay ov)))
  6173. (overlays-at start)))
  6174. (setq ov (make-overlay start end))
  6175. (overlay-put ov 'invisible 'org-hide-block)
  6176. ;; make the block accessible to isearch
  6177. (overlay-put
  6178. ov 'isearch-open-invisible
  6179. (lambda (ov)
  6180. (when (member ov org-hide-block-overlays)
  6181. (setq org-hide-block-overlays
  6182. (delq ov org-hide-block-overlays)))
  6183. (when (eq (overlay-get ov 'invisible)
  6184. 'org-hide-block)
  6185. (delete-overlay ov))))
  6186. (push ov org-hide-block-overlays)))
  6187. (error "Not looking at a source block"))))
  6188. ;; org-tab-after-check-for-cycling-hook
  6189. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6190. ;; Remove overlays when changing major mode
  6191. (add-hook 'org-mode-hook
  6192. (lambda () (org-add-hook 'change-major-mode-hook
  6193. 'org-show-block-all 'append 'local)))
  6194. ;;; Org-goto
  6195. (defvar org-goto-window-configuration nil)
  6196. (defvar org-goto-marker nil)
  6197. (defvar org-goto-map)
  6198. (defun org-goto-map ()
  6199. "Set the keymap `org-goto'."
  6200. (setq org-goto-map
  6201. (let ((map (make-sparse-keymap)))
  6202. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6203. mouse-drag-region universal-argument org-occur))
  6204. cmd)
  6205. (while (setq cmd (pop cmds))
  6206. (substitute-key-definition cmd cmd map global-map)))
  6207. (suppress-keymap map)
  6208. (org-defkey map "\C-m" 'org-goto-ret)
  6209. (org-defkey map [(return)] 'org-goto-ret)
  6210. (org-defkey map [(left)] 'org-goto-left)
  6211. (org-defkey map [(right)] 'org-goto-right)
  6212. (org-defkey map [(control ?g)] 'org-goto-quit)
  6213. (org-defkey map "\C-i" 'org-cycle)
  6214. (org-defkey map [(tab)] 'org-cycle)
  6215. (org-defkey map [(down)] 'outline-next-visible-heading)
  6216. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6217. (if org-goto-auto-isearch
  6218. (if (fboundp 'define-key-after)
  6219. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6220. nil)
  6221. (org-defkey map "q" 'org-goto-quit)
  6222. (org-defkey map "n" 'outline-next-visible-heading)
  6223. (org-defkey map "p" 'outline-previous-visible-heading)
  6224. (org-defkey map "f" 'outline-forward-same-level)
  6225. (org-defkey map "b" 'outline-backward-same-level)
  6226. (org-defkey map "u" 'outline-up-heading))
  6227. (org-defkey map "/" 'org-occur)
  6228. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6229. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6230. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6231. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6232. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6233. map)))
  6234. (defconst org-goto-help
  6235. "Browse buffer copy, to find location or copy text.%s
  6236. RET=jump to location C-g=quit and return to previous location
  6237. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6238. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6239. ;; FIXME: Docstring does not mention both interfaces
  6240. (defun org-goto (&optional alternative-interface)
  6241. "Look up a different location in the current file, keeping current visibility.
  6242. When you want look-up or go to a different location in a
  6243. document, the fastest way is often to fold the entire buffer and
  6244. then dive into the tree. This method has the disadvantage, that
  6245. the previous location will be folded, which may not be what you
  6246. want.
  6247. This command works around this by showing a copy of the current
  6248. buffer in an indirect buffer, in overview mode. You can dive
  6249. into the tree in that copy, use org-occur and incremental search
  6250. to find a location. When pressing RET or `Q', the command
  6251. returns to the original buffer in which the visibility is still
  6252. unchanged. After RET it will also jump to the location selected
  6253. in the indirect buffer and expose the headline hierarchy above.
  6254. With a prefix argument, use the alternative interface: e.g. if
  6255. `org-goto-interface' is 'outline use 'outline-path-completion."
  6256. (interactive "P")
  6257. (org-goto-map)
  6258. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6259. (org-refile-use-outline-path t)
  6260. (org-refile-target-verify-function nil)
  6261. (interface
  6262. (if (not alternative-interface)
  6263. org-goto-interface
  6264. (if (eq org-goto-interface 'outline)
  6265. 'outline-path-completion
  6266. 'outline)))
  6267. (org-goto-start-pos (point))
  6268. (selected-point
  6269. (if (eq interface 'outline)
  6270. (car (org-get-location (current-buffer) org-goto-help))
  6271. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6272. (org-refile-check-position pa)
  6273. (nth 3 pa)))))
  6274. (if selected-point
  6275. (progn
  6276. (org-mark-ring-push org-goto-start-pos)
  6277. (goto-char selected-point)
  6278. (if (or (outline-invisible-p) (org-invisible-p2))
  6279. (org-show-context 'org-goto)))
  6280. (message "Quit"))))
  6281. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6282. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6283. (defvar org-goto-local-auto-isearch-map) ; defined below
  6284. (defun org-get-location (buf help)
  6285. "Let the user select a location in the Org-mode buffer BUF.
  6286. This function uses a recursive edit. It returns the selected position
  6287. or nil."
  6288. (org-no-popups
  6289. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6290. (isearch-hide-immediately nil)
  6291. (isearch-search-fun-function
  6292. (lambda () 'org-goto-local-search-headings))
  6293. (org-goto-selected-point org-goto-exit-command))
  6294. (save-excursion
  6295. (save-window-excursion
  6296. (delete-other-windows)
  6297. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6298. (org-pop-to-buffer-same-window
  6299. (condition-case nil
  6300. (make-indirect-buffer (current-buffer) "*org-goto*")
  6301. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6302. (with-output-to-temp-buffer "*Org Help*"
  6303. (princ (format help (if org-goto-auto-isearch
  6304. " Just type for auto-isearch."
  6305. " n/p/f/b/u to navigate, q to quit."))))
  6306. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6307. (setq buffer-read-only nil)
  6308. (let ((org-startup-truncated t)
  6309. (org-startup-folded nil)
  6310. (org-startup-align-all-tables nil))
  6311. (org-mode)
  6312. (org-overview))
  6313. (setq buffer-read-only t)
  6314. (if (and (boundp 'org-goto-start-pos)
  6315. (integer-or-marker-p org-goto-start-pos))
  6316. (let ((org-show-hierarchy-above t)
  6317. (org-show-siblings t)
  6318. (org-show-following-heading t))
  6319. (goto-char org-goto-start-pos)
  6320. (and (outline-invisible-p) (org-show-context)))
  6321. (goto-char (point-min)))
  6322. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6323. (message "Select location and press RET")
  6324. (use-local-map org-goto-map)
  6325. (recursive-edit)))
  6326. (kill-buffer "*org-goto*")
  6327. (cons org-goto-selected-point org-goto-exit-command))))
  6328. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6329. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6330. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6331. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6332. (defun org-goto-local-search-headings (string bound noerror)
  6333. "Search and make sure that any matches are in headlines."
  6334. (catch 'return
  6335. (while (if isearch-forward
  6336. (search-forward string bound noerror)
  6337. (search-backward string bound noerror))
  6338. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6339. (and (member :headline context)
  6340. (not (member :tags context))))
  6341. (throw 'return (point))))))
  6342. (defun org-goto-local-auto-isearch ()
  6343. "Start isearch."
  6344. (interactive)
  6345. (goto-char (point-min))
  6346. (let ((keys (this-command-keys)))
  6347. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6348. (isearch-mode t)
  6349. (isearch-process-search-char (string-to-char keys)))))
  6350. (defun org-goto-ret (&optional arg)
  6351. "Finish `org-goto' by going to the new location."
  6352. (interactive "P")
  6353. (setq org-goto-selected-point (point)
  6354. org-goto-exit-command 'return)
  6355. (throw 'exit nil))
  6356. (defun org-goto-left ()
  6357. "Finish `org-goto' by going to the new location."
  6358. (interactive)
  6359. (if (org-at-heading-p)
  6360. (progn
  6361. (beginning-of-line 1)
  6362. (setq org-goto-selected-point (point)
  6363. org-goto-exit-command 'left)
  6364. (throw 'exit nil))
  6365. (error "Not on a heading")))
  6366. (defun org-goto-right ()
  6367. "Finish `org-goto' by going to the new location."
  6368. (interactive)
  6369. (if (org-at-heading-p)
  6370. (progn
  6371. (setq org-goto-selected-point (point)
  6372. org-goto-exit-command 'right)
  6373. (throw 'exit nil))
  6374. (error "Not on a heading")))
  6375. (defun org-goto-quit ()
  6376. "Finish `org-goto' without cursor motion."
  6377. (interactive)
  6378. (setq org-goto-selected-point nil)
  6379. (setq org-goto-exit-command 'quit)
  6380. (throw 'exit nil))
  6381. ;;; Indirect buffer display of subtrees
  6382. (defvar org-indirect-dedicated-frame nil
  6383. "This is the frame being used for indirect tree display.")
  6384. (defvar org-last-indirect-buffer nil)
  6385. (defun org-tree-to-indirect-buffer (&optional arg)
  6386. "Create indirect buffer and narrow it to current subtree.
  6387. With a numerical prefix ARG, go up to this level and then take that tree.
  6388. If ARG is negative, go up that many levels.
  6389. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6390. indirect buffer previously made with this command, to avoid proliferation of
  6391. indirect buffers. However, when you call the command with a \
  6392. \\[universal-argument] prefix, or
  6393. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6394. is kept so that you can work with several indirect buffers at the same time.
  6395. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6396. \\[universal-argument] prefix also
  6397. requests that a new frame be made for the new buffer, so that the dedicated
  6398. frame is not changed."
  6399. (interactive "P")
  6400. (let ((cbuf (current-buffer))
  6401. (cwin (selected-window))
  6402. (pos (point))
  6403. beg end level heading ibuf)
  6404. (save-excursion
  6405. (org-back-to-heading t)
  6406. (when (numberp arg)
  6407. (setq level (org-outline-level))
  6408. (if (< arg 0) (setq arg (+ level arg)))
  6409. (while (> (setq level (org-outline-level)) arg)
  6410. (org-up-heading-safe)))
  6411. (setq beg (point)
  6412. heading (org-get-heading))
  6413. (org-end-of-subtree t t)
  6414. (if (org-at-heading-p) (backward-char 1))
  6415. (setq end (point)))
  6416. (if (and (buffer-live-p org-last-indirect-buffer)
  6417. (not (eq org-indirect-buffer-display 'new-frame))
  6418. (not arg))
  6419. (kill-buffer org-last-indirect-buffer))
  6420. (setq ibuf (org-get-indirect-buffer cbuf)
  6421. org-last-indirect-buffer ibuf)
  6422. (cond
  6423. ((or (eq org-indirect-buffer-display 'new-frame)
  6424. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6425. (select-frame (make-frame))
  6426. (delete-other-windows)
  6427. (org-pop-to-buffer-same-window ibuf)
  6428. (org-set-frame-title heading))
  6429. ((eq org-indirect-buffer-display 'dedicated-frame)
  6430. (raise-frame
  6431. (select-frame (or (and org-indirect-dedicated-frame
  6432. (frame-live-p org-indirect-dedicated-frame)
  6433. org-indirect-dedicated-frame)
  6434. (setq org-indirect-dedicated-frame (make-frame)))))
  6435. (delete-other-windows)
  6436. (org-pop-to-buffer-same-window ibuf)
  6437. (org-set-frame-title (concat "Indirect: " heading)))
  6438. ((eq org-indirect-buffer-display 'current-window)
  6439. (org-pop-to-buffer-same-window ibuf))
  6440. ((eq org-indirect-buffer-display 'other-window)
  6441. (pop-to-buffer ibuf))
  6442. (t (error "Invalid value")))
  6443. (if (featurep 'xemacs)
  6444. (save-excursion (org-mode) (turn-on-font-lock)))
  6445. (narrow-to-region beg end)
  6446. (show-all)
  6447. (goto-char pos)
  6448. (run-hook-with-args 'org-cycle-hook 'all)
  6449. (and (window-live-p cwin) (select-window cwin))))
  6450. (defun org-get-indirect-buffer (&optional buffer)
  6451. (setq buffer (or buffer (current-buffer)))
  6452. (let ((n 1) (base (buffer-name buffer)) bname)
  6453. (while (buffer-live-p
  6454. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6455. (setq n (1+ n)))
  6456. (condition-case nil
  6457. (make-indirect-buffer buffer bname 'clone)
  6458. (error (make-indirect-buffer buffer bname)))))
  6459. (defun org-set-frame-title (title)
  6460. "Set the title of the current frame to the string TITLE."
  6461. ;; FIXME: how to name a single frame in XEmacs???
  6462. (unless (featurep 'xemacs)
  6463. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6464. ;;;; Structure editing
  6465. ;;; Inserting headlines
  6466. (defun org-previous-line-empty-p ()
  6467. (save-excursion
  6468. (and (not (bobp))
  6469. (or (beginning-of-line 0) t)
  6470. (save-match-data
  6471. (looking-at "[ \t]*$")))))
  6472. (defun org-insert-heading (&optional force-heading invisible-ok)
  6473. "Insert a new heading or item with same depth at point.
  6474. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6475. If point is at the beginning of a headline, insert a sibling before the
  6476. current headline. If point is not at the beginning, split the line,
  6477. create the new headline with the text in the current line after point
  6478. \(but see also the variable `org-M-RET-may-split-line').
  6479. With a double prefix arg, force the heading to be inserted at the
  6480. end of the parent subtree.
  6481. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6482. This is important for non-interactive uses of the command."
  6483. (interactive "P")
  6484. (if (or (= (buffer-size) 0)
  6485. (and (not (save-excursion
  6486. (and (ignore-errors (org-back-to-heading invisible-ok))
  6487. (org-at-heading-p))))
  6488. (or force-heading (not (org-in-item-p)))))
  6489. (progn
  6490. (insert "\n* ")
  6491. (run-hooks 'org-insert-heading-hook))
  6492. (when (or force-heading (not (org-insert-item)))
  6493. (let* ((empty-line-p nil)
  6494. (level nil)
  6495. (on-heading (org-at-heading-p))
  6496. (head (save-excursion
  6497. (condition-case nil
  6498. (progn
  6499. (org-back-to-heading invisible-ok)
  6500. (when (and (not on-heading)
  6501. (featurep 'org-inlinetask)
  6502. (integerp org-inlinetask-min-level)
  6503. (>= (length (match-string 0))
  6504. org-inlinetask-min-level))
  6505. ;; Find a heading level before the inline task
  6506. (while (and (setq level (org-up-heading-safe))
  6507. (>= level org-inlinetask-min-level)))
  6508. (if (org-at-heading-p)
  6509. (org-back-to-heading invisible-ok)
  6510. (error "This should not happen")))
  6511. (setq empty-line-p (org-previous-line-empty-p))
  6512. (match-string 0))
  6513. (error "*"))))
  6514. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6515. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6516. pos hide-previous previous-pos)
  6517. (cond
  6518. ((and (org-at-heading-p) (bolp)
  6519. (or (bobp)
  6520. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6521. ;; insert before the current line
  6522. (open-line (if blank 2 1)))
  6523. ((and (bolp)
  6524. (not org-insert-heading-respect-content)
  6525. (or (bobp)
  6526. (save-excursion
  6527. (backward-char 1) (not (outline-invisible-p)))))
  6528. ;; insert right here
  6529. nil)
  6530. (t
  6531. ;; somewhere in the line
  6532. (save-excursion
  6533. (setq previous-pos (point-at-bol))
  6534. (end-of-line)
  6535. (setq hide-previous (outline-invisible-p)))
  6536. (and org-insert-heading-respect-content (org-show-subtree))
  6537. (let ((split
  6538. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6539. (save-excursion
  6540. (let ((p (point)))
  6541. (goto-char (point-at-bol))
  6542. (and (looking-at org-complex-heading-regexp)
  6543. (match-beginning 4)
  6544. (> p (match-beginning 4)))))))
  6545. tags pos)
  6546. (cond
  6547. (org-insert-heading-respect-content
  6548. (if (not (equal force-heading '(16)))
  6549. (org-end-of-subtree nil t)
  6550. (org-up-heading-safe)
  6551. (org-end-of-subtree nil t))
  6552. (when (featurep 'org-inlinetask)
  6553. (while (and (not (eobp))
  6554. (looking-at "\\(\\*+\\)[ \t]+")
  6555. (>= (length (match-string 1))
  6556. org-inlinetask-min-level))
  6557. (org-end-of-subtree nil t)))
  6558. (or (bolp) (newline))
  6559. (or (org-previous-line-empty-p)
  6560. (and blank (newline)))
  6561. (open-line 1))
  6562. ((org-at-heading-p)
  6563. (when hide-previous
  6564. (show-children)
  6565. (org-show-entry))
  6566. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6567. (setq tags (and (match-end 2) (match-string 2)))
  6568. (and (match-end 1)
  6569. (delete-region (match-beginning 1) (match-end 1)))
  6570. (setq pos (point-at-bol))
  6571. (or split (end-of-line 1))
  6572. (delete-horizontal-space)
  6573. (if (string-match "\\`\\*+\\'"
  6574. (buffer-substring (point-at-bol) (point)))
  6575. (insert " "))
  6576. (newline (if blank 2 1))
  6577. (when tags
  6578. (save-excursion
  6579. (goto-char pos)
  6580. (end-of-line 1)
  6581. (insert " " tags)
  6582. (org-set-tags nil 'align))))
  6583. (t
  6584. (or split (end-of-line 1))
  6585. (newline (if blank 2 1)))))))
  6586. (insert head) (just-one-space)
  6587. (setq pos (point))
  6588. (end-of-line 1)
  6589. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6590. (when (and org-insert-heading-respect-content hide-previous)
  6591. (save-excursion
  6592. (goto-char previous-pos)
  6593. (hide-subtree)))
  6594. (run-hooks 'org-insert-heading-hook)))))
  6595. (defun org-get-heading (&optional no-tags no-todo)
  6596. "Return the heading of the current entry, without the stars.
  6597. When NO-TAGS is non-nil, don't include tags.
  6598. When NO-TODO is non-nil, don't include TODO keywords."
  6599. (save-excursion
  6600. (org-back-to-heading t)
  6601. (cond
  6602. ((and no-tags no-todo)
  6603. (looking-at org-complex-heading-regexp)
  6604. (match-string 4))
  6605. (no-tags
  6606. (looking-at (concat org-outline-regexp
  6607. "\\(.*?\\)"
  6608. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6609. (match-string 1))
  6610. (no-todo
  6611. (looking-at org-todo-line-regexp)
  6612. (match-string 3))
  6613. (t (looking-at org-heading-regexp)
  6614. (match-string 2)))))
  6615. (defun org-heading-components ()
  6616. "Return the components of the current heading.
  6617. This is a list with the following elements:
  6618. - the level as an integer
  6619. - the reduced level, different if `org-odd-levels-only' is set.
  6620. - the TODO keyword, or nil
  6621. - the priority character, like ?A, or nil if no priority is given
  6622. - the headline text itself, or the tags string if no headline text
  6623. - the tags string, or nil."
  6624. (save-excursion
  6625. (org-back-to-heading t)
  6626. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6627. (list (length (match-string 1))
  6628. (org-reduced-level (length (match-string 1)))
  6629. (org-match-string-no-properties 2)
  6630. (and (match-end 3) (aref (match-string 3) 2))
  6631. (org-match-string-no-properties 4)
  6632. (org-match-string-no-properties 5)))))
  6633. (defun org-get-entry ()
  6634. "Get the entry text, after heading, entire subtree."
  6635. (save-excursion
  6636. (org-back-to-heading t)
  6637. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6638. (defun org-insert-heading-after-current ()
  6639. "Insert a new heading with same level as current, after current subtree."
  6640. (interactive)
  6641. (org-back-to-heading)
  6642. (org-insert-heading)
  6643. (org-move-subtree-down)
  6644. (end-of-line 1))
  6645. (defun org-insert-heading-respect-content ()
  6646. (interactive)
  6647. (let ((org-insert-heading-respect-content t))
  6648. (org-insert-heading t)))
  6649. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6650. (interactive "P")
  6651. (let ((org-insert-heading-respect-content t))
  6652. (org-insert-todo-heading force-state t)))
  6653. (defun org-insert-todo-heading (arg &optional force-heading)
  6654. "Insert a new heading with the same level and TODO state as current heading.
  6655. If the heading has no TODO state, or if the state is DONE, use the first
  6656. state (TODO by default). Also one prefix arg, force first state. With two
  6657. prefix args, force inserting at the end of the parent subtree."
  6658. (interactive "P")
  6659. (when (or force-heading (not (org-insert-item 'checkbox)))
  6660. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6661. force-heading))
  6662. (save-excursion
  6663. (org-back-to-heading)
  6664. (outline-previous-heading)
  6665. (looking-at org-todo-line-regexp))
  6666. (let*
  6667. ((new-mark-x
  6668. (if (or arg
  6669. (not (match-beginning 2))
  6670. (member (match-string 2) org-done-keywords))
  6671. (car org-todo-keywords-1)
  6672. (match-string 2)))
  6673. (new-mark
  6674. (or
  6675. (run-hook-with-args-until-success
  6676. 'org-todo-get-default-hook new-mark-x nil)
  6677. new-mark-x)))
  6678. (beginning-of-line 1)
  6679. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6680. (if org-treat-insert-todo-heading-as-state-change
  6681. (org-todo new-mark)
  6682. (insert new-mark " "))))
  6683. (when org-provide-todo-statistics
  6684. (org-update-parent-todo-statistics))))
  6685. (defun org-insert-subheading (arg)
  6686. "Insert a new subheading and demote it.
  6687. Works for outline headings and for plain lists alike."
  6688. (interactive "P")
  6689. (org-insert-heading arg)
  6690. (cond
  6691. ((org-at-heading-p) (org-do-demote))
  6692. ((org-at-item-p) (org-indent-item))))
  6693. (defun org-insert-todo-subheading (arg)
  6694. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6695. Works for outline headings and for plain lists alike."
  6696. (interactive "P")
  6697. (org-insert-todo-heading arg)
  6698. (cond
  6699. ((org-at-heading-p) (org-do-demote))
  6700. ((org-at-item-p) (org-indent-item))))
  6701. ;;; Promotion and Demotion
  6702. (defvar org-after-demote-entry-hook nil
  6703. "Hook run after an entry has been demoted.
  6704. The cursor will be at the beginning of the entry.
  6705. When a subtree is being demoted, the hook will be called for each node.")
  6706. (defvar org-after-promote-entry-hook nil
  6707. "Hook run after an entry has been promoted.
  6708. The cursor will be at the beginning of the entry.
  6709. When a subtree is being promoted, the hook will be called for each node.")
  6710. (defun org-promote-subtree ()
  6711. "Promote the entire subtree.
  6712. See also `org-promote'."
  6713. (interactive)
  6714. (save-excursion
  6715. (org-with-limited-levels (org-map-tree 'org-promote)))
  6716. (org-fix-position-after-promote))
  6717. (defun org-demote-subtree ()
  6718. "Demote the entire subtree. See `org-demote'.
  6719. See also `org-promote'."
  6720. (interactive)
  6721. (save-excursion
  6722. (org-with-limited-levels (org-map-tree 'org-demote)))
  6723. (org-fix-position-after-promote))
  6724. (defun org-do-promote ()
  6725. "Promote the current heading higher up the tree.
  6726. If the region is active in `transient-mark-mode', promote all headings
  6727. in the region."
  6728. (interactive)
  6729. (save-excursion
  6730. (if (org-region-active-p)
  6731. (org-map-region 'org-promote (region-beginning) (region-end))
  6732. (org-promote)))
  6733. (org-fix-position-after-promote))
  6734. (defun org-do-demote ()
  6735. "Demote the current heading lower down the tree.
  6736. If the region is active in `transient-mark-mode', demote all headings
  6737. in the region."
  6738. (interactive)
  6739. (save-excursion
  6740. (if (org-region-active-p)
  6741. (org-map-region 'org-demote (region-beginning) (region-end))
  6742. (org-demote)))
  6743. (org-fix-position-after-promote))
  6744. (defun org-fix-position-after-promote ()
  6745. "Make sure that after pro/demotion cursor position is right."
  6746. (let ((pos (point)))
  6747. (when (save-excursion
  6748. (beginning-of-line 1)
  6749. (looking-at org-todo-line-regexp)
  6750. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6751. (cond ((eobp) (insert " "))
  6752. ((eolp) (insert " "))
  6753. ((equal (char-after) ?\ ) (forward-char 1))))))
  6754. (defun org-current-level ()
  6755. "Return the level of the current entry, or nil if before the first headline.
  6756. The level is the number of stars at the beginning of the headline."
  6757. (save-excursion
  6758. (org-with-limited-levels
  6759. (if (ignore-errors (org-back-to-heading t))
  6760. (funcall outline-level)))))
  6761. (defun org-get-previous-line-level ()
  6762. "Return the outline depth of the last headline before the current line.
  6763. Returns 0 for the first headline in the buffer, and nil if before the
  6764. first headline."
  6765. (let ((current-level (org-current-level))
  6766. (prev-level (when (> (line-number-at-pos) 1)
  6767. (save-excursion
  6768. (beginning-of-line 0)
  6769. (org-current-level)))))
  6770. (cond ((null current-level) nil) ; Before first headline
  6771. ((null prev-level) 0) ; At first headline
  6772. (prev-level))))
  6773. (defun org-reduced-level (l)
  6774. "Compute the effective level of a heading.
  6775. This takes into account the setting of `org-odd-levels-only'."
  6776. (cond
  6777. ((zerop l) 0)
  6778. (org-odd-levels-only (1+ (floor (/ l 2))))
  6779. (t l)))
  6780. (defun org-level-increment ()
  6781. "Return the number of stars that will be added or removed at a
  6782. time to headlines when structure editing, based on the value of
  6783. `org-odd-levels-only'."
  6784. (if org-odd-levels-only 2 1))
  6785. (defun org-get-valid-level (level &optional change)
  6786. "Rectify a level change under the influence of `org-odd-levels-only'
  6787. LEVEL is a current level, CHANGE is by how much the level should be
  6788. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6789. even level numbers will become the next higher odd number."
  6790. (if org-odd-levels-only
  6791. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6792. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6793. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6794. (max 1 (+ level (or change 0)))))
  6795. (org-define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  6796. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6797. ;; ̀org-with-limited-levels'
  6798. (defun org-promote ()
  6799. "Promote the current heading higher up the tree.
  6800. If the region is active in `transient-mark-mode', promote all headings
  6801. in the region."
  6802. (org-back-to-heading t)
  6803. (let* ((level (save-match-data (funcall outline-level)))
  6804. (after-change-functions (remove 'flyspell-after-change-function
  6805. after-change-functions))
  6806. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6807. (diff (abs (- level (length up-head) -1))))
  6808. (cond ((and (= level 1) org-called-with-limited-levels
  6809. org-allow-promoting-top-level-subtree)
  6810. (replace-match "# " nil t))
  6811. ((= level 1)
  6812. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6813. (t (replace-match up-head nil t)))
  6814. ;; Fixup tag positioning
  6815. (unless (= level 1)
  6816. (and org-auto-align-tags (org-set-tags nil t))
  6817. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6818. (run-hooks 'org-after-promote-entry-hook)))
  6819. (defun org-demote ()
  6820. "Demote the current heading lower down the tree.
  6821. If the region is active in `transient-mark-mode', demote all headings
  6822. in the region."
  6823. (org-back-to-heading t)
  6824. (let* ((level (save-match-data (funcall outline-level)))
  6825. (after-change-functions (remove 'flyspell-after-change-function
  6826. after-change-functions))
  6827. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6828. (diff (abs (- level (length down-head) -1))))
  6829. (replace-match down-head nil t)
  6830. ;; Fixup tag positioning
  6831. (and org-auto-align-tags (org-set-tags nil t))
  6832. (if org-adapt-indentation (org-fixup-indentation diff))
  6833. (run-hooks 'org-after-demote-entry-hook)))
  6834. (defun org-cycle-level ()
  6835. "Cycle the level of an empty headline through possible states.
  6836. This goes first to child, then to parent, level, then up the hierarchy.
  6837. After top level, it switches back to sibling level."
  6838. (interactive)
  6839. (let ((org-adapt-indentation nil))
  6840. (when (org-point-at-end-of-empty-headline)
  6841. (setq this-command 'org-cycle-level) ; Only needed for caching
  6842. (let ((cur-level (org-current-level))
  6843. (prev-level (org-get-previous-line-level)))
  6844. (cond
  6845. ;; If first headline in file, promote to top-level.
  6846. ((= prev-level 0)
  6847. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6848. do (org-do-promote)))
  6849. ;; If same level as prev, demote one.
  6850. ((= prev-level cur-level)
  6851. (org-do-demote))
  6852. ;; If parent is top-level, promote to top level if not already.
  6853. ((= prev-level 1)
  6854. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6855. do (org-do-promote)))
  6856. ;; If top-level, return to prev-level.
  6857. ((= cur-level 1)
  6858. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6859. do (org-do-demote)))
  6860. ;; If less than prev-level, promote one.
  6861. ((< cur-level prev-level)
  6862. (org-do-promote))
  6863. ;; If deeper than prev-level, promote until higher than
  6864. ;; prev-level.
  6865. ((> cur-level prev-level)
  6866. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6867. do (org-do-promote))))
  6868. t))))
  6869. (defun org-map-tree (fun)
  6870. "Call FUN for every heading underneath the current one."
  6871. (org-back-to-heading)
  6872. (let ((level (funcall outline-level)))
  6873. (save-excursion
  6874. (funcall fun)
  6875. (while (and (progn
  6876. (outline-next-heading)
  6877. (> (funcall outline-level) level))
  6878. (not (eobp)))
  6879. (funcall fun)))))
  6880. (defun org-map-region (fun beg end)
  6881. "Call FUN for every heading between BEG and END."
  6882. (let ((org-ignore-region t))
  6883. (save-excursion
  6884. (setq end (copy-marker end))
  6885. (goto-char beg)
  6886. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6887. (< (point) end))
  6888. (funcall fun))
  6889. (while (and (progn
  6890. (outline-next-heading)
  6891. (< (point) end))
  6892. (not (eobp)))
  6893. (funcall fun)))))
  6894. (defvar org-property-end-re) ; silence byte-compiler
  6895. (defun org-fixup-indentation (diff)
  6896. "Change the indentation in the current entry by DIFF.
  6897. However, if any line in the current entry has no indentation, or if it
  6898. would end up with no indentation after the change, nothing at all is done."
  6899. (save-excursion
  6900. (let ((end (save-excursion (outline-next-heading)
  6901. (point-marker)))
  6902. (prohibit (if (> diff 0)
  6903. "^\\S-"
  6904. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6905. col)
  6906. (unless (save-excursion (end-of-line 1)
  6907. (re-search-forward prohibit end t))
  6908. (while (and (< (point) end)
  6909. (re-search-forward "^[ \t]+" end t))
  6910. (goto-char (match-end 0))
  6911. (setq col (current-column))
  6912. (if (< diff 0) (replace-match ""))
  6913. (org-indent-to-column (+ diff col))))
  6914. (move-marker end nil))))
  6915. (defun org-convert-to-odd-levels ()
  6916. "Convert an org-mode file with all levels allowed to one with odd levels.
  6917. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6918. level 5 etc."
  6919. (interactive)
  6920. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6921. (let ((outline-level 'org-outline-level)
  6922. (org-odd-levels-only nil) n)
  6923. (save-excursion
  6924. (goto-char (point-min))
  6925. (while (re-search-forward "^\\*\\*+ " nil t)
  6926. (setq n (- (length (match-string 0)) 2))
  6927. (while (>= (setq n (1- n)) 0)
  6928. (org-demote))
  6929. (end-of-line 1))))))
  6930. (defun org-convert-to-oddeven-levels ()
  6931. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6932. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6933. file contains a section with an even level, conversion would
  6934. destroy the structure of the file. An error is signaled in this
  6935. case."
  6936. (interactive)
  6937. (goto-char (point-min))
  6938. ;; First check if there are no even levels
  6939. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6940. (org-show-context t)
  6941. (error "Not all levels are odd in this file. Conversion not possible"))
  6942. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6943. (let ((outline-regexp org-outline-regexp)
  6944. (outline-level 'org-outline-level)
  6945. (org-odd-levels-only nil) n)
  6946. (save-excursion
  6947. (goto-char (point-min))
  6948. (while (re-search-forward "^\\*\\*+ " nil t)
  6949. (setq n (/ (1- (length (match-string 0))) 2))
  6950. (while (>= (setq n (1- n)) 0)
  6951. (org-promote))
  6952. (end-of-line 1))))))
  6953. (defun org-tr-level (n)
  6954. "Make N odd if required."
  6955. (if org-odd-levels-only (1+ (/ n 2)) n))
  6956. ;;; Vertical tree motion, cutting and pasting of subtrees
  6957. (defun org-move-subtree-up (&optional arg)
  6958. "Move the current subtree up past ARG headlines of the same level."
  6959. (interactive "p")
  6960. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6961. (defun org-move-subtree-down (&optional arg)
  6962. "Move the current subtree down past ARG headlines of the same level."
  6963. (interactive "p")
  6964. (setq arg (prefix-numeric-value arg))
  6965. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6966. 'org-get-last-sibling))
  6967. (ins-point (make-marker))
  6968. (cnt (abs arg))
  6969. (col (current-column))
  6970. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6971. ;; Select the tree
  6972. (org-back-to-heading)
  6973. (setq beg0 (point))
  6974. (save-excursion
  6975. (setq ne-beg (org-back-over-empty-lines))
  6976. (setq beg (point)))
  6977. (save-match-data
  6978. (save-excursion (outline-end-of-heading)
  6979. (setq folded (outline-invisible-p)))
  6980. (outline-end-of-subtree))
  6981. (outline-next-heading)
  6982. (setq ne-end (org-back-over-empty-lines))
  6983. (setq end (point))
  6984. (goto-char beg0)
  6985. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6986. ;; include less whitespace
  6987. (save-excursion
  6988. (goto-char beg)
  6989. (forward-line (- ne-beg ne-end))
  6990. (setq beg (point))))
  6991. ;; Find insertion point, with error handling
  6992. (while (> cnt 0)
  6993. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6994. (progn (goto-char beg0)
  6995. (error "Cannot move past superior level or buffer limit")))
  6996. (setq cnt (1- cnt)))
  6997. (if (> arg 0)
  6998. ;; Moving forward - still need to move over subtree
  6999. (progn (org-end-of-subtree t t)
  7000. (save-excursion
  7001. (org-back-over-empty-lines)
  7002. (or (bolp) (newline)))))
  7003. (setq ne-ins (org-back-over-empty-lines))
  7004. (move-marker ins-point (point))
  7005. (setq txt (buffer-substring beg end))
  7006. (org-save-markers-in-region beg end)
  7007. (delete-region beg end)
  7008. (org-remove-empty-overlays-at beg)
  7009. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7010. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7011. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7012. (let ((bbb (point)))
  7013. (insert-before-markers txt)
  7014. (org-reinstall-markers-in-region bbb)
  7015. (move-marker ins-point bbb))
  7016. (or (bolp) (insert "\n"))
  7017. (setq ins-end (point))
  7018. (goto-char ins-point)
  7019. (org-skip-whitespace)
  7020. (when (and (< arg 0)
  7021. (org-first-sibling-p)
  7022. (> ne-ins ne-beg))
  7023. ;; Move whitespace back to beginning
  7024. (save-excursion
  7025. (goto-char ins-end)
  7026. (let ((kill-whole-line t))
  7027. (kill-line (- ne-ins ne-beg)) (point)))
  7028. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7029. (move-marker ins-point nil)
  7030. (if folded
  7031. (hide-subtree)
  7032. (org-show-entry)
  7033. (show-children)
  7034. (org-cycle-hide-drawers 'children))
  7035. (org-clean-visibility-after-subtree-move)
  7036. ;; move back to the initial column we were at
  7037. (move-to-column col)))
  7038. (defvar org-subtree-clip ""
  7039. "Clipboard for cut and paste of subtrees.
  7040. This is actually only a copy of the kill, because we use the normal kill
  7041. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7042. (defvar org-subtree-clip-folded nil
  7043. "Was the last copied subtree folded?
  7044. This is used to fold the tree back after pasting.")
  7045. (defun org-cut-subtree (&optional n)
  7046. "Cut the current subtree into the clipboard.
  7047. With prefix arg N, cut this many sequential subtrees.
  7048. This is a short-hand for marking the subtree and then cutting it."
  7049. (interactive "p")
  7050. (org-copy-subtree n 'cut))
  7051. (defun org-copy-subtree (&optional n cut force-store-markers)
  7052. "Cut the current subtree into the clipboard.
  7053. With prefix arg N, cut this many sequential subtrees.
  7054. This is a short-hand for marking the subtree and then copying it.
  7055. If CUT is non-nil, actually cut the subtree.
  7056. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7057. of some markers in the region, even if CUT is non-nil. This is
  7058. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7059. (interactive "p")
  7060. (let (beg end folded (beg0 (point)))
  7061. (if (org-called-interactively-p 'any)
  7062. (org-back-to-heading nil) ; take what looks like a subtree
  7063. (org-back-to-heading t)) ; take what is really there
  7064. (setq beg (point))
  7065. (skip-chars-forward " \t\r\n")
  7066. (save-match-data
  7067. (save-excursion (outline-end-of-heading)
  7068. (setq folded (outline-invisible-p)))
  7069. (condition-case nil
  7070. (org-forward-heading-same-level (1- n) t)
  7071. (error nil))
  7072. (org-end-of-subtree t t))
  7073. (setq end (point))
  7074. (goto-char beg0)
  7075. (when (> end beg)
  7076. (setq org-subtree-clip-folded folded)
  7077. (when (or cut force-store-markers)
  7078. (org-save-markers-in-region beg end))
  7079. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7080. (setq org-subtree-clip (current-kill 0))
  7081. (message "%s: Subtree(s) with %d characters"
  7082. (if cut "Cut" "Copied")
  7083. (length org-subtree-clip)))))
  7084. (defun org-paste-subtree (&optional level tree for-yank)
  7085. "Paste the clipboard as a subtree, with modification of headline level.
  7086. The entire subtree is promoted or demoted in order to match a new headline
  7087. level.
  7088. If the cursor is at the beginning of a headline, the same level as
  7089. that headline is used to paste the tree
  7090. If not, the new level is derived from the *visible* headings
  7091. before and after the insertion point, and taken to be the inferior headline
  7092. level of the two. So if the previous visible heading is level 3 and the
  7093. next is level 4 (or vice versa), level 4 will be used for insertion.
  7094. This makes sure that the subtree remains an independent subtree and does
  7095. not swallow low level entries.
  7096. You can also force a different level, either by using a numeric prefix
  7097. argument, or by inserting the heading marker by hand. For example, if the
  7098. cursor is after \"*****\", then the tree will be shifted to level 5.
  7099. If optional TREE is given, use this text instead of the kill ring.
  7100. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7101. move back over whitespace before inserting, and move point to the end of
  7102. the inserted text when done."
  7103. (interactive "P")
  7104. (setq tree (or tree (and kill-ring (current-kill 0))))
  7105. (unless (org-kill-is-subtree-p tree)
  7106. (error "%s"
  7107. (substitute-command-keys
  7108. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7109. (org-with-limited-levels
  7110. (let* ((visp (not (outline-invisible-p)))
  7111. (txt tree)
  7112. (^re_ "\\(\\*+\\)[ \t]*")
  7113. (old-level (if (string-match org-outline-regexp-bol txt)
  7114. (- (match-end 0) (match-beginning 0) 1)
  7115. -1))
  7116. (force-level (cond (level (prefix-numeric-value level))
  7117. ((and (looking-at "[ \t]*$")
  7118. (string-match
  7119. "^\\*+$" (buffer-substring
  7120. (point-at-bol) (point))))
  7121. (- (match-end 1) (match-beginning 1)))
  7122. ((and (bolp)
  7123. (looking-at org-outline-regexp))
  7124. (- (match-end 0) (point) 1))))
  7125. (previous-level (save-excursion
  7126. (condition-case nil
  7127. (progn
  7128. (outline-previous-visible-heading 1)
  7129. (if (looking-at ^re_)
  7130. (- (match-end 0) (match-beginning 0) 1)
  7131. 1))
  7132. (error 1))))
  7133. (next-level (save-excursion
  7134. (condition-case nil
  7135. (progn
  7136. (or (looking-at org-outline-regexp)
  7137. (outline-next-visible-heading 1))
  7138. (if (looking-at ^re_)
  7139. (- (match-end 0) (match-beginning 0) 1)
  7140. 1))
  7141. (error 1))))
  7142. (new-level (or force-level (max previous-level next-level)))
  7143. (shift (if (or (= old-level -1)
  7144. (= new-level -1)
  7145. (= old-level new-level))
  7146. 0
  7147. (- new-level old-level)))
  7148. (delta (if (> shift 0) -1 1))
  7149. (func (if (> shift 0) 'org-demote 'org-promote))
  7150. (org-odd-levels-only nil)
  7151. beg end newend)
  7152. ;; Remove the forced level indicator
  7153. (if force-level
  7154. (delete-region (point-at-bol) (point)))
  7155. ;; Paste
  7156. (beginning-of-line (if (bolp) 1 2))
  7157. (setq beg (point))
  7158. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7159. (insert-before-markers txt)
  7160. (unless (string-match "\n\\'" txt) (insert "\n"))
  7161. (setq newend (point))
  7162. (org-reinstall-markers-in-region beg)
  7163. (setq end (point))
  7164. (goto-char beg)
  7165. (skip-chars-forward " \t\n\r")
  7166. (setq beg (point))
  7167. (if (and (outline-invisible-p) visp)
  7168. (save-excursion (outline-show-heading)))
  7169. ;; Shift if necessary
  7170. (unless (= shift 0)
  7171. (save-restriction
  7172. (narrow-to-region beg end)
  7173. (while (not (= shift 0))
  7174. (org-map-region func (point-min) (point-max))
  7175. (setq shift (+ delta shift)))
  7176. (goto-char (point-min))
  7177. (setq newend (point-max))))
  7178. (when (or (org-called-interactively-p 'interactive) for-yank)
  7179. (message "Clipboard pasted as level %d subtree" new-level))
  7180. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7181. kill-ring
  7182. (eq org-subtree-clip (current-kill 0))
  7183. org-subtree-clip-folded)
  7184. ;; The tree was folded before it was killed/copied
  7185. (hide-subtree))
  7186. (and for-yank (goto-char newend)))))
  7187. (defun org-kill-is-subtree-p (&optional txt)
  7188. "Check if the current kill is an outline subtree, or a set of trees.
  7189. Returns nil if kill does not start with a headline, or if the first
  7190. headline level is not the largest headline level in the tree.
  7191. So this will actually accept several entries of equal levels as well,
  7192. which is OK for `org-paste-subtree'.
  7193. If optional TXT is given, check this string instead of the current kill."
  7194. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7195. (re (org-get-limited-outline-regexp))
  7196. (^re (concat "^" re))
  7197. (start-level (and kill
  7198. (string-match
  7199. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7200. kill)
  7201. (- (match-end 2) (match-beginning 2) 1)))
  7202. (start (1+ (or (match-beginning 2) -1))))
  7203. (if (not start-level)
  7204. (progn
  7205. nil) ;; does not even start with a heading
  7206. (catch 'exit
  7207. (while (setq start (string-match ^re kill (1+ start)))
  7208. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7209. (throw 'exit nil)))
  7210. t))))
  7211. (defvar org-markers-to-move nil
  7212. "Markers that should be moved with a cut-and-paste operation.
  7213. Those markers are stored together with their positions relative to
  7214. the start of the region.")
  7215. (defun org-save-markers-in-region (beg end)
  7216. "Check markers in region.
  7217. If these markers are between BEG and END, record their position relative
  7218. to BEG, so that after moving the block of text, we can put the markers back
  7219. into place.
  7220. This function gets called just before an entry or tree gets cut from the
  7221. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7222. called immediately, to move the markers with the entries."
  7223. (setq org-markers-to-move nil)
  7224. (when (featurep 'org-clock)
  7225. (org-clock-save-markers-for-cut-and-paste beg end))
  7226. (when (featurep 'org-agenda)
  7227. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7228. (defun org-check-and-save-marker (marker beg end)
  7229. "Check if MARKER is between BEG and END.
  7230. If yes, remember the marker and the distance to BEG."
  7231. (when (and (marker-buffer marker)
  7232. (equal (marker-buffer marker) (current-buffer)))
  7233. (if (and (>= marker beg) (< marker end))
  7234. (push (cons marker (- marker beg)) org-markers-to-move))))
  7235. (defun org-reinstall-markers-in-region (beg)
  7236. "Move all remembered markers to their position relative to BEG."
  7237. (mapc (lambda (x)
  7238. (move-marker (car x) (+ beg (cdr x))))
  7239. org-markers-to-move)
  7240. (setq org-markers-to-move nil))
  7241. (defun org-narrow-to-subtree ()
  7242. "Narrow buffer to the current subtree."
  7243. (interactive)
  7244. (save-excursion
  7245. (save-match-data
  7246. (org-with-limited-levels
  7247. (narrow-to-region
  7248. (progn (org-back-to-heading t) (point))
  7249. (progn (org-end-of-subtree t t)
  7250. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7251. (point)))))))
  7252. (defun org-narrow-to-block ()
  7253. "Narrow buffer to the current block."
  7254. (interactive)
  7255. (let* ((case-fold-search t)
  7256. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7257. "^[ \t]*#\\+end_.*")))
  7258. (if blockp
  7259. (narrow-to-region (car blockp) (cdr blockp))
  7260. (error "Not in a block"))))
  7261. (eval-when-compile
  7262. (defvar org-property-drawer-re))
  7263. (defvar org-property-start-re) ;; defined below
  7264. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7265. "Clone the task (subtree) at point N times.
  7266. The clones will be inserted as siblings.
  7267. In interactive use, the user will be prompted for the number of
  7268. clones to be produced, and for a time SHIFT, which may be a
  7269. repeater as used in time stamps, for example `+3d'.
  7270. When a valid repeater is given and the entry contains any time
  7271. stamps, the clones will become a sequence in time, with time
  7272. stamps in the subtree shifted for each clone produced. If SHIFT
  7273. is nil or the empty string, time stamps will be left alone. The
  7274. ID property of the original subtree is removed.
  7275. If the original subtree did contain time stamps with a repeater,
  7276. the following will happen:
  7277. - the repeater will be removed in each clone
  7278. - an additional clone will be produced, with the current, unshifted
  7279. date(s) in the entry.
  7280. - the original entry will be placed *after* all the clones, with
  7281. repeater intact.
  7282. - the start days in the repeater in the original entry will be shifted
  7283. to past the last clone.
  7284. In this way you can spell out a number of instances of a repeating task,
  7285. and still retain the repeater to cover future instances of the task."
  7286. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7287. (let (beg end template task idprop
  7288. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7289. (drawer-re org-drawer-regexp))
  7290. (if (not (and (integerp n) (> n 0)))
  7291. (error "Invalid number of replications %s" n))
  7292. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7293. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7294. shift)))
  7295. (error "Invalid shift specification %s" shift))
  7296. (when doshift
  7297. (setq shift-n (string-to-number (match-string 1 shift))
  7298. shift-what (cdr (assoc (match-string 2 shift)
  7299. '(("d" . day) ("w" . week)
  7300. ("m" . month) ("y" . year))))))
  7301. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7302. (setq nmin 1 nmax n)
  7303. (org-back-to-heading t)
  7304. (setq beg (point))
  7305. (setq idprop (org-entry-get nil "ID"))
  7306. (org-end-of-subtree t t)
  7307. (or (bolp) (insert "\n"))
  7308. (setq end (point))
  7309. (setq template (buffer-substring beg end))
  7310. (when (and doshift
  7311. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7312. (delete-region beg end)
  7313. (setq end beg)
  7314. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7315. (goto-char end)
  7316. (loop for n from nmin to nmax do
  7317. ;; prepare clone
  7318. (with-temp-buffer
  7319. (insert template)
  7320. (org-mode)
  7321. (goto-char (point-min))
  7322. (org-show-subtree)
  7323. (and idprop (if org-clone-delete-id
  7324. (org-entry-delete nil "ID")
  7325. (org-id-get-create t)))
  7326. (unless (= n 0)
  7327. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7328. (kill-whole-line))
  7329. (goto-char (point-min))
  7330. (while (re-search-forward drawer-re nil t)
  7331. (mapc (lambda (d)
  7332. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7333. (goto-char (point-min))
  7334. (when doshift
  7335. (while (re-search-forward org-ts-regexp-both nil t)
  7336. (org-timestamp-change (* n shift-n) shift-what))
  7337. (unless (= n n-no-remove)
  7338. (goto-char (point-min))
  7339. (while (re-search-forward org-ts-regexp nil t)
  7340. (save-excursion
  7341. (goto-char (match-beginning 0))
  7342. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7343. (delete-region (match-beginning 1) (match-end 1)))))))
  7344. (setq task (buffer-string)))
  7345. (insert task))
  7346. (goto-char beg)))
  7347. ;;; Outline Sorting
  7348. (defun org-sort (with-case)
  7349. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7350. Optional argument WITH-CASE means sort case-sensitively."
  7351. (interactive "P")
  7352. (cond
  7353. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7354. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7355. (t
  7356. (org-call-with-arg 'org-sort-entries with-case))))
  7357. (defun org-sort-remove-invisible (s)
  7358. (remove-text-properties 0 (length s) org-rm-props s)
  7359. (while (string-match org-bracket-link-regexp s)
  7360. (setq s (replace-match (if (match-end 2)
  7361. (match-string 3 s)
  7362. (match-string 1 s)) t t s)))
  7363. s)
  7364. (defvar org-priority-regexp) ; defined later in the file
  7365. (defvar org-after-sorting-entries-or-items-hook nil
  7366. "Hook that is run after a bunch of entries or items have been sorted.
  7367. When children are sorted, the cursor is in the parent line when this
  7368. hook gets called. When a region or a plain list is sorted, the cursor
  7369. will be in the first entry of the sorted region/list.")
  7370. (defun org-sort-entries
  7371. (&optional with-case sorting-type getkey-func compare-func property)
  7372. "Sort entries on a certain level of an outline tree.
  7373. If there is an active region, the entries in the region are sorted.
  7374. Else, if the cursor is before the first entry, sort the top-level items.
  7375. Else, the children of the entry at point are sorted.
  7376. Sorting can be alphabetically, numerically, by date/time as given by
  7377. a time stamp, by a property or by priority.
  7378. The command prompts for the sorting type unless it has been given to the
  7379. function through the SORTING-TYPE argument, which needs to be a character,
  7380. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7381. precise meaning of each character:
  7382. n Numerically, by converting the beginning of the entry/item to a number.
  7383. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7384. o By order of TODO keywords.
  7385. t By date/time, either the first active time stamp in the entry, or, if
  7386. none exist, by the first inactive one.
  7387. s By the scheduled date/time.
  7388. d By deadline date/time.
  7389. c By creation time, which is assumed to be the first inactive time stamp
  7390. at the beginning of a line.
  7391. p By priority according to the cookie.
  7392. r By the value of a property.
  7393. Capital letters will reverse the sort order.
  7394. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7395. called with point at the beginning of the record. It must return either
  7396. a string or a number that should serve as the sorting key for that record.
  7397. Comparing entries ignores case by default. However, with an optional argument
  7398. WITH-CASE, the sorting considers case as well."
  7399. (interactive "P")
  7400. (let ((case-func (if with-case 'identity 'downcase))
  7401. (cmstr
  7402. ;; The clock marker is lost when using `sort-subr', let's
  7403. ;; store the clocking string.
  7404. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7405. (save-excursion
  7406. (goto-char org-clock-marker)
  7407. (looking-back "^.*") (match-string-no-properties 0))))
  7408. start beg end stars re re2
  7409. txt what tmp)
  7410. ;; Find beginning and end of region to sort
  7411. (cond
  7412. ((org-region-active-p)
  7413. ;; we will sort the region
  7414. (setq end (region-end)
  7415. what "region")
  7416. (goto-char (region-beginning))
  7417. (if (not (org-at-heading-p)) (outline-next-heading))
  7418. (setq start (point)))
  7419. ((or (org-at-heading-p)
  7420. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7421. ;; we will sort the children of the current headline
  7422. (org-back-to-heading)
  7423. (setq start (point)
  7424. end (progn (org-end-of-subtree t t)
  7425. (or (bolp) (insert "\n"))
  7426. (org-back-over-empty-lines)
  7427. (point))
  7428. what "children")
  7429. (goto-char start)
  7430. (show-subtree)
  7431. (outline-next-heading))
  7432. (t
  7433. ;; we will sort the top-level entries in this file
  7434. (goto-char (point-min))
  7435. (or (org-at-heading-p) (outline-next-heading))
  7436. (setq start (point))
  7437. (goto-char (point-max))
  7438. (beginning-of-line 1)
  7439. (when (looking-at ".*?\\S-")
  7440. ;; File ends in a non-white line
  7441. (end-of-line 1)
  7442. (insert "\n"))
  7443. (setq end (point-max))
  7444. (setq what "top-level")
  7445. (goto-char start)
  7446. (show-all)))
  7447. (setq beg (point))
  7448. (if (>= beg end) (error "Nothing to sort"))
  7449. (looking-at "\\(\\*+\\)")
  7450. (setq stars (match-string 1)
  7451. re (concat "^" (regexp-quote stars) " +")
  7452. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7453. txt (buffer-substring beg end))
  7454. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7455. (if (and (not (equal stars "*")) (string-match re2 txt))
  7456. (error "Region to sort contains a level above the first entry"))
  7457. (unless sorting-type
  7458. (message
  7459. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7460. [t]ime [s]cheduled [d]eadline [c]reated
  7461. A/N/P/R/O/F/T/S/D/C means reversed:"
  7462. what)
  7463. (setq sorting-type (read-char-exclusive))
  7464. (unless getkey-func
  7465. (and (= (downcase sorting-type) ?f)
  7466. (setq getkey-func
  7467. (org-icompleting-read "Sort using function: "
  7468. obarray 'fboundp t nil nil))
  7469. (setq getkey-func (intern getkey-func))))
  7470. (and (= (downcase sorting-type) ?r)
  7471. (not property)
  7472. (setq property
  7473. (org-icompleting-read "Property: "
  7474. (mapcar 'list (org-buffer-property-keys t))
  7475. nil t))))
  7476. (message "Sorting entries...")
  7477. (save-restriction
  7478. (narrow-to-region start end)
  7479. (let ((dcst (downcase sorting-type))
  7480. (case-fold-search nil)
  7481. (now (current-time)))
  7482. (sort-subr
  7483. (/= dcst sorting-type)
  7484. ;; This function moves to the beginning character of the "record" to
  7485. ;; be sorted.
  7486. (lambda nil
  7487. (if (re-search-forward re nil t)
  7488. (goto-char (match-beginning 0))
  7489. (goto-char (point-max))))
  7490. ;; This function moves to the last character of the "record" being
  7491. ;; sorted.
  7492. (lambda nil
  7493. (save-match-data
  7494. (condition-case nil
  7495. (outline-forward-same-level 1)
  7496. (error
  7497. (goto-char (point-max))))))
  7498. ;; This function returns the value that gets sorted against.
  7499. (lambda nil
  7500. (cond
  7501. ((= dcst ?n)
  7502. (if (looking-at org-complex-heading-regexp)
  7503. (string-to-number (match-string 4))
  7504. nil))
  7505. ((= dcst ?a)
  7506. (if (looking-at org-complex-heading-regexp)
  7507. (funcall case-func (match-string 4))
  7508. nil))
  7509. ((= dcst ?t)
  7510. (let ((end (save-excursion (outline-next-heading) (point))))
  7511. (if (or (re-search-forward org-ts-regexp end t)
  7512. (re-search-forward org-ts-regexp-both end t))
  7513. (org-time-string-to-seconds (match-string 0))
  7514. (org-float-time now))))
  7515. ((= dcst ?c)
  7516. (let ((end (save-excursion (outline-next-heading) (point))))
  7517. (if (re-search-forward
  7518. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7519. end t)
  7520. (org-time-string-to-seconds (match-string 0))
  7521. (org-float-time now))))
  7522. ((= dcst ?s)
  7523. (let ((end (save-excursion (outline-next-heading) (point))))
  7524. (if (re-search-forward org-scheduled-time-regexp end t)
  7525. (org-time-string-to-seconds (match-string 1))
  7526. (org-float-time now))))
  7527. ((= dcst ?d)
  7528. (let ((end (save-excursion (outline-next-heading) (point))))
  7529. (if (re-search-forward org-deadline-time-regexp end t)
  7530. (org-time-string-to-seconds (match-string 1))
  7531. (org-float-time now))))
  7532. ((= dcst ?p)
  7533. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7534. (string-to-char (match-string 2))
  7535. org-default-priority))
  7536. ((= dcst ?r)
  7537. (or (org-entry-get nil property) ""))
  7538. ((= dcst ?o)
  7539. (if (looking-at org-complex-heading-regexp)
  7540. (- 9999 (length (member (match-string 2)
  7541. org-todo-keywords-1)))))
  7542. ((= dcst ?f)
  7543. (if getkey-func
  7544. (progn
  7545. (setq tmp (funcall getkey-func))
  7546. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7547. tmp)
  7548. (error "Invalid key function `%s'" getkey-func)))
  7549. (t (error "Invalid sorting type `%c'" sorting-type))))
  7550. nil
  7551. (cond
  7552. ((= dcst ?a) 'string<)
  7553. ((= dcst ?f) compare-func)
  7554. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7555. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7556. ;; Reset the clock marker if needed
  7557. (when cmstr
  7558. (save-excursion
  7559. (goto-char start)
  7560. (search-forward cmstr nil t)
  7561. (move-marker org-clock-marker (point))))
  7562. (message "Sorting entries...done")))
  7563. (defun org-do-sort (table what &optional with-case sorting-type)
  7564. "Sort TABLE of WHAT according to SORTING-TYPE.
  7565. The user will be prompted for the SORTING-TYPE if the call to this
  7566. function does not specify it. WHAT is only for the prompt, to indicate
  7567. what is being sorted. The sorting key will be extracted from
  7568. the car of the elements of the table.
  7569. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7570. (unless sorting-type
  7571. (message
  7572. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7573. what)
  7574. (setq sorting-type (read-char-exclusive)))
  7575. (let ((dcst (downcase sorting-type))
  7576. extractfun comparefun)
  7577. ;; Define the appropriate functions
  7578. (cond
  7579. ((= dcst ?n)
  7580. (setq extractfun 'string-to-number
  7581. comparefun (if (= dcst sorting-type) '< '>)))
  7582. ((= dcst ?a)
  7583. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7584. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7585. comparefun (if (= dcst sorting-type)
  7586. 'string<
  7587. (lambda (a b) (and (not (string< a b))
  7588. (not (string= a b)))))))
  7589. ((= dcst ?t)
  7590. (setq extractfun
  7591. (lambda (x)
  7592. (if (or (string-match org-ts-regexp x)
  7593. (string-match org-ts-regexp-both x))
  7594. (org-float-time
  7595. (org-time-string-to-time (match-string 0 x)))
  7596. 0))
  7597. comparefun (if (= dcst sorting-type) '< '>)))
  7598. (t (error "Invalid sorting type `%c'" sorting-type)))
  7599. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7600. table)
  7601. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7602. ;;; The orgstruct minor mode
  7603. ;; Define a minor mode which can be used in other modes in order to
  7604. ;; integrate the org-mode structure editing commands.
  7605. ;; This is really a hack, because the org-mode structure commands use
  7606. ;; keys which normally belong to the major mode. Here is how it
  7607. ;; works: The minor mode defines all the keys necessary to operate the
  7608. ;; structure commands, but wraps the commands into a function which
  7609. ;; tests if the cursor is currently at a headline or a plain list
  7610. ;; item. If that is the case, the structure command is used,
  7611. ;; temporarily setting many Org-mode variables like regular
  7612. ;; expressions for filling etc. However, when any of those keys is
  7613. ;; used at a different location, function uses `key-binding' to look
  7614. ;; up if the key has an associated command in another currently active
  7615. ;; keymap (minor modes, major mode, global), and executes that
  7616. ;; command. There might be problems if any of the keys is otherwise
  7617. ;; used as a prefix key.
  7618. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7619. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7620. ;; addresses this by checking explicitly for both bindings.
  7621. (defvar orgstruct-mode-map (make-sparse-keymap)
  7622. "Keymap for the minor `orgstruct-mode'.")
  7623. (defvar org-local-vars nil
  7624. "List of local variables, for use by `orgstruct-mode'.")
  7625. ;;;###autoload
  7626. (define-minor-mode orgstruct-mode
  7627. "Toggle the minor mode `orgstruct-mode'.
  7628. This mode is for using Org-mode structure commands in other
  7629. modes. The following keys behave as if Org-mode were active, if
  7630. the cursor is on a headline, or on a plain list item (both as
  7631. defined by Org-mode).
  7632. M-up Move entry/item up
  7633. M-down Move entry/item down
  7634. M-left Promote
  7635. M-right Demote
  7636. M-S-up Move entry/item up
  7637. M-S-down Move entry/item down
  7638. M-S-left Promote subtree
  7639. M-S-right Demote subtree
  7640. M-q Fill paragraph and items like in Org-mode
  7641. C-c ^ Sort entries
  7642. C-c - Cycle list bullet
  7643. TAB Cycle item visibility
  7644. M-RET Insert new heading/item
  7645. S-M-RET Insert new TODO heading / Checkbox item
  7646. C-c C-c Set tags / toggle checkbox"
  7647. nil " OrgStruct" nil
  7648. (org-load-modules-maybe)
  7649. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7650. ;;;###autoload
  7651. (defun turn-on-orgstruct ()
  7652. "Unconditionally turn on `orgstruct-mode'."
  7653. (orgstruct-mode 1))
  7654. (defvar org-fb-vars nil)
  7655. (make-variable-buffer-local 'org-fb-vars)
  7656. (defun orgstruct++-mode (&optional arg)
  7657. "Toggle `orgstruct-mode', the enhanced version of it.
  7658. In addition to setting orgstruct-mode, this also exports all
  7659. indentation and autofilling variables from org-mode into the
  7660. buffer. It will also recognize item context in multiline items."
  7661. (interactive "P")
  7662. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7663. (if (< arg 1)
  7664. (progn (orgstruct-mode -1)
  7665. (mapc (lambda(v)
  7666. (org-set-local (car v)
  7667. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7668. org-fb-vars))
  7669. (orgstruct-mode 1)
  7670. (setq org-fb-vars nil)
  7671. (let (var val)
  7672. (mapc
  7673. (lambda (x)
  7674. (when (string-match
  7675. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7676. (symbol-name (car x)))
  7677. (setq var (car x) val (nth 1 x))
  7678. (push (list var `(quote ,(eval var))) org-fb-vars)
  7679. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7680. org-local-vars)
  7681. (org-set-local 'orgstruct-is-++ t))))
  7682. (defvar orgstruct-is-++ nil
  7683. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7684. (make-variable-buffer-local 'orgstruct-is-++)
  7685. ;;;###autoload
  7686. (defun turn-on-orgstruct++ ()
  7687. "Unconditionally turn on `orgstruct++-mode'."
  7688. (orgstruct++-mode 1))
  7689. (defun orgstruct-error ()
  7690. "Error when there is no default binding for a structure key."
  7691. (interactive)
  7692. (error "This key has no function outside structure elements"))
  7693. (defun orgstruct-setup ()
  7694. "Setup orgstruct keymaps."
  7695. (let ((nfunc 0)
  7696. (bindings
  7697. (list
  7698. '([(meta up)] org-metaup)
  7699. '([(meta down)] org-metadown)
  7700. '([(meta left)] org-metaleft)
  7701. '([(meta right)] org-metaright)
  7702. '([(meta shift up)] org-shiftmetaup)
  7703. '([(meta shift down)] org-shiftmetadown)
  7704. '([(meta shift left)] org-shiftmetaleft)
  7705. '([(meta shift right)] org-shiftmetaright)
  7706. '([?\e (up)] org-metaup)
  7707. '([?\e (down)] org-metadown)
  7708. '([?\e (left)] org-metaleft)
  7709. '([?\e (right)] org-metaright)
  7710. '([?\e (shift up)] org-shiftmetaup)
  7711. '([?\e (shift down)] org-shiftmetadown)
  7712. '([?\e (shift left)] org-shiftmetaleft)
  7713. '([?\e (shift right)] org-shiftmetaright)
  7714. '([(shift up)] org-shiftup)
  7715. '([(shift down)] org-shiftdown)
  7716. '([(shift left)] org-shiftleft)
  7717. '([(shift right)] org-shiftright)
  7718. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7719. '("\M-q" fill-paragraph)
  7720. '("\C-c^" org-sort)
  7721. '("\C-c-" org-cycle-list-bullet)))
  7722. elt key fun cmd)
  7723. (while (setq elt (pop bindings))
  7724. (setq nfunc (1+ nfunc))
  7725. (setq key (org-key (car elt))
  7726. fun (nth 1 elt)
  7727. cmd (orgstruct-make-binding fun nfunc key))
  7728. (org-defkey orgstruct-mode-map key cmd))
  7729. ;; Prevent an error for users who forgot to make autoloads
  7730. (require 'org-element)
  7731. ;; Special treatment needed for TAB and RET
  7732. (org-defkey orgstruct-mode-map [(tab)]
  7733. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7734. (org-defkey orgstruct-mode-map "\C-i"
  7735. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7736. (org-defkey orgstruct-mode-map "\M-\C-m"
  7737. (orgstruct-make-binding 'org-insert-heading 105
  7738. "\M-\C-m" [(meta return)]))
  7739. (org-defkey orgstruct-mode-map [(meta return)]
  7740. (orgstruct-make-binding 'org-insert-heading 106
  7741. [(meta return)] "\M-\C-m"))
  7742. (org-defkey orgstruct-mode-map [(shift meta return)]
  7743. (orgstruct-make-binding 'org-insert-todo-heading 107
  7744. [(meta return)] "\M-\C-m"))
  7745. (org-defkey orgstruct-mode-map "\e\C-m"
  7746. (orgstruct-make-binding 'org-insert-heading 108
  7747. "\e\C-m" [?\e (return)]))
  7748. (org-defkey orgstruct-mode-map [?\e (return)]
  7749. (orgstruct-make-binding 'org-insert-heading 109
  7750. [?\e (return)] "\e\C-m"))
  7751. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7752. (orgstruct-make-binding 'org-insert-todo-heading 110
  7753. [?\e (return)] "\e\C-m"))
  7754. (unless org-local-vars
  7755. (setq org-local-vars (org-get-local-variables)))
  7756. t))
  7757. (defun orgstruct-make-binding (fun n &rest keys)
  7758. "Create a function for binding in the structure minor mode.
  7759. FUN is the command to call inside a table. N is used to create a unique
  7760. command name. KEYS are keys that should be checked in for a command
  7761. to execute outside of tables."
  7762. (eval
  7763. (list 'defun
  7764. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7765. '(arg)
  7766. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7767. "Outside of structure, run the binding of `"
  7768. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7769. "'.")
  7770. '(interactive "p")
  7771. (list 'if
  7772. `(org-context-p 'headline 'item
  7773. (and orgstruct-is-++
  7774. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7775. 'item-body))
  7776. (list 'org-run-like-in-org-mode (list 'quote fun))
  7777. (list 'let '(orgstruct-mode)
  7778. (list 'call-interactively
  7779. (append '(or)
  7780. (mapcar (lambda (k)
  7781. (list 'key-binding k))
  7782. keys)
  7783. '('orgstruct-error))))))))
  7784. (defun org-contextualize-keys (alist contexts)
  7785. "Return valid elements in ALIST depending on CONTEXTS.
  7786. `org-agenda-custom-commands' or `org-capture-templates' are the
  7787. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7788. or `org-capture-templates-contexts' are the associated contexts
  7789. definitions."
  7790. (let ((contexts
  7791. ;; normalize contexts
  7792. (mapcar
  7793. (lambda(c) (cond ((listp (cadr c))
  7794. (list (car c) (car c) (cadr c)))
  7795. ((string= "" (cadr c))
  7796. (list (car c) (car c) (caddr c)))
  7797. (t c))) contexts))
  7798. (a alist) c r s)
  7799. ;; loop over all commands or templates
  7800. (while (setq c (pop a))
  7801. (let (vrules repl)
  7802. (cond
  7803. ((not (assoc (car c) contexts))
  7804. (push c r))
  7805. ((and (assoc (car c) contexts)
  7806. (setq vrules (org-contextualize-validate-key
  7807. (car c) contexts)))
  7808. (mapc (lambda (vr)
  7809. (when (not (equal (car vr) (cadr vr)))
  7810. (setq repl vr))) vrules)
  7811. (if (not repl) (push c r)
  7812. (push (cadr repl) s)
  7813. (push
  7814. (cons (car c)
  7815. (cdr (or (assoc (cadr repl) alist)
  7816. (error "Undefined key `%s' as contextual replacement for `%s'"
  7817. (cadr repl) (car c)))))
  7818. r))))))
  7819. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7820. (delq
  7821. nil
  7822. (delete-dups
  7823. (mapcar (lambda (x)
  7824. (let ((tpl (car x)))
  7825. (when (not (delq
  7826. nil
  7827. (mapcar (lambda(y)
  7828. (equal y tpl)) s))) x)))
  7829. (reverse r))))))
  7830. (defun org-contextualize-validate-key (key contexts)
  7831. "Check CONTEXTS for agenda or capture KEY."
  7832. (let (r rr res)
  7833. (while (setq r (pop contexts))
  7834. (mapc
  7835. (lambda (rr)
  7836. (when
  7837. (and (equal key (car r))
  7838. (if (functionp rr) (funcall rr)
  7839. (or (and (eq (car rr) 'in-file)
  7840. (buffer-file-name)
  7841. (string-match (cdr rr) (buffer-file-name)))
  7842. (and (eq (car rr) 'in-mode)
  7843. (string-match (cdr rr) (symbol-name major-mode)))
  7844. (when (and (eq (car rr) 'not-in-file)
  7845. (buffer-file-name))
  7846. (not (string-match (cdr rr) (buffer-file-name))))
  7847. (when (eq (car rr) 'not-in-mode)
  7848. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7849. (push r res)))
  7850. (car (last r))))
  7851. (delete-dups (delq nil res))))
  7852. (defun org-context-p (&rest contexts)
  7853. "Check if local context is any of CONTEXTS.
  7854. Possible values in the list of contexts are `table', `headline', and `item'."
  7855. (let ((pos (point)))
  7856. (goto-char (point-at-bol))
  7857. (prog1 (or (and (memq 'table contexts)
  7858. (looking-at "[ \t]*|"))
  7859. (and (memq 'headline contexts)
  7860. (looking-at org-outline-regexp))
  7861. (and (memq 'item contexts)
  7862. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7863. (and (memq 'item-body contexts)
  7864. (org-in-item-p)))
  7865. (goto-char pos))))
  7866. (defun org-get-local-variables ()
  7867. "Return a list of all local variables in an Org mode buffer."
  7868. (let (varlist)
  7869. (with-current-buffer (get-buffer-create "*Org tmp*")
  7870. (erase-buffer)
  7871. (org-mode)
  7872. (setq varlist (buffer-local-variables)))
  7873. (kill-buffer "*Org tmp*")
  7874. (delq nil
  7875. (mapcar
  7876. (lambda (x)
  7877. (setq x
  7878. (if (symbolp x)
  7879. (list x)
  7880. (list (car x) (list 'quote (cdr x)))))
  7881. (if (string-match
  7882. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7883. (symbol-name (car x)))
  7884. x nil))
  7885. varlist))))
  7886. (defun org-clone-local-variables (from-buffer &optional regexp)
  7887. "Clone local variables from FROM-BUFFER.
  7888. Optional argument REGEXP selects variables to clone."
  7889. (mapc
  7890. (lambda (pair)
  7891. (and (symbolp (car pair))
  7892. (or (null regexp)
  7893. (string-match regexp (symbol-name (car pair))))
  7894. (set (make-local-variable (car pair))
  7895. (cdr pair))))
  7896. (buffer-local-variables from-buffer)))
  7897. ;;;###autoload
  7898. (defun org-run-like-in-org-mode (cmd)
  7899. "Run a command, pretending that the current buffer is in Org-mode.
  7900. This will temporarily bind local variables that are typically bound in
  7901. Org-mode to the values they have in Org-mode, and then interactively
  7902. call CMD."
  7903. (org-load-modules-maybe)
  7904. (unless org-local-vars
  7905. (setq org-local-vars (org-get-local-variables)))
  7906. (eval (list 'let org-local-vars
  7907. (list 'call-interactively (list 'quote cmd)))))
  7908. ;;;; Archiving
  7909. (defun org-get-category (&optional pos force-refresh)
  7910. "Get the category applying to position POS."
  7911. (save-match-data
  7912. (if force-refresh (org-refresh-category-properties))
  7913. (let ((pos (or pos (point))))
  7914. (or (get-text-property pos 'org-category)
  7915. (progn (org-refresh-category-properties)
  7916. (get-text-property pos 'org-category))))))
  7917. (defun org-refresh-category-properties ()
  7918. "Refresh category text properties in the buffer."
  7919. (let ((case-fold-search t)
  7920. (inhibit-read-only t)
  7921. (def-cat (cond
  7922. ((null org-category)
  7923. (if buffer-file-name
  7924. (file-name-sans-extension
  7925. (file-name-nondirectory buffer-file-name))
  7926. "???"))
  7927. ((symbolp org-category) (symbol-name org-category))
  7928. (t org-category)))
  7929. beg end cat pos optionp)
  7930. (org-unmodified
  7931. (save-excursion
  7932. (save-restriction
  7933. (widen)
  7934. (goto-char (point-min))
  7935. (put-text-property (point) (point-max) 'org-category def-cat)
  7936. (while (re-search-forward
  7937. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7938. (setq pos (match-end 0)
  7939. optionp (equal (char-after (match-beginning 0)) ?#)
  7940. cat (org-trim (match-string 2)))
  7941. (if optionp
  7942. (setq beg (point-at-bol) end (point-max))
  7943. (org-back-to-heading t)
  7944. (setq beg (point) end (org-end-of-subtree t t)))
  7945. (put-text-property beg end 'org-category cat)
  7946. (put-text-property beg end 'org-category-position beg)
  7947. (goto-char pos)))))))
  7948. (defun org-refresh-properties (dprop tprop)
  7949. "Refresh buffer text properties.
  7950. DPROP is the drawer property and TPROP is the corresponding text
  7951. property to set."
  7952. (let ((case-fold-search t)
  7953. (inhibit-read-only t) p)
  7954. (org-unmodified
  7955. (save-excursion
  7956. (save-restriction
  7957. (widen)
  7958. (goto-char (point-min))
  7959. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  7960. (setq p (org-match-string-no-properties 1))
  7961. (save-excursion
  7962. (org-back-to-heading t)
  7963. (put-text-property
  7964. (point-at-bol) (point-at-eol) tprop p))))))))
  7965. ;;;; Link Stuff
  7966. ;;; Link abbreviations
  7967. (defun org-link-expand-abbrev (link)
  7968. "Apply replacements as defined in `org-link-abbrev-alist'."
  7969. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7970. (let* ((key (match-string 1 link))
  7971. (as (or (assoc key org-link-abbrev-alist-local)
  7972. (assoc key org-link-abbrev-alist)))
  7973. (tag (and (match-end 2) (match-string 3 link)))
  7974. rpl)
  7975. (if (not as)
  7976. link
  7977. (setq rpl (cdr as))
  7978. (cond
  7979. ((symbolp rpl) (funcall rpl tag))
  7980. ((string-match "%(\\([^)]+\\))" rpl)
  7981. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7982. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7983. ((string-match "%h" rpl)
  7984. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7985. (t (concat rpl tag)))))
  7986. link))
  7987. ;;; Storing and inserting links
  7988. (defvar org-insert-link-history nil
  7989. "Minibuffer history for links inserted with `org-insert-link'.")
  7990. (defvar org-stored-links nil
  7991. "Contains the links stored with `org-store-link'.")
  7992. (defvar org-store-link-plist nil
  7993. "Plist with info about the most recently link created with `org-store-link'.")
  7994. (defvar org-link-protocols nil
  7995. "Link protocols added to Org-mode using `org-add-link-type'.")
  7996. (defvar org-store-link-functions nil
  7997. "List of functions that are called to create and store a link.
  7998. Each function will be called in turn until one returns a non-nil
  7999. value. Each function should check if it is responsible for creating
  8000. this link (for example by looking at the major mode).
  8001. If not, it must exit and return nil.
  8002. If yes, it should return a non-nil value after a calling
  8003. `org-store-link-props' with a list of properties and values.
  8004. Special properties are:
  8005. :type The link prefix, like \"http\". This must be given.
  8006. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8007. This is obligatory as well.
  8008. :description Optional default description for the second pair
  8009. of brackets in an Org-mode link. The user can still change
  8010. this when inserting this link into an Org-mode buffer.
  8011. In addition to these, any additional properties can be specified
  8012. and then used in capture templates.")
  8013. (defun org-add-link-type (type &optional follow export)
  8014. "Add TYPE to the list of `org-link-types'.
  8015. Re-compute all regular expressions depending on `org-link-types'
  8016. FOLLOW and EXPORT are two functions.
  8017. FOLLOW should take the link path as the single argument and do whatever
  8018. is necessary to follow the link, for example find a file or display
  8019. a mail message.
  8020. EXPORT should format the link path for export to one of the export formats.
  8021. It should be a function accepting three arguments:
  8022. path the path of the link, the text after the prefix (like \"http:\")
  8023. desc the description of the link, if any, or a description added by
  8024. org-export-normalize-links if there is none
  8025. format the export format, a symbol like `html' or `latex' or `ascii'..
  8026. The function may use the FORMAT information to return different values
  8027. depending on the format. The return value will be put literally into
  8028. the exported file. If the return value is nil, this means Org should
  8029. do what it normally does with links which do not have EXPORT defined.
  8030. Org-mode has a built-in default for exporting links. If you are happy with
  8031. this default, there is no need to define an export function for the link
  8032. type. For a simple example of an export function, see `org-bbdb.el'."
  8033. (add-to-list 'org-link-types type t)
  8034. (org-make-link-regexps)
  8035. (if (assoc type org-link-protocols)
  8036. (setcdr (assoc type org-link-protocols) (list follow export))
  8037. (push (list type follow export) org-link-protocols)))
  8038. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8039. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8040. ;;;###autoload
  8041. (defun org-store-link (arg)
  8042. "\\<org-mode-map>Store an org-link to the current location.
  8043. This link is added to `org-stored-links' and can later be inserted
  8044. into an org-buffer with \\[org-insert-link].
  8045. For some link types, a prefix arg is interpreted.
  8046. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8047. For file links, arg negates `org-context-in-file-links'.
  8048. A double prefix arg force skipping storing functions that are not
  8049. part of Org's core."
  8050. (interactive "P")
  8051. (org-load-modules-maybe)
  8052. (setq org-store-link-plist nil) ; reset
  8053. (org-with-limited-levels
  8054. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8055. (cond
  8056. ((and (not (equal arg '(16)))
  8057. (setq sfuns
  8058. (delq
  8059. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8060. org-store-link-functions))
  8061. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8062. (or (and (cdr sfuns)
  8063. (funcall (intern
  8064. (completing-read "Which function for creating the link? "
  8065. sfunsn t (car sfunsn)))))
  8066. (funcall (caar sfuns)))
  8067. (setq link (plist-get org-store-link-plist :link)
  8068. desc (or (plist-get org-store-link-plist :description) link))))
  8069. ((org-src-edit-buffer-p)
  8070. (let (label gc)
  8071. (while (or (not label)
  8072. (save-excursion
  8073. (save-restriction
  8074. (widen)
  8075. (goto-char (point-min))
  8076. (re-search-forward
  8077. (regexp-quote (format org-coderef-label-format label))
  8078. nil t))))
  8079. (when label (message "Label exists already") (sit-for 2))
  8080. (setq label (read-string "Code line label: " label)))
  8081. (end-of-line 1)
  8082. (setq link (format org-coderef-label-format label))
  8083. (setq gc (- 79 (length link)))
  8084. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8085. (insert link)
  8086. (setq link (concat "(" label ")") desc nil)))
  8087. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8088. ;; We are in the agenda, link to referenced location
  8089. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8090. (get-text-property (point) 'org-marker))))
  8091. (when m
  8092. (org-with-point-at m
  8093. (setq agenda-link
  8094. (if (org-called-interactively-p 'any)
  8095. (call-interactively 'org-store-link)
  8096. (org-store-link nil)))))))
  8097. ((eq major-mode 'calendar-mode)
  8098. (let ((cd (calendar-cursor-to-date)))
  8099. (setq link
  8100. (format-time-string
  8101. (car org-time-stamp-formats)
  8102. (apply 'encode-time
  8103. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8104. nil nil nil))))
  8105. (org-store-link-props :type "calendar" :date cd)))
  8106. ((eq major-mode 'help-mode)
  8107. (setq link (concat "help:" (save-excursion
  8108. (goto-char (point-min))
  8109. (looking-at "^[^ ]+")
  8110. (match-string 0))))
  8111. (org-store-link-props :type "help"))
  8112. ((eq major-mode 'w3-mode)
  8113. (setq cpltxt (if (and (buffer-name)
  8114. (not (string-match "Untitled" (buffer-name))))
  8115. (buffer-name)
  8116. (url-view-url t))
  8117. link (url-view-url t))
  8118. (org-store-link-props :type "w3" :url (url-view-url t)))
  8119. ((setq search (run-hook-with-args-until-success
  8120. 'org-create-file-search-functions))
  8121. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8122. "::" search))
  8123. (setq cpltxt (or description link)))
  8124. ((eq major-mode 'image-mode)
  8125. (setq cpltxt (concat "file:"
  8126. (abbreviate-file-name buffer-file-name))
  8127. link cpltxt)
  8128. (org-store-link-props :type "image" :file buffer-file-name))
  8129. ((eq major-mode 'dired-mode)
  8130. ;; link to the file in the current line
  8131. (let ((file (dired-get-filename nil t)))
  8132. (setq file (if file
  8133. (abbreviate-file-name
  8134. (expand-file-name (dired-get-filename nil t)))
  8135. ;; otherwise, no file so use current directory.
  8136. default-directory))
  8137. (setq cpltxt (concat "file:" file)
  8138. link cpltxt)))
  8139. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8140. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8141. (cond
  8142. ((org-in-regexp "<<\\(.*?\\)>>")
  8143. (setq cpltxt
  8144. (concat "file:"
  8145. (abbreviate-file-name
  8146. (buffer-file-name (buffer-base-buffer)))
  8147. "::" (match-string 1))
  8148. link cpltxt))
  8149. ((and (featurep 'org-id)
  8150. (or (eq org-id-link-to-org-use-id t)
  8151. (and (org-called-interactively-p 'any)
  8152. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8153. (and (eq org-id-link-to-org-use-id
  8154. 'create-if-interactive-and-no-custom-id)
  8155. (not custom-id))))
  8156. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8157. ;; We can make a link using the ID.
  8158. (setq link (condition-case nil
  8159. (prog1 (org-id-store-link)
  8160. (setq desc (plist-get org-store-link-plist :description)))
  8161. (error
  8162. ;; probably before first headline, link to file only
  8163. (concat "file:"
  8164. (abbreviate-file-name
  8165. (buffer-file-name (buffer-base-buffer))))))))
  8166. (t
  8167. ;; Just link to current headline
  8168. (setq cpltxt (concat "file:"
  8169. (abbreviate-file-name
  8170. (buffer-file-name (buffer-base-buffer)))))
  8171. ;; Add a context search string
  8172. (when (org-xor org-context-in-file-links arg)
  8173. (let* ((ee (org-element-at-point))
  8174. (et (org-element-type ee))
  8175. (ev (plist-get (cadr ee) :value)))
  8176. (setq txt (cond
  8177. ((org-at-heading-p) nil)
  8178. ((eq et 'keyword) ev)
  8179. ((org-region-active-p)
  8180. (buffer-substring (region-beginning) (region-end)))))
  8181. (when (or (null txt) (string-match "\\S-" txt))
  8182. (setq cpltxt
  8183. (concat cpltxt "::"
  8184. (condition-case nil
  8185. (org-make-org-heading-search-string txt)
  8186. (error "")))
  8187. desc (or (and (eq et 'keyword) ev)
  8188. (nth 4 (ignore-errors (org-heading-components)))
  8189. "NONE")))))
  8190. (if (string-match "::\\'" cpltxt)
  8191. (setq cpltxt (substring cpltxt 0 -2)))
  8192. (setq link cpltxt))))
  8193. ((buffer-file-name (buffer-base-buffer))
  8194. ;; Just link to this file here.
  8195. (setq cpltxt (concat "file:"
  8196. (abbreviate-file-name
  8197. (buffer-file-name (buffer-base-buffer)))))
  8198. ;; Add a context string
  8199. (when (org-xor org-context-in-file-links arg)
  8200. (setq txt (if (org-region-active-p)
  8201. (buffer-substring (region-beginning) (region-end))
  8202. (buffer-substring (point-at-bol) (point-at-eol))))
  8203. ;; Only use search option if there is some text.
  8204. (when (string-match "\\S-" txt)
  8205. (setq cpltxt
  8206. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8207. desc "NONE")))
  8208. (setq link cpltxt))
  8209. ((org-called-interactively-p 'interactive)
  8210. (user-error "No method for storing a link from this buffer"))
  8211. (t (setq link nil)))
  8212. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8213. (setq link (or link cpltxt)
  8214. desc (or desc cpltxt))
  8215. (cond ((equal desc "NONE") (setq desc nil))
  8216. ((string-match org-bracket-link-regexp desc)
  8217. (setq desc (replace-regexp-in-string
  8218. org-bracket-link-regexp
  8219. (concat "\\3" (if (equal (length (match-string 0 desc))
  8220. (length desc)) "*" "")) desc))))
  8221. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8222. (progn
  8223. (setq org-stored-links
  8224. (cons (list link desc) org-stored-links))
  8225. (message "Stored: %s" (or desc link))
  8226. (when custom-id
  8227. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8228. "::#" custom-id))
  8229. (setq org-stored-links
  8230. (cons (list link desc) org-stored-links))))
  8231. (or agenda-link (and link (org-make-link-string link desc)))))))
  8232. (defun org-store-link-props (&rest plist)
  8233. "Store link properties, extract names and addresses."
  8234. (let (x adr)
  8235. (when (setq x (plist-get plist :from))
  8236. (setq adr (mail-extract-address-components x))
  8237. (setq plist (plist-put plist :fromname (car adr)))
  8238. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8239. (when (setq x (plist-get plist :to))
  8240. (setq adr (mail-extract-address-components x))
  8241. (setq plist (plist-put plist :toname (car adr)))
  8242. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8243. (let ((from (plist-get plist :from))
  8244. (to (plist-get plist :to)))
  8245. (when (and from to org-from-is-user-regexp)
  8246. (setq plist
  8247. (plist-put plist :fromto
  8248. (if (string-match org-from-is-user-regexp from)
  8249. (concat "to %t")
  8250. (concat "from %f"))))))
  8251. (setq org-store-link-plist plist))
  8252. (defun org-add-link-props (&rest plist)
  8253. "Add these properties to the link property list."
  8254. (let (key value)
  8255. (while plist
  8256. (setq key (pop plist) value (pop plist))
  8257. (setq org-store-link-plist
  8258. (plist-put org-store-link-plist key value)))))
  8259. (defun org-email-link-description (&optional fmt)
  8260. "Return the description part of an email link.
  8261. This takes information from `org-store-link-plist' and formats it
  8262. according to FMT (default from `org-email-link-description-format')."
  8263. (setq fmt (or fmt org-email-link-description-format))
  8264. (let* ((p org-store-link-plist)
  8265. (to (plist-get p :toaddress))
  8266. (from (plist-get p :fromaddress))
  8267. (table
  8268. (list
  8269. (cons "%c" (plist-get p :fromto))
  8270. (cons "%F" (plist-get p :from))
  8271. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8272. (cons "%T" (plist-get p :to))
  8273. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8274. (cons "%s" (plist-get p :subject))
  8275. (cons "%d" (plist-get p :date))
  8276. (cons "%m" (plist-get p :message-id)))))
  8277. (when (string-match "%c" fmt)
  8278. ;; Check if the user wrote this message
  8279. (if (and org-from-is-user-regexp from to
  8280. (save-match-data (string-match org-from-is-user-regexp from)))
  8281. (setq fmt (replace-match "to %t" t t fmt))
  8282. (setq fmt (replace-match "from %f" t t fmt))))
  8283. (org-replace-escapes fmt table)))
  8284. (defun org-make-org-heading-search-string (&optional string)
  8285. "Make search string for the current headline or STRING."
  8286. (let ((s (or string
  8287. (and (derived-mode-p 'org-mode)
  8288. (save-excursion
  8289. (org-back-to-heading t)
  8290. (plist-get (cadr (org-element-at-point))
  8291. :raw-value)))))
  8292. (lines org-context-in-file-links))
  8293. (or string (setq s (concat "*" s))) ; Add * for headlines
  8294. (when (and string (integerp lines) (> lines 0))
  8295. (let ((slines (org-split-string s "\n")))
  8296. (when (< lines (length slines))
  8297. (setq s (mapconcat
  8298. 'identity
  8299. (reverse (nthcdr (- (length slines) lines)
  8300. (reverse slines))) "\n")))))
  8301. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8302. (defun org-make-link-string (link &optional description)
  8303. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8304. (unless (string-match "\\S-" link)
  8305. (error "Empty link"))
  8306. (when (and description
  8307. (stringp description)
  8308. (not (string-match "\\S-" description)))
  8309. (setq description nil))
  8310. (when (stringp description)
  8311. ;; Remove brackets from the description, they are fatal.
  8312. (while (string-match "\\[" description)
  8313. (setq description (replace-match "{" t t description)))
  8314. (while (string-match "\\]" description)
  8315. (setq description (replace-match "}" t t description))))
  8316. (when (equal link description)
  8317. ;; No description needed, it is identical
  8318. (setq description nil))
  8319. (when (and (not description)
  8320. (not (string-match (org-image-file-name-regexp) link))
  8321. (not (equal link (org-link-escape link))))
  8322. (setq description (org-extract-attributes link)))
  8323. (setq link
  8324. (cond ((string-match (org-image-file-name-regexp) link) link)
  8325. ((string-match org-link-types-re link)
  8326. (concat (match-string 1 link)
  8327. (org-link-escape (substring link (match-end 1)))))
  8328. (t (org-link-escape link))))
  8329. (concat "[[" link "]"
  8330. (if description (concat "[" description "]") "")
  8331. "]"))
  8332. (defconst org-link-escape-chars
  8333. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8334. "List of characters that should be escaped in link.
  8335. This is the list that is used for internal purposes.")
  8336. (defconst org-link-escape-chars-browser
  8337. '(?\ )
  8338. "List of escapes for characters that are problematic in links.
  8339. This is the list that is used before handing over to the browser.")
  8340. (defun org-link-escape (text &optional table merge)
  8341. "Return percent escaped representation of TEXT.
  8342. TEXT is a string with the text to escape.
  8343. Optional argument TABLE is a list with characters that should be
  8344. escaped. When nil, `org-link-escape-chars' is used.
  8345. If optional argument MERGE is set, merge TABLE into
  8346. `org-link-escape-chars'."
  8347. (cond
  8348. ((and table merge)
  8349. (mapc (lambda (defchr)
  8350. (unless (member defchr table)
  8351. (setq table (cons defchr table)))) org-link-escape-chars))
  8352. ((null table)
  8353. (setq table org-link-escape-chars)))
  8354. (mapconcat
  8355. (lambda (char)
  8356. (if (or (member char table)
  8357. (and (or (< char 32) (= char 37) (> char 126))
  8358. org-url-hexify-p))
  8359. (mapconcat (lambda (sequence-element)
  8360. (format "%%%.2X" sequence-element))
  8361. (or (encode-coding-char char 'utf-8)
  8362. (error "Unable to percent escape character: %s"
  8363. (char-to-string char))) "")
  8364. (char-to-string char))) text ""))
  8365. (defun org-link-unescape (str)
  8366. "Unhex hexified Unicode strings as returned from the JavaScript function
  8367. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8368. (unless (and (null str) (string= "" str))
  8369. (let ((pos 0) (case-fold-search t) unhexed)
  8370. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8371. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8372. (setq str (replace-match unhexed t t str))
  8373. (setq pos (+ pos (length unhexed))))))
  8374. str)
  8375. (defun org-link-unescape-compound (hex)
  8376. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8377. Note: this function also decodes single byte encodings like
  8378. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8379. (save-match-data
  8380. (let* ((bytes (cdr (split-string hex "%")))
  8381. (ret "")
  8382. (eat 0)
  8383. (sum 0))
  8384. (while bytes
  8385. (let* ((val (string-to-number (pop bytes) 16))
  8386. (shift-xor
  8387. (if (= 0 eat)
  8388. (cond
  8389. ((>= val 252) (cons 6 252))
  8390. ((>= val 248) (cons 5 248))
  8391. ((>= val 240) (cons 4 240))
  8392. ((>= val 224) (cons 3 224))
  8393. ((>= val 192) (cons 2 192))
  8394. (t (cons 0 0)))
  8395. (cons 6 128))))
  8396. (if (>= val 192) (setq eat (car shift-xor)))
  8397. (setq val (logxor val (cdr shift-xor)))
  8398. (setq sum (+ (lsh sum (car shift-xor)) val))
  8399. (if (> eat 0) (setq eat (- eat 1)))
  8400. (cond
  8401. ((= 0 eat) ;multi byte
  8402. (setq ret (concat ret (org-char-to-string sum)))
  8403. (setq sum 0))
  8404. ((not bytes) ; single byte(s)
  8405. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8406. )) ;; end (while bytes
  8407. ret )))
  8408. (defun org-link-unescape-single-byte-sequence (hex)
  8409. "Unhexify hex-encoded single byte character sequences."
  8410. (mapconcat (lambda (byte)
  8411. (char-to-string (string-to-number byte 16)))
  8412. (cdr (split-string hex "%")) ""))
  8413. (defun org-xor (a b)
  8414. "Exclusive or."
  8415. (if a (not b) b))
  8416. (defun org-fixup-message-id-for-http (s)
  8417. "Replace special characters in a message id, so it can be used in an http query."
  8418. (when (string-match "%" s)
  8419. (setq s (mapconcat (lambda (c)
  8420. (if (eq c ?%)
  8421. "%25"
  8422. (char-to-string c)))
  8423. s "")))
  8424. (while (string-match "<" s)
  8425. (setq s (replace-match "%3C" t t s)))
  8426. (while (string-match ">" s)
  8427. (setq s (replace-match "%3E" t t s)))
  8428. (while (string-match "@" s)
  8429. (setq s (replace-match "%40" t t s)))
  8430. s)
  8431. (defun org-link-prettify (link)
  8432. "Return a human-readable representation of LINK.
  8433. The car of LINK must be a raw link the cdr of LINK must be either
  8434. a link description or nil."
  8435. (let ((desc (or (cadr link) "<no description>")))
  8436. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8437. "<" (car link) ">")))
  8438. ;;;###autoload
  8439. (defun org-insert-link-global ()
  8440. "Insert a link like Org-mode does.
  8441. This command can be called in any mode to insert a link in Org-mode syntax."
  8442. (interactive)
  8443. (org-load-modules-maybe)
  8444. (org-run-like-in-org-mode 'org-insert-link))
  8445. (defun org-insert-all-links (&optional keep)
  8446. "Insert all links in `org-stored-links'."
  8447. (interactive "P")
  8448. (let ((links (copy-sequence org-stored-links)) l)
  8449. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8450. (insert "- ")
  8451. (org-insert-link nil (car l) (cadr l))
  8452. (insert "\n"))))
  8453. (defun org-link-fontify-links-to-this-file ()
  8454. "Fontify links to the current file in `org-stored-links'."
  8455. (let ((f (buffer-file-name)) a b)
  8456. (setq a (mapcar (lambda(l)
  8457. (let ((ll (car l)))
  8458. (when (and (string-match "^file:\\(.+\\)::" ll)
  8459. (equal f (expand-file-name (match-string 1 ll))))
  8460. ll)))
  8461. org-stored-links))
  8462. (when (featurep 'org-id)
  8463. (setq b (mapcar (lambda(l)
  8464. (let ((ll (car l)))
  8465. (when (and (string-match "^id:\\(.+\\)$" ll)
  8466. (equal f (expand-file-name
  8467. (or (org-id-find-id-file
  8468. (match-string 1 ll)) ""))))
  8469. ll)))
  8470. org-stored-links)))
  8471. (mapcar (lambda(l)
  8472. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8473. (delq nil (append a b)))))
  8474. (defvar org-link-links-in-this-file nil)
  8475. (defun org-insert-link (&optional complete-file link-location default-description)
  8476. "Insert a link. At the prompt, enter the link.
  8477. Completion can be used to insert any of the link protocol prefixes like
  8478. http or ftp in use.
  8479. The history can be used to select a link previously stored with
  8480. `org-store-link'. When the empty string is entered (i.e. if you just
  8481. press RET at the prompt), the link defaults to the most recently
  8482. stored link. As SPC triggers completion in the minibuffer, you need to
  8483. use M-SPC or C-q SPC to force the insertion of a space character.
  8484. You will also be prompted for a description, and if one is given, it will
  8485. be displayed in the buffer instead of the link.
  8486. If there is already a link at point, this command will allow you to edit link
  8487. and description parts.
  8488. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8489. be selected using completion. The path to the file will be relative to the
  8490. current directory if the file is in the current directory or a subdirectory.
  8491. Otherwise, the link will be the absolute path as completed in the minibuffer
  8492. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8493. option `org-link-file-path-type'.
  8494. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8495. the current directory or below.
  8496. With three \\[universal-argument] prefixes, negate the meaning of
  8497. `org-keep-stored-link-after-insertion'.
  8498. If `org-make-link-description-function' is non-nil, this function will be
  8499. called with the link target, and the result will be the default
  8500. link description.
  8501. If the LINK-LOCATION parameter is non-nil, this value will be
  8502. used as the link location instead of reading one interactively.
  8503. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8504. be used as the default description."
  8505. (interactive "P")
  8506. (let* ((wcf (current-window-configuration))
  8507. (origbuf (current-buffer))
  8508. (region (if (org-region-active-p)
  8509. (buffer-substring (region-beginning) (region-end))))
  8510. (remove (and region (list (region-beginning) (region-end))))
  8511. (desc region)
  8512. tmphist ; byte-compile incorrectly complains about this
  8513. (link link-location)
  8514. (abbrevs org-link-abbrev-alist-local)
  8515. entry file all-prefixes auto-desc)
  8516. (cond
  8517. (link-location) ; specified by arg, just use it.
  8518. ((org-in-regexp org-bracket-link-regexp 1)
  8519. ;; We do have a link at point, and we are going to edit it.
  8520. (setq remove (list (match-beginning 0) (match-end 0)))
  8521. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8522. (setq link (read-string "Link: "
  8523. (org-link-unescape
  8524. (org-match-string-no-properties 1)))))
  8525. ((or (org-in-regexp org-angle-link-re)
  8526. (org-in-regexp org-plain-link-re))
  8527. ;; Convert to bracket link
  8528. (setq remove (list (match-beginning 0) (match-end 0))
  8529. link (read-string "Link: "
  8530. (org-remove-angle-brackets (match-string 0)))))
  8531. ((member complete-file '((4) (16)))
  8532. ;; Completing read for file names.
  8533. (setq link (org-file-complete-link complete-file)))
  8534. (t
  8535. ;; Read link, with completion for stored links.
  8536. (org-link-fontify-links-to-this-file)
  8537. (org-switch-to-buffer-other-window "*Org Links*")
  8538. (with-current-buffer "*Org Links*"
  8539. (erase-buffer)
  8540. (insert "Insert a link.
  8541. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8542. (when org-stored-links
  8543. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8544. (insert (mapconcat 'org-link-prettify
  8545. (reverse org-stored-links) "\n")))
  8546. (goto-char (point-min)))
  8547. (let ((cw (selected-window)))
  8548. (select-window (get-buffer-window "*Org Links*" 'visible))
  8549. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8550. (unless (pos-visible-in-window-p (point-max))
  8551. (org-fit-window-to-buffer))
  8552. (and (window-live-p cw) (select-window cw)))
  8553. ;; Fake a link history, containing the stored links.
  8554. (setq tmphist (append (mapcar 'car org-stored-links)
  8555. org-insert-link-history))
  8556. (setq all-prefixes (append (mapcar 'car abbrevs)
  8557. (mapcar 'car org-link-abbrev-alist)
  8558. org-link-types))
  8559. (unwind-protect
  8560. (progn
  8561. (setq link
  8562. (org-completing-read
  8563. "Link: "
  8564. (append
  8565. (mapcar (lambda (x) (concat x ":"))
  8566. all-prefixes)
  8567. (mapcar 'car org-stored-links))
  8568. nil nil nil
  8569. 'tmphist
  8570. (caar org-stored-links)))
  8571. (if (not (string-match "\\S-" link))
  8572. (error "No link selected"))
  8573. (mapc (lambda(l)
  8574. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8575. org-stored-links)
  8576. (if (or (member link all-prefixes)
  8577. (and (equal ":" (substring link -1))
  8578. (member (substring link 0 -1) all-prefixes)
  8579. (setq link (substring link 0 -1))))
  8580. (setq link (with-current-buffer origbuf
  8581. (org-link-try-special-completion link)))))
  8582. (set-window-configuration wcf)
  8583. (kill-buffer "*Org Links*"))
  8584. (setq entry (assoc link org-stored-links))
  8585. (or entry (push link org-insert-link-history))
  8586. (setq desc (or desc (nth 1 entry)))))
  8587. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8588. (not org-keep-stored-link-after-insertion))
  8589. (setq org-stored-links (delq (assoc link org-stored-links)
  8590. org-stored-links)))
  8591. (if (string-match org-plain-link-re link)
  8592. ;; URL-like link, normalize the use of angular brackets.
  8593. (setq link (org-remove-angle-brackets link)))
  8594. ;; Check if we are linking to the current file with a search
  8595. ;; option If yes, simplify the link by using only the search
  8596. ;; option.
  8597. (when (and buffer-file-name
  8598. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8599. (let* ((path (match-string 1 link))
  8600. (case-fold-search nil)
  8601. (search (match-string 2 link)))
  8602. (save-match-data
  8603. (if (equal (file-truename buffer-file-name) (file-truename path))
  8604. ;; We are linking to this same file, with a search option
  8605. (setq link search)))))
  8606. ;; Check if we can/should use a relative path. If yes, simplify the link
  8607. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8608. (let* ((type (match-string 1 link))
  8609. (path (match-string 2 link))
  8610. (origpath path)
  8611. (case-fold-search nil))
  8612. (cond
  8613. ((or (eq org-link-file-path-type 'absolute)
  8614. (equal complete-file '(16)))
  8615. (setq path (abbreviate-file-name (expand-file-name path))))
  8616. ((eq org-link-file-path-type 'noabbrev)
  8617. (setq path (expand-file-name path)))
  8618. ((eq org-link-file-path-type 'relative)
  8619. (setq path (file-relative-name path)))
  8620. (t
  8621. (save-match-data
  8622. (if (string-match (concat "^" (regexp-quote
  8623. (expand-file-name
  8624. (file-name-as-directory
  8625. default-directory))))
  8626. (expand-file-name path))
  8627. ;; We are linking a file with relative path name.
  8628. (setq path (substring (expand-file-name path)
  8629. (match-end 0)))
  8630. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8631. (setq link (concat type path))
  8632. (if (equal desc origpath)
  8633. (setq desc path))))
  8634. (if org-make-link-description-function
  8635. (setq desc
  8636. (or (condition-case nil
  8637. (funcall org-make-link-description-function link desc)
  8638. (error (progn (message "Can't get link description from `%s'"
  8639. (symbol-name org-make-link-description-function))
  8640. (sit-for 2) nil)))
  8641. (read-string "Description: " default-description)))
  8642. (if default-description (setq desc default-description)
  8643. (setq desc (or (and auto-desc desc)
  8644. (read-string "Description: " desc)))))
  8645. (unless (string-match "\\S-" desc) (setq desc nil))
  8646. (if remove (apply 'delete-region remove))
  8647. (insert (org-make-link-string link desc))))
  8648. (defun org-link-try-special-completion (type)
  8649. "If there is completion support for link type TYPE, offer it."
  8650. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8651. (if (functionp fun)
  8652. (funcall fun)
  8653. (read-string "Link (no completion support): " (concat type ":")))))
  8654. (defun org-file-complete-link (&optional arg)
  8655. "Create a file link using completion."
  8656. (let (file link)
  8657. (setq file (org-iread-file-name "File: "))
  8658. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8659. (pwd1 (file-name-as-directory (abbreviate-file-name
  8660. (expand-file-name ".")))))
  8661. (cond
  8662. ((equal arg '(16))
  8663. (setq link (concat
  8664. "file:"
  8665. (abbreviate-file-name (expand-file-name file)))))
  8666. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8667. (setq link (concat "file:" (match-string 1 file))))
  8668. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8669. (expand-file-name file))
  8670. (setq link (concat
  8671. "file:" (match-string 1 (expand-file-name file)))))
  8672. (t (setq link (concat "file:" file)))))
  8673. link))
  8674. (defun org-iread-file-name (&rest args)
  8675. "Read-file-name using `ido-mode' speedup if available.
  8676. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8677. See `read-file-name' for a description of parameters."
  8678. (org-without-partial-completion
  8679. (if (and org-completion-use-ido
  8680. (fboundp 'ido-read-file-name)
  8681. (boundp 'ido-mode) ido-mode
  8682. (listp (second args)))
  8683. (let ((ido-enter-matching-directory nil))
  8684. (apply 'ido-read-file-name args))
  8685. (apply 'read-file-name args))))
  8686. (defun org-completing-read (&rest args)
  8687. "Completing-read with SPACE being a normal character."
  8688. (let ((enable-recursive-minibuffers t)
  8689. (minibuffer-local-completion-map
  8690. (copy-keymap minibuffer-local-completion-map)))
  8691. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8692. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8693. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8694. (apply 'org-icompleting-read args)))
  8695. (defun org-completing-read-no-i (&rest args)
  8696. (let (org-completion-use-ido org-completion-use-iswitchb)
  8697. (apply 'org-completing-read args)))
  8698. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8699. "Use iswitch as a completing-read replacement to choose from choices.
  8700. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8701. from."
  8702. (let* ((iswitchb-use-virtual-buffers nil)
  8703. (iswitchb-make-buflist-hook
  8704. (lambda ()
  8705. (setq iswitchb-temp-buflist choices))))
  8706. (iswitchb-read-buffer prompt)))
  8707. (defun org-icompleting-read (&rest args)
  8708. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8709. (org-without-partial-completion
  8710. (if (and org-completion-use-ido
  8711. (fboundp 'ido-completing-read)
  8712. (boundp 'ido-mode) ido-mode
  8713. (listp (second args)))
  8714. (let ((ido-enter-matching-directory nil))
  8715. (apply 'ido-completing-read (concat (car args))
  8716. (if (consp (car (nth 1 args)))
  8717. (mapcar 'car (nth 1 args))
  8718. (nth 1 args))
  8719. (cddr args)))
  8720. (if (and org-completion-use-iswitchb
  8721. (boundp 'iswitchb-mode) iswitchb-mode
  8722. (listp (second args)))
  8723. (apply 'org-iswitchb-completing-read (concat (car args))
  8724. (if (consp (car (nth 1 args)))
  8725. (mapcar 'car (nth 1 args))
  8726. (nth 1 args))
  8727. (cddr args))
  8728. (apply 'completing-read args)))))
  8729. (defun org-extract-attributes (s)
  8730. "Extract the attributes cookie from a string and set as text property."
  8731. (let (a attr (start 0) key value)
  8732. (save-match-data
  8733. (when (string-match "{{\\([^}]+\\)}}$" s)
  8734. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8735. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8736. (setq key (match-string 1 a) value (match-string 2 a)
  8737. start (match-end 0)
  8738. attr (plist-put attr (intern key) value))))
  8739. (org-add-props s nil 'org-attr attr))
  8740. s))
  8741. (defun org-extract-attributes-from-string (tag)
  8742. (let (key value attr)
  8743. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8744. (setq key (match-string 1 tag) value (match-string 2 tag)
  8745. tag (replace-match "" t t tag)
  8746. attr (plist-put attr (intern key) value)))
  8747. (cons tag attr)))
  8748. (defun org-attributes-to-string (plist)
  8749. "Format a property list into an HTML attribute list."
  8750. (let ((s "") key value)
  8751. (while plist
  8752. (setq key (pop plist) value (pop plist))
  8753. (and value
  8754. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8755. s))
  8756. ;;; Opening/following a link
  8757. (defvar org-link-search-failed nil)
  8758. (defvar org-open-link-functions nil
  8759. "Hook for functions finding a plain text link.
  8760. These functions must take a single argument, the link content.
  8761. They will be called for links that look like [[link text][description]]
  8762. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8763. like a filename (e.g. \"./blue.png\").
  8764. These functions will be called *before* Org attempts to resolve the
  8765. link by doing text searches in the current buffer - so if you want a
  8766. link \"[[target]]\" to still find \"<<target>>\", your function should
  8767. handle this as a special case.
  8768. When the function does handle the link, it must return a non-nil value.
  8769. If it decides that it is not responsible for this link, it must return
  8770. nil to indicate that that Org-mode can continue with other options
  8771. like exact and fuzzy text search.")
  8772. (defun org-next-link ()
  8773. "Move forward to the next link.
  8774. If the link is in hidden text, expose it."
  8775. (interactive)
  8776. (when (and org-link-search-failed (eq this-command last-command))
  8777. (goto-char (point-min))
  8778. (message "Link search wrapped back to beginning of buffer"))
  8779. (setq org-link-search-failed nil)
  8780. (let* ((pos (point))
  8781. (ct (org-context))
  8782. (a (assoc :link ct)))
  8783. (if a (goto-char (nth 2 a)))
  8784. (if (re-search-forward org-any-link-re nil t)
  8785. (progn
  8786. (goto-char (match-beginning 0))
  8787. (if (outline-invisible-p) (org-show-context)))
  8788. (goto-char pos)
  8789. (setq org-link-search-failed t)
  8790. (error "No further link found"))))
  8791. (defun org-previous-link ()
  8792. "Move backward to the previous link.
  8793. If the link is in hidden text, expose it."
  8794. (interactive)
  8795. (when (and org-link-search-failed (eq this-command last-command))
  8796. (goto-char (point-max))
  8797. (message "Link search wrapped back to end of buffer"))
  8798. (setq org-link-search-failed nil)
  8799. (let* ((pos (point))
  8800. (ct (org-context))
  8801. (a (assoc :link ct)))
  8802. (if a (goto-char (nth 1 a)))
  8803. (if (re-search-backward org-any-link-re nil t)
  8804. (progn
  8805. (goto-char (match-beginning 0))
  8806. (if (outline-invisible-p) (org-show-context)))
  8807. (goto-char pos)
  8808. (setq org-link-search-failed t)
  8809. (error "No further link found"))))
  8810. (defun org-translate-link (s)
  8811. "Translate a link string if a translation function has been defined."
  8812. (if (and org-link-translation-function
  8813. (fboundp org-link-translation-function)
  8814. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8815. (progn
  8816. (setq s (funcall org-link-translation-function
  8817. (match-string 1 s) (match-string 2 s)))
  8818. (concat (car s) ":" (cdr s)))
  8819. s))
  8820. (defun org-translate-link-from-planner (type path)
  8821. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8822. This is still an experimental function, your mileage may vary."
  8823. (cond
  8824. ((member type '("http" "https" "news" "ftp"))
  8825. ;; standard Internet links are the same.
  8826. nil)
  8827. ((and (equal type "irc") (string-match "^//" path))
  8828. ;; Planner has two / at the beginning of an irc link, we have 1.
  8829. ;; We should have zero, actually....
  8830. (setq path (substring path 1)))
  8831. ((and (equal type "lisp") (string-match "^/" path))
  8832. ;; Planner has a slash, we do not.
  8833. (setq type "elisp" path (substring path 1)))
  8834. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8835. ;; A typical message link. Planner has the id after the final slash,
  8836. ;; we separate it with a hash mark
  8837. (setq path (concat (match-string 1 path) "#"
  8838. (org-remove-angle-brackets (match-string 2 path)))))
  8839. )
  8840. (cons type path))
  8841. (defun org-find-file-at-mouse (ev)
  8842. "Open file link or URL at mouse."
  8843. (interactive "e")
  8844. (mouse-set-point ev)
  8845. (org-open-at-point 'in-emacs))
  8846. (defun org-open-at-mouse (ev)
  8847. "Open file link or URL at mouse.
  8848. See the docstring of `org-open-file' for details."
  8849. (interactive "e")
  8850. (mouse-set-point ev)
  8851. (if (eq major-mode 'org-agenda-mode)
  8852. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8853. (org-open-at-point))
  8854. (defvar org-window-config-before-follow-link nil
  8855. "The window configuration before following a link.
  8856. This is saved in case the need arises to restore it.")
  8857. (defvar org-open-link-marker (make-marker)
  8858. "Marker pointing to the location where `org-open-at-point; was called.")
  8859. ;;;###autoload
  8860. (defun org-open-at-point-global ()
  8861. "Follow a link like Org-mode does.
  8862. This command can be called in any mode to follow a link that has
  8863. Org-mode syntax."
  8864. (interactive)
  8865. (org-run-like-in-org-mode 'org-open-at-point))
  8866. ;;;###autoload
  8867. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8868. "Open a link in the string S, as if it was in Org-mode."
  8869. (interactive "sLink: \nP")
  8870. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8871. (with-temp-buffer
  8872. (let ((org-inhibit-startup (not reference-buffer)))
  8873. (org-mode)
  8874. (insert s)
  8875. (goto-char (point-min))
  8876. (when reference-buffer
  8877. (setq org-link-abbrev-alist-local
  8878. (with-current-buffer reference-buffer
  8879. org-link-abbrev-alist-local)))
  8880. (org-open-at-point arg reference-buffer)))))
  8881. (defvar org-open-at-point-functions nil
  8882. "Hook that is run when following a link at point.
  8883. Functions in this hook must return t if they identify and follow
  8884. a link at point. If they don't find anything interesting at point,
  8885. they must return nil.")
  8886. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8887. (defun org-open-at-point (&optional arg reference-buffer)
  8888. "Open link at or after point.
  8889. If there is no link at point, this function will search forward up to
  8890. the end of the current line.
  8891. Normally, files will be opened by an appropriate application. If the
  8892. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8893. With a double prefix argument, try to open outside of Emacs, in the
  8894. application the system uses for this file type."
  8895. (interactive "P")
  8896. ;; if in a code block, then open the block's results
  8897. (unless (call-interactively #'org-babel-open-src-block-result)
  8898. (org-load-modules-maybe)
  8899. (move-marker org-open-link-marker (point))
  8900. (setq org-window-config-before-follow-link (current-window-configuration))
  8901. (org-remove-occur-highlights nil nil t)
  8902. (cond
  8903. ((and (org-at-heading-p)
  8904. (not (org-at-timestamp-p t))
  8905. (not (org-in-regexp
  8906. (concat org-plain-link-re "\\|"
  8907. org-bracket-link-regexp "\\|"
  8908. org-angle-link-re "\\|"
  8909. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8910. (not (get-text-property (point) 'org-linked-text)))
  8911. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8912. (lk (car lkall))
  8913. (lkend (cdr lkall)))
  8914. (when lk
  8915. (prog1 (search-forward lk nil lkend)
  8916. (goto-char (match-beginning 0))
  8917. (org-open-at-point))))
  8918. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8919. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8920. ((and (org-at-timestamp-p t)
  8921. (not (org-in-regexp org-bracket-link-regexp)))
  8922. (org-follow-timestamp-link))
  8923. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8924. (not (org-in-regexp org-any-link-re)))
  8925. (org-footnote-action))
  8926. (t
  8927. (let (type path link line search (pos (point)))
  8928. (catch 'match
  8929. (save-excursion
  8930. (skip-chars-forward "^]\n\r")
  8931. (when (org-in-regexp org-bracket-link-regexp 1)
  8932. (setq link (org-extract-attributes
  8933. (org-link-unescape (org-match-string-no-properties 1))))
  8934. (while (string-match " *\n *" link)
  8935. (setq link (replace-match " " t t link)))
  8936. (setq link (org-link-expand-abbrev link))
  8937. (cond
  8938. ((or (file-name-absolute-p link)
  8939. (string-match "^\\.\\.?/" link))
  8940. (setq type "file" path link))
  8941. ((string-match org-link-re-with-space3 link)
  8942. (setq type (match-string 1 link) path (match-string 2 link)))
  8943. ((string-match "^help:+\\(.+\\)" link)
  8944. (setq type "help" path (match-string 1 link)))
  8945. (t (setq type "thisfile" path link)))
  8946. (throw 'match t)))
  8947. (when (get-text-property (point) 'org-linked-text)
  8948. (setq type "thisfile"
  8949. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8950. (1+ (point)) (point))
  8951. path (buffer-substring
  8952. (or (previous-single-property-change pos 'org-linked-text)
  8953. (point-min))
  8954. (or (next-single-property-change pos 'org-linked-text)
  8955. (point-max))))
  8956. (throw 'match t))
  8957. (save-excursion
  8958. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8959. (when (or (org-in-regexp org-angle-link-re)
  8960. (and plinkpos (goto-char (car plinkpos))
  8961. (save-match-data (not (looking-back "\\[\\[")))))
  8962. (setq type (match-string 1)
  8963. path (org-link-unescape (match-string 2)))
  8964. (throw 'match t))))
  8965. (save-excursion
  8966. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8967. (setq type "tags"
  8968. path (match-string 1))
  8969. (while (string-match ":" path)
  8970. (setq path (replace-match "+" t t path)))
  8971. (throw 'match t)))
  8972. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8973. (setq type "tree-match"
  8974. path (match-string 1))
  8975. (throw 'match t)))
  8976. (unless path
  8977. (user-error "No link found"))
  8978. ;; switch back to reference buffer
  8979. ;; needed when if called in a temporary buffer through
  8980. ;; org-open-link-from-string
  8981. (with-current-buffer (or reference-buffer (current-buffer))
  8982. ;; Remove any trailing spaces in path
  8983. (if (string-match " +\\'" path)
  8984. (setq path (replace-match "" t t path)))
  8985. (if (and org-link-translation-function
  8986. (fboundp org-link-translation-function))
  8987. ;; Check if we need to translate the link
  8988. (let ((tmp (funcall org-link-translation-function type path)))
  8989. (setq type (car tmp) path (cdr tmp))))
  8990. (cond
  8991. ((assoc type org-link-protocols)
  8992. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8993. ((equal type "help")
  8994. (let ((f-or-v (intern path)))
  8995. (cond ((fboundp f-or-v)
  8996. (describe-function f-or-v))
  8997. ((boundp f-or-v)
  8998. (describe-variable f-or-v))
  8999. (t (error "Not a known function or variable")))))
  9000. ((equal type "mailto")
  9001. (let ((cmd (car org-link-mailto-program))
  9002. (args (cdr org-link-mailto-program)) args1
  9003. (address path) (subject "") a)
  9004. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9005. (setq address (match-string 1 path)
  9006. subject (org-link-escape (match-string 2 path))))
  9007. (while args
  9008. (cond
  9009. ((not (stringp (car args))) (push (pop args) args1))
  9010. (t (setq a (pop args))
  9011. (if (string-match "%a" a)
  9012. (setq a (replace-match address t t a)))
  9013. (if (string-match "%s" a)
  9014. (setq a (replace-match subject t t a)))
  9015. (push a args1))))
  9016. (apply cmd (nreverse args1))))
  9017. ((member type '("http" "https" "ftp" "news"))
  9018. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9019. (org-link-escape
  9020. path org-link-escape-chars-browser)
  9021. path))))
  9022. ((string= type "doi")
  9023. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9024. (org-link-escape
  9025. path org-link-escape-chars-browser)
  9026. path))))
  9027. ((member type '("message"))
  9028. (browse-url (concat type ":" path)))
  9029. ((string= type "tags")
  9030. (org-tags-view arg path))
  9031. ((string= type "tree-match")
  9032. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9033. ((string= type "file")
  9034. (if (string-match "::\\([0-9]+\\)\\'" path)
  9035. (setq line (string-to-number (match-string 1 path))
  9036. path (substring path 0 (match-beginning 0)))
  9037. (if (string-match "::\\(.+\\)\\'" path)
  9038. (setq search (match-string 1 path)
  9039. path (substring path 0 (match-beginning 0)))))
  9040. (if (string-match "[*?{]" (file-name-nondirectory path))
  9041. (dired path)
  9042. (org-open-file path arg line search)))
  9043. ((string= type "shell")
  9044. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9045. (cmd path))
  9046. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9047. (string-match org-confirm-shell-link-not-regexp cmd))
  9048. (not org-confirm-shell-link-function)
  9049. (funcall org-confirm-shell-link-function
  9050. (format "Execute \"%s\" in shell? "
  9051. (org-add-props cmd nil
  9052. 'face 'org-warning))))
  9053. (progn
  9054. (message "Executing %s" cmd)
  9055. (shell-command cmd buf)
  9056. (if (featurep 'midnight)
  9057. (setq clean-buffer-list-kill-buffer-names
  9058. (cons buf clean-buffer-list-kill-buffer-names))))
  9059. (error "Abort"))))
  9060. ((string= type "elisp")
  9061. (let ((cmd path))
  9062. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9063. (string-match org-confirm-elisp-link-not-regexp cmd))
  9064. (not org-confirm-elisp-link-function)
  9065. (funcall org-confirm-elisp-link-function
  9066. (format "Execute \"%s\" as elisp? "
  9067. (org-add-props cmd nil
  9068. 'face 'org-warning))))
  9069. (message "%s => %s" cmd
  9070. (if (equal (string-to-char cmd) ?\()
  9071. (eval (read cmd))
  9072. (call-interactively (read cmd))))
  9073. (error "Abort"))))
  9074. ((and (string= type "thisfile")
  9075. (run-hook-with-args-until-success
  9076. 'org-open-link-functions path)))
  9077. ((string= type "thisfile")
  9078. (if arg
  9079. (switch-to-buffer-other-window
  9080. (org-get-buffer-for-internal-link (current-buffer)))
  9081. (org-mark-ring-push))
  9082. (let ((cmd `(org-link-search
  9083. ,path
  9084. ,(cond ((equal arg '(4)) ''occur)
  9085. ((equal arg '(16)) ''org-occur))
  9086. ,pos)))
  9087. (condition-case nil (let ((org-link-search-inhibit-query t))
  9088. (eval cmd))
  9089. (error (progn (widen) (eval cmd))))))
  9090. (t (browse-url-at-point)))))))
  9091. (move-marker org-open-link-marker nil)
  9092. (run-hook-with-args 'org-follow-link-hook)))
  9093. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9094. "Offer links in the current entry and return the selected link.
  9095. If there is only one link, return it.
  9096. If NTH is an integer, return the NTH link found.
  9097. If ZERO is a string, check also this string for a link, and if
  9098. there is one, return it."
  9099. (with-current-buffer buffer
  9100. (save-excursion
  9101. (save-restriction
  9102. (widen)
  9103. (goto-char marker)
  9104. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9105. "\\(" org-angle-link-re "\\)\\|"
  9106. "\\(" org-plain-link-re "\\)"))
  9107. (cnt ?0)
  9108. (in-emacs (if (integerp nth) nil nth))
  9109. have-zero end links link c)
  9110. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9111. (push (match-string 0 zero) links)
  9112. (setq cnt (1- cnt) have-zero t))
  9113. (save-excursion
  9114. (org-back-to-heading t)
  9115. (setq end (save-excursion (outline-next-heading) (point)))
  9116. (while (re-search-forward re end t)
  9117. (push (match-string 0) links))
  9118. (setq links (org-uniquify (reverse links))))
  9119. (cond
  9120. ((null links)
  9121. (message "No links"))
  9122. ((equal (length links) 1)
  9123. (setq link (car links)))
  9124. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9125. (setq link (nth (if have-zero nth (1- nth)) links)))
  9126. (t ; we have to select a link
  9127. (save-excursion
  9128. (save-window-excursion
  9129. (delete-other-windows)
  9130. (with-output-to-temp-buffer "*Select Link*"
  9131. (mapc (lambda (l)
  9132. (if (not (string-match org-bracket-link-regexp l))
  9133. (princ (format "[%c] %s\n" (incf cnt)
  9134. (org-remove-angle-brackets l)))
  9135. (if (match-end 3)
  9136. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9137. (match-string 3 l) (match-string 1 l)))
  9138. (princ (format "[%c] %s\n" (incf cnt)
  9139. (match-string 1 l))))))
  9140. links))
  9141. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9142. (message "Select link to open, RET to open all:")
  9143. (setq c (read-char-exclusive))
  9144. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9145. (when (equal c ?q) (error "Abort"))
  9146. (if (equal c ?\C-m)
  9147. (setq link links)
  9148. (setq nth (- c ?0))
  9149. (if have-zero (setq nth (1+ nth)))
  9150. (unless (and (integerp nth) (>= (length links) nth))
  9151. (error "Invalid link selection"))
  9152. (setq link (nth (1- nth) links)))))
  9153. (cons link end))))))
  9154. ;; Add special file links that specify the way of opening
  9155. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9156. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9157. (defun org-open-file-with-system (path)
  9158. "Open file at PATH using the system way of opening it."
  9159. (org-open-file path 'system))
  9160. (defun org-open-file-with-emacs (path)
  9161. "Open file at PATH in Emacs."
  9162. (org-open-file path 'emacs))
  9163. (defun org-remove-file-link-modifiers ()
  9164. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  9165. (goto-char (point-min))
  9166. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  9167. (org-if-unprotected
  9168. (replace-match "file:" t t))))
  9169. (eval-after-load "org-exp"
  9170. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  9171. 'org-remove-file-link-modifiers))
  9172. ;;; File search
  9173. (defvar org-create-file-search-functions nil
  9174. "List of functions to construct the right search string for a file link.
  9175. These functions are called in turn with point at the location to
  9176. which the link should point.
  9177. A function in the hook should first test if it would like to
  9178. handle this file type, for example by checking the `major-mode'
  9179. or the file extension. If it decides not to handle this file, it
  9180. should just return nil to give other functions a chance. If it
  9181. does handle the file, it must return the search string to be used
  9182. when following the link. The search string will be part of the
  9183. file link, given after a double colon, and `org-open-at-point'
  9184. will automatically search for it. If special measures must be
  9185. taken to make the search successful, another function should be
  9186. added to the companion hook `org-execute-file-search-functions',
  9187. which see.
  9188. A function in this hook may also use `setq' to set the variable
  9189. `description' to provide a suggestion for the descriptive text to
  9190. be used for this link when it gets inserted into an Org-mode
  9191. buffer with \\[org-insert-link].")
  9192. (defvar org-execute-file-search-functions nil
  9193. "List of functions to execute a file search triggered by a link.
  9194. Functions added to this hook must accept a single argument, the
  9195. search string that was part of the file link, the part after the
  9196. double colon. The function must first check if it would like to
  9197. handle this search, for example by checking the `major-mode' or
  9198. the file extension. If it decides not to handle this search, it
  9199. should just return nil to give other functions a chance. If it
  9200. does handle the search, it must return a non-nil value to keep
  9201. other functions from trying.
  9202. Each function can access the current prefix argument through the
  9203. variable `current-prefix-argument'. Note that a single prefix is
  9204. used to force opening a link in Emacs, so it may be good to only
  9205. use a numeric or double prefix to guide the search function.
  9206. In case this is needed, a function in this hook can also restore
  9207. the window configuration before `org-open-at-point' was called using:
  9208. (set-window-configuration org-window-config-before-follow-link)")
  9209. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9210. (defun org-link-search (s &optional type avoid-pos stealth)
  9211. "Search for a link search option.
  9212. If S is surrounded by forward slashes, it is interpreted as a
  9213. regular expression. In org-mode files, this will create an `org-occur'
  9214. sparse tree. In ordinary files, `occur' will be used to list matches.
  9215. If the current buffer is in `dired-mode', grep will be used to search
  9216. in all files. If AVOID-POS is given, ignore matches near that position.
  9217. When optional argument STEALTH is non-nil, do not modify
  9218. visibility around point, thus ignoring
  9219. `org-show-hierarchy-above', `org-show-following-heading' and
  9220. `org-show-siblings' variables."
  9221. (let ((case-fold-search t)
  9222. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9223. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9224. (append '(("") (" ") ("\t") ("\n"))
  9225. org-emphasis-alist)
  9226. "\\|") "\\)"))
  9227. (pos (point))
  9228. (pre nil) (post nil)
  9229. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9230. (cond
  9231. ;; First check if there are any special search functions
  9232. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9233. ;; Now try the builtin stuff
  9234. ((and (equal (string-to-char s0) ?#)
  9235. (> (length s0) 1)
  9236. (save-excursion
  9237. (goto-char (point-min))
  9238. (and
  9239. (re-search-forward
  9240. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9241. (setq type 'dedicated
  9242. pos (match-beginning 0))))
  9243. ;; There is an exact target for this
  9244. (goto-char pos)
  9245. (org-back-to-heading t)))
  9246. ((save-excursion
  9247. (goto-char (point-min))
  9248. (and
  9249. (re-search-forward
  9250. (concat "<<" (regexp-quote s0) ">>") nil t)
  9251. (setq type 'dedicated
  9252. pos (match-beginning 0))))
  9253. ;; There is an exact target for this
  9254. (goto-char pos))
  9255. ((save-excursion
  9256. (goto-char (point-min))
  9257. (and
  9258. (re-search-forward
  9259. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9260. (setq type 'dedicated pos (match-beginning 0))))
  9261. ;; Found an invisible target.
  9262. (goto-char pos))
  9263. ((save-excursion
  9264. (goto-char (point-min))
  9265. (and
  9266. (re-search-forward
  9267. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9268. (setq type 'dedicated pos (match-beginning 0))))
  9269. ;; Found an element with a matching #+name affiliated keyword.
  9270. (goto-char pos))
  9271. ((and (string-match "^(\\(.*\\))$" s0)
  9272. (save-excursion
  9273. (goto-char (point-min))
  9274. (and
  9275. (re-search-forward
  9276. (concat "[^[]" (regexp-quote
  9277. (format org-coderef-label-format
  9278. (match-string 1 s0))))
  9279. nil t)
  9280. (setq type 'dedicated
  9281. pos (1+ (match-beginning 0))))))
  9282. ;; There is a coderef target for this
  9283. (goto-char pos))
  9284. ((string-match "^/\\(.*\\)/$" s)
  9285. ;; A regular expression
  9286. (cond
  9287. ((derived-mode-p 'org-mode)
  9288. (org-occur (match-string 1 s)))
  9289. ;;((eq major-mode 'dired-mode)
  9290. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9291. (t (org-do-occur (match-string 1 s)))))
  9292. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9293. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9294. (goto-char (point-min))
  9295. (cond
  9296. ((let (case-fold-search)
  9297. (re-search-forward (format org-complex-heading-regexp-format
  9298. (regexp-quote s))
  9299. nil t))
  9300. ;; OK, found a match
  9301. (setq type 'dedicated)
  9302. (goto-char (match-beginning 0)))
  9303. ((and (not org-link-search-inhibit-query)
  9304. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9305. (y-or-n-p "No match - create this as a new heading? "))
  9306. (goto-char (point-max))
  9307. (or (bolp) (newline))
  9308. (insert "* " s "\n")
  9309. (beginning-of-line 0))
  9310. (t
  9311. (goto-char pos)
  9312. (error "No match"))))
  9313. (t
  9314. ;; A normal search string
  9315. (when (equal (string-to-char s) ?*)
  9316. ;; Anchor on headlines, post may include tags.
  9317. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9318. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9319. s (substring s 1)))
  9320. (remove-text-properties
  9321. 0 (length s)
  9322. '(face nil mouse-face nil keymap nil fontified nil) s)
  9323. ;; Make a series of regular expressions to find a match
  9324. (setq words (org-split-string s "[ \n\r\t]+")
  9325. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9326. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9327. "\\)" markers)
  9328. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9329. re2a (concat "[ \t\r\n]" re2a_)
  9330. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9331. re4 (concat "[^a-zA-Z_]" re4_)
  9332. re1 (concat pre re2 post)
  9333. re3 (concat pre (if pre re4_ re4) post)
  9334. re5 (concat pre ".*" re4)
  9335. re2 (concat pre re2)
  9336. re2a (concat pre (if pre re2a_ re2a))
  9337. re4 (concat pre (if pre re4_ re4))
  9338. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9339. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9340. re5 "\\)"
  9341. ))
  9342. (cond
  9343. ((eq type 'org-occur) (org-occur reall))
  9344. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9345. (t (goto-char (point-min))
  9346. (setq type 'fuzzy)
  9347. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9348. (org-search-not-self 1 re1 nil t)
  9349. (org-search-not-self 1 re2 nil t)
  9350. (org-search-not-self 1 re2a nil t)
  9351. (org-search-not-self 1 re3 nil t)
  9352. (org-search-not-self 1 re4 nil t)
  9353. (org-search-not-self 1 re5 nil t)
  9354. )
  9355. (goto-char (match-beginning 1))
  9356. (goto-char pos)
  9357. (error "No match"))))))
  9358. (and (derived-mode-p 'org-mode)
  9359. (not stealth)
  9360. (org-show-context 'link-search))
  9361. type))
  9362. (defun org-search-not-self (group &rest args)
  9363. "Execute `re-search-forward', but only accept matches that do not
  9364. enclose the position of `org-open-link-marker'."
  9365. (let ((m org-open-link-marker))
  9366. (catch 'exit
  9367. (while (apply 're-search-forward args)
  9368. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9369. (goto-char (match-end group))
  9370. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9371. (> (match-beginning 0) (marker-position m))
  9372. (< (match-end 0) (marker-position m)))
  9373. (save-match-data
  9374. (or (not (org-in-regexp
  9375. org-bracket-link-analytic-regexp 1))
  9376. (not (match-end 4)) ; no description
  9377. (and (<= (match-beginning 4) (point))
  9378. (>= (match-end 4) (point))))))
  9379. (throw 'exit (point))))))))
  9380. (defun org-get-buffer-for-internal-link (buffer)
  9381. "Return a buffer to be used for displaying the link target of internal links."
  9382. (cond
  9383. ((not org-display-internal-link-with-indirect-buffer)
  9384. buffer)
  9385. ((string-match "(Clone)$" (buffer-name buffer))
  9386. (message "Buffer is already a clone, not making another one")
  9387. ;; we also do not modify visibility in this case
  9388. buffer)
  9389. (t ; make a new indirect buffer for displaying the link
  9390. (let* ((bn (buffer-name buffer))
  9391. (ibn (concat bn "(Clone)"))
  9392. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9393. (with-current-buffer ib (org-overview))
  9394. ib))))
  9395. (defun org-do-occur (regexp &optional cleanup)
  9396. "Call the Emacs command `occur'.
  9397. If CLEANUP is non-nil, remove the printout of the regular expression
  9398. in the *Occur* buffer. This is useful if the regex is long and not useful
  9399. to read."
  9400. (occur regexp)
  9401. (when cleanup
  9402. (let ((cwin (selected-window)) win beg end)
  9403. (when (setq win (get-buffer-window "*Occur*"))
  9404. (select-window win))
  9405. (goto-char (point-min))
  9406. (when (re-search-forward "match[a-z]+" nil t)
  9407. (setq beg (match-end 0))
  9408. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9409. (setq end (1- (match-beginning 0)))))
  9410. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9411. (goto-char (point-min))
  9412. (select-window cwin))))
  9413. ;;; The mark ring for links jumps
  9414. (defvar org-mark-ring nil
  9415. "Mark ring for positions before jumps in Org-mode.")
  9416. (defvar org-mark-ring-last-goto nil
  9417. "Last position in the mark ring used to go back.")
  9418. ;; Fill and close the ring
  9419. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9420. (loop for i from 1 to org-mark-ring-length do
  9421. (push (make-marker) org-mark-ring))
  9422. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9423. org-mark-ring)
  9424. (defun org-mark-ring-push (&optional pos buffer)
  9425. "Put the current position or POS into the mark ring and rotate it."
  9426. (interactive)
  9427. (setq pos (or pos (point)))
  9428. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9429. (move-marker (car org-mark-ring)
  9430. (or pos (point))
  9431. (or buffer (current-buffer)))
  9432. (message "%s"
  9433. (substitute-command-keys
  9434. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9435. (defun org-mark-ring-goto (&optional n)
  9436. "Jump to the previous position in the mark ring.
  9437. With prefix arg N, jump back that many stored positions. When
  9438. called several times in succession, walk through the entire ring.
  9439. Org-mode commands jumping to a different position in the current file,
  9440. or to another Org-mode file, automatically push the old position
  9441. onto the ring."
  9442. (interactive "p")
  9443. (let (p m)
  9444. (if (eq last-command this-command)
  9445. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9446. (setq p org-mark-ring))
  9447. (setq org-mark-ring-last-goto p)
  9448. (setq m (car p))
  9449. (org-pop-to-buffer-same-window (marker-buffer m))
  9450. (goto-char m)
  9451. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9452. (defun org-remove-angle-brackets (s)
  9453. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9454. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9455. s)
  9456. (defun org-add-angle-brackets (s)
  9457. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9458. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9459. s)
  9460. (defun org-remove-double-quotes (s)
  9461. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9462. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9463. s)
  9464. ;;; Following specific links
  9465. (defun org-follow-timestamp-link ()
  9466. "Open an agenda view for the time-stamp date/range at point."
  9467. (cond
  9468. ((org-at-date-range-p t)
  9469. (let ((org-agenda-start-on-weekday)
  9470. (t1 (match-string 1))
  9471. (t2 (match-string 2)) tt1 tt2)
  9472. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9473. tt2 (time-to-days (org-time-string-to-time t2)))
  9474. (let ((org-agenda-buffer-tmp-name
  9475. (format "*Org Agenda(a:%s)"
  9476. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9477. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9478. ((org-at-timestamp-p t)
  9479. (let ((org-agenda-buffer-tmp-name
  9480. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9481. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9482. (substring (match-string 1) 0 10)))
  9483. 1)))
  9484. (t (error "This should not happen"))))
  9485. ;;; Following file links
  9486. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9487. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9488. (declare-function mailcap-mime-info
  9489. "mailcap" (string &optional request no-decode))
  9490. (defvar org-wait nil)
  9491. (defun org-open-file (path &optional in-emacs line search)
  9492. "Open the file at PATH.
  9493. First, this expands any special file name abbreviations. Then the
  9494. configuration variable `org-file-apps' is checked if it contains an
  9495. entry for this file type, and if yes, the corresponding command is launched.
  9496. If no application is found, Emacs simply visits the file.
  9497. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9498. With a double \\[universal-argument] \\[universal-argument] \
  9499. prefix arg, Org tries to avoid opening in Emacs
  9500. and to use an external application to visit the file.
  9501. Optional LINE specifies a line to go to, optional SEARCH a string
  9502. to search for. If LINE or SEARCH is given, the file will be
  9503. opened in Emacs, unless an entry from org-file-apps that makes
  9504. use of groups in a regexp matches.
  9505. If you want to change the way frames are used when following a
  9506. link, please customize `org-link-frame-setup'.
  9507. If the file does not exist, an error is thrown."
  9508. (let* ((file (if (equal path "")
  9509. buffer-file-name
  9510. (substitute-in-file-name (expand-file-name path))))
  9511. (file-apps (append org-file-apps (org-default-apps)))
  9512. (apps (org-remove-if
  9513. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9514. (apps-dlink (org-remove-if-not
  9515. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9516. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9517. (dirp (if remp nil (file-directory-p file)))
  9518. (file (if (and dirp org-open-directory-means-index-dot-org)
  9519. (concat (file-name-as-directory file) "index.org")
  9520. file))
  9521. (a-m-a-p (assq 'auto-mode apps))
  9522. (dfile (downcase file))
  9523. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9524. (link (cond ((and (eq line nil)
  9525. (eq search nil))
  9526. file)
  9527. (line
  9528. (concat file "::" (number-to-string line)))
  9529. (search
  9530. (concat file "::" search))))
  9531. (dlink (downcase link))
  9532. (old-buffer (current-buffer))
  9533. (old-pos (point))
  9534. (old-mode major-mode)
  9535. ext cmd link-match-data)
  9536. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9537. (setq ext (match-string 1 dfile))
  9538. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9539. (setq ext (match-string 1 dfile))))
  9540. (cond
  9541. ((member in-emacs '((16) system))
  9542. (setq cmd (cdr (assoc 'system apps))))
  9543. (in-emacs (setq cmd 'emacs))
  9544. (t
  9545. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9546. (and dirp (cdr (assoc 'directory apps)))
  9547. ; first, try matching against apps-dlink
  9548. ; if we get a match here, store the match data for later
  9549. (let ((match (assoc-default dlink apps-dlink
  9550. 'string-match)))
  9551. (if match
  9552. (progn (setq link-match-data (match-data))
  9553. match)
  9554. (progn (setq in-emacs (or in-emacs line search))
  9555. nil))) ; if we have no match in apps-dlink,
  9556. ; always open the file in emacs if line or search
  9557. ; is given (for backwards compatibility)
  9558. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9559. 'string-match)
  9560. (cdr (assoc ext apps))
  9561. (cdr (assoc t apps))))))
  9562. (when (eq cmd 'system)
  9563. (setq cmd (cdr (assoc 'system apps))))
  9564. (when (eq cmd 'default)
  9565. (setq cmd (cdr (assoc t apps))))
  9566. (when (eq cmd 'mailcap)
  9567. (require 'mailcap)
  9568. (mailcap-parse-mailcaps)
  9569. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9570. (command (mailcap-mime-info mime-type)))
  9571. (if (stringp command)
  9572. (setq cmd command)
  9573. (setq cmd 'emacs))))
  9574. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9575. (not (file-exists-p file))
  9576. (not org-open-non-existing-files))
  9577. (error "No such file: %s" file))
  9578. (cond
  9579. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9580. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9581. (while (string-match "['\"]%s['\"]" cmd)
  9582. (setq cmd (replace-match "%s" t t cmd)))
  9583. (while (string-match "%s" cmd)
  9584. (setq cmd (replace-match
  9585. (save-match-data
  9586. (shell-quote-argument
  9587. (convert-standard-filename file)))
  9588. t t cmd)))
  9589. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9590. (save-match-data
  9591. (let ((match-index 1)
  9592. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9593. (set-match-data link-match-data)
  9594. (while (<= match-index number-of-groups)
  9595. (let ((regex (concat "%" (number-to-string match-index)))
  9596. (replace-with (match-string match-index dlink)))
  9597. (while (string-match regex cmd)
  9598. (setq cmd (replace-match replace-with t t cmd))))
  9599. (setq match-index (+ match-index 1)))))
  9600. (save-window-excursion
  9601. (message "Running %s...done" cmd)
  9602. (start-process-shell-command cmd nil cmd)
  9603. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9604. ((or (stringp cmd)
  9605. (eq cmd 'emacs))
  9606. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9607. (widen)
  9608. (if line (org-goto-line line)
  9609. (if search (org-link-search search))))
  9610. ((consp cmd)
  9611. (let ((file (convert-standard-filename file)))
  9612. (save-match-data
  9613. (set-match-data link-match-data)
  9614. (eval cmd))))
  9615. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9616. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9617. (or (not (equal old-buffer (current-buffer)))
  9618. (not (equal old-pos (point))))
  9619. (org-mark-ring-push old-pos old-buffer))))
  9620. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9621. "This function returns non-nil if `entry' uses a regular
  9622. expression which should be matched against the whole link by
  9623. org-open-file.
  9624. It assumes that is the case when the entry uses a regular
  9625. expression which has at least one grouping construct and the
  9626. action is either a lisp form or a command string containing
  9627. '%1', i.e. using at least one subexpression match as a
  9628. parameter."
  9629. (let ((selector (car entry))
  9630. (action (cdr entry)))
  9631. (if (stringp selector)
  9632. (and (> (regexp-opt-depth selector) 0)
  9633. (or (and (stringp action)
  9634. (string-match "%[0-9]" action))
  9635. (consp action)))
  9636. nil)))
  9637. (defun org-default-apps ()
  9638. "Return the default applications for this operating system."
  9639. (cond
  9640. ((eq system-type 'darwin)
  9641. org-file-apps-defaults-macosx)
  9642. ((eq system-type 'windows-nt)
  9643. org-file-apps-defaults-windowsnt)
  9644. (t org-file-apps-defaults-gnu)))
  9645. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9646. "Convert extensions to regular expressions in the cars of LIST.
  9647. Also, weed out any non-string entries, because the return value is used
  9648. only for regexp matching.
  9649. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9650. point to the symbol `emacs', indicating that the file should
  9651. be opened in Emacs."
  9652. (append
  9653. (delq nil
  9654. (mapcar (lambda (x)
  9655. (if (not (stringp (car x)))
  9656. nil
  9657. (if (string-match "\\W" (car x))
  9658. x
  9659. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9660. list))
  9661. (if add-auto-mode
  9662. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9663. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9664. (defun org-file-remote-p (file)
  9665. "Test whether FILE specifies a location on a remote system.
  9666. Return non-nil if the location is indeed remote.
  9667. For example, the filename \"/user@host:/foo\" specifies a location
  9668. on the system \"/user@host:\"."
  9669. (cond ((fboundp 'file-remote-p)
  9670. (file-remote-p file))
  9671. ((fboundp 'tramp-handle-file-remote-p)
  9672. (tramp-handle-file-remote-p file))
  9673. ((and (boundp 'ange-ftp-name-format)
  9674. (string-match (car ange-ftp-name-format) file))
  9675. t)))
  9676. ;;;; Refiling
  9677. (defun org-get-org-file ()
  9678. "Read a filename, with default directory `org-directory'."
  9679. (let ((default (or org-default-notes-file remember-data-file)))
  9680. (read-file-name (format "File name [%s]: " default)
  9681. (file-name-as-directory org-directory)
  9682. default)))
  9683. (defun org-notes-order-reversed-p ()
  9684. "Check if the current file should receive notes in reversed order."
  9685. (cond
  9686. ((not org-reverse-note-order) nil)
  9687. ((eq t org-reverse-note-order) t)
  9688. ((not (listp org-reverse-note-order)) nil)
  9689. (t (catch 'exit
  9690. (let ((all org-reverse-note-order)
  9691. entry)
  9692. (while (setq entry (pop all))
  9693. (if (string-match (car entry) buffer-file-name)
  9694. (throw 'exit (cdr entry))))
  9695. nil)))))
  9696. (defvar org-refile-target-table nil
  9697. "The list of refile targets, created by `org-refile'.")
  9698. (defvar org-agenda-new-buffers nil
  9699. "Buffers created to visit agenda files.")
  9700. (defvar org-refile-cache nil
  9701. "Cache for refile targets.")
  9702. (defvar org-refile-markers nil
  9703. "All the markers used for caching refile locations.")
  9704. (defun org-refile-marker (pos)
  9705. "Get a new refile marker, but only if caching is in use."
  9706. (if (not org-refile-use-cache)
  9707. pos
  9708. (let ((m (make-marker)))
  9709. (move-marker m pos)
  9710. (push m org-refile-markers)
  9711. m)))
  9712. (defun org-refile-cache-clear ()
  9713. "Clear the refile cache and disable all the markers."
  9714. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9715. (setq org-refile-markers nil)
  9716. (setq org-refile-cache nil)
  9717. (message "Refile cache has been cleared"))
  9718. (defun org-refile-cache-check-set (set)
  9719. "Check if all the markers in the cache still have live buffers."
  9720. (let (marker)
  9721. (catch 'exit
  9722. (while (and set (setq marker (nth 3 (pop set))))
  9723. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9724. (when (and marker (null (marker-buffer marker)))
  9725. (message "not found") (sit-for 3)
  9726. (throw 'exit nil)))
  9727. t)))
  9728. (defun org-refile-cache-put (set &rest identifiers)
  9729. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9730. (let* ((key (sha1 (prin1-to-string identifiers)))
  9731. (entry (assoc key org-refile-cache)))
  9732. (if entry
  9733. (setcdr entry set)
  9734. (push (cons key set) org-refile-cache))))
  9735. (defun org-refile-cache-get (&rest identifiers)
  9736. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9737. (cond
  9738. ((not org-refile-cache) nil)
  9739. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9740. (t
  9741. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9742. org-refile-cache))))
  9743. (and set (org-refile-cache-check-set set) set)))))
  9744. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9745. "Produce a table with refile targets."
  9746. (let ((case-fold-search nil)
  9747. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9748. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9749. targets tgs txt re files f desc descre fast-path-p level pos0)
  9750. (message "Getting targets...")
  9751. (with-current-buffer (or default-buffer (current-buffer))
  9752. (while (setq entry (pop entries))
  9753. (setq files (car entry) desc (cdr entry))
  9754. (setq fast-path-p nil)
  9755. (cond
  9756. ((null files) (setq files (list (current-buffer))))
  9757. ((eq files 'org-agenda-files)
  9758. (setq files (org-agenda-files 'unrestricted)))
  9759. ((and (symbolp files) (fboundp files))
  9760. (setq files (funcall files)))
  9761. ((and (symbolp files) (boundp files))
  9762. (setq files (symbol-value files))))
  9763. (if (stringp files) (setq files (list files)))
  9764. (cond
  9765. ((eq (car desc) :tag)
  9766. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9767. ((eq (car desc) :todo)
  9768. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9769. ((eq (car desc) :regexp)
  9770. (setq descre (cdr desc)))
  9771. ((eq (car desc) :level)
  9772. (setq descre (concat "^\\*\\{" (number-to-string
  9773. (if org-odd-levels-only
  9774. (1- (* 2 (cdr desc)))
  9775. (cdr desc)))
  9776. "\\}[ \t]")))
  9777. ((eq (car desc) :maxlevel)
  9778. (setq fast-path-p t)
  9779. (setq descre (concat "^\\*\\{1," (number-to-string
  9780. (if org-odd-levels-only
  9781. (1- (* 2 (cdr desc)))
  9782. (cdr desc)))
  9783. "\\}[ \t]")))
  9784. (t (error "Bad refiling target description %s" desc)))
  9785. (while (setq f (pop files))
  9786. (with-current-buffer
  9787. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9788. (or
  9789. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9790. (progn
  9791. (if (bufferp f) (setq f (buffer-file-name
  9792. (buffer-base-buffer f))))
  9793. (setq f (and f (expand-file-name f)))
  9794. (if (eq org-refile-use-outline-path 'file)
  9795. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9796. (save-excursion
  9797. (save-restriction
  9798. (widen)
  9799. (goto-char (point-min))
  9800. (while (re-search-forward descre nil t)
  9801. (goto-char (setq pos0 (point-at-bol)))
  9802. (catch 'next
  9803. (when org-refile-target-verify-function
  9804. (save-match-data
  9805. (or (funcall org-refile-target-verify-function)
  9806. (throw 'next t))))
  9807. (when (and (looking-at org-complex-heading-regexp)
  9808. (not (member (match-string 4) excluded-entries))
  9809. (match-string 4))
  9810. (setq level (org-reduced-level
  9811. (- (match-end 1) (match-beginning 1)))
  9812. txt (org-link-display-format (match-string 4))
  9813. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9814. re (format org-complex-heading-regexp-format
  9815. (regexp-quote (match-string 4))))
  9816. (when org-refile-use-outline-path
  9817. (setq txt (mapconcat
  9818. 'org-protect-slash
  9819. (append
  9820. (if (eq org-refile-use-outline-path
  9821. 'file)
  9822. (list (file-name-nondirectory
  9823. (buffer-file-name
  9824. (buffer-base-buffer))))
  9825. (if (eq org-refile-use-outline-path
  9826. 'full-file-path)
  9827. (list (buffer-file-name
  9828. (buffer-base-buffer)))))
  9829. (org-get-outline-path fast-path-p
  9830. level txt)
  9831. (list txt))
  9832. "/")))
  9833. (push (list txt f re (org-refile-marker (point)))
  9834. tgs)))
  9835. (when (= (point) pos0)
  9836. ;; verification function has not moved point
  9837. (goto-char (point-at-eol))))))))
  9838. (when org-refile-use-cache
  9839. (org-refile-cache-put tgs (buffer-file-name) descre))
  9840. (setq targets (append tgs targets))
  9841. ))))
  9842. (message "Getting targets...done")
  9843. (nreverse targets)))
  9844. (defun org-protect-slash (s)
  9845. (while (string-match "/" s)
  9846. (setq s (replace-match "\\" t t s)))
  9847. s)
  9848. (defvar org-olpa (make-vector 20 nil))
  9849. (defun org-get-outline-path (&optional fastp level heading)
  9850. "Return the outline path to the current entry, as a list.
  9851. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9852. routine which makes outline path derivations for an entire file,
  9853. avoiding backtracing. Refile target collection makes use of that."
  9854. (if fastp
  9855. (progn
  9856. (if (> level 19)
  9857. (error "Outline path failure, more than 19 levels"))
  9858. (loop for i from level upto 19 do
  9859. (aset org-olpa i nil))
  9860. (prog1
  9861. (delq nil (append org-olpa nil))
  9862. (aset org-olpa level heading)))
  9863. (let (rtn case-fold-search)
  9864. (save-excursion
  9865. (save-restriction
  9866. (widen)
  9867. (while (org-up-heading-safe)
  9868. (when (looking-at org-complex-heading-regexp)
  9869. (push (org-match-string-no-properties 4) rtn)))
  9870. rtn)))))
  9871. (defun org-format-outline-path (path &optional width prefix separator)
  9872. "Format the outline path PATH for display.
  9873. WIDTH is the maximum number of characters that is available.
  9874. PREFIX is a prefix to be included in the returned string,
  9875. such as the file name.
  9876. SEPARATOR is inserted between the different parts of the path,
  9877. the default is \"/\"."
  9878. (setq width (or width 79))
  9879. (if prefix (setq width (- width (length prefix))))
  9880. (if (not path)
  9881. (or prefix "")
  9882. (let* ((nsteps (length path))
  9883. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9884. (maxwidth (if (<= total-width width)
  9885. 10000 ;; everything fits
  9886. ;; we need to shorten the level headings
  9887. (/ (- width nsteps) nsteps)))
  9888. (org-odd-levels-only nil)
  9889. (n 0)
  9890. (total (1+ (length prefix))))
  9891. (setq maxwidth (max maxwidth 10))
  9892. (concat prefix
  9893. (if prefix (or separator "/"))
  9894. (mapconcat
  9895. (lambda (h)
  9896. (setq n (1+ n))
  9897. (if (and (= n nsteps) (< maxwidth 10000))
  9898. (setq maxwidth (- total-width total)))
  9899. (if (< (length h) maxwidth)
  9900. (progn (setq total (+ total (length h) 1)) h)
  9901. (setq h (substring h 0 (- maxwidth 2))
  9902. total (+ total maxwidth 1))
  9903. (if (string-match "[ \t]+\\'" h)
  9904. (setq h (substring h 0 (match-beginning 0))))
  9905. (setq h (concat h "..")))
  9906. (org-add-props h nil 'face
  9907. (nth (% (1- n) org-n-level-faces)
  9908. org-level-faces))
  9909. h)
  9910. path (or separator "/"))))))
  9911. (defun org-display-outline-path (&optional file current separator just-return-string)
  9912. "Display the current outline path in the echo area.
  9913. If FILE is non-nil, prepend the output with the file name.
  9914. If CURRENT is non-nil, append the current heading to the output.
  9915. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9916. the different parts of the path and defaults to \"/\".
  9917. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9918. (interactive "P")
  9919. (let* (case-fold-search
  9920. message-log-max ; Don't populate the *Messages* buffer
  9921. (bfn (buffer-file-name (buffer-base-buffer)))
  9922. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9923. res)
  9924. (if current (setq path (append path
  9925. (save-excursion
  9926. (org-back-to-heading t)
  9927. (if (looking-at org-complex-heading-regexp)
  9928. (list (match-string 4)))))))
  9929. (setq res
  9930. (org-format-outline-path
  9931. path
  9932. (1- (frame-width))
  9933. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9934. separator))
  9935. (if just-return-string
  9936. (org-no-properties res)
  9937. (message "%s" res))))
  9938. (defvar org-refile-history nil
  9939. "History for refiling operations.")
  9940. (defvar org-after-refile-insert-hook nil
  9941. "Hook run after `org-refile' has inserted its stuff at the new location.
  9942. Note that this is still *before* the stuff will be removed from
  9943. the *old* location.")
  9944. (defvar org-capture-last-stored-marker)
  9945. (defvar org-refile-keep nil
  9946. "Non-nil means `org-refile' will copy instead of refile.")
  9947. (defun org-copy ()
  9948. "Like `org-refile', but copy."
  9949. (interactive)
  9950. (let ((org-refile-keep t))
  9951. (funcall 'org-refile nil nil nil "Copy")))
  9952. (defun org-refile (&optional goto default-buffer rfloc msg)
  9953. "Move the entry or entries at point to another heading.
  9954. The list of target headings is compiled using the information in
  9955. `org-refile-targets', which see.
  9956. At the target location, the entry is filed as a subitem of the target
  9957. heading. Depending on `org-reverse-note-order', the new subitem will
  9958. either be the first or the last subitem.
  9959. If there is an active region, all entries in that region will be moved.
  9960. However, the region must fulfill the requirement that the first heading
  9961. is the first one sets the top-level of the moved text - at most siblings
  9962. below it are allowed.
  9963. With prefix arg GOTO, the command will only visit the target location
  9964. and not actually move anything.
  9965. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9966. go to the location where the last refiling operation has put the subtree.
  9967. With a prefix argument of `2', refile to the running clock.
  9968. RFLOC can be a refile location obtained in a different way.
  9969. MSG is a string to replace \"Refile\" in the default prompt with
  9970. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9971. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9972. If you are using target caching (see `org-refile-use-cache'),
  9973. you have to clear the target cache in order to find new targets.
  9974. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9975. prefix argument (`C-u C-u C-u C-c C-w')."
  9976. (interactive "P")
  9977. (if (member goto '(0 (64)))
  9978. (org-refile-cache-clear)
  9979. (let* ((actionmsg (or msg "Refile"))
  9980. (cbuf (current-buffer))
  9981. (regionp (org-region-active-p))
  9982. (region-start (and regionp (region-beginning)))
  9983. (region-end (and regionp (region-end)))
  9984. (region-length (and regionp (- region-end region-start)))
  9985. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9986. pos it nbuf file re level reversed)
  9987. (setq last-command nil)
  9988. (when regionp
  9989. (goto-char region-start)
  9990. (or (bolp) (goto-char (point-at-bol)))
  9991. (setq region-start (point))
  9992. (unless (or (org-kill-is-subtree-p
  9993. (buffer-substring region-start region-end))
  9994. (prog1 org-refile-active-region-within-subtree
  9995. (org-toggle-heading)))
  9996. (error "The region is not a (sequence of) subtree(s)")))
  9997. (if (equal goto '(16))
  9998. (org-refile-goto-last-stored)
  9999. (when (or
  10000. (and (equal goto 2)
  10001. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10002. (prog1
  10003. (setq it (list (or org-clock-heading "running clock")
  10004. (buffer-file-name
  10005. (marker-buffer org-clock-hd-marker))
  10006. ""
  10007. (marker-position org-clock-hd-marker)))
  10008. (setq goto nil)))
  10009. (setq it (or rfloc
  10010. (let (heading-text)
  10011. (save-excursion
  10012. (unless goto
  10013. (org-back-to-heading t)
  10014. (setq heading-text
  10015. (nth 4 (org-heading-components))))
  10016. (org-refile-get-location
  10017. (cond (goto "Goto")
  10018. (regionp (concat actionmsg " region to"))
  10019. (t (concat actionmsg " subtree \""
  10020. heading-text "\" to")))
  10021. default-buffer
  10022. (and (not (equal '(4) goto))
  10023. org-refile-allow-creating-parent-nodes)
  10024. goto))))))
  10025. (setq file (nth 1 it)
  10026. re (nth 2 it)
  10027. pos (nth 3 it))
  10028. (if (and (not goto)
  10029. pos
  10030. (equal (buffer-file-name) file)
  10031. (if regionp
  10032. (and (>= pos region-start)
  10033. (<= pos region-end))
  10034. (and (>= pos (point))
  10035. (< pos (save-excursion
  10036. (org-end-of-subtree t t))))))
  10037. (error "Cannot refile to position inside the tree or region"))
  10038. (setq nbuf (or (find-buffer-visiting file)
  10039. (find-file-noselect file)))
  10040. (if goto
  10041. (progn
  10042. (org-pop-to-buffer-same-window nbuf)
  10043. (goto-char pos)
  10044. (org-show-context 'org-goto))
  10045. (if regionp
  10046. (progn
  10047. (org-kill-new (buffer-substring region-start region-end))
  10048. (org-save-markers-in-region region-start region-end))
  10049. (org-copy-subtree 1 nil t))
  10050. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10051. (find-file-noselect file)))
  10052. (setq reversed (org-notes-order-reversed-p))
  10053. (save-excursion
  10054. (save-restriction
  10055. (widen)
  10056. (if pos
  10057. (progn
  10058. (goto-char pos)
  10059. (looking-at org-outline-regexp)
  10060. (setq level (org-get-valid-level (funcall outline-level) 1))
  10061. (goto-char
  10062. (if reversed
  10063. (or (outline-next-heading) (point-max))
  10064. (or (save-excursion (org-get-next-sibling))
  10065. (org-end-of-subtree t t)
  10066. (point-max)))))
  10067. (setq level 1)
  10068. (if (not reversed)
  10069. (goto-char (point-max))
  10070. (goto-char (point-min))
  10071. (or (outline-next-heading) (goto-char (point-max)))))
  10072. (if (not (bolp)) (newline))
  10073. (org-paste-subtree level)
  10074. (when org-log-refile
  10075. (org-add-log-setup 'refile nil nil 'findpos
  10076. org-log-refile)
  10077. (unless (eq org-log-refile 'note)
  10078. (save-excursion (org-add-log-note))))
  10079. (and org-auto-align-tags
  10080. (let ((org-loop-over-headlines-in-active-region nil))
  10081. (org-set-tags nil t)))
  10082. (with-demoted-errors
  10083. (bookmark-set "org-refile-last-stored"))
  10084. ;; If we are refiling for capture, make sure that the
  10085. ;; last-capture pointers point here
  10086. (when (org-bound-and-true-p org-refile-for-capture)
  10087. (with-demoted-errors
  10088. (bookmark-set "org-capture-last-stored-marker"))
  10089. (move-marker org-capture-last-stored-marker (point)))
  10090. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10091. (run-hooks 'org-after-refile-insert-hook))))
  10092. (unless org-refile-keep
  10093. (if regionp
  10094. (delete-region (point) (+ (point) region-length))
  10095. (org-cut-subtree)))
  10096. (when (featurep 'org-inlinetask)
  10097. (org-inlinetask-remove-END-maybe))
  10098. (setq org-markers-to-move nil)
  10099. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10100. (defun org-refile-goto-last-stored ()
  10101. "Go to the location where the last refile was stored."
  10102. (interactive)
  10103. (bookmark-jump "org-refile-last-stored")
  10104. (message "This is the location of the last refile"))
  10105. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10106. no-exclude)
  10107. "Prompt the user for a refile location, using PROMPT.
  10108. PROMPT should not be suffixed with a colon and a space, because
  10109. this function appends the default value from
  10110. `org-refile-history' automatically, if that is not empty.
  10111. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10112. this is used for the GOTO interface."
  10113. (let ((org-refile-targets org-refile-targets)
  10114. (org-refile-use-outline-path org-refile-use-outline-path)
  10115. excluded-entries)
  10116. (when (and (derived-mode-p 'org-mode)
  10117. (not org-refile-use-cache)
  10118. (not no-exclude))
  10119. (org-map-tree
  10120. (lambda()
  10121. (setq excluded-entries
  10122. (append excluded-entries (list (org-get-heading t t)))))))
  10123. (setq org-refile-target-table
  10124. (org-refile-get-targets default-buffer excluded-entries)))
  10125. (unless org-refile-target-table
  10126. (error "No refile targets"))
  10127. (let* ((cbuf (current-buffer))
  10128. (partial-completion-mode nil)
  10129. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10130. (cfunc (if (and org-refile-use-outline-path
  10131. org-outline-path-complete-in-steps)
  10132. 'org-olpath-completing-read
  10133. 'org-icompleting-read))
  10134. (extra (if org-refile-use-outline-path "/" ""))
  10135. (cbnex (concat (buffer-name) extra))
  10136. (filename (and cfn (expand-file-name cfn)))
  10137. (tbl (mapcar
  10138. (lambda (x)
  10139. (if (and (not (member org-refile-use-outline-path
  10140. '(file full-file-path)))
  10141. (not (equal filename (nth 1 x))))
  10142. (cons (concat (car x) extra " ("
  10143. (file-name-nondirectory (nth 1 x)) ")")
  10144. (cdr x))
  10145. (cons (concat (car x) extra) (cdr x))))
  10146. org-refile-target-table))
  10147. (completion-ignore-case t)
  10148. cdef
  10149. (prompt (concat prompt
  10150. (or (and (car org-refile-history)
  10151. (concat " (default " (car org-refile-history) ")"))
  10152. (and (assoc cbnex tbl) (setq cdef cbnex)
  10153. (concat " (default " cbnex ")"))) ": "))
  10154. pa answ parent-target child parent old-hist)
  10155. (setq old-hist org-refile-history)
  10156. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10157. nil 'org-refile-history (or cdef (car org-refile-history))))
  10158. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10159. (org-refile-check-position pa)
  10160. (if pa
  10161. (progn
  10162. (when (or (not org-refile-history)
  10163. (not (eq old-hist org-refile-history))
  10164. (not (equal (car pa) (car org-refile-history))))
  10165. (setq org-refile-history
  10166. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10167. org-refile-history
  10168. (cdr org-refile-history))))
  10169. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10170. (pop org-refile-history)))
  10171. pa)
  10172. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10173. (progn
  10174. (setq parent (match-string 1 answ)
  10175. child (match-string 2 answ))
  10176. (setq parent-target (or (assoc parent tbl)
  10177. (assoc (concat parent "/") tbl)))
  10178. (when (and parent-target
  10179. (or (eq new-nodes t)
  10180. (and (eq new-nodes 'confirm)
  10181. (y-or-n-p (format "Create new node \"%s\"? "
  10182. child)))))
  10183. (org-refile-new-child parent-target child)))
  10184. (error "Invalid target location")))))
  10185. (declare-function org-string-nw-p "org-macs" (s))
  10186. (defun org-refile-check-position (refile-pointer)
  10187. "Check if the refile pointer matches the headline to which it points."
  10188. (let* ((file (nth 1 refile-pointer))
  10189. (re (nth 2 refile-pointer))
  10190. (pos (nth 3 refile-pointer))
  10191. buffer)
  10192. (if (and (not (markerp pos)) (not file))
  10193. (error "Please save the buffer to a file before refiling")
  10194. (when (org-string-nw-p re)
  10195. (setq buffer (if (markerp pos)
  10196. (marker-buffer pos)
  10197. (or (find-buffer-visiting file)
  10198. (find-file-noselect file))))
  10199. (with-current-buffer buffer
  10200. (save-excursion
  10201. (save-restriction
  10202. (widen)
  10203. (goto-char pos)
  10204. (beginning-of-line 1)
  10205. (unless (org-looking-at-p re)
  10206. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10207. (defun org-refile-new-child (parent-target child)
  10208. "Use refile target PARENT-TARGET to add new CHILD below it."
  10209. (unless parent-target
  10210. (error "Cannot find parent for new node"))
  10211. (let ((file (nth 1 parent-target))
  10212. (pos (nth 3 parent-target))
  10213. level)
  10214. (with-current-buffer (or (find-buffer-visiting file)
  10215. (find-file-noselect file))
  10216. (save-excursion
  10217. (save-restriction
  10218. (widen)
  10219. (if pos
  10220. (goto-char pos)
  10221. (goto-char (point-max))
  10222. (if (not (bolp)) (newline)))
  10223. (when (looking-at org-outline-regexp)
  10224. (setq level (funcall outline-level))
  10225. (org-end-of-subtree t t))
  10226. (org-back-over-empty-lines)
  10227. (insert "\n" (make-string
  10228. (if pos (org-get-valid-level level 1) 1) ?*)
  10229. " " child "\n")
  10230. (beginning-of-line 0)
  10231. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10232. (defun org-olpath-completing-read (prompt collection &rest args)
  10233. "Read an outline path like a file name."
  10234. (let ((thetable collection)
  10235. (org-completion-use-ido nil) ; does not work with ido.
  10236. (org-completion-use-iswitchb nil)) ; or iswitchb
  10237. (apply
  10238. 'org-icompleting-read prompt
  10239. (lambda (string predicate &optional flag)
  10240. (let (rtn r f (l (length string)))
  10241. (cond
  10242. ((eq flag nil)
  10243. ;; try completion
  10244. (try-completion string thetable))
  10245. ((eq flag t)
  10246. ;; all-completions
  10247. (setq rtn (all-completions string thetable predicate))
  10248. (mapcar
  10249. (lambda (x)
  10250. (setq r (substring x l))
  10251. (if (string-match " ([^)]*)$" x)
  10252. (setq f (match-string 0 x))
  10253. (setq f ""))
  10254. (if (string-match "/" r)
  10255. (concat string (substring r 0 (match-end 0)) f)
  10256. x))
  10257. rtn))
  10258. ((eq flag 'lambda)
  10259. ;; exact match?
  10260. (assoc string thetable)))))
  10261. args)))
  10262. ;;;; Dynamic blocks
  10263. (defun org-find-dblock (name)
  10264. "Find the first dynamic block with name NAME in the buffer.
  10265. If not found, stay at current position and return nil."
  10266. (let ((case-fold-search t) pos)
  10267. (save-excursion
  10268. (goto-char (point-min))
  10269. (setq pos (and (re-search-forward
  10270. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10271. (match-beginning 0))))
  10272. (if pos (goto-char pos))
  10273. pos))
  10274. (defconst org-dblock-start-re
  10275. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10276. "Matches the start line of a dynamic block, with parameters.")
  10277. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10278. "Matches the end of a dynamic block.")
  10279. (defun org-create-dblock (plist)
  10280. "Create a dynamic block section, with parameters taken from PLIST.
  10281. PLIST must contain a :name entry which is used as name of the block."
  10282. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10283. (end-of-line 1)
  10284. (newline))
  10285. (let ((col (current-column))
  10286. (name (plist-get plist :name)))
  10287. (insert "#+BEGIN: " name)
  10288. (while plist
  10289. (if (eq (car plist) :name)
  10290. (setq plist (cddr plist))
  10291. (insert " " (prin1-to-string (pop plist)))))
  10292. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10293. (beginning-of-line -2)))
  10294. (defun org-prepare-dblock ()
  10295. "Prepare dynamic block for refresh.
  10296. This empties the block, puts the cursor at the insert position and returns
  10297. the property list including an extra property :name with the block name."
  10298. (unless (looking-at org-dblock-start-re)
  10299. (error "Not at a dynamic block"))
  10300. (let* ((begdel (1+ (match-end 0)))
  10301. (name (org-no-properties (match-string 1)))
  10302. (params (append (list :name name)
  10303. (read (concat "(" (match-string 3) ")")))))
  10304. (save-excursion
  10305. (beginning-of-line 1)
  10306. (skip-chars-forward " \t")
  10307. (setq params (plist-put params :indentation-column (current-column))))
  10308. (unless (re-search-forward org-dblock-end-re nil t)
  10309. (error "Dynamic block not terminated"))
  10310. (setq params
  10311. (append params
  10312. (list :content (buffer-substring
  10313. begdel (match-beginning 0)))))
  10314. (delete-region begdel (match-beginning 0))
  10315. (goto-char begdel)
  10316. (open-line 1)
  10317. params))
  10318. (defun org-map-dblocks (&optional command)
  10319. "Apply COMMAND to all dynamic blocks in the current buffer.
  10320. If COMMAND is not given, use `org-update-dblock'."
  10321. (let ((cmd (or command 'org-update-dblock)))
  10322. (save-excursion
  10323. (goto-char (point-min))
  10324. (while (re-search-forward org-dblock-start-re nil t)
  10325. (goto-char (match-beginning 0))
  10326. (save-excursion
  10327. (condition-case nil
  10328. (funcall cmd)
  10329. (error (message "Error during update of dynamic block"))))
  10330. (unless (re-search-forward org-dblock-end-re nil t)
  10331. (error "Dynamic block not terminated"))))))
  10332. (defun org-dblock-update (&optional arg)
  10333. "User command for updating dynamic blocks.
  10334. Update the dynamic block at point. With prefix ARG, update all dynamic
  10335. blocks in the buffer."
  10336. (interactive "P")
  10337. (if arg
  10338. (org-update-all-dblocks)
  10339. (or (looking-at org-dblock-start-re)
  10340. (org-beginning-of-dblock))
  10341. (org-update-dblock)))
  10342. (defun org-update-dblock ()
  10343. "Update the dynamic block at point.
  10344. This means to empty the block, parse for parameters and then call
  10345. the correct writing function."
  10346. (interactive)
  10347. (save-window-excursion
  10348. (let* ((pos (point))
  10349. (line (org-current-line))
  10350. (params (org-prepare-dblock))
  10351. (name (plist-get params :name))
  10352. (indent (plist-get params :indentation-column))
  10353. (cmd (intern (concat "org-dblock-write:" name))))
  10354. (message "Updating dynamic block `%s' at line %d..." name line)
  10355. (funcall cmd params)
  10356. (message "Updating dynamic block `%s' at line %d...done" name line)
  10357. (goto-char pos)
  10358. (when (and indent (> indent 0))
  10359. (setq indent (make-string indent ?\ ))
  10360. (save-excursion
  10361. (org-beginning-of-dblock)
  10362. (forward-line 1)
  10363. (while (not (looking-at org-dblock-end-re))
  10364. (insert indent)
  10365. (beginning-of-line 2))
  10366. (when (looking-at org-dblock-end-re)
  10367. (and (looking-at "[ \t]+")
  10368. (replace-match ""))
  10369. (insert indent)))))))
  10370. (defun org-beginning-of-dblock ()
  10371. "Find the beginning of the dynamic block at point.
  10372. Error if there is no such block at point."
  10373. (let ((pos (point))
  10374. beg)
  10375. (end-of-line 1)
  10376. (if (and (re-search-backward org-dblock-start-re nil t)
  10377. (setq beg (match-beginning 0))
  10378. (re-search-forward org-dblock-end-re nil t)
  10379. (> (match-end 0) pos))
  10380. (goto-char beg)
  10381. (goto-char pos)
  10382. (error "Not in a dynamic block"))))
  10383. (defun org-update-all-dblocks ()
  10384. "Update all dynamic blocks in the buffer.
  10385. This function can be used in a hook."
  10386. (interactive)
  10387. (when (derived-mode-p 'org-mode)
  10388. (org-map-dblocks 'org-update-dblock)))
  10389. ;;;; Completion
  10390. (defconst org-additional-option-like-keywords
  10391. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10392. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10393. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10394. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10395. "BEGIN:" "END:"
  10396. "ORGTBL" "TBLFM:" "TBLNAME:"
  10397. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10398. "BEGIN_VERBATIM" "END_VERBATIM"
  10399. "BEGIN_QUOTE" "END_QUOTE"
  10400. "BEGIN_VERSE" "END_VERSE"
  10401. "BEGIN_CENTER" "END_CENTER"
  10402. "BEGIN_SRC" "END_SRC"
  10403. "BEGIN_RESULT" "END_RESULT"
  10404. "BEGIN_lstlisting" "END_lstlisting"
  10405. "NAME:" "RESULTS:"
  10406. "HEADER:" "HEADERS:"
  10407. "COLUMNS:" "PROPERTY:"
  10408. "CAPTION:" "LABEL:"
  10409. "SETUPFILE:"
  10410. "INCLUDE:" "INDEX:"
  10411. "BIND:"
  10412. "MACRO:"))
  10413. (defconst org-options-keywords
  10414. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10415. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10416. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10417. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10418. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10419. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10420. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10421. (defconst org-additional-option-like-keywords-for-flyspell
  10422. (delete-dups
  10423. (split-string
  10424. (mapconcat (lambda(k)
  10425. (replace-regexp-in-string
  10426. "_\\|:" " "
  10427. (concat k " " (downcase k) " " (upcase k))))
  10428. (append org-options-keywords org-additional-option-like-keywords)
  10429. " ")
  10430. " +" t)))
  10431. (defcustom org-structure-template-alist
  10432. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10433. "<src lang=\"?\">\n\n</src>")
  10434. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10435. "<example>\n?\n</example>")
  10436. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10437. "<quote>\n?\n</quote>")
  10438. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10439. "<verse>\n?\n</verse>")
  10440. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10441. "<verbatim>\n?\n</verbatim>")
  10442. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10443. "<center>\n?\n</center>")
  10444. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10445. "<literal style=\"latex\">\n?\n</literal>")
  10446. ("L" "#+LaTeX: "
  10447. "<literal style=\"latex\">?</literal>")
  10448. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10449. "<literal style=\"html\">\n?\n</literal>")
  10450. ("H" "#+HTML: "
  10451. "<literal style=\"html\">?</literal>")
  10452. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10453. ("A" "#+ASCII: ")
  10454. ("i" "#+INDEX: ?"
  10455. "#+INDEX: ?")
  10456. ("I" "#+INCLUDE: %file ?"
  10457. "<include file=%file markup=\"?\">"))
  10458. "Structure completion elements.
  10459. This is a list of abbreviation keys and values. The value gets inserted
  10460. if you type `<' followed by the key and then press the completion key,
  10461. usually `M-TAB'. %file will be replaced by a file name after prompting
  10462. for the file using completion. The cursor will be placed at the position
  10463. of the `?` in the template.
  10464. There are two templates for each key, the first uses the original Org syntax,
  10465. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10466. the default when the /org-mtags.el/ module has been loaded. See also the
  10467. variable `org-mtags-prefer-muse-templates'."
  10468. :group 'org-completion
  10469. :type '(repeat
  10470. (string :tag "Key")
  10471. (string :tag "Template")
  10472. (string :tag "Muse Template")))
  10473. (defun org-try-structure-completion ()
  10474. "Try to complete a structure template before point.
  10475. This looks for strings like \"<e\" on an otherwise empty line and
  10476. expands them."
  10477. (let ((l (buffer-substring (point-at-bol) (point)))
  10478. a)
  10479. (when (and (looking-at "[ \t]*$")
  10480. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10481. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10482. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10483. (match-beginning 1)) a)
  10484. t)))
  10485. (defun org-complete-expand-structure-template (start cell)
  10486. "Expand a structure template."
  10487. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10488. (rpl (nth (if musep 2 1) cell))
  10489. (ind ""))
  10490. (delete-region start (point))
  10491. (when (string-match "\\`#\\+" rpl)
  10492. (cond
  10493. ((bolp))
  10494. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10495. (setq ind (buffer-substring (point-at-bol) (point))))
  10496. (t (newline))))
  10497. (setq start (point))
  10498. (if (string-match "%file" rpl)
  10499. (setq rpl (replace-match
  10500. (concat
  10501. "\""
  10502. (save-match-data
  10503. (abbreviate-file-name (read-file-name "Include file: ")))
  10504. "\"")
  10505. t t rpl)))
  10506. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10507. (concat "\n" ind)))
  10508. (insert rpl)
  10509. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10510. ;;;; TODO, DEADLINE, Comments
  10511. (defun org-toggle-comment ()
  10512. "Change the COMMENT state of an entry."
  10513. (interactive)
  10514. (save-excursion
  10515. (org-back-to-heading)
  10516. (let (case-fold-search)
  10517. (cond
  10518. ((looking-at (format org-heading-keyword-regexp-format
  10519. org-comment-string))
  10520. (goto-char (match-end 1))
  10521. (looking-at (concat " +" org-comment-string))
  10522. (replace-match "" t t)
  10523. (when (eolp) (insert " ")))
  10524. ((looking-at org-outline-regexp)
  10525. (goto-char (match-end 0))
  10526. (insert org-comment-string " "))))))
  10527. (defvar org-last-todo-state-is-todo nil
  10528. "This is non-nil when the last TODO state change led to a TODO state.
  10529. If the last change removed the TODO tag or switched to DONE, then
  10530. this is nil.")
  10531. (defvar org-setting-tags nil) ; dynamically skipped
  10532. (defvar org-todo-setup-filter-hook nil
  10533. "Hook for functions that pre-filter todo specs.
  10534. Each function takes a todo spec and returns either nil or the spec
  10535. transformed into canonical form." )
  10536. (defvar org-todo-get-default-hook nil
  10537. "Hook for functions that get a default item for todo.
  10538. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10539. nil or a string to be used for the todo mark." )
  10540. (defvar org-agenda-headline-snapshot-before-repeat)
  10541. (defun org-current-effective-time ()
  10542. "Return current time adjusted for `org-extend-today-until' variable."
  10543. (let* ((ct (org-current-time))
  10544. (dct (decode-time ct))
  10545. (ct1
  10546. (cond
  10547. (org-use-last-clock-out-time-as-effective-time
  10548. (or (org-clock-get-last-clock-out-time) ct))
  10549. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10550. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10551. (t ct))))
  10552. ct1))
  10553. (defun org-todo-yesterday (&optional arg)
  10554. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10555. (interactive "P")
  10556. (if (eq major-mode 'org-agenda-mode)
  10557. (apply 'org-agenda-todo-yesterday arg)
  10558. (let* ((hour (third (decode-time
  10559. (org-current-time))))
  10560. (org-extend-today-until (1+ hour)))
  10561. (org-todo arg))))
  10562. (defvar org-block-entry-blocking ""
  10563. "First entry preventing the TODO state change.")
  10564. (defun org-todo (&optional arg)
  10565. "Change the TODO state of an item.
  10566. The state of an item is given by a keyword at the start of the heading,
  10567. like
  10568. *** TODO Write paper
  10569. *** DONE Call mom
  10570. The different keywords are specified in the variable `org-todo-keywords'.
  10571. By default the available states are \"TODO\" and \"DONE\".
  10572. So for this example: when the item starts with TODO, it is changed to DONE.
  10573. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10574. DONE are present, add TODO at the beginning of the heading.
  10575. With \\[universal-argument] prefix arg, use completion to determine the new \
  10576. state.
  10577. With numeric prefix arg, switch to that state.
  10578. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10579. keywords (nextset).
  10580. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10581. With a numeric prefix arg of 0, inhibit note taking for the change.
  10582. For calling through lisp, arg is also interpreted in the following way:
  10583. 'none -> empty state
  10584. \"\"(empty string) -> switch to empty state
  10585. 'done -> switch to DONE
  10586. 'nextset -> switch to the next set of keywords
  10587. 'previousset -> switch to the previous set of keywords
  10588. \"WAITING\" -> switch to the specified keyword, but only if it
  10589. really is a member of `org-todo-keywords'."
  10590. (interactive "P")
  10591. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10592. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10593. 'region-start-level 'region))
  10594. org-loop-over-headlines-in-active-region)
  10595. (org-map-entries
  10596. `(org-todo ,arg)
  10597. org-loop-over-headlines-in-active-region
  10598. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10599. (if (equal arg '(16)) (setq arg 'nextset))
  10600. (let ((org-blocker-hook org-blocker-hook)
  10601. commentp
  10602. case-fold-search)
  10603. (when (equal arg '(64))
  10604. (setq arg nil org-blocker-hook nil))
  10605. (when (and org-blocker-hook
  10606. (or org-inhibit-blocking
  10607. (org-entry-get nil "NOBLOCKING")))
  10608. (setq org-blocker-hook nil))
  10609. (save-excursion
  10610. (catch 'exit
  10611. (org-back-to-heading t)
  10612. (when (looking-at (concat "^\\*+ " org-comment-string))
  10613. (org-toggle-comment)
  10614. (setq commentp t))
  10615. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10616. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10617. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10618. (let* ((match-data (match-data))
  10619. (startpos (point-at-bol))
  10620. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10621. (org-log-done org-log-done)
  10622. (org-log-repeat org-log-repeat)
  10623. (org-todo-log-states org-todo-log-states)
  10624. (org-inhibit-logging
  10625. (if (equal arg 0)
  10626. (progn (setq arg nil) 'note) org-inhibit-logging))
  10627. (this (match-string 1))
  10628. (hl-pos (match-beginning 0))
  10629. (head (org-get-todo-sequence-head this))
  10630. (ass (assoc head org-todo-kwd-alist))
  10631. (interpret (nth 1 ass))
  10632. (done-word (nth 3 ass))
  10633. (final-done-word (nth 4 ass))
  10634. (org-last-state (or this ""))
  10635. (completion-ignore-case t)
  10636. (member (member this org-todo-keywords-1))
  10637. (tail (cdr member))
  10638. (org-state (cond
  10639. ((and org-todo-key-trigger
  10640. (or (and (equal arg '(4))
  10641. (eq org-use-fast-todo-selection 'prefix))
  10642. (and (not arg) org-use-fast-todo-selection
  10643. (not (eq org-use-fast-todo-selection
  10644. 'prefix)))))
  10645. ;; Use fast selection
  10646. (org-fast-todo-selection))
  10647. ((and (equal arg '(4))
  10648. (or (not org-use-fast-todo-selection)
  10649. (not org-todo-key-trigger)))
  10650. ;; Read a state with completion
  10651. (org-icompleting-read
  10652. "State: " (mapcar (lambda(x) (list x))
  10653. org-todo-keywords-1)
  10654. nil t))
  10655. ((eq arg 'right)
  10656. (if this
  10657. (if tail (car tail) nil)
  10658. (car org-todo-keywords-1)))
  10659. ((eq arg 'left)
  10660. (if (equal member org-todo-keywords-1)
  10661. nil
  10662. (if this
  10663. (nth (- (length org-todo-keywords-1)
  10664. (length tail) 2)
  10665. org-todo-keywords-1)
  10666. (org-last org-todo-keywords-1))))
  10667. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10668. (setq arg nil))) ; hack to fall back to cycling
  10669. (arg
  10670. ;; user or caller requests a specific state
  10671. (cond
  10672. ((equal arg "") nil)
  10673. ((eq arg 'none) nil)
  10674. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10675. ((eq arg 'nextset)
  10676. (or (car (cdr (member head org-todo-heads)))
  10677. (car org-todo-heads)))
  10678. ((eq arg 'previousset)
  10679. (let ((org-todo-heads (reverse org-todo-heads)))
  10680. (or (car (cdr (member head org-todo-heads)))
  10681. (car org-todo-heads))))
  10682. ((car (member arg org-todo-keywords-1)))
  10683. ((stringp arg)
  10684. (error "State `%s' not valid in this file" arg))
  10685. ((nth (1- (prefix-numeric-value arg))
  10686. org-todo-keywords-1))))
  10687. ((null member) (or head (car org-todo-keywords-1)))
  10688. ((equal this final-done-word) nil) ;; -> make empty
  10689. ((null tail) nil) ;; -> first entry
  10690. ((memq interpret '(type priority))
  10691. (if (eq this-command last-command)
  10692. (car tail)
  10693. (if (> (length tail) 0)
  10694. (or done-word (car org-done-keywords))
  10695. nil)))
  10696. (t
  10697. (car tail))))
  10698. (org-state (or
  10699. (run-hook-with-args-until-success
  10700. 'org-todo-get-default-hook org-state org-last-state)
  10701. org-state))
  10702. (next (if org-state (concat " " org-state " ") " "))
  10703. (change-plist (list :type 'todo-state-change :from this :to org-state
  10704. :position startpos))
  10705. dolog now-done-p)
  10706. (when org-blocker-hook
  10707. (setq org-last-todo-state-is-todo
  10708. (not (member this org-done-keywords)))
  10709. (unless (save-excursion
  10710. (save-match-data
  10711. (org-with-wide-buffer
  10712. (run-hook-with-args-until-failure
  10713. 'org-blocker-hook change-plist))))
  10714. (if (org-called-interactively-p 'interactive)
  10715. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10716. this org-state org-block-entry-blocking)
  10717. ;; fail silently
  10718. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10719. this org-state org-block-entry-blocking)
  10720. (throw 'exit nil))))
  10721. (store-match-data match-data)
  10722. (replace-match next t t)
  10723. (unless (pos-visible-in-window-p hl-pos)
  10724. (message "TODO state changed to %s" (org-trim next)))
  10725. (unless head
  10726. (setq head (org-get-todo-sequence-head org-state)
  10727. ass (assoc head org-todo-kwd-alist)
  10728. interpret (nth 1 ass)
  10729. done-word (nth 3 ass)
  10730. final-done-word (nth 4 ass)))
  10731. (when (memq arg '(nextset previousset))
  10732. (message "Keyword-Set %d/%d: %s"
  10733. (- (length org-todo-sets) -1
  10734. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10735. (length org-todo-sets)
  10736. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10737. (setq org-last-todo-state-is-todo
  10738. (not (member org-state org-done-keywords)))
  10739. (setq now-done-p (and (member org-state org-done-keywords)
  10740. (not (member this org-done-keywords))))
  10741. (and logging (org-local-logging logging))
  10742. (when (and (or org-todo-log-states org-log-done)
  10743. (not (eq org-inhibit-logging t))
  10744. (not (memq arg '(nextset previousset))))
  10745. ;; we need to look at recording a time and note
  10746. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10747. (nth 2 (assoc this org-todo-log-states))))
  10748. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10749. (setq dolog 'time))
  10750. (when (and org-state
  10751. (member org-state org-not-done-keywords)
  10752. (not (member this org-not-done-keywords)))
  10753. ;; This is now a todo state and was not one before
  10754. ;; If there was a CLOSED time stamp, get rid of it.
  10755. (org-add-planning-info nil nil 'closed))
  10756. (when (and now-done-p org-log-done)
  10757. ;; It is now done, and it was not done before
  10758. (org-add-planning-info 'closed (org-current-effective-time))
  10759. (if (and (not dolog) (eq 'note org-log-done))
  10760. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10761. (when (and org-state dolog)
  10762. ;; This is a non-nil state, and we need to log it
  10763. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10764. ;; Fixup tag positioning
  10765. (org-todo-trigger-tag-changes org-state)
  10766. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10767. (when org-provide-todo-statistics
  10768. (org-update-parent-todo-statistics))
  10769. (run-hooks 'org-after-todo-state-change-hook)
  10770. (if (and arg (not (member org-state org-done-keywords)))
  10771. (setq head (org-get-todo-sequence-head org-state)))
  10772. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10773. ;; Do we need to trigger a repeat?
  10774. (when now-done-p
  10775. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10776. ;; This is for the agenda, take a snapshot of the headline.
  10777. (save-match-data
  10778. (setq org-agenda-headline-snapshot-before-repeat
  10779. (org-get-heading))))
  10780. (org-auto-repeat-maybe org-state))
  10781. ;; Fixup cursor location if close to the keyword
  10782. (if (and (outline-on-heading-p)
  10783. (not (bolp))
  10784. (save-excursion (beginning-of-line 1)
  10785. (looking-at org-todo-line-regexp))
  10786. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10787. (progn
  10788. (goto-char (or (match-end 2) (match-end 1)))
  10789. (and (looking-at " ") (just-one-space))))
  10790. (when org-trigger-hook
  10791. (save-excursion
  10792. (run-hook-with-args 'org-trigger-hook change-plist)))
  10793. (when commentp (org-toggle-comment))))))))
  10794. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10795. "Block turning an entry into a TODO, using the hierarchy.
  10796. This checks whether the current task should be blocked from state
  10797. changes. Such blocking occurs when:
  10798. 1. The task has children which are not all in a completed state.
  10799. 2. A task has a parent with the property :ORDERED:, and there
  10800. are siblings prior to the current task with incomplete
  10801. status.
  10802. 3. The parent of the task is blocked because it has siblings that should
  10803. be done first, or is child of a block grandparent TODO entry."
  10804. (if (not org-enforce-todo-dependencies)
  10805. t ; if locally turned off don't block
  10806. (catch 'dont-block
  10807. ;; If this is not a todo state change, or if this entry is already DONE,
  10808. ;; do not block
  10809. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10810. (member (plist-get change-plist :from)
  10811. (cons 'done org-done-keywords))
  10812. (member (plist-get change-plist :to)
  10813. (cons 'todo org-not-done-keywords))
  10814. (not (plist-get change-plist :to)))
  10815. (throw 'dont-block t))
  10816. ;; If this task has children, and any are undone, it's blocked
  10817. (save-excursion
  10818. (org-back-to-heading t)
  10819. (let ((this-level (funcall outline-level)))
  10820. (outline-next-heading)
  10821. (let ((child-level (funcall outline-level)))
  10822. (while (and (not (eobp))
  10823. (> child-level this-level))
  10824. ;; this todo has children, check whether they are all
  10825. ;; completed
  10826. (if (and (not (org-entry-is-done-p))
  10827. (org-entry-is-todo-p))
  10828. (progn (setq org-block-entry-blocking (org-get-heading))
  10829. (throw 'dont-block nil)))
  10830. (outline-next-heading)
  10831. (setq child-level (funcall outline-level))))))
  10832. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10833. ;; any previous siblings are undone, it's blocked
  10834. (save-excursion
  10835. (org-back-to-heading t)
  10836. (let* ((pos (point))
  10837. (parent-pos (and (org-up-heading-safe) (point))))
  10838. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10839. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10840. (forward-line 1)
  10841. (re-search-forward org-not-done-heading-regexp pos t))
  10842. (setq org-block-entry-blocking (match-string 0))
  10843. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10844. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10845. (while t
  10846. (goto-char parent-pos)
  10847. (if (not (looking-at org-not-done-heading-regexp))
  10848. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10849. (setq pos (point))
  10850. (setq parent-pos (and (org-up-heading-safe) (point)))
  10851. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10852. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10853. (forward-line 1)
  10854. (re-search-forward org-not-done-heading-regexp pos t)
  10855. (setq org-block-entry-blocking (org-get-heading)))
  10856. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10857. (defcustom org-track-ordered-property-with-tag nil
  10858. "Should the ORDERED property also be shown as a tag?
  10859. The ORDERED property decides if an entry should require subtasks to be
  10860. completed in sequence. Since a property is not very visible, setting
  10861. this option means that toggling the ORDERED property with the command
  10862. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10863. not relevant for the behavior, but it makes things more visible.
  10864. Note that toggling the tag with tags commands will not change the property
  10865. and therefore not influence behavior!
  10866. This can be t, meaning the tag ORDERED should be used, It can also be a
  10867. string to select a different tag for this task."
  10868. :group 'org-todo
  10869. :type '(choice
  10870. (const :tag "No tracking" nil)
  10871. (const :tag "Track with ORDERED tag" t)
  10872. (string :tag "Use other tag")))
  10873. (defun org-toggle-ordered-property ()
  10874. "Toggle the ORDERED property of the current entry.
  10875. For better visibility, you can track the value of this property with a tag.
  10876. See variable `org-track-ordered-property-with-tag'."
  10877. (interactive)
  10878. (let* ((t1 org-track-ordered-property-with-tag)
  10879. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10880. (save-excursion
  10881. (org-back-to-heading)
  10882. (if (org-entry-get nil "ORDERED")
  10883. (progn
  10884. (org-delete-property "ORDERED")
  10885. (and tag (org-toggle-tag tag 'off))
  10886. (message "Subtasks can be completed in arbitrary order"))
  10887. (org-entry-put nil "ORDERED" "t")
  10888. (and tag (org-toggle-tag tag 'on))
  10889. (message "Subtasks must be completed in sequence")))))
  10890. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10891. (defun org-block-todo-from-checkboxes (change-plist)
  10892. "Block turning an entry into a TODO, using checkboxes.
  10893. This checks whether the current task should be blocked from state
  10894. changes because there are unchecked boxes in this entry."
  10895. (if (not org-enforce-todo-checkbox-dependencies)
  10896. t ; if locally turned off don't block
  10897. (catch 'dont-block
  10898. ;; If this is not a todo state change, or if this entry is already DONE,
  10899. ;; do not block
  10900. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10901. (member (plist-get change-plist :from)
  10902. (cons 'done org-done-keywords))
  10903. (member (plist-get change-plist :to)
  10904. (cons 'todo org-not-done-keywords))
  10905. (not (plist-get change-plist :to)))
  10906. (throw 'dont-block t))
  10907. ;; If this task has checkboxes that are not checked, it's blocked
  10908. (save-excursion
  10909. (org-back-to-heading t)
  10910. (let ((beg (point)) end)
  10911. (outline-next-heading)
  10912. (setq end (point))
  10913. (goto-char beg)
  10914. (if (org-list-search-forward
  10915. (concat (org-item-beginning-re)
  10916. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10917. "\\[[- ]\\]")
  10918. end t)
  10919. (progn
  10920. (if (boundp 'org-blocked-by-checkboxes)
  10921. (setq org-blocked-by-checkboxes t))
  10922. (throw 'dont-block nil)))))
  10923. t))) ; do not block
  10924. (defun org-entry-blocked-p ()
  10925. "Is the current entry blocked?"
  10926. (org-with-buffer-modified-unmodified
  10927. (if (org-entry-get nil "NOBLOCKING")
  10928. nil ;; Never block this entry
  10929. (not
  10930. (run-hook-with-args-until-failure
  10931. 'org-blocker-hook
  10932. (list :type 'todo-state-change
  10933. :position (point)
  10934. :from 'todo
  10935. :to 'done))))))
  10936. (defun org-update-statistics-cookies (all)
  10937. "Update the statistics cookie, either from TODO or from checkboxes.
  10938. This should be called with the cursor in a line with a statistics cookie."
  10939. (interactive "P")
  10940. (if all
  10941. (progn
  10942. (org-update-checkbox-count 'all)
  10943. (org-map-entries 'org-update-parent-todo-statistics))
  10944. (if (not (org-at-heading-p))
  10945. (org-update-checkbox-count)
  10946. (let ((pos (point-marker))
  10947. end l1 l2)
  10948. (ignore-errors (org-back-to-heading t))
  10949. (if (not (org-at-heading-p))
  10950. (org-update-checkbox-count)
  10951. (setq l1 (org-outline-level))
  10952. (setq end (save-excursion
  10953. (outline-next-heading)
  10954. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10955. (point)))
  10956. (if (and (save-excursion
  10957. (re-search-forward
  10958. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10959. (not (save-excursion (re-search-forward
  10960. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10961. (org-update-checkbox-count)
  10962. (if (and l2 (> l2 l1))
  10963. (progn
  10964. (goto-char end)
  10965. (org-update-parent-todo-statistics))
  10966. (goto-char pos)
  10967. (beginning-of-line 1)
  10968. (while (re-search-forward
  10969. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10970. (point-at-eol) t)
  10971. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10972. (goto-char pos)
  10973. (move-marker pos nil)))))
  10974. (defvar org-entry-property-inherited-from) ;; defined below
  10975. (defun org-update-parent-todo-statistics ()
  10976. "Update any statistics cookie in the parent of the current headline.
  10977. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10978. the entire subtree and this will travel up the hierarchy and update
  10979. statistics everywhere."
  10980. (let* ((prop (save-excursion (org-up-heading-safe)
  10981. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10982. (recursive (or (not org-hierarchical-todo-statistics)
  10983. (and prop (string-match "\\<recursive\\>" prop))))
  10984. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10985. 0))
  10986. (first t)
  10987. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10988. level ltoggle l1 new ndel
  10989. (cnt-all 0) (cnt-done 0) is-percent kwd
  10990. checkbox-beg ov ovs ove cookie-present)
  10991. (catch 'exit
  10992. (save-excursion
  10993. (beginning-of-line 1)
  10994. (setq ltoggle (funcall outline-level))
  10995. ;; Three situations are to consider:
  10996. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10997. ;; to the top-level ancestor on the headline;
  10998. ;; 2. If parent has "recursive" property, repeat up to the
  10999. ;; headline setting that property, taking inheritance into
  11000. ;; account;
  11001. ;; 3. Else, move up to direct parent and proceed only once.
  11002. (while (and (setq level (org-up-heading-safe))
  11003. (or recursive first)
  11004. (>= (point) lim))
  11005. (setq first nil cookie-present nil)
  11006. (unless (and level
  11007. (not (string-match
  11008. "\\<checkbox\\>"
  11009. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11010. "")))))
  11011. (throw 'exit nil))
  11012. (while (re-search-forward box-re (point-at-eol) t)
  11013. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11014. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11015. (save-match-data
  11016. (unless (outline-next-heading) (throw 'exit nil))
  11017. (while (and (looking-at org-complex-heading-regexp)
  11018. (> (setq l1 (length (match-string 1))) level))
  11019. (setq kwd (and (or recursive (= l1 ltoggle))
  11020. (match-string 2)))
  11021. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11022. (and (listp org-provide-todo-statistics)
  11023. (or (member kwd org-provide-todo-statistics)
  11024. (member kwd org-done-keywords))))
  11025. (setq cnt-all (1+ cnt-all))
  11026. (if (eq org-provide-todo-statistics t)
  11027. (and kwd (setq cnt-all (1+ cnt-all)))))
  11028. (and (member kwd org-done-keywords)
  11029. (setq cnt-done (1+ cnt-done)))
  11030. (outline-next-heading)))
  11031. (setq new
  11032. (if is-percent
  11033. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11034. (format "[%d/%d]" cnt-done cnt-all))
  11035. ndel (- (match-end 0) checkbox-beg))
  11036. ;; handle overlays when updating cookie from column view
  11037. (when (setq ov (car (overlays-at checkbox-beg)))
  11038. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11039. (delete-overlay ov))
  11040. (goto-char checkbox-beg)
  11041. (insert new)
  11042. (delete-region (point) (+ (point) ndel))
  11043. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11044. (when ov (move-overlay ov ovs ove)))
  11045. (when cookie-present
  11046. (run-hook-with-args 'org-after-todo-statistics-hook
  11047. cnt-done (- cnt-all cnt-done))))))
  11048. (run-hooks 'org-todo-statistics-hook)))
  11049. (defvar org-after-todo-statistics-hook nil
  11050. "Hook that is called after a TODO statistics cookie has been updated.
  11051. Each function is called with two arguments: the number of not-done entries
  11052. and the number of done entries.
  11053. For example, the following function, when added to this hook, will switch
  11054. an entry to DONE when all children are done, and back to TODO when new
  11055. entries are set to a TODO status. Note that this hook is only called
  11056. when there is a statistics cookie in the headline!
  11057. (defun org-summary-todo (n-done n-not-done)
  11058. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11059. (let (org-log-done org-log-states) ; turn off logging
  11060. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11061. ")
  11062. (defvar org-todo-statistics-hook nil
  11063. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11064. This hook runs even if there is no statistics cookie present, in which case
  11065. `org-after-todo-statistics-hook' would not run.")
  11066. (defun org-todo-trigger-tag-changes (state)
  11067. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11068. (let ((l org-todo-state-tags-triggers)
  11069. changes)
  11070. (when (or (not state) (equal state ""))
  11071. (setq changes (append changes (cdr (assoc "" l)))))
  11072. (when (and (stringp state) (> (length state) 0))
  11073. (setq changes (append changes (cdr (assoc state l)))))
  11074. (when (member state org-not-done-keywords)
  11075. (setq changes (append changes (cdr (assoc 'todo l)))))
  11076. (when (member state org-done-keywords)
  11077. (setq changes (append changes (cdr (assoc 'done l)))))
  11078. (dolist (c changes)
  11079. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11080. (defun org-local-logging (value)
  11081. "Get logging settings from a property VALUE."
  11082. (let* (words w a)
  11083. ;; directly set the variables, they are already local.
  11084. (setq org-log-done nil
  11085. org-log-repeat nil
  11086. org-todo-log-states nil)
  11087. (setq words (org-split-string value))
  11088. (while (setq w (pop words))
  11089. (cond
  11090. ((setq a (assoc w org-startup-options))
  11091. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11092. (set (nth 1 a) (nth 2 a))))
  11093. ((setq a (org-extract-log-state-settings w))
  11094. (and (member (car a) org-todo-keywords-1)
  11095. (push a org-todo-log-states)))))))
  11096. (defun org-get-todo-sequence-head (kwd)
  11097. "Return the head of the TODO sequence to which KWD belongs.
  11098. If KWD is not set, check if there is a text property remembering the
  11099. right sequence."
  11100. (let (p)
  11101. (cond
  11102. ((not kwd)
  11103. (or (get-text-property (point-at-bol) 'org-todo-head)
  11104. (progn
  11105. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11106. nil (point-at-eol)))
  11107. (get-text-property p 'org-todo-head))))
  11108. ((not (member kwd org-todo-keywords-1))
  11109. (car org-todo-keywords-1))
  11110. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11111. (defun org-fast-todo-selection ()
  11112. "Fast TODO keyword selection with single keys.
  11113. Returns the new TODO keyword, or nil if no state change should occur."
  11114. (let* ((fulltable org-todo-key-alist)
  11115. (done-keywords org-done-keywords) ;; needed for the faces.
  11116. (maxlen (apply 'max (mapcar
  11117. (lambda (x)
  11118. (if (stringp (car x)) (string-width (car x)) 0))
  11119. fulltable)))
  11120. (expert nil)
  11121. (fwidth (+ maxlen 3 1 3))
  11122. (ncol (/ (- (window-width) 4) fwidth))
  11123. tg cnt e c tbl
  11124. groups ingroup)
  11125. (save-excursion
  11126. (save-window-excursion
  11127. (if expert
  11128. (set-buffer (get-buffer-create " *Org todo*"))
  11129. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11130. (erase-buffer)
  11131. (org-set-local 'org-done-keywords done-keywords)
  11132. (setq tbl fulltable cnt 0)
  11133. (while (setq e (pop tbl))
  11134. (cond
  11135. ((equal e '(:startgroup))
  11136. (push '() groups) (setq ingroup t)
  11137. (when (not (= cnt 0))
  11138. (setq cnt 0)
  11139. (insert "\n"))
  11140. (insert "{ "))
  11141. ((equal e '(:endgroup))
  11142. (setq ingroup nil cnt 0)
  11143. (insert "}\n"))
  11144. ((equal e '(:newline))
  11145. (when (not (= cnt 0))
  11146. (setq cnt 0)
  11147. (insert "\n")
  11148. (setq e (car tbl))
  11149. (while (equal (car tbl) '(:newline))
  11150. (insert "\n")
  11151. (setq tbl (cdr tbl)))))
  11152. (t
  11153. (setq tg (car e) c (cdr e))
  11154. (if ingroup (push tg (car groups)))
  11155. (setq tg (org-add-props tg nil 'face
  11156. (org-get-todo-face tg)))
  11157. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11158. (insert "[" c "] " tg (make-string
  11159. (- fwidth 4 (length tg)) ?\ ))
  11160. (when (= (setq cnt (1+ cnt)) ncol)
  11161. (insert "\n")
  11162. (if ingroup (insert " "))
  11163. (setq cnt 0)))))
  11164. (insert "\n")
  11165. (goto-char (point-min))
  11166. (if (not expert) (org-fit-window-to-buffer))
  11167. (message "[a-z..]:Set [SPC]:clear")
  11168. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11169. (cond
  11170. ((or (= c ?\C-g)
  11171. (and (= c ?q) (not (rassoc c fulltable))))
  11172. (setq quit-flag t))
  11173. ((= c ?\ ) nil)
  11174. ((setq e (rassoc c fulltable) tg (car e))
  11175. tg)
  11176. (t (setq quit-flag t)))))))
  11177. (defun org-entry-is-todo-p ()
  11178. (member (org-get-todo-state) org-not-done-keywords))
  11179. (defun org-entry-is-done-p ()
  11180. (member (org-get-todo-state) org-done-keywords))
  11181. (defun org-get-todo-state ()
  11182. (save-excursion
  11183. (org-back-to-heading t)
  11184. (and (looking-at org-todo-line-regexp)
  11185. (match-end 2)
  11186. (match-string 2))))
  11187. (defun org-at-date-range-p (&optional inactive-ok)
  11188. "Is the cursor inside a date range?"
  11189. (interactive)
  11190. (save-excursion
  11191. (catch 'exit
  11192. (let ((pos (point)))
  11193. (skip-chars-backward "^[<\r\n")
  11194. (skip-chars-backward "<[")
  11195. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11196. (>= (match-end 0) pos)
  11197. (throw 'exit t))
  11198. (skip-chars-backward "^<[\r\n")
  11199. (skip-chars-backward "<[")
  11200. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11201. (>= (match-end 0) pos)
  11202. (throw 'exit t)))
  11203. nil)))
  11204. (defun org-get-repeat (&optional tagline)
  11205. "Check if there is a deadline/schedule with repeater in this entry."
  11206. (save-match-data
  11207. (save-excursion
  11208. (org-back-to-heading t)
  11209. (and (re-search-forward (if tagline
  11210. (concat tagline "\\s-*" org-repeat-re)
  11211. org-repeat-re)
  11212. (org-entry-end-position) t)
  11213. (match-string-no-properties 1)))))
  11214. (defvar org-last-changed-timestamp)
  11215. (defvar org-last-inserted-timestamp)
  11216. (defvar org-log-post-message)
  11217. (defvar org-log-note-purpose)
  11218. (defvar org-log-note-how)
  11219. (defvar org-log-note-extra)
  11220. (defun org-auto-repeat-maybe (done-word)
  11221. "Check if the current headline contains a repeated deadline/schedule.
  11222. If yes, set TODO state back to what it was and change the base date
  11223. of repeating deadline/scheduled time stamps to new date.
  11224. This function is run automatically after each state change to a DONE state."
  11225. ;; last-state is dynamically scoped into this function
  11226. (let* ((repeat (org-get-repeat))
  11227. (aa (assoc org-last-state org-todo-kwd-alist))
  11228. (interpret (nth 1 aa))
  11229. (head (nth 2 aa))
  11230. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11231. (msg "Entry repeats: ")
  11232. (org-log-done nil)
  11233. (org-todo-log-states nil)
  11234. re type n what ts time to-state)
  11235. (when repeat
  11236. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11237. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11238. org-todo-repeat-to-state))
  11239. (unless (and to-state (member to-state org-todo-keywords-1))
  11240. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11241. (org-todo to-state)
  11242. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11243. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11244. (org-time-stamp-format t t))))
  11245. (when org-log-repeat
  11246. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11247. (memq 'org-add-log-note post-command-hook))
  11248. ;; OK, we are already setup for some record
  11249. (if (eq org-log-repeat 'note)
  11250. ;; make sure we take a note, not only a time stamp
  11251. (setq org-log-note-how 'note))
  11252. ;; Set up for taking a record
  11253. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11254. org-last-state
  11255. 'findpos org-log-repeat)))
  11256. (org-back-to-heading t)
  11257. (org-add-planning-info nil nil 'closed)
  11258. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11259. org-deadline-time-regexp "\\)\\|\\("
  11260. org-ts-regexp "\\)"))
  11261. (while (re-search-forward
  11262. re (save-excursion (outline-next-heading) (point)) t)
  11263. (setq type (if (match-end 1) org-scheduled-string
  11264. (if (match-end 3) org-deadline-string "Plain:"))
  11265. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11266. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11267. (setq n (string-to-number (match-string 2 ts))
  11268. what (match-string 3 ts))
  11269. (if (equal what "w") (setq n (* n 7) what "d"))
  11270. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11271. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11272. ;; Preparation, see if we need to modify the start date for the change
  11273. (when (match-end 1)
  11274. (setq time (save-match-data (org-time-string-to-time ts)))
  11275. (cond
  11276. ((equal (match-string 1 ts) ".")
  11277. ;; Shift starting date to today
  11278. (org-timestamp-change
  11279. (- (org-today) (time-to-days time))
  11280. 'day))
  11281. ((equal (match-string 1 ts) "+")
  11282. (let ((nshiftmax 10) (nshift 0))
  11283. (while (or (= nshift 0)
  11284. (<= (time-to-days time)
  11285. (time-to-days (current-time))))
  11286. (when (= (incf nshift) nshiftmax)
  11287. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11288. (error "Abort")))
  11289. (org-timestamp-change n (cdr (assoc what whata)))
  11290. (org-at-timestamp-p t)
  11291. (setq ts (match-string 1))
  11292. (setq time (save-match-data (org-time-string-to-time ts)))))
  11293. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11294. ;; rematch, so that we have everything in place for the real shift
  11295. (org-at-timestamp-p t)
  11296. (setq ts (match-string 1))
  11297. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11298. (org-timestamp-change n (cdr (assoc what whata)))
  11299. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11300. (setq org-log-post-message msg)
  11301. (message "%s" msg))))
  11302. (defun org-show-todo-tree (arg)
  11303. "Make a compact tree which shows all headlines marked with TODO.
  11304. The tree will show the lines where the regexp matches, and all higher
  11305. headlines above the match.
  11306. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11307. With a numeric prefix N, construct a sparse tree for the Nth element
  11308. of `org-todo-keywords-1'."
  11309. (interactive "P")
  11310. (let ((case-fold-search nil)
  11311. (kwd-re
  11312. (cond ((null arg) org-not-done-regexp)
  11313. ((equal arg '(4))
  11314. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11315. (mapcar 'list org-todo-keywords-1))))
  11316. (concat "\\("
  11317. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11318. "\\)\\>")))
  11319. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11320. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11321. org-todo-keywords-1)))
  11322. (t (error "Invalid prefix argument: %s" arg)))))
  11323. (message "%d TODO entries found"
  11324. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11325. (defun org-deadline (&optional remove time)
  11326. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11327. With argument REMOVE, remove any deadline from the item.
  11328. With argument TIME, set the deadline at the corresponding date. TIME
  11329. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11330. (interactive "P")
  11331. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11332. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11333. 'region-start-level 'region))
  11334. org-loop-over-headlines-in-active-region)
  11335. (org-map-entries
  11336. `(org-deadline ',remove ,time)
  11337. org-loop-over-headlines-in-active-region
  11338. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11339. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11340. (repeater (and old-date
  11341. (string-match
  11342. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11343. old-date)
  11344. (match-string 1 old-date))))
  11345. (if remove
  11346. (progn
  11347. (when (and old-date org-log-redeadline)
  11348. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11349. org-log-redeadline))
  11350. (org-remove-timestamp-with-keyword org-deadline-string)
  11351. (message "Item no longer has a deadline."))
  11352. (org-add-planning-info 'deadline time 'closed)
  11353. (when (and old-date org-log-redeadline
  11354. (not (equal old-date
  11355. (substring org-last-inserted-timestamp 1 -1))))
  11356. (org-add-log-setup 'redeadline nil old-date 'findpos
  11357. org-log-redeadline))
  11358. (when repeater
  11359. (save-excursion
  11360. (org-back-to-heading t)
  11361. (when (re-search-forward (concat org-deadline-string " "
  11362. org-last-inserted-timestamp)
  11363. (save-excursion
  11364. (outline-next-heading) (point)) t)
  11365. (goto-char (1- (match-end 0)))
  11366. (insert " " repeater)
  11367. (setq org-last-inserted-timestamp
  11368. (concat (substring org-last-inserted-timestamp 0 -1)
  11369. " " repeater
  11370. (substring org-last-inserted-timestamp -1))))))
  11371. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11372. (defun org-schedule (&optional remove time)
  11373. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11374. With argument REMOVE, remove any scheduling date from the item.
  11375. With argument TIME, scheduled at the corresponding date. TIME can
  11376. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11377. (interactive "P")
  11378. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11379. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11380. 'region-start-level 'region))
  11381. org-loop-over-headlines-in-active-region)
  11382. (org-map-entries
  11383. `(org-schedule ',remove ,time)
  11384. org-loop-over-headlines-in-active-region
  11385. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11386. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11387. (repeater (and old-date
  11388. (string-match
  11389. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11390. old-date)
  11391. (match-string 1 old-date))))
  11392. (if remove
  11393. (progn
  11394. (when (and old-date org-log-reschedule)
  11395. (org-add-log-setup 'delschedule nil old-date 'findpos
  11396. org-log-reschedule))
  11397. (org-remove-timestamp-with-keyword org-scheduled-string)
  11398. (message "Item is no longer scheduled."))
  11399. (org-add-planning-info 'scheduled time 'closed)
  11400. (when (and old-date org-log-reschedule
  11401. (not (equal old-date
  11402. (substring org-last-inserted-timestamp 1 -1))))
  11403. (org-add-log-setup 'reschedule nil old-date 'findpos
  11404. org-log-reschedule))
  11405. (when repeater
  11406. (save-excursion
  11407. (org-back-to-heading t)
  11408. (when (re-search-forward (concat org-scheduled-string " "
  11409. org-last-inserted-timestamp)
  11410. (save-excursion
  11411. (outline-next-heading) (point)) t)
  11412. (goto-char (1- (match-end 0)))
  11413. (insert " " repeater)
  11414. (setq org-last-inserted-timestamp
  11415. (concat (substring org-last-inserted-timestamp 0 -1)
  11416. " " repeater
  11417. (substring org-last-inserted-timestamp -1))))))
  11418. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11419. (defun org-get-scheduled-time (pom &optional inherit)
  11420. "Get the scheduled time as a time tuple, of a format suitable
  11421. for calling org-schedule with, or if there is no scheduling,
  11422. returns nil."
  11423. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11424. (when time
  11425. (apply 'encode-time (org-parse-time-string time)))))
  11426. (defun org-get-deadline-time (pom &optional inherit)
  11427. "Get the deadline as a time tuple, of a format suitable for
  11428. calling org-deadline with, or if there is no scheduling, returns
  11429. nil."
  11430. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11431. (when time
  11432. (apply 'encode-time (org-parse-time-string time)))))
  11433. (defun org-remove-timestamp-with-keyword (keyword)
  11434. "Remove all time stamps with KEYWORD in the current entry."
  11435. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11436. beg)
  11437. (save-excursion
  11438. (org-back-to-heading t)
  11439. (setq beg (point))
  11440. (outline-next-heading)
  11441. (while (re-search-backward re beg t)
  11442. (replace-match "")
  11443. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11444. (equal (char-before) ?\ ))
  11445. (backward-delete-char 1)
  11446. (if (string-match "^[ \t]*$" (buffer-substring
  11447. (point-at-bol) (point-at-eol)))
  11448. (delete-region (point-at-bol)
  11449. (min (point-max) (1+ (point-at-eol))))))))))
  11450. (defun org-add-planning-info (what &optional time &rest remove)
  11451. "Insert new timestamp with keyword in the line directly after the headline.
  11452. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11453. If non is given, the user is prompted for a date.
  11454. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11455. be removed."
  11456. (interactive)
  11457. (let (org-time-was-given org-end-time-was-given ts
  11458. end default-time default-input)
  11459. (catch 'exit
  11460. (when (and (memq what '(scheduled deadline))
  11461. (or (not time)
  11462. (and (stringp time)
  11463. (string-match "^[-+]+[0-9]" time))))
  11464. ;; Try to get a default date/time from existing timestamp
  11465. (save-excursion
  11466. (org-back-to-heading t)
  11467. (setq end (save-excursion (outline-next-heading) (point)))
  11468. (when (re-search-forward (if (eq what 'scheduled)
  11469. org-scheduled-time-regexp
  11470. org-deadline-time-regexp)
  11471. end t)
  11472. (setq ts (match-string 1)
  11473. default-time
  11474. (apply 'encode-time (org-parse-time-string ts))
  11475. default-input (and ts (org-get-compact-tod ts))))))
  11476. (when what
  11477. (setq time
  11478. (if (stringp time)
  11479. ;; This is a string (relative or absolute), set proper date
  11480. (apply 'encode-time
  11481. (org-read-date-analyze
  11482. time default-time (decode-time default-time)))
  11483. ;; If necessary, get the time from the user
  11484. (or time (org-read-date nil 'to-time nil nil
  11485. default-time default-input)))))
  11486. (when (and org-insert-labeled-timestamps-at-point
  11487. (member what '(scheduled deadline)))
  11488. (insert
  11489. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11490. (org-insert-time-stamp time org-time-was-given
  11491. nil nil nil (list org-end-time-was-given))
  11492. (setq what nil))
  11493. (save-excursion
  11494. (save-restriction
  11495. (let (col list elt ts buffer-invisibility-spec)
  11496. (org-back-to-heading t)
  11497. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11498. (goto-char (match-end 1))
  11499. (setq col (current-column))
  11500. (goto-char (match-end 0))
  11501. (if (eobp) (insert "\n") (forward-char 1))
  11502. (when (and (not what)
  11503. (not (looking-at
  11504. (concat "[ \t]*"
  11505. org-keyword-time-not-clock-regexp))))
  11506. ;; Nothing to add, nothing to remove...... :-)
  11507. (throw 'exit nil))
  11508. (if (and (not (looking-at org-outline-regexp))
  11509. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11510. "[^\r\n]*"))
  11511. (not (equal (match-string 1) org-clock-string)))
  11512. (narrow-to-region (match-beginning 0) (match-end 0))
  11513. (insert-before-markers "\n")
  11514. (backward-char 1)
  11515. (narrow-to-region (point) (point))
  11516. (and org-adapt-indentation (org-indent-to-column col)))
  11517. ;; Check if we have to remove something.
  11518. (setq list (cons what remove))
  11519. (while list
  11520. (setq elt (pop list))
  11521. (when (or (and (eq elt 'scheduled)
  11522. (re-search-forward org-scheduled-time-regexp nil t))
  11523. (and (eq elt 'deadline)
  11524. (re-search-forward org-deadline-time-regexp nil t))
  11525. (and (eq elt 'closed)
  11526. (re-search-forward org-closed-time-regexp nil t)))
  11527. (replace-match "")
  11528. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11529. (and (looking-at "[ \t]+") (replace-match ""))
  11530. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11531. (when what
  11532. (insert
  11533. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11534. (cond ((eq what 'scheduled) org-scheduled-string)
  11535. ((eq what 'deadline) org-deadline-string)
  11536. ((eq what 'closed) org-closed-string))
  11537. " ")
  11538. (setq ts (org-insert-time-stamp
  11539. time
  11540. (or org-time-was-given
  11541. (and (eq what 'closed) org-log-done-with-time))
  11542. (eq what 'closed)
  11543. nil nil (list org-end-time-was-given)))
  11544. (insert
  11545. (if (not (or (bolp) (eq (char-before) ?\ )
  11546. (memq (char-after) '(32 10))
  11547. (eobp))) " " ""))
  11548. (end-of-line 1))
  11549. (goto-char (point-min))
  11550. (widen)
  11551. (if (and (looking-at "[ \t]*\n")
  11552. (equal (char-before) ?\n))
  11553. (delete-region (1- (point)) (point-at-eol)))
  11554. ts))))))
  11555. (defvar org-log-note-marker (make-marker))
  11556. (defvar org-log-note-purpose nil)
  11557. (defvar org-log-note-state nil)
  11558. (defvar org-log-note-previous-state nil)
  11559. (defvar org-log-note-how nil)
  11560. (defvar org-log-note-extra nil)
  11561. (defvar org-log-note-window-configuration nil)
  11562. (defvar org-log-note-return-to (make-marker))
  11563. (defvar org-log-note-effective-time nil
  11564. "Remembered current time so that dynamically scoped
  11565. `org-extend-today-until' affects tha timestamps in state change
  11566. log")
  11567. (defvar org-log-post-message nil
  11568. "Message to be displayed after a log note has been stored.
  11569. The auto-repeater uses this.")
  11570. (defun org-add-note ()
  11571. "Add a note to the current entry.
  11572. This is done in the same way as adding a state change note."
  11573. (interactive)
  11574. (org-add-log-setup 'note nil nil 'findpos nil))
  11575. (defvar org-property-end-re)
  11576. (defun org-add-log-setup (&optional purpose state prev-state
  11577. findpos how extra)
  11578. "Set up the post command hook to take a note.
  11579. If this is about to TODO state change, the new state is expected in STATE.
  11580. When FINDPOS is non-nil, find the correct position for the note in
  11581. the current entry. If not, assume that it can be inserted at point.
  11582. HOW is an indicator what kind of note should be created.
  11583. EXTRA is additional text that will be inserted into the notes buffer."
  11584. (let* ((org-log-into-drawer (org-log-into-drawer))
  11585. (drawer (cond ((stringp org-log-into-drawer)
  11586. org-log-into-drawer)
  11587. (org-log-into-drawer "LOGBOOK"))))
  11588. (save-restriction
  11589. (save-excursion
  11590. (when findpos
  11591. (org-back-to-heading t)
  11592. (narrow-to-region (point) (save-excursion
  11593. (outline-next-heading) (point)))
  11594. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11595. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11596. "[^\r\n]*\\)?"))
  11597. (goto-char (match-end 0))
  11598. (cond
  11599. (drawer
  11600. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11601. nil t)
  11602. (progn
  11603. (goto-char (match-end 0))
  11604. (or org-log-states-order-reversed
  11605. (and (re-search-forward org-property-end-re nil t)
  11606. (goto-char (1- (match-beginning 0))))))
  11607. (insert "\n:" drawer ":\n:END:")
  11608. (beginning-of-line 0)
  11609. (org-indent-line)
  11610. (beginning-of-line 2)
  11611. (org-indent-line)
  11612. (end-of-line 0)))
  11613. ((and org-log-state-notes-insert-after-drawers
  11614. (save-excursion
  11615. (forward-line) (looking-at org-drawer-regexp)))
  11616. (forward-line)
  11617. (while (looking-at org-drawer-regexp)
  11618. (goto-char (match-end 0))
  11619. (re-search-forward org-property-end-re (point-max) t)
  11620. (forward-line))
  11621. (forward-line -1)))
  11622. (unless org-log-states-order-reversed
  11623. (and (= (char-after) ?\n) (forward-char 1))
  11624. (org-skip-over-state-notes)
  11625. (skip-chars-backward " \t\n\r")))
  11626. (move-marker org-log-note-marker (point))
  11627. (setq org-log-note-purpose purpose
  11628. org-log-note-state state
  11629. org-log-note-previous-state prev-state
  11630. org-log-note-how how
  11631. org-log-note-extra extra
  11632. org-log-note-effective-time (org-current-effective-time))
  11633. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11634. (defun org-skip-over-state-notes ()
  11635. "Skip past the list of State notes in an entry."
  11636. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11637. (when (ignore-errors (goto-char (org-in-item-p)))
  11638. (let* ((struct (org-list-struct))
  11639. (prevs (org-list-prevs-alist struct)))
  11640. (while (looking-at "[ \t]*- State")
  11641. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11642. (org-list-get-item-end (point) struct)))))))
  11643. (defun org-add-log-note (&optional purpose)
  11644. "Pop up a window for taking a note, and add this note later at point."
  11645. (remove-hook 'post-command-hook 'org-add-log-note)
  11646. (setq org-log-note-window-configuration (current-window-configuration))
  11647. (delete-other-windows)
  11648. (move-marker org-log-note-return-to (point))
  11649. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11650. (goto-char org-log-note-marker)
  11651. (org-switch-to-buffer-other-window "*Org Note*")
  11652. (erase-buffer)
  11653. (if (memq org-log-note-how '(time state))
  11654. (let (current-prefix-arg) (org-store-log-note))
  11655. (let ((org-inhibit-startup t)) (org-mode))
  11656. (insert (format "# Insert note for %s.
  11657. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11658. (cond
  11659. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11660. ((eq org-log-note-purpose 'done) "closed todo item")
  11661. ((eq org-log-note-purpose 'state)
  11662. (format "state change from \"%s\" to \"%s\""
  11663. (or org-log-note-previous-state "")
  11664. (or org-log-note-state "")))
  11665. ((eq org-log-note-purpose 'reschedule)
  11666. "rescheduling")
  11667. ((eq org-log-note-purpose 'delschedule)
  11668. "no longer scheduled")
  11669. ((eq org-log-note-purpose 'redeadline)
  11670. "changing deadline")
  11671. ((eq org-log-note-purpose 'deldeadline)
  11672. "removing deadline")
  11673. ((eq org-log-note-purpose 'refile)
  11674. "refiling")
  11675. ((eq org-log-note-purpose 'note)
  11676. "this entry")
  11677. (t (error "This should not happen")))))
  11678. (if org-log-note-extra (insert org-log-note-extra))
  11679. (org-set-local 'org-finish-function 'org-store-log-note)
  11680. (run-hooks 'org-log-buffer-setup-hook)))
  11681. (defvar org-note-abort nil) ; dynamically scoped
  11682. (defun org-store-log-note ()
  11683. "Finish taking a log note, and insert it to where it belongs."
  11684. (let ((txt (buffer-string)))
  11685. (kill-buffer (current-buffer))
  11686. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11687. lines ind bul)
  11688. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11689. (setq txt (replace-match "" t t txt)))
  11690. (if (string-match "\\s-+\\'" txt)
  11691. (setq txt (replace-match "" t t txt)))
  11692. (setq lines (org-split-string txt "\n"))
  11693. (when (and note (string-match "\\S-" note))
  11694. (setq note
  11695. (org-replace-escapes
  11696. note
  11697. (list (cons "%u" (user-login-name))
  11698. (cons "%U" user-full-name)
  11699. (cons "%t" (format-time-string
  11700. (org-time-stamp-format 'long 'inactive)
  11701. org-log-note-effective-time))
  11702. (cons "%T" (format-time-string
  11703. (org-time-stamp-format 'long nil)
  11704. org-log-note-effective-time))
  11705. (cons "%d" (format-time-string
  11706. (org-time-stamp-format nil 'inactive)
  11707. org-log-note-effective-time))
  11708. (cons "%D" (format-time-string
  11709. (org-time-stamp-format nil nil)
  11710. org-log-note-effective-time))
  11711. (cons "%s" (if org-log-note-state
  11712. (concat "\"" org-log-note-state "\"")
  11713. ""))
  11714. (cons "%S" (if org-log-note-previous-state
  11715. (concat "\"" org-log-note-previous-state "\"")
  11716. "\"\"")))))
  11717. (if lines (setq note (concat note " \\\\")))
  11718. (push note lines))
  11719. (when (or current-prefix-arg org-note-abort)
  11720. (when org-log-into-drawer
  11721. (org-remove-empty-drawer-at
  11722. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11723. org-log-note-marker))
  11724. (setq lines nil))
  11725. (when lines
  11726. (with-current-buffer (marker-buffer org-log-note-marker)
  11727. (save-excursion
  11728. (goto-char org-log-note-marker)
  11729. (move-marker org-log-note-marker nil)
  11730. (end-of-line 1)
  11731. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11732. (setq ind (save-excursion
  11733. (if (ignore-errors (goto-char (org-in-item-p)))
  11734. (let ((struct (org-list-struct)))
  11735. (org-list-get-ind
  11736. (org-list-get-top-point struct) struct))
  11737. (skip-chars-backward " \r\t\n")
  11738. (cond
  11739. ((and (org-at-heading-p)
  11740. org-adapt-indentation)
  11741. (1+ (org-current-level)))
  11742. ((org-at-heading-p) 0)
  11743. (t (org-get-indentation))))))
  11744. (setq bul (org-list-bullet-string "-"))
  11745. (org-indent-line-to ind)
  11746. (insert bul (pop lines))
  11747. (let ((ind-body (+ (length bul) ind)))
  11748. (while lines
  11749. (insert "\n")
  11750. (org-indent-line-to ind-body)
  11751. (insert (pop lines))))
  11752. (message "Note stored")
  11753. (org-back-to-heading t)
  11754. (org-cycle-hide-drawers 'children))))))
  11755. (set-window-configuration org-log-note-window-configuration)
  11756. (with-current-buffer (marker-buffer org-log-note-return-to)
  11757. (goto-char org-log-note-return-to))
  11758. (move-marker org-log-note-return-to nil)
  11759. (and org-log-post-message (message "%s" org-log-post-message)))
  11760. (defun org-remove-empty-drawer-at (drawer pos)
  11761. "Remove an empty drawer DRAWER at position POS.
  11762. POS may also be a marker."
  11763. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11764. (save-excursion
  11765. (save-restriction
  11766. (widen)
  11767. (goto-char pos)
  11768. (if (org-in-regexp
  11769. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11770. (replace-match ""))))))
  11771. (defvar org-ts-type nil)
  11772. (defun org-sparse-tree (&optional arg type)
  11773. "Create a sparse tree, prompt for the details.
  11774. This command can create sparse trees. You first need to select the type
  11775. of match used to create the tree:
  11776. t Show all TODO entries.
  11777. T Show entries with a specific TODO keyword.
  11778. m Show entries selected by a tags/property match.
  11779. p Enter a property name and its value (both with completion on existing
  11780. names/values) and show entries with that property.
  11781. r Show entries matching a regular expression (`/' can be used as well).
  11782. b Show deadlines and scheduled items before a date.
  11783. a Show deadlines and scheduled items after a date.
  11784. d Show deadlines due within `org-deadline-warning-days'.
  11785. D Show deadlines and scheduled items between a date range."
  11786. (interactive "P")
  11787. (let (ans kwd value ts-type)
  11788. (setq type (or type org-sparse-tree-default-date-type))
  11789. (setq org-ts-type type)
  11790. (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"
  11791. (cond ((eq type 'all) "all timestamps")
  11792. ((eq type 'scheduled) "only scheduled")
  11793. ((eq type 'deadline) "only deadline")
  11794. ((eq type 'active) "only active timestamps")
  11795. ((eq type 'inactive) "only inactive timestamps")
  11796. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11797. (t "scheduled/deadline")))
  11798. (setq ans (read-char-exclusive))
  11799. (cond
  11800. ((equal ans ?c)
  11801. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11802. ((equal ans ?d)
  11803. (call-interactively 'org-check-deadlines))
  11804. ((equal ans ?b)
  11805. (call-interactively 'org-check-before-date))
  11806. ((equal ans ?a)
  11807. (call-interactively 'org-check-after-date))
  11808. ((equal ans ?D)
  11809. (call-interactively 'org-check-dates-range))
  11810. ((equal ans ?t)
  11811. (call-interactively 'org-show-todo-tree))
  11812. ((equal ans ?T)
  11813. (org-show-todo-tree '(4)))
  11814. ((member ans '(?T ?m))
  11815. (call-interactively 'org-match-sparse-tree))
  11816. ((member ans '(?p ?P))
  11817. (setq kwd (org-icompleting-read "Property: "
  11818. (mapcar 'list (org-buffer-property-keys))))
  11819. (setq value (org-icompleting-read "Value: "
  11820. (mapcar 'list (org-property-values kwd))))
  11821. (unless (string-match "\\`{.*}\\'" value)
  11822. (setq value (concat "\"" value "\"")))
  11823. (org-match-sparse-tree arg (concat kwd "=" value)))
  11824. ((member ans '(?r ?R ?/))
  11825. (call-interactively 'org-occur))
  11826. (t (error "No such sparse tree command \"%c\"" ans)))))
  11827. (defvar org-occur-highlights nil
  11828. "List of overlays used for occur matches.")
  11829. (make-variable-buffer-local 'org-occur-highlights)
  11830. (defvar org-occur-parameters nil
  11831. "Parameters of the active org-occur calls.
  11832. This is a list, each call to org-occur pushes as cons cell,
  11833. containing the regular expression and the callback, onto the list.
  11834. The list can contain several entries if `org-occur' has been called
  11835. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11836. will only contain one set of parameters. When the highlights are
  11837. removed (for example with `C-c C-c', or with the next edit (depending
  11838. on `org-remove-highlights-with-change'), this variable is emptied
  11839. as well.")
  11840. (make-variable-buffer-local 'org-occur-parameters)
  11841. (defun org-occur (regexp &optional keep-previous callback)
  11842. "Make a compact tree which shows all matches of REGEXP.
  11843. The tree will show the lines where the regexp matches, and all higher
  11844. headlines above the match. It will also show the heading after the match,
  11845. to make sure editing the matching entry is easy.
  11846. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11847. call to `org-occur' will be kept, to allow stacking of calls to this
  11848. command.
  11849. If CALLBACK is non-nil, it is a function which is called to confirm
  11850. that the match should indeed be shown."
  11851. (interactive "sRegexp: \nP")
  11852. (when (equal regexp "")
  11853. (error "Regexp cannot be empty"))
  11854. (unless keep-previous
  11855. (org-remove-occur-highlights nil nil t))
  11856. (push (cons regexp callback) org-occur-parameters)
  11857. (let ((cnt 0))
  11858. (save-excursion
  11859. (goto-char (point-min))
  11860. (if (or (not keep-previous) ; do not want to keep
  11861. (not org-occur-highlights)) ; no previous matches
  11862. ;; hide everything
  11863. (org-overview))
  11864. (while (re-search-forward regexp nil t)
  11865. (when (or (not callback)
  11866. (save-match-data (funcall callback)))
  11867. (setq cnt (1+ cnt))
  11868. (when org-highlight-sparse-tree-matches
  11869. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11870. (org-show-context 'occur-tree))))
  11871. (when org-remove-highlights-with-change
  11872. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11873. nil 'local))
  11874. (unless org-sparse-tree-open-archived-trees
  11875. (org-hide-archived-subtrees (point-min) (point-max)))
  11876. (run-hooks 'org-occur-hook)
  11877. (if (org-called-interactively-p 'interactive)
  11878. (message "%d match(es) for regexp %s" cnt regexp))
  11879. cnt))
  11880. (defun org-occur-next-match (&optional n reset)
  11881. "Function for `next-error-function' to find sparse tree matches.
  11882. N is the number of matches to move, when negative move backwards.
  11883. RESET is entirely ignored - this function always goes back to the
  11884. starting point when no match is found."
  11885. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11886. (search-func (if (< n 0)
  11887. 'previous-single-char-property-change
  11888. 'next-single-char-property-change))
  11889. (n (abs n))
  11890. (pos (point))
  11891. p1)
  11892. (catch 'exit
  11893. (while (setq p1 (funcall search-func (point) 'org-type))
  11894. (when (equal p1 limit)
  11895. (goto-char pos)
  11896. (error "No more matches"))
  11897. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11898. (setq n (1- n))
  11899. (when (= n 0)
  11900. (goto-char p1)
  11901. (throw 'exit (point))))
  11902. (goto-char p1))
  11903. (goto-char p1)
  11904. (error "No more matches"))))
  11905. (defun org-show-context (&optional key)
  11906. "Make sure point and context are visible.
  11907. How much context is shown depends upon the variables
  11908. `org-show-hierarchy-above', `org-show-following-heading',
  11909. `org-show-entry-below' and `org-show-siblings'."
  11910. (let ((heading-p (org-at-heading-p t))
  11911. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11912. (following-p (org-get-alist-option org-show-following-heading key))
  11913. (entry-p (org-get-alist-option org-show-entry-below key))
  11914. (siblings-p (org-get-alist-option org-show-siblings key)))
  11915. (catch 'exit
  11916. ;; Show heading or entry text
  11917. (if (and heading-p (not entry-p))
  11918. (org-flag-heading nil) ; only show the heading
  11919. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11920. (org-show-hidden-entry))) ; show entire entry
  11921. (when following-p
  11922. ;; Show next sibling, or heading below text
  11923. (save-excursion
  11924. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11925. (org-flag-heading nil))))
  11926. (when siblings-p (org-show-siblings))
  11927. (when hierarchy-p
  11928. ;; show all higher headings, possibly with siblings
  11929. (save-excursion
  11930. (while (and (condition-case nil
  11931. (progn (org-up-heading-all 1) t)
  11932. (error nil))
  11933. (not (bobp)))
  11934. (org-flag-heading nil)
  11935. (when siblings-p (org-show-siblings))))))))
  11936. (defvar org-reveal-start-hook nil
  11937. "Hook run before revealing a location.")
  11938. (defun org-reveal (&optional siblings)
  11939. "Show current entry, hierarchy above it, and the following headline.
  11940. This can be used to show a consistent set of context around locations
  11941. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11942. not t for the search context.
  11943. With optional argument SIBLINGS, on each level of the hierarchy all
  11944. siblings are shown. This repairs the tree structure to what it would
  11945. look like when opened with hierarchical calls to `org-cycle'.
  11946. With double optional argument \\[universal-argument] \\[universal-argument], \
  11947. go to the parent and show the
  11948. entire tree."
  11949. (interactive "P")
  11950. (run-hooks 'org-reveal-start-hook)
  11951. (let ((org-show-hierarchy-above t)
  11952. (org-show-following-heading t)
  11953. (org-show-siblings (if siblings t org-show-siblings)))
  11954. (org-show-context nil))
  11955. (when (equal siblings '(16))
  11956. (save-excursion
  11957. (when (org-up-heading-safe)
  11958. (org-show-subtree)
  11959. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11960. (defun org-highlight-new-match (beg end)
  11961. "Highlight from BEG to END and mark the highlight is an occur headline."
  11962. (let ((ov (make-overlay beg end)))
  11963. (overlay-put ov 'face 'secondary-selection)
  11964. (overlay-put ov 'org-type 'org-occur)
  11965. (push ov org-occur-highlights)))
  11966. (defun org-remove-occur-highlights (&optional beg end noremove)
  11967. "Remove the occur highlights from the buffer.
  11968. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11969. from the `before-change-functions' in the current buffer."
  11970. (interactive)
  11971. (unless org-inhibit-highlight-removal
  11972. (mapc 'delete-overlay org-occur-highlights)
  11973. (setq org-occur-highlights nil)
  11974. (setq org-occur-parameters nil)
  11975. (unless noremove
  11976. (remove-hook 'before-change-functions
  11977. 'org-remove-occur-highlights 'local))))
  11978. ;;;; Priorities
  11979. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11980. "Regular expression matching the priority indicator.")
  11981. (defvar org-remove-priority-next-time nil)
  11982. (defun org-priority-up ()
  11983. "Increase the priority of the current item."
  11984. (interactive)
  11985. (org-priority 'up))
  11986. (defun org-priority-down ()
  11987. "Decrease the priority of the current item."
  11988. (interactive)
  11989. (org-priority 'down))
  11990. (defun org-priority (&optional action show)
  11991. "Change the priority of an item.
  11992. ACTION can be `set', `up', `down', or a character."
  11993. (interactive "P")
  11994. (if (equal action '(4))
  11995. (org-show-priority)
  11996. (unless org-enable-priority-commands
  11997. (error "Priority commands are disabled"))
  11998. (setq action (or action 'set))
  11999. (let (current new news have remove)
  12000. (save-excursion
  12001. (org-back-to-heading t)
  12002. (if (looking-at org-priority-regexp)
  12003. (setq current (string-to-char (match-string 2))
  12004. have t))
  12005. (cond
  12006. ((eq action 'remove)
  12007. (setq remove t new ?\ ))
  12008. ((or (eq action 'set)
  12009. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12010. (if (not (eq action 'set))
  12011. (setq new action)
  12012. (message "Priority %c-%c, SPC to remove: "
  12013. org-highest-priority org-lowest-priority)
  12014. (save-match-data
  12015. (setq new (read-char-exclusive))))
  12016. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12017. (= (upcase org-lowest-priority) org-lowest-priority))
  12018. (setq new (upcase new)))
  12019. (cond ((equal new ?\ ) (setq remove t))
  12020. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12021. (error "Priority must be between `%c' and `%c'"
  12022. org-highest-priority org-lowest-priority))))
  12023. ((eq action 'up)
  12024. (setq new (if have
  12025. (1- current) ; normal cycling
  12026. ;; last priority was empty
  12027. (if (eq last-command this-command)
  12028. org-lowest-priority ; wrap around empty to lowest
  12029. ;; default
  12030. (if org-priority-start-cycle-with-default
  12031. org-default-priority
  12032. (1- org-default-priority))))))
  12033. ((eq action 'down)
  12034. (setq new (if have
  12035. (1+ current) ; normal cycling
  12036. ;; last priority was empty
  12037. (if (eq last-command this-command)
  12038. org-highest-priority ; wrap around empty to highest
  12039. ;; default
  12040. (if org-priority-start-cycle-with-default
  12041. org-default-priority
  12042. (1+ org-default-priority))))))
  12043. (t (error "Invalid action")))
  12044. (if (or (< (upcase new) org-highest-priority)
  12045. (> (upcase new) org-lowest-priority))
  12046. (if (and (memq action '(up down))
  12047. (not have) (not (eq last-command this-command)))
  12048. ;; `new' is from default priority
  12049. (error
  12050. "The default can not be set, see `org-default-priority' why")
  12051. ;; normal cycling: `new' is beyond highest/lowest priority
  12052. ;; and is wrapped around to the empty priority
  12053. (setq remove t)))
  12054. (setq news (format "%c" new))
  12055. (if have
  12056. (if remove
  12057. (replace-match "" t t nil 1)
  12058. (replace-match news t t nil 2))
  12059. (if remove
  12060. (error "No priority cookie found in line")
  12061. (let ((case-fold-search nil))
  12062. (looking-at org-todo-line-regexp))
  12063. (if (match-end 2)
  12064. (progn
  12065. (goto-char (match-end 2))
  12066. (insert " [#" news "]"))
  12067. (goto-char (match-beginning 3))
  12068. (insert "[#" news "] "))))
  12069. (org-preserve-lc (org-set-tags nil 'align)))
  12070. (if remove
  12071. (message "Priority removed")
  12072. (message "Priority of current item set to %s" news)))))
  12073. (defun org-show-priority ()
  12074. "Show the priority of the current item.
  12075. This priority is composed of the main priority given with the [#A] cookies,
  12076. and by additional input from the age of a schedules or deadline entry."
  12077. (interactive)
  12078. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12079. (org-get-at-bol 'priority)
  12080. (save-excursion
  12081. (save-match-data
  12082. (beginning-of-line)
  12083. (and (looking-at org-heading-regexp)
  12084. (org-get-priority (match-string 0))))))))
  12085. (message "Priority is %d" (if pri pri -1000))))
  12086. (defun org-get-priority (s)
  12087. "Find priority cookie and return priority."
  12088. (save-match-data
  12089. (if (functionp org-get-priority-function)
  12090. (funcall org-get-priority-function)
  12091. (if (not (string-match org-priority-regexp s))
  12092. (* 1000 (- org-lowest-priority org-default-priority))
  12093. (* 1000 (- org-lowest-priority
  12094. (string-to-char (match-string 2 s))))))))
  12095. ;;;; Tags
  12096. (defvar org-agenda-archives-mode)
  12097. (defvar org-map-continue-from nil
  12098. "Position from where mapping should continue.
  12099. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12100. (defvar org-scanner-tags nil
  12101. "The current tag list while the tags scanner is running.")
  12102. (defvar org-trust-scanner-tags nil
  12103. "Should `org-get-tags-at' use the tags for the scanner.
  12104. This is for internal dynamical scoping only.
  12105. When this is non-nil, the function `org-get-tags-at' will return the value
  12106. of `org-scanner-tags' instead of building the list by itself. This
  12107. can lead to large speed-ups when the tags scanner is used in a file with
  12108. many entries, and when the list of tags is retrieved, for example to
  12109. obtain a list of properties. Building the tags list for each entry in such
  12110. a file becomes an N^2 operation - but with this variable set, it scales
  12111. as N.")
  12112. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12113. "Scan headline tags with inheritance and produce output ACTION.
  12114. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12115. or `agenda' to produce an entry list for an agenda view. It can also be
  12116. a Lisp form or a function that should be called at each matched headline, in
  12117. this case the return value is a list of all return values from these calls.
  12118. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12119. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12120. only lines with a not-done TODO keyword are included in the output.
  12121. This should be the same variable that was scoped into
  12122. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12123. START-LEVEL can be a string with asterisks, reducing the scope to
  12124. headlines matching this string."
  12125. (require 'org-agenda)
  12126. (let* ((re (concat "^"
  12127. (if start-level
  12128. ;; Get the correct level to match
  12129. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12130. org-outline-regexp)
  12131. " *\\(\\<\\("
  12132. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12133. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12134. (props (list 'face 'default
  12135. 'done-face 'org-agenda-done
  12136. 'undone-face 'default
  12137. 'mouse-face 'highlight
  12138. 'org-not-done-regexp org-not-done-regexp
  12139. 'org-todo-regexp org-todo-regexp
  12140. 'org-complex-heading-regexp org-complex-heading-regexp
  12141. 'help-echo
  12142. (format "mouse-2 or RET jump to org file %s"
  12143. (abbreviate-file-name
  12144. (or (buffer-file-name (buffer-base-buffer))
  12145. (buffer-name (buffer-base-buffer)))))))
  12146. (case-fold-search nil)
  12147. (org-map-continue-from nil)
  12148. lspos tags tags-list
  12149. (tags-alist (list (cons 0 org-file-tags)))
  12150. (llast 0) rtn rtn1 level category i txt
  12151. todo marker entry priority)
  12152. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12153. (setq action (list 'lambda nil action)))
  12154. (save-excursion
  12155. (goto-char (point-min))
  12156. (when (eq action 'sparse-tree)
  12157. (org-overview)
  12158. (org-remove-occur-highlights))
  12159. (while (re-search-forward re nil t)
  12160. (setq org-map-continue-from nil)
  12161. (catch :skip
  12162. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12163. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12164. (goto-char (setq lspos (match-beginning 0)))
  12165. (setq level (org-reduced-level (org-outline-level))
  12166. category (org-get-category))
  12167. (setq i llast llast level)
  12168. ;; remove tag lists from same and sublevels
  12169. (while (>= i level)
  12170. (when (setq entry (assoc i tags-alist))
  12171. (setq tags-alist (delete entry tags-alist)))
  12172. (setq i (1- i)))
  12173. ;; add the next tags
  12174. (when tags
  12175. (setq tags (org-split-string tags ":")
  12176. tags-alist
  12177. (cons (cons level tags) tags-alist)))
  12178. ;; compile tags for current headline
  12179. (setq tags-list
  12180. (if org-use-tag-inheritance
  12181. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12182. tags)
  12183. org-scanner-tags tags-list)
  12184. (when org-use-tag-inheritance
  12185. (setcdr (car tags-alist)
  12186. (mapcar (lambda (x)
  12187. (setq x (copy-sequence x))
  12188. (org-add-prop-inherited x))
  12189. (cdar tags-alist))))
  12190. (when (and tags org-use-tag-inheritance
  12191. (or (not (eq t org-use-tag-inheritance))
  12192. org-tags-exclude-from-inheritance))
  12193. ;; selective inheritance, remove uninherited ones
  12194. (setcdr (car tags-alist)
  12195. (org-remove-uninherited-tags (cdar tags-alist))))
  12196. (when (and
  12197. ;; eval matcher only when the todo condition is OK
  12198. (and (or (not todo-only) (member todo org-not-done-keywords))
  12199. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12200. (eval matcher)))
  12201. ;; Call the skipper, but return t if it does not skip,
  12202. ;; so that the `and' form continues evaluating
  12203. (progn
  12204. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12205. t)
  12206. ;; Check if timestamps are deselecting this entry
  12207. (or (not todo-only)
  12208. (and (member todo org-not-done-keywords)
  12209. (or (not org-agenda-tags-todo-honor-ignore-options)
  12210. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12211. ;; select this headline
  12212. (cond
  12213. ((eq action 'sparse-tree)
  12214. (and org-highlight-sparse-tree-matches
  12215. (org-get-heading) (match-end 0)
  12216. (org-highlight-new-match
  12217. (match-beginning 1) (match-end 1)))
  12218. (org-show-context 'tags-tree))
  12219. ((eq action 'agenda)
  12220. (setq txt (org-agenda-format-item
  12221. ""
  12222. (concat
  12223. (if (eq org-tags-match-list-sublevels 'indented)
  12224. (make-string (1- level) ?.) "")
  12225. (org-get-heading))
  12226. level category
  12227. tags-list)
  12228. priority (org-get-priority txt))
  12229. (goto-char lspos)
  12230. (setq marker (org-agenda-new-marker))
  12231. (org-add-props txt props
  12232. 'org-marker marker 'org-hd-marker marker 'org-category category
  12233. 'todo-state todo
  12234. 'priority priority 'type "tagsmatch")
  12235. (push txt rtn))
  12236. ((functionp action)
  12237. (setq org-map-continue-from nil)
  12238. (save-excursion
  12239. (setq rtn1 (funcall action))
  12240. (push rtn1 rtn)))
  12241. (t (error "Invalid action")))
  12242. ;; if we are to skip sublevels, jump to end of subtree
  12243. (unless org-tags-match-list-sublevels
  12244. (org-end-of-subtree t)
  12245. (backward-char 1))))
  12246. ;; Get the correct position from where to continue
  12247. (if org-map-continue-from
  12248. (goto-char org-map-continue-from)
  12249. (and (= (point) lspos) (end-of-line 1)))))
  12250. (when (and (eq action 'sparse-tree)
  12251. (not org-sparse-tree-open-archived-trees))
  12252. (org-hide-archived-subtrees (point-min) (point-max)))
  12253. (nreverse rtn)))
  12254. (defun org-remove-uninherited-tags (tags)
  12255. "Remove all tags that are not inherited from the list TAGS."
  12256. (cond
  12257. ((eq org-use-tag-inheritance t)
  12258. (if org-tags-exclude-from-inheritance
  12259. (org-delete-all org-tags-exclude-from-inheritance tags)
  12260. tags))
  12261. ((not org-use-tag-inheritance) nil)
  12262. ((stringp org-use-tag-inheritance)
  12263. (delq nil (mapcar
  12264. (lambda (x)
  12265. (if (and (string-match org-use-tag-inheritance x)
  12266. (not (member x org-tags-exclude-from-inheritance)))
  12267. x nil))
  12268. tags)))
  12269. ((listp org-use-tag-inheritance)
  12270. (delq nil (mapcar
  12271. (lambda (x)
  12272. (if (member x org-use-tag-inheritance) x nil))
  12273. tags)))))
  12274. (defun org-match-sparse-tree (&optional todo-only match)
  12275. "Create a sparse tree according to tags string MATCH.
  12276. MATCH can contain positive and negative selection of tags, like
  12277. \"+WORK+URGENT-WITHBOSS\".
  12278. If optional argument TODO-ONLY is non-nil, only select lines that are
  12279. also TODO lines."
  12280. (interactive "P")
  12281. (org-agenda-prepare-buffers (list (current-buffer)))
  12282. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12283. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12284. (defvar org-cached-props nil)
  12285. (defun org-cached-entry-get (pom property)
  12286. (if (or (eq t org-use-property-inheritance)
  12287. (and (stringp org-use-property-inheritance)
  12288. (string-match org-use-property-inheritance property))
  12289. (and (listp org-use-property-inheritance)
  12290. (member property org-use-property-inheritance)))
  12291. ;; Caching is not possible, check it directly
  12292. (org-entry-get pom property 'inherit)
  12293. ;; Get all properties, so that we can do complicated checks easily
  12294. (cdr (assoc property (or org-cached-props
  12295. (setq org-cached-props
  12296. (org-entry-properties pom)))))))
  12297. (defun org-global-tags-completion-table (&optional files)
  12298. "Return the list of all tags in all agenda buffer/files.
  12299. Optional FILES argument is a list of files which can be used
  12300. instead of the agenda files."
  12301. (save-excursion
  12302. (org-uniquify
  12303. (delq nil
  12304. (apply 'append
  12305. (mapcar
  12306. (lambda (file)
  12307. (set-buffer (find-file-noselect file))
  12308. (append (org-get-buffer-tags)
  12309. (mapcar (lambda (x) (if (stringp (car-safe x))
  12310. (list (car-safe x)) nil))
  12311. org-tag-alist)))
  12312. (if (and files (car files))
  12313. files
  12314. (org-agenda-files))))))))
  12315. (defun org-make-tags-matcher (match)
  12316. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12317. The variable `todo-only' is scoped dynamically into this function.
  12318. It will be set to t if the matcher restricts matching to TODO entries,
  12319. otherwise will not be touched.
  12320. Returns a cons of the selection string MATCH and the constructed
  12321. lisp form implementing the matcher. The matcher is to be evaluated
  12322. at an Org entry, with point on the headline, and returns t if the
  12323. entry matches the selection string MATCH. The returned lisp form
  12324. references two variables with information about the entry, which
  12325. must be bound around the form's evaluation: todo, the TODO keyword
  12326. at the entry (or nil of none); and tags-list, the list of all tags
  12327. at the entry including inherited ones. Additionally, the category
  12328. of the entry (if any) must be specified as the text property
  12329. 'org-category on the headline.
  12330. See also `org-scan-tags'.
  12331. "
  12332. (declare (special todo-only))
  12333. (unless (boundp 'todo-only)
  12334. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12335. (unless match
  12336. ;; Get a new match request, with completion
  12337. (let ((org-last-tags-completion-table
  12338. (org-global-tags-completion-table)))
  12339. (setq match (org-completing-read-no-i
  12340. "Match: " 'org-tags-completion-function nil nil nil
  12341. 'org-tags-history))))
  12342. ;; Parse the string and create a lisp form
  12343. (let ((match0 match)
  12344. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12345. minus tag mm
  12346. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12347. orterms term orlist re-p str-p level-p level-op time-p
  12348. prop-p pn pv po gv rest)
  12349. (if (string-match "/+" match)
  12350. ;; match contains also a todo-matching request
  12351. (progn
  12352. (setq tagsmatch (substring match 0 (match-beginning 0))
  12353. todomatch (substring match (match-end 0)))
  12354. (if (string-match "^!" todomatch)
  12355. (setq todo-only t todomatch (substring todomatch 1)))
  12356. (if (string-match "^\\s-*$" todomatch)
  12357. (setq todomatch nil)))
  12358. ;; only matching tags
  12359. (setq tagsmatch match todomatch nil))
  12360. ;; Make the tags matcher
  12361. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12362. (setq tagsmatcher t)
  12363. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12364. (while (setq term (pop orterms))
  12365. (while (and (equal (substring term -1) "\\") orterms)
  12366. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12367. (while (string-match re term)
  12368. (setq rest (substring term (match-end 0))
  12369. minus (and (match-end 1)
  12370. (equal (match-string 1 term) "-"))
  12371. tag (save-match-data (replace-regexp-in-string
  12372. "\\\\-" "-"
  12373. (match-string 2 term)))
  12374. re-p (equal (string-to-char tag) ?{)
  12375. level-p (match-end 4)
  12376. prop-p (match-end 5)
  12377. mm (cond
  12378. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12379. (level-p
  12380. (setq level-op (org-op-to-function (match-string 3 term)))
  12381. `(,level-op level ,(string-to-number
  12382. (match-string 4 term))))
  12383. (prop-p
  12384. (setq pn (match-string 5 term)
  12385. po (match-string 6 term)
  12386. pv (match-string 7 term)
  12387. re-p (equal (string-to-char pv) ?{)
  12388. str-p (equal (string-to-char pv) ?\")
  12389. time-p (save-match-data
  12390. (string-match "^\"[[<].*[]>]\"$" pv))
  12391. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12392. (if time-p (setq pv (org-matcher-time pv)))
  12393. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12394. (cond
  12395. ((equal pn "CATEGORY")
  12396. (setq gv '(get-text-property (point) 'org-category)))
  12397. ((equal pn "TODO")
  12398. (setq gv 'todo))
  12399. (t
  12400. (setq gv `(org-cached-entry-get nil ,pn))))
  12401. (if re-p
  12402. (if (eq po 'org<>)
  12403. `(not (string-match ,pv (or ,gv "")))
  12404. `(string-match ,pv (or ,gv "")))
  12405. (if str-p
  12406. `(,po (or ,gv "") ,pv)
  12407. `(,po (string-to-number (or ,gv ""))
  12408. ,(string-to-number pv) ))))
  12409. (t `(member ,tag tags-list)))
  12410. mm (if minus (list 'not mm) mm)
  12411. term rest)
  12412. (push mm tagsmatcher))
  12413. (push (if (> (length tagsmatcher) 1)
  12414. (cons 'and tagsmatcher)
  12415. (car tagsmatcher))
  12416. orlist)
  12417. (setq tagsmatcher nil))
  12418. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12419. (setq tagsmatcher
  12420. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12421. ;; Make the todo matcher
  12422. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12423. (setq todomatcher t)
  12424. (setq orterms (org-split-string todomatch "|") orlist nil)
  12425. (while (setq term (pop orterms))
  12426. (while (string-match re term)
  12427. (setq minus (and (match-end 1)
  12428. (equal (match-string 1 term) "-"))
  12429. kwd (match-string 2 term)
  12430. re-p (equal (string-to-char kwd) ?{)
  12431. term (substring term (match-end 0))
  12432. mm (if re-p
  12433. `(string-match ,(substring kwd 1 -1) todo)
  12434. (list 'equal 'todo kwd))
  12435. mm (if minus (list 'not mm) mm))
  12436. (push mm todomatcher))
  12437. (push (if (> (length todomatcher) 1)
  12438. (cons 'and todomatcher)
  12439. (car todomatcher))
  12440. orlist)
  12441. (setq todomatcher nil))
  12442. (setq todomatcher (if (> (length orlist) 1)
  12443. (cons 'or orlist) (car orlist))))
  12444. ;; Return the string and lisp forms of the matcher
  12445. (setq matcher (if todomatcher
  12446. (list 'and tagsmatcher todomatcher)
  12447. tagsmatcher))
  12448. (when todo-only
  12449. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12450. matcher)))
  12451. (cons match0 matcher)))
  12452. (defun org-op-to-function (op &optional stringp)
  12453. "Turn an operator into the appropriate function."
  12454. (setq op
  12455. (cond
  12456. ((equal op "<" ) '(< string< org-time<))
  12457. ((equal op ">" ) '(> org-string> org-time>))
  12458. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12459. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12460. ((member op '("=" "==")) '(= string= org-time=))
  12461. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12462. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12463. (defun org<> (a b) (not (= a b)))
  12464. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12465. (defun org-string>= (a b) (not (string< a b)))
  12466. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12467. (defun org-string<> (a b) (not (string= a b)))
  12468. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12469. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12470. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12471. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12472. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12473. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12474. (defun org-2ft (s)
  12475. "Convert S to a floating point time.
  12476. If S is already a number, just return it. If it is a string, parse
  12477. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12478. (cond
  12479. ((numberp s) s)
  12480. ((stringp s)
  12481. (condition-case nil
  12482. (float-time (apply 'encode-time (org-parse-time-string s)))
  12483. (error 0.)))
  12484. (t 0.)))
  12485. (defun org-time-today ()
  12486. "Time in seconds today at 0:00.
  12487. Returns the float number of seconds since the beginning of the
  12488. epoch to the beginning of today (00:00)."
  12489. (float-time (apply 'encode-time
  12490. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12491. (defun org-matcher-time (s)
  12492. "Interpret a time comparison value."
  12493. (save-match-data
  12494. (cond
  12495. ((string= s "<now>") (float-time))
  12496. ((string= s "<today>") (org-time-today))
  12497. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12498. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12499. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12500. (+ (org-time-today)
  12501. (* (string-to-number (match-string 1 s))
  12502. (cdr (assoc (match-string 2 s)
  12503. '(("d" . 86400.0) ("w" . 604800.0)
  12504. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12505. (t (org-2ft s)))))
  12506. (defun org-match-any-p (re list)
  12507. "Does re match any element of list?"
  12508. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12509. (delq nil list))
  12510. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12511. (defvar org-tags-overlay (make-overlay 1 1))
  12512. (org-detach-overlay org-tags-overlay)
  12513. (defun org-get-local-tags-at (&optional pos)
  12514. "Get a list of tags defined in the current headline."
  12515. (org-get-tags-at pos 'local))
  12516. (defun org-get-local-tags ()
  12517. "Get a list of tags defined in the current headline."
  12518. (org-get-tags-at nil 'local))
  12519. (defun org-get-tags-at (&optional pos local)
  12520. "Get a list of all headline tags applicable at POS.
  12521. POS defaults to point. If tags are inherited, the list contains
  12522. the targets in the same sequence as the headlines appear, i.e.
  12523. the tags of the current headline come last.
  12524. When LOCAL is non-nil, only return tags from the current headline,
  12525. ignore inherited ones."
  12526. (interactive)
  12527. (if (and org-trust-scanner-tags
  12528. (or (not pos) (equal pos (point)))
  12529. (not local))
  12530. org-scanner-tags
  12531. (let (tags ltags lastpos parent)
  12532. (save-excursion
  12533. (save-restriction
  12534. (widen)
  12535. (goto-char (or pos (point)))
  12536. (save-match-data
  12537. (catch 'done
  12538. (condition-case nil
  12539. (progn
  12540. (org-back-to-heading t)
  12541. (while (not (equal lastpos (point)))
  12542. (setq lastpos (point))
  12543. (when (looking-at
  12544. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12545. (setq ltags (org-split-string
  12546. (org-match-string-no-properties 1) ":"))
  12547. (when parent
  12548. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12549. (setq tags (append
  12550. (if parent
  12551. (org-remove-uninherited-tags ltags)
  12552. ltags)
  12553. tags)))
  12554. (or org-use-tag-inheritance (throw 'done t))
  12555. (if local (throw 'done t))
  12556. (or (org-up-heading-safe) (error nil))
  12557. (setq parent t)))
  12558. (error nil)))))
  12559. (if local
  12560. tags
  12561. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12562. (defun org-add-prop-inherited (s)
  12563. (add-text-properties 0 (length s) '(inherited t) s)
  12564. s)
  12565. (defun org-toggle-tag (tag &optional onoff)
  12566. "Toggle the tag TAG for the current line.
  12567. If ONOFF is `on' or `off', don't toggle but set to this state."
  12568. (let (res current)
  12569. (save-excursion
  12570. (org-back-to-heading t)
  12571. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12572. (point-at-eol) t)
  12573. (progn
  12574. (setq current (match-string 1))
  12575. (replace-match ""))
  12576. (setq current ""))
  12577. (setq current (nreverse (org-split-string current ":")))
  12578. (cond
  12579. ((eq onoff 'on)
  12580. (setq res t)
  12581. (or (member tag current) (push tag current)))
  12582. ((eq onoff 'off)
  12583. (or (not (member tag current)) (setq current (delete tag current))))
  12584. (t (if (member tag current)
  12585. (setq current (delete tag current))
  12586. (setq res t)
  12587. (push tag current))))
  12588. (end-of-line 1)
  12589. (if current
  12590. (progn
  12591. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12592. (org-set-tags nil t))
  12593. (delete-horizontal-space))
  12594. (run-hooks 'org-after-tags-change-hook))
  12595. res))
  12596. (defun org-align-tags-here (to-col)
  12597. ;; Assumes that this is a headline
  12598. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12599. (beginning-of-line 1)
  12600. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12601. (< pos (match-beginning 2)))
  12602. (progn
  12603. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12604. (goto-char (match-beginning 1))
  12605. (insert " ")
  12606. (delete-region (point) (1+ (match-beginning 2)))
  12607. (setq ncol (max (current-column)
  12608. (1+ col)
  12609. (if (> to-col 0)
  12610. to-col
  12611. (- (abs to-col) tags-l))))
  12612. (setq p (point))
  12613. (insert (make-string (- ncol (current-column)) ?\ ))
  12614. (setq ncol (current-column))
  12615. (when indent-tabs-mode (tabify p (point-at-eol)))
  12616. (org-move-to-column (min ncol col) t))
  12617. (goto-char pos))))
  12618. (defun org-set-tags-command (&optional arg just-align)
  12619. "Call the set-tags command for the current entry."
  12620. (interactive "P")
  12621. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12622. (org-set-tags arg just-align)
  12623. (save-excursion
  12624. (org-back-to-heading t)
  12625. (org-set-tags arg just-align))))
  12626. (defun org-set-tags-to (data)
  12627. "Set the tags of the current entry to DATA, replacing the current tags.
  12628. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12629. If DATA is nil or the empty string, any tags will be removed."
  12630. (interactive "sTags: ")
  12631. (setq data
  12632. (cond
  12633. ((eq data nil) "")
  12634. ((equal data "") "")
  12635. ((stringp data)
  12636. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12637. ":"))
  12638. ((listp data)
  12639. (concat ":" (mapconcat 'identity data ":") ":"))))
  12640. (when data
  12641. (save-excursion
  12642. (org-back-to-heading t)
  12643. (when (looking-at org-complex-heading-regexp)
  12644. (if (match-end 5)
  12645. (progn
  12646. (goto-char (match-beginning 5))
  12647. (insert data)
  12648. (delete-region (point) (point-at-eol))
  12649. (org-set-tags nil 'align))
  12650. (goto-char (point-at-eol))
  12651. (insert " " data)
  12652. (org-set-tags nil 'align)))
  12653. (beginning-of-line 1)
  12654. (if (looking-at ".*?\\([ \t]+\\)$")
  12655. (delete-region (match-beginning 1) (match-end 1))))))
  12656. (defun org-align-all-tags ()
  12657. "Align the tags i all headings."
  12658. (interactive)
  12659. (save-excursion
  12660. (or (ignore-errors (org-back-to-heading t))
  12661. (outline-next-heading))
  12662. (if (org-at-heading-p)
  12663. (org-set-tags t)
  12664. (message "No headings"))))
  12665. (defvar org-indent-indentation-per-level)
  12666. (defun org-set-tags (&optional arg just-align)
  12667. "Set the tags for the current headline.
  12668. With prefix ARG, realign all tags in headings in the current buffer."
  12669. (interactive "P")
  12670. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12671. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12672. 'region-start-level 'region))
  12673. org-loop-over-headlines-in-active-region)
  12674. (org-map-entries
  12675. ;; We don't use ARG and JUST-ALIGN here these args are not
  12676. ;; useful when looping over headlines
  12677. `(org-set-tags)
  12678. org-loop-over-headlines-in-active-region
  12679. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12680. (let* ((re org-outline-regexp-bol)
  12681. (current (unless arg (org-get-tags-string)))
  12682. (col (current-column))
  12683. (org-setting-tags t)
  12684. table current-tags inherited-tags ; computed below when needed
  12685. tags p0 c0 c1 rpl di tc level)
  12686. (if arg
  12687. (save-excursion
  12688. (goto-char (point-min))
  12689. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12690. (while (re-search-forward re nil t)
  12691. (org-set-tags nil t)
  12692. (end-of-line 1)))
  12693. (message "All tags realigned to column %d" org-tags-column))
  12694. (if just-align
  12695. (setq tags current)
  12696. ;; Get a new set of tags from the user
  12697. (save-excursion
  12698. (setq table (append org-tag-persistent-alist
  12699. (or org-tag-alist (org-get-buffer-tags))
  12700. (and
  12701. org-complete-tags-always-offer-all-agenda-tags
  12702. (org-global-tags-completion-table
  12703. (org-agenda-files))))
  12704. org-last-tags-completion-table table
  12705. current-tags (org-split-string current ":")
  12706. inherited-tags (nreverse
  12707. (nthcdr (length current-tags)
  12708. (nreverse (org-get-tags-at))))
  12709. tags
  12710. (if (or (eq t org-use-fast-tag-selection)
  12711. (and org-use-fast-tag-selection
  12712. (delq nil (mapcar 'cdr table))))
  12713. (org-fast-tag-selection
  12714. current-tags inherited-tags table
  12715. (if org-fast-tag-selection-include-todo
  12716. org-todo-key-alist))
  12717. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12718. (org-trim
  12719. (org-icompleting-read "Tags: "
  12720. 'org-tags-completion-function
  12721. nil nil current 'org-tags-history))))))
  12722. (while (string-match "[-+&]+" tags)
  12723. ;; No boolean logic, just a list
  12724. (setq tags (replace-match ":" t t tags))))
  12725. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12726. (if org-tags-sort-function
  12727. (setq tags (mapconcat 'identity
  12728. (sort (org-split-string
  12729. tags (org-re "[^[:alnum:]_@#%]+"))
  12730. org-tags-sort-function) ":")))
  12731. (if (string-match "\\`[\t ]*\\'" tags)
  12732. (setq tags "")
  12733. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12734. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12735. ;; Insert new tags at the correct column
  12736. (beginning-of-line 1)
  12737. (setq level (or (and (looking-at org-outline-regexp)
  12738. (- (match-end 0) (point) 1))
  12739. 1))
  12740. (cond
  12741. ((and (equal current "") (equal tags "")))
  12742. ((re-search-forward
  12743. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12744. (point-at-eol) t)
  12745. (if (equal tags "")
  12746. (setq rpl "")
  12747. (goto-char (match-beginning 0))
  12748. (setq c0 (current-column)
  12749. ;; compute offset for the case of org-indent-mode active
  12750. di (if org-indent-mode
  12751. (* (1- org-indent-indentation-per-level) (1- level))
  12752. 0)
  12753. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12754. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12755. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12756. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12757. (replace-match rpl t t)
  12758. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12759. tags)
  12760. (t (error "Tags alignment failed")))
  12761. (org-move-to-column col)
  12762. (unless just-align
  12763. (run-hooks 'org-after-tags-change-hook))))))
  12764. (defun org-change-tag-in-region (beg end tag off)
  12765. "Add or remove TAG for each entry in the region.
  12766. This works in the agenda, and also in an org-mode buffer."
  12767. (interactive
  12768. (list (region-beginning) (region-end)
  12769. (let ((org-last-tags-completion-table
  12770. (if (derived-mode-p 'org-mode)
  12771. (org-get-buffer-tags)
  12772. (org-global-tags-completion-table))))
  12773. (org-icompleting-read
  12774. "Tag: " 'org-tags-completion-function nil nil nil
  12775. 'org-tags-history))
  12776. (progn
  12777. (message "[s]et or [r]emove? ")
  12778. (equal (read-char-exclusive) ?r))))
  12779. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12780. (let ((agendap (equal major-mode 'org-agenda-mode))
  12781. l1 l2 m buf pos newhead (cnt 0))
  12782. (goto-char end)
  12783. (setq l2 (1- (org-current-line)))
  12784. (goto-char beg)
  12785. (setq l1 (org-current-line))
  12786. (loop for l from l1 to l2 do
  12787. (org-goto-line l)
  12788. (setq m (get-text-property (point) 'org-hd-marker))
  12789. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12790. (and agendap m))
  12791. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12792. pos (if agendap m (point)))
  12793. (with-current-buffer buf
  12794. (save-excursion
  12795. (save-restriction
  12796. (goto-char pos)
  12797. (setq cnt (1+ cnt))
  12798. (org-toggle-tag tag (if off 'off 'on))
  12799. (setq newhead (org-get-heading)))))
  12800. (and agendap (org-agenda-change-all-lines newhead m))))
  12801. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12802. (defun org-tags-completion-function (string predicate &optional flag)
  12803. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12804. (confirm (lambda (x) (stringp (car x)))))
  12805. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12806. (setq s1 (match-string 1 string)
  12807. s2 (match-string 2 string))
  12808. (setq s1 "" s2 string))
  12809. (cond
  12810. ((eq flag nil)
  12811. ;; try completion
  12812. (setq rtn (try-completion s2 ctable confirm))
  12813. (if (stringp rtn)
  12814. (setq rtn
  12815. (concat s1 s2 (substring rtn (length s2))
  12816. (if (and org-add-colon-after-tag-completion
  12817. (assoc rtn ctable))
  12818. ":" ""))))
  12819. rtn)
  12820. ((eq flag t)
  12821. ;; all-completions
  12822. (all-completions s2 ctable confirm)
  12823. )
  12824. ((eq flag 'lambda)
  12825. ;; exact match?
  12826. (assoc s2 ctable)))
  12827. ))
  12828. (defun org-fast-tag-insert (kwd tags face &optional end)
  12829. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12830. (insert (format "%-12s" (concat kwd ":"))
  12831. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12832. (or end "")))
  12833. (defun org-fast-tag-show-exit (flag)
  12834. (save-excursion
  12835. (org-goto-line 3)
  12836. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12837. (replace-match ""))
  12838. (when flag
  12839. (end-of-line 1)
  12840. (org-move-to-column (- (window-width) 19) t)
  12841. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12842. (defun org-set-current-tags-overlay (current prefix)
  12843. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12844. (if (featurep 'xemacs)
  12845. (org-overlay-display org-tags-overlay (concat prefix s)
  12846. 'secondary-selection)
  12847. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12848. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12849. (defvar org-last-tag-selection-key nil)
  12850. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12851. "Fast tag selection with single keys.
  12852. CURRENT is the current list of tags in the headline, INHERITED is the
  12853. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12854. possibly with grouping information. TODO-TABLE is a similar table with
  12855. TODO keywords, should these have keys assigned to them.
  12856. If the keys are nil, a-z are automatically assigned.
  12857. Returns the new tags string, or nil to not change the current settings."
  12858. (let* ((fulltable (append table todo-table))
  12859. (maxlen (apply 'max (mapcar
  12860. (lambda (x)
  12861. (if (stringp (car x)) (string-width (car x)) 0))
  12862. fulltable)))
  12863. (buf (current-buffer))
  12864. (expert (eq org-fast-tag-selection-single-key 'expert))
  12865. (buffer-tags nil)
  12866. (fwidth (+ maxlen 3 1 3))
  12867. (ncol (/ (- (window-width) 4) fwidth))
  12868. (i-face 'org-done)
  12869. (c-face 'org-todo)
  12870. tg cnt e c char c1 c2 ntable tbl rtn
  12871. ov-start ov-end ov-prefix
  12872. (exit-after-next org-fast-tag-selection-single-key)
  12873. (done-keywords org-done-keywords)
  12874. groups ingroup)
  12875. (save-excursion
  12876. (beginning-of-line 1)
  12877. (if (looking-at
  12878. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12879. (setq ov-start (match-beginning 1)
  12880. ov-end (match-end 1)
  12881. ov-prefix "")
  12882. (setq ov-start (1- (point-at-eol))
  12883. ov-end (1+ ov-start))
  12884. (skip-chars-forward "^\n\r")
  12885. (setq ov-prefix
  12886. (concat
  12887. (buffer-substring (1- (point)) (point))
  12888. (if (> (current-column) org-tags-column)
  12889. " "
  12890. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12891. (move-overlay org-tags-overlay ov-start ov-end)
  12892. (save-window-excursion
  12893. (if expert
  12894. (set-buffer (get-buffer-create " *Org tags*"))
  12895. (delete-other-windows)
  12896. (split-window-vertically)
  12897. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12898. (erase-buffer)
  12899. (org-set-local 'org-done-keywords done-keywords)
  12900. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12901. (org-fast-tag-insert "Current" current c-face "\n\n")
  12902. (org-fast-tag-show-exit exit-after-next)
  12903. (org-set-current-tags-overlay current ov-prefix)
  12904. (setq tbl fulltable char ?a cnt 0)
  12905. (while (setq e (pop tbl))
  12906. (cond
  12907. ((equal (car e) :startgroup)
  12908. (push '() groups) (setq ingroup t)
  12909. (when (not (= cnt 0))
  12910. (setq cnt 0)
  12911. (insert "\n"))
  12912. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12913. ((equal (car e) :endgroup)
  12914. (setq ingroup nil cnt 0)
  12915. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12916. ((equal e '(:newline))
  12917. (when (not (= cnt 0))
  12918. (setq cnt 0)
  12919. (insert "\n")
  12920. (setq e (car tbl))
  12921. (while (equal (car tbl) '(:newline))
  12922. (insert "\n")
  12923. (setq tbl (cdr tbl)))))
  12924. (t
  12925. (setq tg (copy-sequence (car e)) c2 nil)
  12926. (if (cdr e)
  12927. (setq c (cdr e))
  12928. ;; automatically assign a character.
  12929. (setq c1 (string-to-char
  12930. (downcase (substring
  12931. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12932. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12933. (while (or (rassoc char ntable) (rassoc char table))
  12934. (setq char (1+ char)))
  12935. (setq c2 c1))
  12936. (setq c (or c2 char)))
  12937. (if ingroup (push tg (car groups)))
  12938. (setq tg (org-add-props tg nil 'face
  12939. (cond
  12940. ((not (assoc tg table))
  12941. (org-get-todo-face tg))
  12942. ((member tg current) c-face)
  12943. ((member tg inherited) i-face))))
  12944. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12945. (insert "[" c "] " tg (make-string
  12946. (- fwidth 4 (length tg)) ?\ ))
  12947. (push (cons tg c) ntable)
  12948. (when (= (setq cnt (1+ cnt)) ncol)
  12949. (insert "\n")
  12950. (if ingroup (insert " "))
  12951. (setq cnt 0)))))
  12952. (setq ntable (nreverse ntable))
  12953. (insert "\n")
  12954. (goto-char (point-min))
  12955. (if (not expert) (org-fit-window-to-buffer))
  12956. (setq rtn
  12957. (catch 'exit
  12958. (while t
  12959. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12960. (if (not groups) "no " "")
  12961. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12962. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12963. (setq org-last-tag-selection-key c)
  12964. (cond
  12965. ((= c ?\r) (throw 'exit t))
  12966. ((= c ?!)
  12967. (setq groups (not groups))
  12968. (goto-char (point-min))
  12969. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12970. ((= c ?\C-c)
  12971. (if (not expert)
  12972. (org-fast-tag-show-exit
  12973. (setq exit-after-next (not exit-after-next)))
  12974. (setq expert nil)
  12975. (delete-other-windows)
  12976. (set-window-buffer (split-window-vertically) " *Org tags*")
  12977. (org-switch-to-buffer-other-window " *Org tags*")
  12978. (org-fit-window-to-buffer)))
  12979. ((or (= c ?\C-g)
  12980. (and (= c ?q) (not (rassoc c ntable))))
  12981. (org-detach-overlay org-tags-overlay)
  12982. (setq quit-flag t))
  12983. ((= c ?\ )
  12984. (setq current nil)
  12985. (if exit-after-next (setq exit-after-next 'now)))
  12986. ((= c ?\t)
  12987. (condition-case nil
  12988. (setq tg (org-icompleting-read
  12989. "Tag: "
  12990. (or buffer-tags
  12991. (with-current-buffer buf
  12992. (org-get-buffer-tags)))))
  12993. (quit (setq tg "")))
  12994. (when (string-match "\\S-" tg)
  12995. (add-to-list 'buffer-tags (list tg))
  12996. (if (member tg current)
  12997. (setq current (delete tg current))
  12998. (push tg current)))
  12999. (if exit-after-next (setq exit-after-next 'now)))
  13000. ((setq e (rassoc c todo-table) tg (car e))
  13001. (with-current-buffer buf
  13002. (save-excursion (org-todo tg)))
  13003. (if exit-after-next (setq exit-after-next 'now)))
  13004. ((setq e (rassoc c ntable) tg (car e))
  13005. (if (member tg current)
  13006. (setq current (delete tg current))
  13007. (loop for g in groups do
  13008. (if (member tg g)
  13009. (mapc (lambda (x)
  13010. (setq current (delete x current)))
  13011. g)))
  13012. (push tg current))
  13013. (if exit-after-next (setq exit-after-next 'now))))
  13014. ;; Create a sorted list
  13015. (setq current
  13016. (sort current
  13017. (lambda (a b)
  13018. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13019. (if (eq exit-after-next 'now) (throw 'exit t))
  13020. (goto-char (point-min))
  13021. (beginning-of-line 2)
  13022. (delete-region (point) (point-at-eol))
  13023. (org-fast-tag-insert "Current" current c-face)
  13024. (org-set-current-tags-overlay current ov-prefix)
  13025. (while (re-search-forward
  13026. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13027. (setq tg (match-string 1))
  13028. (add-text-properties
  13029. (match-beginning 1) (match-end 1)
  13030. (list 'face
  13031. (cond
  13032. ((member tg current) c-face)
  13033. ((member tg inherited) i-face)
  13034. (t (get-text-property (match-beginning 1) 'face))))))
  13035. (goto-char (point-min)))))
  13036. (org-detach-overlay org-tags-overlay)
  13037. (if rtn
  13038. (mapconcat 'identity current ":")
  13039. nil))))
  13040. (defun org-get-tags-string ()
  13041. "Get the TAGS string in the current headline."
  13042. (unless (org-at-heading-p t)
  13043. (error "Not on a heading"))
  13044. (save-excursion
  13045. (beginning-of-line 1)
  13046. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13047. (org-match-string-no-properties 1)
  13048. "")))
  13049. (defun org-get-tags ()
  13050. "Get the list of tags specified in the current headline."
  13051. (org-split-string (org-get-tags-string) ":"))
  13052. (defun org-get-buffer-tags ()
  13053. "Get a table of all tags used in the buffer, for completion."
  13054. (let (tags)
  13055. (save-excursion
  13056. (goto-char (point-min))
  13057. (while (re-search-forward
  13058. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13059. (when (equal (char-after (point-at-bol 0)) ?*)
  13060. (mapc (lambda (x) (add-to-list 'tags x))
  13061. (org-split-string (org-match-string-no-properties 1) ":")))))
  13062. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13063. (mapcar 'list tags)))
  13064. ;;;; The mapping API
  13065. (defun org-map-entries (func &optional match scope &rest skip)
  13066. "Call FUNC at each headline selected by MATCH in SCOPE.
  13067. FUNC is a function or a lisp form. The function will be called without
  13068. arguments, with the cursor positioned at the beginning of the headline.
  13069. The return values of all calls to the function will be collected and
  13070. returned as a list.
  13071. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13072. does not need to preserve point. After evaluation, the cursor will be
  13073. moved to the end of the line (presumably of the headline of the
  13074. processed entry) and search continues from there. Under some
  13075. circumstances, this may not produce the wanted results. For example,
  13076. if you have removed (e.g. archived) the current (sub)tree it could
  13077. mean that the next entry will be skipped entirely. In such cases, you
  13078. can specify the position from where search should continue by making
  13079. FUNC set the variable `org-map-continue-from' to the desired buffer
  13080. position.
  13081. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13082. Only headlines that are matched by this query will be considered during
  13083. the iteration. When MATCH is nil or t, all headlines will be
  13084. visited by the iteration.
  13085. SCOPE determines the scope of this command. It can be any of:
  13086. nil The current buffer, respecting the restriction if any
  13087. tree The subtree started with the entry at point
  13088. region The entries within the active region, if any
  13089. region-start-level
  13090. The entries within the active region, but only those at
  13091. the same level than the first one.
  13092. file The current buffer, without restriction
  13093. file-with-archives
  13094. The current buffer, and any archives associated with it
  13095. agenda All agenda files
  13096. agenda-with-archives
  13097. All agenda files with any archive files associated with them
  13098. \(file1 file2 ...)
  13099. If this is a list, all files in the list will be scanned
  13100. The remaining args are treated as settings for the skipping facilities of
  13101. the scanner. The following items can be given here:
  13102. archive skip trees with the archive tag.
  13103. comment skip trees with the COMMENT keyword
  13104. function or Emacs Lisp form:
  13105. will be used as value for `org-agenda-skip-function', so whenever
  13106. the function returns t, FUNC will not be called for that
  13107. entry and search will continue from the point where the
  13108. function leaves it.
  13109. If your function needs to retrieve the tags including inherited tags
  13110. at the *current* entry, you can use the value of the variable
  13111. `org-scanner-tags' which will be much faster than getting the value
  13112. with `org-get-tags-at'. If your function gets properties with
  13113. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13114. to t around the call to `org-entry-properties' to get the same speedup.
  13115. Note that if your function moves around to retrieve tags and properties at
  13116. a *different* entry, you cannot use these techniques."
  13117. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13118. (not (org-region-active-p)))
  13119. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13120. (org-agenda-skip-archived-trees (memq 'archive skip))
  13121. (org-agenda-skip-comment-trees (memq 'comment skip))
  13122. (org-agenda-skip-function
  13123. (car (org-delete-all '(comment archive) skip)))
  13124. (org-tags-match-list-sublevels t)
  13125. (start-level (eq scope 'region-start-level))
  13126. matcher file res
  13127. org-todo-keywords-for-agenda
  13128. org-done-keywords-for-agenda
  13129. org-todo-keyword-alist-for-agenda
  13130. org-drawers-for-agenda
  13131. org-tag-alist-for-agenda
  13132. todo-only)
  13133. (cond
  13134. ((eq match t) (setq matcher t))
  13135. ((eq match nil) (setq matcher t))
  13136. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13137. (save-excursion
  13138. (save-restriction
  13139. (cond ((eq scope 'tree)
  13140. (org-back-to-heading t)
  13141. (org-narrow-to-subtree)
  13142. (setq scope nil))
  13143. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13144. (org-region-active-p))
  13145. ;; If needed, set start-level to a string like "2"
  13146. (when start-level
  13147. (save-excursion
  13148. (goto-char (region-beginning))
  13149. (unless (org-at-heading-p) (outline-next-heading))
  13150. (setq start-level (org-current-level))))
  13151. (narrow-to-region (region-beginning)
  13152. (save-excursion
  13153. (goto-char (region-end))
  13154. (unless (and (bolp) (org-at-heading-p))
  13155. (outline-next-heading))
  13156. (point)))
  13157. (setq scope nil)))
  13158. (if (not scope)
  13159. (progn
  13160. (org-agenda-prepare-buffers
  13161. (list (buffer-file-name (current-buffer))))
  13162. (setq res (org-scan-tags func matcher todo-only start-level)))
  13163. ;; Get the right scope
  13164. (cond
  13165. ((and scope (listp scope) (symbolp (car scope)))
  13166. (setq scope (eval scope)))
  13167. ((eq scope 'agenda)
  13168. (setq scope (org-agenda-files t)))
  13169. ((eq scope 'agenda-with-archives)
  13170. (setq scope (org-agenda-files t))
  13171. (setq scope (org-add-archive-files scope)))
  13172. ((eq scope 'file)
  13173. (setq scope (list (buffer-file-name))))
  13174. ((eq scope 'file-with-archives)
  13175. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13176. (org-agenda-prepare-buffers scope)
  13177. (while (setq file (pop scope))
  13178. (with-current-buffer (org-find-base-buffer-visiting file)
  13179. (save-excursion
  13180. (save-restriction
  13181. (widen)
  13182. (goto-char (point-min))
  13183. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13184. res)))
  13185. ;;;; Properties
  13186. ;;; Setting and retrieving properties
  13187. (defconst org-special-properties
  13188. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13189. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13190. "The special properties valid in Org-mode.
  13191. These are properties that are not defined in the property drawer,
  13192. but in some other way.")
  13193. (defconst org-default-properties
  13194. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13195. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13196. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13197. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13198. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13199. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13200. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13201. "Some properties that are used by Org-mode for various purposes.
  13202. Being in this list makes sure that they are offered for completion.")
  13203. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13204. "Regular expression matching the first line of a property drawer.")
  13205. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13206. "Regular expression matching the last line of a property drawer.")
  13207. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13208. "Regular expression matching the first line of a property drawer.")
  13209. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13210. "Regular expression matching the first line of a property drawer.")
  13211. (defconst org-property-drawer-re
  13212. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13213. org-property-end-re "\\)\n?")
  13214. "Matches an entire property drawer.")
  13215. (defconst org-clock-drawer-re
  13216. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13217. org-property-end-re "\\)\n?")
  13218. "Matches an entire clock drawer.")
  13219. (defsubst org-re-property (property)
  13220. "Return a regexp matching a PROPERTY line.
  13221. Match group 1 will be set to the value."
  13222. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13223. (defsubst org-re-property-keyword (property)
  13224. "Return a regexp matching a PROPERTY line, possibly with no
  13225. value for the property."
  13226. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13227. (defun org-property-action ()
  13228. "Do an action on properties."
  13229. (interactive)
  13230. (let (c)
  13231. (org-at-property-p)
  13232. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13233. (setq c (read-char-exclusive))
  13234. (cond
  13235. ((equal c ?s)
  13236. (call-interactively 'org-set-property))
  13237. ((equal c ?d)
  13238. (call-interactively 'org-delete-property))
  13239. ((equal c ?D)
  13240. (call-interactively 'org-delete-property-globally))
  13241. ((equal c ?c)
  13242. (call-interactively 'org-compute-property-at-point))
  13243. (t (error "No such property action %c" c)))))
  13244. (defun org-inc-effort ()
  13245. "Increment the value of the effort property in the current entry."
  13246. (interactive)
  13247. (org-set-effort nil t))
  13248. (defun org-set-effort (&optional value increment)
  13249. "Set the effort property of the current entry.
  13250. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13251. 10th allowed value.
  13252. When INCREMENT is non-nil, set the property to the next allowed value."
  13253. (interactive "P")
  13254. (if (equal value 0) (setq value 10))
  13255. (let* ((completion-ignore-case t)
  13256. (prop org-effort-property)
  13257. (cur (org-entry-get nil prop))
  13258. (allowed (org-property-get-allowed-values nil prop 'table))
  13259. (existing (mapcar 'list (org-property-values prop)))
  13260. rpl
  13261. (val (cond
  13262. ((stringp value) value)
  13263. ((and allowed (integerp value))
  13264. (or (car (nth (1- value) allowed))
  13265. (car (org-last allowed))))
  13266. ((and allowed increment)
  13267. (or (caadr (member (list cur) allowed))
  13268. (error "Allowed effort values are not set")))
  13269. (allowed
  13270. (message "Select 1-9,0, [RET%s]: %s"
  13271. (if cur (concat "=" cur) "")
  13272. (mapconcat 'car allowed " "))
  13273. (setq rpl (read-char-exclusive))
  13274. (if (equal rpl ?\r)
  13275. cur
  13276. (setq rpl (- rpl ?0))
  13277. (if (equal rpl 0) (setq rpl 10))
  13278. (if (and (> rpl 0) (<= rpl (length allowed)))
  13279. (car (nth (1- rpl) allowed))
  13280. (org-completing-read "Effort: " allowed nil))))
  13281. (t
  13282. (let (org-completion-use-ido org-completion-use-iswitchb)
  13283. (org-completing-read
  13284. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13285. (concat "[" cur "]") "")
  13286. ": ")
  13287. existing nil nil "" nil cur))))))
  13288. (unless (equal (org-entry-get nil prop) val)
  13289. (org-entry-put nil prop val))
  13290. (save-excursion
  13291. (org-back-to-heading t)
  13292. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13293. (message "%s is now %s" prop val)))
  13294. (defun org-at-property-p ()
  13295. "Is cursor inside a property drawer?"
  13296. (save-excursion
  13297. (beginning-of-line 1)
  13298. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13299. (save-match-data ;; Used by calling procedures
  13300. (let ((p (point))
  13301. (range (unless (org-before-first-heading-p)
  13302. (org-get-property-block))))
  13303. (and range (<= (car range) p) (< p (cdr range))))))))
  13304. (defun org-get-property-block (&optional beg end force)
  13305. "Return the (beg . end) range of the body of the property drawer.
  13306. BEG and END are the beginning and end of the current subtree, or of
  13307. the part before the first headline. If they are not given, they will
  13308. be found. If the drawer does not exist and FORCE is non-nil, create
  13309. the drawer."
  13310. (catch 'exit
  13311. (save-excursion
  13312. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13313. (progn (org-back-to-heading t) (point))))
  13314. (end (or end (and (not (outline-next-heading)) (point-max))
  13315. (point))))
  13316. (goto-char beg)
  13317. (if (re-search-forward org-property-start-re end t)
  13318. (setq beg (1+ (match-end 0)))
  13319. (if force
  13320. (save-excursion
  13321. (org-insert-property-drawer)
  13322. (setq end (progn (outline-next-heading) (point))))
  13323. (throw 'exit nil))
  13324. (goto-char beg)
  13325. (if (re-search-forward org-property-start-re end t)
  13326. (setq beg (1+ (match-end 0)))))
  13327. (if (re-search-forward org-property-end-re end t)
  13328. (setq end (match-beginning 0))
  13329. (or force (throw 'exit nil))
  13330. (goto-char beg)
  13331. (setq end beg)
  13332. (org-indent-line)
  13333. (insert ":END:\n"))
  13334. (cons beg end)))))
  13335. (defun org-entry-properties (&optional pom which specific)
  13336. "Get all properties of the entry at point-or-marker POM.
  13337. This includes the TODO keyword, the tags, time strings for deadline,
  13338. scheduled, and clocking, and any additional properties defined in the
  13339. entry. The return value is an alist, keys may occur multiple times
  13340. if the property key was used several times.
  13341. POM may also be nil, in which case the current entry is used.
  13342. If WHICH is nil or `all', get all properties. If WHICH is
  13343. `special' or `standard', only get that subclass. If WHICH
  13344. is a string only get exactly this property. SPECIFIC can be a string, the
  13345. specific property we are interested in. Specifying it can speed
  13346. things up because then unnecessary parsing is avoided."
  13347. (setq which (or which 'all))
  13348. (org-with-point-at pom
  13349. (let ((clockstr (substring org-clock-string 0 -1))
  13350. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13351. (case-fold-search nil)
  13352. beg end range props sum-props key key1 value string clocksum clocksumt)
  13353. (save-excursion
  13354. (when (condition-case nil
  13355. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13356. (error nil))
  13357. (setq beg (point))
  13358. (setq sum-props (get-text-property (point) 'org-summaries))
  13359. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13360. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13361. (outline-next-heading)
  13362. (setq end (point))
  13363. (when (memq which '(all special))
  13364. ;; Get the special properties, like TODO and tags
  13365. (goto-char beg)
  13366. (when (and (or (not specific) (string= specific "TODO"))
  13367. (looking-at org-todo-line-regexp) (match-end 2))
  13368. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13369. (when (and (or (not specific) (string= specific "PRIORITY"))
  13370. (looking-at org-priority-regexp))
  13371. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13372. (when (or (not specific) (string= specific "FILE"))
  13373. (push (cons "FILE" buffer-file-name) props))
  13374. (when (and (or (not specific) (string= specific "TAGS"))
  13375. (setq value (org-get-tags-string))
  13376. (string-match "\\S-" value))
  13377. (push (cons "TAGS" value) props))
  13378. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13379. (setq value (org-get-tags-at)))
  13380. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13381. ":"))
  13382. props))
  13383. (when (or (not specific) (string= specific "BLOCKED"))
  13384. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13385. (when (or (not specific)
  13386. (member specific
  13387. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13388. "TIMESTAMP" "TIMESTAMP_IA")))
  13389. (catch 'match
  13390. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13391. (setq key (if (match-end 1)
  13392. (substring (org-match-string-no-properties 1)
  13393. 0 -1))
  13394. string (if (equal key clockstr)
  13395. (org-trim
  13396. (buffer-substring-no-properties
  13397. (match-beginning 3) (goto-char
  13398. (point-at-eol))))
  13399. (substring (org-match-string-no-properties 3)
  13400. 1 -1)))
  13401. ;; Get the correct property name from the key. This is
  13402. ;; necessary if the user has configured time keywords.
  13403. (setq key1 (concat key ":"))
  13404. (cond
  13405. ((not key)
  13406. (setq key
  13407. (if (= (char-after (match-beginning 3)) ?\[)
  13408. "TIMESTAMP_IA" "TIMESTAMP")))
  13409. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13410. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13411. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13412. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13413. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13414. (progn
  13415. (push (cons key string) props)
  13416. ;; no need to search further if match is found
  13417. (throw 'match t))
  13418. (when (or (equal key "CLOCK") (not (assoc key props)))
  13419. (push (cons key string) props)))))))
  13420. (when (memq which '(all standard))
  13421. ;; Get the standard properties, like :PROP: ...
  13422. (setq range (org-get-property-block beg end))
  13423. (when range
  13424. (goto-char (car range))
  13425. (while (re-search-forward
  13426. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13427. (cdr range) t)
  13428. (setq key (org-match-string-no-properties 1)
  13429. value (org-trim (or (org-match-string-no-properties 2) "")))
  13430. (unless (member key excluded)
  13431. (push (cons key (or value "")) props)))))
  13432. (if clocksum
  13433. (push (cons "CLOCKSUM"
  13434. (org-columns-number-to-string (/ (float clocksum) 60.)
  13435. 'add_times))
  13436. props))
  13437. (if clocksumt
  13438. (push (cons "CLOCKSUM_T"
  13439. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13440. 'add_times))
  13441. props))
  13442. (unless (assoc "CATEGORY" props)
  13443. (push (cons "CATEGORY" (org-get-category)) props))
  13444. (append sum-props (nreverse props)))))))
  13445. (defun org-entry-get (pom property &optional inherit literal-nil)
  13446. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13447. If INHERIT is non-nil and the entry does not have the property,
  13448. then also check higher levels of the hierarchy.
  13449. If INHERIT is the symbol `selective', use inheritance only if the setting
  13450. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13451. If the property is present but empty, the return value is the empty string.
  13452. If the property is not present at all, nil is returned.
  13453. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13454. do not interpret it as the list atom nil. This is used for inheritance
  13455. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13456. (org-with-point-at pom
  13457. (if (and inherit (if (eq inherit 'selective)
  13458. (org-property-inherit-p property)
  13459. t))
  13460. (org-entry-get-with-inheritance property literal-nil)
  13461. (if (member property org-special-properties)
  13462. ;; We need a special property. Use `org-entry-properties' to
  13463. ;; retrieve it, but specify the wanted property
  13464. (cdr (assoc property (org-entry-properties nil 'special property)))
  13465. (let ((range (org-get-property-block)))
  13466. (when (and range (not (eq (car range) (cdr range))))
  13467. (let* ((props (list (or (assoc property org-file-properties)
  13468. (assoc property org-global-properties)
  13469. (assoc property org-global-properties-fixed))))
  13470. (ap (lambda (key)
  13471. (when (re-search-forward
  13472. (org-re-property key) (cdr range) t)
  13473. (setq props
  13474. (org-update-property-plist
  13475. key
  13476. (if (match-end 1)
  13477. (org-match-string-no-properties 1) "")
  13478. props)))))
  13479. val)
  13480. (goto-char (car range))
  13481. (funcall ap property)
  13482. (goto-char (car range))
  13483. (while (funcall ap (concat property "+")))
  13484. (setq val (cdr (assoc property props)))
  13485. (when val (if literal-nil val (org-not-nil val))))))))))
  13486. (defun org-property-or-variable-value (var &optional inherit)
  13487. "Check if there is a property fixing the value of VAR.
  13488. If yes, return this value. If not, return the current value of the variable."
  13489. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13490. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13491. (read prop)
  13492. (symbol-value var))))
  13493. (defun org-entry-delete (pom property)
  13494. "Delete the property PROPERTY from entry at point-or-marker POM."
  13495. (org-with-point-at pom
  13496. (if (member property org-special-properties)
  13497. nil ; cannot delete these properties.
  13498. (let ((range (org-get-property-block)))
  13499. (if (and range
  13500. (goto-char (car range))
  13501. (re-search-forward
  13502. (org-re-property property)
  13503. (cdr range) t))
  13504. (progn
  13505. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13506. t)
  13507. nil)))))
  13508. ;; Multi-values properties are properties that contain multiple values
  13509. ;; These values are assumed to be single words, separated by whitespace.
  13510. (defun org-entry-add-to-multivalued-property (pom property value)
  13511. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13512. (let* ((old (org-entry-get pom property))
  13513. (values (and old (org-split-string old "[ \t]"))))
  13514. (setq value (org-entry-protect-space value))
  13515. (unless (member value values)
  13516. (setq values (cons value values))
  13517. (org-entry-put pom property
  13518. (mapconcat 'identity values " ")))))
  13519. (defun org-entry-remove-from-multivalued-property (pom property value)
  13520. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13521. (let* ((old (org-entry-get pom property))
  13522. (values (and old (org-split-string old "[ \t]"))))
  13523. (setq value (org-entry-protect-space value))
  13524. (when (member value values)
  13525. (setq values (delete value values))
  13526. (org-entry-put pom property
  13527. (mapconcat 'identity values " ")))))
  13528. (defun org-entry-member-in-multivalued-property (pom property value)
  13529. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13530. (let* ((old (org-entry-get pom property))
  13531. (values (and old (org-split-string old "[ \t]"))))
  13532. (setq value (org-entry-protect-space value))
  13533. (member value values)))
  13534. (defun org-entry-get-multivalued-property (pom property)
  13535. "Return a list of values in a multivalued property."
  13536. (let* ((value (org-entry-get pom property))
  13537. (values (and value (org-split-string value "[ \t]"))))
  13538. (mapcar 'org-entry-restore-space values)))
  13539. (defun org-entry-put-multivalued-property (pom property &rest values)
  13540. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13541. VALUES should be a list of strings. Spaces will be protected."
  13542. (org-entry-put pom property
  13543. (mapconcat 'org-entry-protect-space values " "))
  13544. (let* ((value (org-entry-get pom property))
  13545. (values (and value (org-split-string value "[ \t]"))))
  13546. (mapcar 'org-entry-restore-space values)))
  13547. (defun org-entry-protect-space (s)
  13548. "Protect spaces and newline in string S."
  13549. (while (string-match " " s)
  13550. (setq s (replace-match "%20" t t s)))
  13551. (while (string-match "\n" s)
  13552. (setq s (replace-match "%0A" t t s)))
  13553. s)
  13554. (defun org-entry-restore-space (s)
  13555. "Restore spaces and newline in string S."
  13556. (while (string-match "%20" s)
  13557. (setq s (replace-match " " t t s)))
  13558. (while (string-match "%0A" s)
  13559. (setq s (replace-match "\n" t t s)))
  13560. s)
  13561. (defvar org-entry-property-inherited-from (make-marker)
  13562. "Marker pointing to the entry from where a property was inherited.
  13563. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13564. location of the entry where the inheritance search matched. If there was
  13565. no match, the marker will point nowhere.
  13566. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13567. is set.")
  13568. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13569. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13570. The search will stop at the first ancestor which has the property defined.
  13571. If the value found is \"nil\", return nil to show that the property
  13572. should be considered as undefined (this is the meaning of nil here).
  13573. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13574. (move-marker org-entry-property-inherited-from nil)
  13575. (let (tmp)
  13576. (save-excursion
  13577. (save-restriction
  13578. (widen)
  13579. (catch 'ex
  13580. (while t
  13581. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13582. (or (ignore-errors (org-back-to-heading t))
  13583. (goto-char (point-min)))
  13584. (move-marker org-entry-property-inherited-from (point))
  13585. (throw 'ex tmp))
  13586. (or (ignore-errors (org-up-heading-safe))
  13587. (throw 'ex nil))))))
  13588. (setq tmp (or tmp
  13589. (cdr (assoc property org-file-properties))
  13590. (cdr (assoc property org-global-properties))
  13591. (cdr (assoc property org-global-properties-fixed))))
  13592. (if literal-nil tmp (org-not-nil tmp))))
  13593. (defvar org-property-changed-functions nil
  13594. "Hook called when the value of a property has changed.
  13595. Each hook function should accept two arguments, the name of the property
  13596. and the new value.")
  13597. (defun org-entry-put (pom property value)
  13598. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13599. (org-with-point-at pom
  13600. (org-back-to-heading t)
  13601. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13602. range)
  13603. (cond
  13604. ((equal property "TODO")
  13605. (when (and (stringp value) (string-match "\\S-" value)
  13606. (not (member value org-todo-keywords-1)))
  13607. (error "\"%s\" is not a valid TODO state" value))
  13608. (if (or (not value)
  13609. (not (string-match "\\S-" value)))
  13610. (setq value 'none))
  13611. (org-todo value)
  13612. (org-set-tags nil 'align))
  13613. ((equal property "PRIORITY")
  13614. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13615. (string-to-char value) ?\ ))
  13616. (org-set-tags nil 'align))
  13617. ((equal property "CLOCKSUM")
  13618. (if (not (re-search-forward
  13619. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13620. (error "Cannot find a clock log")
  13621. (goto-char (- (match-end 1) 2))
  13622. (cond
  13623. ((eq value 'earlier) (org-timestamp-down))
  13624. ((eq value 'later) (org-timestamp-up)))
  13625. (org-clock-sum-current-item)))
  13626. ((equal property "SCHEDULED")
  13627. (if (re-search-forward org-scheduled-time-regexp end t)
  13628. (cond
  13629. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13630. ((eq value 'later) (org-timestamp-change 1 'day))
  13631. (t (call-interactively 'org-schedule)))
  13632. (call-interactively 'org-schedule)))
  13633. ((equal property "DEADLINE")
  13634. (if (re-search-forward org-deadline-time-regexp end t)
  13635. (cond
  13636. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13637. ((eq value 'later) (org-timestamp-change 1 'day))
  13638. (t (call-interactively 'org-deadline)))
  13639. (call-interactively 'org-deadline)))
  13640. ((member property org-special-properties)
  13641. (error "The %s property can not yet be set with `org-entry-put'"
  13642. property))
  13643. (t ; a non-special property
  13644. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13645. (setq range (org-get-property-block beg end 'force))
  13646. (goto-char (car range))
  13647. (if (re-search-forward
  13648. (org-re-property-keyword property) (cdr range) t)
  13649. (progn
  13650. (delete-region (match-beginning 0) (match-end 0))
  13651. (goto-char (match-beginning 0)))
  13652. (goto-char (cdr range))
  13653. (insert "\n")
  13654. (backward-char 1)
  13655. (org-indent-line))
  13656. (insert ":" property ":")
  13657. (and value (insert " " value))
  13658. (org-indent-line)))))
  13659. (run-hook-with-args 'org-property-changed-functions property value)))
  13660. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13661. "Get all property keys in the current buffer.
  13662. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13663. like tags and TODO state.
  13664. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13665. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13666. and others.
  13667. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13668. formats in the current buffer."
  13669. (let (rtn range cfmt s p)
  13670. (save-excursion
  13671. (save-restriction
  13672. (widen)
  13673. (goto-char (point-min))
  13674. (while (re-search-forward org-property-start-re nil t)
  13675. (setq range (org-get-property-block))
  13676. (goto-char (car range))
  13677. (while (re-search-forward
  13678. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13679. (cdr range) t)
  13680. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13681. (outline-next-heading))))
  13682. (when include-specials
  13683. (setq rtn (append org-special-properties rtn)))
  13684. (when include-defaults
  13685. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13686. (add-to-list 'rtn org-effort-property))
  13687. (when include-columns
  13688. (save-excursion
  13689. (save-restriction
  13690. (widen)
  13691. (goto-char (point-min))
  13692. (while (re-search-forward
  13693. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13694. nil t)
  13695. (setq cfmt (match-string 2) s 0)
  13696. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13697. cfmt s)
  13698. (setq s (match-end 0)
  13699. p (match-string 1 cfmt))
  13700. (unless (or (equal p "ITEM")
  13701. (member p org-special-properties))
  13702. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13703. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13704. (defun org-property-values (key)
  13705. "Return a list of all values of property KEY in the current buffer."
  13706. (save-excursion
  13707. (save-restriction
  13708. (widen)
  13709. (goto-char (point-min))
  13710. (let ((re (org-re-property key))
  13711. values)
  13712. (while (re-search-forward re nil t)
  13713. (add-to-list 'values (org-trim (match-string 1))))
  13714. (delete "" values)))))
  13715. (defun org-insert-property-drawer ()
  13716. "Insert a property drawer into the current entry."
  13717. (org-back-to-heading t)
  13718. (looking-at org-outline-regexp)
  13719. (let ((indent (if org-adapt-indentation
  13720. (- (match-end 0) (match-beginning 0))
  13721. 0))
  13722. (beg (point))
  13723. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13724. end hiddenp)
  13725. (outline-next-heading)
  13726. (setq end (point))
  13727. (goto-char beg)
  13728. (while (re-search-forward re end t))
  13729. (setq hiddenp (outline-invisible-p))
  13730. (end-of-line 1)
  13731. (and (equal (char-after) ?\n) (forward-char 1))
  13732. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13733. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13734. ;; just skip this line
  13735. (beginning-of-line 2)
  13736. ;; Drawer start, find the end
  13737. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13738. (beginning-of-line 1)))
  13739. (org-skip-over-state-notes)
  13740. (skip-chars-backward " \t\n\r")
  13741. (if (eq (char-before) ?*) (forward-char 1))
  13742. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13743. (beginning-of-line 0)
  13744. (org-indent-to-column indent)
  13745. (beginning-of-line 2)
  13746. (org-indent-to-column indent)
  13747. (beginning-of-line 0)
  13748. (if hiddenp
  13749. (save-excursion
  13750. (org-back-to-heading t)
  13751. (hide-entry))
  13752. (org-flag-drawer t))))
  13753. (defun org-insert-drawer (&optional arg drawer)
  13754. "Insert a drawer at point.
  13755. Optional argument DRAWER, when non-nil, is a string representing
  13756. drawer's name. Otherwise, the user is prompted for a name.
  13757. If a region is active, insert the drawer around that region
  13758. instead.
  13759. Point is left between drawer's boundaries."
  13760. (interactive "P")
  13761. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13762. "LOGBOOK"))
  13763. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13764. ;; internally by Org. They are meant to be inserted
  13765. ;; automatically.
  13766. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13767. ;; Remove system drawers from list. Note: For some reason,
  13768. ;; `org-completing-read' ignores the predicate while
  13769. ;; `completing-read' handles it fine.
  13770. (drawer (if arg "PROPERTIES"
  13771. (or drawer
  13772. (completing-read
  13773. "Drawer: " org-drawers
  13774. (lambda (d) (not (member d system-drawers))))))))
  13775. (cond
  13776. ;; With C-u, fall back on `org-insert-property-drawer'
  13777. (arg (org-insert-property-drawer))
  13778. ;; With an active region, insert a drawer at point.
  13779. ((not (org-region-active-p))
  13780. (progn
  13781. (unless (bolp) (insert "\n"))
  13782. (insert (format ":%s:\n\n:END:\n" drawer))
  13783. (forward-line -2)))
  13784. ;; Otherwise, insert the drawer at point
  13785. (t
  13786. (let ((rbeg (region-beginning))
  13787. (rend (copy-marker (region-end))))
  13788. (unwind-protect
  13789. (progn
  13790. (goto-char rbeg)
  13791. (beginning-of-line)
  13792. (when (save-excursion
  13793. (re-search-forward org-outline-regexp-bol rend t))
  13794. (error "Drawers cannot contain headlines"))
  13795. ;; Position point at the beginning of the first
  13796. ;; non-blank line in region. Insert drawer's opening
  13797. ;; there, then indent it.
  13798. (org-skip-whitespace)
  13799. (beginning-of-line)
  13800. (insert ":" drawer ":\n")
  13801. (forward-line -1)
  13802. (indent-for-tab-command)
  13803. ;; Move point to the beginning of the first blank line
  13804. ;; after the last non-blank line in region. Insert
  13805. ;; drawer's closing, then indent it.
  13806. (goto-char rend)
  13807. (skip-chars-backward " \r\t\n")
  13808. (insert "\n:END:")
  13809. (deactivate-mark t)
  13810. (indent-for-tab-command)
  13811. (unless (eolp) (insert "\n")))
  13812. ;; Clear marker, whatever the outcome of insertion is.
  13813. (set-marker rend nil)))))))
  13814. (defvar org-property-set-functions-alist nil
  13815. "Property set function alist.
  13816. Each entry should have the following format:
  13817. (PROPERTY . READ-FUNCTION)
  13818. The read function will be called with the same argument as
  13819. `org-completing-read'.")
  13820. (defun org-set-property-function (property)
  13821. "Get the function that should be used to set PROPERTY.
  13822. This is computed according to `org-property-set-functions-alist'."
  13823. (or (cdr (assoc property org-property-set-functions-alist))
  13824. 'org-completing-read))
  13825. (defun org-read-property-value (property)
  13826. "Read PROPERTY value from user."
  13827. (let* ((completion-ignore-case t)
  13828. (allowed (org-property-get-allowed-values nil property 'table))
  13829. (cur (org-entry-get nil property))
  13830. (prompt (concat property " value"
  13831. (if (and cur (string-match "\\S-" cur))
  13832. (concat " [" cur "]") "") ": "))
  13833. (set-function (org-set-property-function property))
  13834. (val (if allowed
  13835. (funcall set-function prompt allowed nil
  13836. (not (get-text-property 0 'org-unrestricted
  13837. (caar allowed))))
  13838. (let (org-completion-use-ido org-completion-use-iswitchb)
  13839. (funcall set-function prompt
  13840. (mapcar 'list (org-property-values property))
  13841. nil nil "" nil cur)))))
  13842. (if (equal val "")
  13843. cur
  13844. val)))
  13845. (defvar org-last-set-property nil)
  13846. (defvar org-last-set-property-value nil)
  13847. (defun org-read-property-name ()
  13848. "Read a property name."
  13849. (let* ((completion-ignore-case t)
  13850. (keys (org-buffer-property-keys nil t t))
  13851. (default-prop (or (save-excursion
  13852. (save-match-data
  13853. (beginning-of-line)
  13854. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13855. (null (string= (match-string 1) "END"))
  13856. (match-string 1))))
  13857. org-last-set-property))
  13858. (property (org-icompleting-read
  13859. (concat "Property"
  13860. (if default-prop (concat " [" default-prop "]") "")
  13861. ": ")
  13862. (mapcar 'list keys)
  13863. nil nil nil nil
  13864. default-prop)))
  13865. (if (member property keys)
  13866. property
  13867. (or (cdr (assoc (downcase property)
  13868. (mapcar (lambda (x) (cons (downcase x) x))
  13869. keys)))
  13870. property))))
  13871. (defun org-set-property-and-value (use-last)
  13872. "Allow to set [PROPERTY]: [value] direction from prompt.
  13873. When use-default, don't even ask, just use the last
  13874. \"[PROPERTY]: [value]\" string from the history."
  13875. (interactive "P")
  13876. (let* ((completion-ignore-case t)
  13877. (pv (or (and use-last org-last-set-property-value)
  13878. (org-completing-read
  13879. "Enter a \"[Property]: [value]\" pair: "
  13880. nil nil nil nil nil
  13881. org-last-set-property-value)))
  13882. prop val)
  13883. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13884. (setq prop (match-string 1 pv)
  13885. val (match-string 2 pv))
  13886. (org-set-property prop val))))
  13887. (defun org-set-property (property value)
  13888. "In the current entry, set PROPERTY to VALUE.
  13889. When called interactively, this will prompt for a property name, offering
  13890. completion on existing and default properties. And then it will prompt
  13891. for a value, offering completion either on allowed values (via an inherited
  13892. xxx_ALL property) or on existing values in other instances of this property
  13893. in the current file."
  13894. (interactive (list nil nil))
  13895. (let* ((property (or property (org-read-property-name)))
  13896. (value (or value (org-read-property-value property)))
  13897. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13898. (setq org-last-set-property property)
  13899. (setq org-last-set-property-value (concat property ": " value))
  13900. ;; Possibly postprocess the inserted value:
  13901. (when fn (setq value (funcall fn value)))
  13902. (unless (equal (org-entry-get nil property) value)
  13903. (org-entry-put nil property value))))
  13904. (defun org-delete-property (property)
  13905. "In the current entry, delete PROPERTY."
  13906. (interactive
  13907. (let* ((completion-ignore-case t)
  13908. (prop (org-icompleting-read "Property: "
  13909. (org-entry-properties nil 'standard))))
  13910. (list prop)))
  13911. (message "Property %s %s" property
  13912. (if (org-entry-delete nil property)
  13913. "deleted"
  13914. "was not present in the entry")))
  13915. (defun org-delete-property-globally (property)
  13916. "Remove PROPERTY globally, from all entries."
  13917. (interactive
  13918. (let* ((completion-ignore-case t)
  13919. (prop (org-icompleting-read
  13920. "Globally remove property: "
  13921. (mapcar 'list (org-buffer-property-keys)))))
  13922. (list prop)))
  13923. (save-excursion
  13924. (save-restriction
  13925. (widen)
  13926. (goto-char (point-min))
  13927. (let ((cnt 0))
  13928. (while (re-search-forward
  13929. (org-re-property property)
  13930. nil t)
  13931. (setq cnt (1+ cnt))
  13932. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13933. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13934. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13935. (defun org-compute-property-at-point ()
  13936. "Compute the property at point.
  13937. This looks for an enclosing column format, extracts the operator and
  13938. then applies it to the property in the column format's scope."
  13939. (interactive)
  13940. (unless (org-at-property-p)
  13941. (error "Not at a property"))
  13942. (let ((prop (org-match-string-no-properties 2)))
  13943. (org-columns-get-format-and-top-level)
  13944. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13945. (error "No operator defined for property %s" prop))
  13946. (org-columns-compute prop)))
  13947. (defvar org-property-allowed-value-functions nil
  13948. "Hook for functions supplying allowed values for a specific property.
  13949. The functions must take a single argument, the name of the property, and
  13950. return a flat list of allowed values. If \":ETC\" is one of
  13951. the values, this means that these values are intended as defaults for
  13952. completion, but that other values should be allowed too.
  13953. The functions must return nil if they are not responsible for this
  13954. property.")
  13955. (defun org-property-get-allowed-values (pom property &optional table)
  13956. "Get allowed values for the property PROPERTY.
  13957. When TABLE is non-nil, return an alist that can directly be used for
  13958. completion."
  13959. (let (vals)
  13960. (cond
  13961. ((equal property "TODO")
  13962. (setq vals (org-with-point-at pom
  13963. (append org-todo-keywords-1 '("")))))
  13964. ((equal property "PRIORITY")
  13965. (let ((n org-lowest-priority))
  13966. (while (>= n org-highest-priority)
  13967. (push (char-to-string n) vals)
  13968. (setq n (1- n)))))
  13969. ((member property org-special-properties))
  13970. ((setq vals (run-hook-with-args-until-success
  13971. 'org-property-allowed-value-functions property)))
  13972. (t
  13973. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13974. (when (and vals (string-match "\\S-" vals))
  13975. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13976. (setq vals (mapcar (lambda (x)
  13977. (cond ((stringp x) x)
  13978. ((numberp x) (number-to-string x))
  13979. ((symbolp x) (symbol-name x))
  13980. (t "???")))
  13981. vals)))))
  13982. (when (member ":ETC" vals)
  13983. (setq vals (remove ":ETC" vals))
  13984. (org-add-props (car vals) '(org-unrestricted t)))
  13985. (if table (mapcar 'list vals) vals)))
  13986. (defun org-property-previous-allowed-value (&optional previous)
  13987. "Switch to the next allowed value for this property."
  13988. (interactive)
  13989. (org-property-next-allowed-value t))
  13990. (defun org-property-next-allowed-value (&optional previous)
  13991. "Switch to the next allowed value for this property."
  13992. (interactive)
  13993. (unless (org-at-property-p)
  13994. (error "Not at a property"))
  13995. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  13996. (key (match-string 2))
  13997. (value (match-string 3))
  13998. (allowed (or (org-property-get-allowed-values (point) key)
  13999. (and (member value '("[ ]" "[-]" "[X]"))
  14000. '("[ ]" "[X]"))))
  14001. nval)
  14002. (unless allowed
  14003. (error "Allowed values for this property have not been defined"))
  14004. (if previous (setq allowed (reverse allowed)))
  14005. (if (member value allowed)
  14006. (setq nval (car (cdr (member value allowed)))))
  14007. (setq nval (or nval (car allowed)))
  14008. (if (equal nval value)
  14009. (error "Only one allowed value for this property"))
  14010. (org-at-property-p)
  14011. (replace-match (concat " :" key ": " nval) t t)
  14012. (org-indent-line)
  14013. (beginning-of-line 1)
  14014. (skip-chars-forward " \t")
  14015. (when (equal prop org-effort-property)
  14016. (save-excursion
  14017. (org-back-to-heading t)
  14018. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  14019. (run-hook-with-args 'org-property-changed-functions key nval)))
  14020. (defun org-find-olp (path &optional this-buffer)
  14021. "Return a marker pointing to the entry at outline path OLP.
  14022. If anything goes wrong, throw an error.
  14023. You can wrap this call to catch the error like this:
  14024. (condition-case msg
  14025. (org-mobile-locate-entry (match-string 4))
  14026. (error (nth 1 msg)))
  14027. The return value will then be either a string with the error message,
  14028. or a marker if everything is OK.
  14029. If THIS-BUFFER is set, the outline path does not contain a file,
  14030. only headings."
  14031. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14032. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14033. (level 1)
  14034. (lmin 1)
  14035. (lmax 1)
  14036. limit re end found pos heading cnt flevel)
  14037. (unless buffer (error "File not found :%s" file))
  14038. (with-current-buffer buffer
  14039. (save-excursion
  14040. (save-restriction
  14041. (widen)
  14042. (setq limit (point-max))
  14043. (goto-char (point-min))
  14044. (while (setq heading (pop path))
  14045. (setq re (format org-complex-heading-regexp-format
  14046. (regexp-quote heading)))
  14047. (setq cnt 0 pos (point))
  14048. (while (re-search-forward re end t)
  14049. (setq level (- (match-end 1) (match-beginning 1)))
  14050. (if (and (>= level lmin) (<= level lmax))
  14051. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14052. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14053. lmax heading))
  14054. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14055. lmax heading))
  14056. (goto-char found)
  14057. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14058. (setq end (save-excursion (org-end-of-subtree t t))))
  14059. (when (org-at-heading-p)
  14060. (point-marker)))))))
  14061. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14062. "Find node HEADING in BUFFER.
  14063. Return a marker to the heading if it was found, or nil if not.
  14064. If POS-ONLY is set, return just the position instead of a marker.
  14065. The heading text must match exact, but it may have a TODO keyword,
  14066. a priority cookie and tags in the standard locations."
  14067. (with-current-buffer (or buffer (current-buffer))
  14068. (save-excursion
  14069. (save-restriction
  14070. (widen)
  14071. (goto-char (point-min))
  14072. (let (case-fold-search)
  14073. (if (re-search-forward
  14074. (format org-complex-heading-regexp-format
  14075. (regexp-quote heading)) nil t)
  14076. (if pos-only
  14077. (match-beginning 0)
  14078. (move-marker (make-marker) (match-beginning 0)))))))))
  14079. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14080. "Find Org node headline HEADING in all .org files in directory DIR.
  14081. When the target headline is found, return a marker to this location."
  14082. (let ((files (directory-files (or dir default-directory)
  14083. nil "\\`[^.#].*\\.org\\'"))
  14084. file visiting m buffer)
  14085. (catch 'found
  14086. (while (setq file (pop files))
  14087. (message "trying %s" file)
  14088. (setq visiting (org-find-base-buffer-visiting file))
  14089. (setq buffer (or visiting (find-file-noselect file)))
  14090. (setq m (org-find-exact-headline-in-buffer
  14091. heading buffer))
  14092. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14093. (and m (throw 'found m))))))
  14094. (defun org-find-entry-with-id (ident)
  14095. "Locate the entry that contains the ID property with exact value IDENT.
  14096. IDENT can be a string, a symbol or a number, this function will search for
  14097. the string representation of it.
  14098. Return the position where this entry starts, or nil if there is no such entry."
  14099. (interactive "sID: ")
  14100. (let ((id (cond
  14101. ((stringp ident) ident)
  14102. ((symbol-name ident) (symbol-name ident))
  14103. ((numberp ident) (number-to-string ident))
  14104. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14105. (case-fold-search nil))
  14106. (save-excursion
  14107. (save-restriction
  14108. (widen)
  14109. (goto-char (point-min))
  14110. (when (re-search-forward
  14111. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14112. nil t)
  14113. (org-back-to-heading t)
  14114. (point))))))
  14115. ;;;; Timestamps
  14116. (defvar org-last-changed-timestamp nil)
  14117. (defvar org-last-inserted-timestamp nil
  14118. "The last time stamp inserted with `org-insert-time-stamp'.")
  14119. (defvar org-time-was-given) ; dynamically scoped parameter
  14120. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14121. (defvar org-ts-what) ; dynamically scoped parameter
  14122. (defun org-time-stamp (arg &optional inactive)
  14123. "Prompt for a date/time and insert a time stamp.
  14124. If the user specifies a time like HH:MM or if this command is
  14125. called with at least one prefix argument, the time stamp contains
  14126. the date and the time. Otherwise, only the date is be included.
  14127. All parts of a date not specified by the user is filled in from
  14128. the current date/time. So if you just press return without
  14129. typing anything, the time stamp will represent the current
  14130. date/time.
  14131. If there is already a timestamp at the cursor, it will be
  14132. modified.
  14133. With two universal prefix arguments, insert an active timestamp
  14134. with the current time without prompting the user."
  14135. (interactive "P")
  14136. (let* ((ts nil)
  14137. (default-time
  14138. ;; Default time is either today, or, when entering a range,
  14139. ;; the range start.
  14140. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14141. (save-excursion
  14142. (re-search-backward
  14143. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14144. (- (point) 20) t)))
  14145. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14146. (current-time)))
  14147. (default-input (and ts (org-get-compact-tod ts)))
  14148. (repeater (save-excursion
  14149. (save-match-data
  14150. (beginning-of-line)
  14151. (when (re-search-forward
  14152. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14153. (save-excursion (progn (end-of-line) (point))) t)
  14154. (match-string 0)))))
  14155. org-time-was-given org-end-time-was-given time)
  14156. (cond
  14157. ((and (org-at-timestamp-p t)
  14158. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14159. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14160. (insert "--")
  14161. (setq time (let ((this-command this-command))
  14162. (org-read-date arg 'totime nil nil
  14163. default-time default-input inactive)))
  14164. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14165. ((org-at-timestamp-p t)
  14166. (setq time (let ((this-command this-command))
  14167. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14168. (when (org-at-timestamp-p t) ; just to get the match data
  14169. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14170. (replace-match "")
  14171. (setq org-last-changed-timestamp
  14172. (org-insert-time-stamp
  14173. time (or org-time-was-given arg)
  14174. inactive nil nil (list org-end-time-was-given)))
  14175. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14176. (setq org-last-changed-timestamp
  14177. (concat (substring org-last-inserted-timestamp 0 -1)
  14178. " " repeater ">"))))
  14179. (message "Timestamp updated"))
  14180. ((equal arg '(16))
  14181. (org-insert-time-stamp (current-time) t))
  14182. (t
  14183. (setq time (let ((this-command this-command))
  14184. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14185. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14186. nil nil (list org-end-time-was-given))))))
  14187. ;; FIXME: can we use this for something else, like computing time differences?
  14188. (defun org-get-compact-tod (s)
  14189. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14190. (let* ((t1 (match-string 1 s))
  14191. (h1 (string-to-number (match-string 2 s)))
  14192. (m1 (string-to-number (match-string 3 s)))
  14193. (t2 (and (match-end 4) (match-string 5 s)))
  14194. (h2 (and t2 (string-to-number (match-string 6 s))))
  14195. (m2 (and t2 (string-to-number (match-string 7 s))))
  14196. dh dm)
  14197. (if (not t2)
  14198. t1
  14199. (setq dh (- h2 h1) dm (- m2 m1))
  14200. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14201. (concat t1 "+" (number-to-string dh)
  14202. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14203. (defun org-time-stamp-inactive (&optional arg)
  14204. "Insert an inactive time stamp.
  14205. An inactive time stamp is enclosed in square brackets instead of angle
  14206. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14207. does not link to the calendar and cannot be changed with the S-cursor keys.
  14208. So these are more for recording a certain time/date."
  14209. (interactive "P")
  14210. (org-time-stamp arg 'inactive))
  14211. (defvar org-date-ovl (make-overlay 1 1))
  14212. (overlay-put org-date-ovl 'face 'org-date-selected)
  14213. (org-detach-overlay org-date-ovl)
  14214. (defvar org-ans1) ; dynamically scoped parameter
  14215. (defvar org-ans2) ; dynamically scoped parameter
  14216. (defvar org-plain-time-of-day-regexp) ; defined below
  14217. (defvar org-overriding-default-time nil) ; dynamically scoped
  14218. (defvar org-read-date-overlay nil)
  14219. (defvar org-dcst nil) ; dynamically scoped
  14220. (defvar org-read-date-history nil)
  14221. (defvar org-read-date-final-answer nil)
  14222. (defvar org-read-date-analyze-futurep nil)
  14223. (defvar org-read-date-analyze-forced-year nil)
  14224. (defvar org-read-date-inactive)
  14225. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14226. default-time default-input inactive)
  14227. "Read a date, possibly a time, and make things smooth for the user.
  14228. The prompt will suggest to enter an ISO date, but you can also enter anything
  14229. which will at least partially be understood by `parse-time-string'.
  14230. Unrecognized parts of the date will default to the current day, month, year,
  14231. hour and minute. If this command is called to replace a timestamp at point,
  14232. or to enter the second timestamp of a range, the default time is taken
  14233. from the existing stamp. Furthermore, the command prefers the future,
  14234. so if you are giving a date where the year is not given, and the day-month
  14235. combination is already past in the current year, it will assume you
  14236. mean next year. For details, see the manual. A few examples:
  14237. 3-2-5 --> 2003-02-05
  14238. feb 15 --> currentyear-02-15
  14239. 2/15 --> currentyear-02-15
  14240. sep 12 9 --> 2009-09-12
  14241. 12:45 --> today 12:45
  14242. 22 sept 0:34 --> currentyear-09-22 0:34
  14243. 12 --> currentyear-currentmonth-12
  14244. Fri --> nearest Friday (today or later)
  14245. etc.
  14246. Furthermore you can specify a relative date by giving, as the *first* thing
  14247. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14248. change in days weeks, months, years.
  14249. With a single plus or minus, the date is relative to today. With a double
  14250. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14251. +4d --> four days from today
  14252. +4 --> same as above
  14253. +2w --> two weeks from today
  14254. ++5 --> five days from default date
  14255. The function understands only English month and weekday abbreviations.
  14256. While prompting, a calendar is popped up - you can also select the
  14257. date with the mouse (button 1). The calendar shows a period of three
  14258. months. To scroll it to other months, use the keys `>' and `<'.
  14259. If you don't like the calendar, turn it off with
  14260. \(setq org-read-date-popup-calendar nil)
  14261. With optional argument TO-TIME, the date will immediately be converted
  14262. to an internal time.
  14263. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14264. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14265. still enter a time, and this function will inform the calling routine
  14266. about this change. The calling routine may then choose to change the
  14267. format used to insert the time stamp into the buffer to include the time.
  14268. With optional argument FROM-STRING, read from this string instead from
  14269. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14270. the time/date that is used for everything that is not specified by the
  14271. user."
  14272. (require 'parse-time)
  14273. (let* ((org-time-stamp-rounding-minutes
  14274. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14275. (org-dcst org-display-custom-times)
  14276. (ct (org-current-time))
  14277. (org-def (or org-overriding-default-time default-time ct))
  14278. (org-defdecode (decode-time org-def))
  14279. (dummy (progn
  14280. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14281. (setcar (nthcdr 2 org-defdecode) -1)
  14282. (setcar (nthcdr 1 org-defdecode) 59)
  14283. (setq org-def (apply 'encode-time org-defdecode)
  14284. org-defdecode (decode-time org-def)))))
  14285. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14286. (calendar-frame-setup nil)
  14287. (calendar-setup nil)
  14288. (calendar-move-hook nil)
  14289. (calendar-view-diary-initially-flag nil)
  14290. (calendar-view-holidays-initially-flag nil)
  14291. (timestr (format-time-string
  14292. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14293. (prompt (concat (if prompt (concat prompt " ") "")
  14294. (format "Date+time [%s]: " timestr)))
  14295. ans (org-ans0 "") org-ans1 org-ans2 final)
  14296. (cond
  14297. (from-string (setq ans from-string))
  14298. (org-read-date-popup-calendar
  14299. (save-excursion
  14300. (save-window-excursion
  14301. (calendar)
  14302. (org-eval-in-calendar '(setq cursor-type nil) t)
  14303. (unwind-protect
  14304. (progn
  14305. (calendar-forward-day (- (time-to-days org-def)
  14306. (calendar-absolute-from-gregorian
  14307. (calendar-current-date))))
  14308. (org-eval-in-calendar nil t)
  14309. (let* ((old-map (current-local-map))
  14310. (map (copy-keymap calendar-mode-map))
  14311. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14312. (org-defkey map (kbd "RET") 'org-calendar-select)
  14313. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14314. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14315. (org-defkey minibuffer-local-map [(meta shift left)]
  14316. (lambda () (interactive)
  14317. (org-eval-in-calendar '(calendar-backward-month 1))))
  14318. (org-defkey minibuffer-local-map [(meta shift right)]
  14319. (lambda () (interactive)
  14320. (org-eval-in-calendar '(calendar-forward-month 1))))
  14321. (org-defkey minibuffer-local-map [(meta shift up)]
  14322. (lambda () (interactive)
  14323. (org-eval-in-calendar '(calendar-backward-year 1))))
  14324. (org-defkey minibuffer-local-map [(meta shift down)]
  14325. (lambda () (interactive)
  14326. (org-eval-in-calendar '(calendar-forward-year 1))))
  14327. (org-defkey minibuffer-local-map [?\e (shift left)]
  14328. (lambda () (interactive)
  14329. (org-eval-in-calendar '(calendar-backward-month 1))))
  14330. (org-defkey minibuffer-local-map [?\e (shift right)]
  14331. (lambda () (interactive)
  14332. (org-eval-in-calendar '(calendar-forward-month 1))))
  14333. (org-defkey minibuffer-local-map [?\e (shift up)]
  14334. (lambda () (interactive)
  14335. (org-eval-in-calendar '(calendar-backward-year 1))))
  14336. (org-defkey minibuffer-local-map [?\e (shift down)]
  14337. (lambda () (interactive)
  14338. (org-eval-in-calendar '(calendar-forward-year 1))))
  14339. (org-defkey minibuffer-local-map [(shift up)]
  14340. (lambda () (interactive)
  14341. (org-eval-in-calendar '(calendar-backward-week 1))))
  14342. (org-defkey minibuffer-local-map [(shift down)]
  14343. (lambda () (interactive)
  14344. (org-eval-in-calendar '(calendar-forward-week 1))))
  14345. (org-defkey minibuffer-local-map [(shift left)]
  14346. (lambda () (interactive)
  14347. (org-eval-in-calendar '(calendar-backward-day 1))))
  14348. (org-defkey minibuffer-local-map [(shift right)]
  14349. (lambda () (interactive)
  14350. (org-eval-in-calendar '(calendar-forward-day 1))))
  14351. (org-defkey minibuffer-local-map ">"
  14352. (lambda () (interactive)
  14353. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14354. (org-defkey minibuffer-local-map "<"
  14355. (lambda () (interactive)
  14356. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14357. (org-defkey minibuffer-local-map "\C-v"
  14358. (lambda () (interactive)
  14359. (org-eval-in-calendar
  14360. '(calendar-scroll-left-three-months 1))))
  14361. (org-defkey minibuffer-local-map "\M-v"
  14362. (lambda () (interactive)
  14363. (org-eval-in-calendar
  14364. '(calendar-scroll-right-three-months 1))))
  14365. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14366. (unwind-protect
  14367. (progn
  14368. (use-local-map map)
  14369. (setq org-read-date-inactive inactive)
  14370. (add-hook 'post-command-hook 'org-read-date-display)
  14371. (setq org-ans0 (read-string prompt default-input
  14372. 'org-read-date-history nil))
  14373. ;; org-ans0: from prompt
  14374. ;; org-ans1: from mouse click
  14375. ;; org-ans2: from calendar motion
  14376. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14377. (remove-hook 'post-command-hook 'org-read-date-display)
  14378. (use-local-map old-map)
  14379. (when org-read-date-overlay
  14380. (delete-overlay org-read-date-overlay)
  14381. (setq org-read-date-overlay nil)))))
  14382. (bury-buffer "*Calendar*")))))
  14383. (t ; Naked prompt only
  14384. (unwind-protect
  14385. (setq ans (read-string prompt default-input
  14386. 'org-read-date-history timestr))
  14387. (when org-read-date-overlay
  14388. (delete-overlay org-read-date-overlay)
  14389. (setq org-read-date-overlay nil)))))
  14390. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14391. (when org-read-date-analyze-forced-year
  14392. (message "Year was forced into %s"
  14393. (if org-read-date-force-compatible-dates
  14394. "compatible range (1970-2037)"
  14395. "range representable on this machine"))
  14396. (ding))
  14397. ;; One round trip to get rid of 34th of August and stuff like that....
  14398. (setq final (decode-time (apply 'encode-time final)))
  14399. (setq org-read-date-final-answer ans)
  14400. (if to-time
  14401. (apply 'encode-time final)
  14402. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14403. (format "%04d-%02d-%02d %02d:%02d"
  14404. (nth 5 final) (nth 4 final) (nth 3 final)
  14405. (nth 2 final) (nth 1 final))
  14406. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14407. (defvar org-def)
  14408. (defvar org-defdecode)
  14409. (defvar org-with-time)
  14410. (defun org-read-date-display ()
  14411. "Display the current date prompt interpretation in the minibuffer."
  14412. (when org-read-date-display-live
  14413. (when org-read-date-overlay
  14414. (delete-overlay org-read-date-overlay))
  14415. (when (minibufferp (current-buffer))
  14416. (save-excursion
  14417. (end-of-line 1)
  14418. (while (not (equal (buffer-substring
  14419. (max (point-min) (- (point) 4)) (point))
  14420. " "))
  14421. (insert " ")))
  14422. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14423. " " (or org-ans1 org-ans2)))
  14424. (org-end-time-was-given nil)
  14425. (f (org-read-date-analyze ans org-def org-defdecode))
  14426. (fmts (if org-dcst
  14427. org-time-stamp-custom-formats
  14428. org-time-stamp-formats))
  14429. (fmt (if (or org-with-time
  14430. (and (boundp 'org-time-was-given) org-time-was-given))
  14431. (cdr fmts)
  14432. (car fmts)))
  14433. (txt (format-time-string fmt (apply 'encode-time f)))
  14434. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14435. (txt (concat "=> " txt)))
  14436. (when (and org-end-time-was-given
  14437. (string-match org-plain-time-of-day-regexp txt))
  14438. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14439. org-end-time-was-given
  14440. (substring txt (match-end 0)))))
  14441. (when org-read-date-analyze-futurep
  14442. (setq txt (concat txt " (=>F)")))
  14443. (setq org-read-date-overlay
  14444. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14445. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14446. (defun org-read-date-analyze (ans org-def org-defdecode)
  14447. "Analyze the combined answer of the date prompt."
  14448. ;; FIXME: cleanup and comment
  14449. (let ((nowdecode (decode-time (current-time)))
  14450. delta deltan deltaw deltadef year month day
  14451. hour minute second wday pm h2 m2 tl wday1
  14452. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14453. (setq org-read-date-analyze-futurep nil
  14454. org-read-date-analyze-forced-year nil)
  14455. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14456. (setq ans "+0"))
  14457. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14458. (setq ans (replace-match "" t t ans)
  14459. deltan (car delta)
  14460. deltaw (nth 1 delta)
  14461. deltadef (nth 2 delta)))
  14462. ;; Check if there is an iso week date in there. If yes, store the
  14463. ;; info and postpone interpreting it until the rest of the parsing
  14464. ;; is done.
  14465. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14466. (setq iso-year (if (match-end 1)
  14467. (org-small-year-to-year
  14468. (string-to-number (match-string 1 ans))))
  14469. iso-weekday (if (match-end 3)
  14470. (string-to-number (match-string 3 ans)))
  14471. iso-week (string-to-number (match-string 2 ans)))
  14472. (setq ans (replace-match "" t t ans)))
  14473. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14474. (when (string-match
  14475. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14476. (setq year (if (match-end 2)
  14477. (string-to-number (match-string 2 ans))
  14478. (progn (setq kill-year t)
  14479. (string-to-number (format-time-string "%Y"))))
  14480. month (string-to-number (match-string 3 ans))
  14481. day (string-to-number (match-string 4 ans)))
  14482. (if (< year 100) (setq year (+ 2000 year)))
  14483. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14484. t nil ans)))
  14485. ;; Help matching dotted european dates
  14486. (when (string-match
  14487. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14488. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14489. (setq kill-year t)
  14490. (string-to-number (format-time-string "%Y")))
  14491. day (string-to-number (match-string 1 ans))
  14492. month (string-to-number (match-string 2 ans))
  14493. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14494. t nil ans)))
  14495. ;; Help matching american dates, like 5/30 or 5/30/7
  14496. (when (string-match
  14497. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14498. (setq year (if (match-end 4)
  14499. (string-to-number (match-string 4 ans))
  14500. (progn (setq kill-year t)
  14501. (string-to-number (format-time-string "%Y"))))
  14502. month (string-to-number (match-string 1 ans))
  14503. day (string-to-number (match-string 2 ans)))
  14504. (if (< year 100) (setq year (+ 2000 year)))
  14505. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14506. t nil ans)))
  14507. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14508. ;; If there is a time with am/pm, and *no* time without it, we convert
  14509. ;; so that matching will be successful.
  14510. (loop for i from 1 to 2 do ; twice, for end time as well
  14511. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14512. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14513. (setq hour (string-to-number (match-string 1 ans))
  14514. minute (if (match-end 3)
  14515. (string-to-number (match-string 3 ans))
  14516. 0)
  14517. pm (equal ?p
  14518. (string-to-char (downcase (match-string 4 ans)))))
  14519. (if (and (= hour 12) (not pm))
  14520. (setq hour 0)
  14521. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14522. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14523. t t ans))))
  14524. ;; Check if a time range is given as a duration
  14525. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14526. (setq hour (string-to-number (match-string 1 ans))
  14527. h2 (+ hour (string-to-number (match-string 3 ans)))
  14528. minute (string-to-number (match-string 2 ans))
  14529. m2 (+ minute (if (match-end 5) (string-to-number
  14530. (match-string 5 ans))0)))
  14531. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14532. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14533. t t ans)))
  14534. ;; Check if there is a time range
  14535. (when (boundp 'org-end-time-was-given)
  14536. (setq org-time-was-given nil)
  14537. (when (and (string-match org-plain-time-of-day-regexp ans)
  14538. (match-end 8))
  14539. (setq org-end-time-was-given (match-string 8 ans))
  14540. (setq ans (concat (substring ans 0 (match-beginning 7))
  14541. (substring ans (match-end 7))))))
  14542. (setq tl (parse-time-string ans)
  14543. day (or (nth 3 tl) (nth 3 org-defdecode))
  14544. month (or (nth 4 tl)
  14545. (if (and org-read-date-prefer-future
  14546. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14547. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14548. (nth 4 org-defdecode)))
  14549. year (or (and (not kill-year) (nth 5 tl))
  14550. (if (and org-read-date-prefer-future
  14551. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14552. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14553. (nth 5 org-defdecode)))
  14554. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14555. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14556. second (or (nth 0 tl) 0)
  14557. wday (nth 6 tl))
  14558. (when (and (eq org-read-date-prefer-future 'time)
  14559. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14560. (equal day (nth 3 nowdecode))
  14561. (equal month (nth 4 nowdecode))
  14562. (equal year (nth 5 nowdecode))
  14563. (nth 2 tl)
  14564. (or (< (nth 2 tl) (nth 2 nowdecode))
  14565. (and (= (nth 2 tl) (nth 2 nowdecode))
  14566. (nth 1 tl)
  14567. (< (nth 1 tl) (nth 1 nowdecode)))))
  14568. (setq day (1+ day)
  14569. futurep t))
  14570. ;; Special date definitions below
  14571. (cond
  14572. (iso-week
  14573. ;; There was an iso week
  14574. (require 'cal-iso)
  14575. (setq futurep nil)
  14576. (setq year (or iso-year year)
  14577. day (or iso-weekday wday 1)
  14578. wday nil ; to make sure that the trigger below does not match
  14579. iso-date (calendar-gregorian-from-absolute
  14580. (calendar-absolute-from-iso
  14581. (list iso-week day year))))
  14582. ; FIXME: Should we also push ISO weeks into the future?
  14583. ; (when (and org-read-date-prefer-future
  14584. ; (not iso-year)
  14585. ; (< (calendar-absolute-from-gregorian iso-date)
  14586. ; (time-to-days (current-time))))
  14587. ; (setq year (1+ year)
  14588. ; iso-date (calendar-gregorian-from-absolute
  14589. ; (calendar-absolute-from-iso
  14590. ; (list iso-week day year)))))
  14591. (setq month (car iso-date)
  14592. year (nth 2 iso-date)
  14593. day (nth 1 iso-date)))
  14594. (deltan
  14595. (setq futurep nil)
  14596. (unless deltadef
  14597. (let ((now (decode-time (current-time))))
  14598. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14599. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14600. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14601. ((equal deltaw "m") (setq month (+ month deltan)))
  14602. ((equal deltaw "y") (setq year (+ year deltan)))))
  14603. ((and wday (not (nth 3 tl)))
  14604. ;; Weekday was given, but no day, so pick that day in the week
  14605. ;; on or after the derived date.
  14606. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14607. (unless (equal wday wday1)
  14608. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14609. (if (and (boundp 'org-time-was-given)
  14610. (nth 2 tl))
  14611. (setq org-time-was-given t))
  14612. (if (< year 100) (setq year (+ 2000 year)))
  14613. ;; Check of the date is representable
  14614. (if org-read-date-force-compatible-dates
  14615. (progn
  14616. (if (< year 1970)
  14617. (setq year 1970 org-read-date-analyze-forced-year t))
  14618. (if (> year 2037)
  14619. (setq year 2037 org-read-date-analyze-forced-year t)))
  14620. (condition-case nil
  14621. (ignore (encode-time second minute hour day month year))
  14622. (error
  14623. (setq year (nth 5 org-defdecode))
  14624. (setq org-read-date-analyze-forced-year t))))
  14625. (setq org-read-date-analyze-futurep futurep)
  14626. (list second minute hour day month year)))
  14627. (defvar parse-time-weekdays)
  14628. (defun org-read-date-get-relative (s today default)
  14629. "Check string S for special relative date string.
  14630. TODAY and DEFAULT are internal times, for today and for a default.
  14631. Return shift list (N what def-flag)
  14632. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14633. N is the number of WHATs to shift.
  14634. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14635. the DEFAULT date rather than TODAY."
  14636. (require 'parse-time)
  14637. (when (and
  14638. (string-match
  14639. (concat
  14640. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14641. "\\([0-9]+\\)?"
  14642. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14643. "\\([ \t]\\|$\\)") s)
  14644. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14645. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14646. (string-to-char (substring (match-string 1 s) -1))
  14647. ?+))
  14648. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14649. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14650. (what (if (match-end 3) (match-string 3 s) "d"))
  14651. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14652. (date (if rel default today))
  14653. (wday (nth 6 (decode-time date)))
  14654. delta)
  14655. (if wday1
  14656. (progn
  14657. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14658. (if (= dir ?-) (setq delta (- delta 7)))
  14659. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14660. (list delta "d" rel))
  14661. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14662. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14663. "Turn a user-specified date into the internal representation.
  14664. The internal representation needed by the calendar is (month day year).
  14665. This is a wrapper to handle the brain-dead convention in calendar that
  14666. user function argument order change dependent on argument order."
  14667. (if (boundp 'calendar-date-style)
  14668. (cond
  14669. ((eq calendar-date-style 'american)
  14670. (list arg1 arg2 arg3))
  14671. ((eq calendar-date-style 'european)
  14672. (list arg2 arg1 arg3))
  14673. ((eq calendar-date-style 'iso)
  14674. (list arg2 arg3 arg1)))
  14675. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14676. (if (org-bound-and-true-p european-calendar-style)
  14677. (list arg2 arg1 arg3)
  14678. (list arg1 arg2 arg3)))))
  14679. (defun org-eval-in-calendar (form &optional keepdate)
  14680. "Eval FORM in the calendar window and return to current window.
  14681. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14682. otherwise stick to the current value of `org-ans2'."
  14683. (let ((sf (selected-frame))
  14684. (sw (selected-window)))
  14685. (select-window (get-buffer-window "*Calendar*" t))
  14686. (eval form)
  14687. (when (and (not keepdate) (calendar-cursor-to-date))
  14688. (let* ((date (calendar-cursor-to-date))
  14689. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14690. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14691. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14692. (select-window sw)
  14693. (org-select-frame-set-input-focus sf)))
  14694. (defun org-calendar-select ()
  14695. "Return to `org-read-date' with the date currently selected.
  14696. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14697. (interactive)
  14698. (when (calendar-cursor-to-date)
  14699. (let* ((date (calendar-cursor-to-date))
  14700. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14701. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14702. (if (active-minibuffer-window) (exit-minibuffer))))
  14703. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14704. "Insert a date stamp for the date given by the internal TIME.
  14705. WITH-HM means use the stamp format that includes the time of the day.
  14706. INACTIVE means use square brackets instead of angular ones, so that the
  14707. stamp will not contribute to the agenda.
  14708. PRE and POST are optional strings to be inserted before and after the
  14709. stamp.
  14710. The command returns the inserted time stamp."
  14711. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14712. stamp)
  14713. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14714. (insert-before-markers (or pre ""))
  14715. (when (listp extra)
  14716. (setq extra (car extra))
  14717. (if (and (stringp extra)
  14718. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14719. (setq extra (format "-%02d:%02d"
  14720. (string-to-number (match-string 1 extra))
  14721. (string-to-number (match-string 2 extra))))
  14722. (setq extra nil)))
  14723. (when extra
  14724. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14725. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14726. (insert-before-markers (or post ""))
  14727. (setq org-last-inserted-timestamp stamp)))
  14728. (defun org-toggle-time-stamp-overlays ()
  14729. "Toggle the use of custom time stamp formats."
  14730. (interactive)
  14731. (setq org-display-custom-times (not org-display-custom-times))
  14732. (unless org-display-custom-times
  14733. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14734. (while (setq p (next-single-property-change p 'display))
  14735. (if (and (get-text-property p 'display)
  14736. (eq (get-text-property p 'face) 'org-date))
  14737. (remove-text-properties
  14738. p (setq p (next-single-property-change p 'display))
  14739. '(display t))))
  14740. (set-buffer-modified-p bmp)))
  14741. (if (featurep 'xemacs)
  14742. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14743. (org-restart-font-lock)
  14744. (setq org-table-may-need-update t)
  14745. (if org-display-custom-times
  14746. (message "Time stamps are overlaid with custom format")
  14747. (message "Time stamp overlays removed")))
  14748. (defun org-display-custom-time (beg end)
  14749. "Overlay modified time stamp format over timestamp between BEG and END."
  14750. (let* ((ts (buffer-substring beg end))
  14751. t1 w1 with-hm tf time str w2 (off 0))
  14752. (save-match-data
  14753. (setq t1 (org-parse-time-string ts t))
  14754. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14755. (setq off (- (match-end 0) (match-beginning 0)))))
  14756. (setq end (- end off))
  14757. (setq w1 (- end beg)
  14758. with-hm (and (nth 1 t1) (nth 2 t1))
  14759. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14760. time (org-fix-decoded-time t1)
  14761. str (org-add-props
  14762. (format-time-string
  14763. (substring tf 1 -1) (apply 'encode-time time))
  14764. nil 'mouse-face 'highlight)
  14765. w2 (length str))
  14766. (if (not (= w2 w1))
  14767. (add-text-properties (1+ beg) (+ 2 beg)
  14768. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14769. (if (featurep 'xemacs)
  14770. (progn
  14771. (put-text-property beg end 'invisible t)
  14772. (put-text-property beg end 'end-glyph (make-glyph str)))
  14773. (put-text-property beg end 'display str))))
  14774. (defun org-translate-time (string)
  14775. "Translate all timestamps in STRING to custom format.
  14776. But do this only if the variable `org-display-custom-times' is set."
  14777. (when org-display-custom-times
  14778. (save-match-data
  14779. (let* ((start 0)
  14780. (re org-ts-regexp-both)
  14781. t1 with-hm inactive tf time str beg end)
  14782. (while (setq start (string-match re string start))
  14783. (setq beg (match-beginning 0)
  14784. end (match-end 0)
  14785. t1 (save-match-data
  14786. (org-parse-time-string (substring string beg end) t))
  14787. with-hm (and (nth 1 t1) (nth 2 t1))
  14788. inactive (equal (substring string beg (1+ beg)) "[")
  14789. tf (funcall (if with-hm 'cdr 'car)
  14790. org-time-stamp-custom-formats)
  14791. time (org-fix-decoded-time t1)
  14792. str (format-time-string
  14793. (concat
  14794. (if inactive "[" "<") (substring tf 1 -1)
  14795. (if inactive "]" ">"))
  14796. (apply 'encode-time time))
  14797. string (replace-match str t t string)
  14798. start (+ start (length str)))))))
  14799. string)
  14800. (defun org-fix-decoded-time (time)
  14801. "Set 0 instead of nil for the first 6 elements of time.
  14802. Don't touch the rest."
  14803. (let ((n 0))
  14804. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14805. (org-define-obsolete-function-alias
  14806. 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14807. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14808. "Difference between TIMESTAMP-STRING and now in days.
  14809. If SECONDS is non-nil, return the difference in seconds."
  14810. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14811. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14812. (funcall fdiff (current-time)))))
  14813. (defun org-deadline-close (timestamp-string &optional ndays)
  14814. "Is the time in TIMESTAMP-STRING close to the current date?"
  14815. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14816. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14817. (not (org-entry-is-done-p))))
  14818. (defun org-get-wdays (ts)
  14819. "Get the deadline lead time appropriate for timestring TS."
  14820. (cond
  14821. ((<= org-deadline-warning-days 0)
  14822. ;; 0 or negative, enforce this value no matter what
  14823. (- org-deadline-warning-days))
  14824. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14825. ;; lead time is specified.
  14826. (floor (* (string-to-number (match-string 1 ts))
  14827. (cdr (assoc (match-string 2 ts)
  14828. '(("d" . 1) ("w" . 7)
  14829. ("m" . 30.4) ("y" . 365.25)))))))
  14830. ;; go for the default.
  14831. (t org-deadline-warning-days)))
  14832. (defun org-calendar-select-mouse (ev)
  14833. "Return to `org-read-date' with the date currently selected.
  14834. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14835. (interactive "e")
  14836. (mouse-set-point ev)
  14837. (when (calendar-cursor-to-date)
  14838. (let* ((date (calendar-cursor-to-date))
  14839. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14840. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14841. (if (active-minibuffer-window) (exit-minibuffer))))
  14842. (defun org-check-deadlines (ndays)
  14843. "Check if there are any deadlines due or past due.
  14844. A deadline is considered due if it happens within `org-deadline-warning-days'
  14845. days from today's date. If the deadline appears in an entry marked DONE,
  14846. it is not shown. The prefix arg NDAYS can be used to test that many
  14847. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14848. (interactive "P")
  14849. (let* ((org-warn-days
  14850. (cond
  14851. ((equal ndays '(4)) 100000)
  14852. (ndays (prefix-numeric-value ndays))
  14853. (t (abs org-deadline-warning-days))))
  14854. (case-fold-search nil)
  14855. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14856. (callback
  14857. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14858. (message "%d deadlines past-due or due within %d days"
  14859. (org-occur regexp nil callback)
  14860. org-warn-days)))
  14861. (defsubst org-re-timestamp (type)
  14862. "Return a regexp for timestamp TYPE.
  14863. Allowed values for TYPE are:
  14864. all: all timestamps
  14865. active: only active timestamps (<...>)
  14866. inactive: only inactive timestamps ([...])
  14867. scheduled: only scheduled timestamps
  14868. deadline: only deadline timestamps
  14869. When TYPE is nil, fall back on returning a regexp that matches
  14870. both scheduled and deadline timestamps."
  14871. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14872. ((eq type 'active) org-ts-regexp)
  14873. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14874. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14875. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14876. ((eq type 'scheduled-or-deadline)
  14877. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14878. (defun org-check-before-date (date)
  14879. "Check if there are deadlines or scheduled entries before DATE."
  14880. (interactive (list (org-read-date)))
  14881. (let ((case-fold-search nil)
  14882. (regexp (org-re-timestamp org-ts-type))
  14883. (callback
  14884. (lambda () (time-less-p
  14885. (org-time-string-to-time (match-string 1))
  14886. (org-time-string-to-time date)))))
  14887. (message "%d entries before %s"
  14888. (org-occur regexp nil callback) date)))
  14889. (defun org-check-after-date (date)
  14890. "Check if there are deadlines or scheduled entries after DATE."
  14891. (interactive (list (org-read-date)))
  14892. (let ((case-fold-search nil)
  14893. (regexp (org-re-timestamp org-ts-type))
  14894. (callback
  14895. (lambda () (not
  14896. (time-less-p
  14897. (org-time-string-to-time (match-string 1))
  14898. (org-time-string-to-time date))))))
  14899. (message "%d entries after %s"
  14900. (org-occur regexp nil callback) date)))
  14901. (defun org-check-dates-range (start-date end-date)
  14902. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14903. (interactive (list (org-read-date nil nil nil "Range starts")
  14904. (org-read-date nil nil nil "Range end")))
  14905. (let ((case-fold-search nil)
  14906. (regexp (org-re-timestamp org-ts-type))
  14907. (callback
  14908. (lambda ()
  14909. (let ((match (match-string 1)))
  14910. (and
  14911. (not (time-less-p
  14912. (org-time-string-to-time match)
  14913. (org-time-string-to-time start-date)))
  14914. (time-less-p
  14915. (org-time-string-to-time match)
  14916. (org-time-string-to-time end-date)))))))
  14917. (message "%d entries between %s and %s"
  14918. (org-occur regexp nil callback) start-date end-date)))
  14919. (defun org-evaluate-time-range (&optional to-buffer)
  14920. "Evaluate a time range by computing the difference between start and end.
  14921. Normally the result is just printed in the echo area, but with prefix arg
  14922. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14923. If the time range is actually in a table, the result is inserted into the
  14924. next column.
  14925. For time difference computation, a year is assumed to be exactly 365
  14926. days in order to avoid rounding problems."
  14927. (interactive "P")
  14928. (or
  14929. (org-clock-update-time-maybe)
  14930. (save-excursion
  14931. (unless (org-at-date-range-p t)
  14932. (goto-char (point-at-bol))
  14933. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14934. (if (not (org-at-date-range-p t))
  14935. (error "Not at a time-stamp range, and none found in current line")))
  14936. (let* ((ts1 (match-string 1))
  14937. (ts2 (match-string 2))
  14938. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14939. (match-end (match-end 0))
  14940. (time1 (org-time-string-to-time ts1))
  14941. (time2 (org-time-string-to-time ts2))
  14942. (t1 (org-float-time time1))
  14943. (t2 (org-float-time time2))
  14944. (diff (abs (- t2 t1)))
  14945. (negative (< (- t2 t1) 0))
  14946. ;; (ys (floor (* 365 24 60 60)))
  14947. (ds (* 24 60 60))
  14948. (hs (* 60 60))
  14949. (fy "%dy %dd %02d:%02d")
  14950. (fy1 "%dy %dd")
  14951. (fd "%dd %02d:%02d")
  14952. (fd1 "%dd")
  14953. (fh "%02d:%02d")
  14954. y d h m align)
  14955. (if havetime
  14956. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14957. y 0
  14958. d (floor (/ diff ds)) diff (mod diff ds)
  14959. h (floor (/ diff hs)) diff (mod diff hs)
  14960. m (floor (/ diff 60)))
  14961. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14962. y 0
  14963. d (floor (+ (/ diff ds) 0.5))
  14964. h 0 m 0))
  14965. (if (not to-buffer)
  14966. (message "%s" (org-make-tdiff-string y d h m))
  14967. (if (org-at-table-p)
  14968. (progn
  14969. (goto-char match-end)
  14970. (setq align t)
  14971. (and (looking-at " *|") (goto-char (match-end 0))))
  14972. (goto-char match-end))
  14973. (if (looking-at
  14974. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14975. (replace-match ""))
  14976. (if negative (insert " -"))
  14977. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14978. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14979. (insert " " (format fh h m))))
  14980. (if align (org-table-align))
  14981. (message "Time difference inserted")))))
  14982. (defun org-make-tdiff-string (y d h m)
  14983. (let ((fmt "")
  14984. (l nil))
  14985. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14986. l (push y l)))
  14987. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14988. l (push d l)))
  14989. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14990. l (push h l)))
  14991. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14992. l (push m l)))
  14993. (apply 'format fmt (nreverse l))))
  14994. (defun org-time-string-to-time (s &optional buffer pos)
  14995. "Convert a timestamp string into internal time."
  14996. (condition-case errdata
  14997. (apply 'encode-time (org-parse-time-string s))
  14998. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14999. s (if (not (and buffer pos))
  15000. ""
  15001. (format " at %d in buffer `%s'" pos buffer))
  15002. (cdr errdata)))))
  15003. (defun org-time-string-to-seconds (s)
  15004. "Convert a timestamp string to a number of seconds."
  15005. (org-float-time (org-time-string-to-time s)))
  15006. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15007. "Convert a time stamp to an absolute day number.
  15008. If there is a specifier for a cyclic time stamp, get the closest date to
  15009. DAYNR.
  15010. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15011. The variable date is bound by the calendar when this is called."
  15012. (cond
  15013. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15014. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15015. daynr
  15016. (+ daynr 1000)))
  15017. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15018. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15019. (time-to-days (current-time))) (match-string 0 s)
  15020. prefer show-all))
  15021. (t (time-to-days
  15022. (condition-case errdata
  15023. (apply 'encode-time (org-parse-time-string s))
  15024. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15025. s (if (not (and buffer pos))
  15026. ""
  15027. (format " at %d in buffer `%s'" pos buffer))
  15028. (cdr errdata))))))))
  15029. (defun org-days-to-iso-week (days)
  15030. "Return the iso week number."
  15031. (require 'cal-iso)
  15032. (car (calendar-iso-from-absolute days)))
  15033. (defun org-small-year-to-year (year)
  15034. "Convert 2-digit years into 4-digit years.
  15035. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15036. The year 2000 cannot be abbreviated. Any year larger than 99
  15037. is returned unchanged."
  15038. (if (< year 38)
  15039. (setq year (+ 2000 year))
  15040. (if (< year 100)
  15041. (setq year (+ 1900 year))))
  15042. year)
  15043. (defun org-time-from-absolute (d)
  15044. "Return the time corresponding to date D.
  15045. D may be an absolute day number, or a calendar-type list (month day year)."
  15046. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15047. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15048. (defun org-calendar-holiday ()
  15049. "List of holidays, for Diary display in Org-mode."
  15050. (require 'holidays)
  15051. (let ((hl (funcall
  15052. (if (fboundp 'calendar-check-holidays)
  15053. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15054. (if hl (mapconcat 'identity hl "; "))))
  15055. (defun org-diary-sexp-entry (sexp entry date)
  15056. "Process a SEXP diary ENTRY for DATE."
  15057. (require 'diary-lib)
  15058. (let ((result (if calendar-debug-sexp
  15059. (let ((stack-trace-on-error t))
  15060. (eval (car (read-from-string sexp))))
  15061. (condition-case nil
  15062. (eval (car (read-from-string sexp)))
  15063. (error
  15064. (beep)
  15065. (message "Bad sexp at line %d in %s: %s"
  15066. (org-current-line)
  15067. (buffer-file-name) sexp)
  15068. (sleep-for 2))))))
  15069. (cond ((stringp result) (split-string result "; "))
  15070. ((and (consp result)
  15071. (not (consp (cdr result)))
  15072. (stringp (cdr result))) (cdr result))
  15073. ((and (consp result)
  15074. (stringp (car result))) result)
  15075. (result entry))))
  15076. (defun org-diary-to-ical-string (frombuf)
  15077. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15078. This uses the icalendar.el library."
  15079. (let* ((tmpdir (if (featurep 'xemacs)
  15080. (temp-directory)
  15081. temporary-file-directory))
  15082. (tmpfile (make-temp-name
  15083. (expand-file-name "orgics" tmpdir)))
  15084. buf rtn b e)
  15085. (with-current-buffer frombuf
  15086. (icalendar-export-region (point-min) (point-max) tmpfile)
  15087. (setq buf (find-buffer-visiting tmpfile))
  15088. (set-buffer buf)
  15089. (goto-char (point-min))
  15090. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15091. (setq b (match-beginning 0)))
  15092. (goto-char (point-max))
  15093. (if (re-search-backward "^END:VEVENT" nil t)
  15094. (setq e (match-end 0)))
  15095. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15096. (kill-buffer buf)
  15097. (delete-file tmpfile)
  15098. rtn))
  15099. (defun org-closest-date (start current change prefer show-all)
  15100. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15101. When PREFER is `past', return a date that is either CURRENT or past.
  15102. When PREFER is `future', return a date that is either CURRENT or future.
  15103. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15104. ;; Make the proper lists from the dates
  15105. (catch 'exit
  15106. (let ((a1 '(("h" . hour)
  15107. ("d" . day)
  15108. ("w" . week)
  15109. ("m" . month)
  15110. ("y" . year)))
  15111. (shour (nth 2 (org-parse-time-string start)))
  15112. dn dw sday cday n1 n2 n0
  15113. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15114. (setq start (org-date-to-gregorian start)
  15115. current (org-date-to-gregorian
  15116. (if show-all
  15117. current
  15118. (time-to-days (current-time))))
  15119. sday (calendar-absolute-from-gregorian start)
  15120. cday (calendar-absolute-from-gregorian current))
  15121. (if (<= cday sday) (throw 'exit sday))
  15122. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15123. (setq dn (string-to-number (match-string 1 change))
  15124. dw (cdr (assoc (match-string 2 change) a1)))
  15125. (error "Invalid change specifier: %s" change))
  15126. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15127. (cond
  15128. ((eq dw 'hour)
  15129. (let ((missing-hours
  15130. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15131. dn)))
  15132. (setq n1 (if (zerop missing-hours) cday
  15133. (- cday (1+ (floor (/ missing-hours 24)))))
  15134. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15135. ((eq dw 'day)
  15136. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15137. n2 (+ n1 dn)))
  15138. ((eq dw 'year)
  15139. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15140. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15141. (setq date1 (list m d y1)
  15142. n1 (calendar-absolute-from-gregorian date1)
  15143. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15144. n2 (calendar-absolute-from-gregorian date2)))
  15145. ((eq dw 'month)
  15146. ;; approx number of month between the two dates
  15147. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15148. ;; How often does dn fit in there?
  15149. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15150. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15151. m (+ m nm)
  15152. ny (floor (/ m 12))
  15153. y (+ y ny)
  15154. m (- m (* ny 12)))
  15155. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15156. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15157. (setq m2 (+ m dn) y2 y)
  15158. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15159. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15160. (while (<= n2 cday)
  15161. (setq n1 n2 m m2 y y2)
  15162. (setq m2 (+ m dn) y2 y)
  15163. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15164. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15165. ;; Make sure n1 is the earlier date
  15166. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15167. (if show-all
  15168. (cond
  15169. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15170. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15171. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15172. (cond
  15173. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15174. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15175. (t (if (= cday n1) n1 n2)))))))
  15176. (defun org-date-to-gregorian (date)
  15177. "Turn any specification of DATE into a Gregorian date for the calendar."
  15178. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15179. ((and (listp date) (= (length date) 3)) date)
  15180. ((stringp date)
  15181. (setq date (org-parse-time-string date))
  15182. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15183. ((listp date)
  15184. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15185. (defun org-parse-time-string (s &optional nodefault)
  15186. "Parse the standard Org-mode time string.
  15187. This should be a lot faster than the normal `parse-time-string'.
  15188. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15189. hour and minute fields will be nil if not given."
  15190. (cond ((string-match org-ts-regexp0 s)
  15191. (list 0
  15192. (if (or (match-beginning 8) (not nodefault))
  15193. (string-to-number (or (match-string 8 s) "0")))
  15194. (if (or (match-beginning 7) (not nodefault))
  15195. (string-to-number (or (match-string 7 s) "0")))
  15196. (string-to-number (match-string 4 s))
  15197. (string-to-number (match-string 3 s))
  15198. (string-to-number (match-string 2 s))
  15199. nil nil nil))
  15200. ((string-match "^<[^>]+>$" s)
  15201. (decode-time (seconds-to-time (org-matcher-time s))))
  15202. (t (error "Not a standard Org-mode time string: %s" s))))
  15203. (defun org-timestamp-up (&optional arg)
  15204. "Increase the date item at the cursor by one.
  15205. If the cursor is on the year, change the year. If it is on the month,
  15206. the day or the time, change that.
  15207. With prefix ARG, change by that many units."
  15208. (interactive "p")
  15209. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15210. (defun org-timestamp-down (&optional arg)
  15211. "Decrease the date item at the cursor by one.
  15212. If the cursor is on the year, change the year. If it is on the month,
  15213. the day or the time, change that.
  15214. With prefix ARG, change by that many units."
  15215. (interactive "p")
  15216. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15217. (defun org-timestamp-up-day (&optional arg)
  15218. "Increase the date in the time stamp by one day.
  15219. With prefix ARG, change that many days."
  15220. (interactive "p")
  15221. (if (and (not (org-at-timestamp-p t))
  15222. (org-at-heading-p))
  15223. (org-todo 'up)
  15224. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15225. (defun org-timestamp-down-day (&optional arg)
  15226. "Decrease the date in the time stamp by one day.
  15227. With prefix ARG, change that many days."
  15228. (interactive "p")
  15229. (if (and (not (org-at-timestamp-p t))
  15230. (org-at-heading-p))
  15231. (org-todo 'down)
  15232. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15233. (defun org-at-timestamp-p (&optional inactive-ok)
  15234. "Determine if the cursor is in or at a timestamp."
  15235. (interactive)
  15236. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15237. (pos (point))
  15238. (ans (or (looking-at tsr)
  15239. (save-excursion
  15240. (skip-chars-backward "^[<\n\r\t")
  15241. (if (> (point) (point-min)) (backward-char 1))
  15242. (and (looking-at tsr)
  15243. (> (- (match-end 0) pos) -1))))))
  15244. (and ans
  15245. (boundp 'org-ts-what)
  15246. (setq org-ts-what
  15247. (cond
  15248. ((= pos (match-beginning 0)) 'bracket)
  15249. ;; Point is considered to be "on the bracket" whether
  15250. ;; it's really on it or right after it.
  15251. ((= pos (1- (match-end 0))) 'bracket)
  15252. ((= pos (match-end 0)) 'after)
  15253. ((org-pos-in-match-range pos 2) 'year)
  15254. ((org-pos-in-match-range pos 3) 'month)
  15255. ((org-pos-in-match-range pos 7) 'hour)
  15256. ((org-pos-in-match-range pos 8) 'minute)
  15257. ((or (org-pos-in-match-range pos 4)
  15258. (org-pos-in-match-range pos 5)) 'day)
  15259. ((and (> pos (or (match-end 8) (match-end 5)))
  15260. (< pos (match-end 0)))
  15261. (- pos (or (match-end 8) (match-end 5))))
  15262. (t 'day))))
  15263. ans))
  15264. (defun org-toggle-timestamp-type ()
  15265. "Toggle the type (<active> or [inactive]) of a time stamp."
  15266. (interactive)
  15267. (when (org-at-timestamp-p t)
  15268. (let ((beg (match-beginning 0)) (end (match-end 0))
  15269. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15270. (save-excursion
  15271. (goto-char beg)
  15272. (while (re-search-forward "[][<>]" end t)
  15273. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15274. t t)))
  15275. (message "Timestamp is now %sactive"
  15276. (if (equal (char-after beg) ?<) "" "in")))))
  15277. (defvar org-clock-history) ; defined in org-clock.el
  15278. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15279. (defun org-timestamp-change (n &optional what updown)
  15280. "Change the date in the time stamp at point.
  15281. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15282. `year', `minute', `second'. If WHAT is not given, the cursor position
  15283. in the timestamp determines what will be changed."
  15284. (let ((origin (point)) origin-cat
  15285. with-hm inactive
  15286. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15287. org-ts-what
  15288. extra rem
  15289. ts time time0 fixnext clrgx)
  15290. (if (not (org-at-timestamp-p t))
  15291. (error "Not at a timestamp"))
  15292. (if (and (not what) (eq org-ts-what 'bracket))
  15293. (org-toggle-timestamp-type)
  15294. ;; Point isn't on brackets. Remember the part of the time-stamp
  15295. ;; the point was in. Indeed, size of time-stamps may change,
  15296. ;; but point must be kept in the same category nonetheless.
  15297. (setq origin-cat org-ts-what)
  15298. (if (and (not what) (not (eq org-ts-what 'day))
  15299. org-display-custom-times
  15300. (get-text-property (point) 'display)
  15301. (not (get-text-property (1- (point)) 'display)))
  15302. (setq org-ts-what 'day))
  15303. (setq org-ts-what (or what org-ts-what)
  15304. inactive (= (char-after (match-beginning 0)) ?\[)
  15305. ts (match-string 0))
  15306. (replace-match "")
  15307. (if (string-match
  15308. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15309. ts)
  15310. (setq extra (match-string 1 ts)))
  15311. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15312. (setq with-hm t))
  15313. (setq time0 (org-parse-time-string ts))
  15314. (when (and updown
  15315. (eq org-ts-what 'minute)
  15316. (not current-prefix-arg))
  15317. ;; This looks like s-up and s-down. Change by one rounding step.
  15318. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15319. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15320. (setcar (cdr time0) (+ (nth 1 time0)
  15321. (if (> n 0) (- rem) (- dm rem))))))
  15322. (setq time
  15323. (encode-time (or (car time0) 0)
  15324. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15325. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15326. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15327. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15328. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15329. (nthcdr 6 time0)))
  15330. (when (and (member org-ts-what '(hour minute))
  15331. extra
  15332. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15333. (setq extra (org-modify-ts-extra
  15334. extra
  15335. (if (eq org-ts-what 'hour) 2 5)
  15336. n dm)))
  15337. (when (integerp org-ts-what)
  15338. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15339. (if (eq what 'calendar)
  15340. (let ((cal-date (org-get-date-from-calendar)))
  15341. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15342. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15343. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15344. (setcar time0 (or (car time0) 0))
  15345. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15346. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15347. (setq time (apply 'encode-time time0))))
  15348. ;; Insert the new time-stamp, and ensure point stays in the same
  15349. ;; category as before (i.e. not after the last position in that
  15350. ;; category).
  15351. (let ((pos (point)))
  15352. ;; Stay before inserted string. `save-excursion' is of no use.
  15353. (setq org-last-changed-timestamp
  15354. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15355. (goto-char pos))
  15356. (save-match-data
  15357. (looking-at org-ts-regexp3)
  15358. (goto-char (cond
  15359. ;; `day' category ends before `hour' if any, or at
  15360. ;; the end of the day name.
  15361. ((eq origin-cat 'day)
  15362. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15363. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15364. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15365. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15366. ;; `year' and `month' have both fixed size: point
  15367. ;; couldn't have moved into another part.
  15368. (t origin))))
  15369. ;; Update clock if on a CLOCK line.
  15370. (org-clock-update-time-maybe)
  15371. ;; Maybe adjust the closest clock in `org-clock-history'
  15372. (when org-clock-adjust-closest
  15373. (if (not (and (org-at-clock-log-p)
  15374. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15375. org-clock-history))))))
  15376. (message "No clock to adjust")
  15377. (cond ((save-excursion ; fix previous clock?
  15378. (re-search-backward org-ts-regexp0 nil t)
  15379. (org-looking-back (concat org-clock-string " \\[")))
  15380. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15381. ((save-excursion ; fix next clock?
  15382. (re-search-backward org-ts-regexp0 nil t)
  15383. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15384. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15385. (save-window-excursion
  15386. ;; Find closest clock to point, adjust the previous/next one in history
  15387. (let* ((p (save-excursion (org-back-to-heading t)))
  15388. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15389. (clfixnth
  15390. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15391. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15392. (if (not clfixpos)
  15393. (message "No clock to adjust")
  15394. (save-excursion
  15395. (org-goto-marker-or-bmk clfixpos)
  15396. (org-show-subtree)
  15397. (when (re-search-forward clrgx nil t)
  15398. (goto-char (match-beginning 1))
  15399. (let (org-clock-adjust-closest)
  15400. (org-timestamp-change n org-ts-what updown))
  15401. (message "Clock adjusted in %s for heading: %s"
  15402. (file-name-nondirectory (buffer-file-name))
  15403. (org-get-heading t t)))))))))
  15404. ;; Try to recenter the calendar window, if any.
  15405. (if (and org-calendar-follow-timestamp-change
  15406. (get-buffer-window "*Calendar*" t)
  15407. (memq org-ts-what '(day month year)))
  15408. (org-recenter-calendar (time-to-days time))))))
  15409. (defun org-modify-ts-extra (s pos n dm)
  15410. "Change the different parts of the lead-time and repeat fields in timestamp."
  15411. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15412. ng h m new rem)
  15413. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15414. (cond
  15415. ((or (org-pos-in-match-range pos 2)
  15416. (org-pos-in-match-range pos 3))
  15417. (setq m (string-to-number (match-string 3 s))
  15418. h (string-to-number (match-string 2 s)))
  15419. (if (org-pos-in-match-range pos 2)
  15420. (setq h (+ h n))
  15421. (setq n (* dm (org-no-warnings (signum n))))
  15422. (when (not (= 0 (setq rem (% m dm))))
  15423. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15424. (setq m (+ m n)))
  15425. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15426. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15427. (setq h (min 24 (max 0 h)))
  15428. (setq ng 1 new (format "-%02d:%02d" h m)))
  15429. ((org-pos-in-match-range pos 6)
  15430. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15431. ((org-pos-in-match-range pos 5)
  15432. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15433. ((org-pos-in-match-range pos 9)
  15434. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15435. ((org-pos-in-match-range pos 8)
  15436. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15437. (when ng
  15438. (setq s (concat
  15439. (substring s 0 (match-beginning ng))
  15440. new
  15441. (substring s (match-end ng))))))
  15442. s))
  15443. (defun org-recenter-calendar (date)
  15444. "If the calendar is visible, recenter it to DATE."
  15445. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15446. (when cwin
  15447. (let ((calendar-move-hook nil))
  15448. (with-selected-window cwin
  15449. (calendar-goto-date (if (listp date) date
  15450. (calendar-gregorian-from-absolute date))))))))
  15451. (defun org-goto-calendar (&optional arg)
  15452. "Go to the Emacs calendar at the current date.
  15453. If there is a time stamp in the current line, go to that date.
  15454. A prefix ARG can be used to force the current date."
  15455. (interactive "P")
  15456. (let ((tsr org-ts-regexp) diff
  15457. (calendar-move-hook nil)
  15458. (calendar-view-holidays-initially-flag nil)
  15459. (calendar-view-diary-initially-flag nil))
  15460. (if (or (org-at-timestamp-p)
  15461. (save-excursion
  15462. (beginning-of-line 1)
  15463. (looking-at (concat ".*" tsr))))
  15464. (let ((d1 (time-to-days (current-time)))
  15465. (d2 (time-to-days
  15466. (org-time-string-to-time (match-string 1)))))
  15467. (setq diff (- d2 d1))))
  15468. (calendar)
  15469. (calendar-goto-today)
  15470. (if (and diff (not arg)) (calendar-forward-day diff))))
  15471. (defun org-get-date-from-calendar ()
  15472. "Return a list (month day year) of date at point in calendar."
  15473. (with-current-buffer "*Calendar*"
  15474. (save-match-data
  15475. (calendar-cursor-to-date))))
  15476. (defun org-date-from-calendar ()
  15477. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15478. If there is already a time stamp at the cursor position, update it."
  15479. (interactive)
  15480. (if (org-at-timestamp-p t)
  15481. (org-timestamp-change 0 'calendar)
  15482. (let ((cal-date (org-get-date-from-calendar)))
  15483. (org-insert-time-stamp
  15484. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15485. (defun org-minutes-to-clocksum-string (m)
  15486. "Format number of minutes as a clocksum string.
  15487. The format is determined by `org-time-clocksum-format',
  15488. `org-time-clocksum-use-fractional' and
  15489. `org-time-clocksum-fractional-format' and
  15490. `org-time-clocksum-use-effort-durations'."
  15491. (let ((clocksum "") h d w mo y fmt n)
  15492. (setq h (if org-time-clocksum-use-effort-durations
  15493. (cdr (assoc "h" org-effort-durations)) 60)
  15494. d (if org-time-clocksum-use-effort-durations
  15495. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15496. w (if org-time-clocksum-use-effort-durations
  15497. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15498. mo (if org-time-clocksum-use-effort-durations
  15499. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15500. y (if org-time-clocksum-use-effort-durations
  15501. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15502. ;; fractional format
  15503. (if org-time-clocksum-use-fractional
  15504. (cond
  15505. ;; single format string
  15506. ((stringp org-time-clocksum-fractional-format)
  15507. (format org-time-clocksum-fractional-format (/ m (float h))))
  15508. ;; choice of fractional formats for different time units
  15509. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15510. (> (/ (truncate m) (* y d h)) 0))
  15511. (format fmt (/ m (* y d (float h)))))
  15512. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15513. (> (/ (truncate m) (* mo d h)) 0))
  15514. (format fmt (/ m (* mo d (float h)))))
  15515. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15516. (> (/ (truncate m) (* w d h)) 0))
  15517. (format fmt (/ m (* w d (float h)))))
  15518. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15519. (> (/ (truncate m) (* d h)) 0))
  15520. (format fmt (/ m (* d (float h)))))
  15521. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15522. (> (/ (truncate m) h) 0))
  15523. (format fmt (/ m (float h))))
  15524. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15525. (format fmt m))
  15526. ;; fall back to smallest time unit with a format
  15527. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15528. (format fmt (/ m (float h))))
  15529. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15530. (format fmt (/ m (* d (float h)))))
  15531. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15532. (format fmt (/ m (* w d (float h)))))
  15533. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15534. (format fmt (/ m (* mo d (float h)))))
  15535. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15536. (format fmt (/ m (* y d (float h))))))
  15537. ;; standard (non-fractional) format, with single format string
  15538. (if (stringp org-time-clocksum-format)
  15539. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15540. ;; separate formats components
  15541. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15542. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15543. (plist-get org-time-clocksum-format :require-years))
  15544. (setq clocksum (concat clocksum (format fmt n))
  15545. m (- m (* n y d h))))
  15546. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15547. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15548. (plist-get org-time-clocksum-format :require-months))
  15549. (setq clocksum (concat clocksum (format fmt n))
  15550. m (- m (* n mo d h))))
  15551. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15552. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15553. (plist-get org-time-clocksum-format :require-weeks))
  15554. (setq clocksum (concat clocksum (format fmt n))
  15555. m (- m (* n w d h))))
  15556. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15557. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15558. (plist-get org-time-clocksum-format :require-days))
  15559. (setq clocksum (concat clocksum (format fmt n))
  15560. m (- m (* n d h))))
  15561. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15562. (or (> (setq n (/ (truncate m) h)) 0)
  15563. (plist-get org-time-clocksum-format :require-hours))
  15564. (setq clocksum (concat clocksum (format fmt n))
  15565. m (- m (* n h))))
  15566. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15567. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15568. (setq clocksum (concat clocksum (format fmt m))))
  15569. ;; return formatted time duration
  15570. clocksum))))
  15571. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15572. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15573. "Org mode version 8.0")
  15574. (defun org-hours-to-clocksum-string (n)
  15575. (org-minutes-to-clocksum-string (* n 60)))
  15576. (defun org-hh:mm-string-to-minutes (s)
  15577. "Convert a string H:MM to a number of minutes.
  15578. If the string is just a number, interpret it as minutes.
  15579. In fact, the first hh:mm or number in the string will be taken,
  15580. there can be extra stuff in the string.
  15581. If no number is found, the return value is 0."
  15582. (cond
  15583. ((integerp s) s)
  15584. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15585. (+ (* (string-to-number (match-string 1 s)) 60)
  15586. (string-to-number (match-string 2 s))))
  15587. ((string-match "\\([0-9]+\\)" s)
  15588. (string-to-number (match-string 1 s)))
  15589. (t 0)))
  15590. (defcustom org-effort-durations
  15591. `(("h" . 60)
  15592. ("d" . ,(* 60 8))
  15593. ("w" . ,(* 60 8 5))
  15594. ("m" . ,(* 60 8 5 4))
  15595. ("y" . ,(* 60 8 5 40)))
  15596. "Conversion factor to minutes for an effort modifier.
  15597. Each entry has the form (MODIFIER . MINUTES).
  15598. In an effort string, a number followed by MODIFIER is multiplied
  15599. by the specified number of MINUTES to obtain an effort in
  15600. minutes.
  15601. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15602. effort string \"2hours\" is equivalent to 120 minutes."
  15603. :group 'org-agenda
  15604. :version "24.1"
  15605. :type '(alist :key-type (string :tag "Modifier")
  15606. :value-type (number :tag "Minutes")))
  15607. (defcustom org-image-actual-width t
  15608. "Should we use the actual width of images when inlining them?
  15609. When set to `t', always use the image width.
  15610. When set to a number, use imagemagick (when available) to set
  15611. the image's width to this value.
  15612. When set to a number in a list, try to get the width from the
  15613. #+ATTR.* keyword if it matches a width specification like
  15614. width=\"[0-9]+\", and fall back on that number if none is found.
  15615. When set to nil, try to get the width from an #+ATTR.* keyword
  15616. and fall back on the original width if none is found.
  15617. This requires Emacs >= 24.1, build with imagemagick support."
  15618. :group 'org-appearance
  15619. :version "24.3"
  15620. :type '(choice
  15621. (const :tag "Use the image width" t)
  15622. (integer :tag "Use a number of pixels")
  15623. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15624. (const :tag "Use #+ATTR* or don't resize" nil)))
  15625. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15626. "Convert a duration string S to minutes.
  15627. A bare number is interpreted as minutes, modifiers can be set by
  15628. customizing `org-effort-durations' (which see).
  15629. Entries containing a colon are interpreted as H:MM by
  15630. `org-hh:mm-string-to-minutes'."
  15631. (let ((result 0)
  15632. (re (concat "\\([0-9.]+\\) *\\("
  15633. (regexp-opt (mapcar 'car org-effort-durations))
  15634. "\\)")))
  15635. (while (string-match re s)
  15636. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15637. (string-to-number (match-string 1 s))))
  15638. (setq s (replace-match "" nil t s)))
  15639. (setq result (floor result))
  15640. (incf result (org-hh:mm-string-to-minutes s))
  15641. (if output-to-string (number-to-string result) result)))
  15642. ;;;; Files
  15643. (defun org-save-all-org-buffers ()
  15644. "Save all Org-mode buffers without user confirmation."
  15645. (interactive)
  15646. (message "Saving all Org-mode buffers...")
  15647. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15648. (when (featurep 'org-id) (org-id-locations-save))
  15649. (message "Saving all Org-mode buffers... done"))
  15650. (defun org-revert-all-org-buffers ()
  15651. "Revert all Org-mode buffers.
  15652. Prompt for confirmation when there are unsaved changes.
  15653. Be sure you know what you are doing before letting this function
  15654. overwrite your changes.
  15655. This function is useful in a setup where one tracks org files
  15656. with a version control system, to revert on one machine after pulling
  15657. changes from another. I believe the procedure must be like this:
  15658. 1. M-x org-save-all-org-buffers
  15659. 2. Pull changes from the other machine, resolve conflicts
  15660. 3. M-x org-revert-all-org-buffers"
  15661. (interactive)
  15662. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15663. (error "Abort"))
  15664. (save-excursion
  15665. (save-window-excursion
  15666. (mapc
  15667. (lambda (b)
  15668. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15669. (with-current-buffer b buffer-file-name))
  15670. (org-pop-to-buffer-same-window b)
  15671. (revert-buffer t 'no-confirm)))
  15672. (buffer-list))
  15673. (when (and (featurep 'org-id) org-id-track-globally)
  15674. (org-id-locations-load)))))
  15675. ;;;; Agenda files
  15676. ;;;###autoload
  15677. (defun org-switchb (&optional arg)
  15678. "Switch between Org buffers.
  15679. With one prefix argument, restrict available buffers to files.
  15680. With two prefix arguments, restrict available buffers to agenda files.
  15681. Defaults to `iswitchb' for buffer name completion.
  15682. Set `org-completion-use-ido' to make it use ido instead."
  15683. (interactive "P")
  15684. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15685. ((equal arg '(16)) (org-buffer-list 'agenda))
  15686. (t (org-buffer-list))))
  15687. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15688. (org-completion-use-ido org-completion-use-ido))
  15689. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15690. (setq org-completion-use-iswitchb t))
  15691. (org-pop-to-buffer-same-window
  15692. (org-icompleting-read "Org buffer: "
  15693. (mapcar 'list (mapcar 'buffer-name blist))
  15694. nil t))))
  15695. ;;; Define some older names previously used for this functionality
  15696. ;;;###autoload
  15697. (defalias 'org-ido-switchb 'org-switchb)
  15698. ;;;###autoload
  15699. (defalias 'org-iswitchb 'org-switchb)
  15700. (defun org-buffer-list (&optional predicate exclude-tmp)
  15701. "Return a list of Org buffers.
  15702. PREDICATE can be `export', `files' or `agenda'.
  15703. export restrict the list to Export buffers.
  15704. files restrict the list to buffers visiting Org files.
  15705. agenda restrict the list to buffers visiting agenda files.
  15706. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15707. (let* ((bfn nil)
  15708. (agenda-files (and (eq predicate 'agenda)
  15709. (mapcar 'file-truename (org-agenda-files t))))
  15710. (filter
  15711. (cond
  15712. ((eq predicate 'files)
  15713. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15714. ((eq predicate 'export)
  15715. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15716. ((eq predicate 'agenda)
  15717. (lambda (b)
  15718. (with-current-buffer b
  15719. (and (derived-mode-p 'org-mode)
  15720. (setq bfn (buffer-file-name b))
  15721. (member (file-truename bfn) agenda-files)))))
  15722. (t (lambda (b) (with-current-buffer b
  15723. (or (derived-mode-p 'org-mode)
  15724. (string-match "\*Org .*Export"
  15725. (buffer-name b)))))))))
  15726. (delq nil
  15727. (mapcar
  15728. (lambda(b)
  15729. (if (and (funcall filter b)
  15730. (or (not exclude-tmp)
  15731. (not (string-match "tmp" (buffer-name b)))))
  15732. b
  15733. nil))
  15734. (buffer-list)))))
  15735. (defun org-agenda-files (&optional unrestricted archives)
  15736. "Get the list of agenda files.
  15737. Optional UNRESTRICTED means return the full list even if a restriction
  15738. is currently in place.
  15739. When ARCHIVES is t, include all archive files that are really being
  15740. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15741. `org-agenda-archives-mode' is t."
  15742. (let ((files
  15743. (cond
  15744. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15745. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15746. ((listp org-agenda-files) org-agenda-files)
  15747. (t (error "Invalid value of `org-agenda-files'")))))
  15748. (setq files (apply 'append
  15749. (mapcar (lambda (f)
  15750. (if (file-directory-p f)
  15751. (directory-files
  15752. f t org-agenda-file-regexp)
  15753. (list f)))
  15754. files)))
  15755. (when org-agenda-skip-unavailable-files
  15756. (setq files (delq nil
  15757. (mapcar (function
  15758. (lambda (file)
  15759. (and (file-readable-p file) file)))
  15760. files))))
  15761. (when (or (eq archives t)
  15762. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15763. (setq files (org-add-archive-files files)))
  15764. files))
  15765. (defun org-agenda-file-p (&optional file)
  15766. "Return non-nil, if FILE is an agenda file.
  15767. If FILE is omitted, use the file associated with the current
  15768. buffer."
  15769. (member (or file (buffer-file-name))
  15770. (org-agenda-files t)))
  15771. (defun org-edit-agenda-file-list ()
  15772. "Edit the list of agenda files.
  15773. Depending on setup, this either uses customize to edit the variable
  15774. `org-agenda-files', or it visits the file that is holding the list. In the
  15775. latter case, the buffer is set up in a way that saving it automatically kills
  15776. the buffer and restores the previous window configuration."
  15777. (interactive)
  15778. (if (stringp org-agenda-files)
  15779. (let ((cw (current-window-configuration)))
  15780. (find-file org-agenda-files)
  15781. (org-set-local 'org-window-configuration cw)
  15782. (org-add-hook 'after-save-hook
  15783. (lambda ()
  15784. (set-window-configuration
  15785. (prog1 org-window-configuration
  15786. (kill-buffer (current-buffer))))
  15787. (org-install-agenda-files-menu)
  15788. (message "New agenda file list installed"))
  15789. nil 'local)
  15790. (message "%s" (substitute-command-keys
  15791. "Edit list and finish with \\[save-buffer]")))
  15792. (customize-variable 'org-agenda-files)))
  15793. (defun org-store-new-agenda-file-list (list)
  15794. "Set new value for the agenda file list and save it correctly."
  15795. (if (stringp org-agenda-files)
  15796. (let ((fe (org-read-agenda-file-list t)) b u)
  15797. (while (setq b (find-buffer-visiting org-agenda-files))
  15798. (kill-buffer b))
  15799. (with-temp-file org-agenda-files
  15800. (insert
  15801. (mapconcat
  15802. (lambda (f) ;; Keep un-expanded entries.
  15803. (if (setq u (assoc f fe))
  15804. (cdr u)
  15805. f))
  15806. list "\n")
  15807. "\n")))
  15808. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15809. (org-insert-mode-line-in-empty-file nil))
  15810. (setq org-agenda-files list)
  15811. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15812. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15813. "Read the list of agenda files from a file.
  15814. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15815. filenames, used by `org-store-new-agenda-file-list' to write back
  15816. un-expanded file names."
  15817. (when (file-directory-p org-agenda-files)
  15818. (error "`org-agenda-files' cannot be a single directory"))
  15819. (when (stringp org-agenda-files)
  15820. (with-temp-buffer
  15821. (insert-file-contents org-agenda-files)
  15822. (mapcar
  15823. (lambda (f)
  15824. (let ((e (expand-file-name (substitute-in-file-name f)
  15825. org-directory)))
  15826. (if pair-with-expansion
  15827. (cons e f)
  15828. e)))
  15829. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15830. ;;;###autoload
  15831. (defun org-cycle-agenda-files ()
  15832. "Cycle through the files in `org-agenda-files'.
  15833. If the current buffer visits an agenda file, find the next one in the list.
  15834. If the current buffer does not, find the first agenda file."
  15835. (interactive)
  15836. (let* ((fs (org-agenda-files t))
  15837. (files (append fs (list (car fs))))
  15838. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15839. file)
  15840. (unless files (error "No agenda files"))
  15841. (catch 'exit
  15842. (while (setq file (pop files))
  15843. (if (equal (file-truename file) tcf)
  15844. (when (car files)
  15845. (find-file (car files))
  15846. (throw 'exit t))))
  15847. (find-file (car fs)))
  15848. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15849. (defun org-agenda-file-to-front (&optional to-end)
  15850. "Move/add the current file to the top of the agenda file list.
  15851. If the file is not present in the list, it is added to the front. If it is
  15852. present, it is moved there. With optional argument TO-END, add/move to the
  15853. end of the list."
  15854. (interactive "P")
  15855. (let ((org-agenda-skip-unavailable-files nil)
  15856. (file-alist (mapcar (lambda (x)
  15857. (cons (file-truename x) x))
  15858. (org-agenda-files t)))
  15859. (ctf (file-truename
  15860. (or buffer-file-name
  15861. (error "Please save the current buffer to a file"))))
  15862. x had)
  15863. (setq x (assoc ctf file-alist) had x)
  15864. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15865. (if to-end
  15866. (setq file-alist (append (delq x file-alist) (list x)))
  15867. (setq file-alist (cons x (delq x file-alist))))
  15868. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15869. (org-install-agenda-files-menu)
  15870. (message "File %s to %s of agenda file list"
  15871. (if had "moved" "added") (if to-end "end" "front"))))
  15872. (defun org-remove-file (&optional file)
  15873. "Remove current file from the list of files in variable `org-agenda-files'.
  15874. These are the files which are being checked for agenda entries.
  15875. Optional argument FILE means use this file instead of the current."
  15876. (interactive)
  15877. (let* ((org-agenda-skip-unavailable-files nil)
  15878. (file (or file buffer-file-name
  15879. (error "Current buffer does not visit a file")))
  15880. (true-file (file-truename file))
  15881. (afile (abbreviate-file-name file))
  15882. (files (delq nil (mapcar
  15883. (lambda (x)
  15884. (if (equal true-file
  15885. (file-truename x))
  15886. nil x))
  15887. (org-agenda-files t)))))
  15888. (if (not (= (length files) (length (org-agenda-files t))))
  15889. (progn
  15890. (org-store-new-agenda-file-list files)
  15891. (org-install-agenda-files-menu)
  15892. (message "Removed file: %s" afile))
  15893. (message "File was not in list: %s (not removed)" afile))))
  15894. (defun org-file-menu-entry (file)
  15895. (vector file (list 'find-file file) t))
  15896. (defun org-check-agenda-file (file)
  15897. "Make sure FILE exists. If not, ask user what to do."
  15898. (when (not (file-exists-p file))
  15899. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15900. (abbreviate-file-name file))
  15901. (let ((r (downcase (read-char-exclusive))))
  15902. (cond
  15903. ((equal r ?r)
  15904. (org-remove-file file)
  15905. (throw 'nextfile t))
  15906. (t (error "Abort"))))))
  15907. (defun org-get-agenda-file-buffer (file)
  15908. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15909. it to the list of buffers which might be released later."
  15910. (let ((buf (org-find-base-buffer-visiting file)))
  15911. (if buf
  15912. buf ; just return it
  15913. ;; Make a new buffer and remember it
  15914. (setq buf (find-file-noselect file))
  15915. (if buf (push buf org-agenda-new-buffers))
  15916. buf)))
  15917. (defun org-release-buffers (blist)
  15918. "Release all buffers in list, asking the user for confirmation when needed.
  15919. When a buffer is unmodified, it is just killed. When modified, it is saved
  15920. \(if the user agrees) and then killed."
  15921. (let (buf file)
  15922. (while (setq buf (pop blist))
  15923. (setq file (buffer-file-name buf))
  15924. (when (and (buffer-modified-p buf)
  15925. file
  15926. (y-or-n-p (format "Save file %s? " file)))
  15927. (with-current-buffer buf (save-buffer)))
  15928. (kill-buffer buf))))
  15929. (defun org-agenda-prepare-buffers (files)
  15930. "Create buffers for all agenda files, protect archived trees and comments."
  15931. (interactive)
  15932. (let ((pa '(:org-archived t))
  15933. (pc '(:org-comment t))
  15934. (pall '(:org-archived t :org-comment t))
  15935. (inhibit-read-only t)
  15936. (rea (concat ":" org-archive-tag ":"))
  15937. bmp file re)
  15938. (save-excursion
  15939. (save-restriction
  15940. (while (setq file (pop files))
  15941. (catch 'nextfile
  15942. (if (bufferp file)
  15943. (set-buffer file)
  15944. (org-check-agenda-file file)
  15945. (set-buffer (org-get-agenda-file-buffer file)))
  15946. (widen)
  15947. (setq bmp (buffer-modified-p))
  15948. (org-refresh-category-properties)
  15949. (org-refresh-properties org-effort-property 'org-effort)
  15950. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  15951. (setq org-todo-keywords-for-agenda
  15952. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15953. (setq org-done-keywords-for-agenda
  15954. (append org-done-keywords-for-agenda org-done-keywords))
  15955. (setq org-todo-keyword-alist-for-agenda
  15956. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15957. (setq org-drawers-for-agenda
  15958. (append org-drawers-for-agenda org-drawers))
  15959. (setq org-tag-alist-for-agenda
  15960. (append org-tag-alist-for-agenda org-tag-alist))
  15961. (save-excursion
  15962. (remove-text-properties (point-min) (point-max) pall)
  15963. (when org-agenda-skip-archived-trees
  15964. (goto-char (point-min))
  15965. (while (re-search-forward rea nil t)
  15966. (if (org-at-heading-p t)
  15967. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15968. (goto-char (point-min))
  15969. (setq re (format org-heading-keyword-regexp-format
  15970. org-comment-string))
  15971. (while (re-search-forward re nil t)
  15972. (add-text-properties
  15973. (match-beginning 0) (org-end-of-subtree t) pc)))
  15974. (set-buffer-modified-p bmp)))))
  15975. (setq org-todo-keywords-for-agenda
  15976. (org-uniquify org-todo-keywords-for-agenda))
  15977. (setq org-todo-keyword-alist-for-agenda
  15978. (org-uniquify org-todo-keyword-alist-for-agenda)
  15979. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15980. ;;;; Embedded LaTeX
  15981. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15982. "Keymap for the minor `org-cdlatex-mode'.")
  15983. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15984. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15985. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15986. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15987. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15988. (defvar org-cdlatex-texmathp-advice-is-done nil
  15989. "Flag remembering if we have applied the advice to texmathp already.")
  15990. (define-minor-mode org-cdlatex-mode
  15991. "Toggle the minor `org-cdlatex-mode'.
  15992. This mode supports entering LaTeX environment and math in LaTeX fragments
  15993. in Org-mode.
  15994. \\{org-cdlatex-mode-map}"
  15995. nil " OCDL" nil
  15996. (when org-cdlatex-mode
  15997. (require 'cdlatex)
  15998. (run-hooks 'cdlatex-mode-hook)
  15999. (cdlatex-compute-tables))
  16000. (unless org-cdlatex-texmathp-advice-is-done
  16001. (setq org-cdlatex-texmathp-advice-is-done t)
  16002. (defadvice texmathp (around org-math-always-on activate)
  16003. "Always return t in org-mode buffers.
  16004. This is because we want to insert math symbols without dollars even outside
  16005. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16006. an embedded LaTeX fragment, let texmathp do its job.
  16007. \\[org-cdlatex-mode-map]"
  16008. (interactive)
  16009. (let (p)
  16010. (cond
  16011. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16012. ((eq this-command 'cdlatex-math-symbol)
  16013. (setq ad-return-value t
  16014. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16015. (t
  16016. (let ((p (org-inside-LaTeX-fragment-p)))
  16017. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16018. (setq ad-return-value t
  16019. texmathp-why '("Org-mode embedded math" . 0))
  16020. (if p ad-do-it)))))))))
  16021. (defun turn-on-org-cdlatex ()
  16022. "Unconditionally turn on `org-cdlatex-mode'."
  16023. (org-cdlatex-mode 1))
  16024. (defun org-inside-LaTeX-fragment-p ()
  16025. "Test if point is inside a LaTeX fragment.
  16026. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16027. sequence appearing also before point.
  16028. Even though the matchers for math are configurable, this function assumes
  16029. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16030. delimiters are skipped when they have been removed by customization.
  16031. The return value is nil, or a cons cell with the delimiter and the
  16032. position of this delimiter.
  16033. This function does a reasonably good job, but can locally be fooled by
  16034. for example currency specifications. For example it will assume being in
  16035. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16036. fragments that are properly closed, but during editing, we have to live
  16037. with the uncertainty caused by missing closing delimiters. This function
  16038. looks only before point, not after."
  16039. (catch 'exit
  16040. (let ((pos (point))
  16041. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16042. (lim (progn
  16043. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16044. (point)))
  16045. dd-on str (start 0) m re)
  16046. (goto-char pos)
  16047. (when dodollar
  16048. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16049. re (nth 1 (assoc "$" org-latex-regexps)))
  16050. (while (string-match re str start)
  16051. (cond
  16052. ((= (match-end 0) (length str))
  16053. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16054. ((= (match-end 0) (- (length str) 5))
  16055. (throw 'exit nil))
  16056. (t (setq start (match-end 0))))))
  16057. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16058. (goto-char pos)
  16059. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16060. (and (match-beginning 2) (throw 'exit nil))
  16061. ;; count $$
  16062. (while (re-search-backward "\\$\\$" lim t)
  16063. (setq dd-on (not dd-on)))
  16064. (goto-char pos)
  16065. (if dd-on (cons "$$" m))))))
  16066. (defun org-inside-latex-macro-p ()
  16067. "Is point inside a LaTeX macro or its arguments?"
  16068. (save-match-data
  16069. (org-in-regexp
  16070. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16071. (defun org-try-cdlatex-tab ()
  16072. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16073. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16074. - inside a LaTeX fragment, or
  16075. - after the first word in a line, where an abbreviation expansion could
  16076. insert a LaTeX environment."
  16077. (when org-cdlatex-mode
  16078. (cond
  16079. ;; Before any word on the line: No expansion possible.
  16080. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16081. ;; Just after first word on the line: Expand it. Make sure it
  16082. ;; cannot happen on headlines, though.
  16083. ((save-excursion
  16084. (skip-chars-backward "a-zA-Z0-9*")
  16085. (skip-chars-backward " \t")
  16086. (and (bolp) (not (org-at-heading-p))))
  16087. (cdlatex-tab) t)
  16088. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16089. (defun org-cdlatex-underscore-caret (&optional arg)
  16090. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16091. Revert to the normal definition outside of these fragments."
  16092. (interactive "P")
  16093. (if (org-inside-LaTeX-fragment-p)
  16094. (call-interactively 'cdlatex-sub-superscript)
  16095. (let (org-cdlatex-mode)
  16096. (call-interactively (key-binding (vector last-input-event))))))
  16097. (defun org-cdlatex-math-modify (&optional arg)
  16098. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16099. Revert to the normal definition outside of these fragments."
  16100. (interactive "P")
  16101. (if (org-inside-LaTeX-fragment-p)
  16102. (call-interactively 'cdlatex-math-modify)
  16103. (let (org-cdlatex-mode)
  16104. (call-interactively (key-binding (vector last-input-event))))))
  16105. (defvar org-latex-fragment-image-overlays nil
  16106. "List of overlays carrying the images of latex fragments.")
  16107. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16108. (defun org-remove-latex-fragment-image-overlays ()
  16109. "Remove all overlays with LaTeX fragment images in current buffer."
  16110. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16111. (setq org-latex-fragment-image-overlays nil))
  16112. (defun org-preview-latex-fragment (&optional subtree)
  16113. "Preview the LaTeX fragment at point, or all locally or globally.
  16114. If the cursor is in a LaTeX fragment, create the image and overlay
  16115. it over the source code. If there is no fragment at point, display
  16116. all fragments in the current text, from one headline to the next. With
  16117. prefix SUBTREE, display all fragments in the current subtree. With a
  16118. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16119. the cursor is before the first headline,
  16120. display all fragments in the buffer.
  16121. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16122. (interactive "P")
  16123. (unless buffer-file-name
  16124. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16125. (org-remove-latex-fragment-image-overlays)
  16126. (save-excursion
  16127. (save-restriction
  16128. (let (beg end at msg)
  16129. (cond
  16130. ((or (equal subtree '(16))
  16131. (not (save-excursion
  16132. (re-search-backward org-outline-regexp-bol nil t))))
  16133. (setq beg (point-min) end (point-max)
  16134. msg "Creating images for buffer...%s"))
  16135. ((equal subtree '(4))
  16136. (org-back-to-heading)
  16137. (setq beg (point) end (org-end-of-subtree t)
  16138. msg "Creating images for subtree...%s"))
  16139. (t
  16140. (if (setq at (org-inside-LaTeX-fragment-p))
  16141. (goto-char (max (point-min) (- (cdr at) 2)))
  16142. (org-back-to-heading))
  16143. (setq beg (point) end (progn (outline-next-heading) (point))
  16144. msg (if at "Creating image...%s"
  16145. "Creating images for entry...%s"))))
  16146. (message msg "")
  16147. (narrow-to-region beg end)
  16148. (goto-char beg)
  16149. (org-format-latex
  16150. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16151. (file-name-nondirectory
  16152. buffer-file-name)))
  16153. default-directory 'overlays msg at 'forbuffer
  16154. org-latex-create-formula-image-program)
  16155. (message msg "done. Use `C-c C-c' to remove images.")))))
  16156. (defvar org-latex-regexps
  16157. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16158. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16159. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16160. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16161. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16162. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16163. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16164. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16165. "Regular expressions for matching embedded LaTeX.")
  16166. (defvar org-export-have-math nil) ;; dynamic scoping
  16167. (defun org-format-latex (prefix &optional dir overlays msg at
  16168. forbuffer processing-type)
  16169. "Replace LaTeX fragments with links to an image, and produce images.
  16170. Some of the options can be changed using the variable
  16171. `org-format-latex-options'."
  16172. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16173. (let* ((prefixnodir (file-name-nondirectory prefix))
  16174. (absprefix (expand-file-name prefix dir))
  16175. (todir (file-name-directory absprefix))
  16176. (opt org-format-latex-options)
  16177. (optnew org-format-latex-options)
  16178. (matchers (plist-get opt :matchers))
  16179. (re-list org-latex-regexps)
  16180. (org-format-latex-header-extra
  16181. (plist-get (org-infile-export-plist) :latex-header-extra))
  16182. (cnt 0) txt hash link beg end re e checkdir
  16183. string
  16184. m n block-type block linkfile movefile ov)
  16185. ;; Check the different regular expressions
  16186. (while (setq e (pop re-list))
  16187. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16188. block (if block-type "\n\n" ""))
  16189. (when (member m matchers)
  16190. (goto-char (point-min))
  16191. (while (re-search-forward re nil t)
  16192. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16193. (not (get-text-property (match-beginning n)
  16194. 'org-protected))
  16195. (or (not overlays)
  16196. (not (eq (get-char-property (match-beginning n)
  16197. 'org-overlay-type)
  16198. 'org-latex-overlay))))
  16199. (setq org-export-have-math t)
  16200. (cond
  16201. ((eq processing-type 'verbatim)
  16202. ;; Leave the text verbatim, just protect it
  16203. (add-text-properties (match-beginning n) (match-end n)
  16204. '(org-protected t)))
  16205. ((eq processing-type 'mathjax)
  16206. ;; Prepare for MathJax processing
  16207. (setq string (match-string n))
  16208. (if (member m '("$" "$1"))
  16209. (save-excursion
  16210. (delete-region (match-beginning n) (match-end n))
  16211. (goto-char (match-beginning n))
  16212. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  16213. "\\)")
  16214. '(org-protected t))))
  16215. (add-text-properties (match-beginning n) (match-end n)
  16216. '(org-protected t))))
  16217. ((or (eq processing-type 'dvipng)
  16218. (eq processing-type 'imagemagick))
  16219. ;; Process to an image
  16220. (setq txt (match-string n)
  16221. beg (match-beginning n) end (match-end n)
  16222. cnt (1+ cnt))
  16223. (let ((face (face-at-point))
  16224. (fg (plist-get opt :foreground))
  16225. (bg (plist-get opt :background))
  16226. print-length print-level) ; make sure full list is printed
  16227. (when forbuffer
  16228. ; Get the colors from the face at point
  16229. (goto-char beg)
  16230. (when (eq fg 'auto)
  16231. (setq fg (face-attribute face :foreground nil 'default)))
  16232. (when (eq bg 'auto)
  16233. (setq bg (face-attribute face :background nil 'default)))
  16234. (setq optnew (copy-sequence opt))
  16235. (plist-put optnew :foreground fg)
  16236. (plist-put optnew :background bg))
  16237. (setq hash (sha1 (prin1-to-string
  16238. (list org-format-latex-header
  16239. org-format-latex-header-extra
  16240. org-export-latex-default-packages-alist
  16241. org-export-latex-packages-alist
  16242. org-format-latex-options
  16243. forbuffer txt fg bg)))
  16244. linkfile (format "%s_%s.png" prefix hash)
  16245. movefile (format "%s_%s.png" absprefix hash)))
  16246. (setq link (concat block "[[file:" linkfile "]]" block))
  16247. (if msg (message msg cnt))
  16248. (goto-char beg)
  16249. (unless checkdir ; make sure the directory exists
  16250. (setq checkdir t)
  16251. (or (file-directory-p todir) (make-directory todir t)))
  16252. (unless (file-exists-p movefile)
  16253. (org-create-formula-image
  16254. txt movefile optnew forbuffer processing-type))
  16255. (if overlays
  16256. (progn
  16257. (mapc (lambda (o)
  16258. (if (eq (overlay-get o 'org-overlay-type)
  16259. 'org-latex-overlay)
  16260. (delete-overlay o)))
  16261. (overlays-in beg end))
  16262. (setq ov (make-overlay beg end))
  16263. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16264. (if (featurep 'xemacs)
  16265. (progn
  16266. (overlay-put ov 'invisible t)
  16267. (overlay-put
  16268. ov 'end-glyph
  16269. (make-glyph (vector 'png :file movefile))))
  16270. (overlay-put
  16271. ov 'display
  16272. (list 'image :type 'png :file movefile :ascent 'center)))
  16273. (push ov org-latex-fragment-image-overlays)
  16274. (goto-char end))
  16275. (delete-region beg end)
  16276. (insert (org-add-props link
  16277. (list 'org-latex-src
  16278. (replace-regexp-in-string
  16279. "\"" "" txt)
  16280. 'org-latex-src-embed-type
  16281. (if block-type 'paragraph 'character))))))
  16282. ((eq processing-type 'mathml)
  16283. ;; Process to MathML
  16284. (unless (save-match-data (org-format-latex-mathml-available-p))
  16285. (error "LaTeX to MathML converter not configured"))
  16286. (setq txt (match-string n)
  16287. beg (match-beginning n) end (match-end n)
  16288. cnt (1+ cnt))
  16289. (if msg (message msg cnt))
  16290. (goto-char beg)
  16291. (delete-region beg end)
  16292. (insert (org-format-latex-as-mathml
  16293. txt block-type prefix dir)))
  16294. (t
  16295. (error "Unknown conversion type %s for latex fragments"
  16296. processing-type)))))))))
  16297. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16298. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16299. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16300. sucessful, return the portion between \"<math...> </math>\"
  16301. elements otherwise return nil. When MATHML-FILE is specified,
  16302. write the results in to that file. When invoked as an
  16303. interactive command, prompt for LATEX-FRAG, with initial value
  16304. set to the current active region and echo the results for user
  16305. inspection."
  16306. (interactive (list (let ((frag (when (org-region-active-p)
  16307. (buffer-substring-no-properties
  16308. (region-beginning) (region-end)))))
  16309. (read-string "LaTeX Fragment: " frag nil frag))))
  16310. (unless latex-frag (error "Invalid latex-frag"))
  16311. (let* ((tmp-in-file (file-relative-name
  16312. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16313. (ignore (write-region latex-frag nil tmp-in-file))
  16314. (tmp-out-file (file-relative-name
  16315. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16316. (cmd (format-spec
  16317. org-latex-to-mathml-convert-command
  16318. `((?j . ,(shell-quote-argument
  16319. (expand-file-name org-latex-to-mathml-jar-file)))
  16320. (?I . ,(shell-quote-argument tmp-in-file))
  16321. (?o . ,(shell-quote-argument tmp-out-file)))))
  16322. mathml shell-command-output)
  16323. (when (org-called-interactively-p 'any)
  16324. (unless (org-format-latex-mathml-available-p)
  16325. (error "LaTeX to MathML converter not configured")))
  16326. (message "Running %s" cmd)
  16327. (setq shell-command-output (shell-command-to-string cmd))
  16328. (setq mathml
  16329. (when (file-readable-p tmp-out-file)
  16330. (with-current-buffer (find-file-noselect tmp-out-file t)
  16331. (goto-char (point-min))
  16332. (when (re-search-forward
  16333. (concat
  16334. (regexp-quote
  16335. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16336. "\\(.\\|\n\\)*"
  16337. (regexp-quote "</math>")) nil t)
  16338. (prog1 (match-string 0) (kill-buffer))))))
  16339. (cond
  16340. (mathml
  16341. (setq mathml
  16342. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16343. (when mathml-file
  16344. (write-region mathml nil mathml-file))
  16345. (when (org-called-interactively-p 'any)
  16346. (message mathml)))
  16347. ((message "LaTeX to MathML conversion failed")
  16348. (message shell-command-output)))
  16349. (delete-file tmp-in-file)
  16350. (when (file-exists-p tmp-out-file)
  16351. (delete-file tmp-out-file))
  16352. mathml))
  16353. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16354. prefix &optional dir)
  16355. "Use `org-create-math-formula' but check local cache first."
  16356. (let* ((absprefix (expand-file-name prefix dir))
  16357. (print-length nil) (print-level nil)
  16358. (formula-id (concat
  16359. "formula-"
  16360. (sha1
  16361. (prin1-to-string
  16362. (list latex-frag
  16363. org-latex-to-mathml-convert-command)))))
  16364. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16365. (formula-cache-dir (file-name-directory formula-cache)))
  16366. (unless (file-directory-p formula-cache-dir)
  16367. (make-directory formula-cache-dir t))
  16368. (unless (file-exists-p formula-cache)
  16369. (org-create-math-formula latex-frag formula-cache))
  16370. (if (file-exists-p formula-cache)
  16371. ;; Successful conversion. Return the link to MathML file.
  16372. (org-add-props
  16373. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16374. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16375. 'org-latex-src-embed-type (if latex-frag-type
  16376. 'paragraph 'character)))
  16377. ;; Failed conversion. Return the LaTeX fragment verbatim
  16378. (add-text-properties
  16379. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16380. latex-frag)))
  16381. (defun org-create-formula-image (string tofile options buffer &optional type)
  16382. "Create an image from LaTeX source using dvipng or convert.
  16383. This function calls either `org-create-formula-image-with-dvipng'
  16384. or `org-create-formula-image-with-imagemagick' depending on the
  16385. value of `org-latex-create-formula-image-program' or on the value
  16386. of the optional TYPE variable.
  16387. Note: ultimately these two function should be combined as they
  16388. share a good deal of logic."
  16389. (org-check-external-command
  16390. "latex" "needed to convert LaTeX fragments to images")
  16391. (funcall
  16392. (case (or type org-latex-create-formula-image-program)
  16393. ('dvipng
  16394. (org-check-external-command
  16395. "dvipng" "needed to convert LaTeX fragments to images")
  16396. #'org-create-formula-image-with-dvipng)
  16397. ('imagemagick
  16398. (org-check-external-command
  16399. "convert" "you need to install imagemagick")
  16400. #'org-create-formula-image-with-imagemagick)
  16401. (t (error
  16402. "invalid value of `org-latex-create-formula-image-program'")))
  16403. string tofile options buffer))
  16404. ;; This function borrows from Ganesh Swami's latex2png.el
  16405. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16406. "This calls dvipng."
  16407. (require 'org-latex)
  16408. (let* ((tmpdir (if (featurep 'xemacs)
  16409. (temp-directory)
  16410. temporary-file-directory))
  16411. (texfilebase (make-temp-name
  16412. (expand-file-name "orgtex" tmpdir)))
  16413. (texfile (concat texfilebase ".tex"))
  16414. (dvifile (concat texfilebase ".dvi"))
  16415. (pngfile (concat texfilebase ".png"))
  16416. (fnh (if (featurep 'xemacs)
  16417. (font-height (face-font 'default))
  16418. (face-attribute 'default :height nil)))
  16419. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16420. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16421. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16422. "Black"))
  16423. (bg (or (plist-get options (if buffer :background :html-background))
  16424. "Transparent")))
  16425. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16426. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16427. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16428. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16429. (with-temp-file texfile
  16430. (insert (org-splice-latex-header
  16431. org-format-latex-header
  16432. org-export-latex-default-packages-alist
  16433. org-export-latex-packages-alist t
  16434. org-format-latex-header-extra))
  16435. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16436. (require 'org-latex)
  16437. (org-export-latex-fix-inputenc))
  16438. (let ((dir default-directory))
  16439. (condition-case nil
  16440. (progn
  16441. (cd tmpdir)
  16442. (call-process "latex" nil nil nil texfile))
  16443. (error nil))
  16444. (cd dir))
  16445. (if (not (file-exists-p dvifile))
  16446. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16447. (condition-case nil
  16448. (if (featurep 'xemacs)
  16449. (call-process "dvipng" nil nil nil
  16450. "-fg" fg "-bg" bg
  16451. "-T" "tight"
  16452. "-o" pngfile
  16453. dvifile)
  16454. (call-process "dvipng" nil nil nil
  16455. "-fg" fg "-bg" bg
  16456. "-D" dpi
  16457. ;;"-x" scale "-y" scale
  16458. "-T" "tight"
  16459. "-o" pngfile
  16460. dvifile))
  16461. (error nil))
  16462. (if (not (file-exists-p pngfile))
  16463. (if org-format-latex-signal-error
  16464. (error "Failed to create png file from %s" texfile)
  16465. (message "Failed to create png file from %s" texfile)
  16466. nil)
  16467. ;; Use the requested file name and clean up
  16468. (copy-file pngfile tofile 'replace)
  16469. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16470. (if (file-exists-p (concat texfilebase e))
  16471. (delete-file (concat texfilebase e))))
  16472. pngfile))))
  16473. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16474. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16475. "This calls convert, which is included into imagemagick."
  16476. (require 'org-latex)
  16477. (let* ((tmpdir (if (featurep 'xemacs)
  16478. (temp-directory)
  16479. temporary-file-directory))
  16480. (texfilebase (make-temp-name
  16481. (expand-file-name "orgtex" tmpdir)))
  16482. (texfile (concat texfilebase ".tex"))
  16483. (pdffile (concat texfilebase ".pdf"))
  16484. (pngfile (concat texfilebase ".png"))
  16485. (fnh (if (featurep 'xemacs)
  16486. (font-height (face-font 'default))
  16487. (face-attribute 'default :height nil)))
  16488. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16489. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16490. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16491. "black"))
  16492. (bg (or (plist-get options (if buffer :background :html-background))
  16493. "white")))
  16494. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16495. (setq fg (org-latex-color-format fg)))
  16496. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16497. (setq bg (org-latex-color-format
  16498. (if (string= bg "Transparent") "white" bg))))
  16499. (with-temp-file texfile
  16500. (insert (org-splice-latex-header
  16501. org-format-latex-header
  16502. org-export-latex-default-packages-alist
  16503. org-export-latex-packages-alist t
  16504. org-format-latex-header-extra))
  16505. (insert "\n\\begin{document}\n"
  16506. "\\definecolor{fg}{rgb}{" fg "}\n"
  16507. "\\definecolor{bg}{rgb}{" bg "}\n"
  16508. "\n\\pagecolor{bg}\n"
  16509. "\n{\\color{fg}\n"
  16510. string
  16511. "\n}\n"
  16512. "\n\\end{document}\n" )
  16513. (require 'org-latex)
  16514. (org-export-latex-fix-inputenc))
  16515. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16516. (condition-case nil
  16517. (progn
  16518. (cd tmpdir)
  16519. (setq cmds org-latex-to-pdf-process)
  16520. (while cmds
  16521. (setq latex-frags-cmds (pop cmds))
  16522. (if (listp latex-frags-cmds)
  16523. (setq cmds nil)
  16524. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16525. (while latex-frags-cmds
  16526. (setq cmd (pop latex-frags-cmds))
  16527. (while (string-match "%b" cmd)
  16528. (setq cmd (replace-match
  16529. (save-match-data
  16530. (shell-quote-argument texfile))
  16531. t t cmd)))
  16532. (while (string-match "%f" cmd)
  16533. (setq cmd (replace-match
  16534. (save-match-data
  16535. (shell-quote-argument (file-name-nondirectory texfile)))
  16536. t t cmd)))
  16537. (while (string-match "%o" cmd)
  16538. (setq cmd (replace-match
  16539. (save-match-data
  16540. (shell-quote-argument (file-name-directory texfile)))
  16541. t t cmd)))
  16542. (setq cmd (split-string cmd))
  16543. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16544. (error nil))
  16545. (cd dir))
  16546. (if (not (file-exists-p pdffile))
  16547. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16548. (condition-case nil
  16549. (if (featurep 'xemacs)
  16550. (call-process "convert" nil nil nil
  16551. "-density" "96"
  16552. "-trim"
  16553. "-antialias"
  16554. pdffile
  16555. "-quality" "100"
  16556. ;; "-sharpen" "0x1.0"
  16557. pngfile)
  16558. (call-process "convert" nil nil nil
  16559. "-density" dpi
  16560. "-trim"
  16561. "-antialias"
  16562. pdffile
  16563. "-quality" "100"
  16564. ; "-sharpen" "0x1.0"
  16565. pngfile))
  16566. (error nil))
  16567. (if (not (file-exists-p pngfile))
  16568. (if org-format-latex-signal-error
  16569. (error "Failed to create png file from %s" texfile)
  16570. (message "Failed to create png file from %s" texfile)
  16571. nil)
  16572. ;; Use the requested file name and clean up
  16573. (copy-file pngfile tofile 'replace)
  16574. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16575. (if (file-exists-p (concat texfilebase e))
  16576. (delete-file (concat texfilebase e))))
  16577. pngfile))))
  16578. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16579. "Fill a LaTeX header template TPL.
  16580. In the template, the following place holders will be recognized:
  16581. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16582. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16583. [PACKAGES] \\usepackage statements for PKG
  16584. [NO-PACKAGES] do not include PKG
  16585. [EXTRA] the string EXTRA
  16586. [NO-EXTRA] do not include EXTRA
  16587. For backward compatibility, if both the positive and the negative place
  16588. holder is missing, the positive one (without the \"NO-\") will be
  16589. assumed to be present at the end of the template.
  16590. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16591. EXTRA is a string.
  16592. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16593. (let (rpl (end ""))
  16594. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16595. (setq rpl (if (or (match-end 1) (not def-pkg))
  16596. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16597. tpl (replace-match rpl t t tpl))
  16598. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16599. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16600. (setq rpl (if (or (match-end 1) (not pkg))
  16601. "" (org-latex-packages-to-string pkg snippets-p t))
  16602. tpl (replace-match rpl t t tpl))
  16603. (if pkg (setq end
  16604. (concat end "\n"
  16605. (org-latex-packages-to-string pkg snippets-p)))))
  16606. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16607. (setq rpl (if (or (match-end 1) (not extra))
  16608. "" (concat extra "\n"))
  16609. tpl (replace-match rpl t t tpl))
  16610. (if (and extra (string-match "\\S-" extra))
  16611. (setq end (concat end "\n" extra))))
  16612. (if (string-match "\\S-" end)
  16613. (concat tpl "\n" end)
  16614. tpl)))
  16615. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16616. "Turn an alist of packages into a string with the \\usepackage macros."
  16617. (setq pkg (mapconcat (lambda(p)
  16618. (cond
  16619. ((stringp p) p)
  16620. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16621. (format "%% Package %s omitted" (cadr p)))
  16622. ((equal "" (car p))
  16623. (format "\\usepackage{%s}" (cadr p)))
  16624. (t
  16625. (format "\\usepackage[%s]{%s}"
  16626. (car p) (cadr p)))))
  16627. pkg
  16628. "\n"))
  16629. (if newline (concat pkg "\n") pkg))
  16630. (defun org-dvipng-color (attr)
  16631. "Return a RGB color specification for dvipng."
  16632. (apply 'format "rgb %s %s %s"
  16633. (mapcar 'org-normalize-color
  16634. (if (featurep 'xemacs)
  16635. (color-rgb-components
  16636. (face-property 'default
  16637. (cond ((eq attr :foreground) 'foreground)
  16638. ((eq attr :background) 'background))))
  16639. (color-values (face-attribute 'default attr nil))))))
  16640. (defun org-dvipng-color-format (color-name)
  16641. "Convert COLOR-NAME to a RGB color value for dvipng."
  16642. (apply 'format "rgb %s %s %s"
  16643. (mapcar 'org-normalize-color
  16644. (color-values color-name))))
  16645. (defun org-latex-color (attr)
  16646. "Return a RGB color for the LaTeX color package."
  16647. (apply 'format "%s,%s,%s"
  16648. (mapcar 'org-normalize-color
  16649. (if (featurep 'xemacs)
  16650. (color-rgb-components
  16651. (face-property 'default
  16652. (cond ((eq attr :foreground) 'foreground)
  16653. ((eq attr :background) 'background))))
  16654. (color-values (face-attribute 'default attr nil))))))
  16655. (defun org-latex-color-format (color-name)
  16656. "Convert COLOR-NAME to a RGB color value."
  16657. (apply 'format "%s,%s,%s"
  16658. (mapcar 'org-normalize-color
  16659. (color-values color-name))))
  16660. (defun org-normalize-color (value)
  16661. "Return string to be used as color value for an RGB component."
  16662. (format "%g" (/ value 65535.0)))
  16663. ;; Image display
  16664. (defvar org-inline-image-overlays nil)
  16665. (make-variable-buffer-local 'org-inline-image-overlays)
  16666. (defun org-toggle-inline-images (&optional include-linked)
  16667. "Toggle the display of inline images.
  16668. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16669. (interactive "P")
  16670. (if org-inline-image-overlays
  16671. (progn
  16672. (org-remove-inline-images)
  16673. (message "Inline image display turned off"))
  16674. (org-display-inline-images include-linked)
  16675. (if (and (org-called-interactively-p)
  16676. org-inline-image-overlays)
  16677. (message "%d images displayed inline"
  16678. (length org-inline-image-overlays))
  16679. (message "No images to display inline"))))
  16680. (defun org-redisplay-inline-images ()
  16681. "Refresh the display of inline images."
  16682. (interactive)
  16683. (if (not org-inline-image-overlays)
  16684. (org-toggle-inline-images)
  16685. (org-toggle-inline-images)
  16686. (org-toggle-inline-images)))
  16687. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16688. "Display inline images.
  16689. Normally only links without a description part are inlined, because this
  16690. is how it will work for export. When INCLUDE-LINKED is set, also links
  16691. with a description part will be inlined. This can be nice for a quick
  16692. look at those images, but it does not reflect what exported files will look
  16693. like.
  16694. When REFRESH is set, refresh existing images between BEG and END.
  16695. This will create new image displays only if necessary.
  16696. BEG and END default to the buffer boundaries."
  16697. (interactive "P")
  16698. (unless refresh
  16699. (org-remove-inline-images)
  16700. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16701. (save-excursion
  16702. (save-restriction
  16703. (widen)
  16704. (setq beg (or beg (point-min)) end (or end (point-max)))
  16705. (goto-char beg)
  16706. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16707. (substring (org-image-file-name-regexp) 0 -2)
  16708. "\\)\\]" (if include-linked "" "\\]")))
  16709. old file ov img type attrwidth width)
  16710. (while (re-search-forward re end t)
  16711. (setq old (get-char-property-and-overlay (match-beginning 1)
  16712. 'org-image-overlay)
  16713. file (expand-file-name
  16714. (concat (or (match-string 3) "") (match-string 4))))
  16715. (when (image-type-available-p 'imagemagick)
  16716. (setq attrwidth (if (or (listp org-image-actual-width)
  16717. (null org-image-actual-width))
  16718. (save-excursion
  16719. (save-match-data
  16720. (when (re-search-backward
  16721. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16722. (save-excursion
  16723. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16724. (string-to-number (match-string 1))))))
  16725. width (cond ((eq org-image-actual-width t) nil)
  16726. ((null org-image-actual-width) attrwidth)
  16727. ((numberp org-image-actual-width)
  16728. org-image-actual-width)
  16729. ((listp org-image-actual-width)
  16730. (or attrwidth (car org-image-actual-width))))
  16731. type (if width 'imagemagick)))
  16732. (when (file-exists-p file)
  16733. (if (and (car-safe old) refresh)
  16734. (image-refresh (overlay-get (cdr old) 'display))
  16735. (setq img (save-match-data (create-image file type nil :width width)))
  16736. (when img
  16737. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16738. (overlay-put ov 'display img)
  16739. (overlay-put ov 'face 'default)
  16740. (overlay-put ov 'org-image-overlay t)
  16741. (overlay-put ov 'modification-hooks
  16742. (list 'org-display-inline-remove-overlay))
  16743. (push ov org-inline-image-overlays)))))))))
  16744. (org-define-obsolete-function-alias
  16745. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16746. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16747. "Remove inline-display overlay if a corresponding region is modified."
  16748. (let ((inhibit-modification-hooks t))
  16749. (when (and ov after)
  16750. (delete ov org-inline-image-overlays)
  16751. (delete-overlay ov))))
  16752. (defun org-remove-inline-images ()
  16753. "Remove inline display of images."
  16754. (interactive)
  16755. (mapc 'delete-overlay org-inline-image-overlays)
  16756. (setq org-inline-image-overlays nil))
  16757. ;;;; Key bindings
  16758. ;; Outline functions from `outline-mode-prefix-map'
  16759. ;; that can be remapped in Org:
  16760. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16761. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16762. (define-key org-mode-map [remap outline-forward-same-level]
  16763. 'org-forward-heading-same-level)
  16764. (define-key org-mode-map [remap outline-backward-same-level]
  16765. 'org-backward-heading-same-level)
  16766. (define-key org-mode-map [remap show-branches]
  16767. 'org-kill-note-or-show-branches)
  16768. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16769. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16770. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16771. ;; Outline functions from `outline-mode-prefix-map' that can not
  16772. ;; be remapped in Org:
  16773. ;;
  16774. ;; - the column "key binding" shows whether the Outline function is still
  16775. ;; available in Org mode on the same key that it has been bound to in
  16776. ;; Outline mode:
  16777. ;; - "overridden": key used for a different functionality in Org mode
  16778. ;; - else: key still bound to the same Outline function in Org mode
  16779. ;;
  16780. ;; | Outline function | key binding | Org replacement |
  16781. ;; |------------------------------------+-------------+-----------------------|
  16782. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16783. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16784. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16785. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16786. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16787. ;; | `show-entry' | overridden | no replacement |
  16788. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16789. ;; | `show-branches' | `C-c C-k' | still same function |
  16790. ;; | `show-subtree' | overridden | visibility cycling |
  16791. ;; | `show-all' | overridden | no replacement |
  16792. ;; | `hide-subtree' | overridden | visibility cycling |
  16793. ;; | `hide-body' | overridden | no replacement |
  16794. ;; | `hide-entry' | overridden | visibility cycling |
  16795. ;; | `hide-leaves' | overridden | no replacement |
  16796. ;; | `hide-sublevels' | overridden | no replacement |
  16797. ;; | `hide-other' | overridden | no replacement |
  16798. ;; Make `C-c C-x' a prefix key
  16799. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16800. ;; TAB key with modifiers
  16801. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16802. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16803. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16804. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16805. ;; The following line is necessary under Suse GNU/Linux
  16806. (unless (featurep 'xemacs)
  16807. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16808. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16809. (define-key org-mode-map [backtab] 'org-shifttab)
  16810. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16811. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16812. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16813. ;; Cursor keys with modifiers
  16814. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16815. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16816. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16817. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16818. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16819. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16820. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16821. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16822. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16823. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16824. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16825. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16826. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16827. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16828. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16829. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16830. ;; Babel keys
  16831. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16832. (mapc (lambda (pair)
  16833. (define-key org-babel-map (car pair) (cdr pair)))
  16834. org-babel-key-bindings)
  16835. ;;; Extra keys for tty access.
  16836. ;; We only set them when really needed because otherwise the
  16837. ;; menus don't show the simple keys
  16838. (when (or org-use-extra-keys
  16839. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16840. (not window-system))
  16841. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16842. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16843. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16844. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16845. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16846. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16847. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16848. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16849. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16850. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16851. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16852. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16853. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16854. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16855. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16856. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16857. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16858. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16859. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16860. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16861. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16862. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16863. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16864. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16865. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16866. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16867. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16868. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16869. ;; All the other keys
  16870. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16871. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16872. (if (boundp 'narrow-map)
  16873. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16874. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16875. (if (boundp 'narrow-map)
  16876. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16877. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16878. (if (boundp 'narrow-map)
  16879. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16880. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16881. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16882. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16883. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16884. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16885. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16886. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16887. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16888. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16889. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16890. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16891. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16892. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16893. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16894. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16895. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16896. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16897. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16898. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16899. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16900. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16901. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16902. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16903. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16904. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16905. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16906. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16907. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16908. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16909. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16910. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16911. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16912. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16913. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16914. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16915. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16916. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16917. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16918. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16919. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16920. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16921. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16922. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16923. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16924. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16925. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16926. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16927. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16928. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16929. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16930. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16931. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16932. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16933. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16934. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16935. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16936. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16937. (org-defkey org-mode-map "\C-m" 'org-return)
  16938. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16939. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16940. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16941. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16942. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16943. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16944. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16945. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16946. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16947. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16948. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16949. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16950. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16951. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16952. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16953. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16954. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16955. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16956. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16957. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16958. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16959. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16960. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16961. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16962. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16963. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16964. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16965. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16966. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16967. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16968. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16969. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16970. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16971. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16972. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16973. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16974. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16975. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16976. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16977. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16978. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16979. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16980. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  16981. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16982. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16983. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16984. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16985. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16986. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16987. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16988. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16989. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16990. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16991. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16992. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16993. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16994. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16995. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16996. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16997. (when (featurep 'xemacs)
  16998. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16999. (defconst org-speed-commands-default
  17000. '(
  17001. ("Outline Navigation")
  17002. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17003. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17004. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17005. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17006. ("u" . (org-speed-move-safe 'outline-up-heading))
  17007. ("j" . org-goto)
  17008. ("g" . (org-refile t))
  17009. ("Outline Visibility")
  17010. ("c" . org-cycle)
  17011. ("C" . org-shifttab)
  17012. (" " . org-display-outline-path)
  17013. ("=" . org-columns)
  17014. ("Outline Structure Editing")
  17015. ("U" . org-shiftmetaup)
  17016. ("D" . org-shiftmetadown)
  17017. ("r" . org-metaright)
  17018. ("l" . org-metaleft)
  17019. ("R" . org-shiftmetaright)
  17020. ("L" . org-shiftmetaleft)
  17021. ("i" . (progn (forward-char 1) (call-interactively
  17022. 'org-insert-heading-respect-content)))
  17023. ("^" . org-sort)
  17024. ("w" . org-refile)
  17025. ("a" . org-archive-subtree-default-with-confirmation)
  17026. ("." . org-mark-subtree)
  17027. ("#" . org-toggle-comment)
  17028. ("Clock Commands")
  17029. ("I" . org-clock-in)
  17030. ("O" . org-clock-out)
  17031. ("Meta Data Editing")
  17032. ("t" . org-todo)
  17033. ("," . (org-priority))
  17034. ("0" . (org-priority ?\ ))
  17035. ("1" . (org-priority ?A))
  17036. ("2" . (org-priority ?B))
  17037. ("3" . (org-priority ?C))
  17038. (":" . org-set-tags-command)
  17039. ("e" . org-set-effort)
  17040. ("E" . org-inc-effort)
  17041. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17042. (org-entry-put (point) "APPT_WARNTIME" m)))
  17043. ("Agenda Views etc")
  17044. ("v" . org-agenda)
  17045. ("/" . org-sparse-tree)
  17046. ("Misc")
  17047. ("o" . org-open-at-point)
  17048. ("?" . org-speed-command-help)
  17049. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17050. (">" . (org-agenda-remove-restriction-lock))
  17051. )
  17052. "The default speed commands.")
  17053. (defun org-print-speed-command (e)
  17054. (if (> (length (car e)) 1)
  17055. (progn
  17056. (princ "\n")
  17057. (princ (car e))
  17058. (princ "\n")
  17059. (princ (make-string (length (car e)) ?-))
  17060. (princ "\n"))
  17061. (princ (car e))
  17062. (princ " ")
  17063. (if (symbolp (cdr e))
  17064. (princ (symbol-name (cdr e)))
  17065. (prin1 (cdr e)))
  17066. (princ "\n")))
  17067. (defun org-speed-command-help ()
  17068. "Show the available speed commands."
  17069. (interactive)
  17070. (if (not org-use-speed-commands)
  17071. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17072. (with-output-to-temp-buffer "*Help*"
  17073. (princ "User-defined Speed commands\n===========================\n")
  17074. (mapc 'org-print-speed-command org-speed-commands-user)
  17075. (princ "\n")
  17076. (princ "Built-in Speed commands\n=======================\n")
  17077. (mapc 'org-print-speed-command org-speed-commands-default))
  17078. (with-current-buffer "*Help*"
  17079. (setq truncate-lines t))))
  17080. (defun org-speed-move-safe (cmd)
  17081. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17082. If not, return to the original position and throw an error."
  17083. (interactive)
  17084. (let ((pos (point)))
  17085. (call-interactively cmd)
  17086. (unless (and (bolp) (org-at-heading-p))
  17087. (goto-char pos)
  17088. (error "Boundary reached while executing %s" cmd))))
  17089. (defvar org-self-insert-command-undo-counter 0)
  17090. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17091. (defvar org-speed-command nil)
  17092. (org-define-obsolete-function-alias
  17093. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17094. (defun org-speed-command-activate (keys)
  17095. "Hook for activating single-letter speed commands.
  17096. `org-speed-commands-default' specifies a minimal command set.
  17097. Use `org-speed-commands-user' for further customization."
  17098. (when (or (and (bolp) (looking-at org-outline-regexp))
  17099. (and (functionp org-use-speed-commands)
  17100. (funcall org-use-speed-commands)))
  17101. (cdr (assoc keys (append org-speed-commands-user
  17102. org-speed-commands-default)))))
  17103. (org-define-obsolete-function-alias
  17104. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17105. (defun org-babel-speed-command-activate (keys)
  17106. "Hook for activating single-letter code block commands."
  17107. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17108. (cdr (assoc keys org-babel-key-bindings))))
  17109. (defcustom org-speed-command-hook
  17110. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17111. "Hook for activating speed commands at strategic locations.
  17112. Hook functions are called in sequence until a valid handler is
  17113. found.
  17114. Each hook takes a single argument, a user-pressed command key
  17115. which is also a `self-insert-command' from the global map.
  17116. Within the hook, examine the cursor position and the command key
  17117. and return nil or a valid handler as appropriate. Handler could
  17118. be one of an interactive command, a function, or a form.
  17119. Set `org-use-speed-commands' to non-nil value to enable this
  17120. hook. The default setting is `org-speed-command-activate'."
  17121. :group 'org-structure
  17122. :version "24.1"
  17123. :type 'hook)
  17124. (defun org-self-insert-command (N)
  17125. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17126. If the cursor is in a table looking at whitespace, the whitespace is
  17127. overwritten, and the table is not marked as requiring realignment."
  17128. (interactive "p")
  17129. (org-check-before-invisible-edit 'insert)
  17130. (cond
  17131. ((and org-use-speed-commands
  17132. (setq org-speed-command
  17133. (run-hook-with-args-until-success
  17134. 'org-speed-command-hook (this-command-keys))))
  17135. (cond
  17136. ((commandp org-speed-command)
  17137. (setq this-command org-speed-command)
  17138. (call-interactively org-speed-command))
  17139. ((functionp org-speed-command)
  17140. (funcall org-speed-command))
  17141. ((and org-speed-command (listp org-speed-command))
  17142. (eval org-speed-command))
  17143. (t (let (org-use-speed-commands)
  17144. (call-interactively 'org-self-insert-command)))))
  17145. ((and
  17146. (org-table-p)
  17147. (progn
  17148. ;; check if we blank the field, and if that triggers align
  17149. (and (featurep 'org-table) org-table-auto-blank-field
  17150. (member last-command
  17151. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17152. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17153. ;; got extra space, this field does not determine column width
  17154. (let (org-table-may-need-update) (org-table-blank-field))
  17155. ;; no extra space, this field may determine column width
  17156. (org-table-blank-field)))
  17157. t)
  17158. (eq N 1)
  17159. (looking-at "[^|\n]* |"))
  17160. (let (org-table-may-need-update)
  17161. (goto-char (1- (match-end 0)))
  17162. (backward-delete-char 1)
  17163. (goto-char (match-beginning 0))
  17164. (self-insert-command N)))
  17165. (t
  17166. (setq org-table-may-need-update t)
  17167. (self-insert-command N)
  17168. (org-fix-tags-on-the-fly)
  17169. (if org-self-insert-cluster-for-undo
  17170. (if (not (eq last-command 'org-self-insert-command))
  17171. (setq org-self-insert-command-undo-counter 1)
  17172. (if (>= org-self-insert-command-undo-counter 20)
  17173. (setq org-self-insert-command-undo-counter 1)
  17174. (and (> org-self-insert-command-undo-counter 0)
  17175. buffer-undo-list (listp buffer-undo-list)
  17176. (not (cadr buffer-undo-list)) ; remove nil entry
  17177. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17178. (setq org-self-insert-command-undo-counter
  17179. (1+ org-self-insert-command-undo-counter))))))))
  17180. (defun org-check-before-invisible-edit (kind)
  17181. "Check is editing if kind KIND would be dangerous with invisible text around.
  17182. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17183. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17184. (if (and org-catch-invisible-edits
  17185. (or (not (boundp 'visible-mode)) (not visible-mode))
  17186. (or (get-char-property (point) 'invisible)
  17187. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17188. ;; OK, we need to take a closer look
  17189. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17190. (invisible-before-point (if (bobp) nil (get-char-property
  17191. (1- (point)) 'invisible)))
  17192. (border-and-ok-direction
  17193. (or
  17194. ;; Check if we are acting predictably before invisible text
  17195. (and invisible-at-point (not invisible-before-point)
  17196. (memq kind '(insert delete-backward)))
  17197. ;; Check if we are acting predictably after invisible text
  17198. ;; This works not well, and I have turned it off. It seems
  17199. ;; better to always show and stop after invisible text.
  17200. ;; (and (not invisible-at-point) invisible-before-point
  17201. ;; (memq kind '(insert delete)))
  17202. )))
  17203. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17204. (memq invisible-before-point '(outline org-hide-block t)))
  17205. (if (eq org-catch-invisible-edits 'error)
  17206. (error "Editing in invisible areas is prohibited - make visible first"))
  17207. (if (and org-custom-properties-overlays
  17208. (y-or-n-p "Display invisible properties in this buffer? "))
  17209. (org-toggle-custom-properties-visibility)
  17210. ;; Make the area visible
  17211. (save-excursion
  17212. (if invisible-before-point
  17213. (goto-char (previous-single-char-property-change
  17214. (point) 'invisible)))
  17215. (org-cycle))
  17216. (cond
  17217. ((eq org-catch-invisible-edits 'show)
  17218. ;; That's it, we do the edit after showing
  17219. (message
  17220. "Unfolding invisible region around point before editing")
  17221. (sit-for 1))
  17222. ((and (eq org-catch-invisible-edits 'smart)
  17223. border-and-ok-direction)
  17224. (message "Unfolding invisible region around point before editing"))
  17225. (t
  17226. ;; Don't do the edit, make the user repeat it in full visibility
  17227. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17228. (defun org-fix-tags-on-the-fly ()
  17229. (when (and (equal (char-after (point-at-bol)) ?*)
  17230. (org-at-heading-p))
  17231. (org-align-tags-here org-tags-column)))
  17232. (defun org-delete-backward-char (N)
  17233. "Like `delete-backward-char', insert whitespace at field end in tables.
  17234. When deleting backwards, in tables this function will insert whitespace in
  17235. front of the next \"|\" separator, to keep the table aligned. The table will
  17236. still be marked for re-alignment if the field did fill the entire column,
  17237. because, in this case the deletion might narrow the column."
  17238. (interactive "p")
  17239. (save-match-data
  17240. (org-check-before-invisible-edit 'delete-backward)
  17241. (if (and (org-table-p)
  17242. (eq N 1)
  17243. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17244. (looking-at ".*?|"))
  17245. (let ((pos (point))
  17246. (noalign (looking-at "[^|\n\r]* |"))
  17247. (c org-table-may-need-update))
  17248. (backward-delete-char N)
  17249. (if (not overwrite-mode)
  17250. (progn
  17251. (skip-chars-forward "^|")
  17252. (insert " ")
  17253. (goto-char (1- pos))))
  17254. ;; noalign: if there were two spaces at the end, this field
  17255. ;; does not determine the width of the column.
  17256. (if noalign (setq org-table-may-need-update c)))
  17257. (backward-delete-char N)
  17258. (org-fix-tags-on-the-fly))))
  17259. (defun org-delete-char (N)
  17260. "Like `delete-char', but insert whitespace at field end in tables.
  17261. When deleting characters, in tables this function will insert whitespace in
  17262. front of the next \"|\" separator, to keep the table aligned. The table will
  17263. still be marked for re-alignment if the field did fill the entire column,
  17264. because, in this case the deletion might narrow the column."
  17265. (interactive "p")
  17266. (save-match-data
  17267. (org-check-before-invisible-edit 'delete)
  17268. (if (and (org-table-p)
  17269. (not (bolp))
  17270. (not (= (char-after) ?|))
  17271. (eq N 1))
  17272. (if (looking-at ".*?|")
  17273. (let ((pos (point))
  17274. (noalign (looking-at "[^|\n\r]* |"))
  17275. (c org-table-may-need-update))
  17276. (replace-match (concat
  17277. (substring (match-string 0) 1 -1)
  17278. " |"))
  17279. (goto-char pos)
  17280. ;; noalign: if there were two spaces at the end, this field
  17281. ;; does not determine the width of the column.
  17282. (if noalign (setq org-table-may-need-update c)))
  17283. (delete-char N))
  17284. (delete-char N)
  17285. (org-fix-tags-on-the-fly))))
  17286. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17287. (put 'org-self-insert-command 'delete-selection t)
  17288. (put 'orgtbl-self-insert-command 'delete-selection t)
  17289. (put 'org-delete-char 'delete-selection 'supersede)
  17290. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17291. (put 'org-yank 'delete-selection 'yank)
  17292. ;; Make `flyspell-mode' delay after some commands
  17293. (put 'org-self-insert-command 'flyspell-delayed t)
  17294. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17295. (put 'org-delete-char 'flyspell-delayed t)
  17296. (put 'org-delete-backward-char 'flyspell-delayed t)
  17297. ;; Make pabbrev-mode expand after org-mode commands
  17298. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17299. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17300. ;; How to do this: Measure non-white length of current string
  17301. ;; If equal to column width, we should realign.
  17302. (defun org-remap (map &rest commands)
  17303. "In MAP, remap the functions given in COMMANDS.
  17304. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17305. (let (new old)
  17306. (while commands
  17307. (setq old (pop commands) new (pop commands))
  17308. (if (fboundp 'command-remapping)
  17309. (org-defkey map (vector 'remap old) new)
  17310. (substitute-key-definition old new map global-map)))))
  17311. (when (eq org-enable-table-editor 'optimized)
  17312. ;; If the user wants maximum table support, we need to hijack
  17313. ;; some standard editing functions
  17314. (org-remap org-mode-map
  17315. 'self-insert-command 'org-self-insert-command
  17316. 'delete-char 'org-delete-char
  17317. 'delete-backward-char 'org-delete-backward-char)
  17318. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17319. (defvar org-ctrl-c-ctrl-c-hook nil
  17320. "Hook for functions attaching themselves to `C-c C-c'.
  17321. This can be used to add additional functionality to the C-c C-c
  17322. key which executes context-dependent commands. This hook is run
  17323. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17324. run after the last test.
  17325. Each function will be called with no arguments. The function
  17326. must check if the context is appropriate for it to act. If yes,
  17327. it should do its thing and then return a non-nil value. If the
  17328. context is wrong, just do nothing and return nil.")
  17329. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17330. "Hook for functions attaching themselves to `C-c C-c'.
  17331. This can be used to add additional functionality to the C-c C-c
  17332. key which executes context-dependent commands. This hook is run
  17333. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17334. before the first test.
  17335. Each function will be called with no arguments. The function
  17336. must check if the context is appropriate for it to act. If yes,
  17337. it should do its thing and then return a non-nil value. If the
  17338. context is wrong, just do nothing and return nil.")
  17339. (defvar org-tab-first-hook nil
  17340. "Hook for functions to attach themselves to TAB.
  17341. See `org-ctrl-c-ctrl-c-hook' for more information.
  17342. This hook runs as the first action when TAB is pressed, even before
  17343. `org-cycle' messes around with the `outline-regexp' to cater for
  17344. inline tasks and plain list item folding.
  17345. If any function in this hook returns t, any other actions that
  17346. would have been caused by TAB (such as table field motion or visibility
  17347. cycling) will not occur.")
  17348. (defvar org-tab-after-check-for-table-hook nil
  17349. "Hook for functions to attach themselves to TAB.
  17350. See `org-ctrl-c-ctrl-c-hook' for more information.
  17351. This hook runs after it has been established that the cursor is not in a
  17352. table, but before checking if the cursor is in a headline or if global cycling
  17353. should be done.
  17354. If any function in this hook returns t, not other actions like visibility
  17355. cycling will be done.")
  17356. (defvar org-tab-after-check-for-cycling-hook nil
  17357. "Hook for functions to attach themselves to TAB.
  17358. See `org-ctrl-c-ctrl-c-hook' for more information.
  17359. This hook runs after it has been established that not table field motion and
  17360. not visibility should be done because of current context. This is probably
  17361. the place where a package like yasnippets can hook in.")
  17362. (defvar org-tab-before-tab-emulation-hook nil
  17363. "Hook for functions to attach themselves to TAB.
  17364. See `org-ctrl-c-ctrl-c-hook' for more information.
  17365. This hook runs after every other options for TAB have been exhausted, but
  17366. before indentation and \t insertion takes place.")
  17367. (defvar org-metaleft-hook nil
  17368. "Hook for functions attaching themselves to `M-left'.
  17369. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17370. (defvar org-metaright-hook nil
  17371. "Hook for functions attaching themselves to `M-right'.
  17372. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17373. (defvar org-metaup-hook nil
  17374. "Hook for functions attaching themselves to `M-up'.
  17375. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17376. (defvar org-metadown-hook nil
  17377. "Hook for functions attaching themselves to `M-down'.
  17378. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17379. (defvar org-shiftmetaleft-hook nil
  17380. "Hook for functions attaching themselves to `M-S-left'.
  17381. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17382. (defvar org-shiftmetaright-hook nil
  17383. "Hook for functions attaching themselves to `M-S-right'.
  17384. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17385. (defvar org-shiftmetaup-hook nil
  17386. "Hook for functions attaching themselves to `M-S-up'.
  17387. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17388. (defvar org-shiftmetadown-hook nil
  17389. "Hook for functions attaching themselves to `M-S-down'.
  17390. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17391. (defvar org-metareturn-hook nil
  17392. "Hook for functions attaching themselves to `M-RET'.
  17393. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17394. (defvar org-shiftup-hook nil
  17395. "Hook for functions attaching themselves to `S-up'.
  17396. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17397. (defvar org-shiftup-final-hook nil
  17398. "Hook for functions attaching themselves to `S-up'.
  17399. This one runs after all other options except shift-select have been excluded.
  17400. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17401. (defvar org-shiftdown-hook nil
  17402. "Hook for functions attaching themselves to `S-down'.
  17403. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17404. (defvar org-shiftdown-final-hook nil
  17405. "Hook for functions attaching themselves to `S-down'.
  17406. This one runs after all other options except shift-select have been excluded.
  17407. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17408. (defvar org-shiftleft-hook nil
  17409. "Hook for functions attaching themselves to `S-left'.
  17410. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17411. (defvar org-shiftleft-final-hook nil
  17412. "Hook for functions attaching themselves to `S-left'.
  17413. This one runs after all other options except shift-select have been excluded.
  17414. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17415. (defvar org-shiftright-hook nil
  17416. "Hook for functions attaching themselves to `S-right'.
  17417. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17418. (defvar org-shiftright-final-hook nil
  17419. "Hook for functions attaching themselves to `S-right'.
  17420. This one runs after all other options except shift-select have been excluded.
  17421. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17422. (defun org-modifier-cursor-error ()
  17423. "Throw an error, a modified cursor command was applied in wrong context."
  17424. (error "This command is active in special context like tables, headlines or items"))
  17425. (defun org-shiftselect-error ()
  17426. "Throw an error because Shift-Cursor command was applied in wrong context."
  17427. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17428. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17429. (error "This command works only in special context like headlines or timestamps")))
  17430. (defun org-call-for-shift-select (cmd)
  17431. (let ((this-command-keys-shift-translated t))
  17432. (call-interactively cmd)))
  17433. (defun org-shifttab (&optional arg)
  17434. "Global visibility cycling or move to previous table field.
  17435. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17436. on context.
  17437. See the individual commands for more information."
  17438. (interactive "P")
  17439. (cond
  17440. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17441. ((integerp arg)
  17442. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17443. (message "Content view to level: %d" arg)
  17444. (org-content (prefix-numeric-value arg2))
  17445. (setq org-cycle-global-status 'overview)))
  17446. (t (call-interactively 'org-global-cycle))))
  17447. (defun org-shiftmetaleft ()
  17448. "Promote subtree or delete table column.
  17449. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17450. `org-table-delete-column', depending on context. See the
  17451. individual commands for more information."
  17452. (interactive)
  17453. (cond
  17454. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17455. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17456. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17457. ((if (not (org-region-active-p)) (org-at-item-p)
  17458. (save-excursion (goto-char (region-beginning))
  17459. (org-at-item-p)))
  17460. (call-interactively 'org-outdent-item-tree))
  17461. (t (org-modifier-cursor-error))))
  17462. (defun org-shiftmetaright ()
  17463. "Demote subtree or insert table column.
  17464. Calls `org-demote-subtree', `org-indent-item-tree', or
  17465. `org-table-insert-column', depending on context. See the
  17466. individual commands for more information."
  17467. (interactive)
  17468. (cond
  17469. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17470. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17471. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17472. ((if (not (org-region-active-p)) (org-at-item-p)
  17473. (save-excursion (goto-char (region-beginning))
  17474. (org-at-item-p)))
  17475. (call-interactively 'org-indent-item-tree))
  17476. (t (org-modifier-cursor-error))))
  17477. (defun org-shiftmetaup (&optional arg)
  17478. "Move subtree up or kill table row.
  17479. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17480. `org-move-item-up' or `org-timestamp-up', depending on context.
  17481. See the individual commands for more information."
  17482. (interactive "P")
  17483. (cond
  17484. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17485. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17486. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17487. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17488. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17489. (call-interactively 'org-timestamp-up)))
  17490. (t (org-modifier-cursor-error))))
  17491. (defun org-shiftmetadown (&optional arg)
  17492. "Move subtree down or insert table row.
  17493. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17494. `org-move-item-down' or `org-timestamp-up', depending on context.
  17495. See the individual commands for more information."
  17496. (interactive "P")
  17497. (cond
  17498. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17499. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17500. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17501. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17502. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17503. (call-interactively 'org-timestamp-down)))
  17504. (t (org-modifier-cursor-error))))
  17505. (defsubst org-hidden-tree-error ()
  17506. (error
  17507. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17508. (defun org-metaleft (&optional arg)
  17509. "Promote heading or move table column to left.
  17510. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17511. With no specific context, calls the Emacs default `backward-word'.
  17512. See the individual commands for more information."
  17513. (interactive "P")
  17514. (cond
  17515. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17516. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17517. ((org-with-limited-levels
  17518. (or (org-at-heading-p)
  17519. (and (org-region-active-p)
  17520. (save-excursion
  17521. (goto-char (region-beginning))
  17522. (org-at-heading-p)))))
  17523. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17524. (call-interactively 'org-do-promote))
  17525. ;; At an inline task.
  17526. ((org-at-heading-p)
  17527. (call-interactively 'org-inlinetask-promote))
  17528. ((or (org-at-item-p)
  17529. (and (org-region-active-p)
  17530. (save-excursion
  17531. (goto-char (region-beginning))
  17532. (org-at-item-p))))
  17533. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17534. (call-interactively 'org-outdent-item))
  17535. (t (call-interactively 'backward-word))))
  17536. (defun org-metaright (&optional arg)
  17537. "Demote a subtree, a list item or move table column to right.
  17538. In front of a drawer or a block keyword, indent it correctly.
  17539. With no specific context, calls the Emacs default `forward-word'.
  17540. See the individual commands for more information."
  17541. (interactive "P")
  17542. (cond
  17543. ((run-hook-with-args-until-success 'org-metaright-hook))
  17544. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17545. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17546. ((org-at-block-p) (call-interactively 'org-indent-block))
  17547. ((org-with-limited-levels
  17548. (or (org-at-heading-p)
  17549. (and (org-region-active-p)
  17550. (save-excursion
  17551. (goto-char (region-beginning))
  17552. (org-at-heading-p)))))
  17553. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17554. (call-interactively 'org-do-demote))
  17555. ;; At an inline task.
  17556. ((org-at-heading-p)
  17557. (call-interactively 'org-inlinetask-demote))
  17558. ((or (org-at-item-p)
  17559. (and (org-region-active-p)
  17560. (save-excursion
  17561. (goto-char (region-beginning))
  17562. (org-at-item-p))))
  17563. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17564. (call-interactively 'org-indent-item))
  17565. (t (call-interactively 'forward-word))))
  17566. (defun org-check-for-hidden (what)
  17567. "Check if there are hidden headlines/items in the current visual line.
  17568. WHAT can be either `headlines' or `items'. If the current line is
  17569. an outline or item heading and it has a folded subtree below it,
  17570. this function returns t, nil otherwise."
  17571. (let ((re (cond
  17572. ((eq what 'headlines) org-outline-regexp-bol)
  17573. ((eq what 'items) (org-item-beginning-re))
  17574. (t (error "This should not happen"))))
  17575. beg end)
  17576. (save-excursion
  17577. (catch 'exit
  17578. (unless (org-region-active-p)
  17579. (setq beg (point-at-bol))
  17580. (beginning-of-line 2)
  17581. (while (and (not (eobp)) ;; this is like `next-line'
  17582. (get-char-property (1- (point)) 'invisible))
  17583. (beginning-of-line 2))
  17584. (setq end (point))
  17585. (goto-char beg)
  17586. (goto-char (point-at-eol))
  17587. (setq end (max end (point)))
  17588. (while (re-search-forward re end t)
  17589. (if (get-char-property (match-beginning 0) 'invisible)
  17590. (throw 'exit t))))
  17591. nil))))
  17592. (defun org-metaup (&optional arg)
  17593. "Move subtree up or move table row up.
  17594. Calls `org-move-subtree-up' or `org-table-move-row' or
  17595. `org-move-item-up', depending on context. See the individual commands
  17596. for more information."
  17597. (interactive "P")
  17598. (cond
  17599. ((run-hook-with-args-until-success 'org-metaup-hook))
  17600. ((org-region-active-p)
  17601. (let* ((a (min (region-beginning) (region-end)))
  17602. (b (1- (max (region-beginning) (region-end))))
  17603. (c (save-excursion (goto-char a)
  17604. (move-beginning-of-line 0)))
  17605. (d (save-excursion (goto-char a)
  17606. (move-end-of-line 0) (point))))
  17607. (transpose-regions a b c d)
  17608. (goto-char c)))
  17609. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17610. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17611. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17612. (t (org-drag-element-backward))))
  17613. (defun org-metadown (&optional arg)
  17614. "Move subtree down or move table row down.
  17615. Calls `org-move-subtree-down' or `org-table-move-row' or
  17616. `org-move-item-down', depending on context. See the individual
  17617. commands for more information."
  17618. (interactive "P")
  17619. (cond
  17620. ((run-hook-with-args-until-success 'org-metadown-hook))
  17621. ((org-region-active-p)
  17622. (let* ((a (min (region-beginning) (region-end)))
  17623. (b (max (region-beginning) (region-end)))
  17624. (c (save-excursion (goto-char b)
  17625. (move-beginning-of-line 1)))
  17626. (d (save-excursion (goto-char b)
  17627. (move-end-of-line 1) (1+ (point)))))
  17628. (transpose-regions a b c d)
  17629. (goto-char d)))
  17630. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17631. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17632. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17633. (t (org-drag-element-forward))))
  17634. (defun org-shiftup (&optional arg)
  17635. "Increase item in timestamp or increase priority of current headline.
  17636. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17637. depending on context. See the individual commands for more information."
  17638. (interactive "P")
  17639. (cond
  17640. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17641. ((and org-support-shift-select (org-region-active-p))
  17642. (org-call-for-shift-select 'previous-line))
  17643. ((org-at-timestamp-p t)
  17644. (call-interactively (if org-edit-timestamp-down-means-later
  17645. 'org-timestamp-down 'org-timestamp-up)))
  17646. ((and (not (eq org-support-shift-select 'always))
  17647. org-enable-priority-commands
  17648. (org-at-heading-p))
  17649. (call-interactively 'org-priority-up))
  17650. ((and (not org-support-shift-select) (org-at-item-p))
  17651. (call-interactively 'org-previous-item))
  17652. ((org-clocktable-try-shift 'up arg))
  17653. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17654. (org-support-shift-select
  17655. (org-call-for-shift-select 'previous-line))
  17656. (t (org-shiftselect-error))))
  17657. (defun org-shiftdown (&optional arg)
  17658. "Decrease item in timestamp or decrease priority of current headline.
  17659. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17660. depending on context. See the individual commands for more information."
  17661. (interactive "P")
  17662. (cond
  17663. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17664. ((and org-support-shift-select (org-region-active-p))
  17665. (org-call-for-shift-select 'next-line))
  17666. ((org-at-timestamp-p t)
  17667. (call-interactively (if org-edit-timestamp-down-means-later
  17668. 'org-timestamp-up 'org-timestamp-down)))
  17669. ((and (not (eq org-support-shift-select 'always))
  17670. org-enable-priority-commands
  17671. (org-at-heading-p))
  17672. (call-interactively 'org-priority-down))
  17673. ((and (not org-support-shift-select) (org-at-item-p))
  17674. (call-interactively 'org-next-item))
  17675. ((org-clocktable-try-shift 'down arg))
  17676. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17677. (org-support-shift-select
  17678. (org-call-for-shift-select 'next-line))
  17679. (t (org-shiftselect-error))))
  17680. (defun org-shiftright (&optional arg)
  17681. "Cycle the thing at point or in the current line, depending on context.
  17682. Depending on context, this does one of the following:
  17683. - switch a timestamp at point one day into the future
  17684. - on a headline, switch to the next TODO keyword.
  17685. - on an item, switch entire list to the next bullet type
  17686. - on a property line, switch to the next allowed value
  17687. - on a clocktable definition line, move time block into the future"
  17688. (interactive "P")
  17689. (cond
  17690. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17691. ((and org-support-shift-select (org-region-active-p))
  17692. (org-call-for-shift-select 'forward-char))
  17693. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17694. ((and (not (eq org-support-shift-select 'always))
  17695. (org-at-heading-p))
  17696. (let ((org-inhibit-logging
  17697. (not org-treat-S-cursor-todo-selection-as-state-change))
  17698. (org-inhibit-blocking
  17699. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17700. (org-call-with-arg 'org-todo 'right)))
  17701. ((or (and org-support-shift-select
  17702. (not (eq org-support-shift-select 'always))
  17703. (org-at-item-bullet-p))
  17704. (and (not org-support-shift-select) (org-at-item-p)))
  17705. (org-call-with-arg 'org-cycle-list-bullet nil))
  17706. ((and (not (eq org-support-shift-select 'always))
  17707. (org-at-property-p))
  17708. (call-interactively 'org-property-next-allowed-value))
  17709. ((org-clocktable-try-shift 'right arg))
  17710. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17711. (org-support-shift-select
  17712. (org-call-for-shift-select 'forward-char))
  17713. (t (org-shiftselect-error))))
  17714. (defun org-shiftleft (&optional arg)
  17715. "Cycle the thing at point or in the current line, depending on context.
  17716. Depending on context, this does one of the following:
  17717. - switch a timestamp at point one day into the past
  17718. - on a headline, switch to the previous TODO keyword.
  17719. - on an item, switch entire list to the previous bullet type
  17720. - on a property line, switch to the previous allowed value
  17721. - on a clocktable definition line, move time block into the past"
  17722. (interactive "P")
  17723. (cond
  17724. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17725. ((and org-support-shift-select (org-region-active-p))
  17726. (org-call-for-shift-select 'backward-char))
  17727. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17728. ((and (not (eq org-support-shift-select 'always))
  17729. (org-at-heading-p))
  17730. (let ((org-inhibit-logging
  17731. (not org-treat-S-cursor-todo-selection-as-state-change))
  17732. (org-inhibit-blocking
  17733. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17734. (org-call-with-arg 'org-todo 'left)))
  17735. ((or (and org-support-shift-select
  17736. (not (eq org-support-shift-select 'always))
  17737. (org-at-item-bullet-p))
  17738. (and (not org-support-shift-select) (org-at-item-p)))
  17739. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17740. ((and (not (eq org-support-shift-select 'always))
  17741. (org-at-property-p))
  17742. (call-interactively 'org-property-previous-allowed-value))
  17743. ((org-clocktable-try-shift 'left arg))
  17744. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17745. (org-support-shift-select
  17746. (org-call-for-shift-select 'backward-char))
  17747. (t (org-shiftselect-error))))
  17748. (defun org-shiftcontrolright ()
  17749. "Switch to next TODO set."
  17750. (interactive)
  17751. (cond
  17752. ((and org-support-shift-select (org-region-active-p))
  17753. (org-call-for-shift-select 'forward-word))
  17754. ((and (not (eq org-support-shift-select 'always))
  17755. (org-at-heading-p))
  17756. (org-call-with-arg 'org-todo 'nextset))
  17757. (org-support-shift-select
  17758. (org-call-for-shift-select 'forward-word))
  17759. (t (org-shiftselect-error))))
  17760. (defun org-shiftcontrolleft ()
  17761. "Switch to previous TODO set."
  17762. (interactive)
  17763. (cond
  17764. ((and org-support-shift-select (org-region-active-p))
  17765. (org-call-for-shift-select 'backward-word))
  17766. ((and (not (eq org-support-shift-select 'always))
  17767. (org-at-heading-p))
  17768. (org-call-with-arg 'org-todo 'previousset))
  17769. (org-support-shift-select
  17770. (org-call-for-shift-select 'backward-word))
  17771. (t (org-shiftselect-error))))
  17772. (defun org-shiftcontrolup (&optional n)
  17773. "Change timestamps synchronously up in CLOCK log lines.
  17774. Optional argument N tells to change by that many units."
  17775. (interactive "P")
  17776. (cond ((and (not org-support-shift-select)
  17777. (org-at-clock-log-p)
  17778. (org-at-timestamp-p t))
  17779. (org-clock-timestamps-up n))
  17780. (t (org-shiftselect-error))))
  17781. (defun org-shiftcontroldown (&optional n)
  17782. "Change timestamps synchronously down in CLOCK log lines.
  17783. Optional argument N tells to change by that many units."
  17784. (interactive "P")
  17785. (cond ((and (not org-support-shift-select)
  17786. (org-at-clock-log-p)
  17787. (org-at-timestamp-p t))
  17788. (org-clock-timestamps-down n))
  17789. (t (org-shiftselect-error))))
  17790. (defun org-ctrl-c-ret ()
  17791. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17792. (interactive)
  17793. (cond
  17794. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17795. (t (call-interactively 'org-insert-heading))))
  17796. (defun org-find-visible ()
  17797. (let ((s (point)))
  17798. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17799. (get-char-property s 'invisible)))
  17800. s))
  17801. (defun org-find-invisible ()
  17802. (let ((s (point)))
  17803. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17804. (not (get-char-property s 'invisible))))
  17805. s))
  17806. (defun org-copy-visible (beg end)
  17807. "Copy the visible parts of the region."
  17808. (interactive "r")
  17809. (let (snippets s)
  17810. (save-excursion
  17811. (save-restriction
  17812. (narrow-to-region beg end)
  17813. (setq s (goto-char (point-min)))
  17814. (while (not (= (point) (point-max)))
  17815. (goto-char (org-find-invisible))
  17816. (push (buffer-substring s (point)) snippets)
  17817. (setq s (goto-char (org-find-visible))))))
  17818. (kill-new (apply 'concat (nreverse snippets)))))
  17819. (defun org-copy-special ()
  17820. "Copy region in table or copy current subtree.
  17821. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17822. See the individual commands for more information."
  17823. (interactive)
  17824. (call-interactively
  17825. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17826. (defun org-cut-special ()
  17827. "Cut region in table or cut current subtree.
  17828. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17829. See the individual commands for more information."
  17830. (interactive)
  17831. (call-interactively
  17832. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17833. (defun org-paste-special (arg)
  17834. "Paste rectangular region into table, or past subtree relative to level.
  17835. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17836. See the individual commands for more information."
  17837. (interactive "P")
  17838. (if (org-at-table-p)
  17839. (org-table-paste-rectangle)
  17840. (org-paste-subtree arg)))
  17841. (defsubst org-in-fixed-width-region-p ()
  17842. "Is point in a fixed-width region?"
  17843. (save-match-data
  17844. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17845. (defun org-edit-special (&optional arg)
  17846. "Call a special editor for the stuff at point.
  17847. When at a table, call the formula editor with `org-table-edit-formulas'.
  17848. When in a source code block, call `org-edit-src-code'.
  17849. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17850. When in an #+include line, visit the included file.
  17851. On a link, call `ffap' to visit the link at point.
  17852. Otherwise, return a user error."
  17853. (interactive)
  17854. ;; possibly prep session before editing source
  17855. (when (and (org-in-src-block-p) arg)
  17856. (let* ((info (org-babel-get-src-block-info))
  17857. (lang (nth 0 info))
  17858. (params (nth 2 info))
  17859. (session (cdr (assoc :session params))))
  17860. (when (and info session) ;; we are in a source-code block with a session
  17861. (funcall
  17862. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17863. (cond ;; proceed with `org-edit-special'
  17864. ((save-excursion
  17865. (beginning-of-line 1)
  17866. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17867. (find-file (org-trim (match-string 1))))
  17868. ((or (org-at-table-p)
  17869. (save-excursion
  17870. (beginning-of-line 1)
  17871. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17872. (call-interactively 'org-table-edit-formulas))
  17873. ((or (org-in-block-p '("src" "example" "latex" "html"))
  17874. (org-at-table.el-p))
  17875. (org-edit-src-code))
  17876. ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
  17877. ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
  17878. (t (user-error "No special environment to edit here"))))
  17879. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17880. (defun org-ctrl-c-ctrl-c (&optional arg)
  17881. "Set tags in headline, or update according to changed information at point.
  17882. This command does many different things, depending on context:
  17883. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17884. this is what we do.
  17885. - If the cursor is on a statistics cookie, update it.
  17886. - If the cursor is in a headline, prompt for tags and insert them
  17887. into the current line, aligned to `org-tags-column'. When called
  17888. with prefix arg, realign all tags in the current buffer.
  17889. - If the cursor is in one of the special #+KEYWORD lines, this
  17890. triggers scanning the buffer for these lines and updating the
  17891. information.
  17892. - If the cursor is inside a table, realign the table. This command
  17893. works even if the automatic table editor has been turned off.
  17894. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17895. the entire table.
  17896. - If the cursor is at a footnote reference or definition, jump to
  17897. the corresponding definition or references, respectively.
  17898. - If the cursor is a the beginning of a dynamic block, update it.
  17899. - If the current buffer is a capture buffer, close note and file it.
  17900. - If the cursor is on a <<<target>>>, update radio targets and
  17901. corresponding links in this buffer.
  17902. - If the cursor is on a numbered item in a plain list, renumber the
  17903. ordered list.
  17904. - If the cursor is on a checkbox, toggle it.
  17905. - If the cursor is on a code block, evaluate it. The variable
  17906. `org-confirm-babel-evaluate' can be used to control prompting
  17907. before code block evaluation, by default every code block
  17908. evaluation requires confirmation. Code block evaluation can be
  17909. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17910. (interactive "P")
  17911. (let ((org-enable-table-editor t))
  17912. (cond
  17913. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17914. org-occur-highlights
  17915. org-latex-fragment-image-overlays)
  17916. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17917. (org-remove-occur-highlights)
  17918. (org-remove-latex-fragment-image-overlays)
  17919. (message "Temporary highlights/overlays removed from current buffer"))
  17920. ((and (local-variable-p 'org-finish-function (current-buffer))
  17921. (fboundp org-finish-function))
  17922. (funcall org-finish-function))
  17923. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17924. ((org-in-regexp org-ts-regexp-both)
  17925. (org-timestamp-change 0 'day))
  17926. ((or (looking-at org-property-start-re)
  17927. (org-at-property-p))
  17928. (call-interactively 'org-property-action))
  17929. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17930. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17931. (or (org-at-heading-p) (org-at-item-p)))
  17932. (call-interactively 'org-update-statistics-cookies))
  17933. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17934. ((org-at-table.el-p)
  17935. (message "Use C-c ' to edit table.el tables"))
  17936. ((org-at-table-p)
  17937. (org-table-maybe-eval-formula)
  17938. (if arg
  17939. (call-interactively 'org-table-recalculate)
  17940. (org-table-maybe-recalculate-line))
  17941. (call-interactively 'org-table-align)
  17942. (orgtbl-send-table 'maybe))
  17943. ((or (org-footnote-at-reference-p)
  17944. (org-footnote-at-definition-p))
  17945. (call-interactively 'org-footnote-action))
  17946. ((org-at-item-checkbox-p)
  17947. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17948. ;; list only if at top item.
  17949. (let* ((cbox (match-string 1))
  17950. (struct (org-list-struct))
  17951. (old-struct (copy-tree struct))
  17952. (parents (org-list-parents-alist struct))
  17953. (orderedp (org-entry-get nil "ORDERED"))
  17954. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17955. block-item)
  17956. ;; Use a light version of `org-toggle-checkbox' to avoid
  17957. ;; computing list structure twice.
  17958. (let ((new-box (cond
  17959. ((equal arg '(16)) "[-]")
  17960. ((equal arg '(4)) nil)
  17961. ((equal "[X]" cbox) "[ ]")
  17962. (t "[X]"))))
  17963. (if (and firstp arg)
  17964. ;; If at first item of sub-list, remove check-box from
  17965. ;; every item at the same level.
  17966. (mapc
  17967. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17968. (org-list-get-all-items
  17969. (point-at-bol) struct (org-list-prevs-alist struct)))
  17970. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17971. ;; Replicate `org-list-write-struct', while grabbing a return
  17972. ;; value from `org-list-struct-fix-box'.
  17973. (org-list-struct-fix-ind struct parents 2)
  17974. (org-list-struct-fix-item-end struct)
  17975. (let ((prevs (org-list-prevs-alist struct)))
  17976. (org-list-struct-fix-bul struct prevs)
  17977. (org-list-struct-fix-ind struct parents)
  17978. (setq block-item
  17979. (org-list-struct-fix-box struct parents prevs orderedp)))
  17980. (if (equal struct old-struct)
  17981. (user-error "Cannot toggle this checkbox (unchecked subitems?)")
  17982. (org-list-struct-apply-struct struct old-struct)
  17983. (org-update-checkbox-count-maybe))
  17984. (when block-item
  17985. (message
  17986. "Checkboxes were removed due to unchecked box at line %d"
  17987. (org-current-line block-item)))
  17988. (when firstp (org-list-send-list 'maybe))))
  17989. ((org-at-item-p)
  17990. ;; Cursor at an item: repair list. Do checkbox related actions
  17991. ;; only if function was called with an argument. Send list only
  17992. ;; if at top item.
  17993. (let* ((struct (org-list-struct))
  17994. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17995. old-struct)
  17996. (when arg
  17997. (setq old-struct (copy-tree struct))
  17998. (if firstp
  17999. ;; If at first item of sub-list, add check-box to every
  18000. ;; item at the same level.
  18001. (mapc
  18002. (lambda (pos)
  18003. (unless (org-list-get-checkbox pos struct)
  18004. (org-list-set-checkbox pos struct "[ ]")))
  18005. (org-list-get-all-items
  18006. (point-at-bol) struct (org-list-prevs-alist struct)))
  18007. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  18008. (org-list-write-struct
  18009. struct (org-list-parents-alist struct) old-struct)
  18010. (when arg (org-update-checkbox-count-maybe))
  18011. (when firstp (org-list-send-list 'maybe))))
  18012. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  18013. ;; Dynamic block
  18014. (beginning-of-line 1)
  18015. (save-excursion (org-update-dblock)))
  18016. ((save-excursion
  18017. (let ((case-fold-search t))
  18018. (beginning-of-line 1)
  18019. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  18020. (cond
  18021. ((or (equal (match-string 1) "TBLFM")
  18022. (equal (match-string 1) "tblfm"))
  18023. ;; Recalculate the table before this line
  18024. (save-excursion
  18025. (beginning-of-line 1)
  18026. (skip-chars-backward " \r\n\t")
  18027. (if (org-at-table-p)
  18028. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  18029. (t
  18030. (let ((org-inhibit-startup-visibility-stuff t)
  18031. (org-startup-align-all-tables nil))
  18032. (when (boundp 'org-table-coordinate-overlays)
  18033. (mapc 'delete-overlay org-table-coordinate-overlays)
  18034. (setq org-table-coordinate-overlays nil))
  18035. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18036. (message "Local setup has been refreshed"))))
  18037. ((org-clock-update-time-maybe))
  18038. (t
  18039. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18040. (error "C-c C-c can do nothing useful at this location"))))))
  18041. (defun org-mode-restart ()
  18042. "Restart Org-mode, to scan again for special lines.
  18043. Also updates the keyword regular expressions."
  18044. (interactive)
  18045. (org-mode)
  18046. (message "Org-mode restarted"))
  18047. (defun org-kill-note-or-show-branches ()
  18048. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18049. (interactive)
  18050. (if (not org-finish-function)
  18051. (progn
  18052. (hide-subtree)
  18053. (call-interactively 'show-branches))
  18054. (let ((org-note-abort t))
  18055. (funcall org-finish-function))))
  18056. (defun org-return (&optional indent)
  18057. "Goto next table row or insert a newline.
  18058. Calls `org-table-next-row' or `newline', depending on context.
  18059. See the individual commands for more information."
  18060. (interactive)
  18061. (let (org-ts-what)
  18062. (cond
  18063. ((or (bobp) (org-in-src-block-p))
  18064. (if indent (newline-and-indent) (newline)))
  18065. ((org-at-table-p)
  18066. (org-table-justify-field-maybe)
  18067. (call-interactively 'org-table-next-row))
  18068. ;; when `newline-and-indent' is called within a list, make sure
  18069. ;; text moved stays inside the item.
  18070. ((and (org-in-item-p) indent)
  18071. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18072. (progn
  18073. (save-match-data (newline))
  18074. (org-indent-line-to (length (match-string 0))))
  18075. (let ((ind (org-get-indentation)))
  18076. (newline)
  18077. (if (org-looking-back org-list-end-re)
  18078. (org-indent-line)
  18079. (org-indent-line-to ind)))))
  18080. ((and org-return-follows-link
  18081. (org-at-timestamp-p t)
  18082. (not (eq org-ts-what 'after)))
  18083. (org-follow-timestamp-link))
  18084. ((and org-return-follows-link
  18085. (let ((tprop (get-text-property (point) 'face)))
  18086. (or (eq tprop 'org-link)
  18087. (and (listp tprop) (memq 'org-link tprop)))))
  18088. (call-interactively 'org-open-at-point))
  18089. ((and (org-at-heading-p)
  18090. (looking-at
  18091. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18092. (org-show-entry)
  18093. (end-of-line 1)
  18094. (newline))
  18095. (t (if indent (newline-and-indent) (newline))))))
  18096. (defun org-return-indent ()
  18097. "Goto next table row or insert a newline and indent.
  18098. Calls `org-table-next-row' or `newline-and-indent', depending on
  18099. context. See the individual commands for more information."
  18100. (interactive)
  18101. (org-return t))
  18102. (defun org-ctrl-c-star ()
  18103. "Compute table, or change heading status of lines.
  18104. Calls `org-table-recalculate' or `org-toggle-heading',
  18105. depending on context."
  18106. (interactive)
  18107. (cond
  18108. ((org-at-table-p)
  18109. (call-interactively 'org-table-recalculate))
  18110. (t
  18111. ;; Convert all lines in region to list items
  18112. (call-interactively 'org-toggle-heading))))
  18113. (defun org-ctrl-c-minus ()
  18114. "Insert separator line in table or modify bullet status of line.
  18115. Also turns a plain line or a region of lines into list items.
  18116. Calls `org-table-insert-hline', `org-toggle-item', or
  18117. `org-cycle-list-bullet', depending on context."
  18118. (interactive)
  18119. (cond
  18120. ((org-at-table-p)
  18121. (call-interactively 'org-table-insert-hline))
  18122. ((org-region-active-p)
  18123. (call-interactively 'org-toggle-item))
  18124. ((org-in-item-p)
  18125. (call-interactively 'org-cycle-list-bullet))
  18126. (t
  18127. (call-interactively 'org-toggle-item))))
  18128. (defun org-toggle-item (arg)
  18129. "Convert headings or normal lines to items, items to normal lines.
  18130. If there is no active region, only the current line is considered.
  18131. If the first non blank line in the region is an headline, convert
  18132. all headlines to items, shifting text accordingly.
  18133. If it is an item, convert all items to normal lines.
  18134. If it is normal text, change region into an item. With a prefix
  18135. argument ARG, change each line in region into an item."
  18136. (interactive "P")
  18137. (let ((shift-text
  18138. (function
  18139. ;; Shift text in current section to IND, from point to END.
  18140. ;; The function leaves point to END line.
  18141. (lambda (ind end)
  18142. (let ((min-i 1000) (end (copy-marker end)))
  18143. ;; First determine the minimum indentation (MIN-I) of
  18144. ;; the text.
  18145. (save-excursion
  18146. (catch 'exit
  18147. (while (< (point) end)
  18148. (let ((i (org-get-indentation)))
  18149. (cond
  18150. ;; Skip blank lines and inline tasks.
  18151. ((looking-at "^[ \t]*$"))
  18152. ((looking-at org-outline-regexp-bol))
  18153. ;; We can't find less than 0 indentation.
  18154. ((zerop i) (throw 'exit (setq min-i 0)))
  18155. ((< i min-i) (setq min-i i))))
  18156. (forward-line))))
  18157. ;; Then indent each line so that a line indented to
  18158. ;; MIN-I becomes indented to IND. Ignore blank lines
  18159. ;; and inline tasks in the process.
  18160. (let ((delta (- ind min-i)))
  18161. (while (< (point) end)
  18162. (unless (or (looking-at "^[ \t]*$")
  18163. (looking-at org-outline-regexp-bol))
  18164. (org-indent-line-to (+ (org-get-indentation) delta)))
  18165. (forward-line)))))))
  18166. (skip-blanks
  18167. (function
  18168. ;; Return beginning of first non-blank line, starting from
  18169. ;; line at POS.
  18170. (lambda (pos)
  18171. (save-excursion
  18172. (goto-char pos)
  18173. (skip-chars-forward " \r\t\n")
  18174. (point-at-bol)))))
  18175. beg end)
  18176. ;; Determine boundaries of changes.
  18177. (if (org-region-active-p)
  18178. (setq beg (funcall skip-blanks (region-beginning))
  18179. end (copy-marker (region-end)))
  18180. (setq beg (funcall skip-blanks (point-at-bol))
  18181. end (copy-marker (point-at-eol))))
  18182. ;; Depending on the starting line, choose an action on the text
  18183. ;; between BEG and END.
  18184. (org-with-limited-levels
  18185. (save-excursion
  18186. (goto-char beg)
  18187. (cond
  18188. ;; Case 1. Start at an item: de-itemize. Note that it only
  18189. ;; happens when a region is active: `org-ctrl-c-minus'
  18190. ;; would call `org-cycle-list-bullet' otherwise.
  18191. ((org-at-item-p)
  18192. (while (< (point) end)
  18193. (when (org-at-item-p)
  18194. (skip-chars-forward " \t")
  18195. (delete-region (point) (match-end 0)))
  18196. (forward-line)))
  18197. ;; Case 2. Start at an heading: convert to items.
  18198. ((org-at-heading-p)
  18199. (let* ((bul (org-list-bullet-string "-"))
  18200. (bul-len (length bul))
  18201. ;; Indentation of the first heading. It should be
  18202. ;; relative to the indentation of its parent, if any.
  18203. (start-ind (save-excursion
  18204. (cond
  18205. ((not org-adapt-indentation) 0)
  18206. ((not (outline-previous-heading)) 0)
  18207. (t (length (match-string 0))))))
  18208. ;; Level of first heading. Further headings will be
  18209. ;; compared to it to determine hierarchy in the list.
  18210. (ref-level (org-reduced-level (org-outline-level))))
  18211. (while (< (point) end)
  18212. (let* ((level (org-reduced-level (org-outline-level)))
  18213. (delta (max 0 (- level ref-level))))
  18214. ;; If current headline is less indented than the first
  18215. ;; one, set it as reference, in order to preserve
  18216. ;; subtrees.
  18217. (when (< level ref-level) (setq ref-level level))
  18218. (replace-match bul t t)
  18219. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18220. ;; Ensure all text down to END (or SECTION-END) belongs
  18221. ;; to the newly created item.
  18222. (let ((section-end (save-excursion
  18223. (or (outline-next-heading) (point)))))
  18224. (forward-line)
  18225. (funcall shift-text
  18226. (+ start-ind (* (1+ delta) bul-len))
  18227. (min end section-end)))))))
  18228. ;; Case 3. Normal line with ARG: turn each non-item line into
  18229. ;; an item.
  18230. (arg
  18231. (while (< (point) end)
  18232. (unless (or (org-at-heading-p) (org-at-item-p))
  18233. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18234. (replace-match
  18235. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18236. (forward-line)))
  18237. ;; Case 4. Normal line without ARG: make the first line of
  18238. ;; region an item, and shift indentation of others
  18239. ;; lines to set them as item's body.
  18240. (t (let* ((bul (org-list-bullet-string "-"))
  18241. (bul-len (length bul))
  18242. (ref-ind (org-get-indentation)))
  18243. (skip-chars-forward " \t")
  18244. (insert bul)
  18245. (forward-line)
  18246. (while (< (point) end)
  18247. ;; Ensure that lines less indented than first one
  18248. ;; still get included in item body.
  18249. (funcall shift-text
  18250. (+ ref-ind bul-len)
  18251. (min end (save-excursion (or (outline-next-heading)
  18252. (point)))))
  18253. (forward-line)))))))))
  18254. (defun org-toggle-heading (&optional nstars)
  18255. "Convert headings to normal text, or items or text to headings.
  18256. If there is no active region, only the current line is considered.
  18257. With a \\[universal-argument] prefix, convert the whole list at
  18258. point into heading.
  18259. In a region:
  18260. - If the first non blank line is an headline, remove the stars
  18261. from all headlines in the region.
  18262. - If it is a normal line turn each and every normal line (i.e. not an
  18263. heading or an item) in the region into a heading.
  18264. - If it is a plain list item, turn all plain list items into headings.
  18265. When converting a line into a heading, the number of stars is chosen
  18266. such that the lines become children of the current entry. However,
  18267. when a prefix argument is given, its value determines the number of
  18268. stars to add."
  18269. (interactive "P")
  18270. (let ((skip-blanks
  18271. (function
  18272. ;; Return beginning of first non-blank line, starting from
  18273. ;; line at POS.
  18274. (lambda (pos)
  18275. (save-excursion
  18276. (goto-char pos)
  18277. (while (org-at-comment-p) (forward-line))
  18278. (skip-chars-forward " \r\t\n")
  18279. (point-at-bol)))))
  18280. beg end toggled)
  18281. ;; Determine boundaries of changes. If a universal prefix has
  18282. ;; been given, put the list in a region. If region ends at a bol,
  18283. ;; do not consider the last line to be in the region.
  18284. (when (and current-prefix-arg (org-at-item-p))
  18285. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18286. (org-mark-element))
  18287. (if (org-region-active-p)
  18288. (setq beg (funcall skip-blanks (region-beginning))
  18289. end (copy-marker (save-excursion
  18290. (goto-char (region-end))
  18291. (if (bolp) (point) (point-at-eol)))))
  18292. (setq beg (funcall skip-blanks (point-at-bol))
  18293. end (copy-marker (point-at-eol))))
  18294. ;; Ensure inline tasks don't count as headings.
  18295. (org-with-limited-levels
  18296. (save-excursion
  18297. (goto-char beg)
  18298. (cond
  18299. ;; Case 1. Started at an heading: de-star headings.
  18300. ((org-at-heading-p)
  18301. (while (< (point) end)
  18302. (when (org-at-heading-p t)
  18303. (looking-at org-outline-regexp) (replace-match "")
  18304. (setq toggled t))
  18305. (forward-line)))
  18306. ;; Case 2. Started at an item: change items into headlines.
  18307. ;; One star will be added by `org-list-to-subtree'.
  18308. ((org-at-item-p)
  18309. (let* ((stars (make-string
  18310. (if nstars
  18311. ;; subtract the star that will be added again by
  18312. ;; `org-list-to-subtree'
  18313. (1- (prefix-numeric-value current-prefix-arg))
  18314. (or (org-current-level) 0))
  18315. ?*))
  18316. (add-stars
  18317. (cond (nstars "") ; stars from prefix only
  18318. ((equal stars "") "") ; before first heading
  18319. (org-odd-levels-only "*") ; inside heading, odd
  18320. (t "")))) ; inside heading, oddeven
  18321. (while (< (point) end)
  18322. (when (org-at-item-p)
  18323. ;; Pay attention to cases when region ends before list.
  18324. (let* ((struct (org-list-struct))
  18325. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18326. (save-restriction
  18327. (narrow-to-region (point) list-end)
  18328. (insert
  18329. (org-list-to-subtree
  18330. (org-list-parse-list t)
  18331. '(:istart (concat stars add-stars (funcall get-stars depth))
  18332. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18333. (setq toggled t))
  18334. (forward-line))))
  18335. ;; Case 3. Started at normal text: make every line an heading,
  18336. ;; skipping headlines and items.
  18337. (t (let* ((stars (make-string
  18338. (if nstars
  18339. (prefix-numeric-value current-prefix-arg)
  18340. (or (org-current-level) 0))
  18341. ?*))
  18342. (add-stars
  18343. (cond (nstars "") ; stars from prefix only
  18344. ((equal stars "") "*") ; before first heading
  18345. (org-odd-levels-only "**") ; inside heading, odd
  18346. (t "*"))) ; inside heading, oddeven
  18347. (rpl (concat stars add-stars " ")))
  18348. (while (< (point) end)
  18349. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18350. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18351. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18352. (forward-line)))))))
  18353. (unless toggled (message "Cannot toggle heading from here"))))
  18354. (defun org-meta-return (&optional arg)
  18355. "Insert a new heading or wrap a region in a table.
  18356. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18357. See the individual commands for more information."
  18358. (interactive "P")
  18359. (cond
  18360. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18361. ((or (org-at-drawer-p) (org-at-property-p))
  18362. (newline-and-indent))
  18363. ((org-at-table-p)
  18364. (call-interactively 'org-table-wrap-region))
  18365. (t (call-interactively 'org-insert-heading))))
  18366. ;;; Menu entries
  18367. (defsubst org-in-subtree-not-table-p ()
  18368. "Are we in a subtree and not in a table?"
  18369. (and (not (org-before-first-heading-p))
  18370. (not (org-at-table-p))))
  18371. ;; Define the Org-mode menus
  18372. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18373. '("Tbl"
  18374. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18375. ["Next Field" org-cycle (org-at-table-p)]
  18376. ["Previous Field" org-shifttab (org-at-table-p)]
  18377. ["Next Row" org-return (org-at-table-p)]
  18378. "--"
  18379. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18380. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18381. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18382. "--"
  18383. ("Column"
  18384. ["Move Column Left" org-metaleft (org-at-table-p)]
  18385. ["Move Column Right" org-metaright (org-at-table-p)]
  18386. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18387. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18388. ("Row"
  18389. ["Move Row Up" org-metaup (org-at-table-p)]
  18390. ["Move Row Down" org-metadown (org-at-table-p)]
  18391. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18392. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18393. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18394. "--"
  18395. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18396. ("Rectangle"
  18397. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18398. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18399. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18400. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18401. "--"
  18402. ("Calculate"
  18403. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18404. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18405. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18406. "--"
  18407. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18408. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18409. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18410. "--"
  18411. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18412. "--"
  18413. ["Sum Column/Rectangle" org-table-sum
  18414. (or (org-at-table-p) (org-region-active-p))]
  18415. ["Which Column?" org-table-current-column (org-at-table-p)])
  18416. ["Debug Formulas"
  18417. org-table-toggle-formula-debugger
  18418. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18419. ["Show Col/Row Numbers"
  18420. org-table-toggle-coordinate-overlays
  18421. :style toggle
  18422. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18423. "--"
  18424. ["Create" org-table-create (and (not (org-at-table-p))
  18425. org-enable-table-editor)]
  18426. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18427. ["Import from File" org-table-import (not (org-at-table-p))]
  18428. ["Export to File" org-table-export (org-at-table-p)]
  18429. "--"
  18430. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18431. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18432. '("Org"
  18433. ("Show/Hide"
  18434. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18435. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18436. ["Sparse Tree..." org-sparse-tree t]
  18437. ["Reveal Context" org-reveal t]
  18438. ["Show All" show-all t]
  18439. "--"
  18440. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18441. "--"
  18442. ["New Heading" org-insert-heading t]
  18443. ("Navigate Headings"
  18444. ["Up" outline-up-heading t]
  18445. ["Next" outline-next-visible-heading t]
  18446. ["Previous" outline-previous-visible-heading t]
  18447. ["Next Same Level" outline-forward-same-level t]
  18448. ["Previous Same Level" outline-backward-same-level t]
  18449. "--"
  18450. ["Jump" org-goto t])
  18451. ("Edit Structure"
  18452. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18453. "--"
  18454. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18455. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18456. "--"
  18457. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18458. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18459. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18460. "--"
  18461. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18462. "--"
  18463. ["Copy visible text" org-copy-visible t]
  18464. "--"
  18465. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18466. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18467. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18468. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18469. "--"
  18470. ["Sort Region/Children" org-sort t]
  18471. "--"
  18472. ["Convert to odd levels" org-convert-to-odd-levels t]
  18473. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18474. ("Editing"
  18475. ["Emphasis..." org-emphasize t]
  18476. ["Edit Source Example" org-edit-special t]
  18477. "--"
  18478. ["Footnote new/jump" org-footnote-action t]
  18479. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18480. ("Archive"
  18481. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18482. "--"
  18483. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18484. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18485. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18486. )
  18487. "--"
  18488. ("Hyperlinks"
  18489. ["Store Link (Global)" org-store-link t]
  18490. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18491. ["Insert Link" org-insert-link t]
  18492. ["Follow Link" org-open-at-point t]
  18493. "--"
  18494. ["Next link" org-next-link t]
  18495. ["Previous link" org-previous-link t]
  18496. "--"
  18497. ["Descriptive Links"
  18498. org-toggle-link-display
  18499. :style radio
  18500. :selected org-descriptive-links
  18501. ]
  18502. ["Literal Links"
  18503. org-toggle-link-display
  18504. :style radio
  18505. :selected (not org-descriptive-links)])
  18506. "--"
  18507. ("TODO Lists"
  18508. ["TODO/DONE/-" org-todo t]
  18509. ("Select keyword"
  18510. ["Next keyword" org-shiftright (org-at-heading-p)]
  18511. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18512. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18513. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18514. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18515. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18516. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18517. "--"
  18518. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18519. :selected org-enforce-todo-dependencies :style toggle :active t]
  18520. "Settings for tree at point"
  18521. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18522. :selected (org-entry-get nil "ORDERED")
  18523. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18524. ["Do Children parallel" org-toggle-ordered-property :style radio
  18525. :selected (not (org-entry-get nil "ORDERED"))
  18526. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18527. "--"
  18528. ["Set Priority" org-priority t]
  18529. ["Priority Up" org-shiftup t]
  18530. ["Priority Down" org-shiftdown t]
  18531. "--"
  18532. ["Get news from all feeds" org-feed-update-all t]
  18533. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18534. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18535. ("TAGS and Properties"
  18536. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18537. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18538. "--"
  18539. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18540. ["Column view of properties" org-columns t]
  18541. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18542. ("Dates and Scheduling"
  18543. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18544. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18545. ("Change Date"
  18546. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18547. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18548. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18549. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18550. ["Compute Time Range" org-evaluate-time-range t]
  18551. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18552. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18553. "--"
  18554. ["Custom time format" org-toggle-time-stamp-overlays
  18555. :style radio :selected org-display-custom-times]
  18556. "--"
  18557. ["Goto Calendar" org-goto-calendar t]
  18558. ["Date from Calendar" org-date-from-calendar t]
  18559. "--"
  18560. ["Start/Restart Timer" org-timer-start t]
  18561. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18562. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18563. ["Insert Timer String" org-timer t]
  18564. ["Insert Timer Item" org-timer-item t])
  18565. ("Logging work"
  18566. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18567. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18568. ["Clock out" org-clock-out t]
  18569. ["Clock cancel" org-clock-cancel t]
  18570. "--"
  18571. ["Mark as default task" org-clock-mark-default-task t]
  18572. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18573. ["Goto running clock" org-clock-goto t]
  18574. "--"
  18575. ["Display times" org-clock-display t]
  18576. ["Create clock table" org-clock-report t]
  18577. "--"
  18578. ["Record DONE time"
  18579. (progn (setq org-log-done (not org-log-done))
  18580. (message "Switching to %s will %s record a timestamp"
  18581. (car org-done-keywords)
  18582. (if org-log-done "automatically" "not")))
  18583. :style toggle :selected org-log-done])
  18584. "--"
  18585. ["Agenda Command..." org-agenda t]
  18586. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18587. ("File List for Agenda")
  18588. ("Special views current file"
  18589. ["TODO Tree" org-show-todo-tree t]
  18590. ["Check Deadlines" org-check-deadlines t]
  18591. ["Timeline" org-timeline t]
  18592. ["Tags/Property tree" org-match-sparse-tree t])
  18593. "--"
  18594. ["Export/Publish..." org-export t]
  18595. ("LaTeX"
  18596. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18597. :selected org-cdlatex-mode]
  18598. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18599. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18600. ["Modify math symbol" org-cdlatex-math-modify
  18601. (org-inside-LaTeX-fragment-p)]
  18602. ["Insert citation" org-reftex-citation t]
  18603. "--"
  18604. ["Template for BEAMER" (progn (require 'org-beamer)
  18605. (org-insert-beamer-options-template)) t])
  18606. "--"
  18607. ("MobileOrg"
  18608. ["Push Files and Views" org-mobile-push t]
  18609. ["Get Captured and Flagged" org-mobile-pull t]
  18610. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18611. "--"
  18612. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18613. "--"
  18614. ("Documentation"
  18615. ["Show Version" org-version t]
  18616. ["Info Documentation" org-info t])
  18617. ("Customize"
  18618. ["Browse Org Group" org-customize t]
  18619. "--"
  18620. ["Expand This Menu" org-create-customize-menu
  18621. (fboundp 'customize-menu-create)])
  18622. ["Send bug report" org-submit-bug-report t]
  18623. "--"
  18624. ("Refresh/Reload"
  18625. ["Refresh setup current buffer" org-mode-restart t]
  18626. ["Reload Org (after update)" org-reload t]
  18627. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18628. ))
  18629. (defun org-info (&optional node)
  18630. "Read documentation for Org-mode in the info system.
  18631. With optional NODE, go directly to that node."
  18632. (interactive)
  18633. (info (format "(org)%s" (or node ""))))
  18634. ;;;###autoload
  18635. (defun org-submit-bug-report ()
  18636. "Submit a bug report on Org-mode via mail.
  18637. Don't hesitate to report any problems or inaccurate documentation.
  18638. If you don't have setup sending mail from (X)Emacs, please copy the
  18639. output buffer into your mail program, as it gives us important
  18640. information about your Org-mode version and configuration."
  18641. (interactive)
  18642. (require 'reporter)
  18643. (org-load-modules-maybe)
  18644. (org-require-autoloaded-modules)
  18645. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18646. (reporter-submit-bug-report
  18647. "emacs-orgmode@gnu.org"
  18648. (org-version nil 'full)
  18649. (let (list)
  18650. (save-window-excursion
  18651. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18652. (delete-other-windows)
  18653. (erase-buffer)
  18654. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18655. We would like to add your full Org-mode and Outline configuration to the
  18656. bug report. This greatly simplifies the work of the maintainer and
  18657. other experts on the mailing list.
  18658. HOWEVER, some variables you have customized may contain private
  18659. information. The names of customers, colleagues, or friends, might
  18660. appear in the form of file names, tags, todo states, or search strings.
  18661. If you answer yes to the prompt, you might want to check and remove
  18662. such private information before sending the email.")
  18663. (add-text-properties (point-min) (point-max) '(face org-warning))
  18664. (when (yes-or-no-p "Include your Org-mode configuration ")
  18665. (mapatoms
  18666. (lambda (v)
  18667. (and (boundp v)
  18668. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18669. (or (and (symbol-value v)
  18670. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18671. (and
  18672. (get v 'custom-type) (get v 'standard-value)
  18673. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18674. (push v list)))))
  18675. (kill-buffer (get-buffer "*Warn about privacy*"))
  18676. list))
  18677. nil nil
  18678. "Remember to cover the basics, that is, what you expected to happen and
  18679. what in fact did happen. You don't know how to make a good report? See
  18680. http://orgmode.org/manual/Feedback.html#Feedback
  18681. Your bug report will be posted to the Org-mode mailing list.
  18682. ------------------------------------------------------------------------")
  18683. (save-excursion
  18684. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18685. (replace-match "\\1Bug: \\3 [\\2]")))))
  18686. (defun org-install-agenda-files-menu ()
  18687. (let ((bl (buffer-list)))
  18688. (save-excursion
  18689. (while bl
  18690. (set-buffer (pop bl))
  18691. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18692. (when (derived-mode-p 'org-mode)
  18693. (easy-menu-change
  18694. '("Org") "File List for Agenda"
  18695. (append
  18696. (list
  18697. ["Edit File List" (org-edit-agenda-file-list) t]
  18698. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18699. ["Remove Current File from List" org-remove-file t]
  18700. ["Cycle through agenda files" org-cycle-agenda-files t]
  18701. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18702. "--")
  18703. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18704. ;;;; Documentation
  18705. (defun org-require-autoloaded-modules ()
  18706. (interactive)
  18707. (mapc 'require
  18708. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18709. org-docbook org-exp org-html org-icalendar
  18710. org-id org-latex
  18711. org-publish org-remember org-table
  18712. org-timer org-xoxo)))
  18713. ;;;###autoload
  18714. (defun org-reload (&optional uncompiled)
  18715. "Reload all org lisp files.
  18716. With prefix arg UNCOMPILED, load the uncompiled versions."
  18717. (interactive "P")
  18718. (require 'loadhist)
  18719. (let* ((org-dir (org-find-library-dir "org"))
  18720. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18721. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18722. (remove-re (mapconcat 'identity
  18723. (mapcar (lambda (f) (concat "^" f "$"))
  18724. (list (if (featurep 'xemacs)
  18725. "org-colview"
  18726. "org-colview-xemacs")
  18727. "org" "org-loaddefs" "org-version"))
  18728. "\\|"))
  18729. (feats (delete-dups
  18730. (mapcar 'file-name-sans-extension
  18731. (mapcar 'file-name-nondirectory
  18732. (delq nil
  18733. (mapcar 'feature-file
  18734. features))))))
  18735. (lfeat (append
  18736. (sort
  18737. (setq feats
  18738. (delq nil (mapcar
  18739. (lambda (f)
  18740. (if (and (string-match feature-re f)
  18741. (not (string-match remove-re f)))
  18742. f nil))
  18743. feats)))
  18744. 'string-lessp)
  18745. (list "org-version" "org")))
  18746. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18747. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18748. load-uncore load-misses)
  18749. (setq load-misses
  18750. (delq 't
  18751. (mapcar (lambda (f)
  18752. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18753. (and (string= org-dir contrib-dir)
  18754. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18755. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18756. (add-to-list 'load-uncore f 'append)
  18757. 't)
  18758. f))
  18759. lfeat)))
  18760. (if load-uncore
  18761. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18762. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18763. (if load-misses
  18764. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18765. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18766. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18767. ;;;###autoload
  18768. (defun org-customize ()
  18769. "Call the customize function with org as argument."
  18770. (interactive)
  18771. (org-load-modules-maybe)
  18772. (org-require-autoloaded-modules)
  18773. (customize-browse 'org))
  18774. (defun org-create-customize-menu ()
  18775. "Create a full customization menu for Org-mode, insert it into the menu."
  18776. (interactive)
  18777. (org-load-modules-maybe)
  18778. (org-require-autoloaded-modules)
  18779. (if (fboundp 'customize-menu-create)
  18780. (progn
  18781. (easy-menu-change
  18782. '("Org") "Customize"
  18783. `(["Browse Org group" org-customize t]
  18784. "--"
  18785. ,(customize-menu-create 'org)
  18786. ["Set" Custom-set t]
  18787. ["Save" Custom-save t]
  18788. ["Reset to Current" Custom-reset-current t]
  18789. ["Reset to Saved" Custom-reset-saved t]
  18790. ["Reset to Standard Settings" Custom-reset-standard t]))
  18791. (message "\"Org\"-menu now contains full customization menu"))
  18792. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18793. ;;;; Miscellaneous stuff
  18794. ;;; Generally useful functions
  18795. (defun org-get-at-bol (property)
  18796. "Get text property PROPERTY at beginning of line."
  18797. (get-text-property (point-at-bol) property))
  18798. (defun org-find-text-property-in-string (prop s)
  18799. "Return the first non-nil value of property PROP in string S."
  18800. (or (get-text-property 0 prop s)
  18801. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18802. prop s)))
  18803. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18804. "Display the given MESSAGE as a warning."
  18805. (if (fboundp 'display-warning)
  18806. (display-warning 'org message
  18807. (if (featurep 'xemacs) 'warning :warning))
  18808. (let ((buf (get-buffer-create "*Org warnings*")))
  18809. (with-current-buffer buf
  18810. (goto-char (point-max))
  18811. (insert "Warning (Org): " message)
  18812. (unless (bolp)
  18813. (newline)))
  18814. (display-buffer buf)
  18815. (sit-for 0))))
  18816. (defun org-eval (form)
  18817. "Eval FORM and return result."
  18818. (condition-case error
  18819. (eval form)
  18820. (error (format "%%![Error: %s]" error))))
  18821. (defun org-in-clocktable-p ()
  18822. "Check if the cursor is in a clocktable."
  18823. (let ((pos (point)) start)
  18824. (save-excursion
  18825. (end-of-line 1)
  18826. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18827. (setq start (match-beginning 0))
  18828. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18829. (>= (match-end 0) pos)
  18830. start))))
  18831. (defun org-in-commented-line ()
  18832. "Is point in a line starting with `#'?"
  18833. (equal (char-after (point-at-bol)) ?#))
  18834. (defun org-in-indented-comment-line ()
  18835. "Is point in a line starting with `#' after some white space?"
  18836. (save-excursion
  18837. (save-match-data
  18838. (goto-char (point-at-bol))
  18839. (looking-at "[ \t]*#"))))
  18840. (defun org-in-verbatim-emphasis ()
  18841. (save-match-data
  18842. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18843. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18844. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18845. (if (and marker (marker-buffer marker)
  18846. (buffer-live-p (marker-buffer marker)))
  18847. (progn
  18848. (org-pop-to-buffer-same-window (marker-buffer marker))
  18849. (if (or (> marker (point-max)) (< marker (point-min)))
  18850. (widen))
  18851. (goto-char marker)
  18852. (org-show-context 'org-goto))
  18853. (if bookmark
  18854. (bookmark-jump bookmark)
  18855. (error "Cannot find location"))))
  18856. (defun org-quote-csv-field (s)
  18857. "Quote field for inclusion in CSV material."
  18858. (if (string-match "[\",]" s)
  18859. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18860. s))
  18861. (defun org-force-self-insert (N)
  18862. "Needed to enforce self-insert under remapping."
  18863. (interactive "p")
  18864. (self-insert-command N))
  18865. (defun org-string-width (s)
  18866. "Compute width of string, ignoring invisible characters.
  18867. This ignores character with invisibility property `org-link', and also
  18868. characters with property `org-cwidth', because these will become invisible
  18869. upon the next fontification round."
  18870. (let (b l)
  18871. (when (or (eq t buffer-invisibility-spec)
  18872. (assq 'org-link buffer-invisibility-spec))
  18873. (while (setq b (text-property-any 0 (length s)
  18874. 'invisible 'org-link s))
  18875. (setq s (concat (substring s 0 b)
  18876. (substring s (or (next-single-property-change
  18877. b 'invisible s) (length s)))))))
  18878. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18879. (setq s (concat (substring s 0 b)
  18880. (substring s (or (next-single-property-change
  18881. b 'org-cwidth s) (length s))))))
  18882. (setq l (string-width s) b -1)
  18883. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18884. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18885. l))
  18886. (defun org-shorten-string (s maxlength)
  18887. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18888. If the string is shorter or has length MAXLENGTH, just return the
  18889. original string. If it is longer, the functions finds a space in the
  18890. string, breaks this string off at that locations and adds three dots
  18891. as ellipsis. Including the ellipsis, the string will not be longer
  18892. than MAXLENGTH. If finding a good breaking point in the string does
  18893. not work, the string is just chopped off in the middle of a word
  18894. if necessary."
  18895. (if (<= (length s) maxlength)
  18896. s
  18897. (let* ((n (max (- maxlength 4) 1))
  18898. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18899. (if (string-match re s)
  18900. (concat (match-string 1 s) "...")
  18901. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18902. (defun org-get-indentation (&optional line)
  18903. "Get the indentation of the current line, interpreting tabs.
  18904. When LINE is given, assume it represents a line and compute its indentation."
  18905. (if line
  18906. (if (string-match "^ *" (org-remove-tabs line))
  18907. (match-end 0))
  18908. (save-excursion
  18909. (beginning-of-line 1)
  18910. (skip-chars-forward " \t")
  18911. (current-column))))
  18912. (defun org-get-string-indentation (s)
  18913. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18914. (let ((n -1) (i 0) (w tab-width) c)
  18915. (catch 'exit
  18916. (while (< (setq n (1+ n)) (length s))
  18917. (setq c (aref s n))
  18918. (cond ((= c ?\ ) (setq i (1+ i)))
  18919. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18920. (t (throw 'exit t)))))
  18921. i))
  18922. (defun org-remove-tabs (s &optional width)
  18923. "Replace tabulators in S with spaces.
  18924. Assumes that s is a single line, starting in column 0."
  18925. (setq width (or width tab-width))
  18926. (while (string-match "\t" s)
  18927. (setq s (replace-match
  18928. (make-string
  18929. (- (* width (/ (+ (match-beginning 0) width) width))
  18930. (match-beginning 0)) ?\ )
  18931. t t s)))
  18932. s)
  18933. (defun org-fix-indentation (line ind)
  18934. "Fix indentation in LINE.
  18935. IND is a cons cell with target and minimum indentation.
  18936. If the current indentation in LINE is smaller than the minimum,
  18937. leave it alone. If it is larger than ind, set it to the target."
  18938. (let* ((l (org-remove-tabs line))
  18939. (i (org-get-indentation l))
  18940. (i1 (car ind)) (i2 (cdr ind)))
  18941. (if (>= i i2) (setq l (substring line i2)))
  18942. (if (> i1 0)
  18943. (concat (make-string i1 ?\ ) l)
  18944. l)))
  18945. (defun org-remove-indentation (code &optional n)
  18946. "Remove the maximum common indentation from the lines in CODE.
  18947. N may optionally be the number of spaces to remove."
  18948. (with-temp-buffer
  18949. (insert code)
  18950. (org-do-remove-indentation n)
  18951. (buffer-string)))
  18952. (defun org-do-remove-indentation (&optional n)
  18953. "Remove the maximum common indentation from the buffer."
  18954. (untabify (point-min) (point-max))
  18955. (let ((min 10000) re)
  18956. (if n
  18957. (setq min n)
  18958. (goto-char (point-min))
  18959. (while (re-search-forward "^ *[^ \n]" nil t)
  18960. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18961. (unless (or (= min 0) (= min 10000))
  18962. (setq re (format "^ \\{%d\\}" min))
  18963. (goto-char (point-min))
  18964. (while (re-search-forward re nil t)
  18965. (replace-match "")
  18966. (end-of-line 1))
  18967. min)))
  18968. (defun org-fill-template (template alist)
  18969. "Find each %key of ALIST in TEMPLATE and replace it."
  18970. (let ((case-fold-search nil)
  18971. entry key value)
  18972. (setq alist (sort (copy-sequence alist)
  18973. (lambda (a b) (< (length (car a)) (length (car b))))))
  18974. (while (setq entry (pop alist))
  18975. (setq template
  18976. (replace-regexp-in-string
  18977. (concat "%" (regexp-quote (car entry)))
  18978. (or (cdr entry) "") template t t)))
  18979. template))
  18980. (defun org-base-buffer (buffer)
  18981. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18982. (if (not buffer)
  18983. buffer
  18984. (or (buffer-base-buffer buffer)
  18985. buffer)))
  18986. (defun org-trim (s)
  18987. "Remove whitespace at beginning and end of string."
  18988. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18989. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18990. s)
  18991. (defun org-wrap (string &optional width lines)
  18992. "Wrap string to either a number of lines, or a width in characters.
  18993. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18994. that costs. If there is a word longer than WIDTH, the text is actually
  18995. wrapped to the length of that word.
  18996. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18997. many lines, whatever width that takes.
  18998. The return value is a list of lines, without newlines at the end."
  18999. (let* ((words (org-split-string string "[ \t\n]+"))
  19000. (maxword (apply 'max (mapcar 'org-string-width words)))
  19001. w ll)
  19002. (cond (width
  19003. (org-do-wrap words (max maxword width)))
  19004. (lines
  19005. (setq w maxword)
  19006. (setq ll (org-do-wrap words maxword))
  19007. (if (<= (length ll) lines)
  19008. ll
  19009. (setq ll words)
  19010. (while (> (length ll) lines)
  19011. (setq w (1+ w))
  19012. (setq ll (org-do-wrap words w)))
  19013. ll))
  19014. (t (error "Cannot wrap this")))))
  19015. (defun org-do-wrap (words width)
  19016. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19017. (let (lines line)
  19018. (while words
  19019. (setq line (pop words))
  19020. (while (and words (< (+ (length line) (length (car words))) width))
  19021. (setq line (concat line " " (pop words))))
  19022. (setq lines (push line lines)))
  19023. (nreverse lines)))
  19024. (defun org-split-string (string &optional separators)
  19025. "Splits STRING into substrings at SEPARATORS.
  19026. No empty strings are returned if there are matches at the beginning
  19027. and end of string."
  19028. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19029. (start 0)
  19030. notfirst
  19031. (list nil))
  19032. (while (and (string-match rexp string
  19033. (if (and notfirst
  19034. (= start (match-beginning 0))
  19035. (< start (length string)))
  19036. (1+ start) start))
  19037. (< (match-beginning 0) (length string)))
  19038. (setq notfirst t)
  19039. (or (eq (match-beginning 0) 0)
  19040. (and (eq (match-beginning 0) (match-end 0))
  19041. (eq (match-beginning 0) start))
  19042. (setq list
  19043. (cons (substring string start (match-beginning 0))
  19044. list)))
  19045. (setq start (match-end 0)))
  19046. (or (eq start (length string))
  19047. (setq list
  19048. (cons (substring string start)
  19049. list)))
  19050. (nreverse list)))
  19051. (defun org-quote-vert (s)
  19052. "Replace \"|\" with \"\\vert\"."
  19053. (while (string-match "|" s)
  19054. (setq s (replace-match "\\vert" t t s)))
  19055. s)
  19056. (defun org-uuidgen-p (s)
  19057. "Is S an ID created by UUIDGEN?"
  19058. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19059. (defun org-in-src-block-p (&optional inside)
  19060. "Whether point is in a code source block.
  19061. When INSIDE is non-nil, don't consider we are within a src block
  19062. when point is at #+BEGIN_SRC or #+END_SRC."
  19063. (let ((case-fold-search t) ov)
  19064. (or (and (setq ov (overlays-at (point)))
  19065. (memq 'org-block-background
  19066. (overlay-properties (car ov))))
  19067. (and (not inside)
  19068. (save-match-data
  19069. (save-excursion
  19070. (beginning-of-line)
  19071. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19072. (defun org-context ()
  19073. "Return a list of contexts of the current cursor position.
  19074. If several contexts apply, all are returned.
  19075. Each context entry is a list with a symbol naming the context, and
  19076. two positions indicating start and end of the context. Possible
  19077. contexts are:
  19078. :headline anywhere in a headline
  19079. :headline-stars on the leading stars in a headline
  19080. :todo-keyword on a TODO keyword (including DONE) in a headline
  19081. :tags on the TAGS in a headline
  19082. :priority on the priority cookie in a headline
  19083. :item on the first line of a plain list item
  19084. :item-bullet on the bullet/number of a plain list item
  19085. :checkbox on the checkbox in a plain list item
  19086. :table in an org-mode table
  19087. :table-special on a special filed in a table
  19088. :table-table in a table.el table
  19089. :clocktable in a clocktable
  19090. :src-block in a source block
  19091. :link on a hyperlink
  19092. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19093. :target on a <<target>>
  19094. :radio-target on a <<<radio-target>>>
  19095. :latex-fragment on a LaTeX fragment
  19096. :latex-preview on a LaTeX fragment with overlaid preview image
  19097. This function expects the position to be visible because it uses font-lock
  19098. faces as a help to recognize the following contexts: :table-special, :link,
  19099. and :keyword."
  19100. (let* ((f (get-text-property (point) 'face))
  19101. (faces (if (listp f) f (list f)))
  19102. (case-fold-search t)
  19103. (p (point)) clist o)
  19104. ;; First the large context
  19105. (cond
  19106. ((org-at-heading-p t)
  19107. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19108. (when (progn
  19109. (beginning-of-line 1)
  19110. (looking-at org-todo-line-tags-regexp))
  19111. (push (org-point-in-group p 1 :headline-stars) clist)
  19112. (push (org-point-in-group p 2 :todo-keyword) clist)
  19113. (push (org-point-in-group p 4 :tags) clist))
  19114. (goto-char p)
  19115. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19116. (if (looking-at "\\[#[A-Z0-9]\\]")
  19117. (push (org-point-in-group p 0 :priority) clist)))
  19118. ((org-at-item-p)
  19119. (push (org-point-in-group p 2 :item-bullet) clist)
  19120. (push (list :item (point-at-bol)
  19121. (save-excursion (org-end-of-item) (point)))
  19122. clist)
  19123. (and (org-at-item-checkbox-p)
  19124. (push (org-point-in-group p 0 :checkbox) clist)))
  19125. ((org-at-table-p)
  19126. (push (list :table (org-table-begin) (org-table-end)) clist)
  19127. (if (memq 'org-formula faces)
  19128. (push (list :table-special
  19129. (previous-single-property-change p 'face)
  19130. (next-single-property-change p 'face)) clist)))
  19131. ((org-at-table-p 'any)
  19132. (push (list :table-table) clist)))
  19133. (goto-char p)
  19134. (let ((case-fold-search t))
  19135. ;; New the "medium" contexts: clocktables, source blocks
  19136. (cond ((org-in-clocktable-p)
  19137. (push (list :clocktable
  19138. (and (or (looking-at "#\\+BEGIN: clocktable")
  19139. (search-backward "#+BEGIN: clocktable" nil t))
  19140. (match-beginning 0))
  19141. (and (re-search-forward "#\\+END:?" nil t)
  19142. (match-end 0))) clist))
  19143. ((org-in-src-block-p)
  19144. (push (list :src-block
  19145. (and (or (looking-at "#\\+BEGIN_SRC")
  19146. (search-backward "#+BEGIN_SRC" nil t))
  19147. (match-beginning 0))
  19148. (and (search-forward "#+END_SRC" nil t)
  19149. (match-beginning 0))) clist))))
  19150. (goto-char p)
  19151. ;; Now the small context
  19152. (cond
  19153. ((org-at-timestamp-p)
  19154. (push (org-point-in-group p 0 :timestamp) clist))
  19155. ((memq 'org-link faces)
  19156. (push (list :link
  19157. (previous-single-property-change p 'face)
  19158. (next-single-property-change p 'face)) clist))
  19159. ((memq 'org-special-keyword faces)
  19160. (push (list :keyword
  19161. (previous-single-property-change p 'face)
  19162. (next-single-property-change p 'face)) clist))
  19163. ((org-at-target-p)
  19164. (push (org-point-in-group p 0 :target) clist)
  19165. (goto-char (1- (match-beginning 0)))
  19166. (if (looking-at org-radio-target-regexp)
  19167. (push (org-point-in-group p 0 :radio-target) clist))
  19168. (goto-char p))
  19169. ((setq o (car (delq nil
  19170. (mapcar
  19171. (lambda (x)
  19172. (if (memq x org-latex-fragment-image-overlays) x))
  19173. (overlays-at (point))))))
  19174. (push (list :latex-fragment
  19175. (overlay-start o) (overlay-end o)) clist)
  19176. (push (list :latex-preview
  19177. (overlay-start o) (overlay-end o)) clist))
  19178. ((org-inside-LaTeX-fragment-p)
  19179. ;; FIXME: positions wrong.
  19180. (push (list :latex-fragment (point) (point)) clist)))
  19181. (setq clist (nreverse (delq nil clist)))
  19182. clist))
  19183. ;; FIXME: Compare with at-regexp-p Do we need both?
  19184. (defun org-in-regexp (re &optional nlines visually)
  19185. "Check if point is inside a match of regexp.
  19186. Normally only the current line is checked, but you can include NLINES extra
  19187. lines both before and after point into the search.
  19188. If VISUALLY is set, require that the cursor is not after the match but
  19189. really on, so that the block visually is on the match."
  19190. (catch 'exit
  19191. (let ((pos (point))
  19192. (eol (point-at-eol (+ 1 (or nlines 0))))
  19193. (inc (if visually 1 0)))
  19194. (save-excursion
  19195. (beginning-of-line (- 1 (or nlines 0)))
  19196. (while (re-search-forward re eol t)
  19197. (if (and (<= (match-beginning 0) pos)
  19198. (>= (+ inc (match-end 0)) pos))
  19199. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19200. (defun org-at-regexp-p (regexp)
  19201. "Is point inside a match of REGEXP in the current line?"
  19202. (catch 'exit
  19203. (save-excursion
  19204. (let ((pos (point)) (end (point-at-eol)))
  19205. (beginning-of-line 1)
  19206. (while (re-search-forward regexp end t)
  19207. (if (and (<= (match-beginning 0) pos)
  19208. (>= (match-end 0) pos))
  19209. (throw 'exit t)))
  19210. nil))))
  19211. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19212. "Non-nil when point is between matches of START-RE and END-RE.
  19213. Also return a non-nil value when point is on one of the matches.
  19214. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19215. buffer positions. Default values are the positions of headlines
  19216. surrounding the point.
  19217. The functions returns a cons cell whose car (resp. cdr) is the
  19218. position before START-RE (resp. after END-RE)."
  19219. (save-match-data
  19220. (let ((pos (point))
  19221. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19222. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19223. beg end)
  19224. (save-excursion
  19225. ;; Point is on a block when on START-RE or if START-RE can be
  19226. ;; found before it...
  19227. (and (or (org-at-regexp-p start-re)
  19228. (re-search-backward start-re limit-up t))
  19229. (setq beg (match-beginning 0))
  19230. ;; ... and END-RE after it...
  19231. (goto-char (match-end 0))
  19232. (re-search-forward end-re limit-down t)
  19233. (> (setq end (match-end 0)) pos)
  19234. ;; ... without another START-RE in-between.
  19235. (goto-char (match-beginning 0))
  19236. (not (re-search-backward start-re (1+ beg) t))
  19237. ;; Return value.
  19238. (cons beg end))))))
  19239. (defun org-in-block-p (names)
  19240. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19241. NAMES is a list of strings containing names of blocks.
  19242. Return first block name matched, or nil. Beware that in case of
  19243. nested blocks, the returned name may not belong to the closest
  19244. block from point."
  19245. (save-match-data
  19246. (catch 'exit
  19247. (let ((case-fold-search t)
  19248. (lim-up (save-excursion (outline-previous-heading)))
  19249. (lim-down (save-excursion (outline-next-heading))))
  19250. (mapc (lambda (name)
  19251. (let ((n (regexp-quote name)))
  19252. (when (org-between-regexps-p
  19253. (concat "^[ \t]*#\\+begin_" n)
  19254. (concat "^[ \t]*#\\+end_" n)
  19255. lim-up lim-down)
  19256. (throw 'exit n))))
  19257. names))
  19258. nil)))
  19259. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19260. "Call `multi-occur' with buffers for all agenda files."
  19261. (interactive "sOrg-files matching: \np")
  19262. (let* ((files (org-agenda-files))
  19263. (tnames (mapcar 'file-truename files))
  19264. (extra org-agenda-text-search-extra-files)
  19265. f)
  19266. (when (eq (car extra) 'agenda-archives)
  19267. (setq extra (cdr extra))
  19268. (setq files (org-add-archive-files files)))
  19269. (while (setq f (pop extra))
  19270. (unless (member (file-truename f) tnames)
  19271. (add-to-list 'files f 'append)
  19272. (add-to-list 'tnames (file-truename f) 'append)))
  19273. (multi-occur
  19274. (mapcar (lambda (x)
  19275. (with-current-buffer
  19276. (or (get-file-buffer x) (find-file-noselect x))
  19277. (widen)
  19278. (current-buffer)))
  19279. files)
  19280. regexp)))
  19281. (if (boundp 'occur-mode-find-occurrence-hook)
  19282. ;; Emacs 23
  19283. (add-hook 'occur-mode-find-occurrence-hook
  19284. (lambda ()
  19285. (when (derived-mode-p 'org-mode)
  19286. (org-reveal))))
  19287. ;; Emacs 22
  19288. (defadvice occur-mode-goto-occurrence
  19289. (after org-occur-reveal activate)
  19290. (and (derived-mode-p 'org-mode) (org-reveal)))
  19291. (defadvice occur-mode-goto-occurrence-other-window
  19292. (after org-occur-reveal activate)
  19293. (and (derived-mode-p 'org-mode) (org-reveal)))
  19294. (defadvice occur-mode-display-occurrence
  19295. (after org-occur-reveal activate)
  19296. (when (derived-mode-p 'org-mode)
  19297. (let ((pos (occur-mode-find-occurrence)))
  19298. (with-current-buffer (marker-buffer pos)
  19299. (save-excursion
  19300. (goto-char pos)
  19301. (org-reveal)))))))
  19302. (defun org-occur-link-in-agenda-files ()
  19303. "Create a link and search for it in the agendas.
  19304. The link is not stored in `org-stored-links', it is just created
  19305. for the search purpose."
  19306. (interactive)
  19307. (let ((link (condition-case nil
  19308. (org-store-link nil)
  19309. (error "Unable to create a link to here"))))
  19310. (org-occur-in-agenda-files (regexp-quote link))))
  19311. (defun org-uniquify (list)
  19312. "Remove duplicate elements from LIST."
  19313. (let (res)
  19314. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19315. res))
  19316. (defun org-delete-all (elts list)
  19317. "Remove all elements in ELTS from LIST."
  19318. (while elts
  19319. (setq list (delete (pop elts) list)))
  19320. list)
  19321. (defun org-count (cl-item cl-seq)
  19322. "Count the number of occurrences of ITEM in SEQ.
  19323. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19324. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19325. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19326. (while (< cl-start cl-end)
  19327. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19328. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19329. (setq cl-start (1+ cl-start)))
  19330. cl-count))
  19331. (defun org-remove-if (predicate seq)
  19332. "Remove everything from SEQ that fulfills PREDICATE."
  19333. (let (res e)
  19334. (while seq
  19335. (setq e (pop seq))
  19336. (if (not (funcall predicate e)) (push e res)))
  19337. (nreverse res)))
  19338. (defun org-remove-if-not (predicate seq)
  19339. "Remove everything from SEQ that does not fulfill PREDICATE."
  19340. (let (res e)
  19341. (while seq
  19342. (setq e (pop seq))
  19343. (if (funcall predicate e) (push e res)))
  19344. (nreverse res)))
  19345. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19346. "Reduce two-argument FUNCTION across SEQ.
  19347. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19348. \":initial-value\" removed."
  19349. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19350. (cadr (memq :initial-value cl-keys)))
  19351. (cl-seq (pop cl-seq))
  19352. (t (funcall cl-func)))))
  19353. (while cl-seq
  19354. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19355. cl-accum))
  19356. (defun org-back-over-empty-lines ()
  19357. "Move backwards over whitespace, to the beginning of the first empty line.
  19358. Returns the number of empty lines passed."
  19359. (let ((pos (point)))
  19360. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19361. (skip-chars-backward " \t\n\r")
  19362. (unless (eobp)
  19363. (forward-line -1)))
  19364. (beginning-of-line 2)
  19365. (goto-char (min (point) pos))
  19366. (count-lines (point) pos)))
  19367. (defun org-skip-whitespace ()
  19368. (skip-chars-forward " \t\n\r"))
  19369. (defun org-point-in-group (point group &optional context)
  19370. "Check if POINT is in match-group GROUP.
  19371. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19372. match. If the match group does not exist or point is not inside it,
  19373. return nil."
  19374. (and (match-beginning group)
  19375. (>= point (match-beginning group))
  19376. (<= point (match-end group))
  19377. (if context
  19378. (list context (match-beginning group) (match-end group))
  19379. t)))
  19380. (defun org-switch-to-buffer-other-window (&rest args)
  19381. "Switch to buffer in a second window on the current frame.
  19382. In particular, do not allow pop-up frames.
  19383. Returns the newly created buffer."
  19384. (org-no-popups
  19385. (apply 'switch-to-buffer-other-window args)))
  19386. (defun org-combine-plists (&rest plists)
  19387. "Create a single property list from all plists in PLISTS.
  19388. The process starts by copying the first list, and then setting properties
  19389. from the other lists. Settings in the last list are the most significant
  19390. ones and overrule settings in the other lists."
  19391. (let ((rtn (copy-sequence (pop plists)))
  19392. p v ls)
  19393. (while plists
  19394. (setq ls (pop plists))
  19395. (while ls
  19396. (setq p (pop ls) v (pop ls))
  19397. (setq rtn (plist-put rtn p v))))
  19398. rtn))
  19399. (defun org-replace-escapes (string table)
  19400. "Replace %-escapes in STRING with values in TABLE.
  19401. TABLE is an association list with keys like \"%a\" and string values.
  19402. The sequences in STRING may contain normal field width and padding information,
  19403. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19404. so values can contain further %-escapes if they are define later in TABLE."
  19405. (let ((tbl (copy-alist table))
  19406. (case-fold-search nil)
  19407. (pchg 0)
  19408. e re rpl)
  19409. (while (setq e (pop tbl))
  19410. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19411. (when (and (cdr e) (string-match re (cdr e)))
  19412. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19413. (safe "SREF"))
  19414. (add-text-properties 0 3 (list 'sref sref) safe)
  19415. (setcdr e (replace-match safe t t (cdr e)))))
  19416. (while (string-match re string)
  19417. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19418. (cdr e)))
  19419. (setq string (replace-match rpl t t string))))
  19420. (while (setq pchg (next-property-change pchg string))
  19421. (let ((sref (get-text-property pchg 'sref string)))
  19422. (when (and sref (string-match "SREF" string pchg))
  19423. (setq string (replace-match sref t t string)))))
  19424. string))
  19425. (defun org-sublist (list start end)
  19426. "Return a section of LIST, from START to END.
  19427. Counting starts at 1."
  19428. (let (rtn (c start))
  19429. (setq list (nthcdr (1- start) list))
  19430. (while (and list (<= c end))
  19431. (push (pop list) rtn)
  19432. (setq c (1+ c)))
  19433. (nreverse rtn)))
  19434. (defun org-find-base-buffer-visiting (file)
  19435. "Like `find-buffer-visiting' but always return the base buffer and
  19436. not an indirect buffer."
  19437. (let ((buf (or (get-file-buffer file)
  19438. (find-buffer-visiting file))))
  19439. (if buf
  19440. (or (buffer-base-buffer buf) buf)
  19441. nil)))
  19442. (defun org-image-file-name-regexp (&optional extensions)
  19443. "Return regexp matching the file names of images.
  19444. If EXTENSIONS is given, only match these."
  19445. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19446. (image-file-name-regexp)
  19447. (let ((image-file-name-extensions
  19448. (or extensions
  19449. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19450. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19451. (concat "\\."
  19452. (regexp-opt (nconc (mapcar 'upcase
  19453. image-file-name-extensions)
  19454. image-file-name-extensions)
  19455. t)
  19456. "\\'"))))
  19457. (defun org-file-image-p (file &optional extensions)
  19458. "Return non-nil if FILE is an image."
  19459. (save-match-data
  19460. (string-match (org-image-file-name-regexp extensions) file)))
  19461. (defun org-get-cursor-date (&optional with-time)
  19462. "Return the date at cursor in as a time.
  19463. This works in the calendar and in the agenda, anywhere else it just
  19464. returns the current time.
  19465. If WITH-TIME is non-nil, returns the time of the event at point (in
  19466. the agenda) or the current time of the day."
  19467. (let (date day defd tp tm hod mod)
  19468. (when with-time
  19469. (setq tp (get-text-property (point) 'time))
  19470. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19471. (setq hod (string-to-number (match-string 1 tp))
  19472. mod (string-to-number (match-string 2 tp))))
  19473. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19474. mod (nth 1 (decode-time (current-time))))))
  19475. (cond
  19476. ((eq major-mode 'calendar-mode)
  19477. (setq date (calendar-cursor-to-date)
  19478. defd (encode-time 0 (or mod 0) (or hod 0)
  19479. (nth 1 date) (nth 0 date) (nth 2 date))))
  19480. ((eq major-mode 'org-agenda-mode)
  19481. (setq day (get-text-property (point) 'day))
  19482. (if day
  19483. (setq date (calendar-gregorian-from-absolute day)
  19484. defd (encode-time 0 (or mod 0) (or hod 0)
  19485. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19486. (or defd (current-time))))
  19487. (defun org-mark-subtree (&optional up)
  19488. "Mark the current subtree.
  19489. This puts point at the start of the current subtree, and mark at
  19490. the end. If a numeric prefix UP is given, move up into the
  19491. hierarchy of headlines by UP levels before marking the subtree."
  19492. (interactive "P")
  19493. (org-with-limited-levels
  19494. (cond ((org-at-heading-p) (beginning-of-line))
  19495. ((org-before-first-heading-p) (error "Not in a subtree"))
  19496. (t (outline-previous-visible-heading 1))))
  19497. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19498. (if (org-called-interactively-p 'any)
  19499. (call-interactively 'org-mark-element)
  19500. (org-mark-element)))
  19501. ;;; Macros
  19502. ;; Macros are expanded with `org-macro-replace-all', which relies
  19503. ;; internally on `org-macro-expand'.
  19504. ;; Default templates for expansion are stored in the buffer-local
  19505. ;; variable `org-macro-templates'. This variable is updated by
  19506. ;; `org-macro-initialize-templates'.
  19507. ;; Along with macros defined through #+MACRO: keyword, default
  19508. ;; templates include the following hard-coded macros:
  19509. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19510. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19511. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19512. ;; {{{title}}} will also be provided.
  19513. (defvar org-macro-templates nil
  19514. "Alist containing all macro templates in current buffer.
  19515. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19516. stands for macro's name and template for its replacement value,
  19517. both as strings. This is an internal variable. Do not set it
  19518. directly, use instead:
  19519. #+MACRO: name template")
  19520. (make-variable-buffer-local 'org-macro-templates)
  19521. (defun org-macro-expand (macro templates)
  19522. "Return expanded MACRO, as a string.
  19523. MACRO is an object, obtained, for example, with
  19524. `org-element-context'. TEMPLATES is an alist of templates used
  19525. for expansion. See `org-macro-templates' for a buffer-local
  19526. default value. Return nil if no template was found."
  19527. (let ((template
  19528. ;; Macro names are case-insensitive.
  19529. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19530. (when template
  19531. (let ((value (replace-regexp-in-string
  19532. "\\$[0-9]+"
  19533. (lambda (arg)
  19534. (or (nth (1- (string-to-number (substring arg 1)))
  19535. (org-element-property :args macro))
  19536. ;; No argument provided: remove
  19537. ;; place-holder.
  19538. ""))
  19539. template)))
  19540. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19541. (when (string-match "\\`(eval\\>" value)
  19542. (setq value (eval (read value))))
  19543. ;; Return string.
  19544. (format "%s" (or value ""))))))
  19545. (defun org-macro-replace-all (templates)
  19546. "Replace all macros in current buffer by their expansion.
  19547. TEMPLATES is an alist of templates used for expansion. See
  19548. `org-macro-templates' for a buffer-local default value."
  19549. (save-excursion
  19550. (goto-char (point-min))
  19551. (let (record)
  19552. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19553. (let ((object (org-element-context)))
  19554. (when (eq (org-element-type object) 'macro)
  19555. (let* ((value (org-macro-expand object templates))
  19556. (begin (org-element-property :begin object))
  19557. (signature (list begin
  19558. object
  19559. (org-element-property :args object))))
  19560. ;; Avoid circular dependencies by checking if the same
  19561. ;; macro with the same arguments is expanded at the same
  19562. ;; position twice.
  19563. (if (member signature record)
  19564. (error "Circular macro expansion: %s"
  19565. (org-element-property :key object))
  19566. (when value
  19567. (push signature record)
  19568. (delete-region
  19569. begin
  19570. ;; Preserve white spaces after the macro.
  19571. (progn (goto-char (org-element-property :end object))
  19572. (skip-chars-backward " \t")
  19573. (point)))
  19574. ;; Leave point before replacement in case of recursive
  19575. ;; expansions.
  19576. (save-excursion (insert value)))))))))))
  19577. (defun org-macro-initialize-templates ()
  19578. "Collect macro templates defined in current buffer.
  19579. Templates are stored in buffer-local variable
  19580. `org-macro-templates'. In addition to buffer-defined macros, the
  19581. function installs the following ones: \"property\",
  19582. \"time\". and, if the buffer is associated to a file,
  19583. \"input-file\" and \"modification-time\"."
  19584. (let ((case-fold-search t)
  19585. (set-template
  19586. (lambda (cell)
  19587. ;; Add CELL to `org-macro-templates' if there's no
  19588. ;; association matching its name already. Otherwise,
  19589. ;; replace old association with the new one in that
  19590. ;; variable.
  19591. (let ((old-template (assoc (car cell) org-macro-templates)))
  19592. (if old-template (setcdr old-template (cdr cell))
  19593. (push cell org-macro-templates))))))
  19594. ;; Install buffer-local macros.
  19595. (org-with-wide-buffer
  19596. (goto-char (point-min))
  19597. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19598. (let ((element (org-element-at-point)))
  19599. (when (eq (org-element-type element) 'keyword)
  19600. (let ((value (org-element-property :value element)))
  19601. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19602. (funcall set-template
  19603. (cons (match-string 1 value)
  19604. (or (match-string 2 value) "")))))))))
  19605. ;; Install hard-coded macros.
  19606. (mapc (lambda (cell) (funcall set-template cell))
  19607. (list
  19608. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19609. (cons "time" "(eval (format-time-string \"$1\"))")))
  19610. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19611. (when (and visited-file (file-exists-p visited-file))
  19612. (mapc (lambda (cell) (funcall set-template cell))
  19613. (list
  19614. (cons "input-file" (file-name-nondirectory visited-file))
  19615. (cons "modification-time"
  19616. (format "(eval (format-time-string \"$1\" '%s))"
  19617. (prin1-to-string
  19618. (nth 5 (file-attributes visited-file)))))))))))
  19619. ;;; Indentation
  19620. (defun org-indent-line ()
  19621. "Indent line depending on context."
  19622. (interactive)
  19623. (let* ((pos (point))
  19624. (itemp (org-at-item-p))
  19625. (case-fold-search t)
  19626. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19627. (inline-task-p (and (featurep 'org-inlinetask)
  19628. (org-inlinetask-in-task-p)))
  19629. (inline-re (and inline-task-p
  19630. (org-inlinetask-outline-regexp)))
  19631. column)
  19632. (if (and orgstruct-is-++ (eq pos (point)))
  19633. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19634. (indent-according-to-mode))
  19635. (beginning-of-line 1)
  19636. (cond
  19637. ;; Headings
  19638. ((looking-at org-outline-regexp) (setq column 0))
  19639. ;; Included files
  19640. ((looking-at "#\\+include:") (setq column 0))
  19641. ;; Footnote definition
  19642. ((looking-at org-footnote-definition-re) (setq column 0))
  19643. ;; Literal examples
  19644. ((looking-at "[ \t]*:\\( \\|$\\)")
  19645. (setq column (org-get-indentation))) ; do nothing
  19646. ;; Lists
  19647. ((ignore-errors (goto-char (org-in-item-p)))
  19648. (setq column (if itemp
  19649. (org-get-indentation)
  19650. (org-list-item-body-column (point))))
  19651. (goto-char pos))
  19652. ;; Drawers
  19653. ((and (looking-at "[ \t]*:END:")
  19654. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19655. (save-excursion
  19656. (goto-char (1- (match-beginning 1)))
  19657. (setq column (current-column))))
  19658. ;; Special blocks
  19659. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19660. (save-excursion
  19661. (re-search-backward
  19662. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19663. (setq column (org-get-indentation (match-string 0))))
  19664. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19665. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19666. (save-excursion
  19667. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19668. (setq column
  19669. (cond ((equal (downcase (match-string 1)) "src")
  19670. ;; src blocks: let `org-edit-src-exit' handle them
  19671. (org-get-indentation))
  19672. ((equal (downcase (match-string 1)) "example")
  19673. (max (org-get-indentation)
  19674. (org-get-indentation (match-string 0))))
  19675. (t
  19676. (org-get-indentation (match-string 0))))))
  19677. ;; This line has nothing special, look at the previous relevant
  19678. ;; line to compute indentation
  19679. (t
  19680. (beginning-of-line 0)
  19681. (while (and (not (bobp))
  19682. (not (looking-at org-table-line-regexp))
  19683. (not (looking-at org-drawer-regexp))
  19684. ;; When point started in an inline task, do not move
  19685. ;; above task starting line.
  19686. (not (and inline-task-p (looking-at inline-re)))
  19687. ;; Skip drawers, blocks, empty lines, verbatim,
  19688. ;; comments, tables, footnotes definitions, lists,
  19689. ;; inline tasks.
  19690. (or (and (looking-at "[ \t]*:END:")
  19691. (re-search-backward org-drawer-regexp nil t))
  19692. (and (looking-at "[ \t]*#\\+end_")
  19693. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19694. (looking-at "[ \t]*[\n:#|]")
  19695. (looking-at org-footnote-definition-re)
  19696. (and (ignore-errors (goto-char (org-in-item-p)))
  19697. (goto-char
  19698. (org-list-get-top-point (org-list-struct))))
  19699. (and (not inline-task-p)
  19700. (featurep 'org-inlinetask)
  19701. (org-inlinetask-in-task-p)
  19702. (or (org-inlinetask-goto-beginning) t))))
  19703. (beginning-of-line 0))
  19704. (cond
  19705. ;; There was an heading above.
  19706. ((looking-at "\\*+[ \t]+")
  19707. (if (not org-adapt-indentation)
  19708. (setq column 0)
  19709. (goto-char (match-end 0))
  19710. (setq column (current-column))))
  19711. ;; A drawer had started and is unfinished
  19712. ((looking-at org-drawer-regexp)
  19713. (goto-char (1- (match-beginning 1)))
  19714. (setq column (current-column)))
  19715. ;; Else, nothing noticeable found: get indentation and go on.
  19716. (t (setq column (org-get-indentation))))))
  19717. ;; Now apply indentation and move cursor accordingly
  19718. (goto-char pos)
  19719. (if (<= (current-column) (current-indentation))
  19720. (org-indent-line-to column)
  19721. (save-excursion (org-indent-line-to column)))
  19722. ;; Special polishing for properties, see `org-property-format'
  19723. (setq column (current-column))
  19724. (beginning-of-line 1)
  19725. (if (looking-at
  19726. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19727. (replace-match (concat (match-string 1)
  19728. (format org-property-format
  19729. (match-string 2) (match-string 3)))
  19730. t t))
  19731. (org-move-to-column column))))
  19732. (defun org-indent-drawer ()
  19733. "Indent the drawer at point."
  19734. (interactive)
  19735. (let ((p (point))
  19736. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19737. (match-end 0)))
  19738. (folded
  19739. (save-excursion
  19740. (end-of-line)
  19741. (when (overlays-at (point))
  19742. (member 'invisible (overlay-properties
  19743. (car (overlays-at (point)))))))))
  19744. (when folded (org-cycle))
  19745. (indent-for-tab-command)
  19746. (while (and (move-beginning-of-line 2) (< (point) e))
  19747. (indent-for-tab-command))
  19748. (goto-char p)
  19749. (when folded (org-cycle)))
  19750. (message "Drawer at point indented"))
  19751. (defun org-indent-block ()
  19752. "Indent the block at point."
  19753. (interactive)
  19754. (let ((p (point))
  19755. (case-fold-search t)
  19756. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19757. (match-end 0)))
  19758. (folded
  19759. (save-excursion
  19760. (end-of-line)
  19761. (when (overlays-at (point))
  19762. (member 'invisible (overlay-properties
  19763. (car (overlays-at (point)))))))))
  19764. (when folded (org-cycle))
  19765. (indent-for-tab-command)
  19766. (while (and (move-beginning-of-line 2) (< (point) e))
  19767. (indent-for-tab-command))
  19768. (goto-char p)
  19769. (when folded (org-cycle)))
  19770. (message "Block at point indented"))
  19771. (defun org-indent-region (start end)
  19772. "Indent region."
  19773. (interactive "r")
  19774. (save-excursion
  19775. (let ((line-end (org-current-line end)))
  19776. (goto-char start)
  19777. (while (< (org-current-line) line-end)
  19778. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19779. (t (call-interactively 'org-indent-line)))
  19780. (move-beginning-of-line 2)))))
  19781. ;;; Filling
  19782. ;; We use our own fill-paragraph and auto-fill functions.
  19783. ;; `org-fill-paragraph' relies on adaptive filling and context
  19784. ;; checking. Appropriate `fill-prefix' is computed with
  19785. ;; `org-adaptive-fill-function'.
  19786. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19787. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19788. ;; `org-adaptive-fill-function' value. Internally,
  19789. ;; `org-comment-line-break-function' breaks the line.
  19790. ;; `org-setup-filling' installs filling and auto-filling related
  19791. ;; variables during `org-mode' initialization.
  19792. (defun org-setup-filling ()
  19793. (interactive)
  19794. ;; Prevent auto-fill from inserting unwanted new items.
  19795. (when (boundp 'fill-nobreak-predicate)
  19796. (org-set-local
  19797. 'fill-nobreak-predicate
  19798. (org-uniquify
  19799. (append fill-nobreak-predicate
  19800. '(org-fill-paragraph-separate-nobreak-p
  19801. org-fill-line-break-nobreak-p)))))
  19802. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19803. (org-set-local 'auto-fill-inhibit-regexp nil)
  19804. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19805. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19806. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19807. (defvar org-element-paragraph-separate) ; org-element.el
  19808. (defun org-fill-paragraph-separate-nobreak-p ()
  19809. "Non-nil when a line break at point would insert a new item."
  19810. (looking-at (substring org-element-paragraph-separate 1)))
  19811. (defun org-fill-line-break-nobreak-p ()
  19812. "Non-nil when a line break at point would create an Org line break."
  19813. (save-excursion
  19814. (skip-chars-backward "[ \t]")
  19815. (skip-chars-backward "\\\\")
  19816. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19817. (declare-function message-in-body-p "message" ())
  19818. (defun org-adaptive-fill-function ()
  19819. "Compute a fill prefix for the current line.
  19820. Return fill prefix, as a string, or nil if current line isn't
  19821. meant to be filled."
  19822. (let (prefix)
  19823. (when (and (derived-mode-p 'message-mode) (message-in-body-p))
  19824. (save-excursion
  19825. (beginning-of-line)
  19826. (cond ((looking-at message-cite-prefix-regexp)
  19827. (setq prefix (match-string-no-properties 0)))
  19828. ((looking-at org-outline-regexp)
  19829. (setq prefix "")))))
  19830. (or prefix
  19831. (org-with-wide-buffer
  19832. (let* ((p (line-beginning-position))
  19833. (element (save-excursion (beginning-of-line)
  19834. (org-element-at-point)))
  19835. (type (org-element-type element))
  19836. (post-affiliated (org-element-property :post-affiliated element)))
  19837. (unless (and post-affiliated (< p post-affiliated))
  19838. (case type
  19839. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19840. (footnote-definition "")
  19841. ((item plain-list)
  19842. (make-string (org-list-item-body-column
  19843. (or post-affiliated
  19844. (org-element-property :begin element)))
  19845. ? ))
  19846. (paragraph
  19847. ;; Fill prefix is usually the same as the current line,
  19848. ;; except if the paragraph is at the beginning of an item.
  19849. (let ((parent (org-element-property :parent element)))
  19850. (cond ((eq (org-element-type parent) 'item)
  19851. (make-string (org-list-item-body-column
  19852. (org-element-property :begin parent))
  19853. ? ))
  19854. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19855. (match-string 0))
  19856. (t ""))))
  19857. (comment-block
  19858. ;; Only fill contents if P is within block boundaries.
  19859. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19860. (forward-line)
  19861. (point)))
  19862. (cend (save-excursion
  19863. (goto-char (org-element-property :end element))
  19864. (skip-chars-backward " \r\t\n")
  19865. (line-beginning-position))))
  19866. (when (and (>= p cbeg) (< p cend))
  19867. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19868. (match-string 0)
  19869. "")))))))))))
  19870. (declare-function message-goto-body "message" ())
  19871. (defvar message-cite-prefix-regexp) ; From message.el
  19872. (defvar org-element-all-objects) ; From org-element.el
  19873. (defun org-fill-paragraph (&optional justify)
  19874. "Fill element at point, when applicable.
  19875. This function only applies to comment blocks, comments, example
  19876. blocks and paragraphs. Also, as a special case, re-align table
  19877. when point is at one.
  19878. If JUSTIFY is non-nil (interactively, with prefix argument),
  19879. justify as well. If `sentence-end-double-space' is non-nil, then
  19880. period followed by one space does not end a sentence, so don't
  19881. break a line there. The variable `fill-column' controls the
  19882. width for filling.
  19883. For convenience, when point is at a plain list, an item or
  19884. a footnote definition, try to fill the first paragraph within."
  19885. (interactive)
  19886. (if (and (derived-mode-p 'message-mode)
  19887. (or (not (message-in-body-p))
  19888. (save-excursion (move-beginning-of-line 1)
  19889. (looking-at message-cite-prefix-regexp))))
  19890. ;; First ensure filling is correct in message-mode.
  19891. (let ((fill-paragraph-function
  19892. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19893. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19894. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19895. (paragraph-separate
  19896. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19897. (fill-paragraph nil))
  19898. (save-excursion
  19899. ;; Move to end of line in order to get the first paragraph
  19900. ;; within a plain list or a footnote definition.
  19901. (end-of-line)
  19902. (let ((element (org-element-at-point)))
  19903. ;; First check if point is in a blank line at the beginning of
  19904. ;; the buffer. In that case, ignore filling.
  19905. (if (< (point) (org-element-property :begin element)) t
  19906. (case (org-element-type element)
  19907. ;; Use major mode filling function is src blocks.
  19908. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19909. ;; Align Org tables, leave table.el tables as-is.
  19910. (table-row (org-table-align) t)
  19911. (table
  19912. (when (eq (org-element-property :type element) 'org)
  19913. (org-table-align))
  19914. t)
  19915. (paragraph
  19916. ;; Paragraphs may contain `line-break' type objects.
  19917. (let ((beg (max (point-min)
  19918. (org-element-property :contents-begin element)))
  19919. (end (min (point-max)
  19920. (org-element-property :contents-end element))))
  19921. ;; Do nothing if point is at an affiliated keyword.
  19922. (if (< (point) beg) t
  19923. (when (derived-mode-p 'message-mode)
  19924. ;; In `message-mode', do not fill following
  19925. ;; citation in current paragraph nor text before
  19926. ;; message body.
  19927. (let ((body-start (save-excursion (message-goto-body))))
  19928. (when body-start (setq beg (max body-start beg))))
  19929. (when (save-excursion
  19930. (re-search-forward
  19931. (concat "^" message-cite-prefix-regexp) end t))
  19932. (setq end (match-beginning 0))))
  19933. ;; Fill paragraph, taking line breaks into
  19934. ;; consideration. For that, slice the paragraph
  19935. ;; using line breaks as separators, and fill the
  19936. ;; parts in reverse order to avoid messing with
  19937. ;; markers.
  19938. (save-excursion
  19939. (goto-char end)
  19940. (mapc
  19941. (lambda (pos)
  19942. (fill-region-as-paragraph pos (point) justify)
  19943. (goto-char pos))
  19944. ;; Find the list of ending positions for line
  19945. ;; breaks in the current paragraph. Add paragraph
  19946. ;; beginning to include first slice.
  19947. (nreverse
  19948. (cons
  19949. beg
  19950. (org-element-map
  19951. (org-element--parse-objects
  19952. beg end nil org-element-all-objects)
  19953. 'line-break
  19954. (lambda (lb) (org-element-property :end lb)))))))
  19955. t)))
  19956. ;; Contents of `comment-block' type elements should be
  19957. ;; filled as plain text, but only if point is within block
  19958. ;; markers.
  19959. (comment-block
  19960. (let* ((case-fold-search t)
  19961. (beg (save-excursion
  19962. (goto-char (org-element-property :begin element))
  19963. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19964. (forward-line)
  19965. (point)))
  19966. (end (save-excursion
  19967. (goto-char (org-element-property :end element))
  19968. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19969. (line-beginning-position))))
  19970. (when (and (>= (point) beg) (< (point) end))
  19971. (fill-region-as-paragraph
  19972. (save-excursion
  19973. (end-of-line)
  19974. (re-search-backward "^[ \t]*$" beg 'move)
  19975. (line-beginning-position))
  19976. (save-excursion
  19977. (beginning-of-line)
  19978. (re-search-forward "^[ \t]*$" end 'move)
  19979. (line-beginning-position))
  19980. justify)))
  19981. t)
  19982. ;; Fill comments.
  19983. (comment (fill-comment-paragraph justify))
  19984. ;; Ignore every other element.
  19985. (otherwise t)))))))
  19986. (defun org-auto-fill-function ()
  19987. "Auto-fill function."
  19988. ;; Check if auto-filling is meaningful.
  19989. (let ((fc (current-fill-column)))
  19990. (when (and fc (> (current-column) fc))
  19991. (let* ((fill-prefix (org-adaptive-fill-function))
  19992. ;; Enforce empty fill prefix, if required. Otherwise, it
  19993. ;; will be computed again.
  19994. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19995. (when fill-prefix (do-auto-fill))))))
  19996. (defun org-comment-line-break-function (&optional soft)
  19997. "Break line at point and indent, continuing comment if within one.
  19998. The inserted newline is marked hard if variable
  19999. `use-hard-newlines' is true, unless optional argument SOFT is
  20000. non-nil."
  20001. (if soft (insert-and-inherit ?\n) (newline 1))
  20002. (save-excursion (forward-char -1) (delete-horizontal-space))
  20003. (delete-horizontal-space)
  20004. (indent-to-left-margin)
  20005. (insert-before-markers-and-inherit fill-prefix))
  20006. ;;; Comments
  20007. ;; Org comments syntax is quite complex. It requires the entire line
  20008. ;; to be just a comment. Also, even with the right syntax at the
  20009. ;; beginning of line, some some elements (i.e. verse-block or
  20010. ;; example-block) don't accept comments. Usual Emacs comment commands
  20011. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20012. ;; Org still relies on `comment-dwim', but cannot trust
  20013. ;; `comment-only-p'. So, `comment-region-function' and
  20014. ;; `uncomment-region-function' both point
  20015. ;; to`org-comment-or-uncomment-region'. Eventually,
  20016. ;; `org-insert-comment' takes care of insertion of comments at the
  20017. ;; beginning of line.
  20018. ;; `org-setup-comments-handling' install comments related variables
  20019. ;; during `org-mode' initialization.
  20020. (defun org-setup-comments-handling ()
  20021. (interactive)
  20022. (org-set-local 'comment-use-syntax nil)
  20023. (org-set-local 'comment-start "# ")
  20024. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20025. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20026. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20027. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20028. (defun org-insert-comment ()
  20029. "Insert an empty comment above current line.
  20030. If the line is empty, insert comment at its beginning."
  20031. (beginning-of-line)
  20032. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20033. (org-indent-line)
  20034. (insert "# "))
  20035. (defvar comment-empty-lines) ; From newcomment.el.
  20036. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20037. "Comment or uncomment each non-blank line in the region.
  20038. Uncomment each non-blank line between BEG and END if it only
  20039. contains commented lines. Otherwise, comment them."
  20040. (save-restriction
  20041. ;; Restrict region
  20042. (narrow-to-region (save-excursion (goto-char beg)
  20043. (skip-chars-forward " \r\t\n" end)
  20044. (line-beginning-position))
  20045. (save-excursion (goto-char end)
  20046. (skip-chars-backward " \r\t\n" beg)
  20047. (line-end-position)))
  20048. (let ((uncommentp
  20049. ;; UNCOMMENTP is non-nil when every non blank line between
  20050. ;; BEG and END is a comment.
  20051. (save-excursion
  20052. (goto-char (point-min))
  20053. (while (and (not (eobp))
  20054. (let ((element (org-element-at-point)))
  20055. (and (eq (org-element-type element) 'comment)
  20056. (goto-char (min (point-max)
  20057. (org-element-property
  20058. :end element)))))))
  20059. (eobp))))
  20060. (if uncommentp
  20061. ;; Only blank lines and comments in region: uncomment it.
  20062. (save-excursion
  20063. (goto-char (point-min))
  20064. (while (not (eobp))
  20065. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20066. (replace-match "" nil nil nil 1))
  20067. (forward-line)))
  20068. ;; Comment each line in region.
  20069. (let ((min-indent (point-max)))
  20070. ;; First find the minimum indentation across all lines.
  20071. (save-excursion
  20072. (goto-char (point-min))
  20073. (while (and (not (eobp)) (not (zerop min-indent)))
  20074. (unless (looking-at "[ \t]*$")
  20075. (setq min-indent (min min-indent (current-indentation))))
  20076. (forward-line)))
  20077. ;; Then loop over all lines.
  20078. (save-excursion
  20079. (goto-char (point-min))
  20080. (while (not (eobp))
  20081. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20082. (org-move-to-column min-indent t)
  20083. (insert comment-start))
  20084. (forward-line))))))))
  20085. ;;; Other stuff.
  20086. (defun org-toggle-fixed-width-section (arg)
  20087. "Toggle the fixed-width export.
  20088. If there is no active region, the QUOTE keyword at the current headline is
  20089. inserted or removed. When present, it causes the text between this headline
  20090. and the next to be exported as fixed-width text, and unmodified.
  20091. If there is an active region, this command adds or removes a colon as the
  20092. first character of this line. If the first character of a line is a colon,
  20093. this line is also exported in fixed-width font."
  20094. (interactive "P")
  20095. (let* ((cc 0)
  20096. (regionp (org-region-active-p))
  20097. (beg (if regionp (region-beginning) (point)))
  20098. (end (if regionp (region-end)))
  20099. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20100. (case-fold-search nil)
  20101. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20102. off)
  20103. (if regionp
  20104. (save-excursion
  20105. (goto-char beg)
  20106. (setq cc (current-column))
  20107. (beginning-of-line 1)
  20108. (setq off (looking-at re))
  20109. (while (> nlines 0)
  20110. (setq nlines (1- nlines))
  20111. (beginning-of-line 1)
  20112. (cond
  20113. (arg
  20114. (org-move-to-column cc t)
  20115. (insert ": \n")
  20116. (forward-line -1))
  20117. ((and off (looking-at re))
  20118. (replace-match "" t t nil 1))
  20119. ((not off) (org-move-to-column cc t) (insert ": ")))
  20120. (forward-line 1)))
  20121. (save-excursion
  20122. (org-back-to-heading)
  20123. (cond
  20124. ((looking-at (format org-heading-keyword-regexp-format
  20125. org-quote-string))
  20126. (goto-char (match-end 1))
  20127. (looking-at (concat " +" org-quote-string))
  20128. (replace-match "" t t)
  20129. (when (eolp) (insert " ")))
  20130. ((looking-at org-outline-regexp)
  20131. (goto-char (match-end 0))
  20132. (insert org-quote-string " ")))))))
  20133. (defun org-reftex-citation ()
  20134. "Use reftex-citation to insert a citation into the buffer.
  20135. This looks for a line like
  20136. #+BIBLIOGRAPHY: foo plain option:-d
  20137. and derives from it that foo.bib is the bibliography file relevant
  20138. for this document. It then installs the necessary environment for RefTeX
  20139. to work in this buffer and calls `reftex-citation' to insert a citation
  20140. into the buffer.
  20141. Export of such citations to both LaTeX and HTML is handled by the contributed
  20142. package org-exp-bibtex by Taru Karttunen."
  20143. (interactive)
  20144. (let ((reftex-docstruct-symbol 'rds)
  20145. (reftex-cite-format "\\cite{%l}")
  20146. rds bib)
  20147. (save-excursion
  20148. (save-restriction
  20149. (widen)
  20150. (let ((case-fold-search t)
  20151. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20152. (if (not (save-excursion
  20153. (or (re-search-forward re nil t)
  20154. (re-search-backward re nil t))))
  20155. (error "No bibliography defined in file")
  20156. (setq bib (concat (match-string 1) ".bib")
  20157. rds (list (list 'bib bib)))))))
  20158. (call-interactively 'reftex-citation)))
  20159. ;;;; Functions extending outline functionality
  20160. (defun org-beginning-of-line (&optional arg)
  20161. "Go to the beginning of the current line. If that is invisible, continue
  20162. to a visible line beginning. This makes the function of C-a more intuitive.
  20163. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20164. first attempt, and only move to after the tags when the cursor is already
  20165. beyond the end of the headline."
  20166. (interactive "P")
  20167. (let ((pos (point))
  20168. (special (if (consp org-special-ctrl-a/e)
  20169. (car org-special-ctrl-a/e)
  20170. org-special-ctrl-a/e))
  20171. refpos)
  20172. (if (org-bound-and-true-p visual-line-mode)
  20173. (beginning-of-visual-line 1)
  20174. (beginning-of-line 1))
  20175. (if (and arg (fboundp 'move-beginning-of-line))
  20176. (call-interactively 'move-beginning-of-line)
  20177. (if (bobp)
  20178. nil
  20179. (backward-char 1)
  20180. (if (org-truely-invisible-p)
  20181. (while (and (not (bobp)) (org-truely-invisible-p))
  20182. (backward-char 1)
  20183. (beginning-of-line 1))
  20184. (forward-char 1))))
  20185. (when special
  20186. (cond
  20187. ((and (looking-at org-complex-heading-regexp)
  20188. (= (char-after (match-end 1)) ?\ ))
  20189. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20190. (point-at-eol)))
  20191. (goto-char
  20192. (if (eq special t)
  20193. (cond ((> pos refpos) refpos)
  20194. ((= pos (point)) refpos)
  20195. (t (point)))
  20196. (cond ((> pos (point)) (point))
  20197. ((not (eq last-command this-command)) (point))
  20198. (t refpos)))))
  20199. ((org-at-item-p)
  20200. ;; Being at an item and not looking at an the item means point
  20201. ;; was previously moved to beginning of a visual line, which
  20202. ;; doesn't contain the item. Therefore, do nothing special,
  20203. ;; just stay here.
  20204. (when (looking-at org-list-full-item-re)
  20205. ;; Set special position at first white space character after
  20206. ;; bullet, and check-box, if any.
  20207. (let ((after-bullet
  20208. (let ((box (match-end 3)))
  20209. (if (not box) (match-end 1)
  20210. (let ((after (char-after box)))
  20211. (if (and after (= after ? )) (1+ box) box))))))
  20212. ;; Special case: Move point to special position when
  20213. ;; currently after it or at beginning of line.
  20214. (if (eq special t)
  20215. (when (or (> pos after-bullet) (= (point) pos))
  20216. (goto-char after-bullet))
  20217. ;; Reversed case: Move point to special position when
  20218. ;; point was already at beginning of line and command is
  20219. ;; repeated.
  20220. (when (and (= (point) pos) (eq last-command this-command))
  20221. (goto-char after-bullet))))))))
  20222. (org-no-warnings
  20223. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20224. (defun org-end-of-line (&optional arg)
  20225. "Go to the end of the line.
  20226. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20227. tags on the first attempt, and only move to after the tags when
  20228. the cursor is already beyond the end of the headline."
  20229. (interactive "P")
  20230. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20231. org-special-ctrl-a/e))
  20232. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20233. 'end-of-visual-line)
  20234. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20235. (t 'end-of-line))))
  20236. (if (or (not special) arg) (call-interactively move-fun)
  20237. (let* ((element (save-excursion (beginning-of-line)
  20238. (org-element-at-point)))
  20239. (type (org-element-type element)))
  20240. (cond
  20241. ((memq type '(headline inlinetask))
  20242. (let ((pos (point)))
  20243. (beginning-of-line 1)
  20244. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20245. (if (eq special t)
  20246. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20247. (goto-char (match-beginning 1))
  20248. (goto-char (match-end 0)))
  20249. (if (or (< pos (match-end 0))
  20250. (not (eq this-command last-command)))
  20251. (goto-char (match-end 0))
  20252. (goto-char (match-beginning 1))))
  20253. (call-interactively move-fun))))
  20254. ((org-element-property :hiddenp element)
  20255. ;; If element is hidden, `move-end-of-line' would put point
  20256. ;; after it. Use `end-of-line' to stay on current line.
  20257. (call-interactively 'end-of-line))
  20258. (t (call-interactively move-fun)))))
  20259. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20260. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20261. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20262. (defun org-backward-sentence (&optional arg)
  20263. "Go to beginning of sentence, or beginning of table field.
  20264. This will call `backward-sentence' or `org-table-beginning-of-field',
  20265. depending on context."
  20266. (interactive "P")
  20267. (cond
  20268. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20269. (t (call-interactively 'backward-sentence))))
  20270. (defun org-forward-sentence (&optional arg)
  20271. "Go to end of sentence, or end of table field.
  20272. This will call `forward-sentence' or `org-table-end-of-field',
  20273. depending on context."
  20274. (interactive "P")
  20275. (cond
  20276. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20277. (t (call-interactively 'forward-sentence))))
  20278. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20279. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20280. (defun org-kill-line (&optional arg)
  20281. "Kill line, to tags or end of line."
  20282. (interactive "P")
  20283. (cond
  20284. ((or (not org-special-ctrl-k)
  20285. (bolp)
  20286. (not (org-at-heading-p)))
  20287. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20288. org-ctrl-k-protect-subtree)
  20289. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20290. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20291. (error "C-k aborted - would kill hidden subtree")))
  20292. (call-interactively
  20293. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20294. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20295. (kill-region (point) (match-beginning 1))
  20296. (org-set-tags nil t))
  20297. (t (kill-region (point) (point-at-eol)))))
  20298. (define-key org-mode-map "\C-k" 'org-kill-line)
  20299. (defun org-yank (&optional arg)
  20300. "Yank. If the kill is a subtree, treat it specially.
  20301. This command will look at the current kill and check if is a single
  20302. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20303. cursor is at the beginning of a line or after the stars of a currently
  20304. empty headline, then the yank is handled specially. How exactly depends
  20305. on the value of the following variables, both set by default.
  20306. org-yank-folded-subtrees
  20307. When set, the subtree(s) will be folded after insertion, but only
  20308. if doing so would now swallow text after the yanked text.
  20309. org-yank-adjusted-subtrees
  20310. When set, the subtree will be promoted or demoted in order to
  20311. fit into the local outline tree structure, which means that the level
  20312. will be adjusted so that it becomes the smaller one of the two
  20313. *visible* surrounding headings.
  20314. Any prefix to this command will cause `yank' to be called directly with
  20315. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20316. will just
  20317. plainly yank the text as it is.
  20318. \[1] The test checks if the first non-white line is a heading
  20319. and if there are no other headings with fewer stars."
  20320. (interactive "P")
  20321. (org-yank-generic 'yank arg))
  20322. (defun org-yank-generic (command arg)
  20323. "Perform some yank-like command.
  20324. This function implements the behavior described in the `org-yank'
  20325. documentation. However, it has been generalized to work for any
  20326. interactive command with similar behavior."
  20327. ;; pretend to be command COMMAND
  20328. (setq this-command command)
  20329. (if arg
  20330. (call-interactively command)
  20331. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20332. (and (org-kill-is-subtree-p)
  20333. (or (bolp)
  20334. (and (looking-at "[ \t]*$")
  20335. (string-match
  20336. "\\`\\*+\\'"
  20337. (buffer-substring (point-at-bol) (point)))))))
  20338. swallowp)
  20339. (cond
  20340. ((and subtreep org-yank-folded-subtrees)
  20341. (let ((beg (point))
  20342. end)
  20343. (if (and subtreep org-yank-adjusted-subtrees)
  20344. (org-paste-subtree nil nil 'for-yank)
  20345. (call-interactively command))
  20346. (setq end (point))
  20347. (goto-char beg)
  20348. (when (and (bolp) subtreep
  20349. (not (setq swallowp
  20350. (org-yank-folding-would-swallow-text beg end))))
  20351. (org-with-limited-levels
  20352. (or (looking-at org-outline-regexp)
  20353. (re-search-forward org-outline-regexp-bol end t))
  20354. (while (and (< (point) end) (looking-at org-outline-regexp))
  20355. (hide-subtree)
  20356. (org-cycle-show-empty-lines 'folded)
  20357. (condition-case nil
  20358. (outline-forward-same-level 1)
  20359. (error (goto-char end))))))
  20360. (when swallowp
  20361. (message
  20362. "Inserted text not folded because that would swallow text"))
  20363. (goto-char end)
  20364. (skip-chars-forward " \t\n\r")
  20365. (beginning-of-line 1)
  20366. (push-mark beg 'nomsg)))
  20367. ((and subtreep org-yank-adjusted-subtrees)
  20368. (let ((beg (point-at-bol)))
  20369. (org-paste-subtree nil nil 'for-yank)
  20370. (push-mark beg 'nomsg)))
  20371. (t
  20372. (call-interactively command))))))
  20373. (defun org-yank-folding-would-swallow-text (beg end)
  20374. "Would hide-subtree at BEG swallow any text after END?"
  20375. (let (level)
  20376. (org-with-limited-levels
  20377. (save-excursion
  20378. (goto-char beg)
  20379. (when (or (looking-at org-outline-regexp)
  20380. (re-search-forward org-outline-regexp-bol end t))
  20381. (setq level (org-outline-level)))
  20382. (goto-char end)
  20383. (skip-chars-forward " \t\r\n\v\f")
  20384. (if (or (eobp)
  20385. (and (bolp) (looking-at org-outline-regexp)
  20386. (<= (org-outline-level) level)))
  20387. nil ; Nothing would be swallowed
  20388. t))))) ; something would swallow
  20389. (define-key org-mode-map "\C-y" 'org-yank)
  20390. (defun org-truely-invisible-p ()
  20391. "Check if point is at a character currently not visible.
  20392. This version does not only check the character property, but also
  20393. `visible-mode'."
  20394. ;; Early versions of noutline don't have `outline-invisible-p'.
  20395. (if (org-bound-and-true-p visible-mode)
  20396. nil
  20397. (outline-invisible-p)))
  20398. (defun org-invisible-p2 ()
  20399. "Check if point is at a character currently not visible."
  20400. (save-excursion
  20401. (if (and (eolp) (not (bobp))) (backward-char 1))
  20402. ;; Early versions of noutline don't have `outline-invisible-p'.
  20403. (outline-invisible-p)))
  20404. (defun org-back-to-heading (&optional invisible-ok)
  20405. "Call `outline-back-to-heading', but provide a better error message."
  20406. (condition-case nil
  20407. (outline-back-to-heading invisible-ok)
  20408. (error (error "Before first headline at position %d in buffer %s"
  20409. (point) (current-buffer)))))
  20410. (defun org-before-first-heading-p ()
  20411. "Before first heading?"
  20412. (save-excursion
  20413. (end-of-line)
  20414. (null (re-search-backward org-outline-regexp-bol nil t))))
  20415. (defun org-at-heading-p (&optional ignored)
  20416. (outline-on-heading-p t))
  20417. ;; Compatibility alias with Org versions < 7.8.03
  20418. (defalias 'org-on-heading-p 'org-at-heading-p)
  20419. (defun org-at-comment-p nil
  20420. "Is cursor in a line starting with a # character?"
  20421. (save-excursion
  20422. (beginning-of-line)
  20423. (looking-at "^#")))
  20424. (defun org-at-drawer-p nil
  20425. "Is cursor at a drawer keyword?"
  20426. (save-excursion
  20427. (move-beginning-of-line 1)
  20428. (looking-at org-drawer-regexp)))
  20429. (defun org-at-block-p nil
  20430. "Is cursor at a block keyword?"
  20431. (save-excursion
  20432. (move-beginning-of-line 1)
  20433. (looking-at org-block-regexp)))
  20434. (defun org-point-at-end-of-empty-headline ()
  20435. "If point is at the end of an empty headline, return t, else nil.
  20436. If the heading only contains a TODO keyword, it is still still considered
  20437. empty."
  20438. (and (looking-at "[ \t]*$")
  20439. (when org-todo-line-regexp
  20440. (save-excursion
  20441. (beginning-of-line 1)
  20442. (let ((case-fold-search nil))
  20443. (looking-at org-todo-line-regexp)
  20444. (string= (match-string 3) ""))))))
  20445. (defun org-at-heading-or-item-p ()
  20446. (or (org-at-heading-p) (org-at-item-p)))
  20447. (defun org-at-target-p ()
  20448. (or (org-in-regexp org-radio-target-regexp)
  20449. (org-in-regexp org-target-regexp)))
  20450. ;; Compatibility alias with Org versions < 7.8.03
  20451. (defalias 'org-on-target-p 'org-at-target-p)
  20452. (defun org-up-heading-all (arg)
  20453. "Move to the heading line of which the present line is a subheading.
  20454. This function considers both visible and invisible heading lines.
  20455. With argument, move up ARG levels."
  20456. (if (fboundp 'outline-up-heading-all)
  20457. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20458. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20459. (defun org-up-heading-safe ()
  20460. "Move to the heading line of which the present line is a subheading.
  20461. This version will not throw an error. It will return the level of the
  20462. headline found, or nil if no higher level is found.
  20463. Also, this function will be a lot faster than `outline-up-heading',
  20464. because it relies on stars being the outline starters. This can really
  20465. make a significant difference in outlines with very many siblings."
  20466. (let (start-level re)
  20467. (org-back-to-heading t)
  20468. (setq start-level (funcall outline-level))
  20469. (if (equal start-level 1)
  20470. nil
  20471. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20472. (if (re-search-backward re nil t)
  20473. (funcall outline-level)))))
  20474. (defun org-first-sibling-p ()
  20475. "Is this heading the first child of its parents?"
  20476. (interactive)
  20477. (let ((re org-outline-regexp-bol)
  20478. level l)
  20479. (unless (org-at-heading-p t)
  20480. (error "Not at a heading"))
  20481. (setq level (funcall outline-level))
  20482. (save-excursion
  20483. (if (not (re-search-backward re nil t))
  20484. t
  20485. (setq l (funcall outline-level))
  20486. (< l level)))))
  20487. (defun org-goto-sibling (&optional previous)
  20488. "Goto the next sibling, even if it is invisible.
  20489. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20490. when a sibling was found. When none is found, return nil and don't
  20491. move point."
  20492. (let ((fun (if previous 're-search-backward 're-search-forward))
  20493. (pos (point))
  20494. (re org-outline-regexp-bol)
  20495. level l)
  20496. (when (condition-case nil (org-back-to-heading t) (error nil))
  20497. (setq level (funcall outline-level))
  20498. (catch 'exit
  20499. (or previous (forward-char 1))
  20500. (while (funcall fun re nil t)
  20501. (setq l (funcall outline-level))
  20502. (when (< l level) (goto-char pos) (throw 'exit nil))
  20503. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20504. (goto-char pos)
  20505. nil))))
  20506. (defun org-show-siblings ()
  20507. "Show all siblings of the current headline."
  20508. (save-excursion
  20509. (while (org-goto-sibling) (org-flag-heading nil)))
  20510. (save-excursion
  20511. (while (org-goto-sibling 'previous)
  20512. (org-flag-heading nil))))
  20513. (defun org-goto-first-child ()
  20514. "Goto the first child, even if it is invisible.
  20515. Return t when a child was found. Otherwise don't move point and
  20516. return nil."
  20517. (let (level (pos (point)) (re org-outline-regexp-bol))
  20518. (when (condition-case nil (org-back-to-heading t) (error nil))
  20519. (setq level (outline-level))
  20520. (forward-char 1)
  20521. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20522. (progn (goto-char (match-beginning 0)) t)
  20523. (goto-char pos) nil))))
  20524. (defun org-show-hidden-entry ()
  20525. "Show an entry where even the heading is hidden."
  20526. (save-excursion
  20527. (org-show-entry)))
  20528. (defun org-flag-heading (flag &optional entry)
  20529. "Flag the current heading. FLAG non-nil means make invisible.
  20530. When ENTRY is non-nil, show the entire entry."
  20531. (save-excursion
  20532. (org-back-to-heading t)
  20533. ;; Check if we should show the entire entry
  20534. (if entry
  20535. (progn
  20536. (org-show-entry)
  20537. (save-excursion
  20538. (and (outline-next-heading)
  20539. (org-flag-heading nil))))
  20540. (outline-flag-region (max (point-min) (1- (point)))
  20541. (save-excursion (outline-end-of-heading) (point))
  20542. flag))))
  20543. (defun org-get-next-sibling ()
  20544. "Move to next heading of the same level, and return point.
  20545. If there is no such heading, return nil.
  20546. This is like outline-next-sibling, but invisible headings are ok."
  20547. (let ((level (funcall outline-level)))
  20548. (outline-next-heading)
  20549. (while (and (not (eobp)) (> (funcall outline-level) level))
  20550. (outline-next-heading))
  20551. (if (or (eobp) (< (funcall outline-level) level))
  20552. nil
  20553. (point))))
  20554. (defun org-get-last-sibling ()
  20555. "Move to previous heading of the same level, and return point.
  20556. If there is no such heading, return nil."
  20557. (let ((opoint (point))
  20558. (level (funcall outline-level)))
  20559. (outline-previous-heading)
  20560. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20561. (while (and (> (funcall outline-level) level)
  20562. (not (bobp)))
  20563. (outline-previous-heading))
  20564. (if (< (funcall outline-level) level)
  20565. nil
  20566. (point)))))
  20567. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20568. "Goto to the end of a subtree."
  20569. ;; This contains an exact copy of the original function, but it uses
  20570. ;; `org-back-to-heading', to make it work also in invisible
  20571. ;; trees. And is uses an invisible-ok argument.
  20572. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20573. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20574. ;; with many children and grandchildren etc, this can be much faster
  20575. ;; than the outline version.
  20576. (org-back-to-heading invisible-ok)
  20577. (let ((first t)
  20578. (level (funcall outline-level)))
  20579. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20580. ;; A true heading (not a plain list item), in Org-mode
  20581. ;; This means we can easily find the end by looking
  20582. ;; only for the right number of stars. Using a regexp to do
  20583. ;; this is so much faster than using a Lisp loop.
  20584. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20585. (forward-char 1)
  20586. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20587. ;; something else, do it the slow way
  20588. (while (and (not (eobp))
  20589. (or first (> (funcall outline-level) level)))
  20590. (setq first nil)
  20591. (outline-next-heading)))
  20592. (unless to-heading
  20593. (if (memq (preceding-char) '(?\n ?\^M))
  20594. (progn
  20595. ;; Go to end of line before heading
  20596. (forward-char -1)
  20597. (if (memq (preceding-char) '(?\n ?\^M))
  20598. ;; leave blank line before heading
  20599. (forward-char -1))))))
  20600. (point))
  20601. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20602. "Use Org version in org-mode, for dramatic speed-up."
  20603. (if (derived-mode-p 'org-mode)
  20604. (progn
  20605. (org-end-of-subtree nil t)
  20606. (unless (eobp) (backward-char 1)))
  20607. ad-do-it))
  20608. (defun org-end-of-meta-data-and-drawers ()
  20609. "Jump to the first text after meta data and drawers in the current entry.
  20610. This will move over empty lines, lines with planning time stamps,
  20611. clocking lines, and drawers."
  20612. (org-back-to-heading t)
  20613. (let ((end (save-excursion (outline-next-heading) (point)))
  20614. (re (concat "\\(" org-drawer-regexp "\\)"
  20615. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20616. (forward-line 1)
  20617. (while (re-search-forward re end t)
  20618. (if (not (match-end 1))
  20619. ;; empty or planning line
  20620. (forward-line 1)
  20621. ;; a drawer, find the end
  20622. (re-search-forward "^[ \t]*:END:" end 'move)
  20623. (forward-line 1)))
  20624. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20625. (point)))
  20626. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20627. "Move forward to the arg'th subheading at same level as this one.
  20628. Stop at the first and last subheadings of a superior heading.
  20629. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20630. non-nil it will also look at invisible ones."
  20631. (interactive "p")
  20632. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20633. (outline-next-heading)
  20634. (org-at-heading-p)
  20635. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20636. (re (format "^\\*\\{1,%d\\} " level))
  20637. l)
  20638. (forward-char 1)
  20639. (while (> arg 0)
  20640. (while (and (re-search-forward re nil 'move)
  20641. (setq l (- (match-end 0) (match-beginning 0) 1))
  20642. (= l level)
  20643. (not invisible-ok)
  20644. (progn (backward-char 1) (outline-invisible-p)))
  20645. (if (< l level) (setq arg 1)))
  20646. (setq arg (1- arg)))
  20647. (beginning-of-line 1))))
  20648. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20649. "Move backward to the arg'th subheading at same level as this one.
  20650. Stop at the first and last subheadings of a superior heading."
  20651. (interactive "p")
  20652. (if (not (ignore-errors (org-back-to-heading)))
  20653. (goto-char (point-min))
  20654. (org-at-heading-p)
  20655. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20656. (re (format "^\\*\\{1,%d\\} " level))
  20657. l)
  20658. (while (> arg 0)
  20659. (while (and (re-search-backward re nil 'move)
  20660. (setq l (- (match-end 0) (match-beginning 0) 1))
  20661. (= l level)
  20662. (not invisible-ok)
  20663. (outline-invisible-p))
  20664. (if (< l level) (setq arg 1)))
  20665. (setq arg (1- arg))))))
  20666. (defun org-forward-element ()
  20667. "Move forward by one element.
  20668. Move to the next element at the same level, when possible."
  20669. (interactive)
  20670. (cond ((eobp) (error "Cannot move further down"))
  20671. ((org-with-limited-levels (org-at-heading-p))
  20672. (let ((origin (point)))
  20673. (org-forward-heading-same-level 1)
  20674. (unless (org-with-limited-levels (org-at-heading-p))
  20675. (goto-char origin)
  20676. (error "Cannot move further down"))))
  20677. (t
  20678. (let* ((elem (org-element-at-point))
  20679. (end (org-element-property :end elem))
  20680. (parent (org-element-property :parent elem)))
  20681. (if (and parent (= (org-element-property :contents-end parent) end))
  20682. (goto-char (org-element-property :end parent))
  20683. (goto-char end))))))
  20684. (defun org-backward-element ()
  20685. "Move backward by one element.
  20686. Move to the previous element at the same level, when possible."
  20687. (interactive)
  20688. (cond ((bobp) (error "Cannot move further up"))
  20689. ((org-with-limited-levels (org-at-heading-p))
  20690. ;; At an headline, move to the previous one, if any, or stay
  20691. ;; here.
  20692. (let ((origin (point)))
  20693. (org-backward-heading-same-level 1)
  20694. (unless (org-with-limited-levels (org-at-heading-p))
  20695. (goto-char origin)
  20696. (error "Cannot move further up"))))
  20697. (t
  20698. (let* ((trail (org-element-at-point 'keep-trail))
  20699. (elem (car trail))
  20700. (prev-elem (nth 1 trail))
  20701. (beg (org-element-property :begin elem)))
  20702. (cond
  20703. ;; Move to beginning of current element if point isn't
  20704. ;; there already.
  20705. ((/= (point) beg) (goto-char beg))
  20706. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20707. ((org-before-first-heading-p) (goto-char (point-min)))
  20708. (t (org-back-to-heading)))))))
  20709. (defun org-up-element ()
  20710. "Move to upper element."
  20711. (interactive)
  20712. (if (org-with-limited-levels (org-at-heading-p))
  20713. (unless (org-up-heading-safe) (error "No surrounding element"))
  20714. (let* ((elem (org-element-at-point))
  20715. (parent (org-element-property :parent elem)))
  20716. (if parent (goto-char (org-element-property :begin parent))
  20717. (if (org-with-limited-levels (org-before-first-heading-p))
  20718. (error "No surrounding element")
  20719. (org-with-limited-levels (org-back-to-heading)))))))
  20720. (defvar org-element-greater-elements)
  20721. (defun org-down-element ()
  20722. "Move to inner element."
  20723. (interactive)
  20724. (let ((element (org-element-at-point)))
  20725. (cond
  20726. ((memq (org-element-type element) '(plain-list table))
  20727. (goto-char (org-element-property :contents-begin element))
  20728. (forward-char))
  20729. ((memq (org-element-type element) org-element-greater-elements)
  20730. ;; If contents are hidden, first disclose them.
  20731. (when (org-element-property :hiddenp element) (org-cycle))
  20732. (goto-char (or (org-element-property :contents-begin element)
  20733. (error "No content for this element"))))
  20734. (t (error "No inner element")))))
  20735. (defun org-drag-element-backward ()
  20736. "Move backward element at point."
  20737. (interactive)
  20738. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20739. (let* ((trail (org-element-at-point 'keep-trail))
  20740. (elem (car trail))
  20741. (prev-elem (nth 1 trail)))
  20742. ;; Error out if no previous element or previous element is
  20743. ;; a parent of the current one.
  20744. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20745. (error "Cannot drag element backward")
  20746. (let ((pos (point)))
  20747. (org-element-swap-A-B prev-elem elem)
  20748. (goto-char (+ (org-element-property :begin prev-elem)
  20749. (- pos (org-element-property :begin elem)))))))))
  20750. (defun org-drag-element-forward ()
  20751. "Move forward element at point."
  20752. (interactive)
  20753. (let* ((pos (point))
  20754. (elem (org-element-at-point)))
  20755. (when (= (point-max) (org-element-property :end elem))
  20756. (error "Cannot drag element forward"))
  20757. (goto-char (org-element-property :end elem))
  20758. (let ((next-elem (org-element-at-point)))
  20759. (when (or (org-element-nested-p elem next-elem)
  20760. (and (eq (org-element-type next-elem) 'headline)
  20761. (not (eq (org-element-type elem) 'headline))))
  20762. (goto-char pos)
  20763. (error "Cannot drag element forward"))
  20764. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20765. ;; body's length (without final blanks) and by the length of
  20766. ;; blanks between ELEM and NEXT-ELEM.
  20767. (let ((size-next (- (save-excursion
  20768. (goto-char (org-element-property :end next-elem))
  20769. (skip-chars-backward " \r\t\n")
  20770. (forward-line)
  20771. ;; Small correction if buffer doesn't end
  20772. ;; with a newline character.
  20773. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20774. (org-element-property :begin next-elem)))
  20775. (size-blank (- (org-element-property :end elem)
  20776. (save-excursion
  20777. (goto-char (org-element-property :end elem))
  20778. (skip-chars-backward " \r\t\n")
  20779. (forward-line)
  20780. (point)))))
  20781. (org-element-swap-A-B elem next-elem)
  20782. (goto-char (+ pos size-next size-blank))))))
  20783. (defun org-mark-element ()
  20784. "Put point at beginning of this element, mark at end.
  20785. Interactively, if this command is repeated or (in Transient Mark
  20786. mode) if the mark is active, it marks the next element after the
  20787. ones already marked."
  20788. (interactive)
  20789. (let (deactivate-mark)
  20790. (if (and (org-called-interactively-p 'any)
  20791. (or (and (eq last-command this-command) (mark t))
  20792. (and transient-mark-mode mark-active)))
  20793. (set-mark
  20794. (save-excursion
  20795. (goto-char (mark))
  20796. (goto-char (org-element-property :end (org-element-at-point)))))
  20797. (let ((element (org-element-at-point)))
  20798. (end-of-line)
  20799. (push-mark (org-element-property :end element) t t)
  20800. (goto-char (org-element-property :begin element))))))
  20801. (defun org-narrow-to-element ()
  20802. "Narrow buffer to current element."
  20803. (interactive)
  20804. (let ((elem (org-element-at-point)))
  20805. (cond
  20806. ((eq (car elem) 'headline)
  20807. (narrow-to-region
  20808. (org-element-property :begin elem)
  20809. (org-element-property :end elem)))
  20810. ((memq (car elem) org-element-greater-elements)
  20811. (narrow-to-region
  20812. (org-element-property :contents-begin elem)
  20813. (org-element-property :contents-end elem)))
  20814. (t
  20815. (narrow-to-region
  20816. (org-element-property :begin elem)
  20817. (org-element-property :end elem))))))
  20818. (defun org-transpose-words ()
  20819. "Transpose words, using `org-mode' syntax table."
  20820. (interactive)
  20821. (with-syntax-table org-syntax-table
  20822. (call-interactively 'transpose-words)))
  20823. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  20824. (defun org-transpose-element ()
  20825. "Transpose current and previous elements, keeping blank lines between.
  20826. Point is moved after both elements."
  20827. (interactive)
  20828. (org-skip-whitespace)
  20829. (let ((end (org-element-property :end (org-element-at-point))))
  20830. (org-drag-element-backward)
  20831. (goto-char end)))
  20832. (defun org-unindent-buffer ()
  20833. "Un-indent the visible part of the buffer.
  20834. Relative indentation (between items, inside blocks, etc.) isn't
  20835. modified."
  20836. (interactive)
  20837. (unless (eq major-mode 'org-mode)
  20838. (error "Cannot un-indent a buffer not in Org mode"))
  20839. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20840. unindent-tree ; For byte-compiler.
  20841. (unindent-tree
  20842. (function
  20843. (lambda (contents)
  20844. (mapc
  20845. (lambda (element)
  20846. (if (memq (org-element-type element) '(headline section))
  20847. (funcall unindent-tree (org-element-contents element))
  20848. (save-excursion
  20849. (save-restriction
  20850. (narrow-to-region
  20851. (org-element-property :begin element)
  20852. (org-element-property :end element))
  20853. (org-do-remove-indentation)))))
  20854. (reverse contents))))))
  20855. (funcall unindent-tree (org-element-contents parse-tree))))
  20856. (defun org-show-subtree ()
  20857. "Show everything after this heading at deeper levels."
  20858. (interactive)
  20859. (outline-flag-region
  20860. (point)
  20861. (save-excursion
  20862. (org-end-of-subtree t t))
  20863. nil))
  20864. (defun org-show-entry ()
  20865. "Show the body directly following this heading.
  20866. Show the heading too, if it is currently invisible."
  20867. (interactive)
  20868. (save-excursion
  20869. (condition-case nil
  20870. (progn
  20871. (org-back-to-heading t)
  20872. (outline-flag-region
  20873. (max (point-min) (1- (point)))
  20874. (save-excursion
  20875. (if (re-search-forward
  20876. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20877. (match-beginning 1)
  20878. (point-max)))
  20879. nil)
  20880. (org-cycle-hide-drawers 'children))
  20881. (error nil))))
  20882. (defun org-make-options-regexp (kwds &optional extra)
  20883. "Make a regular expression for keyword lines."
  20884. (concat
  20885. "^#\\+\\("
  20886. (mapconcat 'regexp-quote kwds "\\|")
  20887. (if extra (concat "\\|" extra))
  20888. "\\):[ \t]*\\(.*\\)"))
  20889. ;; Make isearch reveal the necessary context
  20890. (defun org-isearch-end ()
  20891. "Reveal context after isearch exits."
  20892. (when isearch-success ; only if search was successful
  20893. (if (featurep 'xemacs)
  20894. ;; Under XEmacs, the hook is run in the correct place,
  20895. ;; we directly show the context.
  20896. (org-show-context 'isearch)
  20897. ;; In Emacs the hook runs *before* restoring the overlays.
  20898. ;; So we have to use a one-time post-command-hook to do this.
  20899. ;; (Emacs 22 has a special variable, see function `org-mode')
  20900. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20901. isearch-mode-end-hook-quit)
  20902. ;; Only when the isearch was not quitted.
  20903. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20904. 'append 'local)))))
  20905. (defun org-isearch-post-command ()
  20906. "Remove self from hook, and show context."
  20907. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20908. (org-show-context 'isearch))
  20909. ;;;; Integration with and fixes for other packages
  20910. ;;; Imenu support
  20911. (defvar org-imenu-markers nil
  20912. "All markers currently used by Imenu.")
  20913. (make-variable-buffer-local 'org-imenu-markers)
  20914. (defun org-imenu-new-marker (&optional pos)
  20915. "Return a new marker for use by Imenu, and remember the marker."
  20916. (let ((m (make-marker)))
  20917. (move-marker m (or pos (point)))
  20918. (push m org-imenu-markers)
  20919. m))
  20920. (defun org-imenu-get-tree ()
  20921. "Produce the index for Imenu."
  20922. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20923. (setq org-imenu-markers nil)
  20924. (let* ((n org-imenu-depth)
  20925. (re (concat "^" (org-get-limited-outline-regexp)))
  20926. (subs (make-vector (1+ n) nil))
  20927. (last-level 0)
  20928. m level head)
  20929. (save-excursion
  20930. (save-restriction
  20931. (widen)
  20932. (goto-char (point-max))
  20933. (while (re-search-backward re nil t)
  20934. (setq level (org-reduced-level (funcall outline-level)))
  20935. (when (and (<= level n)
  20936. (looking-at org-complex-heading-regexp))
  20937. (setq head (org-link-display-format
  20938. (org-match-string-no-properties 4))
  20939. m (org-imenu-new-marker))
  20940. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20941. (if (>= level last-level)
  20942. (push (cons head m) (aref subs level))
  20943. (push (cons head (aref subs (1+ level))) (aref subs level))
  20944. (loop for i from (1+ level) to n do (aset subs i nil)))
  20945. (setq last-level level)))))
  20946. (aref subs 1)))
  20947. (eval-after-load "imenu"
  20948. '(progn
  20949. (add-hook 'imenu-after-jump-hook
  20950. (lambda ()
  20951. (if (derived-mode-p 'org-mode)
  20952. (org-show-context 'org-goto))))))
  20953. (defun org-link-display-format (link)
  20954. "Replace a link with either the description, or the link target
  20955. if no description is present"
  20956. (save-match-data
  20957. (if (string-match org-bracket-link-analytic-regexp link)
  20958. (replace-match (if (match-end 5)
  20959. (match-string 5 link)
  20960. (concat (match-string 1 link)
  20961. (match-string 3 link)))
  20962. nil t link)
  20963. link)))
  20964. (defun org-toggle-link-display ()
  20965. "Toggle the literal or descriptive display of links."
  20966. (interactive)
  20967. (if org-descriptive-links
  20968. (progn (org-remove-from-invisibility-spec '(org-link))
  20969. (org-restart-font-lock)
  20970. (setq org-descriptive-links nil))
  20971. (progn (add-to-invisibility-spec '(org-link))
  20972. (org-restart-font-lock)
  20973. (setq org-descriptive-links t))))
  20974. ;; Speedbar support
  20975. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20976. "Overlay marking the agenda restriction line in speedbar.")
  20977. (overlay-put org-speedbar-restriction-lock-overlay
  20978. 'face 'org-agenda-restriction-lock)
  20979. (overlay-put org-speedbar-restriction-lock-overlay
  20980. 'help-echo "Agendas are currently limited to this item.")
  20981. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20982. (defun org-speedbar-set-agenda-restriction ()
  20983. "Restrict future agenda commands to the location at point in speedbar.
  20984. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20985. (interactive)
  20986. (require 'org-agenda)
  20987. (let (p m tp np dir txt)
  20988. (cond
  20989. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20990. 'org-imenu t))
  20991. (setq m (get-text-property p 'org-imenu-marker))
  20992. (with-current-buffer (marker-buffer m)
  20993. (goto-char m)
  20994. (org-agenda-set-restriction-lock 'subtree)))
  20995. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20996. 'speedbar-function 'speedbar-find-file))
  20997. (setq tp (previous-single-property-change
  20998. (1+ p) 'speedbar-function)
  20999. np (next-single-property-change
  21000. tp 'speedbar-function)
  21001. dir (speedbar-line-directory)
  21002. txt (buffer-substring-no-properties (or tp (point-min))
  21003. (or np (point-max))))
  21004. (with-current-buffer (find-file-noselect
  21005. (let ((default-directory dir))
  21006. (expand-file-name txt)))
  21007. (unless (derived-mode-p 'org-mode)
  21008. (error "Cannot restrict to non-Org-mode file"))
  21009. (org-agenda-set-restriction-lock 'file)))
  21010. (t (error "Don't know how to restrict Org-mode's agenda")))
  21011. (move-overlay org-speedbar-restriction-lock-overlay
  21012. (point-at-bol) (point-at-eol))
  21013. (setq current-prefix-arg nil)
  21014. (org-agenda-maybe-redo)))
  21015. (eval-after-load "speedbar"
  21016. '(progn
  21017. (speedbar-add-supported-extension ".org")
  21018. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21019. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21020. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21021. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21022. (add-hook 'speedbar-visiting-tag-hook
  21023. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21024. ;;; Fixes and Hacks for problems with other packages
  21025. ;; Make flyspell not check words in links, to not mess up our keymap
  21026. (defun org-mode-flyspell-verify ()
  21027. "Don't let flyspell put overlays at active buttons, or on
  21028. {todo,all-time,additional-option-like}-keywords."
  21029. (let ((pos (max (1- (point)) (point-min)))
  21030. (word (thing-at-point 'word)))
  21031. (and (not (get-text-property pos 'keymap))
  21032. (not (get-text-property pos 'org-no-flyspell))
  21033. (not (member word org-todo-keywords-1))
  21034. (not (member word org-all-time-keywords))
  21035. (not (member word org-options-keywords))
  21036. (not (member word (mapcar 'car org-startup-options)))
  21037. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  21038. (defun org-remove-flyspell-overlays-in (beg end)
  21039. "Remove flyspell overlays in region."
  21040. (and (org-bound-and-true-p flyspell-mode)
  21041. (fboundp 'flyspell-delete-region-overlays)
  21042. (flyspell-delete-region-overlays beg end))
  21043. (add-text-properties beg end '(org-no-flyspell t)))
  21044. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21045. (eval-after-load "bookmark"
  21046. '(if (boundp 'bookmark-after-jump-hook)
  21047. ;; We can use the hook
  21048. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21049. ;; Hook not available, use advice
  21050. (defadvice bookmark-jump (after org-make-visible activate)
  21051. "Make the position visible."
  21052. (org-bookmark-jump-unhide))))
  21053. ;; Make sure saveplace shows the location if it was hidden
  21054. (eval-after-load "saveplace"
  21055. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21056. "Make the position visible."
  21057. (org-bookmark-jump-unhide)))
  21058. ;; Make sure ecb shows the location if it was hidden
  21059. (eval-after-load "ecb"
  21060. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21061. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21062. (if (derived-mode-p 'org-mode)
  21063. (org-show-context))))
  21064. (defun org-bookmark-jump-unhide ()
  21065. "Unhide the current position, to show the bookmark location."
  21066. (and (derived-mode-p 'org-mode)
  21067. (or (outline-invisible-p)
  21068. (save-excursion (goto-char (max (point-min) (1- (point))))
  21069. (outline-invisible-p)))
  21070. (org-show-context 'bookmark-jump)))
  21071. ;; Make session.el ignore our circular variable
  21072. (eval-after-load "session"
  21073. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21074. ;;;; Experimental code
  21075. (defun org-closed-in-range ()
  21076. "Sparse tree of items closed in a certain time range.
  21077. Still experimental, may disappear in the future."
  21078. (interactive)
  21079. ;; Get the time interval from the user.
  21080. (let* ((time1 (org-float-time
  21081. (org-read-date nil 'to-time nil "Starting date: ")))
  21082. (time2 (org-float-time
  21083. (org-read-date nil 'to-time nil "End date:")))
  21084. ;; callback function
  21085. (callback (lambda ()
  21086. (let ((time
  21087. (org-float-time
  21088. (apply 'encode-time
  21089. (org-parse-time-string
  21090. (match-string 1))))))
  21091. ;; check if time in interval
  21092. (and (>= time time1) (<= time time2))))))
  21093. ;; make tree, check each match with the callback
  21094. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21095. ;;;; Finish up
  21096. (provide 'org)
  21097. (run-hooks 'org-load-hook)
  21098. ;;; org.el ends here