org.el 863 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (let ((load-suffixes (list ".el")))
  74. (load "org-loaddefs" 'noerror nil nil 'mustsuffix))
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro org-with-limited-levels.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches an headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (when (fboundp 'defvaralias)
  93. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-at-clock-log-p "org-clock" ())
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  108. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "org-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (autoload 'org-element-at-point "org-element")
  124. (autoload 'org-element-type "org-element")
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-type "org-element" (element))
  127. (declare-function org-element-context "org-element" ())
  128. (declare-function org-element-contents "org-element" (element))
  129. (declare-function org-element-property "org-element" (property element))
  130. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  131. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  132. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  133. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  134. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  135. ;; load languages based on value of `org-babel-load-languages'
  136. (defvar org-babel-load-languages)
  137. ;;;###autoload
  138. (defun org-babel-do-load-languages (sym value)
  139. "Load the languages defined in `org-babel-load-languages'."
  140. (set-default sym value)
  141. (mapc (lambda (pair)
  142. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  143. (if active
  144. (progn
  145. (require (intern (concat "ob-" lang))))
  146. (progn
  147. (funcall 'fmakunbound
  148. (intern (concat "org-babel-execute:" lang)))
  149. (funcall 'fmakunbound
  150. (intern (concat "org-babel-expand-body:" lang)))))))
  151. org-babel-load-languages))
  152. (defcustom org-babel-load-languages '((emacs-lisp . t))
  153. "Languages which can be evaluated in Org-mode buffers.
  154. This list can be used to load support for any of the languages
  155. below, note that each language will depend on a different set of
  156. system executables and/or Emacs modes. When a language is
  157. \"loaded\", then code blocks in that language can be evaluated
  158. with `org-babel-execute-src-block' bound by default to C-c
  159. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  160. be set to remove code block evaluation from the C-c C-c
  161. keybinding. By default only Emacs Lisp (which has no
  162. requirements) is loaded."
  163. :group 'org-babel
  164. :set 'org-babel-do-load-languages
  165. :version "24.1"
  166. :type '(alist :tag "Babel Languages"
  167. :key-type
  168. (choice
  169. (const :tag "Awk" awk)
  170. (const :tag "C" C)
  171. (const :tag "R" R)
  172. (const :tag "Asymptote" asymptote)
  173. (const :tag "Calc" calc)
  174. (const :tag "Clojure" clojure)
  175. (const :tag "CSS" css)
  176. (const :tag "Ditaa" ditaa)
  177. (const :tag "Dot" dot)
  178. (const :tag "Emacs Lisp" emacs-lisp)
  179. (const :tag "Fortran" fortran)
  180. (const :tag "Gnuplot" gnuplot)
  181. (const :tag "Haskell" haskell)
  182. (const :tag "IO" io)
  183. (const :tag "Java" java)
  184. (const :tag "Javascript" js)
  185. (const :tag "LaTeX" latex)
  186. (const :tag "Ledger" ledger)
  187. (const :tag "Lilypond" lilypond)
  188. (const :tag "Lisp" lisp)
  189. (const :tag "Makefile" makefile)
  190. (const :tag "Maxima" maxima)
  191. (const :tag "Matlab" matlab)
  192. (const :tag "Mscgen" mscgen)
  193. (const :tag "Ocaml" ocaml)
  194. (const :tag "Octave" octave)
  195. (const :tag "Org" org)
  196. (const :tag "Perl" perl)
  197. (const :tag "Pico Lisp" picolisp)
  198. (const :tag "PlantUML" plantuml)
  199. (const :tag "Python" python)
  200. (const :tag "Ruby" ruby)
  201. (const :tag "Sass" sass)
  202. (const :tag "Scala" scala)
  203. (const :tag "Scheme" scheme)
  204. (const :tag "Screen" screen)
  205. (const :tag "Shell Script" sh)
  206. (const :tag "Shen" shen)
  207. (const :tag "Sql" sql)
  208. (const :tag "Sqlite" sqlite))
  209. :value-type (boolean :tag "Activate" :value t)))
  210. ;;;; Customization variables
  211. (defcustom org-clone-delete-id nil
  212. "Remove ID property of clones of a subtree.
  213. When non-nil, clones of a subtree don't inherit the ID property.
  214. Otherwise they inherit the ID property with a new unique
  215. identifier."
  216. :type 'boolean
  217. :version "24.1"
  218. :group 'org-id)
  219. ;;; Version
  220. (require 'org-compat)
  221. (org-check-version)
  222. ;;;###autoload
  223. (defun org-version (&optional here full message)
  224. "Show the org-mode version in the echo area.
  225. With prefix argument HERE, insert it at point.
  226. When FULL is non-nil, use a verbose version string.
  227. When MESSAGE is non-nil, display a message with the version."
  228. (interactive "P")
  229. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  230. (save-load-suffixes load-suffixes)
  231. (load-suffixes (list ".el"))
  232. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  233. (org-trash (or
  234. (and (fboundp 'org-release) (fboundp 'org-git-version))
  235. (load (concat org-dir "org-version")
  236. 'noerror 'nomessage nil 'mustsuffix)))
  237. (load-suffixes save-load-suffixes)
  238. (org-version (org-release))
  239. (git-version (org-git-version))
  240. (version (format "Org-mode version %s (%s @ %s)"
  241. org-version
  242. git-version
  243. (if org-install-dir
  244. (if (string= org-dir org-install-dir)
  245. org-install-dir
  246. (concat "mixed installation! " org-install-dir " and " org-dir))
  247. "org-loaddefs.el can not be found!")))
  248. (_version (if full version org-version)))
  249. (if (org-called-interactively-p 'interactive)
  250. (if here
  251. (insert version)
  252. (message version))
  253. (if message (message _version))
  254. _version)))
  255. (defconst org-version (org-version))
  256. ;;; Compatibility constants
  257. ;;; The custom variables
  258. (defgroup org nil
  259. "Outline-based notes management and organizer."
  260. :tag "Org"
  261. :group 'outlines
  262. :group 'calendar)
  263. (defcustom org-mode-hook nil
  264. "Mode hook for Org-mode, run after the mode was turned on."
  265. :group 'org
  266. :type 'hook)
  267. (defcustom org-load-hook nil
  268. "Hook that is run after org.el has been loaded."
  269. :group 'org
  270. :type 'hook)
  271. (defcustom org-log-buffer-setup-hook nil
  272. "Hook that is run after an Org log buffer is created."
  273. :group 'org
  274. :version "24.1"
  275. :type 'hook)
  276. (defvar org-modules) ; defined below
  277. (defvar org-modules-loaded nil
  278. "Have the modules been loaded already?")
  279. (defun org-load-modules-maybe (&optional force)
  280. "Load all extensions listed in `org-modules'."
  281. (when (or force (not org-modules-loaded))
  282. (mapc (lambda (ext)
  283. (condition-case nil (require ext)
  284. (error (message "Problems while trying to load feature `%s'" ext))))
  285. org-modules)
  286. (setq org-modules-loaded t)))
  287. (defun org-set-modules (var value)
  288. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  289. (set var value)
  290. (when (featurep 'org)
  291. (org-load-modules-maybe 'force)))
  292. (when (org-bound-and-true-p org-modules)
  293. (let ((a (member 'org-infojs org-modules)))
  294. (and a (setcar a 'org-jsinfo))))
  295. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  296. "Modules that should always be loaded together with org.el.
  297. If a description starts with <C>, the file is not part of Emacs
  298. and loading it will require that you have downloaded and properly installed
  299. the org-mode distribution.
  300. You can also use this system to load external packages (i.e. neither Org
  301. core modules, nor modules from the CONTRIB directory). Just add symbols
  302. to the end of the list. If the package is called org-xyz.el, then you need
  303. to add the symbol `xyz', and the package must have a call to
  304. (provide 'org-xyz)"
  305. :group 'org
  306. :set 'org-set-modules
  307. :type
  308. '(set :greedy t
  309. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  310. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  311. (const :tag " crypt: Encryption of subtrees" org-crypt)
  312. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  313. (const :tag " docview: Links to doc-view buffers" org-docview)
  314. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  315. (const :tag " id: Global IDs for identifying entries" org-id)
  316. (const :tag " info: Links to Info nodes" org-info)
  317. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  318. (const :tag " habit: Track your consistency with habits" org-habit)
  319. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  320. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  321. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  322. (const :tag " mew Links to Mew folders/messages" org-mew)
  323. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  324. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  325. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  326. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  327. (const :tag " vm: Links to VM folders/messages" org-vm)
  328. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  329. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  330. (const :tag " mouse: Additional mouse support" org-mouse)
  331. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  332. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  333. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  334. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  335. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  336. (const :tag "C collector: Collect properties into tables" org-collector)
  337. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  338. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  339. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  340. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  341. (const :tag "C eval: Include command output as text" org-eval)
  342. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  343. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  344. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  345. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  346. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  347. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  348. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  349. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  350. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  351. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  352. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  353. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  354. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  355. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  356. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  357. (const :tag "C registry: A registry for Org-mode links" org-registry)
  358. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  359. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  360. (const :tag "C secretary: Team management with org-mode" org-secretary)
  361. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  362. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  363. (const :tag "C track: Keep up with Org-mode development" org-track)
  364. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  365. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  366. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  367. (defcustom org-support-shift-select nil
  368. "Non-nil means make shift-cursor commands select text when possible.
  369. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  370. start selecting a region, or enlarge regions started in this way.
  371. In Org-mode, in special contexts, these same keys are used for
  372. other purposes, important enough to compete with shift selection.
  373. Org tries to balance these needs by supporting `shift-select-mode'
  374. outside these special contexts, under control of this variable.
  375. The default of this variable is nil, to avoid confusing behavior. Shifted
  376. cursor keys will then execute Org commands in the following contexts:
  377. - on a headline, changing TODO state (left/right) and priority (up/down)
  378. - on a time stamp, changing the time
  379. - in a plain list item, changing the bullet type
  380. - in a property definition line, switching between allowed values
  381. - in the BEGIN line of a clock table (changing the time block).
  382. Outside these contexts, the commands will throw an error.
  383. When this variable is t and the cursor is not in a special
  384. context, Org-mode will support shift-selection for making and
  385. enlarging regions. To make this more effective, the bullet
  386. cycling will no longer happen anywhere in an item line, but only
  387. if the cursor is exactly on the bullet.
  388. If you set this variable to the symbol `always', then the keys
  389. will not be special in headlines, property lines, and item lines,
  390. to make shift selection work there as well. If this is what you
  391. want, you can use the following alternative commands: `C-c C-t'
  392. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  393. can be used to switch TODO sets, `C-c -' to cycle item bullet
  394. types, and properties can be edited by hand or in column view.
  395. However, when the cursor is on a timestamp, shift-cursor commands
  396. will still edit the time stamp - this is just too good to give up.
  397. XEmacs user should have this variable set to nil, because
  398. `shift-select-mode' is in Emacs 23 or later only."
  399. :group 'org
  400. :type '(choice
  401. (const :tag "Never" nil)
  402. (const :tag "When outside special context" t)
  403. (const :tag "Everywhere except timestamps" always)))
  404. (defcustom org-loop-over-headlines-in-active-region nil
  405. "Shall some commands act upon headlines in the active region?
  406. When set to `t', some commands will be performed in all headlines
  407. within the active region.
  408. When set to `start-level', some commands will be performed in all
  409. headlines within the active region, provided that these headlines
  410. are of the same level than the first one.
  411. When set to a string, those commands will be performed on the
  412. matching headlines within the active region. Such string must be
  413. a tags/property/todo match as it is used in the agenda tags view.
  414. The list of commands is: `org-schedule', `org-deadline',
  415. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  416. `org-archive-to-archive-sibling'. The archiving commands skip
  417. already archived entries."
  418. :type '(choice (const :tag "Don't loop" nil)
  419. (const :tag "All headlines in active region" t)
  420. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  421. (string :tag "Tags/Property/Todo matcher"))
  422. :version "24.1"
  423. :group 'org-todo
  424. :group 'org-archive)
  425. (defgroup org-startup nil
  426. "Options concerning startup of Org-mode."
  427. :tag "Org Startup"
  428. :group 'org)
  429. (defcustom org-startup-folded t
  430. "Non-nil means entering Org-mode will switch to OVERVIEW.
  431. This can also be configured on a per-file basis by adding one of
  432. the following lines anywhere in the buffer:
  433. #+STARTUP: fold (or `overview', this is equivalent)
  434. #+STARTUP: nofold (or `showall', this is equivalent)
  435. #+STARTUP: content
  436. #+STARTUP: showeverything"
  437. :group 'org-startup
  438. :type '(choice
  439. (const :tag "nofold: show all" nil)
  440. (const :tag "fold: overview" t)
  441. (const :tag "content: all headlines" content)
  442. (const :tag "show everything, even drawers" showeverything)))
  443. (defcustom org-startup-truncated t
  444. "Non-nil means entering Org-mode will set `truncate-lines'.
  445. This is useful since some lines containing links can be very long and
  446. uninteresting. Also tables look terrible when wrapped."
  447. :group 'org-startup
  448. :type 'boolean)
  449. (defcustom org-startup-indented nil
  450. "Non-nil means turn on `org-indent-mode' on startup.
  451. This can also be configured on a per-file basis by adding one of
  452. the following lines anywhere in the buffer:
  453. #+STARTUP: indent
  454. #+STARTUP: noindent"
  455. :group 'org-structure
  456. :type '(choice
  457. (const :tag "Not" nil)
  458. (const :tag "Globally (slow on startup in large files)" t)))
  459. (defcustom org-use-sub-superscripts t
  460. "Non-nil means interpret \"_\" and \"^\" for export.
  461. When this option is turned on, you can use TeX-like syntax for sub- and
  462. superscripts. Several characters after \"_\" or \"^\" will be
  463. considered as a single item - so grouping with {} is normally not
  464. needed. For example, the following things will be parsed as single
  465. sub- or superscripts.
  466. 10^24 or 10^tau several digits will be considered 1 item.
  467. 10^-12 or 10^-tau a leading sign with digits or a word
  468. x^2-y^3 will be read as x^2 - y^3, because items are
  469. terminated by almost any nonword/nondigit char.
  470. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  471. Still, ambiguity is possible - so when in doubt use {} to enclose the
  472. sub/superscript. If you set this variable to the symbol `{}',
  473. the braces are *required* in order to trigger interpretations as
  474. sub/superscript. This can be helpful in documents that need \"_\"
  475. frequently in plain text.
  476. Not all export backends support this, but HTML does.
  477. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  478. :group 'org-startup
  479. :group 'org-export-translation
  480. :version "24.1"
  481. :type '(choice
  482. (const :tag "Always interpret" t)
  483. (const :tag "Only with braces" {})
  484. (const :tag "Never interpret" nil)))
  485. (if (fboundp 'defvaralias)
  486. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  487. (defcustom org-startup-with-beamer-mode nil
  488. "Non-nil means turn on `org-beamer-mode' on startup.
  489. This can also be configured on a per-file basis by adding one of
  490. the following lines anywhere in the buffer:
  491. #+STARTUP: beamer"
  492. :group 'org-startup
  493. :version "24.1"
  494. :type 'boolean)
  495. (defcustom org-startup-align-all-tables nil
  496. "Non-nil means align all tables when visiting a file.
  497. This is useful when the column width in tables is forced with <N> cookies
  498. in table fields. Such tables will look correct only after the first re-align.
  499. This can also be configured on a per-file basis by adding one of
  500. the following lines anywhere in the buffer:
  501. #+STARTUP: align
  502. #+STARTUP: noalign"
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-startup-with-inline-images nil
  506. "Non-nil means show inline images when loading a new Org file.
  507. This can also be configured on a per-file basis by adding one of
  508. the following lines anywhere in the buffer:
  509. #+STARTUP: inlineimages
  510. #+STARTUP: noinlineimages"
  511. :group 'org-startup
  512. :version "24.1"
  513. :type 'boolean)
  514. (defcustom org-insert-mode-line-in-empty-file nil
  515. "Non-nil means insert the first line setting Org-mode in empty files.
  516. When the function `org-mode' is called interactively in an empty file, this
  517. normally means that the file name does not automatically trigger Org-mode.
  518. To ensure that the file will always be in Org-mode in the future, a
  519. line enforcing Org-mode will be inserted into the buffer, if this option
  520. has been set."
  521. :group 'org-startup
  522. :type 'boolean)
  523. (defcustom org-replace-disputed-keys nil
  524. "Non-nil means use alternative key bindings for some keys.
  525. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  526. These keys are also used by other packages like shift-selection-mode'
  527. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  528. If you want to use Org-mode together with one of these other modes,
  529. or more generally if you would like to move some Org-mode commands to
  530. other keys, set this variable and configure the keys with the variable
  531. `org-disputed-keys'.
  532. This option is only relevant at load-time of Org-mode, and must be set
  533. *before* org.el is loaded. Changing it requires a restart of Emacs to
  534. become effective."
  535. :group 'org-startup
  536. :type 'boolean)
  537. (defcustom org-use-extra-keys nil
  538. "Non-nil means use extra key sequence definitions for certain commands.
  539. This happens automatically if you run XEmacs or if `window-system'
  540. is nil. This variable lets you do the same manually. You must
  541. set it before loading org.
  542. Example: on Carbon Emacs 22 running graphically, with an external
  543. keyboard on a Powerbook, the default way of setting M-left might
  544. not work for either Alt or ESC. Setting this variable will make
  545. it work for ESC."
  546. :group 'org-startup
  547. :type 'boolean)
  548. (if (fboundp 'defvaralias)
  549. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  550. (defcustom org-disputed-keys
  551. '(([(shift up)] . [(meta p)])
  552. ([(shift down)] . [(meta n)])
  553. ([(shift left)] . [(meta -)])
  554. ([(shift right)] . [(meta +)])
  555. ([(control shift right)] . [(meta shift +)])
  556. ([(control shift left)] . [(meta shift -)]))
  557. "Keys for which Org-mode and other modes compete.
  558. This is an alist, cars are the default keys, second element specifies
  559. the alternative to use when `org-replace-disputed-keys' is t.
  560. Keys can be specified in any syntax supported by `define-key'.
  561. The value of this option takes effect only at Org-mode's startup,
  562. therefore you'll have to restart Emacs to apply it after changing."
  563. :group 'org-startup
  564. :type 'alist)
  565. (defun org-key (key)
  566. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  567. Or return the original if not disputed.
  568. Also apply the translations defined in `org-xemacs-key-equivalents'."
  569. (when org-replace-disputed-keys
  570. (let* ((nkey (key-description key))
  571. (x (org-find-if (lambda (x)
  572. (equal (key-description (car x)) nkey))
  573. org-disputed-keys)))
  574. (setq key (if x (cdr x) key))))
  575. (when (featurep 'xemacs)
  576. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  577. key)
  578. (defun org-find-if (predicate seq)
  579. (catch 'exit
  580. (while seq
  581. (if (funcall predicate (car seq))
  582. (throw 'exit (car seq))
  583. (pop seq)))))
  584. (defun org-defkey (keymap key def)
  585. "Define a key, possibly translated, as returned by `org-key'."
  586. (define-key keymap (org-key key) def))
  587. (defcustom org-ellipsis nil
  588. "The ellipsis to use in the Org-mode outline.
  589. When nil, just use the standard three dots. When a string, use that instead,
  590. When a face, use the standard 3 dots, but with the specified face.
  591. The change affects only Org-mode (which will then use its own display table).
  592. Changing this requires executing `M-x org-mode' in a buffer to become
  593. effective."
  594. :group 'org-startup
  595. :type '(choice (const :tag "Default" nil)
  596. (face :tag "Face" :value org-warning)
  597. (string :tag "String" :value "...#")))
  598. (defvar org-display-table nil
  599. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  600. (defgroup org-keywords nil
  601. "Keywords in Org-mode."
  602. :tag "Org Keywords"
  603. :group 'org)
  604. (defcustom org-deadline-string "DEADLINE:"
  605. "String to mark deadline entries.
  606. A deadline is this string, followed by a time stamp. Should be a word,
  607. terminated by a colon. You can insert a schedule keyword and
  608. a timestamp with \\[org-deadline].
  609. Changes become only effective after restarting Emacs."
  610. :group 'org-keywords
  611. :type 'string)
  612. (defcustom org-scheduled-string "SCHEDULED:"
  613. "String to mark scheduled TODO entries.
  614. A schedule is this string, followed by a time stamp. Should be a word,
  615. terminated by a colon. You can insert a schedule keyword and
  616. a timestamp with \\[org-schedule].
  617. Changes become only effective after restarting Emacs."
  618. :group 'org-keywords
  619. :type 'string)
  620. (defcustom org-closed-string "CLOSED:"
  621. "String used as the prefix for timestamps logging closing a TODO entry."
  622. :group 'org-keywords
  623. :type 'string)
  624. (defcustom org-clock-string "CLOCK:"
  625. "String used as prefix for timestamps clocking work hours on an item."
  626. :group 'org-keywords
  627. :type 'string)
  628. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  629. org-scheduled-string "\\|"
  630. org-deadline-string "\\|"
  631. org-closed-string "\\|"
  632. org-clock-string "\\)")
  633. "Matches a line with planning or clock info.")
  634. (defcustom org-comment-string "COMMENT"
  635. "Entries starting with this keyword will never be exported.
  636. An entry can be toggled between COMMENT and normal with
  637. \\[org-toggle-comment].
  638. Changes become only effective after restarting Emacs."
  639. :group 'org-keywords
  640. :type 'string)
  641. (defcustom org-quote-string "QUOTE"
  642. "Entries starting with this keyword will be exported in fixed-width font.
  643. Quoting applies only to the text in the entry following the headline, and does
  644. not extend beyond the next headline, even if that is lower level.
  645. An entry can be toggled between QUOTE and normal with
  646. \\[org-toggle-fixed-width-section]."
  647. :group 'org-keywords
  648. :type 'string)
  649. (defconst org-repeat-re
  650. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  651. "Regular expression for specifying repeated events.
  652. After a match, group 1 contains the repeat expression.")
  653. (defgroup org-structure nil
  654. "Options concerning the general structure of Org-mode files."
  655. :tag "Org Structure"
  656. :group 'org)
  657. (defgroup org-reveal-location nil
  658. "Options about how to make context of a location visible."
  659. :tag "Org Reveal Location"
  660. :group 'org-structure)
  661. (defconst org-context-choice
  662. '(choice
  663. (const :tag "Always" t)
  664. (const :tag "Never" nil)
  665. (repeat :greedy t :tag "Individual contexts"
  666. (cons
  667. (choice :tag "Context"
  668. (const agenda)
  669. (const org-goto)
  670. (const occur-tree)
  671. (const tags-tree)
  672. (const link-search)
  673. (const mark-goto)
  674. (const bookmark-jump)
  675. (const isearch)
  676. (const default))
  677. (boolean))))
  678. "Contexts for the reveal options.")
  679. (defcustom org-show-hierarchy-above '((default . t))
  680. "Non-nil means show full hierarchy when revealing a location.
  681. Org-mode often shows locations in an org-mode file which might have
  682. been invisible before. When this is set, the hierarchy of headings
  683. above the exposed location is shown.
  684. Turning this off for example for sparse trees makes them very compact.
  685. Instead of t, this can also be an alist specifying this option for different
  686. contexts. Valid contexts are
  687. agenda when exposing an entry from the agenda
  688. org-goto when using the command `org-goto' on key C-c C-j
  689. occur-tree when using the command `org-occur' on key C-c /
  690. tags-tree when constructing a sparse tree based on tags matches
  691. link-search when exposing search matches associated with a link
  692. mark-goto when exposing the jump goal of a mark
  693. bookmark-jump when exposing a bookmark location
  694. isearch when exiting from an incremental search
  695. default default for all contexts not set explicitly"
  696. :group 'org-reveal-location
  697. :type org-context-choice)
  698. (defcustom org-show-following-heading '((default . nil))
  699. "Non-nil means show following heading when revealing a location.
  700. Org-mode often shows locations in an org-mode file which might have
  701. been invisible before. When this is set, the heading following the
  702. match is shown.
  703. Turning this off for example for sparse trees makes them very compact,
  704. but makes it harder to edit the location of the match. In such a case,
  705. use the command \\[org-reveal] to show more context.
  706. Instead of t, this can also be an alist specifying this option for different
  707. contexts. See `org-show-hierarchy-above' for valid contexts."
  708. :group 'org-reveal-location
  709. :type org-context-choice)
  710. (defcustom org-show-siblings '((default . nil) (isearch t))
  711. "Non-nil means show all sibling heading when revealing a location.
  712. Org-mode often shows locations in an org-mode file which might have
  713. been invisible before. When this is set, the sibling of the current entry
  714. heading are all made visible. If `org-show-hierarchy-above' is t,
  715. the same happens on each level of the hierarchy above the current entry.
  716. By default this is on for the isearch context, off for all other contexts.
  717. Turning this off for example for sparse trees makes them very compact,
  718. but makes it harder to edit the location of the match. In such a case,
  719. use the command \\[org-reveal] to show more context.
  720. Instead of t, this can also be an alist specifying this option for different
  721. contexts. See `org-show-hierarchy-above' for valid contexts."
  722. :group 'org-reveal-location
  723. :type org-context-choice)
  724. (defcustom org-show-entry-below '((default . nil))
  725. "Non-nil means show the entry below a headline when revealing a location.
  726. Org-mode often shows locations in an org-mode file which might have
  727. been invisible before. When this is set, the text below the headline that is
  728. exposed is also shown.
  729. By default this is off for all contexts.
  730. Instead of t, this can also be an alist specifying this option for different
  731. contexts. See `org-show-hierarchy-above' for valid contexts."
  732. :group 'org-reveal-location
  733. :type org-context-choice)
  734. (defcustom org-indirect-buffer-display 'other-window
  735. "How should indirect tree buffers be displayed?
  736. This applies to indirect buffers created with the commands
  737. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  738. Valid values are:
  739. current-window Display in the current window
  740. other-window Just display in another window.
  741. dedicated-frame Create one new frame, and re-use it each time.
  742. new-frame Make a new frame each time. Note that in this case
  743. previously-made indirect buffers are kept, and you need to
  744. kill these buffers yourself."
  745. :group 'org-structure
  746. :group 'org-agenda-windows
  747. :type '(choice
  748. (const :tag "In current window" current-window)
  749. (const :tag "In current frame, other window" other-window)
  750. (const :tag "Each time a new frame" new-frame)
  751. (const :tag "One dedicated frame" dedicated-frame)))
  752. (defcustom org-use-speed-commands nil
  753. "Non-nil means activate single letter commands at beginning of a headline.
  754. This may also be a function to test for appropriate locations where speed
  755. commands should be active."
  756. :group 'org-structure
  757. :type '(choice
  758. (const :tag "Never" nil)
  759. (const :tag "At beginning of headline stars" t)
  760. (function)))
  761. (defcustom org-speed-commands-user nil
  762. "Alist of additional speed commands.
  763. This list will be checked before `org-speed-commands-default'
  764. when the variable `org-use-speed-commands' is non-nil
  765. and when the cursor is at the beginning of a headline.
  766. The car if each entry is a string with a single letter, which must
  767. be assigned to `self-insert-command' in the global map.
  768. The cdr is either a command to be called interactively, a function
  769. to be called, or a form to be evaluated.
  770. An entry that is just a list with a single string will be interpreted
  771. as a descriptive headline that will be added when listing the speed
  772. commands in the Help buffer using the `?' speed command."
  773. :group 'org-structure
  774. :type '(repeat :value ("k" . ignore)
  775. (choice :value ("k" . ignore)
  776. (list :tag "Descriptive Headline" (string :tag "Headline"))
  777. (cons :tag "Letter and Command"
  778. (string :tag "Command letter")
  779. (choice
  780. (function)
  781. (sexp))))))
  782. (defgroup org-cycle nil
  783. "Options concerning visibility cycling in Org-mode."
  784. :tag "Org Cycle"
  785. :group 'org-structure)
  786. (defcustom org-cycle-skip-children-state-if-no-children t
  787. "Non-nil means skip CHILDREN state in entries that don't have any."
  788. :group 'org-cycle
  789. :type 'boolean)
  790. (defcustom org-cycle-max-level nil
  791. "Maximum level which should still be subject to visibility cycling.
  792. Levels higher than this will, for cycling, be treated as text, not a headline.
  793. When `org-odd-levels-only' is set, a value of N in this variable actually
  794. means 2N-1 stars as the limiting headline.
  795. When nil, cycle all levels.
  796. Note that the limiting level of cycling is also influenced by
  797. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  798. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  799. than its value."
  800. :group 'org-cycle
  801. :type '(choice
  802. (const :tag "No limit" nil)
  803. (integer :tag "Maximum level")))
  804. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  805. "Names of drawers. Drawers are not opened by cycling on the headline above.
  806. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  807. this:
  808. :DRAWERNAME:
  809. .....
  810. :END:
  811. The drawer \"PROPERTIES\" is special for capturing properties through
  812. the property API.
  813. Drawers can be defined on the per-file basis with a line like:
  814. #+DRAWERS: HIDDEN STATE PROPERTIES"
  815. :group 'org-structure
  816. :group 'org-cycle
  817. :type '(repeat (string :tag "Drawer Name")))
  818. (defcustom org-hide-block-startup nil
  819. "Non-nil means entering Org-mode will fold all blocks.
  820. This can also be set in on a per-file basis with
  821. #+STARTUP: hideblocks
  822. #+STARTUP: showblocks"
  823. :group 'org-startup
  824. :group 'org-cycle
  825. :type 'boolean)
  826. (defcustom org-cycle-global-at-bob nil
  827. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  828. This makes it possible to do global cycling without having to use S-TAB or
  829. \\[universal-argument] TAB. For this special case to work, the first line
  830. of the buffer must not be a headline -- it may be empty or some other text.
  831. When used in this way, `org-cycle-hook' is disabled temporarily to make
  832. sure the cursor stays at the beginning of the buffer. When this option is
  833. nil, don't do anything special at the beginning of the buffer."
  834. :group 'org-cycle
  835. :type 'boolean)
  836. (defcustom org-cycle-level-after-item/entry-creation t
  837. "Non-nil means cycle entry level or item indentation in new empty entries.
  838. When the cursor is at the end of an empty headline, i.e with only stars
  839. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  840. and then all possible ancestor states, before returning to the original state.
  841. This makes data entry extremely fast: M-RET to create a new headline,
  842. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  843. When the cursor is at the end of an empty plain list item, one TAB will
  844. make it a subitem, two or more tabs will back up to make this an item
  845. higher up in the item hierarchy."
  846. :group 'org-cycle
  847. :type 'boolean)
  848. (defcustom org-cycle-emulate-tab t
  849. "Where should `org-cycle' emulate TAB.
  850. nil Never
  851. white Only in completely white lines
  852. whitestart Only at the beginning of lines, before the first non-white char
  853. t Everywhere except in headlines
  854. exc-hl-bol Everywhere except at the start of a headline
  855. If TAB is used in a place where it does not emulate TAB, the current subtree
  856. visibility is cycled."
  857. :group 'org-cycle
  858. :type '(choice (const :tag "Never" nil)
  859. (const :tag "Only in completely white lines" white)
  860. (const :tag "Before first char in a line" whitestart)
  861. (const :tag "Everywhere except in headlines" t)
  862. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  863. ))
  864. (defcustom org-cycle-separator-lines 2
  865. "Number of empty lines needed to keep an empty line between collapsed trees.
  866. If you leave an empty line between the end of a subtree and the following
  867. headline, this empty line is hidden when the subtree is folded.
  868. Org-mode will leave (exactly) one empty line visible if the number of
  869. empty lines is equal or larger to the number given in this variable.
  870. So the default 2 means at least 2 empty lines after the end of a subtree
  871. are needed to produce free space between a collapsed subtree and the
  872. following headline.
  873. If the number is negative, and the number of empty lines is at least -N,
  874. all empty lines are shown.
  875. Special case: when 0, never leave empty lines in collapsed view."
  876. :group 'org-cycle
  877. :type 'integer)
  878. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  879. (defcustom org-pre-cycle-hook nil
  880. "Hook that is run before visibility cycling is happening.
  881. The function(s) in this hook must accept a single argument which indicates
  882. the new state that will be set right after running this hook. The
  883. argument is a symbol. Before a global state change, it can have the values
  884. `overview', `content', or `all'. Before a local state change, it can have
  885. the values `folded', `children', or `subtree'."
  886. :group 'org-cycle
  887. :type 'hook)
  888. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  889. org-cycle-hide-drawers
  890. org-cycle-hide-inline-tasks
  891. org-cycle-show-empty-lines
  892. org-optimize-window-after-visibility-change)
  893. "Hook that is run after `org-cycle' has changed the buffer visibility.
  894. The function(s) in this hook must accept a single argument which indicates
  895. the new state that was set by the most recent `org-cycle' command. The
  896. argument is a symbol. After a global state change, it can have the values
  897. `overview', `contents', or `all'. After a local state change, it can have
  898. the values `folded', `children', or `subtree'."
  899. :group 'org-cycle
  900. :type 'hook)
  901. (defgroup org-edit-structure nil
  902. "Options concerning structure editing in Org-mode."
  903. :tag "Org Edit Structure"
  904. :group 'org-structure)
  905. (defcustom org-odd-levels-only nil
  906. "Non-nil means skip even levels and only use odd levels for the outline.
  907. This has the effect that two stars are being added/taken away in
  908. promotion/demotion commands. It also influences how levels are
  909. handled by the exporters.
  910. Changing it requires restart of `font-lock-mode' to become effective
  911. for fontification also in regions already fontified.
  912. You may also set this on a per-file basis by adding one of the following
  913. lines to the buffer:
  914. #+STARTUP: odd
  915. #+STARTUP: oddeven"
  916. :group 'org-edit-structure
  917. :group 'org-appearance
  918. :type 'boolean)
  919. (defcustom org-adapt-indentation t
  920. "Non-nil means adapt indentation to outline node level.
  921. When this variable is set, Org assumes that you write outlines by
  922. indenting text in each node to align with the headline (after the stars).
  923. The following issues are influenced by this variable:
  924. - When this is set and the *entire* text in an entry is indented, the
  925. indentation is increased by one space in a demotion command, and
  926. decreased by one in a promotion command. If any line in the entry
  927. body starts with text at column 0, indentation is not changed at all.
  928. - Property drawers and planning information is inserted indented when
  929. this variable s set. When nil, they will not be indented.
  930. - TAB indents a line relative to context. The lines below a headline
  931. will be indented when this variable is set.
  932. Note that this is all about true indentation, by adding and removing
  933. space characters. See also `org-indent.el' which does level-dependent
  934. indentation in a virtual way, i.e. at display time in Emacs."
  935. :group 'org-edit-structure
  936. :type 'boolean)
  937. (defcustom org-special-ctrl-a/e nil
  938. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  939. When t, `C-a' will bring back the cursor to the beginning of the
  940. headline text, i.e. after the stars and after a possible TODO
  941. keyword. In an item, this will be the position after bullet and
  942. check-box, if any. When the cursor is already at that position,
  943. another `C-a' will bring it to the beginning of the line.
  944. `C-e' will jump to the end of the headline, ignoring the presence
  945. of tags in the headline. A second `C-e' will then jump to the
  946. true end of the line, after any tags. This also means that, when
  947. this variable is non-nil, `C-e' also will never jump beyond the
  948. end of the heading of a folded section, i.e. not after the
  949. ellipses.
  950. When set to the symbol `reversed', the first `C-a' or `C-e' works
  951. normally, going to the true line boundary first. Only a directly
  952. following, identical keypress will bring the cursor to the
  953. special positions.
  954. This may also be a cons cell where the behavior for `C-a' and
  955. `C-e' is set separately."
  956. :group 'org-edit-structure
  957. :type '(choice
  958. (const :tag "off" nil)
  959. (const :tag "on: after stars/bullet and before tags first" t)
  960. (const :tag "reversed: true line boundary first" reversed)
  961. (cons :tag "Set C-a and C-e separately"
  962. (choice :tag "Special C-a"
  963. (const :tag "off" nil)
  964. (const :tag "on: after stars/bullet first" t)
  965. (const :tag "reversed: before stars/bullet first" reversed))
  966. (choice :tag "Special C-e"
  967. (const :tag "off" nil)
  968. (const :tag "on: before tags first" t)
  969. (const :tag "reversed: after tags first" reversed)))))
  970. (if (fboundp 'defvaralias)
  971. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  972. (defcustom org-special-ctrl-k nil
  973. "Non-nil means `C-k' will behave specially in headlines.
  974. When nil, `C-k' will call the default `kill-line' command.
  975. When t, the following will happen while the cursor is in the headline:
  976. - When the cursor is at the beginning of a headline, kill the entire
  977. line and possible the folded subtree below the line.
  978. - When in the middle of the headline text, kill the headline up to the tags.
  979. - When after the headline text, kill the tags."
  980. :group 'org-edit-structure
  981. :type 'boolean)
  982. (defcustom org-ctrl-k-protect-subtree nil
  983. "Non-nil means, do not delete a hidden subtree with C-k.
  984. When set to the symbol `error', simply throw an error when C-k is
  985. used to kill (part-of) a headline that has hidden text behind it.
  986. Any other non-nil value will result in a query to the user, if it is
  987. OK to kill that hidden subtree. When nil, kill without remorse."
  988. :group 'org-edit-structure
  989. :version "24.1"
  990. :type '(choice
  991. (const :tag "Do not protect hidden subtrees" nil)
  992. (const :tag "Protect hidden subtrees with a security query" t)
  993. (const :tag "Never kill a hidden subtree with C-k" error)))
  994. (defcustom org-catch-invisible-edits nil
  995. "Check if in invisible region before inserting or deleting a character.
  996. Valid values are:
  997. nil Do not check, so just do invisible edits.
  998. error Throw an error and do nothing.
  999. show Make point visible, and do the requested edit.
  1000. show-and-error Make point visible, then throw an error and abort the edit.
  1001. smart Make point visible, and do insertion/deletion if it is
  1002. adjacent to visible text and the change feels predictable.
  1003. Never delete a previously invisible character or add in the
  1004. middle or right after an invisible region. Basically, this
  1005. allows insertion and backward-delete right before ellipses.
  1006. FIXME: maybe in this case we should not even show?"
  1007. :group 'org-edit-structure
  1008. :version "24.1"
  1009. :type '(choice
  1010. (const :tag "Do not check" nil)
  1011. (const :tag "Throw error when trying to edit" error)
  1012. (const :tag "Unhide, but do not do the edit" show-and-error)
  1013. (const :tag "Show invisible part and do the edit" show)
  1014. (const :tag "Be smart and do the right thing" smart)))
  1015. (defcustom org-yank-folded-subtrees t
  1016. "Non-nil means when yanking subtrees, fold them.
  1017. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1018. it starts with a heading and all other headings in it are either children
  1019. or siblings, then fold all the subtrees. However, do this only if no
  1020. text after the yank would be swallowed into a folded tree by this action."
  1021. :group 'org-edit-structure
  1022. :type 'boolean)
  1023. (defcustom org-yank-adjusted-subtrees nil
  1024. "Non-nil means when yanking subtrees, adjust the level.
  1025. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1026. this function for details."
  1027. :group 'org-edit-structure
  1028. :type 'boolean)
  1029. (defcustom org-M-RET-may-split-line '((default . t))
  1030. "Non-nil means M-RET will split the line at the cursor position.
  1031. When nil, it will go to the end of the line before making a
  1032. new line.
  1033. You may also set this option in a different way for different
  1034. contexts. Valid contexts are:
  1035. headline when creating a new headline
  1036. item when creating a new item
  1037. table in a table field
  1038. default the value to be used for all contexts not explicitly
  1039. customized"
  1040. :group 'org-structure
  1041. :group 'org-table
  1042. :type '(choice
  1043. (const :tag "Always" t)
  1044. (const :tag "Never" nil)
  1045. (repeat :greedy t :tag "Individual contexts"
  1046. (cons
  1047. (choice :tag "Context"
  1048. (const headline)
  1049. (const item)
  1050. (const table)
  1051. (const default))
  1052. (boolean)))))
  1053. (defcustom org-insert-heading-respect-content nil
  1054. "Non-nil means insert new headings after the current subtree.
  1055. When nil, the new heading is created directly after the current line.
  1056. The commands \\[org-insert-heading-respect-content] and
  1057. \\[org-insert-todo-heading-respect-content] turn this variable on
  1058. for the duration of the command."
  1059. :group 'org-structure
  1060. :type 'boolean)
  1061. (defcustom org-blank-before-new-entry '((heading . auto)
  1062. (plain-list-item . auto))
  1063. "Should `org-insert-heading' leave a blank line before new heading/item?
  1064. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1065. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1066. which case Org will look at the surrounding headings/items and try to
  1067. make an intelligent decision whether to insert a blank line or not.
  1068. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1069. set, the setting here is ignored and no empty line is inserted, to avoid
  1070. breaking the list structure."
  1071. :group 'org-edit-structure
  1072. :type '(list
  1073. (cons (const heading)
  1074. (choice (const :tag "Never" nil)
  1075. (const :tag "Always" t)
  1076. (const :tag "Auto" auto)))
  1077. (cons (const plain-list-item)
  1078. (choice (const :tag "Never" nil)
  1079. (const :tag "Always" t)
  1080. (const :tag "Auto" auto)))))
  1081. (defcustom org-insert-heading-hook nil
  1082. "Hook being run after inserting a new heading."
  1083. :group 'org-edit-structure
  1084. :type 'hook)
  1085. (defcustom org-enable-fixed-width-editor t
  1086. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1087. This currently only means they are never auto-wrapped.
  1088. When nil, such lines will be treated like ordinary lines.
  1089. See also the QUOTE keyword."
  1090. :group 'org-edit-structure
  1091. :type 'boolean)
  1092. (defcustom org-goto-auto-isearch t
  1093. "Non-nil means typing characters in `org-goto' starts incremental search."
  1094. :group 'org-edit-structure
  1095. :type 'boolean)
  1096. (defgroup org-sparse-trees nil
  1097. "Options concerning sparse trees in Org-mode."
  1098. :tag "Org Sparse Trees"
  1099. :group 'org-structure)
  1100. (defcustom org-highlight-sparse-tree-matches t
  1101. "Non-nil means highlight all matches that define a sparse tree.
  1102. The highlights will automatically disappear the next time the buffer is
  1103. changed by an edit command."
  1104. :group 'org-sparse-trees
  1105. :type 'boolean)
  1106. (defcustom org-remove-highlights-with-change t
  1107. "Non-nil means any change to the buffer will remove temporary highlights.
  1108. Such highlights are created by `org-occur' and `org-clock-display'.
  1109. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1110. The highlights created by `org-preview-latex-fragment' always need
  1111. `C-c C-c' to be removed."
  1112. :group 'org-sparse-trees
  1113. :group 'org-time
  1114. :type 'boolean)
  1115. (defcustom org-occur-hook '(org-first-headline-recenter)
  1116. "Hook that is run after `org-occur' has constructed a sparse tree.
  1117. This can be used to recenter the window to show as much of the structure
  1118. as possible."
  1119. :group 'org-sparse-trees
  1120. :type 'hook)
  1121. (defgroup org-imenu-and-speedbar nil
  1122. "Options concerning imenu and speedbar in Org-mode."
  1123. :tag "Org Imenu and Speedbar"
  1124. :group 'org-structure)
  1125. (defcustom org-imenu-depth 2
  1126. "The maximum level for Imenu access to Org-mode headlines.
  1127. This also applied for speedbar access."
  1128. :group 'org-imenu-and-speedbar
  1129. :type 'integer)
  1130. (defgroup org-table nil
  1131. "Options concerning tables in Org-mode."
  1132. :tag "Org Table"
  1133. :group 'org)
  1134. (defcustom org-enable-table-editor 'optimized
  1135. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1136. When nil, such lines will be treated like ordinary lines.
  1137. When equal to the symbol `optimized', the table editor will be optimized to
  1138. do the following:
  1139. - Automatic overwrite mode in front of whitespace in table fields.
  1140. This makes the structure of the table stay in tact as long as the edited
  1141. field does not exceed the column width.
  1142. - Minimize the number of realigns. Normally, the table is aligned each time
  1143. TAB or RET are pressed to move to another field. With optimization this
  1144. happens only if changes to a field might have changed the column width.
  1145. Optimization requires replacing the functions `self-insert-command',
  1146. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1147. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1148. very good at guessing when a re-align will be necessary, but you can always
  1149. force one with \\[org-ctrl-c-ctrl-c].
  1150. If you would like to use the optimized version in Org-mode, but the
  1151. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1152. This variable can be used to turn on and off the table editor during a session,
  1153. but in order to toggle optimization, a restart is required.
  1154. See also the variable `org-table-auto-blank-field'."
  1155. :group 'org-table
  1156. :type '(choice
  1157. (const :tag "off" nil)
  1158. (const :tag "on" t)
  1159. (const :tag "on, optimized" optimized)))
  1160. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1161. (version<= emacs-version "24.1"))
  1162. "Non-nil means cluster self-insert commands for undo when possible.
  1163. If this is set, then, like in the Emacs command loop, 20 consecutive
  1164. characters will be undone together.
  1165. This is configurable, because there is some impact on typing performance."
  1166. :group 'org-table
  1167. :type 'boolean)
  1168. (defcustom org-table-tab-recognizes-table.el t
  1169. "Non-nil means TAB will automatically notice a table.el table.
  1170. When it sees such a table, it moves point into it and - if necessary -
  1171. calls `table-recognize-table'."
  1172. :group 'org-table-editing
  1173. :type 'boolean)
  1174. (defgroup org-link nil
  1175. "Options concerning links in Org-mode."
  1176. :tag "Org Link"
  1177. :group 'org)
  1178. (defvar org-link-abbrev-alist-local nil
  1179. "Buffer-local version of `org-link-abbrev-alist', which see.
  1180. The value of this is taken from the #+LINK lines.")
  1181. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1182. (defcustom org-link-abbrev-alist nil
  1183. "Alist of link abbreviations.
  1184. The car of each element is a string, to be replaced at the start of a link.
  1185. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1186. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1187. [[linkkey:tag][description]]
  1188. The 'linkkey' must be a word word, starting with a letter, followed
  1189. by letters, numbers, '-' or '_'.
  1190. If REPLACE is a string, the tag will simply be appended to create the link.
  1191. If the string contains \"%s\", the tag will be inserted there. If the string
  1192. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1193. at that point (see the function `url-hexify-string'). If the string contains
  1194. the specifier \"%(my-function)\", then the custom function `my-function' will
  1195. be invoked: this function takes the tag as its only argument and must return
  1196. a string.
  1197. REPLACE may also be a function that will be called with the tag as the
  1198. only argument to create the link, which should be returned as a string.
  1199. See the manual for examples."
  1200. :group 'org-link
  1201. :type '(repeat
  1202. (cons
  1203. (string :tag "Protocol")
  1204. (choice
  1205. (string :tag "Format")
  1206. (function)))))
  1207. (defcustom org-descriptive-links t
  1208. "Non-nil means Org will display descriptive links.
  1209. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1210. \"Org Website\", hiding the link itself and just displaying its
  1211. description. When set to `nil', Org will display the full links
  1212. literally.
  1213. You can interactively set the value of this variable by calling
  1214. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1215. :group 'org-link
  1216. :type 'boolean)
  1217. (defcustom org-link-file-path-type 'adaptive
  1218. "How the path name in file links should be stored.
  1219. Valid values are:
  1220. relative Relative to the current directory, i.e. the directory of the file
  1221. into which the link is being inserted.
  1222. absolute Absolute path, if possible with ~ for home directory.
  1223. noabbrev Absolute path, no abbreviation of home directory.
  1224. adaptive Use relative path for files in the current directory and sub-
  1225. directories of it. For other files, use an absolute path."
  1226. :group 'org-link
  1227. :type '(choice
  1228. (const relative)
  1229. (const absolute)
  1230. (const noabbrev)
  1231. (const adaptive)))
  1232. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1233. "Types of links that should be activated in Org-mode files.
  1234. This is a list of symbols, each leading to the activation of a certain link
  1235. type. In principle, it does not hurt to turn on most link types - there may
  1236. be a small gain when turning off unused link types. The types are:
  1237. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1238. angle Links in angular brackets that may contain whitespace like
  1239. <bbdb:Carsten Dominik>.
  1240. plain Plain links in normal text, no whitespace, like http://google.com.
  1241. radio Text that is matched by a radio target, see manual for details.
  1242. tag Tag settings in a headline (link to tag search).
  1243. date Time stamps (link to calendar).
  1244. footnote Footnote labels.
  1245. Changing this variable requires a restart of Emacs to become effective."
  1246. :group 'org-link
  1247. :type '(set :greedy t
  1248. (const :tag "Double bracket links" bracket)
  1249. (const :tag "Angular bracket links" angle)
  1250. (const :tag "Plain text links" plain)
  1251. (const :tag "Radio target matches" radio)
  1252. (const :tag "Tags" tag)
  1253. (const :tag "Timestamps" date)
  1254. (const :tag "Footnotes" footnote)))
  1255. (defcustom org-make-link-description-function nil
  1256. "Function to use for generating link descriptions from links.
  1257. When nil, the link location will be used. This function must take
  1258. two parameters: the first one is the link, the second one is the
  1259. description generated by `org-insert-link'. The function should
  1260. return the description to use."
  1261. :group 'org-link
  1262. :type 'function)
  1263. (defgroup org-link-store nil
  1264. "Options concerning storing links in Org-mode."
  1265. :tag "Org Store Link"
  1266. :group 'org-link)
  1267. (defcustom org-url-hexify-p t
  1268. "When non-nil, hexify URL when creating a link."
  1269. :type 'boolean
  1270. :version "24.3"
  1271. :group 'org-link-store)
  1272. (defcustom org-email-link-description-format "Email %c: %.30s"
  1273. "Format of the description part of a link to an email or usenet message.
  1274. The following %-escapes will be replaced by corresponding information:
  1275. %F full \"From\" field
  1276. %f name, taken from \"From\" field, address if no name
  1277. %T full \"To\" field
  1278. %t first name in \"To\" field, address if no name
  1279. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1280. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1281. %s subject
  1282. %d date
  1283. %m message-id.
  1284. You may use normal field width specification between the % and the letter.
  1285. This is for example useful to limit the length of the subject.
  1286. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1287. :group 'org-link-store
  1288. :type 'string)
  1289. (defcustom org-from-is-user-regexp
  1290. (let (r1 r2)
  1291. (when (and user-mail-address (not (string= user-mail-address "")))
  1292. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1293. (when (and user-full-name (not (string= user-full-name "")))
  1294. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1295. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1296. "Regexp matched against the \"From:\" header of an email or usenet message.
  1297. It should match if the message is from the user him/herself."
  1298. :group 'org-link-store
  1299. :type 'regexp)
  1300. (defcustom org-context-in-file-links t
  1301. "Non-nil means file links from `org-store-link' contain context.
  1302. A search string will be added to the file name with :: as separator and
  1303. used to find the context when the link is activated by the command
  1304. `org-open-at-point'. When this option is t, the entire active region
  1305. will be placed in the search string of the file link. If set to a
  1306. positive integer, only the first n lines of context will be stored.
  1307. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1308. negates this setting for the duration of the command."
  1309. :group 'org-link-store
  1310. :type '(choice boolean integer))
  1311. (defcustom org-keep-stored-link-after-insertion nil
  1312. "Non-nil means keep link in list for entire session.
  1313. The command `org-store-link' adds a link pointing to the current
  1314. location to an internal list. These links accumulate during a session.
  1315. The command `org-insert-link' can be used to insert links into any
  1316. Org-mode file (offering completion for all stored links). When this
  1317. option is nil, every link which has been inserted once using \\[org-insert-link]
  1318. will be removed from the list, to make completing the unused links
  1319. more efficient."
  1320. :group 'org-link-store
  1321. :type 'boolean)
  1322. (defgroup org-link-follow nil
  1323. "Options concerning following links in Org-mode."
  1324. :tag "Org Follow Link"
  1325. :group 'org-link)
  1326. (defcustom org-link-translation-function nil
  1327. "Function to translate links with different syntax to Org syntax.
  1328. This can be used to translate links created for example by the Planner
  1329. or emacs-wiki packages to Org syntax.
  1330. The function must accept two parameters, a TYPE containing the link
  1331. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1332. which is everything after the link protocol. It should return a cons
  1333. with possibly modified values of type and path.
  1334. Org contains a function for this, so if you set this variable to
  1335. `org-translate-link-from-planner', you should be able follow many
  1336. links created by planner."
  1337. :group 'org-link-follow
  1338. :type 'function)
  1339. (defcustom org-follow-link-hook nil
  1340. "Hook that is run after a link has been followed."
  1341. :group 'org-link-follow
  1342. :type 'hook)
  1343. (defcustom org-tab-follows-link nil
  1344. "Non-nil means on links TAB will follow the link.
  1345. Needs to be set before org.el is loaded.
  1346. This really should not be used, it does not make sense, and the
  1347. implementation is bad."
  1348. :group 'org-link-follow
  1349. :type 'boolean)
  1350. (defcustom org-return-follows-link nil
  1351. "Non-nil means on links RET will follow the link."
  1352. :group 'org-link-follow
  1353. :type 'boolean)
  1354. (defcustom org-mouse-1-follows-link
  1355. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1356. "Non-nil means mouse-1 on a link will follow the link.
  1357. A longer mouse click will still set point. Does not work on XEmacs.
  1358. Needs to be set before org.el is loaded."
  1359. :group 'org-link-follow
  1360. :type 'boolean)
  1361. (defcustom org-mark-ring-length 4
  1362. "Number of different positions to be recorded in the ring.
  1363. Changing this requires a restart of Emacs to work correctly."
  1364. :group 'org-link-follow
  1365. :type 'integer)
  1366. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1367. "Non-nil means internal links in Org files must exactly match a headline.
  1368. When nil, the link search tries to match a phrase with all words
  1369. in the search text."
  1370. :group 'org-link-follow
  1371. :version "24.1"
  1372. :type '(choice
  1373. (const :tag "Use fuzzy text search" nil)
  1374. (const :tag "Match only exact headline" t)
  1375. (const :tag "Match exact headline or query to create it"
  1376. query-to-create)))
  1377. (defcustom org-link-frame-setup
  1378. '((vm . vm-visit-folder-other-frame)
  1379. (vm-imap . vm-visit-imap-folder-other-frame)
  1380. (gnus . org-gnus-no-new-news)
  1381. (file . find-file-other-window)
  1382. (wl . wl-other-frame))
  1383. "Setup the frame configuration for following links.
  1384. When following a link with Emacs, it may often be useful to display
  1385. this link in another window or frame. This variable can be used to
  1386. set this up for the different types of links.
  1387. For VM, use any of
  1388. `vm-visit-folder'
  1389. `vm-visit-folder-other-window'
  1390. `vm-visit-folder-other-frame'
  1391. For Gnus, use any of
  1392. `gnus'
  1393. `gnus-other-frame'
  1394. `org-gnus-no-new-news'
  1395. For FILE, use any of
  1396. `find-file'
  1397. `find-file-other-window'
  1398. `find-file-other-frame'
  1399. For Wanderlust use any of
  1400. `wl'
  1401. `wl-other-frame'
  1402. For the calendar, use the variable `calendar-setup'.
  1403. For BBDB, it is currently only possible to display the matches in
  1404. another window."
  1405. :group 'org-link-follow
  1406. :type '(list
  1407. (cons (const vm)
  1408. (choice
  1409. (const vm-visit-folder)
  1410. (const vm-visit-folder-other-window)
  1411. (const vm-visit-folder-other-frame)))
  1412. (cons (const gnus)
  1413. (choice
  1414. (const gnus)
  1415. (const gnus-other-frame)
  1416. (const org-gnus-no-new-news)))
  1417. (cons (const file)
  1418. (choice
  1419. (const find-file)
  1420. (const find-file-other-window)
  1421. (const find-file-other-frame)))
  1422. (cons (const wl)
  1423. (choice
  1424. (const wl)
  1425. (const wl-other-frame)))))
  1426. (defcustom org-display-internal-link-with-indirect-buffer nil
  1427. "Non-nil means use indirect buffer to display infile links.
  1428. Activating internal links (from one location in a file to another location
  1429. in the same file) normally just jumps to the location. When the link is
  1430. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1431. is displayed in
  1432. another window. When this option is set, the other window actually displays
  1433. an indirect buffer clone of the current buffer, to avoid any visibility
  1434. changes to the current buffer."
  1435. :group 'org-link-follow
  1436. :type 'boolean)
  1437. (defcustom org-open-non-existing-files nil
  1438. "Non-nil means `org-open-file' will open non-existing files.
  1439. When nil, an error will be generated.
  1440. This variable applies only to external applications because they
  1441. might choke on non-existing files. If the link is to a file that
  1442. will be opened in Emacs, the variable is ignored."
  1443. :group 'org-link-follow
  1444. :type 'boolean)
  1445. (defcustom org-open-directory-means-index-dot-org nil
  1446. "Non-nil means a link to a directory really means to index.org.
  1447. When nil, following a directory link will run dired or open a finder/explorer
  1448. window on that directory."
  1449. :group 'org-link-follow
  1450. :type 'boolean)
  1451. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1452. "Function and arguments to call for following mailto links.
  1453. This is a list with the first element being a Lisp function, and the
  1454. remaining elements being arguments to the function. In string arguments,
  1455. %a will be replaced by the address, and %s will be replaced by the subject
  1456. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1457. :group 'org-link-follow
  1458. :type '(choice
  1459. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1460. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1461. (const :tag "message-mail" (message-mail "%a" "%s"))
  1462. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1463. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1464. "Non-nil means ask for confirmation before executing shell links.
  1465. Shell links can be dangerous: just think about a link
  1466. [[shell:rm -rf ~/*][Google Search]]
  1467. This link would show up in your Org-mode document as \"Google Search\",
  1468. but really it would remove your entire home directory.
  1469. Therefore we advise against setting this variable to nil.
  1470. Just change it to `y-or-n-p' if you want to confirm with a
  1471. single keystroke rather than having to type \"yes\"."
  1472. :group 'org-link-follow
  1473. :type '(choice
  1474. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1475. (const :tag "with y-or-n (faster)" y-or-n-p)
  1476. (const :tag "no confirmation (dangerous)" nil)))
  1477. (put 'org-confirm-shell-link-function
  1478. 'safe-local-variable
  1479. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1480. (defcustom org-confirm-shell-link-not-regexp ""
  1481. "A regexp to skip confirmation for shell links."
  1482. :group 'org-link-follow
  1483. :version "24.1"
  1484. :type 'regexp)
  1485. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1486. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1487. Elisp links can be dangerous: just think about a link
  1488. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1489. This link would show up in your Org-mode document as \"Google Search\",
  1490. but really it would remove your entire home directory.
  1491. Therefore we advise against setting this variable to nil.
  1492. Just change it to `y-or-n-p' if you want to confirm with a
  1493. single keystroke rather than having to type \"yes\"."
  1494. :group 'org-link-follow
  1495. :type '(choice
  1496. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1497. (const :tag "with y-or-n (faster)" y-or-n-p)
  1498. (const :tag "no confirmation (dangerous)" nil)))
  1499. (put 'org-confirm-shell-link-function
  1500. 'safe-local-variable
  1501. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1502. (defcustom org-confirm-elisp-link-not-regexp ""
  1503. "A regexp to skip confirmation for Elisp links."
  1504. :group 'org-link-follow
  1505. :version "24.1"
  1506. :type 'regexp)
  1507. (defconst org-file-apps-defaults-gnu
  1508. '((remote . emacs)
  1509. (system . mailcap)
  1510. (t . mailcap))
  1511. "Default file applications on a UNIX or GNU/Linux system.
  1512. See `org-file-apps'.")
  1513. (defconst org-file-apps-defaults-macosx
  1514. '((remote . emacs)
  1515. (t . "open %s")
  1516. (system . "open %s")
  1517. ("ps.gz" . "gv %s")
  1518. ("eps.gz" . "gv %s")
  1519. ("dvi" . "xdvi %s")
  1520. ("fig" . "xfig %s"))
  1521. "Default file applications on a MacOS X system.
  1522. The system \"open\" is known as a default, but we use X11 applications
  1523. for some files for which the OS does not have a good default.
  1524. See `org-file-apps'.")
  1525. (defconst org-file-apps-defaults-windowsnt
  1526. (list
  1527. '(remote . emacs)
  1528. (cons t
  1529. (list (if (featurep 'xemacs)
  1530. 'mswindows-shell-execute
  1531. 'w32-shell-execute)
  1532. "open" 'file))
  1533. (cons 'system
  1534. (list (if (featurep 'xemacs)
  1535. 'mswindows-shell-execute
  1536. 'w32-shell-execute)
  1537. "open" 'file)))
  1538. "Default file applications on a Windows NT system.
  1539. The system \"open\" is used for most files.
  1540. See `org-file-apps'.")
  1541. (defcustom org-file-apps
  1542. '(
  1543. (auto-mode . emacs)
  1544. ("\\.mm\\'" . default)
  1545. ("\\.x?html?\\'" . default)
  1546. ("\\.pdf\\'" . default)
  1547. )
  1548. "External applications for opening `file:path' items in a document.
  1549. Org-mode uses system defaults for different file types, but
  1550. you can use this variable to set the application for a given file
  1551. extension. The entries in this list are cons cells where the car identifies
  1552. files and the cdr the corresponding command. Possible values for the
  1553. file identifier are
  1554. \"string\" A string as a file identifier can be interpreted in different
  1555. ways, depending on its contents:
  1556. - Alphanumeric characters only:
  1557. Match links with this file extension.
  1558. Example: (\"pdf\" . \"evince %s\")
  1559. to open PDFs with evince.
  1560. - Regular expression: Match links where the
  1561. filename matches the regexp. If you want to
  1562. use groups here, use shy groups.
  1563. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1564. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1565. to open *.html and *.xhtml with firefox.
  1566. - Regular expression which contains (non-shy) groups:
  1567. Match links where the whole link, including \"::\", and
  1568. anything after that, matches the regexp.
  1569. In a custom command string, %1, %2, etc. are replaced with
  1570. the parts of the link that were matched by the groups.
  1571. For backwards compatibility, if a command string is given
  1572. that does not use any of the group matches, this case is
  1573. handled identically to the second one (i.e. match against
  1574. file name only).
  1575. In a custom lisp form, you can access the group matches with
  1576. (match-string n link).
  1577. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1578. to open [[file:document.pdf::5]] with evince at page 5.
  1579. `directory' Matches a directory
  1580. `remote' Matches a remote file, accessible through tramp or efs.
  1581. Remote files most likely should be visited through Emacs
  1582. because external applications cannot handle such paths.
  1583. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1584. so all files Emacs knows how to handle. Using this with
  1585. command `emacs' will open most files in Emacs. Beware that this
  1586. will also open html files inside Emacs, unless you add
  1587. (\"html\" . default) to the list as well.
  1588. t Default for files not matched by any of the other options.
  1589. `system' The system command to open files, like `open' on Windows
  1590. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1591. that will be selected if you call `C-c C-o' with a double
  1592. \\[universal-argument] \\[universal-argument] prefix.
  1593. Possible values for the command are:
  1594. `emacs' The file will be visited by the current Emacs process.
  1595. `default' Use the default application for this file type, which is the
  1596. association for t in the list, most likely in the system-specific
  1597. part.
  1598. This can be used to overrule an unwanted setting in the
  1599. system-specific variable.
  1600. `system' Use the system command for opening files, like \"open\".
  1601. This command is specified by the entry whose car is `system'.
  1602. Most likely, the system-specific version of this variable
  1603. does define this command, but you can overrule/replace it
  1604. here.
  1605. string A command to be executed by a shell; %s will be replaced
  1606. by the path to the file.
  1607. sexp A Lisp form which will be evaluated. The file path will
  1608. be available in the Lisp variable `file'.
  1609. For more examples, see the system specific constants
  1610. `org-file-apps-defaults-macosx'
  1611. `org-file-apps-defaults-windowsnt'
  1612. `org-file-apps-defaults-gnu'."
  1613. :group 'org-link-follow
  1614. :type '(repeat
  1615. (cons (choice :value ""
  1616. (string :tag "Extension")
  1617. (const :tag "System command to open files" system)
  1618. (const :tag "Default for unrecognized files" t)
  1619. (const :tag "Remote file" remote)
  1620. (const :tag "Links to a directory" directory)
  1621. (const :tag "Any files that have Emacs modes"
  1622. auto-mode))
  1623. (choice :value ""
  1624. (const :tag "Visit with Emacs" emacs)
  1625. (const :tag "Use default" default)
  1626. (const :tag "Use the system command" system)
  1627. (string :tag "Command")
  1628. (sexp :tag "Lisp form")))))
  1629. (defcustom org-doi-server-url "http://dx.doi.org/"
  1630. "The URL of the DOI server."
  1631. :type 'string
  1632. :version "24.3"
  1633. :group 'org-link-follow)
  1634. (defgroup org-refile nil
  1635. "Options concerning refiling entries in Org-mode."
  1636. :tag "Org Refile"
  1637. :group 'org)
  1638. (defcustom org-directory "~/org"
  1639. "Directory with org files.
  1640. This is just a default location to look for Org files. There is no need
  1641. at all to put your files into this directory. It is only used in the
  1642. following situations:
  1643. 1. When a capture template specifies a target file that is not an
  1644. absolute path. The path will then be interpreted relative to
  1645. `org-directory'
  1646. 2. When a capture note is filed away in an interactive way (when exiting the
  1647. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1648. with `org-directory' as the default path."
  1649. :group 'org-refile
  1650. :group 'org-remember
  1651. :group 'org-capture
  1652. :type 'directory)
  1653. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1654. "Default target for storing notes.
  1655. Used as a fall back file for org-remember.el and org-capture.el, for
  1656. templates that do not specify a target file."
  1657. :group 'org-refile
  1658. :group 'org-remember
  1659. :group 'org-capture
  1660. :type '(choice
  1661. (const :tag "Default from remember-data-file" nil)
  1662. file))
  1663. (defcustom org-goto-interface 'outline
  1664. "The default interface to be used for `org-goto'.
  1665. Allowed values are:
  1666. outline The interface shows an outline of the relevant file
  1667. and the correct heading is found by moving through
  1668. the outline or by searching with incremental search.
  1669. outline-path-completion Headlines in the current buffer are offered via
  1670. completion. This is the interface also used by
  1671. the refile command."
  1672. :group 'org-refile
  1673. :type '(choice
  1674. (const :tag "Outline" outline)
  1675. (const :tag "Outline-path-completion" outline-path-completion)))
  1676. (defcustom org-goto-max-level 5
  1677. "Maximum target level when running `org-goto' with refile interface."
  1678. :group 'org-refile
  1679. :type 'integer)
  1680. (defcustom org-reverse-note-order nil
  1681. "Non-nil means store new notes at the beginning of a file or entry.
  1682. When nil, new notes will be filed to the end of a file or entry.
  1683. This can also be a list with cons cells of regular expressions that
  1684. are matched against file names, and values."
  1685. :group 'org-remember
  1686. :group 'org-capture
  1687. :group 'org-refile
  1688. :type '(choice
  1689. (const :tag "Reverse always" t)
  1690. (const :tag "Reverse never" nil)
  1691. (repeat :tag "By file name regexp"
  1692. (cons regexp boolean))))
  1693. (defcustom org-log-refile nil
  1694. "Information to record when a task is refiled.
  1695. Possible values are:
  1696. nil Don't add anything
  1697. time Add a time stamp to the task
  1698. note Prompt for a note and add it with template `org-log-note-headings'
  1699. This option can also be set with on a per-file-basis with
  1700. #+STARTUP: nologrefile
  1701. #+STARTUP: logrefile
  1702. #+STARTUP: lognoterefile
  1703. You can have local logging settings for a subtree by setting the LOGGING
  1704. property to one or more of these keywords.
  1705. When bulk-refiling from the agenda, the value `note' is forbidden and
  1706. will temporarily be changed to `time'."
  1707. :group 'org-refile
  1708. :group 'org-progress
  1709. :version "24.1"
  1710. :type '(choice
  1711. (const :tag "No logging" nil)
  1712. (const :tag "Record timestamp" time)
  1713. (const :tag "Record timestamp with note." note)))
  1714. (defcustom org-refile-targets nil
  1715. "Targets for refiling entries with \\[org-refile].
  1716. This is a list of cons cells. Each cell contains:
  1717. - a specification of the files to be considered, either a list of files,
  1718. or a symbol whose function or variable value will be used to retrieve
  1719. a file name or a list of file names. If you use `org-agenda-files' for
  1720. that, all agenda files will be scanned for targets. Nil means consider
  1721. headings in the current buffer.
  1722. - A specification of how to find candidate refile targets. This may be
  1723. any of:
  1724. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1725. This tag has to be present in all target headlines, inheritance will
  1726. not be considered.
  1727. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1728. todo keyword.
  1729. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1730. headlines that are refiling targets.
  1731. - a cons cell (:level . N). Any headline of level N is considered a target.
  1732. Note that, when `org-odd-levels-only' is set, level corresponds to
  1733. order in hierarchy, not to the number of stars.
  1734. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1735. Note that, when `org-odd-levels-only' is set, level corresponds to
  1736. order in hierarchy, not to the number of stars.
  1737. Each element of this list generates a set of possible targets.
  1738. The union of these sets is presented (with completion) to
  1739. the user by `org-refile'.
  1740. You can set the variable `org-refile-target-verify-function' to a function
  1741. to verify each headline found by the simple criteria above.
  1742. When this variable is nil, all top-level headlines in the current buffer
  1743. are used, equivalent to the value `((nil . (:level . 1))'."
  1744. :group 'org-refile
  1745. :type '(repeat
  1746. (cons
  1747. (choice :value org-agenda-files
  1748. (const :tag "All agenda files" org-agenda-files)
  1749. (const :tag "Current buffer" nil)
  1750. (function) (variable) (file))
  1751. (choice :tag "Identify target headline by"
  1752. (cons :tag "Specific tag" (const :value :tag) (string))
  1753. (cons :tag "TODO keyword" (const :value :todo) (string))
  1754. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1755. (cons :tag "Level number" (const :value :level) (integer))
  1756. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1757. (defcustom org-refile-target-verify-function nil
  1758. "Function to verify if the headline at point should be a refile target.
  1759. The function will be called without arguments, with point at the
  1760. beginning of the headline. It should return t and leave point
  1761. where it is if the headline is a valid target for refiling.
  1762. If the target should not be selected, the function must return nil.
  1763. In addition to this, it may move point to a place from where the search
  1764. should be continued. For example, the function may decide that the entire
  1765. subtree of the current entry should be excluded and move point to the end
  1766. of the subtree."
  1767. :group 'org-refile
  1768. :type 'function)
  1769. (defcustom org-refile-use-cache nil
  1770. "Non-nil means cache refile targets to speed up the process.
  1771. The cache for a particular file will be updated automatically when
  1772. the buffer has been killed, or when any of the marker used for flagging
  1773. refile targets no longer points at a live buffer.
  1774. If you have added new entries to a buffer that might themselves be targets,
  1775. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1776. find that easier, `C-u C-u C-u C-c C-w'."
  1777. :group 'org-refile
  1778. :version "24.1"
  1779. :type 'boolean)
  1780. (defcustom org-refile-use-outline-path nil
  1781. "Non-nil means provide refile targets as paths.
  1782. So a level 3 headline will be available as level1/level2/level3.
  1783. When the value is `file', also include the file name (without directory)
  1784. into the path. In this case, you can also stop the completion after
  1785. the file name, to get entries inserted as top level in the file.
  1786. When `full-file-path', include the full file path."
  1787. :group 'org-refile
  1788. :type '(choice
  1789. (const :tag "Not" nil)
  1790. (const :tag "Yes" t)
  1791. (const :tag "Start with file name" file)
  1792. (const :tag "Start with full file path" full-file-path)))
  1793. (defcustom org-outline-path-complete-in-steps t
  1794. "Non-nil means complete the outline path in hierarchical steps.
  1795. When Org-mode uses the refile interface to select an outline path
  1796. \(see variable `org-refile-use-outline-path'), the completion of
  1797. the path can be done is a single go, or if can be done in steps down
  1798. the headline hierarchy. Going in steps is probably the best if you
  1799. do not use a special completion package like `ido' or `icicles'.
  1800. However, when using these packages, going in one step can be very
  1801. fast, while still showing the whole path to the entry."
  1802. :group 'org-refile
  1803. :type 'boolean)
  1804. (defcustom org-refile-allow-creating-parent-nodes nil
  1805. "Non-nil means allow to create new nodes as refile targets.
  1806. New nodes are then created by adding \"/new node name\" to the completion
  1807. of an existing node. When the value of this variable is `confirm',
  1808. new node creation must be confirmed by the user (recommended)
  1809. When nil, the completion must match an existing entry.
  1810. Note that, if the new heading is not seen by the criteria
  1811. listed in `org-refile-targets', multiple instances of the same
  1812. heading would be created by trying again to file under the new
  1813. heading."
  1814. :group 'org-refile
  1815. :type '(choice
  1816. (const :tag "Never" nil)
  1817. (const :tag "Always" t)
  1818. (const :tag "Prompt for confirmation" confirm)))
  1819. (defcustom org-refile-active-region-within-subtree nil
  1820. "Non-nil means also refile active region within a subtree.
  1821. By default `org-refile' doesn't allow refiling regions if they
  1822. don't contain a set of subtrees, but it might be convenient to
  1823. do so sometimes: in that case, the first line of the region is
  1824. converted to a headline before refiling."
  1825. :group 'org-refile
  1826. :version "24.1"
  1827. :type 'boolean)
  1828. (defgroup org-todo nil
  1829. "Options concerning TODO items in Org-mode."
  1830. :tag "Org TODO"
  1831. :group 'org)
  1832. (defgroup org-progress nil
  1833. "Options concerning Progress logging in Org-mode."
  1834. :tag "Org Progress"
  1835. :group 'org-time)
  1836. (defvar org-todo-interpretation-widgets
  1837. '((:tag "Sequence (cycling hits every state)" sequence)
  1838. (:tag "Type (cycling directly to DONE)" type))
  1839. "The available interpretation symbols for customizing `org-todo-keywords'.
  1840. Interested libraries should add to this list.")
  1841. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1842. "List of TODO entry keyword sequences and their interpretation.
  1843. \\<org-mode-map>This is a list of sequences.
  1844. Each sequence starts with a symbol, either `sequence' or `type',
  1845. indicating if the keywords should be interpreted as a sequence of
  1846. action steps, or as different types of TODO items. The first
  1847. keywords are states requiring action - these states will select a headline
  1848. for inclusion into the global TODO list Org-mode produces. If one of
  1849. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1850. signify that no further action is necessary. If \"|\" is not found,
  1851. the last keyword is treated as the only DONE state of the sequence.
  1852. The command \\[org-todo] cycles an entry through these states, and one
  1853. additional state where no keyword is present. For details about this
  1854. cycling, see the manual.
  1855. TODO keywords and interpretation can also be set on a per-file basis with
  1856. the special #+SEQ_TODO and #+TYP_TODO lines.
  1857. Each keyword can optionally specify a character for fast state selection
  1858. \(in combination with the variable `org-use-fast-todo-selection')
  1859. and specifiers for state change logging, using the same syntax that
  1860. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1861. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1862. indicates to record a time stamp each time this state is selected.
  1863. Each keyword may also specify if a timestamp or a note should be
  1864. recorded when entering or leaving the state, by adding additional
  1865. characters in the parenthesis after the keyword. This looks like this:
  1866. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1867. record only the time of the state change. With X and Y being either
  1868. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1869. Y when leaving the state if and only if the *target* state does not
  1870. define X. You may omit any of the fast-selection key or X or /Y,
  1871. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1872. For backward compatibility, this variable may also be just a list
  1873. of keywords. In this case the interpretation (sequence or type) will be
  1874. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1875. :group 'org-todo
  1876. :group 'org-keywords
  1877. :type '(choice
  1878. (repeat :tag "Old syntax, just keywords"
  1879. (string :tag "Keyword"))
  1880. (repeat :tag "New syntax"
  1881. (cons
  1882. (choice
  1883. :tag "Interpretation"
  1884. ;;Quick and dirty way to see
  1885. ;;`org-todo-interpretations'. This takes the
  1886. ;;place of item arguments
  1887. :convert-widget
  1888. (lambda (widget)
  1889. (widget-put widget
  1890. :args (mapcar
  1891. #'(lambda (x)
  1892. (widget-convert
  1893. (cons 'const x)))
  1894. org-todo-interpretation-widgets))
  1895. widget))
  1896. (repeat
  1897. (string :tag "Keyword"))))))
  1898. (defvar org-todo-keywords-1 nil
  1899. "All TODO and DONE keywords active in a buffer.")
  1900. (make-variable-buffer-local 'org-todo-keywords-1)
  1901. (defvar org-todo-keywords-for-agenda nil)
  1902. (defvar org-done-keywords-for-agenda nil)
  1903. (defvar org-drawers-for-agenda nil)
  1904. (defvar org-todo-keyword-alist-for-agenda nil)
  1905. (defvar org-tag-alist-for-agenda nil)
  1906. (defvar org-agenda-contributing-files nil)
  1907. (defvar org-not-done-keywords nil)
  1908. (make-variable-buffer-local 'org-not-done-keywords)
  1909. (defvar org-done-keywords nil)
  1910. (make-variable-buffer-local 'org-done-keywords)
  1911. (defvar org-todo-heads nil)
  1912. (make-variable-buffer-local 'org-todo-heads)
  1913. (defvar org-todo-sets nil)
  1914. (make-variable-buffer-local 'org-todo-sets)
  1915. (defvar org-todo-log-states nil)
  1916. (make-variable-buffer-local 'org-todo-log-states)
  1917. (defvar org-todo-kwd-alist nil)
  1918. (make-variable-buffer-local 'org-todo-kwd-alist)
  1919. (defvar org-todo-key-alist nil)
  1920. (make-variable-buffer-local 'org-todo-key-alist)
  1921. (defvar org-todo-key-trigger nil)
  1922. (make-variable-buffer-local 'org-todo-key-trigger)
  1923. (defcustom org-todo-interpretation 'sequence
  1924. "Controls how TODO keywords are interpreted.
  1925. This variable is in principle obsolete and is only used for
  1926. backward compatibility, if the interpretation of todo keywords is
  1927. not given already in `org-todo-keywords'. See that variable for
  1928. more information."
  1929. :group 'org-todo
  1930. :group 'org-keywords
  1931. :type '(choice (const sequence)
  1932. (const type)))
  1933. (defcustom org-use-fast-todo-selection t
  1934. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1935. This variable describes if and under what circumstances the cycling
  1936. mechanism for TODO keywords will be replaced by a single-key, direct
  1937. selection scheme.
  1938. When nil, fast selection is never used.
  1939. When the symbol `prefix', it will be used when `org-todo' is called
  1940. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1941. `C-u t' in an agenda buffer.
  1942. When t, fast selection is used by default. In this case, the prefix
  1943. argument forces cycling instead.
  1944. In all cases, the special interface is only used if access keys have
  1945. actually been assigned by the user, i.e. if keywords in the configuration
  1946. are followed by a letter in parenthesis, like TODO(t)."
  1947. :group 'org-todo
  1948. :type '(choice
  1949. (const :tag "Never" nil)
  1950. (const :tag "By default" t)
  1951. (const :tag "Only with C-u C-c C-t" prefix)))
  1952. (defcustom org-provide-todo-statistics t
  1953. "Non-nil means update todo statistics after insert and toggle.
  1954. ALL-HEADLINES means update todo statistics by including headlines
  1955. with no TODO keyword as well, counting them as not done.
  1956. A list of TODO keywords means the same, but skip keywords that are
  1957. not in this list.
  1958. When this is set, todo statistics is updated in the parent of the
  1959. current entry each time a todo state is changed."
  1960. :group 'org-todo
  1961. :type '(choice
  1962. (const :tag "Yes, only for TODO entries" t)
  1963. (const :tag "Yes, including all entries" 'all-headlines)
  1964. (repeat :tag "Yes, for TODOs in this list"
  1965. (string :tag "TODO keyword"))
  1966. (other :tag "No TODO statistics" nil)))
  1967. (defcustom org-hierarchical-todo-statistics t
  1968. "Non-nil means TODO statistics covers just direct children.
  1969. When nil, all entries in the subtree are considered.
  1970. This has only an effect if `org-provide-todo-statistics' is set.
  1971. To set this to nil for only a single subtree, use a COOKIE_DATA
  1972. property and include the word \"recursive\" into the value."
  1973. :group 'org-todo
  1974. :type 'boolean)
  1975. (defcustom org-after-todo-state-change-hook nil
  1976. "Hook which is run after the state of a TODO item was changed.
  1977. The new state (a string with a TODO keyword, or nil) is available in the
  1978. Lisp variable `org-state'."
  1979. :group 'org-todo
  1980. :type 'hook)
  1981. (defvar org-blocker-hook nil
  1982. "Hook for functions that are allowed to block a state change.
  1983. Each function gets as its single argument a property list, see
  1984. `org-trigger-hook' for more information about this list.
  1985. If any of the functions in this hook returns nil, the state change
  1986. is blocked.")
  1987. (defvar org-trigger-hook nil
  1988. "Hook for functions that are triggered by a state change.
  1989. Each function gets as its single argument a property list with at least
  1990. the following elements:
  1991. (:type type-of-change :position pos-at-entry-start
  1992. :from old-state :to new-state)
  1993. Depending on the type, more properties may be present.
  1994. This mechanism is currently implemented for:
  1995. TODO state changes
  1996. ------------------
  1997. :type todo-state-change
  1998. :from previous state (keyword as a string), or nil, or a symbol
  1999. 'todo' or 'done', to indicate the general type of state.
  2000. :to new state, like in :from")
  2001. (defcustom org-enforce-todo-dependencies nil
  2002. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2003. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2004. be blocked if any prior sibling is not yet done.
  2005. Finally, if the parent is blocked because of ordered siblings of its own,
  2006. the child will also be blocked."
  2007. :set (lambda (var val)
  2008. (set var val)
  2009. (if val
  2010. (add-hook 'org-blocker-hook
  2011. 'org-block-todo-from-children-or-siblings-or-parent)
  2012. (remove-hook 'org-blocker-hook
  2013. 'org-block-todo-from-children-or-siblings-or-parent)))
  2014. :group 'org-todo
  2015. :type 'boolean)
  2016. (defcustom org-enforce-todo-checkbox-dependencies nil
  2017. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2018. When this is nil, checkboxes have no influence on switching TODO states.
  2019. When non-nil, you first need to check off all check boxes before the TODO
  2020. entry can be switched to DONE.
  2021. This variable needs to be set before org.el is loaded, and you need to
  2022. restart Emacs after a change to make the change effective. The only way
  2023. to change is while Emacs is running is through the customize interface."
  2024. :set (lambda (var val)
  2025. (set var val)
  2026. (if val
  2027. (add-hook 'org-blocker-hook
  2028. 'org-block-todo-from-checkboxes)
  2029. (remove-hook 'org-blocker-hook
  2030. 'org-block-todo-from-checkboxes)))
  2031. :group 'org-todo
  2032. :type 'boolean)
  2033. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2034. "Non-nil means inserting a TODO heading is treated as state change.
  2035. So when the command \\[org-insert-todo-heading] is used, state change
  2036. logging will apply if appropriate. When nil, the new TODO item will
  2037. be inserted directly, and no logging will take place."
  2038. :group 'org-todo
  2039. :type 'boolean)
  2040. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2041. "Non-nil means switching TODO states with S-cursor counts as state change.
  2042. This is the default behavior. However, setting this to nil allows a
  2043. convenient way to select a TODO state and bypass any logging associated
  2044. with that."
  2045. :group 'org-todo
  2046. :type 'boolean)
  2047. (defcustom org-todo-state-tags-triggers nil
  2048. "Tag changes that should be triggered by TODO state changes.
  2049. This is a list. Each entry is
  2050. (state-change (tag . flag) .......)
  2051. State-change can be a string with a state, and empty string to indicate the
  2052. state that has no TODO keyword, or it can be one of the symbols `todo'
  2053. or `done', meaning any not-done or done state, respectively."
  2054. :group 'org-todo
  2055. :group 'org-tags
  2056. :type '(repeat
  2057. (cons (choice :tag "When changing to"
  2058. (const :tag "Not-done state" todo)
  2059. (const :tag "Done state" done)
  2060. (string :tag "State"))
  2061. (repeat
  2062. (cons :tag "Tag action"
  2063. (string :tag "Tag")
  2064. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2065. (defcustom org-log-done nil
  2066. "Information to record when a task moves to the DONE state.
  2067. Possible values are:
  2068. nil Don't add anything, just change the keyword
  2069. time Add a time stamp to the task
  2070. note Prompt for a note and add it with template `org-log-note-headings'
  2071. This option can also be set with on a per-file-basis with
  2072. #+STARTUP: nologdone
  2073. #+STARTUP: logdone
  2074. #+STARTUP: lognotedone
  2075. You can have local logging settings for a subtree by setting the LOGGING
  2076. property to one or more of these keywords."
  2077. :group 'org-todo
  2078. :group 'org-progress
  2079. :type '(choice
  2080. (const :tag "No logging" nil)
  2081. (const :tag "Record CLOSED timestamp" time)
  2082. (const :tag "Record CLOSED timestamp with note." note)))
  2083. ;; Normalize old uses of org-log-done.
  2084. (cond
  2085. ((eq org-log-done t) (setq org-log-done 'time))
  2086. ((and (listp org-log-done) (memq 'done org-log-done))
  2087. (setq org-log-done 'note)))
  2088. (defcustom org-log-reschedule nil
  2089. "Information to record when the scheduling date of a tasks is modified.
  2090. Possible values are:
  2091. nil Don't add anything, just change the date
  2092. time Add a time stamp to the task
  2093. note Prompt for a note and add it with template `org-log-note-headings'
  2094. This option can also be set with on a per-file-basis with
  2095. #+STARTUP: nologreschedule
  2096. #+STARTUP: logreschedule
  2097. #+STARTUP: lognotereschedule"
  2098. :group 'org-todo
  2099. :group 'org-progress
  2100. :type '(choice
  2101. (const :tag "No logging" nil)
  2102. (const :tag "Record timestamp" time)
  2103. (const :tag "Record timestamp with note." note)))
  2104. (defcustom org-log-redeadline nil
  2105. "Information to record when the deadline date of a tasks is modified.
  2106. Possible values are:
  2107. nil Don't add anything, just change the date
  2108. time Add a time stamp to the task
  2109. note Prompt for a note and add it with template `org-log-note-headings'
  2110. This option can also be set with on a per-file-basis with
  2111. #+STARTUP: nologredeadline
  2112. #+STARTUP: logredeadline
  2113. #+STARTUP: lognoteredeadline
  2114. You can have local logging settings for a subtree by setting the LOGGING
  2115. property to one or more of these keywords."
  2116. :group 'org-todo
  2117. :group 'org-progress
  2118. :type '(choice
  2119. (const :tag "No logging" nil)
  2120. (const :tag "Record timestamp" time)
  2121. (const :tag "Record timestamp with note." note)))
  2122. (defcustom org-log-note-clock-out nil
  2123. "Non-nil means record a note when clocking out of an item.
  2124. This can also be configured on a per-file basis by adding one of
  2125. the following lines anywhere in the buffer:
  2126. #+STARTUP: lognoteclock-out
  2127. #+STARTUP: nolognoteclock-out"
  2128. :group 'org-todo
  2129. :group 'org-progress
  2130. :type 'boolean)
  2131. (defcustom org-log-done-with-time t
  2132. "Non-nil means the CLOSED time stamp will contain date and time.
  2133. When nil, only the date will be recorded."
  2134. :group 'org-progress
  2135. :type 'boolean)
  2136. (defcustom org-log-note-headings
  2137. '((done . "CLOSING NOTE %t")
  2138. (state . "State %-12s from %-12S %t")
  2139. (note . "Note taken on %t")
  2140. (reschedule . "Rescheduled from %S on %t")
  2141. (delschedule . "Not scheduled, was %S on %t")
  2142. (redeadline . "New deadline from %S on %t")
  2143. (deldeadline . "Removed deadline, was %S on %t")
  2144. (refile . "Refiled on %t")
  2145. (clock-out . ""))
  2146. "Headings for notes added to entries.
  2147. The value is an alist, with the car being a symbol indicating the note
  2148. context, and the cdr is the heading to be used. The heading may also be the
  2149. empty string.
  2150. %t in the heading will be replaced by a time stamp.
  2151. %T will be an active time stamp instead the default inactive one
  2152. %d will be replaced by a short-format time stamp.
  2153. %D will be replaced by an active short-format time stamp.
  2154. %s will be replaced by the new TODO state, in double quotes.
  2155. %S will be replaced by the old TODO state, in double quotes.
  2156. %u will be replaced by the user name.
  2157. %U will be replaced by the full user name.
  2158. In fact, it is not a good idea to change the `state' entry, because
  2159. agenda log mode depends on the format of these entries."
  2160. :group 'org-todo
  2161. :group 'org-progress
  2162. :type '(list :greedy t
  2163. (cons (const :tag "Heading when closing an item" done) string)
  2164. (cons (const :tag
  2165. "Heading when changing todo state (todo sequence only)"
  2166. state) string)
  2167. (cons (const :tag "Heading when just taking a note" note) string)
  2168. (cons (const :tag "Heading when clocking out" clock-out) string)
  2169. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2170. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2171. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2172. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2173. (cons (const :tag "Heading when refiling" refile) string)))
  2174. (unless (assq 'note org-log-note-headings)
  2175. (push '(note . "%t") org-log-note-headings))
  2176. (defcustom org-log-into-drawer nil
  2177. "Non-nil means insert state change notes and time stamps into a drawer.
  2178. When nil, state changes notes will be inserted after the headline and
  2179. any scheduling and clock lines, but not inside a drawer.
  2180. The value of this variable should be the name of the drawer to use.
  2181. LOGBOOK is proposed as the default drawer for this purpose, you can
  2182. also set this to a string to define the drawer of your choice.
  2183. A value of t is also allowed, representing \"LOGBOOK\".
  2184. A value of t or nil can also be set with on a per-file-basis with
  2185. #+STARTUP: logdrawer
  2186. #+STARTUP: nologdrawer
  2187. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2188. will be ignored.
  2189. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2190. a subtree."
  2191. :group 'org-todo
  2192. :group 'org-progress
  2193. :type '(choice
  2194. (const :tag "Not into a drawer" nil)
  2195. (const :tag "LOGBOOK" t)
  2196. (string :tag "Other")))
  2197. (if (fboundp 'defvaralias)
  2198. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2199. (defun org-log-into-drawer ()
  2200. "Return the value of `org-log-into-drawer', but let properties overrule.
  2201. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2202. used instead of the default value."
  2203. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2204. (cond
  2205. ((not p) org-log-into-drawer)
  2206. ((equal p "nil") nil)
  2207. ((equal p "t") "LOGBOOK")
  2208. (t p))))
  2209. (defcustom org-log-state-notes-insert-after-drawers nil
  2210. "Non-nil means insert state change notes after any drawers in entry.
  2211. Only the drawers that *immediately* follow the headline and the
  2212. deadline/scheduled line are skipped.
  2213. When nil, insert notes right after the heading and perhaps the line
  2214. with deadline/scheduling if present.
  2215. This variable will have no effect if `org-log-into-drawer' is
  2216. set."
  2217. :group 'org-todo
  2218. :group 'org-progress
  2219. :type 'boolean)
  2220. (defcustom org-log-states-order-reversed t
  2221. "Non-nil means the latest state note will be directly after heading.
  2222. When nil, the state change notes will be ordered according to time.
  2223. This option can also be set with on a per-file-basis with
  2224. #+STARTUP: logstatesreversed
  2225. #+STARTUP: nologstatesreversed"
  2226. :group 'org-todo
  2227. :group 'org-progress
  2228. :type 'boolean)
  2229. (defcustom org-todo-repeat-to-state nil
  2230. "The TODO state to which a repeater should return the repeating task.
  2231. By default this is the first task in a TODO sequence, or the previous state
  2232. in a TODO_TYP set. But you can specify another task here.
  2233. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2234. :group 'org-todo
  2235. :version "24.1"
  2236. :type '(choice (const :tag "Head of sequence" nil)
  2237. (string :tag "Specific state")))
  2238. (defcustom org-log-repeat 'time
  2239. "Non-nil means record moving through the DONE state when triggering repeat.
  2240. An auto-repeating task is immediately switched back to TODO when
  2241. marked DONE. If you are not logging state changes (by adding \"@\"
  2242. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2243. record a closing note, there will be no record of the task moving
  2244. through DONE. This variable forces taking a note anyway.
  2245. nil Don't force a record
  2246. time Record a time stamp
  2247. note Prompt for a note and add it with template `org-log-note-headings'
  2248. This option can also be set with on a per-file-basis with
  2249. #+STARTUP: nologrepeat
  2250. #+STARTUP: logrepeat
  2251. #+STARTUP: lognoterepeat
  2252. You can have local logging settings for a subtree by setting the LOGGING
  2253. property to one or more of these keywords."
  2254. :group 'org-todo
  2255. :group 'org-progress
  2256. :type '(choice
  2257. (const :tag "Don't force a record" nil)
  2258. (const :tag "Force recording the DONE state" time)
  2259. (const :tag "Force recording a note with the DONE state" note)))
  2260. (defgroup org-priorities nil
  2261. "Priorities in Org-mode."
  2262. :tag "Org Priorities"
  2263. :group 'org-todo)
  2264. (defcustom org-enable-priority-commands t
  2265. "Non-nil means priority commands are active.
  2266. When nil, these commands will be disabled, so that you never accidentally
  2267. set a priority."
  2268. :group 'org-priorities
  2269. :type 'boolean)
  2270. (defcustom org-highest-priority ?A
  2271. "The highest priority of TODO items. A character like ?A, ?B etc.
  2272. Must have a smaller ASCII number than `org-lowest-priority'."
  2273. :group 'org-priorities
  2274. :type 'character)
  2275. (defcustom org-lowest-priority ?C
  2276. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2277. Must have a larger ASCII number than `org-highest-priority'."
  2278. :group 'org-priorities
  2279. :type 'character)
  2280. (defcustom org-default-priority ?B
  2281. "The default priority of TODO items.
  2282. This is the priority an item gets if no explicit priority is given.
  2283. When starting to cycle on an empty priority the first step in the cycle
  2284. depends on `org-priority-start-cycle-with-default'. The resulting first
  2285. step priority must not exceed the range from `org-highest-priority' to
  2286. `org-lowest-priority' which means that `org-default-priority' has to be
  2287. in this range exclusive or inclusive the range boundaries. Else the
  2288. first step refuses to set the default and the second will fall back
  2289. to (depending on the command used) the highest or lowest priority."
  2290. :group 'org-priorities
  2291. :type 'character)
  2292. (defcustom org-priority-start-cycle-with-default t
  2293. "Non-nil means start with default priority when starting to cycle.
  2294. When this is nil, the first step in the cycle will be (depending on the
  2295. command used) one higher or lower than the default priority.
  2296. See also `org-default-priority'."
  2297. :group 'org-priorities
  2298. :type 'boolean)
  2299. (defcustom org-get-priority-function nil
  2300. "Function to extract the priority from a string.
  2301. The string is normally the headline. If this is nil Org computes the
  2302. priority from the priority cookie like [#A] in the headline. It returns
  2303. an integer, increasing by 1000 for each priority level.
  2304. The user can set a different function here, which should take a string
  2305. as an argument and return the numeric priority."
  2306. :group 'org-priorities
  2307. :version "24.1"
  2308. :type 'function)
  2309. (defgroup org-time nil
  2310. "Options concerning time stamps and deadlines in Org-mode."
  2311. :tag "Org Time"
  2312. :group 'org)
  2313. (defcustom org-insert-labeled-timestamps-at-point nil
  2314. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2315. When nil, these labeled time stamps are forces into the second line of an
  2316. entry, just after the headline. When scheduling from the global TODO list,
  2317. the time stamp will always be forced into the second line."
  2318. :group 'org-time
  2319. :type 'boolean)
  2320. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2321. "Formats for `format-time-string' which are used for time stamps.
  2322. It is not recommended to change this constant.")
  2323. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2324. "Number of minutes to round time stamps to.
  2325. These are two values, the first applies when first creating a time stamp.
  2326. The second applies when changing it with the commands `S-up' and `S-down'.
  2327. When changing the time stamp, this means that it will change in steps
  2328. of N minutes, as given by the second value.
  2329. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2330. numbers should be factors of 60, so for example 5, 10, 15.
  2331. When this is larger than 1, you can still force an exact time stamp by using
  2332. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2333. and by using a prefix arg to `S-up/down' to specify the exact number
  2334. of minutes to shift."
  2335. :group 'org-time
  2336. :get #'(lambda (var) ; Make sure both elements are there
  2337. (if (integerp (default-value var))
  2338. (list (default-value var) 5)
  2339. (default-value var)))
  2340. :type '(list
  2341. (integer :tag "when inserting times")
  2342. (integer :tag "when modifying times")))
  2343. ;; Normalize old customizations of this variable.
  2344. (when (integerp org-time-stamp-rounding-minutes)
  2345. (setq org-time-stamp-rounding-minutes
  2346. (list org-time-stamp-rounding-minutes
  2347. org-time-stamp-rounding-minutes)))
  2348. (defcustom org-display-custom-times nil
  2349. "Non-nil means overlay custom formats over all time stamps.
  2350. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2351. To turn this on on a per-file basis, insert anywhere in the file:
  2352. #+STARTUP: customtime"
  2353. :group 'org-time
  2354. :set 'set-default
  2355. :type 'sexp)
  2356. (make-variable-buffer-local 'org-display-custom-times)
  2357. (defcustom org-time-stamp-custom-formats
  2358. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2359. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2360. These are overlaid over the default ISO format if the variable
  2361. `org-display-custom-times' is set. Time like %H:%M should be at the
  2362. end of the second format. The custom formats are also honored by export
  2363. commands, if custom time display is turned on at the time of export."
  2364. :group 'org-time
  2365. :type 'sexp)
  2366. (defun org-time-stamp-format (&optional long inactive)
  2367. "Get the right format for a time string."
  2368. (let ((f (if long (cdr org-time-stamp-formats)
  2369. (car org-time-stamp-formats))))
  2370. (if inactive
  2371. (concat "[" (substring f 1 -1) "]")
  2372. f)))
  2373. (defcustom org-time-clocksum-format
  2374. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2375. "The format string used when creating CLOCKSUM lines.
  2376. This is also used when Org mode generates a time duration.
  2377. The value can be a single format string containing two
  2378. %-sequences, which will be filled with the number of hours and
  2379. minutes in that order.
  2380. Alternatively, the value can be a plist associating any of the
  2381. keys :years, :months, :weeks, :days, :hours or :minutes with
  2382. format strings. The time duration is formatted using only the
  2383. time components that are needed and concatenating the results.
  2384. If a time unit in absent, it falls back to the next smallest
  2385. unit.
  2386. The keys :require-years, :require-months, :require-days,
  2387. :require-weeks, :require-hours, :require-minutes are also
  2388. meaningful. A non-nil value for these keys indicates that the
  2389. corresponding time component should always be included, even if
  2390. its value is 0.
  2391. For example,
  2392. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2393. :require-minutes t)
  2394. means durations longer than a day will be expressed in days,
  2395. hours and minutes, and durations less than a day will always be
  2396. expressed in hours and minutes (even for durations less than an
  2397. hour).
  2398. The value
  2399. \(:days \"%dd\" :minutes \"%dm\")
  2400. means durations longer than a day will be expressed in days and
  2401. minutes, and durations less than a day will be expressed entirely
  2402. in minutes (even for durations longer than an hour)."
  2403. :group 'org-time
  2404. :type '(choice (string :tag "Format string")
  2405. (set :tag "Plist"
  2406. (group :inline t (const :tag "Years" :years)
  2407. (string :tag "Format string"))
  2408. (group :inline t
  2409. (const :tag "Always show years" :require-years)
  2410. (const t))
  2411. (group :inline t (const :tag "Months" :months)
  2412. (string :tag "Format string"))
  2413. (group :inline t
  2414. (const :tag "Always show months" :require-months)
  2415. (const t))
  2416. (group :inline t (const :tag "Weeks" :weeks)
  2417. (string :tag "Format string"))
  2418. (group :inline t
  2419. (const :tag "Always show weeks" :require-weeks)
  2420. (const t))
  2421. (group :inline t (const :tag "Days" :days)
  2422. (string :tag "Format string"))
  2423. (group :inline t
  2424. (const :tag "Always show days" :require-days)
  2425. (const t))
  2426. (group :inline t (const :tag "Hours" :hours)
  2427. (string :tag "Format string"))
  2428. (group :inline t
  2429. (const :tag "Always show hours" :require-hours)
  2430. (const t))
  2431. (group :inline t (const :tag "Minutes" :minutes)
  2432. (string :tag "Format string"))
  2433. (group :inline t
  2434. (const :tag "Always show minutes" :require-minutes)
  2435. (const t)))))
  2436. (defcustom org-time-clocksum-use-fractional nil
  2437. "If non-nil, \\[org-clock-display] uses fractional times.
  2438. org-mode generates a time duration."
  2439. :group 'org-time
  2440. :type 'boolean)
  2441. (defcustom org-time-clocksum-fractional-format "%.2f"
  2442. "The format string used when creating CLOCKSUM lines,
  2443. or when Org mode generates a time duration, if
  2444. `org-time-clocksum-use-fractional' is enabled.
  2445. The value can be a single format string containing one
  2446. %-sequence, which will be filled with the number of hours as
  2447. a float.
  2448. Alternatively, the value can be a plist associating any of the
  2449. keys :years, :months, :weeks, :days, :hours or :minutes with
  2450. a format string. The time duration is formatted using the
  2451. largest time unit which gives a non-zero integer part. If all
  2452. specified formats have zero integer part, the smallest time unit
  2453. is used."
  2454. :group 'org-time
  2455. :type '(choice (string :tag "Format string")
  2456. (set (group :inline t (const :tag "Years" :years)
  2457. (string :tag "Format string"))
  2458. (group :inline t (const :tag "Months" :months)
  2459. (string :tag "Format string"))
  2460. (group :inline t (const :tag "Weeks" :weeks)
  2461. (string :tag "Format string"))
  2462. (group :inline t (const :tag "Days" :days)
  2463. (string :tag "Format string"))
  2464. (group :inline t (const :tag "Hours" :hours)
  2465. (string :tag "Format string"))
  2466. (group :inline t (const :tag "Minutes" :minutes)
  2467. (string :tag "Format string")))))
  2468. (defcustom org-deadline-warning-days 14
  2469. "No. of days before expiration during which a deadline becomes active.
  2470. This variable governs the display in sparse trees and in the agenda.
  2471. When 0 or negative, it means use this number (the absolute value of it)
  2472. even if a deadline has a different individual lead time specified.
  2473. Custom commands can set this variable in the options section."
  2474. :group 'org-time
  2475. :group 'org-agenda-daily/weekly
  2476. :type 'integer)
  2477. (defcustom org-read-date-prefer-future t
  2478. "Non-nil means assume future for incomplete date input from user.
  2479. This affects the following situations:
  2480. 1. The user gives a month but not a year.
  2481. For example, if it is April and you enter \"feb 2\", this will be read
  2482. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2483. 2. The user gives a day, but no month.
  2484. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2485. read this as the third of *next* month. However, if you enter \"17\",
  2486. it will be considered as *this* month.
  2487. If you set this variable to the symbol `time', then also the following
  2488. will work:
  2489. 3. If the user gives a time.
  2490. If the time is before now, it will be interpreted as tomorrow.
  2491. Currently none of this works for ISO week specifications.
  2492. When this option is nil, the current day, month and year will always be
  2493. used as defaults.
  2494. See also `org-agenda-jump-prefer-future'."
  2495. :group 'org-time
  2496. :type '(choice
  2497. (const :tag "Never" nil)
  2498. (const :tag "Check month and day" t)
  2499. (const :tag "Check month, day, and time" time)))
  2500. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2501. "Should the agenda jump command prefer the future for incomplete dates?
  2502. The default is to do the same as configured in `org-read-date-prefer-future'.
  2503. But you can also set a deviating value here.
  2504. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2505. :group 'org-agenda
  2506. :group 'org-time
  2507. :version "24.1"
  2508. :type '(choice
  2509. (const :tag "Use org-read-date-prefer-future"
  2510. org-read-date-prefer-future)
  2511. (const :tag "Never" nil)
  2512. (const :tag "Always" t)))
  2513. (defcustom org-read-date-force-compatible-dates t
  2514. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2515. Depending on the system Emacs is running on, certain dates cannot
  2516. be represented with the type used internally to represent time.
  2517. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2518. correctly. Some systems allow for earlier dates, some for later,
  2519. some for both. One way to find out it to insert any date into an
  2520. Org buffer, putting the cursor on the year and hitting S-up and
  2521. S-down to test the range.
  2522. When this variable is set to t, the date/time prompt will not let
  2523. you specify dates outside the 1970-2037 range, so it is certain that
  2524. these dates will work in whatever version of Emacs you are
  2525. running, and also that you can move a file from one Emacs implementation
  2526. to another. WHenever Org is forcing the year for you, it will display
  2527. a message and beep.
  2528. When this variable is nil, Org will check if the date is
  2529. representable in the specific Emacs implementation you are using.
  2530. If not, it will force a year, usually the current year, and beep
  2531. to remind you. Currently this setting is not recommended because
  2532. the likelihood that you will open your Org files in an Emacs that
  2533. has limited date range is not negligible.
  2534. A workaround for this problem is to use diary sexp dates for time
  2535. stamps outside of this range."
  2536. :group 'org-time
  2537. :version "24.1"
  2538. :type 'boolean)
  2539. (defcustom org-read-date-display-live t
  2540. "Non-nil means display current interpretation of date prompt live.
  2541. This display will be in an overlay, in the minibuffer."
  2542. :group 'org-time
  2543. :type 'boolean)
  2544. (defcustom org-read-date-popup-calendar t
  2545. "Non-nil means pop up a calendar when prompting for a date.
  2546. In the calendar, the date can be selected with mouse-1. However, the
  2547. minibuffer will also be active, and you can simply enter the date as well.
  2548. When nil, only the minibuffer will be available."
  2549. :group 'org-time
  2550. :type 'boolean)
  2551. (if (fboundp 'defvaralias)
  2552. (defvaralias 'org-popup-calendar-for-date-prompt
  2553. 'org-read-date-popup-calendar))
  2554. (defcustom org-read-date-minibuffer-setup-hook nil
  2555. "Hook to be used to set up keys for the date/time interface.
  2556. Add key definitions to `minibuffer-local-map', which will be a temporary
  2557. copy."
  2558. :group 'org-time
  2559. :type 'hook)
  2560. (defcustom org-extend-today-until 0
  2561. "The hour when your day really ends. Must be an integer.
  2562. This has influence for the following applications:
  2563. - When switching the agenda to \"today\". It it is still earlier than
  2564. the time given here, the day recognized as TODAY is actually yesterday.
  2565. - When a date is read from the user and it is still before the time given
  2566. here, the current date and time will be assumed to be yesterday, 23:59.
  2567. Also, timestamps inserted in capture templates follow this rule.
  2568. IMPORTANT: This is a feature whose implementation is and likely will
  2569. remain incomplete. Really, it is only here because past midnight seems to
  2570. be the favorite working time of John Wiegley :-)"
  2571. :group 'org-time
  2572. :type 'integer)
  2573. (defcustom org-use-effective-time nil
  2574. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2575. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2576. \"effective time\" of any timestamps between midnight and 8am will be
  2577. 23:59 of the previous day."
  2578. :group 'org-time
  2579. :version "24.1"
  2580. :type 'boolean)
  2581. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2582. "When non-nil, use the last clock out time for `org-todo'.
  2583. Note that this option has precedence over the combined use of
  2584. `org-use-effective-time' and `org-extend-today-until'."
  2585. :group 'org-time
  2586. ;; :version "24.3"
  2587. :type 'boolean)
  2588. (defcustom org-edit-timestamp-down-means-later nil
  2589. "Non-nil means S-down will increase the time in a time stamp.
  2590. When nil, S-up will increase."
  2591. :group 'org-time
  2592. :type 'boolean)
  2593. (defcustom org-calendar-follow-timestamp-change t
  2594. "Non-nil means make the calendar window follow timestamp changes.
  2595. When a timestamp is modified and the calendar window is visible, it will be
  2596. moved to the new date."
  2597. :group 'org-time
  2598. :type 'boolean)
  2599. (defgroup org-tags nil
  2600. "Options concerning tags in Org-mode."
  2601. :tag "Org Tags"
  2602. :group 'org)
  2603. (defcustom org-tag-alist nil
  2604. "List of tags allowed in Org-mode files.
  2605. When this list is nil, Org-mode will base TAG input on what is already in the
  2606. buffer.
  2607. The value of this variable is an alist, the car of each entry must be a
  2608. keyword as a string, the cdr may be a character that is used to select
  2609. that tag through the fast-tag-selection interface.
  2610. See the manual for details."
  2611. :group 'org-tags
  2612. :type '(repeat
  2613. (choice
  2614. (cons (string :tag "Tag name")
  2615. (character :tag "Access char"))
  2616. (list :tag "Start radio group"
  2617. (const :startgroup)
  2618. (option (string :tag "Group description")))
  2619. (list :tag "End radio group"
  2620. (const :endgroup)
  2621. (option (string :tag "Group description")))
  2622. (const :tag "New line" (:newline)))))
  2623. (defcustom org-tag-persistent-alist nil
  2624. "List of tags that will always appear in all Org-mode files.
  2625. This is in addition to any in buffer settings or customizations
  2626. of `org-tag-alist'.
  2627. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2628. The value of this variable is an alist, the car of each entry must be a
  2629. keyword as a string, the cdr may be a character that is used to select
  2630. that tag through the fast-tag-selection interface.
  2631. See the manual for details.
  2632. To disable these tags on a per-file basis, insert anywhere in the file:
  2633. #+STARTUP: noptag"
  2634. :group 'org-tags
  2635. :type '(repeat
  2636. (choice
  2637. (cons (string :tag "Tag name")
  2638. (character :tag "Access char"))
  2639. (const :tag "Start radio group" (:startgroup))
  2640. (const :tag "End radio group" (:endgroup))
  2641. (const :tag "New line" (:newline)))))
  2642. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2643. "If non-nil, always offer completion for all tags of all agenda files.
  2644. Instead of customizing this variable directly, you might want to
  2645. set it locally for capture buffers, because there no list of
  2646. tags in that file can be created dynamically (there are none).
  2647. (add-hook 'org-capture-mode-hook
  2648. (lambda ()
  2649. (set (make-local-variable
  2650. 'org-complete-tags-always-offer-all-agenda-tags)
  2651. t)))"
  2652. :group 'org-tags
  2653. :version "24.1"
  2654. :type 'boolean)
  2655. (defvar org-file-tags nil
  2656. "List of tags that can be inherited by all entries in the file.
  2657. The tags will be inherited if the variable `org-use-tag-inheritance'
  2658. says they should be.
  2659. This variable is populated from #+FILETAGS lines.")
  2660. (defcustom org-use-fast-tag-selection 'auto
  2661. "Non-nil means use fast tag selection scheme.
  2662. This is a special interface to select and deselect tags with single keys.
  2663. When nil, fast selection is never used.
  2664. When the symbol `auto', fast selection is used if and only if selection
  2665. characters for tags have been configured, either through the variable
  2666. `org-tag-alist' or through a #+TAGS line in the buffer.
  2667. When t, fast selection is always used and selection keys are assigned
  2668. automatically if necessary."
  2669. :group 'org-tags
  2670. :type '(choice
  2671. (const :tag "Always" t)
  2672. (const :tag "Never" nil)
  2673. (const :tag "When selection characters are configured" 'auto)))
  2674. (defcustom org-fast-tag-selection-single-key nil
  2675. "Non-nil means fast tag selection exits after first change.
  2676. When nil, you have to press RET to exit it.
  2677. During fast tag selection, you can toggle this flag with `C-c'.
  2678. This variable can also have the value `expert'. In this case, the window
  2679. displaying the tags menu is not even shown, until you press C-c again."
  2680. :group 'org-tags
  2681. :type '(choice
  2682. (const :tag "No" nil)
  2683. (const :tag "Yes" t)
  2684. (const :tag "Expert" expert)))
  2685. (defvar org-fast-tag-selection-include-todo nil
  2686. "Non-nil means fast tags selection interface will also offer TODO states.
  2687. This is an undocumented feature, you should not rely on it.")
  2688. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2689. "The column to which tags should be indented in a headline.
  2690. If this number is positive, it specifies the column. If it is negative,
  2691. it means that the tags should be flushright to that column. For example,
  2692. -80 works well for a normal 80 character screen.
  2693. When 0, place tags directly after headline text, with only one space in
  2694. between."
  2695. :group 'org-tags
  2696. :type 'integer)
  2697. (defcustom org-auto-align-tags t
  2698. "Non-nil keeps tags aligned when modifying headlines.
  2699. Some operations (i.e. demoting) change the length of a headline and
  2700. therefore shift the tags around. With this option turned on, after
  2701. each such operation the tags are again aligned to `org-tags-column'."
  2702. :group 'org-tags
  2703. :type 'boolean)
  2704. (defcustom org-use-tag-inheritance t
  2705. "Non-nil means tags in levels apply also for sublevels.
  2706. When nil, only the tags directly given in a specific line apply there.
  2707. This may also be a list of tags that should be inherited, or a regexp that
  2708. matches tags that should be inherited. Additional control is possible
  2709. with the variable `org-tags-exclude-from-inheritance' which gives an
  2710. explicit list of tags to be excluded from inheritance., even if the value of
  2711. `org-use-tag-inheritance' would select it for inheritance.
  2712. If this option is t, a match early-on in a tree can lead to a large
  2713. number of matches in the subtree when constructing the agenda or creating
  2714. a sparse tree. If you only want to see the first match in a tree during
  2715. a search, check out the variable `org-tags-match-list-sublevels'."
  2716. :group 'org-tags
  2717. :type '(choice
  2718. (const :tag "Not" nil)
  2719. (const :tag "Always" t)
  2720. (repeat :tag "Specific tags" (string :tag "Tag"))
  2721. (regexp :tag "Tags matched by regexp")))
  2722. (defcustom org-tags-exclude-from-inheritance nil
  2723. "List of tags that should never be inherited.
  2724. This is a way to exclude a few tags from inheritance. For way to do
  2725. the opposite, to actively allow inheritance for selected tags,
  2726. see the variable `org-use-tag-inheritance'."
  2727. :group 'org-tags
  2728. :type '(repeat (string :tag "Tag")))
  2729. (defun org-tag-inherit-p (tag)
  2730. "Check if TAG is one that should be inherited."
  2731. (cond
  2732. ((member tag org-tags-exclude-from-inheritance) nil)
  2733. ((eq org-use-tag-inheritance t) t)
  2734. ((not org-use-tag-inheritance) nil)
  2735. ((stringp org-use-tag-inheritance)
  2736. (string-match org-use-tag-inheritance tag))
  2737. ((listp org-use-tag-inheritance)
  2738. (member tag org-use-tag-inheritance))
  2739. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2740. (defcustom org-tags-match-list-sublevels t
  2741. "Non-nil means list also sublevels of headlines matching a search.
  2742. This variable applies to tags/property searches, and also to stuck
  2743. projects because this search is based on a tags match as well.
  2744. When set to the symbol `indented', sublevels are indented with
  2745. leading dots.
  2746. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2747. the sublevels of a headline matching a tag search often also match
  2748. the same search. Listing all of them can create very long lists.
  2749. Setting this variable to nil causes subtrees of a match to be skipped.
  2750. This variable is semi-obsolete and probably should always be true. It
  2751. is better to limit inheritance to certain tags using the variables
  2752. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2753. :group 'org-tags
  2754. :type '(choice
  2755. (const :tag "No, don't list them" nil)
  2756. (const :tag "Yes, do list them" t)
  2757. (const :tag "List them, indented with leading dots" indented)))
  2758. (defcustom org-tags-sort-function nil
  2759. "When set, tags are sorted using this function as a comparator."
  2760. :group 'org-tags
  2761. :type '(choice
  2762. (const :tag "No sorting" nil)
  2763. (const :tag "Alphabetical" string<)
  2764. (const :tag "Reverse alphabetical" string>)
  2765. (function :tag "Custom function" nil)))
  2766. (defvar org-tags-history nil
  2767. "History of minibuffer reads for tags.")
  2768. (defvar org-last-tags-completion-table nil
  2769. "The last used completion table for tags.")
  2770. (defvar org-after-tags-change-hook nil
  2771. "Hook that is run after the tags in a line have changed.")
  2772. (defgroup org-properties nil
  2773. "Options concerning properties in Org-mode."
  2774. :tag "Org Properties"
  2775. :group 'org)
  2776. (defcustom org-property-format "%-10s %s"
  2777. "How property key/value pairs should be formatted by `indent-line'.
  2778. When `indent-line' hits a property definition, it will format the line
  2779. according to this format, mainly to make sure that the values are
  2780. lined-up with respect to each other."
  2781. :group 'org-properties
  2782. :type 'string)
  2783. (defcustom org-properties-postprocess-alist nil
  2784. "Alist of properties and functions to adjust inserted values.
  2785. Elements of this alist must be of the form
  2786. ([string] [function])
  2787. where [string] must be a property name and [function] must be a
  2788. lambda expression: this lambda expression must take one argument,
  2789. the value to adjust, and return the new value as a string.
  2790. For example, this element will allow the property \"Remaining\"
  2791. to be updated wrt the relation between the \"Effort\" property
  2792. and the clock summary:
  2793. ((\"Remaining\" (lambda(value)
  2794. (let ((clocksum (org-clock-sum-current-item))
  2795. (effort (org-duration-string-to-minutes
  2796. (org-entry-get (point) \"Effort\"))))
  2797. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2798. :group 'org-properties
  2799. :version "24.1"
  2800. :type '(alist :key-type (string :tag "Property")
  2801. :value-type (function :tag "Function")))
  2802. (defcustom org-use-property-inheritance nil
  2803. "Non-nil means properties apply also for sublevels.
  2804. This setting is chiefly used during property searches. Turning it on can
  2805. cause significant overhead when doing a search, which is why it is not
  2806. on by default.
  2807. When nil, only the properties directly given in the current entry count.
  2808. When t, every property is inherited. The value may also be a list of
  2809. properties that should have inheritance, or a regular expression matching
  2810. properties that should be inherited.
  2811. However, note that some special properties use inheritance under special
  2812. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2813. and the properties ending in \"_ALL\" when they are used as descriptor
  2814. for valid values of a property.
  2815. Note for programmers:
  2816. When querying an entry with `org-entry-get', you can control if inheritance
  2817. should be used. By default, `org-entry-get' looks only at the local
  2818. properties. You can request inheritance by setting the inherit argument
  2819. to t (to force inheritance) or to `selective' (to respect the setting
  2820. in this variable)."
  2821. :group 'org-properties
  2822. :type '(choice
  2823. (const :tag "Not" nil)
  2824. (const :tag "Always" t)
  2825. (repeat :tag "Specific properties" (string :tag "Property"))
  2826. (regexp :tag "Properties matched by regexp")))
  2827. (defun org-property-inherit-p (property)
  2828. "Check if PROPERTY is one that should be inherited."
  2829. (cond
  2830. ((eq org-use-property-inheritance t) t)
  2831. ((not org-use-property-inheritance) nil)
  2832. ((stringp org-use-property-inheritance)
  2833. (string-match org-use-property-inheritance property))
  2834. ((listp org-use-property-inheritance)
  2835. (member property org-use-property-inheritance))
  2836. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2837. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2838. "The default column format, if no other format has been defined.
  2839. This variable can be set on the per-file basis by inserting a line
  2840. #+COLUMNS: %25ITEM ....."
  2841. :group 'org-properties
  2842. :type 'string)
  2843. (defcustom org-columns-ellipses ".."
  2844. "The ellipses to be used when a field in column view is truncated.
  2845. When this is the empty string, as many characters as possible are shown,
  2846. but then there will be no visual indication that the field has been truncated.
  2847. When this is a string of length N, the last N characters of a truncated
  2848. field are replaced by this string. If the column is narrower than the
  2849. ellipses string, only part of the ellipses string will be shown."
  2850. :group 'org-properties
  2851. :type 'string)
  2852. (defcustom org-columns-modify-value-for-display-function nil
  2853. "Function that modifies values for display in column view.
  2854. For example, it can be used to cut out a certain part from a time stamp.
  2855. The function must take 2 arguments:
  2856. column-title The title of the column (*not* the property name)
  2857. value The value that should be modified.
  2858. The function should return the value that should be displayed,
  2859. or nil if the normal value should be used."
  2860. :group 'org-properties
  2861. :type 'function)
  2862. (defcustom org-effort-property "Effort"
  2863. "The property that is being used to keep track of effort estimates.
  2864. Effort estimates given in this property need to have the format H:MM."
  2865. :group 'org-properties
  2866. :group 'org-progress
  2867. :type '(string :tag "Property"))
  2868. (defconst org-global-properties-fixed
  2869. '(("VISIBILITY_ALL" . "folded children content all")
  2870. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2871. "List of property/value pairs that can be inherited by any entry.
  2872. These are fixed values, for the preset properties. The user variable
  2873. that can be used to add to this list is `org-global-properties'.
  2874. The entries in this list are cons cells where the car is a property
  2875. name and cdr is a string with the value. If the value represents
  2876. multiple items like an \"_ALL\" property, separate the items by
  2877. spaces.")
  2878. (defcustom org-global-properties nil
  2879. "List of property/value pairs that can be inherited by any entry.
  2880. This list will be combined with the constant `org-global-properties-fixed'.
  2881. The entries in this list are cons cells where the car is a property
  2882. name and cdr is a string with the value.
  2883. You can set buffer-local values for the same purpose in the variable
  2884. `org-file-properties' this by adding lines like
  2885. #+PROPERTY: NAME VALUE"
  2886. :group 'org-properties
  2887. :type '(repeat
  2888. (cons (string :tag "Property")
  2889. (string :tag "Value"))))
  2890. (defvar org-file-properties nil
  2891. "List of property/value pairs that can be inherited by any entry.
  2892. Valid for the current buffer.
  2893. This variable is populated from #+PROPERTY lines.")
  2894. (make-variable-buffer-local 'org-file-properties)
  2895. (defgroup org-agenda nil
  2896. "Options concerning agenda views in Org-mode."
  2897. :tag "Org Agenda"
  2898. :group 'org)
  2899. (defvar org-category nil
  2900. "Variable used by org files to set a category for agenda display.
  2901. Such files should use a file variable to set it, for example
  2902. # -*- mode: org; org-category: \"ELisp\"
  2903. or contain a special line
  2904. #+CATEGORY: ELisp
  2905. If the file does not specify a category, then file's base name
  2906. is used instead.")
  2907. (make-variable-buffer-local 'org-category)
  2908. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2909. (defcustom org-agenda-files nil
  2910. "The files to be used for agenda display.
  2911. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2912. \\[org-remove-file]. You can also use customize to edit the list.
  2913. If an entry is a directory, all files in that directory that are matched by
  2914. `org-agenda-file-regexp' will be part of the file list.
  2915. If the value of the variable is not a list but a single file name, then
  2916. the list of agenda files is actually stored and maintained in that file, one
  2917. agenda file per line. In this file paths can be given relative to
  2918. `org-directory'. Tilde expansion and environment variable substitution
  2919. are also made."
  2920. :group 'org-agenda
  2921. :type '(choice
  2922. (repeat :tag "List of files and directories" file)
  2923. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2924. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2925. "Regular expression to match files for `org-agenda-files'.
  2926. If any element in the list in that variable contains a directory instead
  2927. of a normal file, all files in that directory that are matched by this
  2928. regular expression will be included."
  2929. :group 'org-agenda
  2930. :type 'regexp)
  2931. (defcustom org-agenda-text-search-extra-files nil
  2932. "List of extra files to be searched by text search commands.
  2933. These files will be search in addition to the agenda files by the
  2934. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2935. Note that these files will only be searched for text search commands,
  2936. not for the other agenda views like todo lists, tag searches or the weekly
  2937. agenda. This variable is intended to list notes and possibly archive files
  2938. that should also be searched by these two commands.
  2939. In fact, if the first element in the list is the symbol `agenda-archives',
  2940. than all archive files of all agenda files will be added to the search
  2941. scope."
  2942. :group 'org-agenda
  2943. :type '(set :greedy t
  2944. (const :tag "Agenda Archives" agenda-archives)
  2945. (repeat :inline t (file))))
  2946. (if (fboundp 'defvaralias)
  2947. (defvaralias 'org-agenda-multi-occur-extra-files
  2948. 'org-agenda-text-search-extra-files))
  2949. (defcustom org-agenda-skip-unavailable-files nil
  2950. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2951. A nil value means to remove them, after a query, from the list."
  2952. :group 'org-agenda
  2953. :type 'boolean)
  2954. (defcustom org-calendar-to-agenda-key [?c]
  2955. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2956. The command `org-calendar-goto-agenda' will be bound to this key. The
  2957. default is the character `c' because then `c' can be used to switch back and
  2958. forth between agenda and calendar."
  2959. :group 'org-agenda
  2960. :type 'sexp)
  2961. (defcustom org-calendar-insert-diary-entry-key [?i]
  2962. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2963. This option is irrelevant until `org-agenda-diary-file' has been configured
  2964. to point to an Org-mode file. When that is the case, the command
  2965. `org-agenda-diary-entry' will be bound to the key given here, by default
  2966. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2967. if you want to continue doing this, you need to change this to a different
  2968. key."
  2969. :group 'org-agenda
  2970. :type 'sexp)
  2971. (defcustom org-agenda-diary-file 'diary-file
  2972. "File to which to add new entries with the `i' key in agenda and calendar.
  2973. When this is the symbol `diary-file', the functionality in the Emacs
  2974. calendar will be used to add entries to the `diary-file'. But when this
  2975. points to a file, `org-agenda-diary-entry' will be used instead."
  2976. :group 'org-agenda
  2977. :type '(choice
  2978. (const :tag "The standard Emacs diary file" diary-file)
  2979. (file :tag "Special Org file diary entries")))
  2980. (eval-after-load "calendar"
  2981. '(progn
  2982. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2983. 'org-calendar-goto-agenda)
  2984. (add-hook 'calendar-mode-hook
  2985. (lambda ()
  2986. (unless (eq org-agenda-diary-file 'diary-file)
  2987. (define-key calendar-mode-map
  2988. org-calendar-insert-diary-entry-key
  2989. 'org-agenda-diary-entry))))))
  2990. (defgroup org-latex nil
  2991. "Options for embedding LaTeX code into Org-mode."
  2992. :tag "Org LaTeX"
  2993. :group 'org)
  2994. (defcustom org-format-latex-options
  2995. '(:foreground default :background default :scale 1.0
  2996. :html-foreground "Black" :html-background "Transparent"
  2997. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2998. "Options for creating images from LaTeX fragments.
  2999. This is a property list with the following properties:
  3000. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3001. `default' means use the foreground of the default face.
  3002. `auto' means use the foreground from the text face.
  3003. :background the background color, or \"Transparent\".
  3004. `default' means use the background of the default face.
  3005. `auto' means use the background from the text face.
  3006. :scale a scaling factor for the size of the images, to get more pixels
  3007. :html-foreground, :html-background, :html-scale
  3008. the same numbers for HTML export.
  3009. :matchers a list indicating which matchers should be used to
  3010. find LaTeX fragments. Valid members of this list are:
  3011. \"begin\" find environments
  3012. \"$1\" find single characters surrounded by $.$
  3013. \"$\" find math expressions surrounded by $...$
  3014. \"$$\" find math expressions surrounded by $$....$$
  3015. \"\\(\" find math expressions surrounded by \\(...\\)
  3016. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3017. :group 'org-latex
  3018. :type 'plist)
  3019. (defcustom org-format-latex-signal-error t
  3020. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3021. When nil, just push out a message."
  3022. :group 'org-latex
  3023. :version "24.1"
  3024. :type 'boolean)
  3025. (defcustom org-latex-to-mathml-jar-file nil
  3026. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3027. Use this to specify additional executable file say a jar file.
  3028. When using MathToWeb as the converter, specify the full-path to
  3029. your mathtoweb.jar file."
  3030. :group 'org-latex
  3031. :version "24.1"
  3032. :type '(choice
  3033. (const :tag "None" nil)
  3034. (file :tag "JAR file" :must-match t)))
  3035. (defcustom org-latex-to-mathml-convert-command nil
  3036. "Command to convert LaTeX fragments to MathML.
  3037. Replace format-specifiers in the command as noted below and use
  3038. `shell-command' to convert LaTeX to MathML.
  3039. %j: Executable file in fully expanded form as specified by
  3040. `org-latex-to-mathml-jar-file'.
  3041. %I: Input LaTeX file in fully expanded form
  3042. %o: Output MathML file
  3043. This command is used by `org-create-math-formula'.
  3044. When using MathToWeb as the converter, set this to
  3045. \"java -jar %j -unicode -force -df %o %I\"."
  3046. :group 'org-latex
  3047. :version "24.1"
  3048. :type '(choice
  3049. (const :tag "None" nil)
  3050. (string :tag "\nShell command")))
  3051. (defcustom org-latex-create-formula-image-program 'dvipng
  3052. "Program to convert LaTeX fragments with.
  3053. dvipng Process the LaTeX fragments to dvi file, then convert
  3054. dvi files to png files using dvipng.
  3055. This will also include processing of non-math environments.
  3056. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3057. to convert pdf files to png files"
  3058. :group 'org-latex
  3059. :version "24.1"
  3060. :type '(choice
  3061. (const :tag "dvipng" dvipng)
  3062. (const :tag "imagemagick" imagemagick)))
  3063. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3064. "Path to store latex preview images.
  3065. A relative path here creates many directories relative to the
  3066. processed org files paths. An absolute path puts all preview
  3067. images at the same place."
  3068. :group 'org-latex
  3069. :version "24.3"
  3070. :type 'string)
  3071. (defun org-format-latex-mathml-available-p ()
  3072. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3073. (save-match-data
  3074. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3075. org-latex-to-mathml-convert-command)
  3076. (let ((executable (car (split-string
  3077. org-latex-to-mathml-convert-command))))
  3078. (when (executable-find executable)
  3079. (if (string-match
  3080. "%j" org-latex-to-mathml-convert-command)
  3081. (file-readable-p org-latex-to-mathml-jar-file)
  3082. t))))))
  3083. (defcustom org-format-latex-header "\\documentclass{article}
  3084. \\usepackage[usenames]{color}
  3085. \\usepackage{amsmath}
  3086. \\usepackage[mathscr]{eucal}
  3087. \\pagestyle{empty} % do not remove
  3088. \[PACKAGES]
  3089. \[DEFAULT-PACKAGES]
  3090. % The settings below are copied from fullpage.sty
  3091. \\setlength{\\textwidth}{\\paperwidth}
  3092. \\addtolength{\\textwidth}{-3cm}
  3093. \\setlength{\\oddsidemargin}{1.5cm}
  3094. \\addtolength{\\oddsidemargin}{-2.54cm}
  3095. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3096. \\setlength{\\textheight}{\\paperheight}
  3097. \\addtolength{\\textheight}{-\\headheight}
  3098. \\addtolength{\\textheight}{-\\headsep}
  3099. \\addtolength{\\textheight}{-\\footskip}
  3100. \\addtolength{\\textheight}{-3cm}
  3101. \\setlength{\\topmargin}{1.5cm}
  3102. \\addtolength{\\topmargin}{-2.54cm}"
  3103. "The document header used for processing LaTeX fragments.
  3104. It is imperative that this header make sure that no page number
  3105. appears on the page. The package defined in the variables
  3106. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3107. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3108. will be appended."
  3109. :group 'org-latex
  3110. :type 'string)
  3111. (defvar org-format-latex-header-extra nil)
  3112. (defun org-set-packages-alist (var val)
  3113. "Set the packages alist and make sure it has 3 elements per entry."
  3114. (set var (mapcar (lambda (x)
  3115. (if (and (consp x) (= (length x) 2))
  3116. (list (car x) (nth 1 x) t)
  3117. x))
  3118. val)))
  3119. (defun org-get-packages-alist (var)
  3120. "Get the packages alist and make sure it has 3 elements per entry."
  3121. (mapcar (lambda (x)
  3122. (if (and (consp x) (= (length x) 2))
  3123. (list (car x) (nth 1 x) t)
  3124. x))
  3125. (default-value var)))
  3126. ;; The following variables are defined here because is it also used
  3127. ;; when formatting latex fragments. Originally it was part of the
  3128. ;; LaTeX exporter, which is why the name includes "export".
  3129. (defcustom org-export-latex-default-packages-alist
  3130. '(("AUTO" "inputenc" t)
  3131. ("T1" "fontenc" t)
  3132. ("" "fixltx2e" nil)
  3133. ("" "graphicx" t)
  3134. ("" "longtable" nil)
  3135. ("" "float" nil)
  3136. ("" "wrapfig" nil)
  3137. ("" "soul" t)
  3138. ("" "textcomp" t)
  3139. ("" "marvosym" t)
  3140. ("" "wasysym" t)
  3141. ("" "latexsym" t)
  3142. ("" "amssymb" t)
  3143. ("" "hyperref" nil)
  3144. "\\tolerance=1000"
  3145. )
  3146. "Alist of default packages to be inserted in the header.
  3147. Change this only if one of the packages here causes an incompatibility
  3148. with another package you are using.
  3149. The packages in this list are needed by one part or another of Org-mode
  3150. to function properly.
  3151. - inputenc, fontenc: for basic font and character selection
  3152. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3153. for interpreting the entities in `org-entities'. You can skip some of these
  3154. packages if you don't use any of the symbols in it.
  3155. - graphicx: for including images
  3156. - float, wrapfig: for figure placement
  3157. - longtable: for long tables
  3158. - hyperref: for cross references
  3159. Therefore you should not modify this variable unless you know what you
  3160. are doing. The one reason to change it anyway is that you might be loading
  3161. some other package that conflicts with one of the default packages.
  3162. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3163. If SNIPPET-FLAG is t, the package also needs to be included when
  3164. compiling LaTeX snippets into images for inclusion into HTML."
  3165. :group 'org-export-latex
  3166. :set 'org-set-packages-alist
  3167. :get 'org-get-packages-alist
  3168. :version "24.1"
  3169. :type '(repeat
  3170. (choice
  3171. (list :tag "options/package pair"
  3172. (string :tag "options")
  3173. (string :tag "package")
  3174. (boolean :tag "Snippet"))
  3175. (string :tag "A line of LaTeX"))))
  3176. (defcustom org-export-latex-packages-alist nil
  3177. "Alist of packages to be inserted in every LaTeX header.
  3178. These will be inserted after `org-export-latex-default-packages-alist'.
  3179. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3180. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3181. turning LaTeX snippets into images for inclusion into HTML.
  3182. Make sure that you only list packages here which:
  3183. - you want in every file
  3184. - do not conflict with the default packages in
  3185. `org-export-latex-default-packages-alist'
  3186. - do not conflict with the setup in `org-format-latex-header'."
  3187. :group 'org-export-latex
  3188. :set 'org-set-packages-alist
  3189. :get 'org-get-packages-alist
  3190. :type '(repeat
  3191. (choice
  3192. (list :tag "options/package pair"
  3193. (string :tag "options")
  3194. (string :tag "package")
  3195. (boolean :tag "Snippet"))
  3196. (string :tag "A line of LaTeX"))))
  3197. (defgroup org-appearance nil
  3198. "Settings for Org-mode appearance."
  3199. :tag "Org Appearance"
  3200. :group 'org)
  3201. (defcustom org-level-color-stars-only nil
  3202. "Non-nil means fontify only the stars in each headline.
  3203. When nil, the entire headline is fontified.
  3204. Changing it requires restart of `font-lock-mode' to become effective
  3205. also in regions already fontified."
  3206. :group 'org-appearance
  3207. :type 'boolean)
  3208. (defcustom org-hide-leading-stars nil
  3209. "Non-nil means hide the first N-1 stars in a headline.
  3210. This works by using the face `org-hide' for these stars. This
  3211. face is white for a light background, and black for a dark
  3212. background. You may have to customize the face `org-hide' to
  3213. make this work.
  3214. Changing it requires restart of `font-lock-mode' to become effective
  3215. also in regions already fontified.
  3216. You may also set this on a per-file basis by adding one of the following
  3217. lines to the buffer:
  3218. #+STARTUP: hidestars
  3219. #+STARTUP: showstars"
  3220. :group 'org-appearance
  3221. :type 'boolean)
  3222. (defcustom org-hidden-keywords nil
  3223. "List of symbols corresponding to keywords to be hidden the org buffer.
  3224. For example, a value '(title) for this list will make the document's title
  3225. appear in the buffer without the initial #+TITLE: keyword."
  3226. :group 'org-appearance
  3227. :version "24.1"
  3228. :type '(set (const :tag "#+AUTHOR" author)
  3229. (const :tag "#+DATE" date)
  3230. (const :tag "#+EMAIL" email)
  3231. (const :tag "#+TITLE" title)))
  3232. (defcustom org-custom-properties nil
  3233. "List of properties (as strings) with a special meaning.
  3234. The default use of these custom properties is to let the user
  3235. hide them with `org-toggle-custom-properties-visibility'."
  3236. :group 'org-properties
  3237. :group 'org-appearance
  3238. :version "24.3"
  3239. :type '(repeat (string :tag "Property Name")))
  3240. (defcustom org-fontify-done-headline nil
  3241. "Non-nil means change the face of a headline if it is marked DONE.
  3242. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3243. When this is non-nil, the headline after the keyword is set to the
  3244. `org-headline-done' as an additional indication."
  3245. :group 'org-appearance
  3246. :type 'boolean)
  3247. (defcustom org-fontify-emphasized-text t
  3248. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3249. Changing this variable requires a restart of Emacs to take effect."
  3250. :group 'org-appearance
  3251. :type 'boolean)
  3252. (defcustom org-fontify-whole-heading-line nil
  3253. "Non-nil means fontify the whole line for headings.
  3254. This is useful when setting a background color for the
  3255. org-level-* faces."
  3256. :group 'org-appearance
  3257. :type 'boolean)
  3258. (defcustom org-highlight-latex-fragments-and-specials nil
  3259. "Non-nil means fontify what is treated specially by the exporters."
  3260. :group 'org-appearance
  3261. :type 'boolean)
  3262. (defcustom org-hide-emphasis-markers nil
  3263. "Non-nil mean font-lock should hide the emphasis marker characters."
  3264. :group 'org-appearance
  3265. :type 'boolean)
  3266. (defcustom org-pretty-entities nil
  3267. "Non-nil means show entities as UTF8 characters.
  3268. When nil, the \\name form remains in the buffer."
  3269. :group 'org-appearance
  3270. :version "24.1"
  3271. :type 'boolean)
  3272. (defcustom org-pretty-entities-include-sub-superscripts t
  3273. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3274. :group 'org-appearance
  3275. :version "24.1"
  3276. :type 'boolean)
  3277. (defvar org-emph-re nil
  3278. "Regular expression for matching emphasis.
  3279. After a match, the match groups contain these elements:
  3280. 0 The match of the full regular expression, including the characters
  3281. before and after the proper match
  3282. 1 The character before the proper match, or empty at beginning of line
  3283. 2 The proper match, including the leading and trailing markers
  3284. 3 The leading marker like * or /, indicating the type of highlighting
  3285. 4 The text between the emphasis markers, not including the markers
  3286. 5 The character after the match, empty at the end of a line")
  3287. (defvar org-verbatim-re nil
  3288. "Regular expression for matching verbatim text.")
  3289. (defvar org-emphasis-regexp-components) ; defined just below
  3290. (defvar org-emphasis-alist) ; defined just below
  3291. (defun org-set-emph-re (var val)
  3292. "Set variable and compute the emphasis regular expression."
  3293. (set var val)
  3294. (when (and (boundp 'org-emphasis-alist)
  3295. (boundp 'org-emphasis-regexp-components)
  3296. org-emphasis-alist org-emphasis-regexp-components)
  3297. (let* ((e org-emphasis-regexp-components)
  3298. (pre (car e))
  3299. (post (nth 1 e))
  3300. (border (nth 2 e))
  3301. (body (nth 3 e))
  3302. (nl (nth 4 e))
  3303. (body1 (concat body "*?"))
  3304. (markers (mapconcat 'car org-emphasis-alist ""))
  3305. (vmarkers (mapconcat
  3306. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3307. org-emphasis-alist "")))
  3308. ;; make sure special characters appear at the right position in the class
  3309. (if (string-match "\\^" markers)
  3310. (setq markers (concat (replace-match "" t t markers) "^")))
  3311. (if (string-match "-" markers)
  3312. (setq markers (concat (replace-match "" t t markers) "-")))
  3313. (if (string-match "\\^" vmarkers)
  3314. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3315. (if (string-match "-" vmarkers)
  3316. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3317. (if (> nl 0)
  3318. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3319. (int-to-string nl) "\\}")))
  3320. ;; Make the regexp
  3321. (setq org-emph-re
  3322. (concat "\\([" pre "]\\|^\\)"
  3323. "\\("
  3324. "\\([" markers "]\\)"
  3325. "\\("
  3326. "[^" border "]\\|"
  3327. "[^" border "]"
  3328. body1
  3329. "[^" border "]"
  3330. "\\)"
  3331. "\\3\\)"
  3332. "\\([" post "]\\|$\\)"))
  3333. (setq org-verbatim-re
  3334. (concat "\\([" pre "]\\|^\\)"
  3335. "\\("
  3336. "\\([" vmarkers "]\\)"
  3337. "\\("
  3338. "[^" border "]\\|"
  3339. "[^" border "]"
  3340. body1
  3341. "[^" border "]"
  3342. "\\)"
  3343. "\\3\\)"
  3344. "\\([" post "]\\|$\\)")))))
  3345. (defcustom org-emphasis-regexp-components
  3346. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3347. "Components used to build the regular expression for emphasis.
  3348. This is a list with five entries. Terminology: In an emphasis string
  3349. like \" *strong word* \", we call the initial space PREMATCH, the final
  3350. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3351. and \"trong wor\" is the body. The different components in this variable
  3352. specify what is allowed/forbidden in each part:
  3353. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3354. post Chars allowed as postmatch. End of line will be allowed too.
  3355. border The chars *forbidden* as border characters.
  3356. body-regexp A regexp like \".\" to match a body character. Don't use
  3357. non-shy groups here, and don't allow newline here.
  3358. newline The maximum number of newlines allowed in an emphasis exp.
  3359. Use customize to modify this, or restart Emacs after changing it."
  3360. :group 'org-appearance
  3361. :set 'org-set-emph-re
  3362. :type '(list
  3363. (sexp :tag "Allowed chars in pre ")
  3364. (sexp :tag "Allowed chars in post ")
  3365. (sexp :tag "Forbidden chars in border ")
  3366. (sexp :tag "Regexp for body ")
  3367. (integer :tag "number of newlines allowed")
  3368. (option (boolean :tag "Please ignore this button"))))
  3369. (defcustom org-emphasis-alist
  3370. `(("*" bold "<b>" "</b>")
  3371. ("/" italic "<i>" "</i>")
  3372. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3373. ("=" org-code "<code>" "</code>" verbatim)
  3374. ("~" org-verbatim "<code>" "</code>" verbatim)
  3375. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3376. "<del>" "</del>")
  3377. )
  3378. "Special syntax for emphasized text.
  3379. Text starting and ending with a special character will be emphasized, for
  3380. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3381. characters, the face to be used by font-lock for highlighting in Org-mode
  3382. Emacs buffers, and the HTML tags to be used for this.
  3383. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3384. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3385. Use customize to modify this, or restart Emacs after changing it."
  3386. :group 'org-appearance
  3387. :set 'org-set-emph-re
  3388. :type '(repeat
  3389. (list
  3390. (string :tag "Marker character")
  3391. (choice
  3392. (face :tag "Font-lock-face")
  3393. (plist :tag "Face property list"))
  3394. (string :tag "HTML start tag")
  3395. (string :tag "HTML end tag")
  3396. (option (const verbatim)))))
  3397. (defvar org-syntax-table
  3398. (let ((st (make-syntax-table)))
  3399. (mapc (lambda(c) (modify-syntax-entry
  3400. (string-to-char (car c)) "w p" st))
  3401. org-emphasis-alist)
  3402. st))
  3403. (defvar org-protecting-blocks
  3404. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3405. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3406. This is needed for font-lock setup.")
  3407. ;;; Miscellaneous options
  3408. (defgroup org-completion nil
  3409. "Completion in Org-mode."
  3410. :tag "Org Completion"
  3411. :group 'org)
  3412. (defcustom org-completion-use-ido nil
  3413. "Non-nil means use ido completion wherever possible.
  3414. Note that `ido-mode' must be active for this variable to be relevant.
  3415. If you decide to turn this variable on, you might well want to turn off
  3416. `org-outline-path-complete-in-steps'.
  3417. See also `org-completion-use-iswitchb'."
  3418. :group 'org-completion
  3419. :type 'boolean)
  3420. (defcustom org-completion-use-iswitchb nil
  3421. "Non-nil means use iswitchb completion wherever possible.
  3422. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3423. If you decide to turn this variable on, you might well want to turn off
  3424. `org-outline-path-complete-in-steps'.
  3425. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3426. :group 'org-completion
  3427. :type 'boolean)
  3428. (defcustom org-completion-fallback-command 'hippie-expand
  3429. "The expansion command called by \\[pcomplete] in normal context.
  3430. Normal means, no org-mode-specific context."
  3431. :group 'org-completion
  3432. :type 'function)
  3433. ;;; Functions and variables from their packages
  3434. ;; Declared here to avoid compiler warnings
  3435. ;; XEmacs only
  3436. (defvar outline-mode-menu-heading)
  3437. (defvar outline-mode-menu-show)
  3438. (defvar outline-mode-menu-hide)
  3439. (defvar zmacs-regions) ; XEmacs regions
  3440. ;; Emacs only
  3441. (defvar mark-active)
  3442. ;; Various packages
  3443. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3444. (declare-function calendar-forward-day "cal-move" (arg))
  3445. (declare-function calendar-goto-date "cal-move" (date))
  3446. (declare-function calendar-goto-today "cal-move" ())
  3447. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3448. (defvar calc-embedded-close-formula)
  3449. (defvar calc-embedded-open-formula)
  3450. (declare-function cdlatex-tab "ext:cdlatex" ())
  3451. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3452. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3453. (defvar font-lock-unfontify-region-function)
  3454. (declare-function iswitchb-read-buffer "iswitchb"
  3455. (prompt &optional default require-match start matches-set))
  3456. (defvar iswitchb-temp-buflist)
  3457. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3458. (defvar org-agenda-tags-todo-honor-ignore-options)
  3459. (declare-function org-agenda-skip "org-agenda" ())
  3460. (declare-function
  3461. org-agenda-format-item "org-agenda"
  3462. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3463. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3464. (declare-function org-agenda-change-all-lines "org-agenda"
  3465. (newhead hdmarker &optional fixface just-this))
  3466. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3467. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3468. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3469. (beg end))
  3470. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3471. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3472. "org-agenda" (&optional end))
  3473. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3474. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3475. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3476. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3477. (declare-function org-indent-mode "org-indent" (&optional arg))
  3478. (declare-function parse-time-string "parse-time" (string))
  3479. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3480. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3481. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3482. (defvar remember-data-file)
  3483. (defvar texmathp-why)
  3484. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3485. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3486. (defvar org-latex-regexps)
  3487. ;;; Autoload and prepare some org modules
  3488. ;; Some table stuff that needs to be defined here, because it is used
  3489. ;; by the functions setting up org-mode or checking for table context.
  3490. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3491. "Detect an org-type or table-type table.")
  3492. (defconst org-table-line-regexp "^[ \t]*|"
  3493. "Detect an org-type table line.")
  3494. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3495. "Detect an org-type table line.")
  3496. (defconst org-table-hline-regexp "^[ \t]*|-"
  3497. "Detect an org-type table hline.")
  3498. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3499. "Detect a table-type table hline.")
  3500. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3501. "Detect the first line outside a table when searching from within it.
  3502. This works for both table types.")
  3503. ;; Autoload the functions in org-table.el that are needed by functions here.
  3504. (eval-and-compile
  3505. (org-autoload "org-table"
  3506. '(org-table-begin org-table-blank-field org-table-end)))
  3507. ;;;###autoload
  3508. (defun turn-on-orgtbl ()
  3509. "Unconditionally turn on `orgtbl-mode'."
  3510. (require 'org-table)
  3511. (orgtbl-mode 1))
  3512. (defun org-at-table-p (&optional table-type)
  3513. "Return t if the cursor is inside an org-type table.
  3514. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3515. (if org-enable-table-editor
  3516. (save-excursion
  3517. (beginning-of-line 1)
  3518. (looking-at (if table-type org-table-any-line-regexp
  3519. org-table-line-regexp)))
  3520. nil))
  3521. (defsubst org-table-p () (org-at-table-p))
  3522. (defun org-at-table.el-p ()
  3523. "Return t if and only if we are at a table.el table."
  3524. (and (org-at-table-p 'any)
  3525. (save-excursion
  3526. (goto-char (org-table-begin 'any))
  3527. (looking-at org-table1-hline-regexp))))
  3528. (defun org-table-recognize-table.el ()
  3529. "If there is a table.el table nearby, recognize it and move into it."
  3530. (if org-table-tab-recognizes-table.el
  3531. (if (org-at-table.el-p)
  3532. (progn
  3533. (beginning-of-line 1)
  3534. (if (looking-at org-table-dataline-regexp)
  3535. nil
  3536. (if (looking-at org-table1-hline-regexp)
  3537. (progn
  3538. (beginning-of-line 2)
  3539. (if (looking-at org-table-any-border-regexp)
  3540. (beginning-of-line -1)))))
  3541. (if (re-search-forward "|" (org-table-end t) t)
  3542. (progn
  3543. (require 'table)
  3544. (if (table--at-cell-p (point))
  3545. t
  3546. (message "recognizing table.el table...")
  3547. (table-recognize-table)
  3548. (message "recognizing table.el table...done")))
  3549. (error "This should not happen"))
  3550. t)
  3551. nil)
  3552. nil))
  3553. (defun org-at-table-hline-p ()
  3554. "Return t if the cursor is inside a hline in a table."
  3555. (if org-enable-table-editor
  3556. (save-excursion
  3557. (beginning-of-line 1)
  3558. (looking-at org-table-hline-regexp))
  3559. nil))
  3560. (defvar org-table-clean-did-remove-column nil)
  3561. (defun org-table-map-tables (function &optional quietly)
  3562. "Apply FUNCTION to the start of all tables in the buffer."
  3563. (save-excursion
  3564. (save-restriction
  3565. (widen)
  3566. (goto-char (point-min))
  3567. (while (re-search-forward org-table-any-line-regexp nil t)
  3568. (unless quietly
  3569. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3570. (beginning-of-line 1)
  3571. (when (and (looking-at org-table-line-regexp)
  3572. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3573. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3574. (save-excursion (funcall function))
  3575. (or (looking-at org-table-line-regexp)
  3576. (forward-char 1)))
  3577. (re-search-forward org-table-any-border-regexp nil 1))))
  3578. (unless quietly (message "Mapping tables: done")))
  3579. ;; Declare and autoload functions from org-exp.el & Co
  3580. (declare-function org-default-export-plist "org-exp")
  3581. (declare-function org-infile-export-plist "org-exp")
  3582. (declare-function org-get-current-options "org-exp")
  3583. ;; Declare and autoload functions from org-agenda.el
  3584. (eval-and-compile
  3585. (org-autoload "org-agenda"
  3586. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3587. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3588. (declare-function org-clock-update-mode-line "org-clock" ())
  3589. (declare-function org-resolve-clocks "org-clock"
  3590. (&optional also-non-dangling-p prompt last-valid))
  3591. (defvar org-clock-start-time)
  3592. (defvar org-clock-marker (make-marker)
  3593. "Marker recording the last clock-in.")
  3594. (defvar org-clock-hd-marker (make-marker)
  3595. "Marker recording the last clock-in, but the headline position.")
  3596. (defvar org-clock-heading ""
  3597. "The heading of the current clock entry.")
  3598. (defun org-clock-is-active ()
  3599. "Return non-nil if clock is currently running.
  3600. The return value is actually the clock marker."
  3601. (marker-buffer org-clock-marker))
  3602. (eval-and-compile
  3603. (org-autoload "org-clock" '(org-clock-remove-overlays
  3604. org-clock-update-time-maybe
  3605. org-clocktable-shift)))
  3606. (defun org-check-running-clock ()
  3607. "Check if the current buffer contains the running clock.
  3608. If yes, offer to stop it and to save the buffer with the changes."
  3609. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3610. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3611. (buffer-name))))
  3612. (org-clock-out)
  3613. (when (y-or-n-p "Save changed buffer?")
  3614. (save-buffer))))
  3615. (defun org-clocktable-try-shift (dir n)
  3616. "Check if this line starts a clock table, if yes, shift the time block."
  3617. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3618. (org-clocktable-shift dir n)))
  3619. ;;;###autoload
  3620. (defun org-clock-persistence-insinuate ()
  3621. "Set up hooks for clock persistence."
  3622. (require 'org-clock)
  3623. (add-hook 'org-mode-hook 'org-clock-load)
  3624. (add-hook 'kill-emacs-hook 'org-clock-save))
  3625. ;; Define the variable already here, to make sure we have it.
  3626. (defvar org-indent-mode nil
  3627. "Non-nil if Org-Indent mode is enabled.
  3628. Use the command `org-indent-mode' to change this variable.")
  3629. ;; Autoload archiving code
  3630. ;; The stuff that is needed for cycling and tags has to be defined here.
  3631. (defgroup org-archive nil
  3632. "Options concerning archiving in Org-mode."
  3633. :tag "Org Archive"
  3634. :group 'org-structure)
  3635. (defcustom org-archive-location "%s_archive::"
  3636. "The location where subtrees should be archived.
  3637. The value of this variable is a string, consisting of two parts,
  3638. separated by a double-colon. The first part is a filename and
  3639. the second part is a headline.
  3640. When the filename is omitted, archiving happens in the same file.
  3641. %s in the filename will be replaced by the current file
  3642. name (without the directory part). Archiving to a different file
  3643. is useful to keep archived entries from contributing to the
  3644. Org-mode Agenda.
  3645. The archived entries will be filed as subtrees of the specified
  3646. headline. When the headline is omitted, the subtrees are simply
  3647. filed away at the end of the file, as top-level entries. Also in
  3648. the heading you can use %s to represent the file name, this can be
  3649. useful when using the same archive for a number of different files.
  3650. Here are a few examples:
  3651. \"%s_archive::\"
  3652. If the current file is Projects.org, archive in file
  3653. Projects.org_archive, as top-level trees. This is the default.
  3654. \"::* Archived Tasks\"
  3655. Archive in the current file, under the top-level headline
  3656. \"* Archived Tasks\".
  3657. \"~/org/archive.org::\"
  3658. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3659. \"~/org/archive.org::* From %s\"
  3660. Archive in file ~/org/archive.org (absolute path), under headlines
  3661. \"From FILENAME\" where file name is the current file name.
  3662. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3663. The \"datetree/\" string is special, signifying to archive
  3664. items to the datetree. Items are placed in either the CLOSED
  3665. date of the item, or the current date if there is no CLOSED date.
  3666. The heading will be a subentry to the current date. There doesn't
  3667. need to be a heading, but there always needs to be a slash after
  3668. datetree. For example, to store archived items directly in the
  3669. datetree, use \"~/org/datetree.org::datetree/\".
  3670. \"basement::** Finished Tasks\"
  3671. Archive in file ./basement (relative path), as level 3 trees
  3672. below the level 2 heading \"** Finished Tasks\".
  3673. You may set this option on a per-file basis by adding to the buffer a
  3674. line like
  3675. #+ARCHIVE: basement::** Finished Tasks
  3676. You may also define it locally for a subtree by setting an ARCHIVE property
  3677. in the entry. If such a property is found in an entry, or anywhere up
  3678. the hierarchy, it will be used."
  3679. :group 'org-archive
  3680. :type 'string)
  3681. (defcustom org-archive-tag "ARCHIVE"
  3682. "The tag that marks a subtree as archived.
  3683. An archived subtree does not open during visibility cycling, and does
  3684. not contribute to the agenda listings.
  3685. After changing this, font-lock must be restarted in the relevant buffers to
  3686. get the proper fontification."
  3687. :group 'org-archive
  3688. :group 'org-keywords
  3689. :type 'string)
  3690. (defcustom org-agenda-skip-archived-trees t
  3691. "Non-nil means the agenda will skip any items located in archived trees.
  3692. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3693. variable is no longer recommended, you should leave it at the value t.
  3694. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3695. :group 'org-archive
  3696. :group 'org-agenda-skip
  3697. :type 'boolean)
  3698. (defcustom org-columns-skip-archived-trees t
  3699. "Non-nil means ignore archived trees when creating column view."
  3700. :group 'org-archive
  3701. :group 'org-properties
  3702. :type 'boolean)
  3703. (defcustom org-cycle-open-archived-trees nil
  3704. "Non-nil means `org-cycle' will open archived trees.
  3705. An archived tree is a tree marked with the tag ARCHIVE.
  3706. When nil, archived trees will stay folded. You can still open them with
  3707. normal outline commands like `show-all', but not with the cycling commands."
  3708. :group 'org-archive
  3709. :group 'org-cycle
  3710. :type 'boolean)
  3711. (defcustom org-sparse-tree-open-archived-trees nil
  3712. "Non-nil means sparse tree construction shows matches in archived trees.
  3713. When nil, matches in these trees are highlighted, but the trees are kept in
  3714. collapsed state."
  3715. :group 'org-archive
  3716. :group 'org-sparse-trees
  3717. :type 'boolean)
  3718. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3719. "The default date type when building a sparse tree.
  3720. When this is nil, a date is a scheduled or a deadline timestamp.
  3721. Otherwise, these types are allowed:
  3722. all: all timestamps
  3723. active: only active timestamps (<...>)
  3724. inactive: only inactive timestamps (<...)
  3725. scheduled: only scheduled timestamps
  3726. deadline: only deadline timestamps"
  3727. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3728. (const :tag "All timestamps" all)
  3729. (const :tag "Only active timestamps" active)
  3730. (const :tag "Only inactive timestamps" inactive)
  3731. (const :tag "Only scheduled timestamps" scheduled)
  3732. (const :tag "Only deadline timestamps" deadline))
  3733. :version "24.3"
  3734. :group 'org-sparse-trees)
  3735. (defun org-cycle-hide-archived-subtrees (state)
  3736. "Re-hide all archived subtrees after a visibility state change."
  3737. (when (and (not org-cycle-open-archived-trees)
  3738. (not (memq state '(overview folded))))
  3739. (save-excursion
  3740. (let* ((globalp (memq state '(contents all)))
  3741. (beg (if globalp (point-min) (point)))
  3742. (end (if globalp (point-max) (org-end-of-subtree t))))
  3743. (org-hide-archived-subtrees beg end)
  3744. (goto-char beg)
  3745. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3746. (message "%s" (substitute-command-keys
  3747. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3748. (defun org-force-cycle-archived ()
  3749. "Cycle subtree even if it is archived."
  3750. (interactive)
  3751. (setq this-command 'org-cycle)
  3752. (let ((org-cycle-open-archived-trees t))
  3753. (call-interactively 'org-cycle)))
  3754. (defun org-hide-archived-subtrees (beg end)
  3755. "Re-hide all archived subtrees after a visibility state change."
  3756. (save-excursion
  3757. (let* ((re (concat ":" org-archive-tag ":")))
  3758. (goto-char beg)
  3759. (while (re-search-forward re end t)
  3760. (when (org-at-heading-p)
  3761. (org-flag-subtree t)
  3762. (org-end-of-subtree t))))))
  3763. (declare-function outline-end-of-heading "outline" ())
  3764. (declare-function outline-flag-region "outline" (from to flag))
  3765. (defun org-flag-subtree (flag)
  3766. (save-excursion
  3767. (org-back-to-heading t)
  3768. (outline-end-of-heading)
  3769. (outline-flag-region (point)
  3770. (progn (org-end-of-subtree t) (point))
  3771. flag)))
  3772. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3773. (eval-and-compile
  3774. (org-autoload "org-archive"
  3775. '(org-add-archive-files)))
  3776. ;; Autoload Column View Code
  3777. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3778. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3779. (declare-function org-columns-compute "org-colview" (property))
  3780. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3781. '(org-columns-number-to-string
  3782. org-columns-get-format-and-top-level
  3783. org-columns-compute
  3784. org-columns-remove-overlays))
  3785. ;; Autoload ID code
  3786. (declare-function org-id-store-link "org-id")
  3787. (declare-function org-id-locations-load "org-id")
  3788. (declare-function org-id-locations-save "org-id")
  3789. (defvar org-id-track-globally)
  3790. (org-autoload "org-id"
  3791. '(org-id-new
  3792. org-id-copy
  3793. org-id-get-with-outline-path-completion
  3794. org-id-get-with-outline-drilling))
  3795. ;;; Variables for pre-computed regular expressions, all buffer local
  3796. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3797. "Matches first line of a hidden block.")
  3798. (make-variable-buffer-local 'org-drawer-regexp)
  3799. (defvar org-todo-regexp nil
  3800. "Matches any of the TODO state keywords.")
  3801. (make-variable-buffer-local 'org-todo-regexp)
  3802. (defvar org-not-done-regexp nil
  3803. "Matches any of the TODO state keywords except the last one.")
  3804. (make-variable-buffer-local 'org-not-done-regexp)
  3805. (defvar org-not-done-heading-regexp nil
  3806. "Matches a TODO headline that is not done.")
  3807. (make-variable-buffer-local 'org-not-done-regexp)
  3808. (defvar org-todo-line-regexp nil
  3809. "Matches a headline and puts TODO state into group 2 if present.")
  3810. (make-variable-buffer-local 'org-todo-line-regexp)
  3811. (defvar org-complex-heading-regexp nil
  3812. "Matches a headline and puts everything into groups:
  3813. group 1: the stars
  3814. group 2: The todo keyword, maybe
  3815. group 3: Priority cookie
  3816. group 4: True headline
  3817. group 5: Tags")
  3818. (make-variable-buffer-local 'org-complex-heading-regexp)
  3819. (defvar org-complex-heading-regexp-format nil
  3820. "Printf format to make regexp to match an exact headline.
  3821. This regexp will match the headline of any node which has the
  3822. exact headline text that is put into the format, but may have any
  3823. TODO state, priority and tags.")
  3824. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3825. (defvar org-todo-line-tags-regexp nil
  3826. "Matches a headline and puts TODO state into group 2 if present.
  3827. Also put tags into group 4 if tags are present.")
  3828. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3829. (defvar org-ds-keyword-length 12
  3830. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3831. (make-variable-buffer-local 'org-ds-keyword-length)
  3832. (defvar org-deadline-regexp nil
  3833. "Matches the DEADLINE keyword.")
  3834. (make-variable-buffer-local 'org-deadline-regexp)
  3835. (defvar org-deadline-time-regexp nil
  3836. "Matches the DEADLINE keyword together with a time stamp.")
  3837. (make-variable-buffer-local 'org-deadline-time-regexp)
  3838. (defvar org-deadline-line-regexp nil
  3839. "Matches the DEADLINE keyword and the rest of the line.")
  3840. (make-variable-buffer-local 'org-deadline-line-regexp)
  3841. (defvar org-scheduled-regexp nil
  3842. "Matches the SCHEDULED keyword.")
  3843. (make-variable-buffer-local 'org-scheduled-regexp)
  3844. (defvar org-scheduled-time-regexp nil
  3845. "Matches the SCHEDULED keyword together with a time stamp.")
  3846. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3847. (defvar org-closed-time-regexp nil
  3848. "Matches the CLOSED keyword together with a time stamp.")
  3849. (make-variable-buffer-local 'org-closed-time-regexp)
  3850. (defvar org-keyword-time-regexp nil
  3851. "Matches any of the 4 keywords, together with the time stamp.")
  3852. (make-variable-buffer-local 'org-keyword-time-regexp)
  3853. (defvar org-keyword-time-not-clock-regexp nil
  3854. "Matches any of the 3 keywords, together with the time stamp.")
  3855. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3856. (defvar org-maybe-keyword-time-regexp nil
  3857. "Matches a timestamp, possibly preceded by a keyword.")
  3858. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3859. (defvar org-all-time-keywords nil
  3860. "List of time keywords.")
  3861. (make-variable-buffer-local 'org-all-time-keywords)
  3862. (defconst org-plain-time-of-day-regexp
  3863. (concat
  3864. "\\(\\<[012]?[0-9]"
  3865. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3866. "\\(--?"
  3867. "\\(\\<[012]?[0-9]"
  3868. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3869. "\\)?")
  3870. "Regular expression to match a plain time or time range.
  3871. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3872. groups carry important information:
  3873. 0 the full match
  3874. 1 the first time, range or not
  3875. 8 the second time, if it is a range.")
  3876. (defconst org-plain-time-extension-regexp
  3877. (concat
  3878. "\\(\\<[012]?[0-9]"
  3879. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3880. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3881. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3882. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3883. groups carry important information:
  3884. 0 the full match
  3885. 7 hours of duration
  3886. 9 minutes of duration")
  3887. (defconst org-stamp-time-of-day-regexp
  3888. (concat
  3889. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3890. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3891. "\\(--?"
  3892. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3893. "Regular expression to match a timestamp time or time range.
  3894. After a match, the following groups carry important information:
  3895. 0 the full match
  3896. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3897. 2 the first time, range or not
  3898. 4 the second time, if it is a range.")
  3899. (defconst org-startup-options
  3900. '(("fold" org-startup-folded t)
  3901. ("overview" org-startup-folded t)
  3902. ("nofold" org-startup-folded nil)
  3903. ("showall" org-startup-folded nil)
  3904. ("showeverything" org-startup-folded showeverything)
  3905. ("content" org-startup-folded content)
  3906. ("indent" org-startup-indented t)
  3907. ("noindent" org-startup-indented nil)
  3908. ("hidestars" org-hide-leading-stars t)
  3909. ("showstars" org-hide-leading-stars nil)
  3910. ("odd" org-odd-levels-only t)
  3911. ("oddeven" org-odd-levels-only nil)
  3912. ("align" org-startup-align-all-tables t)
  3913. ("noalign" org-startup-align-all-tables nil)
  3914. ("inlineimages" org-startup-with-inline-images t)
  3915. ("noinlineimages" org-startup-with-inline-images nil)
  3916. ("customtime" org-display-custom-times t)
  3917. ("logdone" org-log-done time)
  3918. ("lognotedone" org-log-done note)
  3919. ("nologdone" org-log-done nil)
  3920. ("lognoteclock-out" org-log-note-clock-out t)
  3921. ("nolognoteclock-out" org-log-note-clock-out nil)
  3922. ("logrepeat" org-log-repeat state)
  3923. ("lognoterepeat" org-log-repeat note)
  3924. ("logdrawer" org-log-into-drawer t)
  3925. ("nologdrawer" org-log-into-drawer nil)
  3926. ("logstatesreversed" org-log-states-order-reversed t)
  3927. ("nologstatesreversed" org-log-states-order-reversed nil)
  3928. ("nologrepeat" org-log-repeat nil)
  3929. ("logreschedule" org-log-reschedule time)
  3930. ("lognotereschedule" org-log-reschedule note)
  3931. ("nologreschedule" org-log-reschedule nil)
  3932. ("logredeadline" org-log-redeadline time)
  3933. ("lognoteredeadline" org-log-redeadline note)
  3934. ("nologredeadline" org-log-redeadline nil)
  3935. ("logrefile" org-log-refile time)
  3936. ("lognoterefile" org-log-refile note)
  3937. ("nologrefile" org-log-refile nil)
  3938. ("fninline" org-footnote-define-inline t)
  3939. ("nofninline" org-footnote-define-inline nil)
  3940. ("fnlocal" org-footnote-section nil)
  3941. ("fnauto" org-footnote-auto-label t)
  3942. ("fnprompt" org-footnote-auto-label nil)
  3943. ("fnconfirm" org-footnote-auto-label confirm)
  3944. ("fnplain" org-footnote-auto-label plain)
  3945. ("fnadjust" org-footnote-auto-adjust t)
  3946. ("nofnadjust" org-footnote-auto-adjust nil)
  3947. ("constcgs" constants-unit-system cgs)
  3948. ("constSI" constants-unit-system SI)
  3949. ("noptag" org-tag-persistent-alist nil)
  3950. ("hideblocks" org-hide-block-startup t)
  3951. ("nohideblocks" org-hide-block-startup nil)
  3952. ("beamer" org-startup-with-beamer-mode t)
  3953. ("entitiespretty" org-pretty-entities t)
  3954. ("entitiesplain" org-pretty-entities nil))
  3955. "Variable associated with STARTUP options for org-mode.
  3956. Each element is a list of three items: the startup options (as written
  3957. in the #+STARTUP line), the corresponding variable, and the value to set
  3958. this variable to if the option is found. An optional forth element PUSH
  3959. means to push this value onto the list in the variable.")
  3960. (defun org-update-property-plist (key val props)
  3961. "Update PROPS with KEY and VAL."
  3962. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3963. (key (if appending (substring key 0 (- (length key) 1)) key))
  3964. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3965. (previous (cdr (assoc key props))))
  3966. (if appending
  3967. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3968. (cons (cons key val) remainder))))
  3969. (defconst org-block-regexp
  3970. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3971. "Regular expression for hiding blocks.")
  3972. (defconst org-heading-keyword-regexp-format
  3973. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3974. "Printf format for a regexp matching an headline with some keyword.
  3975. This regexp will match the headline of any node which has the
  3976. exact keyword that is put into the format. The keyword isn't in
  3977. any group by default, but the stars and the body are.")
  3978. (defconst org-heading-keyword-maybe-regexp-format
  3979. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3980. "Printf format for a regexp matching an headline, possibly with some keyword.
  3981. This regexp can match any headline with the specified keyword, or
  3982. without a keyword. The keyword isn't in any group by default,
  3983. but the stars and the body are.")
  3984. (defun org-set-regexps-and-options ()
  3985. "Precompute regular expressions for current buffer."
  3986. (when (derived-mode-p 'org-mode)
  3987. (org-set-local 'org-todo-kwd-alist nil)
  3988. (org-set-local 'org-todo-key-alist nil)
  3989. (org-set-local 'org-todo-key-trigger nil)
  3990. (org-set-local 'org-todo-keywords-1 nil)
  3991. (org-set-local 'org-done-keywords nil)
  3992. (org-set-local 'org-todo-heads nil)
  3993. (org-set-local 'org-todo-sets nil)
  3994. (org-set-local 'org-todo-log-states nil)
  3995. (org-set-local 'org-file-properties nil)
  3996. (org-set-local 'org-file-tags nil)
  3997. (let ((re (org-make-options-regexp
  3998. '("CATEGORY" "TODO" "COLUMNS"
  3999. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4000. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4001. "OPTIONS")
  4002. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4003. (splitre "[ \t]+")
  4004. (scripts org-use-sub-superscripts)
  4005. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4006. tail sep kws1 prio props ftags drawers beamer-p
  4007. ext-setup-or-nil setup-contents (start 0))
  4008. (save-excursion
  4009. (save-restriction
  4010. (widen)
  4011. (goto-char (point-min))
  4012. (while (or (and ext-setup-or-nil
  4013. (string-match re ext-setup-or-nil start)
  4014. (setq start (match-end 0)))
  4015. (and (setq ext-setup-or-nil nil start 0)
  4016. (re-search-forward re nil t)))
  4017. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4018. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4019. (if (stringp value) (setq value (org-trim value)))
  4020. (cond
  4021. ((equal key "CATEGORY")
  4022. (setq cat value))
  4023. ((member key '("SEQ_TODO" "TODO"))
  4024. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4025. ((equal key "TYP_TODO")
  4026. (push (cons 'type (org-split-string value splitre)) kwds))
  4027. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4028. ;; general TODO-like setup
  4029. (push (cons (intern (downcase (match-string 1 key)))
  4030. (org-split-string value splitre)) kwds))
  4031. ((equal key "TAGS")
  4032. (setq tags (append tags (if tags '("\\n") nil)
  4033. (org-split-string value splitre))))
  4034. ((equal key "COLUMNS")
  4035. (org-set-local 'org-columns-default-format value))
  4036. ((equal key "LINK")
  4037. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4038. (push (cons (match-string 1 value)
  4039. (org-trim (match-string 2 value)))
  4040. links)))
  4041. ((equal key "PRIORITIES")
  4042. (setq prio (org-split-string value " +")))
  4043. ((equal key "PROPERTY")
  4044. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4045. (setq props (org-update-property-plist (match-string 1 value)
  4046. (match-string 2 value)
  4047. props))))
  4048. ((equal key "FILETAGS")
  4049. (when (string-match "\\S-" value)
  4050. (setq ftags
  4051. (append
  4052. ftags
  4053. (apply 'append
  4054. (mapcar (lambda (x) (org-split-string x ":"))
  4055. (org-split-string value)))))))
  4056. ((equal key "DRAWERS")
  4057. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4058. ((equal key "CONSTANTS")
  4059. (setq const (append const (org-split-string value splitre))))
  4060. ((equal key "STARTUP")
  4061. (let ((opts (org-split-string value splitre))
  4062. l var val)
  4063. (while (setq l (pop opts))
  4064. (when (setq l (assoc l org-startup-options))
  4065. (setq var (nth 1 l) val (nth 2 l))
  4066. (if (not (nth 3 l))
  4067. (set (make-local-variable var) val)
  4068. (if (not (listp (symbol-value var)))
  4069. (set (make-local-variable var) nil))
  4070. (set (make-local-variable var) (symbol-value var))
  4071. (add-to-list var val))))))
  4072. ((equal key "ARCHIVE")
  4073. (setq arch value)
  4074. (remove-text-properties 0 (length arch)
  4075. '(face t fontified t) arch))
  4076. ((equal key "LATEX_CLASS")
  4077. (setq beamer-p (equal value "beamer")))
  4078. ((equal key "OPTIONS")
  4079. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4080. (setq scripts (read (match-string 2 value)))))
  4081. ((equal key "SETUPFILE")
  4082. (setq setup-contents (org-file-contents
  4083. (expand-file-name
  4084. (org-remove-double-quotes value))
  4085. 'noerror))
  4086. (if (not ext-setup-or-nil)
  4087. (setq ext-setup-or-nil setup-contents start 0)
  4088. (setq ext-setup-or-nil
  4089. (concat (substring ext-setup-or-nil 0 start)
  4090. "\n" setup-contents "\n"
  4091. (substring ext-setup-or-nil start)))))))
  4092. ;; search for property blocks
  4093. (goto-char (point-min))
  4094. (while (re-search-forward org-block-regexp nil t)
  4095. (when (equal "PROPERTY" (upcase (match-string 1)))
  4096. (setq value (replace-regexp-in-string
  4097. "[\n\r]" " " (match-string 4)))
  4098. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4099. (setq props (org-update-property-plist (match-string 1 value)
  4100. (match-string 2 value)
  4101. props)))))))
  4102. (org-set-local 'org-use-sub-superscripts scripts)
  4103. (when cat
  4104. (org-set-local 'org-category (intern cat))
  4105. (push (cons "CATEGORY" cat) props))
  4106. (when prio
  4107. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4108. (setq prio (mapcar 'string-to-char prio))
  4109. (org-set-local 'org-highest-priority (nth 0 prio))
  4110. (org-set-local 'org-lowest-priority (nth 1 prio))
  4111. (org-set-local 'org-default-priority (nth 2 prio)))
  4112. (and props (org-set-local 'org-file-properties (nreverse props)))
  4113. (and ftags (org-set-local 'org-file-tags
  4114. (mapcar 'org-add-prop-inherited ftags)))
  4115. (and drawers (org-set-local 'org-drawers drawers))
  4116. (and arch (org-set-local 'org-archive-location arch))
  4117. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4118. ;; Process the TODO keywords
  4119. (unless kwds
  4120. ;; Use the global values as if they had been given locally.
  4121. (setq kwds (default-value 'org-todo-keywords))
  4122. (if (stringp (car kwds))
  4123. (setq kwds (list (cons org-todo-interpretation
  4124. (default-value 'org-todo-keywords)))))
  4125. (setq kwds (reverse kwds)))
  4126. (setq kwds (nreverse kwds))
  4127. (let (inter kws kw)
  4128. (while (setq kws (pop kwds))
  4129. (let ((kws (or
  4130. (run-hook-with-args-until-success
  4131. 'org-todo-setup-filter-hook kws)
  4132. kws)))
  4133. (setq inter (pop kws) sep (member "|" kws)
  4134. kws0 (delete "|" (copy-sequence kws))
  4135. kwsa nil
  4136. kws1 (mapcar
  4137. (lambda (x)
  4138. ;; 1 2
  4139. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4140. (progn
  4141. (setq kw (match-string 1 x)
  4142. key (and (match-end 2) (match-string 2 x))
  4143. log (org-extract-log-state-settings x))
  4144. (push (cons kw (and key (string-to-char key))) kwsa)
  4145. (and log (push log org-todo-log-states))
  4146. kw)
  4147. (error "Invalid TODO keyword %s" x)))
  4148. kws0)
  4149. kwsa (if kwsa (append '((:startgroup))
  4150. (nreverse kwsa)
  4151. '((:endgroup))))
  4152. hw (car kws1)
  4153. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4154. tail (list inter hw (car dws) (org-last dws))))
  4155. (add-to-list 'org-todo-heads hw 'append)
  4156. (push kws1 org-todo-sets)
  4157. (setq org-done-keywords (append org-done-keywords dws nil))
  4158. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4159. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4160. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4161. (setq org-todo-sets (nreverse org-todo-sets)
  4162. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4163. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4164. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4165. ;; Process the constants
  4166. (when const
  4167. (let (e cst)
  4168. (while (setq e (pop const))
  4169. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4170. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4171. (setq org-table-formula-constants-local cst)))
  4172. ;; Process the tags.
  4173. (when tags
  4174. (let (e tgs)
  4175. (while (setq e (pop tags))
  4176. (cond
  4177. ((equal e "{") (push '(:startgroup) tgs))
  4178. ((equal e "}") (push '(:endgroup) tgs))
  4179. ((equal e "\\n") (push '(:newline) tgs))
  4180. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4181. (push (cons (match-string 1 e)
  4182. (string-to-char (match-string 2 e)))
  4183. tgs))
  4184. (t (push (list e) tgs))))
  4185. (org-set-local 'org-tag-alist nil)
  4186. (while (setq e (pop tgs))
  4187. (or (and (stringp (car e))
  4188. (assoc (car e) org-tag-alist))
  4189. (push e org-tag-alist)))))
  4190. ;; Compute the regular expressions and other local variables.
  4191. ;; Using `org-outline-regexp-bol' would complicate them much,
  4192. ;; because of the fixed white space at the end of that string.
  4193. (if (not org-done-keywords)
  4194. (setq org-done-keywords (and org-todo-keywords-1
  4195. (list (org-last org-todo-keywords-1)))))
  4196. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4197. (length org-scheduled-string)
  4198. (length org-clock-string)
  4199. (length org-closed-string)))
  4200. org-drawer-regexp
  4201. (concat "^[ \t]*:\\("
  4202. (mapconcat 'regexp-quote org-drawers "\\|")
  4203. "\\):[ \t]*$")
  4204. org-not-done-keywords
  4205. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4206. org-todo-regexp
  4207. (concat "\\("
  4208. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4209. "\\)")
  4210. org-not-done-regexp
  4211. (concat "\\("
  4212. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4213. "\\)")
  4214. org-not-done-heading-regexp
  4215. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4216. org-todo-line-regexp
  4217. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4218. org-complex-heading-regexp
  4219. (concat "^\\(\\*+\\)"
  4220. "\\(?: +" org-todo-regexp "\\)?"
  4221. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4222. "\\(?: +\\(.*?\\)\\)??"
  4223. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4224. "[ \t]*$")
  4225. org-complex-heading-regexp-format
  4226. (concat "^\\(\\*+\\)"
  4227. "\\(?: +" org-todo-regexp "\\)?"
  4228. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4229. "\\(?: +"
  4230. ;; Stats cookies can be stuck to body.
  4231. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4232. "\\(%s\\)"
  4233. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4234. "\\)"
  4235. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4236. "[ \t]*$")
  4237. org-todo-line-tags-regexp
  4238. (concat "^\\(\\*+\\)"
  4239. "\\(?: +" org-todo-regexp "\\)?"
  4240. "\\(?: +\\(.*?\\)\\)??"
  4241. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4242. "[ \t]*$")
  4243. org-deadline-regexp (concat "\\<" org-deadline-string)
  4244. org-deadline-time-regexp
  4245. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4246. org-deadline-line-regexp
  4247. (concat "\\<\\(" org-deadline-string "\\).*")
  4248. org-scheduled-regexp
  4249. (concat "\\<" org-scheduled-string)
  4250. org-scheduled-time-regexp
  4251. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4252. org-closed-time-regexp
  4253. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4254. org-keyword-time-regexp
  4255. (concat "\\<\\(" org-scheduled-string
  4256. "\\|" org-deadline-string
  4257. "\\|" org-closed-string
  4258. "\\|" org-clock-string "\\)"
  4259. " *[[<]\\([^]>]+\\)[]>]")
  4260. org-keyword-time-not-clock-regexp
  4261. (concat "\\<\\(" org-scheduled-string
  4262. "\\|" org-deadline-string
  4263. "\\|" org-closed-string
  4264. "\\)"
  4265. " *[[<]\\([^]>]+\\)[]>]")
  4266. org-maybe-keyword-time-regexp
  4267. (concat "\\(\\<\\(" org-scheduled-string
  4268. "\\|" org-deadline-string
  4269. "\\|" org-closed-string
  4270. "\\|" org-clock-string "\\)\\)?"
  4271. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4272. org-all-time-keywords
  4273. (mapcar (lambda (w) (substring w 0 -1))
  4274. (list org-scheduled-string org-deadline-string
  4275. org-clock-string org-closed-string))
  4276. )
  4277. (org-compute-latex-and-specials-regexp)
  4278. (org-set-font-lock-defaults))))
  4279. (defun org-file-contents (file &optional noerror)
  4280. "Return the contents of FILE, as a string."
  4281. (if (or (not file)
  4282. (not (file-readable-p file)))
  4283. (if noerror
  4284. (progn
  4285. (message "Cannot read file \"%s\"" file)
  4286. (ding) (sit-for 2)
  4287. "")
  4288. (error "Cannot read file \"%s\"" file))
  4289. (with-temp-buffer
  4290. (insert-file-contents file)
  4291. (buffer-string))))
  4292. (defun org-extract-log-state-settings (x)
  4293. "Extract the log state setting from a TODO keyword string.
  4294. This will extract info from a string like \"WAIT(w@/!)\"."
  4295. (let (kw key log1 log2)
  4296. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4297. (setq kw (match-string 1 x)
  4298. key (and (match-end 2) (match-string 2 x))
  4299. log1 (and (match-end 3) (match-string 3 x))
  4300. log2 (and (match-end 4) (match-string 4 x)))
  4301. (and (or log1 log2)
  4302. (list kw
  4303. (and log1 (if (equal log1 "!") 'time 'note))
  4304. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4305. (defun org-remove-keyword-keys (list)
  4306. "Remove a pair of parenthesis at the end of each string in LIST."
  4307. (mapcar (lambda (x)
  4308. (if (string-match "(.*)$" x)
  4309. (substring x 0 (match-beginning 0))
  4310. x))
  4311. list))
  4312. (defun org-assign-fast-keys (alist)
  4313. "Assign fast keys to a keyword-key alist.
  4314. Respect keys that are already there."
  4315. (let (new e (alt ?0))
  4316. (while (setq e (pop alist))
  4317. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4318. (cdr e)) ;; Key already assigned.
  4319. (push e new)
  4320. (let ((clist (string-to-list (downcase (car e))))
  4321. (used (append new alist)))
  4322. (when (= (car clist) ?@)
  4323. (pop clist))
  4324. (while (and clist (rassoc (car clist) used))
  4325. (pop clist))
  4326. (unless clist
  4327. (while (rassoc alt used)
  4328. (incf alt)))
  4329. (push (cons (car e) (or (car clist) alt)) new))))
  4330. (nreverse new)))
  4331. ;;; Some variables used in various places
  4332. (defvar org-window-configuration nil
  4333. "Used in various places to store a window configuration.")
  4334. (defvar org-selected-window nil
  4335. "Used in various places to store a window configuration.")
  4336. (defvar org-finish-function nil
  4337. "Function to be called when `C-c C-c' is used.
  4338. This is for getting out of special buffers like capture.")
  4339. ;; FIXME: Occasionally check by commenting these, to make sure
  4340. ;; no other functions uses these, forgetting to let-bind them.
  4341. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4342. (defvar org-last-state)
  4343. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4344. ;; Defined somewhere in this file, but used before definition.
  4345. (defvar org-entities) ;; defined in org-entities.el
  4346. (defvar org-struct-menu)
  4347. (defvar org-org-menu)
  4348. (defvar org-tbl-menu)
  4349. ;;;; Define the Org-mode
  4350. ;; We use a before-change function to check if a table might need
  4351. ;; an update.
  4352. (defvar org-table-may-need-update t
  4353. "Indicates that a table might need an update.
  4354. This variable is set by `org-before-change-function'.
  4355. `org-table-align' sets it back to nil.")
  4356. (defun org-before-change-function (beg end)
  4357. "Every change indicates that a table might need an update."
  4358. (setq org-table-may-need-update t))
  4359. (defvar org-mode-map)
  4360. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4361. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4362. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4363. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4364. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4365. (defvar org-table-buffer-is-an nil)
  4366. (defvar bidi-paragraph-direction)
  4367. (defvar buffer-face-mode-face)
  4368. (require 'outline)
  4369. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4370. (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"))
  4371. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4372. ;; Other stuff we need.
  4373. (require 'time-date)
  4374. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4375. (require 'easymenu)
  4376. (require 'overlay)
  4377. (require 'org-macs)
  4378. (require 'org-entities)
  4379. ;; (require 'org-compat) moved higher up in the file before it is first used
  4380. (require 'org-faces)
  4381. (require 'org-list)
  4382. (require 'org-pcomplete)
  4383. (require 'org-src)
  4384. (require 'org-footnote)
  4385. ;; babel
  4386. (require 'ob)
  4387. ;;;###autoload
  4388. (define-derived-mode org-mode outline-mode "Org"
  4389. "Outline-based notes management and organizer, alias
  4390. \"Carsten's outline-mode for keeping track of everything.\"
  4391. Org-mode develops organizational tasks around a NOTES file which
  4392. contains information about projects as plain text. Org-mode is
  4393. implemented on top of outline-mode, which is ideal to keep the content
  4394. of large files well structured. It supports ToDo items, deadlines and
  4395. time stamps, which magically appear in the diary listing of the Emacs
  4396. calendar. Tables are easily created with a built-in table editor.
  4397. Plain text URL-like links connect to websites, emails (VM), Usenet
  4398. messages (Gnus), BBDB entries, and any files related to the project.
  4399. For printing and sharing of notes, an Org-mode file (or a part of it)
  4400. can be exported as a structured ASCII or HTML file.
  4401. The following commands are available:
  4402. \\{org-mode-map}"
  4403. ;; Get rid of Outline menus, they are not needed
  4404. ;; Need to do this here because define-derived-mode sets up
  4405. ;; the keymap so late. Still, it is a waste to call this each time
  4406. ;; we switch another buffer into org-mode.
  4407. (if (featurep 'xemacs)
  4408. (when (boundp 'outline-mode-menu-heading)
  4409. ;; Assume this is Greg's port, it uses easymenu
  4410. (easy-menu-remove outline-mode-menu-heading)
  4411. (easy-menu-remove outline-mode-menu-show)
  4412. (easy-menu-remove outline-mode-menu-hide))
  4413. (define-key org-mode-map [menu-bar headings] 'undefined)
  4414. (define-key org-mode-map [menu-bar hide] 'undefined)
  4415. (define-key org-mode-map [menu-bar show] 'undefined))
  4416. (org-load-modules-maybe)
  4417. (easy-menu-add org-org-menu)
  4418. (easy-menu-add org-tbl-menu)
  4419. (org-install-agenda-files-menu)
  4420. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4421. (add-to-invisibility-spec '(org-cwidth))
  4422. (add-to-invisibility-spec '(org-hide-block . t))
  4423. (when (featurep 'xemacs)
  4424. (org-set-local 'line-move-ignore-invisible t))
  4425. (org-set-local 'outline-regexp org-outline-regexp)
  4426. (org-set-local 'outline-level 'org-outline-level)
  4427. (setq bidi-paragraph-direction 'left-to-right)
  4428. (when (and org-ellipsis
  4429. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4430. (fboundp 'make-glyph-code))
  4431. (unless org-display-table
  4432. (setq org-display-table (make-display-table)))
  4433. (set-display-table-slot
  4434. org-display-table 4
  4435. (vconcat (mapcar
  4436. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4437. org-ellipsis)))
  4438. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4439. (setq buffer-display-table org-display-table))
  4440. (org-set-regexps-and-options)
  4441. (when (and org-tag-faces (not org-tags-special-faces-re))
  4442. ;; tag faces set outside customize.... force initialization.
  4443. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4444. ;; Calc embedded
  4445. (org-set-local 'calc-embedded-open-mode "# ")
  4446. (modify-syntax-entry ?@ "w")
  4447. (modify-syntax-entry ?\" "\"")
  4448. (if org-startup-truncated (setq truncate-lines t))
  4449. (org-set-local 'font-lock-unfontify-region-function
  4450. 'org-unfontify-region)
  4451. ;; Activate before-change-function
  4452. (org-set-local 'org-table-may-need-update t)
  4453. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4454. 'local)
  4455. ;; Check for running clock before killing a buffer
  4456. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4457. ;; Initialize macros templates.
  4458. (org-macro-initialize-templates)
  4459. ;; Initialize radio targets.
  4460. (org-update-radio-target-regexp)
  4461. ;; Indentation.
  4462. (org-set-local 'indent-line-function 'org-indent-line)
  4463. (org-set-local 'indent-region-function 'org-indent-region)
  4464. ;; Filling and auto-filling.
  4465. (org-setup-filling)
  4466. ;; Comments.
  4467. (org-setup-comments-handling)
  4468. ;; Beginning/end of defun
  4469. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4470. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4471. ;; Next error for sparse trees
  4472. (org-set-local 'next-error-function 'org-occur-next-match)
  4473. ;; Make sure dependence stuff works reliably, even for users who set it
  4474. ;; too late :-(
  4475. (if org-enforce-todo-dependencies
  4476. (add-hook 'org-blocker-hook
  4477. 'org-block-todo-from-children-or-siblings-or-parent)
  4478. (remove-hook 'org-blocker-hook
  4479. 'org-block-todo-from-children-or-siblings-or-parent))
  4480. (if org-enforce-todo-checkbox-dependencies
  4481. (add-hook 'org-blocker-hook
  4482. 'org-block-todo-from-checkboxes)
  4483. (remove-hook 'org-blocker-hook
  4484. 'org-block-todo-from-checkboxes))
  4485. ;; Align options lines
  4486. (org-set-local
  4487. 'align-mode-rules-list
  4488. '((org-in-buffer-settings
  4489. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4490. (modes . '(org-mode)))))
  4491. ;; Imenu
  4492. (org-set-local 'imenu-create-index-function
  4493. 'org-imenu-get-tree)
  4494. ;; Make isearch reveal context
  4495. (if (or (featurep 'xemacs)
  4496. (not (boundp 'outline-isearch-open-invisible-function)))
  4497. ;; Emacs 21 and XEmacs make use of the hook
  4498. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4499. ;; Emacs 22 deals with this through a special variable
  4500. (org-set-local 'outline-isearch-open-invisible-function
  4501. (lambda (&rest ignore) (org-show-context 'isearch))))
  4502. ;; Turn on org-beamer-mode?
  4503. (and org-startup-with-beamer-mode (org-beamer-mode))
  4504. ;; Setup the pcomplete hooks
  4505. (set (make-local-variable 'pcomplete-command-completion-function)
  4506. 'org-pcomplete-initial)
  4507. (set (make-local-variable 'pcomplete-command-name-function)
  4508. 'org-command-at-point)
  4509. (set (make-local-variable 'pcomplete-default-completion-function)
  4510. 'ignore)
  4511. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4512. 'org-parse-arguments)
  4513. (set (make-local-variable 'pcomplete-termination-string) "")
  4514. (when (>= emacs-major-version 23)
  4515. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4516. ;; If empty file that did not turn on org-mode automatically, make it to.
  4517. (if (and org-insert-mode-line-in-empty-file
  4518. (org-called-interactively-p 'any)
  4519. (= (point-min) (point-max)))
  4520. (insert "# -*- mode: org -*-\n\n"))
  4521. (unless org-inhibit-startup
  4522. (when org-startup-align-all-tables
  4523. (let ((bmp (buffer-modified-p)))
  4524. (org-table-map-tables 'org-table-align 'quietly)
  4525. (set-buffer-modified-p bmp)))
  4526. (when org-startup-with-inline-images
  4527. (org-display-inline-images))
  4528. (when org-startup-indented
  4529. (require 'org-indent)
  4530. (org-indent-mode 1))
  4531. (unless org-inhibit-startup-visibility-stuff
  4532. (org-set-startup-visibility)))
  4533. ;; Try to set org-hide correctly
  4534. (set-face-foreground 'org-hide (face-background 'default nil 'default)))
  4535. (when (fboundp 'abbrev-table-put)
  4536. (abbrev-table-put org-mode-abbrev-table
  4537. :parents (list text-mode-abbrev-table)))
  4538. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4539. (defun org-current-time ()
  4540. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4541. (if (> (car org-time-stamp-rounding-minutes) 1)
  4542. (let ((r (car org-time-stamp-rounding-minutes))
  4543. (time (decode-time)))
  4544. (apply 'encode-time
  4545. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4546. (nthcdr 2 time))))
  4547. (current-time)))
  4548. (defun org-today ()
  4549. "Return today date, considering `org-extend-today-until'."
  4550. (time-to-days
  4551. (time-subtract (current-time)
  4552. (list 0 (* 3600 org-extend-today-until) 0))))
  4553. ;;;; Font-Lock stuff, including the activators
  4554. (defvar org-mouse-map (make-sparse-keymap))
  4555. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4556. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4557. (when org-mouse-1-follows-link
  4558. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4559. (when org-tab-follows-link
  4560. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4561. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4562. (require 'font-lock)
  4563. (defconst org-non-link-chars "]\t\n\r<>")
  4564. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4565. "shell" "elisp" "doi" "message"))
  4566. (defvar org-link-types-re nil
  4567. "Matches a link that has a url-like prefix like \"http:\"")
  4568. (defvar org-link-re-with-space nil
  4569. "Matches a link with spaces, optional angular brackets around it.")
  4570. (defvar org-link-re-with-space2 nil
  4571. "Matches a link with spaces, optional angular brackets around it.")
  4572. (defvar org-link-re-with-space3 nil
  4573. "Matches a link with spaces, only for internal part in bracket links.")
  4574. (defvar org-angle-link-re nil
  4575. "Matches link with angular brackets, spaces are allowed.")
  4576. (defvar org-plain-link-re nil
  4577. "Matches plain link, without spaces.")
  4578. (defvar org-bracket-link-regexp nil
  4579. "Matches a link in double brackets.")
  4580. (defvar org-bracket-link-analytic-regexp nil
  4581. "Regular expression used to analyze links.
  4582. Here is what the match groups contain after a match:
  4583. 1: http:
  4584. 2: http
  4585. 3: path
  4586. 4: [desc]
  4587. 5: desc")
  4588. (defvar org-bracket-link-analytic-regexp++ nil
  4589. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4590. (defvar org-any-link-re nil
  4591. "Regular expression matching any link.")
  4592. (defcustom org-match-sexp-depth 3
  4593. "Number of stacked braces for sub/superscript matching.
  4594. This has to be set before loading org.el to be effective."
  4595. :group 'org-export-translation ; ??????????????????????????/
  4596. :type 'integer)
  4597. (defun org-create-multibrace-regexp (left right n)
  4598. "Create a regular expression which will match a balanced sexp.
  4599. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4600. as single character strings.
  4601. The regexp returned will match the entire expression including the
  4602. delimiters. It will also define a single group which contains the
  4603. match except for the outermost delimiters. The maximum depth of
  4604. stacked delimiters is N. Escaping delimiters is not possible."
  4605. (let* ((nothing (concat "[^" left right "]*?"))
  4606. (or "\\|")
  4607. (re nothing)
  4608. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4609. (while (> n 1)
  4610. (setq n (1- n)
  4611. re (concat re or next)
  4612. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4613. (concat left "\\(" re "\\)" right)))
  4614. (defvar org-match-substring-regexp
  4615. (concat
  4616. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4617. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4618. "\\|"
  4619. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4620. "\\|"
  4621. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4622. "The regular expression matching a sub- or superscript.")
  4623. (defvar org-match-substring-with-braces-regexp
  4624. (concat
  4625. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4626. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4627. "\\)")
  4628. "The regular expression matching a sub- or superscript, forcing braces.")
  4629. (defun org-make-link-regexps ()
  4630. "Update the link regular expressions.
  4631. This should be called after the variable `org-link-types' has changed."
  4632. (setq org-link-types-re
  4633. (concat
  4634. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4635. org-link-re-with-space
  4636. (concat
  4637. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4638. "\\([^" org-non-link-chars " ]"
  4639. "[^" org-non-link-chars "]*"
  4640. "[^" org-non-link-chars " ]\\)>?")
  4641. org-link-re-with-space2
  4642. (concat
  4643. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4644. "\\([^" org-non-link-chars " ]"
  4645. "[^\t\n\r]*"
  4646. "[^" org-non-link-chars " ]\\)>?")
  4647. org-link-re-with-space3
  4648. (concat
  4649. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4650. "\\([^" org-non-link-chars " ]"
  4651. "[^\t\n\r]*\\)")
  4652. org-angle-link-re
  4653. (concat
  4654. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4655. "\\([^" org-non-link-chars " ]"
  4656. "[^" org-non-link-chars "]*"
  4657. "\\)>")
  4658. org-plain-link-re
  4659. (concat
  4660. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4661. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4662. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4663. org-bracket-link-regexp
  4664. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4665. org-bracket-link-analytic-regexp
  4666. (concat
  4667. "\\[\\["
  4668. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4669. "\\([^]]+\\)"
  4670. "\\]"
  4671. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4672. "\\]")
  4673. org-bracket-link-analytic-regexp++
  4674. (concat
  4675. "\\[\\["
  4676. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4677. "\\([^]]+\\)"
  4678. "\\]"
  4679. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4680. "\\]")
  4681. org-any-link-re
  4682. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4683. org-angle-link-re "\\)\\|\\("
  4684. org-plain-link-re "\\)")))
  4685. (org-make-link-regexps)
  4686. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4687. "Regular expression for fast time stamp matching.")
  4688. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4689. "Regular expression for fast time stamp matching.")
  4690. (defconst org-ts-regexp0
  4691. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4692. "Regular expression matching time strings for analysis.
  4693. This one does not require the space after the date, so it can be used
  4694. on a string that terminates immediately after the date.")
  4695. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4696. "Regular expression matching time strings for analysis.")
  4697. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4698. "Regular expression matching time stamps, with groups.")
  4699. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4700. "Regular expression matching time stamps (also [..]), with groups.")
  4701. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4702. "Regular expression matching a time stamp range.")
  4703. (defconst org-tr-regexp-both
  4704. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4705. "Regular expression matching a time stamp range.")
  4706. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4707. org-ts-regexp "\\)?")
  4708. "Regular expression matching a time stamp or time stamp range.")
  4709. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4710. org-ts-regexp-both "\\)?")
  4711. "Regular expression matching a time stamp or time stamp range.
  4712. The time stamps may be either active or inactive.")
  4713. (defvar org-emph-face nil)
  4714. (defun org-do-emphasis-faces (limit)
  4715. "Run through the buffer and add overlays to emphasized strings."
  4716. (let (rtn a)
  4717. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4718. (if (not (= (char-after (match-beginning 3))
  4719. (char-after (match-beginning 4))))
  4720. (progn
  4721. (setq rtn t)
  4722. (setq a (assoc (match-string 3) org-emphasis-alist))
  4723. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4724. 'face
  4725. (nth 1 a))
  4726. (and (nth 4 a)
  4727. (org-remove-flyspell-overlays-in
  4728. (match-beginning 0) (match-end 0)))
  4729. (add-text-properties (match-beginning 2) (match-end 2)
  4730. '(font-lock-multiline t org-emphasis t))
  4731. (when org-hide-emphasis-markers
  4732. (add-text-properties (match-end 4) (match-beginning 5)
  4733. '(invisible org-link))
  4734. (add-text-properties (match-beginning 3) (match-end 3)
  4735. '(invisible org-link)))))
  4736. (backward-char 1))
  4737. rtn))
  4738. (defun org-emphasize (&optional char)
  4739. "Insert or change an emphasis, i.e. a font like bold or italic.
  4740. If there is an active region, change that region to a new emphasis.
  4741. If there is no region, just insert the marker characters and position
  4742. the cursor between them.
  4743. CHAR should be either the marker character, or the first character of the
  4744. HTML tag associated with that emphasis. If CHAR is a space, the means
  4745. to remove the emphasis of the selected region.
  4746. If char is not given (for example in an interactive call) it
  4747. will be prompted for."
  4748. (interactive)
  4749. (let ((eal org-emphasis-alist) e det
  4750. (erc org-emphasis-regexp-components)
  4751. (prompt "")
  4752. (string "") beg end move tag c s)
  4753. (if (org-region-active-p)
  4754. (setq beg (region-beginning) end (region-end)
  4755. string (buffer-substring beg end))
  4756. (setq move t))
  4757. (while (setq e (pop eal))
  4758. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4759. c (aref tag 0))
  4760. (push (cons c (string-to-char (car e))) det)
  4761. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4762. (substring tag 1)))))
  4763. (setq det (nreverse det))
  4764. (unless char
  4765. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4766. (setq char (read-char-exclusive)))
  4767. (setq char (or (cdr (assoc char det)) char))
  4768. (if (equal char ?\ )
  4769. (setq s "" move nil)
  4770. (unless (assoc (char-to-string char) org-emphasis-alist)
  4771. (error "No such emphasis marker: \"%c\"" char))
  4772. (setq s (char-to-string char)))
  4773. (while (and (> (length string) 1)
  4774. (equal (substring string 0 1) (substring string -1))
  4775. (assoc (substring string 0 1) org-emphasis-alist))
  4776. (setq string (substring string 1 -1)))
  4777. (setq string (concat s string s))
  4778. (if beg (delete-region beg end))
  4779. (unless (or (bolp)
  4780. (string-match (concat "[" (nth 0 erc) "\n]")
  4781. (char-to-string (char-before (point)))))
  4782. (insert " "))
  4783. (unless (or (eobp)
  4784. (string-match (concat "[" (nth 1 erc) "\n]")
  4785. (char-to-string (char-after (point)))))
  4786. (insert " ") (backward-char 1))
  4787. (insert string)
  4788. (and move (backward-char 1))))
  4789. (defconst org-nonsticky-props
  4790. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4791. (defsubst org-rear-nonsticky-at (pos)
  4792. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4793. (defun org-activate-plain-links (limit)
  4794. "Run through the buffer and add overlays to links."
  4795. (catch 'exit
  4796. (let (f hl)
  4797. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4798. (not (org-in-src-block-p)))
  4799. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4800. (setq f (get-text-property (match-beginning 0) 'face))
  4801. (setq hl (org-match-string-no-properties 0))
  4802. (if (or (eq f 'org-tag)
  4803. (and (listp f) (memq 'org-tag f)))
  4804. nil
  4805. (add-text-properties (match-beginning 0) (match-end 0)
  4806. (list 'mouse-face 'highlight
  4807. 'face 'org-link
  4808. 'htmlize-link `(:uri ,hl)
  4809. 'keymap org-mouse-map))
  4810. (org-rear-nonsticky-at (match-end 0)))
  4811. t))))
  4812. (defun org-activate-code (limit)
  4813. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4814. (progn
  4815. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4816. (remove-text-properties (match-beginning 0) (match-end 0)
  4817. '(display t invisible t intangible t))
  4818. t)))
  4819. (defcustom org-src-fontify-natively nil
  4820. "When non-nil, fontify code in code blocks."
  4821. :type 'boolean
  4822. :version "24.1"
  4823. :group 'org-appearance
  4824. :group 'org-babel)
  4825. (defcustom org-allow-promoting-top-level-subtree nil
  4826. "When non-nil, allow promoting a top level subtree.
  4827. The leading star of the top level headline will be replaced
  4828. by a #."
  4829. :type 'boolean
  4830. :version "24.1"
  4831. :group 'org-appearance)
  4832. (defun org-fontify-meta-lines-and-blocks (limit)
  4833. (condition-case nil
  4834. (org-fontify-meta-lines-and-blocks-1 limit)
  4835. (error (message "org-mode fontification error"))))
  4836. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4837. "Fontify #+ lines and blocks, in the correct ways."
  4838. (let ((case-fold-search t))
  4839. (if (re-search-forward
  4840. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4841. limit t)
  4842. (let ((beg (match-beginning 0))
  4843. (block-start (match-end 0))
  4844. (block-end nil)
  4845. (lang (match-string 7))
  4846. (beg1 (line-beginning-position 2))
  4847. (dc1 (downcase (match-string 2)))
  4848. (dc3 (downcase (match-string 3)))
  4849. end end1 quoting block-type ovl)
  4850. (cond
  4851. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4852. ;; a single line of backend-specific content
  4853. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4854. (remove-text-properties (match-beginning 0) (match-end 0)
  4855. '(display t invisible t intangible t))
  4856. (add-text-properties (match-beginning 1) (match-end 3)
  4857. '(font-lock-fontified t face org-meta-line))
  4858. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4859. '(font-lock-fontified t face org-block))
  4860. ; for backend-specific code
  4861. t)
  4862. ((and (match-end 4) (equal dc3 "+begin"))
  4863. ;; Truly a block
  4864. (setq block-type (downcase (match-string 5))
  4865. quoting (member block-type org-protecting-blocks))
  4866. (when (re-search-forward
  4867. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4868. nil t) ;; on purpose, we look further than LIMIT
  4869. (setq end (min (point-max) (match-end 0))
  4870. end1 (min (point-max) (1- (match-beginning 0))))
  4871. (setq block-end (match-beginning 0))
  4872. (when quoting
  4873. (remove-text-properties beg end
  4874. '(display t invisible t intangible t)))
  4875. (add-text-properties
  4876. beg end
  4877. '(font-lock-fontified t font-lock-multiline t))
  4878. (add-text-properties beg beg1 '(face org-meta-line))
  4879. (add-text-properties end1 (min (point-max) (1+ end))
  4880. '(face org-meta-line)) ; for end_src
  4881. (cond
  4882. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4883. (org-src-font-lock-fontify-block lang block-start block-end)
  4884. ;; remove old background overlays
  4885. (mapc (lambda (ov)
  4886. (if (eq (overlay-get ov 'face) 'org-block-background)
  4887. (delete-overlay ov)))
  4888. (overlays-at (/ (+ beg1 block-end) 2)))
  4889. ;; add a background overlay
  4890. (setq ovl (make-overlay beg1 block-end))
  4891. (overlay-put ovl 'face 'org-block-background)
  4892. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4893. (quoting
  4894. (add-text-properties beg1 (min (point-max) (1+ end1))
  4895. '(face org-block))) ; end of source block
  4896. ((not org-fontify-quote-and-verse-blocks))
  4897. ((string= block-type "quote")
  4898. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4899. ((string= block-type "verse")
  4900. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4901. (add-text-properties beg beg1 '(face org-block-begin-line))
  4902. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4903. '(face org-block-end-line))
  4904. t))
  4905. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4906. (add-text-properties
  4907. beg (match-end 3)
  4908. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4909. '(font-lock-fontified t invisible t)
  4910. '(font-lock-fontified t face org-document-info-keyword)))
  4911. (add-text-properties
  4912. (match-beginning 6) (match-end 6)
  4913. (if (string-equal dc1 "+title:")
  4914. '(font-lock-fontified t face org-document-title)
  4915. '(font-lock-fontified t face org-document-info))))
  4916. ((or (equal dc1 "+results")
  4917. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4918. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4919. "+call:" "+header:" "+headers:" "+name:"))
  4920. (and (match-end 4) (equal dc3 "+attr")))
  4921. (add-text-properties
  4922. beg (match-end 0)
  4923. '(font-lock-fontified t face org-meta-line))
  4924. t)
  4925. ((member dc3 '(" " ""))
  4926. (add-text-properties
  4927. beg (match-end 0)
  4928. '(font-lock-fontified t face font-lock-comment-face)))
  4929. ((not (member (char-after beg) '(?\ ?\t)))
  4930. ;; just any other in-buffer setting, but not indented
  4931. (add-text-properties
  4932. beg (match-end 0)
  4933. '(font-lock-fontified t face org-meta-line))
  4934. t)
  4935. (t nil))))))
  4936. (defun org-activate-angle-links (limit)
  4937. "Run through the buffer and add overlays to links."
  4938. (if (and (re-search-forward org-angle-link-re limit t)
  4939. (not (org-in-src-block-p)))
  4940. (progn
  4941. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4942. (add-text-properties (match-beginning 0) (match-end 0)
  4943. (list 'mouse-face 'highlight
  4944. 'keymap org-mouse-map))
  4945. (org-rear-nonsticky-at (match-end 0))
  4946. t)))
  4947. (defun org-activate-footnote-links (limit)
  4948. "Run through the buffer and add overlays to footnotes."
  4949. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4950. (when fn
  4951. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4952. (org-remove-flyspell-overlays-in beg end)
  4953. (add-text-properties beg end
  4954. (list 'mouse-face 'highlight
  4955. 'keymap org-mouse-map
  4956. 'help-echo
  4957. (if (= (point-at-bol) beg)
  4958. "Footnote definition"
  4959. "Footnote reference")
  4960. 'font-lock-fontified t
  4961. 'font-lock-multiline t
  4962. 'face 'org-footnote))))))
  4963. (defun org-activate-bracket-links (limit)
  4964. "Run through the buffer and add overlays to bracketed links."
  4965. (if (and (re-search-forward org-bracket-link-regexp limit t)
  4966. (not (org-in-src-block-p)))
  4967. (let* ((hl (org-match-string-no-properties 1))
  4968. (help (concat "LINK: " hl))
  4969. ;; FIXME: Above we should remove the escapes. But that
  4970. ;; requires another match, protecting match data, a lot
  4971. ;; of overhead for font-lock.
  4972. (ip (org-maybe-intangible
  4973. (list 'invisible 'org-link
  4974. 'keymap org-mouse-map 'mouse-face 'highlight
  4975. 'font-lock-multiline t 'help-echo help
  4976. 'htmlize-link `(:uri ,hl))))
  4977. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4978. 'font-lock-multiline t 'help-echo help
  4979. 'htmlize-link `(:uri ,hl))))
  4980. ;; We need to remove the invisible property here. Table narrowing
  4981. ;; may have made some of this invisible.
  4982. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4983. (remove-text-properties (match-beginning 0) (match-end 0)
  4984. '(invisible nil))
  4985. (if (match-end 3)
  4986. (progn
  4987. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4988. (org-rear-nonsticky-at (match-beginning 3))
  4989. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4990. (org-rear-nonsticky-at (match-end 3))
  4991. (add-text-properties (match-end 3) (match-end 0) ip)
  4992. (org-rear-nonsticky-at (match-end 0)))
  4993. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4994. (org-rear-nonsticky-at (match-beginning 1))
  4995. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4996. (org-rear-nonsticky-at (match-end 1))
  4997. (add-text-properties (match-end 1) (match-end 0) ip)
  4998. (org-rear-nonsticky-at (match-end 0)))
  4999. t)))
  5000. (defun org-activate-dates (limit)
  5001. "Run through the buffer and add overlays to dates."
  5002. (if (re-search-forward org-tsr-regexp-both limit t)
  5003. (progn
  5004. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5005. (add-text-properties (match-beginning 0) (match-end 0)
  5006. (list 'mouse-face 'highlight
  5007. 'keymap org-mouse-map))
  5008. (org-rear-nonsticky-at (match-end 0))
  5009. (when org-display-custom-times
  5010. (if (match-end 3)
  5011. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5012. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5013. t)))
  5014. (defvar org-target-link-regexp nil
  5015. "Regular expression matching radio targets in plain text.")
  5016. (make-variable-buffer-local 'org-target-link-regexp)
  5017. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5018. "Regular expression matching a link target.")
  5019. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5020. "Regular expression matching a radio target.")
  5021. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5022. "Regular expression matching any target.")
  5023. (defun org-activate-target-links (limit)
  5024. "Run through the buffer and add overlays to target matches."
  5025. (when org-target-link-regexp
  5026. (let ((case-fold-search t))
  5027. (if (re-search-forward org-target-link-regexp limit t)
  5028. (progn
  5029. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5030. (add-text-properties (match-beginning 0) (match-end 0)
  5031. (list 'mouse-face 'highlight
  5032. 'keymap org-mouse-map
  5033. 'help-echo "Radio target link"
  5034. 'org-linked-text t))
  5035. (org-rear-nonsticky-at (match-end 0))
  5036. t)))))
  5037. (defun org-update-radio-target-regexp ()
  5038. "Find all radio targets in this file and update the regular expression."
  5039. (interactive)
  5040. (when (memq 'radio org-activate-links)
  5041. (setq org-target-link-regexp
  5042. (org-make-target-link-regexp (org-all-targets 'radio)))
  5043. (org-restart-font-lock)))
  5044. (defun org-hide-wide-columns (limit)
  5045. (let (s e)
  5046. (setq s (text-property-any (point) (or limit (point-max))
  5047. 'org-cwidth t))
  5048. (when s
  5049. (setq e (next-single-property-change s 'org-cwidth))
  5050. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5051. (goto-char e)
  5052. t)))
  5053. (defvar org-latex-and-specials-regexp nil
  5054. "Regular expression for highlighting export special stuff.")
  5055. (defvar org-match-substring-regexp)
  5056. (defvar org-match-substring-with-braces-regexp)
  5057. ;; This should be with the exporter code, but we also use if for font-locking
  5058. (defconst org-export-html-special-string-regexps
  5059. '(("\\\\-" . "&shy;")
  5060. ("---\\([^-]\\)" . "&mdash;\\1")
  5061. ("--\\([^-]\\)" . "&ndash;\\1")
  5062. ("\\.\\.\\." . "&hellip;"))
  5063. "Regular expressions for special string conversion.")
  5064. (defun org-compute-latex-and-specials-regexp ()
  5065. "Compute regular expression for stuff treated specially by exporters."
  5066. (if (not org-highlight-latex-fragments-and-specials)
  5067. (org-set-local 'org-latex-and-specials-regexp nil)
  5068. (require 'org-exp)
  5069. (let*
  5070. ((matchers (plist-get org-format-latex-options :matchers))
  5071. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5072. org-latex-regexps)))
  5073. (org-export-allow-BIND nil)
  5074. (options (org-combine-plists (org-default-export-plist)
  5075. (org-infile-export-plist)))
  5076. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5077. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5078. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5079. (org-export-html-expand (plist-get options :expand-quoted-html))
  5080. (org-export-with-special-strings (plist-get options :special-strings))
  5081. (re-sub
  5082. (cond
  5083. ((equal org-export-with-sub-superscripts '{})
  5084. (list org-match-substring-with-braces-regexp))
  5085. (org-export-with-sub-superscripts
  5086. (list org-match-substring-regexp))))
  5087. (re-latex
  5088. (if org-export-with-LaTeX-fragments
  5089. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5090. (re-macros
  5091. (if org-export-with-TeX-macros
  5092. (list (concat "\\\\"
  5093. (regexp-opt
  5094. (append
  5095. (delq nil
  5096. (mapcar 'car-safe
  5097. (append org-entities-user
  5098. org-entities)))
  5099. (if (boundp 'org-latex-entities)
  5100. (mapcar (lambda (x)
  5101. (or (car-safe x) x))
  5102. org-latex-entities)
  5103. nil))
  5104. 'words))) ; FIXME
  5105. ))
  5106. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5107. (re-special (if org-export-with-special-strings
  5108. (mapcar (lambda (x) (car x))
  5109. org-export-html-special-string-regexps)))
  5110. (re-rest
  5111. (delq nil
  5112. (list
  5113. (if org-export-html-expand "@<[^>\n]+>")
  5114. ))))
  5115. (org-set-local
  5116. 'org-latex-and-specials-regexp
  5117. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5118. re-rest) "\\|")))))
  5119. (defun org-do-latex-and-special-faces (limit)
  5120. "Run through the buffer and add overlays to links."
  5121. (when org-latex-and-specials-regexp
  5122. (let (rtn d)
  5123. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5124. limit t))
  5125. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5126. 'face))
  5127. '(org-code org-verbatim underline)))
  5128. (progn
  5129. (setq rtn t
  5130. d (cond ((member (char-after (1+ (match-beginning 0)))
  5131. '(?_ ?^)) 1)
  5132. (t 0)))
  5133. (font-lock-prepend-text-property
  5134. (+ d (match-beginning 0)) (match-end 0)
  5135. 'face 'org-latex-and-export-specials)
  5136. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5137. '(font-lock-multiline t)))))
  5138. rtn)))
  5139. (defun org-restart-font-lock ()
  5140. "Restart `font-lock-mode', to force refontification."
  5141. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5142. (font-lock-mode -1)
  5143. (font-lock-mode 1)))
  5144. (defun org-all-targets (&optional radio)
  5145. "Return a list of all targets in this file.
  5146. When optional argument RADIO is non-nil, only find radio
  5147. targets."
  5148. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5149. (save-excursion
  5150. (goto-char (point-min))
  5151. (while (re-search-forward re nil t)
  5152. ;; Make sure point is really within the object.
  5153. (backward-char)
  5154. (let ((obj (org-element-context)))
  5155. (when (memq (org-element-type obj) '(radio-target target))
  5156. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5157. rtn)))
  5158. (defun org-make-target-link-regexp (targets)
  5159. "Make regular expression matching all strings in TARGETS.
  5160. The regular expression finds the targets also if there is a line break
  5161. between words."
  5162. (and targets
  5163. (concat
  5164. "\\<\\("
  5165. (mapconcat
  5166. (lambda (x)
  5167. (setq x (regexp-quote x))
  5168. (while (string-match " +" x)
  5169. (setq x (replace-match "\\s-+" t t x)))
  5170. x)
  5171. targets
  5172. "\\|")
  5173. "\\)\\>")))
  5174. (defun org-activate-tags (limit)
  5175. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5176. (progn
  5177. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5178. (add-text-properties (match-beginning 1) (match-end 1)
  5179. (list 'mouse-face 'highlight
  5180. 'keymap org-mouse-map))
  5181. (org-rear-nonsticky-at (match-end 1))
  5182. t)))
  5183. (defun org-outline-level ()
  5184. "Compute the outline level of the heading at point.
  5185. If this is called at a normal headline, the level is the number of stars.
  5186. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5187. (save-excursion
  5188. (if (not (condition-case nil
  5189. (org-back-to-heading t)
  5190. (error nil)))
  5191. 0
  5192. (looking-at org-outline-regexp)
  5193. (1- (- (match-end 0) (match-beginning 0))))))
  5194. (defvar org-font-lock-keywords nil)
  5195. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5196. "Regular expression matching a property line.")
  5197. (defvar org-font-lock-hook nil
  5198. "Functions to be called for special font lock stuff.")
  5199. (defvar org-font-lock-set-keywords-hook nil
  5200. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5201. This is called after `org-font-lock-extra-keywords' is defined, but before
  5202. it is installed to be used by font lock. This can be useful if something
  5203. needs to be inserted at a specific position in the font-lock sequence.")
  5204. (defun org-font-lock-hook (limit)
  5205. "Run `org-font-lock-hook' within LIMIT."
  5206. (run-hook-with-args 'org-font-lock-hook limit))
  5207. (defun org-set-font-lock-defaults ()
  5208. "Set font lock defaults for the current buffer."
  5209. (let* ((em org-fontify-emphasized-text)
  5210. (lk org-activate-links)
  5211. (org-font-lock-extra-keywords
  5212. (list
  5213. ;; Call the hook
  5214. '(org-font-lock-hook)
  5215. ;; Headlines
  5216. `(,(if org-fontify-whole-heading-line
  5217. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5218. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5219. (1 (org-get-level-face 1))
  5220. (2 (org-get-level-face 2))
  5221. (3 (org-get-level-face 3)))
  5222. ;; Table lines
  5223. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5224. (1 'org-table t))
  5225. ;; Table internals
  5226. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5227. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5228. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5229. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5230. ;; Drawers
  5231. (list org-drawer-regexp '(0 'org-special-keyword t))
  5232. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5233. ;; Properties
  5234. (list org-property-re
  5235. '(1 'org-special-keyword t)
  5236. '(3 'org-property-value t))
  5237. ;; Links
  5238. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5239. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5240. (if (memq 'plain lk) '(org-activate-plain-links))
  5241. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5242. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5243. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5244. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5245. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5246. '(org-hide-wide-columns (0 nil append))
  5247. ;; TODO keyword
  5248. (list (format org-heading-keyword-regexp-format
  5249. org-todo-regexp)
  5250. '(2 (org-get-todo-face 2) t))
  5251. ;; DONE
  5252. (if org-fontify-done-headline
  5253. (list (format org-heading-keyword-regexp-format
  5254. (concat
  5255. "\\(?:"
  5256. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5257. "\\)"))
  5258. '(2 'org-headline-done t))
  5259. nil)
  5260. ;; Priorities
  5261. '(org-font-lock-add-priority-faces)
  5262. ;; Tags
  5263. '(org-font-lock-add-tag-faces)
  5264. ;; Special keywords
  5265. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5266. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5267. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5268. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5269. ;; Emphasis
  5270. (if em
  5271. (if (featurep 'xemacs)
  5272. '(org-do-emphasis-faces (0 nil append))
  5273. '(org-do-emphasis-faces)))
  5274. ;; Checkboxes
  5275. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5276. 1 'org-checkbox prepend)
  5277. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5278. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5279. (0 (org-get-checkbox-statistics-face) t)))
  5280. ;; Description list items
  5281. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5282. 1 'org-list-dt prepend)
  5283. ;; ARCHIVEd headings
  5284. (list (concat
  5285. org-outline-regexp-bol
  5286. "\\(.*:" org-archive-tag ":.*\\)")
  5287. '(1 'org-archived prepend))
  5288. ;; Specials
  5289. '(org-do-latex-and-special-faces)
  5290. '(org-fontify-entities)
  5291. '(org-raise-scripts)
  5292. ;; Code
  5293. '(org-activate-code (1 'org-code t))
  5294. ;; COMMENT
  5295. (list (format org-heading-keyword-regexp-format
  5296. (concat "\\("
  5297. org-comment-string "\\|" org-quote-string
  5298. "\\)"))
  5299. '(2 'org-special-keyword t))
  5300. ;; Blocks and meta lines
  5301. '(org-fontify-meta-lines-and-blocks)
  5302. )))
  5303. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5304. (run-hooks 'org-font-lock-set-keywords-hook)
  5305. ;; Now set the full font-lock-keywords
  5306. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5307. (org-set-local 'font-lock-defaults
  5308. '(org-font-lock-keywords t nil nil backward-paragraph))
  5309. (kill-local-variable 'font-lock-keywords) nil))
  5310. (defun org-toggle-pretty-entities ()
  5311. "Toggle the composition display of entities as UTF8 characters."
  5312. (interactive)
  5313. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5314. (org-restart-font-lock)
  5315. (if org-pretty-entities
  5316. (message "Entities are displayed as UTF8 characters")
  5317. (save-restriction
  5318. (widen)
  5319. (org-decompose-region (point-min) (point-max))
  5320. (message "Entities are displayed plain"))))
  5321. (defvar org-custom-properties-overlays nil
  5322. "List of overlays used for custom properties.")
  5323. (make-variable-buffer-local 'org-custom-properties-overlays)
  5324. (defun org-toggle-custom-properties-visibility ()
  5325. "Display or hide properties in `org-custom-properties'."
  5326. (interactive)
  5327. (if org-custom-properties-overlays
  5328. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5329. (setq org-custom-properties-overlays nil))
  5330. (unless (not org-custom-properties)
  5331. (save-excursion
  5332. (save-restriction
  5333. (widen)
  5334. (goto-char (point-min))
  5335. (while (re-search-forward org-property-re nil t)
  5336. (mapc (lambda(p)
  5337. (when (equal p (substring (match-string 1) 1 -1))
  5338. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5339. (overlay-put o 'invisible t)
  5340. (overlay-put o 'org-custom-property t)
  5341. (push o org-custom-properties-overlays))))
  5342. org-custom-properties)))))))
  5343. (defun org-fontify-entities (limit)
  5344. "Find an entity to fontify."
  5345. (let (ee)
  5346. (when org-pretty-entities
  5347. (catch 'match
  5348. (while (re-search-forward
  5349. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5350. limit t)
  5351. (if (and (not (org-in-indented-comment-line))
  5352. (setq ee (org-entity-get (match-string 1)))
  5353. (= (length (nth 6 ee)) 1))
  5354. (let*
  5355. ((end (if (equal (match-string 2) "{}")
  5356. (match-end 2)
  5357. (match-end 1))))
  5358. (add-text-properties
  5359. (match-beginning 0) end
  5360. (list 'font-lock-fontified t))
  5361. (compose-region (match-beginning 0) end
  5362. (nth 6 ee) nil)
  5363. (backward-char 1)
  5364. (throw 'match t))))
  5365. nil))))
  5366. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5367. "Fontify string S like in Org-mode."
  5368. (with-temp-buffer
  5369. (insert s)
  5370. (let ((org-odd-levels-only odd-levels))
  5371. (org-mode)
  5372. (font-lock-fontify-buffer)
  5373. (buffer-string))))
  5374. (defvar org-m nil)
  5375. (defvar org-l nil)
  5376. (defvar org-f nil)
  5377. (defun org-get-level-face (n)
  5378. "Get the right face for match N in font-lock matching of headlines."
  5379. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5380. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5381. (if org-cycle-level-faces
  5382. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5383. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5384. (cond
  5385. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5386. ((eq n 2) org-f)
  5387. (t (if org-level-color-stars-only nil org-f))))
  5388. (defun org-get-todo-face (kwd)
  5389. "Get the right face for a TODO keyword KWD.
  5390. If KWD is a number, get the corresponding match group."
  5391. (if (numberp kwd) (setq kwd (match-string kwd)))
  5392. (or (org-face-from-face-or-color
  5393. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5394. (and (member kwd org-done-keywords) 'org-done)
  5395. 'org-todo))
  5396. (defun org-face-from-face-or-color (context inherit face-or-color)
  5397. "Create a face list that inherits INHERIT, but sets the foreground color.
  5398. When FACE-OR-COLOR is not a string, just return it."
  5399. (if (stringp face-or-color)
  5400. (list :inherit inherit
  5401. (cdr (assoc context org-faces-easy-properties))
  5402. face-or-color)
  5403. face-or-color))
  5404. (defun org-font-lock-add-tag-faces (limit)
  5405. "Add the special tag faces."
  5406. (when (and org-tag-faces org-tags-special-faces-re)
  5407. (while (re-search-forward org-tags-special-faces-re limit t)
  5408. (add-text-properties (match-beginning 1) (match-end 1)
  5409. (list 'face (org-get-tag-face 1)
  5410. 'font-lock-fontified t))
  5411. (backward-char 1))))
  5412. (defun org-font-lock-add-priority-faces (limit)
  5413. "Add the special priority faces."
  5414. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5415. (when (save-match-data (org-at-heading-p))
  5416. (add-text-properties
  5417. (match-beginning 0) (match-end 0)
  5418. (list 'face (or (org-face-from-face-or-color
  5419. 'priority 'org-special-keyword
  5420. (cdr (assoc (char-after (match-beginning 1))
  5421. org-priority-faces)))
  5422. 'org-special-keyword)
  5423. 'font-lock-fontified t)))))
  5424. (defun org-get-tag-face (kwd)
  5425. "Get the right face for a TODO keyword KWD.
  5426. If KWD is a number, get the corresponding match group."
  5427. (if (numberp kwd) (setq kwd (match-string kwd)))
  5428. (or (org-face-from-face-or-color
  5429. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5430. 'org-tag))
  5431. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5432. "Remove fontification and activation overlays from links."
  5433. (font-lock-default-unfontify-region beg end)
  5434. (let* ((buffer-undo-list t)
  5435. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5436. (inhibit-modification-hooks t)
  5437. deactivate-mark buffer-file-name buffer-file-truename)
  5438. (org-decompose-region beg end)
  5439. (remove-text-properties beg end
  5440. '(mouse-face t keymap t org-linked-text t
  5441. invisible t intangible t
  5442. org-no-flyspell t org-emphasis t))
  5443. (org-remove-font-lock-display-properties beg end)))
  5444. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5445. ((raise 0.3) (height 0.7))
  5446. ((raise -0.5))
  5447. ((raise 0.5)))
  5448. "Display properties for showing superscripts and subscripts.")
  5449. (defun org-remove-font-lock-display-properties (beg end)
  5450. "Remove specific display properties that have been added by font lock.
  5451. The will remove the raise properties that are used to show superscripts
  5452. and subscripts."
  5453. (let (next prop)
  5454. (while (< beg end)
  5455. (setq next (next-single-property-change beg 'display nil end)
  5456. prop (get-text-property beg 'display))
  5457. (if (member prop org-script-display)
  5458. (put-text-property beg next 'display nil))
  5459. (setq beg next))))
  5460. (defun org-raise-scripts (limit)
  5461. "Add raise properties to sub/superscripts."
  5462. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5463. (if (re-search-forward
  5464. (if (eq org-use-sub-superscripts t)
  5465. org-match-substring-regexp
  5466. org-match-substring-with-braces-regexp)
  5467. limit t)
  5468. (let* ((pos (point)) table-p comment-p
  5469. (mpos (match-beginning 3))
  5470. (emph-p (get-text-property mpos 'org-emphasis))
  5471. (link-p (get-text-property mpos 'mouse-face))
  5472. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5473. (goto-char (point-at-bol))
  5474. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5475. comment-p (org-looking-at-p "[ \t]*#"))
  5476. (goto-char pos)
  5477. ;; FIXME: Should we go back one character here, for a_b^c
  5478. ;; (goto-char (1- pos)) ;????????????????????
  5479. (if (or comment-p emph-p link-p keyw-p)
  5480. t
  5481. (put-text-property (match-beginning 3) (match-end 0)
  5482. 'display
  5483. (if (equal (char-after (match-beginning 2)) ?^)
  5484. (nth (if table-p 3 1) org-script-display)
  5485. (nth (if table-p 2 0) org-script-display)))
  5486. (add-text-properties (match-beginning 2) (match-end 2)
  5487. (list 'invisible t
  5488. 'org-dwidth t 'org-dwidth-n 1))
  5489. (if (and (eq (char-after (match-beginning 3)) ?{)
  5490. (eq (char-before (match-end 3)) ?}))
  5491. (progn
  5492. (add-text-properties
  5493. (match-beginning 3) (1+ (match-beginning 3))
  5494. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5495. (add-text-properties
  5496. (1- (match-end 3)) (match-end 3)
  5497. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5498. t)))))
  5499. ;;;; Visibility cycling, including org-goto and indirect buffer
  5500. ;;; Cycling
  5501. (defvar org-cycle-global-status nil)
  5502. (make-variable-buffer-local 'org-cycle-global-status)
  5503. (defvar org-cycle-subtree-status nil)
  5504. (make-variable-buffer-local 'org-cycle-subtree-status)
  5505. (defvar org-inlinetask-min-level)
  5506. ;;;###autoload
  5507. (defun org-cycle (&optional arg)
  5508. "TAB-action and visibility cycling for Org-mode.
  5509. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5510. is outline visibility cycling, but it also invokes other actions
  5511. in special contexts.
  5512. - When this function is called with a prefix argument, rotate the entire
  5513. buffer through 3 states (global cycling)
  5514. 1. OVERVIEW: Show only top-level headlines.
  5515. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5516. 3. SHOW ALL: Show everything.
  5517. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5518. determined by the variable `org-startup-folded', and by any VISIBILITY
  5519. properties in the buffer.
  5520. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5521. including any drawers.
  5522. - When inside a table, re-align the table and move to the next field.
  5523. - When point is at the beginning of a headline, rotate the subtree started
  5524. by this line through 3 different states (local cycling)
  5525. 1. FOLDED: Only the main headline is shown.
  5526. 2. CHILDREN: The main headline and the direct children are shown.
  5527. From this state, you can move to one of the children
  5528. and zoom in further.
  5529. 3. SUBTREE: Show the entire subtree, including body text.
  5530. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5531. - When point is at the beginning of an empty headline and the variable
  5532. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5533. of the headline by demoting and promoting it to likely levels. This
  5534. speeds up creation document structure by pressing TAB once or several
  5535. times right after creating a new headline.
  5536. - When there is a numeric prefix, go up to a heading with level ARG, do
  5537. a `show-subtree' and return to the previous cursor position. If ARG
  5538. is negative, go up that many levels.
  5539. - When point is not at the beginning of a headline, execute the global
  5540. binding for TAB, which is re-indenting the line. See the option
  5541. `org-cycle-emulate-tab' for details.
  5542. - Special case: if point is at the beginning of the buffer and there is
  5543. no headline in line 1, this function will act as if called with prefix arg
  5544. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5545. But only if also the variable `org-cycle-global-at-bob' is t."
  5546. (interactive "P")
  5547. (org-load-modules-maybe)
  5548. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5549. (and org-cycle-level-after-item/entry-creation
  5550. (or (org-cycle-level)
  5551. (org-cycle-item-indentation))))
  5552. (let* (message-log-max ; Don't populate the *Messages* buffer
  5553. (limit-level
  5554. (or org-cycle-max-level
  5555. (and (boundp 'org-inlinetask-min-level)
  5556. org-inlinetask-min-level
  5557. (1- org-inlinetask-min-level))))
  5558. (nstars (and limit-level
  5559. (if org-odd-levels-only
  5560. (and limit-level (1- (* limit-level 2)))
  5561. limit-level)))
  5562. (org-outline-regexp
  5563. (if (not (derived-mode-p 'org-mode))
  5564. outline-regexp
  5565. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5566. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5567. (not (looking-at org-outline-regexp))))
  5568. (org-cycle-hook
  5569. (if bob-special
  5570. (delq 'org-optimize-window-after-visibility-change
  5571. (copy-sequence org-cycle-hook))
  5572. org-cycle-hook))
  5573. (pos (point)))
  5574. (if (or bob-special (equal arg '(4)))
  5575. ;; special case: use global cycling
  5576. (setq arg t))
  5577. (cond
  5578. ((equal arg '(16))
  5579. (setq last-command 'dummy)
  5580. (org-set-startup-visibility)
  5581. (message "Startup visibility, plus VISIBILITY properties"))
  5582. ((equal arg '(64))
  5583. (show-all)
  5584. (message "Entire buffer visible, including drawers"))
  5585. ;; Table: enter it or move to the next field.
  5586. ((org-at-table-p 'any)
  5587. (if (org-at-table.el-p)
  5588. (message "Use C-c ' to edit table.el tables")
  5589. (if arg (org-table-edit-field t)
  5590. (org-table-justify-field-maybe)
  5591. (call-interactively 'org-table-next-field))))
  5592. ((run-hook-with-args-until-success
  5593. 'org-tab-after-check-for-table-hook))
  5594. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5595. ((eq arg t) (org-cycle-internal-global))
  5596. ;; Drawers: delegate to `org-flag-drawer'.
  5597. ((and org-drawers org-drawer-regexp
  5598. (save-excursion
  5599. (beginning-of-line 1)
  5600. (looking-at org-drawer-regexp)))
  5601. (org-flag-drawer ; toggle block visibility
  5602. (not (get-char-property (match-end 0) 'invisible))))
  5603. ;; Show-subtree, ARG levels up from here.
  5604. ((integerp arg)
  5605. (save-excursion
  5606. (org-back-to-heading)
  5607. (outline-up-heading (if (< arg 0) (- arg)
  5608. (- (funcall outline-level) arg)))
  5609. (org-show-subtree)))
  5610. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5611. ((and (featurep 'org-inlinetask)
  5612. (org-inlinetask-at-task-p)
  5613. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5614. (org-inlinetask-toggle-visibility))
  5615. ((org-try-cdlatex-tab))
  5616. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5617. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5618. (save-excursion (beginning-of-line 1)
  5619. (looking-at org-outline-regexp)))
  5620. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5621. (org-cycle-internal-local))
  5622. ;; From there: TAB emulation and template completion.
  5623. (buffer-read-only (org-back-to-heading))
  5624. ((run-hook-with-args-until-success
  5625. 'org-tab-after-check-for-cycling-hook))
  5626. ((org-try-structure-completion))
  5627. ((run-hook-with-args-until-success
  5628. 'org-tab-before-tab-emulation-hook))
  5629. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5630. (or (not (bolp))
  5631. (not (looking-at org-outline-regexp))))
  5632. (call-interactively (global-key-binding "\t")))
  5633. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5634. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5635. (or (and (eq org-cycle-emulate-tab 'white)
  5636. (= (match-end 0) (point-at-eol)))
  5637. (and (eq org-cycle-emulate-tab 'whitestart)
  5638. (>= (match-end 0) pos))))
  5639. t
  5640. (eq org-cycle-emulate-tab t))
  5641. (call-interactively (global-key-binding "\t")))
  5642. (t (save-excursion
  5643. (org-back-to-heading)
  5644. (org-cycle)))))))
  5645. (defun org-cycle-internal-global ()
  5646. "Do the global cycling action."
  5647. ;; Hack to avoid display of messages for .org attachments in Gnus
  5648. (let (message-log-max ; Don't populate the *Messages* buffer
  5649. (ga (string-match "\\*fontification" (buffer-name))))
  5650. (cond
  5651. ((and (eq last-command this-command)
  5652. (eq org-cycle-global-status 'overview))
  5653. ;; We just created the overview - now do table of contents
  5654. ;; This can be slow in very large buffers, so indicate action
  5655. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5656. (unless ga (message "CONTENTS..."))
  5657. (org-content)
  5658. (unless ga (message "CONTENTS...done"))
  5659. (setq org-cycle-global-status 'contents)
  5660. (run-hook-with-args 'org-cycle-hook 'contents))
  5661. ((and (eq last-command this-command)
  5662. (eq org-cycle-global-status 'contents))
  5663. ;; We just showed the table of contents - now show everything
  5664. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5665. (show-all)
  5666. (unless ga (message "SHOW ALL"))
  5667. (setq org-cycle-global-status 'all)
  5668. (run-hook-with-args 'org-cycle-hook 'all))
  5669. (t
  5670. ;; Default action: go to overview
  5671. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5672. (org-overview)
  5673. (unless ga (message "OVERVIEW"))
  5674. (setq org-cycle-global-status 'overview)
  5675. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5676. (defun org-cycle-internal-local ()
  5677. "Do the local cycling action."
  5678. (let (message-log-max ; Don't populate the *Messages* buffer
  5679. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5680. ;; First, determine end of headline (EOH), end of subtree or item
  5681. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5682. (save-excursion
  5683. (if (org-at-item-p)
  5684. (progn
  5685. (beginning-of-line)
  5686. (setq struct (org-list-struct))
  5687. (setq eoh (point-at-eol))
  5688. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5689. (setq has-children (org-list-has-child-p (point) struct)))
  5690. (org-back-to-heading)
  5691. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5692. (setq eos (save-excursion
  5693. (org-end-of-subtree t)
  5694. (unless (eobp)
  5695. (skip-chars-forward " \t\n"))
  5696. (if (eobp) (point) (1- (point)))))
  5697. (setq has-children
  5698. (or (save-excursion
  5699. (let ((level (funcall outline-level)))
  5700. (outline-next-heading)
  5701. (and (org-at-heading-p t)
  5702. (> (funcall outline-level) level))))
  5703. (save-excursion
  5704. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5705. ;; Determine end invisible part of buffer (EOL)
  5706. (beginning-of-line 2)
  5707. ;; XEmacs doesn't have `next-single-char-property-change'
  5708. (if (featurep 'xemacs)
  5709. (while (and (not (eobp)) ;; this is like `next-line'
  5710. (get-char-property (1- (point)) 'invisible))
  5711. (beginning-of-line 2))
  5712. (while (and (not (eobp)) ;; this is like `next-line'
  5713. (get-char-property (1- (point)) 'invisible))
  5714. (goto-char (next-single-char-property-change (point) 'invisible))
  5715. (and (eolp) (beginning-of-line 2))))
  5716. (setq eol (point)))
  5717. ;; Find out what to do next and set `this-command'
  5718. (cond
  5719. ((= eos eoh)
  5720. ;; Nothing is hidden behind this heading
  5721. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5722. (message "EMPTY ENTRY")
  5723. (setq org-cycle-subtree-status nil)
  5724. (save-excursion
  5725. (goto-char eos)
  5726. (outline-next-heading)
  5727. (if (outline-invisible-p) (org-flag-heading nil))))
  5728. ((and (or (>= eol eos)
  5729. (not (string-match "\\S-" (buffer-substring eol eos))))
  5730. (or has-children
  5731. (not (setq children-skipped
  5732. org-cycle-skip-children-state-if-no-children))))
  5733. ;; Entire subtree is hidden in one line: children view
  5734. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5735. (if (org-at-item-p)
  5736. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5737. (org-show-entry)
  5738. (org-with-limited-levels (show-children))
  5739. ;; FIXME: This slows down the func way too much.
  5740. ;; How keep drawers hidden in subtree anyway?
  5741. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5742. ;; (org-cycle-hide-drawers 'subtree))
  5743. ;; Fold every list in subtree to top-level items.
  5744. (when (eq org-cycle-include-plain-lists 'integrate)
  5745. (save-excursion
  5746. (org-back-to-heading)
  5747. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5748. (beginning-of-line 1)
  5749. (let* ((struct (org-list-struct))
  5750. (prevs (org-list-prevs-alist struct))
  5751. (end (org-list-get-bottom-point struct)))
  5752. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5753. (org-list-get-all-items (point) struct prevs))
  5754. (goto-char end))))))
  5755. (message "CHILDREN")
  5756. (save-excursion
  5757. (goto-char eos)
  5758. (outline-next-heading)
  5759. (if (outline-invisible-p) (org-flag-heading nil)))
  5760. (setq org-cycle-subtree-status 'children)
  5761. (run-hook-with-args 'org-cycle-hook 'children))
  5762. ((or children-skipped
  5763. (and (eq last-command this-command)
  5764. (eq org-cycle-subtree-status 'children)))
  5765. ;; We just showed the children, or no children are there,
  5766. ;; now show everything.
  5767. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5768. (outline-flag-region eoh eos nil)
  5769. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5770. (setq org-cycle-subtree-status 'subtree)
  5771. (run-hook-with-args 'org-cycle-hook 'subtree))
  5772. (t
  5773. ;; Default action: hide the subtree.
  5774. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5775. (outline-flag-region eoh eos t)
  5776. (message "FOLDED")
  5777. (setq org-cycle-subtree-status 'folded)
  5778. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5779. ;;;###autoload
  5780. (defun org-global-cycle (&optional arg)
  5781. "Cycle the global visibility. For details see `org-cycle'.
  5782. With \\[universal-argument] prefix arg, switch to startup visibility.
  5783. With a numeric prefix, show all headlines up to that level."
  5784. (interactive "P")
  5785. (let ((org-cycle-include-plain-lists
  5786. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5787. (cond
  5788. ((integerp arg)
  5789. (show-all)
  5790. (hide-sublevels arg)
  5791. (setq org-cycle-global-status 'contents))
  5792. ((equal arg '(4))
  5793. (org-set-startup-visibility)
  5794. (message "Startup visibility, plus VISIBILITY properties."))
  5795. (t
  5796. (org-cycle '(4))))))
  5797. (defun org-set-startup-visibility ()
  5798. "Set the visibility required by startup options and properties."
  5799. (cond
  5800. ((eq org-startup-folded t)
  5801. (org-cycle '(4)))
  5802. ((eq org-startup-folded 'content)
  5803. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5804. (org-cycle '(4)) (org-cycle '(4)))))
  5805. (unless (eq org-startup-folded 'showeverything)
  5806. (if org-hide-block-startup (org-hide-block-all))
  5807. (org-set-visibility-according-to-property 'no-cleanup)
  5808. (org-cycle-hide-archived-subtrees 'all)
  5809. (org-cycle-hide-drawers 'all)
  5810. (org-cycle-show-empty-lines t)))
  5811. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5812. "Switch subtree visibilities according to :VISIBILITY: property."
  5813. (interactive)
  5814. (let (org-show-entry-below state)
  5815. (save-excursion
  5816. (goto-char (point-min))
  5817. (while (re-search-forward
  5818. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5819. nil t)
  5820. (setq state (match-string 1))
  5821. (save-excursion
  5822. (org-back-to-heading t)
  5823. (hide-subtree)
  5824. (org-reveal)
  5825. (cond
  5826. ((equal state '("fold" "folded"))
  5827. (hide-subtree))
  5828. ((equal state "children")
  5829. (org-show-hidden-entry)
  5830. (show-children))
  5831. ((equal state "content")
  5832. (save-excursion
  5833. (save-restriction
  5834. (org-narrow-to-subtree)
  5835. (org-content))))
  5836. ((member state '("all" "showall"))
  5837. (show-subtree)))))
  5838. (unless no-cleanup
  5839. (org-cycle-hide-archived-subtrees 'all)
  5840. (org-cycle-hide-drawers 'all)
  5841. (org-cycle-show-empty-lines 'all)))))
  5842. ;; This function uses outline-regexp instead of the more fundamental
  5843. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5844. ;; buffers, where outline-regexp is needed.
  5845. (defun org-overview ()
  5846. "Switch to overview mode, showing only top-level headlines.
  5847. Really, this shows all headlines with level equal or greater than the level
  5848. of the first headline in the buffer. This is important, because if the
  5849. first headline is not level one, then (hide-sublevels 1) gives confusing
  5850. results."
  5851. (interactive)
  5852. (let ((level (save-excursion
  5853. (goto-char (point-min))
  5854. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5855. (progn
  5856. (goto-char (match-beginning 0))
  5857. (funcall outline-level))))))
  5858. (and level (hide-sublevels level))))
  5859. (defun org-content (&optional arg)
  5860. "Show all headlines in the buffer, like a table of contents.
  5861. With numerical argument N, show content up to level N."
  5862. (interactive "P")
  5863. (save-excursion
  5864. ;; Visit all headings and show their offspring
  5865. (and (integerp arg) (org-overview))
  5866. (goto-char (point-max))
  5867. (catch 'exit
  5868. (while (and (progn (condition-case nil
  5869. (outline-previous-visible-heading 1)
  5870. (error (goto-char (point-min))))
  5871. t)
  5872. (looking-at org-outline-regexp))
  5873. (if (integerp arg)
  5874. (show-children (1- arg))
  5875. (show-branches))
  5876. (if (bobp) (throw 'exit nil))))))
  5877. (defun org-optimize-window-after-visibility-change (state)
  5878. "Adjust the window after a change in outline visibility.
  5879. This function is the default value of the hook `org-cycle-hook'."
  5880. (when (get-buffer-window (current-buffer))
  5881. (cond
  5882. ((eq state 'content) nil)
  5883. ((eq state 'all) nil)
  5884. ((eq state 'folded) nil)
  5885. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5886. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5887. (defun org-remove-empty-overlays-at (pos)
  5888. "Remove outline overlays that do not contain non-white stuff."
  5889. (mapc
  5890. (lambda (o)
  5891. (and (eq 'outline (overlay-get o 'invisible))
  5892. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5893. (overlay-end o))))
  5894. (delete-overlay o)))
  5895. (overlays-at pos)))
  5896. (defun org-clean-visibility-after-subtree-move ()
  5897. "Fix visibility issues after moving a subtree."
  5898. ;; First, find a reasonable region to look at:
  5899. ;; Start two siblings above, end three below
  5900. (let* ((beg (save-excursion
  5901. (and (org-get-last-sibling)
  5902. (org-get-last-sibling))
  5903. (point)))
  5904. (end (save-excursion
  5905. (and (org-get-next-sibling)
  5906. (org-get-next-sibling)
  5907. (org-get-next-sibling))
  5908. (if (org-at-heading-p)
  5909. (point-at-eol)
  5910. (point))))
  5911. (level (looking-at "\\*+"))
  5912. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5913. (save-excursion
  5914. (save-restriction
  5915. (narrow-to-region beg end)
  5916. (when re
  5917. ;; Properly fold already folded siblings
  5918. (goto-char (point-min))
  5919. (while (re-search-forward re nil t)
  5920. (if (and (not (outline-invisible-p))
  5921. (save-excursion
  5922. (goto-char (point-at-eol)) (outline-invisible-p)))
  5923. (hide-entry))))
  5924. (org-cycle-show-empty-lines 'overview)
  5925. (org-cycle-hide-drawers 'overview)))))
  5926. (defun org-cycle-show-empty-lines (state)
  5927. "Show empty lines above all visible headlines.
  5928. The region to be covered depends on STATE when called through
  5929. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5930. entire buffer covered. Note that an empty line is only shown if there
  5931. are at least `org-cycle-separator-lines' empty lines before the headline."
  5932. (when (not (= org-cycle-separator-lines 0))
  5933. (save-excursion
  5934. (let* ((n (abs org-cycle-separator-lines))
  5935. (re (cond
  5936. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5937. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5938. (t (let ((ns (number-to-string (- n 2))))
  5939. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5940. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5941. beg end b e)
  5942. (cond
  5943. ((memq state '(overview contents t))
  5944. (setq beg (point-min) end (point-max)))
  5945. ((memq state '(children folded))
  5946. (setq beg (point) end (progn (org-end-of-subtree t t)
  5947. (beginning-of-line 2)
  5948. (point)))))
  5949. (when beg
  5950. (goto-char beg)
  5951. (while (re-search-forward re end t)
  5952. (unless (get-char-property (match-end 1) 'invisible)
  5953. (setq e (match-end 1))
  5954. (if (< org-cycle-separator-lines 0)
  5955. (setq b (save-excursion
  5956. (goto-char (match-beginning 0))
  5957. (org-back-over-empty-lines)
  5958. (if (save-excursion
  5959. (goto-char (max (point-min) (1- (point))))
  5960. (org-at-heading-p))
  5961. (1- (point))
  5962. (point))))
  5963. (setq b (match-beginning 1)))
  5964. (outline-flag-region b e nil)))))))
  5965. ;; Never hide empty lines at the end of the file.
  5966. (save-excursion
  5967. (goto-char (point-max))
  5968. (outline-previous-heading)
  5969. (outline-end-of-heading)
  5970. (if (and (looking-at "[ \t\n]+")
  5971. (= (match-end 0) (point-max)))
  5972. (outline-flag-region (point) (match-end 0) nil))))
  5973. (defun org-show-empty-lines-in-parent ()
  5974. "Move to the parent and re-show empty lines before visible headlines."
  5975. (save-excursion
  5976. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5977. (org-cycle-show-empty-lines context))))
  5978. (defun org-files-list ()
  5979. "Return `org-agenda-files' list, plus all open org-mode files.
  5980. This is useful for operations that need to scan all of a user's
  5981. open and agenda-wise Org files."
  5982. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5983. (dolist (buf (buffer-list))
  5984. (with-current-buffer buf
  5985. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5986. (let ((file (expand-file-name (buffer-file-name))))
  5987. (unless (member file files)
  5988. (push file files))))))
  5989. files))
  5990. (defsubst org-entry-beginning-position ()
  5991. "Return the beginning position of the current entry."
  5992. (save-excursion (outline-back-to-heading t) (point)))
  5993. (defsubst org-entry-end-position ()
  5994. "Return the end position of the current entry."
  5995. (save-excursion (outline-next-heading) (point)))
  5996. (defun org-cycle-hide-drawers (state)
  5997. "Re-hide all drawers after a visibility state change."
  5998. (when (and (derived-mode-p 'org-mode)
  5999. (not (memq state '(overview folded contents))))
  6000. (save-excursion
  6001. (let* ((globalp (memq state '(contents all)))
  6002. (beg (if globalp (point-min) (point)))
  6003. (end (if globalp (point-max)
  6004. (if (eq state 'children)
  6005. (save-excursion (outline-next-heading) (point))
  6006. (org-end-of-subtree t)))))
  6007. (goto-char beg)
  6008. (while (re-search-forward org-drawer-regexp end t)
  6009. (org-flag-drawer t))))))
  6010. (defun org-cycle-hide-inline-tasks (state)
  6011. "Re-hide inline task when switching to 'contents visibility state."
  6012. (when (and (eq state 'contents)
  6013. (boundp 'org-inlinetask-min-level)
  6014. org-inlinetask-min-level)
  6015. (hide-sublevels (1- org-inlinetask-min-level))))
  6016. (defun org-flag-drawer (flag)
  6017. "When FLAG is non-nil, hide the drawer we are within.
  6018. Otherwise make it visible."
  6019. (save-excursion
  6020. (beginning-of-line 1)
  6021. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6022. (let ((b (match-end 0)))
  6023. (if (re-search-forward
  6024. "^[ \t]*:END:"
  6025. (save-excursion (outline-next-heading) (point)) t)
  6026. (outline-flag-region b (point-at-eol) flag)
  6027. (error ":END: line missing at position %s" b))))))
  6028. (defun org-subtree-end-visible-p ()
  6029. "Is the end of the current subtree visible?"
  6030. (pos-visible-in-window-p
  6031. (save-excursion (org-end-of-subtree t) (point))))
  6032. (defun org-first-headline-recenter (&optional N)
  6033. "Move cursor to the first headline and recenter the headline.
  6034. Optional argument N means put the headline into the Nth line of the window."
  6035. (goto-char (point-min))
  6036. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6037. (beginning-of-line)
  6038. (recenter (prefix-numeric-value N))))
  6039. ;;; Saving and restoring visibility
  6040. (defun org-outline-overlay-data (&optional use-markers)
  6041. "Return a list of the locations of all outline overlays.
  6042. These are overlays with the `invisible' property value `outline'.
  6043. The return value is a list of cons cells, with start and stop
  6044. positions for each overlay.
  6045. If USE-MARKERS is set, return the positions as markers."
  6046. (let (beg end)
  6047. (save-excursion
  6048. (save-restriction
  6049. (widen)
  6050. (delq nil
  6051. (mapcar (lambda (o)
  6052. (when (eq (overlay-get o 'invisible) 'outline)
  6053. (setq beg (overlay-start o)
  6054. end (overlay-end o))
  6055. (and beg end (> end beg)
  6056. (if use-markers
  6057. (cons (move-marker (make-marker) beg)
  6058. (move-marker (make-marker) end))
  6059. (cons beg end)))))
  6060. (overlays-in (point-min) (point-max))))))))
  6061. (defun org-set-outline-overlay-data (data)
  6062. "Create visibility overlays for all positions in DATA.
  6063. DATA should have been made by `org-outline-overlay-data'."
  6064. (let (o)
  6065. (save-excursion
  6066. (save-restriction
  6067. (widen)
  6068. (show-all)
  6069. (mapc (lambda (c)
  6070. (outline-flag-region (car c) (cdr c) t))
  6071. data)))))
  6072. ;;; Folding of blocks
  6073. (defvar org-hide-block-overlays nil
  6074. "Overlays hiding blocks.")
  6075. (make-variable-buffer-local 'org-hide-block-overlays)
  6076. (defun org-block-map (function &optional start end)
  6077. "Call FUNCTION at the head of all source blocks in the current buffer.
  6078. Optional arguments START and END can be used to limit the range."
  6079. (let ((start (or start (point-min)))
  6080. (end (or end (point-max))))
  6081. (save-excursion
  6082. (goto-char start)
  6083. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6084. (save-excursion
  6085. (save-match-data
  6086. (goto-char (match-beginning 0))
  6087. (funcall function)))))))
  6088. (defun org-hide-block-toggle-all ()
  6089. "Toggle the visibility of all blocks in the current buffer."
  6090. (org-block-map #'org-hide-block-toggle))
  6091. (defun org-hide-block-all ()
  6092. "Fold all blocks in the current buffer."
  6093. (interactive)
  6094. (org-show-block-all)
  6095. (org-block-map #'org-hide-block-toggle-maybe))
  6096. (defun org-show-block-all ()
  6097. "Unfold all blocks in the current buffer."
  6098. (interactive)
  6099. (mapc 'delete-overlay org-hide-block-overlays)
  6100. (setq org-hide-block-overlays nil))
  6101. (defun org-hide-block-toggle-maybe ()
  6102. "Toggle visibility of block at point."
  6103. (interactive)
  6104. (let ((case-fold-search t))
  6105. (if (save-excursion
  6106. (beginning-of-line 1)
  6107. (looking-at org-block-regexp))
  6108. (progn (org-hide-block-toggle)
  6109. t) ;; to signal that we took action
  6110. nil))) ;; to signal that we did not
  6111. (defun org-hide-block-toggle (&optional force)
  6112. "Toggle the visibility of the current block."
  6113. (interactive)
  6114. (save-excursion
  6115. (beginning-of-line)
  6116. (if (re-search-forward org-block-regexp nil t)
  6117. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6118. (end (match-end 0)) ;; end of entire body
  6119. ov)
  6120. (if (memq t (mapcar (lambda (overlay)
  6121. (eq (overlay-get overlay 'invisible)
  6122. 'org-hide-block))
  6123. (overlays-at start)))
  6124. (if (or (not force) (eq force 'off))
  6125. (mapc (lambda (ov)
  6126. (when (member ov org-hide-block-overlays)
  6127. (setq org-hide-block-overlays
  6128. (delq ov org-hide-block-overlays)))
  6129. (when (eq (overlay-get ov 'invisible)
  6130. 'org-hide-block)
  6131. (delete-overlay ov)))
  6132. (overlays-at start)))
  6133. (setq ov (make-overlay start end))
  6134. (overlay-put ov 'invisible 'org-hide-block)
  6135. ;; make the block accessible to isearch
  6136. (overlay-put
  6137. ov 'isearch-open-invisible
  6138. (lambda (ov)
  6139. (when (member ov org-hide-block-overlays)
  6140. (setq org-hide-block-overlays
  6141. (delq ov org-hide-block-overlays)))
  6142. (when (eq (overlay-get ov 'invisible)
  6143. 'org-hide-block)
  6144. (delete-overlay ov))))
  6145. (push ov org-hide-block-overlays)))
  6146. (error "Not looking at a source block"))))
  6147. ;; org-tab-after-check-for-cycling-hook
  6148. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6149. ;; Remove overlays when changing major mode
  6150. (add-hook 'org-mode-hook
  6151. (lambda () (org-add-hook 'change-major-mode-hook
  6152. 'org-show-block-all 'append 'local)))
  6153. ;;; Org-goto
  6154. (defvar org-goto-window-configuration nil)
  6155. (defvar org-goto-marker nil)
  6156. (defvar org-goto-map
  6157. (let ((map (make-sparse-keymap)))
  6158. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6159. (while (setq cmd (pop cmds))
  6160. (substitute-key-definition cmd cmd map global-map)))
  6161. (suppress-keymap map)
  6162. (org-defkey map "\C-m" 'org-goto-ret)
  6163. (org-defkey map [(return)] 'org-goto-ret)
  6164. (org-defkey map [(left)] 'org-goto-left)
  6165. (org-defkey map [(right)] 'org-goto-right)
  6166. (org-defkey map [(control ?g)] 'org-goto-quit)
  6167. (org-defkey map "\C-i" 'org-cycle)
  6168. (org-defkey map [(tab)] 'org-cycle)
  6169. (org-defkey map [(down)] 'outline-next-visible-heading)
  6170. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6171. (if org-goto-auto-isearch
  6172. (if (fboundp 'define-key-after)
  6173. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6174. nil)
  6175. (org-defkey map "q" 'org-goto-quit)
  6176. (org-defkey map "n" 'outline-next-visible-heading)
  6177. (org-defkey map "p" 'outline-previous-visible-heading)
  6178. (org-defkey map "f" 'outline-forward-same-level)
  6179. (org-defkey map "b" 'outline-backward-same-level)
  6180. (org-defkey map "u" 'outline-up-heading))
  6181. (org-defkey map "/" 'org-occur)
  6182. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6183. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6184. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6185. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6186. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6187. map))
  6188. (defconst org-goto-help
  6189. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6190. RET=jump to location [Q]uit and return to previous location
  6191. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6192. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6193. ;; FIXME: Docstring does not mention both interfaces
  6194. (defun org-goto (&optional alternative-interface)
  6195. "Look up a different location in the current file, keeping current visibility.
  6196. When you want look-up or go to a different location in a
  6197. document, the fastest way is often to fold the entire buffer and
  6198. then dive into the tree. This method has the disadvantage, that
  6199. the previous location will be folded, which may not be what you
  6200. want.
  6201. This command works around this by showing a copy of the current
  6202. buffer in an indirect buffer, in overview mode. You can dive
  6203. into the tree in that copy, use org-occur and incremental search
  6204. to find a location. When pressing RET or `Q', the command
  6205. returns to the original buffer in which the visibility is still
  6206. unchanged. After RET it will also jump to the location selected
  6207. in the indirect buffer and expose the headline hierarchy above.
  6208. With a prefix argument, use the alternative interface: e.g. if
  6209. `org-goto-interface' is 'outline use 'outline-path-completion."
  6210. (interactive "P")
  6211. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6212. (org-refile-use-outline-path t)
  6213. (org-refile-target-verify-function nil)
  6214. (interface
  6215. (if (not alternative-interface)
  6216. org-goto-interface
  6217. (if (eq org-goto-interface 'outline)
  6218. 'outline-path-completion
  6219. 'outline)))
  6220. (org-goto-start-pos (point))
  6221. (selected-point
  6222. (if (eq interface 'outline)
  6223. (car (org-get-location (current-buffer) org-goto-help))
  6224. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6225. (org-refile-check-position pa)
  6226. (nth 3 pa)))))
  6227. (if selected-point
  6228. (progn
  6229. (org-mark-ring-push org-goto-start-pos)
  6230. (goto-char selected-point)
  6231. (if (or (outline-invisible-p) (org-invisible-p2))
  6232. (org-show-context 'org-goto)))
  6233. (message "Quit"))))
  6234. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6235. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6236. (defvar org-goto-local-auto-isearch-map) ; defined below
  6237. (defun org-get-location (buf help)
  6238. "Let the user select a location in the Org-mode buffer BUF.
  6239. This function uses a recursive edit. It returns the selected position
  6240. or nil."
  6241. (org-no-popups
  6242. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6243. (isearch-hide-immediately nil)
  6244. (isearch-search-fun-function
  6245. (lambda () 'org-goto-local-search-headings))
  6246. (org-goto-selected-point org-goto-exit-command))
  6247. (save-excursion
  6248. (save-window-excursion
  6249. (delete-other-windows)
  6250. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6251. (org-pop-to-buffer-same-window
  6252. (condition-case nil
  6253. (make-indirect-buffer (current-buffer) "*org-goto*")
  6254. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6255. (with-output-to-temp-buffer "*Help*"
  6256. (princ help))
  6257. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6258. (setq buffer-read-only nil)
  6259. (let ((org-startup-truncated t)
  6260. (org-startup-folded nil)
  6261. (org-startup-align-all-tables nil))
  6262. (org-mode)
  6263. (org-overview))
  6264. (setq buffer-read-only t)
  6265. (if (and (boundp 'org-goto-start-pos)
  6266. (integer-or-marker-p org-goto-start-pos))
  6267. (let ((org-show-hierarchy-above t)
  6268. (org-show-siblings t)
  6269. (org-show-following-heading t))
  6270. (goto-char org-goto-start-pos)
  6271. (and (outline-invisible-p) (org-show-context)))
  6272. (goto-char (point-min)))
  6273. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6274. (message "Select location and press RET")
  6275. (use-local-map org-goto-map)
  6276. (recursive-edit)
  6277. ))
  6278. (kill-buffer "*org-goto*")
  6279. (cons org-goto-selected-point org-goto-exit-command))))
  6280. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6281. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6282. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6283. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6284. (defun org-goto-local-search-headings (string bound noerror)
  6285. "Search and make sure that any matches are in headlines."
  6286. (catch 'return
  6287. (while (if isearch-forward
  6288. (search-forward string bound noerror)
  6289. (search-backward string bound noerror))
  6290. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6291. (and (member :headline context)
  6292. (not (member :tags context))))
  6293. (throw 'return (point))))))
  6294. (defun org-goto-local-auto-isearch ()
  6295. "Start isearch."
  6296. (interactive)
  6297. (goto-char (point-min))
  6298. (let ((keys (this-command-keys)))
  6299. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6300. (isearch-mode t)
  6301. (isearch-process-search-char (string-to-char keys)))))
  6302. (defun org-goto-ret (&optional arg)
  6303. "Finish `org-goto' by going to the new location."
  6304. (interactive "P")
  6305. (setq org-goto-selected-point (point)
  6306. org-goto-exit-command 'return)
  6307. (throw 'exit nil))
  6308. (defun org-goto-left ()
  6309. "Finish `org-goto' by going to the new location."
  6310. (interactive)
  6311. (if (org-at-heading-p)
  6312. (progn
  6313. (beginning-of-line 1)
  6314. (setq org-goto-selected-point (point)
  6315. org-goto-exit-command 'left)
  6316. (throw 'exit nil))
  6317. (error "Not on a heading")))
  6318. (defun org-goto-right ()
  6319. "Finish `org-goto' by going to the new location."
  6320. (interactive)
  6321. (if (org-at-heading-p)
  6322. (progn
  6323. (setq org-goto-selected-point (point)
  6324. org-goto-exit-command 'right)
  6325. (throw 'exit nil))
  6326. (error "Not on a heading")))
  6327. (defun org-goto-quit ()
  6328. "Finish `org-goto' without cursor motion."
  6329. (interactive)
  6330. (setq org-goto-selected-point nil)
  6331. (setq org-goto-exit-command 'quit)
  6332. (throw 'exit nil))
  6333. ;;; Indirect buffer display of subtrees
  6334. (defvar org-indirect-dedicated-frame nil
  6335. "This is the frame being used for indirect tree display.")
  6336. (defvar org-last-indirect-buffer nil)
  6337. (defun org-tree-to-indirect-buffer (&optional arg)
  6338. "Create indirect buffer and narrow it to current subtree.
  6339. With a numerical prefix ARG, go up to this level and then take that tree.
  6340. If ARG is negative, go up that many levels.
  6341. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6342. indirect buffer previously made with this command, to avoid proliferation of
  6343. indirect buffers. However, when you call the command with a \
  6344. \\[universal-argument] prefix, or
  6345. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6346. is kept so that you can work with several indirect buffers at the same time.
  6347. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6348. \\[universal-argument] prefix also
  6349. requests that a new frame be made for the new buffer, so that the dedicated
  6350. frame is not changed."
  6351. (interactive "P")
  6352. (let ((cbuf (current-buffer))
  6353. (cwin (selected-window))
  6354. (pos (point))
  6355. beg end level heading ibuf)
  6356. (save-excursion
  6357. (org-back-to-heading t)
  6358. (when (numberp arg)
  6359. (setq level (org-outline-level))
  6360. (if (< arg 0) (setq arg (+ level arg)))
  6361. (while (> (setq level (org-outline-level)) arg)
  6362. (org-up-heading-safe)))
  6363. (setq beg (point)
  6364. heading (org-get-heading))
  6365. (org-end-of-subtree t t)
  6366. (if (org-at-heading-p) (backward-char 1))
  6367. (setq end (point)))
  6368. (if (and (buffer-live-p org-last-indirect-buffer)
  6369. (not (eq org-indirect-buffer-display 'new-frame))
  6370. (not arg))
  6371. (kill-buffer org-last-indirect-buffer))
  6372. (setq ibuf (org-get-indirect-buffer cbuf)
  6373. org-last-indirect-buffer ibuf)
  6374. (cond
  6375. ((or (eq org-indirect-buffer-display 'new-frame)
  6376. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6377. (select-frame (make-frame))
  6378. (delete-other-windows)
  6379. (org-pop-to-buffer-same-window ibuf)
  6380. (org-set-frame-title heading))
  6381. ((eq org-indirect-buffer-display 'dedicated-frame)
  6382. (raise-frame
  6383. (select-frame (or (and org-indirect-dedicated-frame
  6384. (frame-live-p org-indirect-dedicated-frame)
  6385. org-indirect-dedicated-frame)
  6386. (setq org-indirect-dedicated-frame (make-frame)))))
  6387. (delete-other-windows)
  6388. (org-pop-to-buffer-same-window ibuf)
  6389. (org-set-frame-title (concat "Indirect: " heading)))
  6390. ((eq org-indirect-buffer-display 'current-window)
  6391. (org-pop-to-buffer-same-window ibuf))
  6392. ((eq org-indirect-buffer-display 'other-window)
  6393. (pop-to-buffer ibuf))
  6394. (t (error "Invalid value")))
  6395. (if (featurep 'xemacs)
  6396. (save-excursion (org-mode) (turn-on-font-lock)))
  6397. (narrow-to-region beg end)
  6398. (show-all)
  6399. (goto-char pos)
  6400. (run-hook-with-args 'org-cycle-hook 'all)
  6401. (and (window-live-p cwin) (select-window cwin))))
  6402. (defun org-get-indirect-buffer (&optional buffer)
  6403. (setq buffer (or buffer (current-buffer)))
  6404. (let ((n 1) (base (buffer-name buffer)) bname)
  6405. (while (buffer-live-p
  6406. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6407. (setq n (1+ n)))
  6408. (condition-case nil
  6409. (make-indirect-buffer buffer bname 'clone)
  6410. (error (make-indirect-buffer buffer bname)))))
  6411. (defun org-set-frame-title (title)
  6412. "Set the title of the current frame to the string TITLE."
  6413. ;; FIXME: how to name a single frame in XEmacs???
  6414. (unless (featurep 'xemacs)
  6415. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6416. ;;;; Structure editing
  6417. ;;; Inserting headlines
  6418. (defun org-previous-line-empty-p ()
  6419. (save-excursion
  6420. (and (not (bobp))
  6421. (or (beginning-of-line 0) t)
  6422. (save-match-data
  6423. (looking-at "[ \t]*$")))))
  6424. (defun org-insert-heading (&optional force-heading invisible-ok)
  6425. "Insert a new heading or item with same depth at point.
  6426. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6427. If point is at the beginning of a headline, insert a sibling before the
  6428. current headline. If point is not at the beginning, split the line,
  6429. create the new headline with the text in the current line after point
  6430. \(but see also the variable `org-M-RET-may-split-line').
  6431. With a double prefix arg, force the heading to be inserted at the
  6432. end of the parent subtree.
  6433. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6434. This is important for non-interactive uses of the command."
  6435. (interactive "P")
  6436. (if (or (= (buffer-size) 0)
  6437. (and (not (save-excursion
  6438. (and (ignore-errors (org-back-to-heading invisible-ok))
  6439. (org-at-heading-p))))
  6440. (or force-heading (not (org-in-item-p)))))
  6441. (progn
  6442. (insert "\n* ")
  6443. (run-hooks 'org-insert-heading-hook))
  6444. (when (or force-heading (not (org-insert-item)))
  6445. (let* ((empty-line-p nil)
  6446. (level nil)
  6447. (on-heading (org-at-heading-p))
  6448. (head (save-excursion
  6449. (condition-case nil
  6450. (progn
  6451. (org-back-to-heading invisible-ok)
  6452. (when (and (not on-heading)
  6453. (featurep 'org-inlinetask)
  6454. (integerp org-inlinetask-min-level)
  6455. (>= (length (match-string 0))
  6456. org-inlinetask-min-level))
  6457. ;; Find a heading level before the inline task
  6458. (while (and (setq level (org-up-heading-safe))
  6459. (>= level org-inlinetask-min-level)))
  6460. (if (org-at-heading-p)
  6461. (org-back-to-heading invisible-ok)
  6462. (error "This should not happen")))
  6463. (setq empty-line-p (org-previous-line-empty-p))
  6464. (match-string 0))
  6465. (error "*"))))
  6466. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6467. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6468. pos hide-previous previous-pos)
  6469. (cond
  6470. ((and (org-at-heading-p) (bolp)
  6471. (or (bobp)
  6472. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6473. ;; insert before the current line
  6474. (open-line (if blank 2 1)))
  6475. ((and (bolp)
  6476. (not org-insert-heading-respect-content)
  6477. (or (bobp)
  6478. (save-excursion
  6479. (backward-char 1) (not (outline-invisible-p)))))
  6480. ;; insert right here
  6481. nil)
  6482. (t
  6483. ;; somewhere in the line
  6484. (save-excursion
  6485. (setq previous-pos (point-at-bol))
  6486. (end-of-line)
  6487. (setq hide-previous (outline-invisible-p)))
  6488. (and org-insert-heading-respect-content (org-show-subtree))
  6489. (let ((split
  6490. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6491. (save-excursion
  6492. (let ((p (point)))
  6493. (goto-char (point-at-bol))
  6494. (and (looking-at org-complex-heading-regexp)
  6495. (match-beginning 4)
  6496. (> p (match-beginning 4)))))))
  6497. tags pos)
  6498. (cond
  6499. (org-insert-heading-respect-content
  6500. (if (not (equal force-heading '(16)))
  6501. (org-end-of-subtree nil t)
  6502. (org-up-heading-safe)
  6503. (org-end-of-subtree nil t))
  6504. (when (featurep 'org-inlinetask)
  6505. (while (and (not (eobp))
  6506. (looking-at "\\(\\*+\\)[ \t]+")
  6507. (>= (length (match-string 1))
  6508. org-inlinetask-min-level))
  6509. (org-end-of-subtree nil t)))
  6510. (or (bolp) (newline))
  6511. (or (org-previous-line-empty-p)
  6512. (and blank (newline)))
  6513. (open-line 1))
  6514. ((org-at-heading-p)
  6515. (when hide-previous
  6516. (show-children)
  6517. (org-show-entry))
  6518. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6519. (setq tags (and (match-end 2) (match-string 2)))
  6520. (and (match-end 1)
  6521. (delete-region (match-beginning 1) (match-end 1)))
  6522. (setq pos (point-at-bol))
  6523. (or split (end-of-line 1))
  6524. (delete-horizontal-space)
  6525. (if (string-match "\\`\\*+\\'"
  6526. (buffer-substring (point-at-bol) (point)))
  6527. (insert " "))
  6528. (newline (if blank 2 1))
  6529. (when tags
  6530. (save-excursion
  6531. (goto-char pos)
  6532. (end-of-line 1)
  6533. (insert " " tags)
  6534. (org-set-tags nil 'align))))
  6535. (t
  6536. (or split (end-of-line 1))
  6537. (newline (if blank 2 1)))))))
  6538. (insert head) (just-one-space)
  6539. (setq pos (point))
  6540. (end-of-line 1)
  6541. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6542. (when (and org-insert-heading-respect-content hide-previous)
  6543. (save-excursion
  6544. (goto-char previous-pos)
  6545. (hide-subtree)))
  6546. (run-hooks 'org-insert-heading-hook)))))
  6547. (defun org-get-heading (&optional no-tags no-todo)
  6548. "Return the heading of the current entry, without the stars.
  6549. When NO-TAGS is non-nil, don't include tags.
  6550. When NO-TODO is non-nil, don't include TODO keywords."
  6551. (save-excursion
  6552. (org-back-to-heading t)
  6553. (cond
  6554. ((and no-tags no-todo)
  6555. (looking-at org-complex-heading-regexp)
  6556. (match-string 4))
  6557. (no-tags
  6558. (looking-at (concat org-outline-regexp
  6559. "\\(.*?\\)"
  6560. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6561. (match-string 1))
  6562. (no-todo
  6563. (looking-at org-todo-line-regexp)
  6564. (match-string 3))
  6565. (t (looking-at org-heading-regexp)
  6566. (match-string 2)))))
  6567. (defun org-heading-components ()
  6568. "Return the components of the current heading.
  6569. This is a list with the following elements:
  6570. - the level as an integer
  6571. - the reduced level, different if `org-odd-levels-only' is set.
  6572. - the TODO keyword, or nil
  6573. - the priority character, like ?A, or nil if no priority is given
  6574. - the headline text itself, or the tags string if no headline text
  6575. - the tags string, or nil."
  6576. (save-excursion
  6577. (org-back-to-heading t)
  6578. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6579. (list (length (match-string 1))
  6580. (org-reduced-level (length (match-string 1)))
  6581. (org-match-string-no-properties 2)
  6582. (and (match-end 3) (aref (match-string 3) 2))
  6583. (org-match-string-no-properties 4)
  6584. (org-match-string-no-properties 5)))))
  6585. (defun org-get-entry ()
  6586. "Get the entry text, after heading, entire subtree."
  6587. (save-excursion
  6588. (org-back-to-heading t)
  6589. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6590. (defun org-insert-heading-after-current ()
  6591. "Insert a new heading with same level as current, after current subtree."
  6592. (interactive)
  6593. (org-back-to-heading)
  6594. (org-insert-heading)
  6595. (org-move-subtree-down)
  6596. (end-of-line 1))
  6597. (defun org-insert-heading-respect-content ()
  6598. (interactive)
  6599. (let ((org-insert-heading-respect-content t))
  6600. (org-insert-heading t)))
  6601. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6602. (interactive "P")
  6603. (let ((org-insert-heading-respect-content t))
  6604. (org-insert-todo-heading force-state t)))
  6605. (defun org-insert-todo-heading (arg &optional force-heading)
  6606. "Insert a new heading with the same level and TODO state as current heading.
  6607. If the heading has no TODO state, or if the state is DONE, use the first
  6608. state (TODO by default). Also one prefix arg, force first state. With two
  6609. prefix args, force inserting at the end of the parent subtree."
  6610. (interactive "P")
  6611. (when (or force-heading (not (org-insert-item 'checkbox)))
  6612. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6613. force-heading))
  6614. (save-excursion
  6615. (org-back-to-heading)
  6616. (outline-previous-heading)
  6617. (looking-at org-todo-line-regexp))
  6618. (let*
  6619. ((new-mark-x
  6620. (if (or arg
  6621. (not (match-beginning 2))
  6622. (member (match-string 2) org-done-keywords))
  6623. (car org-todo-keywords-1)
  6624. (match-string 2)))
  6625. (new-mark
  6626. (or
  6627. (run-hook-with-args-until-success
  6628. 'org-todo-get-default-hook new-mark-x nil)
  6629. new-mark-x)))
  6630. (beginning-of-line 1)
  6631. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6632. (if org-treat-insert-todo-heading-as-state-change
  6633. (org-todo new-mark)
  6634. (insert new-mark " "))))
  6635. (when org-provide-todo-statistics
  6636. (org-update-parent-todo-statistics))))
  6637. (defun org-insert-subheading (arg)
  6638. "Insert a new subheading and demote it.
  6639. Works for outline headings and for plain lists alike."
  6640. (interactive "P")
  6641. (org-insert-heading arg)
  6642. (cond
  6643. ((org-at-heading-p) (org-do-demote))
  6644. ((org-at-item-p) (org-indent-item))))
  6645. (defun org-insert-todo-subheading (arg)
  6646. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6647. Works for outline headings and for plain lists alike."
  6648. (interactive "P")
  6649. (org-insert-todo-heading arg)
  6650. (cond
  6651. ((org-at-heading-p) (org-do-demote))
  6652. ((org-at-item-p) (org-indent-item))))
  6653. ;;; Promotion and Demotion
  6654. (defvar org-after-demote-entry-hook nil
  6655. "Hook run after an entry has been demoted.
  6656. The cursor will be at the beginning of the entry.
  6657. When a subtree is being demoted, the hook will be called for each node.")
  6658. (defvar org-after-promote-entry-hook nil
  6659. "Hook run after an entry has been promoted.
  6660. The cursor will be at the beginning of the entry.
  6661. When a subtree is being promoted, the hook will be called for each node.")
  6662. (defun org-promote-subtree ()
  6663. "Promote the entire subtree.
  6664. See also `org-promote'."
  6665. (interactive)
  6666. (save-excursion
  6667. (org-with-limited-levels (org-map-tree 'org-promote)))
  6668. (org-fix-position-after-promote))
  6669. (defun org-demote-subtree ()
  6670. "Demote the entire subtree. See `org-demote'.
  6671. See also `org-promote'."
  6672. (interactive)
  6673. (save-excursion
  6674. (org-with-limited-levels (org-map-tree 'org-demote)))
  6675. (org-fix-position-after-promote))
  6676. (defun org-do-promote ()
  6677. "Promote the current heading higher up the tree.
  6678. If the region is active in `transient-mark-mode', promote all headings
  6679. in the region."
  6680. (interactive)
  6681. (save-excursion
  6682. (if (org-region-active-p)
  6683. (org-map-region 'org-promote (region-beginning) (region-end))
  6684. (org-promote)))
  6685. (org-fix-position-after-promote))
  6686. (defun org-do-demote ()
  6687. "Demote the current heading lower down the tree.
  6688. If the region is active in `transient-mark-mode', demote all headings
  6689. in the region."
  6690. (interactive)
  6691. (save-excursion
  6692. (if (org-region-active-p)
  6693. (org-map-region 'org-demote (region-beginning) (region-end))
  6694. (org-demote)))
  6695. (org-fix-position-after-promote))
  6696. (defun org-fix-position-after-promote ()
  6697. "Make sure that after pro/demotion cursor position is right."
  6698. (let ((pos (point)))
  6699. (when (save-excursion
  6700. (beginning-of-line 1)
  6701. (looking-at org-todo-line-regexp)
  6702. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6703. (cond ((eobp) (insert " "))
  6704. ((eolp) (insert " "))
  6705. ((equal (char-after) ?\ ) (forward-char 1))))))
  6706. (defun org-current-level ()
  6707. "Return the level of the current entry, or nil if before the first headline.
  6708. The level is the number of stars at the beginning of the headline."
  6709. (save-excursion
  6710. (org-with-limited-levels
  6711. (if (ignore-errors (org-back-to-heading t))
  6712. (funcall outline-level)))))
  6713. (defun org-get-previous-line-level ()
  6714. "Return the outline depth of the last headline before the current line.
  6715. Returns 0 for the first headline in the buffer, and nil if before the
  6716. first headline."
  6717. (let ((current-level (org-current-level))
  6718. (prev-level (when (> (line-number-at-pos) 1)
  6719. (save-excursion
  6720. (beginning-of-line 0)
  6721. (org-current-level)))))
  6722. (cond ((null current-level) nil) ; Before first headline
  6723. ((null prev-level) 0) ; At first headline
  6724. (prev-level))))
  6725. (defun org-reduced-level (l)
  6726. "Compute the effective level of a heading.
  6727. This takes into account the setting of `org-odd-levels-only'."
  6728. (cond
  6729. ((zerop l) 0)
  6730. (org-odd-levels-only (1+ (floor (/ l 2))))
  6731. (t l)))
  6732. (defun org-level-increment ()
  6733. "Return the number of stars that will be added or removed at a
  6734. time to headlines when structure editing, based on the value of
  6735. `org-odd-levels-only'."
  6736. (if org-odd-levels-only 2 1))
  6737. (defun org-get-valid-level (level &optional change)
  6738. "Rectify a level change under the influence of `org-odd-levels-only'
  6739. LEVEL is a current level, CHANGE is by how much the level should be
  6740. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6741. even level numbers will become the next higher odd number."
  6742. (if org-odd-levels-only
  6743. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6744. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6745. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6746. (max 1 (+ level (or change 0)))))
  6747. (if (boundp 'define-obsolete-function-alias)
  6748. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6749. (define-obsolete-function-alias 'org-get-legal-level
  6750. 'org-get-valid-level)
  6751. (define-obsolete-function-alias 'org-get-legal-level
  6752. 'org-get-valid-level "23.1")))
  6753. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6754. ;; ̀org-with-limited-levels'
  6755. (defun org-promote ()
  6756. "Promote the current heading higher up the tree.
  6757. If the region is active in `transient-mark-mode', promote all headings
  6758. in the region."
  6759. (org-back-to-heading t)
  6760. (let* ((level (save-match-data (funcall outline-level)))
  6761. (after-change-functions (remove 'flyspell-after-change-function
  6762. after-change-functions))
  6763. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6764. (diff (abs (- level (length up-head) -1))))
  6765. (cond ((and (= level 1) org-called-with-limited-levels
  6766. org-allow-promoting-top-level-subtree)
  6767. (replace-match "# " nil t))
  6768. ((= level 1)
  6769. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6770. (t (replace-match up-head nil t)))
  6771. ;; Fixup tag positioning
  6772. (unless (= level 1)
  6773. (and org-auto-align-tags (org-set-tags nil t))
  6774. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6775. (run-hooks 'org-after-promote-entry-hook)))
  6776. (defun org-demote ()
  6777. "Demote the current heading lower down the tree.
  6778. If the region is active in `transient-mark-mode', demote all headings
  6779. in the region."
  6780. (org-back-to-heading t)
  6781. (let* ((level (save-match-data (funcall outline-level)))
  6782. (after-change-functions (remove 'flyspell-after-change-function
  6783. after-change-functions))
  6784. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6785. (diff (abs (- level (length down-head) -1))))
  6786. (replace-match down-head nil t)
  6787. ;; Fixup tag positioning
  6788. (and org-auto-align-tags (org-set-tags nil t))
  6789. (if org-adapt-indentation (org-fixup-indentation diff))
  6790. (run-hooks 'org-after-demote-entry-hook)))
  6791. (defun org-cycle-level ()
  6792. "Cycle the level of an empty headline through possible states.
  6793. This goes first to child, then to parent, level, then up the hierarchy.
  6794. After top level, it switches back to sibling level."
  6795. (interactive)
  6796. (let ((org-adapt-indentation nil))
  6797. (when (org-point-at-end-of-empty-headline)
  6798. (setq this-command 'org-cycle-level) ; Only needed for caching
  6799. (let ((cur-level (org-current-level))
  6800. (prev-level (org-get-previous-line-level)))
  6801. (cond
  6802. ;; If first headline in file, promote to top-level.
  6803. ((= prev-level 0)
  6804. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6805. do (org-do-promote)))
  6806. ;; If same level as prev, demote one.
  6807. ((= prev-level cur-level)
  6808. (org-do-demote))
  6809. ;; If parent is top-level, promote to top level if not already.
  6810. ((= prev-level 1)
  6811. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6812. do (org-do-promote)))
  6813. ;; If top-level, return to prev-level.
  6814. ((= cur-level 1)
  6815. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6816. do (org-do-demote)))
  6817. ;; If less than prev-level, promote one.
  6818. ((< cur-level prev-level)
  6819. (org-do-promote))
  6820. ;; If deeper than prev-level, promote until higher than
  6821. ;; prev-level.
  6822. ((> cur-level prev-level)
  6823. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6824. do (org-do-promote))))
  6825. t))))
  6826. (defun org-map-tree (fun)
  6827. "Call FUN for every heading underneath the current one."
  6828. (org-back-to-heading)
  6829. (let ((level (funcall outline-level)))
  6830. (save-excursion
  6831. (funcall fun)
  6832. (while (and (progn
  6833. (outline-next-heading)
  6834. (> (funcall outline-level) level))
  6835. (not (eobp)))
  6836. (funcall fun)))))
  6837. (defun org-map-region (fun beg end)
  6838. "Call FUN for every heading between BEG and END."
  6839. (let ((org-ignore-region t))
  6840. (save-excursion
  6841. (setq end (copy-marker end))
  6842. (goto-char beg)
  6843. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6844. (< (point) end))
  6845. (funcall fun))
  6846. (while (and (progn
  6847. (outline-next-heading)
  6848. (< (point) end))
  6849. (not (eobp)))
  6850. (funcall fun)))))
  6851. (defvar org-property-end-re) ; silence byte-compiler
  6852. (defun org-fixup-indentation (diff)
  6853. "Change the indentation in the current entry by DIFF.
  6854. However, if any line in the current entry has no indentation, or if it
  6855. would end up with no indentation after the change, nothing at all is done."
  6856. (save-excursion
  6857. (let ((end (save-excursion (outline-next-heading)
  6858. (point-marker)))
  6859. (prohibit (if (> diff 0)
  6860. "^\\S-"
  6861. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6862. col)
  6863. (unless (save-excursion (end-of-line 1)
  6864. (re-search-forward prohibit end t))
  6865. (while (and (< (point) end)
  6866. (re-search-forward "^[ \t]+" end t))
  6867. (goto-char (match-end 0))
  6868. (setq col (current-column))
  6869. (if (< diff 0) (replace-match ""))
  6870. (org-indent-to-column (+ diff col))))
  6871. (move-marker end nil))))
  6872. (defun org-convert-to-odd-levels ()
  6873. "Convert an org-mode file with all levels allowed to one with odd levels.
  6874. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6875. level 5 etc."
  6876. (interactive)
  6877. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6878. (let ((outline-level 'org-outline-level)
  6879. (org-odd-levels-only nil) n)
  6880. (save-excursion
  6881. (goto-char (point-min))
  6882. (while (re-search-forward "^\\*\\*+ " nil t)
  6883. (setq n (- (length (match-string 0)) 2))
  6884. (while (>= (setq n (1- n)) 0)
  6885. (org-demote))
  6886. (end-of-line 1))))))
  6887. (defun org-convert-to-oddeven-levels ()
  6888. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6889. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6890. file contains a section with an even level, conversion would
  6891. destroy the structure of the file. An error is signaled in this
  6892. case."
  6893. (interactive)
  6894. (goto-char (point-min))
  6895. ;; First check if there are no even levels
  6896. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6897. (org-show-context t)
  6898. (error "Not all levels are odd in this file. Conversion not possible"))
  6899. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6900. (let ((outline-regexp org-outline-regexp)
  6901. (outline-level 'org-outline-level)
  6902. (org-odd-levels-only nil) n)
  6903. (save-excursion
  6904. (goto-char (point-min))
  6905. (while (re-search-forward "^\\*\\*+ " nil t)
  6906. (setq n (/ (1- (length (match-string 0))) 2))
  6907. (while (>= (setq n (1- n)) 0)
  6908. (org-promote))
  6909. (end-of-line 1))))))
  6910. (defun org-tr-level (n)
  6911. "Make N odd if required."
  6912. (if org-odd-levels-only (1+ (/ n 2)) n))
  6913. ;;; Vertical tree motion, cutting and pasting of subtrees
  6914. (defun org-move-subtree-up (&optional arg)
  6915. "Move the current subtree up past ARG headlines of the same level."
  6916. (interactive "p")
  6917. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6918. (defun org-move-subtree-down (&optional arg)
  6919. "Move the current subtree down past ARG headlines of the same level."
  6920. (interactive "p")
  6921. (setq arg (prefix-numeric-value arg))
  6922. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6923. 'org-get-last-sibling))
  6924. (ins-point (make-marker))
  6925. (cnt (abs arg))
  6926. (col (current-column))
  6927. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6928. ;; Select the tree
  6929. (org-back-to-heading)
  6930. (setq beg0 (point))
  6931. (save-excursion
  6932. (setq ne-beg (org-back-over-empty-lines))
  6933. (setq beg (point)))
  6934. (save-match-data
  6935. (save-excursion (outline-end-of-heading)
  6936. (setq folded (outline-invisible-p)))
  6937. (outline-end-of-subtree))
  6938. (outline-next-heading)
  6939. (setq ne-end (org-back-over-empty-lines))
  6940. (setq end (point))
  6941. (goto-char beg0)
  6942. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6943. ;; include less whitespace
  6944. (save-excursion
  6945. (goto-char beg)
  6946. (forward-line (- ne-beg ne-end))
  6947. (setq beg (point))))
  6948. ;; Find insertion point, with error handling
  6949. (while (> cnt 0)
  6950. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6951. (progn (goto-char beg0)
  6952. (error "Cannot move past superior level or buffer limit")))
  6953. (setq cnt (1- cnt)))
  6954. (if (> arg 0)
  6955. ;; Moving forward - still need to move over subtree
  6956. (progn (org-end-of-subtree t t)
  6957. (save-excursion
  6958. (org-back-over-empty-lines)
  6959. (or (bolp) (newline)))))
  6960. (setq ne-ins (org-back-over-empty-lines))
  6961. (move-marker ins-point (point))
  6962. (setq txt (buffer-substring beg end))
  6963. (org-save-markers-in-region beg end)
  6964. (delete-region beg end)
  6965. (org-remove-empty-overlays-at beg)
  6966. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6967. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6968. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6969. (let ((bbb (point)))
  6970. (insert-before-markers txt)
  6971. (org-reinstall-markers-in-region bbb)
  6972. (move-marker ins-point bbb))
  6973. (or (bolp) (insert "\n"))
  6974. (setq ins-end (point))
  6975. (goto-char ins-point)
  6976. (org-skip-whitespace)
  6977. (when (and (< arg 0)
  6978. (org-first-sibling-p)
  6979. (> ne-ins ne-beg))
  6980. ;; Move whitespace back to beginning
  6981. (save-excursion
  6982. (goto-char ins-end)
  6983. (let ((kill-whole-line t))
  6984. (kill-line (- ne-ins ne-beg)) (point)))
  6985. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6986. (move-marker ins-point nil)
  6987. (if folded
  6988. (hide-subtree)
  6989. (org-show-entry)
  6990. (show-children)
  6991. (org-cycle-hide-drawers 'children))
  6992. (org-clean-visibility-after-subtree-move)
  6993. ;; move back to the initial column we were at
  6994. (move-to-column col)))
  6995. (defvar org-subtree-clip ""
  6996. "Clipboard for cut and paste of subtrees.
  6997. This is actually only a copy of the kill, because we use the normal kill
  6998. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6999. (defvar org-subtree-clip-folded nil
  7000. "Was the last copied subtree folded?
  7001. This is used to fold the tree back after pasting.")
  7002. (defun org-cut-subtree (&optional n)
  7003. "Cut the current subtree into the clipboard.
  7004. With prefix arg N, cut this many sequential subtrees.
  7005. This is a short-hand for marking the subtree and then cutting it."
  7006. (interactive "p")
  7007. (org-copy-subtree n 'cut))
  7008. (defun org-copy-subtree (&optional n cut force-store-markers)
  7009. "Cut the current subtree into the clipboard.
  7010. With prefix arg N, cut this many sequential subtrees.
  7011. This is a short-hand for marking the subtree and then copying it.
  7012. If CUT is non-nil, actually cut the subtree.
  7013. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7014. of some markers in the region, even if CUT is non-nil. This is
  7015. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7016. (interactive "p")
  7017. (let (beg end folded (beg0 (point)))
  7018. (if (org-called-interactively-p 'any)
  7019. (org-back-to-heading nil) ; take what looks like a subtree
  7020. (org-back-to-heading t)) ; take what is really there
  7021. (setq beg (point))
  7022. (skip-chars-forward " \t\r\n")
  7023. (save-match-data
  7024. (save-excursion (outline-end-of-heading)
  7025. (setq folded (outline-invisible-p)))
  7026. (condition-case nil
  7027. (org-forward-heading-same-level (1- n) t)
  7028. (error nil))
  7029. (org-end-of-subtree t t))
  7030. (setq end (point))
  7031. (goto-char beg0)
  7032. (when (> end beg)
  7033. (setq org-subtree-clip-folded folded)
  7034. (when (or cut force-store-markers)
  7035. (org-save-markers-in-region beg end))
  7036. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7037. (setq org-subtree-clip (current-kill 0))
  7038. (message "%s: Subtree(s) with %d characters"
  7039. (if cut "Cut" "Copied")
  7040. (length org-subtree-clip)))))
  7041. (defun org-paste-subtree (&optional level tree for-yank)
  7042. "Paste the clipboard as a subtree, with modification of headline level.
  7043. The entire subtree is promoted or demoted in order to match a new headline
  7044. level.
  7045. If the cursor is at the beginning of a headline, the same level as
  7046. that headline is used to paste the tree
  7047. If not, the new level is derived from the *visible* headings
  7048. before and after the insertion point, and taken to be the inferior headline
  7049. level of the two. So if the previous visible heading is level 3 and the
  7050. next is level 4 (or vice versa), level 4 will be used for insertion.
  7051. This makes sure that the subtree remains an independent subtree and does
  7052. not swallow low level entries.
  7053. You can also force a different level, either by using a numeric prefix
  7054. argument, or by inserting the heading marker by hand. For example, if the
  7055. cursor is after \"*****\", then the tree will be shifted to level 5.
  7056. If optional TREE is given, use this text instead of the kill ring.
  7057. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7058. move back over whitespace before inserting, and move point to the end of
  7059. the inserted text when done."
  7060. (interactive "P")
  7061. (setq tree (or tree (and kill-ring (current-kill 0))))
  7062. (unless (org-kill-is-subtree-p tree)
  7063. (error "%s"
  7064. (substitute-command-keys
  7065. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7066. (org-with-limited-levels
  7067. (let* ((visp (not (outline-invisible-p)))
  7068. (txt tree)
  7069. (^re_ "\\(\\*+\\)[ \t]*")
  7070. (old-level (if (string-match org-outline-regexp-bol txt)
  7071. (- (match-end 0) (match-beginning 0) 1)
  7072. -1))
  7073. (force-level (cond (level (prefix-numeric-value level))
  7074. ((and (looking-at "[ \t]*$")
  7075. (string-match
  7076. "^\\*+$" (buffer-substring
  7077. (point-at-bol) (point))))
  7078. (- (match-end 1) (match-beginning 1)))
  7079. ((and (bolp)
  7080. (looking-at org-outline-regexp))
  7081. (- (match-end 0) (point) 1))))
  7082. (previous-level (save-excursion
  7083. (condition-case nil
  7084. (progn
  7085. (outline-previous-visible-heading 1)
  7086. (if (looking-at ^re_)
  7087. (- (match-end 0) (match-beginning 0) 1)
  7088. 1))
  7089. (error 1))))
  7090. (next-level (save-excursion
  7091. (condition-case nil
  7092. (progn
  7093. (or (looking-at org-outline-regexp)
  7094. (outline-next-visible-heading 1))
  7095. (if (looking-at ^re_)
  7096. (- (match-end 0) (match-beginning 0) 1)
  7097. 1))
  7098. (error 1))))
  7099. (new-level (or force-level (max previous-level next-level)))
  7100. (shift (if (or (= old-level -1)
  7101. (= new-level -1)
  7102. (= old-level new-level))
  7103. 0
  7104. (- new-level old-level)))
  7105. (delta (if (> shift 0) -1 1))
  7106. (func (if (> shift 0) 'org-demote 'org-promote))
  7107. (org-odd-levels-only nil)
  7108. beg end newend)
  7109. ;; Remove the forced level indicator
  7110. (if force-level
  7111. (delete-region (point-at-bol) (point)))
  7112. ;; Paste
  7113. (beginning-of-line (if (bolp) 1 2))
  7114. (setq beg (point))
  7115. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7116. (insert-before-markers txt)
  7117. (unless (string-match "\n\\'" txt) (insert "\n"))
  7118. (setq newend (point))
  7119. (org-reinstall-markers-in-region beg)
  7120. (setq end (point))
  7121. (goto-char beg)
  7122. (skip-chars-forward " \t\n\r")
  7123. (setq beg (point))
  7124. (if (and (outline-invisible-p) visp)
  7125. (save-excursion (outline-show-heading)))
  7126. ;; Shift if necessary
  7127. (unless (= shift 0)
  7128. (save-restriction
  7129. (narrow-to-region beg end)
  7130. (while (not (= shift 0))
  7131. (org-map-region func (point-min) (point-max))
  7132. (setq shift (+ delta shift)))
  7133. (goto-char (point-min))
  7134. (setq newend (point-max))))
  7135. (when (or (org-called-interactively-p 'interactive) for-yank)
  7136. (message "Clipboard pasted as level %d subtree" new-level))
  7137. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7138. kill-ring
  7139. (eq org-subtree-clip (current-kill 0))
  7140. org-subtree-clip-folded)
  7141. ;; The tree was folded before it was killed/copied
  7142. (hide-subtree))
  7143. (and for-yank (goto-char newend)))))
  7144. (defun org-kill-is-subtree-p (&optional txt)
  7145. "Check if the current kill is an outline subtree, or a set of trees.
  7146. Returns nil if kill does not start with a headline, or if the first
  7147. headline level is not the largest headline level in the tree.
  7148. So this will actually accept several entries of equal levels as well,
  7149. which is OK for `org-paste-subtree'.
  7150. If optional TXT is given, check this string instead of the current kill."
  7151. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7152. (re (org-get-limited-outline-regexp))
  7153. (^re (concat "^" re))
  7154. (start-level (and kill
  7155. (string-match
  7156. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7157. kill)
  7158. (- (match-end 2) (match-beginning 2) 1)))
  7159. (start (1+ (or (match-beginning 2) -1))))
  7160. (if (not start-level)
  7161. (progn
  7162. nil) ;; does not even start with a heading
  7163. (catch 'exit
  7164. (while (setq start (string-match ^re kill (1+ start)))
  7165. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7166. (throw 'exit nil)))
  7167. t))))
  7168. (defvar org-markers-to-move nil
  7169. "Markers that should be moved with a cut-and-paste operation.
  7170. Those markers are stored together with their positions relative to
  7171. the start of the region.")
  7172. (defun org-save-markers-in-region (beg end)
  7173. "Check markers in region.
  7174. If these markers are between BEG and END, record their position relative
  7175. to BEG, so that after moving the block of text, we can put the markers back
  7176. into place.
  7177. This function gets called just before an entry or tree gets cut from the
  7178. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7179. called immediately, to move the markers with the entries."
  7180. (setq org-markers-to-move nil)
  7181. (when (featurep 'org-clock)
  7182. (org-clock-save-markers-for-cut-and-paste beg end))
  7183. (when (featurep 'org-agenda)
  7184. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7185. (defun org-check-and-save-marker (marker beg end)
  7186. "Check if MARKER is between BEG and END.
  7187. If yes, remember the marker and the distance to BEG."
  7188. (when (and (marker-buffer marker)
  7189. (equal (marker-buffer marker) (current-buffer)))
  7190. (if (and (>= marker beg) (< marker end))
  7191. (push (cons marker (- marker beg)) org-markers-to-move))))
  7192. (defun org-reinstall-markers-in-region (beg)
  7193. "Move all remembered markers to their position relative to BEG."
  7194. (mapc (lambda (x)
  7195. (move-marker (car x) (+ beg (cdr x))))
  7196. org-markers-to-move)
  7197. (setq org-markers-to-move nil))
  7198. (defun org-narrow-to-subtree ()
  7199. "Narrow buffer to the current subtree."
  7200. (interactive)
  7201. (save-excursion
  7202. (save-match-data
  7203. (org-with-limited-levels
  7204. (narrow-to-region
  7205. (progn (org-back-to-heading t) (point))
  7206. (progn (org-end-of-subtree t t)
  7207. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7208. (point)))))))
  7209. (defun org-narrow-to-block ()
  7210. "Narrow buffer to the current block."
  7211. (interactive)
  7212. (let* ((case-fold-search t)
  7213. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7214. "^[ \t]*#\\+end_.*")))
  7215. (if blockp
  7216. (narrow-to-region (car blockp) (cdr blockp))
  7217. (error "Not in a block"))))
  7218. (eval-when-compile
  7219. (defvar org-property-drawer-re))
  7220. (defvar org-property-start-re) ;; defined below
  7221. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7222. "Clone the task (subtree) at point N times.
  7223. The clones will be inserted as siblings.
  7224. In interactive use, the user will be prompted for the number of
  7225. clones to be produced, and for a time SHIFT, which may be a
  7226. repeater as used in time stamps, for example `+3d'.
  7227. When a valid repeater is given and the entry contains any time
  7228. stamps, the clones will become a sequence in time, with time
  7229. stamps in the subtree shifted for each clone produced. If SHIFT
  7230. is nil or the empty string, time stamps will be left alone. The
  7231. ID property of the original subtree is removed.
  7232. If the original subtree did contain time stamps with a repeater,
  7233. the following will happen:
  7234. - the repeater will be removed in each clone
  7235. - an additional clone will be produced, with the current, unshifted
  7236. date(s) in the entry.
  7237. - the original entry will be placed *after* all the clones, with
  7238. repeater intact.
  7239. - the start days in the repeater in the original entry will be shifted
  7240. to past the last clone.
  7241. In this way you can spell out a number of instances of a repeating task,
  7242. and still retain the repeater to cover future instances of the task."
  7243. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7244. (let (beg end template task idprop
  7245. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7246. (drawer-re org-drawer-regexp))
  7247. (if (not (and (integerp n) (> n 0)))
  7248. (error "Invalid number of replications %s" n))
  7249. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7250. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7251. shift)))
  7252. (error "Invalid shift specification %s" shift))
  7253. (when doshift
  7254. (setq shift-n (string-to-number (match-string 1 shift))
  7255. shift-what (cdr (assoc (match-string 2 shift)
  7256. '(("d" . day) ("w" . week)
  7257. ("m" . month) ("y" . year))))))
  7258. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7259. (setq nmin 1 nmax n)
  7260. (org-back-to-heading t)
  7261. (setq beg (point))
  7262. (setq idprop (org-entry-get nil "ID"))
  7263. (org-end-of-subtree t t)
  7264. (or (bolp) (insert "\n"))
  7265. (setq end (point))
  7266. (setq template (buffer-substring beg end))
  7267. (when (and doshift
  7268. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7269. (delete-region beg end)
  7270. (setq end beg)
  7271. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7272. (goto-char end)
  7273. (loop for n from nmin to nmax do
  7274. ;; prepare clone
  7275. (with-temp-buffer
  7276. (insert template)
  7277. (org-mode)
  7278. (goto-char (point-min))
  7279. (org-show-subtree)
  7280. (and idprop (if org-clone-delete-id
  7281. (org-entry-delete nil "ID")
  7282. (org-id-get-create t)))
  7283. (unless (= n 0)
  7284. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7285. (kill-whole-line))
  7286. (goto-char (point-min))
  7287. (while (re-search-forward drawer-re nil t)
  7288. (mapc (lambda (d)
  7289. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7290. (goto-char (point-min))
  7291. (when doshift
  7292. (while (re-search-forward org-ts-regexp-both nil t)
  7293. (org-timestamp-change (* n shift-n) shift-what))
  7294. (unless (= n n-no-remove)
  7295. (goto-char (point-min))
  7296. (while (re-search-forward org-ts-regexp nil t)
  7297. (save-excursion
  7298. (goto-char (match-beginning 0))
  7299. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7300. (delete-region (match-beginning 1) (match-end 1)))))))
  7301. (setq task (buffer-string)))
  7302. (insert task))
  7303. (goto-char beg)))
  7304. ;;; Outline Sorting
  7305. (defun org-sort (with-case)
  7306. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7307. Optional argument WITH-CASE means sort case-sensitively."
  7308. (interactive "P")
  7309. (cond
  7310. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7311. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7312. (t
  7313. (org-call-with-arg 'org-sort-entries with-case))))
  7314. (defun org-sort-remove-invisible (s)
  7315. (remove-text-properties 0 (length s) org-rm-props s)
  7316. (while (string-match org-bracket-link-regexp s)
  7317. (setq s (replace-match (if (match-end 2)
  7318. (match-string 3 s)
  7319. (match-string 1 s)) t t s)))
  7320. s)
  7321. (defvar org-priority-regexp) ; defined later in the file
  7322. (defvar org-after-sorting-entries-or-items-hook nil
  7323. "Hook that is run after a bunch of entries or items have been sorted.
  7324. When children are sorted, the cursor is in the parent line when this
  7325. hook gets called. When a region or a plain list is sorted, the cursor
  7326. will be in the first entry of the sorted region/list.")
  7327. (defun org-sort-entries
  7328. (&optional with-case sorting-type getkey-func compare-func property)
  7329. "Sort entries on a certain level of an outline tree.
  7330. If there is an active region, the entries in the region are sorted.
  7331. Else, if the cursor is before the first entry, sort the top-level items.
  7332. Else, the children of the entry at point are sorted.
  7333. Sorting can be alphabetically, numerically, by date/time as given by
  7334. a time stamp, by a property or by priority.
  7335. The command prompts for the sorting type unless it has been given to the
  7336. function through the SORTING-TYPE argument, which needs to be a character,
  7337. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7338. precise meaning of each character:
  7339. n Numerically, by converting the beginning of the entry/item to a number.
  7340. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7341. o By order of TODO keywords.
  7342. t By date/time, either the first active time stamp in the entry, or, if
  7343. none exist, by the first inactive one.
  7344. s By the scheduled date/time.
  7345. d By deadline date/time.
  7346. c By creation time, which is assumed to be the first inactive time stamp
  7347. at the beginning of a line.
  7348. p By priority according to the cookie.
  7349. r By the value of a property.
  7350. Capital letters will reverse the sort order.
  7351. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7352. called with point at the beginning of the record. It must return either
  7353. a string or a number that should serve as the sorting key for that record.
  7354. Comparing entries ignores case by default. However, with an optional argument
  7355. WITH-CASE, the sorting considers case as well."
  7356. (interactive "P")
  7357. (let ((case-func (if with-case 'identity 'downcase))
  7358. (cmstr
  7359. ;; The clock marker is lost when using `sort-subr', let's
  7360. ;; store the clocking string.
  7361. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7362. (save-excursion
  7363. (goto-char org-clock-marker)
  7364. (looking-back "^.*") (match-string-no-properties 0))))
  7365. start beg end stars re re2
  7366. txt what tmp)
  7367. ;; Find beginning and end of region to sort
  7368. (cond
  7369. ((org-region-active-p)
  7370. ;; we will sort the region
  7371. (setq end (region-end)
  7372. what "region")
  7373. (goto-char (region-beginning))
  7374. (if (not (org-at-heading-p)) (outline-next-heading))
  7375. (setq start (point)))
  7376. ((or (org-at-heading-p)
  7377. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7378. ;; we will sort the children of the current headline
  7379. (org-back-to-heading)
  7380. (setq start (point)
  7381. end (progn (org-end-of-subtree t t)
  7382. (or (bolp) (insert "\n"))
  7383. (org-back-over-empty-lines)
  7384. (point))
  7385. what "children")
  7386. (goto-char start)
  7387. (show-subtree)
  7388. (outline-next-heading))
  7389. (t
  7390. ;; we will sort the top-level entries in this file
  7391. (goto-char (point-min))
  7392. (or (org-at-heading-p) (outline-next-heading))
  7393. (setq start (point))
  7394. (goto-char (point-max))
  7395. (beginning-of-line 1)
  7396. (when (looking-at ".*?\\S-")
  7397. ;; File ends in a non-white line
  7398. (end-of-line 1)
  7399. (insert "\n"))
  7400. (setq end (point-max))
  7401. (setq what "top-level")
  7402. (goto-char start)
  7403. (show-all)))
  7404. (setq beg (point))
  7405. (if (>= beg end) (error "Nothing to sort"))
  7406. (looking-at "\\(\\*+\\)")
  7407. (setq stars (match-string 1)
  7408. re (concat "^" (regexp-quote stars) " +")
  7409. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7410. txt (buffer-substring beg end))
  7411. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7412. (if (and (not (equal stars "*")) (string-match re2 txt))
  7413. (error "Region to sort contains a level above the first entry"))
  7414. (unless sorting-type
  7415. (message
  7416. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7417. [t]ime [s]cheduled [d]eadline [c]reated
  7418. A/N/P/R/O/F/T/S/D/C means reversed:"
  7419. what)
  7420. (setq sorting-type (read-char-exclusive))
  7421. (unless getkey-func
  7422. (and (= (downcase sorting-type) ?f)
  7423. (setq getkey-func
  7424. (org-icompleting-read "Sort using function: "
  7425. obarray 'fboundp t nil nil))
  7426. (setq getkey-func (intern getkey-func))))
  7427. (and (= (downcase sorting-type) ?r)
  7428. (not property)
  7429. (setq property
  7430. (org-icompleting-read "Property: "
  7431. (mapcar 'list (org-buffer-property-keys t))
  7432. nil t))))
  7433. (message "Sorting entries...")
  7434. (save-restriction
  7435. (narrow-to-region start end)
  7436. (let ((dcst (downcase sorting-type))
  7437. (case-fold-search nil)
  7438. (now (current-time)))
  7439. (sort-subr
  7440. (/= dcst sorting-type)
  7441. ;; This function moves to the beginning character of the "record" to
  7442. ;; be sorted.
  7443. (lambda nil
  7444. (if (re-search-forward re nil t)
  7445. (goto-char (match-beginning 0))
  7446. (goto-char (point-max))))
  7447. ;; This function moves to the last character of the "record" being
  7448. ;; sorted.
  7449. (lambda nil
  7450. (save-match-data
  7451. (condition-case nil
  7452. (outline-forward-same-level 1)
  7453. (error
  7454. (goto-char (point-max))))))
  7455. ;; This function returns the value that gets sorted against.
  7456. (lambda nil
  7457. (cond
  7458. ((= dcst ?n)
  7459. (if (looking-at org-complex-heading-regexp)
  7460. (string-to-number (match-string 4))
  7461. nil))
  7462. ((= dcst ?a)
  7463. (if (looking-at org-complex-heading-regexp)
  7464. (funcall case-func (match-string 4))
  7465. nil))
  7466. ((= dcst ?t)
  7467. (let ((end (save-excursion (outline-next-heading) (point))))
  7468. (if (or (re-search-forward org-ts-regexp end t)
  7469. (re-search-forward org-ts-regexp-both end t))
  7470. (org-time-string-to-seconds (match-string 0))
  7471. (org-float-time now))))
  7472. ((= dcst ?c)
  7473. (let ((end (save-excursion (outline-next-heading) (point))))
  7474. (if (re-search-forward
  7475. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7476. end t)
  7477. (org-time-string-to-seconds (match-string 0))
  7478. (org-float-time now))))
  7479. ((= dcst ?s)
  7480. (let ((end (save-excursion (outline-next-heading) (point))))
  7481. (if (re-search-forward org-scheduled-time-regexp end t)
  7482. (org-time-string-to-seconds (match-string 1))
  7483. (org-float-time now))))
  7484. ((= dcst ?d)
  7485. (let ((end (save-excursion (outline-next-heading) (point))))
  7486. (if (re-search-forward org-deadline-time-regexp end t)
  7487. (org-time-string-to-seconds (match-string 1))
  7488. (org-float-time now))))
  7489. ((= dcst ?p)
  7490. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7491. (string-to-char (match-string 2))
  7492. org-default-priority))
  7493. ((= dcst ?r)
  7494. (or (org-entry-get nil property) ""))
  7495. ((= dcst ?o)
  7496. (if (looking-at org-complex-heading-regexp)
  7497. (- 9999 (length (member (match-string 2)
  7498. org-todo-keywords-1)))))
  7499. ((= dcst ?f)
  7500. (if getkey-func
  7501. (progn
  7502. (setq tmp (funcall getkey-func))
  7503. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7504. tmp)
  7505. (error "Invalid key function `%s'" getkey-func)))
  7506. (t (error "Invalid sorting type `%c'" sorting-type))))
  7507. nil
  7508. (cond
  7509. ((= dcst ?a) 'string<)
  7510. ((= dcst ?f) compare-func)
  7511. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7512. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7513. ;; Reset the clock marker if needed
  7514. (when cmstr
  7515. (save-excursion
  7516. (goto-char start)
  7517. (search-forward cmstr nil t)
  7518. (move-marker org-clock-marker (point))))
  7519. (message "Sorting entries...done")))
  7520. (defun org-do-sort (table what &optional with-case sorting-type)
  7521. "Sort TABLE of WHAT according to SORTING-TYPE.
  7522. The user will be prompted for the SORTING-TYPE if the call to this
  7523. function does not specify it. WHAT is only for the prompt, to indicate
  7524. what is being sorted. The sorting key will be extracted from
  7525. the car of the elements of the table.
  7526. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7527. (unless sorting-type
  7528. (message
  7529. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7530. what)
  7531. (setq sorting-type (read-char-exclusive)))
  7532. (let ((dcst (downcase sorting-type))
  7533. extractfun comparefun)
  7534. ;; Define the appropriate functions
  7535. (cond
  7536. ((= dcst ?n)
  7537. (setq extractfun 'string-to-number
  7538. comparefun (if (= dcst sorting-type) '< '>)))
  7539. ((= dcst ?a)
  7540. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7541. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7542. comparefun (if (= dcst sorting-type)
  7543. 'string<
  7544. (lambda (a b) (and (not (string< a b))
  7545. (not (string= a b)))))))
  7546. ((= dcst ?t)
  7547. (setq extractfun
  7548. (lambda (x)
  7549. (if (or (string-match org-ts-regexp x)
  7550. (string-match org-ts-regexp-both x))
  7551. (org-float-time
  7552. (org-time-string-to-time (match-string 0 x)))
  7553. 0))
  7554. comparefun (if (= dcst sorting-type) '< '>)))
  7555. (t (error "Invalid sorting type `%c'" sorting-type)))
  7556. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7557. table)
  7558. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7559. ;;; The orgstruct minor mode
  7560. ;; Define a minor mode which can be used in other modes in order to
  7561. ;; integrate the org-mode structure editing commands.
  7562. ;; This is really a hack, because the org-mode structure commands use
  7563. ;; keys which normally belong to the major mode. Here is how it
  7564. ;; works: The minor mode defines all the keys necessary to operate the
  7565. ;; structure commands, but wraps the commands into a function which
  7566. ;; tests if the cursor is currently at a headline or a plain list
  7567. ;; item. If that is the case, the structure command is used,
  7568. ;; temporarily setting many Org-mode variables like regular
  7569. ;; expressions for filling etc. However, when any of those keys is
  7570. ;; used at a different location, function uses `key-binding' to look
  7571. ;; up if the key has an associated command in another currently active
  7572. ;; keymap (minor modes, major mode, global), and executes that
  7573. ;; command. There might be problems if any of the keys is otherwise
  7574. ;; used as a prefix key.
  7575. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7576. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7577. ;; addresses this by checking explicitly for both bindings.
  7578. (defvar orgstruct-mode-map (make-sparse-keymap)
  7579. "Keymap for the minor `orgstruct-mode'.")
  7580. (defvar org-local-vars nil
  7581. "List of local variables, for use by `orgstruct-mode'.")
  7582. ;;;###autoload
  7583. (define-minor-mode orgstruct-mode
  7584. "Toggle the minor mode `orgstruct-mode'.
  7585. This mode is for using Org-mode structure commands in other
  7586. modes. The following keys behave as if Org-mode were active, if
  7587. the cursor is on a headline, or on a plain list item (both as
  7588. defined by Org-mode).
  7589. M-up Move entry/item up
  7590. M-down Move entry/item down
  7591. M-left Promote
  7592. M-right Demote
  7593. M-S-up Move entry/item up
  7594. M-S-down Move entry/item down
  7595. M-S-left Promote subtree
  7596. M-S-right Demote subtree
  7597. M-q Fill paragraph and items like in Org-mode
  7598. C-c ^ Sort entries
  7599. C-c - Cycle list bullet
  7600. TAB Cycle item visibility
  7601. M-RET Insert new heading/item
  7602. S-M-RET Insert new TODO heading / Checkbox item
  7603. C-c C-c Set tags / toggle checkbox"
  7604. nil " OrgStruct" nil
  7605. (org-load-modules-maybe)
  7606. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7607. ;;;###autoload
  7608. (defun turn-on-orgstruct ()
  7609. "Unconditionally turn on `orgstruct-mode'."
  7610. (orgstruct-mode 1))
  7611. (defvar org-fb-vars nil)
  7612. (make-variable-buffer-local 'org-fb-vars)
  7613. (defun orgstruct++-mode (&optional arg)
  7614. "Toggle `orgstruct-mode', the enhanced version of it.
  7615. In addition to setting orgstruct-mode, this also exports all
  7616. indentation and autofilling variables from org-mode into the
  7617. buffer. It will also recognize item context in multiline items."
  7618. (interactive "P")
  7619. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7620. (if (< arg 1)
  7621. (progn (orgstruct-mode -1)
  7622. (mapc (lambda(v)
  7623. (org-set-local (car v)
  7624. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7625. org-fb-vars))
  7626. (orgstruct-mode 1)
  7627. (setq org-fb-vars nil)
  7628. (let (var val)
  7629. (mapc
  7630. (lambda (x)
  7631. (when (string-match
  7632. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7633. (symbol-name (car x)))
  7634. (setq var (car x) val (nth 1 x))
  7635. (push (list var `(quote ,(eval var))) org-fb-vars)
  7636. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7637. org-local-vars)
  7638. (org-set-local 'orgstruct-is-++ t))))
  7639. (defvar orgstruct-is-++ nil
  7640. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7641. (make-variable-buffer-local 'orgstruct-is-++)
  7642. ;;;###autoload
  7643. (defun turn-on-orgstruct++ ()
  7644. "Unconditionally turn on `orgstruct++-mode'."
  7645. (orgstruct++-mode 1))
  7646. (defun orgstruct-error ()
  7647. "Error when there is no default binding for a structure key."
  7648. (interactive)
  7649. (error "This key has no function outside structure elements"))
  7650. (defun orgstruct-setup ()
  7651. "Setup orgstruct keymaps."
  7652. (let ((nfunc 0)
  7653. (bindings
  7654. (list
  7655. '([(meta up)] org-metaup)
  7656. '([(meta down)] org-metadown)
  7657. '([(meta left)] org-metaleft)
  7658. '([(meta right)] org-metaright)
  7659. '([(meta shift up)] org-shiftmetaup)
  7660. '([(meta shift down)] org-shiftmetadown)
  7661. '([(meta shift left)] org-shiftmetaleft)
  7662. '([(meta shift right)] org-shiftmetaright)
  7663. '([?\e (up)] org-metaup)
  7664. '([?\e (down)] org-metadown)
  7665. '([?\e (left)] org-metaleft)
  7666. '([?\e (right)] org-metaright)
  7667. '([?\e (shift up)] org-shiftmetaup)
  7668. '([?\e (shift down)] org-shiftmetadown)
  7669. '([?\e (shift left)] org-shiftmetaleft)
  7670. '([?\e (shift right)] org-shiftmetaright)
  7671. '([(shift up)] org-shiftup)
  7672. '([(shift down)] org-shiftdown)
  7673. '([(shift left)] org-shiftleft)
  7674. '([(shift right)] org-shiftright)
  7675. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7676. '("\M-q" fill-paragraph)
  7677. '("\C-c^" org-sort)
  7678. '("\C-c-" org-cycle-list-bullet)))
  7679. elt key fun cmd)
  7680. (while (setq elt (pop bindings))
  7681. (setq nfunc (1+ nfunc))
  7682. (setq key (org-key (car elt))
  7683. fun (nth 1 elt)
  7684. cmd (orgstruct-make-binding fun nfunc key))
  7685. (org-defkey orgstruct-mode-map key cmd))
  7686. ;; Prevent an error for users who forgot to make autoloads
  7687. (require 'org-element)
  7688. ;; Special treatment needed for TAB and RET
  7689. (org-defkey orgstruct-mode-map [(tab)]
  7690. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7691. (org-defkey orgstruct-mode-map "\C-i"
  7692. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7693. (org-defkey orgstruct-mode-map "\M-\C-m"
  7694. (orgstruct-make-binding 'org-insert-heading 105
  7695. "\M-\C-m" [(meta return)]))
  7696. (org-defkey orgstruct-mode-map [(meta return)]
  7697. (orgstruct-make-binding 'org-insert-heading 106
  7698. [(meta return)] "\M-\C-m"))
  7699. (org-defkey orgstruct-mode-map [(shift meta return)]
  7700. (orgstruct-make-binding 'org-insert-todo-heading 107
  7701. [(meta return)] "\M-\C-m"))
  7702. (org-defkey orgstruct-mode-map "\e\C-m"
  7703. (orgstruct-make-binding 'org-insert-heading 108
  7704. "\e\C-m" [?\e (return)]))
  7705. (org-defkey orgstruct-mode-map [?\e (return)]
  7706. (orgstruct-make-binding 'org-insert-heading 109
  7707. [?\e (return)] "\e\C-m"))
  7708. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7709. (orgstruct-make-binding 'org-insert-todo-heading 110
  7710. [?\e (return)] "\e\C-m"))
  7711. (unless org-local-vars
  7712. (setq org-local-vars (org-get-local-variables)))
  7713. t))
  7714. (defun orgstruct-make-binding (fun n &rest keys)
  7715. "Create a function for binding in the structure minor mode.
  7716. FUN is the command to call inside a table. N is used to create a unique
  7717. command name. KEYS are keys that should be checked in for a command
  7718. to execute outside of tables."
  7719. (eval
  7720. (list 'defun
  7721. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7722. '(arg)
  7723. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7724. "Outside of structure, run the binding of `"
  7725. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7726. "'.")
  7727. '(interactive "p")
  7728. (list 'if
  7729. `(org-context-p 'headline 'item
  7730. (and orgstruct-is-++
  7731. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7732. 'item-body))
  7733. (list 'org-run-like-in-org-mode (list 'quote fun))
  7734. (list 'let '(orgstruct-mode)
  7735. (list 'call-interactively
  7736. (append '(or)
  7737. (mapcar (lambda (k)
  7738. (list 'key-binding k))
  7739. keys)
  7740. '('orgstruct-error))))))))
  7741. (defun org-contextualize-keys (alist contexts)
  7742. "Return valid elements in ALIST depending on CONTEXTS.
  7743. `org-agenda-custom-commands' or `org-capture-templates' are the
  7744. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7745. or `org-capture-templates-contexts' are the associated contexts
  7746. definitions."
  7747. (let ((contexts
  7748. ;; normalize contexts
  7749. (mapcar
  7750. (lambda(c) (cond ((listp (cadr c))
  7751. (list (car c) (car c) (cadr c)))
  7752. ((string= "" (cadr c))
  7753. (list (car c) (car c) (caddr c)))
  7754. (t c))) contexts))
  7755. (a alist) c r s)
  7756. ;; loop over all commands or templates
  7757. (while (setq c (pop a))
  7758. (let (vrules repl)
  7759. (cond
  7760. ((not (assoc (car c) contexts))
  7761. (push c r))
  7762. ((and (assoc (car c) contexts)
  7763. (setq vrules (org-contextualize-validate-key
  7764. (car c) contexts)))
  7765. (mapc (lambda (vr)
  7766. (when (not (equal (car vr) (cadr vr)))
  7767. (setq repl vr))) vrules)
  7768. (if (not repl) (push c r)
  7769. (push (cadr repl) s)
  7770. (push
  7771. (cons (car c)
  7772. (cdr (or (assoc (cadr repl) alist)
  7773. (error "Undefined key `%s' as contextual replacement for `%s'"
  7774. (cadr repl) (car c)))))
  7775. r))))))
  7776. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7777. (delq
  7778. nil
  7779. (delete-dups
  7780. (mapcar (lambda (x)
  7781. (let ((tpl (car x)))
  7782. (when (not (delq
  7783. nil
  7784. (mapcar (lambda(y)
  7785. (equal y tpl)) s))) x)))
  7786. (reverse r))))))
  7787. (defun org-contextualize-validate-key (key contexts)
  7788. "Check CONTEXTS for agenda or capture KEY."
  7789. (let (r rr res)
  7790. (while (setq r (pop contexts))
  7791. (mapc
  7792. (lambda (rr)
  7793. (when
  7794. (and (equal key (car r))
  7795. (if (functionp rr) (funcall rr)
  7796. (or (and (eq (car rr) 'in-file)
  7797. (buffer-file-name)
  7798. (string-match (cdr rr) (buffer-file-name)))
  7799. (and (eq (car rr) 'in-mode)
  7800. (string-match (cdr rr) (symbol-name major-mode)))
  7801. (when (and (eq (car rr) 'not-in-file)
  7802. (buffer-file-name))
  7803. (not (string-match (cdr rr) (buffer-file-name))))
  7804. (when (eq (car rr) 'not-in-mode)
  7805. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7806. (push r res)))
  7807. (car (last r))))
  7808. (delete-dups (delq nil res))))
  7809. (defun org-context-p (&rest contexts)
  7810. "Check if local context is any of CONTEXTS.
  7811. Possible values in the list of contexts are `table', `headline', and `item'."
  7812. (let ((pos (point)))
  7813. (goto-char (point-at-bol))
  7814. (prog1 (or (and (memq 'table contexts)
  7815. (looking-at "[ \t]*|"))
  7816. (and (memq 'headline contexts)
  7817. (looking-at org-outline-regexp))
  7818. (and (memq 'item contexts)
  7819. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7820. (and (memq 'item-body contexts)
  7821. (org-in-item-p)))
  7822. (goto-char pos))))
  7823. (defun org-get-local-variables ()
  7824. "Return a list of all local variables in an Org mode buffer."
  7825. (let (varlist)
  7826. (with-current-buffer (get-buffer-create "*Org tmp*")
  7827. (erase-buffer)
  7828. (org-mode)
  7829. (setq varlist (buffer-local-variables)))
  7830. (kill-buffer "*Org tmp*")
  7831. (delq nil
  7832. (mapcar
  7833. (lambda (x)
  7834. (setq x
  7835. (if (symbolp x)
  7836. (list x)
  7837. (list (car x) (list 'quote (cdr x)))))
  7838. (if (string-match
  7839. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7840. (symbol-name (car x)))
  7841. x nil))
  7842. varlist))))
  7843. (defun org-clone-local-variables (from-buffer &optional regexp)
  7844. "Clone local variables from FROM-BUFFER.
  7845. Optional argument REGEXP selects variables to clone."
  7846. (mapc
  7847. (lambda (pair)
  7848. (and (symbolp (car pair))
  7849. (or (null regexp)
  7850. (string-match regexp (symbol-name (car pair))))
  7851. (set (make-local-variable (car pair))
  7852. (cdr pair))))
  7853. (buffer-local-variables from-buffer)))
  7854. ;;;###autoload
  7855. (defun org-run-like-in-org-mode (cmd)
  7856. "Run a command, pretending that the current buffer is in Org-mode.
  7857. This will temporarily bind local variables that are typically bound in
  7858. Org-mode to the values they have in Org-mode, and then interactively
  7859. call CMD."
  7860. (org-load-modules-maybe)
  7861. (unless org-local-vars
  7862. (setq org-local-vars (org-get-local-variables)))
  7863. (eval (list 'let org-local-vars
  7864. (list 'call-interactively (list 'quote cmd)))))
  7865. ;;;; Archiving
  7866. (defun org-get-category (&optional pos force-refresh)
  7867. "Get the category applying to position POS."
  7868. (save-match-data
  7869. (if force-refresh (org-refresh-category-properties))
  7870. (let ((pos (or pos (point))))
  7871. (or (get-text-property pos 'org-category)
  7872. (progn (org-refresh-category-properties)
  7873. (get-text-property pos 'org-category))))))
  7874. (defun org-refresh-category-properties ()
  7875. "Refresh category text properties in the buffer."
  7876. (let ((case-fold-search t)
  7877. (inhibit-read-only t)
  7878. (def-cat (cond
  7879. ((null org-category)
  7880. (if buffer-file-name
  7881. (file-name-sans-extension
  7882. (file-name-nondirectory buffer-file-name))
  7883. "???"))
  7884. ((symbolp org-category) (symbol-name org-category))
  7885. (t org-category)))
  7886. beg end cat pos optionp)
  7887. (org-unmodified
  7888. (save-excursion
  7889. (save-restriction
  7890. (widen)
  7891. (goto-char (point-min))
  7892. (put-text-property (point) (point-max) 'org-category def-cat)
  7893. (while (re-search-forward
  7894. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7895. (setq pos (match-end 0)
  7896. optionp (equal (char-after (match-beginning 0)) ?#)
  7897. cat (org-trim (match-string 2)))
  7898. (if optionp
  7899. (setq beg (point-at-bol) end (point-max))
  7900. (org-back-to-heading t)
  7901. (setq beg (point) end (org-end-of-subtree t t)))
  7902. (put-text-property beg end 'org-category cat)
  7903. (put-text-property beg end 'org-category-position beg)
  7904. (goto-char pos)))))))
  7905. ;;;; Link Stuff
  7906. ;;; Link abbreviations
  7907. (defun org-link-expand-abbrev (link)
  7908. "Apply replacements as defined in `org-link-abbrev-alist'."
  7909. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7910. (let* ((key (match-string 1 link))
  7911. (as (or (assoc key org-link-abbrev-alist-local)
  7912. (assoc key org-link-abbrev-alist)))
  7913. (tag (and (match-end 2) (match-string 3 link)))
  7914. rpl)
  7915. (if (not as)
  7916. link
  7917. (setq rpl (cdr as))
  7918. (cond
  7919. ((symbolp rpl) (funcall rpl tag))
  7920. ((string-match "%(\\([^)]+\\))" rpl)
  7921. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7922. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7923. ((string-match "%h" rpl)
  7924. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7925. (t (concat rpl tag)))))
  7926. link))
  7927. ;;; Storing and inserting links
  7928. (defvar org-insert-link-history nil
  7929. "Minibuffer history for links inserted with `org-insert-link'.")
  7930. (defvar org-stored-links nil
  7931. "Contains the links stored with `org-store-link'.")
  7932. (defvar org-store-link-plist nil
  7933. "Plist with info about the most recently link created with `org-store-link'.")
  7934. (defvar org-link-protocols nil
  7935. "Link protocols added to Org-mode using `org-add-link-type'.")
  7936. (defvar org-store-link-functions nil
  7937. "List of functions that are called to create and store a link.
  7938. Each function will be called in turn until one returns a non-nil
  7939. value. Each function should check if it is responsible for creating
  7940. this link (for example by looking at the major mode).
  7941. If not, it must exit and return nil.
  7942. If yes, it should return a non-nil value after a calling
  7943. `org-store-link-props' with a list of properties and values.
  7944. Special properties are:
  7945. :type The link prefix, like \"http\". This must be given.
  7946. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7947. This is obligatory as well.
  7948. :description Optional default description for the second pair
  7949. of brackets in an Org-mode link. The user can still change
  7950. this when inserting this link into an Org-mode buffer.
  7951. In addition to these, any additional properties can be specified
  7952. and then used in capture templates.")
  7953. (defun org-add-link-type (type &optional follow export)
  7954. "Add TYPE to the list of `org-link-types'.
  7955. Re-compute all regular expressions depending on `org-link-types'
  7956. FOLLOW and EXPORT are two functions.
  7957. FOLLOW should take the link path as the single argument and do whatever
  7958. is necessary to follow the link, for example find a file or display
  7959. a mail message.
  7960. EXPORT should format the link path for export to one of the export formats.
  7961. It should be a function accepting three arguments:
  7962. path the path of the link, the text after the prefix (like \"http:\")
  7963. desc the description of the link, if any, or a description added by
  7964. org-export-normalize-links if there is none
  7965. format the export format, a symbol like `html' or `latex' or `ascii'..
  7966. The function may use the FORMAT information to return different values
  7967. depending on the format. The return value will be put literally into
  7968. the exported file. If the return value is nil, this means Org should
  7969. do what it normally does with links which do not have EXPORT defined.
  7970. Org-mode has a built-in default for exporting links. If you are happy with
  7971. this default, there is no need to define an export function for the link
  7972. type. For a simple example of an export function, see `org-bbdb.el'."
  7973. (add-to-list 'org-link-types type t)
  7974. (org-make-link-regexps)
  7975. (if (assoc type org-link-protocols)
  7976. (setcdr (assoc type org-link-protocols) (list follow export))
  7977. (push (list type follow export) org-link-protocols)))
  7978. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7979. (defvar org-link-to-org-use-id) ; Defined in org-id.el
  7980. ;;;###autoload
  7981. (defun org-store-link (arg)
  7982. "\\<org-mode-map>Store an org-link to the current location.
  7983. This link is added to `org-stored-links' and can later be inserted
  7984. into an org-buffer with \\[org-insert-link].
  7985. For some link types, a prefix arg is interpreted.
  7986. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  7987. For file links, arg negates `org-context-in-file-links'.
  7988. A double prefix arg force skipping storing functions that are not
  7989. part of Org's core."
  7990. (interactive "P")
  7991. (org-load-modules-maybe)
  7992. (setq org-store-link-plist nil) ; reset
  7993. (org-with-limited-levels
  7994. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  7995. (cond
  7996. ((and (not (equal arg '(16)))
  7997. (setq sfuns
  7998. (delq
  7999. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8000. org-store-link-functions))
  8001. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8002. (or (and (cdr sfuns)
  8003. (funcall (intern
  8004. (completing-read "Which function for creating the link? "
  8005. sfunsn t (car sfunsn)))))
  8006. (funcall (caar sfuns)))
  8007. (setq link (plist-get org-store-link-plist :link)
  8008. desc (or (plist-get org-store-link-plist :description) link))))
  8009. ((org-src-edit-buffer-p)
  8010. (let (label gc)
  8011. (while (or (not label)
  8012. (save-excursion
  8013. (save-restriction
  8014. (widen)
  8015. (goto-char (point-min))
  8016. (re-search-forward
  8017. (regexp-quote (format org-coderef-label-format label))
  8018. nil t))))
  8019. (when label (message "Label exists already") (sit-for 2))
  8020. (setq label (read-string "Code line label: " label)))
  8021. (end-of-line 1)
  8022. (setq link (format org-coderef-label-format label))
  8023. (setq gc (- 79 (length link)))
  8024. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8025. (insert link)
  8026. (setq link (concat "(" label ")") desc nil)))
  8027. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8028. ;; We are in the agenda, link to referenced location
  8029. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8030. (get-text-property (point) 'org-marker))))
  8031. (when m
  8032. (org-with-point-at m
  8033. (setq agenda-link
  8034. (if (org-called-interactively-p 'any)
  8035. (call-interactively 'org-store-link)
  8036. (org-store-link nil)))))))
  8037. ((eq major-mode 'calendar-mode)
  8038. (let ((cd (calendar-cursor-to-date)))
  8039. (setq link
  8040. (format-time-string
  8041. (car org-time-stamp-formats)
  8042. (apply 'encode-time
  8043. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8044. nil nil nil))))
  8045. (org-store-link-props :type "calendar" :date cd)))
  8046. ((eq major-mode 'help-mode)
  8047. (setq link (concat "help:" (save-excursion
  8048. (goto-char (point-min))
  8049. (looking-at "^[^ ]+")
  8050. (match-string 0))))
  8051. (org-store-link-props :type "help"))
  8052. ((eq major-mode 'w3-mode)
  8053. (setq cpltxt (if (and (buffer-name)
  8054. (not (string-match "Untitled" (buffer-name))))
  8055. (buffer-name)
  8056. (url-view-url t))
  8057. link (url-view-url t))
  8058. (org-store-link-props :type "w3" :url (url-view-url t)))
  8059. ((setq search (run-hook-with-args-until-success
  8060. 'org-create-file-search-functions))
  8061. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8062. "::" search))
  8063. (setq cpltxt (or description link)))
  8064. ((eq major-mode 'image-mode)
  8065. (setq cpltxt (concat "file:"
  8066. (abbreviate-file-name buffer-file-name))
  8067. link cpltxt)
  8068. (org-store-link-props :type "image" :file buffer-file-name))
  8069. ((eq major-mode 'dired-mode)
  8070. ;; link to the file in the current line
  8071. (let ((file (dired-get-filename nil t)))
  8072. (setq file (if file
  8073. (abbreviate-file-name
  8074. (expand-file-name (dired-get-filename nil t)))
  8075. ;; otherwise, no file so use current directory.
  8076. default-directory))
  8077. (setq cpltxt (concat "file:" file)
  8078. link cpltxt)))
  8079. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8080. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8081. (cond
  8082. ((org-in-regexp "<<\\(.*?\\)>>")
  8083. (setq cpltxt
  8084. (concat "file:"
  8085. (abbreviate-file-name
  8086. (buffer-file-name (buffer-base-buffer)))
  8087. "::" (match-string 1))
  8088. link cpltxt))
  8089. ((and (featurep 'org-id)
  8090. (or (eq org-link-to-org-use-id t)
  8091. (and (org-called-interactively-p 'any)
  8092. (or (eq org-link-to-org-use-id 'create-if-interactive)
  8093. (and (eq org-link-to-org-use-id
  8094. 'create-if-interactive-and-no-custom-id)
  8095. (not custom-id))))
  8096. (and org-link-to-org-use-id (org-entry-get nil "ID"))))
  8097. ;; We can make a link using the ID.
  8098. (setq link (condition-case nil
  8099. (prog1 (org-id-store-link)
  8100. (setq desc (plist-get org-store-link-plist :description)))
  8101. (error
  8102. ;; probably before first headline, link to file only
  8103. (concat "file:"
  8104. (abbreviate-file-name
  8105. (buffer-file-name (buffer-base-buffer))))))))
  8106. (t
  8107. ;; Just link to current headline
  8108. (setq cpltxt (concat "file:"
  8109. (abbreviate-file-name
  8110. (buffer-file-name (buffer-base-buffer)))))
  8111. ;; Add a context search string
  8112. (when (org-xor org-context-in-file-links arg)
  8113. (let ((e (org-element-at-point)))
  8114. (setq txt (cond
  8115. ((org-at-heading-p) nil)
  8116. ((eq (org-element-type e) 'keyword)
  8117. (plist-get (cadr e) :value))
  8118. ((org-region-active-p)
  8119. (buffer-substring (region-beginning) (region-end))))))
  8120. (when (or (null txt) (string-match "\\S-" txt))
  8121. (setq cpltxt
  8122. (concat cpltxt "::"
  8123. (condition-case nil
  8124. (org-make-org-heading-search-string txt)
  8125. (error "")))
  8126. desc (or (nth 4 (ignore-errors
  8127. (org-heading-components))) "NONE"))))
  8128. (if (string-match "::\\'" cpltxt)
  8129. (setq cpltxt (substring cpltxt 0 -2)))
  8130. (setq link cpltxt))))
  8131. ((buffer-file-name (buffer-base-buffer))
  8132. ;; Just link to this file here.
  8133. (setq cpltxt (concat "file:"
  8134. (abbreviate-file-name
  8135. (buffer-file-name (buffer-base-buffer)))))
  8136. ;; Add a context string
  8137. (when (org-xor org-context-in-file-links arg)
  8138. (setq txt (if (org-region-active-p)
  8139. (buffer-substring (region-beginning) (region-end))
  8140. (buffer-substring (point-at-bol) (point-at-eol))))
  8141. ;; Only use search option if there is some text.
  8142. (when (string-match "\\S-" txt)
  8143. (setq cpltxt
  8144. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8145. desc "NONE")))
  8146. (setq link cpltxt))
  8147. ((org-called-interactively-p 'interactive)
  8148. (user-error "No method for storing a link from this buffer"))
  8149. (t (setq link nil)))
  8150. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8151. (setq link (or link cpltxt)
  8152. desc (or desc cpltxt))
  8153. (cond ((equal desc "NONE") (setq desc nil))
  8154. ((string-match org-bracket-link-regexp desc)
  8155. (setq desc (replace-regexp-in-string
  8156. org-bracket-link-regexp
  8157. (concat "\\3" (if (equal (length (match-string 0 desc))
  8158. (length desc)) "*" "")) desc))))
  8159. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8160. (progn
  8161. (setq org-stored-links
  8162. (cons (list link desc) org-stored-links))
  8163. (message "Stored: %s" (or desc link))
  8164. (when custom-id
  8165. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8166. "::#" custom-id))
  8167. (setq org-stored-links
  8168. (cons (list link desc) org-stored-links))))
  8169. (or agenda-link (and link (org-make-link-string link desc)))))))
  8170. (defun org-store-link-props (&rest plist)
  8171. "Store link properties, extract names and addresses."
  8172. (let (x adr)
  8173. (when (setq x (plist-get plist :from))
  8174. (setq adr (mail-extract-address-components x))
  8175. (setq plist (plist-put plist :fromname (car adr)))
  8176. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8177. (when (setq x (plist-get plist :to))
  8178. (setq adr (mail-extract-address-components x))
  8179. (setq plist (plist-put plist :toname (car adr)))
  8180. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8181. (let ((from (plist-get plist :from))
  8182. (to (plist-get plist :to)))
  8183. (when (and from to org-from-is-user-regexp)
  8184. (setq plist
  8185. (plist-put plist :fromto
  8186. (if (string-match org-from-is-user-regexp from)
  8187. (concat "to %t")
  8188. (concat "from %f"))))))
  8189. (setq org-store-link-plist plist))
  8190. (defun org-add-link-props (&rest plist)
  8191. "Add these properties to the link property list."
  8192. (let (key value)
  8193. (while plist
  8194. (setq key (pop plist) value (pop plist))
  8195. (setq org-store-link-plist
  8196. (plist-put org-store-link-plist key value)))))
  8197. (defun org-email-link-description (&optional fmt)
  8198. "Return the description part of an email link.
  8199. This takes information from `org-store-link-plist' and formats it
  8200. according to FMT (default from `org-email-link-description-format')."
  8201. (setq fmt (or fmt org-email-link-description-format))
  8202. (let* ((p org-store-link-plist)
  8203. (to (plist-get p :toaddress))
  8204. (from (plist-get p :fromaddress))
  8205. (table
  8206. (list
  8207. (cons "%c" (plist-get p :fromto))
  8208. (cons "%F" (plist-get p :from))
  8209. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8210. (cons "%T" (plist-get p :to))
  8211. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8212. (cons "%s" (plist-get p :subject))
  8213. (cons "%d" (plist-get p :date))
  8214. (cons "%m" (plist-get p :message-id)))))
  8215. (when (string-match "%c" fmt)
  8216. ;; Check if the user wrote this message
  8217. (if (and org-from-is-user-regexp from to
  8218. (save-match-data (string-match org-from-is-user-regexp from)))
  8219. (setq fmt (replace-match "to %t" t t fmt))
  8220. (setq fmt (replace-match "from %f" t t fmt))))
  8221. (org-replace-escapes fmt table)))
  8222. (defun org-make-org-heading-search-string (&optional string)
  8223. "Make search string for the current headline or STRING."
  8224. (let ((s (or string
  8225. (and (derived-mode-p 'org-mode)
  8226. (save-excursion
  8227. (org-back-to-heading t)
  8228. (plist-get (cadr (org-element-at-point))
  8229. :raw-value)))))
  8230. (lines org-context-in-file-links))
  8231. (or string (setq s (concat "*" s))) ; Add * for headlines
  8232. (when (and string (integerp lines) (> lines 0))
  8233. (let ((slines (org-split-string s "\n")))
  8234. (when (< lines (length slines))
  8235. (setq s (mapconcat
  8236. 'identity
  8237. (reverse (nthcdr (- (length slines) lines)
  8238. (reverse slines))) "\n")))))
  8239. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8240. (defun org-make-link-string (link &optional description)
  8241. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8242. (unless (string-match "\\S-" link)
  8243. (error "Empty link"))
  8244. (when (and description
  8245. (stringp description)
  8246. (not (string-match "\\S-" description)))
  8247. (setq description nil))
  8248. (when (stringp description)
  8249. ;; Remove brackets from the description, they are fatal.
  8250. (while (string-match "\\[" description)
  8251. (setq description (replace-match "{" t t description)))
  8252. (while (string-match "\\]" description)
  8253. (setq description (replace-match "}" t t description))))
  8254. (when (equal link description)
  8255. ;; No description needed, it is identical
  8256. (setq description nil))
  8257. (when (and (not description)
  8258. (not (string-match (org-image-file-name-regexp) link))
  8259. (not (equal link (org-link-escape link))))
  8260. (setq description (org-extract-attributes link)))
  8261. (setq link
  8262. (cond ((string-match (org-image-file-name-regexp) link) link)
  8263. ((string-match org-link-types-re link)
  8264. (concat (match-string 1 link)
  8265. (org-link-escape (substring link (match-end 1)))))
  8266. (t (org-link-escape link))))
  8267. (concat "[[" link "]"
  8268. (if description (concat "[" description "]") "")
  8269. "]"))
  8270. (defconst org-link-escape-chars
  8271. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8272. "List of characters that should be escaped in link.
  8273. This is the list that is used for internal purposes.")
  8274. (defconst org-link-escape-chars-browser
  8275. '(?\ )
  8276. "List of escapes for characters that are problematic in links.
  8277. This is the list that is used before handing over to the browser.")
  8278. (defun org-link-escape (text &optional table merge)
  8279. "Return percent escaped representation of TEXT.
  8280. TEXT is a string with the text to escape.
  8281. Optional argument TABLE is a list with characters that should be
  8282. escaped. When nil, `org-link-escape-chars' is used.
  8283. If optional argument MERGE is set, merge TABLE into
  8284. `org-link-escape-chars'."
  8285. (cond
  8286. ((and table merge)
  8287. (mapc (lambda (defchr)
  8288. (unless (member defchr table)
  8289. (setq table (cons defchr table)))) org-link-escape-chars))
  8290. ((null table)
  8291. (setq table org-link-escape-chars)))
  8292. (mapconcat
  8293. (lambda (char)
  8294. (if (or (member char table)
  8295. (and (or (< char 32) (= char 37) (> char 126))
  8296. org-url-hexify-p))
  8297. (mapconcat (lambda (sequence-element)
  8298. (format "%%%.2X" sequence-element))
  8299. (or (encode-coding-char char 'utf-8)
  8300. (error "Unable to percent escape character: %s"
  8301. (char-to-string char))) "")
  8302. (char-to-string char))) text ""))
  8303. (defun org-link-unescape (str)
  8304. "Unhex hexified Unicode strings as returned from the JavaScript function
  8305. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8306. (unless (and (null str) (string= "" str))
  8307. (let ((pos 0) (case-fold-search t) unhexed)
  8308. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8309. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8310. (setq str (replace-match unhexed t t str))
  8311. (setq pos (+ pos (length unhexed))))))
  8312. str)
  8313. (defun org-link-unescape-compound (hex)
  8314. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8315. Note: this function also decodes single byte encodings like
  8316. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8317. (save-match-data
  8318. (let* ((bytes (cdr (split-string hex "%")))
  8319. (ret "")
  8320. (eat 0)
  8321. (sum 0))
  8322. (while bytes
  8323. (let* ((val (string-to-number (pop bytes) 16))
  8324. (shift-xor
  8325. (if (= 0 eat)
  8326. (cond
  8327. ((>= val 252) (cons 6 252))
  8328. ((>= val 248) (cons 5 248))
  8329. ((>= val 240) (cons 4 240))
  8330. ((>= val 224) (cons 3 224))
  8331. ((>= val 192) (cons 2 192))
  8332. (t (cons 0 0)))
  8333. (cons 6 128))))
  8334. (if (>= val 192) (setq eat (car shift-xor)))
  8335. (setq val (logxor val (cdr shift-xor)))
  8336. (setq sum (+ (lsh sum (car shift-xor)) val))
  8337. (if (> eat 0) (setq eat (- eat 1)))
  8338. (cond
  8339. ((= 0 eat) ;multi byte
  8340. (setq ret (concat ret (org-char-to-string sum)))
  8341. (setq sum 0))
  8342. ((not bytes) ; single byte(s)
  8343. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8344. )) ;; end (while bytes
  8345. ret )))
  8346. (defun org-link-unescape-single-byte-sequence (hex)
  8347. "Unhexify hex-encoded single byte character sequences."
  8348. (mapconcat (lambda (byte)
  8349. (char-to-string (string-to-number byte 16)))
  8350. (cdr (split-string hex "%")) ""))
  8351. (defun org-xor (a b)
  8352. "Exclusive or."
  8353. (if a (not b) b))
  8354. (defun org-fixup-message-id-for-http (s)
  8355. "Replace special characters in a message id, so it can be used in an http query."
  8356. (when (string-match "%" s)
  8357. (setq s (mapconcat (lambda (c)
  8358. (if (eq c ?%)
  8359. "%25"
  8360. (char-to-string c)))
  8361. s "")))
  8362. (while (string-match "<" s)
  8363. (setq s (replace-match "%3C" t t s)))
  8364. (while (string-match ">" s)
  8365. (setq s (replace-match "%3E" t t s)))
  8366. (while (string-match "@" s)
  8367. (setq s (replace-match "%40" t t s)))
  8368. s)
  8369. (defun org-link-prettify (link)
  8370. "Return a human-readable representation of LINK.
  8371. The car of LINK must be a raw link the cdr of LINK must be either
  8372. a link description or nil."
  8373. (let ((desc (or (cadr link) "<no description>")))
  8374. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8375. "<" (car link) ">")))
  8376. ;;;###autoload
  8377. (defun org-insert-link-global ()
  8378. "Insert a link like Org-mode does.
  8379. This command can be called in any mode to insert a link in Org-mode syntax."
  8380. (interactive)
  8381. (org-load-modules-maybe)
  8382. (org-run-like-in-org-mode 'org-insert-link))
  8383. (defun org-insert-all-links (&optional keep)
  8384. "Insert all links in `org-stored-links'."
  8385. (interactive "P")
  8386. (let ((links (copy-sequence org-stored-links)) l)
  8387. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8388. (insert "- ")
  8389. (org-insert-link nil (car l) (cadr l))
  8390. (insert "\n"))))
  8391. (defun org-link-fontify-links-to-this-file ()
  8392. "Fontify links to the current file in `org-stored-links'."
  8393. (let ((f (buffer-file-name)) a b)
  8394. (setq a (mapcar (lambda(l)
  8395. (let ((ll (car l)))
  8396. (when (and (string-match "^file:\\(.+\\)::" ll)
  8397. (equal f (expand-file-name (match-string 1 ll))))
  8398. ll)))
  8399. org-stored-links))
  8400. (when (featurep 'org-id)
  8401. (setq b (mapcar (lambda(l)
  8402. (let ((ll (car l)))
  8403. (when (and (string-match "^id:\\(.+\\)$" ll)
  8404. (equal f (expand-file-name
  8405. (or (org-id-find-id-file
  8406. (match-string 1 ll)) ""))))
  8407. ll)))
  8408. org-stored-links)))
  8409. (mapcar (lambda(l)
  8410. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8411. (delq nil (append a b)))))
  8412. (defvar org-link-links-in-this-file nil)
  8413. (defun org-insert-link (&optional complete-file link-location default-description)
  8414. "Insert a link. At the prompt, enter the link.
  8415. Completion can be used to insert any of the link protocol prefixes like
  8416. http or ftp in use.
  8417. The history can be used to select a link previously stored with
  8418. `org-store-link'. When the empty string is entered (i.e. if you just
  8419. press RET at the prompt), the link defaults to the most recently
  8420. stored link. As SPC triggers completion in the minibuffer, you need to
  8421. use M-SPC or C-q SPC to force the insertion of a space character.
  8422. You will also be prompted for a description, and if one is given, it will
  8423. be displayed in the buffer instead of the link.
  8424. If there is already a link at point, this command will allow you to edit link
  8425. and description parts.
  8426. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8427. be selected using completion. The path to the file will be relative to the
  8428. current directory if the file is in the current directory or a subdirectory.
  8429. Otherwise, the link will be the absolute path as completed in the minibuffer
  8430. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8431. option `org-link-file-path-type'.
  8432. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8433. the current directory or below.
  8434. With three \\[universal-argument] prefixes, negate the meaning of
  8435. `org-keep-stored-link-after-insertion'.
  8436. If `org-make-link-description-function' is non-nil, this function will be
  8437. called with the link target, and the result will be the default
  8438. link description.
  8439. If the LINK-LOCATION parameter is non-nil, this value will be
  8440. used as the link location instead of reading one interactively.
  8441. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8442. be used as the default description."
  8443. (interactive "P")
  8444. (let* ((wcf (current-window-configuration))
  8445. (origbuf (current-buffer))
  8446. (region (if (org-region-active-p)
  8447. (buffer-substring (region-beginning) (region-end))))
  8448. (remove (and region (list (region-beginning) (region-end))))
  8449. (desc region)
  8450. tmphist ; byte-compile incorrectly complains about this
  8451. (link link-location)
  8452. (abbrevs org-link-abbrev-alist-local)
  8453. entry file all-prefixes auto-desc)
  8454. (cond
  8455. (link-location) ; specified by arg, just use it.
  8456. ((org-in-regexp org-bracket-link-regexp 1)
  8457. ;; We do have a link at point, and we are going to edit it.
  8458. (setq remove (list (match-beginning 0) (match-end 0)))
  8459. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8460. (setq link (read-string "Link: "
  8461. (org-link-unescape
  8462. (org-match-string-no-properties 1)))))
  8463. ((or (org-in-regexp org-angle-link-re)
  8464. (org-in-regexp org-plain-link-re))
  8465. ;; Convert to bracket link
  8466. (setq remove (list (match-beginning 0) (match-end 0))
  8467. link (read-string "Link: "
  8468. (org-remove-angle-brackets (match-string 0)))))
  8469. ((member complete-file '((4) (16)))
  8470. ;; Completing read for file names.
  8471. (setq link (org-file-complete-link complete-file)))
  8472. (t
  8473. ;; Read link, with completion for stored links.
  8474. (org-link-fontify-links-to-this-file)
  8475. (org-switch-to-buffer-other-window "*Org Links*")
  8476. (with-current-buffer "*Org Links*"
  8477. (erase-buffer)
  8478. (insert "Insert a link.
  8479. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8480. (when org-stored-links
  8481. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8482. (insert (mapconcat 'org-link-prettify
  8483. (reverse org-stored-links) "\n")))
  8484. (goto-char (point-min)))
  8485. (let ((cw (selected-window)))
  8486. (select-window (get-buffer-window "*Org Links*" 'visible))
  8487. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8488. (unless (pos-visible-in-window-p (point-max))
  8489. (org-fit-window-to-buffer))
  8490. (and (window-live-p cw) (select-window cw)))
  8491. ;; Fake a link history, containing the stored links.
  8492. (setq tmphist (append (mapcar 'car org-stored-links)
  8493. org-insert-link-history))
  8494. (setq all-prefixes (append (mapcar 'car abbrevs)
  8495. (mapcar 'car org-link-abbrev-alist)
  8496. org-link-types))
  8497. (unwind-protect
  8498. (progn
  8499. (setq link
  8500. (org-completing-read
  8501. "Link: "
  8502. (append
  8503. (mapcar (lambda (x) (concat x ":"))
  8504. all-prefixes)
  8505. (mapcar 'car org-stored-links))
  8506. nil nil nil
  8507. 'tmphist
  8508. (caar org-stored-links)))
  8509. (if (not (string-match "\\S-" link))
  8510. (error "No link selected"))
  8511. (mapc (lambda(l)
  8512. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8513. org-stored-links)
  8514. (if (or (member link all-prefixes)
  8515. (and (equal ":" (substring link -1))
  8516. (member (substring link 0 -1) all-prefixes)
  8517. (setq link (substring link 0 -1))))
  8518. (setq link (with-current-buffer origbuf
  8519. (org-link-try-special-completion link)))))
  8520. (set-window-configuration wcf)
  8521. (kill-buffer "*Org Links*"))
  8522. (setq entry (assoc link org-stored-links))
  8523. (or entry (push link org-insert-link-history))
  8524. (setq desc (or desc (nth 1 entry)))))
  8525. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8526. (not org-keep-stored-link-after-insertion))
  8527. (setq org-stored-links (delq (assoc link org-stored-links)
  8528. org-stored-links)))
  8529. (if (string-match org-plain-link-re link)
  8530. ;; URL-like link, normalize the use of angular brackets.
  8531. (setq link (org-remove-angle-brackets link)))
  8532. ;; Check if we are linking to the current file with a search option
  8533. ;; If yes, simplify the link by using only the search option.
  8534. (when (and buffer-file-name
  8535. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8536. (let* ((path (match-string 1 link))
  8537. (case-fold-search nil)
  8538. (search (match-string 2 link)))
  8539. (save-match-data
  8540. (if (equal (file-truename buffer-file-name) (file-truename path))
  8541. ;; We are linking to this same file, with a search option
  8542. (setq link search)))))
  8543. ;; Check if we can/should use a relative path. If yes, simplify the link
  8544. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8545. (let* ((type (match-string 1 link))
  8546. (path (match-string 2 link))
  8547. (origpath path)
  8548. (case-fold-search nil))
  8549. (cond
  8550. ((or (eq org-link-file-path-type 'absolute)
  8551. (equal complete-file '(16)))
  8552. (setq path (abbreviate-file-name (expand-file-name path))))
  8553. ((eq org-link-file-path-type 'noabbrev)
  8554. (setq path (expand-file-name path)))
  8555. ((eq org-link-file-path-type 'relative)
  8556. (setq path (file-relative-name path)))
  8557. (t
  8558. (save-match-data
  8559. (if (string-match (concat "^" (regexp-quote
  8560. (expand-file-name
  8561. (file-name-as-directory
  8562. default-directory))))
  8563. (expand-file-name path))
  8564. ;; We are linking a file with relative path name.
  8565. (setq path (substring (expand-file-name path)
  8566. (match-end 0)))
  8567. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8568. (setq link (concat type path))
  8569. (if (equal desc origpath)
  8570. (setq desc path))))
  8571. (if org-make-link-description-function
  8572. (setq desc
  8573. (or (condition-case nil
  8574. (funcall org-make-link-description-function link desc)
  8575. (error (progn (message "Can't get link description from `%s'"
  8576. (symbol-name org-make-link-description-function))
  8577. (sit-for 2) nil)))
  8578. (read-string "Description: " default-description)))
  8579. (if default-description (setq desc default-description)
  8580. (setq desc (or (and auto-desc desc)
  8581. (read-string "Description: " desc)))))
  8582. (unless (string-match "\\S-" desc) (setq desc nil))
  8583. (if remove (apply 'delete-region remove))
  8584. (insert (org-make-link-string link desc))))
  8585. (defun org-link-try-special-completion (type)
  8586. "If there is completion support for link type TYPE, offer it."
  8587. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8588. (if (functionp fun)
  8589. (funcall fun)
  8590. (read-string "Link (no completion support): " (concat type ":")))))
  8591. (defun org-file-complete-link (&optional arg)
  8592. "Create a file link using completion."
  8593. (let (file link)
  8594. (setq file (org-iread-file-name "File: "))
  8595. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8596. (pwd1 (file-name-as-directory (abbreviate-file-name
  8597. (expand-file-name ".")))))
  8598. (cond
  8599. ((equal arg '(16))
  8600. (setq link (concat
  8601. "file:"
  8602. (abbreviate-file-name (expand-file-name file)))))
  8603. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8604. (setq link (concat "file:" (match-string 1 file))))
  8605. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8606. (expand-file-name file))
  8607. (setq link (concat
  8608. "file:" (match-string 1 (expand-file-name file)))))
  8609. (t (setq link (concat "file:" file)))))
  8610. link))
  8611. (defun org-iread-file-name (&rest args)
  8612. "Read-file-name using `ido-mode' speedup if available.
  8613. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8614. See `read-file-name' for a description of parameters."
  8615. (org-without-partial-completion
  8616. (if (and org-completion-use-ido
  8617. (fboundp 'ido-read-file-name)
  8618. (boundp 'ido-mode) ido-mode
  8619. (listp (second args)))
  8620. (let ((ido-enter-matching-directory nil))
  8621. (apply 'ido-read-file-name args))
  8622. (apply 'read-file-name args))))
  8623. (defun org-completing-read (&rest args)
  8624. "Completing-read with SPACE being a normal character."
  8625. (let ((enable-recursive-minibuffers t)
  8626. (minibuffer-local-completion-map
  8627. (copy-keymap minibuffer-local-completion-map)))
  8628. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8629. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8630. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8631. (apply 'org-icompleting-read args)))
  8632. (defun org-completing-read-no-i (&rest args)
  8633. (let (org-completion-use-ido org-completion-use-iswitchb)
  8634. (apply 'org-completing-read args)))
  8635. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8636. "Use iswitch as a completing-read replacement to choose from choices.
  8637. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8638. from."
  8639. (let* ((iswitchb-use-virtual-buffers nil)
  8640. (iswitchb-make-buflist-hook
  8641. (lambda ()
  8642. (setq iswitchb-temp-buflist choices))))
  8643. (iswitchb-read-buffer prompt)))
  8644. (defun org-icompleting-read (&rest args)
  8645. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8646. (org-without-partial-completion
  8647. (if (and org-completion-use-ido
  8648. (fboundp 'ido-completing-read)
  8649. (boundp 'ido-mode) ido-mode
  8650. (listp (second args)))
  8651. (let ((ido-enter-matching-directory nil))
  8652. (apply 'ido-completing-read (concat (car args))
  8653. (if (consp (car (nth 1 args)))
  8654. (mapcar 'car (nth 1 args))
  8655. (nth 1 args))
  8656. (cddr args)))
  8657. (if (and org-completion-use-iswitchb
  8658. (boundp 'iswitchb-mode) iswitchb-mode
  8659. (listp (second args)))
  8660. (apply 'org-iswitchb-completing-read (concat (car args))
  8661. (if (consp (car (nth 1 args)))
  8662. (mapcar 'car (nth 1 args))
  8663. (nth 1 args))
  8664. (cddr args))
  8665. (apply 'completing-read args)))))
  8666. (defun org-extract-attributes (s)
  8667. "Extract the attributes cookie from a string and set as text property."
  8668. (let (a attr (start 0) key value)
  8669. (save-match-data
  8670. (when (string-match "{{\\([^}]+\\)}}$" s)
  8671. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8672. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8673. (setq key (match-string 1 a) value (match-string 2 a)
  8674. start (match-end 0)
  8675. attr (plist-put attr (intern key) value))))
  8676. (org-add-props s nil 'org-attr attr))
  8677. s))
  8678. (defun org-extract-attributes-from-string (tag)
  8679. (let (key value attr)
  8680. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8681. (setq key (match-string 1 tag) value (match-string 2 tag)
  8682. tag (replace-match "" t t tag)
  8683. attr (plist-put attr (intern key) value)))
  8684. (cons tag attr)))
  8685. (defun org-attributes-to-string (plist)
  8686. "Format a property list into an HTML attribute list."
  8687. (let ((s "") key value)
  8688. (while plist
  8689. (setq key (pop plist) value (pop plist))
  8690. (and value
  8691. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8692. s))
  8693. ;;; Opening/following a link
  8694. (defvar org-link-search-failed nil)
  8695. (defvar org-open-link-functions nil
  8696. "Hook for functions finding a plain text link.
  8697. These functions must take a single argument, the link content.
  8698. They will be called for links that look like [[link text][description]]
  8699. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8700. like a filename (e.g. \"./blue.png\").
  8701. These functions will be called *before* Org attempts to resolve the
  8702. link by doing text searches in the current buffer - so if you want a
  8703. link \"[[target]]\" to still find \"<<target>>\", your function should
  8704. handle this as a special case.
  8705. When the function does handle the link, it must return a non-nil value.
  8706. If it decides that it is not responsible for this link, it must return
  8707. nil to indicate that that Org-mode can continue with other options
  8708. like exact and fuzzy text search.")
  8709. (defun org-next-link ()
  8710. "Move forward to the next link.
  8711. If the link is in hidden text, expose it."
  8712. (interactive)
  8713. (when (and org-link-search-failed (eq this-command last-command))
  8714. (goto-char (point-min))
  8715. (message "Link search wrapped back to beginning of buffer"))
  8716. (setq org-link-search-failed nil)
  8717. (let* ((pos (point))
  8718. (ct (org-context))
  8719. (a (assoc :link ct)))
  8720. (if a (goto-char (nth 2 a)))
  8721. (if (re-search-forward org-any-link-re nil t)
  8722. (progn
  8723. (goto-char (match-beginning 0))
  8724. (if (outline-invisible-p) (org-show-context)))
  8725. (goto-char pos)
  8726. (setq org-link-search-failed t)
  8727. (error "No further link found"))))
  8728. (defun org-previous-link ()
  8729. "Move backward to the previous link.
  8730. If the link is in hidden text, expose it."
  8731. (interactive)
  8732. (when (and org-link-search-failed (eq this-command last-command))
  8733. (goto-char (point-max))
  8734. (message "Link search wrapped back to end of buffer"))
  8735. (setq org-link-search-failed nil)
  8736. (let* ((pos (point))
  8737. (ct (org-context))
  8738. (a (assoc :link ct)))
  8739. (if a (goto-char (nth 1 a)))
  8740. (if (re-search-backward org-any-link-re nil t)
  8741. (progn
  8742. (goto-char (match-beginning 0))
  8743. (if (outline-invisible-p) (org-show-context)))
  8744. (goto-char pos)
  8745. (setq org-link-search-failed t)
  8746. (error "No further link found"))))
  8747. (defun org-translate-link (s)
  8748. "Translate a link string if a translation function has been defined."
  8749. (if (and org-link-translation-function
  8750. (fboundp org-link-translation-function)
  8751. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8752. (progn
  8753. (setq s (funcall org-link-translation-function
  8754. (match-string 1 s) (match-string 2 s)))
  8755. (concat (car s) ":" (cdr s)))
  8756. s))
  8757. (defun org-translate-link-from-planner (type path)
  8758. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8759. This is still an experimental function, your mileage may vary."
  8760. (cond
  8761. ((member type '("http" "https" "news" "ftp"))
  8762. ;; standard Internet links are the same.
  8763. nil)
  8764. ((and (equal type "irc") (string-match "^//" path))
  8765. ;; Planner has two / at the beginning of an irc link, we have 1.
  8766. ;; We should have zero, actually....
  8767. (setq path (substring path 1)))
  8768. ((and (equal type "lisp") (string-match "^/" path))
  8769. ;; Planner has a slash, we do not.
  8770. (setq type "elisp" path (substring path 1)))
  8771. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8772. ;; A typical message link. Planner has the id after the final slash,
  8773. ;; we separate it with a hash mark
  8774. (setq path (concat (match-string 1 path) "#"
  8775. (org-remove-angle-brackets (match-string 2 path)))))
  8776. )
  8777. (cons type path))
  8778. (defun org-find-file-at-mouse (ev)
  8779. "Open file link or URL at mouse."
  8780. (interactive "e")
  8781. (mouse-set-point ev)
  8782. (org-open-at-point 'in-emacs))
  8783. (defun org-open-at-mouse (ev)
  8784. "Open file link or URL at mouse.
  8785. See the docstring of `org-open-file' for details."
  8786. (interactive "e")
  8787. (mouse-set-point ev)
  8788. (if (eq major-mode 'org-agenda-mode)
  8789. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8790. (org-open-at-point))
  8791. (defvar org-window-config-before-follow-link nil
  8792. "The window configuration before following a link.
  8793. This is saved in case the need arises to restore it.")
  8794. (defvar org-open-link-marker (make-marker)
  8795. "Marker pointing to the location where `org-open-at-point; was called.")
  8796. ;;;###autoload
  8797. (defun org-open-at-point-global ()
  8798. "Follow a link like Org-mode does.
  8799. This command can be called in any mode to follow a link that has
  8800. Org-mode syntax."
  8801. (interactive)
  8802. (org-run-like-in-org-mode 'org-open-at-point))
  8803. ;;;###autoload
  8804. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8805. "Open a link in the string S, as if it was in Org-mode."
  8806. (interactive "sLink: \nP")
  8807. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8808. (with-temp-buffer
  8809. (let ((org-inhibit-startup (not reference-buffer)))
  8810. (org-mode)
  8811. (insert s)
  8812. (goto-char (point-min))
  8813. (when reference-buffer
  8814. (setq org-link-abbrev-alist-local
  8815. (with-current-buffer reference-buffer
  8816. org-link-abbrev-alist-local)))
  8817. (org-open-at-point arg reference-buffer)))))
  8818. (defvar org-open-at-point-functions nil
  8819. "Hook that is run when following a link at point.
  8820. Functions in this hook must return t if they identify and follow
  8821. a link at point. If they don't find anything interesting at point,
  8822. they must return nil.")
  8823. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8824. (defun org-open-at-point (&optional arg reference-buffer)
  8825. "Open link at or after point.
  8826. If there is no link at point, this function will search forward up to
  8827. the end of the current line.
  8828. Normally, files will be opened by an appropriate application. If the
  8829. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8830. With a double prefix argument, try to open outside of Emacs, in the
  8831. application the system uses for this file type."
  8832. (interactive "P")
  8833. ;; if in a code block, then open the block's results
  8834. (unless (call-interactively #'org-babel-open-src-block-result)
  8835. (org-load-modules-maybe)
  8836. (move-marker org-open-link-marker (point))
  8837. (setq org-window-config-before-follow-link (current-window-configuration))
  8838. (org-remove-occur-highlights nil nil t)
  8839. (cond
  8840. ((and (org-at-heading-p)
  8841. (not (org-at-timestamp-p t))
  8842. (not (org-in-regexp
  8843. (concat org-plain-link-re "\\|"
  8844. org-bracket-link-regexp "\\|"
  8845. org-angle-link-re "\\|"
  8846. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8847. (not (get-text-property (point) 'org-linked-text)))
  8848. (or (org-offer-links-in-entry arg)
  8849. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8850. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8851. ((and (org-at-timestamp-p t)
  8852. (not (org-in-regexp org-bracket-link-regexp)))
  8853. (org-follow-timestamp-link))
  8854. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8855. (not (org-in-regexp org-any-link-re)))
  8856. (org-footnote-action))
  8857. (t
  8858. (let (type path link line search (pos (point)))
  8859. (catch 'match
  8860. (save-excursion
  8861. (skip-chars-forward "^]\n\r")
  8862. (when (org-in-regexp org-bracket-link-regexp 1)
  8863. (setq link (org-extract-attributes
  8864. (org-link-unescape (org-match-string-no-properties 1))))
  8865. (while (string-match " *\n *" link)
  8866. (setq link (replace-match " " t t link)))
  8867. (setq link (org-link-expand-abbrev link))
  8868. (cond
  8869. ((or (file-name-absolute-p link)
  8870. (string-match "^\\.\\.?/" link))
  8871. (setq type "file" path link))
  8872. ((string-match org-link-re-with-space3 link)
  8873. (setq type (match-string 1 link) path (match-string 2 link)))
  8874. ((string-match "^help:+\\(.+\\)" link)
  8875. (setq type "help" path (match-string 1 link)))
  8876. (t (setq type "thisfile" path link)))
  8877. (throw 'match t)))
  8878. (when (get-text-property (point) 'org-linked-text)
  8879. (setq type "thisfile"
  8880. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8881. (1+ (point)) (point))
  8882. path (buffer-substring
  8883. (or (previous-single-property-change pos 'org-linked-text)
  8884. (point-min))
  8885. (or (next-single-property-change pos 'org-linked-text)
  8886. (point-max))))
  8887. (throw 'match t))
  8888. (save-excursion
  8889. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8890. (when (or (org-in-regexp org-angle-link-re)
  8891. (and plinkpos (goto-char (car plinkpos))
  8892. (save-match-data (not (looking-back "\\[\\[")))))
  8893. (setq type (match-string 1)
  8894. path (org-link-unescape (match-string 2)))
  8895. (throw 'match t))))
  8896. (save-excursion
  8897. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8898. (setq type "tags"
  8899. path (match-string 1))
  8900. (while (string-match ":" path)
  8901. (setq path (replace-match "+" t t path)))
  8902. (throw 'match t)))
  8903. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8904. (setq type "tree-match"
  8905. path (match-string 1))
  8906. (throw 'match t)))
  8907. (unless path
  8908. (user-error "No link found"))
  8909. ;; switch back to reference buffer
  8910. ;; needed when if called in a temporary buffer through
  8911. ;; org-open-link-from-string
  8912. (with-current-buffer (or reference-buffer (current-buffer))
  8913. ;; Remove any trailing spaces in path
  8914. (if (string-match " +\\'" path)
  8915. (setq path (replace-match "" t t path)))
  8916. (if (and org-link-translation-function
  8917. (fboundp org-link-translation-function))
  8918. ;; Check if we need to translate the link
  8919. (let ((tmp (funcall org-link-translation-function type path)))
  8920. (setq type (car tmp) path (cdr tmp))))
  8921. (cond
  8922. ((assoc type org-link-protocols)
  8923. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8924. ((equal type "help")
  8925. (let ((f-or-v (intern path)))
  8926. (cond ((fboundp f-or-v)
  8927. (describe-function f-or-v))
  8928. ((boundp f-or-v)
  8929. (describe-variable f-or-v))
  8930. (t (error "Not a known function or variable")))))
  8931. ((equal type "mailto")
  8932. (let ((cmd (car org-link-mailto-program))
  8933. (args (cdr org-link-mailto-program)) args1
  8934. (address path) (subject "") a)
  8935. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8936. (setq address (match-string 1 path)
  8937. subject (org-link-escape (match-string 2 path))))
  8938. (while args
  8939. (cond
  8940. ((not (stringp (car args))) (push (pop args) args1))
  8941. (t (setq a (pop args))
  8942. (if (string-match "%a" a)
  8943. (setq a (replace-match address t t a)))
  8944. (if (string-match "%s" a)
  8945. (setq a (replace-match subject t t a)))
  8946. (push a args1))))
  8947. (apply cmd (nreverse args1))))
  8948. ((member type '("http" "https" "ftp" "news"))
  8949. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8950. (org-link-escape
  8951. path org-link-escape-chars-browser)
  8952. path))))
  8953. ((string= type "doi")
  8954. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8955. (org-link-escape
  8956. path org-link-escape-chars-browser)
  8957. path))))
  8958. ((member type '("message"))
  8959. (browse-url (concat type ":" path)))
  8960. ((string= type "tags")
  8961. (org-tags-view arg path))
  8962. ((string= type "tree-match")
  8963. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8964. ((string= type "file")
  8965. (if (string-match "::\\([0-9]+\\)\\'" path)
  8966. (setq line (string-to-number (match-string 1 path))
  8967. path (substring path 0 (match-beginning 0)))
  8968. (if (string-match "::\\(.+\\)\\'" path)
  8969. (setq search (match-string 1 path)
  8970. path (substring path 0 (match-beginning 0)))))
  8971. (if (string-match "[*?{]" (file-name-nondirectory path))
  8972. (dired path)
  8973. (org-open-file path arg line search)))
  8974. ((string= type "shell")
  8975. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8976. (cmd path))
  8977. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8978. (string-match org-confirm-shell-link-not-regexp cmd))
  8979. (not org-confirm-shell-link-function)
  8980. (funcall org-confirm-shell-link-function
  8981. (format "Execute \"%s\" in shell? "
  8982. (org-add-props cmd nil
  8983. 'face 'org-warning))))
  8984. (progn
  8985. (message "Executing %s" cmd)
  8986. (shell-command cmd buf)
  8987. (if (featurep 'midnight)
  8988. (setq clean-buffer-list-kill-buffer-names
  8989. (cons buf clean-buffer-list-kill-buffer-names))))
  8990. (error "Abort"))))
  8991. ((string= type "elisp")
  8992. (let ((cmd path))
  8993. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8994. (string-match org-confirm-elisp-link-not-regexp cmd))
  8995. (not org-confirm-elisp-link-function)
  8996. (funcall org-confirm-elisp-link-function
  8997. (format "Execute \"%s\" as elisp? "
  8998. (org-add-props cmd nil
  8999. 'face 'org-warning))))
  9000. (message "%s => %s" cmd
  9001. (if (equal (string-to-char cmd) ?\()
  9002. (eval (read cmd))
  9003. (call-interactively (read cmd))))
  9004. (error "Abort"))))
  9005. ((and (string= type "thisfile")
  9006. (run-hook-with-args-until-success
  9007. 'org-open-link-functions path)))
  9008. ((string= type "thisfile")
  9009. (if arg
  9010. (switch-to-buffer-other-window
  9011. (org-get-buffer-for-internal-link (current-buffer)))
  9012. (org-mark-ring-push))
  9013. (let ((cmd `(org-link-search
  9014. ,path
  9015. ,(cond ((equal arg '(4)) ''occur)
  9016. ((equal arg '(16)) ''org-occur))
  9017. ,pos)))
  9018. (condition-case nil (let ((org-link-search-inhibit-query t))
  9019. (eval cmd))
  9020. (error (progn (widen) (eval cmd))))))
  9021. (t (browse-url-at-point)))))))
  9022. (move-marker org-open-link-marker nil)
  9023. (run-hook-with-args 'org-follow-link-hook)))
  9024. (defun org-offer-links-in-entry (&optional nth zero)
  9025. "Offer links in the current entry and follow the selected link.
  9026. If there is only one link, follow it immediately as well.
  9027. If NTH is an integer, immediately pick the NTH link found.
  9028. If ZERO is a string, check also this string for a link, and if
  9029. there is one, offer it as link number zero."
  9030. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9031. "\\(" org-angle-link-re "\\)\\|"
  9032. "\\(" org-plain-link-re "\\)"))
  9033. (cnt ?0)
  9034. (in-emacs (if (integerp nth) nil nth))
  9035. have-zero end links link c)
  9036. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9037. (push (match-string 0 zero) links)
  9038. (setq cnt (1- cnt) have-zero t))
  9039. (save-excursion
  9040. (org-back-to-heading t)
  9041. (setq end (save-excursion (outline-next-heading) (point)))
  9042. (while (re-search-forward re end t)
  9043. (push (match-string 0) links))
  9044. (setq links (org-uniquify (reverse links))))
  9045. (cond
  9046. ((null links)
  9047. (message "No links"))
  9048. ((equal (length links) 1)
  9049. (setq link (list (car links))))
  9050. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9051. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  9052. (t ; we have to select a link
  9053. (save-excursion
  9054. (save-window-excursion
  9055. (delete-other-windows)
  9056. (with-output-to-temp-buffer "*Select Link*"
  9057. (mapc (lambda (l)
  9058. (if (not (string-match org-bracket-link-regexp l))
  9059. (princ (format "[%c] %s\n" (incf cnt)
  9060. (org-remove-angle-brackets l)))
  9061. (if (match-end 3)
  9062. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9063. (match-string 3 l) (match-string 1 l)))
  9064. (princ (format "[%c] %s\n" (incf cnt)
  9065. (match-string 1 l))))))
  9066. links))
  9067. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9068. (message "Select link to open, RET to open all:")
  9069. (setq c (read-char-exclusive))
  9070. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9071. (when (equal c ?q) (error "Abort"))
  9072. (if (equal c ?\C-m)
  9073. (setq link links)
  9074. (setq nth (- c ?0))
  9075. (if have-zero (setq nth (1+ nth)))
  9076. (unless (and (integerp nth) (>= (length links) nth))
  9077. (error "Invalid link selection"))
  9078. (setq link (list (nth (1- nth) links))))))
  9079. (if link
  9080. (let ((buf (current-buffer)))
  9081. (dolist (l link)
  9082. (org-open-link-from-string l in-emacs buf))
  9083. t)
  9084. nil)))
  9085. ;; Add special file links that specify the way of opening
  9086. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9087. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9088. (defun org-open-file-with-system (path)
  9089. "Open file at PATH using the system way of opening it."
  9090. (org-open-file path 'system))
  9091. (defun org-open-file-with-emacs (path)
  9092. "Open file at PATH in Emacs."
  9093. (org-open-file path 'emacs))
  9094. (defun org-remove-file-link-modifiers ()
  9095. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  9096. (goto-char (point-min))
  9097. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  9098. (org-if-unprotected
  9099. (replace-match "file:" t t))))
  9100. (eval-after-load "org-exp"
  9101. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  9102. 'org-remove-file-link-modifiers))
  9103. ;;;; Time estimates
  9104. (defun org-get-effort (&optional pom)
  9105. "Get the effort estimate for the current entry."
  9106. (org-entry-get pom org-effort-property))
  9107. ;;; File search
  9108. (defvar org-create-file-search-functions nil
  9109. "List of functions to construct the right search string for a file link.
  9110. These functions are called in turn with point at the location to
  9111. which the link should point.
  9112. A function in the hook should first test if it would like to
  9113. handle this file type, for example by checking the `major-mode'
  9114. or the file extension. If it decides not to handle this file, it
  9115. should just return nil to give other functions a chance. If it
  9116. does handle the file, it must return the search string to be used
  9117. when following the link. The search string will be part of the
  9118. file link, given after a double colon, and `org-open-at-point'
  9119. will automatically search for it. If special measures must be
  9120. taken to make the search successful, another function should be
  9121. added to the companion hook `org-execute-file-search-functions',
  9122. which see.
  9123. A function in this hook may also use `setq' to set the variable
  9124. `description' to provide a suggestion for the descriptive text to
  9125. be used for this link when it gets inserted into an Org-mode
  9126. buffer with \\[org-insert-link].")
  9127. (defvar org-execute-file-search-functions nil
  9128. "List of functions to execute a file search triggered by a link.
  9129. Functions added to this hook must accept a single argument, the
  9130. search string that was part of the file link, the part after the
  9131. double colon. The function must first check if it would like to
  9132. handle this search, for example by checking the `major-mode' or
  9133. the file extension. If it decides not to handle this search, it
  9134. should just return nil to give other functions a chance. If it
  9135. does handle the search, it must return a non-nil value to keep
  9136. other functions from trying.
  9137. Each function can access the current prefix argument through the
  9138. variable `current-prefix-argument'. Note that a single prefix is
  9139. used to force opening a link in Emacs, so it may be good to only
  9140. use a numeric or double prefix to guide the search function.
  9141. In case this is needed, a function in this hook can also restore
  9142. the window configuration before `org-open-at-point' was called using:
  9143. (set-window-configuration org-window-config-before-follow-link)")
  9144. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9145. (defun org-link-search (s &optional type avoid-pos stealth)
  9146. "Search for a link search option.
  9147. If S is surrounded by forward slashes, it is interpreted as a
  9148. regular expression. In org-mode files, this will create an `org-occur'
  9149. sparse tree. In ordinary files, `occur' will be used to list matches.
  9150. If the current buffer is in `dired-mode', grep will be used to search
  9151. in all files. If AVOID-POS is given, ignore matches near that position.
  9152. When optional argument STEALTH is non-nil, do not modify
  9153. visibility around point, thus ignoring
  9154. `org-show-hierarchy-above', `org-show-following-heading' and
  9155. `org-show-siblings' variables."
  9156. (let ((case-fold-search t)
  9157. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9158. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9159. (append '(("") (" ") ("\t") ("\n"))
  9160. org-emphasis-alist)
  9161. "\\|") "\\)"))
  9162. (pos (point))
  9163. (pre nil) (post nil)
  9164. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9165. (cond
  9166. ;; First check if there are any special search functions
  9167. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9168. ;; Now try the builtin stuff
  9169. ((and (equal (string-to-char s0) ?#)
  9170. (> (length s0) 1)
  9171. (save-excursion
  9172. (goto-char (point-min))
  9173. (and
  9174. (re-search-forward
  9175. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9176. (setq type 'dedicated
  9177. pos (match-beginning 0))))
  9178. ;; There is an exact target for this
  9179. (goto-char pos)
  9180. (org-back-to-heading t)))
  9181. ((save-excursion
  9182. (goto-char (point-min))
  9183. (and
  9184. (re-search-forward
  9185. (concat "<<" (regexp-quote s0) ">>") nil t)
  9186. (setq type 'dedicated
  9187. pos (match-beginning 0))))
  9188. ;; There is an exact target for this
  9189. (goto-char pos))
  9190. ((save-excursion
  9191. (goto-char (point-min))
  9192. (and
  9193. (re-search-forward
  9194. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9195. (setq type 'dedicated pos (match-beginning 0))))
  9196. ;; Found an invisible target.
  9197. (goto-char pos))
  9198. ((save-excursion
  9199. (goto-char (point-min))
  9200. (and
  9201. (re-search-forward
  9202. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9203. (setq type 'dedicated pos (match-beginning 0))))
  9204. ;; Found an element with a matching #+name affiliated keyword.
  9205. (goto-char pos))
  9206. ((and (string-match "^(\\(.*\\))$" s0)
  9207. (save-excursion
  9208. (goto-char (point-min))
  9209. (and
  9210. (re-search-forward
  9211. (concat "[^[]" (regexp-quote
  9212. (format org-coderef-label-format
  9213. (match-string 1 s0))))
  9214. nil t)
  9215. (setq type 'dedicated
  9216. pos (1+ (match-beginning 0))))))
  9217. ;; There is a coderef target for this
  9218. (goto-char pos))
  9219. ((string-match "^/\\(.*\\)/$" s)
  9220. ;; A regular expression
  9221. (cond
  9222. ((derived-mode-p 'org-mode)
  9223. (org-occur (match-string 1 s)))
  9224. ;;((eq major-mode 'dired-mode)
  9225. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9226. (t (org-do-occur (match-string 1 s)))))
  9227. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9228. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9229. (goto-char (point-min))
  9230. (cond
  9231. ((let (case-fold-search)
  9232. (re-search-forward (format org-complex-heading-regexp-format
  9233. (regexp-quote s))
  9234. nil t))
  9235. ;; OK, found a match
  9236. (setq type 'dedicated)
  9237. (goto-char (match-beginning 0)))
  9238. ((and (not org-link-search-inhibit-query)
  9239. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9240. (y-or-n-p "No match - create this as a new heading? "))
  9241. (goto-char (point-max))
  9242. (or (bolp) (newline))
  9243. (insert "* " s "\n")
  9244. (beginning-of-line 0))
  9245. (t
  9246. (goto-char pos)
  9247. (error "No match"))))
  9248. (t
  9249. ;; A normal search string
  9250. (when (equal (string-to-char s) ?*)
  9251. ;; Anchor on headlines, post may include tags.
  9252. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9253. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9254. s (substring s 1)))
  9255. (remove-text-properties
  9256. 0 (length s)
  9257. '(face nil mouse-face nil keymap nil fontified nil) s)
  9258. ;; Make a series of regular expressions to find a match
  9259. (setq words (org-split-string s "[ \n\r\t]+")
  9260. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9261. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9262. "\\)" markers)
  9263. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9264. re2a (concat "[ \t\r\n]" re2a_)
  9265. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9266. re4 (concat "[^a-zA-Z_]" re4_)
  9267. re1 (concat pre re2 post)
  9268. re3 (concat pre (if pre re4_ re4) post)
  9269. re5 (concat pre ".*" re4)
  9270. re2 (concat pre re2)
  9271. re2a (concat pre (if pre re2a_ re2a))
  9272. re4 (concat pre (if pre re4_ re4))
  9273. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9274. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9275. re5 "\\)"
  9276. ))
  9277. (cond
  9278. ((eq type 'org-occur) (org-occur reall))
  9279. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9280. (t (goto-char (point-min))
  9281. (setq type 'fuzzy)
  9282. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9283. (org-search-not-self 1 re1 nil t)
  9284. (org-search-not-self 1 re2 nil t)
  9285. (org-search-not-self 1 re2a nil t)
  9286. (org-search-not-self 1 re3 nil t)
  9287. (org-search-not-self 1 re4 nil t)
  9288. (org-search-not-self 1 re5 nil t)
  9289. )
  9290. (goto-char (match-beginning 1))
  9291. (goto-char pos)
  9292. (error "No match"))))))
  9293. (and (derived-mode-p 'org-mode)
  9294. (not stealth)
  9295. (org-show-context 'link-search))
  9296. type))
  9297. (defun org-search-not-self (group &rest args)
  9298. "Execute `re-search-forward', but only accept matches that do not
  9299. enclose the position of `org-open-link-marker'."
  9300. (let ((m org-open-link-marker))
  9301. (catch 'exit
  9302. (while (apply 're-search-forward args)
  9303. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9304. (goto-char (match-end group))
  9305. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9306. (> (match-beginning 0) (marker-position m))
  9307. (< (match-end 0) (marker-position m)))
  9308. (save-match-data
  9309. (or (not (org-in-regexp
  9310. org-bracket-link-analytic-regexp 1))
  9311. (not (match-end 4)) ; no description
  9312. (and (<= (match-beginning 4) (point))
  9313. (>= (match-end 4) (point))))))
  9314. (throw 'exit (point))))))))
  9315. (defun org-get-buffer-for-internal-link (buffer)
  9316. "Return a buffer to be used for displaying the link target of internal links."
  9317. (cond
  9318. ((not org-display-internal-link-with-indirect-buffer)
  9319. buffer)
  9320. ((string-match "(Clone)$" (buffer-name buffer))
  9321. (message "Buffer is already a clone, not making another one")
  9322. ;; we also do not modify visibility in this case
  9323. buffer)
  9324. (t ; make a new indirect buffer for displaying the link
  9325. (let* ((bn (buffer-name buffer))
  9326. (ibn (concat bn "(Clone)"))
  9327. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9328. (with-current-buffer ib (org-overview))
  9329. ib))))
  9330. (defun org-do-occur (regexp &optional cleanup)
  9331. "Call the Emacs command `occur'.
  9332. If CLEANUP is non-nil, remove the printout of the regular expression
  9333. in the *Occur* buffer. This is useful if the regex is long and not useful
  9334. to read."
  9335. (occur regexp)
  9336. (when cleanup
  9337. (let ((cwin (selected-window)) win beg end)
  9338. (when (setq win (get-buffer-window "*Occur*"))
  9339. (select-window win))
  9340. (goto-char (point-min))
  9341. (when (re-search-forward "match[a-z]+" nil t)
  9342. (setq beg (match-end 0))
  9343. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9344. (setq end (1- (match-beginning 0)))))
  9345. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9346. (goto-char (point-min))
  9347. (select-window cwin))))
  9348. ;;; The mark ring for links jumps
  9349. (defvar org-mark-ring nil
  9350. "Mark ring for positions before jumps in Org-mode.")
  9351. (defvar org-mark-ring-last-goto nil
  9352. "Last position in the mark ring used to go back.")
  9353. ;; Fill and close the ring
  9354. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9355. (loop for i from 1 to org-mark-ring-length do
  9356. (push (make-marker) org-mark-ring))
  9357. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9358. org-mark-ring)
  9359. (defun org-mark-ring-push (&optional pos buffer)
  9360. "Put the current position or POS into the mark ring and rotate it."
  9361. (interactive)
  9362. (setq pos (or pos (point)))
  9363. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9364. (move-marker (car org-mark-ring)
  9365. (or pos (point))
  9366. (or buffer (current-buffer)))
  9367. (message "%s"
  9368. (substitute-command-keys
  9369. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9370. (defun org-mark-ring-goto (&optional n)
  9371. "Jump to the previous position in the mark ring.
  9372. With prefix arg N, jump back that many stored positions. When
  9373. called several times in succession, walk through the entire ring.
  9374. Org-mode commands jumping to a different position in the current file,
  9375. or to another Org-mode file, automatically push the old position
  9376. onto the ring."
  9377. (interactive "p")
  9378. (let (p m)
  9379. (if (eq last-command this-command)
  9380. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9381. (setq p org-mark-ring))
  9382. (setq org-mark-ring-last-goto p)
  9383. (setq m (car p))
  9384. (org-pop-to-buffer-same-window (marker-buffer m))
  9385. (goto-char m)
  9386. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9387. (defun org-remove-angle-brackets (s)
  9388. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9389. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9390. s)
  9391. (defun org-add-angle-brackets (s)
  9392. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9393. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9394. s)
  9395. (defun org-remove-double-quotes (s)
  9396. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9397. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9398. s)
  9399. ;;; Following specific links
  9400. (defun org-follow-timestamp-link ()
  9401. "Open an agenda view for the time-stamp date/range at point."
  9402. (cond
  9403. ((org-at-date-range-p t)
  9404. (let ((org-agenda-start-on-weekday)
  9405. (t1 (match-string 1))
  9406. (t2 (match-string 2)) tt1 tt2)
  9407. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9408. tt2 (time-to-days (org-time-string-to-time t2)))
  9409. (let ((org-agenda-buffer-tmp-name
  9410. (format "*Org Agenda(a:%s)"
  9411. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9412. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9413. ((org-at-timestamp-p t)
  9414. (let ((org-agenda-buffer-tmp-name
  9415. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9416. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9417. (substring (match-string 1) 0 10)))
  9418. 1)))
  9419. (t (error "This should not happen"))))
  9420. ;;; Following file links
  9421. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9422. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9423. (declare-function mailcap-mime-info
  9424. "mailcap" (string &optional request no-decode))
  9425. (defvar org-wait nil)
  9426. (defun org-open-file (path &optional in-emacs line search)
  9427. "Open the file at PATH.
  9428. First, this expands any special file name abbreviations. Then the
  9429. configuration variable `org-file-apps' is checked if it contains an
  9430. entry for this file type, and if yes, the corresponding command is launched.
  9431. If no application is found, Emacs simply visits the file.
  9432. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9433. With a double \\[universal-argument] \\[universal-argument] \
  9434. prefix arg, Org tries to avoid opening in Emacs
  9435. and to use an external application to visit the file.
  9436. Optional LINE specifies a line to go to, optional SEARCH a string
  9437. to search for. If LINE or SEARCH is given, the file will be
  9438. opened in Emacs, unless an entry from org-file-apps that makes
  9439. use of groups in a regexp matches.
  9440. If you want to change the way frames are used when following a
  9441. link, please customize `org-link-frame-setup'.
  9442. If the file does not exist, an error is thrown."
  9443. (let* ((file (if (equal path "")
  9444. buffer-file-name
  9445. (substitute-in-file-name (expand-file-name path))))
  9446. (file-apps (append org-file-apps (org-default-apps)))
  9447. (apps (org-remove-if
  9448. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9449. (apps-dlink (org-remove-if-not
  9450. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9451. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9452. (dirp (if remp nil (file-directory-p file)))
  9453. (file (if (and dirp org-open-directory-means-index-dot-org)
  9454. (concat (file-name-as-directory file) "index.org")
  9455. file))
  9456. (a-m-a-p (assq 'auto-mode apps))
  9457. (dfile (downcase file))
  9458. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9459. (link (cond ((and (eq line nil)
  9460. (eq search nil))
  9461. file)
  9462. (line
  9463. (concat file "::" (number-to-string line)))
  9464. (search
  9465. (concat file "::" search))))
  9466. (dlink (downcase link))
  9467. (old-buffer (current-buffer))
  9468. (old-pos (point))
  9469. (old-mode major-mode)
  9470. ext cmd link-match-data)
  9471. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9472. (setq ext (match-string 1 dfile))
  9473. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9474. (setq ext (match-string 1 dfile))))
  9475. (cond
  9476. ((member in-emacs '((16) system))
  9477. (setq cmd (cdr (assoc 'system apps))))
  9478. (in-emacs (setq cmd 'emacs))
  9479. (t
  9480. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9481. (and dirp (cdr (assoc 'directory apps)))
  9482. ; first, try matching against apps-dlink
  9483. ; if we get a match here, store the match data for later
  9484. (let ((match (assoc-default dlink apps-dlink
  9485. 'string-match)))
  9486. (if match
  9487. (progn (setq link-match-data (match-data))
  9488. match)
  9489. (progn (setq in-emacs (or in-emacs line search))
  9490. nil))) ; if we have no match in apps-dlink,
  9491. ; always open the file in emacs if line or search
  9492. ; is given (for backwards compatibility)
  9493. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9494. 'string-match)
  9495. (cdr (assoc ext apps))
  9496. (cdr (assoc t apps))))))
  9497. (when (eq cmd 'system)
  9498. (setq cmd (cdr (assoc 'system apps))))
  9499. (when (eq cmd 'default)
  9500. (setq cmd (cdr (assoc t apps))))
  9501. (when (eq cmd 'mailcap)
  9502. (require 'mailcap)
  9503. (mailcap-parse-mailcaps)
  9504. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9505. (command (mailcap-mime-info mime-type)))
  9506. (if (stringp command)
  9507. (setq cmd command)
  9508. (setq cmd 'emacs))))
  9509. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9510. (not (file-exists-p file))
  9511. (not org-open-non-existing-files))
  9512. (error "No such file: %s" file))
  9513. (cond
  9514. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9515. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9516. (while (string-match "['\"]%s['\"]" cmd)
  9517. (setq cmd (replace-match "%s" t t cmd)))
  9518. (while (string-match "%s" cmd)
  9519. (setq cmd (replace-match
  9520. (save-match-data
  9521. (shell-quote-argument
  9522. (convert-standard-filename file)))
  9523. t t cmd)))
  9524. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9525. (save-match-data
  9526. (let ((match-index 1)
  9527. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9528. (set-match-data link-match-data)
  9529. (while (<= match-index number-of-groups)
  9530. (let ((regex (concat "%" (number-to-string match-index)))
  9531. (replace-with (match-string match-index dlink)))
  9532. (while (string-match regex cmd)
  9533. (setq cmd (replace-match replace-with t t cmd))))
  9534. (setq match-index (+ match-index 1)))))
  9535. (save-window-excursion
  9536. (message "Running %s...done" cmd)
  9537. (start-process-shell-command cmd nil cmd)
  9538. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9539. ((or (stringp cmd)
  9540. (eq cmd 'emacs))
  9541. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9542. (widen)
  9543. (if line (org-goto-line line)
  9544. (if search (org-link-search search))))
  9545. ((consp cmd)
  9546. (let ((file (convert-standard-filename file)))
  9547. (save-match-data
  9548. (set-match-data link-match-data)
  9549. (eval cmd))))
  9550. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9551. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9552. (or (not (equal old-buffer (current-buffer)))
  9553. (not (equal old-pos (point))))
  9554. (org-mark-ring-push old-pos old-buffer))))
  9555. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9556. "This function returns non-nil if `entry' uses a regular
  9557. expression which should be matched against the whole link by
  9558. org-open-file.
  9559. It assumes that is the case when the entry uses a regular
  9560. expression which has at least one grouping construct and the
  9561. action is either a lisp form or a command string containing
  9562. '%1', i.e. using at least one subexpression match as a
  9563. parameter."
  9564. (let ((selector (car entry))
  9565. (action (cdr entry)))
  9566. (if (stringp selector)
  9567. (and (> (regexp-opt-depth selector) 0)
  9568. (or (and (stringp action)
  9569. (string-match "%[0-9]" action))
  9570. (consp action)))
  9571. nil)))
  9572. (defun org-default-apps ()
  9573. "Return the default applications for this operating system."
  9574. (cond
  9575. ((eq system-type 'darwin)
  9576. org-file-apps-defaults-macosx)
  9577. ((eq system-type 'windows-nt)
  9578. org-file-apps-defaults-windowsnt)
  9579. (t org-file-apps-defaults-gnu)))
  9580. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9581. "Convert extensions to regular expressions in the cars of LIST.
  9582. Also, weed out any non-string entries, because the return value is used
  9583. only for regexp matching.
  9584. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9585. point to the symbol `emacs', indicating that the file should
  9586. be opened in Emacs."
  9587. (append
  9588. (delq nil
  9589. (mapcar (lambda (x)
  9590. (if (not (stringp (car x)))
  9591. nil
  9592. (if (string-match "\\W" (car x))
  9593. x
  9594. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9595. list))
  9596. (if add-auto-mode
  9597. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9598. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9599. (defun org-file-remote-p (file)
  9600. "Test whether FILE specifies a location on a remote system.
  9601. Return non-nil if the location is indeed remote.
  9602. For example, the filename \"/user@host:/foo\" specifies a location
  9603. on the system \"/user@host:\"."
  9604. (cond ((fboundp 'file-remote-p)
  9605. (file-remote-p file))
  9606. ((fboundp 'tramp-handle-file-remote-p)
  9607. (tramp-handle-file-remote-p file))
  9608. ((and (boundp 'ange-ftp-name-format)
  9609. (string-match (car ange-ftp-name-format) file))
  9610. t)))
  9611. ;;;; Refiling
  9612. (defun org-get-org-file ()
  9613. "Read a filename, with default directory `org-directory'."
  9614. (let ((default (or org-default-notes-file remember-data-file)))
  9615. (read-file-name (format "File name [%s]: " default)
  9616. (file-name-as-directory org-directory)
  9617. default)))
  9618. (defun org-notes-order-reversed-p ()
  9619. "Check if the current file should receive notes in reversed order."
  9620. (cond
  9621. ((not org-reverse-note-order) nil)
  9622. ((eq t org-reverse-note-order) t)
  9623. ((not (listp org-reverse-note-order)) nil)
  9624. (t (catch 'exit
  9625. (let ((all org-reverse-note-order)
  9626. entry)
  9627. (while (setq entry (pop all))
  9628. (if (string-match (car entry) buffer-file-name)
  9629. (throw 'exit (cdr entry))))
  9630. nil)))))
  9631. (defvar org-refile-target-table nil
  9632. "The list of refile targets, created by `org-refile'.")
  9633. (defvar org-agenda-new-buffers nil
  9634. "Buffers created to visit agenda files.")
  9635. (defvar org-refile-cache nil
  9636. "Cache for refile targets.")
  9637. (defvar org-refile-markers nil
  9638. "All the markers used for caching refile locations.")
  9639. (defun org-refile-marker (pos)
  9640. "Get a new refile marker, but only if caching is in use."
  9641. (if (not org-refile-use-cache)
  9642. pos
  9643. (let ((m (make-marker)))
  9644. (move-marker m pos)
  9645. (push m org-refile-markers)
  9646. m)))
  9647. (defun org-refile-cache-clear ()
  9648. "Clear the refile cache and disable all the markers."
  9649. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9650. (setq org-refile-markers nil)
  9651. (setq org-refile-cache nil)
  9652. (message "Refile cache has been cleared"))
  9653. (defun org-refile-cache-check-set (set)
  9654. "Check if all the markers in the cache still have live buffers."
  9655. (let (marker)
  9656. (catch 'exit
  9657. (while (and set (setq marker (nth 3 (pop set))))
  9658. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9659. (when (and marker (null (marker-buffer marker)))
  9660. (message "not found") (sit-for 3)
  9661. (throw 'exit nil)))
  9662. t)))
  9663. (defun org-refile-cache-put (set &rest identifiers)
  9664. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9665. (let* ((key (sha1 (prin1-to-string identifiers)))
  9666. (entry (assoc key org-refile-cache)))
  9667. (if entry
  9668. (setcdr entry set)
  9669. (push (cons key set) org-refile-cache))))
  9670. (defun org-refile-cache-get (&rest identifiers)
  9671. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9672. (cond
  9673. ((not org-refile-cache) nil)
  9674. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9675. (t
  9676. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9677. org-refile-cache))))
  9678. (and set (org-refile-cache-check-set set) set)))))
  9679. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9680. "Produce a table with refile targets."
  9681. (let ((case-fold-search nil)
  9682. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9683. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9684. targets tgs txt re files f desc descre fast-path-p level pos0)
  9685. (message "Getting targets...")
  9686. (with-current-buffer (or default-buffer (current-buffer))
  9687. (while (setq entry (pop entries))
  9688. (setq files (car entry) desc (cdr entry))
  9689. (setq fast-path-p nil)
  9690. (cond
  9691. ((null files) (setq files (list (current-buffer))))
  9692. ((eq files 'org-agenda-files)
  9693. (setq files (org-agenda-files 'unrestricted)))
  9694. ((and (symbolp files) (fboundp files))
  9695. (setq files (funcall files)))
  9696. ((and (symbolp files) (boundp files))
  9697. (setq files (symbol-value files))))
  9698. (if (stringp files) (setq files (list files)))
  9699. (cond
  9700. ((eq (car desc) :tag)
  9701. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9702. ((eq (car desc) :todo)
  9703. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9704. ((eq (car desc) :regexp)
  9705. (setq descre (cdr desc)))
  9706. ((eq (car desc) :level)
  9707. (setq descre (concat "^\\*\\{" (number-to-string
  9708. (if org-odd-levels-only
  9709. (1- (* 2 (cdr desc)))
  9710. (cdr desc)))
  9711. "\\}[ \t]")))
  9712. ((eq (car desc) :maxlevel)
  9713. (setq fast-path-p t)
  9714. (setq descre (concat "^\\*\\{1," (number-to-string
  9715. (if org-odd-levels-only
  9716. (1- (* 2 (cdr desc)))
  9717. (cdr desc)))
  9718. "\\}[ \t]")))
  9719. (t (error "Bad refiling target description %s" desc)))
  9720. (while (setq f (pop files))
  9721. (with-current-buffer
  9722. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9723. (or
  9724. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9725. (progn
  9726. (if (bufferp f) (setq f (buffer-file-name
  9727. (buffer-base-buffer f))))
  9728. (setq f (and f (expand-file-name f)))
  9729. (if (eq org-refile-use-outline-path 'file)
  9730. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9731. (save-excursion
  9732. (save-restriction
  9733. (widen)
  9734. (goto-char (point-min))
  9735. (while (re-search-forward descre nil t)
  9736. (goto-char (setq pos0 (point-at-bol)))
  9737. (catch 'next
  9738. (when org-refile-target-verify-function
  9739. (save-match-data
  9740. (or (funcall org-refile-target-verify-function)
  9741. (throw 'next t))))
  9742. (when (and (looking-at org-complex-heading-regexp)
  9743. (not (member (match-string 4) excluded-entries))
  9744. (match-string 4))
  9745. (setq level (org-reduced-level
  9746. (- (match-end 1) (match-beginning 1)))
  9747. txt (org-link-display-format (match-string 4))
  9748. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9749. re (format org-complex-heading-regexp-format
  9750. (regexp-quote (match-string 4))))
  9751. (when org-refile-use-outline-path
  9752. (setq txt (mapconcat
  9753. 'org-protect-slash
  9754. (append
  9755. (if (eq org-refile-use-outline-path
  9756. 'file)
  9757. (list (file-name-nondirectory
  9758. (buffer-file-name
  9759. (buffer-base-buffer))))
  9760. (if (eq org-refile-use-outline-path
  9761. 'full-file-path)
  9762. (list (buffer-file-name
  9763. (buffer-base-buffer)))))
  9764. (org-get-outline-path fast-path-p
  9765. level txt)
  9766. (list txt))
  9767. "/")))
  9768. (push (list txt f re (org-refile-marker (point)))
  9769. tgs)))
  9770. (when (= (point) pos0)
  9771. ;; verification function has not moved point
  9772. (goto-char (point-at-eol))))))))
  9773. (when org-refile-use-cache
  9774. (org-refile-cache-put tgs (buffer-file-name) descre))
  9775. (setq targets (append tgs targets))
  9776. ))))
  9777. (message "Getting targets...done")
  9778. (nreverse targets)))
  9779. (defun org-protect-slash (s)
  9780. (while (string-match "/" s)
  9781. (setq s (replace-match "\\" t t s)))
  9782. s)
  9783. (defvar org-olpa (make-vector 20 nil))
  9784. (defun org-get-outline-path (&optional fastp level heading)
  9785. "Return the outline path to the current entry, as a list.
  9786. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9787. routine which makes outline path derivations for an entire file,
  9788. avoiding backtracing. Refile target collection makes use of that."
  9789. (if fastp
  9790. (progn
  9791. (if (> level 19)
  9792. (error "Outline path failure, more than 19 levels"))
  9793. (loop for i from level upto 19 do
  9794. (aset org-olpa i nil))
  9795. (prog1
  9796. (delq nil (append org-olpa nil))
  9797. (aset org-olpa level heading)))
  9798. (let (rtn case-fold-search)
  9799. (save-excursion
  9800. (save-restriction
  9801. (widen)
  9802. (while (org-up-heading-safe)
  9803. (when (looking-at org-complex-heading-regexp)
  9804. (push (org-match-string-no-properties 4) rtn)))
  9805. rtn)))))
  9806. (defun org-format-outline-path (path &optional width prefix separator)
  9807. "Format the outline path PATH for display.
  9808. WIDTH is the maximum number of characters that is available.
  9809. PREFIX is a prefix to be included in the returned string,
  9810. such as the file name.
  9811. SEPARATOR is inserted between the different parts of the path,
  9812. the default is \"/\"."
  9813. (setq width (or width 79))
  9814. (if prefix (setq width (- width (length prefix))))
  9815. (if (not path)
  9816. (or prefix "")
  9817. (let* ((nsteps (length path))
  9818. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9819. (maxwidth (if (<= total-width width)
  9820. 10000 ;; everything fits
  9821. ;; we need to shorten the level headings
  9822. (/ (- width nsteps) nsteps)))
  9823. (org-odd-levels-only nil)
  9824. (n 0)
  9825. (total (1+ (length prefix))))
  9826. (setq maxwidth (max maxwidth 10))
  9827. (concat prefix
  9828. (if prefix (or separator "/"))
  9829. (mapconcat
  9830. (lambda (h)
  9831. (setq n (1+ n))
  9832. (if (and (= n nsteps) (< maxwidth 10000))
  9833. (setq maxwidth (- total-width total)))
  9834. (if (< (length h) maxwidth)
  9835. (progn (setq total (+ total (length h) 1)) h)
  9836. (setq h (substring h 0 (- maxwidth 2))
  9837. total (+ total maxwidth 1))
  9838. (if (string-match "[ \t]+\\'" h)
  9839. (setq h (substring h 0 (match-beginning 0))))
  9840. (setq h (concat h "..")))
  9841. (org-add-props h nil 'face
  9842. (nth (% (1- n) org-n-level-faces)
  9843. org-level-faces))
  9844. h)
  9845. path (or separator "/"))))))
  9846. (defun org-display-outline-path (&optional file current separator just-return-string)
  9847. "Display the current outline path in the echo area.
  9848. If FILE is non-nil, prepend the output with the file name.
  9849. If CURRENT is non-nil, append the current heading to the output.
  9850. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9851. the different parts of the path and defaults to \"/\".
  9852. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9853. (interactive "P")
  9854. (let* (case-fold-search
  9855. message-log-max ; Don't populate the *Messages* buffer
  9856. (bfn (buffer-file-name (buffer-base-buffer)))
  9857. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9858. res)
  9859. (if current (setq path (append path
  9860. (save-excursion
  9861. (org-back-to-heading t)
  9862. (if (looking-at org-complex-heading-regexp)
  9863. (list (match-string 4)))))))
  9864. (setq res
  9865. (org-format-outline-path
  9866. path
  9867. (1- (frame-width))
  9868. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9869. separator))
  9870. (if just-return-string
  9871. (org-no-properties res)
  9872. (message "%s" res))))
  9873. (defvar org-refile-history nil
  9874. "History for refiling operations.")
  9875. (defvar org-after-refile-insert-hook nil
  9876. "Hook run after `org-refile' has inserted its stuff at the new location.
  9877. Note that this is still *before* the stuff will be removed from
  9878. the *old* location.")
  9879. (defvar org-capture-last-stored-marker)
  9880. (defvar org-refile-keep nil
  9881. "Non-nil means `org-refile' will copy instead of refile.")
  9882. (defun org-copy ()
  9883. "Like `org-refile', but copy."
  9884. (interactive)
  9885. (let ((org-refile-keep t))
  9886. (funcall 'org-refile nil nil nil "Copy")))
  9887. (defun org-refile (&optional goto default-buffer rfloc msg)
  9888. "Move the entry or entries at point to another heading.
  9889. The list of target headings is compiled using the information in
  9890. `org-refile-targets', which see.
  9891. At the target location, the entry is filed as a subitem of the target
  9892. heading. Depending on `org-reverse-note-order', the new subitem will
  9893. either be the first or the last subitem.
  9894. If there is an active region, all entries in that region will be moved.
  9895. However, the region must fulfill the requirement that the first heading
  9896. is the first one sets the top-level of the moved text - at most siblings
  9897. below it are allowed.
  9898. With prefix arg GOTO, the command will only visit the target location
  9899. and not actually move anything.
  9900. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9901. go to the location where the last refiling operation has put the subtree.
  9902. With a prefix argument of `2', refile to the running clock.
  9903. RFLOC can be a refile location obtained in a different way.
  9904. MSG is a string to replace \"Refile\" in the default prompt with
  9905. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9906. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9907. If you are using target caching (see `org-refile-use-cache'),
  9908. you have to clear the target cache in order to find new targets.
  9909. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9910. prefix argument (`C-u C-u C-u C-c C-w')."
  9911. (interactive "P")
  9912. (if (member goto '(0 (64)))
  9913. (org-refile-cache-clear)
  9914. (let* ((actionmsg (or msg "Refile"))
  9915. (cbuf (current-buffer))
  9916. (regionp (org-region-active-p))
  9917. (region-start (and regionp (region-beginning)))
  9918. (region-end (and regionp (region-end)))
  9919. (region-length (and regionp (- region-end region-start)))
  9920. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9921. pos it nbuf file re level reversed)
  9922. (setq last-command nil)
  9923. (when regionp
  9924. (goto-char region-start)
  9925. (or (bolp) (goto-char (point-at-bol)))
  9926. (setq region-start (point))
  9927. (unless (or (org-kill-is-subtree-p
  9928. (buffer-substring region-start region-end))
  9929. (prog1 org-refile-active-region-within-subtree
  9930. (org-toggle-heading)))
  9931. (error "The region is not a (sequence of) subtree(s)")))
  9932. (if (equal goto '(16))
  9933. (org-refile-goto-last-stored)
  9934. (when (or
  9935. (and (equal goto 2)
  9936. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9937. (prog1
  9938. (setq it (list (or org-clock-heading "running clock")
  9939. (buffer-file-name
  9940. (marker-buffer org-clock-hd-marker))
  9941. ""
  9942. (marker-position org-clock-hd-marker)))
  9943. (setq goto nil)))
  9944. (setq it (or rfloc
  9945. (let (heading-text)
  9946. (save-excursion
  9947. (unless goto
  9948. (org-back-to-heading t)
  9949. (setq heading-text
  9950. (nth 4 (org-heading-components))))
  9951. (org-refile-get-location
  9952. (cond (goto "Goto")
  9953. (regionp (concat actionmsg " region to"))
  9954. (t (concat actionmsg " subtree \""
  9955. heading-text "\" to")))
  9956. default-buffer
  9957. (and (not (equal '(4) goto))
  9958. org-refile-allow-creating-parent-nodes)
  9959. goto))))))
  9960. (setq file (nth 1 it)
  9961. re (nth 2 it)
  9962. pos (nth 3 it))
  9963. (if (and (not goto)
  9964. pos
  9965. (equal (buffer-file-name) file)
  9966. (if regionp
  9967. (and (>= pos region-start)
  9968. (<= pos region-end))
  9969. (and (>= pos (point))
  9970. (< pos (save-excursion
  9971. (org-end-of-subtree t t))))))
  9972. (error "Cannot refile to position inside the tree or region"))
  9973. (setq nbuf (or (find-buffer-visiting file)
  9974. (find-file-noselect file)))
  9975. (if goto
  9976. (progn
  9977. (org-pop-to-buffer-same-window nbuf)
  9978. (goto-char pos)
  9979. (org-show-context 'org-goto))
  9980. (if regionp
  9981. (progn
  9982. (org-kill-new (buffer-substring region-start region-end))
  9983. (org-save-markers-in-region region-start region-end))
  9984. (org-copy-subtree 1 nil t))
  9985. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9986. (find-file-noselect file)))
  9987. (setq reversed (org-notes-order-reversed-p))
  9988. (save-excursion
  9989. (save-restriction
  9990. (widen)
  9991. (if pos
  9992. (progn
  9993. (goto-char pos)
  9994. (looking-at org-outline-regexp)
  9995. (setq level (org-get-valid-level (funcall outline-level) 1))
  9996. (goto-char
  9997. (if reversed
  9998. (or (outline-next-heading) (point-max))
  9999. (or (save-excursion (org-get-next-sibling))
  10000. (org-end-of-subtree t t)
  10001. (point-max)))))
  10002. (setq level 1)
  10003. (if (not reversed)
  10004. (goto-char (point-max))
  10005. (goto-char (point-min))
  10006. (or (outline-next-heading) (goto-char (point-max)))))
  10007. (if (not (bolp)) (newline))
  10008. (org-paste-subtree level)
  10009. (when org-log-refile
  10010. (org-add-log-setup 'refile nil nil 'findpos
  10011. org-log-refile)
  10012. (unless (eq org-log-refile 'note)
  10013. (save-excursion (org-add-log-note))))
  10014. (and org-auto-align-tags
  10015. (let ((org-loop-over-headlines-in-active-region nil))
  10016. (org-set-tags nil t)))
  10017. (with-demoted-errors
  10018. (bookmark-set "org-refile-last-stored"))
  10019. ;; If we are refiling for capture, make sure that the
  10020. ;; last-capture pointers point here
  10021. (when (org-bound-and-true-p org-refile-for-capture)
  10022. (with-demoted-errors
  10023. (bookmark-set "org-capture-last-stored-marker"))
  10024. (move-marker org-capture-last-stored-marker (point)))
  10025. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10026. (run-hooks 'org-after-refile-insert-hook))))
  10027. (unless org-refile-keep
  10028. (if regionp
  10029. (delete-region (point) (+ (point) region-length))
  10030. (org-cut-subtree)))
  10031. (when (featurep 'org-inlinetask)
  10032. (org-inlinetask-remove-END-maybe))
  10033. (setq org-markers-to-move nil)
  10034. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10035. (defun org-refile-goto-last-stored ()
  10036. "Go to the location where the last refile was stored."
  10037. (interactive)
  10038. (bookmark-jump "org-refile-last-stored")
  10039. (message "This is the location of the last refile"))
  10040. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10041. no-exclude)
  10042. "Prompt the user for a refile location, using PROMPT.
  10043. PROMPT should not be suffixed with a colon and a space, because
  10044. this function appends the default value from
  10045. `org-refile-history' automatically, if that is not empty.
  10046. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10047. this is used for the GOTO interface."
  10048. (let ((org-refile-targets org-refile-targets)
  10049. (org-refile-use-outline-path org-refile-use-outline-path)
  10050. excluded-entries)
  10051. (when (and (derived-mode-p 'org-mode)
  10052. (not org-refile-use-cache)
  10053. (not no-exclude))
  10054. (org-map-tree
  10055. (lambda()
  10056. (setq excluded-entries
  10057. (append excluded-entries (list (org-get-heading t t)))))))
  10058. (setq org-refile-target-table
  10059. (org-refile-get-targets default-buffer excluded-entries)))
  10060. (unless org-refile-target-table
  10061. (error "No refile targets"))
  10062. (let* ((cbuf (current-buffer))
  10063. (partial-completion-mode nil)
  10064. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10065. (cfunc (if (and org-refile-use-outline-path
  10066. org-outline-path-complete-in-steps)
  10067. 'org-olpath-completing-read
  10068. 'org-icompleting-read))
  10069. (extra (if org-refile-use-outline-path "/" ""))
  10070. (cbnex (concat (buffer-name) extra))
  10071. (filename (and cfn (expand-file-name cfn)))
  10072. (tbl (mapcar
  10073. (lambda (x)
  10074. (if (and (not (member org-refile-use-outline-path
  10075. '(file full-file-path)))
  10076. (not (equal filename (nth 1 x))))
  10077. (cons (concat (car x) extra " ("
  10078. (file-name-nondirectory (nth 1 x)) ")")
  10079. (cdr x))
  10080. (cons (concat (car x) extra) (cdr x))))
  10081. org-refile-target-table))
  10082. (completion-ignore-case t)
  10083. cdef
  10084. (prompt (concat prompt
  10085. (or (and (car org-refile-history)
  10086. (concat " (default " (car org-refile-history) ")"))
  10087. (and (assoc cbnex tbl) (setq cdef cbnex)
  10088. (concat " (default " cbnex ")"))) ": "))
  10089. pa answ parent-target child parent old-hist)
  10090. (setq old-hist org-refile-history)
  10091. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10092. nil 'org-refile-history (or cdef (car org-refile-history))))
  10093. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10094. (org-refile-check-position pa)
  10095. (if pa
  10096. (progn
  10097. (when (or (not org-refile-history)
  10098. (not (eq old-hist org-refile-history))
  10099. (not (equal (car pa) (car org-refile-history))))
  10100. (setq org-refile-history
  10101. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10102. org-refile-history
  10103. (cdr org-refile-history))))
  10104. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10105. (pop org-refile-history)))
  10106. pa)
  10107. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10108. (progn
  10109. (setq parent (match-string 1 answ)
  10110. child (match-string 2 answ))
  10111. (setq parent-target (or (assoc parent tbl)
  10112. (assoc (concat parent "/") tbl)))
  10113. (when (and parent-target
  10114. (or (eq new-nodes t)
  10115. (and (eq new-nodes 'confirm)
  10116. (y-or-n-p (format "Create new node \"%s\"? "
  10117. child)))))
  10118. (org-refile-new-child parent-target child)))
  10119. (error "Invalid target location")))))
  10120. (declare-function org-string-nw-p "org-macs" (s))
  10121. (defun org-refile-check-position (refile-pointer)
  10122. "Check if the refile pointer matches the headline to which it points."
  10123. (let* ((file (nth 1 refile-pointer))
  10124. (re (nth 2 refile-pointer))
  10125. (pos (nth 3 refile-pointer))
  10126. buffer)
  10127. (if (and (not (markerp pos)) (not file))
  10128. (error "Please save the buffer to a file before refiling")
  10129. (when (org-string-nw-p re)
  10130. (setq buffer (if (markerp pos)
  10131. (marker-buffer pos)
  10132. (or (find-buffer-visiting file)
  10133. (find-file-noselect file))))
  10134. (with-current-buffer buffer
  10135. (save-excursion
  10136. (save-restriction
  10137. (widen)
  10138. (goto-char pos)
  10139. (beginning-of-line 1)
  10140. (unless (org-looking-at-p re)
  10141. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10142. (defun org-refile-new-child (parent-target child)
  10143. "Use refile target PARENT-TARGET to add new CHILD below it."
  10144. (unless parent-target
  10145. (error "Cannot find parent for new node"))
  10146. (let ((file (nth 1 parent-target))
  10147. (pos (nth 3 parent-target))
  10148. level)
  10149. (with-current-buffer (or (find-buffer-visiting file)
  10150. (find-file-noselect file))
  10151. (save-excursion
  10152. (save-restriction
  10153. (widen)
  10154. (if pos
  10155. (goto-char pos)
  10156. (goto-char (point-max))
  10157. (if (not (bolp)) (newline)))
  10158. (when (looking-at org-outline-regexp)
  10159. (setq level (funcall outline-level))
  10160. (org-end-of-subtree t t))
  10161. (org-back-over-empty-lines)
  10162. (insert "\n" (make-string
  10163. (if pos (org-get-valid-level level 1) 1) ?*)
  10164. " " child "\n")
  10165. (beginning-of-line 0)
  10166. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10167. (defun org-olpath-completing-read (prompt collection &rest args)
  10168. "Read an outline path like a file name."
  10169. (let ((thetable collection)
  10170. (org-completion-use-ido nil) ; does not work with ido.
  10171. (org-completion-use-iswitchb nil)) ; or iswitchb
  10172. (apply
  10173. 'org-icompleting-read prompt
  10174. (lambda (string predicate &optional flag)
  10175. (let (rtn r f (l (length string)))
  10176. (cond
  10177. ((eq flag nil)
  10178. ;; try completion
  10179. (try-completion string thetable))
  10180. ((eq flag t)
  10181. ;; all-completions
  10182. (setq rtn (all-completions string thetable predicate))
  10183. (mapcar
  10184. (lambda (x)
  10185. (setq r (substring x l))
  10186. (if (string-match " ([^)]*)$" x)
  10187. (setq f (match-string 0 x))
  10188. (setq f ""))
  10189. (if (string-match "/" r)
  10190. (concat string (substring r 0 (match-end 0)) f)
  10191. x))
  10192. rtn))
  10193. ((eq flag 'lambda)
  10194. ;; exact match?
  10195. (assoc string thetable)))))
  10196. args)))
  10197. ;;;; Dynamic blocks
  10198. (defun org-find-dblock (name)
  10199. "Find the first dynamic block with name NAME in the buffer.
  10200. If not found, stay at current position and return nil."
  10201. (let ((case-fold-search t) pos)
  10202. (save-excursion
  10203. (goto-char (point-min))
  10204. (setq pos (and (re-search-forward
  10205. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10206. (match-beginning 0))))
  10207. (if pos (goto-char pos))
  10208. pos))
  10209. (defconst org-dblock-start-re
  10210. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10211. "Matches the start line of a dynamic block, with parameters.")
  10212. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10213. "Matches the end of a dynamic block.")
  10214. (defun org-create-dblock (plist)
  10215. "Create a dynamic block section, with parameters taken from PLIST.
  10216. PLIST must contain a :name entry which is used as name of the block."
  10217. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10218. (end-of-line 1)
  10219. (newline))
  10220. (let ((col (current-column))
  10221. (name (plist-get plist :name)))
  10222. (insert "#+BEGIN: " name)
  10223. (while plist
  10224. (if (eq (car plist) :name)
  10225. (setq plist (cddr plist))
  10226. (insert " " (prin1-to-string (pop plist)))))
  10227. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10228. (beginning-of-line -2)))
  10229. (defun org-prepare-dblock ()
  10230. "Prepare dynamic block for refresh.
  10231. This empties the block, puts the cursor at the insert position and returns
  10232. the property list including an extra property :name with the block name."
  10233. (unless (looking-at org-dblock-start-re)
  10234. (error "Not at a dynamic block"))
  10235. (let* ((begdel (1+ (match-end 0)))
  10236. (name (org-no-properties (match-string 1)))
  10237. (params (append (list :name name)
  10238. (read (concat "(" (match-string 3) ")")))))
  10239. (save-excursion
  10240. (beginning-of-line 1)
  10241. (skip-chars-forward " \t")
  10242. (setq params (plist-put params :indentation-column (current-column))))
  10243. (unless (re-search-forward org-dblock-end-re nil t)
  10244. (error "Dynamic block not terminated"))
  10245. (setq params
  10246. (append params
  10247. (list :content (buffer-substring
  10248. begdel (match-beginning 0)))))
  10249. (delete-region begdel (match-beginning 0))
  10250. (goto-char begdel)
  10251. (open-line 1)
  10252. params))
  10253. (defun org-map-dblocks (&optional command)
  10254. "Apply COMMAND to all dynamic blocks in the current buffer.
  10255. If COMMAND is not given, use `org-update-dblock'."
  10256. (let ((cmd (or command 'org-update-dblock)))
  10257. (save-excursion
  10258. (goto-char (point-min))
  10259. (while (re-search-forward org-dblock-start-re nil t)
  10260. (goto-char (match-beginning 0))
  10261. (save-excursion
  10262. (condition-case nil
  10263. (funcall cmd)
  10264. (error (message "Error during update of dynamic block"))))
  10265. (unless (re-search-forward org-dblock-end-re nil t)
  10266. (error "Dynamic block not terminated"))))))
  10267. (defun org-dblock-update (&optional arg)
  10268. "User command for updating dynamic blocks.
  10269. Update the dynamic block at point. With prefix ARG, update all dynamic
  10270. blocks in the buffer."
  10271. (interactive "P")
  10272. (if arg
  10273. (org-update-all-dblocks)
  10274. (or (looking-at org-dblock-start-re)
  10275. (org-beginning-of-dblock))
  10276. (org-update-dblock)))
  10277. (defun org-update-dblock ()
  10278. "Update the dynamic block at point.
  10279. This means to empty the block, parse for parameters and then call
  10280. the correct writing function."
  10281. (interactive)
  10282. (save-window-excursion
  10283. (let* ((pos (point))
  10284. (line (org-current-line))
  10285. (params (org-prepare-dblock))
  10286. (name (plist-get params :name))
  10287. (indent (plist-get params :indentation-column))
  10288. (cmd (intern (concat "org-dblock-write:" name))))
  10289. (message "Updating dynamic block `%s' at line %d..." name line)
  10290. (funcall cmd params)
  10291. (message "Updating dynamic block `%s' at line %d...done" name line)
  10292. (goto-char pos)
  10293. (when (and indent (> indent 0))
  10294. (setq indent (make-string indent ?\ ))
  10295. (save-excursion
  10296. (org-beginning-of-dblock)
  10297. (forward-line 1)
  10298. (while (not (looking-at org-dblock-end-re))
  10299. (insert indent)
  10300. (beginning-of-line 2))
  10301. (when (looking-at org-dblock-end-re)
  10302. (and (looking-at "[ \t]+")
  10303. (replace-match ""))
  10304. (insert indent)))))))
  10305. (defun org-beginning-of-dblock ()
  10306. "Find the beginning of the dynamic block at point.
  10307. Error if there is no such block at point."
  10308. (let ((pos (point))
  10309. beg)
  10310. (end-of-line 1)
  10311. (if (and (re-search-backward org-dblock-start-re nil t)
  10312. (setq beg (match-beginning 0))
  10313. (re-search-forward org-dblock-end-re nil t)
  10314. (> (match-end 0) pos))
  10315. (goto-char beg)
  10316. (goto-char pos)
  10317. (error "Not in a dynamic block"))))
  10318. (defun org-update-all-dblocks ()
  10319. "Update all dynamic blocks in the buffer.
  10320. This function can be used in a hook."
  10321. (interactive)
  10322. (when (derived-mode-p 'org-mode)
  10323. (org-map-dblocks 'org-update-dblock)))
  10324. ;;;; Completion
  10325. (defconst org-additional-option-like-keywords
  10326. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10327. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10328. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10329. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10330. "BEGIN:" "END:"
  10331. "ORGTBL" "TBLFM:" "TBLNAME:"
  10332. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10333. "BEGIN_VERBATIM" "END_VERBATIM"
  10334. "BEGIN_QUOTE" "END_QUOTE"
  10335. "BEGIN_VERSE" "END_VERSE"
  10336. "BEGIN_CENTER" "END_CENTER"
  10337. "BEGIN_SRC" "END_SRC"
  10338. "BEGIN_RESULT" "END_RESULT"
  10339. "BEGIN_lstlisting" "END_lstlisting"
  10340. "NAME:" "RESULTS:"
  10341. "HEADER:" "HEADERS:"
  10342. "COLUMNS:" "PROPERTY:"
  10343. "CAPTION:" "LABEL:"
  10344. "SETUPFILE:"
  10345. "INCLUDE:" "INDEX:"
  10346. "BIND:"
  10347. "MACRO:"))
  10348. (defconst org-options-keywords
  10349. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10350. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10351. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10352. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10353. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10354. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10355. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10356. (defconst org-additional-option-like-keywords-for-flyspell
  10357. (delete-dups
  10358. (split-string
  10359. (mapconcat (lambda(k)
  10360. (replace-regexp-in-string
  10361. "_\\|:" " "
  10362. (concat k " " (downcase k) " " (upcase k))))
  10363. (append org-options-keywords org-additional-option-like-keywords)
  10364. " ")
  10365. " +" t)))
  10366. (defcustom org-structure-template-alist
  10367. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10368. "<src lang=\"?\">\n\n</src>")
  10369. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10370. "<example>\n?\n</example>")
  10371. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10372. "<quote>\n?\n</quote>")
  10373. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10374. "<verse>\n?\n</verse>")
  10375. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10376. "<verbatim>\n?\n</verbatim>")
  10377. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10378. "<center>\n?\n</center>")
  10379. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10380. "<literal style=\"latex\">\n?\n</literal>")
  10381. ("L" "#+LaTeX: "
  10382. "<literal style=\"latex\">?</literal>")
  10383. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10384. "<literal style=\"html\">\n?\n</literal>")
  10385. ("H" "#+HTML: "
  10386. "<literal style=\"html\">?</literal>")
  10387. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10388. ("A" "#+ASCII: ")
  10389. ("i" "#+INDEX: ?"
  10390. "#+INDEX: ?")
  10391. ("I" "#+INCLUDE: %file ?"
  10392. "<include file=%file markup=\"?\">"))
  10393. "Structure completion elements.
  10394. This is a list of abbreviation keys and values. The value gets inserted
  10395. if you type `<' followed by the key and then press the completion key,
  10396. usually `M-TAB'. %file will be replaced by a file name after prompting
  10397. for the file using completion. The cursor will be placed at the position
  10398. of the `?` in the template.
  10399. There are two templates for each key, the first uses the original Org syntax,
  10400. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10401. the default when the /org-mtags.el/ module has been loaded. See also the
  10402. variable `org-mtags-prefer-muse-templates'."
  10403. :group 'org-completion
  10404. :type '(repeat
  10405. (string :tag "Key")
  10406. (string :tag "Template")
  10407. (string :tag "Muse Template")))
  10408. (defun org-try-structure-completion ()
  10409. "Try to complete a structure template before point.
  10410. This looks for strings like \"<e\" on an otherwise empty line and
  10411. expands them."
  10412. (let ((l (buffer-substring (point-at-bol) (point)))
  10413. a)
  10414. (when (and (looking-at "[ \t]*$")
  10415. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10416. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10417. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10418. (match-beginning 1)) a)
  10419. t)))
  10420. (defun org-complete-expand-structure-template (start cell)
  10421. "Expand a structure template."
  10422. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10423. (rpl (nth (if musep 2 1) cell))
  10424. (ind ""))
  10425. (delete-region start (point))
  10426. (when (string-match "\\`#\\+" rpl)
  10427. (cond
  10428. ((bolp))
  10429. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10430. (setq ind (buffer-substring (point-at-bol) (point))))
  10431. (t (newline))))
  10432. (setq start (point))
  10433. (if (string-match "%file" rpl)
  10434. (setq rpl (replace-match
  10435. (concat
  10436. "\""
  10437. (save-match-data
  10438. (abbreviate-file-name (read-file-name "Include file: ")))
  10439. "\"")
  10440. t t rpl)))
  10441. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10442. (concat "\n" ind)))
  10443. (insert rpl)
  10444. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10445. ;;;; TODO, DEADLINE, Comments
  10446. (defun org-toggle-comment ()
  10447. "Change the COMMENT state of an entry."
  10448. (interactive)
  10449. (save-excursion
  10450. (org-back-to-heading)
  10451. (let (case-fold-search)
  10452. (cond
  10453. ((looking-at (format org-heading-keyword-regexp-format
  10454. org-comment-string))
  10455. (goto-char (match-end 1))
  10456. (looking-at (concat " +" org-comment-string))
  10457. (replace-match "" t t)
  10458. (when (eolp) (insert " ")))
  10459. ((looking-at org-outline-regexp)
  10460. (goto-char (match-end 0))
  10461. (insert org-comment-string " "))))))
  10462. (defvar org-last-todo-state-is-todo nil
  10463. "This is non-nil when the last TODO state change led to a TODO state.
  10464. If the last change removed the TODO tag or switched to DONE, then
  10465. this is nil.")
  10466. (defvar org-setting-tags nil) ; dynamically skipped
  10467. (defvar org-todo-setup-filter-hook nil
  10468. "Hook for functions that pre-filter todo specs.
  10469. Each function takes a todo spec and returns either nil or the spec
  10470. transformed into canonical form." )
  10471. (defvar org-todo-get-default-hook nil
  10472. "Hook for functions that get a default item for todo.
  10473. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10474. nil or a string to be used for the todo mark." )
  10475. (defvar org-agenda-headline-snapshot-before-repeat)
  10476. (defun org-current-effective-time ()
  10477. "Return current time adjusted for `org-extend-today-until' variable."
  10478. (let* ((ct (org-current-time))
  10479. (dct (decode-time ct))
  10480. (ct1
  10481. (cond
  10482. (org-use-last-clock-out-time-as-effective-time
  10483. (or (org-clock-get-last-clock-out-time) ct))
  10484. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10485. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10486. (t ct))))
  10487. ct1))
  10488. (defun org-todo-yesterday (&optional arg)
  10489. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10490. (interactive "P")
  10491. (if (eq major-mode 'org-agenda-mode)
  10492. (apply 'org-agenda-todo-yesterday arg)
  10493. (let* ((hour (third (decode-time
  10494. (org-current-time))))
  10495. (org-extend-today-until (1+ hour)))
  10496. (org-todo arg))))
  10497. (defvar org-block-entry-blocking ""
  10498. "First entry preventing the TODO state change.")
  10499. (defun org-todo (&optional arg)
  10500. "Change the TODO state of an item.
  10501. The state of an item is given by a keyword at the start of the heading,
  10502. like
  10503. *** TODO Write paper
  10504. *** DONE Call mom
  10505. The different keywords are specified in the variable `org-todo-keywords'.
  10506. By default the available states are \"TODO\" and \"DONE\".
  10507. So for this example: when the item starts with TODO, it is changed to DONE.
  10508. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10509. DONE are present, add TODO at the beginning of the heading.
  10510. With \\[universal-argument] prefix arg, use completion to determine the new \
  10511. state.
  10512. With numeric prefix arg, switch to that state.
  10513. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10514. keywords (nextset).
  10515. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10516. With a numeric prefix arg of 0, inhibit note taking for the change.
  10517. For calling through lisp, arg is also interpreted in the following way:
  10518. 'none -> empty state
  10519. \"\"(empty string) -> switch to empty state
  10520. 'done -> switch to DONE
  10521. 'nextset -> switch to the next set of keywords
  10522. 'previousset -> switch to the previous set of keywords
  10523. \"WAITING\" -> switch to the specified keyword, but only if it
  10524. really is a member of `org-todo-keywords'."
  10525. (interactive "P")
  10526. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10527. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10528. 'region-start-level 'region))
  10529. org-loop-over-headlines-in-active-region)
  10530. (org-map-entries
  10531. `(org-todo ,arg)
  10532. org-loop-over-headlines-in-active-region
  10533. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10534. (if (equal arg '(16)) (setq arg 'nextset))
  10535. (let ((org-blocker-hook org-blocker-hook)
  10536. commentp
  10537. case-fold-search)
  10538. (when (equal arg '(64))
  10539. (setq arg nil org-blocker-hook nil))
  10540. (when (and org-blocker-hook
  10541. (or org-inhibit-blocking
  10542. (org-entry-get nil "NOBLOCKING")))
  10543. (setq org-blocker-hook nil))
  10544. (save-excursion
  10545. (catch 'exit
  10546. (org-back-to-heading t)
  10547. (when (looking-at (concat "^\\*+ " org-comment-string))
  10548. (org-toggle-comment)
  10549. (setq commentp t))
  10550. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10551. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10552. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10553. (let* ((match-data (match-data))
  10554. (startpos (point-at-bol))
  10555. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10556. (org-log-done org-log-done)
  10557. (org-log-repeat org-log-repeat)
  10558. (org-todo-log-states org-todo-log-states)
  10559. (org-inhibit-logging
  10560. (if (equal arg 0)
  10561. (progn (setq arg nil) 'note) org-inhibit-logging))
  10562. (this (match-string 1))
  10563. (hl-pos (match-beginning 0))
  10564. (head (org-get-todo-sequence-head this))
  10565. (ass (assoc head org-todo-kwd-alist))
  10566. (interpret (nth 1 ass))
  10567. (done-word (nth 3 ass))
  10568. (final-done-word (nth 4 ass))
  10569. (org-last-state (or this ""))
  10570. (completion-ignore-case t)
  10571. (member (member this org-todo-keywords-1))
  10572. (tail (cdr member))
  10573. (org-state (cond
  10574. ((and org-todo-key-trigger
  10575. (or (and (equal arg '(4))
  10576. (eq org-use-fast-todo-selection 'prefix))
  10577. (and (not arg) org-use-fast-todo-selection
  10578. (not (eq org-use-fast-todo-selection
  10579. 'prefix)))))
  10580. ;; Use fast selection
  10581. (org-fast-todo-selection))
  10582. ((and (equal arg '(4))
  10583. (or (not org-use-fast-todo-selection)
  10584. (not org-todo-key-trigger)))
  10585. ;; Read a state with completion
  10586. (org-icompleting-read
  10587. "State: " (mapcar (lambda(x) (list x))
  10588. org-todo-keywords-1)
  10589. nil t))
  10590. ((eq arg 'right)
  10591. (if this
  10592. (if tail (car tail) nil)
  10593. (car org-todo-keywords-1)))
  10594. ((eq arg 'left)
  10595. (if (equal member org-todo-keywords-1)
  10596. nil
  10597. (if this
  10598. (nth (- (length org-todo-keywords-1)
  10599. (length tail) 2)
  10600. org-todo-keywords-1)
  10601. (org-last org-todo-keywords-1))))
  10602. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10603. (setq arg nil))) ; hack to fall back to cycling
  10604. (arg
  10605. ;; user or caller requests a specific state
  10606. (cond
  10607. ((equal arg "") nil)
  10608. ((eq arg 'none) nil)
  10609. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10610. ((eq arg 'nextset)
  10611. (or (car (cdr (member head org-todo-heads)))
  10612. (car org-todo-heads)))
  10613. ((eq arg 'previousset)
  10614. (let ((org-todo-heads (reverse org-todo-heads)))
  10615. (or (car (cdr (member head org-todo-heads)))
  10616. (car org-todo-heads))))
  10617. ((car (member arg org-todo-keywords-1)))
  10618. ((stringp arg)
  10619. (error "State `%s' not valid in this file" arg))
  10620. ((nth (1- (prefix-numeric-value arg))
  10621. org-todo-keywords-1))))
  10622. ((null member) (or head (car org-todo-keywords-1)))
  10623. ((equal this final-done-word) nil) ;; -> make empty
  10624. ((null tail) nil) ;; -> first entry
  10625. ((memq interpret '(type priority))
  10626. (if (eq this-command last-command)
  10627. (car tail)
  10628. (if (> (length tail) 0)
  10629. (or done-word (car org-done-keywords))
  10630. nil)))
  10631. (t
  10632. (car tail))))
  10633. (org-state (or
  10634. (run-hook-with-args-until-success
  10635. 'org-todo-get-default-hook org-state org-last-state)
  10636. org-state))
  10637. (next (if org-state (concat " " org-state " ") " "))
  10638. (change-plist (list :type 'todo-state-change :from this :to org-state
  10639. :position startpos))
  10640. dolog now-done-p)
  10641. (when org-blocker-hook
  10642. (setq org-last-todo-state-is-todo
  10643. (not (member this org-done-keywords)))
  10644. (unless (save-excursion
  10645. (save-match-data
  10646. (org-with-wide-buffer
  10647. (run-hook-with-args-until-failure
  10648. 'org-blocker-hook change-plist))))
  10649. (if (org-called-interactively-p 'interactive)
  10650. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10651. this org-state org-block-entry-blocking)
  10652. ;; fail silently
  10653. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10654. this org-state org-block-entry-blocking)
  10655. (throw 'exit nil))))
  10656. (store-match-data match-data)
  10657. (replace-match next t t)
  10658. (unless (pos-visible-in-window-p hl-pos)
  10659. (message "TODO state changed to %s" (org-trim next)))
  10660. (unless head
  10661. (setq head (org-get-todo-sequence-head org-state)
  10662. ass (assoc head org-todo-kwd-alist)
  10663. interpret (nth 1 ass)
  10664. done-word (nth 3 ass)
  10665. final-done-word (nth 4 ass)))
  10666. (when (memq arg '(nextset previousset))
  10667. (message "Keyword-Set %d/%d: %s"
  10668. (- (length org-todo-sets) -1
  10669. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10670. (length org-todo-sets)
  10671. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10672. (setq org-last-todo-state-is-todo
  10673. (not (member org-state org-done-keywords)))
  10674. (setq now-done-p (and (member org-state org-done-keywords)
  10675. (not (member this org-done-keywords))))
  10676. (and logging (org-local-logging logging))
  10677. (when (and (or org-todo-log-states org-log-done)
  10678. (not (eq org-inhibit-logging t))
  10679. (not (memq arg '(nextset previousset))))
  10680. ;; we need to look at recording a time and note
  10681. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10682. (nth 2 (assoc this org-todo-log-states))))
  10683. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10684. (setq dolog 'time))
  10685. (when (and org-state
  10686. (member org-state org-not-done-keywords)
  10687. (not (member this org-not-done-keywords)))
  10688. ;; This is now a todo state and was not one before
  10689. ;; If there was a CLOSED time stamp, get rid of it.
  10690. (org-add-planning-info nil nil 'closed))
  10691. (when (and now-done-p org-log-done)
  10692. ;; It is now done, and it was not done before
  10693. (org-add-planning-info 'closed (org-current-effective-time))
  10694. (if (and (not dolog) (eq 'note org-log-done))
  10695. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10696. (when (and org-state dolog)
  10697. ;; This is a non-nil state, and we need to log it
  10698. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10699. ;; Fixup tag positioning
  10700. (org-todo-trigger-tag-changes org-state)
  10701. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10702. (when org-provide-todo-statistics
  10703. (org-update-parent-todo-statistics))
  10704. (run-hooks 'org-after-todo-state-change-hook)
  10705. (if (and arg (not (member org-state org-done-keywords)))
  10706. (setq head (org-get-todo-sequence-head org-state)))
  10707. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10708. ;; Do we need to trigger a repeat?
  10709. (when now-done-p
  10710. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10711. ;; This is for the agenda, take a snapshot of the headline.
  10712. (save-match-data
  10713. (setq org-agenda-headline-snapshot-before-repeat
  10714. (org-get-heading))))
  10715. (org-auto-repeat-maybe org-state))
  10716. ;; Fixup cursor location if close to the keyword
  10717. (if (and (outline-on-heading-p)
  10718. (not (bolp))
  10719. (save-excursion (beginning-of-line 1)
  10720. (looking-at org-todo-line-regexp))
  10721. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10722. (progn
  10723. (goto-char (or (match-end 2) (match-end 1)))
  10724. (and (looking-at " ") (just-one-space))))
  10725. (when org-trigger-hook
  10726. (save-excursion
  10727. (run-hook-with-args 'org-trigger-hook change-plist)))
  10728. (when commentp (org-toggle-comment))))))))
  10729. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10730. "Block turning an entry into a TODO, using the hierarchy.
  10731. This checks whether the current task should be blocked from state
  10732. changes. Such blocking occurs when:
  10733. 1. The task has children which are not all in a completed state.
  10734. 2. A task has a parent with the property :ORDERED:, and there
  10735. are siblings prior to the current task with incomplete
  10736. status.
  10737. 3. The parent of the task is blocked because it has siblings that should
  10738. be done first, or is child of a block grandparent TODO entry."
  10739. (if (not org-enforce-todo-dependencies)
  10740. t ; if locally turned off don't block
  10741. (catch 'dont-block
  10742. ;; If this is not a todo state change, or if this entry is already DONE,
  10743. ;; do not block
  10744. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10745. (member (plist-get change-plist :from)
  10746. (cons 'done org-done-keywords))
  10747. (member (plist-get change-plist :to)
  10748. (cons 'todo org-not-done-keywords))
  10749. (not (plist-get change-plist :to)))
  10750. (throw 'dont-block t))
  10751. ;; If this task has children, and any are undone, it's blocked
  10752. (save-excursion
  10753. (org-back-to-heading t)
  10754. (let ((this-level (funcall outline-level)))
  10755. (outline-next-heading)
  10756. (let ((child-level (funcall outline-level)))
  10757. (while (and (not (eobp))
  10758. (> child-level this-level))
  10759. ;; this todo has children, check whether they are all
  10760. ;; completed
  10761. (if (and (not (org-entry-is-done-p))
  10762. (org-entry-is-todo-p))
  10763. (progn (setq org-block-entry-blocking (org-get-heading))
  10764. (throw 'dont-block nil)))
  10765. (outline-next-heading)
  10766. (setq child-level (funcall outline-level))))))
  10767. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10768. ;; any previous siblings are undone, it's blocked
  10769. (save-excursion
  10770. (org-back-to-heading t)
  10771. (let* ((pos (point))
  10772. (parent-pos (and (org-up-heading-safe) (point))))
  10773. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10774. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10775. (forward-line 1)
  10776. (re-search-forward org-not-done-heading-regexp pos t))
  10777. (setq org-block-entry-blocking (match-string 0))
  10778. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10779. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10780. (while t
  10781. (goto-char parent-pos)
  10782. (if (not (looking-at org-not-done-heading-regexp))
  10783. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10784. (setq pos (point))
  10785. (setq parent-pos (and (org-up-heading-safe) (point)))
  10786. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10787. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10788. (forward-line 1)
  10789. (re-search-forward org-not-done-heading-regexp pos t)
  10790. (setq org-block-entry-blocking (org-get-heading)))
  10791. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10792. (defcustom org-track-ordered-property-with-tag nil
  10793. "Should the ORDERED property also be shown as a tag?
  10794. The ORDERED property decides if an entry should require subtasks to be
  10795. completed in sequence. Since a property is not very visible, setting
  10796. this option means that toggling the ORDERED property with the command
  10797. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10798. not relevant for the behavior, but it makes things more visible.
  10799. Note that toggling the tag with tags commands will not change the property
  10800. and therefore not influence behavior!
  10801. This can be t, meaning the tag ORDERED should be used, It can also be a
  10802. string to select a different tag for this task."
  10803. :group 'org-todo
  10804. :type '(choice
  10805. (const :tag "No tracking" nil)
  10806. (const :tag "Track with ORDERED tag" t)
  10807. (string :tag "Use other tag")))
  10808. (defun org-toggle-ordered-property ()
  10809. "Toggle the ORDERED property of the current entry.
  10810. For better visibility, you can track the value of this property with a tag.
  10811. See variable `org-track-ordered-property-with-tag'."
  10812. (interactive)
  10813. (let* ((t1 org-track-ordered-property-with-tag)
  10814. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10815. (save-excursion
  10816. (org-back-to-heading)
  10817. (if (org-entry-get nil "ORDERED")
  10818. (progn
  10819. (org-delete-property "ORDERED")
  10820. (and tag (org-toggle-tag tag 'off))
  10821. (message "Subtasks can be completed in arbitrary order"))
  10822. (org-entry-put nil "ORDERED" "t")
  10823. (and tag (org-toggle-tag tag 'on))
  10824. (message "Subtasks must be completed in sequence")))))
  10825. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10826. (defun org-block-todo-from-checkboxes (change-plist)
  10827. "Block turning an entry into a TODO, using checkboxes.
  10828. This checks whether the current task should be blocked from state
  10829. changes because there are unchecked boxes in this entry."
  10830. (if (not org-enforce-todo-checkbox-dependencies)
  10831. t ; if locally turned off don't block
  10832. (catch 'dont-block
  10833. ;; If this is not a todo state change, or if this entry is already DONE,
  10834. ;; do not block
  10835. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10836. (member (plist-get change-plist :from)
  10837. (cons 'done org-done-keywords))
  10838. (member (plist-get change-plist :to)
  10839. (cons 'todo org-not-done-keywords))
  10840. (not (plist-get change-plist :to)))
  10841. (throw 'dont-block t))
  10842. ;; If this task has checkboxes that are not checked, it's blocked
  10843. (save-excursion
  10844. (org-back-to-heading t)
  10845. (let ((beg (point)) end)
  10846. (outline-next-heading)
  10847. (setq end (point))
  10848. (goto-char beg)
  10849. (if (org-list-search-forward
  10850. (concat (org-item-beginning-re)
  10851. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10852. "\\[[- ]\\]")
  10853. end t)
  10854. (progn
  10855. (if (boundp 'org-blocked-by-checkboxes)
  10856. (setq org-blocked-by-checkboxes t))
  10857. (throw 'dont-block nil)))))
  10858. t))) ; do not block
  10859. (defun org-entry-blocked-p ()
  10860. "Is the current entry blocked?"
  10861. (if (org-entry-get nil "NOBLOCKING")
  10862. nil ;; Never block this entry
  10863. (not
  10864. (run-hook-with-args-until-failure
  10865. 'org-blocker-hook
  10866. (list :type 'todo-state-change
  10867. :position (point)
  10868. :from 'todo
  10869. :to 'done)))))
  10870. (defun org-update-statistics-cookies (all)
  10871. "Update the statistics cookie, either from TODO or from checkboxes.
  10872. This should be called with the cursor in a line with a statistics cookie."
  10873. (interactive "P")
  10874. (if all
  10875. (progn
  10876. (org-update-checkbox-count 'all)
  10877. (org-map-entries 'org-update-parent-todo-statistics))
  10878. (if (not (org-at-heading-p))
  10879. (org-update-checkbox-count)
  10880. (let ((pos (point-marker))
  10881. end l1 l2)
  10882. (ignore-errors (org-back-to-heading t))
  10883. (if (not (org-at-heading-p))
  10884. (org-update-checkbox-count)
  10885. (setq l1 (org-outline-level))
  10886. (setq end (save-excursion
  10887. (outline-next-heading)
  10888. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10889. (point)))
  10890. (if (and (save-excursion
  10891. (re-search-forward
  10892. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10893. (not (save-excursion (re-search-forward
  10894. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10895. (org-update-checkbox-count)
  10896. (if (and l2 (> l2 l1))
  10897. (progn
  10898. (goto-char end)
  10899. (org-update-parent-todo-statistics))
  10900. (goto-char pos)
  10901. (beginning-of-line 1)
  10902. (while (re-search-forward
  10903. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10904. (point-at-eol) t)
  10905. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10906. (goto-char pos)
  10907. (move-marker pos nil)))))
  10908. (defvar org-entry-property-inherited-from) ;; defined below
  10909. (defun org-update-parent-todo-statistics ()
  10910. "Update any statistics cookie in the parent of the current headline.
  10911. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10912. the entire subtree and this will travel up the hierarchy and update
  10913. statistics everywhere."
  10914. (let* ((prop (save-excursion (org-up-heading-safe)
  10915. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10916. (recursive (or (not org-hierarchical-todo-statistics)
  10917. (and prop (string-match "\\<recursive\\>" prop))))
  10918. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10919. 0))
  10920. (first t)
  10921. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10922. level ltoggle l1 new ndel
  10923. (cnt-all 0) (cnt-done 0) is-percent kwd
  10924. checkbox-beg ov ovs ove cookie-present)
  10925. (catch 'exit
  10926. (save-excursion
  10927. (beginning-of-line 1)
  10928. (setq ltoggle (funcall outline-level))
  10929. ;; Three situations are to consider:
  10930. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10931. ;; to the top-level ancestor on the headline;
  10932. ;; 2. If parent has "recursive" property, repeat up to the
  10933. ;; headline setting that property, taking inheritance into
  10934. ;; account;
  10935. ;; 3. Else, move up to direct parent and proceed only once.
  10936. (while (and (setq level (org-up-heading-safe))
  10937. (or recursive first)
  10938. (>= (point) lim))
  10939. (setq first nil cookie-present nil)
  10940. (unless (and level
  10941. (not (string-match
  10942. "\\<checkbox\\>"
  10943. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10944. "")))))
  10945. (throw 'exit nil))
  10946. (while (re-search-forward box-re (point-at-eol) t)
  10947. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10948. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10949. (save-match-data
  10950. (unless (outline-next-heading) (throw 'exit nil))
  10951. (while (and (looking-at org-complex-heading-regexp)
  10952. (> (setq l1 (length (match-string 1))) level))
  10953. (setq kwd (and (or recursive (= l1 ltoggle))
  10954. (match-string 2)))
  10955. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10956. (and (listp org-provide-todo-statistics)
  10957. (or (member kwd org-provide-todo-statistics)
  10958. (member kwd org-done-keywords))))
  10959. (setq cnt-all (1+ cnt-all))
  10960. (if (eq org-provide-todo-statistics t)
  10961. (and kwd (setq cnt-all (1+ cnt-all)))))
  10962. (and (member kwd org-done-keywords)
  10963. (setq cnt-done (1+ cnt-done)))
  10964. (outline-next-heading)))
  10965. (setq new
  10966. (if is-percent
  10967. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10968. (format "[%d/%d]" cnt-done cnt-all))
  10969. ndel (- (match-end 0) checkbox-beg))
  10970. ;; handle overlays when updating cookie from column view
  10971. (when (setq ov (car (overlays-at checkbox-beg)))
  10972. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10973. (delete-overlay ov))
  10974. (goto-char checkbox-beg)
  10975. (insert new)
  10976. (delete-region (point) (+ (point) ndel))
  10977. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10978. (when ov (move-overlay ov ovs ove)))
  10979. (when cookie-present
  10980. (run-hook-with-args 'org-after-todo-statistics-hook
  10981. cnt-done (- cnt-all cnt-done))))))
  10982. (run-hooks 'org-todo-statistics-hook)))
  10983. (defvar org-after-todo-statistics-hook nil
  10984. "Hook that is called after a TODO statistics cookie has been updated.
  10985. Each function is called with two arguments: the number of not-done entries
  10986. and the number of done entries.
  10987. For example, the following function, when added to this hook, will switch
  10988. an entry to DONE when all children are done, and back to TODO when new
  10989. entries are set to a TODO status. Note that this hook is only called
  10990. when there is a statistics cookie in the headline!
  10991. (defun org-summary-todo (n-done n-not-done)
  10992. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10993. (let (org-log-done org-log-states) ; turn off logging
  10994. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10995. ")
  10996. (defvar org-todo-statistics-hook nil
  10997. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10998. This hook runs even if there is no statistics cookie present, in which case
  10999. `org-after-todo-statistics-hook' would not run.")
  11000. (defun org-todo-trigger-tag-changes (state)
  11001. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11002. (let ((l org-todo-state-tags-triggers)
  11003. changes)
  11004. (when (or (not state) (equal state ""))
  11005. (setq changes (append changes (cdr (assoc "" l)))))
  11006. (when (and (stringp state) (> (length state) 0))
  11007. (setq changes (append changes (cdr (assoc state l)))))
  11008. (when (member state org-not-done-keywords)
  11009. (setq changes (append changes (cdr (assoc 'todo l)))))
  11010. (when (member state org-done-keywords)
  11011. (setq changes (append changes (cdr (assoc 'done l)))))
  11012. (dolist (c changes)
  11013. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11014. (defun org-local-logging (value)
  11015. "Get logging settings from a property VALUE."
  11016. (let* (words w a)
  11017. ;; directly set the variables, they are already local.
  11018. (setq org-log-done nil
  11019. org-log-repeat nil
  11020. org-todo-log-states nil)
  11021. (setq words (org-split-string value))
  11022. (while (setq w (pop words))
  11023. (cond
  11024. ((setq a (assoc w org-startup-options))
  11025. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11026. (set (nth 1 a) (nth 2 a))))
  11027. ((setq a (org-extract-log-state-settings w))
  11028. (and (member (car a) org-todo-keywords-1)
  11029. (push a org-todo-log-states)))))))
  11030. (defun org-get-todo-sequence-head (kwd)
  11031. "Return the head of the TODO sequence to which KWD belongs.
  11032. If KWD is not set, check if there is a text property remembering the
  11033. right sequence."
  11034. (let (p)
  11035. (cond
  11036. ((not kwd)
  11037. (or (get-text-property (point-at-bol) 'org-todo-head)
  11038. (progn
  11039. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11040. nil (point-at-eol)))
  11041. (get-text-property p 'org-todo-head))))
  11042. ((not (member kwd org-todo-keywords-1))
  11043. (car org-todo-keywords-1))
  11044. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11045. (defun org-fast-todo-selection ()
  11046. "Fast TODO keyword selection with single keys.
  11047. Returns the new TODO keyword, or nil if no state change should occur."
  11048. (let* ((fulltable org-todo-key-alist)
  11049. (done-keywords org-done-keywords) ;; needed for the faces.
  11050. (maxlen (apply 'max (mapcar
  11051. (lambda (x)
  11052. (if (stringp (car x)) (string-width (car x)) 0))
  11053. fulltable)))
  11054. (expert nil)
  11055. (fwidth (+ maxlen 3 1 3))
  11056. (ncol (/ (- (window-width) 4) fwidth))
  11057. tg cnt e c tbl
  11058. groups ingroup)
  11059. (save-excursion
  11060. (save-window-excursion
  11061. (if expert
  11062. (set-buffer (get-buffer-create " *Org todo*"))
  11063. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11064. (erase-buffer)
  11065. (org-set-local 'org-done-keywords done-keywords)
  11066. (setq tbl fulltable cnt 0)
  11067. (while (setq e (pop tbl))
  11068. (cond
  11069. ((equal e '(:startgroup))
  11070. (push '() groups) (setq ingroup t)
  11071. (when (not (= cnt 0))
  11072. (setq cnt 0)
  11073. (insert "\n"))
  11074. (insert "{ "))
  11075. ((equal e '(:endgroup))
  11076. (setq ingroup nil cnt 0)
  11077. (insert "}\n"))
  11078. ((equal e '(:newline))
  11079. (when (not (= cnt 0))
  11080. (setq cnt 0)
  11081. (insert "\n")
  11082. (setq e (car tbl))
  11083. (while (equal (car tbl) '(:newline))
  11084. (insert "\n")
  11085. (setq tbl (cdr tbl)))))
  11086. (t
  11087. (setq tg (car e) c (cdr e))
  11088. (if ingroup (push tg (car groups)))
  11089. (setq tg (org-add-props tg nil 'face
  11090. (org-get-todo-face tg)))
  11091. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11092. (insert "[" c "] " tg (make-string
  11093. (- fwidth 4 (length tg)) ?\ ))
  11094. (when (= (setq cnt (1+ cnt)) ncol)
  11095. (insert "\n")
  11096. (if ingroup (insert " "))
  11097. (setq cnt 0)))))
  11098. (insert "\n")
  11099. (goto-char (point-min))
  11100. (if (not expert) (org-fit-window-to-buffer))
  11101. (message "[a-z..]:Set [SPC]:clear")
  11102. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11103. (cond
  11104. ((or (= c ?\C-g)
  11105. (and (= c ?q) (not (rassoc c fulltable))))
  11106. (setq quit-flag t))
  11107. ((= c ?\ ) nil)
  11108. ((setq e (rassoc c fulltable) tg (car e))
  11109. tg)
  11110. (t (setq quit-flag t)))))))
  11111. (defun org-entry-is-todo-p ()
  11112. (member (org-get-todo-state) org-not-done-keywords))
  11113. (defun org-entry-is-done-p ()
  11114. (member (org-get-todo-state) org-done-keywords))
  11115. (defun org-get-todo-state ()
  11116. (save-excursion
  11117. (org-back-to-heading t)
  11118. (and (looking-at org-todo-line-regexp)
  11119. (match-end 2)
  11120. (match-string 2))))
  11121. (defun org-at-date-range-p (&optional inactive-ok)
  11122. "Is the cursor inside a date range?"
  11123. (interactive)
  11124. (save-excursion
  11125. (catch 'exit
  11126. (let ((pos (point)))
  11127. (skip-chars-backward "^[<\r\n")
  11128. (skip-chars-backward "<[")
  11129. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11130. (>= (match-end 0) pos)
  11131. (throw 'exit t))
  11132. (skip-chars-backward "^<[\r\n")
  11133. (skip-chars-backward "<[")
  11134. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11135. (>= (match-end 0) pos)
  11136. (throw 'exit t)))
  11137. nil)))
  11138. (defun org-get-repeat (&optional tagline)
  11139. "Check if there is a deadline/schedule with repeater in this entry."
  11140. (save-match-data
  11141. (save-excursion
  11142. (org-back-to-heading t)
  11143. (and (re-search-forward (if tagline
  11144. (concat tagline "\\s-*" org-repeat-re)
  11145. org-repeat-re)
  11146. (org-entry-end-position) t)
  11147. (match-string-no-properties 1)))))
  11148. (defvar org-last-changed-timestamp)
  11149. (defvar org-last-inserted-timestamp)
  11150. (defvar org-log-post-message)
  11151. (defvar org-log-note-purpose)
  11152. (defvar org-log-note-how)
  11153. (defvar org-log-note-extra)
  11154. (defun org-auto-repeat-maybe (done-word)
  11155. "Check if the current headline contains a repeated deadline/schedule.
  11156. If yes, set TODO state back to what it was and change the base date
  11157. of repeating deadline/scheduled time stamps to new date.
  11158. This function is run automatically after each state change to a DONE state."
  11159. ;; last-state is dynamically scoped into this function
  11160. (let* ((repeat (org-get-repeat))
  11161. (aa (assoc org-last-state org-todo-kwd-alist))
  11162. (interpret (nth 1 aa))
  11163. (head (nth 2 aa))
  11164. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11165. (msg "Entry repeats: ")
  11166. (org-log-done nil)
  11167. (org-todo-log-states nil)
  11168. re type n what ts time to-state)
  11169. (when repeat
  11170. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11171. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11172. org-todo-repeat-to-state))
  11173. (unless (and to-state (member to-state org-todo-keywords-1))
  11174. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11175. (org-todo to-state)
  11176. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11177. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11178. (org-time-stamp-format t t))))
  11179. (when org-log-repeat
  11180. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11181. (memq 'org-add-log-note post-command-hook))
  11182. ;; OK, we are already setup for some record
  11183. (if (eq org-log-repeat 'note)
  11184. ;; make sure we take a note, not only a time stamp
  11185. (setq org-log-note-how 'note))
  11186. ;; Set up for taking a record
  11187. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11188. org-last-state
  11189. 'findpos org-log-repeat)))
  11190. (org-back-to-heading t)
  11191. (org-add-planning-info nil nil 'closed)
  11192. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11193. org-deadline-time-regexp "\\)\\|\\("
  11194. org-ts-regexp "\\)"))
  11195. (while (re-search-forward
  11196. re (save-excursion (outline-next-heading) (point)) t)
  11197. (setq type (if (match-end 1) org-scheduled-string
  11198. (if (match-end 3) org-deadline-string "Plain:"))
  11199. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11200. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11201. (setq n (string-to-number (match-string 2 ts))
  11202. what (match-string 3 ts))
  11203. (if (equal what "w") (setq n (* n 7) what "d"))
  11204. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11205. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11206. ;; Preparation, see if we need to modify the start date for the change
  11207. (when (match-end 1)
  11208. (setq time (save-match-data (org-time-string-to-time ts)))
  11209. (cond
  11210. ((equal (match-string 1 ts) ".")
  11211. ;; Shift starting date to today
  11212. (org-timestamp-change
  11213. (- (org-today) (time-to-days time))
  11214. 'day))
  11215. ((equal (match-string 1 ts) "+")
  11216. (let ((nshiftmax 10) (nshift 0))
  11217. (while (or (= nshift 0)
  11218. (<= (time-to-days time)
  11219. (time-to-days (current-time))))
  11220. (when (= (incf nshift) nshiftmax)
  11221. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11222. (error "Abort")))
  11223. (org-timestamp-change n (cdr (assoc what whata)))
  11224. (org-at-timestamp-p t)
  11225. (setq ts (match-string 1))
  11226. (setq time (save-match-data (org-time-string-to-time ts)))))
  11227. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11228. ;; rematch, so that we have everything in place for the real shift
  11229. (org-at-timestamp-p t)
  11230. (setq ts (match-string 1))
  11231. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11232. (org-timestamp-change n (cdr (assoc what whata)))
  11233. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11234. (setq org-log-post-message msg)
  11235. (message "%s" msg))))
  11236. (defun org-show-todo-tree (arg)
  11237. "Make a compact tree which shows all headlines marked with TODO.
  11238. The tree will show the lines where the regexp matches, and all higher
  11239. headlines above the match.
  11240. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11241. With a numeric prefix N, construct a sparse tree for the Nth element
  11242. of `org-todo-keywords-1'."
  11243. (interactive "P")
  11244. (let ((case-fold-search nil)
  11245. (kwd-re
  11246. (cond ((null arg) org-not-done-regexp)
  11247. ((equal arg '(4))
  11248. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11249. (mapcar 'list org-todo-keywords-1))))
  11250. (concat "\\("
  11251. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11252. "\\)\\>")))
  11253. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11254. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11255. org-todo-keywords-1)))
  11256. (t (error "Invalid prefix argument: %s" arg)))))
  11257. (message "%d TODO entries found"
  11258. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11259. (defun org-deadline (&optional remove time)
  11260. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11261. With argument REMOVE, remove any deadline from the item.
  11262. With argument TIME, set the deadline at the corresponding date. TIME
  11263. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11264. (interactive "P")
  11265. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11266. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11267. 'region-start-level 'region))
  11268. org-loop-over-headlines-in-active-region)
  11269. (org-map-entries
  11270. `(org-deadline ',remove ,time)
  11271. org-loop-over-headlines-in-active-region
  11272. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11273. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11274. (repeater (and old-date
  11275. (string-match
  11276. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11277. old-date)
  11278. (match-string 1 old-date))))
  11279. (if remove
  11280. (progn
  11281. (when (and old-date org-log-redeadline)
  11282. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11283. org-log-redeadline))
  11284. (org-remove-timestamp-with-keyword org-deadline-string)
  11285. (message "Item no longer has a deadline."))
  11286. (org-add-planning-info 'deadline time 'closed)
  11287. (when (and old-date org-log-redeadline
  11288. (not (equal old-date
  11289. (substring org-last-inserted-timestamp 1 -1))))
  11290. (org-add-log-setup 'redeadline nil old-date 'findpos
  11291. org-log-redeadline))
  11292. (when repeater
  11293. (save-excursion
  11294. (org-back-to-heading t)
  11295. (when (re-search-forward (concat org-deadline-string " "
  11296. org-last-inserted-timestamp)
  11297. (save-excursion
  11298. (outline-next-heading) (point)) t)
  11299. (goto-char (1- (match-end 0)))
  11300. (insert " " repeater)
  11301. (setq org-last-inserted-timestamp
  11302. (concat (substring org-last-inserted-timestamp 0 -1)
  11303. " " repeater
  11304. (substring org-last-inserted-timestamp -1))))))
  11305. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11306. (defun org-schedule (&optional remove time)
  11307. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11308. With argument REMOVE, remove any scheduling date from the item.
  11309. With argument TIME, scheduled at the corresponding date. TIME can
  11310. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11311. (interactive "P")
  11312. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11313. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11314. 'region-start-level 'region))
  11315. org-loop-over-headlines-in-active-region)
  11316. (org-map-entries
  11317. `(org-schedule ',remove ,time)
  11318. org-loop-over-headlines-in-active-region
  11319. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11320. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11321. (repeater (and old-date
  11322. (string-match
  11323. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11324. old-date)
  11325. (match-string 1 old-date))))
  11326. (if remove
  11327. (progn
  11328. (when (and old-date org-log-reschedule)
  11329. (org-add-log-setup 'delschedule nil old-date 'findpos
  11330. org-log-reschedule))
  11331. (org-remove-timestamp-with-keyword org-scheduled-string)
  11332. (message "Item is no longer scheduled."))
  11333. (org-add-planning-info 'scheduled time 'closed)
  11334. (when (and old-date org-log-reschedule
  11335. (not (equal old-date
  11336. (substring org-last-inserted-timestamp 1 -1))))
  11337. (org-add-log-setup 'reschedule nil old-date 'findpos
  11338. org-log-reschedule))
  11339. (when repeater
  11340. (save-excursion
  11341. (org-back-to-heading t)
  11342. (when (re-search-forward (concat org-scheduled-string " "
  11343. org-last-inserted-timestamp)
  11344. (save-excursion
  11345. (outline-next-heading) (point)) t)
  11346. (goto-char (1- (match-end 0)))
  11347. (insert " " repeater)
  11348. (setq org-last-inserted-timestamp
  11349. (concat (substring org-last-inserted-timestamp 0 -1)
  11350. " " repeater
  11351. (substring org-last-inserted-timestamp -1))))))
  11352. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11353. (defun org-get-scheduled-time (pom &optional inherit)
  11354. "Get the scheduled time as a time tuple, of a format suitable
  11355. for calling org-schedule with, or if there is no scheduling,
  11356. returns nil."
  11357. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11358. (when time
  11359. (apply 'encode-time (org-parse-time-string time)))))
  11360. (defun org-get-deadline-time (pom &optional inherit)
  11361. "Get the deadline as a time tuple, of a format suitable for
  11362. calling org-deadline with, or if there is no scheduling, returns
  11363. nil."
  11364. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11365. (when time
  11366. (apply 'encode-time (org-parse-time-string time)))))
  11367. (defun org-remove-timestamp-with-keyword (keyword)
  11368. "Remove all time stamps with KEYWORD in the current entry."
  11369. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11370. beg)
  11371. (save-excursion
  11372. (org-back-to-heading t)
  11373. (setq beg (point))
  11374. (outline-next-heading)
  11375. (while (re-search-backward re beg t)
  11376. (replace-match "")
  11377. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11378. (equal (char-before) ?\ ))
  11379. (backward-delete-char 1)
  11380. (if (string-match "^[ \t]*$" (buffer-substring
  11381. (point-at-bol) (point-at-eol)))
  11382. (delete-region (point-at-bol)
  11383. (min (point-max) (1+ (point-at-eol))))))))))
  11384. (defun org-add-planning-info (what &optional time &rest remove)
  11385. "Insert new timestamp with keyword in the line directly after the headline.
  11386. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11387. If non is given, the user is prompted for a date.
  11388. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11389. be removed."
  11390. (interactive)
  11391. (let (org-time-was-given org-end-time-was-given ts
  11392. end default-time default-input)
  11393. (catch 'exit
  11394. (when (and (memq what '(scheduled deadline))
  11395. (or (not time)
  11396. (and (stringp time)
  11397. (string-match "^[-+]+[0-9]" time))))
  11398. ;; Try to get a default date/time from existing timestamp
  11399. (save-excursion
  11400. (org-back-to-heading t)
  11401. (setq end (save-excursion (outline-next-heading) (point)))
  11402. (when (re-search-forward (if (eq what 'scheduled)
  11403. org-scheduled-time-regexp
  11404. org-deadline-time-regexp)
  11405. end t)
  11406. (setq ts (match-string 1)
  11407. default-time
  11408. (apply 'encode-time (org-parse-time-string ts))
  11409. default-input (and ts (org-get-compact-tod ts))))))
  11410. (when what
  11411. (setq time
  11412. (if (stringp time)
  11413. ;; This is a string (relative or absolute), set proper date
  11414. (apply 'encode-time
  11415. (org-read-date-analyze
  11416. time default-time (decode-time default-time)))
  11417. ;; If necessary, get the time from the user
  11418. (or time (org-read-date nil 'to-time nil nil
  11419. default-time default-input)))))
  11420. (when (and org-insert-labeled-timestamps-at-point
  11421. (member what '(scheduled deadline)))
  11422. (insert
  11423. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11424. (org-insert-time-stamp time org-time-was-given
  11425. nil nil nil (list org-end-time-was-given))
  11426. (setq what nil))
  11427. (save-excursion
  11428. (save-restriction
  11429. (let (col list elt ts buffer-invisibility-spec)
  11430. (org-back-to-heading t)
  11431. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11432. (goto-char (match-end 1))
  11433. (setq col (current-column))
  11434. (goto-char (match-end 0))
  11435. (if (eobp) (insert "\n") (forward-char 1))
  11436. (when (and (not what)
  11437. (not (looking-at
  11438. (concat "[ \t]*"
  11439. org-keyword-time-not-clock-regexp))))
  11440. ;; Nothing to add, nothing to remove...... :-)
  11441. (throw 'exit nil))
  11442. (if (and (not (looking-at org-outline-regexp))
  11443. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11444. "[^\r\n]*"))
  11445. (not (equal (match-string 1) org-clock-string)))
  11446. (narrow-to-region (match-beginning 0) (match-end 0))
  11447. (insert-before-markers "\n")
  11448. (backward-char 1)
  11449. (narrow-to-region (point) (point))
  11450. (and org-adapt-indentation (org-indent-to-column col)))
  11451. ;; Check if we have to remove something.
  11452. (setq list (cons what remove))
  11453. (while list
  11454. (setq elt (pop list))
  11455. (when (or (and (eq elt 'scheduled)
  11456. (re-search-forward org-scheduled-time-regexp nil t))
  11457. (and (eq elt 'deadline)
  11458. (re-search-forward org-deadline-time-regexp nil t))
  11459. (and (eq elt 'closed)
  11460. (re-search-forward org-closed-time-regexp nil t)))
  11461. (replace-match "")
  11462. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11463. (and (looking-at "[ \t]+") (replace-match ""))
  11464. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11465. (when what
  11466. (insert
  11467. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11468. (cond ((eq what 'scheduled) org-scheduled-string)
  11469. ((eq what 'deadline) org-deadline-string)
  11470. ((eq what 'closed) org-closed-string))
  11471. " ")
  11472. (setq ts (org-insert-time-stamp
  11473. time
  11474. (or org-time-was-given
  11475. (and (eq what 'closed) org-log-done-with-time))
  11476. (eq what 'closed)
  11477. nil nil (list org-end-time-was-given)))
  11478. (insert
  11479. (if (not (or (bolp) (eq (char-before) ?\ )
  11480. (memq (char-after) '(32 10))
  11481. (eobp))) " " ""))
  11482. (end-of-line 1))
  11483. (goto-char (point-min))
  11484. (widen)
  11485. (if (and (looking-at "[ \t]*\n")
  11486. (equal (char-before) ?\n))
  11487. (delete-region (1- (point)) (point-at-eol)))
  11488. ts))))))
  11489. (defvar org-log-note-marker (make-marker))
  11490. (defvar org-log-note-purpose nil)
  11491. (defvar org-log-note-state nil)
  11492. (defvar org-log-note-previous-state nil)
  11493. (defvar org-log-note-how nil)
  11494. (defvar org-log-note-extra nil)
  11495. (defvar org-log-note-window-configuration nil)
  11496. (defvar org-log-note-return-to (make-marker))
  11497. (defvar org-log-note-effective-time nil
  11498. "Remembered current time so that dynamically scoped
  11499. `org-extend-today-until' affects tha timestamps in state change
  11500. log")
  11501. (defvar org-log-post-message nil
  11502. "Message to be displayed after a log note has been stored.
  11503. The auto-repeater uses this.")
  11504. (defun org-add-note ()
  11505. "Add a note to the current entry.
  11506. This is done in the same way as adding a state change note."
  11507. (interactive)
  11508. (org-add-log-setup 'note nil nil 'findpos nil))
  11509. (defvar org-property-end-re)
  11510. (defun org-add-log-setup (&optional purpose state prev-state
  11511. findpos how extra)
  11512. "Set up the post command hook to take a note.
  11513. If this is about to TODO state change, the new state is expected in STATE.
  11514. When FINDPOS is non-nil, find the correct position for the note in
  11515. the current entry. If not, assume that it can be inserted at point.
  11516. HOW is an indicator what kind of note should be created.
  11517. EXTRA is additional text that will be inserted into the notes buffer."
  11518. (let* ((org-log-into-drawer (org-log-into-drawer))
  11519. (drawer (cond ((stringp org-log-into-drawer)
  11520. org-log-into-drawer)
  11521. (org-log-into-drawer "LOGBOOK"))))
  11522. (save-restriction
  11523. (save-excursion
  11524. (when findpos
  11525. (org-back-to-heading t)
  11526. (narrow-to-region (point) (save-excursion
  11527. (outline-next-heading) (point)))
  11528. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11529. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11530. "[^\r\n]*\\)?"))
  11531. (goto-char (match-end 0))
  11532. (cond
  11533. (drawer
  11534. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11535. nil t)
  11536. (progn
  11537. (goto-char (match-end 0))
  11538. (or org-log-states-order-reversed
  11539. (and (re-search-forward org-property-end-re nil t)
  11540. (goto-char (1- (match-beginning 0))))))
  11541. (insert "\n:" drawer ":\n:END:")
  11542. (beginning-of-line 0)
  11543. (org-indent-line)
  11544. (beginning-of-line 2)
  11545. (org-indent-line)
  11546. (end-of-line 0)))
  11547. ((and org-log-state-notes-insert-after-drawers
  11548. (save-excursion
  11549. (forward-line) (looking-at org-drawer-regexp)))
  11550. (forward-line)
  11551. (while (looking-at org-drawer-regexp)
  11552. (goto-char (match-end 0))
  11553. (re-search-forward org-property-end-re (point-max) t)
  11554. (forward-line))
  11555. (forward-line -1)))
  11556. (unless org-log-states-order-reversed
  11557. (and (= (char-after) ?\n) (forward-char 1))
  11558. (org-skip-over-state-notes)
  11559. (skip-chars-backward " \t\n\r")))
  11560. (move-marker org-log-note-marker (point))
  11561. (setq org-log-note-purpose purpose
  11562. org-log-note-state state
  11563. org-log-note-previous-state prev-state
  11564. org-log-note-how how
  11565. org-log-note-extra extra
  11566. org-log-note-effective-time (org-current-effective-time))
  11567. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11568. (defun org-skip-over-state-notes ()
  11569. "Skip past the list of State notes in an entry."
  11570. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11571. (when (ignore-errors (goto-char (org-in-item-p)))
  11572. (let* ((struct (org-list-struct))
  11573. (prevs (org-list-prevs-alist struct)))
  11574. (while (looking-at "[ \t]*- State")
  11575. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11576. (org-list-get-item-end (point) struct)))))))
  11577. (defun org-add-log-note (&optional purpose)
  11578. "Pop up a window for taking a note, and add this note later at point."
  11579. (remove-hook 'post-command-hook 'org-add-log-note)
  11580. (setq org-log-note-window-configuration (current-window-configuration))
  11581. (delete-other-windows)
  11582. (move-marker org-log-note-return-to (point))
  11583. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11584. (goto-char org-log-note-marker)
  11585. (org-switch-to-buffer-other-window "*Org Note*")
  11586. (erase-buffer)
  11587. (if (memq org-log-note-how '(time state))
  11588. (let (current-prefix-arg) (org-store-log-note))
  11589. (let ((org-inhibit-startup t)) (org-mode))
  11590. (insert (format "# Insert note for %s.
  11591. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11592. (cond
  11593. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11594. ((eq org-log-note-purpose 'done) "closed todo item")
  11595. ((eq org-log-note-purpose 'state)
  11596. (format "state change from \"%s\" to \"%s\""
  11597. (or org-log-note-previous-state "")
  11598. (or org-log-note-state "")))
  11599. ((eq org-log-note-purpose 'reschedule)
  11600. "rescheduling")
  11601. ((eq org-log-note-purpose 'delschedule)
  11602. "no longer scheduled")
  11603. ((eq org-log-note-purpose 'redeadline)
  11604. "changing deadline")
  11605. ((eq org-log-note-purpose 'deldeadline)
  11606. "removing deadline")
  11607. ((eq org-log-note-purpose 'refile)
  11608. "refiling")
  11609. ((eq org-log-note-purpose 'note)
  11610. "this entry")
  11611. (t (error "This should not happen")))))
  11612. (if org-log-note-extra (insert org-log-note-extra))
  11613. (org-set-local 'org-finish-function 'org-store-log-note)
  11614. (run-hooks 'org-log-buffer-setup-hook)))
  11615. (defvar org-note-abort nil) ; dynamically scoped
  11616. (defun org-store-log-note ()
  11617. "Finish taking a log note, and insert it to where it belongs."
  11618. (let ((txt (buffer-string)))
  11619. (kill-buffer (current-buffer))
  11620. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11621. lines ind bul)
  11622. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11623. (setq txt (replace-match "" t t txt)))
  11624. (if (string-match "\\s-+\\'" txt)
  11625. (setq txt (replace-match "" t t txt)))
  11626. (setq lines (org-split-string txt "\n"))
  11627. (when (and note (string-match "\\S-" note))
  11628. (setq note
  11629. (org-replace-escapes
  11630. note
  11631. (list (cons "%u" (user-login-name))
  11632. (cons "%U" user-full-name)
  11633. (cons "%t" (format-time-string
  11634. (org-time-stamp-format 'long 'inactive)
  11635. org-log-note-effective-time))
  11636. (cons "%T" (format-time-string
  11637. (org-time-stamp-format 'long nil)
  11638. org-log-note-effective-time))
  11639. (cons "%d" (format-time-string
  11640. (org-time-stamp-format nil 'inactive)
  11641. org-log-note-effective-time))
  11642. (cons "%D" (format-time-string
  11643. (org-time-stamp-format nil nil)
  11644. org-log-note-effective-time))
  11645. (cons "%s" (if org-log-note-state
  11646. (concat "\"" org-log-note-state "\"")
  11647. ""))
  11648. (cons "%S" (if org-log-note-previous-state
  11649. (concat "\"" org-log-note-previous-state "\"")
  11650. "\"\"")))))
  11651. (if lines (setq note (concat note " \\\\")))
  11652. (push note lines))
  11653. (when (or current-prefix-arg org-note-abort)
  11654. (when org-log-into-drawer
  11655. (org-remove-empty-drawer-at
  11656. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11657. org-log-note-marker))
  11658. (setq lines nil))
  11659. (when lines
  11660. (with-current-buffer (marker-buffer org-log-note-marker)
  11661. (save-excursion
  11662. (goto-char org-log-note-marker)
  11663. (move-marker org-log-note-marker nil)
  11664. (end-of-line 1)
  11665. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11666. (setq ind (save-excursion
  11667. (if (ignore-errors (goto-char (org-in-item-p)))
  11668. (let ((struct (org-list-struct)))
  11669. (org-list-get-ind
  11670. (org-list-get-top-point struct) struct))
  11671. (skip-chars-backward " \r\t\n")
  11672. (cond
  11673. ((and (org-at-heading-p)
  11674. org-adapt-indentation)
  11675. (1+ (org-current-level)))
  11676. ((org-at-heading-p) 0)
  11677. (t (org-get-indentation))))))
  11678. (setq bul (org-list-bullet-string "-"))
  11679. (org-indent-line-to ind)
  11680. (insert bul (pop lines))
  11681. (let ((ind-body (+ (length bul) ind)))
  11682. (while lines
  11683. (insert "\n")
  11684. (org-indent-line-to ind-body)
  11685. (insert (pop lines))))
  11686. (message "Note stored")
  11687. (org-back-to-heading t)
  11688. (org-cycle-hide-drawers 'children))))))
  11689. (set-window-configuration org-log-note-window-configuration)
  11690. (with-current-buffer (marker-buffer org-log-note-return-to)
  11691. (goto-char org-log-note-return-to))
  11692. (move-marker org-log-note-return-to nil)
  11693. (and org-log-post-message (message "%s" org-log-post-message)))
  11694. (defun org-remove-empty-drawer-at (drawer pos)
  11695. "Remove an empty drawer DRAWER at position POS.
  11696. POS may also be a marker."
  11697. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11698. (save-excursion
  11699. (save-restriction
  11700. (widen)
  11701. (goto-char pos)
  11702. (if (org-in-regexp
  11703. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11704. (replace-match ""))))))
  11705. (defvar org-ts-type nil)
  11706. (defun org-sparse-tree (&optional arg type)
  11707. "Create a sparse tree, prompt for the details.
  11708. This command can create sparse trees. You first need to select the type
  11709. of match used to create the tree:
  11710. t Show all TODO entries.
  11711. T Show entries with a specific TODO keyword.
  11712. m Show entries selected by a tags/property match.
  11713. p Enter a property name and its value (both with completion on existing
  11714. names/values) and show entries with that property.
  11715. r Show entries matching a regular expression (`/' can be used as well).
  11716. b Show deadlines and scheduled items before a date.
  11717. a Show deadlines and scheduled items after a date.
  11718. d Show deadlines due within `org-deadline-warning-days'.
  11719. D Show deadlines and scheduled items between a date range."
  11720. (interactive "P")
  11721. (let (ans kwd value ts-type)
  11722. (setq type (or type org-sparse-tree-default-date-type))
  11723. (setq org-ts-type type)
  11724. (message "Sparse tree: [r]egexp [/]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"
  11725. (cond ((eq type 'all) "all timestamps")
  11726. ((eq type 'scheduled) "only scheduled")
  11727. ((eq type 'deadline) "only deadline")
  11728. ((eq type 'active) "only active timestamps")
  11729. ((eq type 'inactive) "only inactive timestamps")
  11730. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11731. (t "scheduled/deadline")))
  11732. (setq ans (read-char-exclusive))
  11733. (cond
  11734. ((equal ans ?c)
  11735. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11736. ((equal ans ?d)
  11737. (call-interactively 'org-check-deadlines))
  11738. ((equal ans ?b)
  11739. (call-interactively 'org-check-before-date))
  11740. ((equal ans ?a)
  11741. (call-interactively 'org-check-after-date))
  11742. ((equal ans ?D)
  11743. (call-interactively 'org-check-dates-range))
  11744. ((equal ans ?t)
  11745. (call-interactively 'org-show-todo-tree))
  11746. ((equal ans ?T)
  11747. (org-show-todo-tree '(4)))
  11748. ((member ans '(?T ?m))
  11749. (call-interactively 'org-match-sparse-tree))
  11750. ((member ans '(?p ?P))
  11751. (setq kwd (org-icompleting-read "Property: "
  11752. (mapcar 'list (org-buffer-property-keys))))
  11753. (setq value (org-icompleting-read "Value: "
  11754. (mapcar 'list (org-property-values kwd))))
  11755. (unless (string-match "\\`{.*}\\'" value)
  11756. (setq value (concat "\"" value "\"")))
  11757. (org-match-sparse-tree arg (concat kwd "=" value)))
  11758. ((member ans '(?r ?R ?/))
  11759. (call-interactively 'org-occur))
  11760. (t (error "No such sparse tree command \"%c\"" ans)))))
  11761. (defvar org-occur-highlights nil
  11762. "List of overlays used for occur matches.")
  11763. (make-variable-buffer-local 'org-occur-highlights)
  11764. (defvar org-occur-parameters nil
  11765. "Parameters of the active org-occur calls.
  11766. This is a list, each call to org-occur pushes as cons cell,
  11767. containing the regular expression and the callback, onto the list.
  11768. The list can contain several entries if `org-occur' has been called
  11769. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11770. will only contain one set of parameters. When the highlights are
  11771. removed (for example with `C-c C-c', or with the next edit (depending
  11772. on `org-remove-highlights-with-change'), this variable is emptied
  11773. as well.")
  11774. (make-variable-buffer-local 'org-occur-parameters)
  11775. (defun org-occur (regexp &optional keep-previous callback)
  11776. "Make a compact tree which shows all matches of REGEXP.
  11777. The tree will show the lines where the regexp matches, and all higher
  11778. headlines above the match. It will also show the heading after the match,
  11779. to make sure editing the matching entry is easy.
  11780. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11781. call to `org-occur' will be kept, to allow stacking of calls to this
  11782. command.
  11783. If CALLBACK is non-nil, it is a function which is called to confirm
  11784. that the match should indeed be shown."
  11785. (interactive "sRegexp: \nP")
  11786. (when (equal regexp "")
  11787. (error "Regexp cannot be empty"))
  11788. (unless keep-previous
  11789. (org-remove-occur-highlights nil nil t))
  11790. (push (cons regexp callback) org-occur-parameters)
  11791. (let ((cnt 0))
  11792. (save-excursion
  11793. (goto-char (point-min))
  11794. (if (or (not keep-previous) ; do not want to keep
  11795. (not org-occur-highlights)) ; no previous matches
  11796. ;; hide everything
  11797. (org-overview))
  11798. (while (re-search-forward regexp nil t)
  11799. (when (or (not callback)
  11800. (save-match-data (funcall callback)))
  11801. (setq cnt (1+ cnt))
  11802. (when org-highlight-sparse-tree-matches
  11803. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11804. (org-show-context 'occur-tree))))
  11805. (when org-remove-highlights-with-change
  11806. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11807. nil 'local))
  11808. (unless org-sparse-tree-open-archived-trees
  11809. (org-hide-archived-subtrees (point-min) (point-max)))
  11810. (run-hooks 'org-occur-hook)
  11811. (if (org-called-interactively-p 'interactive)
  11812. (message "%d match(es) for regexp %s" cnt regexp))
  11813. cnt))
  11814. (defun org-occur-next-match (&optional n reset)
  11815. "Function for `next-error-function' to find sparse tree matches.
  11816. N is the number of matches to move, when negative move backwards.
  11817. RESET is entirely ignored - this function always goes back to the
  11818. starting point when no match is found."
  11819. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11820. (search-func (if (< n 0)
  11821. 'previous-single-char-property-change
  11822. 'next-single-char-property-change))
  11823. (n (abs n))
  11824. (pos (point))
  11825. p1)
  11826. (catch 'exit
  11827. (while (setq p1 (funcall search-func (point) 'org-type))
  11828. (when (equal p1 limit)
  11829. (goto-char pos)
  11830. (error "No more matches"))
  11831. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11832. (setq n (1- n))
  11833. (when (= n 0)
  11834. (goto-char p1)
  11835. (throw 'exit (point))))
  11836. (goto-char p1))
  11837. (goto-char p1)
  11838. (error "No more matches"))))
  11839. (defun org-show-context (&optional key)
  11840. "Make sure point and context are visible.
  11841. How much context is shown depends upon the variables
  11842. `org-show-hierarchy-above', `org-show-following-heading',
  11843. `org-show-entry-below' and `org-show-siblings'."
  11844. (let ((heading-p (org-at-heading-p t))
  11845. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11846. (following-p (org-get-alist-option org-show-following-heading key))
  11847. (entry-p (org-get-alist-option org-show-entry-below key))
  11848. (siblings-p (org-get-alist-option org-show-siblings key)))
  11849. (catch 'exit
  11850. ;; Show heading or entry text
  11851. (if (and heading-p (not entry-p))
  11852. (org-flag-heading nil) ; only show the heading
  11853. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11854. (org-show-hidden-entry))) ; show entire entry
  11855. (when following-p
  11856. ;; Show next sibling, or heading below text
  11857. (save-excursion
  11858. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11859. (org-flag-heading nil))))
  11860. (when siblings-p (org-show-siblings))
  11861. (when hierarchy-p
  11862. ;; show all higher headings, possibly with siblings
  11863. (save-excursion
  11864. (while (and (condition-case nil
  11865. (progn (org-up-heading-all 1) t)
  11866. (error nil))
  11867. (not (bobp)))
  11868. (org-flag-heading nil)
  11869. (when siblings-p (org-show-siblings))))))))
  11870. (defvar org-reveal-start-hook nil
  11871. "Hook run before revealing a location.")
  11872. (defun org-reveal (&optional siblings)
  11873. "Show current entry, hierarchy above it, and the following headline.
  11874. This can be used to show a consistent set of context around locations
  11875. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11876. not t for the search context.
  11877. With optional argument SIBLINGS, on each level of the hierarchy all
  11878. siblings are shown. This repairs the tree structure to what it would
  11879. look like when opened with hierarchical calls to `org-cycle'.
  11880. With double optional argument \\[universal-argument] \\[universal-argument], \
  11881. go to the parent and show the
  11882. entire tree."
  11883. (interactive "P")
  11884. (run-hooks 'org-reveal-start-hook)
  11885. (let ((org-show-hierarchy-above t)
  11886. (org-show-following-heading t)
  11887. (org-show-siblings (if siblings t org-show-siblings)))
  11888. (org-show-context nil))
  11889. (when (equal siblings '(16))
  11890. (save-excursion
  11891. (when (org-up-heading-safe)
  11892. (org-show-subtree)
  11893. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11894. (defun org-highlight-new-match (beg end)
  11895. "Highlight from BEG to END and mark the highlight is an occur headline."
  11896. (let ((ov (make-overlay beg end)))
  11897. (overlay-put ov 'face 'secondary-selection)
  11898. (overlay-put ov 'org-type 'org-occur)
  11899. (push ov org-occur-highlights)))
  11900. (defun org-remove-occur-highlights (&optional beg end noremove)
  11901. "Remove the occur highlights from the buffer.
  11902. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11903. from the `before-change-functions' in the current buffer."
  11904. (interactive)
  11905. (unless org-inhibit-highlight-removal
  11906. (mapc 'delete-overlay org-occur-highlights)
  11907. (setq org-occur-highlights nil)
  11908. (setq org-occur-parameters nil)
  11909. (unless noremove
  11910. (remove-hook 'before-change-functions
  11911. 'org-remove-occur-highlights 'local))))
  11912. ;;;; Priorities
  11913. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11914. "Regular expression matching the priority indicator.")
  11915. (defvar org-remove-priority-next-time nil)
  11916. (defun org-priority-up ()
  11917. "Increase the priority of the current item."
  11918. (interactive)
  11919. (org-priority 'up))
  11920. (defun org-priority-down ()
  11921. "Decrease the priority of the current item."
  11922. (interactive)
  11923. (org-priority 'down))
  11924. (defun org-priority (&optional action show)
  11925. "Change the priority of an item.
  11926. ACTION can be `set', `up', `down', or a character."
  11927. (interactive "P")
  11928. (if (equal action '(4))
  11929. (org-show-priority)
  11930. (unless org-enable-priority-commands
  11931. (error "Priority commands are disabled"))
  11932. (setq action (or action 'set))
  11933. (let (current new news have remove)
  11934. (save-excursion
  11935. (org-back-to-heading t)
  11936. (if (looking-at org-priority-regexp)
  11937. (setq current (string-to-char (match-string 2))
  11938. have t))
  11939. (cond
  11940. ((eq action 'remove)
  11941. (setq remove t new ?\ ))
  11942. ((or (eq action 'set)
  11943. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11944. (if (not (eq action 'set))
  11945. (setq new action)
  11946. (message "Priority %c-%c, SPC to remove: "
  11947. org-highest-priority org-lowest-priority)
  11948. (save-match-data
  11949. (setq new (read-char-exclusive))))
  11950. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11951. (= (upcase org-lowest-priority) org-lowest-priority))
  11952. (setq new (upcase new)))
  11953. (cond ((equal new ?\ ) (setq remove t))
  11954. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11955. (error "Priority must be between `%c' and `%c'"
  11956. org-highest-priority org-lowest-priority))))
  11957. ((eq action 'up)
  11958. (setq new (if have
  11959. (1- current) ; normal cycling
  11960. ;; last priority was empty
  11961. (if (eq last-command this-command)
  11962. org-lowest-priority ; wrap around empty to lowest
  11963. ;; default
  11964. (if org-priority-start-cycle-with-default
  11965. org-default-priority
  11966. (1- org-default-priority))))))
  11967. ((eq action 'down)
  11968. (setq new (if have
  11969. (1+ current) ; normal cycling
  11970. ;; last priority was empty
  11971. (if (eq last-command this-command)
  11972. org-highest-priority ; wrap around empty to highest
  11973. ;; default
  11974. (if org-priority-start-cycle-with-default
  11975. org-default-priority
  11976. (1+ org-default-priority))))))
  11977. (t (error "Invalid action")))
  11978. (if (or (< (upcase new) org-highest-priority)
  11979. (> (upcase new) org-lowest-priority))
  11980. (if (and (memq action '(up down))
  11981. (not have) (not (eq last-command this-command)))
  11982. ;; `new' is from default priority
  11983. (error
  11984. "The default can not be set, see `org-default-priority' why")
  11985. ;; normal cycling: `new' is beyond highest/lowest priority
  11986. ;; and is wrapped around to the empty priority
  11987. (setq remove t)))
  11988. (setq news (format "%c" new))
  11989. (if have
  11990. (if remove
  11991. (replace-match "" t t nil 1)
  11992. (replace-match news t t nil 2))
  11993. (if remove
  11994. (error "No priority cookie found in line")
  11995. (let ((case-fold-search nil))
  11996. (looking-at org-todo-line-regexp))
  11997. (if (match-end 2)
  11998. (progn
  11999. (goto-char (match-end 2))
  12000. (insert " [#" news "]"))
  12001. (goto-char (match-beginning 3))
  12002. (insert "[#" news "] "))))
  12003. (org-preserve-lc (org-set-tags nil 'align)))
  12004. (if remove
  12005. (message "Priority removed")
  12006. (message "Priority of current item set to %s" news)))))
  12007. (defun org-show-priority ()
  12008. "Show the priority of the current item.
  12009. This priority is composed of the main priority given with the [#A] cookies,
  12010. and by additional input from the age of a schedules or deadline entry."
  12011. (interactive)
  12012. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12013. (org-get-at-bol 'priority)
  12014. (save-excursion
  12015. (save-match-data
  12016. (beginning-of-line)
  12017. (and (looking-at org-heading-regexp)
  12018. (org-get-priority (match-string 0))))))))
  12019. (message "Priority is %d" (if pri pri -1000))))
  12020. (defun org-get-priority (s)
  12021. "Find priority cookie and return priority."
  12022. (if (functionp org-get-priority-function)
  12023. (funcall org-get-priority-function)
  12024. (save-match-data
  12025. (if (not (string-match org-priority-regexp s))
  12026. (* 1000 (- org-lowest-priority org-default-priority))
  12027. (* 1000 (- org-lowest-priority
  12028. (string-to-char (match-string 2 s))))))))
  12029. ;;;; Tags
  12030. (defvar org-agenda-archives-mode)
  12031. (defvar org-map-continue-from nil
  12032. "Position from where mapping should continue.
  12033. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12034. (defvar org-scanner-tags nil
  12035. "The current tag list while the tags scanner is running.")
  12036. (defvar org-trust-scanner-tags nil
  12037. "Should `org-get-tags-at' use the tags for the scanner.
  12038. This is for internal dynamical scoping only.
  12039. When this is non-nil, the function `org-get-tags-at' will return the value
  12040. of `org-scanner-tags' instead of building the list by itself. This
  12041. can lead to large speed-ups when the tags scanner is used in a file with
  12042. many entries, and when the list of tags is retrieved, for example to
  12043. obtain a list of properties. Building the tags list for each entry in such
  12044. a file becomes an N^2 operation - but with this variable set, it scales
  12045. as N.")
  12046. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12047. "Scan headline tags with inheritance and produce output ACTION.
  12048. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12049. or `agenda' to produce an entry list for an agenda view. It can also be
  12050. a Lisp form or a function that should be called at each matched headline, in
  12051. this case the return value is a list of all return values from these calls.
  12052. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12053. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12054. only lines with a not-done TODO keyword are included in the output.
  12055. This should be the same variable that was scoped into
  12056. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12057. START-LEVEL can be a string with asterisks, reducing the scope to
  12058. headlines matching this string."
  12059. (require 'org-agenda)
  12060. (let* ((re (concat "^"
  12061. (if start-level
  12062. ;; Get the correct level to match
  12063. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12064. org-outline-regexp)
  12065. " *\\(\\<\\("
  12066. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12067. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12068. (props (list 'face 'default
  12069. 'done-face 'org-agenda-done
  12070. 'undone-face 'default
  12071. 'mouse-face 'highlight
  12072. 'org-not-done-regexp org-not-done-regexp
  12073. 'org-todo-regexp org-todo-regexp
  12074. 'org-complex-heading-regexp org-complex-heading-regexp
  12075. 'help-echo
  12076. (format "mouse-2 or RET jump to org file %s"
  12077. (abbreviate-file-name
  12078. (or (buffer-file-name (buffer-base-buffer))
  12079. (buffer-name (buffer-base-buffer)))))))
  12080. (case-fold-search nil)
  12081. (org-map-continue-from nil)
  12082. lspos tags tags-list
  12083. (tags-alist (list (cons 0 org-file-tags)))
  12084. (llast 0) rtn rtn1 level category i txt
  12085. todo marker entry priority)
  12086. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12087. (setq action (list 'lambda nil action)))
  12088. (save-excursion
  12089. (goto-char (point-min))
  12090. (when (eq action 'sparse-tree)
  12091. (org-overview)
  12092. (org-remove-occur-highlights))
  12093. (while (re-search-forward re nil t)
  12094. (setq org-map-continue-from nil)
  12095. (catch :skip
  12096. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12097. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12098. (goto-char (setq lspos (match-beginning 0)))
  12099. (setq level (org-reduced-level (org-outline-level))
  12100. category (org-get-category))
  12101. (setq i llast llast level)
  12102. ;; remove tag lists from same and sublevels
  12103. (while (>= i level)
  12104. (when (setq entry (assoc i tags-alist))
  12105. (setq tags-alist (delete entry tags-alist)))
  12106. (setq i (1- i)))
  12107. ;; add the next tags
  12108. (when tags
  12109. (setq tags (org-split-string tags ":")
  12110. tags-alist
  12111. (cons (cons level tags) tags-alist)))
  12112. ;; compile tags for current headline
  12113. (setq tags-list
  12114. (if org-use-tag-inheritance
  12115. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12116. tags)
  12117. org-scanner-tags tags-list)
  12118. (when org-use-tag-inheritance
  12119. (setcdr (car tags-alist)
  12120. (mapcar (lambda (x)
  12121. (setq x (copy-sequence x))
  12122. (org-add-prop-inherited x))
  12123. (cdar tags-alist))))
  12124. (when (and tags org-use-tag-inheritance
  12125. (or (not (eq t org-use-tag-inheritance))
  12126. org-tags-exclude-from-inheritance))
  12127. ;; selective inheritance, remove uninherited ones
  12128. (setcdr (car tags-alist)
  12129. (org-remove-uninherited-tags (cdar tags-alist))))
  12130. (when (and
  12131. ;; eval matcher only when the todo condition is OK
  12132. (and (or (not todo-only) (member todo org-not-done-keywords))
  12133. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12134. (eval matcher)))
  12135. ;; Call the skipper, but return t if it does not skip,
  12136. ;; so that the `and' form continues evaluating
  12137. (progn
  12138. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12139. t)
  12140. ;; Check if timestamps are deselecting this entry
  12141. (or (not todo-only)
  12142. (and (member todo org-not-done-keywords)
  12143. (or (not org-agenda-tags-todo-honor-ignore-options)
  12144. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  12145. ;; Extra check for the archive tag
  12146. ;; FIXME: Does the skipper already do this????
  12147. (or
  12148. (not (member org-archive-tag tags-list))
  12149. ;; we have an archive tag, should we use this anyway?
  12150. (or (not org-agenda-skip-archived-trees)
  12151. (and (eq action 'agenda) org-agenda-archives-mode))))
  12152. ;; select this headline
  12153. (cond
  12154. ((eq action 'sparse-tree)
  12155. (and org-highlight-sparse-tree-matches
  12156. (org-get-heading) (match-end 0)
  12157. (org-highlight-new-match
  12158. (match-beginning 1) (match-end 1)))
  12159. (org-show-context 'tags-tree))
  12160. ((eq action 'agenda)
  12161. (setq txt (org-agenda-format-item
  12162. ""
  12163. (concat
  12164. (if (eq org-tags-match-list-sublevels 'indented)
  12165. (make-string (1- level) ?.) "")
  12166. (org-get-heading))
  12167. level category
  12168. tags-list)
  12169. priority (org-get-priority txt))
  12170. (goto-char lspos)
  12171. (setq marker (org-agenda-new-marker))
  12172. (org-add-props txt props
  12173. 'org-marker marker 'org-hd-marker marker 'org-category category
  12174. 'todo-state todo
  12175. 'priority priority 'type "tagsmatch")
  12176. (push txt rtn))
  12177. ((functionp action)
  12178. (setq org-map-continue-from nil)
  12179. (save-excursion
  12180. (setq rtn1 (funcall action))
  12181. (push rtn1 rtn)))
  12182. (t (error "Invalid action")))
  12183. ;; if we are to skip sublevels, jump to end of subtree
  12184. (unless org-tags-match-list-sublevels
  12185. (org-end-of-subtree t)
  12186. (backward-char 1))))
  12187. ;; Get the correct position from where to continue
  12188. (if org-map-continue-from
  12189. (goto-char org-map-continue-from)
  12190. (and (= (point) lspos) (end-of-line 1)))))
  12191. (when (and (eq action 'sparse-tree)
  12192. (not org-sparse-tree-open-archived-trees))
  12193. (org-hide-archived-subtrees (point-min) (point-max)))
  12194. (nreverse rtn)))
  12195. (defun org-remove-uninherited-tags (tags)
  12196. "Remove all tags that are not inherited from the list TAGS."
  12197. (cond
  12198. ((eq org-use-tag-inheritance t)
  12199. (if org-tags-exclude-from-inheritance
  12200. (org-delete-all org-tags-exclude-from-inheritance tags)
  12201. tags))
  12202. ((not org-use-tag-inheritance) nil)
  12203. ((stringp org-use-tag-inheritance)
  12204. (delq nil (mapcar
  12205. (lambda (x)
  12206. (if (and (string-match org-use-tag-inheritance x)
  12207. (not (member x org-tags-exclude-from-inheritance)))
  12208. x nil))
  12209. tags)))
  12210. ((listp org-use-tag-inheritance)
  12211. (delq nil (mapcar
  12212. (lambda (x)
  12213. (if (member x org-use-tag-inheritance) x nil))
  12214. tags)))))
  12215. (defun org-match-sparse-tree (&optional todo-only match)
  12216. "Create a sparse tree according to tags string MATCH.
  12217. MATCH can contain positive and negative selection of tags, like
  12218. \"+WORK+URGENT-WITHBOSS\".
  12219. If optional argument TODO-ONLY is non-nil, only select lines that are
  12220. also TODO lines."
  12221. (interactive "P")
  12222. (org-agenda-prepare-buffers (list (current-buffer)))
  12223. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12224. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12225. (defvar org-cached-props nil)
  12226. (defun org-cached-entry-get (pom property)
  12227. (if (or (eq t org-use-property-inheritance)
  12228. (and (stringp org-use-property-inheritance)
  12229. (string-match org-use-property-inheritance property))
  12230. (and (listp org-use-property-inheritance)
  12231. (member property org-use-property-inheritance)))
  12232. ;; Caching is not possible, check it directly
  12233. (org-entry-get pom property 'inherit)
  12234. ;; Get all properties, so that we can do complicated checks easily
  12235. (cdr (assoc property (or org-cached-props
  12236. (setq org-cached-props
  12237. (org-entry-properties pom)))))))
  12238. (defun org-global-tags-completion-table (&optional files)
  12239. "Return the list of all tags in all agenda buffer/files.
  12240. Optional FILES argument is a list of files which can be used
  12241. instead of the agenda files."
  12242. (save-excursion
  12243. (org-uniquify
  12244. (delq nil
  12245. (apply 'append
  12246. (mapcar
  12247. (lambda (file)
  12248. (set-buffer (find-file-noselect file))
  12249. (append (org-get-buffer-tags)
  12250. (mapcar (lambda (x) (if (stringp (car-safe x))
  12251. (list (car-safe x)) nil))
  12252. org-tag-alist)))
  12253. (if (and files (car files))
  12254. files
  12255. (org-agenda-files))))))))
  12256. (defun org-make-tags-matcher (match)
  12257. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12258. The variable `todo-only' is scoped dynamically into this function.
  12259. It will be set to t if the matcher restricts matching to TODO entries,
  12260. otherwise will not be touched.
  12261. Returns a cons of the selection string MATCH and the constructed
  12262. lisp form implementing the matcher. The matcher is to be evaluated
  12263. at an Org entry, with point on the headline, and returns t if the
  12264. entry matches the selection string MATCH. The returned lisp form
  12265. references two variables with information about the entry, which
  12266. must be bound around the form's evaluation: todo, the TODO keyword
  12267. at the entry (or nil of none); and tags-list, the list of all tags
  12268. at the entry including inherited ones. Additionally, the category
  12269. of the entry (if any) must be specified as the text property
  12270. 'org-category on the headline.
  12271. See also `org-scan-tags'.
  12272. "
  12273. (declare (special todo-only))
  12274. (unless (boundp 'todo-only)
  12275. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12276. (unless match
  12277. ;; Get a new match request, with completion
  12278. (let ((org-last-tags-completion-table
  12279. (org-global-tags-completion-table)))
  12280. (setq match (org-completing-read-no-i
  12281. "Match: " 'org-tags-completion-function nil nil nil
  12282. 'org-tags-history))))
  12283. ;; Parse the string and create a lisp form
  12284. (let ((match0 match)
  12285. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12286. minus tag mm
  12287. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12288. orterms term orlist re-p str-p level-p level-op time-p
  12289. prop-p pn pv po gv rest)
  12290. (if (string-match "/+" match)
  12291. ;; match contains also a todo-matching request
  12292. (progn
  12293. (setq tagsmatch (substring match 0 (match-beginning 0))
  12294. todomatch (substring match (match-end 0)))
  12295. (if (string-match "^!" todomatch)
  12296. (setq todo-only t todomatch (substring todomatch 1)))
  12297. (if (string-match "^\\s-*$" todomatch)
  12298. (setq todomatch nil)))
  12299. ;; only matching tags
  12300. (setq tagsmatch match todomatch nil))
  12301. ;; Make the tags matcher
  12302. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12303. (setq tagsmatcher t)
  12304. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12305. (while (setq term (pop orterms))
  12306. (while (and (equal (substring term -1) "\\") orterms)
  12307. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12308. (while (string-match re term)
  12309. (setq rest (substring term (match-end 0))
  12310. minus (and (match-end 1)
  12311. (equal (match-string 1 term) "-"))
  12312. tag (save-match-data (replace-regexp-in-string
  12313. "\\\\-" "-"
  12314. (match-string 2 term)))
  12315. re-p (equal (string-to-char tag) ?{)
  12316. level-p (match-end 4)
  12317. prop-p (match-end 5)
  12318. mm (cond
  12319. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12320. (level-p
  12321. (setq level-op (org-op-to-function (match-string 3 term)))
  12322. `(,level-op level ,(string-to-number
  12323. (match-string 4 term))))
  12324. (prop-p
  12325. (setq pn (match-string 5 term)
  12326. po (match-string 6 term)
  12327. pv (match-string 7 term)
  12328. re-p (equal (string-to-char pv) ?{)
  12329. str-p (equal (string-to-char pv) ?\")
  12330. time-p (save-match-data
  12331. (string-match "^\"[[<].*[]>]\"$" pv))
  12332. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12333. (if time-p (setq pv (org-matcher-time pv)))
  12334. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12335. (cond
  12336. ((equal pn "CATEGORY")
  12337. (setq gv '(get-text-property (point) 'org-category)))
  12338. ((equal pn "TODO")
  12339. (setq gv 'todo))
  12340. (t
  12341. (setq gv `(org-cached-entry-get nil ,pn))))
  12342. (if re-p
  12343. (if (eq po 'org<>)
  12344. `(not (string-match ,pv (or ,gv "")))
  12345. `(string-match ,pv (or ,gv "")))
  12346. (if str-p
  12347. `(,po (or ,gv "") ,pv)
  12348. `(,po (string-to-number (or ,gv ""))
  12349. ,(string-to-number pv) ))))
  12350. (t `(member ,tag tags-list)))
  12351. mm (if minus (list 'not mm) mm)
  12352. term rest)
  12353. (push mm tagsmatcher))
  12354. (push (if (> (length tagsmatcher) 1)
  12355. (cons 'and tagsmatcher)
  12356. (car tagsmatcher))
  12357. orlist)
  12358. (setq tagsmatcher nil))
  12359. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12360. (setq tagsmatcher
  12361. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12362. ;; Make the todo matcher
  12363. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12364. (setq todomatcher t)
  12365. (setq orterms (org-split-string todomatch "|") orlist nil)
  12366. (while (setq term (pop orterms))
  12367. (while (string-match re term)
  12368. (setq minus (and (match-end 1)
  12369. (equal (match-string 1 term) "-"))
  12370. kwd (match-string 2 term)
  12371. re-p (equal (string-to-char kwd) ?{)
  12372. term (substring term (match-end 0))
  12373. mm (if re-p
  12374. `(string-match ,(substring kwd 1 -1) todo)
  12375. (list 'equal 'todo kwd))
  12376. mm (if minus (list 'not mm) mm))
  12377. (push mm todomatcher))
  12378. (push (if (> (length todomatcher) 1)
  12379. (cons 'and todomatcher)
  12380. (car todomatcher))
  12381. orlist)
  12382. (setq todomatcher nil))
  12383. (setq todomatcher (if (> (length orlist) 1)
  12384. (cons 'or orlist) (car orlist))))
  12385. ;; Return the string and lisp forms of the matcher
  12386. (setq matcher (if todomatcher
  12387. (list 'and tagsmatcher todomatcher)
  12388. tagsmatcher))
  12389. (when todo-only
  12390. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12391. matcher)))
  12392. (cons match0 matcher)))
  12393. (defun org-op-to-function (op &optional stringp)
  12394. "Turn an operator into the appropriate function."
  12395. (setq op
  12396. (cond
  12397. ((equal op "<" ) '(< string< org-time<))
  12398. ((equal op ">" ) '(> org-string> org-time>))
  12399. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12400. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12401. ((member op '("=" "==")) '(= string= org-time=))
  12402. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12403. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12404. (defun org<> (a b) (not (= a b)))
  12405. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12406. (defun org-string>= (a b) (not (string< a b)))
  12407. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12408. (defun org-string<> (a b) (not (string= a b)))
  12409. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12410. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12411. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12412. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12413. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12414. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12415. (defun org-2ft (s)
  12416. "Convert S to a floating point time.
  12417. If S is already a number, just return it. If it is a string, parse
  12418. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12419. (cond
  12420. ((numberp s) s)
  12421. ((stringp s)
  12422. (condition-case nil
  12423. (float-time (apply 'encode-time (org-parse-time-string s)))
  12424. (error 0.)))
  12425. (t 0.)))
  12426. (defun org-time-today ()
  12427. "Time in seconds today at 0:00.
  12428. Returns the float number of seconds since the beginning of the
  12429. epoch to the beginning of today (00:00)."
  12430. (float-time (apply 'encode-time
  12431. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12432. (defun org-matcher-time (s)
  12433. "Interpret a time comparison value."
  12434. (save-match-data
  12435. (cond
  12436. ((string= s "<now>") (float-time))
  12437. ((string= s "<today>") (org-time-today))
  12438. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12439. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12440. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12441. (+ (org-time-today)
  12442. (* (string-to-number (match-string 1 s))
  12443. (cdr (assoc (match-string 2 s)
  12444. '(("d" . 86400.0) ("w" . 604800.0)
  12445. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12446. (t (org-2ft s)))))
  12447. (defun org-match-any-p (re list)
  12448. "Does re match any element of list?"
  12449. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12450. (delq nil list))
  12451. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12452. (defvar org-tags-overlay (make-overlay 1 1))
  12453. (org-detach-overlay org-tags-overlay)
  12454. (defun org-get-local-tags-at (&optional pos)
  12455. "Get a list of tags defined in the current headline."
  12456. (org-get-tags-at pos 'local))
  12457. (defun org-get-local-tags ()
  12458. "Get a list of tags defined in the current headline."
  12459. (org-get-tags-at nil 'local))
  12460. (defun org-get-tags-at (&optional pos local)
  12461. "Get a list of all headline tags applicable at POS.
  12462. POS defaults to point. If tags are inherited, the list contains
  12463. the targets in the same sequence as the headlines appear, i.e.
  12464. the tags of the current headline come last.
  12465. When LOCAL is non-nil, only return tags from the current headline,
  12466. ignore inherited ones."
  12467. (interactive)
  12468. (if (and org-trust-scanner-tags
  12469. (or (not pos) (equal pos (point)))
  12470. (not local))
  12471. org-scanner-tags
  12472. (let (tags ltags lastpos parent)
  12473. (save-excursion
  12474. (save-restriction
  12475. (widen)
  12476. (goto-char (or pos (point)))
  12477. (save-match-data
  12478. (catch 'done
  12479. (condition-case nil
  12480. (progn
  12481. (org-back-to-heading t)
  12482. (while (not (equal lastpos (point)))
  12483. (setq lastpos (point))
  12484. (when (looking-at
  12485. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12486. (setq ltags (org-split-string
  12487. (org-match-string-no-properties 1) ":"))
  12488. (when parent
  12489. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12490. (setq tags (append
  12491. (if parent
  12492. (org-remove-uninherited-tags ltags)
  12493. ltags)
  12494. tags)))
  12495. (or org-use-tag-inheritance (throw 'done t))
  12496. (if local (throw 'done t))
  12497. (or (org-up-heading-safe) (error nil))
  12498. (setq parent t)))
  12499. (error nil)))))
  12500. (if local
  12501. tags
  12502. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12503. (defun org-add-prop-inherited (s)
  12504. (add-text-properties 0 (length s) '(inherited t) s)
  12505. s)
  12506. (defun org-toggle-tag (tag &optional onoff)
  12507. "Toggle the tag TAG for the current line.
  12508. If ONOFF is `on' or `off', don't toggle but set to this state."
  12509. (let (res current)
  12510. (save-excursion
  12511. (org-back-to-heading t)
  12512. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12513. (point-at-eol) t)
  12514. (progn
  12515. (setq current (match-string 1))
  12516. (replace-match ""))
  12517. (setq current ""))
  12518. (setq current (nreverse (org-split-string current ":")))
  12519. (cond
  12520. ((eq onoff 'on)
  12521. (setq res t)
  12522. (or (member tag current) (push tag current)))
  12523. ((eq onoff 'off)
  12524. (or (not (member tag current)) (setq current (delete tag current))))
  12525. (t (if (member tag current)
  12526. (setq current (delete tag current))
  12527. (setq res t)
  12528. (push tag current))))
  12529. (end-of-line 1)
  12530. (if current
  12531. (progn
  12532. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12533. (org-set-tags nil t))
  12534. (delete-horizontal-space))
  12535. (run-hooks 'org-after-tags-change-hook))
  12536. res))
  12537. (defun org-align-tags-here (to-col)
  12538. ;; Assumes that this is a headline
  12539. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12540. (beginning-of-line 1)
  12541. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12542. (< pos (match-beginning 2)))
  12543. (progn
  12544. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12545. (goto-char (match-beginning 1))
  12546. (insert " ")
  12547. (delete-region (point) (1+ (match-beginning 2)))
  12548. (setq ncol (max (current-column)
  12549. (1+ col)
  12550. (if (> to-col 0)
  12551. to-col
  12552. (- (abs to-col) tags-l))))
  12553. (setq p (point))
  12554. (insert (make-string (- ncol (current-column)) ?\ ))
  12555. (setq ncol (current-column))
  12556. (when indent-tabs-mode (tabify p (point-at-eol)))
  12557. (org-move-to-column (min ncol col) t))
  12558. (goto-char pos))))
  12559. (defun org-set-tags-command (&optional arg just-align)
  12560. "Call the set-tags command for the current entry."
  12561. (interactive "P")
  12562. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12563. (org-set-tags arg just-align)
  12564. (save-excursion
  12565. (org-back-to-heading t)
  12566. (org-set-tags arg just-align))))
  12567. (defun org-set-tags-to (data)
  12568. "Set the tags of the current entry to DATA, replacing the current tags.
  12569. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12570. If DATA is nil or the empty string, any tags will be removed."
  12571. (interactive "sTags: ")
  12572. (setq data
  12573. (cond
  12574. ((eq data nil) "")
  12575. ((equal data "") "")
  12576. ((stringp data)
  12577. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12578. ":"))
  12579. ((listp data)
  12580. (concat ":" (mapconcat 'identity data ":") ":"))))
  12581. (when data
  12582. (save-excursion
  12583. (org-back-to-heading t)
  12584. (when (looking-at org-complex-heading-regexp)
  12585. (if (match-end 5)
  12586. (progn
  12587. (goto-char (match-beginning 5))
  12588. (insert data)
  12589. (delete-region (point) (point-at-eol))
  12590. (org-set-tags nil 'align))
  12591. (goto-char (point-at-eol))
  12592. (insert " " data)
  12593. (org-set-tags nil 'align)))
  12594. (beginning-of-line 1)
  12595. (if (looking-at ".*?\\([ \t]+\\)$")
  12596. (delete-region (match-beginning 1) (match-end 1))))))
  12597. (defun org-align-all-tags ()
  12598. "Align the tags i all headings."
  12599. (interactive)
  12600. (save-excursion
  12601. (or (ignore-errors (org-back-to-heading t))
  12602. (outline-next-heading))
  12603. (if (org-at-heading-p)
  12604. (org-set-tags t)
  12605. (message "No headings"))))
  12606. (defvar org-indent-indentation-per-level)
  12607. (defun org-set-tags (&optional arg just-align)
  12608. "Set the tags for the current headline.
  12609. With prefix ARG, realign all tags in headings in the current buffer."
  12610. (interactive "P")
  12611. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12612. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12613. 'region-start-level 'region))
  12614. org-loop-over-headlines-in-active-region)
  12615. (org-map-entries
  12616. ;; We don't use ARG and JUST-ALIGN here these args are not
  12617. ;; useful when looping over headlines
  12618. `(org-set-tags)
  12619. org-loop-over-headlines-in-active-region
  12620. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12621. (let* ((re org-outline-regexp-bol)
  12622. (current (unless arg (org-get-tags-string)))
  12623. (col (current-column))
  12624. (org-setting-tags t)
  12625. table current-tags inherited-tags ; computed below when needed
  12626. tags p0 c0 c1 rpl di tc level)
  12627. (if arg
  12628. (save-excursion
  12629. (goto-char (point-min))
  12630. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12631. (while (re-search-forward re nil t)
  12632. (org-set-tags nil t)
  12633. (end-of-line 1)))
  12634. (message "All tags realigned to column %d" org-tags-column))
  12635. (if just-align
  12636. (setq tags current)
  12637. ;; Get a new set of tags from the user
  12638. (save-excursion
  12639. (setq table (append org-tag-persistent-alist
  12640. (or org-tag-alist (org-get-buffer-tags))
  12641. (and
  12642. org-complete-tags-always-offer-all-agenda-tags
  12643. (org-global-tags-completion-table
  12644. (org-agenda-files))))
  12645. org-last-tags-completion-table table
  12646. current-tags (org-split-string current ":")
  12647. inherited-tags (nreverse
  12648. (nthcdr (length current-tags)
  12649. (nreverse (org-get-tags-at))))
  12650. tags
  12651. (if (or (eq t org-use-fast-tag-selection)
  12652. (and org-use-fast-tag-selection
  12653. (delq nil (mapcar 'cdr table))))
  12654. (org-fast-tag-selection
  12655. current-tags inherited-tags table
  12656. (if org-fast-tag-selection-include-todo
  12657. org-todo-key-alist))
  12658. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12659. (org-trim
  12660. (org-icompleting-read "Tags: "
  12661. 'org-tags-completion-function
  12662. nil nil current 'org-tags-history))))))
  12663. (while (string-match "[-+&]+" tags)
  12664. ;; No boolean logic, just a list
  12665. (setq tags (replace-match ":" t t tags))))
  12666. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12667. (if org-tags-sort-function
  12668. (setq tags (mapconcat 'identity
  12669. (sort (org-split-string
  12670. tags (org-re "[^[:alnum:]_@#%]+"))
  12671. org-tags-sort-function) ":")))
  12672. (if (string-match "\\`[\t ]*\\'" tags)
  12673. (setq tags "")
  12674. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12675. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12676. ;; Insert new tags at the correct column
  12677. (beginning-of-line 1)
  12678. (setq level (or (and (looking-at org-outline-regexp)
  12679. (- (match-end 0) (point) 1))
  12680. 1))
  12681. (cond
  12682. ((and (equal current "") (equal tags "")))
  12683. ((re-search-forward
  12684. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12685. (point-at-eol) t)
  12686. (if (equal tags "")
  12687. (setq rpl "")
  12688. (goto-char (match-beginning 0))
  12689. (setq c0 (current-column)
  12690. ;; compute offset for the case of org-indent-mode active
  12691. di (if org-indent-mode
  12692. (* (1- org-indent-indentation-per-level) (1- level))
  12693. 0)
  12694. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12695. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12696. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12697. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12698. (replace-match rpl t t)
  12699. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12700. tags)
  12701. (t (error "Tags alignment failed")))
  12702. (org-move-to-column col)
  12703. (unless just-align
  12704. (run-hooks 'org-after-tags-change-hook))))))
  12705. (defun org-change-tag-in-region (beg end tag off)
  12706. "Add or remove TAG for each entry in the region.
  12707. This works in the agenda, and also in an org-mode buffer."
  12708. (interactive
  12709. (list (region-beginning) (region-end)
  12710. (let ((org-last-tags-completion-table
  12711. (if (derived-mode-p 'org-mode)
  12712. (org-get-buffer-tags)
  12713. (org-global-tags-completion-table))))
  12714. (org-icompleting-read
  12715. "Tag: " 'org-tags-completion-function nil nil nil
  12716. 'org-tags-history))
  12717. (progn
  12718. (message "[s]et or [r]emove? ")
  12719. (equal (read-char-exclusive) ?r))))
  12720. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12721. (let ((agendap (equal major-mode 'org-agenda-mode))
  12722. l1 l2 m buf pos newhead (cnt 0))
  12723. (goto-char end)
  12724. (setq l2 (1- (org-current-line)))
  12725. (goto-char beg)
  12726. (setq l1 (org-current-line))
  12727. (loop for l from l1 to l2 do
  12728. (org-goto-line l)
  12729. (setq m (get-text-property (point) 'org-hd-marker))
  12730. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12731. (and agendap m))
  12732. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12733. pos (if agendap m (point)))
  12734. (with-current-buffer buf
  12735. (save-excursion
  12736. (save-restriction
  12737. (goto-char pos)
  12738. (setq cnt (1+ cnt))
  12739. (org-toggle-tag tag (if off 'off 'on))
  12740. (setq newhead (org-get-heading)))))
  12741. (and agendap (org-agenda-change-all-lines newhead m))))
  12742. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12743. (defun org-tags-completion-function (string predicate &optional flag)
  12744. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12745. (confirm (lambda (x) (stringp (car x)))))
  12746. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12747. (setq s1 (match-string 1 string)
  12748. s2 (match-string 2 string))
  12749. (setq s1 "" s2 string))
  12750. (cond
  12751. ((eq flag nil)
  12752. ;; try completion
  12753. (setq rtn (try-completion s2 ctable confirm))
  12754. (if (stringp rtn)
  12755. (setq rtn
  12756. (concat s1 s2 (substring rtn (length s2))
  12757. (if (and org-add-colon-after-tag-completion
  12758. (assoc rtn ctable))
  12759. ":" ""))))
  12760. rtn)
  12761. ((eq flag t)
  12762. ;; all-completions
  12763. (all-completions s2 ctable confirm)
  12764. )
  12765. ((eq flag 'lambda)
  12766. ;; exact match?
  12767. (assoc s2 ctable)))
  12768. ))
  12769. (defun org-fast-tag-insert (kwd tags face &optional end)
  12770. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12771. (insert (format "%-12s" (concat kwd ":"))
  12772. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12773. (or end "")))
  12774. (defun org-fast-tag-show-exit (flag)
  12775. (save-excursion
  12776. (org-goto-line 3)
  12777. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12778. (replace-match ""))
  12779. (when flag
  12780. (end-of-line 1)
  12781. (org-move-to-column (- (window-width) 19) t)
  12782. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12783. (defun org-set-current-tags-overlay (current prefix)
  12784. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12785. (if (featurep 'xemacs)
  12786. (org-overlay-display org-tags-overlay (concat prefix s)
  12787. 'secondary-selection)
  12788. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12789. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12790. (defvar org-last-tag-selection-key nil)
  12791. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12792. "Fast tag selection with single keys.
  12793. CURRENT is the current list of tags in the headline, INHERITED is the
  12794. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12795. possibly with grouping information. TODO-TABLE is a similar table with
  12796. TODO keywords, should these have keys assigned to them.
  12797. If the keys are nil, a-z are automatically assigned.
  12798. Returns the new tags string, or nil to not change the current settings."
  12799. (let* ((fulltable (append table todo-table))
  12800. (maxlen (apply 'max (mapcar
  12801. (lambda (x)
  12802. (if (stringp (car x)) (string-width (car x)) 0))
  12803. fulltable)))
  12804. (buf (current-buffer))
  12805. (expert (eq org-fast-tag-selection-single-key 'expert))
  12806. (buffer-tags nil)
  12807. (fwidth (+ maxlen 3 1 3))
  12808. (ncol (/ (- (window-width) 4) fwidth))
  12809. (i-face 'org-done)
  12810. (c-face 'org-todo)
  12811. tg cnt e c char c1 c2 ntable tbl rtn
  12812. ov-start ov-end ov-prefix
  12813. (exit-after-next org-fast-tag-selection-single-key)
  12814. (done-keywords org-done-keywords)
  12815. groups ingroup)
  12816. (save-excursion
  12817. (beginning-of-line 1)
  12818. (if (looking-at
  12819. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12820. (setq ov-start (match-beginning 1)
  12821. ov-end (match-end 1)
  12822. ov-prefix "")
  12823. (setq ov-start (1- (point-at-eol))
  12824. ov-end (1+ ov-start))
  12825. (skip-chars-forward "^\n\r")
  12826. (setq ov-prefix
  12827. (concat
  12828. (buffer-substring (1- (point)) (point))
  12829. (if (> (current-column) org-tags-column)
  12830. " "
  12831. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12832. (move-overlay org-tags-overlay ov-start ov-end)
  12833. (save-window-excursion
  12834. (if expert
  12835. (set-buffer (get-buffer-create " *Org tags*"))
  12836. (delete-other-windows)
  12837. (split-window-vertically)
  12838. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12839. (erase-buffer)
  12840. (org-set-local 'org-done-keywords done-keywords)
  12841. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12842. (org-fast-tag-insert "Current" current c-face "\n\n")
  12843. (org-fast-tag-show-exit exit-after-next)
  12844. (org-set-current-tags-overlay current ov-prefix)
  12845. (setq tbl fulltable char ?a cnt 0)
  12846. (while (setq e (pop tbl))
  12847. (cond
  12848. ((equal (car e) :startgroup)
  12849. (push '() groups) (setq ingroup t)
  12850. (when (not (= cnt 0))
  12851. (setq cnt 0)
  12852. (insert "\n"))
  12853. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12854. ((equal (car e) :endgroup)
  12855. (setq ingroup nil cnt 0)
  12856. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12857. ((equal e '(:newline))
  12858. (when (not (= cnt 0))
  12859. (setq cnt 0)
  12860. (insert "\n")
  12861. (setq e (car tbl))
  12862. (while (equal (car tbl) '(:newline))
  12863. (insert "\n")
  12864. (setq tbl (cdr tbl)))))
  12865. (t
  12866. (setq tg (copy-sequence (car e)) c2 nil)
  12867. (if (cdr e)
  12868. (setq c (cdr e))
  12869. ;; automatically assign a character.
  12870. (setq c1 (string-to-char
  12871. (downcase (substring
  12872. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12873. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12874. (while (or (rassoc char ntable) (rassoc char table))
  12875. (setq char (1+ char)))
  12876. (setq c2 c1))
  12877. (setq c (or c2 char)))
  12878. (if ingroup (push tg (car groups)))
  12879. (setq tg (org-add-props tg nil 'face
  12880. (cond
  12881. ((not (assoc tg table))
  12882. (org-get-todo-face tg))
  12883. ((member tg current) c-face)
  12884. ((member tg inherited) i-face))))
  12885. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12886. (insert "[" c "] " tg (make-string
  12887. (- fwidth 4 (length tg)) ?\ ))
  12888. (push (cons tg c) ntable)
  12889. (when (= (setq cnt (1+ cnt)) ncol)
  12890. (insert "\n")
  12891. (if ingroup (insert " "))
  12892. (setq cnt 0)))))
  12893. (setq ntable (nreverse ntable))
  12894. (insert "\n")
  12895. (goto-char (point-min))
  12896. (if (not expert) (org-fit-window-to-buffer))
  12897. (setq rtn
  12898. (catch 'exit
  12899. (while t
  12900. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12901. (if (not groups) "no " "")
  12902. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12903. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12904. (setq org-last-tag-selection-key c)
  12905. (cond
  12906. ((= c ?\r) (throw 'exit t))
  12907. ((= c ?!)
  12908. (setq groups (not groups))
  12909. (goto-char (point-min))
  12910. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12911. ((= c ?\C-c)
  12912. (if (not expert)
  12913. (org-fast-tag-show-exit
  12914. (setq exit-after-next (not exit-after-next)))
  12915. (setq expert nil)
  12916. (delete-other-windows)
  12917. (set-window-buffer (split-window-vertically) " *Org tags*")
  12918. (org-switch-to-buffer-other-window " *Org tags*")
  12919. (org-fit-window-to-buffer)))
  12920. ((or (= c ?\C-g)
  12921. (and (= c ?q) (not (rassoc c ntable))))
  12922. (org-detach-overlay org-tags-overlay)
  12923. (setq quit-flag t))
  12924. ((= c ?\ )
  12925. (setq current nil)
  12926. (if exit-after-next (setq exit-after-next 'now)))
  12927. ((= c ?\t)
  12928. (condition-case nil
  12929. (setq tg (org-icompleting-read
  12930. "Tag: "
  12931. (or buffer-tags
  12932. (with-current-buffer buf
  12933. (org-get-buffer-tags)))))
  12934. (quit (setq tg "")))
  12935. (when (string-match "\\S-" tg)
  12936. (add-to-list 'buffer-tags (list tg))
  12937. (if (member tg current)
  12938. (setq current (delete tg current))
  12939. (push tg current)))
  12940. (if exit-after-next (setq exit-after-next 'now)))
  12941. ((setq e (rassoc c todo-table) tg (car e))
  12942. (with-current-buffer buf
  12943. (save-excursion (org-todo tg)))
  12944. (if exit-after-next (setq exit-after-next 'now)))
  12945. ((setq e (rassoc c ntable) tg (car e))
  12946. (if (member tg current)
  12947. (setq current (delete tg current))
  12948. (loop for g in groups do
  12949. (if (member tg g)
  12950. (mapc (lambda (x)
  12951. (setq current (delete x current)))
  12952. g)))
  12953. (push tg current))
  12954. (if exit-after-next (setq exit-after-next 'now))))
  12955. ;; Create a sorted list
  12956. (setq current
  12957. (sort current
  12958. (lambda (a b)
  12959. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12960. (if (eq exit-after-next 'now) (throw 'exit t))
  12961. (goto-char (point-min))
  12962. (beginning-of-line 2)
  12963. (delete-region (point) (point-at-eol))
  12964. (org-fast-tag-insert "Current" current c-face)
  12965. (org-set-current-tags-overlay current ov-prefix)
  12966. (while (re-search-forward
  12967. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12968. (setq tg (match-string 1))
  12969. (add-text-properties
  12970. (match-beginning 1) (match-end 1)
  12971. (list 'face
  12972. (cond
  12973. ((member tg current) c-face)
  12974. ((member tg inherited) i-face)
  12975. (t (get-text-property (match-beginning 1) 'face))))))
  12976. (goto-char (point-min)))))
  12977. (org-detach-overlay org-tags-overlay)
  12978. (if rtn
  12979. (mapconcat 'identity current ":")
  12980. nil))))
  12981. (defun org-get-tags-string ()
  12982. "Get the TAGS string in the current headline."
  12983. (unless (org-at-heading-p t)
  12984. (error "Not on a heading"))
  12985. (save-excursion
  12986. (beginning-of-line 1)
  12987. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12988. (org-match-string-no-properties 1)
  12989. "")))
  12990. (defun org-get-tags ()
  12991. "Get the list of tags specified in the current headline."
  12992. (org-split-string (org-get-tags-string) ":"))
  12993. (defun org-get-buffer-tags ()
  12994. "Get a table of all tags used in the buffer, for completion."
  12995. (let (tags)
  12996. (save-excursion
  12997. (goto-char (point-min))
  12998. (while (re-search-forward
  12999. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13000. (when (equal (char-after (point-at-bol 0)) ?*)
  13001. (mapc (lambda (x) (add-to-list 'tags x))
  13002. (org-split-string (org-match-string-no-properties 1) ":")))))
  13003. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13004. (mapcar 'list tags)))
  13005. ;;;; The mapping API
  13006. (defun org-map-entries (func &optional match scope &rest skip)
  13007. "Call FUNC at each headline selected by MATCH in SCOPE.
  13008. FUNC is a function or a lisp form. The function will be called without
  13009. arguments, with the cursor positioned at the beginning of the headline.
  13010. The return values of all calls to the function will be collected and
  13011. returned as a list.
  13012. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13013. does not need to preserve point. After evaluation, the cursor will be
  13014. moved to the end of the line (presumably of the headline of the
  13015. processed entry) and search continues from there. Under some
  13016. circumstances, this may not produce the wanted results. For example,
  13017. if you have removed (e.g. archived) the current (sub)tree it could
  13018. mean that the next entry will be skipped entirely. In such cases, you
  13019. can specify the position from where search should continue by making
  13020. FUNC set the variable `org-map-continue-from' to the desired buffer
  13021. position.
  13022. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13023. Only headlines that are matched by this query will be considered during
  13024. the iteration. When MATCH is nil or t, all headlines will be
  13025. visited by the iteration.
  13026. SCOPE determines the scope of this command. It can be any of:
  13027. nil The current buffer, respecting the restriction if any
  13028. tree The subtree started with the entry at point
  13029. region The entries within the active region, if any
  13030. region-start-level
  13031. The entries within the active region, but only those at
  13032. the same level than the first one.
  13033. file The current buffer, without restriction
  13034. file-with-archives
  13035. The current buffer, and any archives associated with it
  13036. agenda All agenda files
  13037. agenda-with-archives
  13038. All agenda files with any archive files associated with them
  13039. \(file1 file2 ...)
  13040. If this is a list, all files in the list will be scanned
  13041. The remaining args are treated as settings for the skipping facilities of
  13042. the scanner. The following items can be given here:
  13043. archive skip trees with the archive tag.
  13044. comment skip trees with the COMMENT keyword
  13045. function or Emacs Lisp form:
  13046. will be used as value for `org-agenda-skip-function', so whenever
  13047. the function returns t, FUNC will not be called for that
  13048. entry and search will continue from the point where the
  13049. function leaves it.
  13050. If your function needs to retrieve the tags including inherited tags
  13051. at the *current* entry, you can use the value of the variable
  13052. `org-scanner-tags' which will be much faster than getting the value
  13053. with `org-get-tags-at'. If your function gets properties with
  13054. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13055. to t around the call to `org-entry-properties' to get the same speedup.
  13056. Note that if your function moves around to retrieve tags and properties at
  13057. a *different* entry, you cannot use these techniques."
  13058. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13059. (not (org-region-active-p)))
  13060. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13061. (org-agenda-skip-archived-trees (memq 'archive skip))
  13062. (org-agenda-skip-comment-trees (memq 'comment skip))
  13063. (org-agenda-skip-function
  13064. (car (org-delete-all '(comment archive) skip)))
  13065. (org-tags-match-list-sublevels t)
  13066. (start-level (eq scope 'region-start-level))
  13067. matcher file res
  13068. org-todo-keywords-for-agenda
  13069. org-done-keywords-for-agenda
  13070. org-todo-keyword-alist-for-agenda
  13071. org-drawers-for-agenda
  13072. org-tag-alist-for-agenda
  13073. todo-only)
  13074. (cond
  13075. ((eq match t) (setq matcher t))
  13076. ((eq match nil) (setq matcher t))
  13077. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13078. (save-excursion
  13079. (save-restriction
  13080. (cond ((eq scope 'tree)
  13081. (org-back-to-heading t)
  13082. (org-narrow-to-subtree)
  13083. (setq scope nil))
  13084. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13085. (org-region-active-p))
  13086. ;; If needed, set start-level to a string like "2"
  13087. (when start-level
  13088. (save-excursion
  13089. (goto-char (region-beginning))
  13090. (unless (org-at-heading-p) (outline-next-heading))
  13091. (setq start-level (org-current-level))))
  13092. (narrow-to-region (region-beginning)
  13093. (save-excursion
  13094. (goto-char (region-end))
  13095. (unless (and (bolp) (org-at-heading-p))
  13096. (outline-next-heading))
  13097. (point)))
  13098. (setq scope nil)))
  13099. (if (not scope)
  13100. (progn
  13101. (org-agenda-prepare-buffers
  13102. (list (buffer-file-name (current-buffer))))
  13103. (setq res (org-scan-tags func matcher todo-only start-level)))
  13104. ;; Get the right scope
  13105. (cond
  13106. ((and scope (listp scope) (symbolp (car scope)))
  13107. (setq scope (eval scope)))
  13108. ((eq scope 'agenda)
  13109. (setq scope (org-agenda-files t)))
  13110. ((eq scope 'agenda-with-archives)
  13111. (setq scope (org-agenda-files t))
  13112. (setq scope (org-add-archive-files scope)))
  13113. ((eq scope 'file)
  13114. (setq scope (list (buffer-file-name))))
  13115. ((eq scope 'file-with-archives)
  13116. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13117. (org-agenda-prepare-buffers scope)
  13118. (while (setq file (pop scope))
  13119. (with-current-buffer (org-find-base-buffer-visiting file)
  13120. (save-excursion
  13121. (save-restriction
  13122. (widen)
  13123. (goto-char (point-min))
  13124. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13125. res)))
  13126. ;;;; Properties
  13127. ;;; Setting and retrieving properties
  13128. (defconst org-special-properties
  13129. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13130. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13131. "The special properties valid in Org-mode.
  13132. These are properties that are not defined in the property drawer,
  13133. but in some other way.")
  13134. (defconst org-default-properties
  13135. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13136. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13137. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13138. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13139. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13140. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13141. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13142. "Some properties that are used by Org-mode for various purposes.
  13143. Being in this list makes sure that they are offered for completion.")
  13144. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13145. "Regular expression matching the first line of a property drawer.")
  13146. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13147. "Regular expression matching the last line of a property drawer.")
  13148. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13149. "Regular expression matching the first line of a property drawer.")
  13150. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13151. "Regular expression matching the first line of a property drawer.")
  13152. (defconst org-property-drawer-re
  13153. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13154. org-property-end-re "\\)\n?")
  13155. "Matches an entire property drawer.")
  13156. (defconst org-clock-drawer-re
  13157. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13158. org-property-end-re "\\)\n?")
  13159. "Matches an entire clock drawer.")
  13160. (defsubst org-re-property (property)
  13161. "Return a regexp matching a PROPERTY line.
  13162. Match group 1 will be set to the value."
  13163. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13164. (defsubst org-re-property-keyword (property)
  13165. "Return a regexp matching a PROPERTY line, possibly with no
  13166. value for the property."
  13167. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13168. (defun org-property-action ()
  13169. "Do an action on properties."
  13170. (interactive)
  13171. (let (c)
  13172. (org-at-property-p)
  13173. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13174. (setq c (read-char-exclusive))
  13175. (cond
  13176. ((equal c ?s)
  13177. (call-interactively 'org-set-property))
  13178. ((equal c ?d)
  13179. (call-interactively 'org-delete-property))
  13180. ((equal c ?D)
  13181. (call-interactively 'org-delete-property-globally))
  13182. ((equal c ?c)
  13183. (call-interactively 'org-compute-property-at-point))
  13184. (t (error "No such property action %c" c)))))
  13185. (defun org-inc-effort ()
  13186. "Increment the value of the effort property in the current entry."
  13187. (interactive)
  13188. (org-set-effort nil t))
  13189. (defun org-set-effort (&optional value increment)
  13190. "Set the effort property of the current entry.
  13191. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13192. 10th allowed value.
  13193. When INCREMENT is non-nil, set the property to the next allowed value."
  13194. (interactive "P")
  13195. (if (equal value 0) (setq value 10))
  13196. (let* ((completion-ignore-case t)
  13197. (prop org-effort-property)
  13198. (cur (org-entry-get nil prop))
  13199. (allowed (org-property-get-allowed-values nil prop 'table))
  13200. (existing (mapcar 'list (org-property-values prop)))
  13201. rpl
  13202. (val (cond
  13203. ((stringp value) value)
  13204. ((and allowed (integerp value))
  13205. (or (car (nth (1- value) allowed))
  13206. (car (org-last allowed))))
  13207. ((and allowed increment)
  13208. (or (caadr (member (list cur) allowed))
  13209. (error "Allowed effort values are not set")))
  13210. (allowed
  13211. (message "Select 1-9,0, [RET%s]: %s"
  13212. (if cur (concat "=" cur) "")
  13213. (mapconcat 'car allowed " "))
  13214. (setq rpl (read-char-exclusive))
  13215. (if (equal rpl ?\r)
  13216. cur
  13217. (setq rpl (- rpl ?0))
  13218. (if (equal rpl 0) (setq rpl 10))
  13219. (if (and (> rpl 0) (<= rpl (length allowed)))
  13220. (car (nth (1- rpl) allowed))
  13221. (org-completing-read "Effort: " allowed nil))))
  13222. (t
  13223. (let (org-completion-use-ido org-completion-use-iswitchb)
  13224. (org-completing-read
  13225. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13226. (concat "[" cur "]") "")
  13227. ": ")
  13228. existing nil nil "" nil cur))))))
  13229. (unless (equal (org-entry-get nil prop) val)
  13230. (org-entry-put nil prop val))
  13231. (message "%s is now %s" prop val)))
  13232. (defun org-at-property-p ()
  13233. "Is cursor inside a property drawer?"
  13234. (save-excursion
  13235. (beginning-of-line 1)
  13236. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13237. (save-match-data ;; Used by calling procedures
  13238. (let ((p (point))
  13239. (range (unless (org-before-first-heading-p)
  13240. (org-get-property-block))))
  13241. (and range (<= (car range) p) (< p (cdr range))))))))
  13242. (defun org-get-property-block (&optional beg end force)
  13243. "Return the (beg . end) range of the body of the property drawer.
  13244. BEG and END are the beginning and end of the current subtree, or of
  13245. the part before the first headline. If they are not given, they will
  13246. be found. If the drawer does not exist and FORCE is non-nil, create
  13247. the drawer."
  13248. (catch 'exit
  13249. (save-excursion
  13250. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13251. (progn (org-back-to-heading t) (point))))
  13252. (end (or end (and (not (outline-next-heading)) (point-max))
  13253. (point))))
  13254. (goto-char beg)
  13255. (if (re-search-forward org-property-start-re end t)
  13256. (setq beg (1+ (match-end 0)))
  13257. (if force
  13258. (save-excursion
  13259. (org-insert-property-drawer)
  13260. (setq end (progn (outline-next-heading) (point))))
  13261. (throw 'exit nil))
  13262. (goto-char beg)
  13263. (if (re-search-forward org-property-start-re end t)
  13264. (setq beg (1+ (match-end 0)))))
  13265. (if (re-search-forward org-property-end-re end t)
  13266. (setq end (match-beginning 0))
  13267. (or force (throw 'exit nil))
  13268. (goto-char beg)
  13269. (setq end beg)
  13270. (org-indent-line)
  13271. (insert ":END:\n"))
  13272. (cons beg end)))))
  13273. (defun org-entry-properties (&optional pom which specific)
  13274. "Get all properties of the entry at point-or-marker POM.
  13275. This includes the TODO keyword, the tags, time strings for deadline,
  13276. scheduled, and clocking, and any additional properties defined in the
  13277. entry. The return value is an alist, keys may occur multiple times
  13278. if the property key was used several times.
  13279. POM may also be nil, in which case the current entry is used.
  13280. If WHICH is nil or `all', get all properties. If WHICH is
  13281. `special' or `standard', only get that subclass. If WHICH
  13282. is a string only get exactly this property. SPECIFIC can be a string, the
  13283. specific property we are interested in. Specifying it can speed
  13284. things up because then unnecessary parsing is avoided."
  13285. (setq which (or which 'all))
  13286. (org-with-point-at pom
  13287. (let ((clockstr (substring org-clock-string 0 -1))
  13288. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13289. (case-fold-search nil)
  13290. beg end range props sum-props key key1 value string clocksum clocksumt)
  13291. (save-excursion
  13292. (when (condition-case nil
  13293. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13294. (error nil))
  13295. (setq beg (point))
  13296. (setq sum-props (get-text-property (point) 'org-summaries))
  13297. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13298. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13299. (outline-next-heading)
  13300. (setq end (point))
  13301. (when (memq which '(all special))
  13302. ;; Get the special properties, like TODO and tags
  13303. (goto-char beg)
  13304. (when (and (or (not specific) (string= specific "TODO"))
  13305. (looking-at org-todo-line-regexp) (match-end 2))
  13306. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13307. (when (and (or (not specific) (string= specific "PRIORITY"))
  13308. (looking-at org-priority-regexp))
  13309. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13310. (when (or (not specific) (string= specific "FILE"))
  13311. (push (cons "FILE" buffer-file-name) props))
  13312. (when (and (or (not specific) (string= specific "TAGS"))
  13313. (setq value (org-get-tags-string))
  13314. (string-match "\\S-" value))
  13315. (push (cons "TAGS" value) props))
  13316. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13317. (setq value (org-get-tags-at)))
  13318. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13319. ":"))
  13320. props))
  13321. (when (or (not specific) (string= specific "BLOCKED"))
  13322. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13323. (when (or (not specific)
  13324. (member specific
  13325. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13326. "TIMESTAMP" "TIMESTAMP_IA")))
  13327. (catch 'match
  13328. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13329. (setq key (if (match-end 1)
  13330. (substring (org-match-string-no-properties 1)
  13331. 0 -1))
  13332. string (if (equal key clockstr)
  13333. (org-trim
  13334. (buffer-substring-no-properties
  13335. (match-beginning 3) (goto-char
  13336. (point-at-eol))))
  13337. (substring (org-match-string-no-properties 3)
  13338. 1 -1)))
  13339. ;; Get the correct property name from the key. This is
  13340. ;; necessary if the user has configured time keywords.
  13341. (setq key1 (concat key ":"))
  13342. (cond
  13343. ((not key)
  13344. (setq key
  13345. (if (= (char-after (match-beginning 3)) ?\[)
  13346. "TIMESTAMP_IA" "TIMESTAMP")))
  13347. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13348. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13349. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13350. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13351. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13352. (progn
  13353. (push (cons key string) props)
  13354. ;; no need to search further if match is found
  13355. (throw 'match t))
  13356. (when (or (equal key "CLOCK") (not (assoc key props)))
  13357. (push (cons key string) props)))))))
  13358. (when (memq which '(all standard))
  13359. ;; Get the standard properties, like :PROP: ...
  13360. (setq range (org-get-property-block beg end))
  13361. (when range
  13362. (goto-char (car range))
  13363. (while (re-search-forward
  13364. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13365. (cdr range) t)
  13366. (setq key (org-match-string-no-properties 1)
  13367. value (org-trim (or (org-match-string-no-properties 2) "")))
  13368. (unless (member key excluded)
  13369. (push (cons key (or value "")) props)))))
  13370. (if clocksum
  13371. (push (cons "CLOCKSUM"
  13372. (org-columns-number-to-string (/ (float clocksum) 60.)
  13373. 'add_times))
  13374. props))
  13375. (if clocksumt
  13376. (push (cons "CLOCKSUM_T"
  13377. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13378. 'add_times))
  13379. props))
  13380. (unless (assoc "CATEGORY" props)
  13381. (push (cons "CATEGORY" (org-get-category)) props))
  13382. (append sum-props (nreverse props)))))))
  13383. (defun org-entry-get (pom property &optional inherit literal-nil)
  13384. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13385. If INHERIT is non-nil and the entry does not have the property,
  13386. then also check higher levels of the hierarchy.
  13387. If INHERIT is the symbol `selective', use inheritance only if the setting
  13388. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13389. If the property is present but empty, the return value is the empty string.
  13390. If the property is not present at all, nil is returned.
  13391. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13392. do not interpret it as the list atom nil. This is used for inheritance
  13393. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13394. (org-with-point-at pom
  13395. (if (and inherit (if (eq inherit 'selective)
  13396. (org-property-inherit-p property)
  13397. t))
  13398. (org-entry-get-with-inheritance property literal-nil)
  13399. (if (member property org-special-properties)
  13400. ;; We need a special property. Use `org-entry-properties' to
  13401. ;; retrieve it, but specify the wanted property
  13402. (cdr (assoc property (org-entry-properties nil 'special property)))
  13403. (let* ((range (org-get-property-block))
  13404. (props (list (or (assoc property org-file-properties)
  13405. (assoc property org-global-properties)
  13406. (assoc property org-global-properties-fixed))))
  13407. (ap (lambda (key)
  13408. (when (re-search-forward
  13409. (org-re-property key) (cdr range) t)
  13410. (setq props
  13411. (org-update-property-plist
  13412. key
  13413. (if (match-end 1)
  13414. (org-match-string-no-properties 1) "")
  13415. props)))))
  13416. val)
  13417. (when (and range (goto-char (car range)))
  13418. (funcall ap property)
  13419. (goto-char (car range))
  13420. (while (funcall ap (concat property "+")))
  13421. (setq val (cdr (assoc property props)))
  13422. (when val (if literal-nil val (org-not-nil val)))))))))
  13423. (defun org-property-or-variable-value (var &optional inherit)
  13424. "Check if there is a property fixing the value of VAR.
  13425. If yes, return this value. If not, return the current value of the variable."
  13426. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13427. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13428. (read prop)
  13429. (symbol-value var))))
  13430. (defun org-entry-delete (pom property)
  13431. "Delete the property PROPERTY from entry at point-or-marker POM."
  13432. (org-with-point-at pom
  13433. (if (member property org-special-properties)
  13434. nil ; cannot delete these properties.
  13435. (let ((range (org-get-property-block)))
  13436. (if (and range
  13437. (goto-char (car range))
  13438. (re-search-forward
  13439. (org-re-property property)
  13440. (cdr range) t))
  13441. (progn
  13442. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13443. t)
  13444. nil)))))
  13445. ;; Multi-values properties are properties that contain multiple values
  13446. ;; These values are assumed to be single words, separated by whitespace.
  13447. (defun org-entry-add-to-multivalued-property (pom property value)
  13448. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13449. (let* ((old (org-entry-get pom property))
  13450. (values (and old (org-split-string old "[ \t]"))))
  13451. (setq value (org-entry-protect-space value))
  13452. (unless (member value values)
  13453. (setq values (cons value values))
  13454. (org-entry-put pom property
  13455. (mapconcat 'identity values " ")))))
  13456. (defun org-entry-remove-from-multivalued-property (pom property value)
  13457. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13458. (let* ((old (org-entry-get pom property))
  13459. (values (and old (org-split-string old "[ \t]"))))
  13460. (setq value (org-entry-protect-space value))
  13461. (when (member value values)
  13462. (setq values (delete value values))
  13463. (org-entry-put pom property
  13464. (mapconcat 'identity values " ")))))
  13465. (defun org-entry-member-in-multivalued-property (pom property value)
  13466. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13467. (let* ((old (org-entry-get pom property))
  13468. (values (and old (org-split-string old "[ \t]"))))
  13469. (setq value (org-entry-protect-space value))
  13470. (member value values)))
  13471. (defun org-entry-get-multivalued-property (pom property)
  13472. "Return a list of values in a multivalued property."
  13473. (let* ((value (org-entry-get pom property))
  13474. (values (and value (org-split-string value "[ \t]"))))
  13475. (mapcar 'org-entry-restore-space values)))
  13476. (defun org-entry-put-multivalued-property (pom property &rest values)
  13477. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13478. VALUES should be a list of strings. Spaces will be protected."
  13479. (org-entry-put pom property
  13480. (mapconcat 'org-entry-protect-space values " "))
  13481. (let* ((value (org-entry-get pom property))
  13482. (values (and value (org-split-string value "[ \t]"))))
  13483. (mapcar 'org-entry-restore-space values)))
  13484. (defun org-entry-protect-space (s)
  13485. "Protect spaces and newline in string S."
  13486. (while (string-match " " s)
  13487. (setq s (replace-match "%20" t t s)))
  13488. (while (string-match "\n" s)
  13489. (setq s (replace-match "%0A" t t s)))
  13490. s)
  13491. (defun org-entry-restore-space (s)
  13492. "Restore spaces and newline in string S."
  13493. (while (string-match "%20" s)
  13494. (setq s (replace-match " " t t s)))
  13495. (while (string-match "%0A" s)
  13496. (setq s (replace-match "\n" t t s)))
  13497. s)
  13498. (defvar org-entry-property-inherited-from (make-marker)
  13499. "Marker pointing to the entry from where a property was inherited.
  13500. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13501. location of the entry where the inheritance search matched. If there was
  13502. no match, the marker will point nowhere.
  13503. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13504. is set.")
  13505. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13506. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13507. The search will stop at the first ancestor which has the property defined.
  13508. If the value found is \"nil\", return nil to show that the property
  13509. should be considered as undefined (this is the meaning of nil here).
  13510. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13511. (move-marker org-entry-property-inherited-from nil)
  13512. (let (tmp)
  13513. (save-excursion
  13514. (save-restriction
  13515. (widen)
  13516. (catch 'ex
  13517. (while t
  13518. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13519. (or (ignore-errors (org-back-to-heading t))
  13520. (goto-char (point-min)))
  13521. (move-marker org-entry-property-inherited-from (point))
  13522. (throw 'ex tmp))
  13523. (or (ignore-errors (org-up-heading-safe))
  13524. (throw 'ex nil))))))
  13525. (setq tmp (or tmp
  13526. (cdr (assoc property org-file-properties))
  13527. (cdr (assoc property org-global-properties))
  13528. (cdr (assoc property org-global-properties-fixed))))
  13529. (if literal-nil tmp (org-not-nil tmp))))
  13530. (defvar org-property-changed-functions nil
  13531. "Hook called when the value of a property has changed.
  13532. Each hook function should accept two arguments, the name of the property
  13533. and the new value.")
  13534. (defun org-entry-put (pom property value)
  13535. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13536. (org-with-point-at pom
  13537. (org-back-to-heading t)
  13538. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13539. range)
  13540. (cond
  13541. ((equal property "TODO")
  13542. (when (and (stringp value) (string-match "\\S-" value)
  13543. (not (member value org-todo-keywords-1)))
  13544. (error "\"%s\" is not a valid TODO state" value))
  13545. (if (or (not value)
  13546. (not (string-match "\\S-" value)))
  13547. (setq value 'none))
  13548. (org-todo value)
  13549. (org-set-tags nil 'align))
  13550. ((equal property "PRIORITY")
  13551. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13552. (string-to-char value) ?\ ))
  13553. (org-set-tags nil 'align))
  13554. ((equal property "CLOCKSUM")
  13555. (if (not (re-search-forward
  13556. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13557. (error "Cannot find a clock log")
  13558. (goto-char (- (match-end 1) 2))
  13559. (cond
  13560. ((eq value 'earlier) (org-timestamp-down))
  13561. ((eq value 'later) (org-timestamp-up)))
  13562. (org-clock-sum-current-item)))
  13563. ((equal property "SCHEDULED")
  13564. (if (re-search-forward org-scheduled-time-regexp end t)
  13565. (cond
  13566. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13567. ((eq value 'later) (org-timestamp-change 1 'day))
  13568. (t (call-interactively 'org-schedule)))
  13569. (call-interactively 'org-schedule)))
  13570. ((equal property "DEADLINE")
  13571. (if (re-search-forward org-deadline-time-regexp end t)
  13572. (cond
  13573. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13574. ((eq value 'later) (org-timestamp-change 1 'day))
  13575. (t (call-interactively 'org-deadline)))
  13576. (call-interactively 'org-deadline)))
  13577. ((member property org-special-properties)
  13578. (error "The %s property can not yet be set with `org-entry-put'"
  13579. property))
  13580. (t ; a non-special property
  13581. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13582. (setq range (org-get-property-block beg end 'force))
  13583. (goto-char (car range))
  13584. (if (re-search-forward
  13585. (org-re-property-keyword property) (cdr range) t)
  13586. (progn
  13587. (delete-region (match-beginning 0) (match-end 0))
  13588. (goto-char (match-beginning 0)))
  13589. (goto-char (cdr range))
  13590. (insert "\n")
  13591. (backward-char 1)
  13592. (org-indent-line))
  13593. (insert ":" property ":")
  13594. (and value (insert " " value))
  13595. (org-indent-line)))))
  13596. (run-hook-with-args 'org-property-changed-functions property value)))
  13597. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13598. "Get all property keys in the current buffer.
  13599. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13600. like tags and TODO state.
  13601. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13602. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13603. and others.
  13604. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13605. formats in the current buffer."
  13606. (let (rtn range cfmt s p)
  13607. (save-excursion
  13608. (save-restriction
  13609. (widen)
  13610. (goto-char (point-min))
  13611. (while (re-search-forward org-property-start-re nil t)
  13612. (setq range (org-get-property-block))
  13613. (goto-char (car range))
  13614. (while (re-search-forward
  13615. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13616. (cdr range) t)
  13617. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13618. (outline-next-heading))))
  13619. (when include-specials
  13620. (setq rtn (append org-special-properties rtn)))
  13621. (when include-defaults
  13622. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13623. (add-to-list 'rtn org-effort-property))
  13624. (when include-columns
  13625. (save-excursion
  13626. (save-restriction
  13627. (widen)
  13628. (goto-char (point-min))
  13629. (while (re-search-forward
  13630. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13631. nil t)
  13632. (setq cfmt (match-string 2) s 0)
  13633. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13634. cfmt s)
  13635. (setq s (match-end 0)
  13636. p (match-string 1 cfmt))
  13637. (unless (or (equal p "ITEM")
  13638. (member p org-special-properties))
  13639. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13640. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13641. (defun org-property-values (key)
  13642. "Return a list of all values of property KEY in the current buffer."
  13643. (save-excursion
  13644. (save-restriction
  13645. (widen)
  13646. (goto-char (point-min))
  13647. (let ((re (org-re-property key))
  13648. values)
  13649. (while (re-search-forward re nil t)
  13650. (add-to-list 'values (org-trim (match-string 1))))
  13651. (delete "" values)))))
  13652. (defun org-insert-property-drawer ()
  13653. "Insert a property drawer into the current entry."
  13654. (org-back-to-heading t)
  13655. (looking-at org-outline-regexp)
  13656. (let ((indent (if org-adapt-indentation
  13657. (- (match-end 0) (match-beginning 0))
  13658. 0))
  13659. (beg (point))
  13660. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13661. end hiddenp)
  13662. (outline-next-heading)
  13663. (setq end (point))
  13664. (goto-char beg)
  13665. (while (re-search-forward re end t))
  13666. (setq hiddenp (outline-invisible-p))
  13667. (end-of-line 1)
  13668. (and (equal (char-after) ?\n) (forward-char 1))
  13669. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13670. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13671. ;; just skip this line
  13672. (beginning-of-line 2)
  13673. ;; Drawer start, find the end
  13674. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13675. (beginning-of-line 1)))
  13676. (org-skip-over-state-notes)
  13677. (skip-chars-backward " \t\n\r")
  13678. (if (eq (char-before) ?*) (forward-char 1))
  13679. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13680. (beginning-of-line 0)
  13681. (org-indent-to-column indent)
  13682. (beginning-of-line 2)
  13683. (org-indent-to-column indent)
  13684. (beginning-of-line 0)
  13685. (if hiddenp
  13686. (save-excursion
  13687. (org-back-to-heading t)
  13688. (hide-entry))
  13689. (org-flag-drawer t))))
  13690. (defun org-insert-drawer (&optional arg drawer)
  13691. "Insert a drawer at point.
  13692. Optional argument DRAWER, when non-nil, is a string representing
  13693. drawer's name. Otherwise, the user is prompted for a name.
  13694. If a region is active, insert the drawer around that region
  13695. instead.
  13696. Point is left between drawer's boundaries."
  13697. (interactive "P")
  13698. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13699. "LOGBOOK"))
  13700. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13701. ;; internally by Org. They are meant to be inserted
  13702. ;; automatically.
  13703. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13704. ;; Remove system drawers from list. Note: For some reason,
  13705. ;; `org-completing-read' ignores the predicate while
  13706. ;; `completing-read' handles it fine.
  13707. (drawer (if arg "PROPERTIES"
  13708. (or drawer
  13709. (completing-read
  13710. "Drawer: " org-drawers
  13711. (lambda (d) (not (member d system-drawers))))))))
  13712. (cond
  13713. ;; With C-u, fall back on `org-insert-property-drawer'
  13714. (arg (org-insert-property-drawer))
  13715. ;; With an active region, insert a drawer at point.
  13716. ((not (org-region-active-p))
  13717. (progn
  13718. (unless (bolp) (insert "\n"))
  13719. (insert (format ":%s:\n\n:END:\n" drawer))
  13720. (forward-line -2)))
  13721. ;; Otherwise, insert the drawer at point
  13722. (t
  13723. (let ((rbeg (region-beginning))
  13724. (rend (copy-marker (region-end))))
  13725. (unwind-protect
  13726. (progn
  13727. (goto-char rbeg)
  13728. (beginning-of-line)
  13729. (when (save-excursion
  13730. (re-search-forward org-outline-regexp-bol rend t))
  13731. (error "Drawers cannot contain headlines"))
  13732. ;; Position point at the beginning of the first
  13733. ;; non-blank line in region. Insert drawer's opening
  13734. ;; there, then indent it.
  13735. (org-skip-whitespace)
  13736. (beginning-of-line)
  13737. (insert ":" drawer ":\n")
  13738. (forward-line -1)
  13739. (indent-for-tab-command)
  13740. ;; Move point to the beginning of the first blank line
  13741. ;; after the last non-blank line in region. Insert
  13742. ;; drawer's closing, then indent it.
  13743. (goto-char rend)
  13744. (skip-chars-backward " \r\t\n")
  13745. (insert "\n:END:")
  13746. (deactivate-mark t)
  13747. (indent-for-tab-command)
  13748. (unless (eolp) (insert "\n")))
  13749. ;; Clear marker, whatever the outcome of insertion is.
  13750. (set-marker rend nil)))))))
  13751. (defvar org-property-set-functions-alist nil
  13752. "Property set function alist.
  13753. Each entry should have the following format:
  13754. (PROPERTY . READ-FUNCTION)
  13755. The read function will be called with the same argument as
  13756. `org-completing-read'.")
  13757. (defun org-set-property-function (property)
  13758. "Get the function that should be used to set PROPERTY.
  13759. This is computed according to `org-property-set-functions-alist'."
  13760. (or (cdr (assoc property org-property-set-functions-alist))
  13761. 'org-completing-read))
  13762. (defun org-read-property-value (property)
  13763. "Read PROPERTY value from user."
  13764. (let* ((completion-ignore-case t)
  13765. (allowed (org-property-get-allowed-values nil property 'table))
  13766. (cur (org-entry-get nil property))
  13767. (prompt (concat property " value"
  13768. (if (and cur (string-match "\\S-" cur))
  13769. (concat " [" cur "]") "") ": "))
  13770. (set-function (org-set-property-function property))
  13771. (val (if allowed
  13772. (funcall set-function prompt allowed nil
  13773. (not (get-text-property 0 'org-unrestricted
  13774. (caar allowed))))
  13775. (let (org-completion-use-ido org-completion-use-iswitchb)
  13776. (funcall set-function prompt
  13777. (mapcar 'list (org-property-values property))
  13778. nil nil "" nil cur)))))
  13779. (if (equal val "")
  13780. cur
  13781. val)))
  13782. (defvar org-last-set-property nil)
  13783. (defvar org-last-set-property-value nil)
  13784. (defun org-read-property-name ()
  13785. "Read a property name."
  13786. (let* ((completion-ignore-case t)
  13787. (keys (org-buffer-property-keys nil t t))
  13788. (default-prop (or (save-excursion
  13789. (save-match-data
  13790. (beginning-of-line)
  13791. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13792. (null (string= (match-string 1) "END"))
  13793. (match-string 1))))
  13794. org-last-set-property))
  13795. (property (org-icompleting-read
  13796. (concat "Property"
  13797. (if default-prop (concat " [" default-prop "]") "")
  13798. ": ")
  13799. (mapcar 'list keys)
  13800. nil nil nil nil
  13801. default-prop)))
  13802. (if (member property keys)
  13803. property
  13804. (or (cdr (assoc (downcase property)
  13805. (mapcar (lambda (x) (cons (downcase x) x))
  13806. keys)))
  13807. property))))
  13808. (defun org-set-property-and-value (use-last)
  13809. "Allow to set [PROPERTY]: [value] direction from prompt.
  13810. When use-default, don't even ask, just use the last
  13811. \"[PROPERTY]: [value]\" string from the history."
  13812. (interactive "P")
  13813. (let* ((completion-ignore-case t)
  13814. (pv (or (and use-last org-last-set-property-value)
  13815. (org-completing-read
  13816. "Enter a \"[Property]: [value]\" pair: "
  13817. nil nil nil nil nil
  13818. org-last-set-property-value)))
  13819. prop val)
  13820. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13821. (setq prop (match-string 1 pv)
  13822. val (match-string 2 pv))
  13823. (org-set-property prop val))))
  13824. (defun org-set-property (property value)
  13825. "In the current entry, set PROPERTY to VALUE.
  13826. When called interactively, this will prompt for a property name, offering
  13827. completion on existing and default properties. And then it will prompt
  13828. for a value, offering completion either on allowed values (via an inherited
  13829. xxx_ALL property) or on existing values in other instances of this property
  13830. in the current file."
  13831. (interactive (list nil nil))
  13832. (let* ((property (or property (org-read-property-name)))
  13833. (value (or value (org-read-property-value property)))
  13834. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13835. (setq org-last-set-property property)
  13836. (setq org-last-set-property-value (concat property ": " value))
  13837. ;; Possibly postprocess the inserted value:
  13838. (when fn (setq value (funcall fn value)))
  13839. (unless (equal (org-entry-get nil property) value)
  13840. (org-entry-put nil property value))))
  13841. (defun org-delete-property (property)
  13842. "In the current entry, delete PROPERTY."
  13843. (interactive
  13844. (let* ((completion-ignore-case t)
  13845. (prop (org-icompleting-read "Property: "
  13846. (org-entry-properties nil 'standard))))
  13847. (list prop)))
  13848. (message "Property %s %s" property
  13849. (if (org-entry-delete nil property)
  13850. "deleted"
  13851. "was not present in the entry")))
  13852. (defun org-delete-property-globally (property)
  13853. "Remove PROPERTY globally, from all entries."
  13854. (interactive
  13855. (let* ((completion-ignore-case t)
  13856. (prop (org-icompleting-read
  13857. "Globally remove property: "
  13858. (mapcar 'list (org-buffer-property-keys)))))
  13859. (list prop)))
  13860. (save-excursion
  13861. (save-restriction
  13862. (widen)
  13863. (goto-char (point-min))
  13864. (let ((cnt 0))
  13865. (while (re-search-forward
  13866. (org-re-property property)
  13867. nil t)
  13868. (setq cnt (1+ cnt))
  13869. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13870. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13871. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13872. (defun org-compute-property-at-point ()
  13873. "Compute the property at point.
  13874. This looks for an enclosing column format, extracts the operator and
  13875. then applies it to the property in the column format's scope."
  13876. (interactive)
  13877. (unless (org-at-property-p)
  13878. (error "Not at a property"))
  13879. (let ((prop (org-match-string-no-properties 2)))
  13880. (org-columns-get-format-and-top-level)
  13881. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13882. (error "No operator defined for property %s" prop))
  13883. (org-columns-compute prop)))
  13884. (defvar org-property-allowed-value-functions nil
  13885. "Hook for functions supplying allowed values for a specific property.
  13886. The functions must take a single argument, the name of the property, and
  13887. return a flat list of allowed values. If \":ETC\" is one of
  13888. the values, this means that these values are intended as defaults for
  13889. completion, but that other values should be allowed too.
  13890. The functions must return nil if they are not responsible for this
  13891. property.")
  13892. (defun org-property-get-allowed-values (pom property &optional table)
  13893. "Get allowed values for the property PROPERTY.
  13894. When TABLE is non-nil, return an alist that can directly be used for
  13895. completion."
  13896. (let (vals)
  13897. (cond
  13898. ((equal property "TODO")
  13899. (setq vals (org-with-point-at pom
  13900. (append org-todo-keywords-1 '("")))))
  13901. ((equal property "PRIORITY")
  13902. (let ((n org-lowest-priority))
  13903. (while (>= n org-highest-priority)
  13904. (push (char-to-string n) vals)
  13905. (setq n (1- n)))))
  13906. ((member property org-special-properties))
  13907. ((setq vals (run-hook-with-args-until-success
  13908. 'org-property-allowed-value-functions property)))
  13909. (t
  13910. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13911. (when (and vals (string-match "\\S-" vals))
  13912. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13913. (setq vals (mapcar (lambda (x)
  13914. (cond ((stringp x) x)
  13915. ((numberp x) (number-to-string x))
  13916. ((symbolp x) (symbol-name x))
  13917. (t "???")))
  13918. vals)))))
  13919. (when (member ":ETC" vals)
  13920. (setq vals (remove ":ETC" vals))
  13921. (org-add-props (car vals) '(org-unrestricted t)))
  13922. (if table (mapcar 'list vals) vals)))
  13923. (defun org-property-previous-allowed-value (&optional previous)
  13924. "Switch to the next allowed value for this property."
  13925. (interactive)
  13926. (org-property-next-allowed-value t))
  13927. (defun org-property-next-allowed-value (&optional previous)
  13928. "Switch to the next allowed value for this property."
  13929. (interactive)
  13930. (unless (org-at-property-p)
  13931. (error "Not at a property"))
  13932. (let* ((key (match-string 2))
  13933. (value (match-string 3))
  13934. (allowed (or (org-property-get-allowed-values (point) key)
  13935. (and (member value '("[ ]" "[-]" "[X]"))
  13936. '("[ ]" "[X]"))))
  13937. nval)
  13938. (unless allowed
  13939. (error "Allowed values for this property have not been defined"))
  13940. (if previous (setq allowed (reverse allowed)))
  13941. (if (member value allowed)
  13942. (setq nval (car (cdr (member value allowed)))))
  13943. (setq nval (or nval (car allowed)))
  13944. (if (equal nval value)
  13945. (error "Only one allowed value for this property"))
  13946. (org-at-property-p)
  13947. (replace-match (concat " :" key ": " nval) t t)
  13948. (org-indent-line)
  13949. (beginning-of-line 1)
  13950. (skip-chars-forward " \t")
  13951. (run-hook-with-args 'org-property-changed-functions key nval)))
  13952. (defun org-find-olp (path &optional this-buffer)
  13953. "Return a marker pointing to the entry at outline path OLP.
  13954. If anything goes wrong, throw an error.
  13955. You can wrap this call to catch the error like this:
  13956. (condition-case msg
  13957. (org-mobile-locate-entry (match-string 4))
  13958. (error (nth 1 msg)))
  13959. The return value will then be either a string with the error message,
  13960. or a marker if everything is OK.
  13961. If THIS-BUFFER is set, the outline path does not contain a file,
  13962. only headings."
  13963. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13964. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13965. (level 1)
  13966. (lmin 1)
  13967. (lmax 1)
  13968. limit re end found pos heading cnt flevel)
  13969. (unless buffer (error "File not found :%s" file))
  13970. (with-current-buffer buffer
  13971. (save-excursion
  13972. (save-restriction
  13973. (widen)
  13974. (setq limit (point-max))
  13975. (goto-char (point-min))
  13976. (while (setq heading (pop path))
  13977. (setq re (format org-complex-heading-regexp-format
  13978. (regexp-quote heading)))
  13979. (setq cnt 0 pos (point))
  13980. (while (re-search-forward re end t)
  13981. (setq level (- (match-end 1) (match-beginning 1)))
  13982. (if (and (>= level lmin) (<= level lmax))
  13983. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13984. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13985. lmax heading))
  13986. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13987. lmax heading))
  13988. (goto-char found)
  13989. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13990. (setq end (save-excursion (org-end-of-subtree t t))))
  13991. (when (org-at-heading-p)
  13992. (point-marker)))))))
  13993. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13994. "Find node HEADING in BUFFER.
  13995. Return a marker to the heading if it was found, or nil if not.
  13996. If POS-ONLY is set, return just the position instead of a marker.
  13997. The heading text must match exact, but it may have a TODO keyword,
  13998. a priority cookie and tags in the standard locations."
  13999. (with-current-buffer (or buffer (current-buffer))
  14000. (save-excursion
  14001. (save-restriction
  14002. (widen)
  14003. (goto-char (point-min))
  14004. (let (case-fold-search)
  14005. (if (re-search-forward
  14006. (format org-complex-heading-regexp-format
  14007. (regexp-quote heading)) nil t)
  14008. (if pos-only
  14009. (match-beginning 0)
  14010. (move-marker (make-marker) (match-beginning 0)))))))))
  14011. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14012. "Find Org node headline HEADING in all .org files in directory DIR.
  14013. When the target headline is found, return a marker to this location."
  14014. (let ((files (directory-files (or dir default-directory)
  14015. nil "\\`[^.#].*\\.org\\'"))
  14016. file visiting m buffer)
  14017. (catch 'found
  14018. (while (setq file (pop files))
  14019. (message "trying %s" file)
  14020. (setq visiting (org-find-base-buffer-visiting file))
  14021. (setq buffer (or visiting (find-file-noselect file)))
  14022. (setq m (org-find-exact-headline-in-buffer
  14023. heading buffer))
  14024. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14025. (and m (throw 'found m))))))
  14026. (defun org-find-entry-with-id (ident)
  14027. "Locate the entry that contains the ID property with exact value IDENT.
  14028. IDENT can be a string, a symbol or a number, this function will search for
  14029. the string representation of it.
  14030. Return the position where this entry starts, or nil if there is no such entry."
  14031. (interactive "sID: ")
  14032. (let ((id (cond
  14033. ((stringp ident) ident)
  14034. ((symbol-name ident) (symbol-name ident))
  14035. ((numberp ident) (number-to-string ident))
  14036. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14037. (case-fold-search nil))
  14038. (save-excursion
  14039. (save-restriction
  14040. (widen)
  14041. (goto-char (point-min))
  14042. (when (re-search-forward
  14043. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14044. nil t)
  14045. (org-back-to-heading t)
  14046. (point))))))
  14047. ;;;; Timestamps
  14048. (defvar org-last-changed-timestamp nil)
  14049. (defvar org-last-inserted-timestamp nil
  14050. "The last time stamp inserted with `org-insert-time-stamp'.")
  14051. (defvar org-time-was-given) ; dynamically scoped parameter
  14052. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14053. (defvar org-ts-what) ; dynamically scoped parameter
  14054. (defun org-time-stamp (arg &optional inactive)
  14055. "Prompt for a date/time and insert a time stamp.
  14056. If the user specifies a time like HH:MM or if this command is
  14057. called with at least one prefix argument, the time stamp contains
  14058. the date and the time. Otherwise, only the date is be included.
  14059. All parts of a date not specified by the user is filled in from
  14060. the current date/time. So if you just press return without
  14061. typing anything, the time stamp will represent the current
  14062. date/time.
  14063. If there is already a timestamp at the cursor, it will be
  14064. modified.
  14065. With two universal prefix arguments, insert an active timestamp
  14066. with the current time without prompting the user."
  14067. (interactive "P")
  14068. (let* ((ts nil)
  14069. (default-time
  14070. ;; Default time is either today, or, when entering a range,
  14071. ;; the range start.
  14072. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14073. (save-excursion
  14074. (re-search-backward
  14075. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14076. (- (point) 20) t)))
  14077. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14078. (current-time)))
  14079. (default-input (and ts (org-get-compact-tod ts)))
  14080. (repeater (save-excursion
  14081. (save-match-data
  14082. (beginning-of-line)
  14083. (when (re-search-forward
  14084. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14085. (save-excursion (progn (end-of-line) (point))) t)
  14086. (match-string 0)))))
  14087. org-time-was-given org-end-time-was-given time)
  14088. (cond
  14089. ((and (org-at-timestamp-p t)
  14090. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14091. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14092. (insert "--")
  14093. (setq time (let ((this-command this-command))
  14094. (org-read-date arg 'totime nil nil
  14095. default-time default-input inactive)))
  14096. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14097. ((org-at-timestamp-p t)
  14098. (setq time (let ((this-command this-command))
  14099. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14100. (when (org-at-timestamp-p t) ; just to get the match data
  14101. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14102. (replace-match "")
  14103. (setq org-last-changed-timestamp
  14104. (org-insert-time-stamp
  14105. time (or org-time-was-given arg)
  14106. inactive nil nil (list org-end-time-was-given)))
  14107. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14108. (setq org-last-changed-timestamp
  14109. (concat (substring org-last-inserted-timestamp 0 -1)
  14110. " " repeater ">"))))
  14111. (message "Timestamp updated"))
  14112. ((equal arg '(16))
  14113. (org-insert-time-stamp (current-time) t))
  14114. (t
  14115. (setq time (let ((this-command this-command))
  14116. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14117. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14118. nil nil (list org-end-time-was-given))))))
  14119. ;; FIXME: can we use this for something else, like computing time differences?
  14120. (defun org-get-compact-tod (s)
  14121. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14122. (let* ((t1 (match-string 1 s))
  14123. (h1 (string-to-number (match-string 2 s)))
  14124. (m1 (string-to-number (match-string 3 s)))
  14125. (t2 (and (match-end 4) (match-string 5 s)))
  14126. (h2 (and t2 (string-to-number (match-string 6 s))))
  14127. (m2 (and t2 (string-to-number (match-string 7 s))))
  14128. dh dm)
  14129. (if (not t2)
  14130. t1
  14131. (setq dh (- h2 h1) dm (- m2 m1))
  14132. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14133. (concat t1 "+" (number-to-string dh)
  14134. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14135. (defun org-time-stamp-inactive (&optional arg)
  14136. "Insert an inactive time stamp.
  14137. An inactive time stamp is enclosed in square brackets instead of angle
  14138. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14139. does not link to the calendar and cannot be changed with the S-cursor keys.
  14140. So these are more for recording a certain time/date."
  14141. (interactive "P")
  14142. (org-time-stamp arg 'inactive))
  14143. (defvar org-date-ovl (make-overlay 1 1))
  14144. (overlay-put org-date-ovl 'face 'org-date-selected)
  14145. (org-detach-overlay org-date-ovl)
  14146. (defvar org-ans1) ; dynamically scoped parameter
  14147. (defvar org-ans2) ; dynamically scoped parameter
  14148. (defvar org-plain-time-of-day-regexp) ; defined below
  14149. (defvar org-overriding-default-time nil) ; dynamically scoped
  14150. (defvar org-read-date-overlay nil)
  14151. (defvar org-dcst nil) ; dynamically scoped
  14152. (defvar org-read-date-history nil)
  14153. (defvar org-read-date-final-answer nil)
  14154. (defvar org-read-date-analyze-futurep nil)
  14155. (defvar org-read-date-analyze-forced-year nil)
  14156. (defvar org-read-date-inactive)
  14157. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14158. default-time default-input inactive)
  14159. "Read a date, possibly a time, and make things smooth for the user.
  14160. The prompt will suggest to enter an ISO date, but you can also enter anything
  14161. which will at least partially be understood by `parse-time-string'.
  14162. Unrecognized parts of the date will default to the current day, month, year,
  14163. hour and minute. If this command is called to replace a timestamp at point,
  14164. or to enter the second timestamp of a range, the default time is taken
  14165. from the existing stamp. Furthermore, the command prefers the future,
  14166. so if you are giving a date where the year is not given, and the day-month
  14167. combination is already past in the current year, it will assume you
  14168. mean next year. For details, see the manual. A few examples:
  14169. 3-2-5 --> 2003-02-05
  14170. feb 15 --> currentyear-02-15
  14171. 2/15 --> currentyear-02-15
  14172. sep 12 9 --> 2009-09-12
  14173. 12:45 --> today 12:45
  14174. 22 sept 0:34 --> currentyear-09-22 0:34
  14175. 12 --> currentyear-currentmonth-12
  14176. Fri --> nearest Friday (today or later)
  14177. etc.
  14178. Furthermore you can specify a relative date by giving, as the *first* thing
  14179. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14180. change in days weeks, months, years.
  14181. With a single plus or minus, the date is relative to today. With a double
  14182. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14183. +4d --> four days from today
  14184. +4 --> same as above
  14185. +2w --> two weeks from today
  14186. ++5 --> five days from default date
  14187. The function understands only English month and weekday abbreviations.
  14188. While prompting, a calendar is popped up - you can also select the
  14189. date with the mouse (button 1). The calendar shows a period of three
  14190. months. To scroll it to other months, use the keys `>' and `<'.
  14191. If you don't like the calendar, turn it off with
  14192. \(setq org-read-date-popup-calendar nil)
  14193. With optional argument TO-TIME, the date will immediately be converted
  14194. to an internal time.
  14195. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14196. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14197. still enter a time, and this function will inform the calling routine
  14198. about this change. The calling routine may then choose to change the
  14199. format used to insert the time stamp into the buffer to include the time.
  14200. With optional argument FROM-STRING, read from this string instead from
  14201. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14202. the time/date that is used for everything that is not specified by the
  14203. user."
  14204. (require 'parse-time)
  14205. (let* ((org-time-stamp-rounding-minutes
  14206. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14207. (org-dcst org-display-custom-times)
  14208. (ct (org-current-time))
  14209. (org-def (or org-overriding-default-time default-time ct))
  14210. (org-defdecode (decode-time org-def))
  14211. (dummy (progn
  14212. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14213. (setcar (nthcdr 2 org-defdecode) -1)
  14214. (setcar (nthcdr 1 org-defdecode) 59)
  14215. (setq org-def (apply 'encode-time org-defdecode)
  14216. org-defdecode (decode-time org-def)))))
  14217. (calendar-frame-setup nil)
  14218. (calendar-setup nil)
  14219. (calendar-move-hook nil)
  14220. (calendar-view-diary-initially-flag nil)
  14221. (calendar-view-holidays-initially-flag nil)
  14222. (timestr (format-time-string
  14223. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14224. (prompt (concat (if prompt (concat prompt " ") "")
  14225. (format "Date+time [%s]: " timestr)))
  14226. ans (org-ans0 "") org-ans1 org-ans2 final)
  14227. (cond
  14228. (from-string (setq ans from-string))
  14229. (org-read-date-popup-calendar
  14230. (save-excursion
  14231. (save-window-excursion
  14232. (calendar)
  14233. (org-eval-in-calendar '(setq cursor-type nil) t)
  14234. (unwind-protect
  14235. (progn
  14236. (calendar-forward-day (- (time-to-days org-def)
  14237. (calendar-absolute-from-gregorian
  14238. (calendar-current-date))))
  14239. (org-eval-in-calendar nil t)
  14240. (let* ((old-map (current-local-map))
  14241. (map (copy-keymap calendar-mode-map))
  14242. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14243. (org-defkey map (kbd "RET") 'org-calendar-select)
  14244. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14245. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14246. (org-defkey minibuffer-local-map [(meta shift left)]
  14247. (lambda () (interactive)
  14248. (org-eval-in-calendar '(calendar-backward-month 1))))
  14249. (org-defkey minibuffer-local-map [(meta shift right)]
  14250. (lambda () (interactive)
  14251. (org-eval-in-calendar '(calendar-forward-month 1))))
  14252. (org-defkey minibuffer-local-map [(meta shift up)]
  14253. (lambda () (interactive)
  14254. (org-eval-in-calendar '(calendar-backward-year 1))))
  14255. (org-defkey minibuffer-local-map [(meta shift down)]
  14256. (lambda () (interactive)
  14257. (org-eval-in-calendar '(calendar-forward-year 1))))
  14258. (org-defkey minibuffer-local-map [?\e (shift left)]
  14259. (lambda () (interactive)
  14260. (org-eval-in-calendar '(calendar-backward-month 1))))
  14261. (org-defkey minibuffer-local-map [?\e (shift right)]
  14262. (lambda () (interactive)
  14263. (org-eval-in-calendar '(calendar-forward-month 1))))
  14264. (org-defkey minibuffer-local-map [?\e (shift up)]
  14265. (lambda () (interactive)
  14266. (org-eval-in-calendar '(calendar-backward-year 1))))
  14267. (org-defkey minibuffer-local-map [?\e (shift down)]
  14268. (lambda () (interactive)
  14269. (org-eval-in-calendar '(calendar-forward-year 1))))
  14270. (org-defkey minibuffer-local-map [(shift up)]
  14271. (lambda () (interactive)
  14272. (org-eval-in-calendar '(calendar-backward-week 1))))
  14273. (org-defkey minibuffer-local-map [(shift down)]
  14274. (lambda () (interactive)
  14275. (org-eval-in-calendar '(calendar-forward-week 1))))
  14276. (org-defkey minibuffer-local-map [(shift left)]
  14277. (lambda () (interactive)
  14278. (org-eval-in-calendar '(calendar-backward-day 1))))
  14279. (org-defkey minibuffer-local-map [(shift right)]
  14280. (lambda () (interactive)
  14281. (org-eval-in-calendar '(calendar-forward-day 1))))
  14282. (org-defkey minibuffer-local-map ">"
  14283. (lambda () (interactive)
  14284. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14285. (org-defkey minibuffer-local-map "<"
  14286. (lambda () (interactive)
  14287. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14288. (org-defkey minibuffer-local-map "\C-v"
  14289. (lambda () (interactive)
  14290. (org-eval-in-calendar
  14291. '(calendar-scroll-left-three-months 1))))
  14292. (org-defkey minibuffer-local-map "\M-v"
  14293. (lambda () (interactive)
  14294. (org-eval-in-calendar
  14295. '(calendar-scroll-right-three-months 1))))
  14296. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14297. (unwind-protect
  14298. (progn
  14299. (use-local-map map)
  14300. (setq org-read-date-inactive inactive)
  14301. (add-hook 'post-command-hook 'org-read-date-display)
  14302. (setq org-ans0 (read-string prompt default-input
  14303. 'org-read-date-history nil))
  14304. ;; org-ans0: from prompt
  14305. ;; org-ans1: from mouse click
  14306. ;; org-ans2: from calendar motion
  14307. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14308. (remove-hook 'post-command-hook 'org-read-date-display)
  14309. (use-local-map old-map)
  14310. (when org-read-date-overlay
  14311. (delete-overlay org-read-date-overlay)
  14312. (setq org-read-date-overlay nil)))))
  14313. (bury-buffer "*Calendar*")))))
  14314. (t ; Naked prompt only
  14315. (unwind-protect
  14316. (setq ans (read-string prompt default-input
  14317. 'org-read-date-history timestr))
  14318. (when org-read-date-overlay
  14319. (delete-overlay org-read-date-overlay)
  14320. (setq org-read-date-overlay nil)))))
  14321. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14322. (when org-read-date-analyze-forced-year
  14323. (message "Year was forced into %s"
  14324. (if org-read-date-force-compatible-dates
  14325. "compatible range (1970-2037)"
  14326. "range representable on this machine"))
  14327. (ding))
  14328. ;; One round trip to get rid of 34th of August and stuff like that....
  14329. (setq final (decode-time (apply 'encode-time final)))
  14330. (setq org-read-date-final-answer ans)
  14331. (if to-time
  14332. (apply 'encode-time final)
  14333. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14334. (format "%04d-%02d-%02d %02d:%02d"
  14335. (nth 5 final) (nth 4 final) (nth 3 final)
  14336. (nth 2 final) (nth 1 final))
  14337. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14338. (defvar org-def)
  14339. (defvar org-defdecode)
  14340. (defvar org-with-time)
  14341. (defun org-read-date-display ()
  14342. "Display the current date prompt interpretation in the minibuffer."
  14343. (when org-read-date-display-live
  14344. (when org-read-date-overlay
  14345. (delete-overlay org-read-date-overlay))
  14346. (when (minibufferp (current-buffer))
  14347. (save-excursion
  14348. (end-of-line 1)
  14349. (while (not (equal (buffer-substring
  14350. (max (point-min) (- (point) 4)) (point))
  14351. " "))
  14352. (insert " ")))
  14353. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14354. " " (or org-ans1 org-ans2)))
  14355. (org-end-time-was-given nil)
  14356. (f (org-read-date-analyze ans org-def org-defdecode))
  14357. (fmts (if org-dcst
  14358. org-time-stamp-custom-formats
  14359. org-time-stamp-formats))
  14360. (fmt (if (or org-with-time
  14361. (and (boundp 'org-time-was-given) org-time-was-given))
  14362. (cdr fmts)
  14363. (car fmts)))
  14364. (txt (format-time-string fmt (apply 'encode-time f)))
  14365. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14366. (txt (concat "=> " txt)))
  14367. (when (and org-end-time-was-given
  14368. (string-match org-plain-time-of-day-regexp txt))
  14369. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14370. org-end-time-was-given
  14371. (substring txt (match-end 0)))))
  14372. (when org-read-date-analyze-futurep
  14373. (setq txt (concat txt " (=>F)")))
  14374. (setq org-read-date-overlay
  14375. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14376. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14377. (defun org-read-date-analyze (ans org-def org-defdecode)
  14378. "Analyze the combined answer of the date prompt."
  14379. ;; FIXME: cleanup and comment
  14380. (let ((nowdecode (decode-time (current-time)))
  14381. delta deltan deltaw deltadef year month day
  14382. hour minute second wday pm h2 m2 tl wday1
  14383. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14384. (setq org-read-date-analyze-futurep nil
  14385. org-read-date-analyze-forced-year nil)
  14386. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14387. (setq ans "+0"))
  14388. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14389. (setq ans (replace-match "" t t ans)
  14390. deltan (car delta)
  14391. deltaw (nth 1 delta)
  14392. deltadef (nth 2 delta)))
  14393. ;; Check if there is an iso week date in there. If yes, store the
  14394. ;; info and postpone interpreting it until the rest of the parsing
  14395. ;; is done.
  14396. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14397. (setq iso-year (if (match-end 1)
  14398. (org-small-year-to-year
  14399. (string-to-number (match-string 1 ans))))
  14400. iso-weekday (if (match-end 3)
  14401. (string-to-number (match-string 3 ans)))
  14402. iso-week (string-to-number (match-string 2 ans)))
  14403. (setq ans (replace-match "" t t ans)))
  14404. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14405. (when (string-match
  14406. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14407. (setq year (if (match-end 2)
  14408. (string-to-number (match-string 2 ans))
  14409. (progn (setq kill-year t)
  14410. (string-to-number (format-time-string "%Y"))))
  14411. month (string-to-number (match-string 3 ans))
  14412. day (string-to-number (match-string 4 ans)))
  14413. (if (< year 100) (setq year (+ 2000 year)))
  14414. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14415. t nil ans)))
  14416. ;; Help matching dotted european dates
  14417. (when (string-match
  14418. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14419. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14420. (setq kill-year t)
  14421. (string-to-number (format-time-string "%Y")))
  14422. day (string-to-number (match-string 1 ans))
  14423. month (string-to-number (match-string 2 ans))
  14424. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14425. t nil ans)))
  14426. ;; Help matching american dates, like 5/30 or 5/30/7
  14427. (when (string-match
  14428. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14429. (setq year (if (match-end 4)
  14430. (string-to-number (match-string 4 ans))
  14431. (progn (setq kill-year t)
  14432. (string-to-number (format-time-string "%Y"))))
  14433. month (string-to-number (match-string 1 ans))
  14434. day (string-to-number (match-string 2 ans)))
  14435. (if (< year 100) (setq year (+ 2000 year)))
  14436. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14437. t nil ans)))
  14438. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14439. ;; If there is a time with am/pm, and *no* time without it, we convert
  14440. ;; so that matching will be successful.
  14441. (loop for i from 1 to 2 do ; twice, for end time as well
  14442. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14443. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14444. (setq hour (string-to-number (match-string 1 ans))
  14445. minute (if (match-end 3)
  14446. (string-to-number (match-string 3 ans))
  14447. 0)
  14448. pm (equal ?p
  14449. (string-to-char (downcase (match-string 4 ans)))))
  14450. (if (and (= hour 12) (not pm))
  14451. (setq hour 0)
  14452. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14453. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14454. t t ans))))
  14455. ;; Check if a time range is given as a duration
  14456. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14457. (setq hour (string-to-number (match-string 1 ans))
  14458. h2 (+ hour (string-to-number (match-string 3 ans)))
  14459. minute (string-to-number (match-string 2 ans))
  14460. m2 (+ minute (if (match-end 5) (string-to-number
  14461. (match-string 5 ans))0)))
  14462. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14463. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14464. t t ans)))
  14465. ;; Check if there is a time range
  14466. (when (boundp 'org-end-time-was-given)
  14467. (setq org-time-was-given nil)
  14468. (when (and (string-match org-plain-time-of-day-regexp ans)
  14469. (match-end 8))
  14470. (setq org-end-time-was-given (match-string 8 ans))
  14471. (setq ans (concat (substring ans 0 (match-beginning 7))
  14472. (substring ans (match-end 7))))))
  14473. (setq tl (parse-time-string ans)
  14474. day (or (nth 3 tl) (nth 3 org-defdecode))
  14475. month (or (nth 4 tl)
  14476. (if (and org-read-date-prefer-future
  14477. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14478. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14479. (nth 4 org-defdecode)))
  14480. year (or (and (not kill-year) (nth 5 tl))
  14481. (if (and org-read-date-prefer-future
  14482. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14483. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14484. (nth 5 org-defdecode)))
  14485. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14486. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14487. second (or (nth 0 tl) 0)
  14488. wday (nth 6 tl))
  14489. (when (and (eq org-read-date-prefer-future 'time)
  14490. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14491. (equal day (nth 3 nowdecode))
  14492. (equal month (nth 4 nowdecode))
  14493. (equal year (nth 5 nowdecode))
  14494. (nth 2 tl)
  14495. (or (< (nth 2 tl) (nth 2 nowdecode))
  14496. (and (= (nth 2 tl) (nth 2 nowdecode))
  14497. (nth 1 tl)
  14498. (< (nth 1 tl) (nth 1 nowdecode)))))
  14499. (setq day (1+ day)
  14500. futurep t))
  14501. ;; Special date definitions below
  14502. (cond
  14503. (iso-week
  14504. ;; There was an iso week
  14505. (require 'cal-iso)
  14506. (setq futurep nil)
  14507. (setq year (or iso-year year)
  14508. day (or iso-weekday wday 1)
  14509. wday nil ; to make sure that the trigger below does not match
  14510. iso-date (calendar-gregorian-from-absolute
  14511. (calendar-absolute-from-iso
  14512. (list iso-week day year))))
  14513. ; FIXME: Should we also push ISO weeks into the future?
  14514. ; (when (and org-read-date-prefer-future
  14515. ; (not iso-year)
  14516. ; (< (calendar-absolute-from-gregorian iso-date)
  14517. ; (time-to-days (current-time))))
  14518. ; (setq year (1+ year)
  14519. ; iso-date (calendar-gregorian-from-absolute
  14520. ; (calendar-absolute-from-iso
  14521. ; (list iso-week day year)))))
  14522. (setq month (car iso-date)
  14523. year (nth 2 iso-date)
  14524. day (nth 1 iso-date)))
  14525. (deltan
  14526. (setq futurep nil)
  14527. (unless deltadef
  14528. (let ((now (decode-time (current-time))))
  14529. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14530. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14531. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14532. ((equal deltaw "m") (setq month (+ month deltan)))
  14533. ((equal deltaw "y") (setq year (+ year deltan)))))
  14534. ((and wday (not (nth 3 tl)))
  14535. ;; Weekday was given, but no day, so pick that day in the week
  14536. ;; on or after the derived date.
  14537. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14538. (unless (equal wday wday1)
  14539. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14540. (if (and (boundp 'org-time-was-given)
  14541. (nth 2 tl))
  14542. (setq org-time-was-given t))
  14543. (if (< year 100) (setq year (+ 2000 year)))
  14544. ;; Check of the date is representable
  14545. (if org-read-date-force-compatible-dates
  14546. (progn
  14547. (if (< year 1970)
  14548. (setq year 1970 org-read-date-analyze-forced-year t))
  14549. (if (> year 2037)
  14550. (setq year 2037 org-read-date-analyze-forced-year t)))
  14551. (condition-case nil
  14552. (ignore (encode-time second minute hour day month year))
  14553. (error
  14554. (setq year (nth 5 org-defdecode))
  14555. (setq org-read-date-analyze-forced-year t))))
  14556. (setq org-read-date-analyze-futurep futurep)
  14557. (list second minute hour day month year)))
  14558. (defvar parse-time-weekdays)
  14559. (defun org-read-date-get-relative (s today default)
  14560. "Check string S for special relative date string.
  14561. TODAY and DEFAULT are internal times, for today and for a default.
  14562. Return shift list (N what def-flag)
  14563. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14564. N is the number of WHATs to shift.
  14565. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14566. the DEFAULT date rather than TODAY."
  14567. (require 'parse-time)
  14568. (when (and
  14569. (string-match
  14570. (concat
  14571. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14572. "\\([0-9]+\\)?"
  14573. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14574. "\\([ \t]\\|$\\)") s)
  14575. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14576. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14577. (string-to-char (substring (match-string 1 s) -1))
  14578. ?+))
  14579. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14580. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14581. (what (if (match-end 3) (match-string 3 s) "d"))
  14582. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14583. (date (if rel default today))
  14584. (wday (nth 6 (decode-time date)))
  14585. delta)
  14586. (if wday1
  14587. (progn
  14588. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14589. (if (= dir ?-) (setq delta (- delta 7)))
  14590. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14591. (list delta "d" rel))
  14592. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14593. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14594. "Turn a user-specified date into the internal representation.
  14595. The internal representation needed by the calendar is (month day year).
  14596. This is a wrapper to handle the brain-dead convention in calendar that
  14597. user function argument order change dependent on argument order."
  14598. (if (boundp 'calendar-date-style)
  14599. (cond
  14600. ((eq calendar-date-style 'american)
  14601. (list arg1 arg2 arg3))
  14602. ((eq calendar-date-style 'european)
  14603. (list arg2 arg1 arg3))
  14604. ((eq calendar-date-style 'iso)
  14605. (list arg2 arg3 arg1)))
  14606. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14607. (if (org-bound-and-true-p european-calendar-style)
  14608. (list arg2 arg1 arg3)
  14609. (list arg1 arg2 arg3)))))
  14610. (defun org-eval-in-calendar (form &optional keepdate)
  14611. "Eval FORM in the calendar window and return to current window.
  14612. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14613. otherwise stick to the current value of `org-ans2'."
  14614. (let ((sf (selected-frame))
  14615. (sw (selected-window)))
  14616. (select-window (get-buffer-window "*Calendar*" t))
  14617. (eval form)
  14618. (when (and (not keepdate) (calendar-cursor-to-date))
  14619. (let* ((date (calendar-cursor-to-date))
  14620. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14621. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14622. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14623. (select-window sw)
  14624. (org-select-frame-set-input-focus sf)))
  14625. (defun org-calendar-select ()
  14626. "Return to `org-read-date' with the date currently selected.
  14627. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14628. (interactive)
  14629. (when (calendar-cursor-to-date)
  14630. (let* ((date (calendar-cursor-to-date))
  14631. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14632. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14633. (if (active-minibuffer-window) (exit-minibuffer))))
  14634. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14635. "Insert a date stamp for the date given by the internal TIME.
  14636. WITH-HM means use the stamp format that includes the time of the day.
  14637. INACTIVE means use square brackets instead of angular ones, so that the
  14638. stamp will not contribute to the agenda.
  14639. PRE and POST are optional strings to be inserted before and after the
  14640. stamp.
  14641. The command returns the inserted time stamp."
  14642. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14643. stamp)
  14644. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14645. (insert-before-markers (or pre ""))
  14646. (when (listp extra)
  14647. (setq extra (car extra))
  14648. (if (and (stringp extra)
  14649. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14650. (setq extra (format "-%02d:%02d"
  14651. (string-to-number (match-string 1 extra))
  14652. (string-to-number (match-string 2 extra))))
  14653. (setq extra nil)))
  14654. (when extra
  14655. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14656. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14657. (insert-before-markers (or post ""))
  14658. (setq org-last-inserted-timestamp stamp)))
  14659. (defun org-toggle-time-stamp-overlays ()
  14660. "Toggle the use of custom time stamp formats."
  14661. (interactive)
  14662. (setq org-display-custom-times (not org-display-custom-times))
  14663. (unless org-display-custom-times
  14664. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14665. (while (setq p (next-single-property-change p 'display))
  14666. (if (and (get-text-property p 'display)
  14667. (eq (get-text-property p 'face) 'org-date))
  14668. (remove-text-properties
  14669. p (setq p (next-single-property-change p 'display))
  14670. '(display t))))
  14671. (set-buffer-modified-p bmp)))
  14672. (if (featurep 'xemacs)
  14673. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14674. (org-restart-font-lock)
  14675. (setq org-table-may-need-update t)
  14676. (if org-display-custom-times
  14677. (message "Time stamps are overlaid with custom format")
  14678. (message "Time stamp overlays removed")))
  14679. (defun org-display-custom-time (beg end)
  14680. "Overlay modified time stamp format over timestamp between BEG and END."
  14681. (let* ((ts (buffer-substring beg end))
  14682. t1 w1 with-hm tf time str w2 (off 0))
  14683. (save-match-data
  14684. (setq t1 (org-parse-time-string ts t))
  14685. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14686. (setq off (- (match-end 0) (match-beginning 0)))))
  14687. (setq end (- end off))
  14688. (setq w1 (- end beg)
  14689. with-hm (and (nth 1 t1) (nth 2 t1))
  14690. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14691. time (org-fix-decoded-time t1)
  14692. str (org-add-props
  14693. (format-time-string
  14694. (substring tf 1 -1) (apply 'encode-time time))
  14695. nil 'mouse-face 'highlight)
  14696. w2 (length str))
  14697. (if (not (= w2 w1))
  14698. (add-text-properties (1+ beg) (+ 2 beg)
  14699. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14700. (if (featurep 'xemacs)
  14701. (progn
  14702. (put-text-property beg end 'invisible t)
  14703. (put-text-property beg end 'end-glyph (make-glyph str)))
  14704. (put-text-property beg end 'display str))))
  14705. (defun org-translate-time (string)
  14706. "Translate all timestamps in STRING to custom format.
  14707. But do this only if the variable `org-display-custom-times' is set."
  14708. (when org-display-custom-times
  14709. (save-match-data
  14710. (let* ((start 0)
  14711. (re org-ts-regexp-both)
  14712. t1 with-hm inactive tf time str beg end)
  14713. (while (setq start (string-match re string start))
  14714. (setq beg (match-beginning 0)
  14715. end (match-end 0)
  14716. t1 (save-match-data
  14717. (org-parse-time-string (substring string beg end) t))
  14718. with-hm (and (nth 1 t1) (nth 2 t1))
  14719. inactive (equal (substring string beg (1+ beg)) "[")
  14720. tf (funcall (if with-hm 'cdr 'car)
  14721. org-time-stamp-custom-formats)
  14722. time (org-fix-decoded-time t1)
  14723. str (format-time-string
  14724. (concat
  14725. (if inactive "[" "<") (substring tf 1 -1)
  14726. (if inactive "]" ">"))
  14727. (apply 'encode-time time))
  14728. string (replace-match str t t string)
  14729. start (+ start (length str)))))))
  14730. string)
  14731. (defun org-fix-decoded-time (time)
  14732. "Set 0 instead of nil for the first 6 elements of time.
  14733. Don't touch the rest."
  14734. (let ((n 0))
  14735. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14736. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14737. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14738. "Difference between TIMESTAMP-STRING and now in days.
  14739. If SECONDS is non-nil, return the difference in seconds."
  14740. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14741. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14742. (funcall fdiff (current-time)))))
  14743. (defun org-deadline-close (timestamp-string &optional ndays)
  14744. "Is the time in TIMESTAMP-STRING close to the current date?"
  14745. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14746. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14747. (not (org-entry-is-done-p))))
  14748. (defun org-get-wdays (ts)
  14749. "Get the deadline lead time appropriate for timestring TS."
  14750. (cond
  14751. ((<= org-deadline-warning-days 0)
  14752. ;; 0 or negative, enforce this value no matter what
  14753. (- org-deadline-warning-days))
  14754. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14755. ;; lead time is specified.
  14756. (floor (* (string-to-number (match-string 1 ts))
  14757. (cdr (assoc (match-string 2 ts)
  14758. '(("d" . 1) ("w" . 7)
  14759. ("m" . 30.4) ("y" . 365.25)))))))
  14760. ;; go for the default.
  14761. (t org-deadline-warning-days)))
  14762. (defun org-calendar-select-mouse (ev)
  14763. "Return to `org-read-date' with the date currently selected.
  14764. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14765. (interactive "e")
  14766. (mouse-set-point ev)
  14767. (when (calendar-cursor-to-date)
  14768. (let* ((date (calendar-cursor-to-date))
  14769. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14770. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14771. (if (active-minibuffer-window) (exit-minibuffer))))
  14772. (defun org-check-deadlines (ndays)
  14773. "Check if there are any deadlines due or past due.
  14774. A deadline is considered due if it happens within `org-deadline-warning-days'
  14775. days from today's date. If the deadline appears in an entry marked DONE,
  14776. it is not shown. The prefix arg NDAYS can be used to test that many
  14777. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14778. (interactive "P")
  14779. (let* ((org-warn-days
  14780. (cond
  14781. ((equal ndays '(4)) 100000)
  14782. (ndays (prefix-numeric-value ndays))
  14783. (t (abs org-deadline-warning-days))))
  14784. (case-fold-search nil)
  14785. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14786. (callback
  14787. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14788. (message "%d deadlines past-due or due within %d days"
  14789. (org-occur regexp nil callback)
  14790. org-warn-days)))
  14791. (defsubst org-re-timestamp (type)
  14792. "Return a regexp for timestamp TYPE.
  14793. Allowed values for TYPE are:
  14794. all: all timestamps
  14795. active: only active timestamps (<...>)
  14796. inactive: only inactive timestamps ([...])
  14797. scheduled: only scheduled timestamps
  14798. deadline: only deadline timestamps
  14799. When TYPE is nil, fall back on returning a regexp that matches
  14800. both scheduled and deadline timestamps."
  14801. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14802. ((eq type 'active) org-ts-regexp)
  14803. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14804. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14805. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14806. ((eq type 'scheduled-or-deadline)
  14807. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14808. (defun org-check-before-date (date)
  14809. "Check if there are deadlines or scheduled entries before DATE."
  14810. (interactive (list (org-read-date)))
  14811. (let ((case-fold-search nil)
  14812. (regexp (org-re-timestamp org-ts-type))
  14813. (callback
  14814. (lambda () (time-less-p
  14815. (org-time-string-to-time (match-string 1))
  14816. (org-time-string-to-time date)))))
  14817. (message "%d entries before %s"
  14818. (org-occur regexp nil callback) date)))
  14819. (defun org-check-after-date (date)
  14820. "Check if there are deadlines or scheduled entries after DATE."
  14821. (interactive (list (org-read-date)))
  14822. (let ((case-fold-search nil)
  14823. (regexp (org-re-timestamp org-ts-type))
  14824. (callback
  14825. (lambda () (not
  14826. (time-less-p
  14827. (org-time-string-to-time (match-string 1))
  14828. (org-time-string-to-time date))))))
  14829. (message "%d entries after %s"
  14830. (org-occur regexp nil callback) date)))
  14831. (defun org-check-dates-range (start-date end-date)
  14832. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14833. (interactive (list (org-read-date nil nil nil "Range starts")
  14834. (org-read-date nil nil nil "Range end")))
  14835. (let ((case-fold-search nil)
  14836. (regexp (org-re-timestamp org-ts-type))
  14837. (callback
  14838. (lambda ()
  14839. (let ((match (match-string 1)))
  14840. (and
  14841. (not (time-less-p
  14842. (org-time-string-to-time match)
  14843. (org-time-string-to-time start-date)))
  14844. (time-less-p
  14845. (org-time-string-to-time match)
  14846. (org-time-string-to-time end-date)))))))
  14847. (message "%d entries between %s and %s"
  14848. (org-occur regexp nil callback) start-date end-date)))
  14849. (defun org-evaluate-time-range (&optional to-buffer)
  14850. "Evaluate a time range by computing the difference between start and end.
  14851. Normally the result is just printed in the echo area, but with prefix arg
  14852. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14853. If the time range is actually in a table, the result is inserted into the
  14854. next column.
  14855. For time difference computation, a year is assumed to be exactly 365
  14856. days in order to avoid rounding problems."
  14857. (interactive "P")
  14858. (or
  14859. (org-clock-update-time-maybe)
  14860. (save-excursion
  14861. (unless (org-at-date-range-p t)
  14862. (goto-char (point-at-bol))
  14863. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14864. (if (not (org-at-date-range-p t))
  14865. (error "Not at a time-stamp range, and none found in current line")))
  14866. (let* ((ts1 (match-string 1))
  14867. (ts2 (match-string 2))
  14868. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14869. (match-end (match-end 0))
  14870. (time1 (org-time-string-to-time ts1))
  14871. (time2 (org-time-string-to-time ts2))
  14872. (t1 (org-float-time time1))
  14873. (t2 (org-float-time time2))
  14874. (diff (abs (- t2 t1)))
  14875. (negative (< (- t2 t1) 0))
  14876. ;; (ys (floor (* 365 24 60 60)))
  14877. (ds (* 24 60 60))
  14878. (hs (* 60 60))
  14879. (fy "%dy %dd %02d:%02d")
  14880. (fy1 "%dy %dd")
  14881. (fd "%dd %02d:%02d")
  14882. (fd1 "%dd")
  14883. (fh "%02d:%02d")
  14884. y d h m align)
  14885. (if havetime
  14886. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14887. y 0
  14888. d (floor (/ diff ds)) diff (mod diff ds)
  14889. h (floor (/ diff hs)) diff (mod diff hs)
  14890. m (floor (/ diff 60)))
  14891. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14892. y 0
  14893. d (floor (+ (/ diff ds) 0.5))
  14894. h 0 m 0))
  14895. (if (not to-buffer)
  14896. (message "%s" (org-make-tdiff-string y d h m))
  14897. (if (org-at-table-p)
  14898. (progn
  14899. (goto-char match-end)
  14900. (setq align t)
  14901. (and (looking-at " *|") (goto-char (match-end 0))))
  14902. (goto-char match-end))
  14903. (if (looking-at
  14904. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14905. (replace-match ""))
  14906. (if negative (insert " -"))
  14907. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14908. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14909. (insert " " (format fh h m))))
  14910. (if align (org-table-align))
  14911. (message "Time difference inserted")))))
  14912. (defun org-make-tdiff-string (y d h m)
  14913. (let ((fmt "")
  14914. (l nil))
  14915. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14916. l (push y l)))
  14917. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14918. l (push d l)))
  14919. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14920. l (push h l)))
  14921. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14922. l (push m l)))
  14923. (apply 'format fmt (nreverse l))))
  14924. (defun org-time-string-to-time (s &optional buffer pos)
  14925. "Convert a timestamp string into internal time."
  14926. (condition-case errdata
  14927. (apply 'encode-time (org-parse-time-string s))
  14928. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14929. s (if (not (and buffer pos))
  14930. ""
  14931. (format " at %d in buffer `%s'" pos buffer))
  14932. (cdr errdata)))))
  14933. (defun org-time-string-to-seconds (s)
  14934. "Convert a timestamp string to a number of seconds."
  14935. (org-float-time (org-time-string-to-time s)))
  14936. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14937. "Convert a time stamp to an absolute day number.
  14938. If there is a specifier for a cyclic time stamp, get the closest date to
  14939. DAYNR.
  14940. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14941. The variable date is bound by the calendar when this is called."
  14942. (cond
  14943. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14944. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14945. daynr
  14946. (+ daynr 1000)))
  14947. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14948. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14949. (time-to-days (current-time))) (match-string 0 s)
  14950. prefer show-all))
  14951. (t (time-to-days
  14952. (condition-case errdata
  14953. (apply 'encode-time (org-parse-time-string s))
  14954. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14955. s (if (not (and buffer pos))
  14956. ""
  14957. (format " at %d in buffer `%s'" pos buffer))
  14958. (cdr errdata))))))))
  14959. (defun org-days-to-iso-week (days)
  14960. "Return the iso week number."
  14961. (require 'cal-iso)
  14962. (car (calendar-iso-from-absolute days)))
  14963. (defun org-small-year-to-year (year)
  14964. "Convert 2-digit years into 4-digit years.
  14965. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14966. The year 2000 cannot be abbreviated. Any year larger than 99
  14967. is returned unchanged."
  14968. (if (< year 38)
  14969. (setq year (+ 2000 year))
  14970. (if (< year 100)
  14971. (setq year (+ 1900 year))))
  14972. year)
  14973. (defun org-time-from-absolute (d)
  14974. "Return the time corresponding to date D.
  14975. D may be an absolute day number, or a calendar-type list (month day year)."
  14976. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14977. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14978. (defun org-calendar-holiday ()
  14979. "List of holidays, for Diary display in Org-mode."
  14980. (require 'holidays)
  14981. (let ((hl (funcall
  14982. (if (fboundp 'calendar-check-holidays)
  14983. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14984. (if hl (mapconcat 'identity hl "; "))))
  14985. (defun org-diary-sexp-entry (sexp entry date)
  14986. "Process a SEXP diary ENTRY for DATE."
  14987. (require 'diary-lib)
  14988. (let ((result (if calendar-debug-sexp
  14989. (let ((stack-trace-on-error t))
  14990. (eval (car (read-from-string sexp))))
  14991. (condition-case nil
  14992. (eval (car (read-from-string sexp)))
  14993. (error
  14994. (beep)
  14995. (message "Bad sexp at line %d in %s: %s"
  14996. (org-current-line)
  14997. (buffer-file-name) sexp)
  14998. (sleep-for 2))))))
  14999. (cond ((stringp result) (split-string result "; "))
  15000. ((and (consp result)
  15001. (not (consp (cdr result)))
  15002. (stringp (cdr result))) (cdr result))
  15003. ((and (consp result)
  15004. (stringp (car result))) result)
  15005. (result entry))))
  15006. (defun org-diary-to-ical-string (frombuf)
  15007. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15008. This uses the icalendar.el library."
  15009. (let* ((tmpdir (if (featurep 'xemacs)
  15010. (temp-directory)
  15011. temporary-file-directory))
  15012. (tmpfile (make-temp-name
  15013. (expand-file-name "orgics" tmpdir)))
  15014. buf rtn b e)
  15015. (with-current-buffer frombuf
  15016. (icalendar-export-region (point-min) (point-max) tmpfile)
  15017. (setq buf (find-buffer-visiting tmpfile))
  15018. (set-buffer buf)
  15019. (goto-char (point-min))
  15020. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15021. (setq b (match-beginning 0)))
  15022. (goto-char (point-max))
  15023. (if (re-search-backward "^END:VEVENT" nil t)
  15024. (setq e (match-end 0)))
  15025. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15026. (kill-buffer buf)
  15027. (delete-file tmpfile)
  15028. rtn))
  15029. (defun org-closest-date (start current change prefer show-all)
  15030. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15031. When PREFER is `past', return a date that is either CURRENT or past.
  15032. When PREFER is `future', return a date that is either CURRENT or future.
  15033. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15034. ;; Make the proper lists from the dates
  15035. (catch 'exit
  15036. (let ((a1 '(("h" . hour)
  15037. ("d" . day)
  15038. ("w" . week)
  15039. ("m" . month)
  15040. ("y" . year)))
  15041. (shour (nth 2 (org-parse-time-string start)))
  15042. dn dw sday cday n1 n2 n0
  15043. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15044. (setq start (org-date-to-gregorian start)
  15045. current (org-date-to-gregorian
  15046. (if show-all
  15047. current
  15048. (time-to-days (current-time))))
  15049. sday (calendar-absolute-from-gregorian start)
  15050. cday (calendar-absolute-from-gregorian current))
  15051. (if (<= cday sday) (throw 'exit sday))
  15052. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15053. (setq dn (string-to-number (match-string 1 change))
  15054. dw (cdr (assoc (match-string 2 change) a1)))
  15055. (error "Invalid change specifier: %s" change))
  15056. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15057. (cond
  15058. ((eq dw 'hour)
  15059. (let ((missing-hours
  15060. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15061. dn)))
  15062. (setq n1 (if (zerop missing-hours) cday
  15063. (- cday (1+ (floor (/ missing-hours 24)))))
  15064. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15065. ((eq dw 'day)
  15066. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15067. n2 (+ n1 dn)))
  15068. ((eq dw 'year)
  15069. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15070. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15071. (setq date1 (list m d y1)
  15072. n1 (calendar-absolute-from-gregorian date1)
  15073. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15074. n2 (calendar-absolute-from-gregorian date2)))
  15075. ((eq dw 'month)
  15076. ;; approx number of month between the two dates
  15077. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15078. ;; How often does dn fit in there?
  15079. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15080. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15081. m (+ m nm)
  15082. ny (floor (/ m 12))
  15083. y (+ y ny)
  15084. m (- m (* ny 12)))
  15085. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15086. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15087. (setq m2 (+ m dn) y2 y)
  15088. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15089. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15090. (while (<= n2 cday)
  15091. (setq n1 n2 m m2 y y2)
  15092. (setq m2 (+ m dn) y2 y)
  15093. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15094. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15095. ;; Make sure n1 is the earlier date
  15096. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15097. (if show-all
  15098. (cond
  15099. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15100. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15101. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15102. (cond
  15103. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15104. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15105. (t (if (= cday n1) n1 n2)))))))
  15106. (defun org-date-to-gregorian (date)
  15107. "Turn any specification of DATE into a Gregorian date for the calendar."
  15108. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15109. ((and (listp date) (= (length date) 3)) date)
  15110. ((stringp date)
  15111. (setq date (org-parse-time-string date))
  15112. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15113. ((listp date)
  15114. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15115. (defun org-parse-time-string (s &optional nodefault)
  15116. "Parse the standard Org-mode time string.
  15117. This should be a lot faster than the normal `parse-time-string'.
  15118. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15119. hour and minute fields will be nil if not given."
  15120. (if (string-match org-ts-regexp0 s)
  15121. (list 0
  15122. (if (or (match-beginning 8) (not nodefault))
  15123. (string-to-number (or (match-string 8 s) "0")))
  15124. (if (or (match-beginning 7) (not nodefault))
  15125. (string-to-number (or (match-string 7 s) "0")))
  15126. (string-to-number (match-string 4 s))
  15127. (string-to-number (match-string 3 s))
  15128. (string-to-number (match-string 2 s))
  15129. nil nil nil)
  15130. (error "Not a standard Org-mode time string: %s" s)))
  15131. (defun org-timestamp-up (&optional arg)
  15132. "Increase the date item at the cursor by one.
  15133. If the cursor is on the year, change the year. If it is on the month,
  15134. the day or the time, change that.
  15135. With prefix ARG, change by that many units."
  15136. (interactive "p")
  15137. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15138. (defun org-timestamp-down (&optional arg)
  15139. "Decrease the date item at the cursor by one.
  15140. If the cursor is on the year, change the year. If it is on the month,
  15141. the day or the time, change that.
  15142. With prefix ARG, change by that many units."
  15143. (interactive "p")
  15144. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15145. (defun org-timestamp-up-day (&optional arg)
  15146. "Increase the date in the time stamp by one day.
  15147. With prefix ARG, change that many days."
  15148. (interactive "p")
  15149. (if (and (not (org-at-timestamp-p t))
  15150. (org-at-heading-p))
  15151. (org-todo 'up)
  15152. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15153. (defun org-timestamp-down-day (&optional arg)
  15154. "Decrease the date in the time stamp by one day.
  15155. With prefix ARG, change that many days."
  15156. (interactive "p")
  15157. (if (and (not (org-at-timestamp-p t))
  15158. (org-at-heading-p))
  15159. (org-todo 'down)
  15160. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15161. (defun org-at-timestamp-p (&optional inactive-ok)
  15162. "Determine if the cursor is in or at a timestamp."
  15163. (interactive)
  15164. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15165. (pos (point))
  15166. (ans (or (looking-at tsr)
  15167. (save-excursion
  15168. (skip-chars-backward "^[<\n\r\t")
  15169. (if (> (point) (point-min)) (backward-char 1))
  15170. (and (looking-at tsr)
  15171. (> (- (match-end 0) pos) -1))))))
  15172. (and ans
  15173. (boundp 'org-ts-what)
  15174. (setq org-ts-what
  15175. (cond
  15176. ((= pos (match-beginning 0)) 'bracket)
  15177. ;; Point is considered to be "on the bracket" whether
  15178. ;; it's really on it or right after it.
  15179. ((= pos (1- (match-end 0))) 'bracket)
  15180. ((= pos (match-end 0)) 'after)
  15181. ((org-pos-in-match-range pos 2) 'year)
  15182. ((org-pos-in-match-range pos 3) 'month)
  15183. ((org-pos-in-match-range pos 7) 'hour)
  15184. ((org-pos-in-match-range pos 8) 'minute)
  15185. ((or (org-pos-in-match-range pos 4)
  15186. (org-pos-in-match-range pos 5)) 'day)
  15187. ((and (> pos (or (match-end 8) (match-end 5)))
  15188. (< pos (match-end 0)))
  15189. (- pos (or (match-end 8) (match-end 5))))
  15190. (t 'day))))
  15191. ans))
  15192. (defun org-toggle-timestamp-type ()
  15193. "Toggle the type (<active> or [inactive]) of a time stamp."
  15194. (interactive)
  15195. (when (org-at-timestamp-p t)
  15196. (let ((beg (match-beginning 0)) (end (match-end 0))
  15197. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15198. (save-excursion
  15199. (goto-char beg)
  15200. (while (re-search-forward "[][<>]" end t)
  15201. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15202. t t)))
  15203. (message "Timestamp is now %sactive"
  15204. (if (equal (char-after beg) ?<) "" "in")))))
  15205. (defvar org-clock-history) ; defined in org-clock.el
  15206. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15207. (defun org-timestamp-change (n &optional what updown)
  15208. "Change the date in the time stamp at point.
  15209. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15210. `year', `minute', `second'. If WHAT is not given, the cursor position
  15211. in the timestamp determines what will be changed."
  15212. (let ((origin (point)) origin-cat
  15213. with-hm inactive
  15214. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15215. org-ts-what
  15216. extra rem
  15217. ts time time0 fixnext clrgx)
  15218. (if (not (org-at-timestamp-p t))
  15219. (error "Not at a timestamp"))
  15220. (if (and (not what) (eq org-ts-what 'bracket))
  15221. (org-toggle-timestamp-type)
  15222. ;; Point isn't on brackets. Remember the part of the time-stamp
  15223. ;; the point was in. Indeed, size of time-stamps may change,
  15224. ;; but point must be kept in the same category nonetheless.
  15225. (setq origin-cat org-ts-what)
  15226. (if (and (not what) (not (eq org-ts-what 'day))
  15227. org-display-custom-times
  15228. (get-text-property (point) 'display)
  15229. (not (get-text-property (1- (point)) 'display)))
  15230. (setq org-ts-what 'day))
  15231. (setq org-ts-what (or what org-ts-what)
  15232. inactive (= (char-after (match-beginning 0)) ?\[)
  15233. ts (match-string 0))
  15234. (replace-match "")
  15235. (if (string-match
  15236. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15237. ts)
  15238. (setq extra (match-string 1 ts)))
  15239. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15240. (setq with-hm t))
  15241. (setq time0 (org-parse-time-string ts))
  15242. (when (and updown
  15243. (eq org-ts-what 'minute)
  15244. (not current-prefix-arg))
  15245. ;; This looks like s-up and s-down. Change by one rounding step.
  15246. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15247. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15248. (setcar (cdr time0) (+ (nth 1 time0)
  15249. (if (> n 0) (- rem) (- dm rem))))))
  15250. (setq time
  15251. (encode-time (or (car time0) 0)
  15252. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15253. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15254. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15255. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15256. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15257. (nthcdr 6 time0)))
  15258. (when (and (member org-ts-what '(hour minute))
  15259. extra
  15260. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15261. (setq extra (org-modify-ts-extra
  15262. extra
  15263. (if (eq org-ts-what 'hour) 2 5)
  15264. n dm)))
  15265. (when (integerp org-ts-what)
  15266. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15267. (if (eq what 'calendar)
  15268. (let ((cal-date (org-get-date-from-calendar)))
  15269. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15270. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15271. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15272. (setcar time0 (or (car time0) 0))
  15273. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15274. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15275. (setq time (apply 'encode-time time0))))
  15276. ;; Insert the new time-stamp, and ensure point stays in the same
  15277. ;; category as before (i.e. not after the last position in that
  15278. ;; category).
  15279. (let ((pos (point)))
  15280. ;; Stay before inserted string. `save-excursion' is of no use.
  15281. (setq org-last-changed-timestamp
  15282. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15283. (goto-char pos))
  15284. (save-match-data
  15285. (looking-at org-ts-regexp3)
  15286. (goto-char (cond
  15287. ;; `day' category ends before `hour' if any, or at
  15288. ;; the end of the day name.
  15289. ((eq origin-cat 'day)
  15290. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15291. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15292. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15293. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15294. ;; `year' and `month' have both fixed size: point
  15295. ;; couldn't have moved into another part.
  15296. (t origin))))
  15297. ;; Update clock if on a CLOCK line.
  15298. (org-clock-update-time-maybe)
  15299. ;; Maybe adjust the closest clock in `org-clock-history'
  15300. (when org-clock-adjust-closest
  15301. (if (not (and (org-at-clock-log-p)
  15302. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15303. org-clock-history))))))
  15304. (message "No clock to adjust")
  15305. (cond ((save-excursion ; fix previous clock?
  15306. (re-search-backward org-ts-regexp0 nil t)
  15307. (org-looking-back (concat org-clock-string " \\[")))
  15308. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15309. ((save-excursion ; fix next clock?
  15310. (re-search-backward org-ts-regexp0 nil t)
  15311. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15312. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15313. (save-window-excursion
  15314. ;; Find closest clock to point, adjust the previous/next one in history
  15315. (let* ((p (save-excursion (org-back-to-heading t)))
  15316. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15317. (clfixnth
  15318. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15319. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15320. (if (not clfixpos)
  15321. (message "No clock to adjust")
  15322. (save-excursion
  15323. (org-goto-marker-or-bmk clfixpos)
  15324. (org-show-subtree)
  15325. (when (re-search-forward clrgx nil t)
  15326. (goto-char (match-beginning 1))
  15327. (let (org-clock-adjust-closest)
  15328. (org-timestamp-change n org-ts-what updown))
  15329. (message "Clock adjusted in %s for heading: %s"
  15330. (file-name-nondirectory (buffer-file-name))
  15331. (org-get-heading t t)))))))))
  15332. ;; Try to recenter the calendar window, if any.
  15333. (if (and org-calendar-follow-timestamp-change
  15334. (get-buffer-window "*Calendar*" t)
  15335. (memq org-ts-what '(day month year)))
  15336. (org-recenter-calendar (time-to-days time))))))
  15337. (defun org-modify-ts-extra (s pos n dm)
  15338. "Change the different parts of the lead-time and repeat fields in timestamp."
  15339. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15340. ng h m new rem)
  15341. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15342. (cond
  15343. ((or (org-pos-in-match-range pos 2)
  15344. (org-pos-in-match-range pos 3))
  15345. (setq m (string-to-number (match-string 3 s))
  15346. h (string-to-number (match-string 2 s)))
  15347. (if (org-pos-in-match-range pos 2)
  15348. (setq h (+ h n))
  15349. (setq n (* dm (org-no-warnings (signum n))))
  15350. (when (not (= 0 (setq rem (% m dm))))
  15351. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15352. (setq m (+ m n)))
  15353. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15354. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15355. (setq h (min 24 (max 0 h)))
  15356. (setq ng 1 new (format "-%02d:%02d" h m)))
  15357. ((org-pos-in-match-range pos 6)
  15358. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15359. ((org-pos-in-match-range pos 5)
  15360. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15361. ((org-pos-in-match-range pos 9)
  15362. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15363. ((org-pos-in-match-range pos 8)
  15364. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15365. (when ng
  15366. (setq s (concat
  15367. (substring s 0 (match-beginning ng))
  15368. new
  15369. (substring s (match-end ng))))))
  15370. s))
  15371. (defun org-recenter-calendar (date)
  15372. "If the calendar is visible, recenter it to DATE."
  15373. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15374. (when cwin
  15375. (let ((calendar-move-hook nil))
  15376. (with-selected-window cwin
  15377. (calendar-goto-date (if (listp date) date
  15378. (calendar-gregorian-from-absolute date))))))))
  15379. (defun org-goto-calendar (&optional arg)
  15380. "Go to the Emacs calendar at the current date.
  15381. If there is a time stamp in the current line, go to that date.
  15382. A prefix ARG can be used to force the current date."
  15383. (interactive "P")
  15384. (let ((tsr org-ts-regexp) diff
  15385. (calendar-move-hook nil)
  15386. (calendar-view-holidays-initially-flag nil)
  15387. (calendar-view-diary-initially-flag nil))
  15388. (if (or (org-at-timestamp-p)
  15389. (save-excursion
  15390. (beginning-of-line 1)
  15391. (looking-at (concat ".*" tsr))))
  15392. (let ((d1 (time-to-days (current-time)))
  15393. (d2 (time-to-days
  15394. (org-time-string-to-time (match-string 1)))))
  15395. (setq diff (- d2 d1))))
  15396. (calendar)
  15397. (calendar-goto-today)
  15398. (if (and diff (not arg)) (calendar-forward-day diff))))
  15399. (defun org-get-date-from-calendar ()
  15400. "Return a list (month day year) of date at point in calendar."
  15401. (with-current-buffer "*Calendar*"
  15402. (save-match-data
  15403. (calendar-cursor-to-date))))
  15404. (defun org-date-from-calendar ()
  15405. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15406. If there is already a time stamp at the cursor position, update it."
  15407. (interactive)
  15408. (if (org-at-timestamp-p t)
  15409. (org-timestamp-change 0 'calendar)
  15410. (let ((cal-date (org-get-date-from-calendar)))
  15411. (org-insert-time-stamp
  15412. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15413. (defun org-minutes-to-clocksum-string (m)
  15414. "Format number of minutes as a clocksum string.
  15415. The format is determined by `org-time-clocksum-format',
  15416. `org-time-clocksum-use-fractional' and
  15417. `org-time-clocksum-fractional-format'."
  15418. (let ((clocksum "") fmt n)
  15419. ;; fractional format
  15420. (if org-time-clocksum-use-fractional
  15421. (cond
  15422. ;; single format string
  15423. ((stringp org-time-clocksum-fractional-format)
  15424. (format org-time-clocksum-fractional-format (/ m 60.0)))
  15425. ;; choice of fractional formats for different time units
  15426. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15427. (> (/ (truncate m) (* 365 24 60)) 0))
  15428. (format fmt (/ m (* 365 24 60.0))))
  15429. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15430. (> (/ (truncate m) (* 30 24 60)) 0))
  15431. (format fmt (/ m (* 30 24 60.0))))
  15432. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15433. (> (/ (truncate m) (* 7 24 60)) 0))
  15434. (format fmt (/ m (* 7 24 60.0))))
  15435. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15436. (> (/ (truncate m) (* 24 60)) 0))
  15437. (format fmt (/ m (* 24 60.0))))
  15438. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15439. (> (/ (truncate m) 60) 0))
  15440. (format fmt (/ m 60.0)))
  15441. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15442. (format fmt m))
  15443. ;; fall back to smallest time unit with a format
  15444. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15445. (format fmt (/ m 60.0)))
  15446. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15447. (format fmt (/ m (* 24 60.0))))
  15448. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15449. (format fmt (/ m (* 7 24 60.0))))
  15450. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15451. (format fmt (/ m (* 30 24 60.0))))
  15452. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15453. (format fmt (/ m (* 365 24 60.0)))))
  15454. ;; standard (non-fractional) format, with single format string
  15455. (if (stringp org-time-clocksum-format)
  15456. (format org-time-clocksum-format (setq n (/ m 60)) (- m (* 60 n)))
  15457. ;; separate formats components
  15458. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15459. (or (> (setq n (/ (truncate m) (* 365 24 60))) 0)
  15460. (plist-get org-time-clocksum-format :require-years))
  15461. (setq clocksum (concat clocksum (format fmt n))
  15462. m (- m (* n 365 24 60))))
  15463. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15464. (or (> (setq n (/ (truncate m) (* 30 24 60))) 0)
  15465. (plist-get org-time-clocksum-format :require-months))
  15466. (setq clocksum (concat clocksum (format fmt n))
  15467. m (- m (* n 30 24 60))))
  15468. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15469. (or (> (setq n (/ (truncate m) (* 7 24 60))) 0)
  15470. (plist-get org-time-clocksum-format :require-weeks))
  15471. (setq clocksum (concat clocksum (format fmt n))
  15472. m (- m (* n 7 24 60))))
  15473. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15474. (or (> (setq n (/ (truncate m) (* 24 60))) 0)
  15475. (plist-get org-time-clocksum-format :require-days))
  15476. (setq clocksum (concat clocksum (format fmt n))
  15477. m (- m (* n 24 60))))
  15478. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15479. (or (> (setq n (/ (truncate m) 60)) 0)
  15480. (plist-get org-time-clocksum-format :require-hours))
  15481. (setq clocksum (concat clocksum (format fmt n))
  15482. m (- m (* n 60))))
  15483. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15484. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15485. (setq clocksum (concat clocksum (format fmt m))))
  15486. ;; return formatted time duration
  15487. clocksum))))
  15488. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15489. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15490. "Org mode version 8.0")
  15491. (defun org-hours-to-clocksum-string (n)
  15492. (org-minutes-to-clocksum-string (* n 60)))
  15493. (defun org-hh:mm-string-to-minutes (s)
  15494. "Convert a string H:MM to a number of minutes.
  15495. If the string is just a number, interpret it as minutes.
  15496. In fact, the first hh:mm or number in the string will be taken,
  15497. there can be extra stuff in the string.
  15498. If no number is found, the return value is 0."
  15499. (cond
  15500. ((integerp s) s)
  15501. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15502. (+ (* (string-to-number (match-string 1 s)) 60)
  15503. (string-to-number (match-string 2 s))))
  15504. ((string-match "\\([0-9]+\\)" s)
  15505. (string-to-number (match-string 1 s)))
  15506. (t 0)))
  15507. (defcustom org-effort-durations
  15508. `(("h" . 60)
  15509. ("d" . ,(* 60 8))
  15510. ("w" . ,(* 60 8 5))
  15511. ("m" . ,(* 60 8 5 4))
  15512. ("y" . ,(* 60 8 5 40)))
  15513. "Conversion factor to minutes for an effort modifier.
  15514. Each entry has the form (MODIFIER . MINUTES).
  15515. In an effort string, a number followed by MODIFIER is multiplied
  15516. by the specified number of MINUTES to obtain an effort in
  15517. minutes.
  15518. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15519. effort string \"2hours\" is equivalent to 120 minutes."
  15520. :group 'org-agenda
  15521. :version "24.1"
  15522. :type '(alist :key-type (string :tag "Modifier")
  15523. :value-type (number :tag "Minutes")))
  15524. (defcustom org-image-actual-width t
  15525. "Should we use the actual width of images when inlining them?
  15526. When set to `t', always use the image width.
  15527. When set to a number, use imagemagick (when available) to set
  15528. the image's width to this value.
  15529. When set to a number in a list, try to get the width from the
  15530. #+ATTR.* keyword if it matches a width specification like
  15531. width=\"[0-9]+\", and fall back on that number if none is found.
  15532. When set to nil, try to get the width from an #+ATTR.* keyword
  15533. and fall back on the original width if none is found.
  15534. This requires Emacs >= 24.1, build with imagemagick support."
  15535. :group 'org-appearance
  15536. :version "24.3"
  15537. :type '(choice
  15538. (const :tag "Use the image width" t)
  15539. (integer :tag "Use a number of pixels")
  15540. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15541. (const :tag "Use #+ATTR* or don't resize" nil)))
  15542. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15543. "Convert a duration string S to minutes.
  15544. A bare number is interpreted as minutes, modifiers can be set by
  15545. customizing `org-effort-durations' (which see).
  15546. Entries containing a colon are interpreted as H:MM by
  15547. `org-hh:mm-string-to-minutes'."
  15548. (let ((result 0)
  15549. (re (concat "\\([0-9.]+\\) *\\("
  15550. (regexp-opt (mapcar 'car org-effort-durations))
  15551. "\\)")))
  15552. (while (string-match re s)
  15553. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15554. (string-to-number (match-string 1 s))))
  15555. (setq s (replace-match "" nil t s)))
  15556. (setq result (floor result))
  15557. (incf result (org-hh:mm-string-to-minutes s))
  15558. (if output-to-string (number-to-string result) result)))
  15559. ;;;; Files
  15560. (defun org-save-all-org-buffers ()
  15561. "Save all Org-mode buffers without user confirmation."
  15562. (interactive)
  15563. (message "Saving all Org-mode buffers...")
  15564. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15565. (when (featurep 'org-id) (org-id-locations-save))
  15566. (message "Saving all Org-mode buffers... done"))
  15567. (defun org-revert-all-org-buffers ()
  15568. "Revert all Org-mode buffers.
  15569. Prompt for confirmation when there are unsaved changes.
  15570. Be sure you know what you are doing before letting this function
  15571. overwrite your changes.
  15572. This function is useful in a setup where one tracks org files
  15573. with a version control system, to revert on one machine after pulling
  15574. changes from another. I believe the procedure must be like this:
  15575. 1. M-x org-save-all-org-buffers
  15576. 2. Pull changes from the other machine, resolve conflicts
  15577. 3. M-x org-revert-all-org-buffers"
  15578. (interactive)
  15579. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15580. (error "Abort"))
  15581. (save-excursion
  15582. (save-window-excursion
  15583. (mapc
  15584. (lambda (b)
  15585. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15586. (with-current-buffer b buffer-file-name))
  15587. (org-pop-to-buffer-same-window b)
  15588. (revert-buffer t 'no-confirm)))
  15589. (buffer-list))
  15590. (when (and (featurep 'org-id) org-id-track-globally)
  15591. (org-id-locations-load)))))
  15592. ;;;; Agenda files
  15593. ;;;###autoload
  15594. (defun org-switchb (&optional arg)
  15595. "Switch between Org buffers.
  15596. With one prefix argument, restrict available buffers to files.
  15597. With two prefix arguments, restrict available buffers to agenda files.
  15598. Defaults to `iswitchb' for buffer name completion.
  15599. Set `org-completion-use-ido' to make it use ido instead."
  15600. (interactive "P")
  15601. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15602. ((equal arg '(16)) (org-buffer-list 'agenda))
  15603. (t (org-buffer-list))))
  15604. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15605. (org-completion-use-ido org-completion-use-ido))
  15606. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15607. (setq org-completion-use-iswitchb t))
  15608. (org-pop-to-buffer-same-window
  15609. (org-icompleting-read "Org buffer: "
  15610. (mapcar 'list (mapcar 'buffer-name blist))
  15611. nil t))))
  15612. ;;; Define some older names previously used for this functionality
  15613. ;;;###autoload
  15614. (defalias 'org-ido-switchb 'org-switchb)
  15615. ;;;###autoload
  15616. (defalias 'org-iswitchb 'org-switchb)
  15617. (defun org-buffer-list (&optional predicate exclude-tmp)
  15618. "Return a list of Org buffers.
  15619. PREDICATE can be `export', `files' or `agenda'.
  15620. export restrict the list to Export buffers.
  15621. files restrict the list to buffers visiting Org files.
  15622. agenda restrict the list to buffers visiting agenda files.
  15623. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15624. (let* ((bfn nil)
  15625. (agenda-files (and (eq predicate 'agenda)
  15626. (mapcar 'file-truename (org-agenda-files t))))
  15627. (filter
  15628. (cond
  15629. ((eq predicate 'files)
  15630. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15631. ((eq predicate 'export)
  15632. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15633. ((eq predicate 'agenda)
  15634. (lambda (b)
  15635. (with-current-buffer b
  15636. (and (derived-mode-p 'org-mode)
  15637. (setq bfn (buffer-file-name b))
  15638. (member (file-truename bfn) agenda-files)))))
  15639. (t (lambda (b) (with-current-buffer b
  15640. (or (derived-mode-p 'org-mode)
  15641. (string-match "\*Org .*Export"
  15642. (buffer-name b)))))))))
  15643. (delq nil
  15644. (mapcar
  15645. (lambda(b)
  15646. (if (and (funcall filter b)
  15647. (or (not exclude-tmp)
  15648. (not (string-match "tmp" (buffer-name b)))))
  15649. b
  15650. nil))
  15651. (buffer-list)))))
  15652. (defun org-agenda-files (&optional unrestricted archives)
  15653. "Get the list of agenda files.
  15654. Optional UNRESTRICTED means return the full list even if a restriction
  15655. is currently in place.
  15656. When ARCHIVES is t, include all archive files that are really being
  15657. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15658. `org-agenda-archives-mode' is t."
  15659. (let ((files
  15660. (cond
  15661. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15662. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15663. ((listp org-agenda-files) org-agenda-files)
  15664. (t (error "Invalid value of `org-agenda-files'")))))
  15665. (setq files (apply 'append
  15666. (mapcar (lambda (f)
  15667. (if (file-directory-p f)
  15668. (directory-files
  15669. f t org-agenda-file-regexp)
  15670. (list f)))
  15671. files)))
  15672. (when org-agenda-skip-unavailable-files
  15673. (setq files (delq nil
  15674. (mapcar (function
  15675. (lambda (file)
  15676. (and (file-readable-p file) file)))
  15677. files))))
  15678. (when (or (eq archives t)
  15679. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15680. (setq files (org-add-archive-files files)))
  15681. files))
  15682. (defun org-agenda-file-p (&optional file)
  15683. "Return non-nil, if FILE is an agenda file.
  15684. If FILE is omitted, use the file associated with the current
  15685. buffer."
  15686. (member (or file (buffer-file-name))
  15687. (org-agenda-files t)))
  15688. (defun org-edit-agenda-file-list ()
  15689. "Edit the list of agenda files.
  15690. Depending on setup, this either uses customize to edit the variable
  15691. `org-agenda-files', or it visits the file that is holding the list. In the
  15692. latter case, the buffer is set up in a way that saving it automatically kills
  15693. the buffer and restores the previous window configuration."
  15694. (interactive)
  15695. (if (stringp org-agenda-files)
  15696. (let ((cw (current-window-configuration)))
  15697. (find-file org-agenda-files)
  15698. (org-set-local 'org-window-configuration cw)
  15699. (org-add-hook 'after-save-hook
  15700. (lambda ()
  15701. (set-window-configuration
  15702. (prog1 org-window-configuration
  15703. (kill-buffer (current-buffer))))
  15704. (org-install-agenda-files-menu)
  15705. (message "New agenda file list installed"))
  15706. nil 'local)
  15707. (message "%s" (substitute-command-keys
  15708. "Edit list and finish with \\[save-buffer]")))
  15709. (customize-variable 'org-agenda-files)))
  15710. (defun org-store-new-agenda-file-list (list)
  15711. "Set new value for the agenda file list and save it correctly."
  15712. (if (stringp org-agenda-files)
  15713. (let ((fe (org-read-agenda-file-list t)) b u)
  15714. (while (setq b (find-buffer-visiting org-agenda-files))
  15715. (kill-buffer b))
  15716. (with-temp-file org-agenda-files
  15717. (insert
  15718. (mapconcat
  15719. (lambda (f) ;; Keep un-expanded entries.
  15720. (if (setq u (assoc f fe))
  15721. (cdr u)
  15722. f))
  15723. list "\n")
  15724. "\n")))
  15725. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15726. (org-insert-mode-line-in-empty-file nil))
  15727. (setq org-agenda-files list)
  15728. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15729. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15730. "Read the list of agenda files from a file.
  15731. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15732. filenames, used by `org-store-new-agenda-file-list' to write back
  15733. un-expanded file names."
  15734. (when (file-directory-p org-agenda-files)
  15735. (error "`org-agenda-files' cannot be a single directory"))
  15736. (when (stringp org-agenda-files)
  15737. (with-temp-buffer
  15738. (insert-file-contents org-agenda-files)
  15739. (mapcar
  15740. (lambda (f)
  15741. (let ((e (expand-file-name (substitute-in-file-name f)
  15742. org-directory)))
  15743. (if pair-with-expansion
  15744. (cons e f)
  15745. e)))
  15746. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15747. ;;;###autoload
  15748. (defun org-cycle-agenda-files ()
  15749. "Cycle through the files in `org-agenda-files'.
  15750. If the current buffer visits an agenda file, find the next one in the list.
  15751. If the current buffer does not, find the first agenda file."
  15752. (interactive)
  15753. (let* ((fs (org-agenda-files t))
  15754. (files (append fs (list (car fs))))
  15755. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15756. file)
  15757. (unless files (error "No agenda files"))
  15758. (catch 'exit
  15759. (while (setq file (pop files))
  15760. (if (equal (file-truename file) tcf)
  15761. (when (car files)
  15762. (find-file (car files))
  15763. (throw 'exit t))))
  15764. (find-file (car fs)))
  15765. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15766. (defun org-agenda-file-to-front (&optional to-end)
  15767. "Move/add the current file to the top of the agenda file list.
  15768. If the file is not present in the list, it is added to the front. If it is
  15769. present, it is moved there. With optional argument TO-END, add/move to the
  15770. end of the list."
  15771. (interactive "P")
  15772. (let ((org-agenda-skip-unavailable-files nil)
  15773. (file-alist (mapcar (lambda (x)
  15774. (cons (file-truename x) x))
  15775. (org-agenda-files t)))
  15776. (ctf (file-truename
  15777. (or buffer-file-name
  15778. (error "Please save the current buffer to a file"))))
  15779. x had)
  15780. (setq x (assoc ctf file-alist) had x)
  15781. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15782. (if to-end
  15783. (setq file-alist (append (delq x file-alist) (list x)))
  15784. (setq file-alist (cons x (delq x file-alist))))
  15785. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15786. (org-install-agenda-files-menu)
  15787. (message "File %s to %s of agenda file list"
  15788. (if had "moved" "added") (if to-end "end" "front"))))
  15789. (defun org-remove-file (&optional file)
  15790. "Remove current file from the list of files in variable `org-agenda-files'.
  15791. These are the files which are being checked for agenda entries.
  15792. Optional argument FILE means use this file instead of the current."
  15793. (interactive)
  15794. (let* ((org-agenda-skip-unavailable-files nil)
  15795. (file (or file buffer-file-name
  15796. (error "Current buffer does not visit a file")))
  15797. (true-file (file-truename file))
  15798. (afile (abbreviate-file-name file))
  15799. (files (delq nil (mapcar
  15800. (lambda (x)
  15801. (if (equal true-file
  15802. (file-truename x))
  15803. nil x))
  15804. (org-agenda-files t)))))
  15805. (if (not (= (length files) (length (org-agenda-files t))))
  15806. (progn
  15807. (org-store-new-agenda-file-list files)
  15808. (org-install-agenda-files-menu)
  15809. (message "Removed file: %s" afile))
  15810. (message "File was not in list: %s (not removed)" afile))))
  15811. (defun org-file-menu-entry (file)
  15812. (vector file (list 'find-file file) t))
  15813. (defun org-check-agenda-file (file)
  15814. "Make sure FILE exists. If not, ask user what to do."
  15815. (when (not (file-exists-p file))
  15816. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15817. (abbreviate-file-name file))
  15818. (let ((r (downcase (read-char-exclusive))))
  15819. (cond
  15820. ((equal r ?r)
  15821. (org-remove-file file)
  15822. (throw 'nextfile t))
  15823. (t (error "Abort"))))))
  15824. (defun org-get-agenda-file-buffer (file)
  15825. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15826. it to the list of buffers which might be released later."
  15827. (let ((buf (org-find-base-buffer-visiting file)))
  15828. (if buf
  15829. buf ; just return it
  15830. ;; Make a new buffer and remember it
  15831. (setq buf (find-file-noselect file))
  15832. (if buf (push buf org-agenda-new-buffers))
  15833. buf)))
  15834. (defun org-release-buffers (blist)
  15835. "Release all buffers in list, asking the user for confirmation when needed.
  15836. When a buffer is unmodified, it is just killed. When modified, it is saved
  15837. \(if the user agrees) and then killed."
  15838. (let (buf file)
  15839. (while (setq buf (pop blist))
  15840. (setq file (buffer-file-name buf))
  15841. (when (and (buffer-modified-p buf)
  15842. file
  15843. (y-or-n-p (format "Save file %s? " file)))
  15844. (with-current-buffer buf (save-buffer)))
  15845. (kill-buffer buf))))
  15846. (defun org-agenda-prepare-buffers (files)
  15847. "Create buffers for all agenda files, protect archived trees and comments."
  15848. (interactive)
  15849. (let ((pa '(:org-archived t))
  15850. (pc '(:org-comment t))
  15851. (pall '(:org-archived t :org-comment t))
  15852. (inhibit-read-only t)
  15853. (rea (concat ":" org-archive-tag ":"))
  15854. bmp file re)
  15855. (save-excursion
  15856. (save-restriction
  15857. (while (setq file (pop files))
  15858. (catch 'nextfile
  15859. (if (bufferp file)
  15860. (set-buffer file)
  15861. (org-check-agenda-file file)
  15862. (set-buffer (org-get-agenda-file-buffer file)))
  15863. (widen)
  15864. (setq bmp (buffer-modified-p))
  15865. (org-refresh-category-properties)
  15866. (setq org-todo-keywords-for-agenda
  15867. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15868. (setq org-done-keywords-for-agenda
  15869. (append org-done-keywords-for-agenda org-done-keywords))
  15870. (setq org-todo-keyword-alist-for-agenda
  15871. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15872. (setq org-drawers-for-agenda
  15873. (append org-drawers-for-agenda org-drawers))
  15874. (setq org-tag-alist-for-agenda
  15875. (append org-tag-alist-for-agenda org-tag-alist))
  15876. (save-excursion
  15877. (remove-text-properties (point-min) (point-max) pall)
  15878. (when org-agenda-skip-archived-trees
  15879. (goto-char (point-min))
  15880. (while (re-search-forward rea nil t)
  15881. (if (org-at-heading-p t)
  15882. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15883. (goto-char (point-min))
  15884. (setq re (format org-heading-keyword-regexp-format
  15885. org-comment-string))
  15886. (while (re-search-forward re nil t)
  15887. (add-text-properties
  15888. (match-beginning 0) (org-end-of-subtree t) pc)))
  15889. (set-buffer-modified-p bmp)))))
  15890. (setq org-todo-keywords-for-agenda
  15891. (org-uniquify org-todo-keywords-for-agenda))
  15892. (setq org-todo-keyword-alist-for-agenda
  15893. (org-uniquify org-todo-keyword-alist-for-agenda)
  15894. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15895. ;;;; Embedded LaTeX
  15896. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15897. "Keymap for the minor `org-cdlatex-mode'.")
  15898. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15899. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15900. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15901. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15902. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15903. (defvar org-cdlatex-texmathp-advice-is-done nil
  15904. "Flag remembering if we have applied the advice to texmathp already.")
  15905. (define-minor-mode org-cdlatex-mode
  15906. "Toggle the minor `org-cdlatex-mode'.
  15907. This mode supports entering LaTeX environment and math in LaTeX fragments
  15908. in Org-mode.
  15909. \\{org-cdlatex-mode-map}"
  15910. nil " OCDL" nil
  15911. (when org-cdlatex-mode
  15912. (require 'cdlatex)
  15913. (run-hooks 'cdlatex-mode-hook)
  15914. (cdlatex-compute-tables))
  15915. (unless org-cdlatex-texmathp-advice-is-done
  15916. (setq org-cdlatex-texmathp-advice-is-done t)
  15917. (defadvice texmathp (around org-math-always-on activate)
  15918. "Always return t in org-mode buffers.
  15919. This is because we want to insert math symbols without dollars even outside
  15920. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15921. an embedded LaTeX fragment, let texmathp do its job.
  15922. \\[org-cdlatex-mode-map]"
  15923. (interactive)
  15924. (let (p)
  15925. (cond
  15926. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15927. ((eq this-command 'cdlatex-math-symbol)
  15928. (setq ad-return-value t
  15929. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15930. (t
  15931. (let ((p (org-inside-LaTeX-fragment-p)))
  15932. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15933. (setq ad-return-value t
  15934. texmathp-why '("Org-mode embedded math" . 0))
  15935. (if p ad-do-it)))))))))
  15936. (defun turn-on-org-cdlatex ()
  15937. "Unconditionally turn on `org-cdlatex-mode'."
  15938. (org-cdlatex-mode 1))
  15939. (defun org-inside-LaTeX-fragment-p ()
  15940. "Test if point is inside a LaTeX fragment.
  15941. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15942. sequence appearing also before point.
  15943. Even though the matchers for math are configurable, this function assumes
  15944. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15945. delimiters are skipped when they have been removed by customization.
  15946. The return value is nil, or a cons cell with the delimiter and the
  15947. position of this delimiter.
  15948. This function does a reasonably good job, but can locally be fooled by
  15949. for example currency specifications. For example it will assume being in
  15950. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15951. fragments that are properly closed, but during editing, we have to live
  15952. with the uncertainty caused by missing closing delimiters. This function
  15953. looks only before point, not after."
  15954. (catch 'exit
  15955. (let ((pos (point))
  15956. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15957. (lim (progn
  15958. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15959. (point)))
  15960. dd-on str (start 0) m re)
  15961. (goto-char pos)
  15962. (when dodollar
  15963. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15964. re (nth 1 (assoc "$" org-latex-regexps)))
  15965. (while (string-match re str start)
  15966. (cond
  15967. ((= (match-end 0) (length str))
  15968. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15969. ((= (match-end 0) (- (length str) 5))
  15970. (throw 'exit nil))
  15971. (t (setq start (match-end 0))))))
  15972. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15973. (goto-char pos)
  15974. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15975. (and (match-beginning 2) (throw 'exit nil))
  15976. ;; count $$
  15977. (while (re-search-backward "\\$\\$" lim t)
  15978. (setq dd-on (not dd-on)))
  15979. (goto-char pos)
  15980. (if dd-on (cons "$$" m))))))
  15981. (defun org-inside-latex-macro-p ()
  15982. "Is point inside a LaTeX macro or its arguments?"
  15983. (save-match-data
  15984. (org-in-regexp
  15985. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15986. (defun org-try-cdlatex-tab ()
  15987. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15988. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15989. - inside a LaTeX fragment, or
  15990. - after the first word in a line, where an abbreviation expansion could
  15991. insert a LaTeX environment."
  15992. (when org-cdlatex-mode
  15993. (cond
  15994. ;; Before any word on the line: No expansion possible.
  15995. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15996. ;; Just after first word on the line: Expand it. Make sure it
  15997. ;; cannot happen on headlines, though.
  15998. ((save-excursion
  15999. (skip-chars-backward "a-zA-Z0-9*")
  16000. (skip-chars-backward " \t")
  16001. (and (bolp) (not (org-at-heading-p))))
  16002. (cdlatex-tab) t)
  16003. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16004. (defun org-cdlatex-underscore-caret (&optional arg)
  16005. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16006. Revert to the normal definition outside of these fragments."
  16007. (interactive "P")
  16008. (if (org-inside-LaTeX-fragment-p)
  16009. (call-interactively 'cdlatex-sub-superscript)
  16010. (let (org-cdlatex-mode)
  16011. (call-interactively (key-binding (vector last-input-event))))))
  16012. (defun org-cdlatex-math-modify (&optional arg)
  16013. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16014. Revert to the normal definition outside of these fragments."
  16015. (interactive "P")
  16016. (if (org-inside-LaTeX-fragment-p)
  16017. (call-interactively 'cdlatex-math-modify)
  16018. (let (org-cdlatex-mode)
  16019. (call-interactively (key-binding (vector last-input-event))))))
  16020. (defvar org-latex-fragment-image-overlays nil
  16021. "List of overlays carrying the images of latex fragments.")
  16022. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16023. (defun org-remove-latex-fragment-image-overlays ()
  16024. "Remove all overlays with LaTeX fragment images in current buffer."
  16025. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16026. (setq org-latex-fragment-image-overlays nil))
  16027. (defun org-preview-latex-fragment (&optional subtree)
  16028. "Preview the LaTeX fragment at point, or all locally or globally.
  16029. If the cursor is in a LaTeX fragment, create the image and overlay
  16030. it over the source code. If there is no fragment at point, display
  16031. all fragments in the current text, from one headline to the next. With
  16032. prefix SUBTREE, display all fragments in the current subtree. With a
  16033. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16034. the cursor is before the first headline,
  16035. display all fragments in the buffer.
  16036. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16037. (interactive "P")
  16038. (unless buffer-file-name
  16039. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16040. (org-remove-latex-fragment-image-overlays)
  16041. (save-excursion
  16042. (save-restriction
  16043. (let (beg end at msg)
  16044. (cond
  16045. ((or (equal subtree '(16))
  16046. (not (save-excursion
  16047. (re-search-backward org-outline-regexp-bol nil t))))
  16048. (setq beg (point-min) end (point-max)
  16049. msg "Creating images for buffer...%s"))
  16050. ((equal subtree '(4))
  16051. (org-back-to-heading)
  16052. (setq beg (point) end (org-end-of-subtree t)
  16053. msg "Creating images for subtree...%s"))
  16054. (t
  16055. (if (setq at (org-inside-LaTeX-fragment-p))
  16056. (goto-char (max (point-min) (- (cdr at) 2)))
  16057. (org-back-to-heading))
  16058. (setq beg (point) end (progn (outline-next-heading) (point))
  16059. msg (if at "Creating image...%s"
  16060. "Creating images for entry...%s"))))
  16061. (message msg "")
  16062. (narrow-to-region beg end)
  16063. (goto-char beg)
  16064. (org-format-latex
  16065. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16066. (file-name-nondirectory
  16067. buffer-file-name)))
  16068. default-directory 'overlays msg at 'forbuffer
  16069. org-latex-create-formula-image-program)
  16070. (message msg "done. Use `C-c C-c' to remove images.")))))
  16071. (defvar org-latex-regexps
  16072. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16073. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16074. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16075. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16076. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16077. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16078. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16079. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16080. "Regular expressions for matching embedded LaTeX.")
  16081. (defvar org-export-have-math nil) ;; dynamic scoping
  16082. (defun org-format-latex (prefix &optional dir overlays msg at
  16083. forbuffer processing-type)
  16084. "Replace LaTeX fragments with links to an image, and produce images.
  16085. Some of the options can be changed using the variable
  16086. `org-format-latex-options'."
  16087. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16088. (let* ((prefixnodir (file-name-nondirectory prefix))
  16089. (absprefix (expand-file-name prefix dir))
  16090. (todir (file-name-directory absprefix))
  16091. (opt org-format-latex-options)
  16092. (optnew org-format-latex-options)
  16093. (matchers (plist-get opt :matchers))
  16094. (re-list org-latex-regexps)
  16095. (org-format-latex-header-extra
  16096. (plist-get (org-infile-export-plist) :latex-header-extra))
  16097. (cnt 0) txt hash link beg end re e checkdir
  16098. string
  16099. m n block-type block linkfile movefile ov)
  16100. ;; Check the different regular expressions
  16101. (while (setq e (pop re-list))
  16102. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16103. block (if block-type "\n\n" ""))
  16104. (when (member m matchers)
  16105. (goto-char (point-min))
  16106. (while (re-search-forward re nil t)
  16107. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16108. (not (get-text-property (match-beginning n)
  16109. 'org-protected))
  16110. (or (not overlays)
  16111. (not (eq (get-char-property (match-beginning n)
  16112. 'org-overlay-type)
  16113. 'org-latex-overlay))))
  16114. (setq org-export-have-math t)
  16115. (cond
  16116. ((eq processing-type 'verbatim)
  16117. ;; Leave the text verbatim, just protect it
  16118. (add-text-properties (match-beginning n) (match-end n)
  16119. '(org-protected t)))
  16120. ((eq processing-type 'mathjax)
  16121. ;; Prepare for MathJax processing
  16122. (setq string (match-string n))
  16123. (if (member m '("$" "$1"))
  16124. (save-excursion
  16125. (delete-region (match-beginning n) (match-end n))
  16126. (goto-char (match-beginning n))
  16127. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  16128. "\\)")
  16129. '(org-protected t))))
  16130. (add-text-properties (match-beginning n) (match-end n)
  16131. '(org-protected t))))
  16132. ((or (eq processing-type 'dvipng)
  16133. (eq processing-type 'imagemagick))
  16134. ;; Process to an image
  16135. (setq txt (match-string n)
  16136. beg (match-beginning n) end (match-end n)
  16137. cnt (1+ cnt))
  16138. (let ((face (face-at-point))
  16139. (fg (plist-get opt :foreground))
  16140. (bg (plist-get opt :background))
  16141. print-length print-level) ; make sure full list is printed
  16142. (when forbuffer
  16143. ; Get the colors from the face at point
  16144. (goto-char beg)
  16145. (when (eq fg 'auto)
  16146. (setq fg (face-attribute face :foreground nil 'default)))
  16147. (when (eq bg 'auto)
  16148. (setq bg (face-attribute face :background nil 'default)))
  16149. (setq optnew (copy-sequence opt))
  16150. (plist-put optnew :foreground fg)
  16151. (plist-put optnew :background bg))
  16152. (setq hash (sha1 (prin1-to-string
  16153. (list org-format-latex-header
  16154. org-format-latex-header-extra
  16155. org-export-latex-default-packages-alist
  16156. org-export-latex-packages-alist
  16157. org-format-latex-options
  16158. forbuffer txt fg bg)))
  16159. linkfile (format "%s_%s.png" prefix hash)
  16160. movefile (format "%s_%s.png" absprefix hash)))
  16161. (setq link (concat block "[[file:" linkfile "]]" block))
  16162. (if msg (message msg cnt))
  16163. (goto-char beg)
  16164. (unless checkdir ; make sure the directory exists
  16165. (setq checkdir t)
  16166. (or (file-directory-p todir) (make-directory todir t)))
  16167. (org-create-formula-image
  16168. txt movefile optnew forbuffer processing-type)
  16169. (if overlays
  16170. (progn
  16171. (mapc (lambda (o)
  16172. (if (eq (overlay-get o 'org-overlay-type)
  16173. 'org-latex-overlay)
  16174. (delete-overlay o)))
  16175. (overlays-in beg end))
  16176. (setq ov (make-overlay beg end))
  16177. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16178. (if (featurep 'xemacs)
  16179. (progn
  16180. (overlay-put ov 'invisible t)
  16181. (overlay-put
  16182. ov 'end-glyph
  16183. (make-glyph (vector 'png :file movefile))))
  16184. (overlay-put
  16185. ov 'display
  16186. (list 'image :type 'png :file movefile :ascent 'center)))
  16187. (push ov org-latex-fragment-image-overlays)
  16188. (goto-char end))
  16189. (delete-region beg end)
  16190. (insert (org-add-props link
  16191. (list 'org-latex-src
  16192. (replace-regexp-in-string
  16193. "\"" "" txt)
  16194. 'org-latex-src-embed-type
  16195. (if block-type 'paragraph 'character))))))
  16196. ((eq processing-type 'mathml)
  16197. ;; Process to MathML
  16198. (unless (save-match-data (org-format-latex-mathml-available-p))
  16199. (error "LaTeX to MathML converter not configured"))
  16200. (setq txt (match-string n)
  16201. beg (match-beginning n) end (match-end n)
  16202. cnt (1+ cnt))
  16203. (if msg (message msg cnt))
  16204. (goto-char beg)
  16205. (delete-region beg end)
  16206. (insert (org-format-latex-as-mathml
  16207. txt block-type prefix dir)))
  16208. (t
  16209. (error "Unknown conversion type %s for latex fragments"
  16210. processing-type)))))))))
  16211. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16212. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16213. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16214. sucessful, return the portion between \"<math...> </math>\"
  16215. elements otherwise return nil. When MATHML-FILE is specified,
  16216. write the results in to that file. When invoked as an
  16217. interactive command, prompt for LATEX-FRAG, with initial value
  16218. set to the current active region and echo the results for user
  16219. inspection."
  16220. (interactive (list (let ((frag (when (org-region-active-p)
  16221. (buffer-substring-no-properties
  16222. (region-beginning) (region-end)))))
  16223. (read-string "LaTeX Fragment: " frag nil frag))))
  16224. (unless latex-frag (error "Invalid latex-frag"))
  16225. (let* ((tmp-in-file (file-relative-name
  16226. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16227. (ignore (write-region latex-frag nil tmp-in-file))
  16228. (tmp-out-file (file-relative-name
  16229. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16230. (cmd (format-spec
  16231. org-latex-to-mathml-convert-command
  16232. `((?j . ,(shell-quote-argument
  16233. (expand-file-name org-latex-to-mathml-jar-file)))
  16234. (?I . ,(shell-quote-argument tmp-in-file))
  16235. (?o . ,(shell-quote-argument tmp-out-file)))))
  16236. mathml shell-command-output)
  16237. (when (org-called-interactively-p 'any)
  16238. (unless (org-format-latex-mathml-available-p)
  16239. (error "LaTeX to MathML converter not configured")))
  16240. (message "Running %s" cmd)
  16241. (setq shell-command-output (shell-command-to-string cmd))
  16242. (setq mathml
  16243. (when (file-readable-p tmp-out-file)
  16244. (with-current-buffer (find-file-noselect tmp-out-file t)
  16245. (goto-char (point-min))
  16246. (when (re-search-forward
  16247. (concat
  16248. (regexp-quote
  16249. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16250. "\\(.\\|\n\\)*"
  16251. (regexp-quote "</math>")) nil t)
  16252. (prog1 (match-string 0) (kill-buffer))))))
  16253. (cond
  16254. (mathml
  16255. (setq mathml
  16256. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16257. (when mathml-file
  16258. (write-region mathml nil mathml-file))
  16259. (when (org-called-interactively-p 'any)
  16260. (message mathml)))
  16261. ((message "LaTeX to MathML conversion failed")
  16262. (message shell-command-output)))
  16263. (delete-file tmp-in-file)
  16264. (when (file-exists-p tmp-out-file)
  16265. (delete-file tmp-out-file))
  16266. mathml))
  16267. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16268. prefix &optional dir)
  16269. "Use `org-create-math-formula' but check local cache first."
  16270. (let* ((absprefix (expand-file-name prefix dir))
  16271. (print-length nil) (print-level nil)
  16272. (formula-id (concat
  16273. "formula-"
  16274. (sha1
  16275. (prin1-to-string
  16276. (list latex-frag
  16277. org-latex-to-mathml-convert-command)))))
  16278. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16279. (formula-cache-dir (file-name-directory formula-cache)))
  16280. (unless (file-directory-p formula-cache-dir)
  16281. (make-directory formula-cache-dir t))
  16282. (unless (file-exists-p formula-cache)
  16283. (org-create-math-formula latex-frag formula-cache))
  16284. (if (file-exists-p formula-cache)
  16285. ;; Successful conversion. Return the link to MathML file.
  16286. (org-add-props
  16287. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16288. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16289. 'org-latex-src-embed-type (if latex-frag-type
  16290. 'paragraph 'character)))
  16291. ;; Failed conversion. Return the LaTeX fragment verbatim
  16292. (add-text-properties
  16293. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16294. latex-frag)))
  16295. (defun org-create-formula-image (string tofile options buffer &optional type)
  16296. "Create an image from LaTeX source using dvipng or convert.
  16297. This function calls either `org-create-formula-image-with-dvipng'
  16298. or `org-create-formula-image-with-imagemagick' depending on the
  16299. value of `org-latex-create-formula-image-program' or on the value
  16300. of the optional TYPE variable.
  16301. Note: ultimately these two function should be combined as they
  16302. share a good deal of logic."
  16303. (org-check-external-command
  16304. "latex" "needed to convert LaTeX fragments to images")
  16305. (funcall
  16306. (case (or type org-latex-create-formula-image-program)
  16307. ('dvipng
  16308. (org-check-external-command
  16309. "dvipng" "needed to convert LaTeX fragments to images")
  16310. #'org-create-formula-image-with-dvipng)
  16311. ('imagemagick
  16312. (org-check-external-command
  16313. "convert" "you need to install imagemagick")
  16314. #'org-create-formula-image-with-imagemagick)
  16315. (t (error
  16316. "invalid value of `org-latex-create-formula-image-program'")))
  16317. string tofile options buffer))
  16318. ;; This function borrows from Ganesh Swami's latex2png.el
  16319. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16320. "This calls dvipng."
  16321. (require 'org-latex)
  16322. (let* ((tmpdir (if (featurep 'xemacs)
  16323. (temp-directory)
  16324. temporary-file-directory))
  16325. (texfilebase (make-temp-name
  16326. (expand-file-name "orgtex" tmpdir)))
  16327. (texfile (concat texfilebase ".tex"))
  16328. (dvifile (concat texfilebase ".dvi"))
  16329. (pngfile (concat texfilebase ".png"))
  16330. (fnh (if (featurep 'xemacs)
  16331. (font-height (face-font 'default))
  16332. (face-attribute 'default :height nil)))
  16333. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16334. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16335. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16336. "Black"))
  16337. (bg (or (plist-get options (if buffer :background :html-background))
  16338. "Transparent")))
  16339. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16340. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16341. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16342. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16343. (with-temp-file texfile
  16344. (insert (org-splice-latex-header
  16345. org-format-latex-header
  16346. org-export-latex-default-packages-alist
  16347. org-export-latex-packages-alist t
  16348. org-format-latex-header-extra))
  16349. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16350. (require 'org-latex)
  16351. (org-export-latex-fix-inputenc))
  16352. (let ((dir default-directory))
  16353. (condition-case nil
  16354. (progn
  16355. (cd tmpdir)
  16356. (call-process "latex" nil nil nil texfile))
  16357. (error nil))
  16358. (cd dir))
  16359. (if (not (file-exists-p dvifile))
  16360. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16361. (condition-case nil
  16362. (if (featurep 'xemacs)
  16363. (call-process "dvipng" nil nil nil
  16364. "-fg" fg "-bg" bg
  16365. "-T" "tight"
  16366. "-o" pngfile
  16367. dvifile)
  16368. (call-process "dvipng" nil nil nil
  16369. "-fg" fg "-bg" bg
  16370. "-D" dpi
  16371. ;;"-x" scale "-y" scale
  16372. "-T" "tight"
  16373. "-o" pngfile
  16374. dvifile))
  16375. (error nil))
  16376. (if (not (file-exists-p pngfile))
  16377. (if org-format-latex-signal-error
  16378. (error "Failed to create png file from %s" texfile)
  16379. (message "Failed to create png file from %s" texfile)
  16380. nil)
  16381. ;; Use the requested file name and clean up
  16382. (copy-file pngfile tofile 'replace)
  16383. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16384. (if (file-exists-p (concat texfilebase e))
  16385. (delete-file (concat texfilebase e))))
  16386. pngfile))))
  16387. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16388. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16389. "This calls convert, which is included into imagemagick."
  16390. (require 'org-latex)
  16391. (let* ((tmpdir (if (featurep 'xemacs)
  16392. (temp-directory)
  16393. temporary-file-directory))
  16394. (texfilebase (make-temp-name
  16395. (expand-file-name "orgtex" tmpdir)))
  16396. (texfile (concat texfilebase ".tex"))
  16397. (pdffile (concat texfilebase ".pdf"))
  16398. (pngfile (concat texfilebase ".png"))
  16399. (fnh (if (featurep 'xemacs)
  16400. (font-height (face-font 'default))
  16401. (face-attribute 'default :height nil)))
  16402. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16403. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16404. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16405. "black"))
  16406. (bg (or (plist-get options (if buffer :background :html-background))
  16407. "white")))
  16408. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16409. (setq fg (org-latex-color-format fg)))
  16410. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16411. (setq bg (org-latex-color-format
  16412. (if (string= bg "Transparent") "white" bg))))
  16413. (with-temp-file texfile
  16414. (insert (org-splice-latex-header
  16415. org-format-latex-header
  16416. org-export-latex-default-packages-alist
  16417. org-export-latex-packages-alist t
  16418. org-format-latex-header-extra))
  16419. (insert "\n\\begin{document}\n"
  16420. "\\definecolor{fg}{rgb}{" fg "}\n"
  16421. "\\definecolor{bg}{rgb}{" bg "}\n"
  16422. "\n\\pagecolor{bg}\n"
  16423. "\n{\\color{fg}\n"
  16424. string
  16425. "\n}\n"
  16426. "\n\\end{document}\n" )
  16427. (require 'org-latex)
  16428. (org-export-latex-fix-inputenc))
  16429. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16430. (condition-case nil
  16431. (progn
  16432. (cd tmpdir)
  16433. (setq cmds org-latex-to-pdf-process)
  16434. (while cmds
  16435. (setq latex-frags-cmds (pop cmds))
  16436. (if (listp latex-frags-cmds)
  16437. (setq cmds nil)
  16438. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16439. (while latex-frags-cmds
  16440. (setq cmd (pop latex-frags-cmds))
  16441. (while (string-match "%b" cmd)
  16442. (setq cmd (replace-match
  16443. (save-match-data
  16444. (shell-quote-argument texfile))
  16445. t t cmd)))
  16446. (while (string-match "%f" cmd)
  16447. (setq cmd (replace-match
  16448. (save-match-data
  16449. (shell-quote-argument (file-name-nondirectory texfile)))
  16450. t t cmd)))
  16451. (while (string-match "%o" cmd)
  16452. (setq cmd (replace-match
  16453. (save-match-data
  16454. (shell-quote-argument (file-name-directory texfile)))
  16455. t t cmd)))
  16456. (setq cmd (split-string cmd))
  16457. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16458. (error nil))
  16459. (cd dir))
  16460. (if (not (file-exists-p pdffile))
  16461. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16462. (condition-case nil
  16463. (if (featurep 'xemacs)
  16464. (call-process "convert" nil nil nil
  16465. "-density" "96"
  16466. "-trim"
  16467. "-antialias"
  16468. pdffile
  16469. "-quality" "100"
  16470. ;; "-sharpen" "0x1.0"
  16471. pngfile)
  16472. (call-process "convert" nil nil nil
  16473. "-density" dpi
  16474. "-trim"
  16475. "-antialias"
  16476. pdffile
  16477. "-quality" "100"
  16478. ; "-sharpen" "0x1.0"
  16479. pngfile))
  16480. (error nil))
  16481. (if (not (file-exists-p pngfile))
  16482. (if org-format-latex-signal-error
  16483. (error "Failed to create png file from %s" texfile)
  16484. (message "Failed to create png file from %s" texfile)
  16485. nil)
  16486. ;; Use the requested file name and clean up
  16487. (copy-file pngfile tofile 'replace)
  16488. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16489. (if (file-exists-p (concat texfilebase e))
  16490. (delete-file (concat texfilebase e))))
  16491. pngfile))))
  16492. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16493. "Fill a LaTeX header template TPL.
  16494. In the template, the following place holders will be recognized:
  16495. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16496. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16497. [PACKAGES] \\usepackage statements for PKG
  16498. [NO-PACKAGES] do not include PKG
  16499. [EXTRA] the string EXTRA
  16500. [NO-EXTRA] do not include EXTRA
  16501. For backward compatibility, if both the positive and the negative place
  16502. holder is missing, the positive one (without the \"NO-\") will be
  16503. assumed to be present at the end of the template.
  16504. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16505. EXTRA is a string.
  16506. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16507. (let (rpl (end ""))
  16508. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16509. (setq rpl (if (or (match-end 1) (not def-pkg))
  16510. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16511. tpl (replace-match rpl t t tpl))
  16512. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16513. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16514. (setq rpl (if (or (match-end 1) (not pkg))
  16515. "" (org-latex-packages-to-string pkg snippets-p t))
  16516. tpl (replace-match rpl t t tpl))
  16517. (if pkg (setq end
  16518. (concat end "\n"
  16519. (org-latex-packages-to-string pkg snippets-p)))))
  16520. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16521. (setq rpl (if (or (match-end 1) (not extra))
  16522. "" (concat extra "\n"))
  16523. tpl (replace-match rpl t t tpl))
  16524. (if (and extra (string-match "\\S-" extra))
  16525. (setq end (concat end "\n" extra))))
  16526. (if (string-match "\\S-" end)
  16527. (concat tpl "\n" end)
  16528. tpl)))
  16529. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16530. "Turn an alist of packages into a string with the \\usepackage macros."
  16531. (setq pkg (mapconcat (lambda(p)
  16532. (cond
  16533. ((stringp p) p)
  16534. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16535. (format "%% Package %s omitted" (cadr p)))
  16536. ((equal "" (car p))
  16537. (format "\\usepackage{%s}" (cadr p)))
  16538. (t
  16539. (format "\\usepackage[%s]{%s}"
  16540. (car p) (cadr p)))))
  16541. pkg
  16542. "\n"))
  16543. (if newline (concat pkg "\n") pkg))
  16544. (defun org-dvipng-color (attr)
  16545. "Return a RGB color specification for dvipng."
  16546. (apply 'format "rgb %s %s %s"
  16547. (mapcar 'org-normalize-color
  16548. (if (featurep 'xemacs)
  16549. (color-rgb-components
  16550. (face-property 'default
  16551. (cond ((eq attr :foreground) 'foreground)
  16552. ((eq attr :background) 'background))))
  16553. (color-values (face-attribute 'default attr nil))))))
  16554. (defun org-dvipng-color-format (color-name)
  16555. "Convert COLOR-NAME to a RGB color value for dvipng."
  16556. (apply 'format "rgb %s %s %s"
  16557. (mapcar 'org-normalize-color
  16558. (color-values color-name))))
  16559. (defun org-latex-color (attr)
  16560. "Return a RGB color for the LaTeX color package."
  16561. (apply 'format "%s,%s,%s"
  16562. (mapcar 'org-normalize-color
  16563. (if (featurep 'xemacs)
  16564. (color-rgb-components
  16565. (face-property 'default
  16566. (cond ((eq attr :foreground) 'foreground)
  16567. ((eq attr :background) 'background))))
  16568. (color-values (face-attribute 'default attr nil))))))
  16569. (defun org-latex-color-format (color-name)
  16570. "Convert COLOR-NAME to a RGB color value."
  16571. (apply 'format "%s,%s,%s"
  16572. (mapcar 'org-normalize-color
  16573. (color-values color-name))))
  16574. (defun org-normalize-color (value)
  16575. "Return string to be used as color value for an RGB component."
  16576. (format "%g" (/ value 65535.0)))
  16577. ;; Image display
  16578. (defvar org-inline-image-overlays nil)
  16579. (make-variable-buffer-local 'org-inline-image-overlays)
  16580. (defun org-toggle-inline-images (&optional include-linked)
  16581. "Toggle the display of inline images.
  16582. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16583. (interactive "P")
  16584. (if org-inline-image-overlays
  16585. (progn
  16586. (org-remove-inline-images)
  16587. (message "Inline image display turned off"))
  16588. (org-display-inline-images include-linked)
  16589. (if (and (org-called-interactively-p)
  16590. org-inline-image-overlays)
  16591. (message "%d images displayed inline"
  16592. (length org-inline-image-overlays))
  16593. (message "No images to display inline"))))
  16594. (defun org-redisplay-inline-images ()
  16595. "Refresh the display of inline images."
  16596. (interactive)
  16597. (if (not org-inline-image-overlays)
  16598. (org-toggle-inline-images)
  16599. (org-toggle-inline-images)
  16600. (org-toggle-inline-images)))
  16601. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16602. "Display inline images.
  16603. Normally only links without a description part are inlined, because this
  16604. is how it will work for export. When INCLUDE-LINKED is set, also links
  16605. with a description part will be inlined. This can be nice for a quick
  16606. look at those images, but it does not reflect what exported files will look
  16607. like.
  16608. When REFRESH is set, refresh existing images between BEG and END.
  16609. This will create new image displays only if necessary.
  16610. BEG and END default to the buffer boundaries."
  16611. (interactive "P")
  16612. (unless refresh
  16613. (org-remove-inline-images)
  16614. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16615. (save-excursion
  16616. (save-restriction
  16617. (widen)
  16618. (setq beg (or beg (point-min)) end (or end (point-max)))
  16619. (goto-char beg)
  16620. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16621. (substring (org-image-file-name-regexp) 0 -2)
  16622. "\\)\\]" (if include-linked "" "\\]")))
  16623. old file ov img type attrwidth width)
  16624. (while (re-search-forward re end t)
  16625. (setq old (get-char-property-and-overlay (match-beginning 1)
  16626. 'org-image-overlay)
  16627. file (expand-file-name
  16628. (concat (or (match-string 3) "") (match-string 4))))
  16629. (when (image-type-available-p 'imagemagick)
  16630. (setq attrwidth (if (or (listp org-image-actual-width)
  16631. (null org-image-actual-width))
  16632. (save-excursion
  16633. (save-match-data
  16634. (when (re-search-backward
  16635. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16636. (save-excursion
  16637. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16638. (string-to-number (match-string 1))))))
  16639. width (cond ((eq org-image-actual-width t) nil)
  16640. ((null org-image-actual-width) attrwidth)
  16641. ((numberp org-image-actual-width)
  16642. org-image-actual-width)
  16643. ((listp org-image-actual-width)
  16644. (or attrwidth (car org-image-actual-width))))
  16645. type (if width 'imagemagick)))
  16646. (when (file-exists-p file)
  16647. (if (and (car-safe old) refresh)
  16648. (image-refresh (overlay-get (cdr old) 'display))
  16649. (setq img (save-match-data (create-image file type nil :width width)))
  16650. (when img
  16651. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16652. (overlay-put ov 'display img)
  16653. (overlay-put ov 'face 'default)
  16654. (overlay-put ov 'org-image-overlay t)
  16655. (overlay-put ov 'modification-hooks
  16656. (list 'org-display-inline-remove-overlay))
  16657. (push ov org-inline-image-overlays)))))))))
  16658. (define-obsolete-function-alias
  16659. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16660. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16661. "Remove inline-display overlay if a corresponding region is modified."
  16662. (let ((inhibit-modification-hooks t))
  16663. (when (and ov after)
  16664. (delete ov org-inline-image-overlays)
  16665. (delete-overlay ov))))
  16666. (defun org-remove-inline-images ()
  16667. "Remove inline display of images."
  16668. (interactive)
  16669. (mapc 'delete-overlay org-inline-image-overlays)
  16670. (setq org-inline-image-overlays nil))
  16671. ;;;; Key bindings
  16672. ;; Outline functions from `outline-mode-prefix-map'
  16673. ;; that can be remapped in Org:
  16674. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16675. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16676. (define-key org-mode-map [remap outline-forward-same-level]
  16677. 'org-forward-heading-same-level)
  16678. (define-key org-mode-map [remap outline-backward-same-level]
  16679. 'org-backward-heading-same-level)
  16680. (define-key org-mode-map [remap show-branches]
  16681. 'org-kill-note-or-show-branches)
  16682. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16683. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16684. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16685. ;; Outline functions from `outline-mode-prefix-map' that can not
  16686. ;; be remapped in Org:
  16687. ;;
  16688. ;; - the column "key binding" shows whether the Outline function is still
  16689. ;; available in Org mode on the same key that it has been bound to in
  16690. ;; Outline mode:
  16691. ;; - "overridden": key used for a different functionality in Org mode
  16692. ;; - else: key still bound to the same Outline function in Org mode
  16693. ;;
  16694. ;; | Outline function | key binding | Org replacement |
  16695. ;; |------------------------------------+-------------+-----------------------|
  16696. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16697. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16698. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16699. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16700. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16701. ;; | `show-entry' | overridden | no replacement |
  16702. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16703. ;; | `show-branches' | `C-c C-k' | still same function |
  16704. ;; | `show-subtree' | overridden | visibility cycling |
  16705. ;; | `show-all' | overridden | no replacement |
  16706. ;; | `hide-subtree' | overridden | visibility cycling |
  16707. ;; | `hide-body' | overridden | no replacement |
  16708. ;; | `hide-entry' | overridden | visibility cycling |
  16709. ;; | `hide-leaves' | overridden | no replacement |
  16710. ;; | `hide-sublevels' | overridden | no replacement |
  16711. ;; | `hide-other' | overridden | no replacement |
  16712. ;; Make `C-c C-x' a prefix key
  16713. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16714. ;; TAB key with modifiers
  16715. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16716. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16717. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16718. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16719. ;; The following line is necessary under Suse GNU/Linux
  16720. (unless (featurep 'xemacs)
  16721. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16722. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16723. (define-key org-mode-map [backtab] 'org-shifttab)
  16724. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16725. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16726. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16727. ;; Cursor keys with modifiers
  16728. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16729. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16730. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16731. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16732. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16733. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16734. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16735. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16736. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16737. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16738. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16739. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16740. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16741. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16742. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16743. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16744. ;; Babel keys
  16745. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16746. (mapc (lambda (pair)
  16747. (define-key org-babel-map (car pair) (cdr pair)))
  16748. org-babel-key-bindings)
  16749. ;;; Extra keys for tty access.
  16750. ;; We only set them when really needed because otherwise the
  16751. ;; menus don't show the simple keys
  16752. (when (or org-use-extra-keys
  16753. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16754. (not window-system))
  16755. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16756. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16757. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16758. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16759. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16760. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16761. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16762. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16763. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16764. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16765. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16766. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16767. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16768. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16769. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16770. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16771. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16772. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16773. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16774. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16775. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16776. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16777. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16778. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16779. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16780. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16781. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16782. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16783. ;; All the other keys
  16784. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16785. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16786. (if (boundp 'narrow-map)
  16787. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16788. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16789. (if (boundp 'narrow-map)
  16790. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16791. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16792. (if (boundp 'narrow-map)
  16793. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16794. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16795. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16796. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16797. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16798. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16799. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16800. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16801. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16802. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16803. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16804. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16805. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16806. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16807. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16808. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16809. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16810. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16811. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16812. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16813. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16814. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16815. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16816. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16817. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16818. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16819. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16820. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16821. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16822. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16823. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16824. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16825. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16826. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16827. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16828. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16829. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16830. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16831. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16832. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16833. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16834. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16835. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16836. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16837. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16838. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16839. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16840. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16841. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16842. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16843. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16844. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16845. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16846. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16847. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16848. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16849. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16850. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16851. (org-defkey org-mode-map "\C-m" 'org-return)
  16852. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16853. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16854. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16855. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16856. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16857. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16858. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16859. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16860. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16861. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16862. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16863. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16864. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16865. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16866. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16867. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16868. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16869. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16870. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16871. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16872. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16873. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16874. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16875. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16876. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16877. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16878. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16879. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16880. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16881. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16882. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16883. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16884. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16885. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16886. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16887. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16888. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16889. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16890. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16891. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16892. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16893. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16894. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  16895. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16896. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16897. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16898. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16899. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16900. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16901. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16902. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16903. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16904. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16905. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16906. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16907. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16908. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16909. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16910. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16911. (when (featurep 'xemacs)
  16912. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16913. (defconst org-speed-commands-default
  16914. '(
  16915. ("Outline Navigation")
  16916. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16917. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16918. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16919. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16920. ("u" . (org-speed-move-safe 'outline-up-heading))
  16921. ("j" . org-goto)
  16922. ("g" . (org-refile t))
  16923. ("Outline Visibility")
  16924. ("c" . org-cycle)
  16925. ("C" . org-shifttab)
  16926. (" " . org-display-outline-path)
  16927. (":" . org-columns)
  16928. ("Outline Structure Editing")
  16929. ("U" . org-shiftmetaup)
  16930. ("D" . org-shiftmetadown)
  16931. ("r" . org-metaright)
  16932. ("l" . org-metaleft)
  16933. ("R" . org-shiftmetaright)
  16934. ("L" . org-shiftmetaleft)
  16935. ("i" . (progn (forward-char 1) (call-interactively
  16936. 'org-insert-heading-respect-content)))
  16937. ("^" . org-sort)
  16938. ("w" . org-refile)
  16939. ("a" . org-archive-subtree-default-with-confirmation)
  16940. ("." . org-mark-subtree)
  16941. ("#" . org-toggle-comment)
  16942. ("Clock Commands")
  16943. ("I" . org-clock-in)
  16944. ("O" . org-clock-out)
  16945. ("Meta Data Editing")
  16946. ("t" . org-todo)
  16947. ("," . (org-priority))
  16948. ("0" . (org-priority ?\ ))
  16949. ("1" . (org-priority ?A))
  16950. ("2" . (org-priority ?B))
  16951. ("3" . (org-priority ?C))
  16952. (";" . org-set-tags-command)
  16953. ("e" . org-set-effort)
  16954. ("E" . org-inc-effort)
  16955. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16956. (org-entry-put (point) "APPT_WARNTIME" m)))
  16957. ("Agenda Views etc")
  16958. ("v" . org-agenda)
  16959. ("/" . org-sparse-tree)
  16960. ("Misc")
  16961. ("o" . org-open-at-point)
  16962. ("?" . org-speed-command-help)
  16963. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16964. (">" . (org-agenda-remove-restriction-lock))
  16965. )
  16966. "The default speed commands.")
  16967. (defun org-print-speed-command (e)
  16968. (if (> (length (car e)) 1)
  16969. (progn
  16970. (princ "\n")
  16971. (princ (car e))
  16972. (princ "\n")
  16973. (princ (make-string (length (car e)) ?-))
  16974. (princ "\n"))
  16975. (princ (car e))
  16976. (princ " ")
  16977. (if (symbolp (cdr e))
  16978. (princ (symbol-name (cdr e)))
  16979. (prin1 (cdr e)))
  16980. (princ "\n")))
  16981. (defun org-speed-command-help ()
  16982. "Show the available speed commands."
  16983. (interactive)
  16984. (if (not org-use-speed-commands)
  16985. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16986. (with-output-to-temp-buffer "*Help*"
  16987. (princ "User-defined Speed commands\n===========================\n")
  16988. (mapc 'org-print-speed-command org-speed-commands-user)
  16989. (princ "\n")
  16990. (princ "Built-in Speed commands\n=======================\n")
  16991. (mapc 'org-print-speed-command org-speed-commands-default))
  16992. (with-current-buffer "*Help*"
  16993. (setq truncate-lines t))))
  16994. (defun org-speed-move-safe (cmd)
  16995. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16996. If not, return to the original position and throw an error."
  16997. (interactive)
  16998. (let ((pos (point)))
  16999. (call-interactively cmd)
  17000. (unless (and (bolp) (org-at-heading-p))
  17001. (goto-char pos)
  17002. (error "Boundary reached while executing %s" cmd))))
  17003. (defvar org-self-insert-command-undo-counter 0)
  17004. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17005. (defvar org-speed-command nil)
  17006. (define-obsolete-function-alias
  17007. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17008. (defun org-speed-command-activate (keys)
  17009. "Hook for activating single-letter speed commands.
  17010. `org-speed-commands-default' specifies a minimal command set.
  17011. Use `org-speed-commands-user' for further customization."
  17012. (when (or (and (bolp) (looking-at org-outline-regexp))
  17013. (and (functionp org-use-speed-commands)
  17014. (funcall org-use-speed-commands)))
  17015. (cdr (assoc keys (append org-speed-commands-user
  17016. org-speed-commands-default)))))
  17017. (define-obsolete-function-alias
  17018. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17019. (defun org-babel-speed-command-activate (keys)
  17020. "Hook for activating single-letter code block commands."
  17021. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17022. (cdr (assoc keys org-babel-key-bindings))))
  17023. (defcustom org-speed-command-hook
  17024. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17025. "Hook for activating speed commands at strategic locations.
  17026. Hook functions are called in sequence until a valid handler is
  17027. found.
  17028. Each hook takes a single argument, a user-pressed command key
  17029. which is also a `self-insert-command' from the global map.
  17030. Within the hook, examine the cursor position and the command key
  17031. and return nil or a valid handler as appropriate. Handler could
  17032. be one of an interactive command, a function, or a form.
  17033. Set `org-use-speed-commands' to non-nil value to enable this
  17034. hook. The default setting is `org-speed-command-activate'."
  17035. :group 'org-structure
  17036. :version "24.1"
  17037. :type 'hook)
  17038. (defun org-self-insert-command (N)
  17039. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17040. If the cursor is in a table looking at whitespace, the whitespace is
  17041. overwritten, and the table is not marked as requiring realignment."
  17042. (interactive "p")
  17043. (org-check-before-invisible-edit 'insert)
  17044. (cond
  17045. ((and org-use-speed-commands
  17046. (setq org-speed-command
  17047. (run-hook-with-args-until-success
  17048. 'org-speed-command-hook (this-command-keys))))
  17049. (cond
  17050. ((commandp org-speed-command)
  17051. (setq this-command org-speed-command)
  17052. (call-interactively org-speed-command))
  17053. ((functionp org-speed-command)
  17054. (funcall org-speed-command))
  17055. ((and org-speed-command (listp org-speed-command))
  17056. (eval org-speed-command))
  17057. (t (let (org-use-speed-commands)
  17058. (call-interactively 'org-self-insert-command)))))
  17059. ((and
  17060. (org-table-p)
  17061. (progn
  17062. ;; check if we blank the field, and if that triggers align
  17063. (and (featurep 'org-table) org-table-auto-blank-field
  17064. (member last-command
  17065. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17066. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17067. ;; got extra space, this field does not determine column width
  17068. (let (org-table-may-need-update) (org-table-blank-field))
  17069. ;; no extra space, this field may determine column width
  17070. (org-table-blank-field)))
  17071. t)
  17072. (eq N 1)
  17073. (looking-at "[^|\n]* |"))
  17074. (let (org-table-may-need-update)
  17075. (goto-char (1- (match-end 0)))
  17076. (backward-delete-char 1)
  17077. (goto-char (match-beginning 0))
  17078. (self-insert-command N)))
  17079. (t
  17080. (setq org-table-may-need-update t)
  17081. (self-insert-command N)
  17082. (org-fix-tags-on-the-fly)
  17083. (if org-self-insert-cluster-for-undo
  17084. (if (not (eq last-command 'org-self-insert-command))
  17085. (setq org-self-insert-command-undo-counter 1)
  17086. (if (>= org-self-insert-command-undo-counter 20)
  17087. (setq org-self-insert-command-undo-counter 1)
  17088. (and (> org-self-insert-command-undo-counter 0)
  17089. buffer-undo-list (listp buffer-undo-list)
  17090. (not (cadr buffer-undo-list)) ; remove nil entry
  17091. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17092. (setq org-self-insert-command-undo-counter
  17093. (1+ org-self-insert-command-undo-counter))))))))
  17094. (defun org-check-before-invisible-edit (kind)
  17095. "Check is editing if kind KIND would be dangerous with invisible text around.
  17096. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17097. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17098. (if (and org-catch-invisible-edits
  17099. (or (not (boundp 'visible-mode)) (not visible-mode))
  17100. (or (get-char-property (point) 'invisible)
  17101. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17102. ;; OK, we need to take a closer look
  17103. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17104. (invisible-before-point (if (bobp) nil (get-char-property
  17105. (1- (point)) 'invisible)))
  17106. (border-and-ok-direction
  17107. (or
  17108. ;; Check if we are acting predictably before invisible text
  17109. (and invisible-at-point (not invisible-before-point)
  17110. (memq kind '(insert delete-backward)))
  17111. ;; Check if we are acting predictably after invisible text
  17112. ;; This works not well, and I have turned it off. It seems
  17113. ;; better to always show and stop after invisible text.
  17114. ;; (and (not invisible-at-point) invisible-before-point
  17115. ;; (memq kind '(insert delete)))
  17116. )))
  17117. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17118. (memq invisible-before-point '(outline org-hide-block t)))
  17119. (if (eq org-catch-invisible-edits 'error)
  17120. (error "Editing in invisible areas is prohibited - make visible first"))
  17121. (if (and org-custom-properties-overlays
  17122. (y-or-n-p "Display invisible properties in this buffer? "))
  17123. (org-toggle-custom-properties-visibility)
  17124. ;; Make the area visible
  17125. (save-excursion
  17126. (if invisible-before-point
  17127. (goto-char (previous-single-char-property-change
  17128. (point) 'invisible)))
  17129. (org-cycle))
  17130. (cond
  17131. ((eq org-catch-invisible-edits 'show)
  17132. ;; That's it, we do the edit after showing
  17133. (message
  17134. "Unfolding invisible region around point before editing")
  17135. (sit-for 1))
  17136. ((and (eq org-catch-invisible-edits 'smart)
  17137. border-and-ok-direction)
  17138. (message "Unfolding invisible region around point before editing"))
  17139. (t
  17140. ;; Don't do the edit, make the user repeat it in full visibility
  17141. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17142. (defun org-fix-tags-on-the-fly ()
  17143. (when (and (equal (char-after (point-at-bol)) ?*)
  17144. (org-at-heading-p))
  17145. (org-align-tags-here org-tags-column)))
  17146. (defun org-delete-backward-char (N)
  17147. "Like `delete-backward-char', insert whitespace at field end in tables.
  17148. When deleting backwards, in tables this function will insert whitespace in
  17149. front of the next \"|\" separator, to keep the table aligned. The table will
  17150. still be marked for re-alignment if the field did fill the entire column,
  17151. because, in this case the deletion might narrow the column."
  17152. (interactive "p")
  17153. (save-match-data
  17154. (org-check-before-invisible-edit 'delete-backward)
  17155. (if (and (org-table-p)
  17156. (eq N 1)
  17157. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17158. (looking-at ".*?|"))
  17159. (let ((pos (point))
  17160. (noalign (looking-at "[^|\n\r]* |"))
  17161. (c org-table-may-need-update))
  17162. (backward-delete-char N)
  17163. (if (not overwrite-mode)
  17164. (progn
  17165. (skip-chars-forward "^|")
  17166. (insert " ")
  17167. (goto-char (1- pos))))
  17168. ;; noalign: if there were two spaces at the end, this field
  17169. ;; does not determine the width of the column.
  17170. (if noalign (setq org-table-may-need-update c)))
  17171. (backward-delete-char N)
  17172. (org-fix-tags-on-the-fly))))
  17173. (defun org-delete-char (N)
  17174. "Like `delete-char', but insert whitespace at field end in tables.
  17175. When deleting characters, in tables this function will insert whitespace in
  17176. front of the next \"|\" separator, to keep the table aligned. The table will
  17177. still be marked for re-alignment if the field did fill the entire column,
  17178. because, in this case the deletion might narrow the column."
  17179. (interactive "p")
  17180. (save-match-data
  17181. (org-check-before-invisible-edit 'delete)
  17182. (if (and (org-table-p)
  17183. (not (bolp))
  17184. (not (= (char-after) ?|))
  17185. (eq N 1))
  17186. (if (looking-at ".*?|")
  17187. (let ((pos (point))
  17188. (noalign (looking-at "[^|\n\r]* |"))
  17189. (c org-table-may-need-update))
  17190. (replace-match (concat
  17191. (substring (match-string 0) 1 -1)
  17192. " |"))
  17193. (goto-char pos)
  17194. ;; noalign: if there were two spaces at the end, this field
  17195. ;; does not determine the width of the column.
  17196. (if noalign (setq org-table-may-need-update c)))
  17197. (delete-char N))
  17198. (delete-char N)
  17199. (org-fix-tags-on-the-fly))))
  17200. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17201. (put 'org-self-insert-command 'delete-selection t)
  17202. (put 'orgtbl-self-insert-command 'delete-selection t)
  17203. (put 'org-delete-char 'delete-selection 'supersede)
  17204. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17205. (put 'org-yank 'delete-selection 'yank)
  17206. ;; Make `flyspell-mode' delay after some commands
  17207. (put 'org-self-insert-command 'flyspell-delayed t)
  17208. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17209. (put 'org-delete-char 'flyspell-delayed t)
  17210. (put 'org-delete-backward-char 'flyspell-delayed t)
  17211. ;; Make pabbrev-mode expand after org-mode commands
  17212. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17213. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17214. ;; How to do this: Measure non-white length of current string
  17215. ;; If equal to column width, we should realign.
  17216. (defun org-remap (map &rest commands)
  17217. "In MAP, remap the functions given in COMMANDS.
  17218. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17219. (let (new old)
  17220. (while commands
  17221. (setq old (pop commands) new (pop commands))
  17222. (if (fboundp 'command-remapping)
  17223. (org-defkey map (vector 'remap old) new)
  17224. (substitute-key-definition old new map global-map)))))
  17225. (when (eq org-enable-table-editor 'optimized)
  17226. ;; If the user wants maximum table support, we need to hijack
  17227. ;; some standard editing functions
  17228. (org-remap org-mode-map
  17229. 'self-insert-command 'org-self-insert-command
  17230. 'delete-char 'org-delete-char
  17231. 'delete-backward-char 'org-delete-backward-char)
  17232. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17233. (defvar org-ctrl-c-ctrl-c-hook nil
  17234. "Hook for functions attaching themselves to `C-c C-c'.
  17235. This can be used to add additional functionality to the C-c C-c
  17236. key which executes context-dependent commands. This hook is run
  17237. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17238. run after the last test.
  17239. Each function will be called with no arguments. The function
  17240. must check if the context is appropriate for it to act. If yes,
  17241. it should do its thing and then return a non-nil value. If the
  17242. context is wrong, just do nothing and return nil.")
  17243. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17244. "Hook for functions attaching themselves to `C-c C-c'.
  17245. This can be used to add additional functionality to the C-c C-c
  17246. key which executes context-dependent commands. This hook is run
  17247. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17248. before the first test.
  17249. Each function will be called with no arguments. The function
  17250. must check if the context is appropriate for it to act. If yes,
  17251. it should do its thing and then return a non-nil value. If the
  17252. context is wrong, just do nothing and return nil.")
  17253. (defvar org-tab-first-hook nil
  17254. "Hook for functions to attach themselves to TAB.
  17255. See `org-ctrl-c-ctrl-c-hook' for more information.
  17256. This hook runs as the first action when TAB is pressed, even before
  17257. `org-cycle' messes around with the `outline-regexp' to cater for
  17258. inline tasks and plain list item folding.
  17259. If any function in this hook returns t, any other actions that
  17260. would have been caused by TAB (such as table field motion or visibility
  17261. cycling) will not occur.")
  17262. (defvar org-tab-after-check-for-table-hook nil
  17263. "Hook for functions to attach themselves to TAB.
  17264. See `org-ctrl-c-ctrl-c-hook' for more information.
  17265. This hook runs after it has been established that the cursor is not in a
  17266. table, but before checking if the cursor is in a headline or if global cycling
  17267. should be done.
  17268. If any function in this hook returns t, not other actions like visibility
  17269. cycling will be done.")
  17270. (defvar org-tab-after-check-for-cycling-hook nil
  17271. "Hook for functions to attach themselves to TAB.
  17272. See `org-ctrl-c-ctrl-c-hook' for more information.
  17273. This hook runs after it has been established that not table field motion and
  17274. not visibility should be done because of current context. This is probably
  17275. the place where a package like yasnippets can hook in.")
  17276. (defvar org-tab-before-tab-emulation-hook nil
  17277. "Hook for functions to attach themselves to TAB.
  17278. See `org-ctrl-c-ctrl-c-hook' for more information.
  17279. This hook runs after every other options for TAB have been exhausted, but
  17280. before indentation and \t insertion takes place.")
  17281. (defvar org-metaleft-hook nil
  17282. "Hook for functions attaching themselves to `M-left'.
  17283. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17284. (defvar org-metaright-hook nil
  17285. "Hook for functions attaching themselves to `M-right'.
  17286. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17287. (defvar org-metaup-hook nil
  17288. "Hook for functions attaching themselves to `M-up'.
  17289. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17290. (defvar org-metadown-hook nil
  17291. "Hook for functions attaching themselves to `M-down'.
  17292. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17293. (defvar org-shiftmetaleft-hook nil
  17294. "Hook for functions attaching themselves to `M-S-left'.
  17295. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17296. (defvar org-shiftmetaright-hook nil
  17297. "Hook for functions attaching themselves to `M-S-right'.
  17298. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17299. (defvar org-shiftmetaup-hook nil
  17300. "Hook for functions attaching themselves to `M-S-up'.
  17301. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17302. (defvar org-shiftmetadown-hook nil
  17303. "Hook for functions attaching themselves to `M-S-down'.
  17304. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17305. (defvar org-metareturn-hook nil
  17306. "Hook for functions attaching themselves to `M-RET'.
  17307. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17308. (defvar org-shiftup-hook nil
  17309. "Hook for functions attaching themselves to `S-up'.
  17310. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17311. (defvar org-shiftup-final-hook nil
  17312. "Hook for functions attaching themselves to `S-up'.
  17313. This one runs after all other options except shift-select have been excluded.
  17314. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17315. (defvar org-shiftdown-hook nil
  17316. "Hook for functions attaching themselves to `S-down'.
  17317. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17318. (defvar org-shiftdown-final-hook nil
  17319. "Hook for functions attaching themselves to `S-down'.
  17320. This one runs after all other options except shift-select have been excluded.
  17321. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17322. (defvar org-shiftleft-hook nil
  17323. "Hook for functions attaching themselves to `S-left'.
  17324. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17325. (defvar org-shiftleft-final-hook nil
  17326. "Hook for functions attaching themselves to `S-left'.
  17327. This one runs after all other options except shift-select have been excluded.
  17328. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17329. (defvar org-shiftright-hook nil
  17330. "Hook for functions attaching themselves to `S-right'.
  17331. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17332. (defvar org-shiftright-final-hook nil
  17333. "Hook for functions attaching themselves to `S-right'.
  17334. This one runs after all other options except shift-select have been excluded.
  17335. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17336. (defun org-modifier-cursor-error ()
  17337. "Throw an error, a modified cursor command was applied in wrong context."
  17338. (error "This command is active in special context like tables, headlines or items"))
  17339. (defun org-shiftselect-error ()
  17340. "Throw an error because Shift-Cursor command was applied in wrong context."
  17341. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17342. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17343. (error "This command works only in special context like headlines or timestamps")))
  17344. (defun org-call-for-shift-select (cmd)
  17345. (let ((this-command-keys-shift-translated t))
  17346. (call-interactively cmd)))
  17347. (defun org-shifttab (&optional arg)
  17348. "Global visibility cycling or move to previous table field.
  17349. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17350. on context.
  17351. See the individual commands for more information."
  17352. (interactive "P")
  17353. (cond
  17354. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17355. ((integerp arg)
  17356. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17357. (message "Content view to level: %d" arg)
  17358. (org-content (prefix-numeric-value arg2))
  17359. (setq org-cycle-global-status 'overview)))
  17360. (t (call-interactively 'org-global-cycle))))
  17361. (defun org-shiftmetaleft ()
  17362. "Promote subtree or delete table column.
  17363. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17364. `org-table-delete-column', depending on context. See the
  17365. individual commands for more information."
  17366. (interactive)
  17367. (cond
  17368. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17369. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17370. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17371. ((if (not (org-region-active-p)) (org-at-item-p)
  17372. (save-excursion (goto-char (region-beginning))
  17373. (org-at-item-p)))
  17374. (call-interactively 'org-outdent-item-tree))
  17375. (t (org-modifier-cursor-error))))
  17376. (defun org-shiftmetaright ()
  17377. "Demote subtree or insert table column.
  17378. Calls `org-demote-subtree', `org-indent-item-tree', or
  17379. `org-table-insert-column', depending on context. See the
  17380. individual commands for more information."
  17381. (interactive)
  17382. (cond
  17383. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17384. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17385. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17386. ((if (not (org-region-active-p)) (org-at-item-p)
  17387. (save-excursion (goto-char (region-beginning))
  17388. (org-at-item-p)))
  17389. (call-interactively 'org-indent-item-tree))
  17390. (t (org-modifier-cursor-error))))
  17391. (defun org-shiftmetaup (&optional arg)
  17392. "Move subtree up or kill table row.
  17393. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17394. `org-move-item-up' or `org-timestamp-up', depending on context.
  17395. See the individual commands for more information."
  17396. (interactive "P")
  17397. (cond
  17398. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17399. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17400. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17401. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17402. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17403. (call-interactively 'org-timestamp-up)))
  17404. (t (org-modifier-cursor-error))))
  17405. (defun org-shiftmetadown (&optional arg)
  17406. "Move subtree down or insert table row.
  17407. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17408. `org-move-item-down' or `org-timestamp-up', depending on context.
  17409. See the individual commands for more information."
  17410. (interactive "P")
  17411. (cond
  17412. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17413. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17414. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17415. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17416. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17417. (call-interactively 'org-timestamp-down)))
  17418. (t (org-modifier-cursor-error))))
  17419. (defsubst org-hidden-tree-error ()
  17420. (error
  17421. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17422. (defun org-metaleft (&optional arg)
  17423. "Promote heading or move table column to left.
  17424. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17425. With no specific context, calls the Emacs default `backward-word'.
  17426. See the individual commands for more information."
  17427. (interactive "P")
  17428. (cond
  17429. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17430. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17431. ((org-with-limited-levels
  17432. (or (org-at-heading-p)
  17433. (and (org-region-active-p)
  17434. (save-excursion
  17435. (goto-char (region-beginning))
  17436. (org-at-heading-p)))))
  17437. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17438. (call-interactively 'org-do-promote))
  17439. ;; At an inline task.
  17440. ((org-at-heading-p)
  17441. (call-interactively 'org-inlinetask-promote))
  17442. ((or (org-at-item-p)
  17443. (and (org-region-active-p)
  17444. (save-excursion
  17445. (goto-char (region-beginning))
  17446. (org-at-item-p))))
  17447. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17448. (call-interactively 'org-outdent-item))
  17449. (t (call-interactively 'backward-word))))
  17450. (defun org-metaright (&optional arg)
  17451. "Demote a subtree, a list item or move table column to right.
  17452. In front of a drawer or a block keyword, indent it correctly.
  17453. With no specific context, calls the Emacs default `forward-word'.
  17454. See the individual commands for more information."
  17455. (interactive "P")
  17456. (cond
  17457. ((run-hook-with-args-until-success 'org-metaright-hook))
  17458. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17459. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17460. ((org-at-block-p) (call-interactively 'org-indent-block))
  17461. ((org-with-limited-levels
  17462. (or (org-at-heading-p)
  17463. (and (org-region-active-p)
  17464. (save-excursion
  17465. (goto-char (region-beginning))
  17466. (org-at-heading-p)))))
  17467. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17468. (call-interactively 'org-do-demote))
  17469. ;; At an inline task.
  17470. ((org-at-heading-p)
  17471. (call-interactively 'org-inlinetask-demote))
  17472. ((or (org-at-item-p)
  17473. (and (org-region-active-p)
  17474. (save-excursion
  17475. (goto-char (region-beginning))
  17476. (org-at-item-p))))
  17477. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17478. (call-interactively 'org-indent-item))
  17479. (t (call-interactively 'forward-word))))
  17480. (defun org-check-for-hidden (what)
  17481. "Check if there are hidden headlines/items in the current visual line.
  17482. WHAT can be either `headlines' or `items'. If the current line is
  17483. an outline or item heading and it has a folded subtree below it,
  17484. this function returns t, nil otherwise."
  17485. (let ((re (cond
  17486. ((eq what 'headlines) org-outline-regexp-bol)
  17487. ((eq what 'items) (org-item-beginning-re))
  17488. (t (error "This should not happen"))))
  17489. beg end)
  17490. (save-excursion
  17491. (catch 'exit
  17492. (unless (org-region-active-p)
  17493. (setq beg (point-at-bol))
  17494. (beginning-of-line 2)
  17495. (while (and (not (eobp)) ;; this is like `next-line'
  17496. (get-char-property (1- (point)) 'invisible))
  17497. (beginning-of-line 2))
  17498. (setq end (point))
  17499. (goto-char beg)
  17500. (goto-char (point-at-eol))
  17501. (setq end (max end (point)))
  17502. (while (re-search-forward re end t)
  17503. (if (get-char-property (match-beginning 0) 'invisible)
  17504. (throw 'exit t))))
  17505. nil))))
  17506. (defun org-metaup (&optional arg)
  17507. "Move subtree up or move table row up.
  17508. Calls `org-move-subtree-up' or `org-table-move-row' or
  17509. `org-move-item-up', depending on context. See the individual commands
  17510. for more information."
  17511. (interactive "P")
  17512. (cond
  17513. ((run-hook-with-args-until-success 'org-metaup-hook))
  17514. ((org-region-active-p)
  17515. (let* ((a (min (region-beginning) (region-end)))
  17516. (b (1- (max (region-beginning) (region-end))))
  17517. (c (save-excursion (goto-char a)
  17518. (move-beginning-of-line 0)))
  17519. (d (save-excursion (goto-char a)
  17520. (move-end-of-line 0) (point))))
  17521. (transpose-regions a b c d)
  17522. (goto-char c)))
  17523. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17524. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17525. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17526. (t (org-drag-element-backward))))
  17527. (defun org-metadown (&optional arg)
  17528. "Move subtree down or move table row down.
  17529. Calls `org-move-subtree-down' or `org-table-move-row' or
  17530. `org-move-item-down', depending on context. See the individual
  17531. commands for more information."
  17532. (interactive "P")
  17533. (cond
  17534. ((run-hook-with-args-until-success 'org-metadown-hook))
  17535. ((org-region-active-p)
  17536. (let* ((a (min (region-beginning) (region-end)))
  17537. (b (max (region-beginning) (region-end)))
  17538. (c (save-excursion (goto-char b)
  17539. (move-beginning-of-line 1)))
  17540. (d (save-excursion (goto-char b)
  17541. (move-end-of-line 1) (1+ (point)))))
  17542. (transpose-regions a b c d)
  17543. (goto-char d)))
  17544. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17545. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17546. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17547. (t (org-drag-element-forward))))
  17548. (defun org-shiftup (&optional arg)
  17549. "Increase item in timestamp or increase priority of current headline.
  17550. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17551. depending on context. See the individual commands for more information."
  17552. (interactive "P")
  17553. (cond
  17554. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17555. ((and org-support-shift-select (org-region-active-p))
  17556. (org-call-for-shift-select 'previous-line))
  17557. ((org-at-timestamp-p t)
  17558. (call-interactively (if org-edit-timestamp-down-means-later
  17559. 'org-timestamp-down 'org-timestamp-up)))
  17560. ((and (not (eq org-support-shift-select 'always))
  17561. org-enable-priority-commands
  17562. (org-at-heading-p))
  17563. (call-interactively 'org-priority-up))
  17564. ((and (not org-support-shift-select) (org-at-item-p))
  17565. (call-interactively 'org-previous-item))
  17566. ((org-clocktable-try-shift 'up arg))
  17567. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17568. (org-support-shift-select
  17569. (org-call-for-shift-select 'previous-line))
  17570. (t (org-shiftselect-error))))
  17571. (defun org-shiftdown (&optional arg)
  17572. "Decrease item in timestamp or decrease priority of current headline.
  17573. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17574. depending on context. See the individual commands for more information."
  17575. (interactive "P")
  17576. (cond
  17577. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17578. ((and org-support-shift-select (org-region-active-p))
  17579. (org-call-for-shift-select 'next-line))
  17580. ((org-at-timestamp-p t)
  17581. (call-interactively (if org-edit-timestamp-down-means-later
  17582. 'org-timestamp-up 'org-timestamp-down)))
  17583. ((and (not (eq org-support-shift-select 'always))
  17584. org-enable-priority-commands
  17585. (org-at-heading-p))
  17586. (call-interactively 'org-priority-down))
  17587. ((and (not org-support-shift-select) (org-at-item-p))
  17588. (call-interactively 'org-next-item))
  17589. ((org-clocktable-try-shift 'down arg))
  17590. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17591. (org-support-shift-select
  17592. (org-call-for-shift-select 'next-line))
  17593. (t (org-shiftselect-error))))
  17594. (defun org-shiftright (&optional arg)
  17595. "Cycle the thing at point or in the current line, depending on context.
  17596. Depending on context, this does one of the following:
  17597. - switch a timestamp at point one day into the future
  17598. - on a headline, switch to the next TODO keyword.
  17599. - on an item, switch entire list to the next bullet type
  17600. - on a property line, switch to the next allowed value
  17601. - on a clocktable definition line, move time block into the future"
  17602. (interactive "P")
  17603. (cond
  17604. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17605. ((and org-support-shift-select (org-region-active-p))
  17606. (org-call-for-shift-select 'forward-char))
  17607. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17608. ((and (not (eq org-support-shift-select 'always))
  17609. (org-at-heading-p))
  17610. (let ((org-inhibit-logging
  17611. (not org-treat-S-cursor-todo-selection-as-state-change))
  17612. (org-inhibit-blocking
  17613. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17614. (org-call-with-arg 'org-todo 'right)))
  17615. ((or (and org-support-shift-select
  17616. (not (eq org-support-shift-select 'always))
  17617. (org-at-item-bullet-p))
  17618. (and (not org-support-shift-select) (org-at-item-p)))
  17619. (org-call-with-arg 'org-cycle-list-bullet nil))
  17620. ((and (not (eq org-support-shift-select 'always))
  17621. (org-at-property-p))
  17622. (call-interactively 'org-property-next-allowed-value))
  17623. ((org-clocktable-try-shift 'right arg))
  17624. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17625. (org-support-shift-select
  17626. (org-call-for-shift-select 'forward-char))
  17627. (t (org-shiftselect-error))))
  17628. (defun org-shiftleft (&optional arg)
  17629. "Cycle the thing at point or in the current line, depending on context.
  17630. Depending on context, this does one of the following:
  17631. - switch a timestamp at point one day into the past
  17632. - on a headline, switch to the previous TODO keyword.
  17633. - on an item, switch entire list to the previous bullet type
  17634. - on a property line, switch to the previous allowed value
  17635. - on a clocktable definition line, move time block into the past"
  17636. (interactive "P")
  17637. (cond
  17638. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17639. ((and org-support-shift-select (org-region-active-p))
  17640. (org-call-for-shift-select 'backward-char))
  17641. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17642. ((and (not (eq org-support-shift-select 'always))
  17643. (org-at-heading-p))
  17644. (let ((org-inhibit-logging
  17645. (not org-treat-S-cursor-todo-selection-as-state-change))
  17646. (org-inhibit-blocking
  17647. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17648. (org-call-with-arg 'org-todo 'left)))
  17649. ((or (and org-support-shift-select
  17650. (not (eq org-support-shift-select 'always))
  17651. (org-at-item-bullet-p))
  17652. (and (not org-support-shift-select) (org-at-item-p)))
  17653. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17654. ((and (not (eq org-support-shift-select 'always))
  17655. (org-at-property-p))
  17656. (call-interactively 'org-property-previous-allowed-value))
  17657. ((org-clocktable-try-shift 'left arg))
  17658. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17659. (org-support-shift-select
  17660. (org-call-for-shift-select 'backward-char))
  17661. (t (org-shiftselect-error))))
  17662. (defun org-shiftcontrolright ()
  17663. "Switch to next TODO set."
  17664. (interactive)
  17665. (cond
  17666. ((and org-support-shift-select (org-region-active-p))
  17667. (org-call-for-shift-select 'forward-word))
  17668. ((and (not (eq org-support-shift-select 'always))
  17669. (org-at-heading-p))
  17670. (org-call-with-arg 'org-todo 'nextset))
  17671. (org-support-shift-select
  17672. (org-call-for-shift-select 'forward-word))
  17673. (t (org-shiftselect-error))))
  17674. (defun org-shiftcontrolleft ()
  17675. "Switch to previous TODO set."
  17676. (interactive)
  17677. (cond
  17678. ((and org-support-shift-select (org-region-active-p))
  17679. (org-call-for-shift-select 'backward-word))
  17680. ((and (not (eq org-support-shift-select 'always))
  17681. (org-at-heading-p))
  17682. (org-call-with-arg 'org-todo 'previousset))
  17683. (org-support-shift-select
  17684. (org-call-for-shift-select 'backward-word))
  17685. (t (org-shiftselect-error))))
  17686. (defun org-shiftcontrolup (&optional n)
  17687. "Change timestamps synchronously up in CLOCK log lines.
  17688. Optional argument N tells to change by that many units."
  17689. (interactive "P")
  17690. (cond ((and (not org-support-shift-select)
  17691. (org-at-clock-log-p)
  17692. (org-at-timestamp-p t))
  17693. (org-clock-timestamps-up n))
  17694. (t (org-shiftselect-error))))
  17695. (defun org-shiftcontroldown (&optional n)
  17696. "Change timestamps synchronously down in CLOCK log lines.
  17697. Optional argument N tells to change by that many units."
  17698. (interactive "P")
  17699. (cond ((and (not org-support-shift-select)
  17700. (org-at-clock-log-p)
  17701. (org-at-timestamp-p t))
  17702. (org-clock-timestamps-down n))
  17703. (t (org-shiftselect-error))))
  17704. (defun org-ctrl-c-ret ()
  17705. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17706. (interactive)
  17707. (cond
  17708. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17709. (t (call-interactively 'org-insert-heading))))
  17710. (defun org-find-visible ()
  17711. (let ((s (point)))
  17712. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17713. (get-char-property s 'invisible)))
  17714. s))
  17715. (defun org-find-invisible ()
  17716. (let ((s (point)))
  17717. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17718. (not (get-char-property s 'invisible))))
  17719. s))
  17720. (defun org-copy-visible (beg end)
  17721. "Copy the visible parts of the region."
  17722. (interactive "r")
  17723. (let (snippets s)
  17724. (save-excursion
  17725. (save-restriction
  17726. (narrow-to-region beg end)
  17727. (setq s (goto-char (point-min)))
  17728. (while (not (= (point) (point-max)))
  17729. (goto-char (org-find-invisible))
  17730. (push (buffer-substring s (point)) snippets)
  17731. (setq s (goto-char (org-find-visible))))))
  17732. (kill-new (apply 'concat (nreverse snippets)))))
  17733. (defun org-copy-special ()
  17734. "Copy region in table or copy current subtree.
  17735. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17736. See the individual commands for more information."
  17737. (interactive)
  17738. (call-interactively
  17739. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17740. (defun org-cut-special ()
  17741. "Cut region in table or cut current subtree.
  17742. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17743. See the individual commands for more information."
  17744. (interactive)
  17745. (call-interactively
  17746. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17747. (defun org-paste-special (arg)
  17748. "Paste rectangular region into table, or past subtree relative to level.
  17749. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17750. See the individual commands for more information."
  17751. (interactive "P")
  17752. (if (org-at-table-p)
  17753. (org-table-paste-rectangle)
  17754. (org-paste-subtree arg)))
  17755. (defsubst org-in-fixed-width-region-p ()
  17756. "Is point in a fixed-width region?"
  17757. (save-match-data
  17758. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17759. (defun org-edit-special (&optional arg)
  17760. "Call a special editor for the stuff at point.
  17761. When at a table, call the formula editor with `org-table-edit-formulas'.
  17762. When in a source code block, call `org-edit-src-code'.
  17763. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17764. When in an #+include line, visit the included file.
  17765. On a link, call `ffap' to visit the link at point.
  17766. Otherwise, return a user error."
  17767. (interactive)
  17768. ;; possibly prep session before editing source
  17769. (when (and (org-in-src-block-p) arg)
  17770. (let* ((info (org-babel-get-src-block-info))
  17771. (lang (nth 0 info))
  17772. (params (nth 2 info))
  17773. (session (cdr (assoc :session params))))
  17774. (when (and info session) ;; we are in a source-code block with a session
  17775. (funcall
  17776. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17777. (cond ;; proceed with `org-edit-special'
  17778. ((save-excursion
  17779. (beginning-of-line 1)
  17780. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17781. (find-file (org-trim (match-string 1))))
  17782. ((or (org-at-table-p)
  17783. (save-excursion
  17784. (beginning-of-line 1)
  17785. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17786. (call-interactively 'org-table-edit-formulas))
  17787. ((or (org-in-block-p '("src" "example" "latex" "html"))
  17788. (org-at-table.el-p))
  17789. (org-edit-src-code))
  17790. ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
  17791. ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
  17792. (t (user-error "No special environment to edit here"))))
  17793. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17794. (defun org-ctrl-c-ctrl-c (&optional arg)
  17795. "Set tags in headline, or update according to changed information at point.
  17796. This command does many different things, depending on context:
  17797. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17798. this is what we do.
  17799. - If the cursor is on a statistics cookie, update it.
  17800. - If the cursor is in a headline, prompt for tags and insert them
  17801. into the current line, aligned to `org-tags-column'. When called
  17802. with prefix arg, realign all tags in the current buffer.
  17803. - If the cursor is in one of the special #+KEYWORD lines, this
  17804. triggers scanning the buffer for these lines and updating the
  17805. information.
  17806. - If the cursor is inside a table, realign the table. This command
  17807. works even if the automatic table editor has been turned off.
  17808. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17809. the entire table.
  17810. - If the cursor is at a footnote reference or definition, jump to
  17811. the corresponding definition or references, respectively.
  17812. - If the cursor is a the beginning of a dynamic block, update it.
  17813. - If the current buffer is a capture buffer, close note and file it.
  17814. - If the cursor is on a <<<target>>>, update radio targets and
  17815. corresponding links in this buffer.
  17816. - If the cursor is on a numbered item in a plain list, renumber the
  17817. ordered list.
  17818. - If the cursor is on a checkbox, toggle it.
  17819. - If the cursor is on a code block, evaluate it. The variable
  17820. `org-confirm-babel-evaluate' can be used to control prompting
  17821. before code block evaluation, by default every code block
  17822. evaluation requires confirmation. Code block evaluation can be
  17823. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17824. (interactive "P")
  17825. (let ((org-enable-table-editor t))
  17826. (cond
  17827. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17828. org-occur-highlights
  17829. org-latex-fragment-image-overlays)
  17830. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17831. (org-remove-occur-highlights)
  17832. (org-remove-latex-fragment-image-overlays)
  17833. (message "Temporary highlights/overlays removed from current buffer"))
  17834. ((and (local-variable-p 'org-finish-function (current-buffer))
  17835. (fboundp org-finish-function))
  17836. (funcall org-finish-function))
  17837. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17838. ((org-in-regexp org-ts-regexp-both)
  17839. (org-timestamp-change 0 'day))
  17840. ((or (looking-at org-property-start-re)
  17841. (org-at-property-p))
  17842. (call-interactively 'org-property-action))
  17843. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17844. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17845. (or (org-at-heading-p) (org-at-item-p)))
  17846. (call-interactively 'org-update-statistics-cookies))
  17847. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17848. ((org-at-table.el-p)
  17849. (message "Use C-c ' to edit table.el tables"))
  17850. ((org-at-table-p)
  17851. (org-table-maybe-eval-formula)
  17852. (if arg
  17853. (call-interactively 'org-table-recalculate)
  17854. (org-table-maybe-recalculate-line))
  17855. (call-interactively 'org-table-align)
  17856. (orgtbl-send-table 'maybe))
  17857. ((or (org-footnote-at-reference-p)
  17858. (org-footnote-at-definition-p))
  17859. (call-interactively 'org-footnote-action))
  17860. ((org-at-item-checkbox-p)
  17861. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17862. ;; list only if at top item.
  17863. (let* ((cbox (match-string 1))
  17864. (struct (org-list-struct))
  17865. (old-struct (copy-tree struct))
  17866. (parents (org-list-parents-alist struct))
  17867. (orderedp (org-entry-get nil "ORDERED"))
  17868. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17869. block-item)
  17870. ;; Use a light version of `org-toggle-checkbox' to avoid
  17871. ;; computing list structure twice.
  17872. (let ((new-box (cond
  17873. ((equal arg '(16)) "[-]")
  17874. ((equal arg '(4)) nil)
  17875. ((equal "[X]" cbox) "[ ]")
  17876. (t "[X]"))))
  17877. (if (and firstp arg)
  17878. ;; If at first item of sub-list, remove check-box from
  17879. ;; every item at the same level.
  17880. (mapc
  17881. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17882. (org-list-get-all-items
  17883. (point-at-bol) struct (org-list-prevs-alist struct)))
  17884. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17885. ;; Replicate `org-list-write-struct', while grabbing a return
  17886. ;; value from `org-list-struct-fix-box'.
  17887. (org-list-struct-fix-ind struct parents 2)
  17888. (org-list-struct-fix-item-end struct)
  17889. (let ((prevs (org-list-prevs-alist struct)))
  17890. (org-list-struct-fix-bul struct prevs)
  17891. (org-list-struct-fix-ind struct parents)
  17892. (setq block-item
  17893. (org-list-struct-fix-box struct parents prevs orderedp)))
  17894. (org-list-struct-apply-struct struct old-struct)
  17895. (org-update-checkbox-count-maybe)
  17896. (when block-item
  17897. (message
  17898. "Checkboxes were removed due to unchecked box at line %d"
  17899. (org-current-line block-item)))
  17900. (when firstp (org-list-send-list 'maybe))))
  17901. ((org-at-item-p)
  17902. ;; Cursor at an item: repair list. Do checkbox related actions
  17903. ;; only if function was called with an argument. Send list only
  17904. ;; if at top item.
  17905. (let* ((struct (org-list-struct))
  17906. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17907. old-struct)
  17908. (when arg
  17909. (setq old-struct (copy-tree struct))
  17910. (if firstp
  17911. ;; If at first item of sub-list, add check-box to every
  17912. ;; item at the same level.
  17913. (mapc
  17914. (lambda (pos)
  17915. (unless (org-list-get-checkbox pos struct)
  17916. (org-list-set-checkbox pos struct "[ ]")))
  17917. (org-list-get-all-items
  17918. (point-at-bol) struct (org-list-prevs-alist struct)))
  17919. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17920. (org-list-write-struct
  17921. struct (org-list-parents-alist struct) old-struct)
  17922. (when arg (org-update-checkbox-count-maybe))
  17923. (when firstp (org-list-send-list 'maybe))))
  17924. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17925. ;; Dynamic block
  17926. (beginning-of-line 1)
  17927. (save-excursion (org-update-dblock)))
  17928. ((save-excursion
  17929. (let ((case-fold-search t))
  17930. (beginning-of-line 1)
  17931. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17932. (cond
  17933. ((or (equal (match-string 1) "TBLFM")
  17934. (equal (match-string 1) "tblfm"))
  17935. ;; Recalculate the table before this line
  17936. (save-excursion
  17937. (beginning-of-line 1)
  17938. (skip-chars-backward " \r\n\t")
  17939. (if (org-at-table-p)
  17940. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17941. (t
  17942. (let ((org-inhibit-startup-visibility-stuff t)
  17943. (org-startup-align-all-tables nil))
  17944. (when (boundp 'org-table-coordinate-overlays)
  17945. (mapc 'delete-overlay org-table-coordinate-overlays)
  17946. (setq org-table-coordinate-overlays nil))
  17947. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17948. (message "Local setup has been refreshed"))))
  17949. ((org-clock-update-time-maybe))
  17950. (t
  17951. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17952. (error "C-c C-c can do nothing useful at this location"))))))
  17953. (defun org-mode-restart ()
  17954. "Restart Org-mode, to scan again for special lines.
  17955. Also updates the keyword regular expressions."
  17956. (interactive)
  17957. (org-mode)
  17958. (message "Org-mode restarted"))
  17959. (defun org-kill-note-or-show-branches ()
  17960. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17961. (interactive)
  17962. (if (not org-finish-function)
  17963. (progn
  17964. (hide-subtree)
  17965. (call-interactively 'show-branches))
  17966. (let ((org-note-abort t))
  17967. (funcall org-finish-function))))
  17968. (defun org-return (&optional indent)
  17969. "Goto next table row or insert a newline.
  17970. Calls `org-table-next-row' or `newline', depending on context.
  17971. See the individual commands for more information."
  17972. (interactive)
  17973. (let (org-ts-what)
  17974. (cond
  17975. ((or (bobp) (org-in-src-block-p))
  17976. (if indent (newline-and-indent) (newline)))
  17977. ((org-at-table-p)
  17978. (org-table-justify-field-maybe)
  17979. (call-interactively 'org-table-next-row))
  17980. ;; when `newline-and-indent' is called within a list, make sure
  17981. ;; text moved stays inside the item.
  17982. ((and (org-in-item-p) indent)
  17983. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17984. (progn
  17985. (save-match-data (newline))
  17986. (org-indent-line-to (length (match-string 0))))
  17987. (let ((ind (org-get-indentation)))
  17988. (newline)
  17989. (if (org-looking-back org-list-end-re)
  17990. (org-indent-line)
  17991. (org-indent-line-to ind)))))
  17992. ((and org-return-follows-link
  17993. (org-at-timestamp-p t)
  17994. (not (eq org-ts-what 'after)))
  17995. (org-follow-timestamp-link))
  17996. ((and org-return-follows-link
  17997. (let ((tprop (get-text-property (point) 'face)))
  17998. (or (eq tprop 'org-link)
  17999. (and (listp tprop) (memq 'org-link tprop)))))
  18000. (call-interactively 'org-open-at-point))
  18001. ((and (org-at-heading-p)
  18002. (looking-at
  18003. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18004. (org-show-entry)
  18005. (end-of-line 1)
  18006. (newline))
  18007. (t (if indent (newline-and-indent) (newline))))))
  18008. (defun org-return-indent ()
  18009. "Goto next table row or insert a newline and indent.
  18010. Calls `org-table-next-row' or `newline-and-indent', depending on
  18011. context. See the individual commands for more information."
  18012. (interactive)
  18013. (org-return t))
  18014. (defun org-ctrl-c-star ()
  18015. "Compute table, or change heading status of lines.
  18016. Calls `org-table-recalculate' or `org-toggle-heading',
  18017. depending on context."
  18018. (interactive)
  18019. (cond
  18020. ((org-at-table-p)
  18021. (call-interactively 'org-table-recalculate))
  18022. (t
  18023. ;; Convert all lines in region to list items
  18024. (call-interactively 'org-toggle-heading))))
  18025. (defun org-ctrl-c-minus ()
  18026. "Insert separator line in table or modify bullet status of line.
  18027. Also turns a plain line or a region of lines into list items.
  18028. Calls `org-table-insert-hline', `org-toggle-item', or
  18029. `org-cycle-list-bullet', depending on context."
  18030. (interactive)
  18031. (cond
  18032. ((org-at-table-p)
  18033. (call-interactively 'org-table-insert-hline))
  18034. ((org-region-active-p)
  18035. (call-interactively 'org-toggle-item))
  18036. ((org-in-item-p)
  18037. (call-interactively 'org-cycle-list-bullet))
  18038. (t
  18039. (call-interactively 'org-toggle-item))))
  18040. (defun org-toggle-item (arg)
  18041. "Convert headings or normal lines to items, items to normal lines.
  18042. If there is no active region, only the current line is considered.
  18043. If the first non blank line in the region is an headline, convert
  18044. all headlines to items, shifting text accordingly.
  18045. If it is an item, convert all items to normal lines.
  18046. If it is normal text, change region into an item. With a prefix
  18047. argument ARG, change each line in region into an item."
  18048. (interactive "P")
  18049. (let ((shift-text
  18050. (function
  18051. ;; Shift text in current section to IND, from point to END.
  18052. ;; The function leaves point to END line.
  18053. (lambda (ind end)
  18054. (let ((min-i 1000) (end (copy-marker end)))
  18055. ;; First determine the minimum indentation (MIN-I) of
  18056. ;; the text.
  18057. (save-excursion
  18058. (catch 'exit
  18059. (while (< (point) end)
  18060. (let ((i (org-get-indentation)))
  18061. (cond
  18062. ;; Skip blank lines and inline tasks.
  18063. ((looking-at "^[ \t]*$"))
  18064. ((looking-at org-outline-regexp-bol))
  18065. ;; We can't find less than 0 indentation.
  18066. ((zerop i) (throw 'exit (setq min-i 0)))
  18067. ((< i min-i) (setq min-i i))))
  18068. (forward-line))))
  18069. ;; Then indent each line so that a line indented to
  18070. ;; MIN-I becomes indented to IND. Ignore blank lines
  18071. ;; and inline tasks in the process.
  18072. (let ((delta (- ind min-i)))
  18073. (while (< (point) end)
  18074. (unless (or (looking-at "^[ \t]*$")
  18075. (looking-at org-outline-regexp-bol))
  18076. (org-indent-line-to (+ (org-get-indentation) delta)))
  18077. (forward-line)))))))
  18078. (skip-blanks
  18079. (function
  18080. ;; Return beginning of first non-blank line, starting from
  18081. ;; line at POS.
  18082. (lambda (pos)
  18083. (save-excursion
  18084. (goto-char pos)
  18085. (skip-chars-forward " \r\t\n")
  18086. (point-at-bol)))))
  18087. beg end)
  18088. ;; Determine boundaries of changes.
  18089. (if (org-region-active-p)
  18090. (setq beg (funcall skip-blanks (region-beginning))
  18091. end (copy-marker (region-end)))
  18092. (setq beg (funcall skip-blanks (point-at-bol))
  18093. end (copy-marker (point-at-eol))))
  18094. ;; Depending on the starting line, choose an action on the text
  18095. ;; between BEG and END.
  18096. (org-with-limited-levels
  18097. (save-excursion
  18098. (goto-char beg)
  18099. (cond
  18100. ;; Case 1. Start at an item: de-itemize. Note that it only
  18101. ;; happens when a region is active: `org-ctrl-c-minus'
  18102. ;; would call `org-cycle-list-bullet' otherwise.
  18103. ((org-at-item-p)
  18104. (while (< (point) end)
  18105. (when (org-at-item-p)
  18106. (skip-chars-forward " \t")
  18107. (delete-region (point) (match-end 0)))
  18108. (forward-line)))
  18109. ;; Case 2. Start at an heading: convert to items.
  18110. ((org-at-heading-p)
  18111. (let* ((bul (org-list-bullet-string "-"))
  18112. (bul-len (length bul))
  18113. ;; Indentation of the first heading. It should be
  18114. ;; relative to the indentation of its parent, if any.
  18115. (start-ind (save-excursion
  18116. (cond
  18117. ((not org-adapt-indentation) 0)
  18118. ((not (outline-previous-heading)) 0)
  18119. (t (length (match-string 0))))))
  18120. ;; Level of first heading. Further headings will be
  18121. ;; compared to it to determine hierarchy in the list.
  18122. (ref-level (org-reduced-level (org-outline-level))))
  18123. (while (< (point) end)
  18124. (let* ((level (org-reduced-level (org-outline-level)))
  18125. (delta (max 0 (- level ref-level))))
  18126. ;; If current headline is less indented than the first
  18127. ;; one, set it as reference, in order to preserve
  18128. ;; subtrees.
  18129. (when (< level ref-level) (setq ref-level level))
  18130. (replace-match bul t t)
  18131. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18132. ;; Ensure all text down to END (or SECTION-END) belongs
  18133. ;; to the newly created item.
  18134. (let ((section-end (save-excursion
  18135. (or (outline-next-heading) (point)))))
  18136. (forward-line)
  18137. (funcall shift-text
  18138. (+ start-ind (* (1+ delta) bul-len))
  18139. (min end section-end)))))))
  18140. ;; Case 3. Normal line with ARG: turn each non-item line into
  18141. ;; an item.
  18142. (arg
  18143. (while (< (point) end)
  18144. (unless (or (org-at-heading-p) (org-at-item-p))
  18145. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18146. (replace-match
  18147. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18148. (forward-line)))
  18149. ;; Case 4. Normal line without ARG: make the first line of
  18150. ;; region an item, and shift indentation of others
  18151. ;; lines to set them as item's body.
  18152. (t (let* ((bul (org-list-bullet-string "-"))
  18153. (bul-len (length bul))
  18154. (ref-ind (org-get-indentation)))
  18155. (skip-chars-forward " \t")
  18156. (insert bul)
  18157. (forward-line)
  18158. (while (< (point) end)
  18159. ;; Ensure that lines less indented than first one
  18160. ;; still get included in item body.
  18161. (funcall shift-text
  18162. (+ ref-ind bul-len)
  18163. (min end (save-excursion (or (outline-next-heading)
  18164. (point)))))
  18165. (forward-line)))))))))
  18166. (defun org-toggle-heading (&optional nstars)
  18167. "Convert headings to normal text, or items or text to headings.
  18168. If there is no active region, only the current line is considered.
  18169. With a \\[universal-argument] prefix, convert the whole list at
  18170. point into heading.
  18171. In a region:
  18172. - If the first non blank line is an headline, remove the stars
  18173. from all headlines in the region.
  18174. - If it is a normal line turn each and every normal line (i.e. not an
  18175. heading or an item) in the region into a heading.
  18176. - If it is a plain list item, turn all plain list items into headings.
  18177. When converting a line into a heading, the number of stars is chosen
  18178. such that the lines become children of the current entry. However,
  18179. when a prefix argument is given, its value determines the number of
  18180. stars to add."
  18181. (interactive "P")
  18182. (let ((skip-blanks
  18183. (function
  18184. ;; Return beginning of first non-blank line, starting from
  18185. ;; line at POS.
  18186. (lambda (pos)
  18187. (save-excursion
  18188. (goto-char pos)
  18189. (while (org-at-comment-p) (forward-line))
  18190. (skip-chars-forward " \r\t\n")
  18191. (point-at-bol)))))
  18192. beg end toggled)
  18193. ;; Determine boundaries of changes. If a universal prefix has
  18194. ;; been given, put the list in a region. If region ends at a bol,
  18195. ;; do not consider the last line to be in the region.
  18196. (when (and current-prefix-arg (org-at-item-p))
  18197. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18198. (org-mark-element))
  18199. (if (org-region-active-p)
  18200. (setq beg (funcall skip-blanks (region-beginning))
  18201. end (copy-marker (save-excursion
  18202. (goto-char (region-end))
  18203. (if (bolp) (point) (point-at-eol)))))
  18204. (setq beg (funcall skip-blanks (point-at-bol))
  18205. end (copy-marker (point-at-eol))))
  18206. ;; Ensure inline tasks don't count as headings.
  18207. (org-with-limited-levels
  18208. (save-excursion
  18209. (goto-char beg)
  18210. (cond
  18211. ;; Case 1. Started at an heading: de-star headings.
  18212. ((org-at-heading-p)
  18213. (while (< (point) end)
  18214. (when (org-at-heading-p t)
  18215. (looking-at org-outline-regexp) (replace-match "")
  18216. (setq toggled t))
  18217. (forward-line)))
  18218. ;; Case 2. Started at an item: change items into headlines.
  18219. ;; One star will be added by `org-list-to-subtree'.
  18220. ((org-at-item-p)
  18221. (let* ((stars (make-string
  18222. (if nstars
  18223. ;; subtract the star that will be added again by
  18224. ;; `org-list-to-subtree'
  18225. (1- (prefix-numeric-value current-prefix-arg))
  18226. (or (org-current-level) 0))
  18227. ?*))
  18228. (add-stars
  18229. (cond (nstars "") ; stars from prefix only
  18230. ((equal stars "") "") ; before first heading
  18231. (org-odd-levels-only "*") ; inside heading, odd
  18232. (t "")))) ; inside heading, oddeven
  18233. (while (< (point) end)
  18234. (when (org-at-item-p)
  18235. ;; Pay attention to cases when region ends before list.
  18236. (let* ((struct (org-list-struct))
  18237. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18238. (save-restriction
  18239. (narrow-to-region (point) list-end)
  18240. (insert
  18241. (org-list-to-subtree
  18242. (org-list-parse-list t)
  18243. '(:istart (concat stars add-stars (funcall get-stars depth))
  18244. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18245. (setq toggled t))
  18246. (forward-line))))
  18247. ;; Case 3. Started at normal text: make every line an heading,
  18248. ;; skipping headlines and items.
  18249. (t (let* ((stars (make-string
  18250. (if nstars
  18251. (prefix-numeric-value current-prefix-arg)
  18252. (or (org-current-level) 0))
  18253. ?*))
  18254. (add-stars
  18255. (cond (nstars "") ; stars from prefix only
  18256. ((equal stars "") "*") ; before first heading
  18257. (org-odd-levels-only "**") ; inside heading, odd
  18258. (t "*"))) ; inside heading, oddeven
  18259. (rpl (concat stars add-stars " ")))
  18260. (while (< (point) end)
  18261. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18262. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18263. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18264. (forward-line)))))))
  18265. (unless toggled (message "Cannot toggle heading from here"))))
  18266. (defun org-meta-return (&optional arg)
  18267. "Insert a new heading or wrap a region in a table.
  18268. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18269. See the individual commands for more information."
  18270. (interactive "P")
  18271. (cond
  18272. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18273. ((or (org-at-drawer-p) (org-at-property-p))
  18274. (newline-and-indent))
  18275. ((org-at-table-p)
  18276. (call-interactively 'org-table-wrap-region))
  18277. (t (call-interactively 'org-insert-heading))))
  18278. ;;; Menu entries
  18279. (defsubst org-in-subtree-not-table-p ()
  18280. "Are we in a subtree and not in a table?"
  18281. (and (not (org-before-first-heading-p))
  18282. (not (org-at-table-p))))
  18283. ;; Define the Org-mode menus
  18284. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18285. '("Tbl"
  18286. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18287. ["Next Field" org-cycle (org-at-table-p)]
  18288. ["Previous Field" org-shifttab (org-at-table-p)]
  18289. ["Next Row" org-return (org-at-table-p)]
  18290. "--"
  18291. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18292. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18293. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18294. "--"
  18295. ("Column"
  18296. ["Move Column Left" org-metaleft (org-at-table-p)]
  18297. ["Move Column Right" org-metaright (org-at-table-p)]
  18298. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18299. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18300. ("Row"
  18301. ["Move Row Up" org-metaup (org-at-table-p)]
  18302. ["Move Row Down" org-metadown (org-at-table-p)]
  18303. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18304. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18305. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18306. "--"
  18307. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18308. ("Rectangle"
  18309. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18310. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18311. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18312. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18313. "--"
  18314. ("Calculate"
  18315. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18316. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18317. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18318. "--"
  18319. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18320. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18321. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18322. "--"
  18323. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18324. "--"
  18325. ["Sum Column/Rectangle" org-table-sum
  18326. (or (org-at-table-p) (org-region-active-p))]
  18327. ["Which Column?" org-table-current-column (org-at-table-p)])
  18328. ["Debug Formulas"
  18329. org-table-toggle-formula-debugger
  18330. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18331. ["Show Col/Row Numbers"
  18332. org-table-toggle-coordinate-overlays
  18333. :style toggle
  18334. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18335. "--"
  18336. ["Create" org-table-create (and (not (org-at-table-p))
  18337. org-enable-table-editor)]
  18338. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18339. ["Import from File" org-table-import (not (org-at-table-p))]
  18340. ["Export to File" org-table-export (org-at-table-p)]
  18341. "--"
  18342. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18343. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18344. '("Org"
  18345. ("Show/Hide"
  18346. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18347. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18348. ["Sparse Tree..." org-sparse-tree t]
  18349. ["Reveal Context" org-reveal t]
  18350. ["Show All" show-all t]
  18351. "--"
  18352. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18353. "--"
  18354. ["New Heading" org-insert-heading t]
  18355. ("Navigate Headings"
  18356. ["Up" outline-up-heading t]
  18357. ["Next" outline-next-visible-heading t]
  18358. ["Previous" outline-previous-visible-heading t]
  18359. ["Next Same Level" outline-forward-same-level t]
  18360. ["Previous Same Level" outline-backward-same-level t]
  18361. "--"
  18362. ["Jump" org-goto t])
  18363. ("Edit Structure"
  18364. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18365. "--"
  18366. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18367. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18368. "--"
  18369. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18370. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18371. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18372. "--"
  18373. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18374. "--"
  18375. ["Copy visible text" org-copy-visible t]
  18376. "--"
  18377. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18378. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18379. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18380. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18381. "--"
  18382. ["Sort Region/Children" org-sort t]
  18383. "--"
  18384. ["Convert to odd levels" org-convert-to-odd-levels t]
  18385. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18386. ("Editing"
  18387. ["Emphasis..." org-emphasize t]
  18388. ["Edit Source Example" org-edit-special t]
  18389. "--"
  18390. ["Footnote new/jump" org-footnote-action t]
  18391. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18392. ("Archive"
  18393. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18394. "--"
  18395. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18396. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18397. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18398. )
  18399. "--"
  18400. ("Hyperlinks"
  18401. ["Store Link (Global)" org-store-link t]
  18402. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18403. ["Insert Link" org-insert-link t]
  18404. ["Follow Link" org-open-at-point t]
  18405. "--"
  18406. ["Next link" org-next-link t]
  18407. ["Previous link" org-previous-link t]
  18408. "--"
  18409. ["Descriptive Links"
  18410. org-toggle-link-display
  18411. :style radio
  18412. :selected org-descriptive-links
  18413. ]
  18414. ["Literal Links"
  18415. org-toggle-link-display
  18416. :style radio
  18417. :selected (not org-descriptive-links)])
  18418. "--"
  18419. ("TODO Lists"
  18420. ["TODO/DONE/-" org-todo t]
  18421. ("Select keyword"
  18422. ["Next keyword" org-shiftright (org-at-heading-p)]
  18423. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18424. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18425. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18426. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18427. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18428. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18429. "--"
  18430. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18431. :selected org-enforce-todo-dependencies :style toggle :active t]
  18432. "Settings for tree at point"
  18433. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18434. :selected (org-entry-get nil "ORDERED")
  18435. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18436. ["Do Children parallel" org-toggle-ordered-property :style radio
  18437. :selected (not (org-entry-get nil "ORDERED"))
  18438. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18439. "--"
  18440. ["Set Priority" org-priority t]
  18441. ["Priority Up" org-shiftup t]
  18442. ["Priority Down" org-shiftdown t]
  18443. "--"
  18444. ["Get news from all feeds" org-feed-update-all t]
  18445. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18446. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18447. ("TAGS and Properties"
  18448. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18449. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18450. "--"
  18451. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18452. ["Column view of properties" org-columns t]
  18453. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18454. ("Dates and Scheduling"
  18455. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18456. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18457. ("Change Date"
  18458. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18459. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18460. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18461. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18462. ["Compute Time Range" org-evaluate-time-range t]
  18463. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18464. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18465. "--"
  18466. ["Custom time format" org-toggle-time-stamp-overlays
  18467. :style radio :selected org-display-custom-times]
  18468. "--"
  18469. ["Goto Calendar" org-goto-calendar t]
  18470. ["Date from Calendar" org-date-from-calendar t]
  18471. "--"
  18472. ["Start/Restart Timer" org-timer-start t]
  18473. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18474. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18475. ["Insert Timer String" org-timer t]
  18476. ["Insert Timer Item" org-timer-item t])
  18477. ("Logging work"
  18478. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18479. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18480. ["Clock out" org-clock-out t]
  18481. ["Clock cancel" org-clock-cancel t]
  18482. "--"
  18483. ["Mark as default task" org-clock-mark-default-task t]
  18484. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18485. ["Goto running clock" org-clock-goto t]
  18486. "--"
  18487. ["Display times" org-clock-display t]
  18488. ["Create clock table" org-clock-report t]
  18489. "--"
  18490. ["Record DONE time"
  18491. (progn (setq org-log-done (not org-log-done))
  18492. (message "Switching to %s will %s record a timestamp"
  18493. (car org-done-keywords)
  18494. (if org-log-done "automatically" "not")))
  18495. :style toggle :selected org-log-done])
  18496. "--"
  18497. ["Agenda Command..." org-agenda t]
  18498. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18499. ("File List for Agenda")
  18500. ("Special views current file"
  18501. ["TODO Tree" org-show-todo-tree t]
  18502. ["Check Deadlines" org-check-deadlines t]
  18503. ["Timeline" org-timeline t]
  18504. ["Tags/Property tree" org-match-sparse-tree t])
  18505. "--"
  18506. ["Export/Publish..." org-export t]
  18507. ("LaTeX"
  18508. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18509. :selected org-cdlatex-mode]
  18510. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18511. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18512. ["Modify math symbol" org-cdlatex-math-modify
  18513. (org-inside-LaTeX-fragment-p)]
  18514. ["Insert citation" org-reftex-citation t]
  18515. "--"
  18516. ["Template for BEAMER" (progn (require 'org-beamer)
  18517. (org-insert-beamer-options-template)) t])
  18518. "--"
  18519. ("MobileOrg"
  18520. ["Push Files and Views" org-mobile-push t]
  18521. ["Get Captured and Flagged" org-mobile-pull t]
  18522. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18523. "--"
  18524. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18525. "--"
  18526. ("Documentation"
  18527. ["Show Version" org-version t]
  18528. ["Info Documentation" org-info t])
  18529. ("Customize"
  18530. ["Browse Org Group" org-customize t]
  18531. "--"
  18532. ["Expand This Menu" org-create-customize-menu
  18533. (fboundp 'customize-menu-create)])
  18534. ["Send bug report" org-submit-bug-report t]
  18535. "--"
  18536. ("Refresh/Reload"
  18537. ["Refresh setup current buffer" org-mode-restart t]
  18538. ["Reload Org (after update)" org-reload t]
  18539. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18540. ))
  18541. (defun org-info (&optional node)
  18542. "Read documentation for Org-mode in the info system.
  18543. With optional NODE, go directly to that node."
  18544. (interactive)
  18545. (info (format "(org)%s" (or node ""))))
  18546. ;;;###autoload
  18547. (defun org-submit-bug-report ()
  18548. "Submit a bug report on Org-mode via mail.
  18549. Don't hesitate to report any problems or inaccurate documentation.
  18550. If you don't have setup sending mail from (X)Emacs, please copy the
  18551. output buffer into your mail program, as it gives us important
  18552. information about your Org-mode version and configuration."
  18553. (interactive)
  18554. (require 'reporter)
  18555. (org-load-modules-maybe)
  18556. (org-require-autoloaded-modules)
  18557. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18558. (reporter-submit-bug-report
  18559. "emacs-orgmode@gnu.org"
  18560. (org-version nil 'full)
  18561. (let (list)
  18562. (save-window-excursion
  18563. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18564. (delete-other-windows)
  18565. (erase-buffer)
  18566. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18567. We would like to add your full Org-mode and Outline configuration to the
  18568. bug report. This greatly simplifies the work of the maintainer and
  18569. other experts on the mailing list.
  18570. HOWEVER, some variables you have customized may contain private
  18571. information. The names of customers, colleagues, or friends, might
  18572. appear in the form of file names, tags, todo states, or search strings.
  18573. If you answer yes to the prompt, you might want to check and remove
  18574. such private information before sending the email.")
  18575. (add-text-properties (point-min) (point-max) '(face org-warning))
  18576. (when (yes-or-no-p "Include your Org-mode configuration ")
  18577. (mapatoms
  18578. (lambda (v)
  18579. (and (boundp v)
  18580. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18581. (or (and (symbol-value v)
  18582. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18583. (and
  18584. (get v 'custom-type) (get v 'standard-value)
  18585. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18586. (push v list)))))
  18587. (kill-buffer (get-buffer "*Warn about privacy*"))
  18588. list))
  18589. nil nil
  18590. "Remember to cover the basics, that is, what you expected to happen and
  18591. what in fact did happen. You don't know how to make a good report? See
  18592. http://orgmode.org/manual/Feedback.html#Feedback
  18593. Your bug report will be posted to the Org-mode mailing list.
  18594. ------------------------------------------------------------------------")
  18595. (save-excursion
  18596. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18597. (replace-match "\\1Bug: \\3 [\\2]")))))
  18598. (defun org-install-agenda-files-menu ()
  18599. (let ((bl (buffer-list)))
  18600. (save-excursion
  18601. (while bl
  18602. (set-buffer (pop bl))
  18603. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18604. (when (derived-mode-p 'org-mode)
  18605. (easy-menu-change
  18606. '("Org") "File List for Agenda"
  18607. (append
  18608. (list
  18609. ["Edit File List" (org-edit-agenda-file-list) t]
  18610. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18611. ["Remove Current File from List" org-remove-file t]
  18612. ["Cycle through agenda files" org-cycle-agenda-files t]
  18613. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18614. "--")
  18615. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18616. ;;;; Documentation
  18617. (defun org-require-autoloaded-modules ()
  18618. (interactive)
  18619. (mapc 'require
  18620. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18621. org-docbook org-exp org-html org-icalendar
  18622. org-id org-latex
  18623. org-publish org-remember org-table
  18624. org-timer org-xoxo)))
  18625. ;;;###autoload
  18626. (defun org-reload (&optional uncompiled)
  18627. "Reload all org lisp files.
  18628. With prefix arg UNCOMPILED, load the uncompiled versions."
  18629. (interactive "P")
  18630. (require 'loadhist)
  18631. (let* ((org-dir (org-find-library-dir "org"))
  18632. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18633. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18634. (remove-re (mapconcat 'identity
  18635. (mapcar (lambda (f) (concat "^" f "$"))
  18636. (list (if (featurep 'xemacs)
  18637. "org-colview"
  18638. "org-colview-xemacs")
  18639. "org" "org-loaddefs" "org-version"))
  18640. "\\|"))
  18641. (feats (delete-dups
  18642. (mapcar 'file-name-sans-extension
  18643. (mapcar 'file-name-nondirectory
  18644. (delq nil
  18645. (mapcar 'feature-file
  18646. features))))))
  18647. (lfeat (append
  18648. (sort
  18649. (setq feats
  18650. (delq nil (mapcar
  18651. (lambda (f)
  18652. (if (and (string-match feature-re f)
  18653. (not (string-match remove-re f)))
  18654. f nil))
  18655. feats)))
  18656. 'string-lessp)
  18657. (list "org-version" "org")))
  18658. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18659. load-uncore load-misses)
  18660. (setq load-misses
  18661. (delq 't
  18662. (mapcar (lambda (f)
  18663. (or (load (concat org-dir f) 'noerror nil nil 'mustsuffix)
  18664. (and (string= org-dir contrib-dir)
  18665. (load (concat contrib-dir f) 'noerror nil nil 'mustsuffix))
  18666. (and (load (concat (org-find-library-dir f) f) 'noerror nil nil 'mustsuffix)
  18667. (add-to-list 'load-uncore f 'append)
  18668. 't)
  18669. f))
  18670. lfeat)))
  18671. (if load-uncore
  18672. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18673. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18674. (if load-misses
  18675. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18676. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18677. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18678. ;;;###autoload
  18679. (defun org-customize ()
  18680. "Call the customize function with org as argument."
  18681. (interactive)
  18682. (org-load-modules-maybe)
  18683. (org-require-autoloaded-modules)
  18684. (customize-browse 'org))
  18685. (defun org-create-customize-menu ()
  18686. "Create a full customization menu for Org-mode, insert it into the menu."
  18687. (interactive)
  18688. (org-load-modules-maybe)
  18689. (org-require-autoloaded-modules)
  18690. (if (fboundp 'customize-menu-create)
  18691. (progn
  18692. (easy-menu-change
  18693. '("Org") "Customize"
  18694. `(["Browse Org group" org-customize t]
  18695. "--"
  18696. ,(customize-menu-create 'org)
  18697. ["Set" Custom-set t]
  18698. ["Save" Custom-save t]
  18699. ["Reset to Current" Custom-reset-current t]
  18700. ["Reset to Saved" Custom-reset-saved t]
  18701. ["Reset to Standard Settings" Custom-reset-standard t]))
  18702. (message "\"Org\"-menu now contains full customization menu"))
  18703. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18704. ;;;; Miscellaneous stuff
  18705. ;;; Generally useful functions
  18706. (defun org-get-at-bol (property)
  18707. "Get text property PROPERTY at beginning of line."
  18708. (get-text-property (point-at-bol) property))
  18709. (defun org-find-text-property-in-string (prop s)
  18710. "Return the first non-nil value of property PROP in string S."
  18711. (or (get-text-property 0 prop s)
  18712. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18713. prop s)))
  18714. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18715. "Display the given MESSAGE as a warning."
  18716. (if (fboundp 'display-warning)
  18717. (display-warning 'org message
  18718. (if (featurep 'xemacs) 'warning :warning))
  18719. (let ((buf (get-buffer-create "*Org warnings*")))
  18720. (with-current-buffer buf
  18721. (goto-char (point-max))
  18722. (insert "Warning (Org): " message)
  18723. (unless (bolp)
  18724. (newline)))
  18725. (display-buffer buf)
  18726. (sit-for 0))))
  18727. (defun org-eval (form)
  18728. "Eval FORM and return result."
  18729. (condition-case error
  18730. (eval form)
  18731. (error (format "%%![Error: %s]" error))))
  18732. (defun org-in-clocktable-p ()
  18733. "Check if the cursor is in a clocktable."
  18734. (let ((pos (point)) start)
  18735. (save-excursion
  18736. (end-of-line 1)
  18737. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18738. (setq start (match-beginning 0))
  18739. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18740. (>= (match-end 0) pos)
  18741. start))))
  18742. (defun org-in-commented-line ()
  18743. "Is point in a line starting with `#'?"
  18744. (equal (char-after (point-at-bol)) ?#))
  18745. (defun org-in-indented-comment-line ()
  18746. "Is point in a line starting with `#' after some white space?"
  18747. (save-excursion
  18748. (save-match-data
  18749. (goto-char (point-at-bol))
  18750. (looking-at "[ \t]*#"))))
  18751. (defun org-in-verbatim-emphasis ()
  18752. (save-match-data
  18753. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18754. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18755. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18756. (if (and marker (marker-buffer marker)
  18757. (buffer-live-p (marker-buffer marker)))
  18758. (progn
  18759. (org-pop-to-buffer-same-window (marker-buffer marker))
  18760. (if (or (> marker (point-max)) (< marker (point-min)))
  18761. (widen))
  18762. (goto-char marker)
  18763. (org-show-context 'org-goto))
  18764. (if bookmark
  18765. (bookmark-jump bookmark)
  18766. (error "Cannot find location"))))
  18767. (defun org-quote-csv-field (s)
  18768. "Quote field for inclusion in CSV material."
  18769. (if (string-match "[\",]" s)
  18770. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18771. s))
  18772. (defun org-force-self-insert (N)
  18773. "Needed to enforce self-insert under remapping."
  18774. (interactive "p")
  18775. (self-insert-command N))
  18776. (defun org-string-width (s)
  18777. "Compute width of string, ignoring invisible characters.
  18778. This ignores character with invisibility property `org-link', and also
  18779. characters with property `org-cwidth', because these will become invisible
  18780. upon the next fontification round."
  18781. (let (b l)
  18782. (when (or (eq t buffer-invisibility-spec)
  18783. (assq 'org-link buffer-invisibility-spec))
  18784. (while (setq b (text-property-any 0 (length s)
  18785. 'invisible 'org-link s))
  18786. (setq s (concat (substring s 0 b)
  18787. (substring s (or (next-single-property-change
  18788. b 'invisible s) (length s)))))))
  18789. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18790. (setq s (concat (substring s 0 b)
  18791. (substring s (or (next-single-property-change
  18792. b 'org-cwidth s) (length s))))))
  18793. (setq l (string-width s) b -1)
  18794. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18795. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18796. l))
  18797. (defun org-shorten-string (s maxlength)
  18798. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18799. If the string is shorter or has length MAXLENGTH, just return the
  18800. original string. If it is longer, the functions finds a space in the
  18801. string, breaks this string off at that locations and adds three dots
  18802. as ellipsis. Including the ellipsis, the string will not be longer
  18803. than MAXLENGTH. If finding a good breaking point in the string does
  18804. not work, the string is just chopped off in the middle of a word
  18805. if necessary."
  18806. (if (<= (length s) maxlength)
  18807. s
  18808. (let* ((n (max (- maxlength 4) 1))
  18809. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18810. (if (string-match re s)
  18811. (concat (match-string 1 s) "...")
  18812. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18813. (defun org-get-indentation (&optional line)
  18814. "Get the indentation of the current line, interpreting tabs.
  18815. When LINE is given, assume it represents a line and compute its indentation."
  18816. (if line
  18817. (if (string-match "^ *" (org-remove-tabs line))
  18818. (match-end 0))
  18819. (save-excursion
  18820. (beginning-of-line 1)
  18821. (skip-chars-forward " \t")
  18822. (current-column))))
  18823. (defun org-get-string-indentation (s)
  18824. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18825. (let ((n -1) (i 0) (w tab-width) c)
  18826. (catch 'exit
  18827. (while (< (setq n (1+ n)) (length s))
  18828. (setq c (aref s n))
  18829. (cond ((= c ?\ ) (setq i (1+ i)))
  18830. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18831. (t (throw 'exit t)))))
  18832. i))
  18833. (defun org-remove-tabs (s &optional width)
  18834. "Replace tabulators in S with spaces.
  18835. Assumes that s is a single line, starting in column 0."
  18836. (setq width (or width tab-width))
  18837. (while (string-match "\t" s)
  18838. (setq s (replace-match
  18839. (make-string
  18840. (- (* width (/ (+ (match-beginning 0) width) width))
  18841. (match-beginning 0)) ?\ )
  18842. t t s)))
  18843. s)
  18844. (defun org-fix-indentation (line ind)
  18845. "Fix indentation in LINE.
  18846. IND is a cons cell with target and minimum indentation.
  18847. If the current indentation in LINE is smaller than the minimum,
  18848. leave it alone. If it is larger than ind, set it to the target."
  18849. (let* ((l (org-remove-tabs line))
  18850. (i (org-get-indentation l))
  18851. (i1 (car ind)) (i2 (cdr ind)))
  18852. (if (>= i i2) (setq l (substring line i2)))
  18853. (if (> i1 0)
  18854. (concat (make-string i1 ?\ ) l)
  18855. l)))
  18856. (defun org-remove-indentation (code &optional n)
  18857. "Remove the maximum common indentation from the lines in CODE.
  18858. N may optionally be the number of spaces to remove."
  18859. (with-temp-buffer
  18860. (insert code)
  18861. (org-do-remove-indentation n)
  18862. (buffer-string)))
  18863. (defun org-do-remove-indentation (&optional n)
  18864. "Remove the maximum common indentation from the buffer."
  18865. (untabify (point-min) (point-max))
  18866. (let ((min 10000) re)
  18867. (if n
  18868. (setq min n)
  18869. (goto-char (point-min))
  18870. (while (re-search-forward "^ *[^ \n]" nil t)
  18871. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18872. (unless (or (= min 0) (= min 10000))
  18873. (setq re (format "^ \\{%d\\}" min))
  18874. (goto-char (point-min))
  18875. (while (re-search-forward re nil t)
  18876. (replace-match "")
  18877. (end-of-line 1))
  18878. min)))
  18879. (defun org-fill-template (template alist)
  18880. "Find each %key of ALIST in TEMPLATE and replace it."
  18881. (let ((case-fold-search nil)
  18882. entry key value)
  18883. (setq alist (sort (copy-sequence alist)
  18884. (lambda (a b) (< (length (car a)) (length (car b))))))
  18885. (while (setq entry (pop alist))
  18886. (setq template
  18887. (replace-regexp-in-string
  18888. (concat "%" (regexp-quote (car entry)))
  18889. (or (cdr entry) "") template t t)))
  18890. template))
  18891. (defun org-base-buffer (buffer)
  18892. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18893. (if (not buffer)
  18894. buffer
  18895. (or (buffer-base-buffer buffer)
  18896. buffer)))
  18897. (defun org-trim (s)
  18898. "Remove whitespace at beginning and end of string."
  18899. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18900. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18901. s)
  18902. (defun org-wrap (string &optional width lines)
  18903. "Wrap string to either a number of lines, or a width in characters.
  18904. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18905. that costs. If there is a word longer than WIDTH, the text is actually
  18906. wrapped to the length of that word.
  18907. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18908. many lines, whatever width that takes.
  18909. The return value is a list of lines, without newlines at the end."
  18910. (let* ((words (org-split-string string "[ \t\n]+"))
  18911. (maxword (apply 'max (mapcar 'org-string-width words)))
  18912. w ll)
  18913. (cond (width
  18914. (org-do-wrap words (max maxword width)))
  18915. (lines
  18916. (setq w maxword)
  18917. (setq ll (org-do-wrap words maxword))
  18918. (if (<= (length ll) lines)
  18919. ll
  18920. (setq ll words)
  18921. (while (> (length ll) lines)
  18922. (setq w (1+ w))
  18923. (setq ll (org-do-wrap words w)))
  18924. ll))
  18925. (t (error "Cannot wrap this")))))
  18926. (defun org-do-wrap (words width)
  18927. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18928. (let (lines line)
  18929. (while words
  18930. (setq line (pop words))
  18931. (while (and words (< (+ (length line) (length (car words))) width))
  18932. (setq line (concat line " " (pop words))))
  18933. (setq lines (push line lines)))
  18934. (nreverse lines)))
  18935. (defun org-split-string (string &optional separators)
  18936. "Splits STRING into substrings at SEPARATORS.
  18937. No empty strings are returned if there are matches at the beginning
  18938. and end of string."
  18939. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18940. (start 0)
  18941. notfirst
  18942. (list nil))
  18943. (while (and (string-match rexp string
  18944. (if (and notfirst
  18945. (= start (match-beginning 0))
  18946. (< start (length string)))
  18947. (1+ start) start))
  18948. (< (match-beginning 0) (length string)))
  18949. (setq notfirst t)
  18950. (or (eq (match-beginning 0) 0)
  18951. (and (eq (match-beginning 0) (match-end 0))
  18952. (eq (match-beginning 0) start))
  18953. (setq list
  18954. (cons (substring string start (match-beginning 0))
  18955. list)))
  18956. (setq start (match-end 0)))
  18957. (or (eq start (length string))
  18958. (setq list
  18959. (cons (substring string start)
  18960. list)))
  18961. (nreverse list)))
  18962. (defun org-quote-vert (s)
  18963. "Replace \"|\" with \"\\vert\"."
  18964. (while (string-match "|" s)
  18965. (setq s (replace-match "\\vert" t t s)))
  18966. s)
  18967. (defun org-uuidgen-p (s)
  18968. "Is S an ID created by UUIDGEN?"
  18969. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18970. (defun org-in-src-block-p (&optional inside)
  18971. "Whether point is in a code source block.
  18972. When INSIDE is non-nil, don't consider we are within a src block
  18973. when point is at #+BEGIN_SRC or #+END_SRC."
  18974. (let ((case-fold-search t) ov)
  18975. (or (and (setq ov (overlays-at (point)))
  18976. (memq 'org-block-background
  18977. (overlay-properties (car ov))))
  18978. (and (not inside)
  18979. (save-match-data
  18980. (save-excursion
  18981. (beginning-of-line)
  18982. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  18983. (defun org-context ()
  18984. "Return a list of contexts of the current cursor position.
  18985. If several contexts apply, all are returned.
  18986. Each context entry is a list with a symbol naming the context, and
  18987. two positions indicating start and end of the context. Possible
  18988. contexts are:
  18989. :headline anywhere in a headline
  18990. :headline-stars on the leading stars in a headline
  18991. :todo-keyword on a TODO keyword (including DONE) in a headline
  18992. :tags on the TAGS in a headline
  18993. :priority on the priority cookie in a headline
  18994. :item on the first line of a plain list item
  18995. :item-bullet on the bullet/number of a plain list item
  18996. :checkbox on the checkbox in a plain list item
  18997. :table in an org-mode table
  18998. :table-special on a special filed in a table
  18999. :table-table in a table.el table
  19000. :clocktable in a clocktable
  19001. :src-block in a source block
  19002. :link on a hyperlink
  19003. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19004. :target on a <<target>>
  19005. :radio-target on a <<<radio-target>>>
  19006. :latex-fragment on a LaTeX fragment
  19007. :latex-preview on a LaTeX fragment with overlaid preview image
  19008. This function expects the position to be visible because it uses font-lock
  19009. faces as a help to recognize the following contexts: :table-special, :link,
  19010. and :keyword."
  19011. (let* ((f (get-text-property (point) 'face))
  19012. (faces (if (listp f) f (list f)))
  19013. (case-fold-search t)
  19014. (p (point)) clist o)
  19015. ;; First the large context
  19016. (cond
  19017. ((org-at-heading-p t)
  19018. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19019. (when (progn
  19020. (beginning-of-line 1)
  19021. (looking-at org-todo-line-tags-regexp))
  19022. (push (org-point-in-group p 1 :headline-stars) clist)
  19023. (push (org-point-in-group p 2 :todo-keyword) clist)
  19024. (push (org-point-in-group p 4 :tags) clist))
  19025. (goto-char p)
  19026. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19027. (if (looking-at "\\[#[A-Z0-9]\\]")
  19028. (push (org-point-in-group p 0 :priority) clist)))
  19029. ((org-at-item-p)
  19030. (push (org-point-in-group p 2 :item-bullet) clist)
  19031. (push (list :item (point-at-bol)
  19032. (save-excursion (org-end-of-item) (point)))
  19033. clist)
  19034. (and (org-at-item-checkbox-p)
  19035. (push (org-point-in-group p 0 :checkbox) clist)))
  19036. ((org-at-table-p)
  19037. (push (list :table (org-table-begin) (org-table-end)) clist)
  19038. (if (memq 'org-formula faces)
  19039. (push (list :table-special
  19040. (previous-single-property-change p 'face)
  19041. (next-single-property-change p 'face)) clist)))
  19042. ((org-at-table-p 'any)
  19043. (push (list :table-table) clist)))
  19044. (goto-char p)
  19045. (let ((case-fold-search t))
  19046. ;; New the "medium" contexts: clocktables, source blocks
  19047. (cond ((org-in-clocktable-p)
  19048. (push (list :clocktable
  19049. (and (or (looking-at "#\\+BEGIN: clocktable")
  19050. (search-backward "#+BEGIN: clocktable" nil t))
  19051. (match-beginning 0))
  19052. (and (re-search-forward "#\\+END:?" nil t)
  19053. (match-end 0))) clist))
  19054. ((org-in-src-block-p)
  19055. (push (list :src-block
  19056. (and (or (looking-at "#\\+BEGIN_SRC")
  19057. (search-backward "#+BEGIN_SRC" nil t))
  19058. (match-beginning 0))
  19059. (and (search-forward "#+END_SRC" nil t)
  19060. (match-beginning 0))) clist))))
  19061. (goto-char p)
  19062. ;; Now the small context
  19063. (cond
  19064. ((org-at-timestamp-p)
  19065. (push (org-point-in-group p 0 :timestamp) clist))
  19066. ((memq 'org-link faces)
  19067. (push (list :link
  19068. (previous-single-property-change p 'face)
  19069. (next-single-property-change p 'face)) clist))
  19070. ((memq 'org-special-keyword faces)
  19071. (push (list :keyword
  19072. (previous-single-property-change p 'face)
  19073. (next-single-property-change p 'face)) clist))
  19074. ((org-at-target-p)
  19075. (push (org-point-in-group p 0 :target) clist)
  19076. (goto-char (1- (match-beginning 0)))
  19077. (if (looking-at org-radio-target-regexp)
  19078. (push (org-point-in-group p 0 :radio-target) clist))
  19079. (goto-char p))
  19080. ((setq o (car (delq nil
  19081. (mapcar
  19082. (lambda (x)
  19083. (if (memq x org-latex-fragment-image-overlays) x))
  19084. (overlays-at (point))))))
  19085. (push (list :latex-fragment
  19086. (overlay-start o) (overlay-end o)) clist)
  19087. (push (list :latex-preview
  19088. (overlay-start o) (overlay-end o)) clist))
  19089. ((org-inside-LaTeX-fragment-p)
  19090. ;; FIXME: positions wrong.
  19091. (push (list :latex-fragment (point) (point)) clist)))
  19092. (setq clist (nreverse (delq nil clist)))
  19093. clist))
  19094. ;; FIXME: Compare with at-regexp-p Do we need both?
  19095. (defun org-in-regexp (re &optional nlines visually)
  19096. "Check if point is inside a match of regexp.
  19097. Normally only the current line is checked, but you can include NLINES extra
  19098. lines both before and after point into the search.
  19099. If VISUALLY is set, require that the cursor is not after the match but
  19100. really on, so that the block visually is on the match."
  19101. (catch 'exit
  19102. (let ((pos (point))
  19103. (eol (point-at-eol (+ 1 (or nlines 0))))
  19104. (inc (if visually 1 0)))
  19105. (save-excursion
  19106. (beginning-of-line (- 1 (or nlines 0)))
  19107. (while (re-search-forward re eol t)
  19108. (if (and (<= (match-beginning 0) pos)
  19109. (>= (+ inc (match-end 0)) pos))
  19110. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19111. (defun org-at-regexp-p (regexp)
  19112. "Is point inside a match of REGEXP in the current line?"
  19113. (catch 'exit
  19114. (save-excursion
  19115. (let ((pos (point)) (end (point-at-eol)))
  19116. (beginning-of-line 1)
  19117. (while (re-search-forward regexp end t)
  19118. (if (and (<= (match-beginning 0) pos)
  19119. (>= (match-end 0) pos))
  19120. (throw 'exit t)))
  19121. nil))))
  19122. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19123. "Non-nil when point is between matches of START-RE and END-RE.
  19124. Also return a non-nil value when point is on one of the matches.
  19125. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19126. buffer positions. Default values are the positions of headlines
  19127. surrounding the point.
  19128. The functions returns a cons cell whose car (resp. cdr) is the
  19129. position before START-RE (resp. after END-RE)."
  19130. (save-match-data
  19131. (let ((pos (point))
  19132. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19133. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19134. beg end)
  19135. (save-excursion
  19136. ;; Point is on a block when on START-RE or if START-RE can be
  19137. ;; found before it...
  19138. (and (or (org-at-regexp-p start-re)
  19139. (re-search-backward start-re limit-up t))
  19140. (setq beg (match-beginning 0))
  19141. ;; ... and END-RE after it...
  19142. (goto-char (match-end 0))
  19143. (re-search-forward end-re limit-down t)
  19144. (> (setq end (match-end 0)) pos)
  19145. ;; ... without another START-RE in-between.
  19146. (goto-char (match-beginning 0))
  19147. (not (re-search-backward start-re (1+ beg) t))
  19148. ;; Return value.
  19149. (cons beg end))))))
  19150. (defun org-in-block-p (names)
  19151. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19152. NAMES is a list of strings containing names of blocks.
  19153. Return first block name matched, or nil. Beware that in case of
  19154. nested blocks, the returned name may not belong to the closest
  19155. block from point."
  19156. (save-match-data
  19157. (catch 'exit
  19158. (let ((case-fold-search t)
  19159. (lim-up (save-excursion (outline-previous-heading)))
  19160. (lim-down (save-excursion (outline-next-heading))))
  19161. (mapc (lambda (name)
  19162. (let ((n (regexp-quote name)))
  19163. (when (org-between-regexps-p
  19164. (concat "^[ \t]*#\\+begin_" n)
  19165. (concat "^[ \t]*#\\+end_" n)
  19166. lim-up lim-down)
  19167. (throw 'exit n))))
  19168. names))
  19169. nil)))
  19170. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19171. "Call `multi-occur' with buffers for all agenda files."
  19172. (interactive "sOrg-files matching: \np")
  19173. (let* ((files (org-agenda-files))
  19174. (tnames (mapcar 'file-truename files))
  19175. (extra org-agenda-text-search-extra-files)
  19176. f)
  19177. (when (eq (car extra) 'agenda-archives)
  19178. (setq extra (cdr extra))
  19179. (setq files (org-add-archive-files files)))
  19180. (while (setq f (pop extra))
  19181. (unless (member (file-truename f) tnames)
  19182. (add-to-list 'files f 'append)
  19183. (add-to-list 'tnames (file-truename f) 'append)))
  19184. (multi-occur
  19185. (mapcar (lambda (x)
  19186. (with-current-buffer
  19187. (or (get-file-buffer x) (find-file-noselect x))
  19188. (widen)
  19189. (current-buffer)))
  19190. files)
  19191. regexp)))
  19192. (if (boundp 'occur-mode-find-occurrence-hook)
  19193. ;; Emacs 23
  19194. (add-hook 'occur-mode-find-occurrence-hook
  19195. (lambda ()
  19196. (when (derived-mode-p 'org-mode)
  19197. (org-reveal))))
  19198. ;; Emacs 22
  19199. (defadvice occur-mode-goto-occurrence
  19200. (after org-occur-reveal activate)
  19201. (and (derived-mode-p 'org-mode) (org-reveal)))
  19202. (defadvice occur-mode-goto-occurrence-other-window
  19203. (after org-occur-reveal activate)
  19204. (and (derived-mode-p 'org-mode) (org-reveal)))
  19205. (defadvice occur-mode-display-occurrence
  19206. (after org-occur-reveal activate)
  19207. (when (derived-mode-p 'org-mode)
  19208. (let ((pos (occur-mode-find-occurrence)))
  19209. (with-current-buffer (marker-buffer pos)
  19210. (save-excursion
  19211. (goto-char pos)
  19212. (org-reveal)))))))
  19213. (defun org-occur-link-in-agenda-files ()
  19214. "Create a link and search for it in the agendas.
  19215. The link is not stored in `org-stored-links', it is just created
  19216. for the search purpose."
  19217. (interactive)
  19218. (let ((link (condition-case nil
  19219. (org-store-link nil)
  19220. (error "Unable to create a link to here"))))
  19221. (org-occur-in-agenda-files (regexp-quote link))))
  19222. (defun org-uniquify (list)
  19223. "Remove duplicate elements from LIST."
  19224. (let (res)
  19225. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19226. res))
  19227. (defun org-delete-all (elts list)
  19228. "Remove all elements in ELTS from LIST."
  19229. (while elts
  19230. (setq list (delete (pop elts) list)))
  19231. list)
  19232. (defun org-count (cl-item cl-seq)
  19233. "Count the number of occurrences of ITEM in SEQ.
  19234. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19235. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19236. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19237. (while (< cl-start cl-end)
  19238. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19239. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19240. (setq cl-start (1+ cl-start)))
  19241. cl-count))
  19242. (defun org-remove-if (predicate seq)
  19243. "Remove everything from SEQ that fulfills PREDICATE."
  19244. (let (res e)
  19245. (while seq
  19246. (setq e (pop seq))
  19247. (if (not (funcall predicate e)) (push e res)))
  19248. (nreverse res)))
  19249. (defun org-remove-if-not (predicate seq)
  19250. "Remove everything from SEQ that does not fulfill PREDICATE."
  19251. (let (res e)
  19252. (while seq
  19253. (setq e (pop seq))
  19254. (if (funcall predicate e) (push e res)))
  19255. (nreverse res)))
  19256. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19257. "Reduce two-argument FUNCTION across SEQ.
  19258. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19259. \":initial-value\" removed."
  19260. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19261. (cadr (memq :initial-value cl-keys)))
  19262. (cl-seq (pop cl-seq))
  19263. (t (funcall cl-func)))))
  19264. (while cl-seq
  19265. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19266. cl-accum))
  19267. (defun org-back-over-empty-lines ()
  19268. "Move backwards over whitespace, to the beginning of the first empty line.
  19269. Returns the number of empty lines passed."
  19270. (let ((pos (point)))
  19271. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19272. (skip-chars-backward " \t\n\r")
  19273. (unless (eobp)
  19274. (forward-line -1)))
  19275. (beginning-of-line 2)
  19276. (goto-char (min (point) pos))
  19277. (count-lines (point) pos)))
  19278. (defun org-skip-whitespace ()
  19279. (skip-chars-forward " \t\n\r"))
  19280. (defun org-point-in-group (point group &optional context)
  19281. "Check if POINT is in match-group GROUP.
  19282. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19283. match. If the match group does not exist or point is not inside it,
  19284. return nil."
  19285. (and (match-beginning group)
  19286. (>= point (match-beginning group))
  19287. (<= point (match-end group))
  19288. (if context
  19289. (list context (match-beginning group) (match-end group))
  19290. t)))
  19291. (defun org-switch-to-buffer-other-window (&rest args)
  19292. "Switch to buffer in a second window on the current frame.
  19293. In particular, do not allow pop-up frames.
  19294. Returns the newly created buffer."
  19295. (org-no-popups
  19296. (apply 'switch-to-buffer-other-window args)))
  19297. (defun org-combine-plists (&rest plists)
  19298. "Create a single property list from all plists in PLISTS.
  19299. The process starts by copying the first list, and then setting properties
  19300. from the other lists. Settings in the last list are the most significant
  19301. ones and overrule settings in the other lists."
  19302. (let ((rtn (copy-sequence (pop plists)))
  19303. p v ls)
  19304. (while plists
  19305. (setq ls (pop plists))
  19306. (while ls
  19307. (setq p (pop ls) v (pop ls))
  19308. (setq rtn (plist-put rtn p v))))
  19309. rtn))
  19310. (defun org-replace-escapes (string table)
  19311. "Replace %-escapes in STRING with values in TABLE.
  19312. TABLE is an association list with keys like \"%a\" and string values.
  19313. The sequences in STRING may contain normal field width and padding information,
  19314. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19315. so values can contain further %-escapes if they are define later in TABLE."
  19316. (let ((tbl (copy-alist table))
  19317. (case-fold-search nil)
  19318. (pchg 0)
  19319. e re rpl)
  19320. (while (setq e (pop tbl))
  19321. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19322. (when (and (cdr e) (string-match re (cdr e)))
  19323. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19324. (safe "SREF"))
  19325. (add-text-properties 0 3 (list 'sref sref) safe)
  19326. (setcdr e (replace-match safe t t (cdr e)))))
  19327. (while (string-match re string)
  19328. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19329. (cdr e)))
  19330. (setq string (replace-match rpl t t string))))
  19331. (while (setq pchg (next-property-change pchg string))
  19332. (let ((sref (get-text-property pchg 'sref string)))
  19333. (when (and sref (string-match "SREF" string pchg))
  19334. (setq string (replace-match sref t t string)))))
  19335. string))
  19336. (defun org-sublist (list start end)
  19337. "Return a section of LIST, from START to END.
  19338. Counting starts at 1."
  19339. (let (rtn (c start))
  19340. (setq list (nthcdr (1- start) list))
  19341. (while (and list (<= c end))
  19342. (push (pop list) rtn)
  19343. (setq c (1+ c)))
  19344. (nreverse rtn)))
  19345. (defun org-find-base-buffer-visiting (file)
  19346. "Like `find-buffer-visiting' but always return the base buffer and
  19347. not an indirect buffer."
  19348. (let ((buf (or (get-file-buffer file)
  19349. (find-buffer-visiting file))))
  19350. (if buf
  19351. (or (buffer-base-buffer buf) buf)
  19352. nil)))
  19353. (defun org-image-file-name-regexp (&optional extensions)
  19354. "Return regexp matching the file names of images.
  19355. If EXTENSIONS is given, only match these."
  19356. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19357. (image-file-name-regexp)
  19358. (let ((image-file-name-extensions
  19359. (or extensions
  19360. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19361. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19362. (concat "\\."
  19363. (regexp-opt (nconc (mapcar 'upcase
  19364. image-file-name-extensions)
  19365. image-file-name-extensions)
  19366. t)
  19367. "\\'"))))
  19368. (defun org-file-image-p (file &optional extensions)
  19369. "Return non-nil if FILE is an image."
  19370. (save-match-data
  19371. (string-match (org-image-file-name-regexp extensions) file)))
  19372. (defun org-get-cursor-date (&optional with-time)
  19373. "Return the date at cursor in as a time.
  19374. This works in the calendar and in the agenda, anywhere else it just
  19375. returns the current time.
  19376. If WITH-TIME is non-nil, returns the time of the event at point (in
  19377. the agenda) or the current time of the day."
  19378. (let (date day defd tp tm hod mod)
  19379. (when with-time
  19380. (setq tp (get-text-property (point) 'time))
  19381. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19382. (setq hod (string-to-number (match-string 1 tp))
  19383. mod (string-to-number (match-string 2 tp))))
  19384. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19385. mod (nth 1 (decode-time (current-time))))))
  19386. (cond
  19387. ((eq major-mode 'calendar-mode)
  19388. (setq date (calendar-cursor-to-date)
  19389. defd (encode-time 0 (or mod 0) (or hod 0)
  19390. (nth 1 date) (nth 0 date) (nth 2 date))))
  19391. ((eq major-mode 'org-agenda-mode)
  19392. (setq day (get-text-property (point) 'day))
  19393. (if day
  19394. (setq date (calendar-gregorian-from-absolute day)
  19395. defd (encode-time 0 (or mod 0) (or hod 0)
  19396. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19397. (or defd (current-time))))
  19398. (defun org-mark-subtree (&optional up)
  19399. "Mark the current subtree.
  19400. This puts point at the start of the current subtree, and mark at
  19401. the end. If a numeric prefix UP is given, move up into the
  19402. hierarchy of headlines by UP levels before marking the subtree."
  19403. (interactive "P")
  19404. (org-with-limited-levels
  19405. (cond ((org-at-heading-p) (beginning-of-line))
  19406. ((org-before-first-heading-p) (error "Not in a subtree"))
  19407. (t (outline-previous-visible-heading 1))))
  19408. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19409. (if (org-called-interactively-p 'any)
  19410. (call-interactively 'org-mark-element)
  19411. (org-mark-element)))
  19412. ;;; Macros
  19413. ;; Macros are expanded with `org-macro-replace-all', which relies
  19414. ;; internally on `org-macro-expand'.
  19415. ;; Default templates for expansion are stored in the buffer-local
  19416. ;; variable `org-macro-templates'. This variable is updated by
  19417. ;; `org-macro-initialize-templates'.
  19418. ;; Along with macros defined through #+MACRO: keyword, default
  19419. ;; templates include the following hard-coded macros:
  19420. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19421. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19422. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19423. ;; {{{title}}} will also be provided.
  19424. (defvar org-macro-templates nil
  19425. "Alist containing all macro templates in current buffer.
  19426. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19427. stands for macro's name and template for its replacement value,
  19428. both as strings. This is an internal variable. Do not set it
  19429. directly, use instead:
  19430. #+MACRO: name template")
  19431. (make-variable-buffer-local 'org-macro-templates)
  19432. (defun org-macro-expand (macro templates)
  19433. "Return expanded MACRO, as a string.
  19434. MACRO is an object, obtained, for example, with
  19435. `org-element-context'. TEMPLATES is an alist of templates used
  19436. for expansion. See `org-macro-templates' for a buffer-local
  19437. default value. Return nil if no template was found."
  19438. (let ((template
  19439. ;; Macro names are case-insensitive.
  19440. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19441. (when template
  19442. (let ((value (replace-regexp-in-string
  19443. "\\$[0-9]+"
  19444. (lambda (arg)
  19445. (or (nth (1- (string-to-number (substring arg 1)))
  19446. (org-element-property :args macro))
  19447. ;; No argument provided: remove
  19448. ;; place-holder.
  19449. ""))
  19450. template)))
  19451. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19452. (when (string-match "\\`(eval\\>" value)
  19453. (setq value (eval (read value))))
  19454. ;; Return string.
  19455. (format "%s" (or value ""))))))
  19456. (defun org-macro-replace-all (templates)
  19457. "Replace all macros in current buffer by their expansion.
  19458. TEMPLATES is an alist of templates used for expansion. See
  19459. `org-macro-templates' for a buffer-local default value."
  19460. (save-excursion
  19461. (goto-char (point-min))
  19462. (let (record)
  19463. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19464. (let ((object (org-element-context)))
  19465. (when (eq (org-element-type object) 'macro)
  19466. (let* ((value (org-macro-expand object templates))
  19467. (begin (org-element-property :begin object))
  19468. (signature (list begin
  19469. object
  19470. (org-element-property :args object))))
  19471. ;; Avoid circular dependencies by checking if the same
  19472. ;; macro with the same arguments is expanded at the same
  19473. ;; position twice.
  19474. (if (member signature record)
  19475. (error "Circular macro expansion: %s"
  19476. (org-element-property :key object))
  19477. (when value
  19478. (push signature record)
  19479. (delete-region
  19480. begin
  19481. ;; Preserve white spaces after the macro.
  19482. (progn (goto-char (org-element-property :end object))
  19483. (skip-chars-backward " \t")
  19484. (point)))
  19485. ;; Leave point before replacement in case of recursive
  19486. ;; expansions.
  19487. (save-excursion (insert value)))))))))))
  19488. (defun org-macro-initialize-templates ()
  19489. "Collect macro templates defined in current buffer.
  19490. Templates are stored in buffer-local variable
  19491. `org-macro-templates'. In addition to buffer-defined macros, the
  19492. function installs the following ones: \"property\",
  19493. \"time\". and, if the buffer is associated to a file,
  19494. \"input-file\" and \"modification-time\"."
  19495. (let ((case-fold-search t)
  19496. (set-template
  19497. (lambda (cell)
  19498. ;; Add CELL to `org-macro-templates' if there's no
  19499. ;; association matching its name already. Otherwise,
  19500. ;; replace old association with the new one in that
  19501. ;; variable.
  19502. (let ((old-template (assoc (car cell) org-macro-templates)))
  19503. (if old-template (setcdr old-template (cdr cell))
  19504. (push cell org-macro-templates))))))
  19505. ;; Install buffer-local macros.
  19506. (org-with-wide-buffer
  19507. (goto-char (point-min))
  19508. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19509. (let ((element (org-element-at-point)))
  19510. (when (eq (org-element-type element) 'keyword)
  19511. (let ((value (org-element-property :value element)))
  19512. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19513. (funcall set-template
  19514. (cons (match-string 1 value)
  19515. (or (match-string 2 value) "")))))))))
  19516. ;; Install hard-coded macros.
  19517. (mapc (lambda (cell) (funcall set-template cell))
  19518. (list
  19519. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19520. (cons "time" "(eval (format-time-string \"$1\"))")))
  19521. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19522. (when (and visited-file (file-exists-p visited-file))
  19523. (mapc (lambda (cell) (funcall set-template cell))
  19524. (list
  19525. (cons "input-file" (file-name-nondirectory visited-file))
  19526. (cons "modification-time"
  19527. (format "(eval (format-time-string \"$1\" '%s))"
  19528. (prin1-to-string
  19529. (nth 5 (file-attributes visited-file)))))))))))
  19530. ;;; Indentation
  19531. (defun org-indent-line ()
  19532. "Indent line depending on context."
  19533. (interactive)
  19534. (let* ((pos (point))
  19535. (itemp (org-at-item-p))
  19536. (case-fold-search t)
  19537. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19538. (inline-task-p (and (featurep 'org-inlinetask)
  19539. (org-inlinetask-in-task-p)))
  19540. (inline-re (and inline-task-p
  19541. (org-inlinetask-outline-regexp)))
  19542. column)
  19543. (if (and orgstruct-is-++ (eq pos (point)))
  19544. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19545. (indent-according-to-mode))
  19546. (beginning-of-line 1)
  19547. (cond
  19548. ;; Headings
  19549. ((looking-at org-outline-regexp) (setq column 0))
  19550. ;; Included files
  19551. ((looking-at "#\\+include:") (setq column 0))
  19552. ;; Footnote definition
  19553. ((looking-at org-footnote-definition-re) (setq column 0))
  19554. ;; Literal examples
  19555. ((looking-at "[ \t]*:\\( \\|$\\)")
  19556. (setq column (org-get-indentation))) ; do nothing
  19557. ;; Lists
  19558. ((ignore-errors (goto-char (org-in-item-p)))
  19559. (setq column (if itemp
  19560. (org-get-indentation)
  19561. (org-list-item-body-column (point))))
  19562. (goto-char pos))
  19563. ;; Drawers
  19564. ((and (looking-at "[ \t]*:END:")
  19565. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19566. (save-excursion
  19567. (goto-char (1- (match-beginning 1)))
  19568. (setq column (current-column))))
  19569. ;; Special blocks
  19570. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19571. (save-excursion
  19572. (re-search-backward
  19573. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19574. (setq column (org-get-indentation (match-string 0))))
  19575. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19576. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19577. (save-excursion
  19578. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19579. (setq column
  19580. (cond ((equal (downcase (match-string 1)) "src")
  19581. ;; src blocks: let `org-edit-src-exit' handle them
  19582. (org-get-indentation))
  19583. ((equal (downcase (match-string 1)) "example")
  19584. (max (org-get-indentation)
  19585. (org-get-indentation (match-string 0))))
  19586. (t
  19587. (org-get-indentation (match-string 0))))))
  19588. ;; This line has nothing special, look at the previous relevant
  19589. ;; line to compute indentation
  19590. (t
  19591. (beginning-of-line 0)
  19592. (while (and (not (bobp))
  19593. (not (looking-at org-drawer-regexp))
  19594. ;; When point started in an inline task, do not move
  19595. ;; above task starting line.
  19596. (not (and inline-task-p (looking-at inline-re)))
  19597. ;; Skip drawers, blocks, empty lines, verbatim,
  19598. ;; comments, tables, footnotes definitions, lists,
  19599. ;; inline tasks.
  19600. (or (and (looking-at "[ \t]*:END:")
  19601. (re-search-backward org-drawer-regexp nil t))
  19602. (and (looking-at "[ \t]*#\\+end_")
  19603. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19604. (looking-at "[ \t]*[\n:#|]")
  19605. (looking-at org-footnote-definition-re)
  19606. (and (ignore-errors (goto-char (org-in-item-p)))
  19607. (goto-char
  19608. (org-list-get-top-point (org-list-struct))))
  19609. (and (not inline-task-p)
  19610. (featurep 'org-inlinetask)
  19611. (org-inlinetask-in-task-p)
  19612. (or (org-inlinetask-goto-beginning) t))))
  19613. (beginning-of-line 0))
  19614. (cond
  19615. ;; There was an heading above.
  19616. ((looking-at "\\*+[ \t]+")
  19617. (if (not org-adapt-indentation)
  19618. (setq column 0)
  19619. (goto-char (match-end 0))
  19620. (setq column (current-column))))
  19621. ;; A drawer had started and is unfinished
  19622. ((looking-at org-drawer-regexp)
  19623. (goto-char (1- (match-beginning 1)))
  19624. (setq column (current-column)))
  19625. ;; Else, nothing noticeable found: get indentation and go on.
  19626. (t (setq column (org-get-indentation))))))
  19627. ;; Now apply indentation and move cursor accordingly
  19628. (goto-char pos)
  19629. (if (<= (current-column) (current-indentation))
  19630. (org-indent-line-to column)
  19631. (save-excursion (org-indent-line-to column)))
  19632. ;; Special polishing for properties, see `org-property-format'
  19633. (setq column (current-column))
  19634. (beginning-of-line 1)
  19635. (if (looking-at
  19636. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19637. (replace-match (concat (match-string 1)
  19638. (format org-property-format
  19639. (match-string 2) (match-string 3)))
  19640. t t))
  19641. (org-move-to-column column))))
  19642. (defun org-indent-drawer ()
  19643. "Indent the drawer at point."
  19644. (interactive)
  19645. (let ((p (point))
  19646. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19647. (match-end 0)))
  19648. (folded
  19649. (save-excursion
  19650. (end-of-line)
  19651. (when (overlays-at (point))
  19652. (member 'invisible (overlay-properties
  19653. (car (overlays-at (point)))))))))
  19654. (when folded (org-cycle))
  19655. (indent-for-tab-command)
  19656. (while (and (move-beginning-of-line 2) (< (point) e))
  19657. (indent-for-tab-command))
  19658. (goto-char p)
  19659. (when folded (org-cycle)))
  19660. (message "Drawer at point indented"))
  19661. (defun org-indent-block ()
  19662. "Indent the block at point."
  19663. (interactive)
  19664. (let ((p (point))
  19665. (case-fold-search t)
  19666. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19667. (match-end 0)))
  19668. (folded
  19669. (save-excursion
  19670. (end-of-line)
  19671. (when (overlays-at (point))
  19672. (member 'invisible (overlay-properties
  19673. (car (overlays-at (point)))))))))
  19674. (when folded (org-cycle))
  19675. (indent-for-tab-command)
  19676. (while (and (move-beginning-of-line 2) (< (point) e))
  19677. (indent-for-tab-command))
  19678. (goto-char p)
  19679. (when folded (org-cycle)))
  19680. (message "Block at point indented"))
  19681. (defun org-indent-region (start end)
  19682. "Indent region."
  19683. (interactive "r")
  19684. (save-excursion
  19685. (let ((line-end (org-current-line end)))
  19686. (goto-char start)
  19687. (while (< (org-current-line) line-end)
  19688. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19689. (t (call-interactively 'org-indent-line)))
  19690. (move-beginning-of-line 2)))))
  19691. ;;; Filling
  19692. ;; We use our own fill-paragraph and auto-fill functions.
  19693. ;; `org-fill-paragraph' relies on adaptive filling and context
  19694. ;; checking. Appropriate `fill-prefix' is computed with
  19695. ;; `org-adaptive-fill-function'.
  19696. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19697. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19698. ;; `org-adaptive-fill-function' value. Internally,
  19699. ;; `org-comment-line-break-function' breaks the line.
  19700. ;; `org-setup-filling' installs filling and auto-filling related
  19701. ;; variables during `org-mode' initialization.
  19702. (defun org-setup-filling ()
  19703. (interactive)
  19704. ;; Prevent auto-fill from inserting unwanted new items.
  19705. (when (boundp 'fill-nobreak-predicate)
  19706. (org-set-local
  19707. 'fill-nobreak-predicate
  19708. (org-uniquify
  19709. (append fill-nobreak-predicate
  19710. '(org-fill-paragraph-separate-nobreak-p
  19711. org-fill-line-break-nobreak-p)))))
  19712. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19713. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19714. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19715. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19716. (defvar org-element-paragraph-separate) ; org-element.el
  19717. (defun org-fill-paragraph-separate-nobreak-p ()
  19718. "Non-nil when a line break at point would insert a new item."
  19719. (looking-at (substring org-element-paragraph-separate 1)))
  19720. (defun org-fill-line-break-nobreak-p ()
  19721. "Non-nil when a line break at point would create an Org line break."
  19722. (save-excursion
  19723. (skip-chars-backward "[ \t]")
  19724. (skip-chars-backward "\\\\")
  19725. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19726. (declare-function message-in-body-p "message" ())
  19727. (defun org-adaptive-fill-function ()
  19728. "Compute a fill prefix for the current line.
  19729. Return fill prefix, as a string, or nil if current line isn't
  19730. meant to be filled."
  19731. (org-with-wide-buffer
  19732. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19733. ;; FIXME: This is really the job of orgstruct++-mode
  19734. (let* ((p (line-beginning-position))
  19735. (element (save-excursion (beginning-of-line)
  19736. (org-element-at-point)))
  19737. (type (org-element-type element))
  19738. (post-affiliated (org-element-property :post-affiliated element)))
  19739. (unless (and post-affiliated (< p post-affiliated))
  19740. (case type
  19741. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19742. (footnote-definition "")
  19743. ((item plain-list)
  19744. (make-string (org-list-item-body-column
  19745. (or post-affiliated
  19746. (org-element-property :begin element)))
  19747. ? ))
  19748. (paragraph
  19749. ;; Fill prefix is usually the same as the current line,
  19750. ;; except if the paragraph is at the beginning of an item.
  19751. (let ((parent (org-element-property :parent element)))
  19752. (cond ((eq (org-element-type parent) 'item)
  19753. (make-string (org-list-item-body-column
  19754. (org-element-property :begin parent))
  19755. ? ))
  19756. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19757. (match-string 0))
  19758. (t ""))))
  19759. (comment-block
  19760. ;; Only fill contents if P is within block boundaries.
  19761. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19762. (forward-line)
  19763. (point)))
  19764. (cend (save-excursion
  19765. (goto-char (org-element-property :end element))
  19766. (skip-chars-backward " \r\t\n")
  19767. (line-beginning-position))))
  19768. (when (and (>= p cbeg) (< p cend))
  19769. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19770. (match-string 0)
  19771. ""))))))))))
  19772. (declare-function message-goto-body "message" ())
  19773. (defvar message-cite-prefix-regexp) ; From message.el
  19774. (defvar org-element-all-objects) ; From org-element.el
  19775. (defun org-fill-paragraph (&optional justify)
  19776. "Fill element at point, when applicable.
  19777. This function only applies to comment blocks, comments, example
  19778. blocks and paragraphs. Also, as a special case, re-align table
  19779. when point is at one.
  19780. If JUSTIFY is non-nil (interactively, with prefix argument),
  19781. justify as well. If `sentence-end-double-space' is non-nil, then
  19782. period followed by one space does not end a sentence, so don't
  19783. break a line there. The variable `fill-column' controls the
  19784. width for filling.
  19785. For convenience, when point is at a plain list, an item or
  19786. a footnote definition, try to fill the first paragraph within."
  19787. (interactive)
  19788. (if (and (derived-mode-p 'message-mode)
  19789. (or (not (message-in-body-p))
  19790. (save-excursion (move-beginning-of-line 1)
  19791. (looking-at message-cite-prefix-regexp))))
  19792. ;; First ensure filling is correct in message-mode.
  19793. (let ((fill-paragraph-function
  19794. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19795. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19796. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19797. (paragraph-separate
  19798. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19799. (fill-paragraph nil))
  19800. (save-excursion
  19801. ;; Move to end of line in order to get the first paragraph
  19802. ;; within a plain list or a footnote definition.
  19803. (end-of-line)
  19804. (let ((element (org-element-at-point)))
  19805. ;; First check if point is in a blank line at the beginning of
  19806. ;; the buffer. In that case, ignore filling.
  19807. (if (< (point) (org-element-property :begin element)) t
  19808. (case (org-element-type element)
  19809. ;; Use major mode filling function is src blocks.
  19810. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19811. ;; Align Org tables, leave table.el tables as-is.
  19812. (table-row (org-table-align) t)
  19813. (table
  19814. (when (eq (org-element-property :type element) 'org)
  19815. (org-table-align))
  19816. t)
  19817. (paragraph
  19818. ;; Paragraphs may contain `line-break' type objects.
  19819. (let ((beg (max (point-min)
  19820. (org-element-property :contents-begin element)))
  19821. (end (min (point-max)
  19822. (org-element-property :contents-end element))))
  19823. ;; Do nothing if point is at an affiliated keyword.
  19824. (if (< (point) beg) t
  19825. (when (derived-mode-p 'message-mode)
  19826. ;; In `message-mode', do not fill following
  19827. ;; citation in current paragraph nor text before
  19828. ;; message body.
  19829. (let ((body-start (save-excursion (message-goto-body))))
  19830. (when body-start (setq beg (max body-start beg))))
  19831. (when (save-excursion
  19832. (re-search-forward
  19833. (concat "^" message-cite-prefix-regexp) end t))
  19834. (setq end (match-beginning 0))))
  19835. ;; Fill paragraph, taking line breaks into
  19836. ;; consideration. For that, slice the paragraph
  19837. ;; using line breaks as separators, and fill the
  19838. ;; parts in reverse order to avoid messing with
  19839. ;; markers.
  19840. (save-excursion
  19841. (goto-char end)
  19842. (mapc
  19843. (lambda (pos)
  19844. (fill-region-as-paragraph pos (point) justify)
  19845. (goto-char pos))
  19846. ;; Find the list of ending positions for line
  19847. ;; breaks in the current paragraph. Add paragraph
  19848. ;; beginning to include first slice.
  19849. (nreverse
  19850. (cons
  19851. beg
  19852. (org-element-map
  19853. (org-element--parse-objects
  19854. beg end nil org-element-all-objects)
  19855. 'line-break
  19856. (lambda (lb) (org-element-property :end lb)))))))
  19857. t)))
  19858. ;; Contents of `comment-block' type elements should be
  19859. ;; filled as plain text, but only if point is within block
  19860. ;; markers.
  19861. (comment-block
  19862. (let* ((case-fold-search t)
  19863. (beg (save-excursion
  19864. (goto-char (org-element-property :begin element))
  19865. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19866. (forward-line)
  19867. (point)))
  19868. (end (save-excursion
  19869. (goto-char (org-element-property :end element))
  19870. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19871. (line-beginning-position))))
  19872. (when (and (>= (point) beg) (< (point) end))
  19873. (fill-region-as-paragraph
  19874. (save-excursion
  19875. (end-of-line)
  19876. (re-search-backward "^[ \t]*$" beg 'move)
  19877. (line-beginning-position))
  19878. (save-excursion
  19879. (beginning-of-line)
  19880. (re-search-forward "^[ \t]*$" end 'move)
  19881. (line-beginning-position))
  19882. justify)))
  19883. t)
  19884. ;; Fill comments.
  19885. (comment (fill-comment-paragraph justify))
  19886. ;; Ignore every other element.
  19887. (otherwise t)))))))
  19888. (defun org-auto-fill-function ()
  19889. "Auto-fill function."
  19890. ;; Check if auto-filling is meaningful.
  19891. (let ((fc (current-fill-column)))
  19892. (when (and fc (> (current-column) fc))
  19893. (let* ((fill-prefix (org-adaptive-fill-function))
  19894. ;; Enforce empty fill prefix, if required. Otherwise, it
  19895. ;; will be computed again.
  19896. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19897. (when fill-prefix (do-auto-fill))))))
  19898. (defun org-comment-line-break-function (&optional soft)
  19899. "Break line at point and indent, continuing comment if within one.
  19900. The inserted newline is marked hard if variable
  19901. `use-hard-newlines' is true, unless optional argument SOFT is
  19902. non-nil."
  19903. (if soft (insert-and-inherit ?\n) (newline 1))
  19904. (save-excursion (forward-char -1) (delete-horizontal-space))
  19905. (delete-horizontal-space)
  19906. (indent-to-left-margin)
  19907. (insert-before-markers-and-inherit fill-prefix))
  19908. ;;; Comments
  19909. ;; Org comments syntax is quite complex. It requires the entire line
  19910. ;; to be just a comment. Also, even with the right syntax at the
  19911. ;; beginning of line, some some elements (i.e. verse-block or
  19912. ;; example-block) don't accept comments. Usual Emacs comment commands
  19913. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19914. ;; Org still relies on `comment-dwim', but cannot trust
  19915. ;; `comment-only-p'. So, `comment-region-function' and
  19916. ;; `uncomment-region-function' both point
  19917. ;; to`org-comment-or-uncomment-region'. Eventually,
  19918. ;; `org-insert-comment' takes care of insertion of comments at the
  19919. ;; beginning of line.
  19920. ;; `org-setup-comments-handling' install comments related variables
  19921. ;; during `org-mode' initialization.
  19922. (defun org-setup-comments-handling ()
  19923. (interactive)
  19924. (org-set-local 'comment-use-syntax nil)
  19925. (org-set-local 'comment-start "# ")
  19926. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19927. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19928. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19929. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19930. (defun org-insert-comment ()
  19931. "Insert an empty comment above current line.
  19932. If the line is empty, insert comment at its beginning."
  19933. (beginning-of-line)
  19934. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19935. (org-indent-line)
  19936. (insert "# "))
  19937. (defvar comment-empty-lines) ; From newcomment.el.
  19938. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19939. "Comment or uncomment each non-blank line in the region.
  19940. Uncomment each non-blank line between BEG and END if it only
  19941. contains commented lines. Otherwise, comment them."
  19942. (save-restriction
  19943. ;; Restrict region
  19944. (narrow-to-region (save-excursion (goto-char beg)
  19945. (skip-chars-forward " \r\t\n" end)
  19946. (line-beginning-position))
  19947. (save-excursion (goto-char end)
  19948. (skip-chars-backward " \r\t\n" beg)
  19949. (line-end-position)))
  19950. (let ((uncommentp
  19951. ;; UNCOMMENTP is non-nil when every non blank line between
  19952. ;; BEG and END is a comment.
  19953. (save-excursion
  19954. (goto-char (point-min))
  19955. (while (and (not (eobp))
  19956. (let ((element (org-element-at-point)))
  19957. (and (eq (org-element-type element) 'comment)
  19958. (goto-char (min (point-max)
  19959. (org-element-property
  19960. :end element)))))))
  19961. (eobp))))
  19962. (if uncommentp
  19963. ;; Only blank lines and comments in region: uncomment it.
  19964. (save-excursion
  19965. (goto-char (point-min))
  19966. (while (not (eobp))
  19967. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19968. (replace-match "" nil nil nil 1))
  19969. (forward-line)))
  19970. ;; Comment each line in region.
  19971. (let ((min-indent (point-max)))
  19972. ;; First find the minimum indentation across all lines.
  19973. (save-excursion
  19974. (goto-char (point-min))
  19975. (while (and (not (eobp)) (not (zerop min-indent)))
  19976. (unless (looking-at "[ \t]*$")
  19977. (setq min-indent (min min-indent (current-indentation))))
  19978. (forward-line)))
  19979. ;; Then loop over all lines.
  19980. (save-excursion
  19981. (goto-char (point-min))
  19982. (while (not (eobp))
  19983. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19984. (org-move-to-column min-indent t)
  19985. (insert comment-start))
  19986. (forward-line))))))))
  19987. ;;; Other stuff.
  19988. (defun org-toggle-fixed-width-section (arg)
  19989. "Toggle the fixed-width export.
  19990. If there is no active region, the QUOTE keyword at the current headline is
  19991. inserted or removed. When present, it causes the text between this headline
  19992. and the next to be exported as fixed-width text, and unmodified.
  19993. If there is an active region, this command adds or removes a colon as the
  19994. first character of this line. If the first character of a line is a colon,
  19995. this line is also exported in fixed-width font."
  19996. (interactive "P")
  19997. (let* ((cc 0)
  19998. (regionp (org-region-active-p))
  19999. (beg (if regionp (region-beginning) (point)))
  20000. (end (if regionp (region-end)))
  20001. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20002. (case-fold-search nil)
  20003. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20004. off)
  20005. (if regionp
  20006. (save-excursion
  20007. (goto-char beg)
  20008. (setq cc (current-column))
  20009. (beginning-of-line 1)
  20010. (setq off (looking-at re))
  20011. (while (> nlines 0)
  20012. (setq nlines (1- nlines))
  20013. (beginning-of-line 1)
  20014. (cond
  20015. (arg
  20016. (org-move-to-column cc t)
  20017. (insert ": \n")
  20018. (forward-line -1))
  20019. ((and off (looking-at re))
  20020. (replace-match "" t t nil 1))
  20021. ((not off) (org-move-to-column cc t) (insert ": ")))
  20022. (forward-line 1)))
  20023. (save-excursion
  20024. (org-back-to-heading)
  20025. (cond
  20026. ((looking-at (format org-heading-keyword-regexp-format
  20027. org-quote-string))
  20028. (goto-char (match-end 1))
  20029. (looking-at (concat " +" org-quote-string))
  20030. (replace-match "" t t)
  20031. (when (eolp) (insert " ")))
  20032. ((looking-at org-outline-regexp)
  20033. (goto-char (match-end 0))
  20034. (insert org-quote-string " ")))))))
  20035. (defun org-reftex-citation ()
  20036. "Use reftex-citation to insert a citation into the buffer.
  20037. This looks for a line like
  20038. #+BIBLIOGRAPHY: foo plain option:-d
  20039. and derives from it that foo.bib is the bibliography file relevant
  20040. for this document. It then installs the necessary environment for RefTeX
  20041. to work in this buffer and calls `reftex-citation' to insert a citation
  20042. into the buffer.
  20043. Export of such citations to both LaTeX and HTML is handled by the contributed
  20044. package org-exp-bibtex by Taru Karttunen."
  20045. (interactive)
  20046. (let ((reftex-docstruct-symbol 'rds)
  20047. (reftex-cite-format "\\cite{%l}")
  20048. rds bib)
  20049. (save-excursion
  20050. (save-restriction
  20051. (widen)
  20052. (let ((case-fold-search t)
  20053. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20054. (if (not (save-excursion
  20055. (or (re-search-forward re nil t)
  20056. (re-search-backward re nil t))))
  20057. (error "No bibliography defined in file")
  20058. (setq bib (concat (match-string 1) ".bib")
  20059. rds (list (list 'bib bib)))))))
  20060. (call-interactively 'reftex-citation)))
  20061. ;;;; Functions extending outline functionality
  20062. (defun org-beginning-of-line (&optional arg)
  20063. "Go to the beginning of the current line. If that is invisible, continue
  20064. to a visible line beginning. This makes the function of C-a more intuitive.
  20065. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20066. first attempt, and only move to after the tags when the cursor is already
  20067. beyond the end of the headline."
  20068. (interactive "P")
  20069. (let ((pos (point))
  20070. (special (if (consp org-special-ctrl-a/e)
  20071. (car org-special-ctrl-a/e)
  20072. org-special-ctrl-a/e))
  20073. refpos)
  20074. (if (org-bound-and-true-p visual-line-mode)
  20075. (beginning-of-visual-line 1)
  20076. (beginning-of-line 1))
  20077. (if (and arg (fboundp 'move-beginning-of-line))
  20078. (call-interactively 'move-beginning-of-line)
  20079. (if (bobp)
  20080. nil
  20081. (backward-char 1)
  20082. (if (org-truely-invisible-p)
  20083. (while (and (not (bobp)) (org-truely-invisible-p))
  20084. (backward-char 1)
  20085. (beginning-of-line 1))
  20086. (forward-char 1))))
  20087. (when special
  20088. (cond
  20089. ((and (looking-at org-complex-heading-regexp)
  20090. (= (char-after (match-end 1)) ?\ ))
  20091. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20092. (point-at-eol)))
  20093. (goto-char
  20094. (if (eq special t)
  20095. (cond ((> pos refpos) refpos)
  20096. ((= pos (point)) refpos)
  20097. (t (point)))
  20098. (cond ((> pos (point)) (point))
  20099. ((not (eq last-command this-command)) (point))
  20100. (t refpos)))))
  20101. ((org-at-item-p)
  20102. ;; Being at an item and not looking at an the item means point
  20103. ;; was previously moved to beginning of a visual line, which
  20104. ;; doesn't contain the item. Therefore, do nothing special,
  20105. ;; just stay here.
  20106. (when (looking-at org-list-full-item-re)
  20107. ;; Set special position at first white space character after
  20108. ;; bullet, and check-box, if any.
  20109. (let ((after-bullet
  20110. (let ((box (match-end 3)))
  20111. (if (not box) (match-end 1)
  20112. (let ((after (char-after box)))
  20113. (if (and after (= after ? )) (1+ box) box))))))
  20114. ;; Special case: Move point to special position when
  20115. ;; currently after it or at beginning of line.
  20116. (if (eq special t)
  20117. (when (or (> pos after-bullet) (= (point) pos))
  20118. (goto-char after-bullet))
  20119. ;; Reversed case: Move point to special position when
  20120. ;; point was already at beginning of line and command is
  20121. ;; repeated.
  20122. (when (and (= (point) pos) (eq last-command this-command))
  20123. (goto-char after-bullet))))))))
  20124. (org-no-warnings
  20125. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20126. (defun org-end-of-line (&optional arg)
  20127. "Go to the end of the line.
  20128. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20129. tags on the first attempt, and only move to after the tags when
  20130. the cursor is already beyond the end of the headline."
  20131. (interactive "P")
  20132. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20133. org-special-ctrl-a/e))
  20134. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20135. 'end-of-visual-line)
  20136. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20137. (t 'end-of-line))))
  20138. (if (or (not special) arg) (call-interactively move-fun)
  20139. (let* ((element (save-excursion (beginning-of-line)
  20140. (org-element-at-point)))
  20141. (type (org-element-type element)))
  20142. (cond
  20143. ((memq type '(headline inlinetask))
  20144. (let ((pos (point)))
  20145. (beginning-of-line 1)
  20146. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20147. (if (eq special t)
  20148. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20149. (goto-char (match-beginning 1))
  20150. (goto-char (match-end 0)))
  20151. (if (or (< pos (match-end 0))
  20152. (not (eq this-command last-command)))
  20153. (goto-char (match-end 0))
  20154. (goto-char (match-beginning 1))))
  20155. (call-interactively move-fun))))
  20156. ((org-element-property :hiddenp element)
  20157. ;; If element is hidden, `move-end-of-line' would put point
  20158. ;; after it. Use `end-of-line' to stay on current line.
  20159. (call-interactively 'end-of-line))
  20160. (t (call-interactively move-fun)))))
  20161. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20162. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20163. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20164. (defun org-backward-sentence (&optional arg)
  20165. "Go to beginning of sentence, or beginning of table field.
  20166. This will call `backward-sentence' or `org-table-beginning-of-field',
  20167. depending on context."
  20168. (interactive "P")
  20169. (cond
  20170. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20171. (t (call-interactively 'backward-sentence))))
  20172. (defun org-forward-sentence (&optional arg)
  20173. "Go to end of sentence, or end of table field.
  20174. This will call `forward-sentence' or `org-table-end-of-field',
  20175. depending on context."
  20176. (interactive "P")
  20177. (cond
  20178. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20179. (t (call-interactively 'forward-sentence))))
  20180. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20181. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20182. (defun org-kill-line (&optional arg)
  20183. "Kill line, to tags or end of line."
  20184. (interactive "P")
  20185. (cond
  20186. ((or (not org-special-ctrl-k)
  20187. (bolp)
  20188. (not (org-at-heading-p)))
  20189. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20190. org-ctrl-k-protect-subtree)
  20191. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20192. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20193. (error "C-k aborted - would kill hidden subtree")))
  20194. (call-interactively
  20195. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20196. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20197. (kill-region (point) (match-beginning 1))
  20198. (org-set-tags nil t))
  20199. (t (kill-region (point) (point-at-eol)))))
  20200. (define-key org-mode-map "\C-k" 'org-kill-line)
  20201. (defun org-yank (&optional arg)
  20202. "Yank. If the kill is a subtree, treat it specially.
  20203. This command will look at the current kill and check if is a single
  20204. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20205. cursor is at the beginning of a line or after the stars of a currently
  20206. empty headline, then the yank is handled specially. How exactly depends
  20207. on the value of the following variables, both set by default.
  20208. org-yank-folded-subtrees
  20209. When set, the subtree(s) will be folded after insertion, but only
  20210. if doing so would now swallow text after the yanked text.
  20211. org-yank-adjusted-subtrees
  20212. When set, the subtree will be promoted or demoted in order to
  20213. fit into the local outline tree structure, which means that the level
  20214. will be adjusted so that it becomes the smaller one of the two
  20215. *visible* surrounding headings.
  20216. Any prefix to this command will cause `yank' to be called directly with
  20217. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20218. will just
  20219. plainly yank the text as it is.
  20220. \[1] The test checks if the first non-white line is a heading
  20221. and if there are no other headings with fewer stars."
  20222. (interactive "P")
  20223. (org-yank-generic 'yank arg))
  20224. (defun org-yank-generic (command arg)
  20225. "Perform some yank-like command.
  20226. This function implements the behavior described in the `org-yank'
  20227. documentation. However, it has been generalized to work for any
  20228. interactive command with similar behavior."
  20229. ;; pretend to be command COMMAND
  20230. (setq this-command command)
  20231. (if arg
  20232. (call-interactively command)
  20233. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20234. (and (org-kill-is-subtree-p)
  20235. (or (bolp)
  20236. (and (looking-at "[ \t]*$")
  20237. (string-match
  20238. "\\`\\*+\\'"
  20239. (buffer-substring (point-at-bol) (point)))))))
  20240. swallowp)
  20241. (cond
  20242. ((and subtreep org-yank-folded-subtrees)
  20243. (let ((beg (point))
  20244. end)
  20245. (if (and subtreep org-yank-adjusted-subtrees)
  20246. (org-paste-subtree nil nil 'for-yank)
  20247. (call-interactively command))
  20248. (setq end (point))
  20249. (goto-char beg)
  20250. (when (and (bolp) subtreep
  20251. (not (setq swallowp
  20252. (org-yank-folding-would-swallow-text beg end))))
  20253. (org-with-limited-levels
  20254. (or (looking-at org-outline-regexp)
  20255. (re-search-forward org-outline-regexp-bol end t))
  20256. (while (and (< (point) end) (looking-at org-outline-regexp))
  20257. (hide-subtree)
  20258. (org-cycle-show-empty-lines 'folded)
  20259. (condition-case nil
  20260. (outline-forward-same-level 1)
  20261. (error (goto-char end))))))
  20262. (when swallowp
  20263. (message
  20264. "Inserted text not folded because that would swallow text"))
  20265. (goto-char end)
  20266. (skip-chars-forward " \t\n\r")
  20267. (beginning-of-line 1)
  20268. (push-mark beg 'nomsg)))
  20269. ((and subtreep org-yank-adjusted-subtrees)
  20270. (let ((beg (point-at-bol)))
  20271. (org-paste-subtree nil nil 'for-yank)
  20272. (push-mark beg 'nomsg)))
  20273. (t
  20274. (call-interactively command))))))
  20275. (defun org-yank-folding-would-swallow-text (beg end)
  20276. "Would hide-subtree at BEG swallow any text after END?"
  20277. (let (level)
  20278. (org-with-limited-levels
  20279. (save-excursion
  20280. (goto-char beg)
  20281. (when (or (looking-at org-outline-regexp)
  20282. (re-search-forward org-outline-regexp-bol end t))
  20283. (setq level (org-outline-level)))
  20284. (goto-char end)
  20285. (skip-chars-forward " \t\r\n\v\f")
  20286. (if (or (eobp)
  20287. (and (bolp) (looking-at org-outline-regexp)
  20288. (<= (org-outline-level) level)))
  20289. nil ; Nothing would be swallowed
  20290. t))))) ; something would swallow
  20291. (define-key org-mode-map "\C-y" 'org-yank)
  20292. (defun org-truely-invisible-p ()
  20293. "Check if point is at a character currently not visible.
  20294. This version does not only check the character property, but also
  20295. `visible-mode'."
  20296. ;; Early versions of noutline don't have `outline-invisible-p'.
  20297. (if (org-bound-and-true-p visible-mode)
  20298. nil
  20299. (outline-invisible-p)))
  20300. (defun org-invisible-p2 ()
  20301. "Check if point is at a character currently not visible."
  20302. (save-excursion
  20303. (if (and (eolp) (not (bobp))) (backward-char 1))
  20304. ;; Early versions of noutline don't have `outline-invisible-p'.
  20305. (outline-invisible-p)))
  20306. (defun org-back-to-heading (&optional invisible-ok)
  20307. "Call `outline-back-to-heading', but provide a better error message."
  20308. (condition-case nil
  20309. (outline-back-to-heading invisible-ok)
  20310. (error (error "Before first headline at position %d in buffer %s"
  20311. (point) (current-buffer)))))
  20312. (defun org-before-first-heading-p ()
  20313. "Before first heading?"
  20314. (save-excursion
  20315. (end-of-line)
  20316. (null (re-search-backward org-outline-regexp-bol nil t))))
  20317. (defun org-at-heading-p (&optional ignored)
  20318. (outline-on-heading-p t))
  20319. ;; Compatibility alias with Org versions < 7.8.03
  20320. (defalias 'org-on-heading-p 'org-at-heading-p)
  20321. (defun org-at-comment-p nil
  20322. "Is cursor in a line starting with a # character?"
  20323. (save-excursion
  20324. (beginning-of-line)
  20325. (looking-at "^#")))
  20326. (defun org-at-drawer-p nil
  20327. "Is cursor at a drawer keyword?"
  20328. (save-excursion
  20329. (move-beginning-of-line 1)
  20330. (looking-at org-drawer-regexp)))
  20331. (defun org-at-block-p nil
  20332. "Is cursor at a block keyword?"
  20333. (save-excursion
  20334. (move-beginning-of-line 1)
  20335. (looking-at org-block-regexp)))
  20336. (defun org-point-at-end-of-empty-headline ()
  20337. "If point is at the end of an empty headline, return t, else nil.
  20338. If the heading only contains a TODO keyword, it is still still considered
  20339. empty."
  20340. (and (looking-at "[ \t]*$")
  20341. (when org-todo-line-regexp
  20342. (save-excursion
  20343. (beginning-of-line 1)
  20344. (let ((case-fold-search nil))
  20345. (looking-at org-todo-line-regexp)
  20346. (string= (match-string 3) ""))))))
  20347. (defun org-at-heading-or-item-p ()
  20348. (or (org-at-heading-p) (org-at-item-p)))
  20349. (defun org-at-target-p ()
  20350. (or (org-in-regexp org-radio-target-regexp)
  20351. (org-in-regexp org-target-regexp)))
  20352. ;; Compatibility alias with Org versions < 7.8.03
  20353. (defalias 'org-on-target-p 'org-at-target-p)
  20354. (defun org-up-heading-all (arg)
  20355. "Move to the heading line of which the present line is a subheading.
  20356. This function considers both visible and invisible heading lines.
  20357. With argument, move up ARG levels."
  20358. (if (fboundp 'outline-up-heading-all)
  20359. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20360. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20361. (defun org-up-heading-safe ()
  20362. "Move to the heading line of which the present line is a subheading.
  20363. This version will not throw an error. It will return the level of the
  20364. headline found, or nil if no higher level is found.
  20365. Also, this function will be a lot faster than `outline-up-heading',
  20366. because it relies on stars being the outline starters. This can really
  20367. make a significant difference in outlines with very many siblings."
  20368. (let (start-level re)
  20369. (org-back-to-heading t)
  20370. (setq start-level (funcall outline-level))
  20371. (if (equal start-level 1)
  20372. nil
  20373. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20374. (if (re-search-backward re nil t)
  20375. (funcall outline-level)))))
  20376. (defun org-first-sibling-p ()
  20377. "Is this heading the first child of its parents?"
  20378. (interactive)
  20379. (let ((re org-outline-regexp-bol)
  20380. level l)
  20381. (unless (org-at-heading-p t)
  20382. (error "Not at a heading"))
  20383. (setq level (funcall outline-level))
  20384. (save-excursion
  20385. (if (not (re-search-backward re nil t))
  20386. t
  20387. (setq l (funcall outline-level))
  20388. (< l level)))))
  20389. (defun org-goto-sibling (&optional previous)
  20390. "Goto the next sibling, even if it is invisible.
  20391. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20392. when a sibling was found. When none is found, return nil and don't
  20393. move point."
  20394. (let ((fun (if previous 're-search-backward 're-search-forward))
  20395. (pos (point))
  20396. (re org-outline-regexp-bol)
  20397. level l)
  20398. (when (condition-case nil (org-back-to-heading t) (error nil))
  20399. (setq level (funcall outline-level))
  20400. (catch 'exit
  20401. (or previous (forward-char 1))
  20402. (while (funcall fun re nil t)
  20403. (setq l (funcall outline-level))
  20404. (when (< l level) (goto-char pos) (throw 'exit nil))
  20405. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20406. (goto-char pos)
  20407. nil))))
  20408. (defun org-show-siblings ()
  20409. "Show all siblings of the current headline."
  20410. (save-excursion
  20411. (while (org-goto-sibling) (org-flag-heading nil)))
  20412. (save-excursion
  20413. (while (org-goto-sibling 'previous)
  20414. (org-flag-heading nil))))
  20415. (defun org-goto-first-child ()
  20416. "Goto the first child, even if it is invisible.
  20417. Return t when a child was found. Otherwise don't move point and
  20418. return nil."
  20419. (let (level (pos (point)) (re org-outline-regexp-bol))
  20420. (when (condition-case nil (org-back-to-heading t) (error nil))
  20421. (setq level (outline-level))
  20422. (forward-char 1)
  20423. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20424. (progn (goto-char (match-beginning 0)) t)
  20425. (goto-char pos) nil))))
  20426. (defun org-show-hidden-entry ()
  20427. "Show an entry where even the heading is hidden."
  20428. (save-excursion
  20429. (org-show-entry)))
  20430. (defun org-flag-heading (flag &optional entry)
  20431. "Flag the current heading. FLAG non-nil means make invisible.
  20432. When ENTRY is non-nil, show the entire entry."
  20433. (save-excursion
  20434. (org-back-to-heading t)
  20435. ;; Check if we should show the entire entry
  20436. (if entry
  20437. (progn
  20438. (org-show-entry)
  20439. (save-excursion
  20440. (and (outline-next-heading)
  20441. (org-flag-heading nil))))
  20442. (outline-flag-region (max (point-min) (1- (point)))
  20443. (save-excursion (outline-end-of-heading) (point))
  20444. flag))))
  20445. (defun org-get-next-sibling ()
  20446. "Move to next heading of the same level, and return point.
  20447. If there is no such heading, return nil.
  20448. This is like outline-next-sibling, but invisible headings are ok."
  20449. (let ((level (funcall outline-level)))
  20450. (outline-next-heading)
  20451. (while (and (not (eobp)) (> (funcall outline-level) level))
  20452. (outline-next-heading))
  20453. (if (or (eobp) (< (funcall outline-level) level))
  20454. nil
  20455. (point))))
  20456. (defun org-get-last-sibling ()
  20457. "Move to previous heading of the same level, and return point.
  20458. If there is no such heading, return nil."
  20459. (let ((opoint (point))
  20460. (level (funcall outline-level)))
  20461. (outline-previous-heading)
  20462. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20463. (while (and (> (funcall outline-level) level)
  20464. (not (bobp)))
  20465. (outline-previous-heading))
  20466. (if (< (funcall outline-level) level)
  20467. nil
  20468. (point)))))
  20469. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20470. "Goto to the end of a subtree."
  20471. ;; This contains an exact copy of the original function, but it uses
  20472. ;; `org-back-to-heading', to make it work also in invisible
  20473. ;; trees. And is uses an invisible-ok argument.
  20474. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20475. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20476. ;; with many children and grandchildren etc, this can be much faster
  20477. ;; than the outline version.
  20478. (org-back-to-heading invisible-ok)
  20479. (let ((first t)
  20480. (level (funcall outline-level)))
  20481. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20482. ;; A true heading (not a plain list item), in Org-mode
  20483. ;; This means we can easily find the end by looking
  20484. ;; only for the right number of stars. Using a regexp to do
  20485. ;; this is so much faster than using a Lisp loop.
  20486. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20487. (forward-char 1)
  20488. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20489. ;; something else, do it the slow way
  20490. (while (and (not (eobp))
  20491. (or first (> (funcall outline-level) level)))
  20492. (setq first nil)
  20493. (outline-next-heading)))
  20494. (unless to-heading
  20495. (if (memq (preceding-char) '(?\n ?\^M))
  20496. (progn
  20497. ;; Go to end of line before heading
  20498. (forward-char -1)
  20499. (if (memq (preceding-char) '(?\n ?\^M))
  20500. ;; leave blank line before heading
  20501. (forward-char -1))))))
  20502. (point))
  20503. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20504. "Use Org version in org-mode, for dramatic speed-up."
  20505. (if (derived-mode-p 'org-mode)
  20506. (progn
  20507. (org-end-of-subtree nil t)
  20508. (unless (eobp) (backward-char 1)))
  20509. ad-do-it))
  20510. (defun org-end-of-meta-data-and-drawers ()
  20511. "Jump to the first text after meta data and drawers in the current entry.
  20512. This will move over empty lines, lines with planning time stamps,
  20513. clocking lines, and drawers."
  20514. (org-back-to-heading t)
  20515. (let ((end (save-excursion (outline-next-heading) (point)))
  20516. (re (concat "\\(" org-drawer-regexp "\\)"
  20517. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20518. (forward-line 1)
  20519. (while (re-search-forward re end t)
  20520. (if (not (match-end 1))
  20521. ;; empty or planning line
  20522. (forward-line 1)
  20523. ;; a drawer, find the end
  20524. (re-search-forward "^[ \t]*:END:" end 'move)
  20525. (forward-line 1)))
  20526. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20527. (point)))
  20528. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20529. "Move forward to the arg'th subheading at same level as this one.
  20530. Stop at the first and last subheadings of a superior heading.
  20531. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20532. non-nil it will also look at invisible ones."
  20533. (interactive "p")
  20534. (org-back-to-heading invisible-ok)
  20535. (org-at-heading-p)
  20536. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20537. (re (format "^\\*\\{1,%d\\} " level))
  20538. l)
  20539. (forward-char 1)
  20540. (while (> arg 0)
  20541. (while (and (re-search-forward re nil 'move)
  20542. (setq l (- (match-end 0) (match-beginning 0) 1))
  20543. (= l level)
  20544. (not invisible-ok)
  20545. (progn (backward-char 1) (outline-invisible-p)))
  20546. (if (< l level) (setq arg 1)))
  20547. (setq arg (1- arg)))
  20548. (beginning-of-line 1)))
  20549. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20550. "Move backward to the arg'th subheading at same level as this one.
  20551. Stop at the first and last subheadings of a superior heading."
  20552. (interactive "p")
  20553. (org-back-to-heading)
  20554. (org-at-heading-p)
  20555. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20556. (re (format "^\\*\\{1,%d\\} " level))
  20557. l)
  20558. (while (> arg 0)
  20559. (while (and (re-search-backward re nil 'move)
  20560. (setq l (- (match-end 0) (match-beginning 0) 1))
  20561. (= l level)
  20562. (not invisible-ok)
  20563. (outline-invisible-p))
  20564. (if (< l level) (setq arg 1)))
  20565. (setq arg (1- arg)))))
  20566. (defun org-forward-element ()
  20567. "Move forward by one element.
  20568. Move to the next element at the same level, when possible."
  20569. (interactive)
  20570. (cond ((eobp) (error "Cannot move further down"))
  20571. ((org-with-limited-levels (org-at-heading-p))
  20572. (let ((origin (point)))
  20573. (org-forward-heading-same-level 1)
  20574. (unless (org-with-limited-levels (org-at-heading-p))
  20575. (goto-char origin)
  20576. (error "Cannot move further down"))))
  20577. (t
  20578. (let* ((elem (org-element-at-point))
  20579. (end (org-element-property :end elem))
  20580. (parent (org-element-property :parent elem)))
  20581. (if (and parent (= (org-element-property :contents-end parent) end))
  20582. (goto-char (org-element-property :end parent))
  20583. (goto-char end))))))
  20584. (defun org-backward-element ()
  20585. "Move backward by one element.
  20586. Move to the previous element at the same level, when possible."
  20587. (interactive)
  20588. (cond ((bobp) (error "Cannot move further up"))
  20589. ((org-with-limited-levels (org-at-heading-p))
  20590. ;; At an headline, move to the previous one, if any, or stay
  20591. ;; here.
  20592. (let ((origin (point)))
  20593. (org-backward-heading-same-level 1)
  20594. (unless (org-with-limited-levels (org-at-heading-p))
  20595. (goto-char origin)
  20596. (error "Cannot move further up"))))
  20597. (t
  20598. (let* ((trail (org-element-at-point 'keep-trail))
  20599. (elem (car trail))
  20600. (prev-elem (nth 1 trail))
  20601. (beg (org-element-property :begin elem)))
  20602. (cond
  20603. ;; Move to beginning of current element if point isn't
  20604. ;; there already.
  20605. ((/= (point) beg) (goto-char beg))
  20606. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20607. ((org-before-first-heading-p) (goto-char (point-min)))
  20608. (t (org-back-to-heading)))))))
  20609. (defun org-up-element ()
  20610. "Move to upper element."
  20611. (interactive)
  20612. (if (org-with-limited-levels (org-at-heading-p))
  20613. (unless (org-up-heading-safe) (error "No surrounding element"))
  20614. (let* ((elem (org-element-at-point))
  20615. (parent (org-element-property :parent elem)))
  20616. (if parent (goto-char (org-element-property :begin parent))
  20617. (if (org-with-limited-levels (org-before-first-heading-p))
  20618. (error "No surrounding element")
  20619. (org-with-limited-levels (org-back-to-heading)))))))
  20620. (defvar org-element-greater-elements)
  20621. (defun org-down-element ()
  20622. "Move to inner element."
  20623. (interactive)
  20624. (let ((element (org-element-at-point)))
  20625. (cond
  20626. ((memq (org-element-type element) '(plain-list table))
  20627. (goto-char (org-element-property :contents-begin element))
  20628. (forward-char))
  20629. ((memq (org-element-type element) org-element-greater-elements)
  20630. ;; If contents are hidden, first disclose them.
  20631. (when (org-element-property :hiddenp element) (org-cycle))
  20632. (goto-char (or (org-element-property :contents-begin element)
  20633. (error "No content for this element"))))
  20634. (t (error "No inner element")))))
  20635. (defun org-drag-element-backward ()
  20636. "Move backward element at point."
  20637. (interactive)
  20638. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20639. (let* ((trail (org-element-at-point 'keep-trail))
  20640. (elem (car trail))
  20641. (prev-elem (nth 1 trail)))
  20642. ;; Error out if no previous element or previous element is
  20643. ;; a parent of the current one.
  20644. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20645. (error "Cannot drag element backward")
  20646. (let ((pos (point)))
  20647. (org-element-swap-A-B prev-elem elem)
  20648. (goto-char (+ (org-element-property :begin prev-elem)
  20649. (- pos (org-element-property :begin elem)))))))))
  20650. (defun org-drag-element-forward ()
  20651. "Move forward element at point."
  20652. (interactive)
  20653. (let* ((pos (point))
  20654. (elem (org-element-at-point)))
  20655. (when (= (point-max) (org-element-property :end elem))
  20656. (error "Cannot drag element forward"))
  20657. (goto-char (org-element-property :end elem))
  20658. (let ((next-elem (org-element-at-point)))
  20659. (when (or (org-element-nested-p elem next-elem)
  20660. (and (eq (org-element-type next-elem) 'headline)
  20661. (not (eq (org-element-type elem) 'headline))))
  20662. (goto-char pos)
  20663. (error "Cannot drag element forward"))
  20664. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20665. ;; body's length (without final blanks) and by the length of
  20666. ;; blanks between ELEM and NEXT-ELEM.
  20667. (let ((size-next (- (save-excursion
  20668. (goto-char (org-element-property :end next-elem))
  20669. (skip-chars-backward " \r\t\n")
  20670. (forward-line)
  20671. ;; Small correction if buffer doesn't end
  20672. ;; with a newline character.
  20673. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20674. (org-element-property :begin next-elem)))
  20675. (size-blank (- (org-element-property :end elem)
  20676. (save-excursion
  20677. (goto-char (org-element-property :end elem))
  20678. (skip-chars-backward " \r\t\n")
  20679. (forward-line)
  20680. (point)))))
  20681. (org-element-swap-A-B elem next-elem)
  20682. (goto-char (+ pos size-next size-blank))))))
  20683. (defun org-mark-element ()
  20684. "Put point at beginning of this element, mark at end.
  20685. Interactively, if this command is repeated or (in Transient Mark
  20686. mode) if the mark is active, it marks the next element after the
  20687. ones already marked."
  20688. (interactive)
  20689. (let (deactivate-mark)
  20690. (if (and (org-called-interactively-p 'any)
  20691. (or (and (eq last-command this-command) (mark t))
  20692. (and transient-mark-mode mark-active)))
  20693. (set-mark
  20694. (save-excursion
  20695. (goto-char (mark))
  20696. (goto-char (org-element-property :end (org-element-at-point)))))
  20697. (let ((element (org-element-at-point)))
  20698. (end-of-line)
  20699. (push-mark (org-element-property :end element) t t)
  20700. (goto-char (org-element-property :begin element))))))
  20701. (defun org-narrow-to-element ()
  20702. "Narrow buffer to current element."
  20703. (interactive)
  20704. (let ((elem (org-element-at-point)))
  20705. (cond
  20706. ((eq (car elem) 'headline)
  20707. (narrow-to-region
  20708. (org-element-property :begin elem)
  20709. (org-element-property :end elem)))
  20710. ((memq (car elem) org-element-greater-elements)
  20711. (narrow-to-region
  20712. (org-element-property :contents-begin elem)
  20713. (org-element-property :contents-end elem)))
  20714. (t
  20715. (narrow-to-region
  20716. (org-element-property :begin elem)
  20717. (org-element-property :end elem))))))
  20718. (defun org-transpose-words ()
  20719. "Transpose words, using `org-mode' syntax table."
  20720. (interactive)
  20721. (with-syntax-table org-syntax-table
  20722. (call-interactively 'transpose-words)))
  20723. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  20724. (defun org-transpose-element ()
  20725. "Transpose current and previous elements, keeping blank lines between.
  20726. Point is moved after both elements."
  20727. (interactive)
  20728. (org-skip-whitespace)
  20729. (let ((end (org-element-property :end (org-element-at-point))))
  20730. (org-drag-element-backward)
  20731. (goto-char end)))
  20732. (defun org-unindent-buffer ()
  20733. "Un-indent the visible part of the buffer.
  20734. Relative indentation (between items, inside blocks, etc.) isn't
  20735. modified."
  20736. (interactive)
  20737. (unless (eq major-mode 'org-mode)
  20738. (error "Cannot un-indent a buffer not in Org mode"))
  20739. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20740. unindent-tree ; For byte-compiler.
  20741. (unindent-tree
  20742. (function
  20743. (lambda (contents)
  20744. (mapc
  20745. (lambda (element)
  20746. (if (memq (org-element-type element) '(headline section))
  20747. (funcall unindent-tree (org-element-contents element))
  20748. (save-excursion
  20749. (save-restriction
  20750. (narrow-to-region
  20751. (org-element-property :begin element)
  20752. (org-element-property :end element))
  20753. (org-do-remove-indentation)))))
  20754. (reverse contents))))))
  20755. (funcall unindent-tree (org-element-contents parse-tree))))
  20756. (defun org-show-subtree ()
  20757. "Show everything after this heading at deeper levels."
  20758. (interactive)
  20759. (outline-flag-region
  20760. (point)
  20761. (save-excursion
  20762. (org-end-of-subtree t t))
  20763. nil))
  20764. (defun org-show-entry ()
  20765. "Show the body directly following this heading.
  20766. Show the heading too, if it is currently invisible."
  20767. (interactive)
  20768. (save-excursion
  20769. (condition-case nil
  20770. (progn
  20771. (org-back-to-heading t)
  20772. (outline-flag-region
  20773. (max (point-min) (1- (point)))
  20774. (save-excursion
  20775. (if (re-search-forward
  20776. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20777. (match-beginning 1)
  20778. (point-max)))
  20779. nil)
  20780. (org-cycle-hide-drawers 'children))
  20781. (error nil))))
  20782. (defun org-make-options-regexp (kwds &optional extra)
  20783. "Make a regular expression for keyword lines."
  20784. (concat
  20785. "^#\\+\\("
  20786. (mapconcat 'regexp-quote kwds "\\|")
  20787. (if extra (concat "\\|" extra))
  20788. "\\):[ \t]*\\(.*\\)"))
  20789. ;; Make isearch reveal the necessary context
  20790. (defun org-isearch-end ()
  20791. "Reveal context after isearch exits."
  20792. (when isearch-success ; only if search was successful
  20793. (if (featurep 'xemacs)
  20794. ;; Under XEmacs, the hook is run in the correct place,
  20795. ;; we directly show the context.
  20796. (org-show-context 'isearch)
  20797. ;; In Emacs the hook runs *before* restoring the overlays.
  20798. ;; So we have to use a one-time post-command-hook to do this.
  20799. ;; (Emacs 22 has a special variable, see function `org-mode')
  20800. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20801. isearch-mode-end-hook-quit)
  20802. ;; Only when the isearch was not quitted.
  20803. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20804. 'append 'local)))))
  20805. (defun org-isearch-post-command ()
  20806. "Remove self from hook, and show context."
  20807. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20808. (org-show-context 'isearch))
  20809. ;;;; Integration with and fixes for other packages
  20810. ;;; Imenu support
  20811. (defvar org-imenu-markers nil
  20812. "All markers currently used by Imenu.")
  20813. (make-variable-buffer-local 'org-imenu-markers)
  20814. (defun org-imenu-new-marker (&optional pos)
  20815. "Return a new marker for use by Imenu, and remember the marker."
  20816. (let ((m (make-marker)))
  20817. (move-marker m (or pos (point)))
  20818. (push m org-imenu-markers)
  20819. m))
  20820. (defun org-imenu-get-tree ()
  20821. "Produce the index for Imenu."
  20822. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20823. (setq org-imenu-markers nil)
  20824. (let* ((n org-imenu-depth)
  20825. (re (concat "^" (org-get-limited-outline-regexp)))
  20826. (subs (make-vector (1+ n) nil))
  20827. (last-level 0)
  20828. m level head)
  20829. (save-excursion
  20830. (save-restriction
  20831. (widen)
  20832. (goto-char (point-max))
  20833. (while (re-search-backward re nil t)
  20834. (setq level (org-reduced-level (funcall outline-level)))
  20835. (when (and (<= level n)
  20836. (looking-at org-complex-heading-regexp))
  20837. (setq head (org-link-display-format
  20838. (org-match-string-no-properties 4))
  20839. m (org-imenu-new-marker))
  20840. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20841. (if (>= level last-level)
  20842. (push (cons head m) (aref subs level))
  20843. (push (cons head (aref subs (1+ level))) (aref subs level))
  20844. (loop for i from (1+ level) to n do (aset subs i nil)))
  20845. (setq last-level level)))))
  20846. (aref subs 1)))
  20847. (eval-after-load "imenu"
  20848. '(progn
  20849. (add-hook 'imenu-after-jump-hook
  20850. (lambda ()
  20851. (if (derived-mode-p 'org-mode)
  20852. (org-show-context 'org-goto))))))
  20853. (defun org-link-display-format (link)
  20854. "Replace a link with either the description, or the link target
  20855. if no description is present"
  20856. (save-match-data
  20857. (if (string-match org-bracket-link-analytic-regexp link)
  20858. (replace-match (if (match-end 5)
  20859. (match-string 5 link)
  20860. (concat (match-string 1 link)
  20861. (match-string 3 link)))
  20862. nil t link)
  20863. link)))
  20864. (defun org-toggle-link-display ()
  20865. "Toggle the literal or descriptive display of links."
  20866. (interactive)
  20867. (if org-descriptive-links
  20868. (progn (org-remove-from-invisibility-spec '(org-link))
  20869. (org-restart-font-lock)
  20870. (setq org-descriptive-links nil))
  20871. (progn (add-to-invisibility-spec '(org-link))
  20872. (org-restart-font-lock)
  20873. (setq org-descriptive-links t))))
  20874. ;; Speedbar support
  20875. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20876. "Overlay marking the agenda restriction line in speedbar.")
  20877. (overlay-put org-speedbar-restriction-lock-overlay
  20878. 'face 'org-agenda-restriction-lock)
  20879. (overlay-put org-speedbar-restriction-lock-overlay
  20880. 'help-echo "Agendas are currently limited to this item.")
  20881. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20882. (defun org-speedbar-set-agenda-restriction ()
  20883. "Restrict future agenda commands to the location at point in speedbar.
  20884. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20885. (interactive)
  20886. (require 'org-agenda)
  20887. (let (p m tp np dir txt)
  20888. (cond
  20889. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20890. 'org-imenu t))
  20891. (setq m (get-text-property p 'org-imenu-marker))
  20892. (with-current-buffer (marker-buffer m)
  20893. (goto-char m)
  20894. (org-agenda-set-restriction-lock 'subtree)))
  20895. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20896. 'speedbar-function 'speedbar-find-file))
  20897. (setq tp (previous-single-property-change
  20898. (1+ p) 'speedbar-function)
  20899. np (next-single-property-change
  20900. tp 'speedbar-function)
  20901. dir (speedbar-line-directory)
  20902. txt (buffer-substring-no-properties (or tp (point-min))
  20903. (or np (point-max))))
  20904. (with-current-buffer (find-file-noselect
  20905. (let ((default-directory dir))
  20906. (expand-file-name txt)))
  20907. (unless (derived-mode-p 'org-mode)
  20908. (error "Cannot restrict to non-Org-mode file"))
  20909. (org-agenda-set-restriction-lock 'file)))
  20910. (t (error "Don't know how to restrict Org-mode's agenda")))
  20911. (move-overlay org-speedbar-restriction-lock-overlay
  20912. (point-at-bol) (point-at-eol))
  20913. (setq current-prefix-arg nil)
  20914. (org-agenda-maybe-redo)))
  20915. (eval-after-load "speedbar"
  20916. '(progn
  20917. (speedbar-add-supported-extension ".org")
  20918. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20919. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20920. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20921. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20922. (add-hook 'speedbar-visiting-tag-hook
  20923. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20924. ;;; Fixes and Hacks for problems with other packages
  20925. ;; Make flyspell not check words in links, to not mess up our keymap
  20926. (defun org-mode-flyspell-verify ()
  20927. "Don't let flyspell put overlays at active buttons, or on
  20928. {todo,all-time,additional-option-like}-keywords."
  20929. (let ((pos (max (1- (point)) (point-min)))
  20930. (word (thing-at-point 'word)))
  20931. (and (not (get-text-property pos 'keymap))
  20932. (not (get-text-property pos 'org-no-flyspell))
  20933. (not (member word org-todo-keywords-1))
  20934. (not (member word org-all-time-keywords))
  20935. (not (member word org-options-keywords))
  20936. (not (member word (mapcar 'car org-startup-options)))
  20937. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20938. (defun org-remove-flyspell-overlays-in (beg end)
  20939. "Remove flyspell overlays in region."
  20940. (and (org-bound-and-true-p flyspell-mode)
  20941. (fboundp 'flyspell-delete-region-overlays)
  20942. (flyspell-delete-region-overlays beg end))
  20943. (add-text-properties beg end '(org-no-flyspell t)))
  20944. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20945. (eval-after-load "bookmark"
  20946. '(if (boundp 'bookmark-after-jump-hook)
  20947. ;; We can use the hook
  20948. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20949. ;; Hook not available, use advice
  20950. (defadvice bookmark-jump (after org-make-visible activate)
  20951. "Make the position visible."
  20952. (org-bookmark-jump-unhide))))
  20953. ;; Make sure saveplace shows the location if it was hidden
  20954. (eval-after-load "saveplace"
  20955. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20956. "Make the position visible."
  20957. (org-bookmark-jump-unhide)))
  20958. ;; Make sure ecb shows the location if it was hidden
  20959. (eval-after-load "ecb"
  20960. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20961. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20962. (if (derived-mode-p 'org-mode)
  20963. (org-show-context))))
  20964. (defun org-bookmark-jump-unhide ()
  20965. "Unhide the current position, to show the bookmark location."
  20966. (and (derived-mode-p 'org-mode)
  20967. (or (outline-invisible-p)
  20968. (save-excursion (goto-char (max (point-min) (1- (point))))
  20969. (outline-invisible-p)))
  20970. (org-show-context 'bookmark-jump)))
  20971. ;; Make session.el ignore our circular variable
  20972. (eval-after-load "session"
  20973. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20974. ;;;; Experimental code
  20975. (defun org-closed-in-range ()
  20976. "Sparse tree of items closed in a certain time range.
  20977. Still experimental, may disappear in the future."
  20978. (interactive)
  20979. ;; Get the time interval from the user.
  20980. (let* ((time1 (org-float-time
  20981. (org-read-date nil 'to-time nil "Starting date: ")))
  20982. (time2 (org-float-time
  20983. (org-read-date nil 'to-time nil "End date:")))
  20984. ;; callback function
  20985. (callback (lambda ()
  20986. (let ((time
  20987. (org-float-time
  20988. (apply 'encode-time
  20989. (org-parse-time-string
  20990. (match-string 1))))))
  20991. ;; check if time in interval
  20992. (and (>= time time1) (<= time time2))))))
  20993. ;; make tree, check each match with the callback
  20994. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20995. ;;;; Finish up
  20996. (provide 'org)
  20997. (run-hooks 'org-load-hook)
  20998. ;;; org.el ends here