org.el 757 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.5
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. ;; babel
  100. (require 'ob)
  101. (require 'ob-table)
  102. (require 'ob-lob)
  103. (require 'ob-ref)
  104. (require 'ob-tangle)
  105. (require 'ob-comint)
  106. (require 'ob-keys)
  107. ;; load languages based on value of `org-babel-load-languages'
  108. (defvar org-babel-load-languages)
  109. ;;;###autoload
  110. (defun org-babel-do-load-languages (sym value)
  111. "Load the languages defined in `org-babel-load-languages'."
  112. (set-default sym value)
  113. (mapc (lambda (pair)
  114. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  115. (if active
  116. (progn
  117. (require (intern (concat "ob-" lang))))
  118. (progn
  119. (funcall 'fmakunbound
  120. (intern (concat "org-babel-execute:" lang)))
  121. (funcall 'fmakunbound
  122. (intern (concat "org-babel-expand-body:" lang)))))))
  123. org-babel-load-languages))
  124. (defcustom org-babel-load-languages '((emacs-lisp . t))
  125. "Languages which can be evaluated in Org-mode buffers.
  126. This list can be used to load support for any of the languages
  127. below, note that each language will depend on a different set of
  128. system executables and/or Emacs modes. When a language is
  129. \"loaded\", then code blocks in that language can be evaluated
  130. with `org-babel-execute-src-block' bound by default to C-c
  131. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  132. be set to remove code block evaluation from the C-c C-c
  133. keybinding. By default only Emacs Lisp (which has no
  134. requirements) is loaded."
  135. :group 'org-babel
  136. :set 'org-babel-do-load-languages
  137. :type '(alist :tag "Babel Languages"
  138. :key-type
  139. (choice
  140. (const :tag "Awk" awk)
  141. (const :tag "C" C)
  142. (const :tag "R" R)
  143. (const :tag "Asymptote" asymptote)
  144. (const :tag "Calc" calc)
  145. (const :tag "Clojure" clojure)
  146. (const :tag "CSS" css)
  147. (const :tag "Ditaa" ditaa)
  148. (const :tag "Dot" dot)
  149. (const :tag "Emacs Lisp" emacs-lisp)
  150. (const :tag "Gnuplot" gnuplot)
  151. (const :tag "Haskell" haskell)
  152. (const :tag "Javascript" js)
  153. (const :tag "Latex" latex)
  154. (const :tag "Ledger" ledger)
  155. (const :tag "Maxima" maxima)
  156. (const :tag "Matlab" matlab)
  157. (const :tag "Mscgen" mscgen)
  158. (const :tag "Ocaml" ocaml)
  159. (const :tag "Octave" octave)
  160. (const :tag "Org" org)
  161. (const :tag "Perl" perl)
  162. (const :tag "PlantUML" plantuml)
  163. (const :tag "Python" python)
  164. (const :tag "Ruby" ruby)
  165. (const :tag "Sass" sass)
  166. (const :tag "Scheme" scheme)
  167. (const :tag "Screen" screen)
  168. (const :tag "Shell Script" sh)
  169. (const :tag "Sql" sql)
  170. (const :tag "Sqlite" sqlite))
  171. :value-type (boolean :tag "Activate" :value t)))
  172. ;;;; Customization variables
  173. (defcustom org-clone-delete-id nil
  174. "Remove ID property of clones of a subtree.
  175. When non-nil, clones of a subtree don't inherit the ID property.
  176. Otherwise they inherit the ID property with a new unique
  177. identifier."
  178. :type 'boolean
  179. :group 'org-id)
  180. ;;; Version
  181. (defconst org-version "7.5"
  182. "The version number of the file org.el.")
  183. (defun org-version (&optional here)
  184. "Show the org-mode version in the echo area.
  185. With prefix arg HERE, insert it at point."
  186. (interactive "P")
  187. (let* ((origin default-directory)
  188. (version org-version)
  189. (git-version)
  190. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  191. (when (and (file-exists-p (expand-file-name ".git" dir))
  192. (executable-find "git"))
  193. (unwind-protect
  194. (progn
  195. (cd dir)
  196. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  197. (with-current-buffer "*Shell Command Output*"
  198. (goto-char (point-min))
  199. (setq git-version (buffer-substring (point) (point-at-eol))))
  200. (subst-char-in-string ?- ?. git-version t)
  201. (when (string-match "\\S-"
  202. (shell-command-to-string
  203. "git diff-index --name-only HEAD --"))
  204. (setq git-version (concat git-version ".dirty")))
  205. (setq version (concat version " (" git-version ")"))))
  206. (cd origin)))
  207. (setq version (format "Org-mode version %s" version))
  208. (if here (insert version))
  209. (message version)))
  210. ;;; Compatibility constants
  211. ;;; The custom variables
  212. (defgroup org nil
  213. "Outline-based notes management and organizer."
  214. :tag "Org"
  215. :group 'outlines
  216. :group 'calendar)
  217. (defcustom org-mode-hook nil
  218. "Mode hook for Org-mode, run after the mode was turned on."
  219. :group 'org
  220. :type 'hook)
  221. (defcustom org-load-hook nil
  222. "Hook that is run after org.el has been loaded."
  223. :group 'org
  224. :type 'hook)
  225. (defvar org-modules) ; defined below
  226. (defvar org-modules-loaded nil
  227. "Have the modules been loaded already?")
  228. (defun org-load-modules-maybe (&optional force)
  229. "Load all extensions listed in `org-modules'."
  230. (when (or force (not org-modules-loaded))
  231. (mapc (lambda (ext)
  232. (condition-case nil (require ext)
  233. (error (message "Problems while trying to load feature `%s'" ext))))
  234. org-modules)
  235. (setq org-modules-loaded t)))
  236. (defun org-set-modules (var value)
  237. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  238. (set var value)
  239. (when (featurep 'org)
  240. (org-load-modules-maybe 'force)))
  241. (when (org-bound-and-true-p org-modules)
  242. (let ((a (member 'org-infojs org-modules)))
  243. (and a (setcar a 'org-jsinfo))))
  244. (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)
  245. "Modules that should always be loaded together with org.el.
  246. If a description starts with <C>, the file is not part of Emacs
  247. and loading it will require that you have downloaded and properly installed
  248. the org-mode distribution.
  249. You can also use this system to load external packages (i.e. neither Org
  250. core modules, nor modules from the CONTRIB directory). Just add symbols
  251. to the end of the list. If the package is called org-xyz.el, then you need
  252. to add the symbol `xyz', and the package must have a call to
  253. (provide 'org-xyz)"
  254. :group 'org
  255. :set 'org-set-modules
  256. :type
  257. '(set :greedy t
  258. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  259. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  260. (const :tag " crypt: Encryption of subtrees" org-crypt)
  261. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  262. (const :tag " docview: Links to doc-view buffers" org-docview)
  263. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  264. (const :tag " id: Global IDs for identifying entries" org-id)
  265. (const :tag " info: Links to Info nodes" org-info)
  266. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  267. (const :tag " habit: Track your consistency with habits" org-habit)
  268. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  269. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  270. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  271. (const :tag " mew Links to Mew folders/messages" org-mew)
  272. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  273. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  274. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  275. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  276. (const :tag " vm: Links to VM folders/messages" org-vm)
  277. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  278. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  279. (const :tag " mouse: Additional mouse support" org-mouse)
  280. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  281. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  282. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  283. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  284. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  285. (const :tag "C collector: Collect properties into tables" org-collector)
  286. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  287. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  288. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  289. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  290. (const :tag "C eval: Include command output as text" org-eval)
  291. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  292. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  293. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  294. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  295. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  296. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  297. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  298. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  299. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  300. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  301. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  302. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  303. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  304. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  305. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  306. (const :tag "C registry: A registry for Org-mode links" org-registry)
  307. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  308. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  309. (const :tag "C secretary: Team management with org-mode" org-secretary)
  310. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  311. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  312. (const :tag "C track: Keep up with Org-mode development" org-track)
  313. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  314. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  315. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  316. (defcustom org-support-shift-select nil
  317. "Non-nil means make shift-cursor commands select text when possible.
  318. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  319. selecting a region, or enlarge regions started in this way.
  320. In Org-mode, in special contexts, these same keys are used for other
  321. purposes, important enough to compete with shift selection. Org tries
  322. to balance these needs by supporting `shift-select-mode' outside these
  323. special contexts, under control of this variable.
  324. The default of this variable is nil, to avoid confusing behavior. Shifted
  325. cursor keys will then execute Org commands in the following contexts:
  326. - on a headline, changing TODO state (left/right) and priority (up/down)
  327. - on a time stamp, changing the time
  328. - in a plain list item, changing the bullet type
  329. - in a property definition line, switching between allowed values
  330. - in the BEGIN line of a clock table (changing the time block).
  331. Outside these contexts, the commands will throw an error.
  332. When this variable is t and the cursor is not in a special context,
  333. Org-mode will support shift-selection for making and enlarging regions.
  334. To make this more effective, the bullet cycling will no longer happen
  335. anywhere in an item line, but only if the cursor is exactly on the bullet.
  336. If you set this variable to the symbol `always', then the keys
  337. will not be special in headlines, property lines, and item lines, to make
  338. shift selection work there as well. If this is what you want, you can
  339. use the following alternative commands: `C-c C-t' and `C-c ,' to
  340. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  341. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  342. edited by hand or in column view.
  343. However, when the cursor is on a timestamp, shift-cursor commands
  344. will still edit the time stamp - this is just too good to give up.
  345. XEmacs user should have this variable set to nil, because shift-select-mode
  346. is Emacs 23 only."
  347. :group 'org
  348. :type '(choice
  349. (const :tag "Never" nil)
  350. (const :tag "When outside special context" t)
  351. (const :tag "Everywhere except timestamps" always)))
  352. (defgroup org-startup nil
  353. "Options concerning startup of Org-mode."
  354. :tag "Org Startup"
  355. :group 'org)
  356. (defcustom org-startup-folded t
  357. "Non-nil means entering Org-mode will switch to OVERVIEW.
  358. This can also be configured on a per-file basis by adding one of
  359. the following lines anywhere in the buffer:
  360. #+STARTUP: fold (or `overview', this is equivalent)
  361. #+STARTUP: nofold (or `showall', this is equivalent)
  362. #+STARTUP: content
  363. #+STARTUP: showeverything"
  364. :group 'org-startup
  365. :type '(choice
  366. (const :tag "nofold: show all" nil)
  367. (const :tag "fold: overview" t)
  368. (const :tag "content: all headlines" content)
  369. (const :tag "show everything, even drawers" showeverything)))
  370. (defcustom org-startup-truncated t
  371. "Non-nil means entering Org-mode will set `truncate-lines'.
  372. This is useful since some lines containing links can be very long and
  373. uninteresting. Also tables look terrible when wrapped."
  374. :group 'org-startup
  375. :type 'boolean)
  376. (defcustom org-startup-indented nil
  377. "Non-nil means turn on `org-indent-mode' on startup.
  378. This can also be configured on a per-file basis by adding one of
  379. the following lines anywhere in the buffer:
  380. #+STARTUP: indent
  381. #+STARTUP: noindent"
  382. :group 'org-structure
  383. :type '(choice
  384. (const :tag "Not" nil)
  385. (const :tag "Globally (slow on startup in large files)" t)))
  386. (defcustom org-use-sub-superscripts t
  387. "Non-nil means interpret \"_\" and \"^\" for export.
  388. When this option is turned on, you can use TeX-like syntax for sub- and
  389. superscripts. Several characters after \"_\" or \"^\" will be
  390. considered as a single item - so grouping with {} is normally not
  391. needed. For example, the following things will be parsed as single
  392. sub- or superscripts.
  393. 10^24 or 10^tau several digits will be considered 1 item.
  394. 10^-12 or 10^-tau a leading sign with digits or a word
  395. x^2-y^3 will be read as x^2 - y^3, because items are
  396. terminated by almost any nonword/nondigit char.
  397. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  398. Still, ambiguity is possible - so when in doubt use {} to enclose the
  399. sub/superscript. If you set this variable to the symbol `{}',
  400. the braces are *required* in order to trigger interpretations as
  401. sub/superscript. This can be helpful in documents that need \"_\"
  402. frequently in plain text.
  403. Not all export backends support this, but HTML does.
  404. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  405. :group 'org-startup
  406. :group 'org-export-translation
  407. :type '(choice
  408. (const :tag "Always interpret" t)
  409. (const :tag "Only with braces" {})
  410. (const :tag "Never interpret" nil)))
  411. (if (fboundp 'defvaralias)
  412. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  413. (defcustom org-startup-with-beamer-mode nil
  414. "Non-nil means turn on `org-beamer-mode' on startup.
  415. This can also be configured on a per-file basis by adding one of
  416. the following lines anywhere in the buffer:
  417. #+STARTUP: beamer"
  418. :group 'org-startup
  419. :type 'boolean)
  420. (defcustom org-startup-align-all-tables nil
  421. "Non-nil means align all tables when visiting a file.
  422. This is useful when the column width in tables is forced with <N> cookies
  423. in table fields. Such tables will look correct only after the first re-align.
  424. This can also be configured on a per-file basis by adding one of
  425. the following lines anywhere in the buffer:
  426. #+STARTUP: align
  427. #+STARTUP: noalign"
  428. :group 'org-startup
  429. :type 'boolean)
  430. (defcustom org-startup-with-inline-images nil
  431. "Non-nil means show inline images when loading a new Org file.
  432. This can also be configured on a per-file basis by adding one of
  433. the following lines anywhere in the buffer:
  434. #+STARTUP: inlineimages
  435. #+STARTUP: noinlineimages"
  436. :group 'org-startup
  437. :type 'boolean)
  438. (defcustom org-insert-mode-line-in-empty-file nil
  439. "Non-nil means insert the first line setting Org-mode in empty files.
  440. When the function `org-mode' is called interactively in an empty file, this
  441. normally means that the file name does not automatically trigger Org-mode.
  442. To ensure that the file will always be in Org-mode in the future, a
  443. line enforcing Org-mode will be inserted into the buffer, if this option
  444. has been set."
  445. :group 'org-startup
  446. :type 'boolean)
  447. (defcustom org-replace-disputed-keys nil
  448. "Non-nil means use alternative key bindings for some keys.
  449. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  450. These keys are also used by other packages like shift-selection-mode'
  451. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  452. If you want to use Org-mode together with one of these other modes,
  453. or more generally if you would like to move some Org-mode commands to
  454. other keys, set this variable and configure the keys with the variable
  455. `org-disputed-keys'.
  456. This option is only relevant at load-time of Org-mode, and must be set
  457. *before* org.el is loaded. Changing it requires a restart of Emacs to
  458. become effective."
  459. :group 'org-startup
  460. :type 'boolean)
  461. (defcustom org-use-extra-keys nil
  462. "Non-nil means use extra key sequence definitions for certain commands.
  463. This happens automatically if you run XEmacs or if `window-system'
  464. is nil. This variable lets you do the same manually. You must
  465. set it before loading org.
  466. Example: on Carbon Emacs 22 running graphically, with an external
  467. keyboard on a Powerbook, the default way of setting M-left might
  468. not work for either Alt or ESC. Setting this variable will make
  469. it work for ESC."
  470. :group 'org-startup
  471. :type 'boolean)
  472. (if (fboundp 'defvaralias)
  473. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  474. (defcustom org-disputed-keys
  475. '(([(shift up)] . [(meta p)])
  476. ([(shift down)] . [(meta n)])
  477. ([(shift left)] . [(meta -)])
  478. ([(shift right)] . [(meta +)])
  479. ([(control shift right)] . [(meta shift +)])
  480. ([(control shift left)] . [(meta shift -)]))
  481. "Keys for which Org-mode and other modes compete.
  482. This is an alist, cars are the default keys, second element specifies
  483. the alternative to use when `org-replace-disputed-keys' is t.
  484. Keys can be specified in any syntax supported by `define-key'.
  485. The value of this option takes effect only at Org-mode's startup,
  486. therefore you'll have to restart Emacs to apply it after changing."
  487. :group 'org-startup
  488. :type 'alist)
  489. (defun org-key (key)
  490. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  491. Or return the original if not disputed.
  492. Also apply the translations defined in `org-xemacs-key-equivalents'."
  493. (when org-replace-disputed-keys
  494. (let* ((nkey (key-description key))
  495. (x (org-find-if (lambda (x)
  496. (equal (key-description (car x)) nkey))
  497. org-disputed-keys)))
  498. (setq key (if x (cdr x) key))))
  499. (when (featurep 'xemacs)
  500. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  501. key)
  502. (defun org-find-if (predicate seq)
  503. (catch 'exit
  504. (while seq
  505. (if (funcall predicate (car seq))
  506. (throw 'exit (car seq))
  507. (pop seq)))))
  508. (defun org-defkey (keymap key def)
  509. "Define a key, possibly translated, as returned by `org-key'."
  510. (define-key keymap (org-key key) def))
  511. (defcustom org-ellipsis nil
  512. "The ellipsis to use in the Org-mode outline.
  513. When nil, just use the standard three dots. When a string, use that instead,
  514. When a face, use the standard 3 dots, but with the specified face.
  515. The change affects only Org-mode (which will then use its own display table).
  516. Changing this requires executing `M-x org-mode' in a buffer to become
  517. effective."
  518. :group 'org-startup
  519. :type '(choice (const :tag "Default" nil)
  520. (face :tag "Face" :value org-warning)
  521. (string :tag "String" :value "...#")))
  522. (defvar org-display-table nil
  523. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  524. (defgroup org-keywords nil
  525. "Keywords in Org-mode."
  526. :tag "Org Keywords"
  527. :group 'org)
  528. (defcustom org-deadline-string "DEADLINE:"
  529. "String to mark deadline entries.
  530. A deadline is this string, followed by a time stamp. Should be a word,
  531. terminated by a colon. You can insert a schedule keyword and
  532. a timestamp with \\[org-deadline].
  533. Changes become only effective after restarting Emacs."
  534. :group 'org-keywords
  535. :type 'string)
  536. (defcustom org-scheduled-string "SCHEDULED:"
  537. "String to mark scheduled TODO entries.
  538. A schedule is this string, followed by a time stamp. Should be a word,
  539. terminated by a colon. You can insert a schedule keyword and
  540. a timestamp with \\[org-schedule].
  541. Changes become only effective after restarting Emacs."
  542. :group 'org-keywords
  543. :type 'string)
  544. (defcustom org-closed-string "CLOSED:"
  545. "String used as the prefix for timestamps logging closing a TODO entry."
  546. :group 'org-keywords
  547. :type 'string)
  548. (defcustom org-clock-string "CLOCK:"
  549. "String used as prefix for timestamps clocking work hours on an item."
  550. :group 'org-keywords
  551. :type 'string)
  552. (defcustom org-comment-string "COMMENT"
  553. "Entries starting with this keyword will never be exported.
  554. An entry can be toggled between COMMENT and normal with
  555. \\[org-toggle-comment].
  556. Changes become only effective after restarting Emacs."
  557. :group 'org-keywords
  558. :type 'string)
  559. (defcustom org-quote-string "QUOTE"
  560. "Entries starting with this keyword will be exported in fixed-width font.
  561. Quoting applies only to the text in the entry following the headline, and does
  562. not extend beyond the next headline, even if that is lower level.
  563. An entry can be toggled between QUOTE and normal with
  564. \\[org-toggle-fixed-width-section]."
  565. :group 'org-keywords
  566. :type 'string)
  567. (defconst org-repeat-re
  568. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  569. "Regular expression for specifying repeated events.
  570. After a match, group 1 contains the repeat expression.")
  571. (defgroup org-structure nil
  572. "Options concerning the general structure of Org-mode files."
  573. :tag "Org Structure"
  574. :group 'org)
  575. (defgroup org-reveal-location nil
  576. "Options about how to make context of a location visible."
  577. :tag "Org Reveal Location"
  578. :group 'org-structure)
  579. (defconst org-context-choice
  580. '(choice
  581. (const :tag "Always" t)
  582. (const :tag "Never" nil)
  583. (repeat :greedy t :tag "Individual contexts"
  584. (cons
  585. (choice :tag "Context"
  586. (const agenda)
  587. (const org-goto)
  588. (const occur-tree)
  589. (const tags-tree)
  590. (const link-search)
  591. (const mark-goto)
  592. (const bookmark-jump)
  593. (const isearch)
  594. (const default))
  595. (boolean))))
  596. "Contexts for the reveal options.")
  597. (defcustom org-show-hierarchy-above '((default . t))
  598. "Non-nil means show full hierarchy when revealing a location.
  599. Org-mode often shows locations in an org-mode file which might have
  600. been invisible before. When this is set, the hierarchy of headings
  601. above the exposed location is shown.
  602. Turning this off for example for sparse trees makes them very compact.
  603. Instead of t, this can also be an alist specifying this option for different
  604. contexts. Valid contexts are
  605. agenda when exposing an entry from the agenda
  606. org-goto when using the command `org-goto' on key C-c C-j
  607. occur-tree when using the command `org-occur' on key C-c /
  608. tags-tree when constructing a sparse tree based on tags matches
  609. link-search when exposing search matches associated with a link
  610. mark-goto when exposing the jump goal of a mark
  611. bookmark-jump when exposing a bookmark location
  612. isearch when exiting from an incremental search
  613. default default for all contexts not set explicitly"
  614. :group 'org-reveal-location
  615. :type org-context-choice)
  616. (defcustom org-show-following-heading '((default . nil))
  617. "Non-nil means show following heading when revealing a location.
  618. Org-mode often shows locations in an org-mode file which might have
  619. been invisible before. When this is set, the heading following the
  620. match is shown.
  621. Turning this off for example for sparse trees makes them very compact,
  622. but makes it harder to edit the location of the match. In such a case,
  623. use the command \\[org-reveal] to show more context.
  624. Instead of t, this can also be an alist specifying this option for different
  625. contexts. See `org-show-hierarchy-above' for valid contexts."
  626. :group 'org-reveal-location
  627. :type org-context-choice)
  628. (defcustom org-show-siblings '((default . nil) (isearch t))
  629. "Non-nil means show all sibling heading when revealing a location.
  630. Org-mode often shows locations in an org-mode file which might have
  631. been invisible before. When this is set, the sibling of the current entry
  632. heading are all made visible. If `org-show-hierarchy-above' is t,
  633. the same happens on each level of the hierarchy above the current entry.
  634. By default this is on for the isearch context, off for all other contexts.
  635. Turning this off for example for sparse trees makes them very compact,
  636. but makes it harder to edit the location of the match. In such a case,
  637. use the command \\[org-reveal] to show more context.
  638. Instead of t, this can also be an alist specifying this option for different
  639. contexts. See `org-show-hierarchy-above' for valid contexts."
  640. :group 'org-reveal-location
  641. :type org-context-choice)
  642. (defcustom org-show-entry-below '((default . nil))
  643. "Non-nil means show the entry below a headline when revealing a location.
  644. Org-mode often shows locations in an org-mode file which might have
  645. been invisible before. When this is set, the text below the headline that is
  646. exposed is also shown.
  647. By default this is off for all contexts.
  648. Instead of t, this can also be an alist specifying this option for different
  649. contexts. See `org-show-hierarchy-above' for valid contexts."
  650. :group 'org-reveal-location
  651. :type org-context-choice)
  652. (defcustom org-indirect-buffer-display 'other-window
  653. "How should indirect tree buffers be displayed?
  654. This applies to indirect buffers created with the commands
  655. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  656. Valid values are:
  657. current-window Display in the current window
  658. other-window Just display in another window.
  659. dedicated-frame Create one new frame, and re-use it each time.
  660. new-frame Make a new frame each time. Note that in this case
  661. previously-made indirect buffers are kept, and you need to
  662. kill these buffers yourself."
  663. :group 'org-structure
  664. :group 'org-agenda-windows
  665. :type '(choice
  666. (const :tag "In current window" current-window)
  667. (const :tag "In current frame, other window" other-window)
  668. (const :tag "Each time a new frame" new-frame)
  669. (const :tag "One dedicated frame" dedicated-frame)))
  670. (defcustom org-use-speed-commands nil
  671. "Non-nil means activate single letter commands at beginning of a headline.
  672. This may also be a function to test for appropriate locations where speed
  673. commands should be active."
  674. :group 'org-structure
  675. :type '(choice
  676. (const :tag "Never" nil)
  677. (const :tag "At beginning of headline stars" t)
  678. (function)))
  679. (defcustom org-speed-commands-user nil
  680. "Alist of additional speed commands.
  681. This list will be checked before `org-speed-commands-default'
  682. when the variable `org-use-speed-commands' is non-nil
  683. and when the cursor is at the beginning of a headline.
  684. The car if each entry is a string with a single letter, which must
  685. be assigned to `self-insert-command' in the global map.
  686. The cdr is either a command to be called interactively, a function
  687. to be called, or a form to be evaluated.
  688. An entry that is just a list with a single string will be interpreted
  689. as a descriptive headline that will be added when listing the speed
  690. commands in the Help buffer using the `?' speed command."
  691. :group 'org-structure
  692. :type '(repeat :value ("k" . ignore)
  693. (choice :value ("k" . ignore)
  694. (list :tag "Descriptive Headline" (string :tag "Headline"))
  695. (cons :tag "Letter and Command"
  696. (string :tag "Command letter")
  697. (choice
  698. (function)
  699. (sexp))))))
  700. (defgroup org-cycle nil
  701. "Options concerning visibility cycling in Org-mode."
  702. :tag "Org Cycle"
  703. :group 'org-structure)
  704. (defcustom org-cycle-skip-children-state-if-no-children t
  705. "Non-nil means skip CHILDREN state in entries that don't have any."
  706. :group 'org-cycle
  707. :type 'boolean)
  708. (defcustom org-cycle-max-level nil
  709. "Maximum level which should still be subject to visibility cycling.
  710. Levels higher than this will, for cycling, be treated as text, not a headline.
  711. When `org-odd-levels-only' is set, a value of N in this variable actually
  712. means 2N-1 stars as the limiting headline.
  713. When nil, cycle all levels.
  714. Note that the limiting level of cycling is also influenced by
  715. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  716. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  717. than its value."
  718. :group 'org-cycle
  719. :type '(choice
  720. (const :tag "No limit" nil)
  721. (integer :tag "Maximum level")))
  722. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  723. "Names of drawers. Drawers are not opened by cycling on the headline above.
  724. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  725. this:
  726. :DRAWERNAME:
  727. .....
  728. :END:
  729. The drawer \"PROPERTIES\" is special for capturing properties through
  730. the property API.
  731. Drawers can be defined on the per-file basis with a line like:
  732. #+DRAWERS: HIDDEN STATE PROPERTIES"
  733. :group 'org-structure
  734. :group 'org-cycle
  735. :type '(repeat (string :tag "Drawer Name")))
  736. (defcustom org-hide-block-startup nil
  737. "Non-nil means entering Org-mode will fold all blocks.
  738. This can also be set in on a per-file basis with
  739. #+STARTUP: hideblocks
  740. #+STARTUP: showblocks"
  741. :group 'org-startup
  742. :group 'org-cycle
  743. :type 'boolean)
  744. (defcustom org-cycle-global-at-bob nil
  745. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  746. This makes it possible to do global cycling without having to use S-TAB or
  747. \\[universal-argument] TAB. For this special case to work, the first line \
  748. of the buffer
  749. must not be a headline - it may be empty or some other text. When used in
  750. this way, `org-cycle-hook' is disables temporarily, to make sure the
  751. cursor stays at the beginning of the buffer.
  752. When this option is nil, don't do anything special at the beginning
  753. of the buffer."
  754. :group 'org-cycle
  755. :type 'boolean)
  756. (defcustom org-cycle-level-after-item/entry-creation t
  757. "Non-nil means cycle entry level or item indentation in new empty entries.
  758. When the cursor is at the end of an empty headline, i.e with only stars
  759. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  760. and then all possible ancestor states, before returning to the original state.
  761. This makes data entry extremely fast: M-RET to create a new headline,
  762. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  763. When the cursor is at the end of an empty plain list item, one TAB will
  764. make it a subitem, two or more tabs will back up to make this an item
  765. higher up in the item hierarchy."
  766. :group 'org-cycle
  767. :type 'boolean)
  768. (defcustom org-cycle-emulate-tab t
  769. "Where should `org-cycle' emulate TAB.
  770. nil Never
  771. white Only in completely white lines
  772. whitestart Only at the beginning of lines, before the first non-white char
  773. t Everywhere except in headlines
  774. exc-hl-bol Everywhere except at the start of a headline
  775. If TAB is used in a place where it does not emulate TAB, the current subtree
  776. visibility is cycled."
  777. :group 'org-cycle
  778. :type '(choice (const :tag "Never" nil)
  779. (const :tag "Only in completely white lines" white)
  780. (const :tag "Before first char in a line" whitestart)
  781. (const :tag "Everywhere except in headlines" t)
  782. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  783. ))
  784. (defcustom org-cycle-separator-lines 2
  785. "Number of empty lines needed to keep an empty line between collapsed trees.
  786. If you leave an empty line between the end of a subtree and the following
  787. headline, this empty line is hidden when the subtree is folded.
  788. Org-mode will leave (exactly) one empty line visible if the number of
  789. empty lines is equal or larger to the number given in this variable.
  790. So the default 2 means at least 2 empty lines after the end of a subtree
  791. are needed to produce free space between a collapsed subtree and the
  792. following headline.
  793. If the number is negative, and the number of empty lines is at least -N,
  794. all empty lines are shown.
  795. Special case: when 0, never leave empty lines in collapsed view."
  796. :group 'org-cycle
  797. :type 'integer)
  798. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  799. (defcustom org-pre-cycle-hook nil
  800. "Hook that is run before visibility cycling is happening.
  801. The function(s) in this hook must accept a single argument which indicates
  802. the new state that will be set right after running this hook. The
  803. argument is a symbol. Before a global state change, it can have the values
  804. `overview', `content', or `all'. Before a local state change, it can have
  805. the values `folded', `children', or `subtree'."
  806. :group 'org-cycle
  807. :type 'hook)
  808. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  809. org-cycle-hide-drawers
  810. org-cycle-show-empty-lines
  811. org-optimize-window-after-visibility-change)
  812. "Hook that is run after `org-cycle' has changed the buffer visibility.
  813. The function(s) in this hook must accept a single argument which indicates
  814. the new state that was set by the most recent `org-cycle' command. The
  815. argument is a symbol. After a global state change, it can have the values
  816. `overview', `content', or `all'. After a local state change, it can have
  817. the values `folded', `children', or `subtree'."
  818. :group 'org-cycle
  819. :type 'hook)
  820. (defgroup org-edit-structure nil
  821. "Options concerning structure editing in Org-mode."
  822. :tag "Org Edit Structure"
  823. :group 'org-structure)
  824. (defcustom org-odd-levels-only nil
  825. "Non-nil means skip even levels and only use odd levels for the outline.
  826. This has the effect that two stars are being added/taken away in
  827. promotion/demotion commands. It also influences how levels are
  828. handled by the exporters.
  829. Changing it requires restart of `font-lock-mode' to become effective
  830. for fontification also in regions already fontified.
  831. You may also set this on a per-file basis by adding one of the following
  832. lines to the buffer:
  833. #+STARTUP: odd
  834. #+STARTUP: oddeven"
  835. :group 'org-edit-structure
  836. :group 'org-appearance
  837. :type 'boolean)
  838. (defcustom org-adapt-indentation t
  839. "Non-nil means adapt indentation to outline node level.
  840. When this variable is set, Org assumes that you write outlines by
  841. indenting text in each node to align with the headline (after the stars).
  842. The following issues are influenced by this variable:
  843. - When this is set and the *entire* text in an entry is indented, the
  844. indentation is increased by one space in a demotion command, and
  845. decreased by one in a promotion command. If any line in the entry
  846. body starts with text at column 0, indentation is not changed at all.
  847. - Property drawers and planning information is inserted indented when
  848. this variable s set. When nil, they will not be indented.
  849. - TAB indents a line relative to context. The lines below a headline
  850. will be indented when this variable is set.
  851. Note that this is all about true indentation, by adding and removing
  852. space characters. See also `org-indent.el' which does level-dependent
  853. indentation in a virtual way, i.e. at display time in Emacs."
  854. :group 'org-edit-structure
  855. :type 'boolean)
  856. (defcustom org-special-ctrl-a/e nil
  857. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  858. When t, `C-a' will bring back the cursor to the beginning of the
  859. headline text, i.e. after the stars and after a possible TODO keyword.
  860. In an item, this will be the position after the bullet.
  861. When the cursor is already at that position, another `C-a' will bring
  862. it to the beginning of the line.
  863. `C-e' will jump to the end of the headline, ignoring the presence of tags
  864. in the headline. A second `C-e' will then jump to the true end of the
  865. line, after any tags. This also means that, when this variable is
  866. non-nil, `C-e' also will never jump beyond the end of the heading of a
  867. folded section, i.e. not after the ellipses.
  868. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  869. going to the true line boundary first. Only a directly following, identical
  870. keypress will bring the cursor to the special positions.
  871. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  872. set separately."
  873. :group 'org-edit-structure
  874. :type '(choice
  875. (const :tag "off" nil)
  876. (const :tag "on: after stars/bullet and before tags first" t)
  877. (const :tag "reversed: true line boundary first" reversed)
  878. (cons :tag "Set C-a and C-e separately"
  879. (choice :tag "Special C-a"
  880. (const :tag "off" nil)
  881. (const :tag "on: after stars/bullet first" t)
  882. (const :tag "reversed: before stars/bullet first" reversed))
  883. (choice :tag "Special C-e"
  884. (const :tag "off" nil)
  885. (const :tag "on: before tags first" t)
  886. (const :tag "reversed: after tags first" reversed)))))
  887. (if (fboundp 'defvaralias)
  888. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  889. (defcustom org-special-ctrl-k nil
  890. "Non-nil means `C-k' will behave specially in headlines.
  891. When nil, `C-k' will call the default `kill-line' command.
  892. When t, the following will happen while the cursor is in the headline:
  893. - When the cursor is at the beginning of a headline, kill the entire
  894. line and possible the folded subtree below the line.
  895. - When in the middle of the headline text, kill the headline up to the tags.
  896. - When after the headline text, kill the tags."
  897. :group 'org-edit-structure
  898. :type 'boolean)
  899. (defcustom org-ctrl-k-protect-subtree nil
  900. "Non-nil means, do not delete a hidden subtree with C-k.
  901. When set to the symbol `error', simply throw an error when C-k is
  902. used to kill (part-of) a headline that has hidden text behind it.
  903. Any other non-nil value will result in a query to the user, if it is
  904. OK to kill that hidden subtree. When nil, kill without remorse."
  905. :group 'org-edit-structure
  906. :type '(choice
  907. (const :tag "Do not protect hidden subtrees" nil)
  908. (const :tag "Protect hidden subtrees with a security query" t)
  909. (const :tag "Never kill a hidden subtree with C-k" error)))
  910. (defcustom org-yank-folded-subtrees t
  911. "Non-nil means when yanking subtrees, fold them.
  912. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  913. it starts with a heading and all other headings in it are either children
  914. or siblings, then fold all the subtrees. However, do this only if no
  915. text after the yank would be swallowed into a folded tree by this action."
  916. :group 'org-edit-structure
  917. :type 'boolean)
  918. (defcustom org-yank-adjusted-subtrees nil
  919. "Non-nil means when yanking subtrees, adjust the level.
  920. With this setting, `org-paste-subtree' is used to insert the subtree, see
  921. this function for details."
  922. :group 'org-edit-structure
  923. :type 'boolean)
  924. (defcustom org-M-RET-may-split-line '((default . t))
  925. "Non-nil means M-RET will split the line at the cursor position.
  926. When nil, it will go to the end of the line before making a
  927. new line.
  928. You may also set this option in a different way for different
  929. contexts. Valid contexts are:
  930. headline when creating a new headline
  931. item when creating a new item
  932. table in a table field
  933. default the value to be used for all contexts not explicitly
  934. customized"
  935. :group 'org-structure
  936. :group 'org-table
  937. :type '(choice
  938. (const :tag "Always" t)
  939. (const :tag "Never" nil)
  940. (repeat :greedy t :tag "Individual contexts"
  941. (cons
  942. (choice :tag "Context"
  943. (const headline)
  944. (const item)
  945. (const table)
  946. (const default))
  947. (boolean)))))
  948. (defcustom org-insert-heading-respect-content nil
  949. "Non-nil means insert new headings after the current subtree.
  950. When nil, the new heading is created directly after the current line.
  951. The commands \\[org-insert-heading-respect-content] and
  952. \\[org-insert-todo-heading-respect-content] turn this variable on
  953. for the duration of the command."
  954. :group 'org-structure
  955. :type 'boolean)
  956. (defcustom org-blank-before-new-entry '((heading . auto)
  957. (plain-list-item . auto))
  958. "Should `org-insert-heading' leave a blank line before new heading/item?
  959. The value is an alist, with `heading' and `plain-list-item' as car,
  960. and a boolean flag as cdr. The cdr may also be the symbol `auto', and then
  961. Org will look at the surrounding headings/items and try to make an
  962. intelligent decision whether to insert a blank line or not.
  963. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  964. set, the setting here is ignored and no empty line is inserted, to avoid
  965. breaking the list structure."
  966. :group 'org-edit-structure
  967. :type '(list
  968. (cons (const heading)
  969. (choice (const :tag "Never" nil)
  970. (const :tag "Always" t)
  971. (const :tag "Auto" auto)))
  972. (cons (const plain-list-item)
  973. (choice (const :tag "Never" nil)
  974. (const :tag "Always" t)
  975. (const :tag "Auto" auto)))))
  976. (defcustom org-insert-heading-hook nil
  977. "Hook being run after inserting a new heading."
  978. :group 'org-edit-structure
  979. :type 'hook)
  980. (defcustom org-enable-fixed-width-editor t
  981. "Non-nil means lines starting with \":\" are treated as fixed-width.
  982. This currently only means they are never auto-wrapped.
  983. When nil, such lines will be treated like ordinary lines.
  984. See also the QUOTE keyword."
  985. :group 'org-edit-structure
  986. :type 'boolean)
  987. (defcustom org-goto-auto-isearch t
  988. "Non-nil means typing characters in `org-goto' starts incremental search."
  989. :group 'org-edit-structure
  990. :type 'boolean)
  991. (defgroup org-sparse-trees nil
  992. "Options concerning sparse trees in Org-mode."
  993. :tag "Org Sparse Trees"
  994. :group 'org-structure)
  995. (defcustom org-highlight-sparse-tree-matches t
  996. "Non-nil means highlight all matches that define a sparse tree.
  997. The highlights will automatically disappear the next time the buffer is
  998. changed by an edit command."
  999. :group 'org-sparse-trees
  1000. :type 'boolean)
  1001. (defcustom org-remove-highlights-with-change t
  1002. "Non-nil means any change to the buffer will remove temporary highlights.
  1003. Such highlights are created by `org-occur' and `org-clock-display'.
  1004. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1005. The highlights created by `org-preview-latex-fragment' always need
  1006. `C-c C-c' to be removed."
  1007. :group 'org-sparse-trees
  1008. :group 'org-time
  1009. :type 'boolean)
  1010. (defcustom org-occur-hook '(org-first-headline-recenter)
  1011. "Hook that is run after `org-occur' has constructed a sparse tree.
  1012. This can be used to recenter the window to show as much of the structure
  1013. as possible."
  1014. :group 'org-sparse-trees
  1015. :type 'hook)
  1016. (defgroup org-imenu-and-speedbar nil
  1017. "Options concerning imenu and speedbar in Org-mode."
  1018. :tag "Org Imenu and Speedbar"
  1019. :group 'org-structure)
  1020. (defcustom org-imenu-depth 2
  1021. "The maximum level for Imenu access to Org-mode headlines.
  1022. This also applied for speedbar access."
  1023. :group 'org-imenu-and-speedbar
  1024. :type 'integer)
  1025. (defgroup org-table nil
  1026. "Options concerning tables in Org-mode."
  1027. :tag "Org Table"
  1028. :group 'org)
  1029. (defcustom org-enable-table-editor 'optimized
  1030. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1031. When nil, such lines will be treated like ordinary lines.
  1032. When equal to the symbol `optimized', the table editor will be optimized to
  1033. do the following:
  1034. - Automatic overwrite mode in front of whitespace in table fields.
  1035. This makes the structure of the table stay in tact as long as the edited
  1036. field does not exceed the column width.
  1037. - Minimize the number of realigns. Normally, the table is aligned each time
  1038. TAB or RET are pressed to move to another field. With optimization this
  1039. happens only if changes to a field might have changed the column width.
  1040. Optimization requires replacing the functions `self-insert-command',
  1041. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1042. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1043. very good at guessing when a re-align will be necessary, but you can always
  1044. force one with \\[org-ctrl-c-ctrl-c].
  1045. If you would like to use the optimized version in Org-mode, but the
  1046. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1047. This variable can be used to turn on and off the table editor during a session,
  1048. but in order to toggle optimization, a restart is required.
  1049. See also the variable `org-table-auto-blank-field'."
  1050. :group 'org-table
  1051. :type '(choice
  1052. (const :tag "off" nil)
  1053. (const :tag "on" t)
  1054. (const :tag "on, optimized" optimized)))
  1055. (defcustom org-self-insert-cluster-for-undo t
  1056. "Non-nil means cluster self-insert commands for undo when possible.
  1057. If this is set, then, like in the Emacs command loop, 20 consecutive
  1058. characters will be undone together.
  1059. This is configurable, because there is some impact on typing performance."
  1060. :group 'org-table
  1061. :type 'boolean)
  1062. (defcustom org-table-tab-recognizes-table.el t
  1063. "Non-nil means TAB will automatically notice a table.el table.
  1064. When it sees such a table, it moves point into it and - if necessary -
  1065. calls `table-recognize-table'."
  1066. :group 'org-table-editing
  1067. :type 'boolean)
  1068. (defgroup org-link nil
  1069. "Options concerning links in Org-mode."
  1070. :tag "Org Link"
  1071. :group 'org)
  1072. (defvar org-link-abbrev-alist-local nil
  1073. "Buffer-local version of `org-link-abbrev-alist', which see.
  1074. The value of this is taken from the #+LINK lines.")
  1075. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1076. (defcustom org-link-abbrev-alist nil
  1077. "Alist of link abbreviations.
  1078. The car of each element is a string, to be replaced at the start of a link.
  1079. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1080. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1081. [[linkkey:tag][description]]
  1082. The 'linkkey' must be a word word, starting with a letter, followed
  1083. by letters, numbers, '-' or '_'.
  1084. If REPLACE is a string, the tag will simply be appended to create the link.
  1085. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1086. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1087. be inserted at that point (see the function `url-hexify-string').
  1088. REPLACE may also be a function that will be called with the tag as the
  1089. only argument to create the link, which should be returned as a string.
  1090. See the manual for examples."
  1091. :group 'org-link
  1092. :type '(repeat
  1093. (cons
  1094. (string :tag "Protocol")
  1095. (choice
  1096. (string :tag "Format")
  1097. (function)))))
  1098. (defcustom org-descriptive-links t
  1099. "Non-nil means hide link part and only show description of bracket links.
  1100. Bracket links are like [[link][description]]. This variable sets the initial
  1101. state in new org-mode buffers. The setting can then be toggled on a
  1102. per-buffer basis from the Org->Hyperlinks menu."
  1103. :group 'org-link
  1104. :type 'boolean)
  1105. (defcustom org-link-file-path-type 'adaptive
  1106. "How the path name in file links should be stored.
  1107. Valid values are:
  1108. relative Relative to the current directory, i.e. the directory of the file
  1109. into which the link is being inserted.
  1110. absolute Absolute path, if possible with ~ for home directory.
  1111. noabbrev Absolute path, no abbreviation of home directory.
  1112. adaptive Use relative path for files in the current directory and sub-
  1113. directories of it. For other files, use an absolute path."
  1114. :group 'org-link
  1115. :type '(choice
  1116. (const relative)
  1117. (const absolute)
  1118. (const noabbrev)
  1119. (const adaptive)))
  1120. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1121. "Types of links that should be activated in Org-mode files.
  1122. This is a list of symbols, each leading to the activation of a certain link
  1123. type. In principle, it does not hurt to turn on most link types - there may
  1124. be a small gain when turning off unused link types. The types are:
  1125. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1126. angle Links in angular brackets that may contain whitespace like
  1127. <bbdb:Carsten Dominik>.
  1128. plain Plain links in normal text, no whitespace, like http://google.com.
  1129. radio Text that is matched by a radio target, see manual for details.
  1130. tag Tag settings in a headline (link to tag search).
  1131. date Time stamps (link to calendar).
  1132. footnote Footnote labels.
  1133. Changing this variable requires a restart of Emacs to become effective."
  1134. :group 'org-link
  1135. :type '(set :greedy t
  1136. (const :tag "Double bracket links" bracket)
  1137. (const :tag "Angular bracket links" angle)
  1138. (const :tag "Plain text links" plain)
  1139. (const :tag "Radio target matches" radio)
  1140. (const :tag "Tags" tag)
  1141. (const :tag "Timestamps" date)
  1142. (const :tag "Footnotes" footnote)))
  1143. (defcustom org-make-link-description-function nil
  1144. "Function to use to generate link descriptions from links.
  1145. If nil the link location will be used. This function must take
  1146. two parameters; the first is the link and the second the
  1147. description `org-insert-link' has generated, and should return the
  1148. description to use."
  1149. :group 'org-link
  1150. :type 'function)
  1151. (defgroup org-link-store nil
  1152. "Options concerning storing links in Org-mode."
  1153. :tag "Org Store Link"
  1154. :group 'org-link)
  1155. (defcustom org-email-link-description-format "Email %c: %.30s"
  1156. "Format of the description part of a link to an email or usenet message.
  1157. The following %-escapes will be replaced by corresponding information:
  1158. %F full \"From\" field
  1159. %f name, taken from \"From\" field, address if no name
  1160. %T full \"To\" field
  1161. %t first name in \"To\" field, address if no name
  1162. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1163. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1164. %s subject
  1165. %d date
  1166. %m message-id.
  1167. You may use normal field width specification between the % and the letter.
  1168. This is for example useful to limit the length of the subject.
  1169. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1170. :group 'org-link-store
  1171. :type 'string)
  1172. (defcustom org-from-is-user-regexp
  1173. (let (r1 r2)
  1174. (when (and user-mail-address (not (string= user-mail-address "")))
  1175. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1176. (when (and user-full-name (not (string= user-full-name "")))
  1177. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1178. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1179. "Regexp matched against the \"From:\" header of an email or usenet message.
  1180. It should match if the message is from the user him/herself."
  1181. :group 'org-link-store
  1182. :type 'regexp)
  1183. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1184. "Non-nil means storing a link to an Org file will use entry IDs.
  1185. Note that before this variable is even considered, org-id must be loaded,
  1186. so please customize `org-modules' and turn it on.
  1187. The variable can have the following values:
  1188. t Create an ID if needed to make a link to the current entry.
  1189. create-if-interactive
  1190. If `org-store-link' is called directly (interactively, as a user
  1191. command), do create an ID to support the link. But when doing the
  1192. job for remember, only use the ID if it already exists. The
  1193. purpose of this setting is to avoid proliferation of unwanted
  1194. IDs, just because you happen to be in an Org file when you
  1195. call `org-remember' that automatically and preemptively
  1196. creates a link. If you do want to get an ID link in a remember
  1197. template to an entry not having an ID, create it first by
  1198. explicitly creating a link to it, using `C-c C-l' first.
  1199. create-if-interactive-and-no-custom-id
  1200. Like create-if-interactive, but do not create an ID if there is
  1201. a CUSTOM_ID property defined in the entry. This is the default.
  1202. use-existing
  1203. Use existing ID, do not create one.
  1204. nil Never use an ID to make a link, instead link using a text search for
  1205. the headline text."
  1206. :group 'org-link-store
  1207. :type '(choice
  1208. (const :tag "Create ID to make link" t)
  1209. (const :tag "Create if storing link interactively"
  1210. create-if-interactive)
  1211. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1212. create-if-interactive-and-no-custom-id)
  1213. (const :tag "Only use existing" use-existing)
  1214. (const :tag "Do not use ID to create link" nil)))
  1215. (defcustom org-context-in-file-links t
  1216. "Non-nil means file links from `org-store-link' contain context.
  1217. A search string will be added to the file name with :: as separator and
  1218. used to find the context when the link is activated by the command
  1219. `org-open-at-point'. When this option is t, the entire active region
  1220. will be placed in the search string of the file link. If set to a
  1221. positive integer, only the first n lines of context will be stored.
  1222. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1223. negates this setting for the duration of the command."
  1224. :group 'org-link-store
  1225. :type '(choice boolean integer))
  1226. (defcustom org-keep-stored-link-after-insertion nil
  1227. "Non-nil means keep link in list for entire session.
  1228. The command `org-store-link' adds a link pointing to the current
  1229. location to an internal list. These links accumulate during a session.
  1230. The command `org-insert-link' can be used to insert links into any
  1231. Org-mode file (offering completion for all stored links). When this
  1232. option is nil, every link which has been inserted once using \\[org-insert-link]
  1233. will be removed from the list, to make completing the unused links
  1234. more efficient."
  1235. :group 'org-link-store
  1236. :type 'boolean)
  1237. (defgroup org-link-follow nil
  1238. "Options concerning following links in Org-mode."
  1239. :tag "Org Follow Link"
  1240. :group 'org-link)
  1241. (defcustom org-link-translation-function nil
  1242. "Function to translate links with different syntax to Org syntax.
  1243. This can be used to translate links created for example by the Planner
  1244. or emacs-wiki packages to Org syntax.
  1245. The function must accept two parameters, a TYPE containing the link
  1246. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1247. which is everything after the link protocol. It should return a cons
  1248. with possibly modified values of type and path.
  1249. Org contains a function for this, so if you set this variable to
  1250. `org-translate-link-from-planner', you should be able follow many
  1251. links created by planner."
  1252. :group 'org-link-follow
  1253. :type 'function)
  1254. (defcustom org-follow-link-hook nil
  1255. "Hook that is run after a link has been followed."
  1256. :group 'org-link-follow
  1257. :type 'hook)
  1258. (defcustom org-tab-follows-link nil
  1259. "Non-nil means on links TAB will follow the link.
  1260. Needs to be set before org.el is loaded.
  1261. This really should not be used, it does not make sense, and the
  1262. implementation is bad."
  1263. :group 'org-link-follow
  1264. :type 'boolean)
  1265. (defcustom org-return-follows-link nil
  1266. "Non-nil means on links RET will follow the link."
  1267. :group 'org-link-follow
  1268. :type 'boolean)
  1269. (defcustom org-mouse-1-follows-link
  1270. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1271. "Non-nil means mouse-1 on a link will follow the link.
  1272. A longer mouse click will still set point. Does not work on XEmacs.
  1273. Needs to be set before org.el is loaded."
  1274. :group 'org-link-follow
  1275. :type 'boolean)
  1276. (defcustom org-mark-ring-length 4
  1277. "Number of different positions to be recorded in the ring.
  1278. Changing this requires a restart of Emacs to work correctly."
  1279. :group 'org-link-follow
  1280. :type 'integer)
  1281. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1282. "Non-nil means internal links in Org files must exactly match a headline.
  1283. When nil, the link search tries to match a phrase with all words
  1284. in the search text."
  1285. :group 'org-link-follow
  1286. :type '(choice
  1287. (const :tag "Use fuzy text search" nil)
  1288. (const :tag "Match only exact headline" t)
  1289. (const :tag "Match extact headline or query to create it"
  1290. query-to-create)))
  1291. (defcustom org-link-frame-setup
  1292. '((vm . vm-visit-folder-other-frame)
  1293. (gnus . org-gnus-no-new-news)
  1294. (file . find-file-other-window)
  1295. (wl . wl-other-frame))
  1296. "Setup the frame configuration for following links.
  1297. When following a link with Emacs, it may often be useful to display
  1298. this link in another window or frame. This variable can be used to
  1299. set this up for the different types of links.
  1300. For VM, use any of
  1301. `vm-visit-folder'
  1302. `vm-visit-folder-other-window'
  1303. `vm-visit-folder-other-frame'
  1304. For Gnus, use any of
  1305. `gnus'
  1306. `gnus-other-frame'
  1307. `org-gnus-no-new-news'
  1308. For FILE, use any of
  1309. `find-file'
  1310. `find-file-other-window'
  1311. `find-file-other-frame'
  1312. For Wanderlust use any of
  1313. `wl'
  1314. `wl-other-frame'
  1315. For the calendar, use the variable `calendar-setup'.
  1316. For BBDB, it is currently only possible to display the matches in
  1317. another window."
  1318. :group 'org-link-follow
  1319. :type '(list
  1320. (cons (const vm)
  1321. (choice
  1322. (const vm-visit-folder)
  1323. (const vm-visit-folder-other-window)
  1324. (const vm-visit-folder-other-frame)))
  1325. (cons (const gnus)
  1326. (choice
  1327. (const gnus)
  1328. (const gnus-other-frame)
  1329. (const org-gnus-no-new-news)))
  1330. (cons (const file)
  1331. (choice
  1332. (const find-file)
  1333. (const find-file-other-window)
  1334. (const find-file-other-frame)))
  1335. (cons (const wl)
  1336. (choice
  1337. (const wl)
  1338. (const wl-other-frame)))))
  1339. (defcustom org-display-internal-link-with-indirect-buffer nil
  1340. "Non-nil means use indirect buffer to display infile links.
  1341. Activating internal links (from one location in a file to another location
  1342. in the same file) normally just jumps to the location. When the link is
  1343. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1344. is displayed in
  1345. another window. When this option is set, the other window actually displays
  1346. an indirect buffer clone of the current buffer, to avoid any visibility
  1347. changes to the current buffer."
  1348. :group 'org-link-follow
  1349. :type 'boolean)
  1350. (defcustom org-open-non-existing-files nil
  1351. "Non-nil means `org-open-file' will open non-existing files.
  1352. When nil, an error will be generated.
  1353. This variable applies only to external applications because they
  1354. might choke on non-existing files. If the link is to a file that
  1355. will be opened in Emacs, the variable is ignored."
  1356. :group 'org-link-follow
  1357. :type 'boolean)
  1358. (defcustom org-open-directory-means-index-dot-org nil
  1359. "Non-nil means a link to a directory really means to index.org.
  1360. When nil, following a directory link will run dired or open a finder/explorer
  1361. window on that directory."
  1362. :group 'org-link-follow
  1363. :type 'boolean)
  1364. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1365. "Function and arguments to call for following mailto links.
  1366. This is a list with the first element being a Lisp function, and the
  1367. remaining elements being arguments to the function. In string arguments,
  1368. %a will be replaced by the address, and %s will be replaced by the subject
  1369. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1370. :group 'org-link-follow
  1371. :type '(choice
  1372. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1373. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1374. (const :tag "message-mail" (message-mail "%a" "%s"))
  1375. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1376. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1377. "Non-nil means ask for confirmation before executing shell links.
  1378. Shell links can be dangerous: just think about a link
  1379. [[shell:rm -rf ~/*][Google Search]]
  1380. This link would show up in your Org-mode document as \"Google Search\",
  1381. but really it would remove your entire home directory.
  1382. Therefore we advise against setting this variable to nil.
  1383. Just change it to `y-or-n-p' if you want to confirm with a
  1384. single keystroke rather than having to type \"yes\"."
  1385. :group 'org-link-follow
  1386. :type '(choice
  1387. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1388. (const :tag "with y-or-n (faster)" y-or-n-p)
  1389. (const :tag "no confirmation (dangerous)" nil)))
  1390. (put 'org-confirm-shell-link-function
  1391. 'safe-local-variable
  1392. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1393. (defcustom org-confirm-shell-link-not-regexp ""
  1394. "A regexp to skip confirmation for shell links."
  1395. :group 'org-link-follow
  1396. :type 'regexp)
  1397. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1398. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1399. Elisp links can be dangerous: just think about a link
  1400. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1401. This link would show up in your Org-mode document as \"Google Search\",
  1402. but really it would remove your entire home directory.
  1403. Therefore we advise against setting this variable to nil.
  1404. Just change it to `y-or-n-p' if you want to confirm with a
  1405. single keystroke rather than having to type \"yes\"."
  1406. :group 'org-link-follow
  1407. :type '(choice
  1408. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1409. (const :tag "with y-or-n (faster)" y-or-n-p)
  1410. (const :tag "no confirmation (dangerous)" nil)))
  1411. (put 'org-confirm-shell-link-function
  1412. 'safe-local-variable
  1413. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1414. (defcustom org-confirm-elisp-link-not-regexp ""
  1415. "A regexp to skip confirmation for Elisp links."
  1416. :group 'org-link-follow
  1417. :type 'regexp)
  1418. (defconst org-file-apps-defaults-gnu
  1419. '((remote . emacs)
  1420. (system . mailcap)
  1421. (t . mailcap))
  1422. "Default file applications on a UNIX or GNU/Linux system.
  1423. See `org-file-apps'.")
  1424. (defconst org-file-apps-defaults-macosx
  1425. '((remote . emacs)
  1426. (t . "open %s")
  1427. (system . "open %s")
  1428. ("ps.gz" . "gv %s")
  1429. ("eps.gz" . "gv %s")
  1430. ("dvi" . "xdvi %s")
  1431. ("fig" . "xfig %s"))
  1432. "Default file applications on a MacOS X system.
  1433. The system \"open\" is known as a default, but we use X11 applications
  1434. for some files for which the OS does not have a good default.
  1435. See `org-file-apps'.")
  1436. (defconst org-file-apps-defaults-windowsnt
  1437. (list
  1438. '(remote . emacs)
  1439. (cons t
  1440. (list (if (featurep 'xemacs)
  1441. 'mswindows-shell-execute
  1442. 'w32-shell-execute)
  1443. "open" 'file))
  1444. (cons 'system
  1445. (list (if (featurep 'xemacs)
  1446. 'mswindows-shell-execute
  1447. 'w32-shell-execute)
  1448. "open" 'file)))
  1449. "Default file applications on a Windows NT system.
  1450. The system \"open\" is used for most files.
  1451. See `org-file-apps'.")
  1452. (defcustom org-file-apps
  1453. '(
  1454. (auto-mode . emacs)
  1455. ("\\.mm\\'" . default)
  1456. ("\\.x?html?\\'" . default)
  1457. ("\\.pdf\\'" . default)
  1458. )
  1459. "External applications for opening `file:path' items in a document.
  1460. Org-mode uses system defaults for different file types, but
  1461. you can use this variable to set the application for a given file
  1462. extension. The entries in this list are cons cells where the car identifies
  1463. files and the cdr the corresponding command. Possible values for the
  1464. file identifier are
  1465. \"string\" A string as a file identifier can be interpreted in different
  1466. ways, depending on its contents:
  1467. - Alphanumeric characters only:
  1468. Match links with this file extension.
  1469. Example: (\"pdf\" . \"evince %s\")
  1470. to open PDFs with evince.
  1471. - Regular expression: Match links where the
  1472. filename matches the regexp. If you want to
  1473. use groups here, use shy groups.
  1474. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1475. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1476. to open *.html and *.xhtml with firefox.
  1477. - Regular expression which contains (non-shy) groups:
  1478. Match links where the whole link, including \"::\", and
  1479. anything after that, matches the regexp.
  1480. In a custom command string, %1, %2, etc. are replaced with
  1481. the parts of the link that were matched by the groups.
  1482. For backwards compatibility, if a command string is given
  1483. that does not use any of the group matches, this case is
  1484. handled identically to the second one (i.e. match against
  1485. file name only).
  1486. In a custom lisp form, you can access the group matches with
  1487. (match-string n link).
  1488. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1489. to open [[file:document.pdf::5]] with evince at page 5.
  1490. `directory' Matches a directory
  1491. `remote' Matches a remote file, accessible through tramp or efs.
  1492. Remote files most likely should be visited through Emacs
  1493. because external applications cannot handle such paths.
  1494. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1495. so all files Emacs knows how to handle. Using this with
  1496. command `emacs' will open most files in Emacs. Beware that this
  1497. will also open html files inside Emacs, unless you add
  1498. (\"html\" . default) to the list as well.
  1499. t Default for files not matched by any of the other options.
  1500. `system' The system command to open files, like `open' on Windows
  1501. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1502. that will be selected if you call `C-c C-o' with a double
  1503. \\[universal-argument] \\[universal-argument] prefix.
  1504. Possible values for the command are:
  1505. `emacs' The file will be visited by the current Emacs process.
  1506. `default' Use the default application for this file type, which is the
  1507. association for t in the list, most likely in the system-specific
  1508. part.
  1509. This can be used to overrule an unwanted setting in the
  1510. system-specific variable.
  1511. `system' Use the system command for opening files, like \"open\".
  1512. This command is specified by the entry whose car is `system'.
  1513. Most likely, the system-specific version of this variable
  1514. does define this command, but you can overrule/replace it
  1515. here.
  1516. string A command to be executed by a shell; %s will be replaced
  1517. by the path to the file.
  1518. sexp A Lisp form which will be evaluated. The file path will
  1519. be available in the Lisp variable `file'.
  1520. For more examples, see the system specific constants
  1521. `org-file-apps-defaults-macosx'
  1522. `org-file-apps-defaults-windowsnt'
  1523. `org-file-apps-defaults-gnu'."
  1524. :group 'org-link-follow
  1525. :type '(repeat
  1526. (cons (choice :value ""
  1527. (string :tag "Extension")
  1528. (const :tag "System command to open files" system)
  1529. (const :tag "Default for unrecognized files" t)
  1530. (const :tag "Remote file" remote)
  1531. (const :tag "Links to a directory" directory)
  1532. (const :tag "Any files that have Emacs modes"
  1533. auto-mode))
  1534. (choice :value ""
  1535. (const :tag "Visit with Emacs" emacs)
  1536. (const :tag "Use default" default)
  1537. (const :tag "Use the system command" system)
  1538. (string :tag "Command")
  1539. (sexp :tag "Lisp form")))))
  1540. (defgroup org-refile nil
  1541. "Options concerning refiling entries in Org-mode."
  1542. :tag "Org Refile"
  1543. :group 'org)
  1544. (defcustom org-directory "~/org"
  1545. "Directory with org files.
  1546. This is just a default location to look for Org files. There is no need
  1547. at all to put your files into this directory. It is only used in the
  1548. following situations:
  1549. 1. When a remember template specifies a target file that is not an
  1550. absolute path. The path will then be interpreted relative to
  1551. `org-directory'
  1552. 2. When a remember note is filed away in an interactive way (when exiting the
  1553. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1554. with `org-directory' as the default path."
  1555. :group 'org-refile
  1556. :group 'org-remember
  1557. :type 'directory)
  1558. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1559. "Default target for storing notes.
  1560. Used as a fall back file for org-remember.el and org-capture.el, for
  1561. templates that do not specify a target file."
  1562. :group 'org-refile
  1563. :group 'org-remember
  1564. :type '(choice
  1565. (const :tag "Default from remember-data-file" nil)
  1566. file))
  1567. (defcustom org-goto-interface 'outline
  1568. "The default interface to be used for `org-goto'.
  1569. Allowed values are:
  1570. outline The interface shows an outline of the relevant file
  1571. and the correct heading is found by moving through
  1572. the outline or by searching with incremental search.
  1573. outline-path-completion Headlines in the current buffer are offered via
  1574. completion. This is the interface also used by
  1575. the refile command."
  1576. :group 'org-refile
  1577. :type '(choice
  1578. (const :tag "Outline" outline)
  1579. (const :tag "Outline-path-completion" outline-path-completion)))
  1580. (defcustom org-goto-max-level 5
  1581. "Maximum target level when running `org-goto' with refile interface."
  1582. :group 'org-refile
  1583. :type 'integer)
  1584. (defcustom org-reverse-note-order nil
  1585. "Non-nil means store new notes at the beginning of a file or entry.
  1586. When nil, new notes will be filed to the end of a file or entry.
  1587. This can also be a list with cons cells of regular expressions that
  1588. are matched against file names, and values."
  1589. :group 'org-remember
  1590. :group 'org-refile
  1591. :type '(choice
  1592. (const :tag "Reverse always" t)
  1593. (const :tag "Reverse never" nil)
  1594. (repeat :tag "By file name regexp"
  1595. (cons regexp boolean))))
  1596. (defcustom org-log-refile nil
  1597. "Information to record when a task is refiled.
  1598. Possible values are:
  1599. nil Don't add anything
  1600. time Add a time stamp to the task
  1601. note Prompt for a note and add it with template `org-log-note-headings'
  1602. This option can also be set with on a per-file-basis with
  1603. #+STARTUP: nologrefile
  1604. #+STARTUP: logrefile
  1605. #+STARTUP: lognoterefile
  1606. You can have local logging settings for a subtree by setting the LOGGING
  1607. property to one or more of these keywords.
  1608. When bulk-refiling from the agenda, the value `note' is forbidden and
  1609. will temporarily be changed to `time'."
  1610. :group 'org-refile
  1611. :group 'org-progress
  1612. :type '(choice
  1613. (const :tag "No logging" nil)
  1614. (const :tag "Record timestamp" time)
  1615. (const :tag "Record timestamp with note." note)))
  1616. (defcustom org-refile-targets nil
  1617. "Targets for refiling entries with \\[org-refile].
  1618. This is list of cons cells. Each cell contains:
  1619. - a specification of the files to be considered, either a list of files,
  1620. or a symbol whose function or variable value will be used to retrieve
  1621. a file name or a list of file names. If you use `org-agenda-files' for
  1622. that, all agenda files will be scanned for targets. Nil means consider
  1623. headings in the current buffer.
  1624. - A specification of how to find candidate refile targets. This may be
  1625. any of:
  1626. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1627. This tag has to be present in all target headlines, inheritance will
  1628. not be considered.
  1629. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1630. todo keyword.
  1631. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1632. headlines that are refiling targets.
  1633. - a cons cell (:level . N). Any headline of level N is considered a target.
  1634. Note that, when `org-odd-levels-only' is set, level corresponds to
  1635. order in hierarchy, not to the number of stars.
  1636. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1637. Note that, when `org-odd-levels-only' is set, level corresponds to
  1638. order in hierarchy, not to the number of stars.
  1639. You can set the variable `org-refile-target-verify-function' to a function
  1640. to verify each headline found by the simple criteria above.
  1641. When this variable is nil, all top-level headlines in the current buffer
  1642. are used, equivalent to the value `((nil . (:level . 1))'."
  1643. :group 'org-refile
  1644. :type '(repeat
  1645. (cons
  1646. (choice :value org-agenda-files
  1647. (const :tag "All agenda files" org-agenda-files)
  1648. (const :tag "Current buffer" nil)
  1649. (function) (variable) (file))
  1650. (choice :tag "Identify target headline by"
  1651. (cons :tag "Specific tag" (const :value :tag) (string))
  1652. (cons :tag "TODO keyword" (const :value :todo) (string))
  1653. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1654. (cons :tag "Level number" (const :value :level) (integer))
  1655. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1656. (defcustom org-refile-target-verify-function nil
  1657. "Function to verify if the headline at point should be a refile target.
  1658. The function will be called without arguments, with point at the
  1659. beginning of the headline. It should return t and leave point
  1660. where it is if the headline is a valid target for refiling.
  1661. If the target should not be selected, the function must return nil.
  1662. In addition to this, it may move point to a place from where the search
  1663. should be continued. For example, the function may decide that the entire
  1664. subtree of the current entry should be excluded and move point to the end
  1665. of the subtree."
  1666. :group 'org-refile
  1667. :type 'function)
  1668. (defcustom org-refile-use-cache nil
  1669. "Non-nil means cache refile targets to speed up the process.
  1670. The cache for a particular file will be updated automatically when
  1671. the buffer has been killed, or when any of the marker used for flagging
  1672. refile targets no longer points at a live buffer.
  1673. If you have added new entries to a buffer that might themselves be targets,
  1674. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1675. find that easier, `C-u C-u C-u C-c C-w'."
  1676. :group 'org-refile
  1677. :type 'boolean)
  1678. (defcustom org-refile-use-outline-path nil
  1679. "Non-nil means provide refile targets as paths.
  1680. So a level 3 headline will be available as level1/level2/level3.
  1681. When the value is `file', also include the file name (without directory)
  1682. into the path. In this case, you can also stop the completion after
  1683. the file name, to get entries inserted as top level in the file.
  1684. When `full-file-path', include the full file path."
  1685. :group 'org-refile
  1686. :type '(choice
  1687. (const :tag "Not" nil)
  1688. (const :tag "Yes" t)
  1689. (const :tag "Start with file name" file)
  1690. (const :tag "Start with full file path" full-file-path)))
  1691. (defcustom org-outline-path-complete-in-steps t
  1692. "Non-nil means complete the outline path in hierarchical steps.
  1693. When Org-mode uses the refile interface to select an outline path
  1694. \(see variable `org-refile-use-outline-path'), the completion of
  1695. the path can be done is a single go, or if can be done in steps down
  1696. the headline hierarchy. Going in steps is probably the best if you
  1697. do not use a special completion package like `ido' or `icicles'.
  1698. However, when using these packages, going in one step can be very
  1699. fast, while still showing the whole path to the entry."
  1700. :group 'org-refile
  1701. :type 'boolean)
  1702. (defcustom org-refile-allow-creating-parent-nodes nil
  1703. "Non-nil means allow to create new nodes as refile targets.
  1704. New nodes are then created by adding \"/new node name\" to the completion
  1705. of an existing node. When the value of this variable is `confirm',
  1706. new node creation must be confirmed by the user (recommended)
  1707. When nil, the completion must match an existing entry.
  1708. Note that, if the new heading is not seen by the criteria
  1709. listed in `org-refile-targets', multiple instances of the same
  1710. heading would be created by trying again to file under the new
  1711. heading."
  1712. :group 'org-refile
  1713. :type '(choice
  1714. (const :tag "Never" nil)
  1715. (const :tag "Always" t)
  1716. (const :tag "Prompt for confirmation" confirm)))
  1717. (defgroup org-todo nil
  1718. "Options concerning TODO items in Org-mode."
  1719. :tag "Org TODO"
  1720. :group 'org)
  1721. (defgroup org-progress nil
  1722. "Options concerning Progress logging in Org-mode."
  1723. :tag "Org Progress"
  1724. :group 'org-time)
  1725. (defvar org-todo-interpretation-widgets
  1726. '(
  1727. (:tag "Sequence (cycling hits every state)" sequence)
  1728. (:tag "Type (cycling directly to DONE)" type))
  1729. "The available interpretation symbols for customizing `org-todo-keywords'.
  1730. Interested libraries should add to this list.")
  1731. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1732. "List of TODO entry keyword sequences and their interpretation.
  1733. \\<org-mode-map>This is a list of sequences.
  1734. Each sequence starts with a symbol, either `sequence' or `type',
  1735. indicating if the keywords should be interpreted as a sequence of
  1736. action steps, or as different types of TODO items. The first
  1737. keywords are states requiring action - these states will select a headline
  1738. for inclusion into the global TODO list Org-mode produces. If one of
  1739. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1740. signify that no further action is necessary. If \"|\" is not found,
  1741. the last keyword is treated as the only DONE state of the sequence.
  1742. The command \\[org-todo] cycles an entry through these states, and one
  1743. additional state where no keyword is present. For details about this
  1744. cycling, see the manual.
  1745. TODO keywords and interpretation can also be set on a per-file basis with
  1746. the special #+SEQ_TODO and #+TYP_TODO lines.
  1747. Each keyword can optionally specify a character for fast state selection
  1748. \(in combination with the variable `org-use-fast-todo-selection')
  1749. and specifiers for state change logging, using the same syntax
  1750. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1751. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1752. indicates to record a time stamp each time this state is selected.
  1753. Each keyword may also specify if a timestamp or a note should be
  1754. recorded when entering or leaving the state, by adding additional
  1755. characters in the parenthesis after the keyword. This looks like this:
  1756. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1757. record only the time of the state change. With X and Y being either
  1758. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1759. Y when leaving the state if and only if the *target* state does not
  1760. define X. You may omit any of the fast-selection key or X or /Y,
  1761. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1762. For backward compatibility, this variable may also be just a list
  1763. of keywords - in this case the interpretation (sequence or type) will be
  1764. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1765. :group 'org-todo
  1766. :group 'org-keywords
  1767. :type '(choice
  1768. (repeat :tag "Old syntax, just keywords"
  1769. (string :tag "Keyword"))
  1770. (repeat :tag "New syntax"
  1771. (cons
  1772. (choice
  1773. :tag "Interpretation"
  1774. ;;Quick and dirty way to see
  1775. ;;`org-todo-interpretations'. This takes the
  1776. ;;place of item arguments
  1777. :convert-widget
  1778. (lambda (widget)
  1779. (widget-put widget
  1780. :args (mapcar
  1781. #'(lambda (x)
  1782. (widget-convert
  1783. (cons 'const x)))
  1784. org-todo-interpretation-widgets))
  1785. widget))
  1786. (repeat
  1787. (string :tag "Keyword"))))))
  1788. (defvar org-todo-keywords-1 nil
  1789. "All TODO and DONE keywords active in a buffer.")
  1790. (make-variable-buffer-local 'org-todo-keywords-1)
  1791. (defvar org-todo-keywords-for-agenda nil)
  1792. (defvar org-done-keywords-for-agenda nil)
  1793. (defvar org-drawers-for-agenda nil)
  1794. (defvar org-todo-keyword-alist-for-agenda nil)
  1795. (defvar org-tag-alist-for-agenda nil)
  1796. (defvar org-agenda-contributing-files nil)
  1797. (defvar org-not-done-keywords nil)
  1798. (make-variable-buffer-local 'org-not-done-keywords)
  1799. (defvar org-done-keywords nil)
  1800. (make-variable-buffer-local 'org-done-keywords)
  1801. (defvar org-todo-heads nil)
  1802. (make-variable-buffer-local 'org-todo-heads)
  1803. (defvar org-todo-sets nil)
  1804. (make-variable-buffer-local 'org-todo-sets)
  1805. (defvar org-todo-log-states nil)
  1806. (make-variable-buffer-local 'org-todo-log-states)
  1807. (defvar org-todo-kwd-alist nil)
  1808. (make-variable-buffer-local 'org-todo-kwd-alist)
  1809. (defvar org-todo-key-alist nil)
  1810. (make-variable-buffer-local 'org-todo-key-alist)
  1811. (defvar org-todo-key-trigger nil)
  1812. (make-variable-buffer-local 'org-todo-key-trigger)
  1813. (defcustom org-todo-interpretation 'sequence
  1814. "Controls how TODO keywords are interpreted.
  1815. This variable is in principle obsolete and is only used for
  1816. backward compatibility, if the interpretation of todo keywords is
  1817. not given already in `org-todo-keywords'. See that variable for
  1818. more information."
  1819. :group 'org-todo
  1820. :group 'org-keywords
  1821. :type '(choice (const sequence)
  1822. (const type)))
  1823. (defcustom org-use-fast-todo-selection t
  1824. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1825. This variable describes if and under what circumstances the cycling
  1826. mechanism for TODO keywords will be replaced by a single-key, direct
  1827. selection scheme.
  1828. When nil, fast selection is never used.
  1829. When the symbol `prefix', it will be used when `org-todo' is called with
  1830. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1831. in an agenda buffer.
  1832. When t, fast selection is used by default. In this case, the prefix
  1833. argument forces cycling instead.
  1834. In all cases, the special interface is only used if access keys have actually
  1835. been assigned by the user, i.e. if keywords in the configuration are followed
  1836. by a letter in parenthesis, like TODO(t)."
  1837. :group 'org-todo
  1838. :type '(choice
  1839. (const :tag "Never" nil)
  1840. (const :tag "By default" t)
  1841. (const :tag "Only with C-u C-c C-t" prefix)))
  1842. (defcustom org-provide-todo-statistics t
  1843. "Non-nil means update todo statistics after insert and toggle.
  1844. ALL-HEADLINES means update todo statistics by including headlines
  1845. with no TODO keyword as well, counting them as not done.
  1846. A list of TODO keywords means the same, but skip keywords that are
  1847. not in this list.
  1848. When this is set, todo statistics is updated in the parent of the
  1849. current entry each time a todo state is changed."
  1850. :group 'org-todo
  1851. :type '(choice
  1852. (const :tag "Yes, only for TODO entries" t)
  1853. (const :tag "Yes, including all entries" 'all-headlines)
  1854. (repeat :tag "Yes, for TODOs in this list"
  1855. (string :tag "TODO keyword"))
  1856. (other :tag "No TODO statistics" nil)))
  1857. (defcustom org-hierarchical-todo-statistics t
  1858. "Non-nil means TODO statistics covers just direct children.
  1859. When nil, all entries in the subtree are considered.
  1860. This has only an effect if `org-provide-todo-statistics' is set.
  1861. To set this to nil for only a single subtree, use a COOKIE_DATA
  1862. property and include the word \"recursive\" into the value."
  1863. :group 'org-todo
  1864. :type 'boolean)
  1865. (defcustom org-after-todo-state-change-hook nil
  1866. "Hook which is run after the state of a TODO item was changed.
  1867. The new state (a string with a TODO keyword, or nil) is available in the
  1868. Lisp variable `state'."
  1869. :group 'org-todo
  1870. :type 'hook)
  1871. (defvar org-blocker-hook nil
  1872. "Hook for functions that are allowed to block a state change.
  1873. Each function gets as its single argument a property list, see
  1874. `org-trigger-hook' for more information about this list.
  1875. If any of the functions in this hook returns nil, the state change
  1876. is blocked.")
  1877. (defvar org-trigger-hook nil
  1878. "Hook for functions that are triggered by a state change.
  1879. Each function gets as its single argument a property list with at least
  1880. the following elements:
  1881. (:type type-of-change :position pos-at-entry-start
  1882. :from old-state :to new-state)
  1883. Depending on the type, more properties may be present.
  1884. This mechanism is currently implemented for:
  1885. TODO state changes
  1886. ------------------
  1887. :type todo-state-change
  1888. :from previous state (keyword as a string), or nil, or a symbol
  1889. 'todo' or 'done', to indicate the general type of state.
  1890. :to new state, like in :from")
  1891. (defcustom org-enforce-todo-dependencies nil
  1892. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1893. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1894. be blocked if any prior sibling is not yet done.
  1895. Finally, if the parent is blocked because of ordered siblings of its own,
  1896. the child will also be blocked.
  1897. This variable needs to be set before org.el is loaded, and you need to
  1898. restart Emacs after a change to make the change effective. The only way
  1899. to change is while Emacs is running is through the customize interface."
  1900. :set (lambda (var val)
  1901. (set var val)
  1902. (if val
  1903. (add-hook 'org-blocker-hook
  1904. 'org-block-todo-from-children-or-siblings-or-parent)
  1905. (remove-hook 'org-blocker-hook
  1906. 'org-block-todo-from-children-or-siblings-or-parent)))
  1907. :group 'org-todo
  1908. :type 'boolean)
  1909. (defcustom org-enforce-todo-checkbox-dependencies nil
  1910. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1911. When this is nil, checkboxes have no influence on switching TODO states.
  1912. When non-nil, you first need to check off all check boxes before the TODO
  1913. entry can be switched to DONE.
  1914. This variable needs to be set before org.el is loaded, and you need to
  1915. restart Emacs after a change to make the change effective. The only way
  1916. to change is while Emacs is running is through the customize interface."
  1917. :set (lambda (var val)
  1918. (set var val)
  1919. (if val
  1920. (add-hook 'org-blocker-hook
  1921. 'org-block-todo-from-checkboxes)
  1922. (remove-hook 'org-blocker-hook
  1923. 'org-block-todo-from-checkboxes)))
  1924. :group 'org-todo
  1925. :type 'boolean)
  1926. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1927. "Non-nil means inserting a TODO heading is treated as state change.
  1928. So when the command \\[org-insert-todo-heading] is used, state change
  1929. logging will apply if appropriate. When nil, the new TODO item will
  1930. be inserted directly, and no logging will take place."
  1931. :group 'org-todo
  1932. :type 'boolean)
  1933. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1934. "Non-nil means switching TODO states with S-cursor counts as state change.
  1935. This is the default behavior. However, setting this to nil allows a
  1936. convenient way to select a TODO state and bypass any logging associated
  1937. with that."
  1938. :group 'org-todo
  1939. :type 'boolean)
  1940. (defcustom org-todo-state-tags-triggers nil
  1941. "Tag changes that should be triggered by TODO state changes.
  1942. This is a list. Each entry is
  1943. (state-change (tag . flag) .......)
  1944. State-change can be a string with a state, and empty string to indicate the
  1945. state that has no TODO keyword, or it can be one of the symbols `todo'
  1946. or `done', meaning any not-done or done state, respectively."
  1947. :group 'org-todo
  1948. :group 'org-tags
  1949. :type '(repeat
  1950. (cons (choice :tag "When changing to"
  1951. (const :tag "Not-done state" todo)
  1952. (const :tag "Done state" done)
  1953. (string :tag "State"))
  1954. (repeat
  1955. (cons :tag "Tag action"
  1956. (string :tag "Tag")
  1957. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1958. (defcustom org-log-done nil
  1959. "Information to record when a task moves to the DONE state.
  1960. Possible values are:
  1961. nil Don't add anything, just change the keyword
  1962. time Add a time stamp to the task
  1963. note Prompt for a note and add it with template `org-log-note-headings'
  1964. This option can also be set with on a per-file-basis with
  1965. #+STARTUP: nologdone
  1966. #+STARTUP: logdone
  1967. #+STARTUP: lognotedone
  1968. You can have local logging settings for a subtree by setting the LOGGING
  1969. property to one or more of these keywords."
  1970. :group 'org-todo
  1971. :group 'org-progress
  1972. :type '(choice
  1973. (const :tag "No logging" nil)
  1974. (const :tag "Record CLOSED timestamp" time)
  1975. (const :tag "Record CLOSED timestamp with note." note)))
  1976. ;; Normalize old uses of org-log-done.
  1977. (cond
  1978. ((eq org-log-done t) (setq org-log-done 'time))
  1979. ((and (listp org-log-done) (memq 'done org-log-done))
  1980. (setq org-log-done 'note)))
  1981. (defcustom org-log-reschedule nil
  1982. "Information to record when the scheduling date of a tasks is modified.
  1983. Possible values are:
  1984. nil Don't add anything, just change the date
  1985. time Add a time stamp to the task
  1986. note Prompt for a note and add it with template `org-log-note-headings'
  1987. This option can also be set with on a per-file-basis with
  1988. #+STARTUP: nologreschedule
  1989. #+STARTUP: logreschedule
  1990. #+STARTUP: lognotereschedule"
  1991. :group 'org-todo
  1992. :group 'org-progress
  1993. :type '(choice
  1994. (const :tag "No logging" nil)
  1995. (const :tag "Record timestamp" time)
  1996. (const :tag "Record timestamp with note." note)))
  1997. (defcustom org-log-redeadline nil
  1998. "Information to record when the deadline date of a tasks is modified.
  1999. Possible values are:
  2000. nil Don't add anything, just change the date
  2001. time Add a time stamp to the task
  2002. note Prompt for a note and add it with template `org-log-note-headings'
  2003. This option can also be set with on a per-file-basis with
  2004. #+STARTUP: nologredeadline
  2005. #+STARTUP: logredeadline
  2006. #+STARTUP: lognoteredeadline
  2007. You can have local logging settings for a subtree by setting the LOGGING
  2008. property to one or more of these keywords."
  2009. :group 'org-todo
  2010. :group 'org-progress
  2011. :type '(choice
  2012. (const :tag "No logging" nil)
  2013. (const :tag "Record timestamp" time)
  2014. (const :tag "Record timestamp with note." note)))
  2015. (defcustom org-log-note-clock-out nil
  2016. "Non-nil means record a note when clocking out of an item.
  2017. This can also be configured on a per-file basis by adding one of
  2018. the following lines anywhere in the buffer:
  2019. #+STARTUP: lognoteclock-out
  2020. #+STARTUP: nolognoteclock-out"
  2021. :group 'org-todo
  2022. :group 'org-progress
  2023. :type 'boolean)
  2024. (defcustom org-log-done-with-time t
  2025. "Non-nil means the CLOSED time stamp will contain date and time.
  2026. When nil, only the date will be recorded."
  2027. :group 'org-progress
  2028. :type 'boolean)
  2029. (defcustom org-log-note-headings
  2030. '((done . "CLOSING NOTE %t")
  2031. (state . "State %-12s from %-12S %t")
  2032. (note . "Note taken on %t")
  2033. (reschedule . "Rescheduled from %S on %t")
  2034. (delschedule . "Not scheduled, was %S on %t")
  2035. (redeadline . "New deadline from %S on %t")
  2036. (deldeadline . "Removed deadline, was %S on %t")
  2037. (refile . "Refiled on %t")
  2038. (clock-out . ""))
  2039. "Headings for notes added to entries.
  2040. The value is an alist, with the car being a symbol indicating the note
  2041. context, and the cdr is the heading to be used. The heading may also be the
  2042. empty string.
  2043. %t in the heading will be replaced by a time stamp.
  2044. %T will be an active time stamp instead the default inactive one
  2045. %s will be replaced by the new TODO state, in double quotes.
  2046. %S will be replaced by the old TODO state, in double quotes.
  2047. %u will be replaced by the user name.
  2048. %U will be replaced by the full user name.
  2049. In fact, it is not a good idea to change the `state' entry, because
  2050. agenda log mode depends on the format of these entries."
  2051. :group 'org-todo
  2052. :group 'org-progress
  2053. :type '(list :greedy t
  2054. (cons (const :tag "Heading when closing an item" done) string)
  2055. (cons (const :tag
  2056. "Heading when changing todo state (todo sequence only)"
  2057. state) string)
  2058. (cons (const :tag "Heading when just taking a note" note) string)
  2059. (cons (const :tag "Heading when clocking out" clock-out) string)
  2060. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2061. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2062. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2063. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2064. (cons (const :tag "Heading when refiling" refile) string)))
  2065. (unless (assq 'note org-log-note-headings)
  2066. (push '(note . "%t") org-log-note-headings))
  2067. (defcustom org-log-into-drawer nil
  2068. "Non-nil means insert state change notes and time stamps into a drawer.
  2069. When nil, state changes notes will be inserted after the headline and
  2070. any scheduling and clock lines, but not inside a drawer.
  2071. The value of this variable should be the name of the drawer to use.
  2072. LOGBOOK is proposed at the default drawer for this purpose, you can
  2073. also set this to a string to define the drawer of your choice.
  2074. A value of t is also allowed, representing \"LOGBOOK\".
  2075. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2076. will be ignored.
  2077. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2078. a subtree."
  2079. :group 'org-todo
  2080. :group 'org-progress
  2081. :type '(choice
  2082. (const :tag "Not into a drawer" nil)
  2083. (const :tag "LOGBOOK" t)
  2084. (string :tag "Other")))
  2085. (if (fboundp 'defvaralias)
  2086. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2087. (defun org-log-into-drawer ()
  2088. "Return the value of `org-log-into-drawer', but let properties overrule.
  2089. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2090. used instead of the default value."
  2091. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2092. (cond
  2093. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2094. ((equal p "t") "LOGBOOK")
  2095. (t p))))
  2096. (defcustom org-log-state-notes-insert-after-drawers nil
  2097. "Non-nil means insert state change notes after any drawers in entry.
  2098. Only the drawers that *immediately* follow the headline and the
  2099. deadline/scheduled line are skipped.
  2100. When nil, insert notes right after the heading and perhaps the line
  2101. with deadline/scheduling if present.
  2102. This variable will have no effect if `org-log-into-drawer' is
  2103. set."
  2104. :group 'org-todo
  2105. :group 'org-progress
  2106. :type 'boolean)
  2107. (defcustom org-log-states-order-reversed t
  2108. "Non-nil means the latest state note will be directly after heading.
  2109. When nil, the state change notes will be ordered according to time."
  2110. :group 'org-todo
  2111. :group 'org-progress
  2112. :type 'boolean)
  2113. (defcustom org-todo-repeat-to-state nil
  2114. "The TODO state to which a repeater should return the repeating task.
  2115. By default this is the first task in a TODO sequence, or the previous state
  2116. in a TODO_TYP set. But you can specify another task here.
  2117. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2118. :group 'org-todo
  2119. :type '(choice (const :tag "Head of sequence" nil)
  2120. (string :tag "Specific state")))
  2121. (defcustom org-log-repeat 'time
  2122. "Non-nil means record moving through the DONE state when triggering repeat.
  2123. An auto-repeating task is immediately switched back to TODO when
  2124. marked DONE. If you are not logging state changes (by adding \"@\"
  2125. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2126. record a closing note, there will be no record of the task moving
  2127. through DONE. This variable forces taking a note anyway.
  2128. nil Don't force a record
  2129. time Record a time stamp
  2130. note Record a note
  2131. This option can also be set with on a per-file-basis with
  2132. #+STARTUP: logrepeat
  2133. #+STARTUP: lognoterepeat
  2134. #+STARTUP: nologrepeat
  2135. You can have local logging settings for a subtree by setting the LOGGING
  2136. property to one or more of these keywords."
  2137. :group 'org-todo
  2138. :group 'org-progress
  2139. :type '(choice
  2140. (const :tag "Don't force a record" nil)
  2141. (const :tag "Force recording the DONE state" time)
  2142. (const :tag "Force recording a note with the DONE state" note)))
  2143. (defgroup org-priorities nil
  2144. "Priorities in Org-mode."
  2145. :tag "Org Priorities"
  2146. :group 'org-todo)
  2147. (defcustom org-enable-priority-commands t
  2148. "Non-nil means priority commands are active.
  2149. When nil, these commands will be disabled, so that you never accidentally
  2150. set a priority."
  2151. :group 'org-priorities
  2152. :type 'boolean)
  2153. (defcustom org-highest-priority ?A
  2154. "The highest priority of TODO items. A character like ?A, ?B etc.
  2155. Must have a smaller ASCII number than `org-lowest-priority'."
  2156. :group 'org-priorities
  2157. :type 'character)
  2158. (defcustom org-lowest-priority ?C
  2159. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2160. Must have a larger ASCII number than `org-highest-priority'."
  2161. :group 'org-priorities
  2162. :type 'character)
  2163. (defcustom org-default-priority ?B
  2164. "The default priority of TODO items.
  2165. This is the priority an item get if no explicit priority is given."
  2166. :group 'org-priorities
  2167. :type 'character)
  2168. (defcustom org-priority-start-cycle-with-default t
  2169. "Non-nil means start with default priority when starting to cycle.
  2170. When this is nil, the first step in the cycle will be (depending on the
  2171. command used) one higher or lower that the default priority."
  2172. :group 'org-priorities
  2173. :type 'boolean)
  2174. (defcustom org-get-priority-function nil
  2175. "Function to extract the priority from a string.
  2176. The string is normally the headline. If this is nil Org computes the
  2177. priority from the priority cookie like [#A] in the headline. It returns
  2178. an integer, increasing by 1000 for each priority level.
  2179. The user can set a different function here, which should take a string
  2180. as an argument and return the numeric priority."
  2181. :group 'org-priorities
  2182. :type 'function)
  2183. (defgroup org-time nil
  2184. "Options concerning time stamps and deadlines in Org-mode."
  2185. :tag "Org Time"
  2186. :group 'org)
  2187. (defcustom org-insert-labeled-timestamps-at-point nil
  2188. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2189. When nil, these labeled time stamps are forces into the second line of an
  2190. entry, just after the headline. When scheduling from the global TODO list,
  2191. the time stamp will always be forced into the second line."
  2192. :group 'org-time
  2193. :type 'boolean)
  2194. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2195. "Formats for `format-time-string' which are used for time stamps.
  2196. It is not recommended to change this constant.")
  2197. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2198. "Number of minutes to round time stamps to.
  2199. These are two values, the first applies when first creating a time stamp.
  2200. The second applies when changing it with the commands `S-up' and `S-down'.
  2201. When changing the time stamp, this means that it will change in steps
  2202. of N minutes, as given by the second value.
  2203. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2204. numbers should be factors of 60, so for example 5, 10, 15.
  2205. When this is larger than 1, you can still force an exact time stamp by using
  2206. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2207. and by using a prefix arg to `S-up/down' to specify the exact number
  2208. of minutes to shift."
  2209. :group 'org-time
  2210. :get '(lambda (var) ; Make sure both elements are there
  2211. (if (integerp (default-value var))
  2212. (list (default-value var) 5)
  2213. (default-value var)))
  2214. :type '(list
  2215. (integer :tag "when inserting times")
  2216. (integer :tag "when modifying times")))
  2217. ;; Normalize old customizations of this variable.
  2218. (when (integerp org-time-stamp-rounding-minutes)
  2219. (setq org-time-stamp-rounding-minutes
  2220. (list org-time-stamp-rounding-minutes
  2221. org-time-stamp-rounding-minutes)))
  2222. (defcustom org-display-custom-times nil
  2223. "Non-nil means overlay custom formats over all time stamps.
  2224. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2225. To turn this on on a per-file basis, insert anywhere in the file:
  2226. #+STARTUP: customtime"
  2227. :group 'org-time
  2228. :set 'set-default
  2229. :type 'sexp)
  2230. (make-variable-buffer-local 'org-display-custom-times)
  2231. (defcustom org-time-stamp-custom-formats
  2232. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2233. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2234. These are overlayed over the default ISO format if the variable
  2235. `org-display-custom-times' is set. Time like %H:%M should be at the
  2236. end of the second format. The custom formats are also honored by export
  2237. commands, if custom time display is turned on at the time of export."
  2238. :group 'org-time
  2239. :type 'sexp)
  2240. (defun org-time-stamp-format (&optional long inactive)
  2241. "Get the right format for a time string."
  2242. (let ((f (if long (cdr org-time-stamp-formats)
  2243. (car org-time-stamp-formats))))
  2244. (if inactive
  2245. (concat "[" (substring f 1 -1) "]")
  2246. f)))
  2247. (defcustom org-time-clocksum-format "%d:%02d"
  2248. "The format string used when creating CLOCKSUM lines.
  2249. This is also used when org-mode generates a time duration."
  2250. :group 'org-time
  2251. :type 'string)
  2252. (defcustom org-time-clocksum-use-fractional nil
  2253. "If non-nil, \\[org-clock-display] uses fractional times.
  2254. org-mode generates a time duration."
  2255. :group 'org-time
  2256. :type 'boolean)
  2257. (defcustom org-time-clocksum-fractional-format "%.2f"
  2258. "The format string used when creating CLOCKSUM lines, or when
  2259. org-mode generates a time duration."
  2260. :group 'org-time
  2261. :type 'string)
  2262. (defcustom org-deadline-warning-days 14
  2263. "No. of days before expiration during which a deadline becomes active.
  2264. This variable governs the display in sparse trees and in the agenda.
  2265. When 0 or negative, it means use this number (the absolute value of it)
  2266. even if a deadline has a different individual lead time specified.
  2267. Custom commands can set this variable in the options section."
  2268. :group 'org-time
  2269. :group 'org-agenda-daily/weekly
  2270. :type 'integer)
  2271. (defcustom org-read-date-prefer-future t
  2272. "Non-nil means assume future for incomplete date input from user.
  2273. This affects the following situations:
  2274. 1. The user gives a month but not a year.
  2275. For example, if it is April and you enter \"feb 2\", this will be read
  2276. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2277. 2. The user gives a day, but no month.
  2278. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2279. read this as the third of *next* month. However, if you enter \"17\",
  2280. it will be considered as *this* month.
  2281. If you set this variable to the symbol `time', then also the following
  2282. will work:
  2283. 3. If the user gives a time, but no day. If the time is before now,
  2284. to will be interpreted as tomorrow.
  2285. Currently none of this works for ISO week specifications.
  2286. When this option is nil, the current day, month and year will always be
  2287. used as defaults.
  2288. See also `org-agenda-jump-prefer-future'."
  2289. :group 'org-time
  2290. :type '(choice
  2291. (const :tag "Never" nil)
  2292. (const :tag "Check month and day" t)
  2293. (const :tag "Check month, day, and time" time)))
  2294. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2295. "Should the agenda jump command prefer the future for incomplete dates?
  2296. The default is to do the same as configured in `org-read-date-prefer-future'.
  2297. But you can also set a deviating value here.
  2298. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2299. :group 'org-agenda
  2300. :group 'org-time
  2301. :type '(choice
  2302. (const :tag "Use org-read-date-prefer-future"
  2303. org-read-date-prefer-future)
  2304. (const :tag "Never" nil)
  2305. (const :tag "Always" t)))
  2306. (defcustom org-read-date-force-compatible-dates t
  2307. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2308. Depending on the system Emacs is running on, certain dates cannot
  2309. be represented with the type used internally to represent time.
  2310. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2311. correctly. Some systems allow for earlier dates, some for later,
  2312. some for both. One way to find out it to insert any date into an
  2313. Org buffer, putting the cursor on the year and hitting S-up and
  2314. S-down to test the range.
  2315. When this variable is set to t, the date/time prompt will not let
  2316. you specify dates outside the 1970-2037 range, so it is certain that
  2317. these dates will work in whatever version of Emacs you are
  2318. running, and also that you can move a file from one Emacs implementation
  2319. to another. WHenever Org is forcing the year for you, it will display
  2320. a message and beep.
  2321. When this variable is nil, Org will check if the date is
  2322. representable in the specific Emacs implementation you are using.
  2323. If not, it will force a year, usually the current year, and beep
  2324. to remind you. Currently this setting is not recommended because
  2325. the likelihood that you will open your Org files in an Emacs that
  2326. has limited date range is not negligible.
  2327. A workaround for this problem is to use diary sexp dates for time
  2328. stamps outside of this range."
  2329. :group 'org-time
  2330. :type 'boolean)
  2331. (defcustom org-read-date-display-live t
  2332. "Non-nil means display current interpretation of date prompt live.
  2333. This display will be in an overlay, in the minibuffer."
  2334. :group 'org-time
  2335. :type 'boolean)
  2336. (defcustom org-read-date-popup-calendar t
  2337. "Non-nil means pop up a calendar when prompting for a date.
  2338. In the calendar, the date can be selected with mouse-1. However, the
  2339. minibuffer will also be active, and you can simply enter the date as well.
  2340. When nil, only the minibuffer will be available."
  2341. :group 'org-time
  2342. :type 'boolean)
  2343. (if (fboundp 'defvaralias)
  2344. (defvaralias 'org-popup-calendar-for-date-prompt
  2345. 'org-read-date-popup-calendar))
  2346. (defcustom org-read-date-minibuffer-setup-hook nil
  2347. "Hook to be used to set up keys for the date/time interface.
  2348. Add key definitions to `minibuffer-local-map', which will be a temporary
  2349. copy."
  2350. :group 'org-time
  2351. :type 'hook)
  2352. (defcustom org-extend-today-until 0
  2353. "The hour when your day really ends. Must be an integer.
  2354. This has influence for the following applications:
  2355. - When switching the agenda to \"today\". It it is still earlier than
  2356. the time given here, the day recognized as TODAY is actually yesterday.
  2357. - When a date is read from the user and it is still before the time given
  2358. here, the current date and time will be assumed to be yesterday, 23:59.
  2359. Also, timestamps inserted in remember templates follow this rule.
  2360. IMPORTANT: This is a feature whose implementation is and likely will
  2361. remain incomplete. Really, it is only here because past midnight seems to
  2362. be the favorite working time of John Wiegley :-)"
  2363. :group 'org-time
  2364. :type 'integer)
  2365. (defcustom org-edit-timestamp-down-means-later nil
  2366. "Non-nil means S-down will increase the time in a time stamp.
  2367. When nil, S-up will increase."
  2368. :group 'org-time
  2369. :type 'boolean)
  2370. (defcustom org-calendar-follow-timestamp-change t
  2371. "Non-nil means make the calendar window follow timestamp changes.
  2372. When a timestamp is modified and the calendar window is visible, it will be
  2373. moved to the new date."
  2374. :group 'org-time
  2375. :type 'boolean)
  2376. (defgroup org-tags nil
  2377. "Options concerning tags in Org-mode."
  2378. :tag "Org Tags"
  2379. :group 'org)
  2380. (defcustom org-tag-alist nil
  2381. "List of tags allowed in Org-mode files.
  2382. When this list is nil, Org-mode will base TAG input on what is already in the
  2383. buffer.
  2384. The value of this variable is an alist, the car of each entry must be a
  2385. keyword as a string, the cdr may be a character that is used to select
  2386. that tag through the fast-tag-selection interface.
  2387. See the manual for details."
  2388. :group 'org-tags
  2389. :type '(repeat
  2390. (choice
  2391. (cons (string :tag "Tag name")
  2392. (character :tag "Access char"))
  2393. (list :tag "Start radio group"
  2394. (const :startgroup)
  2395. (option (string :tag "Group description")))
  2396. (list :tag "End radio group"
  2397. (const :endgroup)
  2398. (option (string :tag "Group description")))
  2399. (const :tag "New line" (:newline)))))
  2400. (defcustom org-tag-persistent-alist nil
  2401. "List of tags that will always appear in all Org-mode files.
  2402. This is in addition to any in buffer settings or customizations
  2403. of `org-tag-alist'.
  2404. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2405. The value of this variable is an alist, the car of each entry must be a
  2406. keyword as a string, the cdr may be a character that is used to select
  2407. that tag through the fast-tag-selection interface.
  2408. See the manual for details.
  2409. To disable these tags on a per-file basis, insert anywhere in the file:
  2410. #+STARTUP: noptag"
  2411. :group 'org-tags
  2412. :type '(repeat
  2413. (choice
  2414. (cons (string :tag "Tag name")
  2415. (character :tag "Access char"))
  2416. (const :tag "Start radio group" (:startgroup))
  2417. (const :tag "End radio group" (:endgroup))
  2418. (const :tag "New line" (:newline)))))
  2419. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2420. "If non-nil, always offer completion for all tags of all agenda files.
  2421. Instead of customizing this variable directly, you might want to
  2422. set it locally for capture buffers, because there no list of
  2423. tags in that file can be created dynamically (there are none).
  2424. (add-hook 'org-capture-mode-hook
  2425. (lambda ()
  2426. (set (make-local-variable
  2427. 'org-complete-tags-always-offer-all-agenda-tags)
  2428. t)))"
  2429. :group 'org-tags
  2430. :type 'boolean)
  2431. (defvar org-file-tags nil
  2432. "List of tags that can be inherited by all entries in the file.
  2433. The tags will be inherited if the variable `org-use-tag-inheritance'
  2434. says they should be.
  2435. This variable is populated from #+FILETAGS lines.")
  2436. (defcustom org-use-fast-tag-selection 'auto
  2437. "Non-nil means use fast tag selection scheme.
  2438. This is a special interface to select and deselect tags with single keys.
  2439. When nil, fast selection is never used.
  2440. When the symbol `auto', fast selection is used if and only if selection
  2441. characters for tags have been configured, either through the variable
  2442. `org-tag-alist' or through a #+TAGS line in the buffer.
  2443. When t, fast selection is always used and selection keys are assigned
  2444. automatically if necessary."
  2445. :group 'org-tags
  2446. :type '(choice
  2447. (const :tag "Always" t)
  2448. (const :tag "Never" nil)
  2449. (const :tag "When selection characters are configured" 'auto)))
  2450. (defcustom org-fast-tag-selection-single-key nil
  2451. "Non-nil means fast tag selection exits after first change.
  2452. When nil, you have to press RET to exit it.
  2453. During fast tag selection, you can toggle this flag with `C-c'.
  2454. This variable can also have the value `expert'. In this case, the window
  2455. displaying the tags menu is not even shown, until you press C-c again."
  2456. :group 'org-tags
  2457. :type '(choice
  2458. (const :tag "No" nil)
  2459. (const :tag "Yes" t)
  2460. (const :tag "Expert" expert)))
  2461. (defvar org-fast-tag-selection-include-todo nil
  2462. "Non-nil means fast tags selection interface will also offer TODO states.
  2463. This is an undocumented feature, you should not rely on it.")
  2464. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2465. "The column to which tags should be indented in a headline.
  2466. If this number is positive, it specifies the column. If it is negative,
  2467. it means that the tags should be flushright to that column. For example,
  2468. -80 works well for a normal 80 character screen."
  2469. :group 'org-tags
  2470. :type 'integer)
  2471. (defcustom org-auto-align-tags t
  2472. "Non-nil means realign tags after pro/demotion of TODO state change.
  2473. These operations change the length of a headline and therefore shift
  2474. the tags around. With this options turned on, after each such operation
  2475. the tags are again aligned to `org-tags-column'."
  2476. :group 'org-tags
  2477. :type 'boolean)
  2478. (defcustom org-use-tag-inheritance t
  2479. "Non-nil means tags in levels apply also for sublevels.
  2480. When nil, only the tags directly given in a specific line apply there.
  2481. This may also be a list of tags that should be inherited, or a regexp that
  2482. matches tags that should be inherited. Additional control is possible
  2483. with the variable `org-tags-exclude-from-inheritance' which gives an
  2484. explicit list of tags to be excluded from inheritance., even if the value of
  2485. `org-use-tag-inheritance' would select it for inheritance.
  2486. If this option is t, a match early-on in a tree can lead to a large
  2487. number of matches in the subtree when constructing the agenda or creating
  2488. a sparse tree. If you only want to see the first match in a tree during
  2489. a search, check out the variable `org-tags-match-list-sublevels'."
  2490. :group 'org-tags
  2491. :type '(choice
  2492. (const :tag "Not" nil)
  2493. (const :tag "Always" t)
  2494. (repeat :tag "Specific tags" (string :tag "Tag"))
  2495. (regexp :tag "Tags matched by regexp")))
  2496. (defcustom org-tags-exclude-from-inheritance nil
  2497. "List of tags that should never be inherited.
  2498. This is a way to exclude a few tags from inheritance. For way to do
  2499. the opposite, to actively allow inheritance for selected tags,
  2500. see the variable `org-use-tag-inheritance'."
  2501. :group 'org-tags
  2502. :type '(repeat (string :tag "Tag")))
  2503. (defun org-tag-inherit-p (tag)
  2504. "Check if TAG is one that should be inherited."
  2505. (cond
  2506. ((member tag org-tags-exclude-from-inheritance) nil)
  2507. ((eq org-use-tag-inheritance t) t)
  2508. ((not org-use-tag-inheritance) nil)
  2509. ((stringp org-use-tag-inheritance)
  2510. (string-match org-use-tag-inheritance tag))
  2511. ((listp org-use-tag-inheritance)
  2512. (member tag org-use-tag-inheritance))
  2513. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2514. (defcustom org-tags-match-list-sublevels t
  2515. "Non-nil means list also sublevels of headlines matching a search.
  2516. This variable applies to tags/property searches, and also to stuck
  2517. projects because this search is based on a tags match as well.
  2518. When set to the symbol `indented', sublevels are indented with
  2519. leading dots.
  2520. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2521. the sublevels of a headline matching a tag search often also match
  2522. the same search. Listing all of them can create very long lists.
  2523. Setting this variable to nil causes subtrees of a match to be skipped.
  2524. This variable is semi-obsolete and probably should always be true. It
  2525. is better to limit inheritance to certain tags using the variables
  2526. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2527. :group 'org-tags
  2528. :type '(choice
  2529. (const :tag "No, don't list them" nil)
  2530. (const :tag "Yes, do list them" t)
  2531. (const :tag "List them, indented with leading dots" indented)))
  2532. (defcustom org-tags-sort-function nil
  2533. "When set, tags are sorted using this function as a comparator."
  2534. :group 'org-tags
  2535. :type '(choice
  2536. (const :tag "No sorting" nil)
  2537. (const :tag "Alphabetical" string<)
  2538. (const :tag "Reverse alphabetical" string>)
  2539. (function :tag "Custom function" nil)))
  2540. (defvar org-tags-history nil
  2541. "History of minibuffer reads for tags.")
  2542. (defvar org-last-tags-completion-table nil
  2543. "The last used completion table for tags.")
  2544. (defvar org-after-tags-change-hook nil
  2545. "Hook that is run after the tags in a line have changed.")
  2546. (defgroup org-properties nil
  2547. "Options concerning properties in Org-mode."
  2548. :tag "Org Properties"
  2549. :group 'org)
  2550. (defcustom org-property-format "%-10s %s"
  2551. "How property key/value pairs should be formatted by `indent-line'.
  2552. When `indent-line' hits a property definition, it will format the line
  2553. according to this format, mainly to make sure that the values are
  2554. lined-up with respect to each other."
  2555. :group 'org-properties
  2556. :type 'string)
  2557. (defcustom org-use-property-inheritance nil
  2558. "Non-nil means properties apply also for sublevels.
  2559. This setting is chiefly used during property searches. Turning it on can
  2560. cause significant overhead when doing a search, which is why it is not
  2561. on by default.
  2562. When nil, only the properties directly given in the current entry count.
  2563. When t, every property is inherited. The value may also be a list of
  2564. properties that should have inheritance, or a regular expression matching
  2565. properties that should be inherited.
  2566. However, note that some special properties use inheritance under special
  2567. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2568. and the properties ending in \"_ALL\" when they are used as descriptor
  2569. for valid values of a property.
  2570. Note for programmers:
  2571. When querying an entry with `org-entry-get', you can control if inheritance
  2572. should be used. By default, `org-entry-get' looks only at the local
  2573. properties. You can request inheritance by setting the inherit argument
  2574. to t (to force inheritance) or to `selective' (to respect the setting
  2575. in this variable)."
  2576. :group 'org-properties
  2577. :type '(choice
  2578. (const :tag "Not" nil)
  2579. (const :tag "Always" t)
  2580. (repeat :tag "Specific properties" (string :tag "Property"))
  2581. (regexp :tag "Properties matched by regexp")))
  2582. (defun org-property-inherit-p (property)
  2583. "Check if PROPERTY is one that should be inherited."
  2584. (cond
  2585. ((eq org-use-property-inheritance t) t)
  2586. ((not org-use-property-inheritance) nil)
  2587. ((stringp org-use-property-inheritance)
  2588. (string-match org-use-property-inheritance property))
  2589. ((listp org-use-property-inheritance)
  2590. (member property org-use-property-inheritance))
  2591. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2592. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2593. "The default column format, if no other format has been defined.
  2594. This variable can be set on the per-file basis by inserting a line
  2595. #+COLUMNS: %25ITEM ....."
  2596. :group 'org-properties
  2597. :type 'string)
  2598. (defcustom org-columns-ellipses ".."
  2599. "The ellipses to be used when a field in column view is truncated.
  2600. When this is the empty string, as many characters as possible are shown,
  2601. but then there will be no visual indication that the field has been truncated.
  2602. When this is a string of length N, the last N characters of a truncated
  2603. field are replaced by this string. If the column is narrower than the
  2604. ellipses string, only part of the ellipses string will be shown."
  2605. :group 'org-properties
  2606. :type 'string)
  2607. (defcustom org-columns-modify-value-for-display-function nil
  2608. "Function that modifies values for display in column view.
  2609. For example, it can be used to cut out a certain part from a time stamp.
  2610. The function must take 2 arguments:
  2611. column-title The title of the column (*not* the property name)
  2612. value The value that should be modified.
  2613. The function should return the value that should be displayed,
  2614. or nil if the normal value should be used."
  2615. :group 'org-properties
  2616. :type 'function)
  2617. (defcustom org-effort-property "Effort"
  2618. "The property that is being used to keep track of effort estimates.
  2619. Effort estimates given in this property need to have the format H:MM."
  2620. :group 'org-properties
  2621. :group 'org-progress
  2622. :type '(string :tag "Property"))
  2623. (defconst org-global-properties-fixed
  2624. '(("VISIBILITY_ALL" . "folded children content all")
  2625. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2626. "List of property/value pairs that can be inherited by any entry.
  2627. These are fixed values, for the preset properties. The user variable
  2628. that can be used to add to this list is `org-global-properties'.
  2629. The entries in this list are cons cells where the car is a property
  2630. name and cdr is a string with the value. If the value represents
  2631. multiple items like an \"_ALL\" property, separate the items by
  2632. spaces.")
  2633. (defcustom org-global-properties nil
  2634. "List of property/value pairs that can be inherited by any entry.
  2635. This list will be combined with the constant `org-global-properties-fixed'.
  2636. The entries in this list are cons cells where the car is a property
  2637. name and cdr is a string with the value.
  2638. You can set buffer-local values for the same purpose in the variable
  2639. `org-file-properties' this by adding lines like
  2640. #+PROPERTY: NAME VALUE"
  2641. :group 'org-properties
  2642. :type '(repeat
  2643. (cons (string :tag "Property")
  2644. (string :tag "Value"))))
  2645. (defvar org-file-properties nil
  2646. "List of property/value pairs that can be inherited by any entry.
  2647. Valid for the current buffer.
  2648. This variable is populated from #+PROPERTY lines.")
  2649. (make-variable-buffer-local 'org-file-properties)
  2650. (defgroup org-agenda nil
  2651. "Options concerning agenda views in Org-mode."
  2652. :tag "Org Agenda"
  2653. :group 'org)
  2654. (defvar org-category nil
  2655. "Variable used by org files to set a category for agenda display.
  2656. Such files should use a file variable to set it, for example
  2657. # -*- mode: org; org-category: \"ELisp\"
  2658. or contain a special line
  2659. #+CATEGORY: ELisp
  2660. If the file does not specify a category, then file's base name
  2661. is used instead.")
  2662. (make-variable-buffer-local 'org-category)
  2663. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2664. (defcustom org-agenda-files nil
  2665. "The files to be used for agenda display.
  2666. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2667. \\[org-remove-file]. You can also use customize to edit the list.
  2668. If an entry is a directory, all files in that directory that are matched by
  2669. `org-agenda-file-regexp' will be part of the file list.
  2670. If the value of the variable is not a list but a single file name, then
  2671. the list of agenda files is actually stored and maintained in that file, one
  2672. agenda file per line. In this file paths can be given relative to
  2673. `org-directory'. Tilde expansion and environment variable substitution
  2674. are also made."
  2675. :group 'org-agenda
  2676. :type '(choice
  2677. (repeat :tag "List of files and directories" file)
  2678. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2679. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2680. "Regular expression to match files for `org-agenda-files'.
  2681. If any element in the list in that variable contains a directory instead
  2682. of a normal file, all files in that directory that are matched by this
  2683. regular expression will be included."
  2684. :group 'org-agenda
  2685. :type 'regexp)
  2686. (defcustom org-agenda-text-search-extra-files nil
  2687. "List of extra files to be searched by text search commands.
  2688. These files will be search in addition to the agenda files by the
  2689. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2690. Note that these files will only be searched for text search commands,
  2691. not for the other agenda views like todo lists, tag searches or the weekly
  2692. agenda. This variable is intended to list notes and possibly archive files
  2693. that should also be searched by these two commands.
  2694. In fact, if the first element in the list is the symbol `agenda-archives',
  2695. than all archive files of all agenda files will be added to the search
  2696. scope."
  2697. :group 'org-agenda
  2698. :type '(set :greedy t
  2699. (const :tag "Agenda Archives" agenda-archives)
  2700. (repeat :inline t (file))))
  2701. (if (fboundp 'defvaralias)
  2702. (defvaralias 'org-agenda-multi-occur-extra-files
  2703. 'org-agenda-text-search-extra-files))
  2704. (defcustom org-agenda-skip-unavailable-files nil
  2705. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2706. A nil value means to remove them, after a query, from the list."
  2707. :group 'org-agenda
  2708. :type 'boolean)
  2709. (defcustom org-calendar-to-agenda-key [?c]
  2710. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2711. The command `org-calendar-goto-agenda' will be bound to this key. The
  2712. default is the character `c' because then `c' can be used to switch back and
  2713. forth between agenda and calendar."
  2714. :group 'org-agenda
  2715. :type 'sexp)
  2716. (defcustom org-calendar-agenda-action-key [?k]
  2717. "The key to be installed in `calendar-mode-map' for agenda-action.
  2718. The command `org-agenda-action' will be bound to this key. The
  2719. default is the character `k' because we use the same key in the agenda."
  2720. :group 'org-agenda
  2721. :type 'sexp)
  2722. (defcustom org-calendar-insert-diary-entry-key [?i]
  2723. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2724. This option is irrelevant until `org-agenda-diary-file' has been configured
  2725. to point to an Org-mode file. When that is the case, the command
  2726. `org-agenda-diary-entry' will be bound to the key given here, by default
  2727. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2728. if you want to continue doing this, you need to change this to a different
  2729. key."
  2730. :group 'org-agenda
  2731. :type 'sexp)
  2732. (defcustom org-agenda-diary-file 'diary-file
  2733. "File to which to add new entries with the `i' key in agenda and calendar.
  2734. When this is the symbol `diary-file', the functionality in the Emacs
  2735. calendar will be used to add entries to the `diary-file'. But when this
  2736. points to a file, `org-agenda-diary-entry' will be used instead."
  2737. :group 'org-agenda
  2738. :type '(choice
  2739. (const :tag "The standard Emacs diary file" diary-file)
  2740. (file :tag "Special Org file diary entries")))
  2741. (eval-after-load "calendar"
  2742. '(progn
  2743. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2744. 'org-calendar-goto-agenda)
  2745. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2746. 'org-agenda-action)
  2747. (add-hook 'calendar-mode-hook
  2748. (lambda ()
  2749. (unless (eq org-agenda-diary-file 'diary-file)
  2750. (define-key calendar-mode-map
  2751. org-calendar-insert-diary-entry-key
  2752. 'org-agenda-diary-entry))))))
  2753. (defgroup org-latex nil
  2754. "Options for embedding LaTeX code into Org-mode."
  2755. :tag "Org LaTeX"
  2756. :group 'org)
  2757. (defcustom org-format-latex-options
  2758. '(:foreground default :background default :scale 1.0
  2759. :html-foreground "Black" :html-background "Transparent"
  2760. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2761. "Options for creating images from LaTeX fragments.
  2762. This is a property list with the following properties:
  2763. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2764. `default' means use the foreground of the default face.
  2765. :background the background color, or \"Transparent\".
  2766. `default' means use the background of the default face.
  2767. :scale a scaling factor for the size of the images, to get more pixels
  2768. :html-foreground, :html-background, :html-scale
  2769. the same numbers for HTML export.
  2770. :matchers a list indicating which matchers should be used to
  2771. find LaTeX fragments. Valid members of this list are:
  2772. \"begin\" find environments
  2773. \"$1\" find single characters surrounded by $.$
  2774. \"$\" find math expressions surrounded by $...$
  2775. \"$$\" find math expressions surrounded by $$....$$
  2776. \"\\(\" find math expressions surrounded by \\(...\\)
  2777. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2778. :group 'org-latex
  2779. :type 'plist)
  2780. (defcustom org-format-latex-signal-error t
  2781. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2782. When nil, just push out a message."
  2783. :group 'org-latex
  2784. :type 'boolean)
  2785. (defcustom org-format-latex-header "\\documentclass{article}
  2786. \\usepackage[usenames]{color}
  2787. \\usepackage{amsmath}
  2788. \\usepackage[mathscr]{eucal}
  2789. \\pagestyle{empty} % do not remove
  2790. \[PACKAGES]
  2791. \[DEFAULT-PACKAGES]
  2792. % The settings below are copied from fullpage.sty
  2793. \\setlength{\\textwidth}{\\paperwidth}
  2794. \\addtolength{\\textwidth}{-3cm}
  2795. \\setlength{\\oddsidemargin}{1.5cm}
  2796. \\addtolength{\\oddsidemargin}{-2.54cm}
  2797. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2798. \\setlength{\\textheight}{\\paperheight}
  2799. \\addtolength{\\textheight}{-\\headheight}
  2800. \\addtolength{\\textheight}{-\\headsep}
  2801. \\addtolength{\\textheight}{-\\footskip}
  2802. \\addtolength{\\textheight}{-3cm}
  2803. \\setlength{\\topmargin}{1.5cm}
  2804. \\addtolength{\\topmargin}{-2.54cm}"
  2805. "The document header used for processing LaTeX fragments.
  2806. It is imperative that this header make sure that no page number
  2807. appears on the page. The package defined in the variables
  2808. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2809. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2810. will be appended."
  2811. :group 'org-latex
  2812. :type 'string)
  2813. (defvar org-format-latex-header-extra nil)
  2814. (defun org-set-packages-alist (var val)
  2815. "Set the packages alist and make sure it has 3 elements per entry."
  2816. (set var (mapcar (lambda (x)
  2817. (if (and (consp x) (= (length x) 2))
  2818. (list (car x) (nth 1 x) t)
  2819. x))
  2820. val)))
  2821. (defun org-get-packages-alist (var)
  2822. "Get the packages alist and make sure it has 3 elements per entry."
  2823. (mapcar (lambda (x)
  2824. (if (and (consp x) (= (length x) 2))
  2825. (list (car x) (nth 1 x) t)
  2826. x))
  2827. (default-value var)))
  2828. ;; The following variables are defined here because is it also used
  2829. ;; when formatting latex fragments. Originally it was part of the
  2830. ;; LaTeX exporter, which is why the name includes "export".
  2831. (defcustom org-export-latex-default-packages-alist
  2832. '(("AUTO" "inputenc" t)
  2833. ("T1" "fontenc" t)
  2834. ("" "fixltx2e" nil)
  2835. ("" "graphicx" t)
  2836. ("" "longtable" nil)
  2837. ("" "float" nil)
  2838. ("" "wrapfig" nil)
  2839. ("" "soul" t)
  2840. ("" "textcomp" t)
  2841. ("" "marvosym" t)
  2842. ("" "wasysym" t)
  2843. ("" "latexsym" t)
  2844. ("" "amssymb" t)
  2845. ("" "hyperref" nil)
  2846. "\\tolerance=1000"
  2847. )
  2848. "Alist of default packages to be inserted in the header.
  2849. Change this only if one of the packages here causes an incompatibility
  2850. with another package you are using.
  2851. The packages in this list are needed by one part or another of Org-mode
  2852. to function properly.
  2853. - inputenc, fontenc: for basic font and character selection
  2854. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2855. for interpreting the entities in `org-entities'. You can skip some of these
  2856. packages if you don't use any of the symbols in it.
  2857. - graphicx: for including images
  2858. - float, wrapfig: for figure placement
  2859. - longtable: for long tables
  2860. - hyperref: for cross references
  2861. Therefore you should not modify this variable unless you know what you
  2862. are doing. The one reason to change it anyway is that you might be loading
  2863. some other package that conflicts with one of the default packages.
  2864. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2865. If SNIPPET-FLAG is t, the package also needs to be included when
  2866. compiling LaTeX snippets into images for inclusion into HTML."
  2867. :group 'org-export-latex
  2868. :set 'org-set-packages-alist
  2869. :get 'org-get-packages-alist
  2870. :type '(repeat
  2871. (choice
  2872. (list :tag "options/package pair"
  2873. (string :tag "options")
  2874. (string :tag "package")
  2875. (boolean :tag "Snippet"))
  2876. (string :tag "A line of LaTeX"))))
  2877. (defcustom org-export-latex-packages-alist nil
  2878. "Alist of packages to be inserted in every LaTeX header.
  2879. These will be inserted after `org-export-latex-default-packages-alist'.
  2880. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2881. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2882. turning LaTeX snippets into images for inclusion into HTML.
  2883. Make sure that you only list packages here which:
  2884. - you want in every file
  2885. - do not conflict with the default packages in
  2886. `org-export-latex-default-packages-alist'
  2887. - do not conflict with the setup in `org-format-latex-header'."
  2888. :group 'org-export-latex
  2889. :set 'org-set-packages-alist
  2890. :get 'org-get-packages-alist
  2891. :type '(repeat
  2892. (choice
  2893. (list :tag "options/package pair"
  2894. (string :tag "options")
  2895. (string :tag "package")
  2896. (boolean :tag "Snippet"))
  2897. (string :tag "A line of LaTeX"))))
  2898. (defgroup org-appearance nil
  2899. "Settings for Org-mode appearance."
  2900. :tag "Org Appearance"
  2901. :group 'org)
  2902. (defcustom org-level-color-stars-only nil
  2903. "Non-nil means fontify only the stars in each headline.
  2904. When nil, the entire headline is fontified.
  2905. Changing it requires restart of `font-lock-mode' to become effective
  2906. also in regions already fontified."
  2907. :group 'org-appearance
  2908. :type 'boolean)
  2909. (defcustom org-hide-leading-stars nil
  2910. "Non-nil means hide the first N-1 stars in a headline.
  2911. This works by using the face `org-hide' for these stars. This
  2912. face is white for a light background, and black for a dark
  2913. background. You may have to customize the face `org-hide' to
  2914. make this work.
  2915. Changing it requires restart of `font-lock-mode' to become effective
  2916. also in regions already fontified.
  2917. You may also set this on a per-file basis by adding one of the following
  2918. lines to the buffer:
  2919. #+STARTUP: hidestars
  2920. #+STARTUP: showstars"
  2921. :group 'org-appearance
  2922. :type 'boolean)
  2923. (defcustom org-hidden-keywords nil
  2924. "List of symbols corresponding to keywords to be hidden the org buffer.
  2925. For example, a value '(title) for this list will make the document's title
  2926. appear in the buffer without the initial #+TITLE: keyword."
  2927. :group 'org-appearance
  2928. :type '(set (const :tag "#+AUTHOR" author)
  2929. (const :tag "#+DATE" date)
  2930. (const :tag "#+EMAIL" email)
  2931. (const :tag "#+TITLE" title)))
  2932. (defcustom org-fontify-done-headline nil
  2933. "Non-nil means change the face of a headline if it is marked DONE.
  2934. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2935. When this is non-nil, the headline after the keyword is set to the
  2936. `org-headline-done' as an additional indication."
  2937. :group 'org-appearance
  2938. :type 'boolean)
  2939. (defcustom org-fontify-emphasized-text t
  2940. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2941. Changing this variable requires a restart of Emacs to take effect."
  2942. :group 'org-appearance
  2943. :type 'boolean)
  2944. (defcustom org-fontify-whole-heading-line nil
  2945. "Non-nil means fontify the whole line for headings.
  2946. This is useful when setting a background color for the
  2947. org-level-* faces."
  2948. :group 'org-appearance
  2949. :type 'boolean)
  2950. (defcustom org-highlight-latex-fragments-and-specials nil
  2951. "Non-nil means fontify what is treated specially by the exporters."
  2952. :group 'org-appearance
  2953. :type 'boolean)
  2954. (defcustom org-hide-emphasis-markers nil
  2955. "Non-nil mean font-lock should hide the emphasis marker characters."
  2956. :group 'org-appearance
  2957. :type 'boolean)
  2958. (defcustom org-pretty-entities nil
  2959. "Non-nil means show entities as UTF8 characters.
  2960. When nil, the \\name form remains in the buffer."
  2961. :group 'org-appearance
  2962. :type 'boolean)
  2963. (defcustom org-pretty-entities-include-sub-superscripts t
  2964. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2965. :group 'org-appearance
  2966. :type 'boolean)
  2967. (defvar org-emph-re nil
  2968. "Regular expression for matching emphasis.
  2969. After a match, the match groups contain these elements:
  2970. 0 The match of the full regular expression, including the characters
  2971. before and after the proper match
  2972. 1 The character before the proper match, or empty at beginning of line
  2973. 2 The proper match, including the leading and trailing markers
  2974. 3 The leading marker like * or /, indicating the type of highlighting
  2975. 4 The text between the emphasis markers, not including the markers
  2976. 5 The character after the match, empty at the end of a line")
  2977. (defvar org-verbatim-re nil
  2978. "Regular expression for matching verbatim text.")
  2979. (defvar org-emphasis-regexp-components) ; defined just below
  2980. (defvar org-emphasis-alist) ; defined just below
  2981. (defun org-set-emph-re (var val)
  2982. "Set variable and compute the emphasis regular expression."
  2983. (set var val)
  2984. (when (and (boundp 'org-emphasis-alist)
  2985. (boundp 'org-emphasis-regexp-components)
  2986. org-emphasis-alist org-emphasis-regexp-components)
  2987. (let* ((e org-emphasis-regexp-components)
  2988. (pre (car e))
  2989. (post (nth 1 e))
  2990. (border (nth 2 e))
  2991. (body (nth 3 e))
  2992. (nl (nth 4 e))
  2993. (body1 (concat body "*?"))
  2994. (markers (mapconcat 'car org-emphasis-alist ""))
  2995. (vmarkers (mapconcat
  2996. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2997. org-emphasis-alist "")))
  2998. ;; make sure special characters appear at the right position in the class
  2999. (if (string-match "\\^" markers)
  3000. (setq markers (concat (replace-match "" t t markers) "^")))
  3001. (if (string-match "-" markers)
  3002. (setq markers (concat (replace-match "" t t markers) "-")))
  3003. (if (string-match "\\^" vmarkers)
  3004. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3005. (if (string-match "-" vmarkers)
  3006. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3007. (if (> nl 0)
  3008. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3009. (int-to-string nl) "\\}")))
  3010. ;; Make the regexp
  3011. (setq org-emph-re
  3012. (concat "\\([" pre "]\\|^\\)"
  3013. "\\("
  3014. "\\([" markers "]\\)"
  3015. "\\("
  3016. "[^" border "]\\|"
  3017. "[^" border "]"
  3018. body1
  3019. "[^" border "]"
  3020. "\\)"
  3021. "\\3\\)"
  3022. "\\([" post "]\\|$\\)"))
  3023. (setq org-verbatim-re
  3024. (concat "\\([" pre "]\\|^\\)"
  3025. "\\("
  3026. "\\([" vmarkers "]\\)"
  3027. "\\("
  3028. "[^" border "]\\|"
  3029. "[^" border "]"
  3030. body1
  3031. "[^" border "]"
  3032. "\\)"
  3033. "\\3\\)"
  3034. "\\([" post "]\\|$\\)")))))
  3035. (defcustom org-emphasis-regexp-components
  3036. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3037. "Components used to build the regular expression for emphasis.
  3038. This is a list with five entries. Terminology: In an emphasis string
  3039. like \" *strong word* \", we call the initial space PREMATCH, the final
  3040. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3041. and \"trong wor\" is the body. The different components in this variable
  3042. specify what is allowed/forbidden in each part:
  3043. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3044. post Chars allowed as postmatch. End of line will be allowed too.
  3045. border The chars *forbidden* as border characters.
  3046. body-regexp A regexp like \".\" to match a body character. Don't use
  3047. non-shy groups here, and don't allow newline here.
  3048. newline The maximum number of newlines allowed in an emphasis exp.
  3049. Use customize to modify this, or restart Emacs after changing it."
  3050. :group 'org-appearance
  3051. :set 'org-set-emph-re
  3052. :type '(list
  3053. (sexp :tag "Allowed chars in pre ")
  3054. (sexp :tag "Allowed chars in post ")
  3055. (sexp :tag "Forbidden chars in border ")
  3056. (sexp :tag "Regexp for body ")
  3057. (integer :tag "number of newlines allowed")
  3058. (option (boolean :tag "Please ignore this button"))))
  3059. (defcustom org-emphasis-alist
  3060. `(("*" bold "<b>" "</b>")
  3061. ("/" italic "<i>" "</i>")
  3062. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3063. ("=" org-code "<code>" "</code>" verbatim)
  3064. ("~" org-verbatim "<code>" "</code>" verbatim)
  3065. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3066. "<del>" "</del>")
  3067. )
  3068. "Special syntax for emphasized text.
  3069. Text starting and ending with a special character will be emphasized, for
  3070. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3071. characters, the face to be used by font-lock for highlighting in Org-mode
  3072. Emacs buffers, and the HTML tags to be used for this.
  3073. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3074. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3075. Use customize to modify this, or restart Emacs after changing it."
  3076. :group 'org-appearance
  3077. :set 'org-set-emph-re
  3078. :type '(repeat
  3079. (list
  3080. (string :tag "Marker character")
  3081. (choice
  3082. (face :tag "Font-lock-face")
  3083. (plist :tag "Face property list"))
  3084. (string :tag "HTML start tag")
  3085. (string :tag "HTML end tag")
  3086. (option (const verbatim)))))
  3087. (defvar org-protecting-blocks
  3088. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3089. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3090. This is needed for font-lock setup.")
  3091. ;;; Miscellaneous options
  3092. (defgroup org-completion nil
  3093. "Completion in Org-mode."
  3094. :tag "Org Completion"
  3095. :group 'org)
  3096. (defcustom org-completion-use-ido nil
  3097. "Non-nil means use ido completion wherever possible.
  3098. Note that `ido-mode' must be active for this variable to be relevant.
  3099. If you decide to turn this variable on, you might well want to turn off
  3100. `org-outline-path-complete-in-steps'.
  3101. See also `org-completion-use-iswitchb'."
  3102. :group 'org-completion
  3103. :type 'boolean)
  3104. (defcustom org-completion-use-iswitchb nil
  3105. "Non-nil means use iswitchb completion wherever possible.
  3106. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3107. If you decide to turn this variable on, you might well want to turn off
  3108. `org-outline-path-complete-in-steps'.
  3109. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3110. :group 'org-completion
  3111. :type 'boolean)
  3112. (defcustom org-completion-fallback-command 'hippie-expand
  3113. "The expansion command called by \\[pcomplete] in normal context.
  3114. Normal means, no org-mode-specific context."
  3115. :group 'org-completion
  3116. :type 'function)
  3117. ;;; Functions and variables from their packages
  3118. ;; Declared here to avoid compiler warnings
  3119. ;; XEmacs only
  3120. (defvar outline-mode-menu-heading)
  3121. (defvar outline-mode-menu-show)
  3122. (defvar outline-mode-menu-hide)
  3123. (defvar zmacs-regions) ; XEmacs regions
  3124. ;; Emacs only
  3125. (defvar mark-active)
  3126. ;; Various packages
  3127. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3128. (declare-function calendar-forward-day "cal-move" (arg))
  3129. (declare-function calendar-goto-date "cal-move" (date))
  3130. (declare-function calendar-goto-today "cal-move" ())
  3131. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3132. (defvar calc-embedded-close-formula)
  3133. (defvar calc-embedded-open-formula)
  3134. (declare-function cdlatex-tab "ext:cdlatex" ())
  3135. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3136. (defvar font-lock-unfontify-region-function)
  3137. (declare-function iswitchb-read-buffer "iswitchb"
  3138. (prompt &optional default require-match start matches-set))
  3139. (defvar iswitchb-temp-buflist)
  3140. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3141. (defvar org-agenda-tags-todo-honor-ignore-options)
  3142. (declare-function org-agenda-skip "org-agenda" ())
  3143. (declare-function
  3144. org-format-agenda-item "org-agenda"
  3145. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3146. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3147. (declare-function org-agenda-change-all-lines "org-agenda"
  3148. (newhead hdmarker &optional fixface just-this))
  3149. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3150. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3151. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3152. (beg end))
  3153. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3154. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3155. "org-agenda" (&optional end))
  3156. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3157. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3158. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3159. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3160. (declare-function org-indent-mode "org-indent" (&optional arg))
  3161. (declare-function parse-time-string "parse-time" (string))
  3162. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3163. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3164. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3165. (defvar remember-data-file)
  3166. (defvar texmathp-why)
  3167. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3168. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3169. (defvar w3m-current-url)
  3170. (defvar w3m-current-title)
  3171. (defvar org-latex-regexps)
  3172. ;;; Autoload and prepare some org modules
  3173. ;; Some table stuff that needs to be defined here, because it is used
  3174. ;; by the functions setting up org-mode or checking for table context.
  3175. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3176. "Detect an org-type or table-type table.")
  3177. (defconst org-table-line-regexp "^[ \t]*|"
  3178. "Detect an org-type table line.")
  3179. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3180. "Detect an org-type table line.")
  3181. (defconst org-table-hline-regexp "^[ \t]*|-"
  3182. "Detect an org-type table hline.")
  3183. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3184. "Detect a table-type table hline.")
  3185. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3186. "Detect the first line outside a table when searching from within it.
  3187. This works for both table types.")
  3188. ;; Autoload the functions in org-table.el that are needed by functions here.
  3189. (eval-and-compile
  3190. (org-autoload "org-table"
  3191. '(org-table-align org-table-begin org-table-blank-field
  3192. org-table-convert org-table-convert-region org-table-copy-down
  3193. org-table-copy-region org-table-create
  3194. org-table-create-or-convert-from-region
  3195. org-table-create-with-table.el org-table-current-dline
  3196. org-table-cut-region org-table-delete-column org-table-edit-field
  3197. org-table-edit-formulas org-table-end org-table-eval-formula
  3198. org-table-export org-table-field-info
  3199. org-table-get-stored-formulas org-table-goto-column
  3200. org-table-hline-and-move org-table-import org-table-insert-column
  3201. org-table-insert-hline org-table-insert-row org-table-iterate
  3202. org-table-justify-field-maybe org-table-kill-row
  3203. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3204. org-table-move-column org-table-move-column-left
  3205. org-table-move-column-right org-table-move-row
  3206. org-table-move-row-down org-table-move-row-up
  3207. org-table-next-field org-table-next-row org-table-paste-rectangle
  3208. org-table-previous-field org-table-recalculate
  3209. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3210. org-table-toggle-coordinate-overlays
  3211. org-table-toggle-formula-debugger org-table-wrap-region
  3212. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3213. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3214. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3215. (defun org-at-table-p (&optional table-type)
  3216. "Return t if the cursor is inside an org-type table.
  3217. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3218. (if org-enable-table-editor
  3219. (save-excursion
  3220. (beginning-of-line 1)
  3221. (looking-at (if table-type org-table-any-line-regexp
  3222. org-table-line-regexp)))
  3223. nil))
  3224. (defsubst org-table-p () (org-at-table-p))
  3225. (defun org-at-table.el-p ()
  3226. "Return t if and only if we are at a table.el table."
  3227. (and (org-at-table-p 'any)
  3228. (save-excursion
  3229. (goto-char (org-table-begin 'any))
  3230. (looking-at org-table1-hline-regexp))))
  3231. (defun org-table-recognize-table.el ()
  3232. "If there is a table.el table nearby, recognize it and move into it."
  3233. (if org-table-tab-recognizes-table.el
  3234. (if (org-at-table.el-p)
  3235. (progn
  3236. (beginning-of-line 1)
  3237. (if (looking-at org-table-dataline-regexp)
  3238. nil
  3239. (if (looking-at org-table1-hline-regexp)
  3240. (progn
  3241. (beginning-of-line 2)
  3242. (if (looking-at org-table-any-border-regexp)
  3243. (beginning-of-line -1)))))
  3244. (if (re-search-forward "|" (org-table-end t) t)
  3245. (progn
  3246. (require 'table)
  3247. (if (table--at-cell-p (point))
  3248. t
  3249. (message "recognizing table.el table...")
  3250. (table-recognize-table)
  3251. (message "recognizing table.el table...done")))
  3252. (error "This should not happen"))
  3253. t)
  3254. nil)
  3255. nil))
  3256. (defun org-at-table-hline-p ()
  3257. "Return t if the cursor is inside a hline in a table."
  3258. (if org-enable-table-editor
  3259. (save-excursion
  3260. (beginning-of-line 1)
  3261. (looking-at org-table-hline-regexp))
  3262. nil))
  3263. (defvar org-table-clean-did-remove-column nil)
  3264. (defun org-table-map-tables (function &optional quietly)
  3265. "Apply FUNCTION to the start of all tables in the buffer."
  3266. (save-excursion
  3267. (save-restriction
  3268. (widen)
  3269. (goto-char (point-min))
  3270. (while (re-search-forward org-table-any-line-regexp nil t)
  3271. (unless quietly
  3272. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3273. (beginning-of-line 1)
  3274. (when (looking-at org-table-line-regexp)
  3275. (save-excursion (funcall function))
  3276. (or (looking-at org-table-line-regexp)
  3277. (forward-char 1)))
  3278. (re-search-forward org-table-any-border-regexp nil 1))))
  3279. (unless quietly (message "Mapping tables: done")))
  3280. ;; Declare and autoload functions from org-exp.el & Co
  3281. (declare-function org-default-export-plist "org-exp")
  3282. (declare-function org-infile-export-plist "org-exp")
  3283. (declare-function org-get-current-options "org-exp")
  3284. (eval-and-compile
  3285. (org-autoload "org-exp"
  3286. '(org-export org-export-visible
  3287. org-insert-export-options-template
  3288. org-table-clean-before-export))
  3289. (org-autoload "org-ascii"
  3290. '(org-export-as-ascii org-export-ascii-preprocess
  3291. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3292. org-export-region-as-ascii))
  3293. (org-autoload "org-latex"
  3294. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3295. org-replace-region-by-latex org-export-region-as-latex
  3296. org-export-as-latex org-export-as-pdf
  3297. org-export-as-pdf-and-open))
  3298. (org-autoload "org-html"
  3299. '(org-export-as-html-and-open
  3300. org-export-as-html-batch org-export-as-html-to-buffer
  3301. org-replace-region-by-html org-export-region-as-html
  3302. org-export-as-html))
  3303. (org-autoload "org-docbook"
  3304. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3305. org-replace-region-by-docbook org-export-region-as-docbook
  3306. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3307. org-export-as-docbook))
  3308. (org-autoload "org-icalendar"
  3309. '(org-export-icalendar-this-file
  3310. org-export-icalendar-all-agenda-files
  3311. org-export-icalendar-combine-agenda-files))
  3312. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3313. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3314. ;; Declare and autoload functions from org-agenda.el
  3315. (eval-and-compile
  3316. (org-autoload "org-agenda"
  3317. '(org-agenda org-agenda-list org-search-view
  3318. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3319. org-diary org-agenda-to-appt
  3320. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3321. ;; Autoload org-remember
  3322. (eval-and-compile
  3323. (org-autoload "org-remember"
  3324. '(org-remember-insinuate org-remember-annotation
  3325. org-remember-apply-template org-remember org-remember-handler)))
  3326. (eval-and-compile
  3327. (org-autoload "org-capture"
  3328. '(org-capture org-capture-insert-template-here
  3329. org-capture-import-remember-templates)))
  3330. ;; Autoload org-clock.el
  3331. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3332. (beg end))
  3333. (declare-function org-clock-update-mode-line "org-clock" ())
  3334. (declare-function org-resolve-clocks "org-clock"
  3335. (&optional also-non-dangling-p prompt last-valid))
  3336. (defvar org-clock-start-time)
  3337. (defvar org-clock-marker (make-marker)
  3338. "Marker recording the last clock-in.")
  3339. (defvar org-clock-hd-marker (make-marker)
  3340. "Marker recording the last clock-in, but the headline position.")
  3341. (defvar org-clock-heading ""
  3342. "The heading of the current clock entry.")
  3343. (defun org-clock-is-active ()
  3344. "Return non-nil if clock is currently running.
  3345. The return value is actually the clock marker."
  3346. (marker-buffer org-clock-marker))
  3347. (eval-and-compile
  3348. (org-autoload
  3349. "org-clock"
  3350. '(org-clock-in org-clock-out org-clock-cancel
  3351. org-clock-goto org-clock-sum org-clock-display
  3352. org-clock-remove-overlays org-clock-report
  3353. org-clocktable-shift org-dblock-write:clocktable
  3354. org-get-clocktable org-resolve-clocks)))
  3355. (defun org-clock-update-time-maybe ()
  3356. "If this is a CLOCK line, update it and return t.
  3357. Otherwise, return nil."
  3358. (interactive)
  3359. (save-excursion
  3360. (beginning-of-line 1)
  3361. (skip-chars-forward " \t")
  3362. (when (looking-at org-clock-string)
  3363. (let ((re (concat "[ \t]*" org-clock-string
  3364. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3365. "\\([ \t]*=>.*\\)?\\)?"))
  3366. ts te h m s neg)
  3367. (cond
  3368. ((not (looking-at re))
  3369. nil)
  3370. ((not (match-end 2))
  3371. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3372. (> org-clock-marker (point))
  3373. (<= org-clock-marker (point-at-eol)))
  3374. ;; The clock is running here
  3375. (setq org-clock-start-time
  3376. (apply 'encode-time
  3377. (org-parse-time-string (match-string 1))))
  3378. (org-clock-update-mode-line)))
  3379. (t
  3380. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3381. (end-of-line 1)
  3382. (setq ts (match-string 1)
  3383. te (match-string 3))
  3384. (setq s (- (org-float-time
  3385. (apply 'encode-time (org-parse-time-string te)))
  3386. (org-float-time
  3387. (apply 'encode-time (org-parse-time-string ts))))
  3388. neg (< s 0)
  3389. s (abs s)
  3390. h (floor (/ s 3600))
  3391. s (- s (* 3600 h))
  3392. m (floor (/ s 60))
  3393. s (- s (* 60 s)))
  3394. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3395. t))))))
  3396. (defun org-check-running-clock ()
  3397. "Check if the current buffer contains the running clock.
  3398. If yes, offer to stop it and to save the buffer with the changes."
  3399. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3400. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3401. (buffer-name))))
  3402. (org-clock-out)
  3403. (when (y-or-n-p "Save changed buffer?")
  3404. (save-buffer))))
  3405. (defun org-clocktable-try-shift (dir n)
  3406. "Check if this line starts a clock table, if yes, shift the time block."
  3407. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3408. (org-clocktable-shift dir n)))
  3409. ;; Autoload org-timer.el
  3410. (eval-and-compile
  3411. (org-autoload
  3412. "org-timer"
  3413. '(org-timer-start org-timer org-timer-item
  3414. org-timer-change-times-in-region
  3415. org-timer-set-timer
  3416. org-timer-reset-timers
  3417. org-timer-show-remaining-time)))
  3418. ;; Autoload org-feed.el
  3419. (eval-and-compile
  3420. (org-autoload
  3421. "org-feed"
  3422. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3423. ;; Autoload org-indent.el
  3424. ;; Define the variable already here, to make sure we have it.
  3425. (defvar org-indent-mode nil
  3426. "Non-nil if Org-Indent mode is enabled.
  3427. Use the command `org-indent-mode' to change this variable.")
  3428. (eval-and-compile
  3429. (org-autoload
  3430. "org-indent"
  3431. '(org-indent-mode)))
  3432. ;; Autoload org-mobile.el
  3433. (eval-and-compile
  3434. (org-autoload
  3435. "org-mobile"
  3436. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3437. ;; Autoload archiving code
  3438. ;; The stuff that is needed for cycling and tags has to be defined here.
  3439. (defgroup org-archive nil
  3440. "Options concerning archiving in Org-mode."
  3441. :tag "Org Archive"
  3442. :group 'org-structure)
  3443. (defcustom org-archive-location "%s_archive::"
  3444. "The location where subtrees should be archived.
  3445. The value of this variable is a string, consisting of two parts,
  3446. separated by a double-colon. The first part is a filename and
  3447. the second part is a headline.
  3448. When the filename is omitted, archiving happens in the same file.
  3449. %s in the filename will be replaced by the current file
  3450. name (without the directory part). Archiving to a different file
  3451. is useful to keep archived entries from contributing to the
  3452. Org-mode Agenda.
  3453. The archived entries will be filed as subtrees of the specified
  3454. headline. When the headline is omitted, the subtrees are simply
  3455. filed away at the end of the file, as top-level entries. Also in
  3456. the heading you can use %s to represent the file name, this can be
  3457. useful when using the same archive for a number of different files.
  3458. Here are a few examples:
  3459. \"%s_archive::\"
  3460. If the current file is Projects.org, archive in file
  3461. Projects.org_archive, as top-level trees. This is the default.
  3462. \"::* Archived Tasks\"
  3463. Archive in the current file, under the top-level headline
  3464. \"* Archived Tasks\".
  3465. \"~/org/archive.org::\"
  3466. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3467. \"~/org/archive.org::From %s\"
  3468. Archive in file ~/org/archive.org (absolute path), under headlines
  3469. \"From FILENAME\" where file name is the current file name.
  3470. \"basement::** Finished Tasks\"
  3471. Archive in file ./basement (relative path), as level 3 trees
  3472. below the level 2 heading \"** Finished Tasks\".
  3473. You may set this option on a per-file basis by adding to the buffer a
  3474. line like
  3475. #+ARCHIVE: basement::** Finished Tasks
  3476. You may also define it locally for a subtree by setting an ARCHIVE property
  3477. in the entry. If such a property is found in an entry, or anywhere up
  3478. the hierarchy, it will be used."
  3479. :group 'org-archive
  3480. :type 'string)
  3481. (defcustom org-archive-tag "ARCHIVE"
  3482. "The tag that marks a subtree as archived.
  3483. An archived subtree does not open during visibility cycling, and does
  3484. not contribute to the agenda listings.
  3485. After changing this, font-lock must be restarted in the relevant buffers to
  3486. get the proper fontification."
  3487. :group 'org-archive
  3488. :group 'org-keywords
  3489. :type 'string)
  3490. (defcustom org-agenda-skip-archived-trees t
  3491. "Non-nil means the agenda will skip any items located in archived trees.
  3492. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3493. variable is no longer recommended, you should leave it at the value t.
  3494. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3495. :group 'org-archive
  3496. :group 'org-agenda-skip
  3497. :type 'boolean)
  3498. (defcustom org-columns-skip-archived-trees t
  3499. "Non-nil means ignore archived trees when creating column view."
  3500. :group 'org-archive
  3501. :group 'org-properties
  3502. :type 'boolean)
  3503. (defcustom org-cycle-open-archived-trees nil
  3504. "Non-nil means `org-cycle' will open archived trees.
  3505. An archived tree is a tree marked with the tag ARCHIVE.
  3506. When nil, archived trees will stay folded. You can still open them with
  3507. normal outline commands like `show-all', but not with the cycling commands."
  3508. :group 'org-archive
  3509. :group 'org-cycle
  3510. :type 'boolean)
  3511. (defcustom org-sparse-tree-open-archived-trees nil
  3512. "Non-nil means sparse tree construction shows matches in archived trees.
  3513. When nil, matches in these trees are highlighted, but the trees are kept in
  3514. collapsed state."
  3515. :group 'org-archive
  3516. :group 'org-sparse-trees
  3517. :type 'boolean)
  3518. (defun org-cycle-hide-archived-subtrees (state)
  3519. "Re-hide all archived subtrees after a visibility state change."
  3520. (when (and (not org-cycle-open-archived-trees)
  3521. (not (memq state '(overview folded))))
  3522. (save-excursion
  3523. (let* ((globalp (memq state '(contents all)))
  3524. (beg (if globalp (point-min) (point)))
  3525. (end (if globalp (point-max) (org-end-of-subtree t))))
  3526. (org-hide-archived-subtrees beg end)
  3527. (goto-char beg)
  3528. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3529. (message "%s" (substitute-command-keys
  3530. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3531. (defun org-force-cycle-archived ()
  3532. "Cycle subtree even if it is archived."
  3533. (interactive)
  3534. (setq this-command 'org-cycle)
  3535. (let ((org-cycle-open-archived-trees t))
  3536. (call-interactively 'org-cycle)))
  3537. (defun org-hide-archived-subtrees (beg end)
  3538. "Re-hide all archived subtrees after a visibility state change."
  3539. (save-excursion
  3540. (let* ((re (concat ":" org-archive-tag ":")))
  3541. (goto-char beg)
  3542. (while (re-search-forward re end t)
  3543. (when (org-on-heading-p)
  3544. (org-flag-subtree t)
  3545. (org-end-of-subtree t))))))
  3546. (defun org-flag-subtree (flag)
  3547. (save-excursion
  3548. (org-back-to-heading t)
  3549. (outline-end-of-heading)
  3550. (outline-flag-region (point)
  3551. (progn (org-end-of-subtree t) (point))
  3552. flag)))
  3553. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3554. (eval-and-compile
  3555. (org-autoload "org-archive"
  3556. '(org-add-archive-files org-archive-subtree
  3557. org-archive-to-archive-sibling org-toggle-archive-tag
  3558. org-archive-subtree-default
  3559. org-archive-subtree-default-with-confirmation)))
  3560. ;; Autoload Column View Code
  3561. (declare-function org-columns-number-to-string "org-colview")
  3562. (declare-function org-columns-get-format-and-top-level "org-colview")
  3563. (declare-function org-columns-compute "org-colview")
  3564. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3565. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3566. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3567. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3568. ;; Autoload ID code
  3569. (declare-function org-id-store-link "org-id")
  3570. (declare-function org-id-locations-load "org-id")
  3571. (declare-function org-id-locations-save "org-id")
  3572. (defvar org-id-track-globally)
  3573. (org-autoload "org-id"
  3574. '(org-id-get-create org-id-new org-id-copy org-id-get
  3575. org-id-get-with-outline-path-completion
  3576. org-id-get-with-outline-drilling org-id-store-link
  3577. org-id-goto org-id-find org-id-store-link))
  3578. ;; Autoload Plotting Code
  3579. (org-autoload "org-plot"
  3580. '(org-plot/gnuplot))
  3581. ;;; Variables for pre-computed regular expressions, all buffer local
  3582. (defvar org-drawer-regexp nil
  3583. "Matches first line of a hidden block.")
  3584. (make-variable-buffer-local 'org-drawer-regexp)
  3585. (defvar org-todo-regexp nil
  3586. "Matches any of the TODO state keywords.")
  3587. (make-variable-buffer-local 'org-todo-regexp)
  3588. (defvar org-not-done-regexp nil
  3589. "Matches any of the TODO state keywords except the last one.")
  3590. (make-variable-buffer-local 'org-not-done-regexp)
  3591. (defvar org-not-done-heading-regexp nil
  3592. "Matches a TODO headline that is not done.")
  3593. (make-variable-buffer-local 'org-not-done-regexp)
  3594. (defvar org-todo-line-regexp nil
  3595. "Matches a headline and puts TODO state into group 2 if present.")
  3596. (make-variable-buffer-local 'org-todo-line-regexp)
  3597. (defvar org-complex-heading-regexp nil
  3598. "Matches a headline and puts everything into groups:
  3599. group 1: the stars
  3600. group 2: The todo keyword, maybe
  3601. group 3: Priority cookie
  3602. group 4: True headline
  3603. group 5: Tags")
  3604. (make-variable-buffer-local 'org-complex-heading-regexp)
  3605. (defvar org-complex-heading-regexp-format nil
  3606. "Printf format to make regexp to match an exact headline.
  3607. This regexp will match the headline of any node which hase the exact
  3608. headline text that is put into the format, but may have any TODO state,
  3609. priority and tags.")
  3610. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3611. (defvar org-todo-line-tags-regexp nil
  3612. "Matches a headline and puts TODO state into group 2 if present.
  3613. Also put tags into group 4 if tags are present.")
  3614. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3615. (defvar org-nl-done-regexp nil
  3616. "Matches newline followed by a headline with the DONE keyword.")
  3617. (make-variable-buffer-local 'org-nl-done-regexp)
  3618. (defvar org-looking-at-done-regexp nil
  3619. "Matches the DONE keyword a point.")
  3620. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3621. (defvar org-ds-keyword-length 12
  3622. "Maximum length of the Deadline and SCHEDULED keywords.")
  3623. (make-variable-buffer-local 'org-ds-keyword-length)
  3624. (defvar org-deadline-regexp nil
  3625. "Matches the DEADLINE keyword.")
  3626. (make-variable-buffer-local 'org-deadline-regexp)
  3627. (defvar org-deadline-time-regexp nil
  3628. "Matches the DEADLINE keyword together with a time stamp.")
  3629. (make-variable-buffer-local 'org-deadline-time-regexp)
  3630. (defvar org-deadline-line-regexp nil
  3631. "Matches the DEADLINE keyword and the rest of the line.")
  3632. (make-variable-buffer-local 'org-deadline-line-regexp)
  3633. (defvar org-scheduled-regexp nil
  3634. "Matches the SCHEDULED keyword.")
  3635. (make-variable-buffer-local 'org-scheduled-regexp)
  3636. (defvar org-scheduled-time-regexp nil
  3637. "Matches the SCHEDULED keyword together with a time stamp.")
  3638. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3639. (defvar org-closed-time-regexp nil
  3640. "Matches the CLOSED keyword together with a time stamp.")
  3641. (make-variable-buffer-local 'org-closed-time-regexp)
  3642. (defvar org-keyword-time-regexp nil
  3643. "Matches any of the 4 keywords, together with the time stamp.")
  3644. (make-variable-buffer-local 'org-keyword-time-regexp)
  3645. (defvar org-keyword-time-not-clock-regexp nil
  3646. "Matches any of the 3 keywords, together with the time stamp.")
  3647. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3648. (defvar org-maybe-keyword-time-regexp nil
  3649. "Matches a timestamp, possibly preceded by a keyword.")
  3650. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3651. (defvar org-planning-or-clock-line-re nil
  3652. "Matches a line with planning or clock info.")
  3653. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3654. (defvar org-all-time-keywords nil
  3655. "List of time keywords.")
  3656. (make-variable-buffer-local 'org-all-time-keywords)
  3657. (defconst org-plain-time-of-day-regexp
  3658. (concat
  3659. "\\(\\<[012]?[0-9]"
  3660. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3661. "\\(--?"
  3662. "\\(\\<[012]?[0-9]"
  3663. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3664. "\\)?")
  3665. "Regular expression to match a plain time or time range.
  3666. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3667. groups carry important information:
  3668. 0 the full match
  3669. 1 the first time, range or not
  3670. 8 the second time, if it is a range.")
  3671. (defconst org-plain-time-extension-regexp
  3672. (concat
  3673. "\\(\\<[012]?[0-9]"
  3674. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3675. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3676. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3677. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3678. groups carry important information:
  3679. 0 the full match
  3680. 7 hours of duration
  3681. 9 minutes of duration")
  3682. (defconst org-stamp-time-of-day-regexp
  3683. (concat
  3684. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3685. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3686. "\\(--?"
  3687. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3688. "Regular expression to match a timestamp time or time range.
  3689. After a match, the following groups carry important information:
  3690. 0 the full match
  3691. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3692. 2 the first time, range or not
  3693. 4 the second time, if it is a range.")
  3694. (defconst org-startup-options
  3695. '(("fold" org-startup-folded t)
  3696. ("overview" org-startup-folded t)
  3697. ("nofold" org-startup-folded nil)
  3698. ("showall" org-startup-folded nil)
  3699. ("showeverything" org-startup-folded showeverything)
  3700. ("content" org-startup-folded content)
  3701. ("indent" org-startup-indented t)
  3702. ("noindent" org-startup-indented nil)
  3703. ("hidestars" org-hide-leading-stars t)
  3704. ("showstars" org-hide-leading-stars nil)
  3705. ("odd" org-odd-levels-only t)
  3706. ("oddeven" org-odd-levels-only nil)
  3707. ("align" org-startup-align-all-tables t)
  3708. ("noalign" org-startup-align-all-tables nil)
  3709. ("inlineimages" org-startup-with-inline-images t)
  3710. ("noinlineimages" org-startup-with-inline-images nil)
  3711. ("customtime" org-display-custom-times t)
  3712. ("logdone" org-log-done time)
  3713. ("lognotedone" org-log-done note)
  3714. ("nologdone" org-log-done nil)
  3715. ("lognoteclock-out" org-log-note-clock-out t)
  3716. ("nolognoteclock-out" org-log-note-clock-out nil)
  3717. ("logrepeat" org-log-repeat state)
  3718. ("lognoterepeat" org-log-repeat note)
  3719. ("nologrepeat" org-log-repeat nil)
  3720. ("logreschedule" org-log-reschedule time)
  3721. ("lognotereschedule" org-log-reschedule note)
  3722. ("nologreschedule" org-log-reschedule nil)
  3723. ("logredeadline" org-log-redeadline time)
  3724. ("lognoteredeadline" org-log-redeadline note)
  3725. ("nologredeadline" org-log-redeadline nil)
  3726. ("logrefile" org-log-refile time)
  3727. ("lognoterefile" org-log-refile note)
  3728. ("nologrefile" org-log-refile nil)
  3729. ("fninline" org-footnote-define-inline t)
  3730. ("nofninline" org-footnote-define-inline nil)
  3731. ("fnlocal" org-footnote-section nil)
  3732. ("fnauto" org-footnote-auto-label t)
  3733. ("fnprompt" org-footnote-auto-label nil)
  3734. ("fnconfirm" org-footnote-auto-label confirm)
  3735. ("fnplain" org-footnote-auto-label plain)
  3736. ("fnadjust" org-footnote-auto-adjust t)
  3737. ("nofnadjust" org-footnote-auto-adjust nil)
  3738. ("constcgs" constants-unit-system cgs)
  3739. ("constSI" constants-unit-system SI)
  3740. ("noptag" org-tag-persistent-alist nil)
  3741. ("hideblocks" org-hide-block-startup t)
  3742. ("nohideblocks" org-hide-block-startup nil)
  3743. ("beamer" org-startup-with-beamer-mode t)
  3744. ("entitiespretty" org-pretty-entities t)
  3745. ("entitiesplain" org-pretty-entities nil))
  3746. "Variable associated with STARTUP options for org-mode.
  3747. Each element is a list of three items: The startup options as written
  3748. in the #+STARTUP line, the corresponding variable, and the value to
  3749. set this variable to if the option is found. An optional forth element PUSH
  3750. means to push this value onto the list in the variable.")
  3751. (defun org-set-regexps-and-options ()
  3752. "Precompute regular expressions for current buffer."
  3753. (when (org-mode-p)
  3754. (org-set-local 'org-todo-kwd-alist nil)
  3755. (org-set-local 'org-todo-key-alist nil)
  3756. (org-set-local 'org-todo-key-trigger nil)
  3757. (org-set-local 'org-todo-keywords-1 nil)
  3758. (org-set-local 'org-done-keywords nil)
  3759. (org-set-local 'org-todo-heads nil)
  3760. (org-set-local 'org-todo-sets nil)
  3761. (org-set-local 'org-todo-log-states nil)
  3762. (org-set-local 'org-file-properties nil)
  3763. (org-set-local 'org-file-tags nil)
  3764. (let ((re (org-make-options-regexp
  3765. '("CATEGORY" "TODO" "COLUMNS"
  3766. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3767. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3768. "OPTIONS")
  3769. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3770. (splitre "[ \t]+")
  3771. (scripts org-use-sub-superscripts)
  3772. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3773. tail sep kws1 prio props ftags drawers beamer-p
  3774. ext-setup-or-nil setup-contents (start 0))
  3775. (save-excursion
  3776. (save-restriction
  3777. (widen)
  3778. (goto-char (point-min))
  3779. (while (or (and ext-setup-or-nil
  3780. (string-match re ext-setup-or-nil start)
  3781. (setq start (match-end 0)))
  3782. (and (setq ext-setup-or-nil nil start 0)
  3783. (re-search-forward re nil t)))
  3784. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3785. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3786. (if (stringp value) (setq value (org-trim value)))
  3787. (cond
  3788. ((equal key "CATEGORY")
  3789. (setq cat value))
  3790. ((member key '("SEQ_TODO" "TODO"))
  3791. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3792. ((equal key "TYP_TODO")
  3793. (push (cons 'type (org-split-string value splitre)) kwds))
  3794. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3795. ;; general TODO-like setup
  3796. (push (cons (intern (downcase (match-string 1 key)))
  3797. (org-split-string value splitre)) kwds))
  3798. ((equal key "TAGS")
  3799. (setq tags (append tags (if tags '("\\n") nil)
  3800. (org-split-string value splitre))))
  3801. ((equal key "COLUMNS")
  3802. (org-set-local 'org-columns-default-format value))
  3803. ((equal key "LINK")
  3804. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3805. (push (cons (match-string 1 value)
  3806. (org-trim (match-string 2 value)))
  3807. links)))
  3808. ((equal key "PRIORITIES")
  3809. (setq prio (org-split-string value " +")))
  3810. ((equal key "PROPERTY")
  3811. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3812. (push (cons (match-string 1 value) (match-string 2 value))
  3813. props)))
  3814. ((equal key "FILETAGS")
  3815. (when (string-match "\\S-" value)
  3816. (setq ftags
  3817. (append
  3818. ftags
  3819. (apply 'append
  3820. (mapcar (lambda (x) (org-split-string x ":"))
  3821. (org-split-string value)))))))
  3822. ((equal key "DRAWERS")
  3823. (setq drawers (org-split-string value splitre)))
  3824. ((equal key "CONSTANTS")
  3825. (setq const (append const (org-split-string value splitre))))
  3826. ((equal key "STARTUP")
  3827. (let ((opts (org-split-string value splitre))
  3828. l var val)
  3829. (while (setq l (pop opts))
  3830. (when (setq l (assoc l org-startup-options))
  3831. (setq var (nth 1 l) val (nth 2 l))
  3832. (if (not (nth 3 l))
  3833. (set (make-local-variable var) val)
  3834. (if (not (listp (symbol-value var)))
  3835. (set (make-local-variable var) nil))
  3836. (set (make-local-variable var) (symbol-value var))
  3837. (add-to-list var val))))))
  3838. ((equal key "ARCHIVE")
  3839. (setq arch value)
  3840. (remove-text-properties 0 (length arch)
  3841. '(face t fontified t) arch))
  3842. ((equal key "LATEX_CLASS")
  3843. (setq beamer-p (equal value "beamer")))
  3844. ((equal key "OPTIONS")
  3845. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3846. (setq scripts (read (match-string 2 value)))))
  3847. ((equal key "SETUPFILE")
  3848. (setq setup-contents (org-file-contents
  3849. (expand-file-name
  3850. (org-remove-double-quotes value))
  3851. 'noerror))
  3852. (if (not ext-setup-or-nil)
  3853. (setq ext-setup-or-nil setup-contents start 0)
  3854. (setq ext-setup-or-nil
  3855. (concat (substring ext-setup-or-nil 0 start)
  3856. "\n" setup-contents "\n"
  3857. (substring ext-setup-or-nil start)))))
  3858. ))))
  3859. (org-set-local 'org-use-sub-superscripts scripts)
  3860. (when cat
  3861. (org-set-local 'org-category (intern cat))
  3862. (push (cons "CATEGORY" cat) props))
  3863. (when prio
  3864. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3865. (setq prio (mapcar 'string-to-char prio))
  3866. (org-set-local 'org-highest-priority (nth 0 prio))
  3867. (org-set-local 'org-lowest-priority (nth 1 prio))
  3868. (org-set-local 'org-default-priority (nth 2 prio)))
  3869. (and props (org-set-local 'org-file-properties (nreverse props)))
  3870. (and ftags (org-set-local 'org-file-tags
  3871. (mapcar 'org-add-prop-inherited ftags)))
  3872. (and drawers (org-set-local 'org-drawers drawers))
  3873. (and arch (org-set-local 'org-archive-location arch))
  3874. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3875. ;; Process the TODO keywords
  3876. (unless kwds
  3877. ;; Use the global values as if they had been given locally.
  3878. (setq kwds (default-value 'org-todo-keywords))
  3879. (if (stringp (car kwds))
  3880. (setq kwds (list (cons org-todo-interpretation
  3881. (default-value 'org-todo-keywords)))))
  3882. (setq kwds (reverse kwds)))
  3883. (setq kwds (nreverse kwds))
  3884. (let (inter kws kw)
  3885. (while (setq kws (pop kwds))
  3886. (let ((kws (or
  3887. (run-hook-with-args-until-success
  3888. 'org-todo-setup-filter-hook kws)
  3889. kws)))
  3890. (setq inter (pop kws) sep (member "|" kws)
  3891. kws0 (delete "|" (copy-sequence kws))
  3892. kwsa nil
  3893. kws1 (mapcar
  3894. (lambda (x)
  3895. ;; 1 2
  3896. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3897. (progn
  3898. (setq kw (match-string 1 x)
  3899. key (and (match-end 2) (match-string 2 x))
  3900. log (org-extract-log-state-settings x))
  3901. (push (cons kw (and key (string-to-char key))) kwsa)
  3902. (and log (push log org-todo-log-states))
  3903. kw)
  3904. (error "Invalid TODO keyword %s" x)))
  3905. kws0)
  3906. kwsa (if kwsa (append '((:startgroup))
  3907. (nreverse kwsa)
  3908. '((:endgroup))))
  3909. hw (car kws1)
  3910. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3911. tail (list inter hw (car dws) (org-last dws))))
  3912. (add-to-list 'org-todo-heads hw 'append)
  3913. (push kws1 org-todo-sets)
  3914. (setq org-done-keywords (append org-done-keywords dws nil))
  3915. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3916. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3917. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3918. (setq org-todo-sets (nreverse org-todo-sets)
  3919. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3920. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3921. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3922. ;; Process the constants
  3923. (when const
  3924. (let (e cst)
  3925. (while (setq e (pop const))
  3926. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3927. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3928. (setq org-table-formula-constants-local cst)))
  3929. ;; Process the tags.
  3930. (when tags
  3931. (let (e tgs)
  3932. (while (setq e (pop tags))
  3933. (cond
  3934. ((equal e "{") (push '(:startgroup) tgs))
  3935. ((equal e "}") (push '(:endgroup) tgs))
  3936. ((equal e "\\n") (push '(:newline) tgs))
  3937. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3938. (push (cons (match-string 1 e)
  3939. (string-to-char (match-string 2 e)))
  3940. tgs))
  3941. (t (push (list e) tgs))))
  3942. (org-set-local 'org-tag-alist nil)
  3943. (while (setq e (pop tgs))
  3944. (or (and (stringp (car e))
  3945. (assoc (car e) org-tag-alist))
  3946. (push e org-tag-alist)))))
  3947. ;; Compute the regular expressions and other local variables
  3948. (if (not org-done-keywords)
  3949. (setq org-done-keywords (and org-todo-keywords-1
  3950. (list (org-last org-todo-keywords-1)))))
  3951. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3952. (length org-scheduled-string)
  3953. (length org-clock-string)
  3954. (length org-closed-string)))
  3955. org-drawer-regexp
  3956. (concat "^[ \t]*:\\("
  3957. (mapconcat 'regexp-quote org-drawers "\\|")
  3958. "\\):[ \t]*$")
  3959. org-not-done-keywords
  3960. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3961. org-todo-regexp
  3962. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3963. "\\|") "\\)\\>")
  3964. org-not-done-regexp
  3965. (concat "\\<\\("
  3966. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3967. "\\)\\>")
  3968. org-not-done-heading-regexp
  3969. (concat "^\\(\\*+\\)[ \t]+\\("
  3970. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3971. "\\)\\>")
  3972. org-todo-line-regexp
  3973. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3974. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3975. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3976. org-complex-heading-regexp
  3977. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3978. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3979. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3980. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3981. org-complex-heading-regexp-format
  3982. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3983. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3984. "\\)\\>\\)?"
  3985. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3986. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3987. "[ \t]*\\(%s\\)"
  3988. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3989. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  3990. org-nl-done-regexp
  3991. (concat "\n\\*+[ \t]+"
  3992. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3993. "\\)" "\\>")
  3994. org-todo-line-tags-regexp
  3995. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3996. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3997. (org-re
  3998. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  3999. org-looking-at-done-regexp
  4000. (concat "^" "\\(?:"
  4001. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4002. "\\>")
  4003. org-deadline-regexp (concat "\\<" org-deadline-string)
  4004. org-deadline-time-regexp
  4005. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4006. org-deadline-line-regexp
  4007. (concat "\\<\\(" org-deadline-string "\\).*")
  4008. org-scheduled-regexp
  4009. (concat "\\<" org-scheduled-string)
  4010. org-scheduled-time-regexp
  4011. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4012. org-closed-time-regexp
  4013. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4014. org-keyword-time-regexp
  4015. (concat "\\<\\(" org-scheduled-string
  4016. "\\|" org-deadline-string
  4017. "\\|" org-closed-string
  4018. "\\|" org-clock-string "\\)"
  4019. " *[[<]\\([^]>]+\\)[]>]")
  4020. org-keyword-time-not-clock-regexp
  4021. (concat "\\<\\(" org-scheduled-string
  4022. "\\|" org-deadline-string
  4023. "\\|" org-closed-string
  4024. "\\)"
  4025. " *[[<]\\([^]>]+\\)[]>]")
  4026. org-maybe-keyword-time-regexp
  4027. (concat "\\(\\<\\(" org-scheduled-string
  4028. "\\|" org-deadline-string
  4029. "\\|" org-closed-string
  4030. "\\|" org-clock-string "\\)\\)?"
  4031. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4032. org-planning-or-clock-line-re
  4033. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4034. "\\|" org-deadline-string
  4035. "\\|" org-closed-string "\\|" org-clock-string
  4036. "\\)\\>\\)")
  4037. org-all-time-keywords
  4038. (mapcar (lambda (w) (substring w 0 -1))
  4039. (list org-scheduled-string org-deadline-string
  4040. org-clock-string org-closed-string))
  4041. )
  4042. (org-compute-latex-and-specials-regexp)
  4043. (org-set-font-lock-defaults))))
  4044. (defun org-file-contents (file &optional noerror)
  4045. "Return the contents of FILE, as a string."
  4046. (if (or (not file)
  4047. (not (file-readable-p file)))
  4048. (if noerror
  4049. (progn
  4050. (message "Cannot read file \"%s\"" file)
  4051. (ding) (sit-for 2)
  4052. "")
  4053. (error "Cannot read file \"%s\"" file))
  4054. (with-temp-buffer
  4055. (insert-file-contents file)
  4056. (buffer-string))))
  4057. (defun org-extract-log-state-settings (x)
  4058. "Extract the log state setting from a TODO keyword string.
  4059. This will extract info from a string like \"WAIT(w@/!)\"."
  4060. (let (kw key log1 log2)
  4061. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4062. (setq kw (match-string 1 x)
  4063. key (and (match-end 2) (match-string 2 x))
  4064. log1 (and (match-end 3) (match-string 3 x))
  4065. log2 (and (match-end 4) (match-string 4 x)))
  4066. (and (or log1 log2)
  4067. (list kw
  4068. (and log1 (if (equal log1 "!") 'time 'note))
  4069. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4070. (defun org-remove-keyword-keys (list)
  4071. "Remove a pair of parenthesis at the end of each string in LIST."
  4072. (mapcar (lambda (x)
  4073. (if (string-match "(.*)$" x)
  4074. (substring x 0 (match-beginning 0))
  4075. x))
  4076. list))
  4077. (defun org-assign-fast-keys (alist)
  4078. "Assign fast keys to a keyword-key alist.
  4079. Respect keys that are already there."
  4080. (let (new e (alt ?0))
  4081. (while (setq e (pop alist))
  4082. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4083. (cdr e)) ;; Key already assigned.
  4084. (push e new)
  4085. (let ((clist (string-to-list (downcase (car e))))
  4086. (used (append new alist)))
  4087. (when (= (car clist) ?@)
  4088. (pop clist))
  4089. (while (and clist (rassoc (car clist) used))
  4090. (pop clist))
  4091. (unless clist
  4092. (while (rassoc alt used)
  4093. (incf alt)))
  4094. (push (cons (car e) (or (car clist) alt)) new))))
  4095. (nreverse new)))
  4096. ;;; Some variables used in various places
  4097. (defvar org-window-configuration nil
  4098. "Used in various places to store a window configuration.")
  4099. (defvar org-selected-window nil
  4100. "Used in various places to store a window configuration.")
  4101. (defvar org-finish-function nil
  4102. "Function to be called when `C-c C-c' is used.
  4103. This is for getting out of special buffers like remember.")
  4104. ;; FIXME: Occasionally check by commenting these, to make sure
  4105. ;; no other functions uses these, forgetting to let-bind them.
  4106. (defvar entry)
  4107. (defvar last-state)
  4108. (defvar date)
  4109. ;; Defined somewhere in this file, but used before definition.
  4110. (defvar org-entities) ;; defined in org-entities.el
  4111. (defvar org-struct-menu)
  4112. (defvar org-org-menu)
  4113. (defvar org-tbl-menu)
  4114. ;;;; Define the Org-mode
  4115. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4116. (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"))
  4117. ;; We use a before-change function to check if a table might need
  4118. ;; an update.
  4119. (defvar org-table-may-need-update t
  4120. "Indicates that a table might need an update.
  4121. This variable is set by `org-before-change-function'.
  4122. `org-table-align' sets it back to nil.")
  4123. (defun org-before-change-function (beg end)
  4124. "Every change indicates that a table might need an update."
  4125. (setq org-table-may-need-update t))
  4126. (defvar org-mode-map)
  4127. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4128. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4129. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4130. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4131. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4132. (defvar org-table-buffer-is-an nil)
  4133. (defconst org-outline-regexp "\\*+ ")
  4134. ;;;###autoload
  4135. (define-derived-mode org-mode outline-mode "Org"
  4136. "Outline-based notes management and organizer, alias
  4137. \"Carsten's outline-mode for keeping track of everything.\"
  4138. Org-mode develops organizational tasks around a NOTES file which
  4139. contains information about projects as plain text. Org-mode is
  4140. implemented on top of outline-mode, which is ideal to keep the content
  4141. of large files well structured. It supports ToDo items, deadlines and
  4142. time stamps, which magically appear in the diary listing of the Emacs
  4143. calendar. Tables are easily created with a built-in table editor.
  4144. Plain text URL-like links connect to websites, emails (VM), Usenet
  4145. messages (Gnus), BBDB entries, and any files related to the project.
  4146. For printing and sharing of notes, an Org-mode file (or a part of it)
  4147. can be exported as a structured ASCII or HTML file.
  4148. The following commands are available:
  4149. \\{org-mode-map}"
  4150. ;; Get rid of Outline menus, they are not needed
  4151. ;; Need to do this here because define-derived-mode sets up
  4152. ;; the keymap so late. Still, it is a waste to call this each time
  4153. ;; we switch another buffer into org-mode.
  4154. (if (featurep 'xemacs)
  4155. (when (boundp 'outline-mode-menu-heading)
  4156. ;; Assume this is Greg's port, it uses easymenu
  4157. (easy-menu-remove outline-mode-menu-heading)
  4158. (easy-menu-remove outline-mode-menu-show)
  4159. (easy-menu-remove outline-mode-menu-hide))
  4160. (define-key org-mode-map [menu-bar headings] 'undefined)
  4161. (define-key org-mode-map [menu-bar hide] 'undefined)
  4162. (define-key org-mode-map [menu-bar show] 'undefined))
  4163. (org-load-modules-maybe)
  4164. (easy-menu-add org-org-menu)
  4165. (easy-menu-add org-tbl-menu)
  4166. (org-install-agenda-files-menu)
  4167. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4168. (add-to-invisibility-spec '(org-cwidth))
  4169. (add-to-invisibility-spec '(org-hide-block . t))
  4170. (when (featurep 'xemacs)
  4171. (org-set-local 'line-move-ignore-invisible t))
  4172. (org-set-local 'outline-regexp org-outline-regexp)
  4173. (org-set-local 'outline-level 'org-outline-level)
  4174. (when (and org-ellipsis
  4175. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4176. (fboundp 'make-glyph-code))
  4177. (unless org-display-table
  4178. (setq org-display-table (make-display-table)))
  4179. (set-display-table-slot
  4180. org-display-table 4
  4181. (vconcat (mapcar
  4182. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4183. org-ellipsis)))
  4184. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4185. (setq buffer-display-table org-display-table))
  4186. (org-set-regexps-and-options)
  4187. (when (and org-tag-faces (not org-tags-special-faces-re))
  4188. ;; tag faces set outside customize.... force initialization.
  4189. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4190. ;; Calc embedded
  4191. (org-set-local 'calc-embedded-open-mode "# ")
  4192. (modify-syntax-entry ?@ "w")
  4193. (if org-startup-truncated (setq truncate-lines t))
  4194. (org-set-local 'font-lock-unfontify-region-function
  4195. 'org-unfontify-region)
  4196. ;; Activate before-change-function
  4197. (org-set-local 'org-table-may-need-update t)
  4198. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4199. 'local)
  4200. ;; Check for running clock before killing a buffer
  4201. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4202. ;; Paragraphs and auto-filling
  4203. (org-set-autofill-regexps)
  4204. (setq indent-line-function 'org-indent-line-function)
  4205. (org-update-radio-target-regexp)
  4206. ;; Beginning/end of defun
  4207. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4208. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4209. ;; Next error for sparse trees
  4210. (org-set-local 'next-error-function 'org-occur-next-match)
  4211. ;; Make sure dependence stuff works reliably, even for users who set it
  4212. ;; too late :-(
  4213. (if org-enforce-todo-dependencies
  4214. (add-hook 'org-blocker-hook
  4215. 'org-block-todo-from-children-or-siblings-or-parent)
  4216. (remove-hook 'org-blocker-hook
  4217. 'org-block-todo-from-children-or-siblings-or-parent))
  4218. (if org-enforce-todo-checkbox-dependencies
  4219. (add-hook 'org-blocker-hook
  4220. 'org-block-todo-from-checkboxes)
  4221. (remove-hook 'org-blocker-hook
  4222. 'org-block-todo-from-checkboxes))
  4223. ;; Comment characters
  4224. (org-set-local 'comment-start "#")
  4225. (org-set-local 'comment-padding " ")
  4226. ;; Align options lines
  4227. (org-set-local
  4228. 'align-mode-rules-list
  4229. '((org-in-buffer-settings
  4230. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4231. (modes . '(org-mode)))))
  4232. ;; Imenu
  4233. (org-set-local 'imenu-create-index-function
  4234. 'org-imenu-get-tree)
  4235. ;; Make isearch reveal context
  4236. (if (or (featurep 'xemacs)
  4237. (not (boundp 'outline-isearch-open-invisible-function)))
  4238. ;; Emacs 21 and XEmacs make use of the hook
  4239. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4240. ;; Emacs 22 deals with this through a special variable
  4241. (org-set-local 'outline-isearch-open-invisible-function
  4242. (lambda (&rest ignore) (org-show-context 'isearch))))
  4243. ;; Turn on org-beamer-mode?
  4244. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4245. ;; Setup the pcomplete hooks
  4246. (set (make-local-variable 'pcomplete-command-completion-function)
  4247. 'org-pcomplete-initial)
  4248. (set (make-local-variable 'pcomplete-command-name-function)
  4249. 'org-command-at-point)
  4250. (set (make-local-variable 'pcomplete-default-completion-function)
  4251. 'ignore)
  4252. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4253. 'org-parse-arguments)
  4254. (set (make-local-variable 'pcomplete-termination-string) "")
  4255. ;; If empty file that did not turn on org-mode automatically, make it to.
  4256. (if (and org-insert-mode-line-in-empty-file
  4257. (interactive-p)
  4258. (= (point-min) (point-max)))
  4259. (insert "# -*- mode: org -*-\n\n"))
  4260. (unless org-inhibit-startup
  4261. (when org-startup-align-all-tables
  4262. (let ((bmp (buffer-modified-p)))
  4263. (org-table-map-tables 'org-table-align 'quietly)
  4264. (set-buffer-modified-p bmp)))
  4265. (when org-startup-with-inline-images
  4266. (org-display-inline-images))
  4267. (when org-startup-indented
  4268. (require 'org-indent)
  4269. (org-indent-mode 1))
  4270. (unless org-inhibit-startup-visibility-stuff
  4271. (org-set-startup-visibility))))
  4272. (when (fboundp 'abbrev-table-put)
  4273. (abbrev-table-put org-mode-abbrev-table
  4274. :parents (list text-mode-abbrev-table)))
  4275. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4276. (defun org-current-time ()
  4277. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4278. (if (> (car org-time-stamp-rounding-minutes) 1)
  4279. (let ((r (car org-time-stamp-rounding-minutes))
  4280. (time (decode-time)))
  4281. (apply 'encode-time
  4282. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4283. (nthcdr 2 time))))
  4284. (current-time)))
  4285. (defun org-today ()
  4286. "Return today date, considering `org-extend-today-until'."
  4287. (time-to-days
  4288. (time-subtract (current-time)
  4289. (list 0 (* 3600 org-extend-today-until) 0))))
  4290. ;;;; Font-Lock stuff, including the activators
  4291. (defvar org-mouse-map (make-sparse-keymap))
  4292. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4293. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4294. (when org-mouse-1-follows-link
  4295. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4296. (when org-tab-follows-link
  4297. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4298. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4299. (require 'font-lock)
  4300. (defconst org-non-link-chars "]\t\n\r<>")
  4301. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4302. "shell" "elisp" "doi" "message"))
  4303. (defvar org-link-types-re nil
  4304. "Matches a link that has a url-like prefix like \"http:\"")
  4305. (defvar org-link-re-with-space nil
  4306. "Matches a link with spaces, optional angular brackets around it.")
  4307. (defvar org-link-re-with-space2 nil
  4308. "Matches a link with spaces, optional angular brackets around it.")
  4309. (defvar org-link-re-with-space3 nil
  4310. "Matches a link with spaces, only for internal part in bracket links.")
  4311. (defvar org-angle-link-re nil
  4312. "Matches link with angular brackets, spaces are allowed.")
  4313. (defvar org-plain-link-re nil
  4314. "Matches plain link, without spaces.")
  4315. (defvar org-bracket-link-regexp nil
  4316. "Matches a link in double brackets.")
  4317. (defvar org-bracket-link-analytic-regexp nil
  4318. "Regular expression used to analyze links.
  4319. Here is what the match groups contain after a match:
  4320. 1: http:
  4321. 2: http
  4322. 3: path
  4323. 4: [desc]
  4324. 5: desc")
  4325. (defvar org-bracket-link-analytic-regexp++ nil
  4326. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4327. (defvar org-any-link-re nil
  4328. "Regular expression matching any link.")
  4329. (defcustom org-match-sexp-depth 3
  4330. "Number of stacked braces for sub/superscript matching.
  4331. This has to be set before loading org.el to be effective."
  4332. :group 'org-export-translation ; ??????????????????????????/
  4333. :type 'integer)
  4334. (defun org-create-multibrace-regexp (left right n)
  4335. "Create a regular expression which will match a balanced sexp.
  4336. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4337. as single character strings.
  4338. The regexp returned will match the entire expression including the
  4339. delimiters. It will also define a single group which contains the
  4340. match except for the outermost delimiters. The maximum depth of
  4341. stacked delimiters is N. Escaping delimiters is not possible."
  4342. (let* ((nothing (concat "[^" left right "]*?"))
  4343. (or "\\|")
  4344. (re nothing)
  4345. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4346. (while (> n 1)
  4347. (setq n (1- n)
  4348. re (concat re or next)
  4349. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4350. (concat left "\\(" re "\\)" right)))
  4351. (defvar org-match-substring-regexp
  4352. (concat
  4353. "\\([^\\]\\)\\([_^]\\)\\("
  4354. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4355. "\\|"
  4356. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4357. "\\|"
  4358. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4359. "The regular expression matching a sub- or superscript.")
  4360. (defvar org-match-substring-with-braces-regexp
  4361. (concat
  4362. "\\([^\\]\\)\\([_^]\\)\\("
  4363. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4364. "\\)")
  4365. "The regular expression matching a sub- or superscript, forcing braces.")
  4366. (defun org-make-link-regexps ()
  4367. "Update the link regular expressions.
  4368. This should be called after the variable `org-link-types' has changed."
  4369. (setq org-link-types-re
  4370. (concat
  4371. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4372. org-link-re-with-space
  4373. (concat
  4374. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4375. "\\([^" org-non-link-chars " ]"
  4376. "[^" org-non-link-chars "]*"
  4377. "[^" org-non-link-chars " ]\\)>?")
  4378. org-link-re-with-space2
  4379. (concat
  4380. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4381. "\\([^" org-non-link-chars " ]"
  4382. "[^\t\n\r]*"
  4383. "[^" org-non-link-chars " ]\\)>?")
  4384. org-link-re-with-space3
  4385. (concat
  4386. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4387. "\\([^" org-non-link-chars " ]"
  4388. "[^\t\n\r]*\\)")
  4389. org-angle-link-re
  4390. (concat
  4391. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4392. "\\([^" org-non-link-chars " ]"
  4393. "[^" org-non-link-chars "]*"
  4394. "\\)>")
  4395. org-plain-link-re
  4396. (concat
  4397. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4398. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4399. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4400. org-bracket-link-regexp
  4401. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4402. org-bracket-link-analytic-regexp
  4403. (concat
  4404. "\\[\\["
  4405. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4406. "\\([^]]+\\)"
  4407. "\\]"
  4408. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4409. "\\]")
  4410. org-bracket-link-analytic-regexp++
  4411. (concat
  4412. "\\[\\["
  4413. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4414. "\\([^]]+\\)"
  4415. "\\]"
  4416. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4417. "\\]")
  4418. org-any-link-re
  4419. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4420. org-angle-link-re "\\)\\|\\("
  4421. org-plain-link-re "\\)")))
  4422. (org-make-link-regexps)
  4423. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4424. "Regular expression for fast time stamp matching.")
  4425. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?\\)[]>]"
  4426. "Regular expression for fast time stamp matching.")
  4427. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4428. "Regular expression matching time strings for analysis.
  4429. This one does not require the space after the date, so it can be used
  4430. on a string that terminates immediately after the date.")
  4431. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4432. "Regular expression matching time strings for analysis.")
  4433. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4434. "Regular expression matching time stamps, with groups.")
  4435. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4436. "Regular expression matching time stamps (also [..]), with groups.")
  4437. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4438. "Regular expression matching a time stamp range.")
  4439. (defconst org-tr-regexp-both
  4440. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4441. "Regular expression matching a time stamp range.")
  4442. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4443. org-ts-regexp "\\)?")
  4444. "Regular expression matching a time stamp or time stamp range.")
  4445. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4446. org-ts-regexp-both "\\)?")
  4447. "Regular expression matching a time stamp or time stamp range.
  4448. The time stamps may be either active or inactive.")
  4449. (defvar org-emph-face nil)
  4450. (defun org-do-emphasis-faces (limit)
  4451. "Run through the buffer and add overlays to links."
  4452. (let (rtn a)
  4453. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4454. (if (not (= (char-after (match-beginning 3))
  4455. (char-after (match-beginning 4))))
  4456. (progn
  4457. (setq rtn t)
  4458. (setq a (assoc (match-string 3) org-emphasis-alist))
  4459. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4460. 'face
  4461. (nth 1 a))
  4462. (and (nth 4 a)
  4463. (org-remove-flyspell-overlays-in
  4464. (match-beginning 0) (match-end 0)))
  4465. (add-text-properties (match-beginning 2) (match-end 2)
  4466. '(font-lock-multiline t org-emphasis t))
  4467. (when org-hide-emphasis-markers
  4468. (add-text-properties (match-end 4) (match-beginning 5)
  4469. '(invisible org-link))
  4470. (add-text-properties (match-beginning 3) (match-end 3)
  4471. '(invisible org-link)))))
  4472. (backward-char 1))
  4473. rtn))
  4474. (defun org-emphasize (&optional char)
  4475. "Insert or change an emphasis, i.e. a font like bold or italic.
  4476. If there is an active region, change that region to a new emphasis.
  4477. If there is no region, just insert the marker characters and position
  4478. the cursor between them.
  4479. CHAR should be either the marker character, or the first character of the
  4480. HTML tag associated with that emphasis. If CHAR is a space, the means
  4481. to remove the emphasis of the selected region.
  4482. If char is not given (for example in an interactive call) it
  4483. will be prompted for."
  4484. (interactive)
  4485. (let ((eal org-emphasis-alist) e det
  4486. (erc org-emphasis-regexp-components)
  4487. (prompt "")
  4488. (string "") beg end move tag c s)
  4489. (if (org-region-active-p)
  4490. (setq beg (region-beginning) end (region-end)
  4491. string (buffer-substring beg end))
  4492. (setq move t))
  4493. (while (setq e (pop eal))
  4494. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4495. c (aref tag 0))
  4496. (push (cons c (string-to-char (car e))) det)
  4497. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4498. (substring tag 1)))))
  4499. (setq det (nreverse det))
  4500. (unless char
  4501. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4502. (setq char (read-char-exclusive)))
  4503. (setq char (or (cdr (assoc char det)) char))
  4504. (if (equal char ?\ )
  4505. (setq s "" move nil)
  4506. (unless (assoc (char-to-string char) org-emphasis-alist)
  4507. (error "No such emphasis marker: \"%c\"" char))
  4508. (setq s (char-to-string char)))
  4509. (while (and (> (length string) 1)
  4510. (equal (substring string 0 1) (substring string -1))
  4511. (assoc (substring string 0 1) org-emphasis-alist))
  4512. (setq string (substring string 1 -1)))
  4513. (setq string (concat s string s))
  4514. (if beg (delete-region beg end))
  4515. (unless (or (bolp)
  4516. (string-match (concat "[" (nth 0 erc) "\n]")
  4517. (char-to-string (char-before (point)))))
  4518. (insert " "))
  4519. (unless (or (eobp)
  4520. (string-match (concat "[" (nth 1 erc) "\n]")
  4521. (char-to-string (char-after (point)))))
  4522. (insert " ") (backward-char 1))
  4523. (insert string)
  4524. (and move (backward-char 1))))
  4525. (defconst org-nonsticky-props
  4526. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4527. (defsubst org-rear-nonsticky-at (pos)
  4528. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4529. (defun org-activate-plain-links (limit)
  4530. "Run through the buffer and add overlays to links."
  4531. (catch 'exit
  4532. (let (f)
  4533. (if (re-search-forward org-plain-link-re limit t)
  4534. (progn
  4535. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4536. (setq f (get-text-property (match-beginning 0) 'face))
  4537. (if (or (eq f 'org-tag)
  4538. (and (listp f) (memq 'org-tag f)))
  4539. nil
  4540. (add-text-properties (match-beginning 0) (match-end 0)
  4541. (list 'mouse-face 'highlight
  4542. 'face 'org-link
  4543. 'keymap org-mouse-map))
  4544. (org-rear-nonsticky-at (match-end 0)))
  4545. t)))))
  4546. (defun org-activate-code (limit)
  4547. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4548. (progn
  4549. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4550. (remove-text-properties (match-beginning 0) (match-end 0)
  4551. '(display t invisible t intangible t))
  4552. t)))
  4553. (defcustom org-src-fontify-natively nil
  4554. "When non-nil, fontify code in code blocks."
  4555. :type 'boolean
  4556. :group 'org-appearance
  4557. :group 'org-babel)
  4558. (defun org-fontify-meta-lines-and-blocks (limit)
  4559. "Fontify #+ lines and blocks, in the correct ways."
  4560. (let ((case-fold-search t))
  4561. (if (re-search-forward
  4562. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4563. limit t)
  4564. (let ((beg (match-beginning 0))
  4565. (block-start (match-end 0))
  4566. (block-end nil)
  4567. (lang (match-string 7))
  4568. (beg1 (line-beginning-position 2))
  4569. (dc1 (downcase (match-string 2)))
  4570. (dc3 (downcase (match-string 3)))
  4571. end end1 quoting block-type ovl)
  4572. (cond
  4573. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4574. ;; a single line of backend-specific content
  4575. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4576. (remove-text-properties (match-beginning 0) (match-end 0)
  4577. '(display t invisible t intangible t))
  4578. (add-text-properties (match-beginning 1) (match-end 3)
  4579. '(font-lock-fontified t face org-meta-line))
  4580. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4581. '(font-lock-fontified t face org-block))
  4582. ; for backend-specific code
  4583. t)
  4584. ((and (match-end 4) (equal dc3 "begin"))
  4585. ;; Truly a block
  4586. (setq block-type (downcase (match-string 5))
  4587. quoting (member block-type org-protecting-blocks))
  4588. (when (re-search-forward
  4589. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4590. nil t) ;; on purpose, we look further than LIMIT
  4591. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4592. (setq block-end (match-beginning 0))
  4593. (when quoting
  4594. (remove-text-properties beg end
  4595. '(display t invisible t intangible t)))
  4596. (add-text-properties
  4597. beg end
  4598. '(font-lock-fontified t font-lock-multiline t))
  4599. (add-text-properties beg beg1 '(face org-meta-line))
  4600. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4601. ; for end_src
  4602. (cond
  4603. ((and lang org-src-fontify-natively)
  4604. (org-src-font-lock-fontify-block lang block-start block-end)
  4605. ;; remove old background overlays
  4606. (mapc (lambda (ov)
  4607. (if (eq (overlay-get ov 'face) 'org-block-background)
  4608. (delete-overlay ov)))
  4609. (overlays-at (/ (+ beg1 block-end) 2)))
  4610. ;; add a background overlay
  4611. (setq ovl (make-overlay beg1 block-end))
  4612. (overlay-put ovl 'face 'org-block-background)
  4613. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4614. (quoting
  4615. (add-text-properties beg1 (+ end1 1) '(face org-block)))
  4616. ; end of source block
  4617. ((not org-fontify-quote-and-verse-blocks))
  4618. ((string= block-type "quote")
  4619. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4620. ((string= block-type "verse")
  4621. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4622. (add-text-properties beg beg1 '(face org-block-begin-line))
  4623. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4624. t))
  4625. ((member dc1 '("title:" "author:" "email:" "date:"))
  4626. (add-text-properties
  4627. beg (match-end 3)
  4628. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4629. '(font-lock-fontified t invisible t)
  4630. '(font-lock-fontified t face org-document-info-keyword)))
  4631. (add-text-properties
  4632. (match-beginning 6) (match-end 6)
  4633. (if (string-equal dc1 "title:")
  4634. '(font-lock-fontified t face org-document-title)
  4635. '(font-lock-fontified t face org-document-info))))
  4636. ((not (member (char-after beg) '(?\ ?\t)))
  4637. ;; just any other in-buffer setting, but not indented
  4638. (add-text-properties
  4639. beg (1+ (match-end 0))
  4640. '(font-lock-fontified t face org-meta-line))
  4641. t)
  4642. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4643. "orgtbl:" "tblfm:" "tblname:" "result:"
  4644. "results:" "source:" "srcname:" "call:"))
  4645. (and (match-end 4) (equal dc3 "attr")))
  4646. (add-text-properties
  4647. beg (match-end 0)
  4648. '(font-lock-fontified t face org-meta-line))
  4649. t)
  4650. ((member dc3 '(" " ""))
  4651. (add-text-properties
  4652. beg (match-end 0)
  4653. '(font-lock-fontified t face font-lock-comment-face)))
  4654. (t nil))))))
  4655. (defun org-activate-angle-links (limit)
  4656. "Run through the buffer and add overlays to links."
  4657. (if (re-search-forward org-angle-link-re limit t)
  4658. (progn
  4659. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4660. (add-text-properties (match-beginning 0) (match-end 0)
  4661. (list 'mouse-face 'highlight
  4662. 'keymap org-mouse-map))
  4663. (org-rear-nonsticky-at (match-end 0))
  4664. t)))
  4665. (defun org-activate-footnote-links (limit)
  4666. "Run through the buffer and add overlays to links."
  4667. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4668. limit t)
  4669. (progn
  4670. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4671. (add-text-properties (match-beginning 2) (match-end 2)
  4672. (list 'mouse-face 'highlight
  4673. 'keymap org-mouse-map
  4674. 'help-echo
  4675. (if (= (point-at-bol) (match-beginning 2))
  4676. "Footnote definition"
  4677. "Footnote reference")
  4678. ))
  4679. (org-rear-nonsticky-at (match-end 2))
  4680. t)))
  4681. (defun org-activate-bracket-links (limit)
  4682. "Run through the buffer and add overlays to bracketed links."
  4683. (if (re-search-forward org-bracket-link-regexp limit t)
  4684. (let* ((help (concat "LINK: "
  4685. (org-match-string-no-properties 1)))
  4686. ;; FIXME: above we should remove the escapes.
  4687. ;; but that requires another match, protecting match data,
  4688. ;; a lot of overhead for font-lock.
  4689. (ip (org-maybe-intangible
  4690. (list 'invisible 'org-link
  4691. 'keymap org-mouse-map 'mouse-face 'highlight
  4692. 'font-lock-multiline t 'help-echo help)))
  4693. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4694. 'font-lock-multiline t 'help-echo help)))
  4695. ;; We need to remove the invisible property here. Table narrowing
  4696. ;; may have made some of this invisible.
  4697. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4698. (remove-text-properties (match-beginning 0) (match-end 0)
  4699. '(invisible nil))
  4700. (if (match-end 3)
  4701. (progn
  4702. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4703. (org-rear-nonsticky-at (match-beginning 3))
  4704. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4705. (org-rear-nonsticky-at (match-end 3))
  4706. (add-text-properties (match-end 3) (match-end 0) ip)
  4707. (org-rear-nonsticky-at (match-end 0)))
  4708. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4709. (org-rear-nonsticky-at (match-beginning 1))
  4710. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4711. (org-rear-nonsticky-at (match-end 1))
  4712. (add-text-properties (match-end 1) (match-end 0) ip)
  4713. (org-rear-nonsticky-at (match-end 0)))
  4714. t)))
  4715. (defun org-activate-dates (limit)
  4716. "Run through the buffer and add overlays to dates."
  4717. (if (re-search-forward org-tsr-regexp-both limit t)
  4718. (progn
  4719. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4720. (add-text-properties (match-beginning 0) (match-end 0)
  4721. (list 'mouse-face 'highlight
  4722. 'keymap org-mouse-map))
  4723. (org-rear-nonsticky-at (match-end 0))
  4724. (when org-display-custom-times
  4725. (if (match-end 3)
  4726. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4727. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4728. t)))
  4729. (defvar org-target-link-regexp nil
  4730. "Regular expression matching radio targets in plain text.")
  4731. (make-variable-buffer-local 'org-target-link-regexp)
  4732. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4733. "Regular expression matching a link target.")
  4734. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4735. "Regular expression matching a radio target.")
  4736. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4737. "Regular expression matching any target.")
  4738. (defun org-activate-target-links (limit)
  4739. "Run through the buffer and add overlays to target matches."
  4740. (when org-target-link-regexp
  4741. (let ((case-fold-search t))
  4742. (if (re-search-forward org-target-link-regexp limit t)
  4743. (progn
  4744. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4745. (add-text-properties (match-beginning 0) (match-end 0)
  4746. (list 'mouse-face 'highlight
  4747. 'keymap org-mouse-map
  4748. 'help-echo "Radio target link"
  4749. 'org-linked-text t))
  4750. (org-rear-nonsticky-at (match-end 0))
  4751. t)))))
  4752. (defun org-update-radio-target-regexp ()
  4753. "Find all radio targets in this file and update the regular expression."
  4754. (interactive)
  4755. (when (memq 'radio org-activate-links)
  4756. (setq org-target-link-regexp
  4757. (org-make-target-link-regexp (org-all-targets 'radio)))
  4758. (org-restart-font-lock)))
  4759. (defun org-hide-wide-columns (limit)
  4760. (let (s e)
  4761. (setq s (text-property-any (point) (or limit (point-max))
  4762. 'org-cwidth t))
  4763. (when s
  4764. (setq e (next-single-property-change s 'org-cwidth))
  4765. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4766. (goto-char e)
  4767. t)))
  4768. (defvar org-latex-and-specials-regexp nil
  4769. "Regular expression for highlighting export special stuff.")
  4770. (defvar org-match-substring-regexp)
  4771. (defvar org-match-substring-with-braces-regexp)
  4772. ;; This should be with the exporter code, but we also use if for font-locking
  4773. (defconst org-export-html-special-string-regexps
  4774. '(("\\\\-" . "&shy;")
  4775. ("---\\([^-]\\)" . "&mdash;\\1")
  4776. ("--\\([^-]\\)" . "&ndash;\\1")
  4777. ("\\.\\.\\." . "&hellip;"))
  4778. "Regular expressions for special string conversion.")
  4779. (defun org-compute-latex-and-specials-regexp ()
  4780. "Compute regular expression for stuff treated specially by exporters."
  4781. (if (not org-highlight-latex-fragments-and-specials)
  4782. (org-set-local 'org-latex-and-specials-regexp nil)
  4783. (require 'org-exp)
  4784. (let*
  4785. ((matchers (plist-get org-format-latex-options :matchers))
  4786. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4787. org-latex-regexps)))
  4788. (org-export-allow-BIND nil)
  4789. (options (org-combine-plists (org-default-export-plist)
  4790. (org-infile-export-plist)))
  4791. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4792. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4793. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4794. (org-export-html-expand (plist-get options :expand-quoted-html))
  4795. (org-export-with-special-strings (plist-get options :special-strings))
  4796. (re-sub
  4797. (cond
  4798. ((equal org-export-with-sub-superscripts '{})
  4799. (list org-match-substring-with-braces-regexp))
  4800. (org-export-with-sub-superscripts
  4801. (list org-match-substring-regexp))
  4802. (t nil)))
  4803. (re-latex
  4804. (if org-export-with-LaTeX-fragments
  4805. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4806. (re-macros
  4807. (if org-export-with-TeX-macros
  4808. (list (concat "\\\\"
  4809. (regexp-opt
  4810. (append
  4811. (delq nil
  4812. (mapcar 'car-safe
  4813. (append org-entities-user
  4814. org-entities)))
  4815. (if (boundp 'org-latex-entities)
  4816. (mapcar (lambda (x)
  4817. (or (car-safe x) x))
  4818. org-latex-entities)
  4819. nil))
  4820. 'words))) ; FIXME
  4821. ))
  4822. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4823. (re-special (if org-export-with-special-strings
  4824. (mapcar (lambda (x) (car x))
  4825. org-export-html-special-string-regexps)))
  4826. (re-rest
  4827. (delq nil
  4828. (list
  4829. (if org-export-html-expand "@<[^>\n]+>")
  4830. ))))
  4831. (org-set-local
  4832. 'org-latex-and-specials-regexp
  4833. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4834. re-rest) "\\|")))))
  4835. (defun org-do-latex-and-special-faces (limit)
  4836. "Run through the buffer and add overlays to links."
  4837. (when org-latex-and-specials-regexp
  4838. (let (rtn d)
  4839. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4840. limit t))
  4841. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4842. 'face))
  4843. '(org-code org-verbatim underline)))
  4844. (progn
  4845. (setq rtn t
  4846. d (cond ((member (char-after (1+ (match-beginning 0)))
  4847. '(?_ ?^)) 1)
  4848. (t 0)))
  4849. (font-lock-prepend-text-property
  4850. (+ d (match-beginning 0)) (match-end 0)
  4851. 'face 'org-latex-and-export-specials)
  4852. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4853. '(font-lock-multiline t)))))
  4854. rtn)))
  4855. (defun org-restart-font-lock ()
  4856. "Restart `font-lock-mode', to force refontification."
  4857. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4858. (font-lock-mode -1)
  4859. (font-lock-mode 1)))
  4860. (defun org-all-targets (&optional radio)
  4861. "Return a list of all targets in this file.
  4862. With optional argument RADIO, only find radio targets."
  4863. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4864. rtn)
  4865. (save-excursion
  4866. (goto-char (point-min))
  4867. (while (re-search-forward re nil t)
  4868. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4869. rtn)))
  4870. (defun org-make-target-link-regexp (targets)
  4871. "Make regular expression matching all strings in TARGETS.
  4872. The regular expression finds the targets also if there is a line break
  4873. between words."
  4874. (and targets
  4875. (concat
  4876. "\\<\\("
  4877. (mapconcat
  4878. (lambda (x)
  4879. (setq x (regexp-quote x))
  4880. (while (string-match " +" x)
  4881. (setq x (replace-match "\\s-+" t t x)))
  4882. x)
  4883. targets
  4884. "\\|")
  4885. "\\)\\>")))
  4886. (defun org-activate-tags (limit)
  4887. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4888. (progn
  4889. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4890. (add-text-properties (match-beginning 1) (match-end 1)
  4891. (list 'mouse-face 'highlight
  4892. 'keymap org-mouse-map))
  4893. (org-rear-nonsticky-at (match-end 1))
  4894. t)))
  4895. (defun org-outline-level ()
  4896. "Compute the outline level of the heading at point.
  4897. This function assumes that the cursor is at the beginning of a line matched
  4898. by `outline-regexp'. Otherwise it returns garbage.
  4899. If this is called at a normal headline, the level is the number of stars.
  4900. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  4901. (save-excursion
  4902. (looking-at outline-regexp)
  4903. (1- (- (match-end 0) (match-beginning 0)))))
  4904. (defvar org-font-lock-keywords nil)
  4905. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4906. "Regular expression matching a property line.")
  4907. (defvar org-font-lock-hook nil
  4908. "Functions to be called for special font lock stuff.")
  4909. (defvar org-font-lock-set-keywords-hook nil
  4910. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4911. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4912. it is installed to be used by font lock. This can be useful if something
  4913. needs to be inserted at a specific position in the font-lock sequence.")
  4914. (defun org-font-lock-hook (limit)
  4915. (run-hook-with-args 'org-font-lock-hook limit))
  4916. (defun org-set-font-lock-defaults ()
  4917. (let* ((em org-fontify-emphasized-text)
  4918. (lk org-activate-links)
  4919. (org-font-lock-extra-keywords
  4920. (list
  4921. ;; Call the hook
  4922. '(org-font-lock-hook)
  4923. ;; Headlines
  4924. `(,(if org-fontify-whole-heading-line
  4925. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4926. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4927. (1 (org-get-level-face 1))
  4928. (2 (org-get-level-face 2))
  4929. (3 (org-get-level-face 3)))
  4930. ;; Table lines
  4931. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4932. (1 'org-table t))
  4933. ;; Table internals
  4934. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4935. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4936. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4937. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4938. ;; Drawers
  4939. (list org-drawer-regexp '(0 'org-special-keyword t))
  4940. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4941. ;; Properties
  4942. (list org-property-re
  4943. '(1 'org-special-keyword t)
  4944. '(3 'org-property-value t))
  4945. ;; Links
  4946. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4947. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4948. (if (memq 'plain lk) '(org-activate-plain-links))
  4949. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4950. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4951. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4952. (if (memq 'footnote lk) '(org-activate-footnote-links
  4953. (2 'org-footnote t)))
  4954. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4955. '(org-hide-wide-columns (0 nil append))
  4956. ;; TODO lines
  4957. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4958. '(1 (org-get-todo-face 1) t))
  4959. ;; DONE
  4960. (if org-fontify-done-headline
  4961. (list (concat "^[*]+ +\\<\\("
  4962. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4963. "\\)\\(.*\\)")
  4964. '(2 'org-headline-done t))
  4965. nil)
  4966. ;; Priorities
  4967. '(org-font-lock-add-priority-faces)
  4968. ;; Tags
  4969. '(org-font-lock-add-tag-faces)
  4970. ;; Special keywords
  4971. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4972. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4973. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4974. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4975. ;; Emphasis
  4976. (if em
  4977. (if (featurep 'xemacs)
  4978. '(org-do-emphasis-faces (0 nil append))
  4979. '(org-do-emphasis-faces)))
  4980. ;; Checkboxes
  4981. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4982. 1 'org-checkbox prepend)
  4983. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4984. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4985. (0 (org-get-checkbox-statistics-face) t)))
  4986. ;; Description list items
  4987. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  4988. 1 'bold prepend)
  4989. ;; ARCHIVEd headings
  4990. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4991. '(1 'org-archived prepend))
  4992. ;; Specials
  4993. '(org-do-latex-and-special-faces)
  4994. '(org-fontify-entities)
  4995. '(org-raise-scripts)
  4996. ;; Code
  4997. '(org-activate-code (1 'org-code t))
  4998. ;; COMMENT
  4999. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5000. "\\|" org-quote-string "\\)\\>")
  5001. '(1 'org-special-keyword t))
  5002. '("^#.*" (0 'font-lock-comment-face t))
  5003. ;; Blocks and meta lines
  5004. '(org-fontify-meta-lines-and-blocks)
  5005. )))
  5006. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5007. (run-hooks 'org-font-lock-set-keywords-hook)
  5008. ;; Now set the full font-lock-keywords
  5009. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5010. (org-set-local 'font-lock-defaults
  5011. '(org-font-lock-keywords t nil nil backward-paragraph))
  5012. (kill-local-variable 'font-lock-keywords) nil))
  5013. (defun org-toggle-pretty-entities ()
  5014. "Toggle the composition display of entities as UTF8 characters."
  5015. (interactive)
  5016. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5017. (org-restart-font-lock)
  5018. (if org-pretty-entities
  5019. (message "Entities are displayed as UTF8 characers")
  5020. (save-restriction
  5021. (widen)
  5022. (org-decompose-region (point-min) (point-max))
  5023. (message "Entities are displayed plain"))))
  5024. (defun org-fontify-entities (limit)
  5025. "Find an entity to fontify."
  5026. (let (ee)
  5027. (when org-pretty-entities
  5028. (catch 'match
  5029. (while (re-search-forward
  5030. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  5031. limit t)
  5032. (if (and (not (org-in-indented-comment-line))
  5033. (setq ee (org-entity-get (match-string 1)))
  5034. (= (length (nth 6 ee)) 1))
  5035. (progn
  5036. (add-text-properties
  5037. (match-beginning 0) (match-end 1)
  5038. (list 'font-lock-fontified t))
  5039. (compose-region (match-beginning 0) (match-end 1)
  5040. (nth 6 ee) nil)
  5041. (backward-char 1)
  5042. (throw 'match t))))
  5043. nil))))
  5044. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5045. "Fontify string S like in Org-mode."
  5046. (with-temp-buffer
  5047. (insert s)
  5048. (let ((org-odd-levels-only odd-levels))
  5049. (org-mode)
  5050. (font-lock-fontify-buffer)
  5051. (buffer-string))))
  5052. (defvar org-m nil)
  5053. (defvar org-l nil)
  5054. (defvar org-f nil)
  5055. (defun org-get-level-face (n)
  5056. "Get the right face for match N in font-lock matching of headlines."
  5057. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5058. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5059. (if org-cycle-level-faces
  5060. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5061. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5062. (cond
  5063. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5064. ((eq n 2) org-f)
  5065. (t (if org-level-color-stars-only nil org-f))))
  5066. (defun org-get-todo-face (kwd)
  5067. "Get the right face for a TODO keyword KWD.
  5068. If KWD is a number, get the corresponding match group."
  5069. (if (numberp kwd) (setq kwd (match-string kwd)))
  5070. (or (org-face-from-face-or-color
  5071. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5072. (and (member kwd org-done-keywords) 'org-done)
  5073. 'org-todo))
  5074. (defun org-face-from-face-or-color (context inherit face-or-color)
  5075. "Create a face list that inherits INHERIT, but sets the foreground color.
  5076. When FACE-OR-COLOR is not a string, just return it."
  5077. (if (stringp face-or-color)
  5078. (list :inherit inherit
  5079. (cdr (assoc context org-faces-easy-properties))
  5080. face-or-color)
  5081. face-or-color))
  5082. (defun org-font-lock-add-tag-faces (limit)
  5083. "Add the special tag faces."
  5084. (when (and org-tag-faces org-tags-special-faces-re)
  5085. (while (re-search-forward org-tags-special-faces-re limit t)
  5086. (add-text-properties (match-beginning 1) (match-end 1)
  5087. (list 'face (org-get-tag-face 1)
  5088. 'font-lock-fontified t))
  5089. (backward-char 1))))
  5090. (defun org-font-lock-add-priority-faces (limit)
  5091. "Add the special priority faces."
  5092. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5093. (add-text-properties
  5094. (match-beginning 0) (match-end 0)
  5095. (list 'face (or (org-face-from-face-or-color
  5096. 'priority 'org-special-keyword
  5097. (cdr (assoc (char-after (match-beginning 1))
  5098. org-priority-faces)))
  5099. 'org-special-keyword)
  5100. 'font-lock-fontified t))))
  5101. (defun org-get-tag-face (kwd)
  5102. "Get the right face for a TODO keyword KWD.
  5103. If KWD is a number, get the corresponding match group."
  5104. (if (numberp kwd) (setq kwd (match-string kwd)))
  5105. (or (org-face-from-face-or-color
  5106. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5107. 'org-tag))
  5108. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5109. "Remove fontification and activation overlays from links."
  5110. (font-lock-default-unfontify-region beg end)
  5111. (let* ((buffer-undo-list t)
  5112. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5113. (inhibit-modification-hooks t)
  5114. deactivate-mark buffer-file-name buffer-file-truename)
  5115. (org-decompose-region beg end)
  5116. (remove-text-properties
  5117. beg end
  5118. (if org-indent-mode
  5119. ;; also remove line-prefix and wrap-prefix properties
  5120. '(mouse-face t keymap t org-linked-text t
  5121. invisible t intangible t
  5122. line-prefix t wrap-prefix t
  5123. org-no-flyspell t org-emphasis t)
  5124. '(mouse-face t keymap t org-linked-text t
  5125. invisible t intangible t
  5126. org-no-flyspell t org-emphasis t)))
  5127. (org-remove-font-lock-display-properties beg end)))
  5128. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5129. ((raise 0.3) (height 0.7))
  5130. ((raise -0.5))
  5131. ((raise 0.5)))
  5132. "Display properties for showing superscripts and subscripts.")
  5133. (defun org-remove-font-lock-display-properties (beg end)
  5134. "Remove specific display properties that have been added by font lock.
  5135. The will remove the raise properties that are used to show superscripts
  5136. and subscripts."
  5137. (let (next prop)
  5138. (while (< beg end)
  5139. (setq next (next-single-property-change beg 'display nil end)
  5140. prop (get-text-property beg 'display))
  5141. (if (member prop org-script-display)
  5142. (put-text-property beg next 'display nil))
  5143. (setq beg next))))
  5144. (defun org-raise-scripts (limit)
  5145. "Add raise properties to sub/superscripts."
  5146. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5147. (if (re-search-forward
  5148. (if (eq org-use-sub-superscripts t)
  5149. org-match-substring-regexp
  5150. org-match-substring-with-braces-regexp)
  5151. limit t)
  5152. (let* ((pos (point)) table-p comment-p
  5153. (mpos (match-beginning 3))
  5154. (emph-p (get-text-property mpos 'org-emphasis))
  5155. (link-p (get-text-property mpos 'mouse-face))
  5156. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5157. (goto-char (point-at-bol))
  5158. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5159. comment-p (org-looking-at-p "[ \t]*#"))
  5160. (goto-char pos)
  5161. ;; FIXME: Should we go back one character here, for a_b^c
  5162. ;; (goto-char (1- pos)) ;????????????????????
  5163. (if (or comment-p emph-p link-p keyw-p)
  5164. t
  5165. (put-text-property (match-beginning 3) (match-end 0)
  5166. 'display
  5167. (if (equal (char-after (match-beginning 2)) ?^)
  5168. (nth (if table-p 3 1) org-script-display)
  5169. (nth (if table-p 2 0) org-script-display)))
  5170. (add-text-properties (match-beginning 2) (match-end 2)
  5171. (list 'invisible t
  5172. 'org-dwidth t 'org-dwidth-n 1))
  5173. (if (and (eq (char-after (match-beginning 3)) ?{)
  5174. (eq (char-before (match-end 3)) ?}))
  5175. (progn
  5176. (add-text-properties
  5177. (match-beginning 3) (1+ (match-beginning 3))
  5178. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5179. (add-text-properties
  5180. (1- (match-end 3)) (match-end 3)
  5181. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5182. t)))))
  5183. ;;;; Visibility cycling, including org-goto and indirect buffer
  5184. ;;; Cycling
  5185. (defvar org-cycle-global-status nil)
  5186. (make-variable-buffer-local 'org-cycle-global-status)
  5187. (defvar org-cycle-subtree-status nil)
  5188. (make-variable-buffer-local 'org-cycle-subtree-status)
  5189. ;;;###autoload
  5190. (defvar org-inlinetask-min-level)
  5191. (defun org-cycle (&optional arg)
  5192. "TAB-action and visibility cycling for Org-mode.
  5193. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5194. is outline visibility cycling, but it also invokes other actions
  5195. in special contexts.
  5196. - When this function is called with a prefix argument, rotate the entire
  5197. buffer through 3 states (global cycling)
  5198. 1. OVERVIEW: Show only top-level headlines.
  5199. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5200. 3. SHOW ALL: Show everything.
  5201. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5202. determined by the variable `org-startup-folded', and by any VISIBILITY
  5203. properties in the buffer.
  5204. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5205. including any drawers.
  5206. - When inside a table, re-align the table and move to the next field.
  5207. - When point is at the beginning of a headline, rotate the subtree started
  5208. by this line through 3 different states (local cycling)
  5209. 1. FOLDED: Only the main headline is shown.
  5210. 2. CHILDREN: The main headline and the direct children are shown.
  5211. From this state, you can move to one of the children
  5212. and zoom in further.
  5213. 3. SUBTREE: Show the entire subtree, including body text.
  5214. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5215. - When point is at the beginning of an empty headline and the variable
  5216. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5217. of the headline by demoting and promoting it to likely levels. This
  5218. speeds up creation document structure by pressing TAB once or several
  5219. times right after creating a new headline.
  5220. - When there is a numeric prefix, go up to a heading with level ARG, do
  5221. a `show-subtree' and return to the previous cursor position. If ARG
  5222. is negative, go up that many levels.
  5223. - When point is not at the beginning of a headline, execute the global
  5224. binding for TAB, which is re-indenting the line. See the option
  5225. `org-cycle-emulate-tab' for details.
  5226. - Special case: if point is at the beginning of the buffer and there is
  5227. no headline in line 1, this function will act as if called with prefix arg
  5228. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5229. But only if also the variable `org-cycle-global-at-bob' is t."
  5230. (interactive "P")
  5231. (org-load-modules-maybe)
  5232. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5233. (and org-cycle-level-after-item/entry-creation
  5234. (or (org-cycle-level)
  5235. (org-cycle-item-indentation))))
  5236. (let* ((limit-level
  5237. (or org-cycle-max-level
  5238. (and (boundp 'org-inlinetask-min-level)
  5239. org-inlinetask-min-level
  5240. (1- org-inlinetask-min-level))))
  5241. (nstars (and limit-level
  5242. (if org-odd-levels-only
  5243. (and limit-level (1- (* limit-level 2)))
  5244. limit-level)))
  5245. (outline-regexp
  5246. (if (not (org-mode-p))
  5247. outline-regexp
  5248. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5249. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5250. (not (looking-at outline-regexp))))
  5251. (org-cycle-hook
  5252. (if bob-special
  5253. (delq 'org-optimize-window-after-visibility-change
  5254. (copy-sequence org-cycle-hook))
  5255. org-cycle-hook))
  5256. (pos (point)))
  5257. (if (or bob-special (equal arg '(4)))
  5258. ;; special case: use global cycling
  5259. (setq arg t))
  5260. (cond
  5261. ((equal arg '(16))
  5262. (setq last-command 'dummy)
  5263. (org-set-startup-visibility)
  5264. (message "Startup visibility, plus VISIBILITY properties"))
  5265. ((equal arg '(64))
  5266. (show-all)
  5267. (message "Entire buffer visible, including drawers"))
  5268. ;; Table: enter it or move to the next field.
  5269. ((org-at-table-p 'any)
  5270. (if (org-at-table.el-p)
  5271. (message "Use C-c ' to edit table.el tables")
  5272. (if arg (org-table-edit-field t)
  5273. (org-table-justify-field-maybe)
  5274. (call-interactively 'org-table-next-field))))
  5275. ((run-hook-with-args-until-success
  5276. 'org-tab-after-check-for-table-hook))
  5277. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5278. ((eq arg t) (org-cycle-internal-global))
  5279. ;; Drawers: delegate to `org-flag-drawer'.
  5280. ((and org-drawers org-drawer-regexp
  5281. (save-excursion
  5282. (beginning-of-line 1)
  5283. (looking-at org-drawer-regexp)))
  5284. (org-flag-drawer ; toggle block visibility
  5285. (not (get-char-property (match-end 0) 'invisible))))
  5286. ;; Show-subtree, ARG levels up from here.
  5287. ((integerp arg)
  5288. (save-excursion
  5289. (org-back-to-heading)
  5290. (outline-up-heading (if (< arg 0) (- arg)
  5291. (- (funcall outline-level) arg)))
  5292. (org-show-subtree)))
  5293. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5294. ((and (featurep 'org-inlinetask)
  5295. (org-inlinetask-at-task-p)
  5296. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5297. (org-inlinetask-toggle-visibility))
  5298. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5299. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5300. (save-excursion (beginning-of-line 1)
  5301. (looking-at outline-regexp)))
  5302. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5303. (org-cycle-internal-local))
  5304. ;; From there: TAB emulation and template completion.
  5305. (buffer-read-only (org-back-to-heading))
  5306. ((run-hook-with-args-until-success
  5307. 'org-tab-after-check-for-cycling-hook))
  5308. ((org-try-structure-completion))
  5309. ((org-try-cdlatex-tab))
  5310. ((run-hook-with-args-until-success
  5311. 'org-tab-before-tab-emulation-hook))
  5312. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5313. (or (not (bolp))
  5314. (not (looking-at outline-regexp))))
  5315. (call-interactively (global-key-binding "\t")))
  5316. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5317. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5318. (or (and (eq org-cycle-emulate-tab 'white)
  5319. (= (match-end 0) (point-at-eol)))
  5320. (and (eq org-cycle-emulate-tab 'whitestart)
  5321. (>= (match-end 0) pos))))
  5322. t
  5323. (eq org-cycle-emulate-tab t))
  5324. (call-interactively (global-key-binding "\t")))
  5325. (t (save-excursion
  5326. (org-back-to-heading)
  5327. (org-cycle)))))))
  5328. (defun org-cycle-internal-global ()
  5329. "Do the global cycling action."
  5330. (cond
  5331. ((and (eq last-command this-command)
  5332. (eq org-cycle-global-status 'overview))
  5333. ;; We just created the overview - now do table of contents
  5334. ;; This can be slow in very large buffers, so indicate action
  5335. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5336. (message "CONTENTS...")
  5337. (org-content)
  5338. (message "CONTENTS...done")
  5339. (setq org-cycle-global-status 'contents)
  5340. (run-hook-with-args 'org-cycle-hook 'contents))
  5341. ((and (eq last-command this-command)
  5342. (eq org-cycle-global-status 'contents))
  5343. ;; We just showed the table of contents - now show everything
  5344. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5345. (show-all)
  5346. (message "SHOW ALL")
  5347. (setq org-cycle-global-status 'all)
  5348. (run-hook-with-args 'org-cycle-hook 'all))
  5349. (t
  5350. ;; Default action: go to overview
  5351. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5352. (org-overview)
  5353. (message "OVERVIEW")
  5354. (setq org-cycle-global-status 'overview)
  5355. (run-hook-with-args 'org-cycle-hook 'overview))))
  5356. (defun org-cycle-internal-local ()
  5357. "Do the local cycling action."
  5358. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5359. ;; First, determine end of headline (EOH), end of subtree or item
  5360. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5361. (save-excursion
  5362. (if (org-at-item-p)
  5363. (progn
  5364. (beginning-of-line)
  5365. (setq struct (org-list-struct))
  5366. (setq eoh (point-at-eol))
  5367. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5368. (setq has-children (org-list-has-child-p (point) struct)))
  5369. (org-back-to-heading)
  5370. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5371. (setq eos (save-excursion
  5372. (org-end-of-subtree t)
  5373. (unless (eobp)
  5374. (skip-chars-forward " \t\n"))
  5375. (if (eobp) (point) (1- (point)))))
  5376. (setq has-children
  5377. (or (save-excursion
  5378. (let ((level (funcall outline-level)))
  5379. (outline-next-heading)
  5380. (and (org-at-heading-p t)
  5381. (> (funcall outline-level) level))))
  5382. (save-excursion
  5383. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5384. ;; Determine end invisible part of buffer (EOL)
  5385. (beginning-of-line 2)
  5386. ;; XEmacs doesn't have `next-single-char-property-change'
  5387. (if (featurep 'xemacs)
  5388. (while (and (not (eobp)) ;; this is like `next-line'
  5389. (get-char-property (1- (point)) 'invisible))
  5390. (beginning-of-line 2))
  5391. (while (and (not (eobp)) ;; this is like `next-line'
  5392. (get-char-property (1- (point)) 'invisible))
  5393. (goto-char (next-single-char-property-change (point) 'invisible))
  5394. (and (eolp) (beginning-of-line 2))))
  5395. (setq eol (point)))
  5396. ;; Find out what to do next and set `this-command'
  5397. (cond
  5398. ((= eos eoh)
  5399. ;; Nothing is hidden behind this heading
  5400. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5401. (message "EMPTY ENTRY")
  5402. (setq org-cycle-subtree-status nil)
  5403. (save-excursion
  5404. (goto-char eos)
  5405. (outline-next-heading)
  5406. (if (outline-invisible-p) (org-flag-heading nil))))
  5407. ((and (or (>= eol eos)
  5408. (not (string-match "\\S-" (buffer-substring eol eos))))
  5409. (or has-children
  5410. (not (setq children-skipped
  5411. org-cycle-skip-children-state-if-no-children))))
  5412. ;; Entire subtree is hidden in one line: children view
  5413. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5414. (if (org-at-item-p)
  5415. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5416. (org-show-entry)
  5417. (show-children)
  5418. ;; Fold every list in subtree to top-level items.
  5419. (when (eq org-cycle-include-plain-lists 'integrate)
  5420. (save-excursion
  5421. (org-back-to-heading)
  5422. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5423. (beginning-of-line 1)
  5424. (let* ((struct (org-list-struct))
  5425. (prevs (org-list-prevs-alist struct))
  5426. (end (org-list-get-bottom-point struct)))
  5427. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5428. (org-list-get-all-items (point) struct prevs))
  5429. (goto-char end))))))
  5430. (message "CHILDREN")
  5431. (save-excursion
  5432. (goto-char eos)
  5433. (outline-next-heading)
  5434. (if (outline-invisible-p) (org-flag-heading nil)))
  5435. (setq org-cycle-subtree-status 'children)
  5436. (run-hook-with-args 'org-cycle-hook 'children))
  5437. ((or children-skipped
  5438. (and (eq last-command this-command)
  5439. (eq org-cycle-subtree-status 'children)))
  5440. ;; We just showed the children, or no children are there,
  5441. ;; now show everything.
  5442. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5443. (outline-flag-region eoh eos nil)
  5444. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5445. (setq org-cycle-subtree-status 'subtree)
  5446. (run-hook-with-args 'org-cycle-hook 'subtree))
  5447. (t
  5448. ;; Default action: hide the subtree.
  5449. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5450. (outline-flag-region eoh eos t)
  5451. (message "FOLDED")
  5452. (setq org-cycle-subtree-status 'folded)
  5453. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5454. ;;;###autoload
  5455. (defun org-global-cycle (&optional arg)
  5456. "Cycle the global visibility. For details see `org-cycle'.
  5457. With \\[universal-argument] prefix arg, switch to startup visibility.
  5458. With a numeric prefix, show all headlines up to that level."
  5459. (interactive "P")
  5460. (let ((org-cycle-include-plain-lists
  5461. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5462. (cond
  5463. ((integerp arg)
  5464. (show-all)
  5465. (hide-sublevels arg)
  5466. (setq org-cycle-global-status 'contents))
  5467. ((equal arg '(4))
  5468. (org-set-startup-visibility)
  5469. (message "Startup visibility, plus VISIBILITY properties."))
  5470. (t
  5471. (org-cycle '(4))))))
  5472. (defun org-set-startup-visibility ()
  5473. "Set the visibility required by startup options and properties."
  5474. (cond
  5475. ((eq org-startup-folded t)
  5476. (org-cycle '(4)))
  5477. ((eq org-startup-folded 'content)
  5478. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5479. (org-cycle '(4)) (org-cycle '(4)))))
  5480. (unless (eq org-startup-folded 'showeverything)
  5481. (if org-hide-block-startup (org-hide-block-all))
  5482. (org-set-visibility-according-to-property 'no-cleanup)
  5483. (org-cycle-hide-archived-subtrees 'all)
  5484. (org-cycle-hide-drawers 'all)
  5485. (org-cycle-show-empty-lines t)))
  5486. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5487. "Switch subtree visibilities according to :VISIBILITY: property."
  5488. (interactive)
  5489. (let (org-show-entry-below state)
  5490. (save-excursion
  5491. (goto-char (point-min))
  5492. (while (re-search-forward
  5493. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5494. nil t)
  5495. (setq state (match-string 1))
  5496. (save-excursion
  5497. (org-back-to-heading t)
  5498. (hide-subtree)
  5499. (org-reveal)
  5500. (cond
  5501. ((equal state '("fold" "folded"))
  5502. (hide-subtree))
  5503. ((equal state "children")
  5504. (org-show-hidden-entry)
  5505. (show-children))
  5506. ((equal state "content")
  5507. (save-excursion
  5508. (save-restriction
  5509. (org-narrow-to-subtree)
  5510. (org-content))))
  5511. ((member state '("all" "showall"))
  5512. (show-subtree)))))
  5513. (unless no-cleanup
  5514. (org-cycle-hide-archived-subtrees 'all)
  5515. (org-cycle-hide-drawers 'all)
  5516. (org-cycle-show-empty-lines 'all)))))
  5517. (defun org-overview ()
  5518. "Switch to overview mode, showing only top-level headlines.
  5519. Really, this shows all headlines with level equal or greater than the level
  5520. of the first headline in the buffer. This is important, because if the
  5521. first headline is not level one, then (hide-sublevels 1) gives confusing
  5522. results."
  5523. (interactive)
  5524. (let ((level (save-excursion
  5525. (goto-char (point-min))
  5526. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5527. (progn
  5528. (goto-char (match-beginning 0))
  5529. (funcall outline-level))))))
  5530. (and level (hide-sublevels level))))
  5531. (defun org-content (&optional arg)
  5532. "Show all headlines in the buffer, like a table of contents.
  5533. With numerical argument N, show content up to level N."
  5534. (interactive "P")
  5535. (save-excursion
  5536. ;; Visit all headings and show their offspring
  5537. (and (integerp arg) (org-overview))
  5538. (goto-char (point-max))
  5539. (catch 'exit
  5540. (while (and (progn (condition-case nil
  5541. (outline-previous-visible-heading 1)
  5542. (error (goto-char (point-min))))
  5543. t)
  5544. (looking-at outline-regexp))
  5545. (if (integerp arg)
  5546. (show-children (1- arg))
  5547. (show-branches))
  5548. (if (bobp) (throw 'exit nil))))))
  5549. (defun org-optimize-window-after-visibility-change (state)
  5550. "Adjust the window after a change in outline visibility.
  5551. This function is the default value of the hook `org-cycle-hook'."
  5552. (when (get-buffer-window (current-buffer))
  5553. (cond
  5554. ((eq state 'content) nil)
  5555. ((eq state 'all) nil)
  5556. ((eq state 'folded) nil)
  5557. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5558. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5559. (defun org-remove-empty-overlays-at (pos)
  5560. "Remove outline overlays that do not contain non-white stuff."
  5561. (mapc
  5562. (lambda (o)
  5563. (and (eq 'outline (overlay-get o 'invisible))
  5564. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5565. (overlay-end o))))
  5566. (delete-overlay o)))
  5567. (overlays-at pos)))
  5568. (defun org-clean-visibility-after-subtree-move ()
  5569. "Fix visibility issues after moving a subtree."
  5570. ;; First, find a reasonable region to look at:
  5571. ;; Start two siblings above, end three below
  5572. (let* ((beg (save-excursion
  5573. (and (org-get-last-sibling)
  5574. (org-get-last-sibling))
  5575. (point)))
  5576. (end (save-excursion
  5577. (and (org-get-next-sibling)
  5578. (org-get-next-sibling)
  5579. (org-get-next-sibling))
  5580. (if (org-at-heading-p)
  5581. (point-at-eol)
  5582. (point))))
  5583. (level (looking-at "\\*+"))
  5584. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5585. (save-excursion
  5586. (save-restriction
  5587. (narrow-to-region beg end)
  5588. (when re
  5589. ;; Properly fold already folded siblings
  5590. (goto-char (point-min))
  5591. (while (re-search-forward re nil t)
  5592. (if (and (not (outline-invisible-p))
  5593. (save-excursion
  5594. (goto-char (point-at-eol)) (outline-invisible-p)))
  5595. (hide-entry))))
  5596. (org-cycle-show-empty-lines 'overview)
  5597. (org-cycle-hide-drawers 'overview)))))
  5598. (defun org-cycle-show-empty-lines (state)
  5599. "Show empty lines above all visible headlines.
  5600. The region to be covered depends on STATE when called through
  5601. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5602. entire buffer covered. Note that an empty line is only shown if there
  5603. are at least `org-cycle-separator-lines' empty lines before the headline."
  5604. (when (not (= org-cycle-separator-lines 0))
  5605. (save-excursion
  5606. (let* ((n (abs org-cycle-separator-lines))
  5607. (re (cond
  5608. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5609. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5610. (t (let ((ns (number-to-string (- n 2))))
  5611. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5612. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5613. beg end b e)
  5614. (cond
  5615. ((memq state '(overview contents t))
  5616. (setq beg (point-min) end (point-max)))
  5617. ((memq state '(children folded))
  5618. (setq beg (point) end (progn (org-end-of-subtree t t)
  5619. (beginning-of-line 2)
  5620. (point)))))
  5621. (when beg
  5622. (goto-char beg)
  5623. (while (re-search-forward re end t)
  5624. (unless (get-char-property (match-end 1) 'invisible)
  5625. (setq e (match-end 1))
  5626. (if (< org-cycle-separator-lines 0)
  5627. (setq b (save-excursion
  5628. (goto-char (match-beginning 0))
  5629. (org-back-over-empty-lines)
  5630. (if (save-excursion
  5631. (goto-char (max (point-min) (1- (point))))
  5632. (org-on-heading-p))
  5633. (1- (point))
  5634. (point))))
  5635. (setq b (match-beginning 1)))
  5636. (outline-flag-region b e nil)))))))
  5637. ;; Never hide empty lines at the end of the file.
  5638. (save-excursion
  5639. (goto-char (point-max))
  5640. (outline-previous-heading)
  5641. (outline-end-of-heading)
  5642. (if (and (looking-at "[ \t\n]+")
  5643. (= (match-end 0) (point-max)))
  5644. (outline-flag-region (point) (match-end 0) nil))))
  5645. (defun org-show-empty-lines-in-parent ()
  5646. "Move to the parent and re-show empty lines before visible headlines."
  5647. (save-excursion
  5648. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5649. (org-cycle-show-empty-lines context))))
  5650. (defun org-files-list ()
  5651. "Return `org-agenda-files' list, plus all open org-mode files.
  5652. This is useful for operations that need to scan all of a user's
  5653. open and agenda-wise Org files."
  5654. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5655. (dolist (buf (buffer-list))
  5656. (with-current-buffer buf
  5657. (if (and (org-mode-p) (buffer-file-name))
  5658. (let ((file (expand-file-name (buffer-file-name))))
  5659. (unless (member file files)
  5660. (push file files))))))
  5661. files))
  5662. (defsubst org-entry-beginning-position ()
  5663. "Return the beginning position of the current entry."
  5664. (save-excursion (outline-back-to-heading t) (point)))
  5665. (defsubst org-entry-end-position ()
  5666. "Return the end position of the current entry."
  5667. (save-excursion (outline-next-heading) (point)))
  5668. (defun org-cycle-hide-drawers (state)
  5669. "Re-hide all drawers after a visibility state change."
  5670. (when (and (org-mode-p)
  5671. (not (memq state '(overview folded contents))))
  5672. (save-excursion
  5673. (let* ((globalp (memq state '(contents all)))
  5674. (beg (if globalp (point-min) (point)))
  5675. (end (if globalp (point-max)
  5676. (if (eq state 'children)
  5677. (save-excursion (outline-next-heading) (point))
  5678. (org-end-of-subtree t)))))
  5679. (goto-char beg)
  5680. (while (re-search-forward org-drawer-regexp end t)
  5681. (org-flag-drawer t))))))
  5682. (defun org-flag-drawer (flag)
  5683. (save-excursion
  5684. (beginning-of-line 1)
  5685. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5686. (let ((b (match-end 0))
  5687. (outline-regexp org-outline-regexp))
  5688. (if (re-search-forward
  5689. "^[ \t]*:END:"
  5690. (save-excursion (outline-next-heading) (point)) t)
  5691. (outline-flag-region b (point-at-eol) flag)
  5692. (error ":END: line missing at position %s" b))))))
  5693. (defun org-subtree-end-visible-p ()
  5694. "Is the end of the current subtree visible?"
  5695. (pos-visible-in-window-p
  5696. (save-excursion (org-end-of-subtree t) (point))))
  5697. (defun org-first-headline-recenter (&optional N)
  5698. "Move cursor to the first headline and recenter the headline.
  5699. Optional argument N means put the headline into the Nth line of the window."
  5700. (goto-char (point-min))
  5701. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5702. (beginning-of-line)
  5703. (recenter (prefix-numeric-value N))))
  5704. ;;; Saving and restoring visibility
  5705. (defun org-outline-overlay-data (&optional use-markers)
  5706. "Return a list of the locations of all outline overlays.
  5707. These are overlays with the `invisible' property value `outline'.
  5708. The return value is a list of cons cells, with start and stop
  5709. positions for each overlay.
  5710. If USE-MARKERS is set, return the positions as markers."
  5711. (let (beg end)
  5712. (save-excursion
  5713. (save-restriction
  5714. (widen)
  5715. (delq nil
  5716. (mapcar (lambda (o)
  5717. (when (eq (overlay-get o 'invisible) 'outline)
  5718. (setq beg (overlay-start o)
  5719. end (overlay-end o))
  5720. (and beg end (> end beg)
  5721. (if use-markers
  5722. (cons (move-marker (make-marker) beg)
  5723. (move-marker (make-marker) end))
  5724. (cons beg end)))))
  5725. (overlays-in (point-min) (point-max))))))))
  5726. (defun org-set-outline-overlay-data (data)
  5727. "Create visibility overlays for all positions in DATA.
  5728. DATA should have been made by `org-outline-overlay-data'."
  5729. (let (o)
  5730. (save-excursion
  5731. (save-restriction
  5732. (widen)
  5733. (show-all)
  5734. (mapc (lambda (c)
  5735. (setq o (make-overlay (car c) (cdr c)))
  5736. (overlay-put o 'invisible 'outline))
  5737. data)))))
  5738. ;;; Folding of blocks
  5739. (defconst org-block-regexp
  5740. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  5741. "Regular expression for hiding blocks.")
  5742. (defvar org-hide-block-overlays nil
  5743. "Overlays hiding blocks.")
  5744. (make-variable-buffer-local 'org-hide-block-overlays)
  5745. (defun org-block-map (function &optional start end)
  5746. "Call FUNCTION at the head of all source blocks in the current buffer.
  5747. Optional arguments START and END can be used to limit the range."
  5748. (let ((start (or start (point-min)))
  5749. (end (or end (point-max))))
  5750. (save-excursion
  5751. (goto-char start)
  5752. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5753. (save-excursion
  5754. (save-match-data
  5755. (goto-char (match-beginning 0))
  5756. (funcall function)))))))
  5757. (defun org-hide-block-toggle-all ()
  5758. "Toggle the visibility of all blocks in the current buffer."
  5759. (org-block-map #'org-hide-block-toggle))
  5760. (defun org-hide-block-all ()
  5761. "Fold all blocks in the current buffer."
  5762. (interactive)
  5763. (org-show-block-all)
  5764. (org-block-map #'org-hide-block-toggle-maybe))
  5765. (defun org-show-block-all ()
  5766. "Unfold all blocks in the current buffer."
  5767. (interactive)
  5768. (mapc 'delete-overlay org-hide-block-overlays)
  5769. (setq org-hide-block-overlays nil))
  5770. (defun org-hide-block-toggle-maybe ()
  5771. "Toggle visibility of block at point."
  5772. (interactive)
  5773. (let ((case-fold-search t))
  5774. (if (save-excursion
  5775. (beginning-of-line 1)
  5776. (looking-at org-block-regexp))
  5777. (progn (org-hide-block-toggle)
  5778. t) ;; to signal that we took action
  5779. nil))) ;; to signal that we did not
  5780. (defun org-hide-block-toggle (&optional force)
  5781. "Toggle the visibility of the current block."
  5782. (interactive)
  5783. (save-excursion
  5784. (beginning-of-line)
  5785. (if (re-search-forward org-block-regexp nil t)
  5786. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5787. (end (match-end 0)) ;; end of entire body
  5788. ov)
  5789. (if (memq t (mapcar (lambda (overlay)
  5790. (eq (overlay-get overlay 'invisible)
  5791. 'org-hide-block))
  5792. (overlays-at start)))
  5793. (if (or (not force) (eq force 'off))
  5794. (mapc (lambda (ov)
  5795. (when (member ov org-hide-block-overlays)
  5796. (setq org-hide-block-overlays
  5797. (delq ov org-hide-block-overlays)))
  5798. (when (eq (overlay-get ov 'invisible)
  5799. 'org-hide-block)
  5800. (delete-overlay ov)))
  5801. (overlays-at start)))
  5802. (setq ov (make-overlay start end))
  5803. (overlay-put ov 'invisible 'org-hide-block)
  5804. ;; make the block accessible to isearch
  5805. (overlay-put
  5806. ov 'isearch-open-invisible
  5807. (lambda (ov)
  5808. (when (member ov org-hide-block-overlays)
  5809. (setq org-hide-block-overlays
  5810. (delq ov org-hide-block-overlays)))
  5811. (when (eq (overlay-get ov 'invisible)
  5812. 'org-hide-block)
  5813. (delete-overlay ov))))
  5814. (push ov org-hide-block-overlays)))
  5815. (error "Not looking at a source block"))))
  5816. ;; org-tab-after-check-for-cycling-hook
  5817. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5818. ;; Remove overlays when changing major mode
  5819. (add-hook 'org-mode-hook
  5820. (lambda () (org-add-hook 'change-major-mode-hook
  5821. 'org-show-block-all 'append 'local)))
  5822. ;;; Org-goto
  5823. (defvar org-goto-window-configuration nil)
  5824. (defvar org-goto-marker nil)
  5825. (defvar org-goto-map
  5826. (let ((map (make-sparse-keymap)))
  5827. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5828. (while (setq cmd (pop cmds))
  5829. (substitute-key-definition cmd cmd map global-map)))
  5830. (suppress-keymap map)
  5831. (org-defkey map "\C-m" 'org-goto-ret)
  5832. (org-defkey map [(return)] 'org-goto-ret)
  5833. (org-defkey map [(left)] 'org-goto-left)
  5834. (org-defkey map [(right)] 'org-goto-right)
  5835. (org-defkey map [(control ?g)] 'org-goto-quit)
  5836. (org-defkey map "\C-i" 'org-cycle)
  5837. (org-defkey map [(tab)] 'org-cycle)
  5838. (org-defkey map [(down)] 'outline-next-visible-heading)
  5839. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5840. (if org-goto-auto-isearch
  5841. (if (fboundp 'define-key-after)
  5842. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5843. nil)
  5844. (org-defkey map "q" 'org-goto-quit)
  5845. (org-defkey map "n" 'outline-next-visible-heading)
  5846. (org-defkey map "p" 'outline-previous-visible-heading)
  5847. (org-defkey map "f" 'outline-forward-same-level)
  5848. (org-defkey map "b" 'outline-backward-same-level)
  5849. (org-defkey map "u" 'outline-up-heading))
  5850. (org-defkey map "/" 'org-occur)
  5851. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5852. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5853. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5854. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5855. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5856. map))
  5857. (defconst org-goto-help
  5858. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5859. RET=jump to location [Q]uit and return to previous location
  5860. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5861. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5862. ;; FIXME: Docstring does not mention both interfaces
  5863. (defun org-goto (&optional alternative-interface)
  5864. "Look up a different location in the current file, keeping current visibility.
  5865. When you want look-up or go to a different location in a document, the
  5866. fastest way is often to fold the entire buffer and then dive into the tree.
  5867. This method has the disadvantage, that the previous location will be folded,
  5868. which may not be what you want.
  5869. This command works around this by showing a copy of the current buffer
  5870. in an indirect buffer, in overview mode. You can dive into the tree in
  5871. that copy, use org-occur and incremental search to find a location.
  5872. When pressing RET or `Q', the command returns to the original buffer in
  5873. which the visibility is still unchanged. After RET is will also jump to
  5874. the location selected in the indirect buffer and expose the
  5875. the headline hierarchy above."
  5876. (interactive "P")
  5877. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5878. (org-refile-use-outline-path t)
  5879. (org-refile-target-verify-function nil)
  5880. (interface
  5881. (if (not alternative-interface)
  5882. org-goto-interface
  5883. (if (eq org-goto-interface 'outline)
  5884. 'outline-path-completion
  5885. 'outline)))
  5886. (org-goto-start-pos (point))
  5887. (selected-point
  5888. (if (eq interface 'outline)
  5889. (car (org-get-location (current-buffer) org-goto-help))
  5890. (let ((pa (org-refile-get-location "Goto")))
  5891. (org-refile-check-position pa)
  5892. (nth 3 pa)))))
  5893. (if selected-point
  5894. (progn
  5895. (org-mark-ring-push org-goto-start-pos)
  5896. (goto-char selected-point)
  5897. (if (or (outline-invisible-p) (org-invisible-p2))
  5898. (org-show-context 'org-goto)))
  5899. (message "Quit"))))
  5900. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5901. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5902. (defvar org-goto-local-auto-isearch-map) ; defined below
  5903. (defun org-get-location (buf help)
  5904. "Let the user select a location in the Org-mode buffer BUF.
  5905. This function uses a recursive edit. It returns the selected position
  5906. or nil."
  5907. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5908. (isearch-hide-immediately nil)
  5909. (isearch-search-fun-function
  5910. (lambda () 'org-goto-local-search-headings))
  5911. (org-goto-selected-point org-goto-exit-command)
  5912. (pop-up-frames nil)
  5913. (special-display-buffer-names nil)
  5914. (special-display-regexps nil)
  5915. (special-display-function nil))
  5916. (save-excursion
  5917. (save-window-excursion
  5918. (delete-other-windows)
  5919. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5920. (switch-to-buffer
  5921. (condition-case nil
  5922. (make-indirect-buffer (current-buffer) "*org-goto*")
  5923. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5924. (with-output-to-temp-buffer "*Help*"
  5925. (princ help))
  5926. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5927. (setq buffer-read-only nil)
  5928. (let ((org-startup-truncated t)
  5929. (org-startup-folded nil)
  5930. (org-startup-align-all-tables nil))
  5931. (org-mode)
  5932. (org-overview))
  5933. (setq buffer-read-only t)
  5934. (if (and (boundp 'org-goto-start-pos)
  5935. (integer-or-marker-p org-goto-start-pos))
  5936. (let ((org-show-hierarchy-above t)
  5937. (org-show-siblings t)
  5938. (org-show-following-heading t))
  5939. (goto-char org-goto-start-pos)
  5940. (and (outline-invisible-p) (org-show-context)))
  5941. (goto-char (point-min)))
  5942. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5943. (message "Select location and press RET")
  5944. (use-local-map org-goto-map)
  5945. (recursive-edit)
  5946. ))
  5947. (kill-buffer "*org-goto*")
  5948. (cons org-goto-selected-point org-goto-exit-command)))
  5949. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5950. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5951. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5952. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5953. (defun org-goto-local-search-headings (string bound noerror)
  5954. "Search and make sure that any matches are in headlines."
  5955. (catch 'return
  5956. (while (if isearch-forward
  5957. (search-forward string bound noerror)
  5958. (search-backward string bound noerror))
  5959. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5960. (and (member :headline context)
  5961. (not (member :tags context))))
  5962. (throw 'return (point))))))
  5963. (defun org-goto-local-auto-isearch ()
  5964. "Start isearch."
  5965. (interactive)
  5966. (goto-char (point-min))
  5967. (let ((keys (this-command-keys)))
  5968. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5969. (isearch-mode t)
  5970. (isearch-process-search-char (string-to-char keys)))))
  5971. (defun org-goto-ret (&optional arg)
  5972. "Finish `org-goto' by going to the new location."
  5973. (interactive "P")
  5974. (setq org-goto-selected-point (point)
  5975. org-goto-exit-command 'return)
  5976. (throw 'exit nil))
  5977. (defun org-goto-left ()
  5978. "Finish `org-goto' by going to the new location."
  5979. (interactive)
  5980. (if (org-on-heading-p)
  5981. (progn
  5982. (beginning-of-line 1)
  5983. (setq org-goto-selected-point (point)
  5984. org-goto-exit-command 'left)
  5985. (throw 'exit nil))
  5986. (error "Not on a heading")))
  5987. (defun org-goto-right ()
  5988. "Finish `org-goto' by going to the new location."
  5989. (interactive)
  5990. (if (org-on-heading-p)
  5991. (progn
  5992. (setq org-goto-selected-point (point)
  5993. org-goto-exit-command 'right)
  5994. (throw 'exit nil))
  5995. (error "Not on a heading")))
  5996. (defun org-goto-quit ()
  5997. "Finish `org-goto' without cursor motion."
  5998. (interactive)
  5999. (setq org-goto-selected-point nil)
  6000. (setq org-goto-exit-command 'quit)
  6001. (throw 'exit nil))
  6002. ;;; Indirect buffer display of subtrees
  6003. (defvar org-indirect-dedicated-frame nil
  6004. "This is the frame being used for indirect tree display.")
  6005. (defvar org-last-indirect-buffer nil)
  6006. (defun org-tree-to-indirect-buffer (&optional arg)
  6007. "Create indirect buffer and narrow it to current subtree.
  6008. With numerical prefix ARG, go up to this level and then take that tree.
  6009. If ARG is negative, go up that many levels.
  6010. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6011. indirect buffer previously made with this command, to avoid proliferation of
  6012. indirect buffers. However, when you call the command with a \
  6013. \\[universal-argument] prefix, or
  6014. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6015. is kept so that you can work with several indirect buffers at the same time.
  6016. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6017. \\[universal-argument] prefix also
  6018. requests that a new frame be made for the new buffer, so that the dedicated
  6019. frame is not changed."
  6020. (interactive "P")
  6021. (let ((cbuf (current-buffer))
  6022. (cwin (selected-window))
  6023. (pos (point))
  6024. beg end level heading ibuf)
  6025. (save-excursion
  6026. (org-back-to-heading t)
  6027. (when (numberp arg)
  6028. (setq level (org-outline-level))
  6029. (if (< arg 0) (setq arg (+ level arg)))
  6030. (while (> (setq level (org-outline-level)) arg)
  6031. (outline-up-heading 1 t)))
  6032. (setq beg (point)
  6033. heading (org-get-heading))
  6034. (org-end-of-subtree t t)
  6035. (if (org-on-heading-p) (backward-char 1))
  6036. (setq end (point)))
  6037. (if (and (buffer-live-p org-last-indirect-buffer)
  6038. (not (eq org-indirect-buffer-display 'new-frame))
  6039. (not arg))
  6040. (kill-buffer org-last-indirect-buffer))
  6041. (setq ibuf (org-get-indirect-buffer cbuf)
  6042. org-last-indirect-buffer ibuf)
  6043. (cond
  6044. ((or (eq org-indirect-buffer-display 'new-frame)
  6045. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6046. (select-frame (make-frame))
  6047. (delete-other-windows)
  6048. (switch-to-buffer ibuf)
  6049. (org-set-frame-title heading))
  6050. ((eq org-indirect-buffer-display 'dedicated-frame)
  6051. (raise-frame
  6052. (select-frame (or (and org-indirect-dedicated-frame
  6053. (frame-live-p org-indirect-dedicated-frame)
  6054. org-indirect-dedicated-frame)
  6055. (setq org-indirect-dedicated-frame (make-frame)))))
  6056. (delete-other-windows)
  6057. (switch-to-buffer ibuf)
  6058. (org-set-frame-title (concat "Indirect: " heading)))
  6059. ((eq org-indirect-buffer-display 'current-window)
  6060. (switch-to-buffer ibuf))
  6061. ((eq org-indirect-buffer-display 'other-window)
  6062. (pop-to-buffer ibuf))
  6063. (t (error "Invalid value")))
  6064. (if (featurep 'xemacs)
  6065. (save-excursion (org-mode) (turn-on-font-lock)))
  6066. (narrow-to-region beg end)
  6067. (show-all)
  6068. (goto-char pos)
  6069. (and (window-live-p cwin) (select-window cwin))))
  6070. (defun org-get-indirect-buffer (&optional buffer)
  6071. (setq buffer (or buffer (current-buffer)))
  6072. (let ((n 1) (base (buffer-name buffer)) bname)
  6073. (while (buffer-live-p
  6074. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6075. (setq n (1+ n)))
  6076. (condition-case nil
  6077. (make-indirect-buffer buffer bname 'clone)
  6078. (error (make-indirect-buffer buffer bname)))))
  6079. (defun org-set-frame-title (title)
  6080. "Set the title of the current frame to the string TITLE."
  6081. ;; FIXME: how to name a single frame in XEmacs???
  6082. (unless (featurep 'xemacs)
  6083. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6084. ;;;; Structure editing
  6085. ;;; Inserting headlines
  6086. (defun org-previous-line-empty-p ()
  6087. (save-excursion
  6088. (and (not (bobp))
  6089. (or (beginning-of-line 0) t)
  6090. (save-match-data
  6091. (looking-at "[ \t]*$")))))
  6092. (defun org-insert-heading (&optional force-heading invisible-ok)
  6093. "Insert a new heading or item with same depth at point.
  6094. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6095. If point is at the beginning of a headline, insert a sibling before the
  6096. current headline. If point is not at the beginning, split the line,
  6097. create the new headline with the text in the current line after point
  6098. \(but see also the variable `org-M-RET-may-split-line').
  6099. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6100. This is important for non-interactive uses of the command."
  6101. (interactive "P")
  6102. (if (or (= (buffer-size) 0)
  6103. (and (not (save-excursion
  6104. (and (ignore-errors (org-back-to-heading invisible-ok))
  6105. (org-on-heading-p))))
  6106. (not (org-in-item-p))))
  6107. (progn
  6108. (insert "\n* ")
  6109. (run-hooks 'org-insert-heading-hook))
  6110. (when (or force-heading (not (org-insert-item)))
  6111. (let* ((empty-line-p nil)
  6112. (level nil)
  6113. (on-heading (org-on-heading-p))
  6114. (head (save-excursion
  6115. (condition-case nil
  6116. (progn
  6117. (org-back-to-heading invisible-ok)
  6118. (when (and (not on-heading)
  6119. (featurep 'org-inlinetask)
  6120. (integerp org-inlinetask-min-level)
  6121. (>= (length (match-string 0))
  6122. org-inlinetask-min-level))
  6123. ;; Find a heading level before the inline task
  6124. (while (and (setq level (org-up-heading-safe))
  6125. (>= level org-inlinetask-min-level)))
  6126. (if (org-on-heading-p)
  6127. (org-back-to-heading invisible-ok)
  6128. (error "This should not happen")))
  6129. (setq empty-line-p (org-previous-line-empty-p))
  6130. (match-string 0))
  6131. (error "*"))))
  6132. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6133. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6134. pos hide-previous previous-pos)
  6135. (cond
  6136. ((and (org-on-heading-p) (bolp)
  6137. (or (bobp)
  6138. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6139. ;; insert before the current line
  6140. (open-line (if blank 2 1)))
  6141. ((and (bolp)
  6142. (not org-insert-heading-respect-content)
  6143. (or (bobp)
  6144. (save-excursion
  6145. (backward-char 1) (not (outline-invisible-p)))))
  6146. ;; insert right here
  6147. nil)
  6148. (t
  6149. ;; somewhere in the line
  6150. (save-excursion
  6151. (setq previous-pos (point-at-bol))
  6152. (end-of-line)
  6153. (setq hide-previous (outline-invisible-p)))
  6154. (and org-insert-heading-respect-content (org-show-subtree))
  6155. (let ((split
  6156. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6157. (save-excursion
  6158. (let ((p (point)))
  6159. (goto-char (point-at-bol))
  6160. (and (looking-at org-complex-heading-regexp)
  6161. (> p (match-beginning 4)))))))
  6162. tags pos)
  6163. (cond
  6164. (org-insert-heading-respect-content
  6165. (org-end-of-subtree nil t)
  6166. (when (featurep 'org-inlinetask)
  6167. (while (and (not (eobp))
  6168. (looking-at "\\(\\*+\\)[ \t]+")
  6169. (>= (length (match-string 1))
  6170. org-inlinetask-min-level))
  6171. (org-end-of-subtree nil t)))
  6172. (or (bolp) (newline))
  6173. (or (org-previous-line-empty-p)
  6174. (and blank (newline)))
  6175. (open-line 1))
  6176. ((org-on-heading-p)
  6177. (when hide-previous
  6178. (show-children)
  6179. (org-show-entry))
  6180. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6181. (setq tags (and (match-end 2) (match-string 2)))
  6182. (and (match-end 1)
  6183. (delete-region (match-beginning 1) (match-end 1)))
  6184. (setq pos (point-at-bol))
  6185. (or split (end-of-line 1))
  6186. (delete-horizontal-space)
  6187. (if (string-match "\\`\\*+\\'"
  6188. (buffer-substring (point-at-bol) (point)))
  6189. (insert " "))
  6190. (newline (if blank 2 1))
  6191. (when tags
  6192. (save-excursion
  6193. (goto-char pos)
  6194. (end-of-line 1)
  6195. (insert " " tags)
  6196. (org-set-tags nil 'align))))
  6197. (t
  6198. (or split (end-of-line 1))
  6199. (newline (if blank 2 1)))))))
  6200. (insert head) (just-one-space)
  6201. (setq pos (point))
  6202. (end-of-line 1)
  6203. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6204. (when (and org-insert-heading-respect-content hide-previous)
  6205. (save-excursion
  6206. (goto-char previous-pos)
  6207. (hide-subtree)))
  6208. (run-hooks 'org-insert-heading-hook)))))
  6209. (defun org-get-heading (&optional no-tags)
  6210. "Return the heading of the current entry, without the stars."
  6211. (save-excursion
  6212. (org-back-to-heading t)
  6213. (if (looking-at
  6214. (if no-tags
  6215. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6216. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6217. (match-string 1) "")))
  6218. (defun org-heading-components ()
  6219. "Return the components of the current heading.
  6220. This is a list with the following elements:
  6221. - the level as an integer
  6222. - the reduced level, different if `org-odd-levels-only' is set.
  6223. - the TODO keyword, or nil
  6224. - the priority character, like ?A, or nil if no priority is given
  6225. - the headline text itself, or the tags string if no headline text
  6226. - the tags string, or nil."
  6227. (save-excursion
  6228. (org-back-to-heading t)
  6229. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6230. (list (length (match-string 1))
  6231. (org-reduced-level (length (match-string 1)))
  6232. (org-match-string-no-properties 2)
  6233. (and (match-end 3) (aref (match-string 3) 2))
  6234. (org-match-string-no-properties 4)
  6235. (org-match-string-no-properties 5)))))
  6236. (defun org-get-entry ()
  6237. "Get the entry text, after heading, entire subtree."
  6238. (save-excursion
  6239. (org-back-to-heading t)
  6240. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6241. (defun org-insert-heading-after-current ()
  6242. "Insert a new heading with same level as current, after current subtree."
  6243. (interactive)
  6244. (org-back-to-heading)
  6245. (org-insert-heading)
  6246. (org-move-subtree-down)
  6247. (end-of-line 1))
  6248. (defun org-insert-heading-respect-content ()
  6249. (interactive)
  6250. (let ((org-insert-heading-respect-content t))
  6251. (org-insert-heading t)))
  6252. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6253. (interactive "P")
  6254. (let ((org-insert-heading-respect-content t))
  6255. (org-insert-todo-heading force-state t)))
  6256. (defun org-insert-todo-heading (arg &optional force-heading)
  6257. "Insert a new heading with the same level and TODO state as current heading.
  6258. If the heading has no TODO state, or if the state is DONE, use the first
  6259. state (TODO by default). Also with prefix arg, force first state."
  6260. (interactive "P")
  6261. (when (or force-heading (not (org-insert-item 'checkbox)))
  6262. (org-insert-heading force-heading)
  6263. (save-excursion
  6264. (org-back-to-heading)
  6265. (outline-previous-heading)
  6266. (looking-at org-todo-line-regexp))
  6267. (let*
  6268. ((new-mark-x
  6269. (if (or arg
  6270. (not (match-beginning 2))
  6271. (member (match-string 2) org-done-keywords))
  6272. (car org-todo-keywords-1)
  6273. (match-string 2)))
  6274. (new-mark
  6275. (or
  6276. (run-hook-with-args-until-success
  6277. 'org-todo-get-default-hook new-mark-x nil)
  6278. new-mark-x)))
  6279. (beginning-of-line 1)
  6280. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6281. (if org-treat-insert-todo-heading-as-state-change
  6282. (org-todo new-mark)
  6283. (insert new-mark " "))))
  6284. (when org-provide-todo-statistics
  6285. (org-update-parent-todo-statistics))))
  6286. (defun org-insert-subheading (arg)
  6287. "Insert a new subheading and demote it.
  6288. Works for outline headings and for plain lists alike."
  6289. (interactive "P")
  6290. (org-insert-heading arg)
  6291. (cond
  6292. ((org-on-heading-p) (org-do-demote))
  6293. ((org-at-item-p) (org-indent-item))))
  6294. (defun org-insert-todo-subheading (arg)
  6295. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6296. Works for outline headings and for plain lists alike."
  6297. (interactive "P")
  6298. (org-insert-todo-heading arg)
  6299. (cond
  6300. ((org-on-heading-p) (org-do-demote))
  6301. ((org-at-item-p) (org-indent-item))))
  6302. ;;; Promotion and Demotion
  6303. (defvar org-after-demote-entry-hook nil
  6304. "Hook run after an entry has been demoted.
  6305. The cursor will be at the beginning of the entry.
  6306. When a subtree is being demoted, the hook will be called for each node.")
  6307. (defvar org-after-promote-entry-hook nil
  6308. "Hook run after an entry has been promoted.
  6309. The cursor will be at the beginning of the entry.
  6310. When a subtree is being promoted, the hook will be called for each node.")
  6311. (defun org-promote-subtree ()
  6312. "Promote the entire subtree.
  6313. See also `org-promote'."
  6314. (interactive)
  6315. (save-excursion
  6316. (org-with-limited-levels (org-map-tree 'org-promote)))
  6317. (org-fix-position-after-promote))
  6318. (defun org-demote-subtree ()
  6319. "Demote the entire subtree. See `org-demote'.
  6320. See also `org-promote'."
  6321. (interactive)
  6322. (save-excursion
  6323. (org-with-limited-levels (org-map-tree 'org-demote)))
  6324. (org-fix-position-after-promote))
  6325. (defun org-do-promote ()
  6326. "Promote the current heading higher up the tree.
  6327. If the region is active in `transient-mark-mode', promote all headings
  6328. in the region."
  6329. (interactive)
  6330. (save-excursion
  6331. (if (org-region-active-p)
  6332. (org-map-region 'org-promote (region-beginning) (region-end))
  6333. (org-promote)))
  6334. (org-fix-position-after-promote))
  6335. (defun org-do-demote ()
  6336. "Demote the current heading lower down the tree.
  6337. If the region is active in `transient-mark-mode', demote all headings
  6338. in the region."
  6339. (interactive)
  6340. (save-excursion
  6341. (if (org-region-active-p)
  6342. (org-map-region 'org-demote (region-beginning) (region-end))
  6343. (org-demote)))
  6344. (org-fix-position-after-promote))
  6345. (defun org-fix-position-after-promote ()
  6346. "Make sure that after pro/demotion cursor position is right."
  6347. (let ((pos (point)))
  6348. (when (save-excursion
  6349. (beginning-of-line 1)
  6350. (looking-at org-todo-line-regexp)
  6351. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6352. (cond ((eobp) (insert " "))
  6353. ((eolp) (insert " "))
  6354. ((equal (char-after) ?\ ) (forward-char 1))))))
  6355. (defun org-current-level ()
  6356. "Return the level of the current entry, or nil if before the first headline.
  6357. The level is the number of stars at the beginning of the headline."
  6358. (save-excursion
  6359. (org-with-limited-levels
  6360. (ignore-errors
  6361. (org-back-to-heading t)
  6362. (funcall outline-level)))))
  6363. (defun org-get-previous-line-level ()
  6364. "Return the outline depth of the last headline before the current line.
  6365. Returns 0 for the first headline in the buffer, and nil if before the
  6366. first headline."
  6367. (let ((current-level (org-current-level))
  6368. (prev-level (when (> (line-number-at-pos) 1)
  6369. (save-excursion
  6370. (beginning-of-line 0)
  6371. (org-current-level)))))
  6372. (cond ((null current-level) nil) ; Before first headline
  6373. ((null prev-level) 0) ; At first headline
  6374. (prev-level))))
  6375. (defun org-reduced-level (l)
  6376. "Compute the effective level of a heading.
  6377. This takes into account the setting of `org-odd-levels-only'."
  6378. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6379. (defun org-level-increment ()
  6380. "Return the number of stars that will be added or removed at a
  6381. time to headlines when structure editing, based on the value of
  6382. `org-odd-levels-only'."
  6383. (if org-odd-levels-only 2 1))
  6384. (defun org-get-valid-level (level &optional change)
  6385. "Rectify a level change under the influence of `org-odd-levels-only'
  6386. LEVEL is a current level, CHANGE is by how much the level should be
  6387. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6388. even level numbers will become the next higher odd number."
  6389. (if org-odd-levels-only
  6390. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6391. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6392. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6393. (max 1 (+ level (or change 0)))))
  6394. (if (boundp 'define-obsolete-function-alias)
  6395. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6396. (define-obsolete-function-alias 'org-get-legal-level
  6397. 'org-get-valid-level)
  6398. (define-obsolete-function-alias 'org-get-legal-level
  6399. 'org-get-valid-level "23.1")))
  6400. (defun org-promote ()
  6401. "Promote the current heading higher up the tree.
  6402. If the region is active in `transient-mark-mode', promote all headings
  6403. in the region."
  6404. (org-back-to-heading t)
  6405. (let* ((level (save-match-data (funcall outline-level)))
  6406. (after-change-functions (remove 'flyspell-after-change-function
  6407. after-change-functions))
  6408. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6409. (diff (abs (- level (length up-head) -1))))
  6410. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6411. (replace-match up-head nil t)
  6412. ;; Fixup tag positioning
  6413. (and org-auto-align-tags (org-set-tags nil t))
  6414. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6415. (run-hooks 'org-after-promote-entry-hook)))
  6416. (defun org-demote ()
  6417. "Demote the current heading lower down the tree.
  6418. If the region is active in `transient-mark-mode', demote all headings
  6419. in the region."
  6420. (org-back-to-heading t)
  6421. (let* ((level (save-match-data (funcall outline-level)))
  6422. (after-change-functions (remove 'flyspell-after-change-function
  6423. after-change-functions))
  6424. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6425. (diff (abs (- level (length down-head) -1))))
  6426. (replace-match down-head nil t)
  6427. ;; Fixup tag positioning
  6428. (and org-auto-align-tags (org-set-tags nil t))
  6429. (if org-adapt-indentation (org-fixup-indentation diff))
  6430. (run-hooks 'org-after-demote-entry-hook)))
  6431. (defun org-cycle-level ()
  6432. "Cycle the level of an empty headline through possible states.
  6433. This goes first to child, then to parent, level, then up the hierarchy.
  6434. After top level, it switches back to sibling level."
  6435. (interactive)
  6436. (let ((org-adapt-indentation nil))
  6437. (when (org-point-at-end-of-empty-headline)
  6438. (setq this-command 'org-cycle-level) ; Only needed for caching
  6439. (let ((cur-level (org-current-level))
  6440. (prev-level (org-get-previous-line-level)))
  6441. (cond
  6442. ;; If first headline in file, promote to top-level.
  6443. ((= prev-level 0)
  6444. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6445. do (org-do-promote)))
  6446. ;; If same level as prev, demote one.
  6447. ((= prev-level cur-level)
  6448. (org-do-demote))
  6449. ;; If parent is top-level, promote to top level if not already.
  6450. ((= prev-level 1)
  6451. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6452. do (org-do-promote)))
  6453. ;; If top-level, return to prev-level.
  6454. ((= cur-level 1)
  6455. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6456. do (org-do-demote)))
  6457. ;; If less than prev-level, promote one.
  6458. ((< cur-level prev-level)
  6459. (org-do-promote))
  6460. ;; If deeper than prev-level, promote until higher than
  6461. ;; prev-level.
  6462. ((> cur-level prev-level)
  6463. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6464. do (org-do-promote))))
  6465. t))))
  6466. (defun org-map-tree (fun)
  6467. "Call FUN for every heading underneath the current one."
  6468. (org-back-to-heading)
  6469. (let ((level (funcall outline-level)))
  6470. (save-excursion
  6471. (funcall fun)
  6472. (while (and (progn
  6473. (outline-next-heading)
  6474. (> (funcall outline-level) level))
  6475. (not (eobp)))
  6476. (funcall fun)))))
  6477. (defun org-map-region (fun beg end)
  6478. "Call FUN for every heading between BEG and END."
  6479. (let ((org-ignore-region t))
  6480. (save-excursion
  6481. (setq end (copy-marker end))
  6482. (goto-char beg)
  6483. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6484. (< (point) end))
  6485. (funcall fun))
  6486. (while (and (progn
  6487. (outline-next-heading)
  6488. (< (point) end))
  6489. (not (eobp)))
  6490. (funcall fun)))))
  6491. (defun org-fixup-indentation (diff)
  6492. "Change the indentation in the current entry by DIFF.
  6493. However, if any line in the current entry has no indentation, or if it
  6494. would end up with no indentation after the change, nothing at all is done."
  6495. (save-excursion
  6496. (let ((end (save-excursion (outline-next-heading)
  6497. (point-marker)))
  6498. (prohibit (if (> diff 0)
  6499. "^\\S-"
  6500. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6501. col)
  6502. (unless (save-excursion (end-of-line 1)
  6503. (re-search-forward prohibit end t))
  6504. (while (and (< (point) end)
  6505. (re-search-forward "^[ \t]+" end t))
  6506. (goto-char (match-end 0))
  6507. (setq col (current-column))
  6508. (if (< diff 0) (replace-match ""))
  6509. (org-indent-to-column (+ diff col))))
  6510. (move-marker end nil))))
  6511. (defun org-convert-to-odd-levels ()
  6512. "Convert an org-mode file with all levels allowed to one with odd levels.
  6513. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6514. level 5 etc."
  6515. (interactive)
  6516. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6517. (let ((outline-regexp org-outline-regexp)
  6518. (outline-level 'org-outline-level)
  6519. (org-odd-levels-only nil) n)
  6520. (save-excursion
  6521. (goto-char (point-min))
  6522. (while (re-search-forward "^\\*\\*+ " nil t)
  6523. (setq n (- (length (match-string 0)) 2))
  6524. (while (>= (setq n (1- n)) 0)
  6525. (org-demote))
  6526. (end-of-line 1))))))
  6527. (defun org-convert-to-oddeven-levels ()
  6528. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6529. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6530. file contains a section with an even level, conversion would
  6531. destroy the structure of the file. An error is signaled in this
  6532. case."
  6533. (interactive)
  6534. (goto-char (point-min))
  6535. ;; First check if there are no even levels
  6536. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6537. (org-show-context t)
  6538. (error "Not all levels are odd in this file. Conversion not possible"))
  6539. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6540. (let ((outline-regexp org-outline-regexp)
  6541. (outline-level 'org-outline-level)
  6542. (org-odd-levels-only nil) n)
  6543. (save-excursion
  6544. (goto-char (point-min))
  6545. (while (re-search-forward "^\\*\\*+ " nil t)
  6546. (setq n (/ (1- (length (match-string 0))) 2))
  6547. (while (>= (setq n (1- n)) 0)
  6548. (org-promote))
  6549. (end-of-line 1))))))
  6550. (defun org-tr-level (n)
  6551. "Make N odd if required."
  6552. (if org-odd-levels-only (1+ (/ n 2)) n))
  6553. ;;; Vertical tree motion, cutting and pasting of subtrees
  6554. (defun org-move-subtree-up (&optional arg)
  6555. "Move the current subtree up past ARG headlines of the same level."
  6556. (interactive "p")
  6557. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6558. (defun org-move-subtree-down (&optional arg)
  6559. "Move the current subtree down past ARG headlines of the same level."
  6560. (interactive "p")
  6561. (setq arg (prefix-numeric-value arg))
  6562. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6563. 'org-get-last-sibling))
  6564. (ins-point (make-marker))
  6565. (cnt (abs arg))
  6566. (col (current-column))
  6567. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6568. ;; Select the tree
  6569. (org-back-to-heading)
  6570. (setq beg0 (point))
  6571. (save-excursion
  6572. (setq ne-beg (org-back-over-empty-lines))
  6573. (setq beg (point)))
  6574. (save-match-data
  6575. (save-excursion (outline-end-of-heading)
  6576. (setq folded (outline-invisible-p)))
  6577. (outline-end-of-subtree))
  6578. (outline-next-heading)
  6579. (setq ne-end (org-back-over-empty-lines))
  6580. (setq end (point))
  6581. (goto-char beg0)
  6582. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6583. ;; include less whitespace
  6584. (save-excursion
  6585. (goto-char beg)
  6586. (forward-line (- ne-beg ne-end))
  6587. (setq beg (point))))
  6588. ;; Find insertion point, with error handling
  6589. (while (> cnt 0)
  6590. (or (and (funcall movfunc) (looking-at outline-regexp))
  6591. (progn (goto-char beg0)
  6592. (error "Cannot move past superior level or buffer limit")))
  6593. (setq cnt (1- cnt)))
  6594. (if (> arg 0)
  6595. ;; Moving forward - still need to move over subtree
  6596. (progn (org-end-of-subtree t t)
  6597. (save-excursion
  6598. (org-back-over-empty-lines)
  6599. (or (bolp) (newline)))))
  6600. (setq ne-ins (org-back-over-empty-lines))
  6601. (move-marker ins-point (point))
  6602. (setq txt (buffer-substring beg end))
  6603. (org-save-markers-in-region beg end)
  6604. (delete-region beg end)
  6605. (org-remove-empty-overlays-at beg)
  6606. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6607. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6608. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6609. (let ((bbb (point)))
  6610. (insert-before-markers txt)
  6611. (org-reinstall-markers-in-region bbb)
  6612. (move-marker ins-point bbb))
  6613. (or (bolp) (insert "\n"))
  6614. (setq ins-end (point))
  6615. (goto-char ins-point)
  6616. (org-skip-whitespace)
  6617. (when (and (< arg 0)
  6618. (org-first-sibling-p)
  6619. (> ne-ins ne-beg))
  6620. ;; Move whitespace back to beginning
  6621. (save-excursion
  6622. (goto-char ins-end)
  6623. (let ((kill-whole-line t))
  6624. (kill-line (- ne-ins ne-beg)) (point)))
  6625. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6626. (move-marker ins-point nil)
  6627. (if folded
  6628. (hide-subtree)
  6629. (org-show-entry)
  6630. (show-children)
  6631. (org-cycle-hide-drawers 'children))
  6632. (org-clean-visibility-after-subtree-move)
  6633. ;; move back to the initial column we were at
  6634. (move-to-column col)))
  6635. (defvar org-subtree-clip ""
  6636. "Clipboard for cut and paste of subtrees.
  6637. This is actually only a copy of the kill, because we use the normal kill
  6638. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6639. (defvar org-subtree-clip-folded nil
  6640. "Was the last copied subtree folded?
  6641. This is used to fold the tree back after pasting.")
  6642. (defun org-cut-subtree (&optional n)
  6643. "Cut the current subtree into the clipboard.
  6644. With prefix arg N, cut this many sequential subtrees.
  6645. This is a short-hand for marking the subtree and then cutting it."
  6646. (interactive "p")
  6647. (org-copy-subtree n 'cut))
  6648. (defun org-copy-subtree (&optional n cut force-store-markers)
  6649. "Cut the current subtree into the clipboard.
  6650. With prefix arg N, cut this many sequential subtrees.
  6651. This is a short-hand for marking the subtree and then copying it.
  6652. If CUT is non-nil, actually cut the subtree.
  6653. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6654. of some markers in the region, even if CUT is non-nil. This is
  6655. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6656. (interactive "p")
  6657. (let (beg end folded (beg0 (point)))
  6658. (if (interactive-p)
  6659. (org-back-to-heading nil) ; take what looks like a subtree
  6660. (org-back-to-heading t)) ; take what is really there
  6661. (org-back-over-empty-lines)
  6662. (setq beg (point))
  6663. (skip-chars-forward " \t\r\n")
  6664. (save-match-data
  6665. (save-excursion (outline-end-of-heading)
  6666. (setq folded (outline-invisible-p)))
  6667. (condition-case nil
  6668. (org-forward-same-level (1- n) t)
  6669. (error nil))
  6670. (org-end-of-subtree t t))
  6671. (org-back-over-empty-lines)
  6672. (setq end (point))
  6673. (goto-char beg0)
  6674. (when (> end beg)
  6675. (setq org-subtree-clip-folded folded)
  6676. (when (or cut force-store-markers)
  6677. (org-save-markers-in-region beg end))
  6678. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6679. (setq org-subtree-clip (current-kill 0))
  6680. (message "%s: Subtree(s) with %d characters"
  6681. (if cut "Cut" "Copied")
  6682. (length org-subtree-clip)))))
  6683. (defun org-paste-subtree (&optional level tree for-yank)
  6684. "Paste the clipboard as a subtree, with modification of headline level.
  6685. The entire subtree is promoted or demoted in order to match a new headline
  6686. level.
  6687. If the cursor is at the beginning of a headline, the same level as
  6688. that headline is used to paste the tree
  6689. If not, the new level is derived from the *visible* headings
  6690. before and after the insertion point, and taken to be the inferior headline
  6691. level of the two. So if the previous visible heading is level 3 and the
  6692. next is level 4 (or vice versa), level 4 will be used for insertion.
  6693. This makes sure that the subtree remains an independent subtree and does
  6694. not swallow low level entries.
  6695. You can also force a different level, either by using a numeric prefix
  6696. argument, or by inserting the heading marker by hand. For example, if the
  6697. cursor is after \"*****\", then the tree will be shifted to level 5.
  6698. If optional TREE is given, use this text instead of the kill ring.
  6699. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6700. move back over whitespace before inserting, and move point to the end of
  6701. the inserted text when done."
  6702. (interactive "P")
  6703. (setq tree (or tree (and kill-ring (current-kill 0))))
  6704. (unless (org-kill-is-subtree-p tree)
  6705. (error "%s"
  6706. (substitute-command-keys
  6707. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6708. (let* ((visp (not (outline-invisible-p)))
  6709. (txt tree)
  6710. (^re (concat "^\\(" outline-regexp "\\)"))
  6711. (re (concat "\\(" outline-regexp "\\)"))
  6712. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6713. (old-level (if (string-match ^re txt)
  6714. (- (match-end 0) (match-beginning 0) 1)
  6715. -1))
  6716. (force-level (cond (level (prefix-numeric-value level))
  6717. ((and (looking-at "[ \t]*$")
  6718. (string-match
  6719. ^re_ (buffer-substring
  6720. (point-at-bol) (point))))
  6721. (- (match-end 1) (match-beginning 1)))
  6722. ((and (bolp)
  6723. (looking-at org-outline-regexp))
  6724. (- (match-end 0) (point) 1))
  6725. (t nil)))
  6726. (previous-level (save-excursion
  6727. (condition-case nil
  6728. (progn
  6729. (outline-previous-visible-heading 1)
  6730. (if (looking-at re)
  6731. (- (match-end 0) (match-beginning 0) 1)
  6732. 1))
  6733. (error 1))))
  6734. (next-level (save-excursion
  6735. (condition-case nil
  6736. (progn
  6737. (or (looking-at outline-regexp)
  6738. (outline-next-visible-heading 1))
  6739. (if (looking-at re)
  6740. (- (match-end 0) (match-beginning 0) 1)
  6741. 1))
  6742. (error 1))))
  6743. (new-level (or force-level (max previous-level next-level)))
  6744. (shift (if (or (= old-level -1)
  6745. (= new-level -1)
  6746. (= old-level new-level))
  6747. 0
  6748. (- new-level old-level)))
  6749. (delta (if (> shift 0) -1 1))
  6750. (func (if (> shift 0) 'org-demote 'org-promote))
  6751. (org-odd-levels-only nil)
  6752. beg end newend)
  6753. ;; Remove the forced level indicator
  6754. (if force-level
  6755. (delete-region (point-at-bol) (point)))
  6756. ;; Paste
  6757. (beginning-of-line 1)
  6758. (unless for-yank (org-back-over-empty-lines))
  6759. (setq beg (point))
  6760. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6761. (insert-before-markers txt)
  6762. (unless (string-match "\n\\'" txt) (insert "\n"))
  6763. (setq newend (point))
  6764. (org-reinstall-markers-in-region beg)
  6765. (setq end (point))
  6766. (goto-char beg)
  6767. (skip-chars-forward " \t\n\r")
  6768. (setq beg (point))
  6769. (if (and (outline-invisible-p) visp)
  6770. (save-excursion (outline-show-heading)))
  6771. ;; Shift if necessary
  6772. (unless (= shift 0)
  6773. (save-restriction
  6774. (narrow-to-region beg end)
  6775. (while (not (= shift 0))
  6776. (org-map-region func (point-min) (point-max))
  6777. (setq shift (+ delta shift)))
  6778. (goto-char (point-min))
  6779. (setq newend (point-max))))
  6780. (when (or (interactive-p) for-yank)
  6781. (message "Clipboard pasted as level %d subtree" new-level))
  6782. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6783. kill-ring
  6784. (eq org-subtree-clip (current-kill 0))
  6785. org-subtree-clip-folded)
  6786. ;; The tree was folded before it was killed/copied
  6787. (hide-subtree))
  6788. (and for-yank (goto-char newend))))
  6789. (defun org-kill-is-subtree-p (&optional txt)
  6790. "Check if the current kill is an outline subtree, or a set of trees.
  6791. Returns nil if kill does not start with a headline, or if the first
  6792. headline level is not the largest headline level in the tree.
  6793. So this will actually accept several entries of equal levels as well,
  6794. which is OK for `org-paste-subtree'.
  6795. If optional TXT is given, check this string instead of the current kill."
  6796. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6797. (start-level (and kill
  6798. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6799. org-outline-regexp "\\)")
  6800. kill)
  6801. (- (match-end 2) (match-beginning 2) 1)))
  6802. (re (concat "^" org-outline-regexp))
  6803. (start (1+ (or (match-beginning 2) -1))))
  6804. (if (not start-level)
  6805. (progn
  6806. nil) ;; does not even start with a heading
  6807. (catch 'exit
  6808. (while (setq start (string-match re kill (1+ start)))
  6809. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6810. (throw 'exit nil)))
  6811. t))))
  6812. (defvar org-markers-to-move nil
  6813. "Markers that should be moved with a cut-and-paste operation.
  6814. Those markers are stored together with their positions relative to
  6815. the start of the region.")
  6816. (defun org-save-markers-in-region (beg end)
  6817. "Check markers in region.
  6818. If these markers are between BEG and END, record their position relative
  6819. to BEG, so that after moving the block of text, we can put the markers back
  6820. into place.
  6821. This function gets called just before an entry or tree gets cut from the
  6822. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6823. called immediately, to move the markers with the entries."
  6824. (setq org-markers-to-move nil)
  6825. (when (featurep 'org-clock)
  6826. (org-clock-save-markers-for-cut-and-paste beg end))
  6827. (when (featurep 'org-agenda)
  6828. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6829. (defun org-check-and-save-marker (marker beg end)
  6830. "Check if MARKER is between BEG and END.
  6831. If yes, remember the marker and the distance to BEG."
  6832. (when (and (marker-buffer marker)
  6833. (equal (marker-buffer marker) (current-buffer)))
  6834. (if (and (>= marker beg) (< marker end))
  6835. (push (cons marker (- marker beg)) org-markers-to-move))))
  6836. (defun org-reinstall-markers-in-region (beg)
  6837. "Move all remembered markers to their position relative to BEG."
  6838. (mapc (lambda (x)
  6839. (move-marker (car x) (+ beg (cdr x))))
  6840. org-markers-to-move)
  6841. (setq org-markers-to-move nil))
  6842. (defun org-narrow-to-subtree ()
  6843. "Narrow buffer to the current subtree."
  6844. (interactive)
  6845. (save-excursion
  6846. (save-match-data
  6847. (org-with-limited-levels
  6848. (narrow-to-region
  6849. (progn (org-back-to-heading t) (point))
  6850. (progn (org-end-of-subtree t t)
  6851. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6852. (point)))))))
  6853. (defun org-narrow-to-block ()
  6854. "Narrow buffer to the current block."
  6855. (interactive)
  6856. (let ((bstart "^[ \t]*#\\+begin")
  6857. (bend "[ \t]*#\\+end")
  6858. (case-fold-search t) ;; allow #+BEGIN
  6859. b_start b_end)
  6860. (if (org-in-regexps-block-p bstart bend)
  6861. (progn
  6862. (save-excursion (re-search-backward bstart nil t)
  6863. (setq b_start (match-beginning 0)))
  6864. (save-excursion (re-search-forward bend nil t)
  6865. (setq b_end (match-end 0)))
  6866. (narrow-to-region b_start b_end))
  6867. (error "Not in a block"))))
  6868. (eval-when-compile
  6869. (defvar org-property-drawer-re))
  6870. (defvar org-property-start-re) ;; defined below
  6871. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6872. "Clone the task (subtree) at point N times.
  6873. The clones will be inserted as siblings.
  6874. In interactive use, the user will be prompted for the number of
  6875. clones to be produced, and for a time SHIFT, which may be a
  6876. repeater as used in time stamps, for example `+3d'.
  6877. When a valid repeater is given and the entry contains any time
  6878. stamps, the clones will become a sequence in time, with time
  6879. stamps in the subtree shifted for each clone produced. If SHIFT
  6880. is nil or the empty string, time stamps will be left alone. The
  6881. ID property of the original subtree is removed.
  6882. If the original subtree did contain time stamps with a repeater,
  6883. the following will happen:
  6884. - the repeater will be removed in each clone
  6885. - an additional clone will be produced, with the current, unshifted
  6886. date(s) in the entry.
  6887. - the original entry will be placed *after* all the clones, with
  6888. repeater intact.
  6889. - the start days in the repeater in the original entry will be shifted
  6890. to past the last clone.
  6891. I this way you can spell out a number of instances of a repeating task,
  6892. and still retain the repeater to cover future instances of the task."
  6893. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6894. (let (beg end template task idprop
  6895. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6896. (if (not (and (integerp n) (> n 0)))
  6897. (error "Invalid number of replications %s" n))
  6898. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6899. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6900. shift)))
  6901. (error "Invalid shift specification %s" shift))
  6902. (when doshift
  6903. (setq shift-n (string-to-number (match-string 1 shift))
  6904. shift-what (cdr (assoc (match-string 2 shift)
  6905. '(("d" . day) ("w" . week)
  6906. ("m" . month) ("y" . year))))))
  6907. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6908. (setq nmin 1 nmax n)
  6909. (org-back-to-heading t)
  6910. (setq beg (point))
  6911. (setq idprop (org-entry-get nil "ID"))
  6912. (org-end-of-subtree t t)
  6913. (or (bolp) (insert "\n"))
  6914. (setq end (point))
  6915. (setq template (buffer-substring beg end))
  6916. (when (and doshift
  6917. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6918. (delete-region beg end)
  6919. (setq end beg)
  6920. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6921. (goto-char end)
  6922. (loop for n from nmin to nmax do
  6923. ;; prepare clone
  6924. (with-temp-buffer
  6925. (insert template)
  6926. (org-mode)
  6927. (goto-char (point-min))
  6928. (and idprop (if org-clone-delete-id
  6929. (org-entry-delete nil "ID")
  6930. (org-id-get-create t)))
  6931. (while (re-search-forward org-property-start-re nil t)
  6932. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6933. (goto-char (point-min))
  6934. (when doshift
  6935. (while (re-search-forward org-ts-regexp-both nil t)
  6936. (org-timestamp-change (* n shift-n) shift-what))
  6937. (unless (= n n-no-remove)
  6938. (goto-char (point-min))
  6939. (while (re-search-forward org-ts-regexp nil t)
  6940. (save-excursion
  6941. (goto-char (match-beginning 0))
  6942. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6943. (delete-region (match-beginning 1) (match-end 1)))))))
  6944. (setq task (buffer-string)))
  6945. (insert task))
  6946. (goto-char beg)))
  6947. ;;; Outline Sorting
  6948. (defun org-sort (with-case)
  6949. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6950. Optional argument WITH-CASE means sort case-sensitively.
  6951. With a double prefix argument, also remove duplicate entries."
  6952. (interactive "P")
  6953. (cond
  6954. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6955. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6956. (t
  6957. (org-call-with-arg 'org-sort-entries with-case))))
  6958. (defun org-sort-remove-invisible (s)
  6959. (remove-text-properties 0 (length s) org-rm-props s)
  6960. (while (string-match org-bracket-link-regexp s)
  6961. (setq s (replace-match (if (match-end 2)
  6962. (match-string 3 s)
  6963. (match-string 1 s)) t t s)))
  6964. s)
  6965. (defvar org-priority-regexp) ; defined later in the file
  6966. (defvar org-after-sorting-entries-or-items-hook nil
  6967. "Hook that is run after a bunch of entries or items have been sorted.
  6968. When children are sorted, the cursor is in the parent line when this
  6969. hook gets called. When a region or a plain list is sorted, the cursor
  6970. will be in the first entry of the sorted region/list.")
  6971. (defun org-sort-entries
  6972. (&optional with-case sorting-type getkey-func compare-func property)
  6973. "Sort entries on a certain level of an outline tree.
  6974. If there is an active region, the entries in the region are sorted.
  6975. Else, if the cursor is before the first entry, sort the top-level items.
  6976. Else, the children of the entry at point are sorted.
  6977. Sorting can be alphabetically, numerically, by date/time as given by
  6978. a time stamp, by a property or by priority.
  6979. The command prompts for the sorting type unless it has been given to the
  6980. function through the SORTING-TYPE argument, which needs to be a character,
  6981. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6982. precise meaning of each character:
  6983. n Numerically, by converting the beginning of the entry/item to a number.
  6984. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6985. t By date/time, either the first active time stamp in the entry, or, if
  6986. none exist, by the first inactive one.
  6987. s By the scheduled date/time.
  6988. d By deadline date/time.
  6989. c By creation time, which is assumed to be the first inactive time stamp
  6990. at the beginning of a line.
  6991. p By priority according to the cookie.
  6992. r By the value of a property.
  6993. Capital letters will reverse the sort order.
  6994. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6995. called with point at the beginning of the record. It must return either
  6996. a string or a number that should serve as the sorting key for that record.
  6997. Comparing entries ignores case by default. However, with an optional argument
  6998. WITH-CASE, the sorting considers case as well."
  6999. (interactive "P")
  7000. (let ((case-func (if with-case 'identity 'downcase))
  7001. start beg end stars re re2
  7002. txt what tmp)
  7003. ;; Find beginning and end of region to sort
  7004. (cond
  7005. ((org-region-active-p)
  7006. ;; we will sort the region
  7007. (setq end (region-end)
  7008. what "region")
  7009. (goto-char (region-beginning))
  7010. (if (not (org-on-heading-p)) (outline-next-heading))
  7011. (setq start (point)))
  7012. ((or (org-on-heading-p)
  7013. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7014. ;; we will sort the children of the current headline
  7015. (org-back-to-heading)
  7016. (setq start (point)
  7017. end (progn (org-end-of-subtree t t)
  7018. (or (bolp) (insert "\n"))
  7019. (org-back-over-empty-lines)
  7020. (point))
  7021. what "children")
  7022. (goto-char start)
  7023. (show-subtree)
  7024. (outline-next-heading))
  7025. (t
  7026. ;; we will sort the top-level entries in this file
  7027. (goto-char (point-min))
  7028. (or (org-on-heading-p) (outline-next-heading))
  7029. (setq start (point))
  7030. (goto-char (point-max))
  7031. (beginning-of-line 1)
  7032. (when (looking-at ".*?\\S-")
  7033. ;; File ends in a non-white line
  7034. (end-of-line 1)
  7035. (insert "\n"))
  7036. (setq end (point-max))
  7037. (setq what "top-level")
  7038. (goto-char start)
  7039. (show-all)))
  7040. (setq beg (point))
  7041. (if (>= beg end) (error "Nothing to sort"))
  7042. (looking-at "\\(\\*+\\)")
  7043. (setq stars (match-string 1)
  7044. re (concat "^" (regexp-quote stars) " +")
  7045. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7046. txt (buffer-substring beg end))
  7047. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7048. (if (and (not (equal stars "*")) (string-match re2 txt))
  7049. (error "Region to sort contains a level above the first entry"))
  7050. (unless sorting-type
  7051. (message
  7052. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7053. [t]ime [s]cheduled [d]eadline [c]reated
  7054. A/N/T/S/D/C/P/O/F means reversed:"
  7055. what)
  7056. (setq sorting-type (read-char-exclusive))
  7057. (and (= (downcase sorting-type) ?f)
  7058. (setq getkey-func
  7059. (org-icompleting-read "Sort using function: "
  7060. obarray 'fboundp t nil nil))
  7061. (setq getkey-func (intern getkey-func)))
  7062. (and (= (downcase sorting-type) ?r)
  7063. (setq property
  7064. (org-icompleting-read "Property: "
  7065. (mapcar 'list (org-buffer-property-keys t))
  7066. nil t))))
  7067. (message "Sorting entries...")
  7068. (save-restriction
  7069. (narrow-to-region start end)
  7070. (let ((dcst (downcase sorting-type))
  7071. (case-fold-search nil)
  7072. (now (current-time)))
  7073. (sort-subr
  7074. (/= dcst sorting-type)
  7075. ;; This function moves to the beginning character of the "record" to
  7076. ;; be sorted.
  7077. (lambda nil
  7078. (if (re-search-forward re nil t)
  7079. (goto-char (match-beginning 0))
  7080. (goto-char (point-max))))
  7081. ;; This function moves to the last character of the "record" being
  7082. ;; sorted.
  7083. (lambda nil
  7084. (save-match-data
  7085. (condition-case nil
  7086. (outline-forward-same-level 1)
  7087. (error
  7088. (goto-char (point-max))))))
  7089. ;; This function returns the value that gets sorted against.
  7090. (lambda nil
  7091. (cond
  7092. ((= dcst ?n)
  7093. (if (looking-at org-complex-heading-regexp)
  7094. (string-to-number (match-string 4))
  7095. nil))
  7096. ((= dcst ?a)
  7097. (if (looking-at org-complex-heading-regexp)
  7098. (funcall case-func (match-string 4))
  7099. nil))
  7100. ((= dcst ?t)
  7101. (let ((end (save-excursion (outline-next-heading) (point))))
  7102. (if (or (re-search-forward org-ts-regexp end t)
  7103. (re-search-forward org-ts-regexp-both end t))
  7104. (org-time-string-to-seconds (match-string 0))
  7105. (org-float-time now))))
  7106. ((= dcst ?c)
  7107. (let ((end (save-excursion (outline-next-heading) (point))))
  7108. (if (re-search-forward
  7109. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7110. end t)
  7111. (org-time-string-to-seconds (match-string 0))
  7112. (org-float-time now))))
  7113. ((= dcst ?s)
  7114. (let ((end (save-excursion (outline-next-heading) (point))))
  7115. (if (re-search-forward org-scheduled-time-regexp end t)
  7116. (org-time-string-to-seconds (match-string 1))
  7117. (org-float-time now))))
  7118. ((= dcst ?d)
  7119. (let ((end (save-excursion (outline-next-heading) (point))))
  7120. (if (re-search-forward org-deadline-time-regexp end t)
  7121. (org-time-string-to-seconds (match-string 1))
  7122. (org-float-time now))))
  7123. ((= dcst ?p)
  7124. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7125. (string-to-char (match-string 2))
  7126. org-default-priority))
  7127. ((= dcst ?r)
  7128. (or (org-entry-get nil property) ""))
  7129. ((= dcst ?o)
  7130. (if (looking-at org-complex-heading-regexp)
  7131. (- 9999 (length (member (match-string 2)
  7132. org-todo-keywords-1)))))
  7133. ((= dcst ?f)
  7134. (if getkey-func
  7135. (progn
  7136. (setq tmp (funcall getkey-func))
  7137. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7138. tmp)
  7139. (error "Invalid key function `%s'" getkey-func)))
  7140. (t (error "Invalid sorting type `%c'" sorting-type))))
  7141. nil
  7142. (cond
  7143. ((= dcst ?a) 'string<)
  7144. ((= dcst ?f) compare-func)
  7145. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7146. (t nil)))))
  7147. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7148. (message "Sorting entries...done")))
  7149. (defun org-do-sort (table what &optional with-case sorting-type)
  7150. "Sort TABLE of WHAT according to SORTING-TYPE.
  7151. The user will be prompted for the SORTING-TYPE if the call to this
  7152. function does not specify it. WHAT is only for the prompt, to indicate
  7153. what is being sorted. The sorting key will be extracted from
  7154. the car of the elements of the table.
  7155. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7156. (unless sorting-type
  7157. (message
  7158. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7159. what)
  7160. (setq sorting-type (read-char-exclusive)))
  7161. (let ((dcst (downcase sorting-type))
  7162. extractfun comparefun)
  7163. ;; Define the appropriate functions
  7164. (cond
  7165. ((= dcst ?n)
  7166. (setq extractfun 'string-to-number
  7167. comparefun (if (= dcst sorting-type) '< '>)))
  7168. ((= dcst ?a)
  7169. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7170. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7171. comparefun (if (= dcst sorting-type)
  7172. 'string<
  7173. (lambda (a b) (and (not (string< a b))
  7174. (not (string= a b)))))))
  7175. ((= dcst ?t)
  7176. (setq extractfun
  7177. (lambda (x)
  7178. (if (or (string-match org-ts-regexp x)
  7179. (string-match org-ts-regexp-both x))
  7180. (org-float-time
  7181. (org-time-string-to-time (match-string 0 x)))
  7182. 0))
  7183. comparefun (if (= dcst sorting-type) '< '>)))
  7184. (t (error "Invalid sorting type `%c'" sorting-type)))
  7185. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7186. table)
  7187. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7188. ;;; The orgstruct minor mode
  7189. ;; Define a minor mode which can be used in other modes in order to
  7190. ;; integrate the org-mode structure editing commands.
  7191. ;; This is really a hack, because the org-mode structure commands use
  7192. ;; keys which normally belong to the major mode. Here is how it
  7193. ;; works: The minor mode defines all the keys necessary to operate the
  7194. ;; structure commands, but wraps the commands into a function which
  7195. ;; tests if the cursor is currently at a headline or a plain list
  7196. ;; item. If that is the case, the structure command is used,
  7197. ;; temporarily setting many Org-mode variables like regular
  7198. ;; expressions for filling etc. However, when any of those keys is
  7199. ;; used at a different location, function uses `key-binding' to look
  7200. ;; up if the key has an associated command in another currently active
  7201. ;; keymap (minor modes, major mode, global), and executes that
  7202. ;; command. There might be problems if any of the keys is otherwise
  7203. ;; used as a prefix key.
  7204. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7205. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7206. ;; addresses this by checking explicitly for both bindings.
  7207. (defvar orgstruct-mode-map (make-sparse-keymap)
  7208. "Keymap for the minor `orgstruct-mode'.")
  7209. (defvar org-local-vars nil
  7210. "List of local variables, for use by `orgstruct-mode'.")
  7211. ;;;###autoload
  7212. (define-minor-mode orgstruct-mode
  7213. "Toggle the minor mode `orgstruct-mode'.
  7214. This mode is for using Org-mode structure commands in other
  7215. modes. The following keys behave as if Org-mode were active, if
  7216. the cursor is on a headline, or on a plain list item (both as
  7217. defined by Org-mode).
  7218. M-up Move entry/item up
  7219. M-down Move entry/item down
  7220. M-left Promote
  7221. M-right Demote
  7222. M-S-up Move entry/item up
  7223. M-S-down Move entry/item down
  7224. M-S-left Promote subtree
  7225. M-S-right Demote subtree
  7226. M-q Fill paragraph and items like in Org-mode
  7227. C-c ^ Sort entries
  7228. C-c - Cycle list bullet
  7229. TAB Cycle item visibility
  7230. M-RET Insert new heading/item
  7231. S-M-RET Insert new TODO heading / Checkbox item
  7232. C-c C-c Set tags / toggle checkbox"
  7233. nil " OrgStruct" nil
  7234. (org-load-modules-maybe)
  7235. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7236. ;;;###autoload
  7237. (defun turn-on-orgstruct ()
  7238. "Unconditionally turn on `orgstruct-mode'."
  7239. (orgstruct-mode 1))
  7240. (defun orgstruct++-mode (&optional arg)
  7241. "Toggle `orgstruct-mode', the enhanced version of it.
  7242. In addition to setting orgstruct-mode, this also exports all indentation
  7243. and autofilling variables from org-mode into the buffer. It will also
  7244. recognize item context in multiline items.
  7245. Note that turning off orgstruct-mode will *not* remove the
  7246. indentation/paragraph settings. This can only be done by refreshing the
  7247. major mode, for example with \\[normal-mode]."
  7248. (interactive "P")
  7249. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7250. (if (< arg 1)
  7251. (orgstruct-mode -1)
  7252. (orgstruct-mode 1)
  7253. (let (var val)
  7254. (mapc
  7255. (lambda (x)
  7256. (when (string-match
  7257. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7258. (symbol-name (car x)))
  7259. (setq var (car x) val (nth 1 x))
  7260. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7261. org-local-vars)
  7262. (org-set-local 'orgstruct-is-++ t))))
  7263. (defvar orgstruct-is-++ nil
  7264. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7265. (make-variable-buffer-local 'orgstruct-is-++)
  7266. ;;;###autoload
  7267. (defun turn-on-orgstruct++ ()
  7268. "Unconditionally turn on `orgstruct++-mode'."
  7269. (orgstruct++-mode 1))
  7270. (defun orgstruct-error ()
  7271. "Error when there is no default binding for a structure key."
  7272. (interactive)
  7273. (error "This key has no function outside structure elements"))
  7274. (defun orgstruct-setup ()
  7275. "Setup orgstruct keymaps."
  7276. (let ((nfunc 0)
  7277. (bindings
  7278. (list
  7279. '([(meta up)] org-metaup)
  7280. '([(meta down)] org-metadown)
  7281. '([(meta left)] org-metaleft)
  7282. '([(meta right)] org-metaright)
  7283. '([(meta shift up)] org-shiftmetaup)
  7284. '([(meta shift down)] org-shiftmetadown)
  7285. '([(meta shift left)] org-shiftmetaleft)
  7286. '([(meta shift right)] org-shiftmetaright)
  7287. '([?\e (up)] org-metaup)
  7288. '([?\e (down)] org-metadown)
  7289. '([?\e (left)] org-metaleft)
  7290. '([?\e (right)] org-metaright)
  7291. '([?\e (shift up)] org-shiftmetaup)
  7292. '([?\e (shift down)] org-shiftmetadown)
  7293. '([?\e (shift left)] org-shiftmetaleft)
  7294. '([?\e (shift right)] org-shiftmetaright)
  7295. '([(shift up)] org-shiftup)
  7296. '([(shift down)] org-shiftdown)
  7297. '([(shift left)] org-shiftleft)
  7298. '([(shift right)] org-shiftright)
  7299. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7300. '("\M-q" fill-paragraph)
  7301. '("\C-c^" org-sort)
  7302. '("\C-c-" org-cycle-list-bullet)))
  7303. elt key fun cmd)
  7304. (while (setq elt (pop bindings))
  7305. (setq nfunc (1+ nfunc))
  7306. (setq key (org-key (car elt))
  7307. fun (nth 1 elt)
  7308. cmd (orgstruct-make-binding fun nfunc key))
  7309. (org-defkey orgstruct-mode-map key cmd))
  7310. ;; Special treatment needed for TAB and RET
  7311. (org-defkey orgstruct-mode-map [(tab)]
  7312. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7313. (org-defkey orgstruct-mode-map "\C-i"
  7314. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7315. (org-defkey orgstruct-mode-map "\M-\C-m"
  7316. (orgstruct-make-binding 'org-insert-heading 105
  7317. "\M-\C-m" [(meta return)]))
  7318. (org-defkey orgstruct-mode-map [(meta return)]
  7319. (orgstruct-make-binding 'org-insert-heading 106
  7320. [(meta return)] "\M-\C-m"))
  7321. (org-defkey orgstruct-mode-map [(shift meta return)]
  7322. (orgstruct-make-binding 'org-insert-todo-heading 107
  7323. [(meta return)] "\M-\C-m"))
  7324. (org-defkey orgstruct-mode-map "\e\C-m"
  7325. (orgstruct-make-binding 'org-insert-heading 108
  7326. "\e\C-m" [?\e (return)]))
  7327. (org-defkey orgstruct-mode-map [?\e (return)]
  7328. (orgstruct-make-binding 'org-insert-heading 109
  7329. [?\e (return)] "\e\C-m"))
  7330. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7331. (orgstruct-make-binding 'org-insert-todo-heading 110
  7332. [?\e (return)] "\e\C-m"))
  7333. (unless org-local-vars
  7334. (setq org-local-vars (org-get-local-variables)))
  7335. t))
  7336. (defun orgstruct-make-binding (fun n &rest keys)
  7337. "Create a function for binding in the structure minor mode.
  7338. FUN is the command to call inside a table. N is used to create a unique
  7339. command name. KEYS are keys that should be checked in for a command
  7340. to execute outside of tables."
  7341. (eval
  7342. (list 'defun
  7343. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7344. '(arg)
  7345. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7346. "Outside of structure, run the binding of `"
  7347. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7348. "'.")
  7349. '(interactive "p")
  7350. (list 'if
  7351. `(org-context-p 'headline 'item
  7352. (and orgstruct-is-++
  7353. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7354. 'item-body))
  7355. (list 'org-run-like-in-org-mode (list 'quote fun))
  7356. (list 'let '(orgstruct-mode)
  7357. (list 'call-interactively
  7358. (append '(or)
  7359. (mapcar (lambda (k)
  7360. (list 'key-binding k))
  7361. keys)
  7362. '('orgstruct-error))))))))
  7363. (defun org-context-p (&rest contexts)
  7364. "Check if local context is any of CONTEXTS.
  7365. Possible values in the list of contexts are `table', `headline', and `item'."
  7366. (let ((pos (point)))
  7367. (goto-char (point-at-bol))
  7368. (prog1 (or (and (memq 'table contexts)
  7369. (looking-at "[ \t]*|"))
  7370. (and (memq 'headline contexts)
  7371. ;;????????? (looking-at "\\*+"))
  7372. (looking-at outline-regexp))
  7373. (and (memq 'item contexts)
  7374. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7375. (and (memq 'item-body contexts)
  7376. (org-in-item-p)))
  7377. (goto-char pos))))
  7378. (defun org-get-local-variables ()
  7379. "Return a list of all local variables in an org-mode buffer."
  7380. (let (varlist)
  7381. (with-current-buffer (get-buffer-create "*Org tmp*")
  7382. (erase-buffer)
  7383. (org-mode)
  7384. (setq varlist (buffer-local-variables)))
  7385. (kill-buffer "*Org tmp*")
  7386. (delq nil
  7387. (mapcar
  7388. (lambda (x)
  7389. (setq x
  7390. (if (symbolp x)
  7391. (list x)
  7392. (list (car x) (list 'quote (cdr x)))))
  7393. (if (string-match
  7394. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7395. (symbol-name (car x)))
  7396. x nil))
  7397. varlist))))
  7398. (defun org-clone-local-variables (from-buffer &optional regexp)
  7399. "Clone local variables from FROM-BUFFER.
  7400. Optional argument REGEXP selects variables to clone."
  7401. (mapc
  7402. (lambda (pair)
  7403. (and (symbolp (car pair))
  7404. (or (null regexp)
  7405. (string-match regexp (symbol-name (car pair))))
  7406. (set (make-local-variable (car pair))
  7407. (cdr pair))))
  7408. (buffer-local-variables from-buffer)))
  7409. ;;;###autoload
  7410. (defun org-run-like-in-org-mode (cmd)
  7411. "Run a command, pretending that the current buffer is in Org-mode.
  7412. This will temporarily bind local variables that are typically bound in
  7413. Org-mode to the values they have in Org-mode, and then interactively
  7414. call CMD."
  7415. (org-load-modules-maybe)
  7416. (unless org-local-vars
  7417. (setq org-local-vars (org-get-local-variables)))
  7418. (eval (list 'let org-local-vars
  7419. (list 'call-interactively (list 'quote cmd)))))
  7420. ;;;; Archiving
  7421. (defun org-get-category (&optional pos force-refresh)
  7422. "Get the category applying to position POS."
  7423. (if force-refresh (org-refresh-category-properties))
  7424. (let ((pos (or pos (point))))
  7425. (or (get-text-property pos 'org-category)
  7426. (progn (org-refresh-category-properties)
  7427. (get-text-property pos 'org-category)))))
  7428. (defun org-refresh-category-properties ()
  7429. "Refresh category text properties in the buffer."
  7430. (let ((def-cat (cond
  7431. ((null org-category)
  7432. (if buffer-file-name
  7433. (file-name-sans-extension
  7434. (file-name-nondirectory buffer-file-name))
  7435. "???"))
  7436. ((symbolp org-category) (symbol-name org-category))
  7437. (t org-category)))
  7438. beg end cat pos optionp)
  7439. (org-unmodified
  7440. (save-excursion
  7441. (save-restriction
  7442. (widen)
  7443. (goto-char (point-min))
  7444. (put-text-property (point) (point-max) 'org-category def-cat)
  7445. (while (re-search-forward
  7446. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7447. (setq pos (match-end 0)
  7448. optionp (equal (char-after (match-beginning 0)) ?#)
  7449. cat (org-trim (match-string 2)))
  7450. (if optionp
  7451. (setq beg (point-at-bol) end (point-max))
  7452. (org-back-to-heading t)
  7453. (setq beg (point) end (org-end-of-subtree t t)))
  7454. (put-text-property beg end 'org-category cat)
  7455. (goto-char pos)))))))
  7456. ;;;; Link Stuff
  7457. ;;; Link abbreviations
  7458. (defun org-link-expand-abbrev (link)
  7459. "Apply replacements as defined in `org-link-abbrev-alist."
  7460. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7461. (let* ((key (match-string 1 link))
  7462. (as (or (assoc key org-link-abbrev-alist-local)
  7463. (assoc key org-link-abbrev-alist)))
  7464. (tag (and (match-end 2) (match-string 3 link)))
  7465. rpl)
  7466. (if (not as)
  7467. link
  7468. (setq rpl (cdr as))
  7469. (cond
  7470. ((symbolp rpl) (funcall rpl tag))
  7471. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7472. ((string-match "%h" rpl)
  7473. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7474. (t (concat rpl tag)))))
  7475. link))
  7476. ;;; Storing and inserting links
  7477. (defvar org-insert-link-history nil
  7478. "Minibuffer history for links inserted with `org-insert-link'.")
  7479. (defvar org-stored-links nil
  7480. "Contains the links stored with `org-store-link'.")
  7481. (defvar org-store-link-plist nil
  7482. "Plist with info about the most recently link created with `org-store-link'.")
  7483. (defvar org-link-protocols nil
  7484. "Link protocols added to Org-mode using `org-add-link-type'.")
  7485. (defvar org-store-link-functions nil
  7486. "List of functions that are called to create and store a link.
  7487. Each function will be called in turn until one returns a non-nil
  7488. value. Each function should check if it is responsible for creating
  7489. this link (for example by looking at the major mode).
  7490. If not, it must exit and return nil.
  7491. If yes, it should return a non-nil value after a calling
  7492. `org-store-link-props' with a list of properties and values.
  7493. Special properties are:
  7494. :type The link prefix, like \"http\". This must be given.
  7495. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7496. This is obligatory as well.
  7497. :description Optional default description for the second pair
  7498. of brackets in an Org-mode link. The user can still change
  7499. this when inserting this link into an Org-mode buffer.
  7500. In addition to these, any additional properties can be specified
  7501. and then used in remember templates.")
  7502. (defun org-add-link-type (type &optional follow export)
  7503. "Add TYPE to the list of `org-link-types'.
  7504. Re-compute all regular expressions depending on `org-link-types'
  7505. FOLLOW and EXPORT are two functions.
  7506. FOLLOW should take the link path as the single argument and do whatever
  7507. is necessary to follow the link, for example find a file or display
  7508. a mail message.
  7509. EXPORT should format the link path for export to one of the export formats.
  7510. It should be a function accepting three arguments:
  7511. path the path of the link, the text after the prefix (like \"http:\")
  7512. desc the description of the link, if any, or a description added by
  7513. org-export-normalize-links if there is none
  7514. format the export format, a symbol like `html' or `latex' or `ascii'..
  7515. The function may use the FORMAT information to return different values
  7516. depending on the format. The return value will be put literally into
  7517. the exported file. If the return value is nil, this means Org should
  7518. do what it normally does with links which do not have EXPORT defined.
  7519. Org-mode has a built-in default for exporting links. If you are happy with
  7520. this default, there is no need to define an export function for the link
  7521. type. For a simple example of an export function, see `org-bbdb.el'."
  7522. (add-to-list 'org-link-types type t)
  7523. (org-make-link-regexps)
  7524. (if (assoc type org-link-protocols)
  7525. (setcdr (assoc type org-link-protocols) (list follow export))
  7526. (push (list type follow export) org-link-protocols)))
  7527. (defvar org-agenda-buffer-name)
  7528. ;;;###autoload
  7529. (defun org-store-link (arg)
  7530. "\\<org-mode-map>Store an org-link to the current location.
  7531. This link is added to `org-stored-links' and can later be inserted
  7532. into an org-buffer with \\[org-insert-link].
  7533. For some link types, a prefix arg is interpreted:
  7534. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7535. For file links, arg negates `org-context-in-file-links'."
  7536. (interactive "P")
  7537. (org-load-modules-maybe)
  7538. (setq org-store-link-plist nil) ; reset
  7539. (org-with-limited-levels
  7540. (let (link cpltxt desc description search txt custom-id agenda-link)
  7541. (cond
  7542. ((run-hook-with-args-until-success 'org-store-link-functions)
  7543. (setq link (plist-get org-store-link-plist :link)
  7544. desc (or (plist-get org-store-link-plist :description) link)))
  7545. ((equal (buffer-name) "*Org Edit Src Example*")
  7546. (let (label gc)
  7547. (while (or (not label)
  7548. (save-excursion
  7549. (save-restriction
  7550. (widen)
  7551. (goto-char (point-min))
  7552. (re-search-forward
  7553. (regexp-quote (format org-coderef-label-format label))
  7554. nil t))))
  7555. (when label (message "Label exists already") (sit-for 2))
  7556. (setq label (read-string "Code line label: " label)))
  7557. (end-of-line 1)
  7558. (setq link (format org-coderef-label-format label))
  7559. (setq gc (- 79 (length link)))
  7560. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7561. (insert link)
  7562. (setq link (concat "(" label ")") desc nil)))
  7563. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7564. ;; We are in the agenda, link to referenced location
  7565. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7566. (get-text-property (point) 'org-marker))))
  7567. (when m
  7568. (org-with-point-at m
  7569. (setq agenda-link
  7570. (if (interactive-p)
  7571. (call-interactively 'org-store-link)
  7572. (org-store-link nil)))))))
  7573. ((eq major-mode 'calendar-mode)
  7574. (let ((cd (calendar-cursor-to-date)))
  7575. (setq link
  7576. (format-time-string
  7577. (car org-time-stamp-formats)
  7578. (apply 'encode-time
  7579. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7580. nil nil nil))))
  7581. (org-store-link-props :type "calendar" :date cd)))
  7582. ((eq major-mode 'w3-mode)
  7583. (setq cpltxt (if (and (buffer-name)
  7584. (not (string-match "Untitled" (buffer-name))))
  7585. (buffer-name)
  7586. (url-view-url t))
  7587. link (org-make-link (url-view-url t)))
  7588. (org-store-link-props :type "w3" :url (url-view-url t)))
  7589. ((eq major-mode 'w3m-mode)
  7590. (setq cpltxt (or w3m-current-title w3m-current-url)
  7591. link (org-make-link w3m-current-url))
  7592. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7593. ((setq search (run-hook-with-args-until-success
  7594. 'org-create-file-search-functions))
  7595. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7596. "::" search))
  7597. (setq cpltxt (or description link)))
  7598. ((eq major-mode 'image-mode)
  7599. (setq cpltxt (concat "file:"
  7600. (abbreviate-file-name buffer-file-name))
  7601. link (org-make-link cpltxt))
  7602. (org-store-link-props :type "image" :file buffer-file-name))
  7603. ((eq major-mode 'dired-mode)
  7604. ;; link to the file in the current line
  7605. (let ((file (dired-get-filename nil t)))
  7606. (setq file (if file
  7607. (abbreviate-file-name
  7608. (expand-file-name (dired-get-filename nil t)))
  7609. ;; otherwise, no file so use current directory.
  7610. default-directory))
  7611. (setq cpltxt (concat "file:" file)
  7612. link (org-make-link cpltxt))))
  7613. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7614. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7615. (cond
  7616. ((org-in-regexp "<<\\(.*?\\)>>")
  7617. (setq cpltxt
  7618. (concat "file:"
  7619. (abbreviate-file-name
  7620. (buffer-file-name (buffer-base-buffer)))
  7621. "::" (match-string 1))
  7622. link (org-make-link cpltxt)))
  7623. ((and (featurep 'org-id)
  7624. (or (eq org-link-to-org-use-id t)
  7625. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7626. (interactive-p))
  7627. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7628. (interactive-p)
  7629. (not custom-id))
  7630. (and org-link-to-org-use-id
  7631. (org-entry-get nil "ID"))))
  7632. ;; We can make a link using the ID.
  7633. (setq link (condition-case nil
  7634. (prog1 (org-id-store-link)
  7635. (setq desc (plist-get org-store-link-plist
  7636. :description)))
  7637. (error
  7638. ;; probably before first headline, link to file only
  7639. (concat "file:"
  7640. (abbreviate-file-name
  7641. (buffer-file-name (buffer-base-buffer))))))))
  7642. (t
  7643. ;; Just link to current headline
  7644. (setq cpltxt (concat "file:"
  7645. (abbreviate-file-name
  7646. (buffer-file-name (buffer-base-buffer)))))
  7647. ;; Add a context search string
  7648. (when (org-xor org-context-in-file-links arg)
  7649. (setq txt (cond
  7650. ((org-on-heading-p) nil)
  7651. ((org-region-active-p)
  7652. (buffer-substring (region-beginning) (region-end)))
  7653. (t nil)))
  7654. (when (or (null txt) (string-match "\\S-" txt))
  7655. (setq cpltxt
  7656. (concat cpltxt "::"
  7657. (condition-case nil
  7658. (org-make-org-heading-search-string txt)
  7659. (error "")))
  7660. desc (or (nth 4 (ignore-errors
  7661. (org-heading-components))) "NONE"))))
  7662. (if (string-match "::\\'" cpltxt)
  7663. (setq cpltxt (substring cpltxt 0 -2)))
  7664. (setq link (org-make-link cpltxt)))))
  7665. ((buffer-file-name (buffer-base-buffer))
  7666. ;; Just link to this file here.
  7667. (setq cpltxt (concat "file:"
  7668. (abbreviate-file-name
  7669. (buffer-file-name (buffer-base-buffer)))))
  7670. ;; Add a context string
  7671. (when (org-xor org-context-in-file-links arg)
  7672. (setq txt (if (org-region-active-p)
  7673. (buffer-substring (region-beginning) (region-end))
  7674. (buffer-substring (point-at-bol) (point-at-eol))))
  7675. ;; Only use search option if there is some text.
  7676. (when (string-match "\\S-" txt)
  7677. (setq cpltxt
  7678. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7679. desc "NONE")))
  7680. (setq link (org-make-link cpltxt)))
  7681. ((interactive-p)
  7682. (error "Cannot link to a buffer which is not visiting a file"))
  7683. (t (setq link nil)))
  7684. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7685. (setq link (or link cpltxt)
  7686. desc (or desc cpltxt))
  7687. (if (equal desc "NONE") (setq desc nil))
  7688. (if (and (or (interactive-p) executing-kbd-macro) link)
  7689. (progn
  7690. (setq org-stored-links
  7691. (cons (list link desc) org-stored-links))
  7692. (message "Stored: %s" (or desc link))
  7693. (when custom-id
  7694. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7695. "::#" custom-id))
  7696. (setq org-stored-links
  7697. (cons (list link desc) org-stored-links))))
  7698. (or agenda-link (and link (org-make-link-string link desc)))))))
  7699. (defun org-store-link-props (&rest plist)
  7700. "Store link properties, extract names and addresses."
  7701. (let (x adr)
  7702. (when (setq x (plist-get plist :from))
  7703. (setq adr (mail-extract-address-components x))
  7704. (setq plist (plist-put plist :fromname (car adr)))
  7705. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7706. (when (setq x (plist-get plist :to))
  7707. (setq adr (mail-extract-address-components x))
  7708. (setq plist (plist-put plist :toname (car adr)))
  7709. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7710. (let ((from (plist-get plist :from))
  7711. (to (plist-get plist :to)))
  7712. (when (and from to org-from-is-user-regexp)
  7713. (setq plist
  7714. (plist-put plist :fromto
  7715. (if (string-match org-from-is-user-regexp from)
  7716. (concat "to %t")
  7717. (concat "from %f"))))))
  7718. (setq org-store-link-plist plist))
  7719. (defun org-add-link-props (&rest plist)
  7720. "Add these properties to the link property list."
  7721. (let (key value)
  7722. (while plist
  7723. (setq key (pop plist) value (pop plist))
  7724. (setq org-store-link-plist
  7725. (plist-put org-store-link-plist key value)))))
  7726. (defun org-email-link-description (&optional fmt)
  7727. "Return the description part of an email link.
  7728. This takes information from `org-store-link-plist' and formats it
  7729. according to FMT (default from `org-email-link-description-format')."
  7730. (setq fmt (or fmt org-email-link-description-format))
  7731. (let* ((p org-store-link-plist)
  7732. (to (plist-get p :toaddress))
  7733. (from (plist-get p :fromaddress))
  7734. (table
  7735. (list
  7736. (cons "%c" (plist-get p :fromto))
  7737. (cons "%F" (plist-get p :from))
  7738. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7739. (cons "%T" (plist-get p :to))
  7740. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7741. (cons "%s" (plist-get p :subject))
  7742. (cons "%d" (plist-get p :date))
  7743. (cons "%m" (plist-get p :message-id)))))
  7744. (when (string-match "%c" fmt)
  7745. ;; Check if the user wrote this message
  7746. (if (and org-from-is-user-regexp from to
  7747. (save-match-data (string-match org-from-is-user-regexp from)))
  7748. (setq fmt (replace-match "to %t" t t fmt))
  7749. (setq fmt (replace-match "from %f" t t fmt))))
  7750. (org-replace-escapes fmt table)))
  7751. (defun org-make-org-heading-search-string (&optional string heading)
  7752. "Make search string for STRING or current headline."
  7753. (interactive)
  7754. (let ((s (or string (org-get-heading)))
  7755. (lines org-context-in-file-links))
  7756. (unless (and string (not heading))
  7757. ;; We are using a headline, clean up garbage in there.
  7758. (if (string-match org-todo-regexp s)
  7759. (setq s (replace-match "" t t s)))
  7760. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7761. (setq s (replace-match "" t t s)))
  7762. (setq s (org-trim s))
  7763. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7764. org-comment-string "\\)") s)
  7765. (setq s (replace-match "" t t s)))
  7766. (while (string-match org-ts-regexp s)
  7767. (setq s (replace-match "" t t s))))
  7768. (or string (setq s (concat "*" s))) ; Add * for headlines
  7769. (when (and string (integerp lines) (> lines 0))
  7770. (let ((slines (org-split-string s "\n")))
  7771. (when (< lines (length slines))
  7772. (setq s (mapconcat
  7773. 'identity
  7774. (reverse (nthcdr (- (length slines) lines)
  7775. (reverse slines))) "\n")))))
  7776. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7777. (defun org-make-link (&rest strings)
  7778. "Concatenate STRINGS."
  7779. (apply 'concat strings))
  7780. (defun org-make-link-string (link &optional description)
  7781. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7782. (unless (string-match "\\S-" link)
  7783. (error "Empty link"))
  7784. (when (and description
  7785. (stringp description)
  7786. (not (string-match "\\S-" description)))
  7787. (setq description nil))
  7788. (when (stringp description)
  7789. ;; Remove brackets from the description, they are fatal.
  7790. (while (string-match "\\[" description)
  7791. (setq description (replace-match "{" t t description)))
  7792. (while (string-match "\\]" description)
  7793. (setq description (replace-match "}" t t description))))
  7794. (when (equal (org-link-escape link) description)
  7795. ;; No description needed, it is identical
  7796. (setq description nil))
  7797. (when (and (not description)
  7798. (not (equal link (org-link-escape link))))
  7799. (setq description (org-extract-attributes link)))
  7800. (setq link (if (string-match org-link-types-re link)
  7801. (concat (match-string 1 link)
  7802. (org-link-escape (substring link (match-end 1))))
  7803. (org-link-escape link)))
  7804. (concat "[[" link "]"
  7805. (if description (concat "[" description "]") "")
  7806. "]"))
  7807. (defconst org-link-escape-chars
  7808. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  7809. "List of characters that should be escaped in link.
  7810. This is the list that is used for internal purposes.")
  7811. (defvar org-url-encoding-use-url-hexify nil)
  7812. (defconst org-link-escape-chars-browser
  7813. '(?\ )
  7814. "List of escapes for characters that are problematic in links.
  7815. This is the list that is used before handing over to the browser.")
  7816. (defun org-link-escape (text &optional table merge)
  7817. "Return percent escaped representation of TEXT.
  7818. TEXT is a string with the text to escape.
  7819. Optional argument TABLE is a list with characters that should be
  7820. escaped. When nil, `org-link-escape-chars' is used.
  7821. If optional argument MERGE is set, merge TABLE into
  7822. `org-link-escape-chars'."
  7823. (if (and org-url-encoding-use-url-hexify (not table))
  7824. (url-hexify-string text)
  7825. (cond
  7826. ((and table merge)
  7827. (mapc (lambda (defchr)
  7828. (unless (member defchr table)
  7829. (setq table (cons defchr table)))) org-link-escape-chars))
  7830. ((null table)
  7831. (setq table org-link-escape-chars)))
  7832. (mapconcat
  7833. (lambda (char)
  7834. (if (or (member char table)
  7835. (< char 32) (= char 37) (> char 126))
  7836. (mapconcat (lambda (sequence-element)
  7837. (format "%%%.2X" sequence-element))
  7838. (or (encode-coding-char char 'utf-8)
  7839. (error "Unable to percent escape character: %s"
  7840. (char-to-string char))) "")
  7841. (char-to-string char))) text "")))
  7842. (defun org-link-unescape (str)
  7843. "Unhex hexified unicode strings as returned from the JavaScript function
  7844. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  7845. (unless (and (null str) (string= "" str))
  7846. (let ((pos 0) (case-fold-search t) unhexed)
  7847. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  7848. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  7849. (setq str (replace-match unhexed t t str))
  7850. (setq pos (+ pos (length unhexed))))))
  7851. str)
  7852. (defun org-link-unescape-compound (hex)
  7853. "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  7854. Note: this function also decodes single byte encodings like
  7855. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  7856. (save-match-data
  7857. (let* ((bytes (cdr (split-string hex "%")))
  7858. (ret "")
  7859. (eat 0)
  7860. (sum 0))
  7861. (while bytes
  7862. (let* ((val (string-to-number (pop bytes) 16))
  7863. (shift-xor
  7864. (if (= 0 eat)
  7865. (cond
  7866. ((>= val 252) (cons 6 252))
  7867. ((>= val 248) (cons 5 248))
  7868. ((>= val 240) (cons 4 240))
  7869. ((>= val 224) (cons 3 224))
  7870. ((>= val 192) (cons 2 192))
  7871. (t (cons 0 0)))
  7872. (cons 6 128))))
  7873. (if (>= val 192) (setq eat (car shift-xor)))
  7874. (setq val (logxor val (cdr shift-xor)))
  7875. (setq sum (+ (lsh sum (car shift-xor)) val))
  7876. (if (> eat 0) (setq eat (- eat 1)))
  7877. (cond
  7878. ((= 0 eat) ;multi byte
  7879. (setq ret (concat ret (org-char-to-string sum)))
  7880. (setq sum 0))
  7881. ((not bytes) ; single byte(s)
  7882. (setq ret (org-link-unescape-single-byte-sequence hex))))
  7883. )) ;; end (while bytes
  7884. ret )))
  7885. (defun org-link-unescape-single-byte-sequence (hex)
  7886. "Unhexify hex-encoded single byte character sequences."
  7887. (mapconcat (lambda (byte)
  7888. (char-to-string (string-to-number byte 16)))
  7889. (cdr (split-string hex "%")) ""))
  7890. (defun org-xor (a b)
  7891. "Exclusive or."
  7892. (if a (not b) b))
  7893. (defun org-fixup-message-id-for-http (s)
  7894. "Replace special characters in a message id, so it can be used in an http query."
  7895. (when (string-match "%" s)
  7896. (setq s (mapconcat (lambda (c)
  7897. (if (eq c ?%)
  7898. "%25"
  7899. (char-to-string c)))
  7900. s "")))
  7901. (while (string-match "<" s)
  7902. (setq s (replace-match "%3C" t t s)))
  7903. (while (string-match ">" s)
  7904. (setq s (replace-match "%3E" t t s)))
  7905. (while (string-match "@" s)
  7906. (setq s (replace-match "%40" t t s)))
  7907. s)
  7908. ;;;###autoload
  7909. (defun org-insert-link-global ()
  7910. "Insert a link like Org-mode does.
  7911. This command can be called in any mode to insert a link in Org-mode syntax."
  7912. (interactive)
  7913. (org-load-modules-maybe)
  7914. (org-run-like-in-org-mode 'org-insert-link))
  7915. (defun org-insert-link (&optional complete-file link-location)
  7916. "Insert a link. At the prompt, enter the link.
  7917. Completion can be used to insert any of the link protocol prefixes like
  7918. http or ftp in use.
  7919. The history can be used to select a link previously stored with
  7920. `org-store-link'. When the empty string is entered (i.e. if you just
  7921. press RET at the prompt), the link defaults to the most recently
  7922. stored link. As SPC triggers completion in the minibuffer, you need to
  7923. use M-SPC or C-q SPC to force the insertion of a space character.
  7924. You will also be prompted for a description, and if one is given, it will
  7925. be displayed in the buffer instead of the link.
  7926. If there is already a link at point, this command will allow you to edit link
  7927. and description parts.
  7928. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7929. be selected using completion. The path to the file will be relative to the
  7930. current directory if the file is in the current directory or a subdirectory.
  7931. Otherwise, the link will be the absolute path as completed in the minibuffer
  7932. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7933. option `org-link-file-path-type'.
  7934. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7935. the current directory or below.
  7936. With three \\[universal-argument] prefixes, negate the meaning of
  7937. `org-keep-stored-link-after-insertion'.
  7938. If `org-make-link-description-function' is non-nil, this function will be
  7939. called with the link target, and the result will be the default
  7940. link description.
  7941. If the LINK-LOCATION parameter is non-nil, this value will be
  7942. used as the link location instead of reading one interactively."
  7943. (interactive "P")
  7944. (let* ((wcf (current-window-configuration))
  7945. (region (if (org-region-active-p)
  7946. (buffer-substring (region-beginning) (region-end))))
  7947. (remove (and region (list (region-beginning) (region-end))))
  7948. (desc region)
  7949. tmphist ; byte-compile incorrectly complains about this
  7950. (link link-location)
  7951. entry file all-prefixes)
  7952. (cond
  7953. (link-location) ; specified by arg, just use it.
  7954. ((org-in-regexp org-bracket-link-regexp 1)
  7955. ;; We do have a link at point, and we are going to edit it.
  7956. (setq remove (list (match-beginning 0) (match-end 0)))
  7957. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7958. (setq link (read-string "Link: "
  7959. (org-link-unescape
  7960. (org-match-string-no-properties 1)))))
  7961. ((or (org-in-regexp org-angle-link-re)
  7962. (org-in-regexp org-plain-link-re))
  7963. ;; Convert to bracket link
  7964. (setq remove (list (match-beginning 0) (match-end 0))
  7965. link (read-string "Link: "
  7966. (org-remove-angle-brackets (match-string 0)))))
  7967. ((member complete-file '((4) (16)))
  7968. ;; Completing read for file names.
  7969. (setq link (org-file-complete-link complete-file)))
  7970. (t
  7971. ;; Read link, with completion for stored links.
  7972. (with-output-to-temp-buffer "*Org Links*"
  7973. (princ "Insert a link.
  7974. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7975. (when org-stored-links
  7976. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7977. (princ (mapconcat
  7978. (lambda (x)
  7979. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7980. (reverse org-stored-links) "\n"))))
  7981. (let ((cw (selected-window)))
  7982. (select-window (get-buffer-window "*Org Links*" 'visible))
  7983. (setq truncate-lines t)
  7984. (unless (pos-visible-in-window-p (point-max))
  7985. (org-fit-window-to-buffer))
  7986. (and (window-live-p cw) (select-window cw)))
  7987. ;; Fake a link history, containing the stored links.
  7988. (setq tmphist (append (mapcar 'car org-stored-links)
  7989. org-insert-link-history))
  7990. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7991. (mapcar 'car org-link-abbrev-alist)
  7992. org-link-types))
  7993. (unwind-protect
  7994. (progn
  7995. (setq link
  7996. (let ((org-completion-use-ido nil)
  7997. (org-completion-use-iswitchb nil))
  7998. (org-completing-read
  7999. "Link: "
  8000. (append
  8001. (mapcar (lambda (x) (list (concat x ":")))
  8002. all-prefixes)
  8003. (mapcar 'car org-stored-links))
  8004. nil nil nil
  8005. 'tmphist
  8006. (car (car org-stored-links)))))
  8007. (if (not (string-match "\\S-" link))
  8008. (error "No link selected"))
  8009. (if (or (member link all-prefixes)
  8010. (and (equal ":" (substring link -1))
  8011. (member (substring link 0 -1) all-prefixes)
  8012. (setq link (substring link 0 -1))))
  8013. (setq link (org-link-try-special-completion link))))
  8014. (set-window-configuration wcf)
  8015. (kill-buffer "*Org Links*"))
  8016. (setq entry (assoc link org-stored-links))
  8017. (or entry (push link org-insert-link-history))
  8018. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8019. (not org-keep-stored-link-after-insertion))
  8020. (setq org-stored-links (delq (assoc link org-stored-links)
  8021. org-stored-links)))
  8022. (setq desc (or desc (nth 1 entry)))))
  8023. (if (string-match org-plain-link-re link)
  8024. ;; URL-like link, normalize the use of angular brackets.
  8025. (setq link (org-make-link (org-remove-angle-brackets link))))
  8026. ;; Check if we are linking to the current file with a search option
  8027. ;; If yes, simplify the link by using only the search option.
  8028. (when (and buffer-file-name
  8029. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8030. (let* ((path (match-string 1 link))
  8031. (case-fold-search nil)
  8032. (search (match-string 2 link)))
  8033. (save-match-data
  8034. (if (equal (file-truename buffer-file-name) (file-truename path))
  8035. ;; We are linking to this same file, with a search option
  8036. (setq link search)))))
  8037. ;; Check if we can/should use a relative path. If yes, simplify the link
  8038. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8039. (let* ((type (match-string 1 link))
  8040. (path (match-string 2 link))
  8041. (origpath path)
  8042. (case-fold-search nil))
  8043. (cond
  8044. ((or (eq org-link-file-path-type 'absolute)
  8045. (equal complete-file '(16)))
  8046. (setq path (abbreviate-file-name (expand-file-name path))))
  8047. ((eq org-link-file-path-type 'noabbrev)
  8048. (setq path (expand-file-name path)))
  8049. ((eq org-link-file-path-type 'relative)
  8050. (setq path (file-relative-name path)))
  8051. (t
  8052. (save-match-data
  8053. (if (string-match (concat "^" (regexp-quote
  8054. (expand-file-name
  8055. (file-name-as-directory
  8056. default-directory))))
  8057. (expand-file-name path))
  8058. ;; We are linking a file with relative path name.
  8059. (setq path (substring (expand-file-name path)
  8060. (match-end 0)))
  8061. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8062. (setq link (concat type path))
  8063. (if (equal desc origpath)
  8064. (setq desc path))))
  8065. (if org-make-link-description-function
  8066. (setq desc (funcall org-make-link-description-function link desc)))
  8067. (setq desc (read-string "Description: " desc))
  8068. (unless (string-match "\\S-" desc) (setq desc nil))
  8069. (if remove (apply 'delete-region remove))
  8070. (insert (org-make-link-string link desc))))
  8071. (defun org-link-try-special-completion (type)
  8072. "If there is completion support for link type TYPE, offer it."
  8073. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8074. (if (functionp fun)
  8075. (funcall fun)
  8076. (read-string "Link (no completion support): " (concat type ":")))))
  8077. (defun org-file-complete-link (&optional arg)
  8078. "Create a file link using completion."
  8079. (let (file link)
  8080. (setq file (read-file-name "File: "))
  8081. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8082. (pwd1 (file-name-as-directory (abbreviate-file-name
  8083. (expand-file-name ".")))))
  8084. (cond
  8085. ((equal arg '(16))
  8086. (setq link (org-make-link
  8087. "file:"
  8088. (abbreviate-file-name (expand-file-name file)))))
  8089. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8090. (setq link (org-make-link "file:" (match-string 1 file))))
  8091. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8092. (expand-file-name file))
  8093. (setq link (org-make-link
  8094. "file:" (match-string 1 (expand-file-name file)))))
  8095. (t (setq link (org-make-link "file:" file)))))
  8096. link))
  8097. (defun org-completing-read (&rest args)
  8098. "Completing-read with SPACE being a normal character."
  8099. (let ((minibuffer-local-completion-map
  8100. (copy-keymap minibuffer-local-completion-map)))
  8101. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8102. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8103. (apply 'org-icompleting-read args)))
  8104. (defun org-completing-read-no-i (&rest args)
  8105. (let (org-completion-use-ido org-completion-use-iswitchb)
  8106. (apply 'org-completing-read args)))
  8107. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8108. "Use iswitch as a completing-read replacement to choose from choices.
  8109. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8110. from."
  8111. (let* ((iswitchb-use-virtual-buffers nil)
  8112. (iswitchb-make-buflist-hook
  8113. (lambda ()
  8114. (setq iswitchb-temp-buflist choices))))
  8115. (iswitchb-read-buffer prompt)))
  8116. (defun org-icompleting-read (&rest args)
  8117. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8118. (org-without-partial-completion
  8119. (if (and org-completion-use-ido
  8120. (fboundp 'ido-completing-read)
  8121. (boundp 'ido-mode) ido-mode
  8122. (listp (second args)))
  8123. (let ((ido-enter-matching-directory nil))
  8124. (apply 'ido-completing-read (concat (car args))
  8125. (if (consp (car (nth 1 args)))
  8126. (mapcar 'car (nth 1 args))
  8127. (nth 1 args))
  8128. (cddr args)))
  8129. (if (and org-completion-use-iswitchb
  8130. (boundp 'iswitchb-mode) iswitchb-mode
  8131. (listp (second args)))
  8132. (apply 'org-iswitchb-completing-read (concat (car args))
  8133. (if (consp (car (nth 1 args)))
  8134. (mapcar 'car (nth 1 args))
  8135. (nth 1 args))
  8136. (cddr args))
  8137. (apply 'completing-read args)))))
  8138. (defun org-extract-attributes (s)
  8139. "Extract the attributes cookie from a string and set as text property."
  8140. (let (a attr (start 0) key value)
  8141. (save-match-data
  8142. (when (string-match "{{\\([^}]+\\)}}$" s)
  8143. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8144. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8145. (setq key (match-string 1 a) value (match-string 2 a)
  8146. start (match-end 0)
  8147. attr (plist-put attr (intern key) value))))
  8148. (org-add-props s nil 'org-attr attr))
  8149. s))
  8150. (defun org-extract-attributes-from-string (tag)
  8151. (let (key value attr)
  8152. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8153. (setq key (match-string 1 tag) value (match-string 2 tag)
  8154. tag (replace-match "" t t tag)
  8155. attr (plist-put attr (intern key) value)))
  8156. (cons tag attr)))
  8157. (defun org-attributes-to-string (plist)
  8158. "Format a property list into an HTML attribute list."
  8159. (let ((s "") key value)
  8160. (while plist
  8161. (setq key (pop plist) value (pop plist))
  8162. (and value
  8163. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8164. s))
  8165. ;;; Opening/following a link
  8166. (defvar org-link-search-failed nil)
  8167. (defvar org-open-link-functions nil
  8168. "Hook for functions finding a plain text link.
  8169. These functions must take a single argument, the link content.
  8170. They will be called for links that look like [[link text][description]]
  8171. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8172. like a filename (e.g. \"./blue.png\").
  8173. These functions will be called *before* Org attempts to resolve the
  8174. link by doing text searches in the current buffer - so if you want a
  8175. link \"[[target]]\" to still find \"<<target>>\", your function should
  8176. handle this as a special case.
  8177. When the function does handle the link, it must return a non-nil value.
  8178. If it decides that it is not responsible for this link, it must return
  8179. nil to indicate that that Org-mode can continue with other options
  8180. like exact and fuzzy text search.")
  8181. (defun org-next-link ()
  8182. "Move forward to the next link.
  8183. If the link is in hidden text, expose it."
  8184. (interactive)
  8185. (when (and org-link-search-failed (eq this-command last-command))
  8186. (goto-char (point-min))
  8187. (message "Link search wrapped back to beginning of buffer"))
  8188. (setq org-link-search-failed nil)
  8189. (let* ((pos (point))
  8190. (ct (org-context))
  8191. (a (assoc :link ct)))
  8192. (if a (goto-char (nth 2 a)))
  8193. (if (re-search-forward org-any-link-re nil t)
  8194. (progn
  8195. (goto-char (match-beginning 0))
  8196. (if (outline-invisible-p) (org-show-context)))
  8197. (goto-char pos)
  8198. (setq org-link-search-failed t)
  8199. (error "No further link found"))))
  8200. (defun org-previous-link ()
  8201. "Move backward to the previous link.
  8202. If the link is in hidden text, expose it."
  8203. (interactive)
  8204. (when (and org-link-search-failed (eq this-command last-command))
  8205. (goto-char (point-max))
  8206. (message "Link search wrapped back to end of buffer"))
  8207. (setq org-link-search-failed nil)
  8208. (let* ((pos (point))
  8209. (ct (org-context))
  8210. (a (assoc :link ct)))
  8211. (if a (goto-char (nth 1 a)))
  8212. (if (re-search-backward org-any-link-re nil t)
  8213. (progn
  8214. (goto-char (match-beginning 0))
  8215. (if (outline-invisible-p) (org-show-context)))
  8216. (goto-char pos)
  8217. (setq org-link-search-failed t)
  8218. (error "No further link found"))))
  8219. (defun org-translate-link (s)
  8220. "Translate a link string if a translation function has been defined."
  8221. (if (and org-link-translation-function
  8222. (fboundp org-link-translation-function)
  8223. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8224. (progn
  8225. (setq s (funcall org-link-translation-function
  8226. (match-string 1) (match-string 2)))
  8227. (concat (car s) ":" (cdr s)))
  8228. s))
  8229. (defun org-translate-link-from-planner (type path)
  8230. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8231. This is still an experimental function, your mileage may vary."
  8232. (cond
  8233. ((member type '("http" "https" "news" "ftp"))
  8234. ;; standard Internet links are the same.
  8235. nil)
  8236. ((and (equal type "irc") (string-match "^//" path))
  8237. ;; Planner has two / at the beginning of an irc link, we have 1.
  8238. ;; We should have zero, actually....
  8239. (setq path (substring path 1)))
  8240. ((and (equal type "lisp") (string-match "^/" path))
  8241. ;; Planner has a slash, we do not.
  8242. (setq type "elisp" path (substring path 1)))
  8243. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8244. ;; A typical message link. Planner has the id after the final slash,
  8245. ;; we separate it with a hash mark
  8246. (setq path (concat (match-string 1 path) "#"
  8247. (org-remove-angle-brackets (match-string 2 path)))))
  8248. )
  8249. (cons type path))
  8250. (defun org-find-file-at-mouse (ev)
  8251. "Open file link or URL at mouse."
  8252. (interactive "e")
  8253. (mouse-set-point ev)
  8254. (org-open-at-point 'in-emacs))
  8255. (defun org-open-at-mouse (ev)
  8256. "Open file link or URL at mouse."
  8257. (interactive "e")
  8258. (mouse-set-point ev)
  8259. (if (eq major-mode 'org-agenda-mode)
  8260. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8261. (org-open-at-point))
  8262. (defvar org-window-config-before-follow-link nil
  8263. "The window configuration before following a link.
  8264. This is saved in case the need arises to restore it.")
  8265. (defvar org-open-link-marker (make-marker)
  8266. "Marker pointing to the location where `org-open-at-point; was called.")
  8267. ;;;###autoload
  8268. (defun org-open-at-point-global ()
  8269. "Follow a link like Org-mode does.
  8270. This command can be called in any mode to follow a link that has
  8271. Org-mode syntax."
  8272. (interactive)
  8273. (org-run-like-in-org-mode 'org-open-at-point))
  8274. ;;;###autoload
  8275. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8276. "Open a link in the string S, as if it was in Org-mode."
  8277. (interactive "sLink: \nP")
  8278. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8279. (with-temp-buffer
  8280. (let ((org-inhibit-startup t))
  8281. (org-mode)
  8282. (insert s)
  8283. (goto-char (point-min))
  8284. (when reference-buffer
  8285. (setq org-link-abbrev-alist-local
  8286. (with-current-buffer reference-buffer
  8287. org-link-abbrev-alist-local)))
  8288. (org-open-at-point arg reference-buffer)))))
  8289. (defvar org-open-at-point-functions nil
  8290. "Hook that is run when following a link at point.
  8291. Functions in this hook must return t if they identify and follow
  8292. a link at point. If they don't find anything interesting at point,
  8293. they must return nil.")
  8294. (defun org-open-at-point (&optional arg reference-buffer)
  8295. "Open link at or after point.
  8296. If there is no link at point, this function will search forward up to
  8297. the end of the current line.
  8298. Normally, files will be opened by an appropriate application. If the
  8299. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8300. With a double prefix argument, try to open outside of Emacs, in the
  8301. application the system uses for this file type."
  8302. (interactive "P")
  8303. ;; if in a code block, then open the block's results
  8304. (unless (call-interactively #'org-babel-open-src-block-result)
  8305. (org-load-modules-maybe)
  8306. (move-marker org-open-link-marker (point))
  8307. (setq org-window-config-before-follow-link (current-window-configuration))
  8308. (org-remove-occur-highlights nil nil t)
  8309. (cond
  8310. ((and (org-on-heading-p)
  8311. (not (org-in-regexp
  8312. (concat org-plain-link-re "\\|"
  8313. org-bracket-link-regexp "\\|"
  8314. org-angle-link-re "\\|"
  8315. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8316. (not (get-text-property (point) 'org-linked-text)))
  8317. (or (org-offer-links-in-entry arg)
  8318. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8319. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8320. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8321. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8322. (not (org-in-regexp org-bracket-link-regexp)))
  8323. (org-footnote-action))
  8324. (t
  8325. (let (type path link line search (pos (point)))
  8326. (catch 'match
  8327. (save-excursion
  8328. (skip-chars-forward "^]\n\r")
  8329. (when (org-in-regexp org-bracket-link-regexp 1)
  8330. (setq link (org-extract-attributes
  8331. (org-link-unescape (org-match-string-no-properties 1))))
  8332. (while (string-match " *\n *" link)
  8333. (setq link (replace-match " " t t link)))
  8334. (setq link (org-link-expand-abbrev link))
  8335. (cond
  8336. ((or (file-name-absolute-p link)
  8337. (string-match "^\\.\\.?/" link))
  8338. (setq type "file" path link))
  8339. ((string-match org-link-re-with-space3 link)
  8340. (setq type (match-string 1 link) path (match-string 2 link)))
  8341. (t (setq type "thisfile" path link)))
  8342. (throw 'match t)))
  8343. (when (get-text-property (point) 'org-linked-text)
  8344. (setq type "thisfile"
  8345. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8346. (1+ (point)) (point))
  8347. path (buffer-substring
  8348. (or (previous-single-property-change pos 'org-linked-text)
  8349. (point-min))
  8350. (or (next-single-property-change pos 'org-linked-text)
  8351. (point-max))))
  8352. (throw 'match t))
  8353. (save-excursion
  8354. (when (or (org-in-regexp org-angle-link-re)
  8355. (org-in-regexp org-plain-link-re))
  8356. (setq type (match-string 1) path (match-string 2))
  8357. (throw 'match t)))
  8358. (save-excursion
  8359. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8360. (setq type "tags"
  8361. path (match-string 1))
  8362. (while (string-match ":" path)
  8363. (setq path (replace-match "+" t t path)))
  8364. (throw 'match t)))
  8365. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8366. (setq type "tree-match"
  8367. path (match-string 1))
  8368. (throw 'match t)))
  8369. (unless path
  8370. (error "No link found"))
  8371. ;; switch back to reference buffer
  8372. ;; needed when if called in a temporary buffer through
  8373. ;; org-open-link-from-string
  8374. (with-current-buffer (or reference-buffer (current-buffer))
  8375. ;; Remove any trailing spaces in path
  8376. (if (string-match " +\\'" path)
  8377. (setq path (replace-match "" t t path)))
  8378. (if (and org-link-translation-function
  8379. (fboundp org-link-translation-function))
  8380. ;; Check if we need to translate the link
  8381. (let ((tmp (funcall org-link-translation-function type path)))
  8382. (setq type (car tmp) path (cdr tmp))))
  8383. (cond
  8384. ((assoc type org-link-protocols)
  8385. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8386. ((equal type "mailto")
  8387. (let ((cmd (car org-link-mailto-program))
  8388. (args (cdr org-link-mailto-program)) args1
  8389. (address path) (subject "") a)
  8390. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8391. (setq address (match-string 1 path)
  8392. subject (org-link-escape (match-string 2 path))))
  8393. (while args
  8394. (cond
  8395. ((not (stringp (car args))) (push (pop args) args1))
  8396. (t (setq a (pop args))
  8397. (if (string-match "%a" a)
  8398. (setq a (replace-match address t t a)))
  8399. (if (string-match "%s" a)
  8400. (setq a (replace-match subject t t a)))
  8401. (push a args1))))
  8402. (apply cmd (nreverse args1))))
  8403. ((member type '("http" "https" "ftp" "news"))
  8404. (browse-url (concat type ":" (org-link-escape
  8405. path org-link-escape-chars-browser))))
  8406. ((string= type "doi")
  8407. (browse-url (concat "http://dx.doi.org/"
  8408. (org-link-escape
  8409. path org-link-escape-chars-browser))))
  8410. ((member type '("message"))
  8411. (browse-url (concat type ":" path)))
  8412. ((string= type "tags")
  8413. (org-tags-view arg path))
  8414. ((string= type "tree-match")
  8415. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8416. ((string= type "file")
  8417. (if (string-match "::\\([0-9]+\\)\\'" path)
  8418. (setq line (string-to-number (match-string 1 path))
  8419. path (substring path 0 (match-beginning 0)))
  8420. (if (string-match "::\\(.+\\)\\'" path)
  8421. (setq search (match-string 1 path)
  8422. path (substring path 0 (match-beginning 0)))))
  8423. (if (string-match "[*?{]" (file-name-nondirectory path))
  8424. (dired path)
  8425. (org-open-file path arg line search)))
  8426. ((string= type "shell")
  8427. (let ((cmd path))
  8428. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8429. (string-match org-confirm-shell-link-not-regexp cmd))
  8430. (not org-confirm-shell-link-function)
  8431. (funcall org-confirm-shell-link-function
  8432. (format "Execute \"%s\" in shell? "
  8433. (org-add-props cmd nil
  8434. 'face 'org-warning))))
  8435. (progn
  8436. (message "Executing %s" cmd)
  8437. (shell-command cmd))
  8438. (error "Abort"))))
  8439. ((string= type "elisp")
  8440. (let ((cmd path))
  8441. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8442. (string-match org-confirm-elisp-link-not-regexp cmd))
  8443. (not org-confirm-elisp-link-function)
  8444. (funcall org-confirm-elisp-link-function
  8445. (format "Execute \"%s\" as elisp? "
  8446. (org-add-props cmd nil
  8447. 'face 'org-warning))))
  8448. (message "%s => %s" cmd
  8449. (if (equal (string-to-char cmd) ?\()
  8450. (eval (read cmd))
  8451. (call-interactively (read cmd))))
  8452. (error "Abort"))))
  8453. ((and (string= type "thisfile")
  8454. (run-hook-with-args-until-success
  8455. 'org-open-link-functions path)))
  8456. ((string= type "thisfile")
  8457. (if arg
  8458. (switch-to-buffer-other-window
  8459. (org-get-buffer-for-internal-link (current-buffer)))
  8460. (org-mark-ring-push))
  8461. (let ((cmd `(org-link-search
  8462. ,path
  8463. ,(cond ((equal arg '(4)) ''occur)
  8464. ((equal arg '(16)) ''org-occur)
  8465. (t nil))
  8466. ,pos)))
  8467. (condition-case nil (eval cmd)
  8468. (error (progn (widen) (eval cmd))))))
  8469. (t
  8470. (browse-url-at-point)))))))
  8471. (move-marker org-open-link-marker nil)
  8472. (run-hook-with-args 'org-follow-link-hook)))
  8473. (defun org-offer-links-in-entry (&optional nth zero)
  8474. "Offer links in the current entry and follow the selected link.
  8475. If there is only one link, follow it immediately as well.
  8476. If NTH is an integer, immediately pick the NTH link found.
  8477. If ZERO is a string, check also this string for a link, and if
  8478. there is one, offer it as link number zero."
  8479. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8480. "\\(" org-angle-link-re "\\)\\|"
  8481. "\\(" org-plain-link-re "\\)"))
  8482. (cnt ?0)
  8483. (in-emacs (if (integerp nth) nil nth))
  8484. have-zero end links link c)
  8485. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8486. (push (match-string 0 zero) links)
  8487. (setq cnt (1- cnt) have-zero t))
  8488. (save-excursion
  8489. (org-back-to-heading t)
  8490. (setq end (save-excursion (outline-next-heading) (point)))
  8491. (while (re-search-forward re end t)
  8492. (push (match-string 0) links))
  8493. (setq links (org-uniquify (reverse links))))
  8494. (cond
  8495. ((null links)
  8496. (message "No links"))
  8497. ((equal (length links) 1)
  8498. (setq link (list (car links))))
  8499. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8500. (setq link (nth (if have-zero nth (1- nth)) links)))
  8501. (t ; we have to select a link
  8502. (save-excursion
  8503. (save-window-excursion
  8504. (delete-other-windows)
  8505. (with-output-to-temp-buffer "*Select Link*"
  8506. (mapc (lambda (l)
  8507. (if (not (string-match org-bracket-link-regexp l))
  8508. (princ (format "[%c] %s\n" (incf cnt)
  8509. (org-remove-angle-brackets l)))
  8510. (if (match-end 3)
  8511. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8512. (match-string 3 l) (match-string 1 l)))
  8513. (princ (format "[%c] %s\n" (incf cnt)
  8514. (match-string 1 l))))))
  8515. links))
  8516. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8517. (message "Select link to open, RET to open all:")
  8518. (setq c (read-char-exclusive))
  8519. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8520. (when (equal c ?q) (error "Abort"))
  8521. (if (equal c ?\C-m)
  8522. (setq link links)
  8523. (setq nth (- c ?0))
  8524. (if have-zero (setq nth (1+ nth)))
  8525. (unless (and (integerp nth) (>= (length links) nth))
  8526. (error "Invalid link selection"))
  8527. (setq link (list (nth (1- nth) links))))))
  8528. (if link
  8529. (let ((buf (current-buffer)))
  8530. (dolist (l link)
  8531. (org-open-link-from-string l in-emacs buf))
  8532. t)
  8533. nil)))
  8534. ;; Add special file links that specify the way of opening
  8535. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8536. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8537. (defun org-open-file-with-system (path)
  8538. "Open file at PATH using the system way of opening it."
  8539. (org-open-file path 'system))
  8540. (defun org-open-file-with-emacs (path)
  8541. "Open file at PATH in Emacs."
  8542. (org-open-file path 'emacs))
  8543. (defun org-remove-file-link-modifiers ()
  8544. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8545. (goto-char (point-min))
  8546. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8547. (org-if-unprotected
  8548. (replace-match "file:" t t))))
  8549. (eval-after-load "org-exp"
  8550. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8551. 'org-remove-file-link-modifiers))
  8552. ;;;; Time estimates
  8553. (defun org-get-effort (&optional pom)
  8554. "Get the effort estimate for the current entry."
  8555. (org-entry-get pom org-effort-property))
  8556. ;;; File search
  8557. (defvar org-create-file-search-functions nil
  8558. "List of functions to construct the right search string for a file link.
  8559. These functions are called in turn with point at the location to
  8560. which the link should point.
  8561. A function in the hook should first test if it would like to
  8562. handle this file type, for example by checking the `major-mode'
  8563. or the file extension. If it decides not to handle this file, it
  8564. should just return nil to give other functions a chance. If it
  8565. does handle the file, it must return the search string to be used
  8566. when following the link. The search string will be part of the
  8567. file link, given after a double colon, and `org-open-at-point'
  8568. will automatically search for it. If special measures must be
  8569. taken to make the search successful, another function should be
  8570. added to the companion hook `org-execute-file-search-functions',
  8571. which see.
  8572. A function in this hook may also use `setq' to set the variable
  8573. `description' to provide a suggestion for the descriptive text to
  8574. be used for this link when it gets inserted into an Org-mode
  8575. buffer with \\[org-insert-link].")
  8576. (defvar org-execute-file-search-functions nil
  8577. "List of functions to execute a file search triggered by a link.
  8578. Functions added to this hook must accept a single argument, the
  8579. search string that was part of the file link, the part after the
  8580. double colon. The function must first check if it would like to
  8581. handle this search, for example by checking the `major-mode' or
  8582. the file extension. If it decides not to handle this search, it
  8583. should just return nil to give other functions a chance. If it
  8584. does handle the search, it must return a non-nil value to keep
  8585. other functions from trying.
  8586. Each function can access the current prefix argument through the
  8587. variable `current-prefix-argument'. Note that a single prefix is
  8588. used to force opening a link in Emacs, so it may be good to only
  8589. use a numeric or double prefix to guide the search function.
  8590. In case this is needed, a function in this hook can also restore
  8591. the window configuration before `org-open-at-point' was called using:
  8592. (set-window-configuration org-window-config-before-follow-link)")
  8593. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8594. (defun org-link-search (s &optional type avoid-pos)
  8595. "Search for a link search option.
  8596. If S is surrounded by forward slashes, it is interpreted as a
  8597. regular expression. In org-mode files, this will create an `org-occur'
  8598. sparse tree. In ordinary files, `occur' will be used to list matches.
  8599. If the current buffer is in `dired-mode', grep will be used to search
  8600. in all files. If AVOID-POS is given, ignore matches near that position."
  8601. (let ((case-fold-search t)
  8602. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8603. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8604. (append '(("") (" ") ("\t") ("\n"))
  8605. org-emphasis-alist)
  8606. "\\|") "\\)"))
  8607. (pos (point))
  8608. (pre nil) (post nil)
  8609. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8610. (cond
  8611. ;; First check if there are any special search functions
  8612. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8613. ;; Now try the builtin stuff
  8614. ((and (equal (string-to-char s0) ?#)
  8615. (> (length s0) 1)
  8616. (save-excursion
  8617. (goto-char (point-min))
  8618. (and
  8619. (re-search-forward
  8620. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8621. (setq type 'dedicated
  8622. pos (match-beginning 0))))
  8623. ;; There is an exact target for this
  8624. (goto-char pos)
  8625. (org-back-to-heading t)))
  8626. ((save-excursion
  8627. (goto-char (point-min))
  8628. (and
  8629. (re-search-forward
  8630. (concat "<<" (regexp-quote s0) ">>") nil t)
  8631. (setq type 'dedicated
  8632. pos (match-beginning 0))))
  8633. ;; There is an exact target for this
  8634. (goto-char pos))
  8635. ((and (string-match "^(\\(.*\\))$" s0)
  8636. (save-excursion
  8637. (goto-char (point-min))
  8638. (and
  8639. (re-search-forward
  8640. (concat "[^[]" (regexp-quote
  8641. (format org-coderef-label-format
  8642. (match-string 1 s0))))
  8643. nil t)
  8644. (setq type 'dedicated
  8645. pos (1+ (match-beginning 0))))))
  8646. ;; There is a coderef target for this
  8647. (goto-char pos))
  8648. ((string-match "^/\\(.*\\)/$" s)
  8649. ;; A regular expression
  8650. (cond
  8651. ((org-mode-p)
  8652. (org-occur (match-string 1 s)))
  8653. ;;((eq major-mode 'dired-mode)
  8654. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8655. (t (org-do-occur (match-string 1 s)))))
  8656. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8657. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8658. (goto-char (point-min))
  8659. (cond
  8660. ((let (case-fold-search)
  8661. (re-search-forward (format org-complex-heading-regexp-format
  8662. (regexp-quote s))
  8663. nil t))
  8664. ;; OK, found a match
  8665. (setq type 'dedicated)
  8666. (goto-char (match-beginning 0)))
  8667. ((and (not org-link-search-inhibit-query)
  8668. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8669. (y-or-n-p "No match - create this as a new heading? "))
  8670. (goto-char (point-max))
  8671. (or (bolp) (newline))
  8672. (insert "* " s "\n")
  8673. (beginning-of-line 0))
  8674. (t
  8675. (goto-char pos)
  8676. (error "No match"))))
  8677. (t
  8678. ;; A normal search string
  8679. (when (equal (string-to-char s) ?*)
  8680. ;; Anchor on headlines, post may include tags.
  8681. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8682. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8683. s (substring s 1)))
  8684. (remove-text-properties
  8685. 0 (length s)
  8686. '(face nil mouse-face nil keymap nil fontified nil) s)
  8687. ;; Make a series of regular expressions to find a match
  8688. (setq words (org-split-string s "[ \n\r\t]+")
  8689. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8690. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8691. "\\)" markers)
  8692. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8693. re2a (concat "[ \t\r\n]" re2a_)
  8694. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8695. re4 (concat "[^a-zA-Z_]" re4_)
  8696. re1 (concat pre re2 post)
  8697. re3 (concat pre (if pre re4_ re4) post)
  8698. re5 (concat pre ".*" re4)
  8699. re2 (concat pre re2)
  8700. re2a (concat pre (if pre re2a_ re2a))
  8701. re4 (concat pre (if pre re4_ re4))
  8702. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8703. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8704. re5 "\\)"
  8705. ))
  8706. (cond
  8707. ((eq type 'org-occur) (org-occur reall))
  8708. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8709. (t (goto-char (point-min))
  8710. (setq type 'fuzzy)
  8711. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8712. (org-search-not-self 1 re1 nil t)
  8713. (org-search-not-self 1 re2 nil t)
  8714. (org-search-not-self 1 re2a nil t)
  8715. (org-search-not-self 1 re3 nil t)
  8716. (org-search-not-self 1 re4 nil t)
  8717. (org-search-not-self 1 re5 nil t)
  8718. )
  8719. (goto-char (match-beginning 1))
  8720. (goto-char pos)
  8721. (error "No match"))))))
  8722. (and (org-mode-p) (org-show-context 'link-search))
  8723. type))
  8724. (defun org-search-not-self (group &rest args)
  8725. "Execute `re-search-forward', but only accept matches that do not
  8726. enclose the position of `org-open-link-marker'."
  8727. (let ((m org-open-link-marker))
  8728. (catch 'exit
  8729. (while (apply 're-search-forward args)
  8730. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8731. (goto-char (match-end group))
  8732. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8733. (> (match-beginning 0) (marker-position m))
  8734. (< (match-end 0) (marker-position m)))
  8735. (save-match-data
  8736. (or (not (org-in-regexp
  8737. org-bracket-link-analytic-regexp 1))
  8738. (not (match-end 4)) ; no description
  8739. (and (<= (match-beginning 4) (point))
  8740. (>= (match-end 4) (point))))))
  8741. (throw 'exit (point))))))))
  8742. (defun org-get-buffer-for-internal-link (buffer)
  8743. "Return a buffer to be used for displaying the link target of internal links."
  8744. (cond
  8745. ((not org-display-internal-link-with-indirect-buffer)
  8746. buffer)
  8747. ((string-match "(Clone)$" (buffer-name buffer))
  8748. (message "Buffer is already a clone, not making another one")
  8749. ;; we also do not modify visibility in this case
  8750. buffer)
  8751. (t ; make a new indirect buffer for displaying the link
  8752. (let* ((bn (buffer-name buffer))
  8753. (ibn (concat bn "(Clone)"))
  8754. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8755. (with-current-buffer ib (org-overview))
  8756. ib))))
  8757. (defun org-do-occur (regexp &optional cleanup)
  8758. "Call the Emacs command `occur'.
  8759. If CLEANUP is non-nil, remove the printout of the regular expression
  8760. in the *Occur* buffer. This is useful if the regex is long and not useful
  8761. to read."
  8762. (occur regexp)
  8763. (when cleanup
  8764. (let ((cwin (selected-window)) win beg end)
  8765. (when (setq win (get-buffer-window "*Occur*"))
  8766. (select-window win))
  8767. (goto-char (point-min))
  8768. (when (re-search-forward "match[a-z]+" nil t)
  8769. (setq beg (match-end 0))
  8770. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8771. (setq end (1- (match-beginning 0)))))
  8772. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8773. (goto-char (point-min))
  8774. (select-window cwin))))
  8775. ;;; The mark ring for links jumps
  8776. (defvar org-mark-ring nil
  8777. "Mark ring for positions before jumps in Org-mode.")
  8778. (defvar org-mark-ring-last-goto nil
  8779. "Last position in the mark ring used to go back.")
  8780. ;; Fill and close the ring
  8781. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8782. (loop for i from 1 to org-mark-ring-length do
  8783. (push (make-marker) org-mark-ring))
  8784. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8785. org-mark-ring)
  8786. (defun org-mark-ring-push (&optional pos buffer)
  8787. "Put the current position or POS into the mark ring and rotate it."
  8788. (interactive)
  8789. (setq pos (or pos (point)))
  8790. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8791. (move-marker (car org-mark-ring)
  8792. (or pos (point))
  8793. (or buffer (current-buffer)))
  8794. (message "%s"
  8795. (substitute-command-keys
  8796. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8797. (defun org-mark-ring-goto (&optional n)
  8798. "Jump to the previous position in the mark ring.
  8799. With prefix arg N, jump back that many stored positions. When
  8800. called several times in succession, walk through the entire ring.
  8801. Org-mode commands jumping to a different position in the current file,
  8802. or to another Org-mode file, automatically push the old position
  8803. onto the ring."
  8804. (interactive "p")
  8805. (let (p m)
  8806. (if (eq last-command this-command)
  8807. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8808. (setq p org-mark-ring))
  8809. (setq org-mark-ring-last-goto p)
  8810. (setq m (car p))
  8811. (switch-to-buffer (marker-buffer m))
  8812. (goto-char m)
  8813. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8814. (defun org-remove-angle-brackets (s)
  8815. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8816. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8817. s)
  8818. (defun org-add-angle-brackets (s)
  8819. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8820. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8821. s)
  8822. (defun org-remove-double-quotes (s)
  8823. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8824. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8825. s)
  8826. ;;; Following specific links
  8827. (defun org-follow-timestamp-link ()
  8828. (cond
  8829. ((org-at-date-range-p t)
  8830. (let ((org-agenda-start-on-weekday)
  8831. (t1 (match-string 1))
  8832. (t2 (match-string 2)))
  8833. (setq t1 (time-to-days (org-time-string-to-time t1))
  8834. t2 (time-to-days (org-time-string-to-time t2)))
  8835. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8836. ((org-at-timestamp-p t)
  8837. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8838. (substring (match-string 1) 0 10)))
  8839. 1))
  8840. (t (error "This should not happen"))))
  8841. ;;; Following file links
  8842. (defvar org-wait nil)
  8843. (defun org-open-file (path &optional in-emacs line search)
  8844. "Open the file at PATH.
  8845. First, this expands any special file name abbreviations. Then the
  8846. configuration variable `org-file-apps' is checked if it contains an
  8847. entry for this file type, and if yes, the corresponding command is launched.
  8848. If no application is found, Emacs simply visits the file.
  8849. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8850. With a double \\[universal-argument] \\[universal-argument] \
  8851. prefix arg, Org tries to avoid opening in Emacs
  8852. and to use an external application to visit the file.
  8853. Optional LINE specifies a line to go to, optional SEARCH a string
  8854. to search for. If LINE or SEARCH is given, the file will be
  8855. opened in Emacs, unless an entry from org-file-apps that makes
  8856. use of groups in a regexp matches.
  8857. If the file does not exist, an error is thrown."
  8858. (let* ((file (if (equal path "")
  8859. buffer-file-name
  8860. (substitute-in-file-name (expand-file-name path))))
  8861. (file-apps (append org-file-apps (org-default-apps)))
  8862. (apps (org-remove-if
  8863. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8864. (apps-dlink (org-remove-if-not
  8865. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8866. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8867. (dirp (if remp nil (file-directory-p file)))
  8868. (file (if (and dirp org-open-directory-means-index-dot-org)
  8869. (concat (file-name-as-directory file) "index.org")
  8870. file))
  8871. (a-m-a-p (assq 'auto-mode apps))
  8872. (dfile (downcase file))
  8873. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8874. (link (cond ((and (eq line nil)
  8875. (eq search nil))
  8876. file)
  8877. (line
  8878. (concat file "::" (number-to-string line)))
  8879. (search
  8880. (concat file "::" search))))
  8881. (dlink (downcase link))
  8882. (old-buffer (current-buffer))
  8883. (old-pos (point))
  8884. (old-mode major-mode)
  8885. ext cmd link-match-data)
  8886. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8887. (setq ext (match-string 1 dfile))
  8888. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8889. (setq ext (match-string 1 dfile))))
  8890. (cond
  8891. ((member in-emacs '((16) system))
  8892. (setq cmd (cdr (assoc 'system apps))))
  8893. (in-emacs (setq cmd 'emacs))
  8894. (t
  8895. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8896. (and dirp (cdr (assoc 'directory apps)))
  8897. ; first, try matching against apps-dlink
  8898. ; if we get a match here, store the match data for later
  8899. (let ((match (assoc-default dlink apps-dlink
  8900. 'string-match)))
  8901. (if match
  8902. (progn (setq link-match-data (match-data))
  8903. match)
  8904. (progn (setq in-emacs (or in-emacs line search))
  8905. nil))) ; if we have no match in apps-dlink,
  8906. ; always open the file in emacs if line or search
  8907. ; is given (for backwards compatibility)
  8908. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8909. 'string-match)
  8910. (cdr (assoc ext apps))
  8911. (cdr (assoc t apps))))))
  8912. (when (eq cmd 'system)
  8913. (setq cmd (cdr (assoc 'system apps))))
  8914. (when (eq cmd 'default)
  8915. (setq cmd (cdr (assoc t apps))))
  8916. (when (eq cmd 'mailcap)
  8917. (require 'mailcap)
  8918. (mailcap-parse-mailcaps)
  8919. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8920. (command (mailcap-mime-info mime-type)))
  8921. (if (stringp command)
  8922. (setq cmd command)
  8923. (setq cmd 'emacs))))
  8924. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8925. (not (file-exists-p file))
  8926. (not org-open-non-existing-files))
  8927. (error "No such file: %s" file))
  8928. (cond
  8929. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8930. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8931. (while (string-match "['\"]%s['\"]" cmd)
  8932. (setq cmd (replace-match "%s" t t cmd)))
  8933. (while (string-match "%s" cmd)
  8934. (setq cmd (replace-match
  8935. (save-match-data
  8936. (shell-quote-argument
  8937. (convert-standard-filename file)))
  8938. t t cmd)))
  8939. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8940. (save-match-data
  8941. (let ((match-index 1)
  8942. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8943. (set-match-data link-match-data)
  8944. (while (<= match-index number-of-groups)
  8945. (let ((regex (concat "%" (number-to-string match-index)))
  8946. (replace-with (match-string match-index dlink)))
  8947. (while (string-match regex cmd)
  8948. (setq cmd (replace-match replace-with t t cmd))))
  8949. (setq match-index (+ match-index 1)))))
  8950. (save-window-excursion
  8951. (start-process-shell-command cmd nil cmd)
  8952. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8953. ))
  8954. ((or (stringp cmd)
  8955. (eq cmd 'emacs))
  8956. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8957. (widen)
  8958. (if line (org-goto-line line)
  8959. (if search (org-link-search search))))
  8960. ((consp cmd)
  8961. (let ((file (convert-standard-filename file)))
  8962. (save-match-data
  8963. (set-match-data link-match-data)
  8964. (eval cmd))))
  8965. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8966. (and (org-mode-p) (eq old-mode 'org-mode)
  8967. (or (not (equal old-buffer (current-buffer)))
  8968. (not (equal old-pos (point))))
  8969. (org-mark-ring-push old-pos old-buffer))))
  8970. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8971. "This function returns non-nil if `entry' uses a regular
  8972. expression which should be matched against the whole link by
  8973. org-open-file.
  8974. It assumes that is the case when the entry uses a regular
  8975. expression which has at least one grouping construct and the
  8976. action is either a lisp form or a command string containing
  8977. '%1', i.e. using at least one subexpression match as a
  8978. parameter."
  8979. (let ((selector (car entry))
  8980. (action (cdr entry)))
  8981. (if (stringp selector)
  8982. (and (> (regexp-opt-depth selector) 0)
  8983. (or (and (stringp action)
  8984. (string-match "%[0-9]" action))
  8985. (consp action)))
  8986. nil)))
  8987. (defun org-default-apps ()
  8988. "Return the default applications for this operating system."
  8989. (cond
  8990. ((eq system-type 'darwin)
  8991. org-file-apps-defaults-macosx)
  8992. ((eq system-type 'windows-nt)
  8993. org-file-apps-defaults-windowsnt)
  8994. (t org-file-apps-defaults-gnu)))
  8995. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8996. "Convert extensions to regular expressions in the cars of LIST.
  8997. Also, weed out any non-string entries, because the return value is used
  8998. only for regexp matching.
  8999. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9000. point to the symbol `emacs', indicating that the file should
  9001. be opened in Emacs."
  9002. (append
  9003. (delq nil
  9004. (mapcar (lambda (x)
  9005. (if (not (stringp (car x)))
  9006. nil
  9007. (if (string-match "\\W" (car x))
  9008. x
  9009. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9010. list))
  9011. (if add-auto-mode
  9012. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9013. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9014. (defun org-file-remote-p (file)
  9015. "Test whether FILE specifies a location on a remote system.
  9016. Return non-nil if the location is indeed remote.
  9017. For example, the filename \"/user@host:/foo\" specifies a location
  9018. on the system \"/user@host:\"."
  9019. (cond ((fboundp 'file-remote-p)
  9020. (file-remote-p file))
  9021. ((fboundp 'tramp-handle-file-remote-p)
  9022. (tramp-handle-file-remote-p file))
  9023. ((and (boundp 'ange-ftp-name-format)
  9024. (string-match (car ange-ftp-name-format) file))
  9025. t)
  9026. (t nil)))
  9027. ;;;; Refiling
  9028. (defun org-get-org-file ()
  9029. "Read a filename, with default directory `org-directory'."
  9030. (let ((default (or org-default-notes-file remember-data-file)))
  9031. (read-file-name (format "File name [%s]: " default)
  9032. (file-name-as-directory org-directory)
  9033. default)))
  9034. (defun org-notes-order-reversed-p ()
  9035. "Check if the current file should receive notes in reversed order."
  9036. (cond
  9037. ((not org-reverse-note-order) nil)
  9038. ((eq t org-reverse-note-order) t)
  9039. ((not (listp org-reverse-note-order)) nil)
  9040. (t (catch 'exit
  9041. (let ((all org-reverse-note-order)
  9042. entry)
  9043. (while (setq entry (pop all))
  9044. (if (string-match (car entry) buffer-file-name)
  9045. (throw 'exit (cdr entry))))
  9046. nil)))))
  9047. (defvar org-refile-target-table nil
  9048. "The list of refile targets, created by `org-refile'.")
  9049. (defvar org-agenda-new-buffers nil
  9050. "Buffers created to visit agenda files.")
  9051. (defvar org-refile-cache nil
  9052. "Cache for refile targets.")
  9053. (defvar org-refile-markers nil
  9054. "All the markers used for caching refile locations.")
  9055. (defun org-refile-marker (pos)
  9056. "Get a new refile marker, but only if caching is in use."
  9057. (if (not org-refile-use-cache)
  9058. pos
  9059. (let ((m (make-marker)))
  9060. (move-marker m pos)
  9061. (push m org-refile-markers)
  9062. m)))
  9063. (defun org-refile-cache-clear ()
  9064. "Clear the refile cache and disable all the markers."
  9065. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9066. (setq org-refile-markers nil)
  9067. (setq org-refile-cache nil)
  9068. (message "Refile cache has been cleared"))
  9069. (defun org-refile-cache-check-set (set)
  9070. "Check if all the markers in the cache still have live buffers."
  9071. (let (marker)
  9072. (catch 'exit
  9073. (while (and set (setq marker (nth 3 (pop set))))
  9074. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9075. (when (and marker (null (marker-buffer marker)))
  9076. (message "not found") (sit-for 3)
  9077. (throw 'exit nil)))
  9078. t)))
  9079. (defun org-refile-cache-put (set &rest identifiers)
  9080. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9081. (let* ((key (sha1 (prin1-to-string identifiers)))
  9082. (entry (assoc key org-refile-cache)))
  9083. (if entry
  9084. (setcdr entry set)
  9085. (push (cons key set) org-refile-cache))))
  9086. (defun org-refile-cache-get (&rest identifiers)
  9087. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9088. (cond
  9089. ((not org-refile-cache) nil)
  9090. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9091. (t
  9092. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9093. org-refile-cache))))
  9094. (and set (org-refile-cache-check-set set) set)))))
  9095. (defun org-refile-get-targets (&optional default-buffer)
  9096. "Produce a table with refile targets."
  9097. (let ((case-fold-search nil)
  9098. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9099. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9100. targets tgs txt re files f desc descre fast-path-p level pos0)
  9101. (message "Getting targets...")
  9102. (with-current-buffer (or default-buffer (current-buffer))
  9103. (while (setq entry (pop entries))
  9104. (setq files (car entry) desc (cdr entry))
  9105. (setq fast-path-p nil)
  9106. (cond
  9107. ((null files) (setq files (list (current-buffer))))
  9108. ((eq files 'org-agenda-files)
  9109. (setq files (org-agenda-files 'unrestricted)))
  9110. ((and (symbolp files) (fboundp files))
  9111. (setq files (funcall files)))
  9112. ((and (symbolp files) (boundp files))
  9113. (setq files (symbol-value files))))
  9114. (if (stringp files) (setq files (list files)))
  9115. (cond
  9116. ((eq (car desc) :tag)
  9117. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9118. ((eq (car desc) :todo)
  9119. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9120. ((eq (car desc) :regexp)
  9121. (setq descre (cdr desc)))
  9122. ((eq (car desc) :level)
  9123. (setq descre (concat "^\\*\\{" (number-to-string
  9124. (if org-odd-levels-only
  9125. (1- (* 2 (cdr desc)))
  9126. (cdr desc)))
  9127. "\\}[ \t]")))
  9128. ((eq (car desc) :maxlevel)
  9129. (setq fast-path-p t)
  9130. (setq descre (concat "^\\*\\{1," (number-to-string
  9131. (if org-odd-levels-only
  9132. (1- (* 2 (cdr desc)))
  9133. (cdr desc)))
  9134. "\\}[ \t]")))
  9135. (t (error "Bad refiling target description %s" desc)))
  9136. (while (setq f (pop files))
  9137. (with-current-buffer
  9138. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9139. (or
  9140. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9141. (progn
  9142. (if (bufferp f) (setq f (buffer-file-name
  9143. (buffer-base-buffer f))))
  9144. (setq f (and f (expand-file-name f)))
  9145. (if (eq org-refile-use-outline-path 'file)
  9146. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9147. (save-excursion
  9148. (save-restriction
  9149. (widen)
  9150. (goto-char (point-min))
  9151. (while (re-search-forward descre nil t)
  9152. (goto-char (setq pos0 (point-at-bol)))
  9153. (catch 'next
  9154. (when org-refile-target-verify-function
  9155. (save-match-data
  9156. (or (funcall org-refile-target-verify-function)
  9157. (throw 'next t))))
  9158. (when (looking-at org-complex-heading-regexp)
  9159. (setq level (org-reduced-level
  9160. (- (match-end 1) (match-beginning 1)))
  9161. txt (org-link-display-format (match-string 4))
  9162. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9163. re (format org-complex-heading-regexp-format
  9164. (regexp-quote (match-string 4))))
  9165. (when org-refile-use-outline-path
  9166. (setq txt (mapconcat
  9167. 'org-protect-slash
  9168. (append
  9169. (if (eq org-refile-use-outline-path
  9170. 'file)
  9171. (list (file-name-nondirectory
  9172. (buffer-file-name
  9173. (buffer-base-buffer))))
  9174. (if (eq org-refile-use-outline-path
  9175. 'full-file-path)
  9176. (list (buffer-file-name
  9177. (buffer-base-buffer)))))
  9178. (org-get-outline-path fast-path-p
  9179. level txt)
  9180. (list txt))
  9181. "/")))
  9182. (push (list txt f re (org-refile-marker (point)))
  9183. tgs)))
  9184. (when (= (point) pos0)
  9185. ;; verification function has not moved point
  9186. (goto-char (point-at-eol))))))))
  9187. (when org-refile-use-cache
  9188. (org-refile-cache-put tgs (buffer-file-name) descre))
  9189. (setq targets (append tgs targets))
  9190. ))))
  9191. (message "Getting targets...done")
  9192. (nreverse targets)))
  9193. (defun org-protect-slash (s)
  9194. (while (string-match "/" s)
  9195. (setq s (replace-match "\\" t t s)))
  9196. s)
  9197. (defvar org-olpa (make-vector 20 nil))
  9198. (defun org-get-outline-path (&optional fastp level heading)
  9199. "Return the outline path to the current entry, as a list.
  9200. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9201. routine which makes outline path derivations for an entire file,
  9202. avoiding backtracing. Refile target collection makes use of that."
  9203. (if fastp
  9204. (progn
  9205. (if (> level 19)
  9206. (error "Outline path failure, more than 19 levels"))
  9207. (loop for i from level upto 19 do
  9208. (aset org-olpa i nil))
  9209. (prog1
  9210. (delq nil (append org-olpa nil))
  9211. (aset org-olpa level heading)))
  9212. (let (rtn case-fold-search)
  9213. (save-excursion
  9214. (save-restriction
  9215. (widen)
  9216. (while (org-up-heading-safe)
  9217. (when (looking-at org-complex-heading-regexp)
  9218. (push (org-match-string-no-properties 4) rtn)))
  9219. rtn)))))
  9220. (defun org-format-outline-path (path &optional width prefix)
  9221. "Format the outline path PATH for display.
  9222. Width is the maximum number of characters that is available.
  9223. Prefix is a prefix to be included in the returned string,
  9224. such as the file name."
  9225. (setq width (or width 79))
  9226. (if prefix (setq width (- width (length prefix))))
  9227. (if (not path)
  9228. (or prefix "")
  9229. (let* ((nsteps (length path))
  9230. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9231. (maxwidth (if (<= total-width width)
  9232. 10000 ;; everything fits
  9233. ;; we need to shorten the level headings
  9234. (/ (- width nsteps) nsteps)))
  9235. (org-odd-levels-only nil)
  9236. (n 0)
  9237. (total (1+ (length prefix))))
  9238. (setq maxwidth (max maxwidth 10))
  9239. (concat prefix
  9240. (mapconcat
  9241. (lambda (h)
  9242. (setq n (1+ n))
  9243. (if (and (= n nsteps) (< maxwidth 10000))
  9244. (setq maxwidth (- total-width total)))
  9245. (if (< (length h) maxwidth)
  9246. (progn (setq total (+ total (length h) 1)) h)
  9247. (setq h (substring h 0 (- maxwidth 2))
  9248. total (+ total maxwidth 1))
  9249. (if (string-match "[ \t]+\\'" h)
  9250. (setq h (substring h 0 (match-beginning 0))))
  9251. (setq h (concat h "..")))
  9252. (org-add-props h nil 'face
  9253. (nth (% (1- n) org-n-level-faces)
  9254. org-level-faces))
  9255. h)
  9256. path "/")))))
  9257. (defun org-display-outline-path (&optional file current)
  9258. "Display the current outline path in the echo area."
  9259. (interactive "P")
  9260. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9261. (case-fold-search nil)
  9262. (path (and (org-mode-p) (org-get-outline-path))))
  9263. (if current (setq path (append path
  9264. (save-excursion
  9265. (org-back-to-heading t)
  9266. (if (looking-at org-complex-heading-regexp)
  9267. (list (match-string 4)))))))
  9268. (message "%s"
  9269. (org-format-outline-path
  9270. path
  9271. (1- (frame-width))
  9272. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9273. (defvar org-refile-history nil
  9274. "History for refiling operations.")
  9275. (defvar org-after-refile-insert-hook nil
  9276. "Hook run after `org-refile' has inserted its stuff at the new location.
  9277. Note that this is still *before* the stuff will be removed from
  9278. the *old* location.")
  9279. (defvar org-capture-last-stored-marker)
  9280. (defun org-refile (&optional goto default-buffer rfloc)
  9281. "Move the entry at point to another heading.
  9282. The list of target headings is compiled using the information in
  9283. `org-refile-targets', which see. This list is created before each use
  9284. and will therefore always be up-to-date.
  9285. At the target location, the entry is filed as a subitem of the target heading.
  9286. Depending on `org-reverse-note-order', the new subitem will either be the
  9287. first or the last subitem.
  9288. If there is an active region, all entries in that region will be moved.
  9289. However, the region must fulfill the requirement that the first heading
  9290. is the first one sets the top-level of the moved text - at most siblings
  9291. below it are allowed.
  9292. With prefix arg GOTO, the command will only visit the target location,
  9293. not actually move anything.
  9294. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9295. go to the location where the last refiling
  9296. operation has put the subtree.
  9297. With a prefix argument of `2', refile to the running clock.
  9298. RFLOC can be a refile location obtained in a different way.
  9299. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9300. If you are using target caching (see `org-refile-use-cache'),
  9301. You have to clear the target cache in order to find new targets.
  9302. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9303. (interactive "P")
  9304. (if (member goto '(0 (64)))
  9305. (org-refile-cache-clear)
  9306. (let* ((cbuf (current-buffer))
  9307. (regionp (org-region-active-p))
  9308. (region-start (and regionp (region-beginning)))
  9309. (region-end (and regionp (region-end)))
  9310. (region-length (and regionp (- region-end region-start)))
  9311. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9312. pos it nbuf file re level reversed)
  9313. (setq last-command nil)
  9314. (when regionp
  9315. (goto-char region-start)
  9316. (or (bolp) (goto-char (point-at-bol)))
  9317. (setq region-start (point))
  9318. (unless (org-kill-is-subtree-p
  9319. (buffer-substring region-start region-end))
  9320. (error "The region is not a (sequence of) subtree(s)")))
  9321. (if (equal goto '(16))
  9322. (org-refile-goto-last-stored)
  9323. (when (or
  9324. (and (equal goto 2)
  9325. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9326. (prog1
  9327. (setq it (list (or org-clock-heading "running clock")
  9328. (buffer-file-name
  9329. (marker-buffer org-clock-hd-marker))
  9330. ""
  9331. (marker-position org-clock-hd-marker)))
  9332. (setq goto nil)))
  9333. (setq it (or rfloc
  9334. (save-excursion
  9335. (org-refile-get-location
  9336. (if goto "Goto" "Refile to") default-buffer
  9337. org-refile-allow-creating-parent-nodes)))))
  9338. (setq file (nth 1 it)
  9339. re (nth 2 it)
  9340. pos (nth 3 it))
  9341. (if (and (not goto)
  9342. pos
  9343. (equal (buffer-file-name) file)
  9344. (if regionp
  9345. (and (>= pos region-start)
  9346. (<= pos region-end))
  9347. (and (>= pos (point))
  9348. (< pos (save-excursion
  9349. (org-end-of-subtree t t))))))
  9350. (error "Cannot refile to position inside the tree or region"))
  9351. (setq nbuf (or (find-buffer-visiting file)
  9352. (find-file-noselect file)))
  9353. (if goto
  9354. (progn
  9355. (switch-to-buffer nbuf)
  9356. (goto-char pos)
  9357. (org-show-context 'org-goto))
  9358. (if regionp
  9359. (progn
  9360. (org-kill-new (buffer-substring region-start region-end))
  9361. (org-save-markers-in-region region-start region-end))
  9362. (org-copy-subtree 1 nil t))
  9363. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9364. (find-file-noselect file)))
  9365. (setq reversed (org-notes-order-reversed-p))
  9366. (save-excursion
  9367. (save-restriction
  9368. (widen)
  9369. (if pos
  9370. (progn
  9371. (goto-char pos)
  9372. (looking-at outline-regexp)
  9373. (setq level (org-get-valid-level (funcall outline-level) 1))
  9374. (goto-char
  9375. (if reversed
  9376. (or (outline-next-heading) (point-max))
  9377. (or (save-excursion (org-get-next-sibling))
  9378. (org-end-of-subtree t t)
  9379. (point-max)))))
  9380. (setq level 1)
  9381. (if (not reversed)
  9382. (goto-char (point-max))
  9383. (goto-char (point-min))
  9384. (or (outline-next-heading) (goto-char (point-max)))))
  9385. (if (not (bolp)) (newline))
  9386. (org-paste-subtree level)
  9387. (when org-log-refile
  9388. (org-add-log-setup 'refile nil nil 'findpos
  9389. org-log-refile)
  9390. (unless (eq org-log-refile 'note)
  9391. (save-excursion (org-add-log-note))))
  9392. (and org-auto-align-tags (org-set-tags nil t))
  9393. (bookmark-set "org-refile-last-stored")
  9394. ;; If we are refiling for capture, make sure that the
  9395. ;; last-capture pointers point here
  9396. (when (org-bound-and-true-p org-refile-for-capture)
  9397. (bookmark-set "org-capture-last-stored-marker")
  9398. (move-marker org-capture-last-stored-marker (point)))
  9399. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9400. (run-hooks 'org-after-refile-insert-hook))))
  9401. (if regionp
  9402. (delete-region (point) (+ (point) region-length))
  9403. (org-cut-subtree))
  9404. (when (featurep 'org-inlinetask)
  9405. (org-inlinetask-remove-END-maybe))
  9406. (setq org-markers-to-move nil)
  9407. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9408. (defun org-refile-goto-last-stored ()
  9409. "Go to the location where the last refile was stored."
  9410. (interactive)
  9411. (bookmark-jump "org-refile-last-stored")
  9412. (message "This is the location of the last refile"))
  9413. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9414. "Prompt the user for a refile location, using PROMPT.
  9415. PROMPT should not be suffixed with a colon and a space, because
  9416. this function appends the default value from
  9417. `org-refile-history' automatically, if that is not empty."
  9418. (let ((org-refile-targets org-refile-targets)
  9419. (org-refile-use-outline-path org-refile-use-outline-path))
  9420. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  9421. (unless org-refile-target-table
  9422. (error "No refile targets"))
  9423. (let* ((prompt (concat prompt
  9424. (and (car org-refile-history)
  9425. (concat " (default " (car org-refile-history) ")"))
  9426. ": "))
  9427. (cbuf (current-buffer))
  9428. (partial-completion-mode nil)
  9429. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9430. (cfunc (if (and org-refile-use-outline-path
  9431. org-outline-path-complete-in-steps)
  9432. 'org-olpath-completing-read
  9433. 'org-icompleting-read))
  9434. (extra (if org-refile-use-outline-path "/" ""))
  9435. (filename (and cfn (expand-file-name cfn)))
  9436. (tbl (mapcar
  9437. (lambda (x)
  9438. (if (and (not (member org-refile-use-outline-path
  9439. '(file full-file-path)))
  9440. (not (equal filename (nth 1 x))))
  9441. (cons (concat (car x) extra " ("
  9442. (file-name-nondirectory (nth 1 x)) ")")
  9443. (cdr x))
  9444. (cons (concat (car x) extra) (cdr x))))
  9445. org-refile-target-table))
  9446. (completion-ignore-case t)
  9447. pa answ parent-target child parent old-hist)
  9448. (setq old-hist org-refile-history)
  9449. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9450. nil 'org-refile-history (car org-refile-history)))
  9451. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9452. (org-refile-check-position pa)
  9453. (if pa
  9454. (progn
  9455. (when (or (not org-refile-history)
  9456. (not (eq old-hist org-refile-history))
  9457. (not (equal (car pa) (car org-refile-history))))
  9458. (setq org-refile-history
  9459. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9460. org-refile-history
  9461. (cdr org-refile-history))))
  9462. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9463. (pop org-refile-history)))
  9464. pa)
  9465. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9466. (progn
  9467. (setq parent (match-string 1 answ)
  9468. child (match-string 2 answ))
  9469. (setq parent-target (or (assoc parent tbl)
  9470. (assoc (concat parent "/") tbl)))
  9471. (when (and parent-target
  9472. (or (eq new-nodes t)
  9473. (and (eq new-nodes 'confirm)
  9474. (y-or-n-p (format "Create new node \"%s\"? "
  9475. child)))))
  9476. (org-refile-new-child parent-target child)))
  9477. (error "Invalid target location")))))
  9478. (defun org-refile-check-position (refile-pointer)
  9479. "Check if the refile pointer matches the readline to which it points."
  9480. (let* ((file (nth 1 refile-pointer))
  9481. (re (nth 2 refile-pointer))
  9482. (pos (nth 3 refile-pointer))
  9483. buffer)
  9484. (when (org-string-nw-p re)
  9485. (setq buffer (if (markerp pos)
  9486. (marker-buffer pos)
  9487. (or (find-buffer-visiting file)
  9488. (find-file-noselect file))))
  9489. (with-current-buffer buffer
  9490. (save-excursion
  9491. (save-restriction
  9492. (widen)
  9493. (goto-char pos)
  9494. (beginning-of-line 1)
  9495. (unless (org-looking-at-p re)
  9496. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9497. (defun org-refile-new-child (parent-target child)
  9498. "Use refile target PARENT-TARGET to add new CHILD below it."
  9499. (unless parent-target
  9500. (error "Cannot find parent for new node"))
  9501. (let ((file (nth 1 parent-target))
  9502. (pos (nth 3 parent-target))
  9503. level)
  9504. (with-current-buffer (or (find-buffer-visiting file)
  9505. (find-file-noselect file))
  9506. (save-excursion
  9507. (save-restriction
  9508. (widen)
  9509. (if pos
  9510. (goto-char pos)
  9511. (goto-char (point-max))
  9512. (if (not (bolp)) (newline)))
  9513. (when (looking-at outline-regexp)
  9514. (setq level (funcall outline-level))
  9515. (org-end-of-subtree t t))
  9516. (org-back-over-empty-lines)
  9517. (insert "\n" (make-string
  9518. (if pos (org-get-valid-level level 1) 1) ?*)
  9519. " " child "\n")
  9520. (beginning-of-line 0)
  9521. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9522. (defun org-olpath-completing-read (prompt collection &rest args)
  9523. "Read an outline path like a file name."
  9524. (let ((thetable collection)
  9525. (org-completion-use-ido nil) ; does not work with ido.
  9526. (org-completion-use-iswitchb nil)) ; or iswitchb
  9527. (apply
  9528. 'org-icompleting-read prompt
  9529. (lambda (string predicate &optional flag)
  9530. (let (rtn r f (l (length string)))
  9531. (cond
  9532. ((eq flag nil)
  9533. ;; try completion
  9534. (try-completion string thetable))
  9535. ((eq flag t)
  9536. ;; all-completions
  9537. (setq rtn (all-completions string thetable predicate))
  9538. (mapcar
  9539. (lambda (x)
  9540. (setq r (substring x l))
  9541. (if (string-match " ([^)]*)$" x)
  9542. (setq f (match-string 0 x))
  9543. (setq f ""))
  9544. (if (string-match "/" r)
  9545. (concat string (substring r 0 (match-end 0)) f)
  9546. x))
  9547. rtn))
  9548. ((eq flag 'lambda)
  9549. ;; exact match?
  9550. (assoc string thetable)))
  9551. ))
  9552. args)))
  9553. ;;;; Dynamic blocks
  9554. (defun org-find-dblock (name)
  9555. "Find the first dynamic block with name NAME in the buffer.
  9556. If not found, stay at current position and return nil."
  9557. (let (pos)
  9558. (save-excursion
  9559. (goto-char (point-min))
  9560. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9561. nil t)
  9562. (match-beginning 0))))
  9563. (if pos (goto-char pos))
  9564. pos))
  9565. (defconst org-dblock-start-re
  9566. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9567. "Matches the start line of a dynamic block, with parameters.")
  9568. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9569. "Matches the end of a dynamic block.")
  9570. (defun org-create-dblock (plist)
  9571. "Create a dynamic block section, with parameters taken from PLIST.
  9572. PLIST must contain a :name entry which is used as name of the block."
  9573. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9574. (end-of-line 1)
  9575. (newline))
  9576. (let ((col (current-column))
  9577. (name (plist-get plist :name)))
  9578. (insert "#+BEGIN: " name)
  9579. (while plist
  9580. (if (eq (car plist) :name)
  9581. (setq plist (cddr plist))
  9582. (insert " " (prin1-to-string (pop plist)))))
  9583. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9584. (beginning-of-line -2)))
  9585. (defun org-prepare-dblock ()
  9586. "Prepare dynamic block for refresh.
  9587. This empties the block, puts the cursor at the insert position and returns
  9588. the property list including an extra property :name with the block name."
  9589. (unless (looking-at org-dblock-start-re)
  9590. (error "Not at a dynamic block"))
  9591. (let* ((begdel (1+ (match-end 0)))
  9592. (name (org-no-properties (match-string 1)))
  9593. (params (append (list :name name)
  9594. (read (concat "(" (match-string 3) ")")))))
  9595. (save-excursion
  9596. (beginning-of-line 1)
  9597. (skip-chars-forward " \t")
  9598. (setq params (plist-put params :indentation-column (current-column))))
  9599. (unless (re-search-forward org-dblock-end-re nil t)
  9600. (error "Dynamic block not terminated"))
  9601. (setq params
  9602. (append params
  9603. (list :content (buffer-substring
  9604. begdel (match-beginning 0)))))
  9605. (delete-region begdel (match-beginning 0))
  9606. (goto-char begdel)
  9607. (open-line 1)
  9608. params))
  9609. (defun org-map-dblocks (&optional command)
  9610. "Apply COMMAND to all dynamic blocks in the current buffer.
  9611. If COMMAND is not given, use `org-update-dblock'."
  9612. (let ((cmd (or command 'org-update-dblock)))
  9613. (save-excursion
  9614. (goto-char (point-min))
  9615. (while (re-search-forward org-dblock-start-re nil t)
  9616. (goto-char (match-beginning 0))
  9617. (save-excursion
  9618. (condition-case nil
  9619. (funcall cmd)
  9620. (error (message "Error during update of dynamic block"))))
  9621. (unless (re-search-forward org-dblock-end-re nil t)
  9622. (error "Dynamic block not terminated"))))))
  9623. (defun org-dblock-update (&optional arg)
  9624. "User command for updating dynamic blocks.
  9625. Update the dynamic block at point. With prefix ARG, update all dynamic
  9626. blocks in the buffer."
  9627. (interactive "P")
  9628. (if arg
  9629. (org-update-all-dblocks)
  9630. (or (looking-at org-dblock-start-re)
  9631. (org-beginning-of-dblock))
  9632. (org-update-dblock)))
  9633. (defun org-update-dblock ()
  9634. "Update the dynamic block at point.
  9635. This means to empty the block, parse for parameters and then call
  9636. the correct writing function."
  9637. (interactive)
  9638. (save-window-excursion
  9639. (let* ((pos (point))
  9640. (line (org-current-line))
  9641. (params (org-prepare-dblock))
  9642. (name (plist-get params :name))
  9643. (indent (plist-get params :indentation-column))
  9644. (cmd (intern (concat "org-dblock-write:" name))))
  9645. (message "Updating dynamic block `%s' at line %d..." name line)
  9646. (funcall cmd params)
  9647. (message "Updating dynamic block `%s' at line %d...done" name line)
  9648. (goto-char pos)
  9649. (when (and indent (> indent 0))
  9650. (setq indent (make-string indent ?\ ))
  9651. (save-excursion
  9652. (org-beginning-of-dblock)
  9653. (forward-line 1)
  9654. (while (not (looking-at org-dblock-end-re))
  9655. (insert indent)
  9656. (beginning-of-line 2))
  9657. (when (looking-at org-dblock-end-re)
  9658. (and (looking-at "[ \t]+")
  9659. (replace-match ""))
  9660. (insert indent)))))))
  9661. (defun org-beginning-of-dblock ()
  9662. "Find the beginning of the dynamic block at point.
  9663. Error if there is no such block at point."
  9664. (let ((pos (point))
  9665. beg)
  9666. (end-of-line 1)
  9667. (if (and (re-search-backward org-dblock-start-re nil t)
  9668. (setq beg (match-beginning 0))
  9669. (re-search-forward org-dblock-end-re nil t)
  9670. (> (match-end 0) pos))
  9671. (goto-char beg)
  9672. (goto-char pos)
  9673. (error "Not in a dynamic block"))))
  9674. (defun org-update-all-dblocks ()
  9675. "Update all dynamic blocks in the buffer.
  9676. This function can be used in a hook."
  9677. (interactive)
  9678. (when (org-mode-p)
  9679. (org-map-dblocks 'org-update-dblock)))
  9680. ;;;; Completion
  9681. (defconst org-additional-option-like-keywords
  9682. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9683. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9684. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9685. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9686. "BEGIN:" "END:"
  9687. "ORGTBL" "TBLFM:" "TBLNAME:"
  9688. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9689. "BEGIN_QUOTE" "END_QUOTE"
  9690. "BEGIN_VERSE" "END_VERSE"
  9691. "BEGIN_CENTER" "END_CENTER"
  9692. "BEGIN_SRC" "END_SRC"
  9693. "BEGIN_RESULT" "END_RESULT"
  9694. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9695. "RESULTS:"
  9696. "HEADER:" "HEADERS:"
  9697. "BABEL:"
  9698. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9699. "CAPTION:" "LABEL:"
  9700. "SETUPFILE:"
  9701. "INCLUDE:"
  9702. "BIND:"
  9703. "MACRO:"))
  9704. (defcustom org-structure-template-alist
  9705. '(
  9706. ("s" "#+begin_src ?\n\n#+end_src"
  9707. "<src lang=\"?\">\n\n</src>")
  9708. ("e" "#+begin_example\n?\n#+end_example"
  9709. "<example>\n?\n</example>")
  9710. ("q" "#+begin_quote\n?\n#+end_quote"
  9711. "<quote>\n?\n</quote>")
  9712. ("v" "#+begin_verse\n?\n#+end_verse"
  9713. "<verse>\n?\n/verse>")
  9714. ("c" "#+begin_center\n?\n#+end_center"
  9715. "<center>\n?\n/center>")
  9716. ("l" "#+begin_latex\n?\n#+end_latex"
  9717. "<literal style=\"latex\">\n?\n</literal>")
  9718. ("L" "#+latex: "
  9719. "<literal style=\"latex\">?</literal>")
  9720. ("h" "#+begin_html\n?\n#+end_html"
  9721. "<literal style=\"html\">\n?\n</literal>")
  9722. ("H" "#+html: "
  9723. "<literal style=\"html\">?</literal>")
  9724. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9725. ("A" "#+ascii: ")
  9726. ("i" "#+index: ?"
  9727. "#+index: ?")
  9728. ("I" "#+include %file ?"
  9729. "<include file=%file markup=\"?\">")
  9730. )
  9731. "Structure completion elements.
  9732. This is a list of abbreviation keys and values. The value gets inserted
  9733. if you type `<' followed by the key and then press the completion key,
  9734. usually `M-TAB'. %file will be replaced by a file name after prompting
  9735. for the file using completion. The cursor will be placed at the position
  9736. of the `?` in the template.
  9737. There are two templates for each key, the first uses the original Org syntax,
  9738. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9739. the default when the /org-mtags.el/ module has been loaded. See also the
  9740. variable `org-mtags-prefer-muse-templates'.
  9741. This is an experimental feature, it is undecided if it is going to stay in."
  9742. :group 'org-completion
  9743. :type '(repeat
  9744. (string :tag "Key")
  9745. (string :tag "Template")
  9746. (string :tag "Muse Template")))
  9747. (defun org-try-structure-completion ()
  9748. "Try to complete a structure template before point.
  9749. This looks for strings like \"<e\" on an otherwise empty line and
  9750. expands them."
  9751. (let ((l (buffer-substring (point-at-bol) (point)))
  9752. a)
  9753. (when (and (looking-at "[ \t]*$")
  9754. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  9755. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9756. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9757. (match-beginning 1)) a)
  9758. t)))
  9759. (defun org-complete-expand-structure-template (start cell)
  9760. "Expand a structure template."
  9761. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9762. (rpl (nth (if musep 2 1) cell))
  9763. (ind ""))
  9764. (delete-region start (point))
  9765. (when (string-match "\\`#\\+" rpl)
  9766. (cond
  9767. ((bolp))
  9768. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9769. (setq ind (buffer-substring (point-at-bol) (point))))
  9770. (t (newline))))
  9771. (setq start (point))
  9772. (if (string-match "%file" rpl)
  9773. (setq rpl (replace-match
  9774. (concat
  9775. "\""
  9776. (save-match-data
  9777. (abbreviate-file-name (read-file-name "Include file: ")))
  9778. "\"")
  9779. t t rpl)))
  9780. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9781. (concat "\n" ind)))
  9782. (insert rpl)
  9783. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9784. ;;;; TODO, DEADLINE, Comments
  9785. (defun org-toggle-comment ()
  9786. "Change the COMMENT state of an entry."
  9787. (interactive)
  9788. (save-excursion
  9789. (org-back-to-heading)
  9790. (let (case-fold-search)
  9791. (if (looking-at (concat outline-regexp
  9792. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9793. (replace-match "" t t nil 1)
  9794. (if (looking-at outline-regexp)
  9795. (progn
  9796. (goto-char (match-end 0))
  9797. (insert org-comment-string " ")))))))
  9798. (defvar org-last-todo-state-is-todo nil
  9799. "This is non-nil when the last TODO state change led to a TODO state.
  9800. If the last change removed the TODO tag or switched to DONE, then
  9801. this is nil.")
  9802. (defvar org-setting-tags nil) ; dynamically skipped
  9803. (defvar org-todo-setup-filter-hook nil
  9804. "Hook for functions that pre-filter todo specs.
  9805. Each function takes a todo spec and returns either nil or the spec
  9806. transformed into canonical form." )
  9807. (defvar org-todo-get-default-hook nil
  9808. "Hook for functions that get a default item for todo.
  9809. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9810. nil or a string to be used for the todo mark." )
  9811. (defvar org-agenda-headline-snapshot-before-repeat)
  9812. (defun org-todo (&optional arg)
  9813. "Change the TODO state of an item.
  9814. The state of an item is given by a keyword at the start of the heading,
  9815. like
  9816. *** TODO Write paper
  9817. *** DONE Call mom
  9818. The different keywords are specified in the variable `org-todo-keywords'.
  9819. By default the available states are \"TODO\" and \"DONE\".
  9820. So for this example: when the item starts with TODO, it is changed to DONE.
  9821. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9822. DONE are present, add TODO at the beginning of the heading.
  9823. With \\[universal-argument] prefix arg, use completion to determine the new \
  9824. state.
  9825. With numeric prefix arg, switch to that state.
  9826. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9827. keywords (nextset).
  9828. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9829. For calling through lisp, arg is also interpreted in the following way:
  9830. 'none -> empty state
  9831. \"\"(empty string) -> switch to empty state
  9832. 'done -> switch to DONE
  9833. 'nextset -> switch to the next set of keywords
  9834. 'previousset -> switch to the previous set of keywords
  9835. \"WAITING\" -> switch to the specified keyword, but only if it
  9836. really is a member of `org-todo-keywords'."
  9837. (interactive "P")
  9838. (if (equal arg '(16)) (setq arg 'nextset))
  9839. (let ((org-blocker-hook org-blocker-hook)
  9840. (case-fold-search nil))
  9841. (when (equal arg '(64))
  9842. (setq arg nil org-blocker-hook nil))
  9843. (when (and org-blocker-hook
  9844. (or org-inhibit-blocking
  9845. (org-entry-get nil "NOBLOCKING")))
  9846. (setq org-blocker-hook nil))
  9847. (save-excursion
  9848. (catch 'exit
  9849. (org-back-to-heading t)
  9850. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9851. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9852. (looking-at " *"))
  9853. (let* ((match-data (match-data))
  9854. (startpos (point-at-bol))
  9855. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9856. (org-log-done org-log-done)
  9857. (org-log-repeat org-log-repeat)
  9858. (org-todo-log-states org-todo-log-states)
  9859. (this (match-string 1))
  9860. (hl-pos (match-beginning 0))
  9861. (head (org-get-todo-sequence-head this))
  9862. (ass (assoc head org-todo-kwd-alist))
  9863. (interpret (nth 1 ass))
  9864. (done-word (nth 3 ass))
  9865. (final-done-word (nth 4 ass))
  9866. (last-state (or this ""))
  9867. (completion-ignore-case t)
  9868. (member (member this org-todo-keywords-1))
  9869. (tail (cdr member))
  9870. (state (cond
  9871. ((and org-todo-key-trigger
  9872. (or (and (equal arg '(4))
  9873. (eq org-use-fast-todo-selection 'prefix))
  9874. (and (not arg) org-use-fast-todo-selection
  9875. (not (eq org-use-fast-todo-selection
  9876. 'prefix)))))
  9877. ;; Use fast selection
  9878. (org-fast-todo-selection))
  9879. ((and (equal arg '(4))
  9880. (or (not org-use-fast-todo-selection)
  9881. (not org-todo-key-trigger)))
  9882. ;; Read a state with completion
  9883. (org-icompleting-read
  9884. "State: " (mapcar (lambda(x) (list x))
  9885. org-todo-keywords-1)
  9886. nil t))
  9887. ((eq arg 'right)
  9888. (if this
  9889. (if tail (car tail) nil)
  9890. (car org-todo-keywords-1)))
  9891. ((eq arg 'left)
  9892. (if (equal member org-todo-keywords-1)
  9893. nil
  9894. (if this
  9895. (nth (- (length org-todo-keywords-1)
  9896. (length tail) 2)
  9897. org-todo-keywords-1)
  9898. (org-last org-todo-keywords-1))))
  9899. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9900. (setq arg nil))) ; hack to fall back to cycling
  9901. (arg
  9902. ;; user or caller requests a specific state
  9903. (cond
  9904. ((equal arg "") nil)
  9905. ((eq arg 'none) nil)
  9906. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9907. ((eq arg 'nextset)
  9908. (or (car (cdr (member head org-todo-heads)))
  9909. (car org-todo-heads)))
  9910. ((eq arg 'previousset)
  9911. (let ((org-todo-heads (reverse org-todo-heads)))
  9912. (or (car (cdr (member head org-todo-heads)))
  9913. (car org-todo-heads))))
  9914. ((car (member arg org-todo-keywords-1)))
  9915. ((stringp arg)
  9916. (error "State `%s' not valid in this file" arg))
  9917. ((nth (1- (prefix-numeric-value arg))
  9918. org-todo-keywords-1))))
  9919. ((null member) (or head (car org-todo-keywords-1)))
  9920. ((equal this final-done-word) nil) ;; -> make empty
  9921. ((null tail) nil) ;; -> first entry
  9922. ((memq interpret '(type priority))
  9923. (if (eq this-command last-command)
  9924. (car tail)
  9925. (if (> (length tail) 0)
  9926. (or done-word (car org-done-keywords))
  9927. nil)))
  9928. (t
  9929. (car tail))))
  9930. (state (or
  9931. (run-hook-with-args-until-success
  9932. 'org-todo-get-default-hook state last-state)
  9933. state))
  9934. (next (if state (concat " " state " ") " "))
  9935. (change-plist (list :type 'todo-state-change :from this :to state
  9936. :position startpos))
  9937. dolog now-done-p)
  9938. (when org-blocker-hook
  9939. (setq org-last-todo-state-is-todo
  9940. (not (member this org-done-keywords)))
  9941. (unless (save-excursion
  9942. (save-match-data
  9943. (org-with-wide-buffer
  9944. (run-hook-with-args-until-failure
  9945. 'org-blocker-hook change-plist))))
  9946. (if (interactive-p)
  9947. (error "TODO state change from %s to %s blocked" this state)
  9948. ;; fail silently
  9949. (message "TODO state change from %s to %s blocked" this state)
  9950. (throw 'exit nil))))
  9951. (store-match-data match-data)
  9952. (replace-match next t t)
  9953. (unless (pos-visible-in-window-p hl-pos)
  9954. (message "TODO state changed to %s" (org-trim next)))
  9955. (unless head
  9956. (setq head (org-get-todo-sequence-head state)
  9957. ass (assoc head org-todo-kwd-alist)
  9958. interpret (nth 1 ass)
  9959. done-word (nth 3 ass)
  9960. final-done-word (nth 4 ass)))
  9961. (when (memq arg '(nextset previousset))
  9962. (message "Keyword-Set %d/%d: %s"
  9963. (- (length org-todo-sets) -1
  9964. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9965. (length org-todo-sets)
  9966. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9967. (setq org-last-todo-state-is-todo
  9968. (not (member state org-done-keywords)))
  9969. (setq now-done-p (and (member state org-done-keywords)
  9970. (not (member this org-done-keywords))))
  9971. (and logging (org-local-logging logging))
  9972. (when (and (or org-todo-log-states org-log-done)
  9973. (not (eq org-inhibit-logging t))
  9974. (not (memq arg '(nextset previousset))))
  9975. ;; we need to look at recording a time and note
  9976. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9977. (nth 2 (assoc this org-todo-log-states))))
  9978. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9979. (setq dolog 'time))
  9980. (when (and state
  9981. (member state org-not-done-keywords)
  9982. (not (member this org-not-done-keywords)))
  9983. ;; This is now a todo state and was not one before
  9984. ;; If there was a CLOSED time stamp, get rid of it.
  9985. (org-add-planning-info nil nil 'closed))
  9986. (when (and now-done-p org-log-done)
  9987. ;; It is now done, and it was not done before
  9988. (org-add-planning-info 'closed (org-current-time))
  9989. (if (and (not dolog) (eq 'note org-log-done))
  9990. (org-add-log-setup 'done state this 'findpos 'note)))
  9991. (when (and state dolog)
  9992. ;; This is a non-nil state, and we need to log it
  9993. (org-add-log-setup 'state state this 'findpos dolog)))
  9994. ;; Fixup tag positioning
  9995. (org-todo-trigger-tag-changes state)
  9996. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9997. (when org-provide-todo-statistics
  9998. (org-update-parent-todo-statistics))
  9999. (run-hooks 'org-after-todo-state-change-hook)
  10000. (if (and arg (not (member state org-done-keywords)))
  10001. (setq head (org-get-todo-sequence-head state)))
  10002. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10003. ;; Do we need to trigger a repeat?
  10004. (when now-done-p
  10005. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10006. ;; This is for the agenda, take a snapshot of the headline.
  10007. (save-match-data
  10008. (setq org-agenda-headline-snapshot-before-repeat
  10009. (org-get-heading))))
  10010. (org-auto-repeat-maybe state))
  10011. ;; Fixup cursor location if close to the keyword
  10012. (if (and (outline-on-heading-p)
  10013. (not (bolp))
  10014. (save-excursion (beginning-of-line 1)
  10015. (looking-at org-todo-line-regexp))
  10016. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10017. (progn
  10018. (goto-char (or (match-end 2) (match-end 1)))
  10019. (and (looking-at " ") (just-one-space))))
  10020. (when org-trigger-hook
  10021. (save-excursion
  10022. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10023. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10024. "Block turning an entry into a TODO, using the hierarchy.
  10025. This checks whether the current task should be blocked from state
  10026. changes. Such blocking occurs when:
  10027. 1. The task has children which are not all in a completed state.
  10028. 2. A task has a parent with the property :ORDERED:, and there
  10029. are siblings prior to the current task with incomplete
  10030. status.
  10031. 3. The parent of the task is blocked because it has siblings that should
  10032. be done first, or is child of a block grandparent TODO entry."
  10033. (if (not org-enforce-todo-dependencies)
  10034. t ; if locally turned off don't block
  10035. (catch 'dont-block
  10036. ;; If this is not a todo state change, or if this entry is already DONE,
  10037. ;; do not block
  10038. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10039. (member (plist-get change-plist :from)
  10040. (cons 'done org-done-keywords))
  10041. (member (plist-get change-plist :to)
  10042. (cons 'todo org-not-done-keywords))
  10043. (not (plist-get change-plist :to)))
  10044. (throw 'dont-block t))
  10045. ;; If this task has children, and any are undone, it's blocked
  10046. (save-excursion
  10047. (org-back-to-heading t)
  10048. (let ((this-level (funcall outline-level)))
  10049. (outline-next-heading)
  10050. (let ((child-level (funcall outline-level)))
  10051. (while (and (not (eobp))
  10052. (> child-level this-level))
  10053. ;; this todo has children, check whether they are all
  10054. ;; completed
  10055. (if (and (not (org-entry-is-done-p))
  10056. (org-entry-is-todo-p))
  10057. (throw 'dont-block nil))
  10058. (outline-next-heading)
  10059. (setq child-level (funcall outline-level))))))
  10060. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10061. ;; any previous siblings are undone, it's blocked
  10062. (save-excursion
  10063. (org-back-to-heading t)
  10064. (let* ((pos (point))
  10065. (parent-pos (and (org-up-heading-safe) (point))))
  10066. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10067. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10068. (forward-line 1)
  10069. (re-search-forward org-not-done-heading-regexp pos t))
  10070. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10071. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10072. (while t
  10073. (goto-char parent-pos)
  10074. (if (not (looking-at org-not-done-heading-regexp))
  10075. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10076. (setq pos (point))
  10077. (setq parent-pos (and (org-up-heading-safe) (point)))
  10078. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10079. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10080. (forward-line 1)
  10081. (re-search-forward org-not-done-heading-regexp pos t))
  10082. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10083. (defcustom org-track-ordered-property-with-tag nil
  10084. "Should the ORDERED property also be shown as a tag?
  10085. The ORDERED property decides if an entry should require subtasks to be
  10086. completed in sequence. Since a property is not very visible, setting
  10087. this option means that toggling the ORDERED property with the command
  10088. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10089. not relevant for the behavior, but it makes things more visible.
  10090. Note that toggling the tag with tags commands will not change the property
  10091. and therefore not influence behavior!
  10092. This can be t, meaning the tag ORDERED should be used, It can also be a
  10093. string to select a different tag for this task."
  10094. :group 'org-todo
  10095. :type '(choice
  10096. (const :tag "No tracking" nil)
  10097. (const :tag "Track with ORDERED tag" t)
  10098. (string :tag "Use other tag")))
  10099. (defun org-toggle-ordered-property ()
  10100. "Toggle the ORDERED property of the current entry.
  10101. For better visibility, you can track the value of this property with a tag.
  10102. See variable `org-track-ordered-property-with-tag'."
  10103. (interactive)
  10104. (let* ((t1 org-track-ordered-property-with-tag)
  10105. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10106. (save-excursion
  10107. (org-back-to-heading)
  10108. (if (org-entry-get nil "ORDERED")
  10109. (progn
  10110. (org-delete-property "ORDERED")
  10111. (and tag (org-toggle-tag tag 'off))
  10112. (message "Subtasks can be completed in arbitrary order"))
  10113. (org-entry-put nil "ORDERED" "t")
  10114. (and tag (org-toggle-tag tag 'on))
  10115. (message "Subtasks must be completed in sequence")))))
  10116. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10117. (defun org-block-todo-from-checkboxes (change-plist)
  10118. "Block turning an entry into a TODO, using checkboxes.
  10119. This checks whether the current task should be blocked from state
  10120. changes because there are unchecked boxes in this entry."
  10121. (if (not org-enforce-todo-checkbox-dependencies)
  10122. t ; if locally turned off don't block
  10123. (catch 'dont-block
  10124. ;; If this is not a todo state change, or if this entry is already DONE,
  10125. ;; do not block
  10126. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10127. (member (plist-get change-plist :from)
  10128. (cons 'done org-done-keywords))
  10129. (member (plist-get change-plist :to)
  10130. (cons 'todo org-not-done-keywords))
  10131. (not (plist-get change-plist :to)))
  10132. (throw 'dont-block t))
  10133. ;; If this task has checkboxes that are not checked, it's blocked
  10134. (save-excursion
  10135. (org-back-to-heading t)
  10136. (let ((beg (point)) end)
  10137. (outline-next-heading)
  10138. (setq end (point))
  10139. (goto-char beg)
  10140. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10141. end t)
  10142. (progn
  10143. (if (boundp 'org-blocked-by-checkboxes)
  10144. (setq org-blocked-by-checkboxes t))
  10145. (throw 'dont-block nil)))))
  10146. t))) ; do not block
  10147. (defun org-entry-blocked-p ()
  10148. "Is the current entry blocked?"
  10149. (if (org-entry-get nil "NOBLOCKING")
  10150. nil ;; Never block this entry
  10151. (not
  10152. (run-hook-with-args-until-failure
  10153. 'org-blocker-hook
  10154. (list :type 'todo-state-change
  10155. :position (point)
  10156. :from 'todo
  10157. :to 'done)))))
  10158. (defun org-update-statistics-cookies (all)
  10159. "Update the statistics cookie, either from TODO or from checkboxes.
  10160. This should be called with the cursor in a line with a statistics cookie."
  10161. (interactive "P")
  10162. (if all
  10163. (progn
  10164. (org-update-checkbox-count 'all)
  10165. (org-map-entries 'org-update-parent-todo-statistics))
  10166. (if (not (org-on-heading-p))
  10167. (org-update-checkbox-count)
  10168. (let ((pos (move-marker (make-marker) (point)))
  10169. end l1 l2)
  10170. (ignore-errors (org-back-to-heading t))
  10171. (if (not (org-on-heading-p))
  10172. (org-update-checkbox-count)
  10173. (setq l1 (org-outline-level))
  10174. (setq end (save-excursion
  10175. (outline-next-heading)
  10176. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10177. (point)))
  10178. (if (and (save-excursion
  10179. (re-search-forward
  10180. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10181. (not (save-excursion (re-search-forward
  10182. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10183. (org-update-checkbox-count)
  10184. (if (and l2 (> l2 l1))
  10185. (progn
  10186. (goto-char end)
  10187. (org-update-parent-todo-statistics))
  10188. (goto-char pos)
  10189. (beginning-of-line 1)
  10190. (while (re-search-forward
  10191. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10192. (point-at-eol) t)
  10193. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10194. (goto-char pos)
  10195. (move-marker pos nil)))))
  10196. (defvar org-entry-property-inherited-from) ;; defined below
  10197. (defun org-update-parent-todo-statistics ()
  10198. "Update any statistics cookie in the parent of the current headline.
  10199. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10200. the entire subtree and this will travel up the hierarchy and update
  10201. statistics everywhere."
  10202. (interactive)
  10203. (let* ((lim 0) prop
  10204. (recursive (or (not org-hierarchical-todo-statistics)
  10205. (string-match
  10206. "\\<recursive\\>"
  10207. (or (setq prop (org-entry-get
  10208. nil "COOKIE_DATA" 'inherit)) ""))))
  10209. (lim (or (and prop (marker-position
  10210. org-entry-property-inherited-from))
  10211. lim))
  10212. (first t)
  10213. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10214. level ltoggle l1 new ndel
  10215. (cnt-all 0) (cnt-done 0) is-percent kwd
  10216. checkbox-beg ov ovs ove cookie-present)
  10217. (catch 'exit
  10218. (save-excursion
  10219. (beginning-of-line 1)
  10220. (if (org-at-heading-p)
  10221. (setq ltoggle (funcall outline-level))
  10222. (error "This should not happen"))
  10223. (while (and (setq level (org-up-heading-safe))
  10224. (or recursive first)
  10225. (>= (point) lim))
  10226. (setq first nil cookie-present nil)
  10227. (unless (and level
  10228. (not (string-match
  10229. "\\<checkbox\\>"
  10230. (downcase
  10231. (or (org-entry-get
  10232. nil "COOKIE_DATA")
  10233. "")))))
  10234. (throw 'exit nil))
  10235. (while (re-search-forward box-re (point-at-eol) t)
  10236. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10237. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10238. (save-match-data
  10239. (unless (outline-next-heading) (throw 'exit nil))
  10240. (while (and (looking-at org-complex-heading-regexp)
  10241. (> (setq l1 (length (match-string 1))) level))
  10242. (setq kwd (and (or recursive (= l1 ltoggle))
  10243. (match-string 2)))
  10244. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10245. (and (listp org-provide-todo-statistics)
  10246. (or (member kwd org-provide-todo-statistics)
  10247. (member kwd org-done-keywords))))
  10248. (setq cnt-all (1+ cnt-all))
  10249. (if (eq org-provide-todo-statistics t)
  10250. (and kwd (setq cnt-all (1+ cnt-all)))))
  10251. (and (member kwd org-done-keywords)
  10252. (setq cnt-done (1+ cnt-done)))
  10253. (outline-next-heading)))
  10254. (setq new
  10255. (if is-percent
  10256. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10257. (format "[%d/%d]" cnt-done cnt-all))
  10258. ndel (- (match-end 0) checkbox-beg))
  10259. ;; handle overlays when updating cookie from column view
  10260. (when (setq ov (car (overlays-at checkbox-beg)))
  10261. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10262. (delete-overlay ov))
  10263. (goto-char checkbox-beg)
  10264. (insert new)
  10265. (delete-region (point) (+ (point) ndel))
  10266. (when ov (move-overlay ov ovs ove)))
  10267. (when cookie-present
  10268. (run-hook-with-args 'org-after-todo-statistics-hook
  10269. cnt-done (- cnt-all cnt-done))))))
  10270. (run-hooks 'org-todo-statistics-hook)))
  10271. (defvar org-after-todo-statistics-hook nil
  10272. "Hook that is called after a TODO statistics cookie has been updated.
  10273. Each function is called with two arguments: the number of not-done entries
  10274. and the number of done entries.
  10275. For example, the following function, when added to this hook, will switch
  10276. an entry to DONE when all children are done, and back to TODO when new
  10277. entries are set to a TODO status. Note that this hook is only called
  10278. when there is a statistics cookie in the headline!
  10279. (defun org-summary-todo (n-done n-not-done)
  10280. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10281. (let (org-log-done org-log-states) ; turn off logging
  10282. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10283. ")
  10284. (defvar org-todo-statistics-hook nil
  10285. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10286. This hook runs even if there is no statistics cookie present, in which case
  10287. `org-after-todo-statistics-hook' would not run.")
  10288. (defun org-todo-trigger-tag-changes (state)
  10289. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10290. (let ((l org-todo-state-tags-triggers)
  10291. changes)
  10292. (when (or (not state) (equal state ""))
  10293. (setq changes (append changes (cdr (assoc "" l)))))
  10294. (when (and (stringp state) (> (length state) 0))
  10295. (setq changes (append changes (cdr (assoc state l)))))
  10296. (when (member state org-not-done-keywords)
  10297. (setq changes (append changes (cdr (assoc 'todo l)))))
  10298. (when (member state org-done-keywords)
  10299. (setq changes (append changes (cdr (assoc 'done l)))))
  10300. (dolist (c changes)
  10301. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10302. (defun org-local-logging (value)
  10303. "Get logging settings from a property VALUE."
  10304. (let* (words w a)
  10305. ;; directly set the variables, they are already local.
  10306. (setq org-log-done nil
  10307. org-log-repeat nil
  10308. org-todo-log-states nil)
  10309. (setq words (org-split-string value))
  10310. (while (setq w (pop words))
  10311. (cond
  10312. ((setq a (assoc w org-startup-options))
  10313. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10314. (set (nth 1 a) (nth 2 a))))
  10315. ((setq a (org-extract-log-state-settings w))
  10316. (and (member (car a) org-todo-keywords-1)
  10317. (push a org-todo-log-states)))))))
  10318. (defun org-get-todo-sequence-head (kwd)
  10319. "Return the head of the TODO sequence to which KWD belongs.
  10320. If KWD is not set, check if there is a text property remembering the
  10321. right sequence."
  10322. (let (p)
  10323. (cond
  10324. ((not kwd)
  10325. (or (get-text-property (point-at-bol) 'org-todo-head)
  10326. (progn
  10327. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10328. nil (point-at-eol)))
  10329. (get-text-property p 'org-todo-head))))
  10330. ((not (member kwd org-todo-keywords-1))
  10331. (car org-todo-keywords-1))
  10332. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10333. (defun org-fast-todo-selection ()
  10334. "Fast TODO keyword selection with single keys.
  10335. Returns the new TODO keyword, or nil if no state change should occur."
  10336. (let* ((fulltable org-todo-key-alist)
  10337. (done-keywords org-done-keywords) ;; needed for the faces.
  10338. (maxlen (apply 'max (mapcar
  10339. (lambda (x)
  10340. (if (stringp (car x)) (string-width (car x)) 0))
  10341. fulltable)))
  10342. (expert nil)
  10343. (fwidth (+ maxlen 3 1 3))
  10344. (ncol (/ (- (window-width) 4) fwidth))
  10345. tg cnt e c tbl
  10346. groups ingroup)
  10347. (save-excursion
  10348. (save-window-excursion
  10349. (if expert
  10350. (set-buffer (get-buffer-create " *Org todo*"))
  10351. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10352. (erase-buffer)
  10353. (org-set-local 'org-done-keywords done-keywords)
  10354. (setq tbl fulltable cnt 0)
  10355. (while (setq e (pop tbl))
  10356. (cond
  10357. ((equal e '(:startgroup))
  10358. (push '() groups) (setq ingroup t)
  10359. (when (not (= cnt 0))
  10360. (setq cnt 0)
  10361. (insert "\n"))
  10362. (insert "{ "))
  10363. ((equal e '(:endgroup))
  10364. (setq ingroup nil cnt 0)
  10365. (insert "}\n"))
  10366. ((equal e '(:newline))
  10367. (when (not (= cnt 0))
  10368. (setq cnt 0)
  10369. (insert "\n")
  10370. (setq e (car tbl))
  10371. (while (equal (car tbl) '(:newline))
  10372. (insert "\n")
  10373. (setq tbl (cdr tbl)))))
  10374. (t
  10375. (setq tg (car e) c (cdr e))
  10376. (if ingroup (push tg (car groups)))
  10377. (setq tg (org-add-props tg nil 'face
  10378. (org-get-todo-face tg)))
  10379. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10380. (insert "[" c "] " tg (make-string
  10381. (- fwidth 4 (length tg)) ?\ ))
  10382. (when (= (setq cnt (1+ cnt)) ncol)
  10383. (insert "\n")
  10384. (if ingroup (insert " "))
  10385. (setq cnt 0)))))
  10386. (insert "\n")
  10387. (goto-char (point-min))
  10388. (if (not expert) (org-fit-window-to-buffer))
  10389. (message "[a-z..]:Set [SPC]:clear")
  10390. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10391. (cond
  10392. ((or (= c ?\C-g)
  10393. (and (= c ?q) (not (rassoc c fulltable))))
  10394. (setq quit-flag t))
  10395. ((= c ?\ ) nil)
  10396. ((setq e (rassoc c fulltable) tg (car e))
  10397. tg)
  10398. (t (setq quit-flag t)))))))
  10399. (defun org-entry-is-todo-p ()
  10400. (member (org-get-todo-state) org-not-done-keywords))
  10401. (defun org-entry-is-done-p ()
  10402. (member (org-get-todo-state) org-done-keywords))
  10403. (defun org-get-todo-state ()
  10404. (save-excursion
  10405. (org-back-to-heading t)
  10406. (and (looking-at org-todo-line-regexp)
  10407. (match-end 2)
  10408. (match-string 2))))
  10409. (defun org-at-date-range-p (&optional inactive-ok)
  10410. "Is the cursor inside a date range?"
  10411. (interactive)
  10412. (save-excursion
  10413. (catch 'exit
  10414. (let ((pos (point)))
  10415. (skip-chars-backward "^[<\r\n")
  10416. (skip-chars-backward "<[")
  10417. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10418. (>= (match-end 0) pos)
  10419. (throw 'exit t))
  10420. (skip-chars-backward "^<[\r\n")
  10421. (skip-chars-backward "<[")
  10422. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10423. (>= (match-end 0) pos)
  10424. (throw 'exit t)))
  10425. nil)))
  10426. (defun org-get-repeat (&optional tagline)
  10427. "Check if there is a deadline/schedule with repeater in this entry."
  10428. (save-match-data
  10429. (save-excursion
  10430. (org-back-to-heading t)
  10431. (and (re-search-forward (if tagline
  10432. (concat tagline "\\s-*" org-repeat-re)
  10433. org-repeat-re)
  10434. (org-entry-end-position) t)
  10435. (match-string-no-properties 1)))))
  10436. (defvar org-last-changed-timestamp)
  10437. (defvar org-last-inserted-timestamp)
  10438. (defvar org-log-post-message)
  10439. (defvar org-log-note-purpose)
  10440. (defvar org-log-note-how)
  10441. (defvar org-log-note-extra)
  10442. (defun org-auto-repeat-maybe (done-word)
  10443. "Check if the current headline contains a repeated deadline/schedule.
  10444. If yes, set TODO state back to what it was and change the base date
  10445. of repeating deadline/scheduled time stamps to new date.
  10446. This function is run automatically after each state change to a DONE state."
  10447. ;; last-state is dynamically scoped into this function
  10448. (let* ((repeat (org-get-repeat))
  10449. (aa (assoc last-state org-todo-kwd-alist))
  10450. (interpret (nth 1 aa))
  10451. (head (nth 2 aa))
  10452. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10453. (msg "Entry repeats: ")
  10454. (org-log-done nil)
  10455. (org-todo-log-states nil)
  10456. re type n what ts time to-state)
  10457. (when repeat
  10458. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10459. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10460. org-todo-repeat-to-state))
  10461. (unless (and to-state (member to-state org-todo-keywords-1))
  10462. (setq to-state (if (eq interpret 'type) last-state head)))
  10463. (org-todo to-state)
  10464. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10465. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10466. (org-time-stamp-format t t))))
  10467. (when org-log-repeat
  10468. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10469. (memq 'org-add-log-note post-command-hook))
  10470. ;; OK, we are already setup for some record
  10471. (if (eq org-log-repeat 'note)
  10472. ;; make sure we take a note, not only a time stamp
  10473. (setq org-log-note-how 'note))
  10474. ;; Set up for taking a record
  10475. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10476. last-state
  10477. 'findpos org-log-repeat)))
  10478. (org-back-to-heading t)
  10479. (org-add-planning-info nil nil 'closed)
  10480. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10481. org-deadline-time-regexp "\\)\\|\\("
  10482. org-ts-regexp "\\)"))
  10483. (while (re-search-forward
  10484. re (save-excursion (outline-next-heading) (point)) t)
  10485. (setq type (if (match-end 1) org-scheduled-string
  10486. (if (match-end 3) org-deadline-string "Plain:"))
  10487. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10488. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10489. (setq n (string-to-number (match-string 2 ts))
  10490. what (match-string 3 ts))
  10491. (if (equal what "w") (setq n (* n 7) what "d"))
  10492. ;; Preparation, see if we need to modify the start date for the change
  10493. (when (match-end 1)
  10494. (setq time (save-match-data (org-time-string-to-time ts)))
  10495. (cond
  10496. ((equal (match-string 1 ts) ".")
  10497. ;; Shift starting date to today
  10498. (org-timestamp-change
  10499. (- (time-to-days (current-time)) (time-to-days time))
  10500. 'day))
  10501. ((equal (match-string 1 ts) "+")
  10502. (let ((nshiftmax 10) (nshift 0))
  10503. (while (or (= nshift 0)
  10504. (<= (time-to-days time)
  10505. (time-to-days (current-time))))
  10506. (when (= (incf nshift) nshiftmax)
  10507. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10508. (error "Abort")))
  10509. (org-timestamp-change n (cdr (assoc what whata)))
  10510. (org-at-timestamp-p t)
  10511. (setq ts (match-string 1))
  10512. (setq time (save-match-data (org-time-string-to-time ts)))))
  10513. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10514. ;; rematch, so that we have everything in place for the real shift
  10515. (org-at-timestamp-p t)
  10516. (setq ts (match-string 1))
  10517. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10518. (org-timestamp-change n (cdr (assoc what whata)))
  10519. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10520. (setq org-log-post-message msg)
  10521. (message "%s" msg))))
  10522. (defun org-show-todo-tree (arg)
  10523. "Make a compact tree which shows all headlines marked with TODO.
  10524. The tree will show the lines where the regexp matches, and all higher
  10525. headlines above the match.
  10526. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10527. With a numeric prefix N, construct a sparse tree for the Nth element
  10528. of `org-todo-keywords-1'."
  10529. (interactive "P")
  10530. (let ((case-fold-search nil)
  10531. (kwd-re
  10532. (cond ((null arg) org-not-done-regexp)
  10533. ((equal arg '(4))
  10534. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10535. (mapcar 'list org-todo-keywords-1))))
  10536. (concat "\\("
  10537. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10538. "\\)\\>")))
  10539. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10540. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10541. org-todo-keywords-1)))
  10542. (t (error "Invalid prefix argument: %s" arg)))))
  10543. (message "%d TODO entries found"
  10544. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10545. (defun org-deadline (&optional remove time)
  10546. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10547. With argument REMOVE, remove any deadline from the item.
  10548. When TIME is set, it should be an internal time specification, and the
  10549. scheduling will use the corresponding date."
  10550. (interactive "P")
  10551. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10552. (repeater (and old-date
  10553. (string-match
  10554. "\\([.+]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10555. old-date)
  10556. (match-string 1 old-date))))
  10557. (if remove
  10558. (progn
  10559. (when (and old-date org-log-redeadline)
  10560. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10561. org-log-redeadline))
  10562. (org-remove-timestamp-with-keyword org-deadline-string)
  10563. (message "Item no longer has a deadline."))
  10564. (org-add-planning-info 'deadline time 'closed)
  10565. (when (and old-date org-log-redeadline
  10566. (not (equal old-date
  10567. (substring org-last-inserted-timestamp 1 -1))))
  10568. (org-add-log-setup 'redeadline nil old-date 'findpos
  10569. org-log-redeadline))
  10570. (when repeater
  10571. (save-excursion
  10572. (org-back-to-heading t)
  10573. (when (re-search-forward (concat org-deadline-string " "
  10574. org-last-inserted-timestamp)
  10575. (save-excursion
  10576. (outline-next-heading) (point)) t)
  10577. (goto-char (1- (match-end 0)))
  10578. (insert " " repeater)
  10579. (setq org-last-inserted-timestamp
  10580. (concat (substring org-last-inserted-timestamp 0 -1)
  10581. " " repeater
  10582. (substring org-last-inserted-timestamp -1))))))
  10583. (message "Deadline on %s" org-last-inserted-timestamp))))
  10584. (defun org-schedule (&optional remove time)
  10585. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10586. With argument REMOVE, remove any scheduling date from the item.
  10587. When TIME is set, it should be an internal time specification, and the
  10588. scheduling will use the corresponding date."
  10589. (interactive "P")
  10590. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10591. (repeater (and old-date
  10592. (string-match
  10593. "\\([.+]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10594. old-date)
  10595. (match-string 1 old-date))))
  10596. (if remove
  10597. (progn
  10598. (when (and old-date org-log-reschedule)
  10599. (org-add-log-setup 'delschedule nil old-date 'findpos
  10600. org-log-reschedule))
  10601. (org-remove-timestamp-with-keyword org-scheduled-string)
  10602. (message "Item is no longer scheduled."))
  10603. (org-add-planning-info 'scheduled time 'closed)
  10604. (when (and old-date org-log-reschedule
  10605. (not (equal old-date
  10606. (substring org-last-inserted-timestamp 1 -1))))
  10607. (org-add-log-setup 'reschedule nil old-date 'findpos
  10608. org-log-reschedule))
  10609. (when repeater
  10610. (save-excursion
  10611. (org-back-to-heading t)
  10612. (when (re-search-forward (concat org-scheduled-string " "
  10613. org-last-inserted-timestamp)
  10614. (save-excursion
  10615. (outline-next-heading) (point)) t)
  10616. (goto-char (1- (match-end 0)))
  10617. (insert " " repeater)
  10618. (setq org-last-inserted-timestamp
  10619. (concat (substring org-last-inserted-timestamp 0 -1)
  10620. " " repeater
  10621. (substring org-last-inserted-timestamp -1))))))
  10622. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10623. (defun org-get-scheduled-time (pom &optional inherit)
  10624. "Get the scheduled time as a time tuple, of a format suitable
  10625. for calling org-schedule with, or if there is no scheduling,
  10626. returns nil."
  10627. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10628. (when time
  10629. (apply 'encode-time (org-parse-time-string time)))))
  10630. (defun org-get-deadline-time (pom &optional inherit)
  10631. "Get the deadline as a time tuple, of a format suitable for
  10632. calling org-deadline with, or if there is no scheduling, returns
  10633. nil."
  10634. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10635. (when time
  10636. (apply 'encode-time (org-parse-time-string time)))))
  10637. (defun org-remove-timestamp-with-keyword (keyword)
  10638. "Remove all time stamps with KEYWORD in the current entry."
  10639. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10640. beg)
  10641. (save-excursion
  10642. (org-back-to-heading t)
  10643. (setq beg (point))
  10644. (outline-next-heading)
  10645. (while (re-search-backward re beg t)
  10646. (replace-match "")
  10647. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10648. (equal (char-before) ?\ ))
  10649. (backward-delete-char 1)
  10650. (if (string-match "^[ \t]*$" (buffer-substring
  10651. (point-at-bol) (point-at-eol)))
  10652. (delete-region (point-at-bol)
  10653. (min (point-max) (1+ (point-at-eol))))))))))
  10654. (defun org-add-planning-info (what &optional time &rest remove)
  10655. "Insert new timestamp with keyword in the line directly after the headline.
  10656. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10657. If non is given, the user is prompted for a date.
  10658. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10659. be removed."
  10660. (interactive)
  10661. (let (org-time-was-given org-end-time-was-given ts
  10662. end default-time default-input)
  10663. (catch 'exit
  10664. (when (and (not time) (memq what '(scheduled deadline)))
  10665. ;; Try to get a default date/time from existing timestamp
  10666. (save-excursion
  10667. (org-back-to-heading t)
  10668. (setq end (save-excursion (outline-next-heading) (point)))
  10669. (when (re-search-forward (if (eq what 'scheduled)
  10670. org-scheduled-time-regexp
  10671. org-deadline-time-regexp)
  10672. end t)
  10673. (setq ts (match-string 1)
  10674. default-time
  10675. (apply 'encode-time (org-parse-time-string ts))
  10676. default-input (and ts (org-get-compact-tod ts))))))
  10677. (when what
  10678. ;; If necessary, get the time from the user
  10679. (setq time (or time (org-read-date nil 'to-time nil nil
  10680. default-time default-input))))
  10681. (when (and org-insert-labeled-timestamps-at-point
  10682. (member what '(scheduled deadline)))
  10683. (insert
  10684. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10685. (org-insert-time-stamp time org-time-was-given
  10686. nil nil nil (list org-end-time-was-given))
  10687. (setq what nil))
  10688. (save-excursion
  10689. (save-restriction
  10690. (let (col list elt ts buffer-invisibility-spec)
  10691. (org-back-to-heading t)
  10692. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10693. (goto-char (match-end 1))
  10694. (setq col (current-column))
  10695. (goto-char (match-end 0))
  10696. (if (eobp) (insert "\n") (forward-char 1))
  10697. (when (and (not what)
  10698. (not (looking-at
  10699. (concat "[ \t]*"
  10700. org-keyword-time-not-clock-regexp))))
  10701. ;; Nothing to add, nothing to remove...... :-)
  10702. (throw 'exit nil))
  10703. (if (and (not (looking-at outline-regexp))
  10704. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10705. "[^\r\n]*"))
  10706. (not (equal (match-string 1) org-clock-string)))
  10707. (narrow-to-region (match-beginning 0) (match-end 0))
  10708. (insert-before-markers "\n")
  10709. (backward-char 1)
  10710. (narrow-to-region (point) (point))
  10711. (and org-adapt-indentation (org-indent-to-column col)))
  10712. ;; Check if we have to remove something.
  10713. (setq list (cons what remove))
  10714. (while list
  10715. (setq elt (pop list))
  10716. (goto-char (point-min))
  10717. (when (or (and (eq elt 'scheduled)
  10718. (re-search-forward org-scheduled-time-regexp nil t))
  10719. (and (eq elt 'deadline)
  10720. (re-search-forward org-deadline-time-regexp nil t))
  10721. (and (eq elt 'closed)
  10722. (re-search-forward org-closed-time-regexp nil t)))
  10723. (replace-match "")
  10724. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10725. (skip-chars-backward " ")
  10726. (if (looking-at " +") (replace-match ""))))
  10727. (goto-char (point-max))
  10728. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10729. (when what
  10730. (insert
  10731. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10732. (cond ((eq what 'scheduled) org-scheduled-string)
  10733. ((eq what 'deadline) org-deadline-string)
  10734. ((eq what 'closed) org-closed-string))
  10735. " ")
  10736. (setq ts (org-insert-time-stamp
  10737. time
  10738. (or org-time-was-given
  10739. (and (eq what 'closed) org-log-done-with-time))
  10740. (eq what 'closed)
  10741. nil nil (list org-end-time-was-given)))
  10742. (end-of-line 1))
  10743. (goto-char (point-min))
  10744. (widen)
  10745. (if (and (looking-at "[ \t]*\n")
  10746. (equal (char-before) ?\n))
  10747. (delete-region (1- (point)) (point-at-eol)))
  10748. ts))))))
  10749. (defvar org-log-note-marker (make-marker))
  10750. (defvar org-log-note-purpose nil)
  10751. (defvar org-log-note-state nil)
  10752. (defvar org-log-note-previous-state nil)
  10753. (defvar org-log-note-how nil)
  10754. (defvar org-log-note-extra nil)
  10755. (defvar org-log-note-window-configuration nil)
  10756. (defvar org-log-note-return-to (make-marker))
  10757. (defvar org-log-post-message nil
  10758. "Message to be displayed after a log note has been stored.
  10759. The auto-repeater uses this.")
  10760. (defun org-add-note ()
  10761. "Add a note to the current entry.
  10762. This is done in the same way as adding a state change note."
  10763. (interactive)
  10764. (org-add-log-setup 'note nil nil 'findpos nil))
  10765. (defvar org-property-end-re)
  10766. (defun org-add-log-setup (&optional purpose state prev-state
  10767. findpos how extra)
  10768. "Set up the post command hook to take a note.
  10769. If this is about to TODO state change, the new state is expected in STATE.
  10770. When FINDPOS is non-nil, find the correct position for the note in
  10771. the current entry. If not, assume that it can be inserted at point.
  10772. HOW is an indicator what kind of note should be created.
  10773. EXTRA is additional text that will be inserted into the notes buffer."
  10774. (let* ((org-log-into-drawer (org-log-into-drawer))
  10775. (drawer (cond ((stringp org-log-into-drawer)
  10776. org-log-into-drawer)
  10777. (org-log-into-drawer "LOGBOOK")
  10778. (t nil))))
  10779. (save-restriction
  10780. (save-excursion
  10781. (when findpos
  10782. (org-back-to-heading t)
  10783. (narrow-to-region (point) (save-excursion
  10784. (outline-next-heading) (point)))
  10785. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10786. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10787. "[^\r\n]*\\)?"))
  10788. (goto-char (match-end 0))
  10789. (cond
  10790. (drawer
  10791. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10792. nil t)
  10793. (progn
  10794. (goto-char (match-end 0))
  10795. (or org-log-states-order-reversed
  10796. (and (re-search-forward org-property-end-re nil t)
  10797. (goto-char (1- (match-beginning 0))))))
  10798. (insert "\n:" drawer ":\n:END:")
  10799. (beginning-of-line 0)
  10800. (org-indent-line-function)
  10801. (beginning-of-line 2)
  10802. (org-indent-line-function)
  10803. (end-of-line 0)))
  10804. ((and org-log-state-notes-insert-after-drawers
  10805. (save-excursion
  10806. (forward-line) (looking-at org-drawer-regexp)))
  10807. (forward-line)
  10808. (while (looking-at org-drawer-regexp)
  10809. (goto-char (match-end 0))
  10810. (re-search-forward org-property-end-re (point-max) t)
  10811. (forward-line))
  10812. (forward-line -1)))
  10813. (unless org-log-states-order-reversed
  10814. (and (= (char-after) ?\n) (forward-char 1))
  10815. (org-skip-over-state-notes)
  10816. (skip-chars-backward " \t\n\r")))
  10817. (move-marker org-log-note-marker (point))
  10818. (setq org-log-note-purpose purpose
  10819. org-log-note-state state
  10820. org-log-note-previous-state prev-state
  10821. org-log-note-how how
  10822. org-log-note-extra extra)
  10823. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10824. (defun org-skip-over-state-notes ()
  10825. "Skip past the list of State notes in an entry."
  10826. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10827. (when (ignore-errors (goto-char (org-in-item-p)))
  10828. (let* ((struct (org-list-struct))
  10829. (prevs (org-list-prevs-alist struct)))
  10830. (while (looking-at "[ \t]*- State")
  10831. (goto-char (or (org-list-get-next-item (point) struct prevs)
  10832. (org-list-get-item-end (point) struct)))))))
  10833. (defun org-add-log-note (&optional purpose)
  10834. "Pop up a window for taking a note, and add this note later at point."
  10835. (remove-hook 'post-command-hook 'org-add-log-note)
  10836. (setq org-log-note-window-configuration (current-window-configuration))
  10837. (delete-other-windows)
  10838. (move-marker org-log-note-return-to (point))
  10839. (switch-to-buffer (marker-buffer org-log-note-marker))
  10840. (goto-char org-log-note-marker)
  10841. (org-switch-to-buffer-other-window "*Org Note*")
  10842. (erase-buffer)
  10843. (if (memq org-log-note-how '(time state))
  10844. (let (current-prefix-arg) (org-store-log-note))
  10845. (let ((org-inhibit-startup t)) (org-mode))
  10846. (insert (format "# Insert note for %s.
  10847. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10848. (cond
  10849. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10850. ((eq org-log-note-purpose 'done) "closed todo item")
  10851. ((eq org-log-note-purpose 'state)
  10852. (format "state change from \"%s\" to \"%s\""
  10853. (or org-log-note-previous-state "")
  10854. (or org-log-note-state "")))
  10855. ((eq org-log-note-purpose 'reschedule)
  10856. "rescheduling")
  10857. ((eq org-log-note-purpose 'delschedule)
  10858. "no longer scheduled")
  10859. ((eq org-log-note-purpose 'redeadline)
  10860. "changing deadline")
  10861. ((eq org-log-note-purpose 'deldeadline)
  10862. "removing deadline")
  10863. ((eq org-log-note-purpose 'refile)
  10864. "refiling")
  10865. ((eq org-log-note-purpose 'note)
  10866. "this entry")
  10867. (t (error "This should not happen")))))
  10868. (if org-log-note-extra (insert org-log-note-extra))
  10869. (org-set-local 'org-finish-function 'org-store-log-note)))
  10870. (defvar org-note-abort nil) ; dynamically scoped
  10871. (defun org-store-log-note ()
  10872. "Finish taking a log note, and insert it to where it belongs."
  10873. (let ((txt (buffer-string))
  10874. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10875. lines ind bul)
  10876. (kill-buffer (current-buffer))
  10877. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10878. (setq txt (replace-match "" t t txt)))
  10879. (if (string-match "\\s-+\\'" txt)
  10880. (setq txt (replace-match "" t t txt)))
  10881. (setq lines (org-split-string txt "\n"))
  10882. (when (and note (string-match "\\S-" note))
  10883. (setq note
  10884. (org-replace-escapes
  10885. note
  10886. (list (cons "%u" (user-login-name))
  10887. (cons "%U" user-full-name)
  10888. (cons "%t" (format-time-string
  10889. (org-time-stamp-format 'long 'inactive)
  10890. (current-time)))
  10891. (cons "%T" (format-time-string
  10892. (org-time-stamp-format 'long nil)
  10893. (current-time)))
  10894. (cons "%s" (if org-log-note-state
  10895. (concat "\"" org-log-note-state "\"")
  10896. ""))
  10897. (cons "%S" (if org-log-note-previous-state
  10898. (concat "\"" org-log-note-previous-state "\"")
  10899. "\"\"")))))
  10900. (if lines (setq note (concat note " \\\\")))
  10901. (push note lines))
  10902. (when (or current-prefix-arg org-note-abort)
  10903. (when org-log-into-drawer
  10904. (org-remove-empty-drawer-at
  10905. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10906. org-log-note-marker))
  10907. (setq lines nil))
  10908. (when lines
  10909. (with-current-buffer (marker-buffer org-log-note-marker)
  10910. (save-excursion
  10911. (goto-char org-log-note-marker)
  10912. (move-marker org-log-note-marker nil)
  10913. (end-of-line 1)
  10914. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10915. (setq ind (save-excursion
  10916. (if (ignore-errors (goto-char (org-in-item-p)))
  10917. (let ((struct (org-list-struct)))
  10918. (org-list-get-ind
  10919. (org-list-get-top-point struct) struct))
  10920. (skip-chars-backward " \r\t\n")
  10921. (cond
  10922. ((and (org-at-heading-p)
  10923. org-adapt-indentation)
  10924. (1+ (org-current-level)))
  10925. ((org-at-heading-p) 0)
  10926. (t (org-get-indentation))))))
  10927. (setq bul (org-list-bullet-string "-"))
  10928. (org-indent-line-to ind)
  10929. (insert bul (pop lines))
  10930. (let ((ind-body (+ (length bul) ind)))
  10931. (while lines
  10932. (insert "\n")
  10933. (org-indent-line-to ind-body)
  10934. (insert (pop lines))))
  10935. (message "Note stored")
  10936. (org-back-to-heading t)
  10937. (org-cycle-hide-drawers 'children)))))
  10938. (set-window-configuration org-log-note-window-configuration)
  10939. (with-current-buffer (marker-buffer org-log-note-return-to)
  10940. (goto-char org-log-note-return-to))
  10941. (move-marker org-log-note-return-to nil)
  10942. (and org-log-post-message (message "%s" org-log-post-message)))
  10943. (defun org-remove-empty-drawer-at (drawer pos)
  10944. "Remove an empty drawer DRAWER at position POS.
  10945. POS may also be a marker."
  10946. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10947. (save-excursion
  10948. (save-restriction
  10949. (widen)
  10950. (goto-char pos)
  10951. (if (org-in-regexp
  10952. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10953. (replace-match ""))))))
  10954. (defun org-sparse-tree (&optional arg)
  10955. "Create a sparse tree, prompt for the details.
  10956. This command can create sparse trees. You first need to select the type
  10957. of match used to create the tree:
  10958. t Show all TODO entries.
  10959. T Show entries with a specific TODO keyword.
  10960. m Show entries selected by a tags/property match.
  10961. p Enter a property name and its value (both with completion on existing
  10962. names/values) and show entries with that property.
  10963. r Show entries matching a regular expression (`/' can be used as well)
  10964. d Show deadlines due within `org-deadline-warning-days'.
  10965. b Show deadlines and scheduled items before a date.
  10966. a Show deadlines and scheduled items after a date."
  10967. (interactive "P")
  10968. (let (ans kwd value)
  10969. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  10970. (setq ans (read-char-exclusive))
  10971. (cond
  10972. ((equal ans ?d)
  10973. (call-interactively 'org-check-deadlines))
  10974. ((equal ans ?b)
  10975. (call-interactively 'org-check-before-date))
  10976. ((equal ans ?a)
  10977. (call-interactively 'org-check-after-date))
  10978. ((equal ans ?t)
  10979. (org-show-todo-tree nil))
  10980. ((equal ans ?T)
  10981. (org-show-todo-tree '(4)))
  10982. ((member ans '(?T ?m))
  10983. (call-interactively 'org-match-sparse-tree))
  10984. ((member ans '(?p ?P))
  10985. (setq kwd (org-icompleting-read "Property: "
  10986. (mapcar 'list (org-buffer-property-keys))))
  10987. (setq value (org-icompleting-read "Value: "
  10988. (mapcar 'list (org-property-values kwd))))
  10989. (unless (string-match "\\`{.*}\\'" value)
  10990. (setq value (concat "\"" value "\"")))
  10991. (org-match-sparse-tree arg (concat kwd "=" value)))
  10992. ((member ans '(?r ?R ?/))
  10993. (call-interactively 'org-occur))
  10994. (t (error "No such sparse tree command \"%c\"" ans)))))
  10995. (defvar org-occur-highlights nil
  10996. "List of overlays used for occur matches.")
  10997. (make-variable-buffer-local 'org-occur-highlights)
  10998. (defvar org-occur-parameters nil
  10999. "Parameters of the active org-occur calls.
  11000. This is a list, each call to org-occur pushes as cons cell,
  11001. containing the regular expression and the callback, onto the list.
  11002. The list can contain several entries if `org-occur' has been called
  11003. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11004. will only contain one set of parameters. When the highlights are
  11005. removed (for example with `C-c C-c', or with the next edit (depending
  11006. on `org-remove-highlights-with-change'), this variable is emptied
  11007. as well.")
  11008. (make-variable-buffer-local 'org-occur-parameters)
  11009. (defun org-occur (regexp &optional keep-previous callback)
  11010. "Make a compact tree which shows all matches of REGEXP.
  11011. The tree will show the lines where the regexp matches, and all higher
  11012. headlines above the match. It will also show the heading after the match,
  11013. to make sure editing the matching entry is easy.
  11014. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11015. call to `org-occur' will be kept, to allow stacking of calls to this
  11016. command.
  11017. If CALLBACK is non-nil, it is a function which is called to confirm
  11018. that the match should indeed be shown."
  11019. (interactive "sRegexp: \nP")
  11020. (when (equal regexp "")
  11021. (error "Regexp cannot be empty"))
  11022. (unless keep-previous
  11023. (org-remove-occur-highlights nil nil t))
  11024. (push (cons regexp callback) org-occur-parameters)
  11025. (let ((cnt 0))
  11026. (save-excursion
  11027. (goto-char (point-min))
  11028. (if (or (not keep-previous) ; do not want to keep
  11029. (not org-occur-highlights)) ; no previous matches
  11030. ;; hide everything
  11031. (org-overview))
  11032. (while (re-search-forward regexp nil t)
  11033. (when (or (not callback)
  11034. (save-match-data (funcall callback)))
  11035. (setq cnt (1+ cnt))
  11036. (when org-highlight-sparse-tree-matches
  11037. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11038. (org-show-context 'occur-tree))))
  11039. (when org-remove-highlights-with-change
  11040. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11041. nil 'local))
  11042. (unless org-sparse-tree-open-archived-trees
  11043. (org-hide-archived-subtrees (point-min) (point-max)))
  11044. (run-hooks 'org-occur-hook)
  11045. (if (interactive-p)
  11046. (message "%d match(es) for regexp %s" cnt regexp))
  11047. cnt))
  11048. (defun org-occur-next-match (&optional n reset)
  11049. "Function for `next-error-function' to find sparse tree matches.
  11050. N is the number of matches to move, when negative move backwards.
  11051. RESET is entirely ignored - this function always goes back to the
  11052. starting point when no match is found."
  11053. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11054. (search-func (if (< n 0)
  11055. 'previous-single-char-property-change
  11056. 'next-single-char-property-change))
  11057. (n (abs n))
  11058. (pos (point))
  11059. p1)
  11060. (catch 'exit
  11061. (while (setq p1 (funcall search-func (point) 'org-type))
  11062. (when (equal p1 limit)
  11063. (goto-char pos)
  11064. (error "No more matches"))
  11065. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11066. (setq n (1- n))
  11067. (when (= n 0)
  11068. (goto-char p1)
  11069. (throw 'exit (point))))
  11070. (goto-char p1))
  11071. (goto-char p1)
  11072. (error "No more matches"))))
  11073. (defun org-show-context (&optional key)
  11074. "Make sure point and context are visible.
  11075. How much context is shown depends upon the variables
  11076. `org-show-hierarchy-above', `org-show-following-heading'. and
  11077. `org-show-siblings'."
  11078. (let ((heading-p (org-on-heading-p t))
  11079. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11080. (following-p (org-get-alist-option org-show-following-heading key))
  11081. (entry-p (org-get-alist-option org-show-entry-below key))
  11082. (siblings-p (org-get-alist-option org-show-siblings key)))
  11083. (catch 'exit
  11084. ;; Show heading or entry text
  11085. (if (and heading-p (not entry-p))
  11086. (org-flag-heading nil) ; only show the heading
  11087. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11088. (org-show-hidden-entry))) ; show entire entry
  11089. (when following-p
  11090. ;; Show next sibling, or heading below text
  11091. (save-excursion
  11092. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11093. (org-flag-heading nil))))
  11094. (when siblings-p (org-show-siblings))
  11095. (when hierarchy-p
  11096. ;; show all higher headings, possibly with siblings
  11097. (save-excursion
  11098. (while (and (condition-case nil
  11099. (progn (org-up-heading-all 1) t)
  11100. (error nil))
  11101. (not (bobp)))
  11102. (org-flag-heading nil)
  11103. (when siblings-p (org-show-siblings))))))))
  11104. (defvar org-reveal-start-hook nil
  11105. "Hook run before revealing a location.")
  11106. (defun org-reveal (&optional siblings)
  11107. "Show current entry, hierarchy above it, and the following headline.
  11108. This can be used to show a consistent set of context around locations
  11109. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11110. not t for the search context.
  11111. With optional argument SIBLINGS, on each level of the hierarchy all
  11112. siblings are shown. This repairs the tree structure to what it would
  11113. look like when opened with hierarchical calls to `org-cycle'.
  11114. With double optional argument \\[universal-argument] \\[universal-argument], \
  11115. go to the parent and show the
  11116. entire tree."
  11117. (interactive "P")
  11118. (run-hooks 'org-reveal-start-hook)
  11119. (let ((org-show-hierarchy-above t)
  11120. (org-show-following-heading t)
  11121. (org-show-siblings (if siblings t org-show-siblings)))
  11122. (org-show-context nil))
  11123. (when (equal siblings '(16))
  11124. (save-excursion
  11125. (when (org-up-heading-safe)
  11126. (org-show-subtree)
  11127. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11128. (defun org-highlight-new-match (beg end)
  11129. "Highlight from BEG to END and mark the highlight is an occur headline."
  11130. (let ((ov (make-overlay beg end)))
  11131. (overlay-put ov 'face 'secondary-selection)
  11132. (overlay-put ov 'org-type 'org-occur)
  11133. (push ov org-occur-highlights)))
  11134. (defun org-remove-occur-highlights (&optional beg end noremove)
  11135. "Remove the occur highlights from the buffer.
  11136. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11137. from the `before-change-functions' in the current buffer."
  11138. (interactive)
  11139. (unless org-inhibit-highlight-removal
  11140. (mapc 'delete-overlay org-occur-highlights)
  11141. (setq org-occur-highlights nil)
  11142. (setq org-occur-parameters nil)
  11143. (unless noremove
  11144. (remove-hook 'before-change-functions
  11145. 'org-remove-occur-highlights 'local))))
  11146. ;;;; Priorities
  11147. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11148. "Regular expression matching the priority indicator.")
  11149. (defvar org-remove-priority-next-time nil)
  11150. (defun org-priority-up ()
  11151. "Increase the priority of the current item."
  11152. (interactive)
  11153. (org-priority 'up))
  11154. (defun org-priority-down ()
  11155. "Decrease the priority of the current item."
  11156. (interactive)
  11157. (org-priority 'down))
  11158. (defun org-priority (&optional action)
  11159. "Change the priority of an item by ARG.
  11160. ACTION can be `set', `up', `down', or a character."
  11161. (interactive)
  11162. (unless org-enable-priority-commands
  11163. (error "Priority commands are disabled"))
  11164. (setq action (or action 'set))
  11165. (let (current new news have remove)
  11166. (save-excursion
  11167. (org-back-to-heading t)
  11168. (if (looking-at org-priority-regexp)
  11169. (setq current (string-to-char (match-string 2))
  11170. have t)
  11171. (setq current org-default-priority))
  11172. (cond
  11173. ((eq action 'remove)
  11174. (setq remove t new ?\ ))
  11175. ((or (eq action 'set)
  11176. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11177. (if (not (eq action 'set))
  11178. (setq new action)
  11179. (message "Priority %c-%c, SPC to remove: "
  11180. org-highest-priority org-lowest-priority)
  11181. (save-match-data
  11182. (setq new (read-char-exclusive))))
  11183. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11184. (= (upcase org-lowest-priority) org-lowest-priority))
  11185. (setq new (upcase new)))
  11186. (cond ((equal new ?\ ) (setq remove t))
  11187. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11188. (error "Priority must be between `%c' and `%c'"
  11189. org-highest-priority org-lowest-priority))))
  11190. ((eq action 'up)
  11191. (if (and (not have) (eq last-command this-command))
  11192. (setq new org-lowest-priority)
  11193. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11194. org-default-priority (1- current)))))
  11195. ((eq action 'down)
  11196. (if (and (not have) (eq last-command this-command))
  11197. (setq new org-highest-priority)
  11198. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11199. org-default-priority (1+ current)))))
  11200. (t (error "Invalid action")))
  11201. (if (or (< (upcase new) org-highest-priority)
  11202. (> (upcase new) org-lowest-priority))
  11203. (setq remove t))
  11204. (setq news (format "%c" new))
  11205. (if have
  11206. (if remove
  11207. (replace-match "" t t nil 1)
  11208. (replace-match news t t nil 2))
  11209. (if remove
  11210. (error "No priority cookie found in line")
  11211. (let ((case-fold-search nil))
  11212. (looking-at org-todo-line-regexp))
  11213. (if (match-end 2)
  11214. (progn
  11215. (goto-char (match-end 2))
  11216. (insert " [#" news "]"))
  11217. (goto-char (match-beginning 3))
  11218. (insert "[#" news "] "))))
  11219. (org-preserve-lc (org-set-tags nil 'align)))
  11220. (if remove
  11221. (message "Priority removed")
  11222. (message "Priority of current item set to %s" news))))
  11223. (defun org-get-priority (s)
  11224. "Find priority cookie and return priority."
  11225. (if (functionp org-get-priority-function)
  11226. (funcall org-get-priority-function)
  11227. (save-match-data
  11228. (if (not (string-match org-priority-regexp s))
  11229. (* 1000 (- org-lowest-priority org-default-priority))
  11230. (* 1000 (- org-lowest-priority
  11231. (string-to-char (match-string 2 s))))))))
  11232. ;;;; Tags
  11233. (defvar org-agenda-archives-mode)
  11234. (defvar org-map-continue-from nil
  11235. "Position from where mapping should continue.
  11236. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11237. (defvar org-scanner-tags nil
  11238. "The current tag list while the tags scanner is running.")
  11239. (defvar org-trust-scanner-tags nil
  11240. "Should `org-get-tags-at' use the tags for the scanner.
  11241. This is for internal dynamical scoping only.
  11242. When this is non-nil, the function `org-get-tags-at' will return the value
  11243. of `org-scanner-tags' instead of building the list by itself. This
  11244. can lead to large speed-ups when the tags scanner is used in a file with
  11245. many entries, and when the list of tags is retrieved, for example to
  11246. obtain a list of properties. Building the tags list for each entry in such
  11247. a file becomes an N^2 operation - but with this variable set, it scales
  11248. as N.")
  11249. (defun org-scan-tags (action matcher &optional todo-only)
  11250. "Scan headline tags with inheritance and produce output ACTION.
  11251. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11252. or `agenda' to produce an entry list for an agenda view. It can also be
  11253. a Lisp form or a function that should be called at each matched headline, in
  11254. this case the return value is a list of all return values from these calls.
  11255. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11256. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11257. only lines with a TODO keyword are included in the output."
  11258. (require 'org-agenda)
  11259. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11260. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11261. (org-re
  11262. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11263. (props (list 'face 'default
  11264. 'done-face 'org-agenda-done
  11265. 'undone-face 'default
  11266. 'mouse-face 'highlight
  11267. 'org-not-done-regexp org-not-done-regexp
  11268. 'org-todo-regexp org-todo-regexp
  11269. 'help-echo
  11270. (format "mouse-2 or RET jump to org file %s"
  11271. (abbreviate-file-name
  11272. (or (buffer-file-name (buffer-base-buffer))
  11273. (buffer-name (buffer-base-buffer)))))))
  11274. (case-fold-search nil)
  11275. (org-map-continue-from nil)
  11276. lspos tags tags-list
  11277. (tags-alist (list (cons 0 org-file-tags)))
  11278. (llast 0) rtn rtn1 level category i txt
  11279. todo marker entry priority)
  11280. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11281. (setq action (list 'lambda nil action)))
  11282. (save-excursion
  11283. (goto-char (point-min))
  11284. (when (eq action 'sparse-tree)
  11285. (org-overview)
  11286. (org-remove-occur-highlights))
  11287. (while (re-search-forward re nil t)
  11288. (catch :skip
  11289. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11290. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11291. (goto-char (setq lspos (match-beginning 0)))
  11292. (setq level (org-reduced-level (funcall outline-level))
  11293. category (org-get-category))
  11294. (setq i llast llast level)
  11295. ;; remove tag lists from same and sublevels
  11296. (while (>= i level)
  11297. (when (setq entry (assoc i tags-alist))
  11298. (setq tags-alist (delete entry tags-alist)))
  11299. (setq i (1- i)))
  11300. ;; add the next tags
  11301. (when tags
  11302. (setq tags (org-split-string tags ":")
  11303. tags-alist
  11304. (cons (cons level tags) tags-alist)))
  11305. ;; compile tags for current headline
  11306. (setq tags-list
  11307. (if org-use-tag-inheritance
  11308. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11309. tags)
  11310. org-scanner-tags tags-list)
  11311. (when org-use-tag-inheritance
  11312. (setcdr (car tags-alist)
  11313. (mapcar (lambda (x)
  11314. (setq x (copy-sequence x))
  11315. (org-add-prop-inherited x))
  11316. (cdar tags-alist))))
  11317. (when (and tags org-use-tag-inheritance
  11318. (or (not (eq t org-use-tag-inheritance))
  11319. org-tags-exclude-from-inheritance))
  11320. ;; selective inheritance, remove uninherited ones
  11321. (setcdr (car tags-alist)
  11322. (org-remove-uninherited-tags (cdar tags-alist))))
  11323. (when (and
  11324. ;; eval matcher only when the todo condition is OK
  11325. (and (or (not todo-only) (member todo org-not-done-keywords))
  11326. (let ((case-fold-search t)) (eval matcher)))
  11327. ;; Call the skipper, but return t if it does not skip,
  11328. ;; so that the `and' form continues evaluating
  11329. (progn
  11330. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11331. t)
  11332. ;; Check if timestamps are deselecting this entry
  11333. (or (not todo-only)
  11334. (and (member todo org-not-done-keywords)
  11335. (or (not org-agenda-tags-todo-honor-ignore-options)
  11336. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11337. ;; Extra check for the archive tag
  11338. ;; FIXME: Does the skipper already do this????
  11339. (or
  11340. (not (member org-archive-tag tags-list))
  11341. ;; we have an archive tag, should we use this anyway?
  11342. (or (not org-agenda-skip-archived-trees)
  11343. (and (eq action 'agenda) org-agenda-archives-mode))))
  11344. ;; select this headline
  11345. (cond
  11346. ((eq action 'sparse-tree)
  11347. (and org-highlight-sparse-tree-matches
  11348. (org-get-heading) (match-end 0)
  11349. (org-highlight-new-match
  11350. (match-beginning 0) (match-beginning 1)))
  11351. (org-show-context 'tags-tree))
  11352. ((eq action 'agenda)
  11353. (setq txt (org-format-agenda-item
  11354. ""
  11355. (concat
  11356. (if (eq org-tags-match-list-sublevels 'indented)
  11357. (make-string (1- level) ?.) "")
  11358. (org-get-heading))
  11359. category
  11360. tags-list
  11361. )
  11362. priority (org-get-priority txt))
  11363. (goto-char lspos)
  11364. (setq marker (org-agenda-new-marker))
  11365. (org-add-props txt props
  11366. 'org-marker marker 'org-hd-marker marker 'org-category category
  11367. 'todo-state todo
  11368. 'priority priority 'type "tagsmatch")
  11369. (push txt rtn))
  11370. ((functionp action)
  11371. (setq org-map-continue-from nil)
  11372. (save-excursion
  11373. (setq rtn1 (funcall action))
  11374. (push rtn1 rtn)))
  11375. (t (error "Invalid action")))
  11376. ;; if we are to skip sublevels, jump to end of subtree
  11377. (unless org-tags-match-list-sublevels
  11378. (org-end-of-subtree t)
  11379. (backward-char 1))))
  11380. ;; Get the correct position from where to continue
  11381. (if org-map-continue-from
  11382. (goto-char org-map-continue-from)
  11383. (and (= (point) lspos) (end-of-line 1)))))
  11384. (when (and (eq action 'sparse-tree)
  11385. (not org-sparse-tree-open-archived-trees))
  11386. (org-hide-archived-subtrees (point-min) (point-max)))
  11387. (nreverse rtn)))
  11388. (defun org-remove-uninherited-tags (tags)
  11389. "Remove all tags that are not inherited from the list TAGS."
  11390. (cond
  11391. ((eq org-use-tag-inheritance t)
  11392. (if org-tags-exclude-from-inheritance
  11393. (org-delete-all org-tags-exclude-from-inheritance tags)
  11394. tags))
  11395. ((not org-use-tag-inheritance) nil)
  11396. ((stringp org-use-tag-inheritance)
  11397. (delq nil (mapcar
  11398. (lambda (x)
  11399. (if (and (string-match org-use-tag-inheritance x)
  11400. (not (member x org-tags-exclude-from-inheritance)))
  11401. x nil))
  11402. tags)))
  11403. ((listp org-use-tag-inheritance)
  11404. (delq nil (mapcar
  11405. (lambda (x)
  11406. (if (member x org-use-tag-inheritance) x nil))
  11407. tags)))))
  11408. (defvar todo-only) ;; dynamically scoped
  11409. (defun org-match-sparse-tree (&optional todo-only match)
  11410. "Create a sparse tree according to tags string MATCH.
  11411. MATCH can contain positive and negative selection of tags, like
  11412. \"+WORK+URGENT-WITHBOSS\".
  11413. If optional argument TODO-ONLY is non-nil, only select lines that are
  11414. also TODO lines."
  11415. (interactive "P")
  11416. (org-prepare-agenda-buffers (list (current-buffer)))
  11417. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11418. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11419. (defvar org-cached-props nil)
  11420. (defun org-cached-entry-get (pom property)
  11421. (if (or (eq t org-use-property-inheritance)
  11422. (and (stringp org-use-property-inheritance)
  11423. (string-match org-use-property-inheritance property))
  11424. (and (listp org-use-property-inheritance)
  11425. (member property org-use-property-inheritance)))
  11426. ;; Caching is not possible, check it directly
  11427. (org-entry-get pom property 'inherit)
  11428. ;; Get all properties, so that we can do complicated checks easily
  11429. (cdr (assoc property (or org-cached-props
  11430. (setq org-cached-props
  11431. (org-entry-properties pom)))))))
  11432. (defun org-global-tags-completion-table (&optional files)
  11433. "Return the list of all tags in all agenda buffer/files.
  11434. Optional FILES argument is a list of files to which can be used
  11435. instead of the agenda files."
  11436. (save-excursion
  11437. (org-uniquify
  11438. (delq nil
  11439. (apply 'append
  11440. (mapcar
  11441. (lambda (file)
  11442. (set-buffer (find-file-noselect file))
  11443. (append (org-get-buffer-tags)
  11444. (mapcar (lambda (x) (if (stringp (car-safe x))
  11445. (list (car-safe x)) nil))
  11446. org-tag-alist)))
  11447. (if (and files (car files))
  11448. files
  11449. (org-agenda-files))))))))
  11450. (defun org-make-tags-matcher (match)
  11451. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11452. ;; todo-only is scoped dynamically into this function, and the function
  11453. ;; may change it if the matcher asks for it.
  11454. (unless match
  11455. ;; Get a new match request, with completion
  11456. (let ((org-last-tags-completion-table
  11457. (org-global-tags-completion-table)))
  11458. (setq match (org-completing-read-no-i
  11459. "Match: " 'org-tags-completion-function nil nil nil
  11460. 'org-tags-history))))
  11461. ;; Parse the string and create a lisp form
  11462. (let ((match0 match)
  11463. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11464. minus tag mm
  11465. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11466. orterms term orlist re-p str-p level-p level-op time-p
  11467. prop-p pn pv po gv rest)
  11468. (if (string-match "/+" match)
  11469. ;; match contains also a todo-matching request
  11470. (progn
  11471. (setq tagsmatch (substring match 0 (match-beginning 0))
  11472. todomatch (substring match (match-end 0)))
  11473. (if (string-match "^!" todomatch)
  11474. (setq todo-only t todomatch (substring todomatch 1)))
  11475. (if (string-match "^\\s-*$" todomatch)
  11476. (setq todomatch nil)))
  11477. ;; only matching tags
  11478. (setq tagsmatch match todomatch nil))
  11479. ;; Make the tags matcher
  11480. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11481. (setq tagsmatcher t)
  11482. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11483. (while (setq term (pop orterms))
  11484. (while (and (equal (substring term -1) "\\") orterms)
  11485. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11486. (while (string-match re term)
  11487. (setq rest (substring term (match-end 0))
  11488. minus (and (match-end 1)
  11489. (equal (match-string 1 term) "-"))
  11490. tag (save-match-data (replace-regexp-in-string
  11491. "\\\\-" "-"
  11492. (match-string 2 term)))
  11493. re-p (equal (string-to-char tag) ?{)
  11494. level-p (match-end 4)
  11495. prop-p (match-end 5)
  11496. mm (cond
  11497. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11498. (level-p
  11499. (setq level-op (org-op-to-function (match-string 3 term)))
  11500. `(,level-op level ,(string-to-number
  11501. (match-string 4 term))))
  11502. (prop-p
  11503. (setq pn (match-string 5 term)
  11504. po (match-string 6 term)
  11505. pv (match-string 7 term)
  11506. re-p (equal (string-to-char pv) ?{)
  11507. str-p (equal (string-to-char pv) ?\")
  11508. time-p (save-match-data
  11509. (string-match "^\"[[<].*[]>]\"$" pv))
  11510. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11511. (if time-p (setq pv (org-matcher-time pv)))
  11512. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11513. (cond
  11514. ((equal pn "CATEGORY")
  11515. (setq gv '(get-text-property (point) 'org-category)))
  11516. ((equal pn "TODO")
  11517. (setq gv 'todo))
  11518. (t
  11519. (setq gv `(org-cached-entry-get nil ,pn))))
  11520. (if re-p
  11521. (if (eq po 'org<>)
  11522. `(not (string-match ,pv (or ,gv "")))
  11523. `(string-match ,pv (or ,gv "")))
  11524. (if str-p
  11525. `(,po (or ,gv "") ,pv)
  11526. `(,po (string-to-number (or ,gv ""))
  11527. ,(string-to-number pv) ))))
  11528. (t `(member ,tag tags-list)))
  11529. mm (if minus (list 'not mm) mm)
  11530. term rest)
  11531. (push mm tagsmatcher))
  11532. (push (if (> (length tagsmatcher) 1)
  11533. (cons 'and tagsmatcher)
  11534. (car tagsmatcher))
  11535. orlist)
  11536. (setq tagsmatcher nil))
  11537. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11538. (setq tagsmatcher
  11539. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11540. ;; Make the todo matcher
  11541. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11542. (setq todomatcher t)
  11543. (setq orterms (org-split-string todomatch "|") orlist nil)
  11544. (while (setq term (pop orterms))
  11545. (while (string-match re term)
  11546. (setq minus (and (match-end 1)
  11547. (equal (match-string 1 term) "-"))
  11548. kwd (match-string 2 term)
  11549. re-p (equal (string-to-char kwd) ?{)
  11550. term (substring term (match-end 0))
  11551. mm (if re-p
  11552. `(string-match ,(substring kwd 1 -1) todo)
  11553. (list 'equal 'todo kwd))
  11554. mm (if minus (list 'not mm) mm))
  11555. (push mm todomatcher))
  11556. (push (if (> (length todomatcher) 1)
  11557. (cons 'and todomatcher)
  11558. (car todomatcher))
  11559. orlist)
  11560. (setq todomatcher nil))
  11561. (setq todomatcher (if (> (length orlist) 1)
  11562. (cons 'or orlist) (car orlist))))
  11563. ;; Return the string and lisp forms of the matcher
  11564. (setq matcher (if todomatcher
  11565. (list 'and tagsmatcher todomatcher)
  11566. tagsmatcher))
  11567. (cons match0 matcher)))
  11568. (defun org-op-to-function (op &optional stringp)
  11569. "Turn an operator into the appropriate function."
  11570. (setq op
  11571. (cond
  11572. ((equal op "<" ) '(< string< org-time<))
  11573. ((equal op ">" ) '(> org-string> org-time>))
  11574. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11575. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11576. ((member op '("=" "==")) '(= string= org-time=))
  11577. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11578. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11579. (defun org<> (a b) (not (= a b)))
  11580. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11581. (defun org-string>= (a b) (not (string< a b)))
  11582. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11583. (defun org-string<> (a b) (not (string= a b)))
  11584. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11585. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11586. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11587. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11588. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11589. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11590. (defun org-2ft (s)
  11591. "Convert S to a floating point time.
  11592. If S is already a number, just return it. If it is a string, parse
  11593. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11594. (cond
  11595. ((numberp s) s)
  11596. ((stringp s)
  11597. (condition-case nil
  11598. (float-time (apply 'encode-time (org-parse-time-string s)))
  11599. (error 0.)))
  11600. (t 0.)))
  11601. (defun org-time-today ()
  11602. "Time in seconds today at 0:00.
  11603. Returns the float number of seconds since the beginning of the
  11604. epoch to the beginning of today (00:00)."
  11605. (float-time (apply 'encode-time
  11606. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11607. (defun org-matcher-time (s)
  11608. "Interpret a time comparison value."
  11609. (save-match-data
  11610. (cond
  11611. ((string= s "<now>") (float-time))
  11612. ((string= s "<today>") (org-time-today))
  11613. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11614. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11615. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11616. (+ (org-time-today)
  11617. (* (string-to-number (match-string 1 s))
  11618. (cdr (assoc (match-string 2 s)
  11619. '(("d" . 86400.0) ("w" . 604800.0)
  11620. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11621. (t (org-2ft s)))))
  11622. (defun org-match-any-p (re list)
  11623. "Does re match any element of list?"
  11624. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11625. (delq nil list))
  11626. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11627. (defvar org-tags-overlay (make-overlay 1 1))
  11628. (org-detach-overlay org-tags-overlay)
  11629. (defun org-get-local-tags-at (&optional pos)
  11630. "Get a list of tags defined in the current headline."
  11631. (org-get-tags-at pos 'local))
  11632. (defun org-get-local-tags ()
  11633. "Get a list of tags defined in the current headline."
  11634. (org-get-tags-at nil 'local))
  11635. (defun org-get-tags-at (&optional pos local)
  11636. "Get a list of all headline tags applicable at POS.
  11637. POS defaults to point. If tags are inherited, the list contains
  11638. the targets in the same sequence as the headlines appear, i.e.
  11639. the tags of the current headline come last.
  11640. When LOCAL is non-nil, only return tags from the current headline,
  11641. ignore inherited ones."
  11642. (interactive)
  11643. (if (and org-trust-scanner-tags
  11644. (or (not pos) (equal pos (point)))
  11645. (not local))
  11646. org-scanner-tags
  11647. (let (tags ltags lastpos parent)
  11648. (save-excursion
  11649. (save-restriction
  11650. (widen)
  11651. (goto-char (or pos (point)))
  11652. (save-match-data
  11653. (catch 'done
  11654. (condition-case nil
  11655. (progn
  11656. (org-back-to-heading t)
  11657. (while (not (equal lastpos (point)))
  11658. (setq lastpos (point))
  11659. (when (looking-at
  11660. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11661. (setq ltags (org-split-string
  11662. (org-match-string-no-properties 1) ":"))
  11663. (when parent
  11664. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11665. (setq tags (append
  11666. (if parent
  11667. (org-remove-uninherited-tags ltags)
  11668. ltags)
  11669. tags)))
  11670. (or org-use-tag-inheritance (throw 'done t))
  11671. (if local (throw 'done t))
  11672. (or (org-up-heading-safe) (error nil))
  11673. (setq parent t)))
  11674. (error nil)))))
  11675. (if local
  11676. tags
  11677. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11678. (defun org-add-prop-inherited (s)
  11679. (add-text-properties 0 (length s) '(inherited t) s)
  11680. s)
  11681. (defun org-toggle-tag (tag &optional onoff)
  11682. "Toggle the tag TAG for the current line.
  11683. If ONOFF is `on' or `off', don't toggle but set to this state."
  11684. (let (res current)
  11685. (save-excursion
  11686. (org-back-to-heading t)
  11687. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11688. (point-at-eol) t)
  11689. (progn
  11690. (setq current (match-string 1))
  11691. (replace-match ""))
  11692. (setq current ""))
  11693. (setq current (nreverse (org-split-string current ":")))
  11694. (cond
  11695. ((eq onoff 'on)
  11696. (setq res t)
  11697. (or (member tag current) (push tag current)))
  11698. ((eq onoff 'off)
  11699. (or (not (member tag current)) (setq current (delete tag current))))
  11700. (t (if (member tag current)
  11701. (setq current (delete tag current))
  11702. (setq res t)
  11703. (push tag current))))
  11704. (end-of-line 1)
  11705. (if current
  11706. (progn
  11707. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11708. (org-set-tags nil t))
  11709. (delete-horizontal-space))
  11710. (run-hooks 'org-after-tags-change-hook))
  11711. res))
  11712. (defun org-align-tags-here (to-col)
  11713. ;; Assumes that this is a headline
  11714. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11715. (beginning-of-line 1)
  11716. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11717. (< pos (match-beginning 2)))
  11718. (progn
  11719. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11720. (goto-char (match-beginning 1))
  11721. (insert " ")
  11722. (delete-region (point) (1+ (match-beginning 2)))
  11723. (setq ncol (max (1+ (current-column))
  11724. (1+ col)
  11725. (if (> to-col 0)
  11726. to-col
  11727. (- (abs to-col) tags-l))))
  11728. (setq p (point))
  11729. (insert (make-string (- ncol (current-column)) ?\ ))
  11730. (setq ncol (current-column))
  11731. (when indent-tabs-mode (tabify p (point-at-eol)))
  11732. (org-move-to-column (min ncol col) t))
  11733. (goto-char pos))))
  11734. (defun org-set-tags-command (&optional arg just-align)
  11735. "Call the set-tags command for the current entry."
  11736. (interactive "P")
  11737. (if (org-on-heading-p)
  11738. (org-set-tags arg just-align)
  11739. (save-excursion
  11740. (org-back-to-heading t)
  11741. (org-set-tags arg just-align))))
  11742. (defun org-set-tags-to (data)
  11743. "Set the tags of the current entry to DATA, replacing the current tags.
  11744. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11745. If DATA is nil or the empty string, any tags will be removed."
  11746. (interactive "sTags: ")
  11747. (setq data
  11748. (cond
  11749. ((eq data nil) "")
  11750. ((equal data "") "")
  11751. ((stringp data)
  11752. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11753. ":"))
  11754. ((listp data)
  11755. (concat ":" (mapconcat 'identity data ":") ":"))
  11756. (t nil)))
  11757. (when data
  11758. (save-excursion
  11759. (org-back-to-heading t)
  11760. (when (looking-at org-complex-heading-regexp)
  11761. (if (match-end 5)
  11762. (progn
  11763. (goto-char (match-beginning 5))
  11764. (insert data)
  11765. (delete-region (point) (point-at-eol))
  11766. (org-set-tags nil 'align))
  11767. (goto-char (point-at-eol))
  11768. (insert " " data)
  11769. (org-set-tags nil 'align)))
  11770. (beginning-of-line 1)
  11771. (if (looking-at ".*?\\([ \t]+\\)$")
  11772. (delete-region (match-beginning 1) (match-end 1))))))
  11773. (defun org-align-all-tags ()
  11774. "Align the tags i all headings."
  11775. (interactive)
  11776. (save-excursion
  11777. (or (ignore-errors (org-back-to-heading t))
  11778. (outline-next-heading))
  11779. (if (org-on-heading-p)
  11780. (org-set-tags t)
  11781. (message "No headings"))))
  11782. (defvar org-indent-indentation-per-level)
  11783. (defun org-set-tags (&optional arg just-align)
  11784. "Set the tags for the current headline.
  11785. With prefix ARG, realign all tags in headings in the current buffer."
  11786. (interactive "P")
  11787. (let* ((re (concat "^" outline-regexp))
  11788. (current (org-get-tags-string))
  11789. (col (current-column))
  11790. (org-setting-tags t)
  11791. table current-tags inherited-tags ; computed below when needed
  11792. tags p0 c0 c1 rpl di tc level)
  11793. (if arg
  11794. (save-excursion
  11795. (goto-char (point-min))
  11796. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11797. (while (re-search-forward re nil t)
  11798. (org-set-tags nil t)
  11799. (end-of-line 1)))
  11800. (message "All tags realigned to column %d" org-tags-column))
  11801. (if just-align
  11802. (setq tags current)
  11803. ;; Get a new set of tags from the user
  11804. (save-excursion
  11805. (setq table (append org-tag-persistent-alist
  11806. (or org-tag-alist (org-get-buffer-tags))
  11807. (and
  11808. org-complete-tags-always-offer-all-agenda-tags
  11809. (org-global-tags-completion-table
  11810. (org-agenda-files))))
  11811. org-last-tags-completion-table table
  11812. current-tags (org-split-string current ":")
  11813. inherited-tags (nreverse
  11814. (nthcdr (length current-tags)
  11815. (nreverse (org-get-tags-at))))
  11816. tags
  11817. (if (or (eq t org-use-fast-tag-selection)
  11818. (and org-use-fast-tag-selection
  11819. (delq nil (mapcar 'cdr table))))
  11820. (org-fast-tag-selection
  11821. current-tags inherited-tags table
  11822. (if org-fast-tag-selection-include-todo
  11823. org-todo-key-alist))
  11824. (let ((org-add-colon-after-tag-completion t))
  11825. (org-trim
  11826. (org-without-partial-completion
  11827. (org-icompleting-read "Tags: "
  11828. 'org-tags-completion-function
  11829. nil nil current 'org-tags-history)))))))
  11830. (while (string-match "[-+&]+" tags)
  11831. ;; No boolean logic, just a list
  11832. (setq tags (replace-match ":" t t tags))))
  11833. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  11834. (if org-tags-sort-function
  11835. (setq tags (mapconcat 'identity
  11836. (sort (org-split-string
  11837. tags (org-re "[^[:alnum:]_@#%]+"))
  11838. org-tags-sort-function) ":")))
  11839. (if (string-match "\\`[\t ]*\\'" tags)
  11840. (setq tags "")
  11841. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11842. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11843. ;; Insert new tags at the correct column
  11844. (beginning-of-line 1)
  11845. (setq level (or (and (looking-at org-outline-regexp)
  11846. (- (match-end 0) (point) 1))
  11847. 1))
  11848. (cond
  11849. ((and (equal current "") (equal tags "")))
  11850. ((re-search-forward
  11851. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11852. (point-at-eol) t)
  11853. (if (equal tags "")
  11854. (setq rpl "")
  11855. (goto-char (match-beginning 0))
  11856. (setq c0 (current-column)
  11857. ;; compute offset for the case of org-indent-mode active
  11858. di (if org-indent-mode
  11859. (* (1- org-indent-indentation-per-level) (1- level))
  11860. 0)
  11861. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11862. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11863. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11864. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11865. (replace-match rpl t t)
  11866. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11867. tags)
  11868. (t (error "Tags alignment failed")))
  11869. (org-move-to-column col)
  11870. (unless just-align
  11871. (run-hooks 'org-after-tags-change-hook)))))
  11872. (defun org-change-tag-in-region (beg end tag off)
  11873. "Add or remove TAG for each entry in the region.
  11874. This works in the agenda, and also in an org-mode buffer."
  11875. (interactive
  11876. (list (region-beginning) (region-end)
  11877. (let ((org-last-tags-completion-table
  11878. (if (org-mode-p)
  11879. (org-get-buffer-tags)
  11880. (org-global-tags-completion-table))))
  11881. (org-icompleting-read
  11882. "Tag: " 'org-tags-completion-function nil nil nil
  11883. 'org-tags-history))
  11884. (progn
  11885. (message "[s]et or [r]emove? ")
  11886. (equal (read-char-exclusive) ?r))))
  11887. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11888. (let ((agendap (equal major-mode 'org-agenda-mode))
  11889. l1 l2 m buf pos newhead (cnt 0))
  11890. (goto-char end)
  11891. (setq l2 (1- (org-current-line)))
  11892. (goto-char beg)
  11893. (setq l1 (org-current-line))
  11894. (loop for l from l1 to l2 do
  11895. (org-goto-line l)
  11896. (setq m (get-text-property (point) 'org-hd-marker))
  11897. (when (or (and (org-mode-p) (org-on-heading-p))
  11898. (and agendap m))
  11899. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11900. pos (if agendap m (point)))
  11901. (with-current-buffer buf
  11902. (save-excursion
  11903. (save-restriction
  11904. (goto-char pos)
  11905. (setq cnt (1+ cnt))
  11906. (org-toggle-tag tag (if off 'off 'on))
  11907. (setq newhead (org-get-heading)))))
  11908. (and agendap (org-agenda-change-all-lines newhead m))))
  11909. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11910. (defun org-tags-completion-function (string predicate &optional flag)
  11911. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11912. (confirm (lambda (x) (stringp (car x)))))
  11913. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11914. (setq s1 (match-string 1 string)
  11915. s2 (match-string 2 string))
  11916. (setq s1 "" s2 string))
  11917. (cond
  11918. ((eq flag nil)
  11919. ;; try completion
  11920. (setq rtn (try-completion s2 ctable confirm))
  11921. (if (stringp rtn)
  11922. (setq rtn
  11923. (concat s1 s2 (substring rtn (length s2))
  11924. (if (and org-add-colon-after-tag-completion
  11925. (assoc rtn ctable))
  11926. ":" ""))))
  11927. rtn)
  11928. ((eq flag t)
  11929. ;; all-completions
  11930. (all-completions s2 ctable confirm)
  11931. )
  11932. ((eq flag 'lambda)
  11933. ;; exact match?
  11934. (assoc s2 ctable)))
  11935. ))
  11936. (defun org-fast-tag-insert (kwd tags face &optional end)
  11937. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11938. (insert (format "%-12s" (concat kwd ":"))
  11939. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11940. (or end "")))
  11941. (defun org-fast-tag-show-exit (flag)
  11942. (save-excursion
  11943. (org-goto-line 3)
  11944. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11945. (replace-match ""))
  11946. (when flag
  11947. (end-of-line 1)
  11948. (org-move-to-column (- (window-width) 19) t)
  11949. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11950. (defun org-set-current-tags-overlay (current prefix)
  11951. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11952. (if (featurep 'xemacs)
  11953. (org-overlay-display org-tags-overlay (concat prefix s)
  11954. 'secondary-selection)
  11955. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11956. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11957. (defvar org-last-tag-selection-key nil)
  11958. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11959. "Fast tag selection with single keys.
  11960. CURRENT is the current list of tags in the headline, INHERITED is the
  11961. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11962. possibly with grouping information. TODO-TABLE is a similar table with
  11963. TODO keywords, should these have keys assigned to them.
  11964. If the keys are nil, a-z are automatically assigned.
  11965. Returns the new tags string, or nil to not change the current settings."
  11966. (let* ((fulltable (append table todo-table))
  11967. (maxlen (apply 'max (mapcar
  11968. (lambda (x)
  11969. (if (stringp (car x)) (string-width (car x)) 0))
  11970. fulltable)))
  11971. (buf (current-buffer))
  11972. (expert (eq org-fast-tag-selection-single-key 'expert))
  11973. (buffer-tags nil)
  11974. (fwidth (+ maxlen 3 1 3))
  11975. (ncol (/ (- (window-width) 4) fwidth))
  11976. (i-face 'org-done)
  11977. (c-face 'org-todo)
  11978. tg cnt e c char c1 c2 ntable tbl rtn
  11979. ov-start ov-end ov-prefix
  11980. (exit-after-next org-fast-tag-selection-single-key)
  11981. (done-keywords org-done-keywords)
  11982. groups ingroup)
  11983. (save-excursion
  11984. (beginning-of-line 1)
  11985. (if (looking-at
  11986. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11987. (setq ov-start (match-beginning 1)
  11988. ov-end (match-end 1)
  11989. ov-prefix "")
  11990. (setq ov-start (1- (point-at-eol))
  11991. ov-end (1+ ov-start))
  11992. (skip-chars-forward "^\n\r")
  11993. (setq ov-prefix
  11994. (concat
  11995. (buffer-substring (1- (point)) (point))
  11996. (if (> (current-column) org-tags-column)
  11997. " "
  11998. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11999. (move-overlay org-tags-overlay ov-start ov-end)
  12000. (save-window-excursion
  12001. (if expert
  12002. (set-buffer (get-buffer-create " *Org tags*"))
  12003. (delete-other-windows)
  12004. (split-window-vertically)
  12005. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12006. (erase-buffer)
  12007. (org-set-local 'org-done-keywords done-keywords)
  12008. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12009. (org-fast-tag-insert "Current" current c-face "\n\n")
  12010. (org-fast-tag-show-exit exit-after-next)
  12011. (org-set-current-tags-overlay current ov-prefix)
  12012. (setq tbl fulltable char ?a cnt 0)
  12013. (while (setq e (pop tbl))
  12014. (cond
  12015. ((equal (car e) :startgroup)
  12016. (push '() groups) (setq ingroup t)
  12017. (when (not (= cnt 0))
  12018. (setq cnt 0)
  12019. (insert "\n"))
  12020. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12021. ((equal (car e) :endgroup)
  12022. (setq ingroup nil cnt 0)
  12023. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12024. ((equal e '(:newline))
  12025. (when (not (= cnt 0))
  12026. (setq cnt 0)
  12027. (insert "\n")
  12028. (setq e (car tbl))
  12029. (while (equal (car tbl) '(:newline))
  12030. (insert "\n")
  12031. (setq tbl (cdr tbl)))))
  12032. (t
  12033. (setq tg (copy-sequence (car e)) c2 nil)
  12034. (if (cdr e)
  12035. (setq c (cdr e))
  12036. ;; automatically assign a character.
  12037. (setq c1 (string-to-char
  12038. (downcase (substring
  12039. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12040. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12041. (while (or (rassoc char ntable) (rassoc char table))
  12042. (setq char (1+ char)))
  12043. (setq c2 c1))
  12044. (setq c (or c2 char)))
  12045. (if ingroup (push tg (car groups)))
  12046. (setq tg (org-add-props tg nil 'face
  12047. (cond
  12048. ((not (assoc tg table))
  12049. (org-get-todo-face tg))
  12050. ((member tg current) c-face)
  12051. ((member tg inherited) i-face)
  12052. (t nil))))
  12053. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12054. (insert "[" c "] " tg (make-string
  12055. (- fwidth 4 (length tg)) ?\ ))
  12056. (push (cons tg c) ntable)
  12057. (when (= (setq cnt (1+ cnt)) ncol)
  12058. (insert "\n")
  12059. (if ingroup (insert " "))
  12060. (setq cnt 0)))))
  12061. (setq ntable (nreverse ntable))
  12062. (insert "\n")
  12063. (goto-char (point-min))
  12064. (if (not expert) (org-fit-window-to-buffer))
  12065. (setq rtn
  12066. (catch 'exit
  12067. (while t
  12068. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12069. (if (not groups) "no " "")
  12070. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12071. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12072. (setq org-last-tag-selection-key c)
  12073. (cond
  12074. ((= c ?\r) (throw 'exit t))
  12075. ((= c ?!)
  12076. (setq groups (not groups))
  12077. (goto-char (point-min))
  12078. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12079. ((= c ?\C-c)
  12080. (if (not expert)
  12081. (org-fast-tag-show-exit
  12082. (setq exit-after-next (not exit-after-next)))
  12083. (setq expert nil)
  12084. (delete-other-windows)
  12085. (set-window-buffer (split-window-vertically) " *Org tags*")
  12086. (org-switch-to-buffer-other-window " *Org tags*")
  12087. (org-fit-window-to-buffer)))
  12088. ((or (= c ?\C-g)
  12089. (and (= c ?q) (not (rassoc c ntable))))
  12090. (org-detach-overlay org-tags-overlay)
  12091. (setq quit-flag t))
  12092. ((= c ?\ )
  12093. (setq current nil)
  12094. (if exit-after-next (setq exit-after-next 'now)))
  12095. ((= c ?\t)
  12096. (condition-case nil
  12097. (setq tg (org-icompleting-read
  12098. "Tag: "
  12099. (or buffer-tags
  12100. (with-current-buffer buf
  12101. (org-get-buffer-tags)))))
  12102. (quit (setq tg "")))
  12103. (when (string-match "\\S-" tg)
  12104. (add-to-list 'buffer-tags (list tg))
  12105. (if (member tg current)
  12106. (setq current (delete tg current))
  12107. (push tg current)))
  12108. (if exit-after-next (setq exit-after-next 'now)))
  12109. ((setq e (rassoc c todo-table) tg (car e))
  12110. (with-current-buffer buf
  12111. (save-excursion (org-todo tg)))
  12112. (if exit-after-next (setq exit-after-next 'now)))
  12113. ((setq e (rassoc c ntable) tg (car e))
  12114. (if (member tg current)
  12115. (setq current (delete tg current))
  12116. (loop for g in groups do
  12117. (if (member tg g)
  12118. (mapc (lambda (x)
  12119. (setq current (delete x current)))
  12120. g)))
  12121. (push tg current))
  12122. (if exit-after-next (setq exit-after-next 'now))))
  12123. ;; Create a sorted list
  12124. (setq current
  12125. (sort current
  12126. (lambda (a b)
  12127. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12128. (if (eq exit-after-next 'now) (throw 'exit t))
  12129. (goto-char (point-min))
  12130. (beginning-of-line 2)
  12131. (delete-region (point) (point-at-eol))
  12132. (org-fast-tag-insert "Current" current c-face)
  12133. (org-set-current-tags-overlay current ov-prefix)
  12134. (while (re-search-forward
  12135. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12136. (setq tg (match-string 1))
  12137. (add-text-properties
  12138. (match-beginning 1) (match-end 1)
  12139. (list 'face
  12140. (cond
  12141. ((member tg current) c-face)
  12142. ((member tg inherited) i-face)
  12143. (t (get-text-property (match-beginning 1) 'face))))))
  12144. (goto-char (point-min)))))
  12145. (org-detach-overlay org-tags-overlay)
  12146. (if rtn
  12147. (mapconcat 'identity current ":")
  12148. nil))))
  12149. (defun org-get-tags-string ()
  12150. "Get the TAGS string in the current headline."
  12151. (unless (org-on-heading-p t)
  12152. (error "Not on a heading"))
  12153. (save-excursion
  12154. (beginning-of-line 1)
  12155. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12156. (org-match-string-no-properties 1)
  12157. "")))
  12158. (defun org-get-tags ()
  12159. "Get the list of tags specified in the current headline."
  12160. (org-split-string (org-get-tags-string) ":"))
  12161. (defun org-get-buffer-tags ()
  12162. "Get a table of all tags used in the buffer, for completion."
  12163. (let (tags)
  12164. (save-excursion
  12165. (goto-char (point-min))
  12166. (while (re-search-forward
  12167. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12168. (when (equal (char-after (point-at-bol 0)) ?*)
  12169. (mapc (lambda (x) (add-to-list 'tags x))
  12170. (org-split-string (org-match-string-no-properties 1) ":")))))
  12171. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12172. (mapcar 'list tags)))
  12173. ;;;; The mapping API
  12174. ;;;###autoload
  12175. (defun org-map-entries (func &optional match scope &rest skip)
  12176. "Call FUNC at each headline selected by MATCH in SCOPE.
  12177. FUNC is a function or a lisp form. The function will be called without
  12178. arguments, with the cursor positioned at the beginning of the headline.
  12179. The return values of all calls to the function will be collected and
  12180. returned as a list.
  12181. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12182. does not need to preserve point. After evaluation, the cursor will be
  12183. moved to the end of the line (presumably of the headline of the
  12184. processed entry) and search continues from there. Under some
  12185. circumstances, this may not produce the wanted results. For example,
  12186. if you have removed (e.g. archived) the current (sub)tree it could
  12187. mean that the next entry will be skipped entirely. In such cases, you
  12188. can specify the position from where search should continue by making
  12189. FUNC set the variable `org-map-continue-from' to the desired buffer
  12190. position.
  12191. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12192. Only headlines that are matched by this query will be considered during
  12193. the iteration. When MATCH is nil or t, all headlines will be
  12194. visited by the iteration.
  12195. SCOPE determines the scope of this command. It can be any of:
  12196. nil The current buffer, respecting the restriction if any
  12197. tree The subtree started with the entry at point
  12198. file The current buffer, without restriction
  12199. file-with-archives
  12200. The current buffer, and any archives associated with it
  12201. agenda All agenda files
  12202. agenda-with-archives
  12203. All agenda files with any archive files associated with them
  12204. \(file1 file2 ...)
  12205. If this is a list, all files in the list will be scanned
  12206. The remaining args are treated as settings for the skipping facilities of
  12207. the scanner. The following items can be given here:
  12208. archive skip trees with the archive tag.
  12209. comment skip trees with the COMMENT keyword
  12210. function or Emacs Lisp form:
  12211. will be used as value for `org-agenda-skip-function', so whenever
  12212. the function returns t, FUNC will not be called for that
  12213. entry and search will continue from the point where the
  12214. function leaves it.
  12215. If your function needs to retrieve the tags including inherited tags
  12216. at the *current* entry, you can use the value of the variable
  12217. `org-scanner-tags' which will be much faster than getting the value
  12218. with `org-get-tags-at'. If your function gets properties with
  12219. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12220. to t around the call to `org-entry-properties' to get the same speedup.
  12221. Note that if your function moves around to retrieve tags and properties at
  12222. a *different* entry, you cannot use these techniques."
  12223. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12224. (org-agenda-skip-archived-trees (memq 'archive skip))
  12225. (org-agenda-skip-comment-trees (memq 'comment skip))
  12226. (org-agenda-skip-function
  12227. (car (org-delete-all '(comment archive) skip)))
  12228. (org-tags-match-list-sublevels t)
  12229. matcher file res
  12230. org-todo-keywords-for-agenda
  12231. org-done-keywords-for-agenda
  12232. org-todo-keyword-alist-for-agenda
  12233. org-drawers-for-agenda
  12234. org-tag-alist-for-agenda)
  12235. (cond
  12236. ((eq match t) (setq matcher t))
  12237. ((eq match nil) (setq matcher t))
  12238. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12239. (save-excursion
  12240. (save-restriction
  12241. (when (eq scope 'tree)
  12242. (org-back-to-heading t)
  12243. (org-narrow-to-subtree)
  12244. (setq scope nil))
  12245. (if (not scope)
  12246. (progn
  12247. (org-prepare-agenda-buffers
  12248. (list (buffer-file-name (current-buffer))))
  12249. (setq res (org-scan-tags func matcher)))
  12250. ;; Get the right scope
  12251. (cond
  12252. ((and scope (listp scope) (symbolp (car scope)))
  12253. (setq scope (eval scope)))
  12254. ((eq scope 'agenda)
  12255. (setq scope (org-agenda-files t)))
  12256. ((eq scope 'agenda-with-archives)
  12257. (setq scope (org-agenda-files t))
  12258. (setq scope (org-add-archive-files scope)))
  12259. ((eq scope 'file)
  12260. (setq scope (list (buffer-file-name))))
  12261. ((eq scope 'file-with-archives)
  12262. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12263. (org-prepare-agenda-buffers scope)
  12264. (while (setq file (pop scope))
  12265. (with-current-buffer (org-find-base-buffer-visiting file)
  12266. (save-excursion
  12267. (save-restriction
  12268. (widen)
  12269. (goto-char (point-min))
  12270. (setq res (append res (org-scan-tags func matcher))))))))))
  12271. res))
  12272. ;;;; Properties
  12273. ;;; Setting and retrieving properties
  12274. (defconst org-special-properties
  12275. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12276. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12277. "The special properties valid in Org-mode.
  12278. These are properties that are not defined in the property drawer,
  12279. but in some other way.")
  12280. (defconst org-default-properties
  12281. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12282. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12283. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12284. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12285. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12286. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12287. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12288. "Some properties that are used by Org-mode for various purposes.
  12289. Being in this list makes sure that they are offered for completion.")
  12290. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12291. "Regular expression matching the first line of a property drawer.")
  12292. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12293. "Regular expression matching the last line of a property drawer.")
  12294. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12295. "Regular expression matching the first line of a property drawer.")
  12296. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12297. "Regular expression matching the first line of a property drawer.")
  12298. (defconst org-property-drawer-re
  12299. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12300. org-property-end-re "\\)\n?")
  12301. "Matches an entire property drawer.")
  12302. (defconst org-clock-drawer-re
  12303. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12304. org-property-end-re "\\)\n?")
  12305. "Matches an entire clock drawer.")
  12306. (defun org-property-action ()
  12307. "Do an action on properties."
  12308. (interactive)
  12309. (let (c)
  12310. (org-at-property-p)
  12311. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12312. (setq c (read-char-exclusive))
  12313. (cond
  12314. ((equal c ?s)
  12315. (call-interactively 'org-set-property))
  12316. ((equal c ?d)
  12317. (call-interactively 'org-delete-property))
  12318. ((equal c ?D)
  12319. (call-interactively 'org-delete-property-globally))
  12320. ((equal c ?c)
  12321. (call-interactively 'org-compute-property-at-point))
  12322. (t (error "No such property action %c" c)))))
  12323. (defun org-set-effort (&optional value)
  12324. "Set the effort property of the current entry.
  12325. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12326. allowed value."
  12327. (interactive "P")
  12328. (if (equal value 0) (setq value 10))
  12329. (let* ((completion-ignore-case t)
  12330. (prop org-effort-property)
  12331. (cur (org-entry-get nil prop))
  12332. (allowed (org-property-get-allowed-values nil prop 'table))
  12333. (existing (mapcar 'list (org-property-values prop)))
  12334. rpl
  12335. (val (cond
  12336. ((stringp value) value)
  12337. ((and allowed (integerp value))
  12338. (or (car (nth (1- value) allowed))
  12339. (car (org-last allowed))))
  12340. (allowed
  12341. (message "Select 1-9,0, [RET%s]: %s"
  12342. (if cur (concat "=" cur) "")
  12343. (mapconcat 'car allowed " "))
  12344. (setq rpl (read-char-exclusive))
  12345. (if (equal rpl ?\r)
  12346. cur
  12347. (setq rpl (- rpl ?0))
  12348. (if (equal rpl 0) (setq rpl 10))
  12349. (if (and (> rpl 0) (<= rpl (length allowed)))
  12350. (car (nth (1- rpl) allowed))
  12351. (org-completing-read "Effort: " allowed nil))))
  12352. (t
  12353. (let (org-completion-use-ido org-completion-use-iswitchb)
  12354. (org-completing-read
  12355. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12356. (concat "[" cur "]") "")
  12357. ": ")
  12358. existing nil nil "" nil cur))))))
  12359. (unless (equal (org-entry-get nil prop) val)
  12360. (org-entry-put nil prop val))
  12361. (message "%s is now %s" prop val)))
  12362. (defun org-at-property-p ()
  12363. "Is cursor inside a property drawer?"
  12364. (save-excursion
  12365. (beginning-of-line 1)
  12366. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12367. (save-match-data ;; Used by calling procedures
  12368. (let ((p (point))
  12369. (range (unless (org-before-first-heading-p)
  12370. (org-get-property-block))))
  12371. (and range (<= (car range) p) (< p (cdr range))))))))
  12372. (defun org-get-property-block (&optional beg end force)
  12373. "Return the (beg . end) range of the body of the property drawer.
  12374. BEG and END can be beginning and end of subtree, if not given
  12375. they will be found.
  12376. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12377. (catch 'exit
  12378. (save-excursion
  12379. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12380. (end (or end (progn (outline-next-heading) (point)))))
  12381. (goto-char beg)
  12382. (if (re-search-forward org-property-start-re end t)
  12383. (setq beg (1+ (match-end 0)))
  12384. (if force
  12385. (save-excursion
  12386. (org-insert-property-drawer)
  12387. (setq end (progn (outline-next-heading) (point))))
  12388. (throw 'exit nil))
  12389. (goto-char beg)
  12390. (if (re-search-forward org-property-start-re end t)
  12391. (setq beg (1+ (match-end 0)))))
  12392. (if (re-search-forward org-property-end-re end t)
  12393. (setq end (match-beginning 0))
  12394. (or force (throw 'exit nil))
  12395. (goto-char beg)
  12396. (setq end beg)
  12397. (org-indent-line-function)
  12398. (insert ":END:\n"))
  12399. (cons beg end)))))
  12400. (defun org-entry-properties (&optional pom which specific)
  12401. "Get all properties of the entry at point-or-marker POM.
  12402. This includes the TODO keyword, the tags, time strings for deadline,
  12403. scheduled, and clocking, and any additional properties defined in the
  12404. entry. The return value is an alist, keys may occur multiple times
  12405. if the property key was used several times.
  12406. POM may also be nil, in which case the current entry is used.
  12407. If WHICH is nil or `all', get all properties. If WHICH is
  12408. `special' or `standard', only get that subclass. If WHICH
  12409. is a string only get exactly this property. SPECIFIC can be a string, the
  12410. specific property we are interested in. Specifying it can speed
  12411. things up because then unnecessary parsing is avoided."
  12412. (setq which (or which 'all))
  12413. (org-with-point-at pom
  12414. (let ((clockstr (substring org-clock-string 0 -1))
  12415. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12416. (case-fold-search nil)
  12417. beg end range props sum-props key key1 value string clocksum)
  12418. (save-excursion
  12419. (when (condition-case nil
  12420. (and (org-mode-p) (org-back-to-heading t))
  12421. (error nil))
  12422. (setq beg (point))
  12423. (setq sum-props (get-text-property (point) 'org-summaries))
  12424. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12425. (outline-next-heading)
  12426. (setq end (point))
  12427. (when (memq which '(all special))
  12428. ;; Get the special properties, like TODO and tags
  12429. (goto-char beg)
  12430. (when (and (or (not specific) (string= specific "TODO"))
  12431. (looking-at org-todo-line-regexp) (match-end 2))
  12432. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12433. (when (and (or (not specific) (string= specific "PRIORITY"))
  12434. (looking-at org-priority-regexp))
  12435. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12436. (when (or (not specific) (string= specific "FILE"))
  12437. (push (cons "FILE" buffer-file-name) props))
  12438. (when (and (or (not specific) (string= specific "TAGS"))
  12439. (setq value (org-get-tags-string))
  12440. (string-match "\\S-" value))
  12441. (push (cons "TAGS" value) props))
  12442. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12443. (setq value (org-get-tags-at)))
  12444. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12445. ":"))
  12446. props))
  12447. (when (or (not specific) (string= specific "BLOCKED"))
  12448. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12449. (when (or (not specific)
  12450. (member specific
  12451. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12452. "TIMESTAMP" "TIMESTAMP_IA")))
  12453. (catch 'match
  12454. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12455. (setq key (if (match-end 1)
  12456. (substring (org-match-string-no-properties 1)
  12457. 0 -1))
  12458. string (if (equal key clockstr)
  12459. (org-no-properties
  12460. (org-trim
  12461. (buffer-substring
  12462. (match-beginning 3) (goto-char
  12463. (point-at-eol)))))
  12464. (substring (org-match-string-no-properties 3)
  12465. 1 -1)))
  12466. ;; Get the correct property name from the key. This is
  12467. ;; necessary if the user has configured time keywords.
  12468. (setq key1 (concat key ":"))
  12469. (cond
  12470. ((not key)
  12471. (setq key
  12472. (if (= (char-after (match-beginning 3)) ?\[)
  12473. "TIMESTAMP_IA" "TIMESTAMP")))
  12474. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12475. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12476. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12477. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12478. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12479. (progn
  12480. (push (cons key string) props)
  12481. ;; no need to search further if match is found
  12482. (throw 'match t))
  12483. (when (or (equal key "CLOCK") (not (assoc key props)))
  12484. (push (cons key string) props))))))
  12485. )
  12486. (when (memq which '(all standard))
  12487. ;; Get the standard properties, like :PROP: ...
  12488. (setq range (org-get-property-block beg end))
  12489. (when range
  12490. (goto-char (car range))
  12491. (while (re-search-forward
  12492. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12493. (cdr range) t)
  12494. (setq key (org-match-string-no-properties 1)
  12495. value (org-trim (or (org-match-string-no-properties 2) "")))
  12496. (unless (member key excluded)
  12497. (push (cons key (or value "")) props)))))
  12498. (if clocksum
  12499. (push (cons "CLOCKSUM"
  12500. (org-columns-number-to-string (/ (float clocksum) 60.)
  12501. 'add_times))
  12502. props))
  12503. (unless (assoc "CATEGORY" props)
  12504. (push (cons "CATEGORY" (org-get-category)) props))
  12505. (append sum-props (nreverse props)))))))
  12506. (defun org-entry-get (pom property &optional inherit literal-nil)
  12507. "Get value of PROPERTY for entry at point-or-marker POM.
  12508. If INHERIT is non-nil and the entry does not have the property,
  12509. then also check higher levels of the hierarchy.
  12510. If INHERIT is the symbol `selective', use inheritance only if the setting
  12511. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12512. If the property is present but empty, the return value is the empty string.
  12513. If the property is not present at all, nil is returned.
  12514. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12515. do not interpret it as the list atom nil. This is used for inheritance
  12516. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12517. (org-with-point-at pom
  12518. (if (and inherit (if (eq inherit 'selective)
  12519. (org-property-inherit-p property)
  12520. t))
  12521. (org-entry-get-with-inheritance property literal-nil)
  12522. (if (member property org-special-properties)
  12523. ;; We need a special property. Use `org-entry-properties' to
  12524. ;; retrieve it, but specify the wanted property
  12525. (cdr (assoc property (org-entry-properties nil 'special property)))
  12526. (let ((range (unless (org-before-first-heading-p)
  12527. (org-get-property-block))))
  12528. (if (and range
  12529. (goto-char (car range))
  12530. (re-search-forward
  12531. (org-re-property property)
  12532. (cdr range) t))
  12533. ;; Found the property, return it.
  12534. (if (match-end 1)
  12535. (if literal-nil
  12536. (org-match-string-no-properties 1)
  12537. (org-not-nil (org-match-string-no-properties 1)))
  12538. "")))))))
  12539. (defun org-property-or-variable-value (var &optional inherit)
  12540. "Check if there is a property fixing the value of VAR.
  12541. If yes, return this value. If not, return the current value of the variable."
  12542. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12543. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12544. (read prop)
  12545. (symbol-value var))))
  12546. (defun org-entry-delete (pom property)
  12547. "Delete the property PROPERTY from entry at point-or-marker POM."
  12548. (org-with-point-at pom
  12549. (if (member property org-special-properties)
  12550. nil ; cannot delete these properties.
  12551. (let ((range (org-get-property-block)))
  12552. (if (and range
  12553. (goto-char (car range))
  12554. (re-search-forward
  12555. (org-re-property property)
  12556. (cdr range) t))
  12557. (progn
  12558. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12559. t)
  12560. nil)))))
  12561. ;; Multi-values properties are properties that contain multiple values
  12562. ;; These values are assumed to be single words, separated by whitespace.
  12563. (defun org-entry-add-to-multivalued-property (pom property value)
  12564. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12565. (let* ((old (org-entry-get pom property))
  12566. (values (and old (org-split-string old "[ \t]"))))
  12567. (setq value (org-entry-protect-space value))
  12568. (unless (member value values)
  12569. (setq values (cons value values))
  12570. (org-entry-put pom property
  12571. (mapconcat 'identity values " ")))))
  12572. (defun org-entry-remove-from-multivalued-property (pom property value)
  12573. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12574. (let* ((old (org-entry-get pom property))
  12575. (values (and old (org-split-string old "[ \t]"))))
  12576. (setq value (org-entry-protect-space value))
  12577. (when (member value values)
  12578. (setq values (delete value values))
  12579. (org-entry-put pom property
  12580. (mapconcat 'identity values " ")))))
  12581. (defun org-entry-member-in-multivalued-property (pom property value)
  12582. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12583. (let* ((old (org-entry-get pom property))
  12584. (values (and old (org-split-string old "[ \t]"))))
  12585. (setq value (org-entry-protect-space value))
  12586. (member value values)))
  12587. (defun org-entry-get-multivalued-property (pom property)
  12588. "Return a list of values in a multivalued property."
  12589. (let* ((value (org-entry-get pom property))
  12590. (values (and value (org-split-string value "[ \t]"))))
  12591. (mapcar 'org-entry-restore-space values)))
  12592. (defun org-entry-put-multivalued-property (pom property &rest values)
  12593. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12594. VALUES should be a list of strings. Spaces will be protected."
  12595. (org-entry-put pom property
  12596. (mapconcat 'org-entry-protect-space values " "))
  12597. (let* ((value (org-entry-get pom property))
  12598. (values (and value (org-split-string value "[ \t]"))))
  12599. (mapcar 'org-entry-restore-space values)))
  12600. (defun org-entry-protect-space (s)
  12601. "Protect spaces and newline in string S."
  12602. (while (string-match " " s)
  12603. (setq s (replace-match "%20" t t s)))
  12604. (while (string-match "\n" s)
  12605. (setq s (replace-match "%0A" t t s)))
  12606. s)
  12607. (defun org-entry-restore-space (s)
  12608. "Restore spaces and newline in string S."
  12609. (while (string-match "%20" s)
  12610. (setq s (replace-match " " t t s)))
  12611. (while (string-match "%0A" s)
  12612. (setq s (replace-match "\n" t t s)))
  12613. s)
  12614. (defvar org-entry-property-inherited-from (make-marker)
  12615. "Marker pointing to the entry from where a property was inherited.
  12616. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12617. location of the entry where the inheritance search matched. If there was
  12618. no match, the marker will point nowhere.
  12619. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12620. is set.")
  12621. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12622. "Get entry property, and search higher levels if not present.
  12623. The search will stop at the first ancestor which has the property defined.
  12624. If the value found is \"nil\", return nil to show that the property
  12625. should be considered as undefined (this is the meaning of nil here).
  12626. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12627. (move-marker org-entry-property-inherited-from nil)
  12628. (let (tmp)
  12629. (unless (org-before-first-heading-p)
  12630. (save-excursion
  12631. (save-restriction
  12632. (widen)
  12633. (catch 'ex
  12634. (while t
  12635. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12636. (org-back-to-heading t)
  12637. (move-marker org-entry-property-inherited-from (point))
  12638. (throw 'ex tmp))
  12639. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12640. (setq tmp (or tmp
  12641. (cdr (assoc property org-file-properties))
  12642. (cdr (assoc property org-global-properties))
  12643. (cdr (assoc property org-global-properties-fixed))))
  12644. (if literal-nil tmp (org-not-nil tmp))))
  12645. (defvar org-property-changed-functions nil
  12646. "Hook called when the value of a property has changed.
  12647. Each hook function should accept two arguments, the name of the property
  12648. and the new value.")
  12649. (defun org-entry-put (pom property value)
  12650. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12651. (org-with-point-at pom
  12652. (org-back-to-heading t)
  12653. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12654. range)
  12655. (cond
  12656. ((equal property "TODO")
  12657. (when (and (stringp value) (string-match "\\S-" value)
  12658. (not (member value org-todo-keywords-1)))
  12659. (error "\"%s\" is not a valid TODO state" value))
  12660. (if (or (not value)
  12661. (not (string-match "\\S-" value)))
  12662. (setq value 'none))
  12663. (org-todo value)
  12664. (org-set-tags nil 'align))
  12665. ((equal property "PRIORITY")
  12666. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12667. (string-to-char value) ?\ ))
  12668. (org-set-tags nil 'align))
  12669. ((equal property "SCHEDULED")
  12670. (if (re-search-forward org-scheduled-time-regexp end t)
  12671. (cond
  12672. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12673. ((eq value 'later) (org-timestamp-change 1 'day))
  12674. (t (call-interactively 'org-schedule)))
  12675. (call-interactively 'org-schedule)))
  12676. ((equal property "DEADLINE")
  12677. (if (re-search-forward org-deadline-time-regexp end t)
  12678. (cond
  12679. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12680. ((eq value 'later) (org-timestamp-change 1 'day))
  12681. (t (call-interactively 'org-deadline)))
  12682. (call-interactively 'org-deadline)))
  12683. ((member property org-special-properties)
  12684. (error "The %s property can not yet be set with `org-entry-put'"
  12685. property))
  12686. (t ; a non-special property
  12687. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12688. (setq range (org-get-property-block beg end 'force))
  12689. (goto-char (car range))
  12690. (if (re-search-forward
  12691. (org-re-property property) (cdr range) t)
  12692. (progn
  12693. (delete-region (match-beginning 0) (match-end 0))
  12694. (goto-char (match-beginning 0)))
  12695. (goto-char (cdr range))
  12696. (insert "\n")
  12697. (backward-char 1)
  12698. (org-indent-line-function))
  12699. (insert ":" property ":")
  12700. (and value (insert " " value))
  12701. (org-indent-line-function)))))
  12702. (run-hook-with-args 'org-property-changed-functions property value)))
  12703. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12704. "Get all property keys in the current buffer.
  12705. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12706. like tags and TODO state.
  12707. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12708. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  12709. and others.
  12710. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12711. formats in the current buffer."
  12712. (let (rtn range cfmt s p)
  12713. (save-excursion
  12714. (save-restriction
  12715. (widen)
  12716. (goto-char (point-min))
  12717. (while (re-search-forward org-property-start-re nil t)
  12718. (setq range (org-get-property-block))
  12719. (goto-char (car range))
  12720. (while (re-search-forward
  12721. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12722. (cdr range) t)
  12723. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12724. (outline-next-heading))))
  12725. (when include-specials
  12726. (setq rtn (append org-special-properties rtn)))
  12727. (when include-defaults
  12728. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12729. (add-to-list 'rtn org-effort-property))
  12730. (when include-columns
  12731. (save-excursion
  12732. (save-restriction
  12733. (widen)
  12734. (goto-char (point-min))
  12735. (while (re-search-forward
  12736. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12737. nil t)
  12738. (setq cfmt (match-string 2) s 0)
  12739. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12740. cfmt s)
  12741. (setq s (match-end 0)
  12742. p (match-string 1 cfmt))
  12743. (unless (or (equal p "ITEM")
  12744. (member p org-special-properties))
  12745. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12746. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12747. (defsubst org-re-property (property)
  12748. "Return a regexp matching PROPERTY.
  12749. Match group 1 will be set to the value "
  12750. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12751. (defun org-property-values (key)
  12752. "Return a list of all values of property KEY in the current buffer."
  12753. (save-excursion
  12754. (save-restriction
  12755. (widen)
  12756. (goto-char (point-min))
  12757. (let ((re (org-re-property key))
  12758. values)
  12759. (while (re-search-forward re nil t)
  12760. (add-to-list 'values (org-trim (match-string 1))))
  12761. (delete "" values)))))
  12762. (defun org-insert-property-drawer ()
  12763. "Insert a property drawer into the current entry."
  12764. (interactive)
  12765. (org-back-to-heading t)
  12766. (looking-at outline-regexp)
  12767. (let ((indent (if org-adapt-indentation
  12768. (- (match-end 0)(match-beginning 0))
  12769. 0))
  12770. (beg (point))
  12771. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12772. end hiddenp)
  12773. (outline-next-heading)
  12774. (setq end (point))
  12775. (goto-char beg)
  12776. (while (re-search-forward re end t))
  12777. (setq hiddenp (outline-invisible-p))
  12778. (end-of-line 1)
  12779. (and (equal (char-after) ?\n) (forward-char 1))
  12780. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12781. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12782. ;; just skip this line
  12783. (beginning-of-line 2)
  12784. ;; Drawer start, find the end
  12785. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12786. (beginning-of-line 1)))
  12787. (org-skip-over-state-notes)
  12788. (skip-chars-backward " \t\n\r")
  12789. (if (eq (char-before) ?*) (forward-char 1))
  12790. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12791. (beginning-of-line 0)
  12792. (org-indent-to-column indent)
  12793. (beginning-of-line 2)
  12794. (org-indent-to-column indent)
  12795. (beginning-of-line 0)
  12796. (if hiddenp
  12797. (save-excursion
  12798. (org-back-to-heading t)
  12799. (hide-entry))
  12800. (org-flag-drawer t))))
  12801. (defvar org-property-set-functions-alist nil
  12802. "Property set function alist.
  12803. Each entry should have the following format:
  12804. (PROPERTY . READ-FUNCTION)
  12805. The read function will be called with the same argument as
  12806. `org-completing-read'.")
  12807. (defun org-set-property-function (property)
  12808. "Get the function that should be used to set PROPERTY.
  12809. This is computed according to `org-property-set-functions-alist'."
  12810. (or (cdr (assoc property org-property-set-functions-alist))
  12811. 'org-completing-read))
  12812. (defun org-read-property-value (property)
  12813. "Read PROPERTY value from user."
  12814. (let* ((completion-ignore-case t)
  12815. (allowed (org-property-get-allowed-values nil property 'table))
  12816. (cur (org-entry-get nil property))
  12817. (prompt (concat property " value"
  12818. (if (and cur (string-match "\\S-" cur))
  12819. (concat " [" cur "]") "") ": "))
  12820. (set-function (org-set-property-function property))
  12821. (val (if allowed
  12822. (funcall set-function prompt allowed nil
  12823. (not (get-text-property 0 'org-unrestricted
  12824. (caar allowed))))
  12825. (let (org-completion-use-ido org-completion-use-iswitchb)
  12826. (funcall set-function prompt
  12827. (mapcar 'list (org-property-values property))
  12828. nil nil "" nil cur)))))
  12829. (if (equal val "")
  12830. cur
  12831. val)))
  12832. (defun org-read-property-name ()
  12833. "Read a property name."
  12834. (let* ((completion-ignore-case t)
  12835. (keys (org-buffer-property-keys nil t t))
  12836. (property (org-icompleting-read "Property: " (mapcar 'list keys))))
  12837. (if (member property keys)
  12838. property
  12839. (or (cdr (assoc (downcase property)
  12840. (mapcar (lambda (x) (cons (downcase x) x))
  12841. keys)))
  12842. property))))
  12843. (defun org-set-property (property value)
  12844. "In the current entry, set PROPERTY to VALUE.
  12845. When called interactively, this will prompt for a property name, offering
  12846. completion on existing and default properties. And then it will prompt
  12847. for a value, offering completion either on allowed values (via an inherited
  12848. xxx_ALL property) or on existing values in other instances of this property
  12849. in the current file."
  12850. (interactive (list nil nil))
  12851. (let* ((property (or property (org-read-property-name)))
  12852. (value (or value (org-read-property-value property))))
  12853. (unless (equal (org-entry-get nil property) value)
  12854. (org-entry-put nil property value))))
  12855. (defun org-delete-property (property)
  12856. "In the current entry, delete PROPERTY."
  12857. (interactive
  12858. (let* ((completion-ignore-case t)
  12859. (prop (org-icompleting-read "Property: "
  12860. (org-entry-properties nil 'standard))))
  12861. (list prop)))
  12862. (message "Property %s %s" property
  12863. (if (org-entry-delete nil property)
  12864. "deleted"
  12865. "was not present in the entry")))
  12866. (defun org-delete-property-globally (property)
  12867. "Remove PROPERTY globally, from all entries."
  12868. (interactive
  12869. (let* ((completion-ignore-case t)
  12870. (prop (org-icompleting-read
  12871. "Globally remove property: "
  12872. (mapcar 'list (org-buffer-property-keys)))))
  12873. (list prop)))
  12874. (save-excursion
  12875. (save-restriction
  12876. (widen)
  12877. (goto-char (point-min))
  12878. (let ((cnt 0))
  12879. (while (re-search-forward
  12880. (org-re-property property)
  12881. nil t)
  12882. (setq cnt (1+ cnt))
  12883. (replace-match ""))
  12884. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12885. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12886. (defun org-compute-property-at-point ()
  12887. "Compute the property at point.
  12888. This looks for an enclosing column format, extracts the operator and
  12889. then applies it to the property in the column format's scope."
  12890. (interactive)
  12891. (unless (org-at-property-p)
  12892. (error "Not at a property"))
  12893. (let ((prop (org-match-string-no-properties 2)))
  12894. (org-columns-get-format-and-top-level)
  12895. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12896. (error "No operator defined for property %s" prop))
  12897. (org-columns-compute prop)))
  12898. (defvar org-property-allowed-value-functions nil
  12899. "Hook for functions supplying allowed values for a specific property.
  12900. The functions must take a single argument, the name of the property, and
  12901. return a flat list of allowed values. If \":ETC\" is one of
  12902. the values, this means that these values are intended as defaults for
  12903. completion, but that other values should be allowed too.
  12904. The functions must return nil if they are not responsible for this
  12905. property.")
  12906. (defun org-property-get-allowed-values (pom property &optional table)
  12907. "Get allowed values for the property PROPERTY.
  12908. When TABLE is non-nil, return an alist that can directly be used for
  12909. completion."
  12910. (let (vals)
  12911. (cond
  12912. ((equal property "TODO")
  12913. (setq vals (org-with-point-at pom
  12914. (append org-todo-keywords-1 '("")))))
  12915. ((equal property "PRIORITY")
  12916. (let ((n org-lowest-priority))
  12917. (while (>= n org-highest-priority)
  12918. (push (char-to-string n) vals)
  12919. (setq n (1- n)))))
  12920. ((member property org-special-properties))
  12921. ((setq vals (run-hook-with-args-until-success
  12922. 'org-property-allowed-value-functions property)))
  12923. (t
  12924. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12925. (when (and vals (string-match "\\S-" vals))
  12926. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12927. (setq vals (mapcar (lambda (x)
  12928. (cond ((stringp x) x)
  12929. ((numberp x) (number-to-string x))
  12930. ((symbolp x) (symbol-name x))
  12931. (t "???")))
  12932. vals)))))
  12933. (when (member ":ETC" vals)
  12934. (setq vals (remove ":ETC" vals))
  12935. (org-add-props (car vals) '(org-unrestricted t)))
  12936. (if table (mapcar 'list vals) vals)))
  12937. (defun org-property-previous-allowed-value (&optional previous)
  12938. "Switch to the next allowed value for this property."
  12939. (interactive)
  12940. (org-property-next-allowed-value t))
  12941. (defun org-property-next-allowed-value (&optional previous)
  12942. "Switch to the next allowed value for this property."
  12943. (interactive)
  12944. (unless (org-at-property-p)
  12945. (error "Not at a property"))
  12946. (let* ((key (match-string 2))
  12947. (value (match-string 3))
  12948. (allowed (or (org-property-get-allowed-values (point) key)
  12949. (and (member value '("[ ]" "[-]" "[X]"))
  12950. '("[ ]" "[X]"))))
  12951. nval)
  12952. (unless allowed
  12953. (error "Allowed values for this property have not been defined"))
  12954. (if previous (setq allowed (reverse allowed)))
  12955. (if (member value allowed)
  12956. (setq nval (car (cdr (member value allowed)))))
  12957. (setq nval (or nval (car allowed)))
  12958. (if (equal nval value)
  12959. (error "Only one allowed value for this property"))
  12960. (org-at-property-p)
  12961. (replace-match (concat " :" key ": " nval) t t)
  12962. (org-indent-line-function)
  12963. (beginning-of-line 1)
  12964. (skip-chars-forward " \t")
  12965. (run-hook-with-args 'org-property-changed-functions key nval)))
  12966. (defun org-find-olp (path &optional this-buffer)
  12967. "Return a marker pointing to the entry at outline path OLP.
  12968. If anything goes wrong, throw an error.
  12969. You can wrap this call to catch the error like this:
  12970. (condition-case msg
  12971. (org-mobile-locate-entry (match-string 4))
  12972. (error (nth 1 msg)))
  12973. The return value will then be either a string with the error message,
  12974. or a marker if everything is OK.
  12975. If THIS-BUFFER is set, the outline path does not contain a file,
  12976. only headings."
  12977. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12978. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12979. (level 1)
  12980. (lmin 1)
  12981. (lmax 1)
  12982. limit re end found pos heading cnt flevel)
  12983. (unless buffer (error "File not found :%s" file))
  12984. (with-current-buffer buffer
  12985. (save-excursion
  12986. (save-restriction
  12987. (widen)
  12988. (setq limit (point-max))
  12989. (goto-char (point-min))
  12990. (while (setq heading (pop path))
  12991. (setq re (format org-complex-heading-regexp-format
  12992. (regexp-quote heading)))
  12993. (setq cnt 0 pos (point))
  12994. (while (re-search-forward re end t)
  12995. (setq level (- (match-end 1) (match-beginning 1)))
  12996. (if (and (>= level lmin) (<= level lmax))
  12997. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  12998. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12999. lmax heading))
  13000. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13001. lmax heading))
  13002. (goto-char found)
  13003. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13004. (setq end (save-excursion (org-end-of-subtree t t))))
  13005. (when (org-on-heading-p)
  13006. (move-marker (make-marker) (point))))))))
  13007. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13008. "Find node HEADING in BUFFER.
  13009. Return a marker to the heading if it was found, or nil if not.
  13010. If POS-ONLY is set, return just the position instead of a marker.
  13011. The heading text must match exact, but it may have a TODO keyword,
  13012. a priority cookie and tags in the standard locations."
  13013. (with-current-buffer (or buffer (current-buffer))
  13014. (save-excursion
  13015. (save-restriction
  13016. (widen)
  13017. (goto-char (point-min))
  13018. (let (case-fold-search)
  13019. (if (re-search-forward
  13020. (format org-complex-heading-regexp-format
  13021. (regexp-quote heading)) nil t)
  13022. (if pos-only
  13023. (match-beginning 0)
  13024. (move-marker (make-marker) (match-beginning 0)))))))))
  13025. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13026. "Find Org node headline HEADING in all .org files in directory DIR.
  13027. When the target headline is found, return a marker to this location."
  13028. (let ((files (directory-files (or dir default-directory)
  13029. nil "\\`[^.#].*\\.org\\'"))
  13030. file visiting m buffer)
  13031. (catch 'found
  13032. (while (setq file (pop files))
  13033. (message "trying %s" file)
  13034. (setq visiting (org-find-base-buffer-visiting file))
  13035. (setq buffer (or visiting (find-file-noselect file)))
  13036. (setq m (org-find-exact-headline-in-buffer
  13037. heading buffer))
  13038. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13039. (and m (throw 'found m))))))
  13040. (defun org-find-entry-with-id (ident)
  13041. "Locate the entry that contains the ID property with exact value IDENT.
  13042. IDENT can be a string, a symbol or a number, this function will search for
  13043. the string representation of it.
  13044. Return the position where this entry starts, or nil if there is no such entry."
  13045. (interactive "sID: ")
  13046. (let ((id (cond
  13047. ((stringp ident) ident)
  13048. ((symbol-name ident) (symbol-name ident))
  13049. ((numberp ident) (number-to-string ident))
  13050. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13051. (case-fold-search nil))
  13052. (save-excursion
  13053. (save-restriction
  13054. (widen)
  13055. (goto-char (point-min))
  13056. (when (re-search-forward
  13057. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13058. nil t)
  13059. (org-back-to-heading t)
  13060. (point))))))
  13061. ;;;; Timestamps
  13062. (defvar org-last-changed-timestamp nil)
  13063. (defvar org-last-inserted-timestamp nil
  13064. "The last time stamp inserted with `org-insert-time-stamp'.")
  13065. (defvar org-time-was-given) ; dynamically scoped parameter
  13066. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13067. (defvar org-ts-what) ; dynamically scoped parameter
  13068. (defun org-time-stamp (arg &optional inactive)
  13069. "Prompt for a date/time and insert a time stamp.
  13070. If the user specifies a time like HH:MM, or if this command is called
  13071. with a prefix argument, the time stamp will contain date and time.
  13072. Otherwise, only the date will be included. All parts of a date not
  13073. specified by the user will be filled in from the current date/time.
  13074. So if you press just return without typing anything, the time stamp
  13075. will represent the current date/time. If there is already a timestamp
  13076. at the cursor, it will be modified."
  13077. (interactive "P")
  13078. (let* ((ts nil)
  13079. (default-time
  13080. ;; Default time is either today, or, when entering a range,
  13081. ;; the range start.
  13082. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13083. (save-excursion
  13084. (re-search-backward
  13085. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13086. (- (point) 20) t)))
  13087. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13088. (current-time)))
  13089. (default-input (and ts (org-get-compact-tod ts)))
  13090. org-time-was-given org-end-time-was-given time)
  13091. (cond
  13092. ((and (org-at-timestamp-p t)
  13093. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13094. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13095. (insert "--")
  13096. (setq time (let ((this-command this-command))
  13097. (org-read-date arg 'totime nil nil
  13098. default-time default-input)))
  13099. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13100. ((org-at-timestamp-p t)
  13101. (setq time (let ((this-command this-command))
  13102. (org-read-date arg 'totime nil nil default-time default-input)))
  13103. (when (org-at-timestamp-p t) ; just to get the match data
  13104. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13105. (replace-match "")
  13106. (setq org-last-changed-timestamp
  13107. (org-insert-time-stamp
  13108. time (or org-time-was-given arg)
  13109. inactive nil nil (list org-end-time-was-given))))
  13110. (message "Timestamp updated"))
  13111. (t
  13112. (setq time (let ((this-command this-command))
  13113. (org-read-date arg 'totime nil nil default-time default-input)))
  13114. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13115. nil nil (list org-end-time-was-given))))))
  13116. ;; FIXME: can we use this for something else, like computing time differences?
  13117. (defun org-get-compact-tod (s)
  13118. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13119. (let* ((t1 (match-string 1 s))
  13120. (h1 (string-to-number (match-string 2 s)))
  13121. (m1 (string-to-number (match-string 3 s)))
  13122. (t2 (and (match-end 4) (match-string 5 s)))
  13123. (h2 (and t2 (string-to-number (match-string 6 s))))
  13124. (m2 (and t2 (string-to-number (match-string 7 s))))
  13125. dh dm)
  13126. (if (not t2)
  13127. t1
  13128. (setq dh (- h2 h1) dm (- m2 m1))
  13129. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13130. (concat t1 "+" (number-to-string dh)
  13131. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13132. (defun org-time-stamp-inactive (&optional arg)
  13133. "Insert an inactive time stamp.
  13134. An inactive time stamp is enclosed in square brackets instead of angle
  13135. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13136. does not link to the calendar and cannot be changed with the S-cursor keys.
  13137. So these are more for recording a certain time/date."
  13138. (interactive "P")
  13139. (org-time-stamp arg 'inactive))
  13140. (defvar org-date-ovl (make-overlay 1 1))
  13141. (overlay-put org-date-ovl 'face 'org-warning)
  13142. (org-detach-overlay org-date-ovl)
  13143. (defvar org-ans1) ; dynamically scoped parameter
  13144. (defvar org-ans2) ; dynamically scoped parameter
  13145. (defvar org-plain-time-of-day-regexp) ; defined below
  13146. (defvar org-overriding-default-time nil) ; dynamically scoped
  13147. (defvar org-read-date-overlay nil)
  13148. (defvar org-dcst nil) ; dynamically scoped
  13149. (defvar org-read-date-history nil)
  13150. (defvar org-read-date-final-answer nil)
  13151. (defvar org-read-date-analyze-futurep nil)
  13152. (defvar org-read-date-analyze-forced-year nil)
  13153. (defun org-read-date (&optional with-time to-time from-string prompt
  13154. default-time default-input)
  13155. "Read a date, possibly a time, and make things smooth for the user.
  13156. The prompt will suggest to enter an ISO date, but you can also enter anything
  13157. which will at least partially be understood by `parse-time-string'.
  13158. Unrecognized parts of the date will default to the current day, month, year,
  13159. hour and minute. If this command is called to replace a timestamp at point,
  13160. of to enter the second timestamp of a range, the default time is taken
  13161. from the existing stamp. Furthermore, the command prefers the future,
  13162. so if you are giving a date where the year is not given, and the day-month
  13163. combination is already past in the current year, it will assume you
  13164. mean next year. For details, see the manual. A few examples:
  13165. 3-2-5 --> 2003-02-05
  13166. feb 15 --> currentyear-02-15
  13167. 2/15 --> currentyear-02-15
  13168. sep 12 9 --> 2009-09-12
  13169. 12:45 --> today 12:45
  13170. 22 sept 0:34 --> currentyear-09-22 0:34
  13171. 12 --> currentyear-currentmonth-12
  13172. Fri --> nearest Friday (today or later)
  13173. etc.
  13174. Furthermore you can specify a relative date by giving, as the *first* thing
  13175. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13176. change in days weeks, months, years.
  13177. With a single plus or minus, the date is relative to today. With a double
  13178. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13179. +4d --> four days from today
  13180. +4 --> same as above
  13181. +2w --> two weeks from today
  13182. ++5 --> five days from default date
  13183. The function understands only English month and weekday abbreviations,
  13184. but this can be configured with the variables `parse-time-months' and
  13185. `parse-time-weekdays'.
  13186. While prompting, a calendar is popped up - you can also select the
  13187. date with the mouse (button 1). The calendar shows a period of three
  13188. months. To scroll it to other months, use the keys `>' and `<'.
  13189. If you don't like the calendar, turn it off with
  13190. \(setq org-read-date-popup-calendar nil)
  13191. With optional argument TO-TIME, the date will immediately be converted
  13192. to an internal time.
  13193. With an optional argument WITH-TIME, the prompt will suggest to also
  13194. insert a time. Note that when WITH-TIME is not set, you can still
  13195. enter a time, and this function will inform the calling routine about
  13196. this change. The calling routine may then choose to change the format
  13197. used to insert the time stamp into the buffer to include the time.
  13198. With optional argument FROM-STRING, read from this string instead from
  13199. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13200. the time/date that is used for everything that is not specified by the
  13201. user."
  13202. (require 'parse-time)
  13203. (let* ((org-time-stamp-rounding-minutes
  13204. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13205. (org-dcst org-display-custom-times)
  13206. (ct (org-current-time))
  13207. (def (or org-overriding-default-time default-time ct))
  13208. (defdecode (decode-time def))
  13209. (dummy (progn
  13210. (when (< (nth 2 defdecode) org-extend-today-until)
  13211. (setcar (nthcdr 2 defdecode) -1)
  13212. (setcar (nthcdr 1 defdecode) 59)
  13213. (setq def (apply 'encode-time defdecode)
  13214. defdecode (decode-time def)))))
  13215. (calendar-frame-setup nil)
  13216. (calendar-setup nil)
  13217. (calendar-move-hook nil)
  13218. (calendar-view-diary-initially-flag nil)
  13219. (calendar-view-holidays-initially-flag nil)
  13220. (timestr (format-time-string
  13221. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13222. (prompt (concat (if prompt (concat prompt " ") "")
  13223. (format "Date+time [%s]: " timestr)))
  13224. ans (org-ans0 "") org-ans1 org-ans2 final)
  13225. (cond
  13226. (from-string (setq ans from-string))
  13227. (org-read-date-popup-calendar
  13228. (save-excursion
  13229. (save-window-excursion
  13230. (calendar)
  13231. (unwind-protect
  13232. (progn
  13233. (calendar-forward-day (- (time-to-days def)
  13234. (calendar-absolute-from-gregorian
  13235. (calendar-current-date))))
  13236. (org-eval-in-calendar nil t)
  13237. (let* ((old-map (current-local-map))
  13238. (map (copy-keymap calendar-mode-map))
  13239. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13240. (org-defkey map (kbd "RET") 'org-calendar-select)
  13241. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13242. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13243. (org-defkey minibuffer-local-map [(meta shift left)]
  13244. (lambda () (interactive)
  13245. (org-eval-in-calendar '(calendar-backward-month 1))))
  13246. (org-defkey minibuffer-local-map [(meta shift right)]
  13247. (lambda () (interactive)
  13248. (org-eval-in-calendar '(calendar-forward-month 1))))
  13249. (org-defkey minibuffer-local-map [(meta shift up)]
  13250. (lambda () (interactive)
  13251. (org-eval-in-calendar '(calendar-backward-year 1))))
  13252. (org-defkey minibuffer-local-map [(meta shift down)]
  13253. (lambda () (interactive)
  13254. (org-eval-in-calendar '(calendar-forward-year 1))))
  13255. (org-defkey minibuffer-local-map [?\e (shift left)]
  13256. (lambda () (interactive)
  13257. (org-eval-in-calendar '(calendar-backward-month 1))))
  13258. (org-defkey minibuffer-local-map [?\e (shift right)]
  13259. (lambda () (interactive)
  13260. (org-eval-in-calendar '(calendar-forward-month 1))))
  13261. (org-defkey minibuffer-local-map [?\e (shift up)]
  13262. (lambda () (interactive)
  13263. (org-eval-in-calendar '(calendar-backward-year 1))))
  13264. (org-defkey minibuffer-local-map [?\e (shift down)]
  13265. (lambda () (interactive)
  13266. (org-eval-in-calendar '(calendar-forward-year 1))))
  13267. (org-defkey minibuffer-local-map [(shift up)]
  13268. (lambda () (interactive)
  13269. (org-eval-in-calendar '(calendar-backward-week 1))))
  13270. (org-defkey minibuffer-local-map [(shift down)]
  13271. (lambda () (interactive)
  13272. (org-eval-in-calendar '(calendar-forward-week 1))))
  13273. (org-defkey minibuffer-local-map [(shift left)]
  13274. (lambda () (interactive)
  13275. (org-eval-in-calendar '(calendar-backward-day 1))))
  13276. (org-defkey minibuffer-local-map [(shift right)]
  13277. (lambda () (interactive)
  13278. (org-eval-in-calendar '(calendar-forward-day 1))))
  13279. (org-defkey minibuffer-local-map ">"
  13280. (lambda () (interactive)
  13281. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13282. (org-defkey minibuffer-local-map "<"
  13283. (lambda () (interactive)
  13284. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13285. (org-defkey minibuffer-local-map "\C-v"
  13286. (lambda () (interactive)
  13287. (org-eval-in-calendar
  13288. '(calendar-scroll-left-three-months 1))))
  13289. (org-defkey minibuffer-local-map "\M-v"
  13290. (lambda () (interactive)
  13291. (org-eval-in-calendar
  13292. '(calendar-scroll-right-three-months 1))))
  13293. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13294. (unwind-protect
  13295. (progn
  13296. (use-local-map map)
  13297. (add-hook 'post-command-hook 'org-read-date-display)
  13298. (setq org-ans0 (read-string prompt default-input
  13299. 'org-read-date-history nil))
  13300. ;; org-ans0: from prompt
  13301. ;; org-ans1: from mouse click
  13302. ;; org-ans2: from calendar motion
  13303. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13304. (remove-hook 'post-command-hook 'org-read-date-display)
  13305. (use-local-map old-map)
  13306. (when org-read-date-overlay
  13307. (delete-overlay org-read-date-overlay)
  13308. (setq org-read-date-overlay nil)))))
  13309. (bury-buffer "*Calendar*")))))
  13310. (t ; Naked prompt only
  13311. (unwind-protect
  13312. (setq ans (read-string prompt default-input
  13313. 'org-read-date-history timestr))
  13314. (when org-read-date-overlay
  13315. (delete-overlay org-read-date-overlay)
  13316. (setq org-read-date-overlay nil)))))
  13317. (setq final (org-read-date-analyze ans def defdecode))
  13318. (when org-read-date-analyze-forced-year
  13319. (message "Year was forced into %s"
  13320. (if org-read-date-force-compatible-dates
  13321. "compatible range (1970-2037)"
  13322. "range representable on this machine"))
  13323. (ding))
  13324. ;; One round trip to get rid of 34th of August and stuff like that....
  13325. (setq final (decode-time (apply 'encode-time final)))
  13326. (setq org-read-date-final-answer ans)
  13327. (if to-time
  13328. (apply 'encode-time final)
  13329. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13330. (format "%04d-%02d-%02d %02d:%02d"
  13331. (nth 5 final) (nth 4 final) (nth 3 final)
  13332. (nth 2 final) (nth 1 final))
  13333. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13334. (defvar def)
  13335. (defvar defdecode)
  13336. (defvar with-time)
  13337. (defun org-read-date-display ()
  13338. "Display the current date prompt interpretation in the minibuffer."
  13339. (when org-read-date-display-live
  13340. (when org-read-date-overlay
  13341. (delete-overlay org-read-date-overlay))
  13342. (let ((p (point)))
  13343. (end-of-line 1)
  13344. (while (not (equal (buffer-substring
  13345. (max (point-min) (- (point) 4)) (point))
  13346. " "))
  13347. (insert " "))
  13348. (goto-char p))
  13349. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13350. " " (or org-ans1 org-ans2)))
  13351. (org-end-time-was-given nil)
  13352. (f (org-read-date-analyze ans def defdecode))
  13353. (fmts (if org-dcst
  13354. org-time-stamp-custom-formats
  13355. org-time-stamp-formats))
  13356. (fmt (if (or with-time
  13357. (and (boundp 'org-time-was-given) org-time-was-given))
  13358. (cdr fmts)
  13359. (car fmts)))
  13360. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13361. (when (and org-end-time-was-given
  13362. (string-match org-plain-time-of-day-regexp txt))
  13363. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13364. org-end-time-was-given
  13365. (substring txt (match-end 0)))))
  13366. (when org-read-date-analyze-futurep
  13367. (setq txt (concat txt " (=>F)")))
  13368. (setq org-read-date-overlay
  13369. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13370. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13371. (defun org-read-date-analyze (ans def defdecode)
  13372. "Analyze the combined answer of the date prompt."
  13373. ;; FIXME: cleanup and comment
  13374. (let ((nowdecode (decode-time (current-time)))
  13375. delta deltan deltaw deltadef year month day
  13376. hour minute second wday pm h2 m2 tl wday1
  13377. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13378. (setq org-read-date-analyze-futurep nil
  13379. org-read-date-analyze-forced-year nil)
  13380. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13381. (setq ans "+0"))
  13382. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13383. (setq ans (replace-match "" t t ans)
  13384. deltan (car delta)
  13385. deltaw (nth 1 delta)
  13386. deltadef (nth 2 delta)))
  13387. ;; Check if there is an iso week date in there
  13388. ;; If yes, store the info and postpone interpreting it until the rest
  13389. ;; of the parsing is done
  13390. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13391. (setq iso-year (if (match-end 1)
  13392. (org-small-year-to-year
  13393. (string-to-number (match-string 1 ans))))
  13394. iso-weekday (if (match-end 3)
  13395. (string-to-number (match-string 3 ans)))
  13396. iso-week (string-to-number (match-string 2 ans)))
  13397. (setq ans (replace-match "" t t ans)))
  13398. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13399. (when (string-match
  13400. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13401. (setq year (if (match-end 2)
  13402. (string-to-number (match-string 2 ans))
  13403. (progn (setq kill-year t)
  13404. (string-to-number (format-time-string "%Y"))))
  13405. month (string-to-number (match-string 3 ans))
  13406. day (string-to-number (match-string 4 ans)))
  13407. (if (< year 100) (setq year (+ 2000 year)))
  13408. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13409. t nil ans)))
  13410. ;; Help matching dottet european dates
  13411. (when (string-match
  13412. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13413. (setq year (if (match-end 3)
  13414. (string-to-number (match-string 3 ans))
  13415. (progn (setq kill-year t)
  13416. (string-to-number (format-time-string "%Y"))))
  13417. day (string-to-number (match-string 1 ans))
  13418. month (string-to-number (match-string 2 ans))
  13419. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13420. t nil ans)))
  13421. ;; Help matching american dates, like 5/30 or 5/30/7
  13422. (when (string-match
  13423. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13424. (setq year (if (match-end 4)
  13425. (string-to-number (match-string 4 ans))
  13426. (progn (setq kill-year t)
  13427. (string-to-number (format-time-string "%Y"))))
  13428. month (string-to-number (match-string 1 ans))
  13429. day (string-to-number (match-string 2 ans)))
  13430. (if (< year 100) (setq year (+ 2000 year)))
  13431. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13432. t nil ans)))
  13433. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13434. ;; If there is a time with am/pm, and *no* time without it, we convert
  13435. ;; so that matching will be successful.
  13436. (loop for i from 1 to 2 do ; twice, for end time as well
  13437. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13438. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13439. (setq hour (string-to-number (match-string 1 ans))
  13440. minute (if (match-end 3)
  13441. (string-to-number (match-string 3 ans))
  13442. 0)
  13443. pm (equal ?p
  13444. (string-to-char (downcase (match-string 4 ans)))))
  13445. (if (and (= hour 12) (not pm))
  13446. (setq hour 0)
  13447. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13448. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13449. t t ans))))
  13450. ;; Check if a time range is given as a duration
  13451. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13452. (setq hour (string-to-number (match-string 1 ans))
  13453. h2 (+ hour (string-to-number (match-string 3 ans)))
  13454. minute (string-to-number (match-string 2 ans))
  13455. m2 (+ minute (if (match-end 5) (string-to-number
  13456. (match-string 5 ans))0)))
  13457. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13458. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13459. t t ans)))
  13460. ;; Check if there is a time range
  13461. (when (boundp 'org-end-time-was-given)
  13462. (setq org-time-was-given nil)
  13463. (when (and (string-match org-plain-time-of-day-regexp ans)
  13464. (match-end 8))
  13465. (setq org-end-time-was-given (match-string 8 ans))
  13466. (setq ans (concat (substring ans 0 (match-beginning 7))
  13467. (substring ans (match-end 7))))))
  13468. (setq tl (parse-time-string ans)
  13469. day (or (nth 3 tl) (nth 3 defdecode))
  13470. month (or (nth 4 tl)
  13471. (if (and org-read-date-prefer-future
  13472. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13473. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13474. (nth 4 defdecode)))
  13475. year (or (and (not kill-year) (nth 5 tl))
  13476. (if (and org-read-date-prefer-future
  13477. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13478. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13479. (nth 5 defdecode)))
  13480. hour (or (nth 2 tl) (nth 2 defdecode))
  13481. minute (or (nth 1 tl) (nth 1 defdecode))
  13482. second (or (nth 0 tl) 0)
  13483. wday (nth 6 tl))
  13484. (when (and (eq org-read-date-prefer-future 'time)
  13485. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13486. (equal day (nth 3 nowdecode))
  13487. (equal month (nth 4 nowdecode))
  13488. (equal year (nth 5 nowdecode))
  13489. (nth 2 tl)
  13490. (or (< (nth 2 tl) (nth 2 nowdecode))
  13491. (and (= (nth 2 tl) (nth 2 nowdecode))
  13492. (nth 1 tl)
  13493. (< (nth 1 tl) (nth 1 nowdecode)))))
  13494. (setq day (1+ day)
  13495. futurep t))
  13496. ;; Special date definitions below
  13497. (cond
  13498. (iso-week
  13499. ;; There was an iso week
  13500. (require 'cal-iso)
  13501. (setq futurep nil)
  13502. (setq year (or iso-year year)
  13503. day (or iso-weekday wday 1)
  13504. wday nil ; to make sure that the trigger below does not match
  13505. iso-date (calendar-gregorian-from-absolute
  13506. (calendar-absolute-from-iso
  13507. (list iso-week day year))))
  13508. ; FIXME: Should we also push ISO weeks into the future?
  13509. ; (when (and org-read-date-prefer-future
  13510. ; (not iso-year)
  13511. ; (< (calendar-absolute-from-gregorian iso-date)
  13512. ; (time-to-days (current-time))))
  13513. ; (setq year (1+ year)
  13514. ; iso-date (calendar-gregorian-from-absolute
  13515. ; (calendar-absolute-from-iso
  13516. ; (list iso-week day year)))))
  13517. (setq month (car iso-date)
  13518. year (nth 2 iso-date)
  13519. day (nth 1 iso-date)))
  13520. (deltan
  13521. (setq futurep nil)
  13522. (unless deltadef
  13523. (let ((now (decode-time (current-time))))
  13524. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13525. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13526. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13527. ((equal deltaw "m") (setq month (+ month deltan)))
  13528. ((equal deltaw "y") (setq year (+ year deltan)))))
  13529. ((and wday (not (nth 3 tl)))
  13530. (setq futurep nil)
  13531. ;; Weekday was given, but no day, so pick that day in the week
  13532. ;; on or after the derived date.
  13533. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13534. (unless (equal wday wday1)
  13535. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13536. (if (and (boundp 'org-time-was-given)
  13537. (nth 2 tl))
  13538. (setq org-time-was-given t))
  13539. (if (< year 100) (setq year (+ 2000 year)))
  13540. ;; Check of the date is representable
  13541. (if org-read-date-force-compatible-dates
  13542. (progn
  13543. (if (< year 1970)
  13544. (setq year 1970 org-read-date-analyze-forced-year t))
  13545. (if (> year 2037)
  13546. (setq year 2037 org-read-date-analyze-forced-year t)))
  13547. (condition-case nil
  13548. (encode-time second minute hour day month year)
  13549. (error
  13550. (setq year (nth 5 defdecode))
  13551. (setq org-read-date-analyze-forced-year t))))
  13552. (setq org-read-date-analyze-futurep futurep)
  13553. (list second minute hour day month year)))
  13554. (defvar parse-time-weekdays)
  13555. (defun org-read-date-get-relative (s today default)
  13556. "Check string S for special relative date string.
  13557. TODAY and DEFAULT are internal times, for today and for a default.
  13558. Return shift list (N what def-flag)
  13559. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13560. N is the number of WHATs to shift.
  13561. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13562. the DEFAULT date rather than TODAY."
  13563. (when (and
  13564. (string-match
  13565. (concat
  13566. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13567. "\\([0-9]+\\)?"
  13568. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13569. "\\([ \t]\\|$\\)") s)
  13570. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13571. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13572. (string-to-char (substring (match-string 1 s) -1))
  13573. ?+))
  13574. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13575. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13576. (what (if (match-end 3) (match-string 3 s) "d"))
  13577. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13578. (date (if rel default today))
  13579. (wday (nth 6 (decode-time date)))
  13580. delta)
  13581. (if wday1
  13582. (progn
  13583. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13584. (if (= dir ?-) (setq delta (- delta 7)))
  13585. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13586. (list delta "d" rel))
  13587. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13588. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13589. "Turn a user-specified date into the internal representation.
  13590. The internal representation needed by the calendar is (month day year).
  13591. This is a wrapper to handle the brain-dead convention in calendar that
  13592. user function argument order change dependent on argument order."
  13593. (if (boundp 'calendar-date-style)
  13594. (cond
  13595. ((eq calendar-date-style 'american)
  13596. (list arg1 arg2 arg3))
  13597. ((eq calendar-date-style 'european)
  13598. (list arg2 arg1 arg3))
  13599. ((eq calendar-date-style 'iso)
  13600. (list arg2 arg3 arg1)))
  13601. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13602. (if (org-bound-and-true-p european-calendar-style)
  13603. (list arg2 arg1 arg3)
  13604. (list arg1 arg2 arg3)))))
  13605. (defun org-eval-in-calendar (form &optional keepdate)
  13606. "Eval FORM in the calendar window and return to current window.
  13607. Also, store the cursor date in variable org-ans2."
  13608. (let ((sf (selected-frame))
  13609. (sw (selected-window)))
  13610. (select-window (get-buffer-window "*Calendar*" t))
  13611. (eval form)
  13612. (when (and (not keepdate) (calendar-cursor-to-date))
  13613. (let* ((date (calendar-cursor-to-date))
  13614. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13615. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13616. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13617. (select-window sw)
  13618. (org-select-frame-set-input-focus sf)))
  13619. (defun org-calendar-select ()
  13620. "Return to `org-read-date' with the date currently selected.
  13621. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13622. (interactive)
  13623. (when (calendar-cursor-to-date)
  13624. (let* ((date (calendar-cursor-to-date))
  13625. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13626. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13627. (if (active-minibuffer-window) (exit-minibuffer))))
  13628. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13629. "Insert a date stamp for the date given by the internal TIME.
  13630. WITH-HM means use the stamp format that includes the time of the day.
  13631. INACTIVE means use square brackets instead of angular ones, so that the
  13632. stamp will not contribute to the agenda.
  13633. PRE and POST are optional strings to be inserted before and after the
  13634. stamp.
  13635. The command returns the inserted time stamp."
  13636. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13637. stamp)
  13638. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13639. (insert-before-markers (or pre ""))
  13640. (when (listp extra)
  13641. (setq extra (car extra))
  13642. (if (and (stringp extra)
  13643. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13644. (setq extra (format "-%02d:%02d"
  13645. (string-to-number (match-string 1 extra))
  13646. (string-to-number (match-string 2 extra))))
  13647. (setq extra nil)))
  13648. (when extra
  13649. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13650. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13651. (insert-before-markers (or post ""))
  13652. (setq org-last-inserted-timestamp stamp)))
  13653. (defun org-toggle-time-stamp-overlays ()
  13654. "Toggle the use of custom time stamp formats."
  13655. (interactive)
  13656. (setq org-display-custom-times (not org-display-custom-times))
  13657. (unless org-display-custom-times
  13658. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13659. (while (setq p (next-single-property-change p 'display))
  13660. (if (and (get-text-property p 'display)
  13661. (eq (get-text-property p 'face) 'org-date))
  13662. (remove-text-properties
  13663. p (setq p (next-single-property-change p 'display))
  13664. '(display t))))
  13665. (set-buffer-modified-p bmp)))
  13666. (if (featurep 'xemacs)
  13667. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13668. (org-restart-font-lock)
  13669. (setq org-table-may-need-update t)
  13670. (if org-display-custom-times
  13671. (message "Time stamps are overlayed with custom format")
  13672. (message "Time stamp overlays removed")))
  13673. (defun org-display-custom-time (beg end)
  13674. "Overlay modified time stamp format over timestamp between BEG and END."
  13675. (let* ((ts (buffer-substring beg end))
  13676. t1 w1 with-hm tf time str w2 (off 0))
  13677. (save-match-data
  13678. (setq t1 (org-parse-time-string ts t))
  13679. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13680. (setq off (- (match-end 0) (match-beginning 0)))))
  13681. (setq end (- end off))
  13682. (setq w1 (- end beg)
  13683. with-hm (and (nth 1 t1) (nth 2 t1))
  13684. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13685. time (org-fix-decoded-time t1)
  13686. str (org-add-props
  13687. (format-time-string
  13688. (substring tf 1 -1) (apply 'encode-time time))
  13689. nil 'mouse-face 'highlight)
  13690. w2 (length str))
  13691. (if (not (= w2 w1))
  13692. (add-text-properties (1+ beg) (+ 2 beg)
  13693. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13694. (if (featurep 'xemacs)
  13695. (progn
  13696. (put-text-property beg end 'invisible t)
  13697. (put-text-property beg end 'end-glyph (make-glyph str)))
  13698. (put-text-property beg end 'display str))))
  13699. (defun org-translate-time (string)
  13700. "Translate all timestamps in STRING to custom format.
  13701. But do this only if the variable `org-display-custom-times' is set."
  13702. (when org-display-custom-times
  13703. (save-match-data
  13704. (let* ((start 0)
  13705. (re org-ts-regexp-both)
  13706. t1 with-hm inactive tf time str beg end)
  13707. (while (setq start (string-match re string start))
  13708. (setq beg (match-beginning 0)
  13709. end (match-end 0)
  13710. t1 (save-match-data
  13711. (org-parse-time-string (substring string beg end) t))
  13712. with-hm (and (nth 1 t1) (nth 2 t1))
  13713. inactive (equal (substring string beg (1+ beg)) "[")
  13714. tf (funcall (if with-hm 'cdr 'car)
  13715. org-time-stamp-custom-formats)
  13716. time (org-fix-decoded-time t1)
  13717. str (format-time-string
  13718. (concat
  13719. (if inactive "[" "<") (substring tf 1 -1)
  13720. (if inactive "]" ">"))
  13721. (apply 'encode-time time))
  13722. string (replace-match str t t string)
  13723. start (+ start (length str)))))))
  13724. string)
  13725. (defun org-fix-decoded-time (time)
  13726. "Set 0 instead of nil for the first 6 elements of time.
  13727. Don't touch the rest."
  13728. (let ((n 0))
  13729. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13730. (defun org-days-to-time (timestamp-string)
  13731. "Difference between TIMESTAMP-STRING and now in days."
  13732. (- (time-to-days (org-time-string-to-time timestamp-string))
  13733. (time-to-days (current-time))))
  13734. (defun org-deadline-close (timestamp-string &optional ndays)
  13735. "Is the time in TIMESTAMP-STRING close to the current date?"
  13736. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13737. (and (< (org-days-to-time timestamp-string) ndays)
  13738. (not (org-entry-is-done-p))))
  13739. (defun org-get-wdays (ts)
  13740. "Get the deadline lead time appropriate for timestring TS."
  13741. (cond
  13742. ((<= org-deadline-warning-days 0)
  13743. ;; 0 or negative, enforce this value no matter what
  13744. (- org-deadline-warning-days))
  13745. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13746. ;; lead time is specified.
  13747. (floor (* (string-to-number (match-string 1 ts))
  13748. (cdr (assoc (match-string 2 ts)
  13749. '(("d" . 1) ("w" . 7)
  13750. ("m" . 30.4) ("y" . 365.25)))))))
  13751. ;; go for the default.
  13752. (t org-deadline-warning-days)))
  13753. (defun org-calendar-select-mouse (ev)
  13754. "Return to `org-read-date' with the date currently selected.
  13755. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13756. (interactive "e")
  13757. (mouse-set-point ev)
  13758. (when (calendar-cursor-to-date)
  13759. (let* ((date (calendar-cursor-to-date))
  13760. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13761. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13762. (if (active-minibuffer-window) (exit-minibuffer))))
  13763. (defun org-check-deadlines (ndays)
  13764. "Check if there are any deadlines due or past due.
  13765. A deadline is considered due if it happens within `org-deadline-warning-days'
  13766. days from today's date. If the deadline appears in an entry marked DONE,
  13767. it is not shown. The prefix arg NDAYS can be used to test that many
  13768. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13769. (interactive "P")
  13770. (let* ((org-warn-days
  13771. (cond
  13772. ((equal ndays '(4)) 100000)
  13773. (ndays (prefix-numeric-value ndays))
  13774. (t (abs org-deadline-warning-days))))
  13775. (case-fold-search nil)
  13776. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13777. (callback
  13778. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13779. (message "%d deadlines past-due or due within %d days"
  13780. (org-occur regexp nil callback)
  13781. org-warn-days)))
  13782. (defun org-check-before-date (date)
  13783. "Check if there are deadlines or scheduled entries before DATE."
  13784. (interactive (list (org-read-date)))
  13785. (let ((case-fold-search nil)
  13786. (regexp (concat "\\<\\(" org-deadline-string
  13787. "\\|" org-scheduled-string
  13788. "\\) *<\\([^>]+\\)>"))
  13789. (callback
  13790. (lambda () (time-less-p
  13791. (org-time-string-to-time (match-string 2))
  13792. (org-time-string-to-time date)))))
  13793. (message "%d entries before %s"
  13794. (org-occur regexp nil callback) date)))
  13795. (defun org-check-after-date (date)
  13796. "Check if there are deadlines or scheduled entries after DATE."
  13797. (interactive (list (org-read-date)))
  13798. (let ((case-fold-search nil)
  13799. (regexp (concat "\\<\\(" org-deadline-string
  13800. "\\|" org-scheduled-string
  13801. "\\) *<\\([^>]+\\)>"))
  13802. (callback
  13803. (lambda () (not
  13804. (time-less-p
  13805. (org-time-string-to-time (match-string 2))
  13806. (org-time-string-to-time date))))))
  13807. (message "%d entries after %s"
  13808. (org-occur regexp nil callback) date)))
  13809. (defun org-evaluate-time-range (&optional to-buffer)
  13810. "Evaluate a time range by computing the difference between start and end.
  13811. Normally the result is just printed in the echo area, but with prefix arg
  13812. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13813. If the time range is actually in a table, the result is inserted into the
  13814. next column.
  13815. For time difference computation, a year is assumed to be exactly 365
  13816. days in order to avoid rounding problems."
  13817. (interactive "P")
  13818. (or
  13819. (org-clock-update-time-maybe)
  13820. (save-excursion
  13821. (unless (org-at-date-range-p t)
  13822. (goto-char (point-at-bol))
  13823. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13824. (if (not (org-at-date-range-p t))
  13825. (error "Not at a time-stamp range, and none found in current line")))
  13826. (let* ((ts1 (match-string 1))
  13827. (ts2 (match-string 2))
  13828. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13829. (match-end (match-end 0))
  13830. (time1 (org-time-string-to-time ts1))
  13831. (time2 (org-time-string-to-time ts2))
  13832. (t1 (org-float-time time1))
  13833. (t2 (org-float-time time2))
  13834. (diff (abs (- t2 t1)))
  13835. (negative (< (- t2 t1) 0))
  13836. ;; (ys (floor (* 365 24 60 60)))
  13837. (ds (* 24 60 60))
  13838. (hs (* 60 60))
  13839. (fy "%dy %dd %02d:%02d")
  13840. (fy1 "%dy %dd")
  13841. (fd "%dd %02d:%02d")
  13842. (fd1 "%dd")
  13843. (fh "%02d:%02d")
  13844. y d h m align)
  13845. (if havetime
  13846. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13847. y 0
  13848. d (floor (/ diff ds)) diff (mod diff ds)
  13849. h (floor (/ diff hs)) diff (mod diff hs)
  13850. m (floor (/ diff 60)))
  13851. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13852. y 0
  13853. d (floor (+ (/ diff ds) 0.5))
  13854. h 0 m 0))
  13855. (if (not to-buffer)
  13856. (message "%s" (org-make-tdiff-string y d h m))
  13857. (if (org-at-table-p)
  13858. (progn
  13859. (goto-char match-end)
  13860. (setq align t)
  13861. (and (looking-at " *|") (goto-char (match-end 0))))
  13862. (goto-char match-end))
  13863. (if (looking-at
  13864. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13865. (replace-match ""))
  13866. (if negative (insert " -"))
  13867. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13868. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13869. (insert " " (format fh h m))))
  13870. (if align (org-table-align))
  13871. (message "Time difference inserted")))))
  13872. (defun org-make-tdiff-string (y d h m)
  13873. (let ((fmt "")
  13874. (l nil))
  13875. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13876. l (push y l)))
  13877. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13878. l (push d l)))
  13879. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13880. l (push h l)))
  13881. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13882. l (push m l)))
  13883. (apply 'format fmt (nreverse l))))
  13884. (defun org-time-string-to-time (s)
  13885. (apply 'encode-time (org-parse-time-string s)))
  13886. (defun org-time-string-to-seconds (s)
  13887. (org-float-time (org-time-string-to-time s)))
  13888. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13889. "Convert a time stamp to an absolute day number.
  13890. If there is a specifier for a cyclic time stamp, get the closest date to
  13891. DAYNR.
  13892. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13893. The variable date is bound by the calendar when this is called."
  13894. (cond
  13895. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13896. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13897. daynr
  13898. (+ daynr 1000)))
  13899. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13900. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13901. (time-to-days (current-time))) (match-string 0 s)
  13902. prefer show-all))
  13903. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13904. (defun org-days-to-iso-week (days)
  13905. "Return the iso week number."
  13906. (require 'cal-iso)
  13907. (car (calendar-iso-from-absolute days)))
  13908. (defun org-small-year-to-year (year)
  13909. "Convert 2-digit years into 4-digit years.
  13910. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13911. The year 2000 cannot be abbreviated. Any year larger than 99
  13912. is returned unchanged."
  13913. (if (< year 38)
  13914. (setq year (+ 2000 year))
  13915. (if (< year 100)
  13916. (setq year (+ 1900 year))))
  13917. year)
  13918. (defun org-time-from-absolute (d)
  13919. "Return the time corresponding to date D.
  13920. D may be an absolute day number, or a calendar-type list (month day year)."
  13921. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13922. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13923. (defun org-calendar-holiday ()
  13924. "List of holidays, for Diary display in Org-mode."
  13925. (require 'holidays)
  13926. (let ((hl (funcall
  13927. (if (fboundp 'calendar-check-holidays)
  13928. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13929. (if hl (mapconcat 'identity hl "; "))))
  13930. (defun org-diary-sexp-entry (sexp entry date)
  13931. "Process a SEXP diary ENTRY for DATE."
  13932. (require 'diary-lib)
  13933. (let ((result (if calendar-debug-sexp
  13934. (let ((stack-trace-on-error t))
  13935. (eval (car (read-from-string sexp))))
  13936. (condition-case nil
  13937. (eval (car (read-from-string sexp)))
  13938. (error
  13939. (beep)
  13940. (message "Bad sexp at line %d in %s: %s"
  13941. (org-current-line)
  13942. (buffer-file-name) sexp)
  13943. (sleep-for 2))))))
  13944. (cond ((stringp result) (split-string result "; "))
  13945. ((and (consp result)
  13946. (not (consp (cdr result)))
  13947. (stringp (cdr result))) (cdr result))
  13948. ((and (consp result)
  13949. (stringp (car result))) result)
  13950. (result entry)
  13951. (t nil))))
  13952. (defun org-diary-to-ical-string (frombuf)
  13953. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13954. This uses the icalendar.el library."
  13955. (let* ((tmpdir (if (featurep 'xemacs)
  13956. (temp-directory)
  13957. temporary-file-directory))
  13958. (tmpfile (make-temp-name
  13959. (expand-file-name "orgics" tmpdir)))
  13960. buf rtn b e)
  13961. (with-current-buffer frombuf
  13962. (icalendar-export-region (point-min) (point-max) tmpfile)
  13963. (setq buf (find-buffer-visiting tmpfile))
  13964. (set-buffer buf)
  13965. (goto-char (point-min))
  13966. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13967. (setq b (match-beginning 0)))
  13968. (goto-char (point-max))
  13969. (if (re-search-backward "^END:VEVENT" nil t)
  13970. (setq e (match-end 0)))
  13971. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13972. (kill-buffer buf)
  13973. (delete-file tmpfile)
  13974. rtn))
  13975. (defun org-closest-date (start current change prefer show-all)
  13976. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13977. When PREFER is `past', return a date that is either CURRENT or past.
  13978. When PREFER is `future', return a date that is either CURRENT or future.
  13979. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13980. ;; Make the proper lists from the dates
  13981. (catch 'exit
  13982. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13983. dn dw sday cday n1 n2 n0
  13984. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13985. (setq start (org-date-to-gregorian start)
  13986. current (org-date-to-gregorian
  13987. (if show-all
  13988. current
  13989. (time-to-days (current-time))))
  13990. sday (calendar-absolute-from-gregorian start)
  13991. cday (calendar-absolute-from-gregorian current))
  13992. (if (<= cday sday) (throw 'exit sday))
  13993. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13994. (setq dn (string-to-number (match-string 1 change))
  13995. dw (cdr (assoc (match-string 2 change) a1)))
  13996. (error "Invalid change specifier: %s" change))
  13997. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13998. (cond
  13999. ((eq dw 'day)
  14000. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14001. n2 (+ n1 dn)))
  14002. ((eq dw 'year)
  14003. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14004. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14005. (setq date1 (list m d y1)
  14006. n1 (calendar-absolute-from-gregorian date1)
  14007. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14008. n2 (calendar-absolute-from-gregorian date2)))
  14009. ((eq dw 'month)
  14010. ;; approx number of month between the two dates
  14011. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14012. ;; How often does dn fit in there?
  14013. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14014. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14015. m (+ m nm)
  14016. ny (floor (/ m 12))
  14017. y (+ y ny)
  14018. m (- m (* ny 12)))
  14019. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14020. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14021. (setq m2 (+ m dn) y2 y)
  14022. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14023. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14024. (while (<= n2 cday)
  14025. (setq n1 n2 m m2 y y2)
  14026. (setq m2 (+ m dn) y2 y)
  14027. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14028. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14029. ;; Make sure n1 is the earlier date
  14030. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14031. (if show-all
  14032. (cond
  14033. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14034. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14035. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14036. (cond
  14037. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14038. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14039. (t (if (= cday n1) n1 n2)))))))
  14040. (defun org-date-to-gregorian (date)
  14041. "Turn any specification of DATE into a Gregorian date for the calendar."
  14042. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14043. ((and (listp date) (= (length date) 3)) date)
  14044. ((stringp date)
  14045. (setq date (org-parse-time-string date))
  14046. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14047. ((listp date)
  14048. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14049. (defun org-parse-time-string (s &optional nodefault)
  14050. "Parse the standard Org-mode time string.
  14051. This should be a lot faster than the normal `parse-time-string'.
  14052. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14053. hour and minute fields will be nil if not given."
  14054. (if (string-match org-ts-regexp0 s)
  14055. (list 0
  14056. (if (or (match-beginning 8) (not nodefault))
  14057. (string-to-number (or (match-string 8 s) "0")))
  14058. (if (or (match-beginning 7) (not nodefault))
  14059. (string-to-number (or (match-string 7 s) "0")))
  14060. (string-to-number (match-string 4 s))
  14061. (string-to-number (match-string 3 s))
  14062. (string-to-number (match-string 2 s))
  14063. nil nil nil)
  14064. (error "Not a standard Org-mode time string: %s" s)))
  14065. (defun org-timestamp-up (&optional arg)
  14066. "Increase the date item at the cursor by one.
  14067. If the cursor is on the year, change the year. If it is on the month or
  14068. the day, change that.
  14069. With prefix ARG, change by that many units."
  14070. (interactive "p")
  14071. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14072. (defun org-timestamp-down (&optional arg)
  14073. "Decrease the date item at the cursor by one.
  14074. If the cursor is on the year, change the year. If it is on the month or
  14075. the day, change that.
  14076. With prefix ARG, change by that many units."
  14077. (interactive "p")
  14078. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14079. (defun org-timestamp-up-day (&optional arg)
  14080. "Increase the date in the time stamp by one day.
  14081. With prefix ARG, change that many days."
  14082. (interactive "p")
  14083. (if (and (not (org-at-timestamp-p t))
  14084. (org-on-heading-p))
  14085. (org-todo 'up)
  14086. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14087. (defun org-timestamp-down-day (&optional arg)
  14088. "Decrease the date in the time stamp by one day.
  14089. With prefix ARG, change that many days."
  14090. (interactive "p")
  14091. (if (and (not (org-at-timestamp-p t))
  14092. (org-on-heading-p))
  14093. (org-todo 'down)
  14094. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14095. (defun org-at-timestamp-p (&optional inactive-ok)
  14096. "Determine if the cursor is in or at a timestamp."
  14097. (interactive)
  14098. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14099. (pos (point))
  14100. (ans (or (looking-at tsr)
  14101. (save-excursion
  14102. (skip-chars-backward "^[<\n\r\t")
  14103. (if (> (point) (point-min)) (backward-char 1))
  14104. (and (looking-at tsr)
  14105. (> (- (match-end 0) pos) -1))))))
  14106. (and ans
  14107. (boundp 'org-ts-what)
  14108. (setq org-ts-what
  14109. (cond
  14110. ((= pos (match-beginning 0)) 'bracket)
  14111. ((= pos (1- (match-end 0))) 'bracket)
  14112. ((org-pos-in-match-range pos 2) 'year)
  14113. ((org-pos-in-match-range pos 3) 'month)
  14114. ((org-pos-in-match-range pos 7) 'hour)
  14115. ((org-pos-in-match-range pos 8) 'minute)
  14116. ((or (org-pos-in-match-range pos 4)
  14117. (org-pos-in-match-range pos 5)) 'day)
  14118. ((and (> pos (or (match-end 8) (match-end 5)))
  14119. (< pos (match-end 0)))
  14120. (- pos (or (match-end 8) (match-end 5))))
  14121. (t 'day))))
  14122. ans))
  14123. (defun org-toggle-timestamp-type ()
  14124. "Toggle the type (<active> or [inactive]) of a time stamp."
  14125. (interactive)
  14126. (when (org-at-timestamp-p t)
  14127. (let ((beg (match-beginning 0)) (end (match-end 0))
  14128. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14129. (save-excursion
  14130. (goto-char beg)
  14131. (while (re-search-forward "[][<>]" end t)
  14132. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14133. t t)))
  14134. (message "Timestamp is now %sactive"
  14135. (if (equal (char-after beg) ?<) "" "in")))))
  14136. (defun org-timestamp-change (n &optional what updown)
  14137. "Change the date in the time stamp at point.
  14138. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14139. `year', `minute', `second'. If WHAT is not given, the cursor position
  14140. in the timestamp determines what will be changed."
  14141. (let ((pos (point))
  14142. with-hm inactive
  14143. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14144. org-ts-what
  14145. extra rem
  14146. ts time time0)
  14147. (if (not (org-at-timestamp-p t))
  14148. (error "Not at a timestamp"))
  14149. (if (and (not what) (eq org-ts-what 'bracket))
  14150. (org-toggle-timestamp-type)
  14151. (if (and (not what) (not (eq org-ts-what 'day))
  14152. org-display-custom-times
  14153. (get-text-property (point) 'display)
  14154. (not (get-text-property (1- (point)) 'display)))
  14155. (setq org-ts-what 'day))
  14156. (setq org-ts-what (or what org-ts-what)
  14157. inactive (= (char-after (match-beginning 0)) ?\[)
  14158. ts (match-string 0))
  14159. (replace-match "")
  14160. (if (string-match
  14161. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14162. ts)
  14163. (setq extra (match-string 1 ts)))
  14164. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14165. (setq with-hm t))
  14166. (setq time0 (org-parse-time-string ts))
  14167. (when (and updown
  14168. (eq org-ts-what 'minute)
  14169. (not current-prefix-arg))
  14170. ;; This looks like s-up and s-down. Change by one rounding step.
  14171. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14172. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14173. (setcar (cdr time0) (+ (nth 1 time0)
  14174. (if (> n 0) (- rem) (- dm rem))))))
  14175. (setq time
  14176. (encode-time (or (car time0) 0)
  14177. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14178. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14179. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14180. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14181. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14182. (nthcdr 6 time0)))
  14183. (when (and (member org-ts-what '(hour minute))
  14184. extra
  14185. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14186. (setq extra (org-modify-ts-extra
  14187. extra
  14188. (if (eq org-ts-what 'hour) 2 5)
  14189. n dm)))
  14190. (when (integerp org-ts-what)
  14191. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14192. (if (eq what 'calendar)
  14193. (let ((cal-date (org-get-date-from-calendar)))
  14194. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14195. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14196. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14197. (setcar time0 (or (car time0) 0))
  14198. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14199. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14200. (setq time (apply 'encode-time time0))))
  14201. (setq org-last-changed-timestamp
  14202. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14203. (org-clock-update-time-maybe)
  14204. (goto-char pos)
  14205. ;; Try to recenter the calendar window, if any
  14206. (if (and org-calendar-follow-timestamp-change
  14207. (get-buffer-window "*Calendar*" t)
  14208. (memq org-ts-what '(day month year)))
  14209. (org-recenter-calendar (time-to-days time))))))
  14210. (defun org-modify-ts-extra (s pos n dm)
  14211. "Change the different parts of the lead-time and repeat fields in timestamp."
  14212. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14213. ng h m new rem)
  14214. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14215. (cond
  14216. ((or (org-pos-in-match-range pos 2)
  14217. (org-pos-in-match-range pos 3))
  14218. (setq m (string-to-number (match-string 3 s))
  14219. h (string-to-number (match-string 2 s)))
  14220. (if (org-pos-in-match-range pos 2)
  14221. (setq h (+ h n))
  14222. (setq n (* dm (org-no-warnings (signum n))))
  14223. (when (not (= 0 (setq rem (% m dm))))
  14224. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14225. (setq m (+ m n)))
  14226. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14227. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14228. (setq h (min 24 (max 0 h)))
  14229. (setq ng 1 new (format "-%02d:%02d" h m)))
  14230. ((org-pos-in-match-range pos 6)
  14231. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14232. ((org-pos-in-match-range pos 5)
  14233. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14234. ((org-pos-in-match-range pos 9)
  14235. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14236. ((org-pos-in-match-range pos 8)
  14237. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14238. (when ng
  14239. (setq s (concat
  14240. (substring s 0 (match-beginning ng))
  14241. new
  14242. (substring s (match-end ng))))))
  14243. s))
  14244. (defun org-recenter-calendar (date)
  14245. "If the calendar is visible, recenter it to DATE."
  14246. (let* ((win (selected-window))
  14247. (cwin (get-buffer-window "*Calendar*" t))
  14248. (calendar-move-hook nil))
  14249. (when cwin
  14250. (select-window cwin)
  14251. (calendar-goto-date (if (listp date) date
  14252. (calendar-gregorian-from-absolute date)))
  14253. (select-window win))))
  14254. (defun org-goto-calendar (&optional arg)
  14255. "Go to the Emacs calendar at the current date.
  14256. If there is a time stamp in the current line, go to that date.
  14257. A prefix ARG can be used to force the current date."
  14258. (interactive "P")
  14259. (let ((tsr org-ts-regexp) diff
  14260. (calendar-move-hook nil)
  14261. (calendar-view-holidays-initially-flag nil)
  14262. (calendar-view-diary-initially-flag nil))
  14263. (if (or (org-at-timestamp-p)
  14264. (save-excursion
  14265. (beginning-of-line 1)
  14266. (looking-at (concat ".*" tsr))))
  14267. (let ((d1 (time-to-days (current-time)))
  14268. (d2 (time-to-days
  14269. (org-time-string-to-time (match-string 1)))))
  14270. (setq diff (- d2 d1))))
  14271. (calendar)
  14272. (calendar-goto-today)
  14273. (if (and diff (not arg)) (calendar-forward-day diff))))
  14274. (defun org-get-date-from-calendar ()
  14275. "Return a list (month day year) of date at point in calendar."
  14276. (with-current-buffer "*Calendar*"
  14277. (save-match-data
  14278. (calendar-cursor-to-date))))
  14279. (defun org-date-from-calendar ()
  14280. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14281. If there is already a time stamp at the cursor position, update it."
  14282. (interactive)
  14283. (if (org-at-timestamp-p t)
  14284. (org-timestamp-change 0 'calendar)
  14285. (let ((cal-date (org-get-date-from-calendar)))
  14286. (org-insert-time-stamp
  14287. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14288. (defun org-minutes-to-hh:mm-string (m)
  14289. "Compute H:MM from a number of minutes."
  14290. (let ((h (/ m 60)))
  14291. (setq m (- m (* 60 h)))
  14292. (format org-time-clocksum-format h m)))
  14293. (defun org-hh:mm-string-to-minutes (s)
  14294. "Convert a string H:MM to a number of minutes.
  14295. If the string is just a number, interpret it as minutes.
  14296. In fact, the first hh:mm or number in the string will be taken,
  14297. there can be extra stuff in the string.
  14298. If no number is found, the return value is 0."
  14299. (cond
  14300. ((integerp s) s)
  14301. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14302. (+ (* (string-to-number (match-string 1 s)) 60)
  14303. (string-to-number (match-string 2 s))))
  14304. ((string-match "\\([0-9]+\\)" s)
  14305. (string-to-number (match-string 1 s)))
  14306. (t 0)))
  14307. (defcustom org-effort-durations
  14308. `(("h" . 60)
  14309. ("d" . ,(* 60 8))
  14310. ("w" . ,(* 60 8 5))
  14311. ("m" . ,(* 60 8 5 4))
  14312. ("y" . ,(* 60 8 5 40)))
  14313. "Conversion factor to minutes for an effort modifier.
  14314. Each entry has the form (MODIFIER . MINUTES).
  14315. In an effort string, a number followed by MODIFIER is multiplied
  14316. by the specified number of MINUTES to obtain an effort in
  14317. minutes.
  14318. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14319. effort string \"2hours\" is equivalent to 120 minutes."
  14320. :group 'org-agenda
  14321. :type '(alist :key-type (string :tag "Modifier")
  14322. :value-type (number :tag "Minutes")))
  14323. (defun org-duration-string-to-minutes (s)
  14324. "Convert a duration string S to minutes.
  14325. A bare number is interpreted as minutes, modifiers can be set by
  14326. customizing `org-effort-durations' (which see).
  14327. Entries containing a colon are interpreted as H:MM by
  14328. `org-hh:mm-string-to-minutes'."
  14329. (let ((result 0)
  14330. (re (concat "\\([0-9]+\\) *\\("
  14331. (regexp-opt (mapcar 'car org-effort-durations))
  14332. "\\)")))
  14333. (while (string-match re s)
  14334. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14335. (string-to-number (match-string 1 s))))
  14336. (setq s (replace-match "" nil t s)))
  14337. (incf result (org-hh:mm-string-to-minutes s))
  14338. result))
  14339. ;;;; Files
  14340. (defun org-save-all-org-buffers ()
  14341. "Save all Org-mode buffers without user confirmation."
  14342. (interactive)
  14343. (message "Saving all Org-mode buffers...")
  14344. (save-some-buffers t 'org-mode-p)
  14345. (when (featurep 'org-id) (org-id-locations-save))
  14346. (message "Saving all Org-mode buffers... done"))
  14347. (defun org-revert-all-org-buffers ()
  14348. "Revert all Org-mode buffers.
  14349. Prompt for confirmation when there are unsaved changes.
  14350. Be sure you know what you are doing before letting this function
  14351. overwrite your changes.
  14352. This function is useful in a setup where one tracks org files
  14353. with a version control system, to revert on one machine after pulling
  14354. changes from another. I believe the procedure must be like this:
  14355. 1. M-x org-save-all-org-buffers
  14356. 2. Pull changes from the other machine, resolve conflicts
  14357. 3. M-x org-revert-all-org-buffers"
  14358. (interactive)
  14359. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14360. (error "Abort"))
  14361. (save-excursion
  14362. (save-window-excursion
  14363. (mapc
  14364. (lambda (b)
  14365. (when (and (with-current-buffer b (org-mode-p))
  14366. (with-current-buffer b buffer-file-name))
  14367. (switch-to-buffer b)
  14368. (revert-buffer t 'no-confirm)))
  14369. (buffer-list))
  14370. (when (and (featurep 'org-id) org-id-track-globally)
  14371. (org-id-locations-load)))))
  14372. ;;;; Agenda files
  14373. ;;;###autoload
  14374. (defun org-switchb (&optional arg)
  14375. "Switch between Org buffers.
  14376. With a prefix argument, restrict available to files.
  14377. With two prefix arguments, restrict available buffers to agenda files.
  14378. Defaults to `iswitchb' for buffer name completion.
  14379. Set `org-completion-use-ido' to make it use ido instead."
  14380. (interactive "P")
  14381. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14382. ((equal arg '(16)) (org-buffer-list 'agenda))
  14383. (t (org-buffer-list))))
  14384. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14385. (org-completion-use-ido org-completion-use-ido))
  14386. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14387. (setq org-completion-use-iswitchb t))
  14388. (switch-to-buffer
  14389. (org-icompleting-read "Org buffer: "
  14390. (mapcar 'list (mapcar 'buffer-name blist))
  14391. nil t))))
  14392. ;;; Define some older names previously used for this functionality
  14393. ;;;###autoload
  14394. (defalias 'org-ido-switchb 'org-switchb)
  14395. ;;;###autoload
  14396. (defalias 'org-iswitchb 'org-switchb)
  14397. (defun org-buffer-list (&optional predicate exclude-tmp)
  14398. "Return a list of Org buffers.
  14399. PREDICATE can be `export', `files' or `agenda'.
  14400. export restrict the list to Export buffers.
  14401. files restrict the list to buffers visiting Org files.
  14402. agenda restrict the list to buffers visiting agenda files.
  14403. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14404. (let* ((bfn nil)
  14405. (agenda-files (and (eq predicate 'agenda)
  14406. (mapcar 'file-truename (org-agenda-files t))))
  14407. (filter
  14408. (cond
  14409. ((eq predicate 'files)
  14410. (lambda (b) (with-current-buffer b (org-mode-p))))
  14411. ((eq predicate 'export)
  14412. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14413. ((eq predicate 'agenda)
  14414. (lambda (b)
  14415. (with-current-buffer b
  14416. (and (org-mode-p)
  14417. (setq bfn (buffer-file-name b))
  14418. (member (file-truename bfn) agenda-files)))))
  14419. (t (lambda (b) (with-current-buffer b
  14420. (or (org-mode-p)
  14421. (string-match "\*Org .*Export"
  14422. (buffer-name b)))))))))
  14423. (delq nil
  14424. (mapcar
  14425. (lambda(b)
  14426. (if (and (funcall filter b)
  14427. (or (not exclude-tmp)
  14428. (not (string-match "tmp" (buffer-name b)))))
  14429. b
  14430. nil))
  14431. (buffer-list)))))
  14432. (defun org-agenda-files (&optional unrestricted archives)
  14433. "Get the list of agenda files.
  14434. Optional UNRESTRICTED means return the full list even if a restriction
  14435. is currently in place.
  14436. When ARCHIVES is t, include all archive files that are really being
  14437. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14438. `org-agenda-archives-mode' is t."
  14439. (let ((files
  14440. (cond
  14441. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14442. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14443. ((listp org-agenda-files) org-agenda-files)
  14444. (t (error "Invalid value of `org-agenda-files'")))))
  14445. (setq files (apply 'append
  14446. (mapcar (lambda (f)
  14447. (if (file-directory-p f)
  14448. (directory-files
  14449. f t org-agenda-file-regexp)
  14450. (list f)))
  14451. files)))
  14452. (when org-agenda-skip-unavailable-files
  14453. (setq files (delq nil
  14454. (mapcar (function
  14455. (lambda (file)
  14456. (and (file-readable-p file) file)))
  14457. files))))
  14458. (when (or (eq archives t)
  14459. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14460. (setq files (org-add-archive-files files)))
  14461. files))
  14462. (defun org-agenda-file-p (&optional file)
  14463. "Return non-nil, if FILE is an agenda file.
  14464. If FILE is omitted, use the file associated with the current
  14465. buffer."
  14466. (member (or file (buffer-file-name))
  14467. (org-agenda-files t)))
  14468. (defun org-edit-agenda-file-list ()
  14469. "Edit the list of agenda files.
  14470. Depending on setup, this either uses customize to edit the variable
  14471. `org-agenda-files', or it visits the file that is holding the list. In the
  14472. latter case, the buffer is set up in a way that saving it automatically kills
  14473. the buffer and restores the previous window configuration."
  14474. (interactive)
  14475. (if (stringp org-agenda-files)
  14476. (let ((cw (current-window-configuration)))
  14477. (find-file org-agenda-files)
  14478. (org-set-local 'org-window-configuration cw)
  14479. (org-add-hook 'after-save-hook
  14480. (lambda ()
  14481. (set-window-configuration
  14482. (prog1 org-window-configuration
  14483. (kill-buffer (current-buffer))))
  14484. (org-install-agenda-files-menu)
  14485. (message "New agenda file list installed"))
  14486. nil 'local)
  14487. (message "%s" (substitute-command-keys
  14488. "Edit list and finish with \\[save-buffer]")))
  14489. (customize-variable 'org-agenda-files)))
  14490. (defun org-store-new-agenda-file-list (list)
  14491. "Set new value for the agenda file list and save it correctly."
  14492. (if (stringp org-agenda-files)
  14493. (let ((fe (org-read-agenda-file-list t)) b u)
  14494. (while (setq b (find-buffer-visiting org-agenda-files))
  14495. (kill-buffer b))
  14496. (with-temp-file org-agenda-files
  14497. (insert
  14498. (mapconcat
  14499. (lambda (f) ;; Keep un-expanded entries.
  14500. (if (setq u (assoc f fe))
  14501. (cdr u)
  14502. f))
  14503. list "\n")
  14504. "\n")))
  14505. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14506. (org-insert-mode-line-in-empty-file nil))
  14507. (setq org-agenda-files list)
  14508. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14509. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14510. "Read the list of agenda files from a file.
  14511. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14512. filenames, used by `org-store-new-agenda-file-list' to write back
  14513. un-expanded file names."
  14514. (when (file-directory-p org-agenda-files)
  14515. (error "`org-agenda-files' cannot be a single directory"))
  14516. (when (stringp org-agenda-files)
  14517. (with-temp-buffer
  14518. (insert-file-contents org-agenda-files)
  14519. (mapcar
  14520. (lambda (f)
  14521. (let ((e (expand-file-name (substitute-in-file-name f)
  14522. org-directory)))
  14523. (if pair-with-expansion
  14524. (cons e f)
  14525. e)))
  14526. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14527. ;;;###autoload
  14528. (defun org-cycle-agenda-files ()
  14529. "Cycle through the files in `org-agenda-files'.
  14530. If the current buffer visits an agenda file, find the next one in the list.
  14531. If the current buffer does not, find the first agenda file."
  14532. (interactive)
  14533. (let* ((fs (org-agenda-files t))
  14534. (files (append fs (list (car fs))))
  14535. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14536. file)
  14537. (unless files (error "No agenda files"))
  14538. (catch 'exit
  14539. (while (setq file (pop files))
  14540. (if (equal (file-truename file) tcf)
  14541. (when (car files)
  14542. (find-file (car files))
  14543. (throw 'exit t))))
  14544. (find-file (car fs)))
  14545. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14546. (defun org-agenda-file-to-front (&optional to-end)
  14547. "Move/add the current file to the top of the agenda file list.
  14548. If the file is not present in the list, it is added to the front. If it is
  14549. present, it is moved there. With optional argument TO-END, add/move to the
  14550. end of the list."
  14551. (interactive "P")
  14552. (let ((org-agenda-skip-unavailable-files nil)
  14553. (file-alist (mapcar (lambda (x)
  14554. (cons (file-truename x) x))
  14555. (org-agenda-files t)))
  14556. (ctf (file-truename buffer-file-name))
  14557. x had)
  14558. (setq x (assoc ctf file-alist) had x)
  14559. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14560. (if to-end
  14561. (setq file-alist (append (delq x file-alist) (list x)))
  14562. (setq file-alist (cons x (delq x file-alist))))
  14563. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14564. (org-install-agenda-files-menu)
  14565. (message "File %s to %s of agenda file list"
  14566. (if had "moved" "added") (if to-end "end" "front"))))
  14567. (defun org-remove-file (&optional file)
  14568. "Remove current file from the list of files in variable `org-agenda-files'.
  14569. These are the files which are being checked for agenda entries.
  14570. Optional argument FILE means use this file instead of the current."
  14571. (interactive)
  14572. (let* ((org-agenda-skip-unavailable-files nil)
  14573. (file (or file buffer-file-name))
  14574. (true-file (file-truename file))
  14575. (afile (abbreviate-file-name file))
  14576. (files (delq nil (mapcar
  14577. (lambda (x)
  14578. (if (equal true-file
  14579. (file-truename x))
  14580. nil x))
  14581. (org-agenda-files t)))))
  14582. (if (not (= (length files) (length (org-agenda-files t))))
  14583. (progn
  14584. (org-store-new-agenda-file-list files)
  14585. (org-install-agenda-files-menu)
  14586. (message "Removed file: %s" afile))
  14587. (message "File was not in list: %s (not removed)" afile))))
  14588. (defun org-file-menu-entry (file)
  14589. (vector file (list 'find-file file) t))
  14590. (defun org-check-agenda-file (file)
  14591. "Make sure FILE exists. If not, ask user what to do."
  14592. (when (not (file-exists-p file))
  14593. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14594. (abbreviate-file-name file))
  14595. (let ((r (downcase (read-char-exclusive))))
  14596. (cond
  14597. ((equal r ?r)
  14598. (org-remove-file file)
  14599. (throw 'nextfile t))
  14600. (t (error "Abort"))))))
  14601. (defun org-get-agenda-file-buffer (file)
  14602. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14603. it to the list of buffers which might be released later."
  14604. (let ((buf (org-find-base-buffer-visiting file)))
  14605. (if buf
  14606. buf ; just return it
  14607. ;; Make a new buffer and remember it
  14608. (setq buf (find-file-noselect file))
  14609. (if buf (push buf org-agenda-new-buffers))
  14610. buf)))
  14611. (defun org-release-buffers (blist)
  14612. "Release all buffers in list, asking the user for confirmation when needed.
  14613. When a buffer is unmodified, it is just killed. When modified, it is saved
  14614. \(if the user agrees) and then killed."
  14615. (let (buf file)
  14616. (while (setq buf (pop blist))
  14617. (setq file (buffer-file-name buf))
  14618. (when (and (buffer-modified-p buf)
  14619. file
  14620. (y-or-n-p (format "Save file %s? " file)))
  14621. (with-current-buffer buf (save-buffer)))
  14622. (kill-buffer buf))))
  14623. (defun org-prepare-agenda-buffers (files)
  14624. "Create buffers for all agenda files, protect archived trees and comments."
  14625. (interactive)
  14626. (let ((pa '(:org-archived t))
  14627. (pc '(:org-comment t))
  14628. (pall '(:org-archived t :org-comment t))
  14629. (inhibit-read-only t)
  14630. (rea (concat ":" org-archive-tag ":"))
  14631. bmp file re)
  14632. (save-excursion
  14633. (save-restriction
  14634. (while (setq file (pop files))
  14635. (catch 'nextfile
  14636. (if (bufferp file)
  14637. (set-buffer file)
  14638. (org-check-agenda-file file)
  14639. (set-buffer (org-get-agenda-file-buffer file)))
  14640. (widen)
  14641. (setq bmp (buffer-modified-p))
  14642. (org-refresh-category-properties)
  14643. (setq org-todo-keywords-for-agenda
  14644. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14645. (setq org-done-keywords-for-agenda
  14646. (append org-done-keywords-for-agenda org-done-keywords))
  14647. (setq org-todo-keyword-alist-for-agenda
  14648. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14649. (setq org-drawers-for-agenda
  14650. (append org-drawers-for-agenda org-drawers))
  14651. (setq org-tag-alist-for-agenda
  14652. (append org-tag-alist-for-agenda org-tag-alist))
  14653. (save-excursion
  14654. (remove-text-properties (point-min) (point-max) pall)
  14655. (when org-agenda-skip-archived-trees
  14656. (goto-char (point-min))
  14657. (while (re-search-forward rea nil t)
  14658. (if (org-on-heading-p t)
  14659. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14660. (goto-char (point-min))
  14661. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14662. (while (re-search-forward re nil t)
  14663. (add-text-properties
  14664. (match-beginning 0) (org-end-of-subtree t) pc)))
  14665. (set-buffer-modified-p bmp)))))
  14666. (setq org-todo-keywords-for-agenda
  14667. (org-uniquify org-todo-keywords-for-agenda))
  14668. (setq org-todo-keyword-alist-for-agenda
  14669. (org-uniquify org-todo-keyword-alist-for-agenda)
  14670. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14671. ;;;; Embedded LaTeX
  14672. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14673. "Keymap for the minor `org-cdlatex-mode'.")
  14674. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14675. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14676. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14677. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14678. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14679. (defvar org-cdlatex-texmathp-advice-is-done nil
  14680. "Flag remembering if we have applied the advice to texmathp already.")
  14681. (define-minor-mode org-cdlatex-mode
  14682. "Toggle the minor `org-cdlatex-mode'.
  14683. This mode supports entering LaTeX environment and math in LaTeX fragments
  14684. in Org-mode.
  14685. \\{org-cdlatex-mode-map}"
  14686. nil " OCDL" nil
  14687. (when org-cdlatex-mode (require 'cdlatex))
  14688. (unless org-cdlatex-texmathp-advice-is-done
  14689. (setq org-cdlatex-texmathp-advice-is-done t)
  14690. (defadvice texmathp (around org-math-always-on activate)
  14691. "Always return t in org-mode buffers.
  14692. This is because we want to insert math symbols without dollars even outside
  14693. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14694. an embedded LaTeX fragment, let texmathp do its job.
  14695. \\[org-cdlatex-mode-map]"
  14696. (interactive)
  14697. (let (p)
  14698. (cond
  14699. ((not (org-mode-p)) ad-do-it)
  14700. ((eq this-command 'cdlatex-math-symbol)
  14701. (setq ad-return-value t
  14702. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14703. (t
  14704. (let ((p (org-inside-LaTeX-fragment-p)))
  14705. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14706. (setq ad-return-value t
  14707. texmathp-why '("Org-mode embedded math" . 0))
  14708. (if p ad-do-it)))))))))
  14709. (defun turn-on-org-cdlatex ()
  14710. "Unconditionally turn on `org-cdlatex-mode'."
  14711. (org-cdlatex-mode 1))
  14712. (defun org-inside-LaTeX-fragment-p ()
  14713. "Test if point is inside a LaTeX fragment.
  14714. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14715. sequence appearing also before point.
  14716. Even though the matchers for math are configurable, this function assumes
  14717. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14718. delimiters are skipped when they have been removed by customization.
  14719. The return value is nil, or a cons cell with the delimiter and
  14720. and the position of this delimiter.
  14721. This function does a reasonably good job, but can locally be fooled by
  14722. for example currency specifications. For example it will assume being in
  14723. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14724. fragments that are properly closed, but during editing, we have to live
  14725. with the uncertainty caused by missing closing delimiters. This function
  14726. looks only before point, not after."
  14727. (catch 'exit
  14728. (let ((pos (point))
  14729. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14730. (lim (progn
  14731. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14732. (point)))
  14733. dd-on str (start 0) m re)
  14734. (goto-char pos)
  14735. (when dodollar
  14736. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14737. re (nth 1 (assoc "$" org-latex-regexps)))
  14738. (while (string-match re str start)
  14739. (cond
  14740. ((= (match-end 0) (length str))
  14741. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14742. ((= (match-end 0) (- (length str) 5))
  14743. (throw 'exit nil))
  14744. (t (setq start (match-end 0))))))
  14745. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14746. (goto-char pos)
  14747. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14748. (and (match-beginning 2) (throw 'exit nil))
  14749. ;; count $$
  14750. (while (re-search-backward "\\$\\$" lim t)
  14751. (setq dd-on (not dd-on)))
  14752. (goto-char pos)
  14753. (if dd-on (cons "$$" m))))))
  14754. (defun org-inside-latex-macro-p ()
  14755. "Is point inside a LaTeX macro or its arguments?"
  14756. (save-match-data
  14757. (org-in-regexp
  14758. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14759. (defun org-try-cdlatex-tab ()
  14760. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14761. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14762. - inside a LaTeX fragment, or
  14763. - after the first word in a line, where an abbreviation expansion could
  14764. insert a LaTeX environment."
  14765. (when org-cdlatex-mode
  14766. (cond
  14767. ((save-excursion
  14768. (skip-chars-backward "a-zA-Z0-9*")
  14769. (skip-chars-backward " \t")
  14770. (bolp))
  14771. (cdlatex-tab) t)
  14772. ((org-inside-LaTeX-fragment-p)
  14773. (cdlatex-tab) t)
  14774. (t nil))))
  14775. (defun org-cdlatex-underscore-caret (&optional arg)
  14776. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14777. Revert to the normal definition outside of these fragments."
  14778. (interactive "P")
  14779. (if (org-inside-LaTeX-fragment-p)
  14780. (call-interactively 'cdlatex-sub-superscript)
  14781. (let (org-cdlatex-mode)
  14782. (call-interactively (key-binding (vector last-input-event))))))
  14783. (defun org-cdlatex-math-modify (&optional arg)
  14784. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14785. Revert to the normal definition outside of these fragments."
  14786. (interactive "P")
  14787. (if (org-inside-LaTeX-fragment-p)
  14788. (call-interactively 'cdlatex-math-modify)
  14789. (let (org-cdlatex-mode)
  14790. (call-interactively (key-binding (vector last-input-event))))))
  14791. (defvar org-latex-fragment-image-overlays nil
  14792. "List of overlays carrying the images of latex fragments.")
  14793. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14794. (defun org-remove-latex-fragment-image-overlays ()
  14795. "Remove all overlays with LaTeX fragment images in current buffer."
  14796. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14797. (setq org-latex-fragment-image-overlays nil))
  14798. (defun org-preview-latex-fragment (&optional subtree)
  14799. "Preview the LaTeX fragment at point, or all locally or globally.
  14800. If the cursor is in a LaTeX fragment, create the image and overlay
  14801. it over the source code. If there is no fragment at point, display
  14802. all fragments in the current text, from one headline to the next. With
  14803. prefix SUBTREE, display all fragments in the current subtree. With a
  14804. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14805. the cursor is before the first headline,
  14806. display all fragments in the buffer.
  14807. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14808. (interactive "P")
  14809. (org-remove-latex-fragment-image-overlays)
  14810. (save-excursion
  14811. (save-restriction
  14812. (let (beg end at msg)
  14813. (cond
  14814. ((or (equal subtree '(16))
  14815. (not (save-excursion
  14816. (re-search-backward (concat "^" outline-regexp) nil t))))
  14817. (setq beg (point-min) end (point-max)
  14818. msg "Creating images for buffer...%s"))
  14819. ((equal subtree '(4))
  14820. (org-back-to-heading)
  14821. (setq beg (point) end (org-end-of-subtree t)
  14822. msg "Creating images for subtree...%s"))
  14823. (t
  14824. (if (setq at (org-inside-LaTeX-fragment-p))
  14825. (goto-char (max (point-min) (- (cdr at) 2)))
  14826. (org-back-to-heading))
  14827. (setq beg (point) end (progn (outline-next-heading) (point))
  14828. msg (if at "Creating image...%s"
  14829. "Creating images for entry...%s"))))
  14830. (message msg "")
  14831. (narrow-to-region beg end)
  14832. (goto-char beg)
  14833. (org-format-latex
  14834. (concat "ltxpng/" (file-name-sans-extension
  14835. (file-name-nondirectory
  14836. buffer-file-name)))
  14837. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14838. (message msg "done. Use `C-c C-c' to remove images.")))))
  14839. (defvar org-latex-regexps
  14840. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14841. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14842. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14843. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14844. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14845. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14846. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14847. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14848. "Regular expressions for matching embedded LaTeX.")
  14849. (defvar org-export-have-math nil) ;; dynamic scoping
  14850. (defun org-format-latex (prefix &optional dir overlays msg at
  14851. forbuffer processing-type)
  14852. "Replace LaTeX fragments with links to an image, and produce images.
  14853. Some of the options can be changed using the variable
  14854. `org-format-latex-options'."
  14855. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14856. (let* ((prefixnodir (file-name-nondirectory prefix))
  14857. (absprefix (expand-file-name prefix dir))
  14858. (todir (file-name-directory absprefix))
  14859. (opt org-format-latex-options)
  14860. (matchers (plist-get opt :matchers))
  14861. (re-list org-latex-regexps)
  14862. (org-format-latex-header-extra
  14863. (plist-get (org-infile-export-plist) :latex-header-extra))
  14864. (cnt 0) txt hash link beg end re e checkdir
  14865. executables-checked string
  14866. m n block linkfile movefile ov)
  14867. ;; Check the different regular expressions
  14868. (while (setq e (pop re-list))
  14869. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14870. block (if (nth 3 e) "\n\n" ""))
  14871. (when (member m matchers)
  14872. (goto-char (point-min))
  14873. (while (re-search-forward re nil t)
  14874. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14875. (not (get-text-property (match-beginning n)
  14876. 'org-protected))
  14877. (or (not overlays)
  14878. (not (eq (get-char-property (match-beginning n)
  14879. 'org-overlay-type)
  14880. 'org-latex-overlay))))
  14881. (setq org-export-have-math t)
  14882. (cond
  14883. ((eq processing-type 'verbatim)
  14884. ;; Leave the text verbatim, just protect it
  14885. (add-text-properties (match-beginning n) (match-end n)
  14886. '(org-protected t)))
  14887. ((eq processing-type 'mathjax)
  14888. ;; Prepare for MathJax processing
  14889. (setq string (match-string n))
  14890. (if (member m '("$" "$1"))
  14891. (save-excursion
  14892. (delete-region (match-beginning n) (match-end n))
  14893. (goto-char (match-beginning n))
  14894. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14895. "\\)")
  14896. '(org-protected t))))
  14897. (add-text-properties (match-beginning n) (match-end n)
  14898. '(org-protected t))))
  14899. ((or (eq processing-type 'dvipng) t)
  14900. ;; Process to an image
  14901. (setq txt (match-string n)
  14902. beg (match-beginning n) end (match-end n)
  14903. cnt (1+ cnt))
  14904. (let (print-length print-level) ; make sure full list is printed
  14905. (setq hash (sha1 (prin1-to-string
  14906. (list org-format-latex-header
  14907. org-format-latex-header-extra
  14908. org-export-latex-default-packages-alist
  14909. org-export-latex-packages-alist
  14910. org-format-latex-options
  14911. forbuffer txt)))
  14912. linkfile (format "%s_%s.png" prefix hash)
  14913. movefile (format "%s_%s.png" absprefix hash)))
  14914. (setq link (concat block "[[file:" linkfile "]]" block))
  14915. (if msg (message msg cnt))
  14916. (goto-char beg)
  14917. (unless checkdir ; make sure the directory exists
  14918. (setq checkdir t)
  14919. (or (file-directory-p todir) (make-directory todir t)))
  14920. (unless executables-checked
  14921. (org-check-external-command
  14922. "latex" "needed to convert LaTeX fragments to images")
  14923. (org-check-external-command
  14924. "dvipng" "needed to convert LaTeX fragments to images")
  14925. (setq executables-checked t))
  14926. (unless (file-exists-p movefile)
  14927. (org-create-formula-image
  14928. txt movefile opt forbuffer))
  14929. (if overlays
  14930. (progn
  14931. (mapc (lambda (o)
  14932. (if (eq (overlay-get o 'org-overlay-type)
  14933. 'org-latex-overlay)
  14934. (delete-overlay o)))
  14935. (overlays-in beg end))
  14936. (setq ov (make-overlay beg end))
  14937. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14938. (if (featurep 'xemacs)
  14939. (progn
  14940. (overlay-put ov 'invisible t)
  14941. (overlay-put
  14942. ov 'end-glyph
  14943. (make-glyph (vector 'png :file movefile))))
  14944. (overlay-put
  14945. ov 'display
  14946. (list 'image :type 'png :file movefile :ascent 'center)))
  14947. (push ov org-latex-fragment-image-overlays)
  14948. (goto-char end))
  14949. (delete-region beg end)
  14950. (insert (org-add-props link
  14951. (list 'org-latex-src
  14952. (replace-regexp-in-string
  14953. "\"" "" txt)))))))))))))
  14954. ;; This function borrows from Ganesh Swami's latex2png.el
  14955. (defun org-create-formula-image (string tofile options buffer)
  14956. "This calls dvipng."
  14957. (require 'org-latex)
  14958. (let* ((tmpdir (if (featurep 'xemacs)
  14959. (temp-directory)
  14960. temporary-file-directory))
  14961. (texfilebase (make-temp-name
  14962. (expand-file-name "orgtex" tmpdir)))
  14963. (texfile (concat texfilebase ".tex"))
  14964. (dvifile (concat texfilebase ".dvi"))
  14965. (pngfile (concat texfilebase ".png"))
  14966. (fnh (if (featurep 'xemacs)
  14967. (font-height (get-face-font 'default))
  14968. (face-attribute 'default :height nil)))
  14969. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14970. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14971. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14972. "Black"))
  14973. (bg (or (plist-get options (if buffer :background :html-background))
  14974. "Transparent")))
  14975. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14976. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14977. (with-temp-file texfile
  14978. (insert (org-splice-latex-header
  14979. org-format-latex-header
  14980. org-export-latex-default-packages-alist
  14981. org-export-latex-packages-alist t
  14982. org-format-latex-header-extra))
  14983. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14984. (require 'org-latex)
  14985. (org-export-latex-fix-inputenc))
  14986. (let ((dir default-directory))
  14987. (condition-case nil
  14988. (progn
  14989. (cd tmpdir)
  14990. (call-process "latex" nil nil nil texfile))
  14991. (error nil))
  14992. (cd dir))
  14993. (if (not (file-exists-p dvifile))
  14994. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14995. (condition-case nil
  14996. (call-process "dvipng" nil nil nil
  14997. "-fg" fg "-bg" bg
  14998. "-D" dpi
  14999. ;;"-x" scale "-y" scale
  15000. "-T" "tight"
  15001. "-o" pngfile
  15002. dvifile)
  15003. (error nil))
  15004. (if (not (file-exists-p pngfile))
  15005. (if org-format-latex-signal-error
  15006. (error "Failed to create png file from %s" texfile)
  15007. (message "Failed to create png file from %s" texfile)
  15008. nil)
  15009. ;; Use the requested file name and clean up
  15010. (copy-file pngfile tofile 'replace)
  15011. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15012. (delete-file (concat texfilebase e)))
  15013. pngfile))))
  15014. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15015. "Fill a LaTeX header template TPL.
  15016. In the template, the following place holders will be recognized:
  15017. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15018. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15019. [PACKAGES] \\usepackage statements for PKG
  15020. [NO-PACKAGES] do not include PKG
  15021. [EXTRA] the string EXTRA
  15022. [NO-EXTRA] do not include EXTRA
  15023. For backward compatibility, if both the positive and the negative place
  15024. holder is missing, the positive one (without the \"NO-\") will be
  15025. assumed to be present at the end of the template.
  15026. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15027. EXTRA is a string.
  15028. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15029. (let (rpl (end ""))
  15030. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15031. (setq rpl (if (or (match-end 1) (not def-pkg))
  15032. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15033. tpl (replace-match rpl t t tpl))
  15034. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15035. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15036. (setq rpl (if (or (match-end 1) (not pkg))
  15037. "" (org-latex-packages-to-string pkg snippets-p t))
  15038. tpl (replace-match rpl t t tpl))
  15039. (if pkg (setq end
  15040. (concat end "\n"
  15041. (org-latex-packages-to-string pkg snippets-p)))))
  15042. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15043. (setq rpl (if (or (match-end 1) (not extra))
  15044. "" (concat extra "\n"))
  15045. tpl (replace-match rpl t t tpl))
  15046. (if (and extra (string-match "\\S-" extra))
  15047. (setq end (concat end "\n" extra))))
  15048. (if (string-match "\\S-" end)
  15049. (concat tpl "\n" end)
  15050. tpl)))
  15051. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15052. "Turn an alist of packages into a string with the \\usepackage macros."
  15053. (setq pkg (mapconcat (lambda(p)
  15054. (cond
  15055. ((stringp p) p)
  15056. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15057. (format "%% Package %s omitted" (cadr p)))
  15058. ((equal "" (car p))
  15059. (format "\\usepackage{%s}" (cadr p)))
  15060. (t
  15061. (format "\\usepackage[%s]{%s}"
  15062. (car p) (cadr p)))))
  15063. pkg
  15064. "\n"))
  15065. (if newline (concat pkg "\n") pkg))
  15066. (defun org-dvipng-color (attr)
  15067. "Return an rgb color specification for dvipng."
  15068. (apply 'format "rgb %s %s %s"
  15069. (mapcar 'org-normalize-color
  15070. (color-values (face-attribute 'default attr nil)))))
  15071. (defun org-normalize-color (value)
  15072. "Return string to be used as color value for an RGB component."
  15073. (format "%g" (/ value 65535.0)))
  15074. ;; Image display
  15075. (defvar org-inline-image-overlays nil)
  15076. (make-variable-buffer-local 'org-inline-image-overlays)
  15077. (defun org-toggle-inline-images (&optional include-linked)
  15078. "Toggle the display of inline images.
  15079. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15080. (interactive "P")
  15081. (if org-inline-image-overlays
  15082. (progn
  15083. (org-remove-inline-images)
  15084. (message "Inline image display turned off"))
  15085. (org-display-inline-images include-linked)
  15086. (if org-inline-image-overlays
  15087. (message "%d images displayed inline"
  15088. (length org-inline-image-overlays))
  15089. (message "No images to display inline"))))
  15090. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15091. "Display inline images.
  15092. Normally only links without a description part are inlined, because this
  15093. is how it will work for export. When INCLUDE-LINKED is set, also links
  15094. with a description part will be inlined. This can be nice for a quick
  15095. look at those images, but it does not reflect what exported files will look
  15096. like.
  15097. When REFRESH is set, refresh existing images between BEG and END.
  15098. This will create new image displays only if necessary.
  15099. BEG and END default to the buffer boundaries."
  15100. (interactive "P")
  15101. (unless refresh
  15102. (org-remove-inline-images)
  15103. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15104. (save-excursion
  15105. (save-restriction
  15106. (widen)
  15107. (setq beg (or beg (point-min)) end (or end (point-max)))
  15108. (goto-char (point-min))
  15109. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15110. (substring (org-image-file-name-regexp) 0 -2)
  15111. "\\)\\]" (if include-linked "" "\\]")))
  15112. old file ov img)
  15113. (while (re-search-forward re end t)
  15114. (setq old (get-char-property-and-overlay (match-beginning 1)
  15115. 'org-image-overlay))
  15116. (setq file (expand-file-name
  15117. (concat (or (match-string 3) "") (match-string 4))))
  15118. (when (file-exists-p file)
  15119. (if (and (car-safe old) refresh)
  15120. (image-refresh (overlay-get (cdr old) 'display))
  15121. (setq img (save-match-data (create-image file)))
  15122. (when img
  15123. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15124. (overlay-put ov 'display img)
  15125. (overlay-put ov 'face 'default)
  15126. (overlay-put ov 'org-image-overlay t)
  15127. (overlay-put ov 'modification-hooks
  15128. (list 'org-display-inline-modification-hook))
  15129. (push ov org-inline-image-overlays)))))))))
  15130. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15131. "Remove inline-display overlay if a corresponding region is modified."
  15132. (let ((inhibit-modification-hooks t))
  15133. (when (and ov after)
  15134. (delete ov org-inline-image-overlays)
  15135. (delete-overlay ov))))
  15136. (defun org-remove-inline-images ()
  15137. "Remove inline display of images."
  15138. (interactive)
  15139. (mapc 'delete-overlay org-inline-image-overlays)
  15140. (setq org-inline-image-overlays nil))
  15141. ;;;; Key bindings
  15142. ;; Make `C-c C-x' a prefix key
  15143. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15144. ;; TAB key with modifiers
  15145. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15146. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15147. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15148. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15149. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15150. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15151. ;; The following line is necessary under Suse GNU/Linux
  15152. (unless (featurep 'xemacs)
  15153. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15154. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15155. (define-key org-mode-map [backtab] 'org-shifttab)
  15156. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15157. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15158. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15159. ;; Cursor keys with modifiers
  15160. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15161. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15162. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15163. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15164. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15165. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15166. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15167. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15168. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15169. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15170. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15171. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15172. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15173. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15174. ;; Babel keys
  15175. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15176. (mapc (lambda (pair)
  15177. (define-key org-babel-map (car pair) (cdr pair)))
  15178. org-babel-key-bindings)
  15179. ;;; Extra keys for tty access.
  15180. ;; We only set them when really needed because otherwise the
  15181. ;; menus don't show the simple keys
  15182. (when (or org-use-extra-keys
  15183. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15184. (not window-system))
  15185. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15186. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15187. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15188. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15189. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15190. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15191. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15192. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15193. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15194. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15195. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15196. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15197. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15198. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15199. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15200. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15201. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15202. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15203. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15204. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15205. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15206. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15207. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15208. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15209. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15210. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15211. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15212. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15213. ;; All the other keys
  15214. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15215. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15216. (if (boundp 'narrow-map)
  15217. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15218. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15219. (if (boundp 'narrow-map)
  15220. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15221. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15222. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15223. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15224. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15225. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15226. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15227. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15228. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15229. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15230. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15231. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15232. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15233. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15234. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15235. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15236. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15237. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15238. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15239. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15240. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15241. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15242. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15243. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15244. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15245. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15246. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15247. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15248. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15249. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15250. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15251. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15252. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15253. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15254. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15255. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15256. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15257. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15258. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15259. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15260. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15261. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15262. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15263. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15264. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15265. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15266. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15267. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15268. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15269. (org-defkey org-mode-map "\C-m" 'org-return)
  15270. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15271. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15272. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15273. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15274. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15275. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15276. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15277. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15278. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15279. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15280. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15281. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15282. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15283. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15284. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15285. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15286. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15287. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15288. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15289. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15290. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15291. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15292. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15293. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15294. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15295. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15296. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15297. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15298. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15299. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15300. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15301. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15302. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15303. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15304. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15305. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15306. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15307. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15308. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15309. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15310. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15311. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15312. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15313. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15314. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15315. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15316. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15317. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15318. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15319. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15320. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15321. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15322. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15323. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15324. (when (featurep 'xemacs)
  15325. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15326. (defconst org-speed-commands-default
  15327. '(
  15328. ("Outline Navigation")
  15329. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15330. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15331. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15332. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15333. ("u" . (org-speed-move-safe 'outline-up-heading))
  15334. ("j" . org-goto)
  15335. ("g" . (org-refile t))
  15336. ("Outline Visibility")
  15337. ("c" . org-cycle)
  15338. ("C" . org-shifttab)
  15339. (" " . org-display-outline-path)
  15340. ("Outline Structure Editing")
  15341. ("U" . org-shiftmetaup)
  15342. ("D" . org-shiftmetadown)
  15343. ("r" . org-metaright)
  15344. ("l" . org-metaleft)
  15345. ("R" . org-shiftmetaright)
  15346. ("L" . org-shiftmetaleft)
  15347. ("i" . (progn (forward-char 1) (call-interactively
  15348. 'org-insert-heading-respect-content)))
  15349. ("^" . org-sort)
  15350. ("w" . org-refile)
  15351. ("a" . org-archive-subtree-default-with-confirmation)
  15352. ("." . org-mark-subtree)
  15353. ("Clock Commands")
  15354. ("I" . org-clock-in)
  15355. ("O" . org-clock-out)
  15356. ("Meta Data Editing")
  15357. ("t" . org-todo)
  15358. ("0" . (org-priority ?\ ))
  15359. ("1" . (org-priority ?A))
  15360. ("2" . (org-priority ?B))
  15361. ("3" . (org-priority ?C))
  15362. (";" . org-set-tags-command)
  15363. ("e" . org-set-effort)
  15364. ("Agenda Views etc")
  15365. ("v" . org-agenda)
  15366. ("/" . org-sparse-tree)
  15367. ("Misc")
  15368. ("o" . org-open-at-point)
  15369. ("?" . org-speed-command-help)
  15370. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15371. (">" . (org-agenda-remove-restriction-lock))
  15372. )
  15373. "The default speed commands.")
  15374. (defun org-print-speed-command (e)
  15375. (if (> (length (car e)) 1)
  15376. (progn
  15377. (princ "\n")
  15378. (princ (car e))
  15379. (princ "\n")
  15380. (princ (make-string (length (car e)) ?-))
  15381. (princ "\n"))
  15382. (princ (car e))
  15383. (princ " ")
  15384. (if (symbolp (cdr e))
  15385. (princ (symbol-name (cdr e)))
  15386. (prin1 (cdr e)))
  15387. (princ "\n")))
  15388. (defun org-speed-command-help ()
  15389. "Show the available speed commands."
  15390. (interactive)
  15391. (if (not org-use-speed-commands)
  15392. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15393. (with-output-to-temp-buffer "*Help*"
  15394. (princ "User-defined Speed commands\n===========================\n")
  15395. (mapc 'org-print-speed-command org-speed-commands-user)
  15396. (princ "\n")
  15397. (princ "Built-in Speed commands\n=======================\n")
  15398. (mapc 'org-print-speed-command org-speed-commands-default))
  15399. (with-current-buffer "*Help*"
  15400. (setq truncate-lines t))))
  15401. (defun org-speed-move-safe (cmd)
  15402. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15403. If not, return to the original position and throw an error."
  15404. (interactive)
  15405. (let ((pos (point)))
  15406. (call-interactively cmd)
  15407. (unless (and (bolp) (org-on-heading-p))
  15408. (goto-char pos)
  15409. (error "Boundary reached while executing %s" cmd))))
  15410. (defvar org-self-insert-command-undo-counter 0)
  15411. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15412. (defvar org-speed-command nil)
  15413. (defun org-speed-command-default-hook (keys)
  15414. "Hook for activating single-letter speed commands.
  15415. `org-speed-commands-default' specifies a minimal command set. Use
  15416. `org-speed-commands-user' for further customization."
  15417. (when (or (and (bolp) (looking-at outline-regexp))
  15418. (and (functionp org-use-speed-commands)
  15419. (funcall org-use-speed-commands)))
  15420. (cdr (assoc keys (append org-speed-commands-user
  15421. org-speed-commands-default)))))
  15422. (defun org-babel-speed-command-hook (keys)
  15423. "Hook for activating single-letter code block commands."
  15424. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15425. (cdr (assoc keys org-babel-key-bindings))))
  15426. (defcustom org-speed-command-hook
  15427. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15428. "Hook for activating speed commands at strategic locations.
  15429. Hook functions are called in sequence until a valid handler is
  15430. found.
  15431. Each hook takes a single argument, a user-pressed command key
  15432. which is also a `self-insert-command' from the global map.
  15433. Within the hook, examine the cursor position and the command key
  15434. and return nil or a valid handler as appropriate. Handler could
  15435. be one of an interactive command, a function, or a form.
  15436. Set `org-use-speed-commands' to non-nil value to enable this
  15437. hook. The default setting is `org-speed-command-default-hook'."
  15438. :group 'org-structure
  15439. :type 'hook)
  15440. (defun org-self-insert-command (N)
  15441. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15442. If the cursor is in a table looking at whitespace, the whitespace is
  15443. overwritten, and the table is not marked as requiring realignment."
  15444. (interactive "p")
  15445. (cond
  15446. ((and org-use-speed-commands
  15447. (setq org-speed-command
  15448. (run-hook-with-args-until-success
  15449. 'org-speed-command-hook (this-command-keys))))
  15450. (cond
  15451. ((commandp org-speed-command)
  15452. (setq this-command org-speed-command)
  15453. (call-interactively org-speed-command))
  15454. ((functionp org-speed-command)
  15455. (funcall org-speed-command))
  15456. ((and org-speed-command (listp org-speed-command))
  15457. (eval org-speed-command))
  15458. (t (let (org-use-speed-commands)
  15459. (call-interactively 'org-self-insert-command)))))
  15460. ((and
  15461. (org-table-p)
  15462. (progn
  15463. ;; check if we blank the field, and if that triggers align
  15464. (and (featurep 'org-table) org-table-auto-blank-field
  15465. (member last-command
  15466. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15467. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15468. ;; got extra space, this field does not determine column width
  15469. (let (org-table-may-need-update) (org-table-blank-field))
  15470. ;; no extra space, this field may determine column width
  15471. (org-table-blank-field)))
  15472. t)
  15473. (eq N 1)
  15474. (looking-at "[^|\n]* |"))
  15475. (let (org-table-may-need-update)
  15476. (goto-char (1- (match-end 0)))
  15477. (delete-backward-char 1)
  15478. (goto-char (match-beginning 0))
  15479. (self-insert-command N)))
  15480. (t
  15481. (setq org-table-may-need-update t)
  15482. (self-insert-command N)
  15483. (org-fix-tags-on-the-fly)
  15484. (if org-self-insert-cluster-for-undo
  15485. (if (not (eq last-command 'org-self-insert-command))
  15486. (setq org-self-insert-command-undo-counter 1)
  15487. (if (>= org-self-insert-command-undo-counter 20)
  15488. (setq org-self-insert-command-undo-counter 1)
  15489. (and (> org-self-insert-command-undo-counter 0)
  15490. buffer-undo-list
  15491. (not (cadr buffer-undo-list)) ; remove nil entry
  15492. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15493. (setq org-self-insert-command-undo-counter
  15494. (1+ org-self-insert-command-undo-counter))))))))
  15495. (defun org-fix-tags-on-the-fly ()
  15496. (when (and (equal (char-after (point-at-bol)) ?*)
  15497. (org-on-heading-p))
  15498. (org-align-tags-here org-tags-column)))
  15499. (defun org-delete-backward-char (N)
  15500. "Like `delete-backward-char', insert whitespace at field end in tables.
  15501. When deleting backwards, in tables this function will insert whitespace in
  15502. front of the next \"|\" separator, to keep the table aligned. The table will
  15503. still be marked for re-alignment if the field did fill the entire column,
  15504. because, in this case the deletion might narrow the column."
  15505. (interactive "p")
  15506. (if (and (org-table-p)
  15507. (eq N 1)
  15508. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15509. (looking-at ".*?|"))
  15510. (let ((pos (point))
  15511. (noalign (looking-at "[^|\n\r]* |"))
  15512. (c org-table-may-need-update))
  15513. (backward-delete-char N)
  15514. (if (not overwrite-mode)
  15515. (progn
  15516. (skip-chars-forward "^|")
  15517. (insert " ")
  15518. (goto-char (1- pos))))
  15519. ;; noalign: if there were two spaces at the end, this field
  15520. ;; does not determine the width of the column.
  15521. (if noalign (setq org-table-may-need-update c)))
  15522. (backward-delete-char N)
  15523. (org-fix-tags-on-the-fly)))
  15524. (defun org-delete-char (N)
  15525. "Like `delete-char', but insert whitespace at field end in tables.
  15526. When deleting characters, in tables this function will insert whitespace in
  15527. front of the next \"|\" separator, to keep the table aligned. The table will
  15528. still be marked for re-alignment if the field did fill the entire column,
  15529. because, in this case the deletion might narrow the column."
  15530. (interactive "p")
  15531. (if (and (org-table-p)
  15532. (not (bolp))
  15533. (not (= (char-after) ?|))
  15534. (eq N 1))
  15535. (if (looking-at ".*?|")
  15536. (let ((pos (point))
  15537. (noalign (looking-at "[^|\n\r]* |"))
  15538. (c org-table-may-need-update))
  15539. (replace-match (concat
  15540. (substring (match-string 0) 1 -1)
  15541. " |"))
  15542. (goto-char pos)
  15543. ;; noalign: if there were two spaces at the end, this field
  15544. ;; does not determine the width of the column.
  15545. (if noalign (setq org-table-may-need-update c)))
  15546. (delete-char N))
  15547. (delete-char N)
  15548. (org-fix-tags-on-the-fly)))
  15549. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15550. (put 'org-self-insert-command 'delete-selection t)
  15551. (put 'orgtbl-self-insert-command 'delete-selection t)
  15552. (put 'org-delete-char 'delete-selection 'supersede)
  15553. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15554. (put 'org-yank 'delete-selection 'yank)
  15555. ;; Make `flyspell-mode' delay after some commands
  15556. (put 'org-self-insert-command 'flyspell-delayed t)
  15557. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15558. (put 'org-delete-char 'flyspell-delayed t)
  15559. (put 'org-delete-backward-char 'flyspell-delayed t)
  15560. ;; Make pabbrev-mode expand after org-mode commands
  15561. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15562. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15563. ;; How to do this: Measure non-white length of current string
  15564. ;; If equal to column width, we should realign.
  15565. (defun org-remap (map &rest commands)
  15566. "In MAP, remap the functions given in COMMANDS.
  15567. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15568. (let (new old)
  15569. (while commands
  15570. (setq old (pop commands) new (pop commands))
  15571. (if (fboundp 'command-remapping)
  15572. (org-defkey map (vector 'remap old) new)
  15573. (substitute-key-definition old new map global-map)))))
  15574. (when (eq org-enable-table-editor 'optimized)
  15575. ;; If the user wants maximum table support, we need to hijack
  15576. ;; some standard editing functions
  15577. (org-remap org-mode-map
  15578. 'self-insert-command 'org-self-insert-command
  15579. 'delete-char 'org-delete-char
  15580. 'delete-backward-char 'org-delete-backward-char)
  15581. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15582. (defvar org-ctrl-c-ctrl-c-hook nil
  15583. "Hook for functions attaching themselves to `C-c C-c'.
  15584. This can be used to add additional functionality to the C-c C-c key which
  15585. executes context-dependent commands.
  15586. Each function will be called with no arguments. The function must check
  15587. if the context is appropriate for it to act. If yes, it should do its
  15588. thing and then return a non-nil value. If the context is wrong,
  15589. just do nothing and return nil.")
  15590. (defvar org-tab-first-hook nil
  15591. "Hook for functions to attach themselves to TAB.
  15592. See `org-ctrl-c-ctrl-c-hook' for more information.
  15593. This hook runs as the first action when TAB is pressed, even before
  15594. `org-cycle' messes around with the `outline-regexp' to cater for
  15595. inline tasks and plain list item folding.
  15596. If any function in this hook returns t, any other actions that
  15597. would have been caused by TAB (such as table field motion or visibility
  15598. cycling) will not occur.")
  15599. (defvar org-tab-after-check-for-table-hook nil
  15600. "Hook for functions to attach themselves to TAB.
  15601. See `org-ctrl-c-ctrl-c-hook' for more information.
  15602. This hook runs after it has been established that the cursor is not in a
  15603. table, but before checking if the cursor is in a headline or if global cycling
  15604. should be done.
  15605. If any function in this hook returns t, not other actions like visibility
  15606. cycling will be done.")
  15607. (defvar org-tab-after-check-for-cycling-hook nil
  15608. "Hook for functions to attach themselves to TAB.
  15609. See `org-ctrl-c-ctrl-c-hook' for more information.
  15610. This hook runs after it has been established that not table field motion and
  15611. not visibility should be done because of current context. This is probably
  15612. the place where a package like yasnippets can hook in.")
  15613. (defvar org-tab-before-tab-emulation-hook nil
  15614. "Hook for functions to attach themselves to TAB.
  15615. See `org-ctrl-c-ctrl-c-hook' for more information.
  15616. This hook runs after every other options for TAB have been exhausted, but
  15617. before indentation and \t insertion takes place.")
  15618. (defvar org-metaleft-hook nil
  15619. "Hook for functions attaching themselves to `M-left'.
  15620. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15621. (defvar org-metaright-hook nil
  15622. "Hook for functions attaching themselves to `M-right'.
  15623. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15624. (defvar org-metaup-hook nil
  15625. "Hook for functions attaching themselves to `M-up'.
  15626. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15627. (defvar org-metadown-hook nil
  15628. "Hook for functions attaching themselves to `M-down'.
  15629. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15630. (defvar org-shiftmetaleft-hook nil
  15631. "Hook for functions attaching themselves to `M-S-left'.
  15632. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15633. (defvar org-shiftmetaright-hook nil
  15634. "Hook for functions attaching themselves to `M-S-right'.
  15635. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15636. (defvar org-shiftmetaup-hook nil
  15637. "Hook for functions attaching themselves to `M-S-up'.
  15638. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15639. (defvar org-shiftmetadown-hook nil
  15640. "Hook for functions attaching themselves to `M-S-down'.
  15641. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15642. (defvar org-metareturn-hook nil
  15643. "Hook for functions attaching themselves to `M-RET'.
  15644. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15645. (defvar org-shiftup-hook nil
  15646. "Hook for functions attaching themselves to `S-up'.
  15647. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15648. (defvar org-shiftup-final-hook nil
  15649. "Hook for functions attaching themselves to `S-up'.
  15650. This one runs after all other options except shift-select have been excluded.
  15651. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15652. (defvar org-shiftdown-hook nil
  15653. "Hook for functions attaching themselves to `S-down'.
  15654. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15655. (defvar org-shiftdown-final-hook nil
  15656. "Hook for functions attaching themselves to `S-down'.
  15657. This one runs after all other options except shift-select have been excluded.
  15658. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15659. (defvar org-shiftleft-hook nil
  15660. "Hook for functions attaching themselves to `S-left'.
  15661. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15662. (defvar org-shiftleft-final-hook nil
  15663. "Hook for functions attaching themselves to `S-left'.
  15664. This one runs after all other options except shift-select have been excluded.
  15665. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15666. (defvar org-shiftright-hook nil
  15667. "Hook for functions attaching themselves to `S-right'.
  15668. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15669. (defvar org-shiftright-final-hook nil
  15670. "Hook for functions attaching themselves to `S-right'.
  15671. This one runs after all other options except shift-select have been excluded.
  15672. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15673. (defun org-modifier-cursor-error ()
  15674. "Throw an error, a modified cursor command was applied in wrong context."
  15675. (error "This command is active in special context like tables, headlines or items"))
  15676. (defun org-shiftselect-error ()
  15677. "Throw an error because Shift-Cursor command was applied in wrong context."
  15678. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15679. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15680. (error "This command works only in special context like headlines or timestamps")))
  15681. (defun org-call-for-shift-select (cmd)
  15682. (let ((this-command-keys-shift-translated t))
  15683. (call-interactively cmd)))
  15684. (defun org-shifttab (&optional arg)
  15685. "Global visibility cycling or move to previous table field.
  15686. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15687. on context.
  15688. See the individual commands for more information."
  15689. (interactive "P")
  15690. (cond
  15691. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15692. ((integerp arg)
  15693. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15694. (message "Content view to level: %d" arg)
  15695. (org-content (prefix-numeric-value arg2))
  15696. (setq org-cycle-global-status 'overview)))
  15697. (t (call-interactively 'org-global-cycle))))
  15698. (defun org-shiftmetaleft ()
  15699. "Promote subtree or delete table column.
  15700. Calls `org-promote-subtree', `org-outdent-item',
  15701. or `org-table-delete-column', depending on context.
  15702. See the individual commands for more information."
  15703. (interactive)
  15704. (cond
  15705. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15706. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15707. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15708. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15709. (t (org-modifier-cursor-error))))
  15710. (defun org-shiftmetaright ()
  15711. "Demote subtree or insert table column.
  15712. Calls `org-demote-subtree', `org-indent-item',
  15713. or `org-table-insert-column', depending on context.
  15714. See the individual commands for more information."
  15715. (interactive)
  15716. (cond
  15717. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15718. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15719. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15720. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15721. (t (org-modifier-cursor-error))))
  15722. (defun org-shiftmetaup (&optional arg)
  15723. "Move subtree up or kill table row.
  15724. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15725. `org-move-item-up' depending on context. See the individual commands
  15726. for more information."
  15727. (interactive "P")
  15728. (cond
  15729. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15730. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15731. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15732. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15733. (t (org-modifier-cursor-error))))
  15734. (defun org-shiftmetadown (&optional arg)
  15735. "Move subtree down or insert table row.
  15736. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15737. `org-move-item-down', depending on context. See the individual
  15738. commands for more information."
  15739. (interactive "P")
  15740. (cond
  15741. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15742. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15743. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15744. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15745. (t (org-modifier-cursor-error))))
  15746. (defsubst org-hidden-tree-error ()
  15747. (error
  15748. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15749. (defun org-metaleft (&optional arg)
  15750. "Promote heading or move table column to left.
  15751. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15752. With no specific context, calls the Emacs default `backward-word'.
  15753. See the individual commands for more information."
  15754. (interactive "P")
  15755. (cond
  15756. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15757. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15758. ((org-with-limited-levels
  15759. (or (org-on-heading-p)
  15760. (and (org-region-active-p)
  15761. (save-excursion
  15762. (goto-char (region-beginning))
  15763. (org-on-heading-p)))))
  15764. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15765. (call-interactively 'org-do-promote))
  15766. ;; At an inline task.
  15767. ((org-on-heading-p)
  15768. (call-interactively 'org-inlinetask-promote))
  15769. ((or (org-at-item-p)
  15770. (and (org-region-active-p)
  15771. (save-excursion
  15772. (goto-char (region-beginning))
  15773. (org-at-item-p))))
  15774. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15775. (call-interactively 'org-outdent-item))
  15776. (t (call-interactively 'backward-word))))
  15777. (defun org-metaright (&optional arg)
  15778. "Demote subtree or move table column to right.
  15779. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15780. With no specific context, calls the Emacs default `forward-word'.
  15781. See the individual commands for more information."
  15782. (interactive "P")
  15783. (cond
  15784. ((run-hook-with-args-until-success 'org-metaright-hook))
  15785. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15786. ((org-with-limited-levels
  15787. (or (org-on-heading-p)
  15788. (and (org-region-active-p)
  15789. (save-excursion
  15790. (goto-char (region-beginning))
  15791. (org-on-heading-p)))))
  15792. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15793. (call-interactively 'org-do-demote))
  15794. ;; At an inline task.
  15795. ((org-on-heading-p)
  15796. (call-interactively 'org-inlinetask-demote))
  15797. ((or (org-at-item-p)
  15798. (and (org-region-active-p)
  15799. (save-excursion
  15800. (goto-char (region-beginning))
  15801. (org-at-item-p))))
  15802. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15803. (call-interactively 'org-indent-item))
  15804. (t (call-interactively 'forward-word))))
  15805. (defun org-check-for-hidden (what)
  15806. "Check if there are hidden headlines/items in the current visual line.
  15807. WHAT can be either `headlines' or `items'. If the current line is
  15808. an outline or item heading and it has a folded subtree below it,
  15809. this function returns t, nil otherwise."
  15810. (let ((re (cond
  15811. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15812. ((eq what 'items) (org-item-beginning-re))
  15813. (t (error "This should not happen"))))
  15814. beg end)
  15815. (save-excursion
  15816. (catch 'exit
  15817. (unless (org-region-active-p)
  15818. (setq beg (point-at-bol))
  15819. (beginning-of-line 2)
  15820. (while (and (not (eobp)) ;; this is like `next-line'
  15821. (get-char-property (1- (point)) 'invisible))
  15822. (beginning-of-line 2))
  15823. (setq end (point))
  15824. (goto-char beg)
  15825. (goto-char (point-at-eol))
  15826. (setq end (max end (point)))
  15827. (while (re-search-forward re end t)
  15828. (if (get-char-property (match-beginning 0) 'invisible)
  15829. (throw 'exit t))))
  15830. nil))))
  15831. (defun org-metaup (&optional arg)
  15832. "Move subtree up or move table row up.
  15833. Calls `org-move-subtree-up' or `org-table-move-row' or
  15834. `org-move-item-up', depending on context. See the individual commands
  15835. for more information."
  15836. (interactive "P")
  15837. (cond
  15838. ((run-hook-with-args-until-success 'org-metaup-hook))
  15839. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15840. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15841. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15842. (t (transpose-lines 1) (beginning-of-line -1))))
  15843. (defun org-metadown (&optional arg)
  15844. "Move subtree down or move table row down.
  15845. Calls `org-move-subtree-down' or `org-table-move-row' or
  15846. `org-move-item-down', depending on context. See the individual
  15847. commands for more information."
  15848. (interactive "P")
  15849. (cond
  15850. ((run-hook-with-args-until-success 'org-metadown-hook))
  15851. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15852. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15853. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15854. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15855. (defun org-shiftup (&optional arg)
  15856. "Increase item in timestamp or increase priority of current headline.
  15857. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15858. depending on context. See the individual commands for more information."
  15859. (interactive "P")
  15860. (cond
  15861. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15862. ((and org-support-shift-select (org-region-active-p))
  15863. (org-call-for-shift-select 'previous-line))
  15864. ((org-at-timestamp-p t)
  15865. (call-interactively (if org-edit-timestamp-down-means-later
  15866. 'org-timestamp-down 'org-timestamp-up)))
  15867. ((and (not (eq org-support-shift-select 'always))
  15868. org-enable-priority-commands
  15869. (org-on-heading-p))
  15870. (call-interactively 'org-priority-up))
  15871. ((and (not org-support-shift-select) (org-at-item-p))
  15872. (call-interactively 'org-previous-item))
  15873. ((org-clocktable-try-shift 'up arg))
  15874. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15875. (org-support-shift-select
  15876. (org-call-for-shift-select 'previous-line))
  15877. (t (org-shiftselect-error))))
  15878. (defun org-shiftdown (&optional arg)
  15879. "Decrease item in timestamp or decrease priority of current headline.
  15880. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15881. depending on context. See the individual commands for more information."
  15882. (interactive "P")
  15883. (cond
  15884. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15885. ((and org-support-shift-select (org-region-active-p))
  15886. (org-call-for-shift-select 'next-line))
  15887. ((org-at-timestamp-p t)
  15888. (call-interactively (if org-edit-timestamp-down-means-later
  15889. 'org-timestamp-up 'org-timestamp-down)))
  15890. ((and (not (eq org-support-shift-select 'always))
  15891. org-enable-priority-commands
  15892. (org-on-heading-p))
  15893. (call-interactively 'org-priority-down))
  15894. ((and (not org-support-shift-select) (org-at-item-p))
  15895. (call-interactively 'org-next-item))
  15896. ((org-clocktable-try-shift 'down arg))
  15897. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15898. (org-support-shift-select
  15899. (org-call-for-shift-select 'next-line))
  15900. (t (org-shiftselect-error))))
  15901. (defun org-shiftright (&optional arg)
  15902. "Cycle the thing at point or in the current line, depending on context.
  15903. Depending on context, this does one of the following:
  15904. - switch a timestamp at point one day into the future
  15905. - on a headline, switch to the next TODO keyword.
  15906. - on an item, switch entire list to the next bullet type
  15907. - on a property line, switch to the next allowed value
  15908. - on a clocktable definition line, move time block into the future"
  15909. (interactive "P")
  15910. (cond
  15911. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15912. ((and org-support-shift-select (org-region-active-p))
  15913. (org-call-for-shift-select 'forward-char))
  15914. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15915. ((and (not (eq org-support-shift-select 'always))
  15916. (org-on-heading-p))
  15917. (let ((org-inhibit-logging
  15918. (not org-treat-S-cursor-todo-selection-as-state-change))
  15919. (org-inhibit-blocking
  15920. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15921. (org-call-with-arg 'org-todo 'right)))
  15922. ((or (and org-support-shift-select
  15923. (not (eq org-support-shift-select 'always))
  15924. (org-at-item-bullet-p))
  15925. (and (not org-support-shift-select) (org-at-item-p)))
  15926. (org-call-with-arg 'org-cycle-list-bullet nil))
  15927. ((and (not (eq org-support-shift-select 'always))
  15928. (org-at-property-p))
  15929. (call-interactively 'org-property-next-allowed-value))
  15930. ((org-clocktable-try-shift 'right arg))
  15931. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15932. (org-support-shift-select
  15933. (org-call-for-shift-select 'forward-char))
  15934. (t (org-shiftselect-error))))
  15935. (defun org-shiftleft (&optional arg)
  15936. "Cycle the thing at point or in the current line, depending on context.
  15937. Depending on context, this does one of the following:
  15938. - switch a timestamp at point one day into the past
  15939. - on a headline, switch to the previous TODO keyword.
  15940. - on an item, switch entire list to the previous bullet type
  15941. - on a property line, switch to the previous allowed value
  15942. - on a clocktable definition line, move time block into the past"
  15943. (interactive "P")
  15944. (cond
  15945. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15946. ((and org-support-shift-select (org-region-active-p))
  15947. (org-call-for-shift-select 'backward-char))
  15948. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15949. ((and (not (eq org-support-shift-select 'always))
  15950. (org-on-heading-p))
  15951. (let ((org-inhibit-logging
  15952. (not org-treat-S-cursor-todo-selection-as-state-change))
  15953. (org-inhibit-blocking
  15954. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15955. (org-call-with-arg 'org-todo 'left)))
  15956. ((or (and org-support-shift-select
  15957. (not (eq org-support-shift-select 'always))
  15958. (org-at-item-bullet-p))
  15959. (and (not org-support-shift-select) (org-at-item-p)))
  15960. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15961. ((and (not (eq org-support-shift-select 'always))
  15962. (org-at-property-p))
  15963. (call-interactively 'org-property-previous-allowed-value))
  15964. ((org-clocktable-try-shift 'left arg))
  15965. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15966. (org-support-shift-select
  15967. (org-call-for-shift-select 'backward-char))
  15968. (t (org-shiftselect-error))))
  15969. (defun org-shiftcontrolright ()
  15970. "Switch to next TODO set."
  15971. (interactive)
  15972. (cond
  15973. ((and org-support-shift-select (org-region-active-p))
  15974. (org-call-for-shift-select 'forward-word))
  15975. ((and (not (eq org-support-shift-select 'always))
  15976. (org-on-heading-p))
  15977. (org-call-with-arg 'org-todo 'nextset))
  15978. (org-support-shift-select
  15979. (org-call-for-shift-select 'forward-word))
  15980. (t (org-shiftselect-error))))
  15981. (defun org-shiftcontrolleft ()
  15982. "Switch to previous TODO set."
  15983. (interactive)
  15984. (cond
  15985. ((and org-support-shift-select (org-region-active-p))
  15986. (org-call-for-shift-select 'backward-word))
  15987. ((and (not (eq org-support-shift-select 'always))
  15988. (org-on-heading-p))
  15989. (org-call-with-arg 'org-todo 'previousset))
  15990. (org-support-shift-select
  15991. (org-call-for-shift-select 'backward-word))
  15992. (t (org-shiftselect-error))))
  15993. (defun org-ctrl-c-ret ()
  15994. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15995. (interactive)
  15996. (cond
  15997. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15998. (t (call-interactively 'org-insert-heading))))
  15999. (defun org-copy-special ()
  16000. "Copy region in table or copy current subtree.
  16001. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16002. See the individual commands for more information."
  16003. (interactive)
  16004. (call-interactively
  16005. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16006. (defun org-cut-special ()
  16007. "Cut region in table or cut current subtree.
  16008. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16009. See the individual commands for more information."
  16010. (interactive)
  16011. (call-interactively
  16012. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16013. (defun org-paste-special (arg)
  16014. "Paste rectangular region into table, or past subtree relative to level.
  16015. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16016. See the individual commands for more information."
  16017. (interactive "P")
  16018. (if (org-at-table-p)
  16019. (org-table-paste-rectangle)
  16020. (org-paste-subtree arg)))
  16021. (defun org-edit-special (&optional arg)
  16022. "Call a special editor for the stuff at point.
  16023. When at a table, call the formula editor with `org-table-edit-formulas'.
  16024. When at the first line of an src example, call `org-edit-src-code'.
  16025. When in an #+include line, visit the include file. Otherwise call
  16026. `ffap' to visit the file at point."
  16027. (interactive)
  16028. ;; possibly prep session before editing source
  16029. (when arg
  16030. (let* ((info (org-babel-get-src-block-info))
  16031. (lang (nth 0 info))
  16032. (params (nth 2 info))
  16033. (session (cdr (assoc :session params))))
  16034. (when (and info session) ;; we are in a source-code block with a session
  16035. (funcall
  16036. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16037. (cond ;; proceed with `org-edit-special'
  16038. ((save-excursion
  16039. (beginning-of-line 1)
  16040. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16041. (find-file (org-trim (match-string 1))))
  16042. ((org-edit-src-code))
  16043. ((org-edit-fixed-width-region))
  16044. ((org-at-table.el-p)
  16045. (org-edit-src-code))
  16046. ((or (org-at-table-p)
  16047. (save-excursion
  16048. (beginning-of-line 1)
  16049. (looking-at "[ \t]*#\\+TBLFM:")))
  16050. (call-interactively 'org-table-edit-formulas))
  16051. (t (call-interactively 'ffap))))
  16052. (defun org-ctrl-c-ctrl-c (&optional arg)
  16053. "Set tags in headline, or update according to changed information at point.
  16054. This command does many different things, depending on context:
  16055. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16056. this is what we do.
  16057. - If the cursor is on a statistics cookie, update it.
  16058. - If the cursor is in a headline, prompt for tags and insert them
  16059. into the current line, aligned to `org-tags-column'. When called
  16060. with prefix arg, realign all tags in the current buffer.
  16061. - If the cursor is in one of the special #+KEYWORD lines, this
  16062. triggers scanning the buffer for these lines and updating the
  16063. information.
  16064. - If the cursor is inside a table, realign the table. This command
  16065. works even if the automatic table editor has been turned off.
  16066. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16067. the entire table.
  16068. - If the cursor is at a footnote reference or definition, jump to
  16069. the corresponding definition or references, respectively.
  16070. - If the cursor is a the beginning of a dynamic block, update it.
  16071. - If the current buffer is a capture buffer, close note and file it.
  16072. - If the cursor is on a <<<target>>>, update radio targets and
  16073. corresponding links in this buffer.
  16074. - If the cursor is on a numbered item in a plain list, renumber the
  16075. ordered list.
  16076. - If the cursor is on a checkbox, toggle it.
  16077. - If the cursor is on a code block, evaluate it. The variable
  16078. `org-confirm-babel-evaluate' can be used to control prompting
  16079. before code block evaluation, by default every code block
  16080. evaluation requires confirmation. Code block evaluation can be
  16081. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16082. (interactive "P")
  16083. (let ((org-enable-table-editor t))
  16084. (cond
  16085. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16086. org-occur-highlights
  16087. org-latex-fragment-image-overlays)
  16088. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16089. (org-remove-occur-highlights)
  16090. (org-remove-latex-fragment-image-overlays)
  16091. (message "Temporary highlights/overlays removed from current buffer"))
  16092. ((and (local-variable-p 'org-finish-function (current-buffer))
  16093. (fboundp org-finish-function))
  16094. (funcall org-finish-function))
  16095. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16096. ((or (looking-at org-property-start-re)
  16097. (org-at-property-p))
  16098. (call-interactively 'org-property-action))
  16099. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16100. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16101. (or (org-on-heading-p) (org-at-item-p)))
  16102. (call-interactively 'org-update-statistics-cookies))
  16103. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16104. ((org-at-table.el-p)
  16105. (message "Use C-c ' to edit table.el tables"))
  16106. ((org-at-table-p)
  16107. (org-table-maybe-eval-formula)
  16108. (if arg
  16109. (call-interactively 'org-table-recalculate)
  16110. (org-table-maybe-recalculate-line))
  16111. (call-interactively 'org-table-align)
  16112. (orgtbl-send-table 'maybe))
  16113. ((or (org-footnote-at-reference-p)
  16114. (org-footnote-at-definition-p))
  16115. (call-interactively 'org-footnote-action))
  16116. ((org-at-item-checkbox-p)
  16117. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16118. ;; list only if at top item.
  16119. (let* ((cbox (match-string 1))
  16120. (struct (org-list-struct))
  16121. (old-struct (copy-tree struct))
  16122. (parents (org-list-parents-alist struct))
  16123. (prevs (org-list-prevs-alist struct))
  16124. (orderedp (org-entry-get nil "ORDERED"))
  16125. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16126. block-item)
  16127. ;; Use a light version of `org-toggle-checkbox' to avoid
  16128. ;; computing list structure twice.
  16129. (org-list-set-checkbox (point-at-bol) struct
  16130. (cond
  16131. ((equal arg '(16)) "[-]")
  16132. ((equal arg '(4)) nil)
  16133. ((equal "[X]" cbox) "[ ]")
  16134. (t "[X]")))
  16135. (org-list-struct-fix-ind struct parents)
  16136. (org-list-struct-fix-bul struct prevs)
  16137. (setq block-item
  16138. (org-list-struct-fix-box struct parents prevs orderedp))
  16139. (when block-item
  16140. (message
  16141. "Checkboxes were removed due to unchecked box at line %d"
  16142. (org-current-line block-item)))
  16143. (org-list-struct-apply-struct struct old-struct)
  16144. (org-update-checkbox-count-maybe)
  16145. (when firstp (org-list-send-list 'maybe))))
  16146. ((org-at-item-p)
  16147. ;; Cursor at an item: repair list. Do checkbox related actions
  16148. ;; only if function was called with an argument. Send list only
  16149. ;; if at top item.
  16150. (let* ((struct (org-list-struct))
  16151. (old-struct (copy-tree struct))
  16152. (parents (org-list-parents-alist struct))
  16153. (prevs (org-list-prevs-alist struct))
  16154. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16155. (org-list-struct-fix-ind struct parents)
  16156. (org-list-struct-fix-bul struct prevs)
  16157. (when arg
  16158. (org-list-set-checkbox (point-at-bol) struct "[ ]")
  16159. (org-list-struct-fix-box struct parents prevs))
  16160. (org-list-struct-apply-struct struct old-struct)
  16161. (when arg (org-update-checkbox-count-maybe))
  16162. (when firstp (org-list-send-list 'maybe))))
  16163. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16164. ;; Dynamic block
  16165. (beginning-of-line 1)
  16166. (save-excursion (org-update-dblock)))
  16167. ((save-excursion
  16168. (beginning-of-line 1)
  16169. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16170. (cond
  16171. ((equal (match-string 1) "TBLFM")
  16172. ;; Recalculate the table before this line
  16173. (save-excursion
  16174. (beginning-of-line 1)
  16175. (skip-chars-backward " \r\n\t")
  16176. (if (org-at-table-p)
  16177. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16178. (t
  16179. (let ((org-inhibit-startup-visibility-stuff t)
  16180. (org-startup-align-all-tables nil))
  16181. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16182. (message "Local setup has been refreshed"))))
  16183. ((org-clock-update-time-maybe))
  16184. (t (error "C-c C-c can do nothing useful at this location")))))
  16185. (defun org-mode-restart ()
  16186. "Restart Org-mode, to scan again for special lines.
  16187. Also updates the keyword regular expressions."
  16188. (interactive)
  16189. (org-mode)
  16190. (message "Org-mode restarted"))
  16191. (defun org-kill-note-or-show-branches ()
  16192. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16193. (interactive)
  16194. (if (not org-finish-function)
  16195. (progn
  16196. (hide-subtree)
  16197. (call-interactively 'show-branches))
  16198. (let ((org-note-abort t))
  16199. (funcall org-finish-function))))
  16200. (defun org-return (&optional indent)
  16201. "Goto next table row or insert a newline.
  16202. Calls `org-table-next-row' or `newline', depending on context.
  16203. See the individual commands for more information."
  16204. (interactive)
  16205. (cond
  16206. ((bobp) (if indent (newline-and-indent) (newline)))
  16207. ((org-at-table-p)
  16208. (org-table-justify-field-maybe)
  16209. (call-interactively 'org-table-next-row))
  16210. ;; when `newline-and-indent' is called within a list, make sure
  16211. ;; text moved stays inside the item.
  16212. ((and (org-in-item-p) indent)
  16213. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16214. (progn
  16215. (newline)
  16216. (org-indent-line-to (length (match-string 0))))
  16217. (let ((ind (org-get-indentation)))
  16218. (newline)
  16219. (if (org-looking-back org-list-end-re)
  16220. (org-indent-line-function)
  16221. (org-indent-line-to ind)))))
  16222. ((and org-return-follows-link
  16223. (eq (get-text-property (point) 'face) 'org-link))
  16224. (call-interactively 'org-open-at-point))
  16225. ((and (org-at-heading-p)
  16226. (looking-at
  16227. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16228. (org-show-entry)
  16229. (end-of-line 1)
  16230. (newline))
  16231. (t (if indent (newline-and-indent) (newline)))))
  16232. (defun org-return-indent ()
  16233. "Goto next table row or insert a newline and indent.
  16234. Calls `org-table-next-row' or `newline-and-indent', depending on
  16235. context. See the individual commands for more information."
  16236. (interactive)
  16237. (org-return t))
  16238. (defun org-ctrl-c-star ()
  16239. "Compute table, or change heading status of lines.
  16240. Calls `org-table-recalculate' or `org-toggle-heading',
  16241. depending on context."
  16242. (interactive)
  16243. (cond
  16244. ((org-at-table-p)
  16245. (call-interactively 'org-table-recalculate))
  16246. (t
  16247. ;; Convert all lines in region to list items
  16248. (call-interactively 'org-toggle-heading))))
  16249. (defun org-ctrl-c-minus ()
  16250. "Insert separator line in table or modify bullet status of line.
  16251. Also turns a plain line or a region of lines into list items.
  16252. Calls `org-table-insert-hline', `org-toggle-item', or
  16253. `org-cycle-list-bullet', depending on context."
  16254. (interactive)
  16255. (cond
  16256. ((org-at-table-p)
  16257. (call-interactively 'org-table-insert-hline))
  16258. ((org-region-active-p)
  16259. (call-interactively 'org-toggle-item))
  16260. ((org-in-item-p)
  16261. (call-interactively 'org-cycle-list-bullet))
  16262. (t
  16263. (call-interactively 'org-toggle-item))))
  16264. (defun org-toggle-item (arg)
  16265. "Convert headings or normal lines to items, items to normal lines.
  16266. If there is no active region, only the current line is considered.
  16267. If the first non blank line in the region is an headline, convert
  16268. all headlines to items.
  16269. If it is an item, convert all items to normal lines.
  16270. If it is normal text, change region into an item. With a prefix
  16271. argument ARG, change each line in region into an item."
  16272. (interactive "P")
  16273. (let (l2 l beg end)
  16274. (if (org-region-active-p)
  16275. (setq beg (region-beginning) end (region-end))
  16276. (setq beg (point-at-bol)
  16277. end (min (1+ (point-at-eol)) (point-max))))
  16278. (org-with-limited-levels
  16279. (save-excursion
  16280. (goto-char end)
  16281. (setq l2 (org-current-line))
  16282. (goto-char beg)
  16283. (beginning-of-line 1)
  16284. ;; Ignore blank lines at beginning of region
  16285. (skip-chars-forward " \t\r\n")
  16286. (beginning-of-line 1)
  16287. (setq l (1- (org-current-line)))
  16288. (cond
  16289. ;; Case 1. Start at an item: de-itemize.
  16290. ((org-at-item-p)
  16291. (while (< (setq l (1+ l)) l2)
  16292. (when (org-at-item-p)
  16293. (skip-chars-forward " \t")
  16294. (delete-region (point) (match-end 0)))
  16295. (beginning-of-line 2)))
  16296. ;; Case 2. Start an an heading: convert to items.
  16297. ((org-on-heading-p)
  16298. (let* ((bul (org-list-bullet-string "-"))
  16299. (len (length bul))
  16300. (ind 0) (level 0))
  16301. (while (< (setq l (1+ l)) l2)
  16302. (cond
  16303. ((looking-at outline-regexp)
  16304. (let* ((lvl (org-reduced-level
  16305. (- (length (match-string 0)) 2)))
  16306. (s (concat (make-string (* len lvl) ? ) bul)))
  16307. (replace-match s t t)
  16308. (setq ind (length s) level lvl)))
  16309. ;; Ignore blank lines and inline tasks.
  16310. ((looking-at "^[ \t]*$"))
  16311. ((looking-at "^\\*+ "))
  16312. ;; Ensure normal text belongs to the new item.
  16313. (t (org-indent-line-to (+ (max (- (org-get-indentation) level 2) 0)
  16314. ind))))
  16315. (beginning-of-line 2))))
  16316. ;; Case 3. Normal line with ARG: turn each of them into items
  16317. ;; unless they are already one.
  16318. (arg
  16319. (while (< (setq l (1+ l)) l2)
  16320. (unless (or (org-on-heading-p) (org-at-item-p))
  16321. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16322. (replace-match
  16323. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16324. (beginning-of-line 2)))
  16325. ;; Case 4. Normal line without ARG: make the first line of
  16326. ;; region an item, and shift indentation of others
  16327. ;; lines to set them as item's body.
  16328. (t (let* ((bul (org-list-bullet-string "-"))
  16329. (bul-len (length bul))
  16330. (ref-ind (org-get-indentation)))
  16331. (skip-chars-forward " \t")
  16332. (insert bul)
  16333. (beginning-of-line 2)
  16334. (while (and (< (setq l (1+ l)) l2) (< (point) end))
  16335. ;; Ensure that lines less indented than first one
  16336. ;; still get included in item body.
  16337. (org-indent-line-to (+ (max ref-ind (org-get-indentation))
  16338. bul-len))
  16339. (beginning-of-line 2)))))))))
  16340. (defun org-toggle-heading (&optional nstars)
  16341. "Convert headings to normal text, or items or text to headings.
  16342. If there is no active region, only the current line is considered.
  16343. If the first non blank line is an headline, remove the stars from
  16344. all headlines in the region.
  16345. If it is a plain list item, turn all plain list items into headings.
  16346. If it is a normal line, turn each and every normal line (i.e. not
  16347. an heading or an item) in the region into a heading.
  16348. When converting a line into a heading, the number of stars is chosen
  16349. such that the lines become children of the current entry. However,
  16350. when a prefix argument is given, its value determines the number of
  16351. stars to add."
  16352. (interactive "P")
  16353. (let (l2 l itemp beg end)
  16354. (if (org-region-active-p)
  16355. (setq beg (region-beginning) end (copy-marker (region-end)))
  16356. (setq beg (point-at-bol)
  16357. end (min (1+ (point-at-eol)) (point-max))))
  16358. ;; Ensure inline tasks don't count as headings.
  16359. (org-with-limited-levels
  16360. (save-excursion
  16361. (goto-char end)
  16362. (setq l2 (org-current-line))
  16363. (goto-char beg)
  16364. (beginning-of-line 1)
  16365. ;; Ignore blank lines at beginning of region
  16366. (skip-chars-forward " \t\r\n")
  16367. (beginning-of-line 1)
  16368. (setq l (1- (org-current-line)))
  16369. (cond
  16370. ;; Case 1. Started at an heading: de-star headings.
  16371. ((org-on-heading-p)
  16372. (while (< (setq l (1+ l)) l2)
  16373. (when (org-on-heading-p t)
  16374. (looking-at outline-regexp) (replace-match ""))
  16375. (beginning-of-line 2)))
  16376. ;; Case 2. Started at an item: change items into headlines.
  16377. ((org-at-item-p)
  16378. (let ((stars (make-string
  16379. (if nstars
  16380. (prefix-numeric-value current-prefix-arg)
  16381. (or (org-current-level) 0))
  16382. ?*)))
  16383. (while (< (point) end)
  16384. (when (org-at-item-p)
  16385. ;; Pay attention to cases when region ends before list.
  16386. (let* ((struct (org-list-struct))
  16387. (list-end (min (org-list-get-bottom-point struct) end)))
  16388. (save-restriction
  16389. (narrow-to-region (point) list-end)
  16390. (insert
  16391. (org-list-to-subtree
  16392. (org-list-parse-list t)
  16393. '(:istart (concat stars (funcall get-stars depth))
  16394. :icount (concat stars
  16395. (funcall get-stars depth))))))))
  16396. (beginning-of-line 2))))
  16397. ;; Case 3. Started at normal text: make every line an heading,
  16398. ;; skipping headlines and items.
  16399. (t (let* ((stars (make-string
  16400. (if nstars
  16401. (prefix-numeric-value current-prefix-arg)
  16402. (or (org-current-level) 0))
  16403. ?*))
  16404. (add-stars (cond (nstars "")
  16405. ((equal stars "") "*")
  16406. (org-odd-levels-only "**")
  16407. (t "*")))
  16408. (rpl (concat stars add-stars " ")))
  16409. (while (< (setq l (1+ l)) l2)
  16410. (unless (or (org-on-heading-p) (org-at-item-p))
  16411. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16412. (replace-match (concat rpl (match-string 2)))))
  16413. (beginning-of-line 2)))))))))
  16414. (defun org-meta-return (&optional arg)
  16415. "Insert a new heading or wrap a region in a table.
  16416. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16417. See the individual commands for more information."
  16418. (interactive "P")
  16419. (cond
  16420. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16421. ((org-at-table-p)
  16422. (call-interactively 'org-table-wrap-region))
  16423. (t (call-interactively 'org-insert-heading))))
  16424. ;;; Menu entries
  16425. ;; Define the Org-mode menus
  16426. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16427. '("Tbl"
  16428. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16429. ["Next Field" org-cycle (org-at-table-p)]
  16430. ["Previous Field" org-shifttab (org-at-table-p)]
  16431. ["Next Row" org-return (org-at-table-p)]
  16432. "--"
  16433. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16434. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16435. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16436. "--"
  16437. ("Column"
  16438. ["Move Column Left" org-metaleft (org-at-table-p)]
  16439. ["Move Column Right" org-metaright (org-at-table-p)]
  16440. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16441. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16442. ("Row"
  16443. ["Move Row Up" org-metaup (org-at-table-p)]
  16444. ["Move Row Down" org-metadown (org-at-table-p)]
  16445. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16446. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16447. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16448. "--"
  16449. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16450. ("Rectangle"
  16451. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16452. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16453. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16454. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16455. "--"
  16456. ("Calculate"
  16457. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16458. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16459. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16460. "--"
  16461. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16462. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16463. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16464. "--"
  16465. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16466. "--"
  16467. ["Sum Column/Rectangle" org-table-sum
  16468. (or (org-at-table-p) (org-region-active-p))]
  16469. ["Which Column?" org-table-current-column (org-at-table-p)])
  16470. ["Debug Formulas"
  16471. org-table-toggle-formula-debugger
  16472. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16473. ["Show Col/Row Numbers"
  16474. org-table-toggle-coordinate-overlays
  16475. :style toggle
  16476. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16477. "--"
  16478. ["Create" org-table-create (and (not (org-at-table-p))
  16479. org-enable-table-editor)]
  16480. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16481. ["Import from File" org-table-import (not (org-at-table-p))]
  16482. ["Export to File" org-table-export (org-at-table-p)]
  16483. "--"
  16484. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16485. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16486. '("Org"
  16487. ("Show/Hide"
  16488. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16489. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16490. ["Sparse Tree..." org-sparse-tree t]
  16491. ["Reveal Context" org-reveal t]
  16492. ["Show All" show-all t]
  16493. "--"
  16494. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16495. "--"
  16496. ["New Heading" org-insert-heading t]
  16497. ("Navigate Headings"
  16498. ["Up" outline-up-heading t]
  16499. ["Next" outline-next-visible-heading t]
  16500. ["Previous" outline-previous-visible-heading t]
  16501. ["Next Same Level" outline-forward-same-level t]
  16502. ["Previous Same Level" outline-backward-same-level t]
  16503. "--"
  16504. ["Jump" org-goto t])
  16505. ("Edit Structure"
  16506. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16507. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16508. "--"
  16509. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16510. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16511. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16512. "--"
  16513. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16514. "--"
  16515. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16516. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16517. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16518. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16519. "--"
  16520. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16521. "--"
  16522. ["Convert to odd levels" org-convert-to-odd-levels t]
  16523. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16524. ("Editing"
  16525. ["Emphasis..." org-emphasize t]
  16526. ["Edit Source Example" org-edit-special t]
  16527. "--"
  16528. ["Footnote new/jump" org-footnote-action t]
  16529. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16530. ("Archive"
  16531. ["Archive (default method)" org-archive-subtree-default t]
  16532. "--"
  16533. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16534. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16535. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16536. )
  16537. "--"
  16538. ("Hyperlinks"
  16539. ["Store Link (Global)" org-store-link t]
  16540. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16541. ["Insert Link" org-insert-link t]
  16542. ["Follow Link" org-open-at-point t]
  16543. "--"
  16544. ["Next link" org-next-link t]
  16545. ["Previous link" org-previous-link t]
  16546. "--"
  16547. ["Descriptive Links"
  16548. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16549. :style radio
  16550. :selected (member '(org-link) buffer-invisibility-spec)]
  16551. ["Literal Links"
  16552. (progn
  16553. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16554. :style radio
  16555. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16556. "--"
  16557. ("TODO Lists"
  16558. ["TODO/DONE/-" org-todo t]
  16559. ("Select keyword"
  16560. ["Next keyword" org-shiftright (org-on-heading-p)]
  16561. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16562. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16563. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16564. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16565. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16566. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16567. "--"
  16568. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16569. :selected org-enforce-todo-dependencies :style toggle :active t]
  16570. "Settings for tree at point"
  16571. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16572. :selected (org-entry-get nil "ORDERED")
  16573. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16574. ["Do Children parallel" org-toggle-ordered-property :style radio
  16575. :selected (not (org-entry-get nil "ORDERED"))
  16576. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16577. "--"
  16578. ["Set Priority" org-priority t]
  16579. ["Priority Up" org-shiftup t]
  16580. ["Priority Down" org-shiftdown t]
  16581. "--"
  16582. ["Get news from all feeds" org-feed-update-all t]
  16583. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16584. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16585. ("TAGS and Properties"
  16586. ["Set Tags" org-set-tags-command t]
  16587. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16588. "--"
  16589. ["Set property" org-set-property t]
  16590. ["Column view of properties" org-columns t]
  16591. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16592. ("Dates and Scheduling"
  16593. ["Timestamp" org-time-stamp t]
  16594. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16595. ("Change Date"
  16596. ["1 Day Later" org-shiftright t]
  16597. ["1 Day Earlier" org-shiftleft t]
  16598. ["1 ... Later" org-shiftup t]
  16599. ["1 ... Earlier" org-shiftdown t])
  16600. ["Compute Time Range" org-evaluate-time-range t]
  16601. ["Schedule Item" org-schedule t]
  16602. ["Deadline" org-deadline t]
  16603. "--"
  16604. ["Custom time format" org-toggle-time-stamp-overlays
  16605. :style radio :selected org-display-custom-times]
  16606. "--"
  16607. ["Goto Calendar" org-goto-calendar t]
  16608. ["Date from Calendar" org-date-from-calendar t]
  16609. "--"
  16610. ["Start/Restart Timer" org-timer-start t]
  16611. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16612. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16613. ["Insert Timer String" org-timer t]
  16614. ["Insert Timer Item" org-timer-item t])
  16615. ("Logging work"
  16616. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16617. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16618. ["Clock out" org-clock-out t]
  16619. ["Clock cancel" org-clock-cancel t]
  16620. "--"
  16621. ["Mark as default task" org-clock-mark-default-task t]
  16622. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16623. ["Goto running clock" org-clock-goto t]
  16624. "--"
  16625. ["Display times" org-clock-display t]
  16626. ["Create clock table" org-clock-report t]
  16627. "--"
  16628. ["Record DONE time"
  16629. (progn (setq org-log-done (not org-log-done))
  16630. (message "Switching to %s will %s record a timestamp"
  16631. (car org-done-keywords)
  16632. (if org-log-done "automatically" "not")))
  16633. :style toggle :selected org-log-done])
  16634. "--"
  16635. ["Agenda Command..." org-agenda t]
  16636. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16637. ("File List for Agenda")
  16638. ("Special views current file"
  16639. ["TODO Tree" org-show-todo-tree t]
  16640. ["Check Deadlines" org-check-deadlines t]
  16641. ["Timeline" org-timeline t]
  16642. ["Tags/Property tree" org-match-sparse-tree t])
  16643. "--"
  16644. ["Export/Publish..." org-export t]
  16645. ("LaTeX"
  16646. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16647. :selected org-cdlatex-mode]
  16648. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16649. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16650. ["Modify math symbol" org-cdlatex-math-modify
  16651. (org-inside-LaTeX-fragment-p)]
  16652. ["Insert citation" org-reftex-citation t]
  16653. "--"
  16654. ["Template for BEAMER" org-insert-beamer-options-template t])
  16655. "--"
  16656. ("MobileOrg"
  16657. ["Push Files and Views" org-mobile-push t]
  16658. ["Get Captured and Flagged" org-mobile-pull t]
  16659. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16660. "--"
  16661. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16662. "--"
  16663. ("Documentation"
  16664. ["Show Version" org-version t]
  16665. ["Info Documentation" org-info t])
  16666. ("Customize"
  16667. ["Browse Org Group" org-customize t]
  16668. "--"
  16669. ["Expand This Menu" org-create-customize-menu
  16670. (fboundp 'customize-menu-create)])
  16671. ["Send bug report" org-submit-bug-report t]
  16672. "--"
  16673. ("Refresh/Reload"
  16674. ["Refresh setup current buffer" org-mode-restart t]
  16675. ["Reload Org (after update)" org-reload t]
  16676. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16677. ))
  16678. (defun org-info (&optional node)
  16679. "Read documentation for Org-mode in the info system.
  16680. With optional NODE, go directly to that node."
  16681. (interactive)
  16682. (info (format "(org)%s" (or node ""))))
  16683. ;;;###autoload
  16684. (defun org-submit-bug-report ()
  16685. "Submit a bug report on Org-mode via mail.
  16686. Don't hesitate to report any problems or inaccurate documentation.
  16687. If you don't have setup sending mail from (X)Emacs, please copy the
  16688. output buffer into your mail program, as it gives us important
  16689. information about your Org-mode version and configuration."
  16690. (interactive)
  16691. (require 'reporter)
  16692. (org-load-modules-maybe)
  16693. (org-require-autoloaded-modules)
  16694. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16695. (reporter-submit-bug-report
  16696. "emacs-orgmode@gnu.org"
  16697. (org-version)
  16698. (let (list)
  16699. (save-window-excursion
  16700. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16701. (delete-other-windows)
  16702. (erase-buffer)
  16703. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16704. We would like to add your full Org-mode and Outline configuration to the
  16705. bug report. This greatly simplifies the work of the maintainer and
  16706. other experts on the mailing list.
  16707. HOWEVER, some variables you have customized may contain private
  16708. information. The names of customers, colleagues, or friends, might
  16709. appear in the form of file names, tags, todo states, or search strings.
  16710. If you answer yes to the prompt, you might want to check and remove
  16711. such private information before sending the email.")
  16712. (add-text-properties (point-min) (point-max) '(face org-warning))
  16713. (when (yes-or-no-p "Include your Org-mode configuration ")
  16714. (mapatoms
  16715. (lambda (v)
  16716. (and (boundp v)
  16717. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16718. (or (and (symbol-value v)
  16719. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16720. (and
  16721. (get v 'custom-type) (get v 'standard-value)
  16722. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16723. (push v list)))))
  16724. (kill-buffer (get-buffer "*Warn about privacy*"))
  16725. list))
  16726. nil nil
  16727. "Remember to cover the basics, that is, what you expected to happen and
  16728. what in fact did happen. You don't know how to make a good report? See
  16729. http://orgmode.org/manual/Feedback.html#Feedback
  16730. Your bug report will be posted to the Org-mode mailing list.
  16731. ------------------------------------------------------------------------")
  16732. (save-excursion
  16733. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16734. (replace-match "\\1Bug: \\3 [\\2]")))))
  16735. (defun org-install-agenda-files-menu ()
  16736. (let ((bl (buffer-list)))
  16737. (save-excursion
  16738. (while bl
  16739. (set-buffer (pop bl))
  16740. (if (org-mode-p) (setq bl nil)))
  16741. (when (org-mode-p)
  16742. (easy-menu-change
  16743. '("Org") "File List for Agenda"
  16744. (append
  16745. (list
  16746. ["Edit File List" (org-edit-agenda-file-list) t]
  16747. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16748. ["Remove Current File from List" org-remove-file t]
  16749. ["Cycle through agenda files" org-cycle-agenda-files t]
  16750. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16751. "--")
  16752. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16753. ;;;; Documentation
  16754. ;;;###autoload
  16755. (defun org-require-autoloaded-modules ()
  16756. (interactive)
  16757. (mapc 'require
  16758. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16759. org-docbook org-exp org-html org-icalendar
  16760. org-id org-latex
  16761. org-publish org-remember org-table
  16762. org-timer org-xoxo)))
  16763. ;;;###autoload
  16764. (defun org-reload (&optional uncompiled)
  16765. "Reload all org lisp files.
  16766. With prefix arg UNCOMPILED, load the uncompiled versions."
  16767. (interactive "P")
  16768. (require 'find-func)
  16769. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16770. (dir-org (file-name-directory (org-find-library-name "org")))
  16771. (dir-org-contrib (ignore-errors
  16772. (file-name-directory
  16773. (org-find-library-name "org-contribdir"))))
  16774. (babel-files
  16775. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16776. (append (list nil "comint" "eval" "exp" "keys"
  16777. "lob" "ref" "table" "tangle")
  16778. (delq nil
  16779. (mapcar
  16780. (lambda (lang)
  16781. (when (cdr lang) (symbol-name (car lang))))
  16782. org-babel-load-languages)))))
  16783. (files
  16784. (append (directory-files dir-org t file-re)
  16785. babel-files
  16786. (and dir-org-contrib
  16787. (directory-files dir-org-contrib t file-re))))
  16788. (remove-re (concat (if (featurep 'xemacs)
  16789. "org-colview" "org-colview-xemacs")
  16790. "\\'")))
  16791. (setq files (mapcar 'file-name-sans-extension files))
  16792. (setq files (mapcar
  16793. (lambda (x) (if (string-match remove-re x) nil x))
  16794. files))
  16795. (setq files (delq nil files))
  16796. (mapc
  16797. (lambda (f)
  16798. (when (featurep (intern (file-name-nondirectory f)))
  16799. (if (and (not uncompiled)
  16800. (file-exists-p (concat f ".elc")))
  16801. (load (concat f ".elc") nil nil t)
  16802. (load (concat f ".el") nil nil t))))
  16803. files))
  16804. (org-version))
  16805. ;;;###autoload
  16806. (defun org-customize ()
  16807. "Call the customize function with org as argument."
  16808. (interactive)
  16809. (org-load-modules-maybe)
  16810. (org-require-autoloaded-modules)
  16811. (customize-browse 'org))
  16812. (defun org-create-customize-menu ()
  16813. "Create a full customization menu for Org-mode, insert it into the menu."
  16814. (interactive)
  16815. (org-load-modules-maybe)
  16816. (org-require-autoloaded-modules)
  16817. (if (fboundp 'customize-menu-create)
  16818. (progn
  16819. (easy-menu-change
  16820. '("Org") "Customize"
  16821. `(["Browse Org group" org-customize t]
  16822. "--"
  16823. ,(customize-menu-create 'org)
  16824. ["Set" Custom-set t]
  16825. ["Save" Custom-save t]
  16826. ["Reset to Current" Custom-reset-current t]
  16827. ["Reset to Saved" Custom-reset-saved t]
  16828. ["Reset to Standard Settings" Custom-reset-standard t]))
  16829. (message "\"Org\"-menu now contains full customization menu"))
  16830. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16831. ;;;; Miscellaneous stuff
  16832. ;;; Generally useful functions
  16833. (defun org-get-at-bol (property)
  16834. "Get text property PROPERTY at beginning of line."
  16835. (get-text-property (point-at-bol) property))
  16836. (defun org-find-text-property-in-string (prop s)
  16837. "Return the first non-nil value of property PROP in string S."
  16838. (or (get-text-property 0 prop s)
  16839. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16840. prop s)))
  16841. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16842. "Display the given MESSAGE as a warning."
  16843. (if (fboundp 'display-warning)
  16844. (display-warning 'org message
  16845. (if (featurep 'xemacs) 'warning :warning))
  16846. (let ((buf (get-buffer-create "*Org warnings*")))
  16847. (with-current-buffer buf
  16848. (goto-char (point-max))
  16849. (insert "Warning (Org): " message)
  16850. (unless (bolp)
  16851. (newline)))
  16852. (display-buffer buf)
  16853. (sit-for 0))))
  16854. (defun org-eval (form)
  16855. "Eval FORM and return result."
  16856. (condition-case error
  16857. (eval form)
  16858. (error (format "%%![Error: %s]" error))))
  16859. (defun org-in-commented-line ()
  16860. "Is point in a line starting with `#'?"
  16861. (equal (char-after (point-at-bol)) ?#))
  16862. (defun org-in-indented-comment-line ()
  16863. "Is point in a line starting with `#' after some white space?"
  16864. (save-excursion
  16865. (save-match-data
  16866. (goto-char (point-at-bol))
  16867. (looking-at "[ \t]*#"))))
  16868. (defun org-in-verbatim-emphasis ()
  16869. (save-match-data
  16870. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16871. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16872. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16873. (if (and marker (marker-buffer marker)
  16874. (buffer-live-p (marker-buffer marker)))
  16875. (progn
  16876. (switch-to-buffer (marker-buffer marker))
  16877. (if (or (> marker (point-max)) (< marker (point-min)))
  16878. (widen))
  16879. (goto-char marker)
  16880. (org-show-context 'org-goto))
  16881. (if bookmark
  16882. (bookmark-jump bookmark)
  16883. (error "Cannot find location"))))
  16884. (defun org-quote-csv-field (s)
  16885. "Quote field for inclusion in CSV material."
  16886. (if (string-match "[\",]" s)
  16887. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16888. s))
  16889. (defun org-force-self-insert (N)
  16890. "Needed to enforce self-insert under remapping."
  16891. (interactive "p")
  16892. (self-insert-command N))
  16893. (defun org-string-width (s)
  16894. "Compute width of string, ignoring invisible characters.
  16895. This ignores character with invisibility property `org-link', and also
  16896. characters with property `org-cwidth', because these will become invisible
  16897. upon the next fontification round."
  16898. (let (b l)
  16899. (when (or (eq t buffer-invisibility-spec)
  16900. (assq 'org-link buffer-invisibility-spec))
  16901. (while (setq b (text-property-any 0 (length s)
  16902. 'invisible 'org-link s))
  16903. (setq s (concat (substring s 0 b)
  16904. (substring s (or (next-single-property-change
  16905. b 'invisible s) (length s)))))))
  16906. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16907. (setq s (concat (substring s 0 b)
  16908. (substring s (or (next-single-property-change
  16909. b 'org-cwidth s) (length s))))))
  16910. (setq l (string-width s) b -1)
  16911. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16912. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16913. l))
  16914. (defun org-shorten-string (s maxlength)
  16915. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  16916. If the string is shorter or has length MAXLENGTH, just return the
  16917. original string. If it is longer, the functions finds a space in the
  16918. string, breaks this string off at that locations and adds three dots
  16919. as ellipsis. Including the ellipsis, the string will not be longer
  16920. than MAXLENGTH. If finding a good breaking point in the string does
  16921. not work, the string is just chopped off in the middle of a word
  16922. if necessary."
  16923. (if (<= (length s) maxlength)
  16924. s
  16925. (let* ((n (max (- maxlength 4) 1))
  16926. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  16927. (if (string-match re s)
  16928. (concat (match-string 1 s) "...")
  16929. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  16930. (defun org-get-indentation (&optional line)
  16931. "Get the indentation of the current line, interpreting tabs.
  16932. When LINE is given, assume it represents a line and compute its indentation."
  16933. (if line
  16934. (if (string-match "^ *" (org-remove-tabs line))
  16935. (match-end 0))
  16936. (save-excursion
  16937. (beginning-of-line 1)
  16938. (skip-chars-forward " \t")
  16939. (current-column))))
  16940. (defun org-get-string-indentation (s)
  16941. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  16942. (let ((n -1) (i 0) (w tab-width) c)
  16943. (catch 'exit
  16944. (while (< (setq n (1+ n)) (length s))
  16945. (setq c (aref s n))
  16946. (cond ((= c ?\ ) (setq i (1+ i)))
  16947. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  16948. (t (throw 'exit t)))))
  16949. i))
  16950. (defun org-remove-tabs (s &optional width)
  16951. "Replace tabulators in S with spaces.
  16952. Assumes that s is a single line, starting in column 0."
  16953. (setq width (or width tab-width))
  16954. (while (string-match "\t" s)
  16955. (setq s (replace-match
  16956. (make-string
  16957. (- (* width (/ (+ (match-beginning 0) width) width))
  16958. (match-beginning 0)) ?\ )
  16959. t t s)))
  16960. s)
  16961. (defun org-fix-indentation (line ind)
  16962. "Fix indentation in LINE.
  16963. IND is a cons cell with target and minimum indentation.
  16964. If the current indentation in LINE is smaller than the minimum,
  16965. leave it alone. If it is larger than ind, set it to the target."
  16966. (let* ((l (org-remove-tabs line))
  16967. (i (org-get-indentation l))
  16968. (i1 (car ind)) (i2 (cdr ind)))
  16969. (if (>= i i2) (setq l (substring line i2)))
  16970. (if (> i1 0)
  16971. (concat (make-string i1 ?\ ) l)
  16972. l)))
  16973. (defun org-remove-indentation (code &optional n)
  16974. "Remove the maximum common indentation from the lines in CODE.
  16975. N may optionally be the number of spaces to remove."
  16976. (with-temp-buffer
  16977. (insert code)
  16978. (org-do-remove-indentation n)
  16979. (buffer-string)))
  16980. (defun org-do-remove-indentation (&optional n)
  16981. "Remove the maximum common indentation from the buffer."
  16982. (untabify (point-min) (point-max))
  16983. (let ((min 10000) re)
  16984. (if n
  16985. (setq min n)
  16986. (goto-char (point-min))
  16987. (while (re-search-forward "^ *[^ \n]" nil t)
  16988. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16989. (unless (or (= min 0) (= min 10000))
  16990. (setq re (format "^ \\{%d\\}" min))
  16991. (goto-char (point-min))
  16992. (while (re-search-forward re nil t)
  16993. (replace-match "")
  16994. (end-of-line 1))
  16995. min)))
  16996. (defun org-fill-template (template alist)
  16997. "Find each %key of ALIST in TEMPLATE and replace it."
  16998. (let ((case-fold-search nil)
  16999. entry key value)
  17000. (setq alist (sort (copy-sequence alist)
  17001. (lambda (a b) (< (length (car a)) (length (car b))))))
  17002. (while (setq entry (pop alist))
  17003. (setq template
  17004. (replace-regexp-in-string
  17005. (concat "%" (regexp-quote (car entry)))
  17006. (cdr entry) template t t)))
  17007. template))
  17008. (defun org-base-buffer (buffer)
  17009. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17010. (if (not buffer)
  17011. buffer
  17012. (or (buffer-base-buffer buffer)
  17013. buffer)))
  17014. (defun org-trim (s)
  17015. "Remove whitespace at beginning and end of string."
  17016. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17017. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17018. s)
  17019. (defun org-wrap (string &optional width lines)
  17020. "Wrap string to either a number of lines, or a width in characters.
  17021. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17022. that costs. If there is a word longer than WIDTH, the text is actually
  17023. wrapped to the length of that word.
  17024. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17025. many lines, whatever width that takes.
  17026. The return value is a list of lines, without newlines at the end."
  17027. (let* ((words (org-split-string string "[ \t\n]+"))
  17028. (maxword (apply 'max (mapcar 'org-string-width words)))
  17029. w ll)
  17030. (cond (width
  17031. (org-do-wrap words (max maxword width)))
  17032. (lines
  17033. (setq w maxword)
  17034. (setq ll (org-do-wrap words maxword))
  17035. (if (<= (length ll) lines)
  17036. ll
  17037. (setq ll words)
  17038. (while (> (length ll) lines)
  17039. (setq w (1+ w))
  17040. (setq ll (org-do-wrap words w)))
  17041. ll))
  17042. (t (error "Cannot wrap this")))))
  17043. (defun org-do-wrap (words width)
  17044. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17045. (let (lines line)
  17046. (while words
  17047. (setq line (pop words))
  17048. (while (and words (< (+ (length line) (length (car words))) width))
  17049. (setq line (concat line " " (pop words))))
  17050. (setq lines (push line lines)))
  17051. (nreverse lines)))
  17052. (defun org-split-string (string &optional separators)
  17053. "Splits STRING into substrings at SEPARATORS.
  17054. No empty strings are returned if there are matches at the beginning
  17055. and end of string."
  17056. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17057. (start 0)
  17058. notfirst
  17059. (list nil))
  17060. (while (and (string-match rexp string
  17061. (if (and notfirst
  17062. (= start (match-beginning 0))
  17063. (< start (length string)))
  17064. (1+ start) start))
  17065. (< (match-beginning 0) (length string)))
  17066. (setq notfirst t)
  17067. (or (eq (match-beginning 0) 0)
  17068. (and (eq (match-beginning 0) (match-end 0))
  17069. (eq (match-beginning 0) start))
  17070. (setq list
  17071. (cons (substring string start (match-beginning 0))
  17072. list)))
  17073. (setq start (match-end 0)))
  17074. (or (eq start (length string))
  17075. (setq list
  17076. (cons (substring string start)
  17077. list)))
  17078. (nreverse list)))
  17079. (defun org-quote-vert (s)
  17080. "Replace \"|\" with \"\\vert\"."
  17081. (while (string-match "|" s)
  17082. (setq s (replace-match "\\vert" t t s)))
  17083. s)
  17084. (defun org-uuidgen-p (s)
  17085. "Is S an ID created by UUIDGEN?"
  17086. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17087. (defun org-context ()
  17088. "Return a list of contexts of the current cursor position.
  17089. If several contexts apply, all are returned.
  17090. Each context entry is a list with a symbol naming the context, and
  17091. two positions indicating start and end of the context. Possible
  17092. contexts are:
  17093. :headline anywhere in a headline
  17094. :headline-stars on the leading stars in a headline
  17095. :todo-keyword on a TODO keyword (including DONE) in a headline
  17096. :tags on the TAGS in a headline
  17097. :priority on the priority cookie in a headline
  17098. :item on the first line of a plain list item
  17099. :item-bullet on the bullet/number of a plain list item
  17100. :checkbox on the checkbox in a plain list item
  17101. :table in an org-mode table
  17102. :table-special on a special filed in a table
  17103. :table-table in a table.el table
  17104. :link on a hyperlink
  17105. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17106. :target on a <<target>>
  17107. :radio-target on a <<<radio-target>>>
  17108. :latex-fragment on a LaTeX fragment
  17109. :latex-preview on a LaTeX fragment with overlayed preview image
  17110. This function expects the position to be visible because it uses font-lock
  17111. faces as a help to recognize the following contexts: :table-special, :link,
  17112. and :keyword."
  17113. (let* ((f (get-text-property (point) 'face))
  17114. (faces (if (listp f) f (list f)))
  17115. (p (point)) clist o)
  17116. ;; First the large context
  17117. (cond
  17118. ((org-on-heading-p t)
  17119. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17120. (when (progn
  17121. (beginning-of-line 1)
  17122. (looking-at org-todo-line-tags-regexp))
  17123. (push (org-point-in-group p 1 :headline-stars) clist)
  17124. (push (org-point-in-group p 2 :todo-keyword) clist)
  17125. (push (org-point-in-group p 4 :tags) clist))
  17126. (goto-char p)
  17127. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17128. (if (looking-at "\\[#[A-Z0-9]\\]")
  17129. (push (org-point-in-group p 0 :priority) clist)))
  17130. ((org-at-item-p)
  17131. (push (org-point-in-group p 2 :item-bullet) clist)
  17132. (push (list :item (point-at-bol)
  17133. (save-excursion (org-end-of-item) (point)))
  17134. clist)
  17135. (and (org-at-item-checkbox-p)
  17136. (push (org-point-in-group p 0 :checkbox) clist)))
  17137. ((org-at-table-p)
  17138. (push (list :table (org-table-begin) (org-table-end)) clist)
  17139. (if (memq 'org-formula faces)
  17140. (push (list :table-special
  17141. (previous-single-property-change p 'face)
  17142. (next-single-property-change p 'face)) clist)))
  17143. ((org-at-table-p 'any)
  17144. (push (list :table-table) clist)))
  17145. (goto-char p)
  17146. ;; Now the small context
  17147. (cond
  17148. ((org-at-timestamp-p)
  17149. (push (org-point-in-group p 0 :timestamp) clist))
  17150. ((memq 'org-link faces)
  17151. (push (list :link
  17152. (previous-single-property-change p 'face)
  17153. (next-single-property-change p 'face)) clist))
  17154. ((memq 'org-special-keyword faces)
  17155. (push (list :keyword
  17156. (previous-single-property-change p 'face)
  17157. (next-single-property-change p 'face)) clist))
  17158. ((org-on-target-p)
  17159. (push (org-point-in-group p 0 :target) clist)
  17160. (goto-char (1- (match-beginning 0)))
  17161. (if (looking-at org-radio-target-regexp)
  17162. (push (org-point-in-group p 0 :radio-target) clist))
  17163. (goto-char p))
  17164. ((setq o (car (delq nil
  17165. (mapcar
  17166. (lambda (x)
  17167. (if (memq x org-latex-fragment-image-overlays) x))
  17168. (overlays-at (point))))))
  17169. (push (list :latex-fragment
  17170. (overlay-start o) (overlay-end o)) clist)
  17171. (push (list :latex-preview
  17172. (overlay-start o) (overlay-end o)) clist))
  17173. ((org-inside-LaTeX-fragment-p)
  17174. ;; FIXME: positions wrong.
  17175. (push (list :latex-fragment (point) (point)) clist)))
  17176. (setq clist (nreverse (delq nil clist)))
  17177. clist))
  17178. ;; FIXME: Compare with at-regexp-p Do we need both?
  17179. (defun org-in-regexp (re &optional nlines visually)
  17180. "Check if point is inside a match of regexp.
  17181. Normally only the current line is checked, but you can include NLINES extra
  17182. lines both before and after point into the search.
  17183. If VISUALLY is set, require that the cursor is not after the match but
  17184. really on, so that the block visually is on the match."
  17185. (catch 'exit
  17186. (let ((pos (point))
  17187. (eol (point-at-eol (+ 1 (or nlines 0))))
  17188. (inc (if visually 1 0)))
  17189. (save-excursion
  17190. (beginning-of-line (- 1 (or nlines 0)))
  17191. (while (re-search-forward re eol t)
  17192. (if (and (<= (match-beginning 0) pos)
  17193. (>= (+ inc (match-end 0)) pos))
  17194. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17195. (defun org-at-regexp-p (regexp)
  17196. "Is point inside a match of REGEXP in the current line?"
  17197. (catch 'exit
  17198. (save-excursion
  17199. (let ((pos (point)) (end (point-at-eol)))
  17200. (beginning-of-line 1)
  17201. (while (re-search-forward regexp end t)
  17202. (if (and (<= (match-beginning 0) pos)
  17203. (>= (match-end 0) pos))
  17204. (throw 'exit t)))
  17205. nil))))
  17206. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  17207. "Return t if the current point is between matches of START-RE and END-RE.
  17208. This will also return t if point is on one of the two matches or
  17209. in an unfinished block. END-RE can be a string or a form
  17210. returning a string.
  17211. An optional third argument bounds the search for START-RE. It
  17212. defaults to previous heading or `point-min'."
  17213. (let ((pos (point))
  17214. (limit (or bound (save-excursion (outline-previous-heading)))))
  17215. (save-excursion
  17216. ;; we're on a block when point is on start-re...
  17217. (or (org-at-regexp-p start-re)
  17218. ;; ... or start-re can be found above...
  17219. (and (re-search-backward start-re limit t)
  17220. ;; ... but no end-re between start-re and point.
  17221. (not (re-search-forward (eval end-re) pos t)))))))
  17222. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17223. "Call `multi-occur' with buffers for all agenda files."
  17224. (interactive "sOrg-files matching: \np")
  17225. (let* ((files (org-agenda-files))
  17226. (tnames (mapcar 'file-truename files))
  17227. (extra org-agenda-text-search-extra-files)
  17228. f)
  17229. (when (eq (car extra) 'agenda-archives)
  17230. (setq extra (cdr extra))
  17231. (setq files (org-add-archive-files files)))
  17232. (while (setq f (pop extra))
  17233. (unless (member (file-truename f) tnames)
  17234. (add-to-list 'files f 'append)
  17235. (add-to-list 'tnames (file-truename f) 'append)))
  17236. (multi-occur
  17237. (mapcar (lambda (x)
  17238. (with-current-buffer
  17239. (or (get-file-buffer x) (find-file-noselect x))
  17240. (widen)
  17241. (current-buffer)))
  17242. files)
  17243. regexp)))
  17244. (if (boundp 'occur-mode-find-occurrence-hook)
  17245. ;; Emacs 23
  17246. (add-hook 'occur-mode-find-occurrence-hook
  17247. (lambda ()
  17248. (when (org-mode-p)
  17249. (org-reveal))))
  17250. ;; Emacs 22
  17251. (defadvice occur-mode-goto-occurrence
  17252. (after org-occur-reveal activate)
  17253. (and (org-mode-p) (org-reveal)))
  17254. (defadvice occur-mode-goto-occurrence-other-window
  17255. (after org-occur-reveal activate)
  17256. (and (org-mode-p) (org-reveal)))
  17257. (defadvice occur-mode-display-occurrence
  17258. (after org-occur-reveal activate)
  17259. (when (org-mode-p)
  17260. (let ((pos (occur-mode-find-occurrence)))
  17261. (with-current-buffer (marker-buffer pos)
  17262. (save-excursion
  17263. (goto-char pos)
  17264. (org-reveal)))))))
  17265. (defun org-occur-link-in-agenda-files ()
  17266. "Create a link and search for it in the agendas.
  17267. The link is not stored in `org-stored-links', it is just created
  17268. for the search purpose."
  17269. (interactive)
  17270. (let ((link (condition-case nil
  17271. (org-store-link nil)
  17272. (error "Unable to create a link to here"))))
  17273. (org-occur-in-agenda-files (regexp-quote link))))
  17274. (defun org-uniquify (list)
  17275. "Remove duplicate elements from LIST."
  17276. (let (res)
  17277. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  17278. res))
  17279. (defun org-delete-all (elts list)
  17280. "Remove all elements in ELTS from LIST."
  17281. (while elts
  17282. (setq list (delete (pop elts) list)))
  17283. list)
  17284. (defun org-count (cl-item cl-seq)
  17285. "Count the number of occurrences of ITEM in SEQ.
  17286. Taken from `count' in cl-seq.el with all keyword arguments removed."
  17287. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  17288. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  17289. (while (< cl-start cl-end)
  17290. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  17291. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  17292. (setq cl-start (1+ cl-start)))
  17293. cl-count))
  17294. (defun org-remove-if (predicate seq)
  17295. "Remove everything from SEQ that fulfills PREDICATE."
  17296. (let (res e)
  17297. (while seq
  17298. (setq e (pop seq))
  17299. (if (not (funcall predicate e)) (push e res)))
  17300. (nreverse res)))
  17301. (defun org-remove-if-not (predicate seq)
  17302. "Remove everything from SEQ that does not fulfill PREDICATE."
  17303. (let (res e)
  17304. (while seq
  17305. (setq e (pop seq))
  17306. (if (funcall predicate e) (push e res)))
  17307. (nreverse res)))
  17308. (defun org-back-over-empty-lines ()
  17309. "Move backwards over whitespace, to the beginning of the first empty line.
  17310. Returns the number of empty lines passed."
  17311. (let ((pos (point)))
  17312. (if (cdr (assoc 'heading org-blank-before-new-entry))
  17313. (skip-chars-backward " \t\n\r")
  17314. (forward-line -1))
  17315. (beginning-of-line 2)
  17316. (goto-char (min (point) pos))
  17317. (count-lines (point) pos)))
  17318. (defun org-skip-whitespace ()
  17319. (skip-chars-forward " \t\n\r"))
  17320. (defun org-point-in-group (point group &optional context)
  17321. "Check if POINT is in match-group GROUP.
  17322. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  17323. match. If the match group does not exist or point is not inside it,
  17324. return nil."
  17325. (and (match-beginning group)
  17326. (>= point (match-beginning group))
  17327. (<= point (match-end group))
  17328. (if context
  17329. (list context (match-beginning group) (match-end group))
  17330. t)))
  17331. (defun org-switch-to-buffer-other-window (&rest args)
  17332. "Switch to buffer in a second window on the current frame.
  17333. In particular, do not allow pop-up frames.
  17334. Returns the newly created buffer."
  17335. (let (pop-up-frames special-display-buffer-names special-display-regexps
  17336. special-display-function)
  17337. (apply 'switch-to-buffer-other-window args)))
  17338. (defun org-combine-plists (&rest plists)
  17339. "Create a single property list from all plists in PLISTS.
  17340. The process starts by copying the first list, and then setting properties
  17341. from the other lists. Settings in the last list are the most significant
  17342. ones and overrule settings in the other lists."
  17343. (let ((rtn (copy-sequence (pop plists)))
  17344. p v ls)
  17345. (while plists
  17346. (setq ls (pop plists))
  17347. (while ls
  17348. (setq p (pop ls) v (pop ls))
  17349. (setq rtn (plist-put rtn p v))))
  17350. rtn))
  17351. (defun org-move-line-down (arg)
  17352. "Move the current line down. With prefix argument, move it past ARG lines."
  17353. (interactive "p")
  17354. (let ((col (current-column))
  17355. beg end pos)
  17356. (beginning-of-line 1) (setq beg (point))
  17357. (beginning-of-line 2) (setq end (point))
  17358. (beginning-of-line (+ 1 arg))
  17359. (setq pos (move-marker (make-marker) (point)))
  17360. (insert (delete-and-extract-region beg end))
  17361. (goto-char pos)
  17362. (org-move-to-column col)))
  17363. (defun org-move-line-up (arg)
  17364. "Move the current line up. With prefix argument, move it past ARG lines."
  17365. (interactive "p")
  17366. (let ((col (current-column))
  17367. beg end pos)
  17368. (beginning-of-line 1) (setq beg (point))
  17369. (beginning-of-line 2) (setq end (point))
  17370. (beginning-of-line (- arg))
  17371. (setq pos (move-marker (make-marker) (point)))
  17372. (insert (delete-and-extract-region beg end))
  17373. (goto-char pos)
  17374. (org-move-to-column col)))
  17375. (defun org-replace-escapes (string table)
  17376. "Replace %-escapes in STRING with values in TABLE.
  17377. TABLE is an association list with keys like \"%a\" and string values.
  17378. The sequences in STRING may contain normal field width and padding information,
  17379. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  17380. so values can contain further %-escapes if they are define later in TABLE."
  17381. (let ((tbl (copy-alist table))
  17382. (case-fold-search nil)
  17383. (pchg 0)
  17384. e re rpl)
  17385. (while (setq e (pop tbl))
  17386. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17387. (when (and (cdr e) (string-match re (cdr e)))
  17388. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  17389. (safe "SREF"))
  17390. (add-text-properties 0 3 (list 'sref sref) safe)
  17391. (setcdr e (replace-match safe t t (cdr e)))))
  17392. (while (string-match re string)
  17393. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17394. (cdr e)))
  17395. (setq string (replace-match rpl t t string))))
  17396. (while (setq pchg (next-property-change pchg string))
  17397. (let ((sref (get-text-property pchg 'sref string)))
  17398. (when (and sref (string-match "SREF" string pchg))
  17399. (setq string (replace-match sref t t string)))))
  17400. string))
  17401. (defun org-sublist (list start end)
  17402. "Return a section of LIST, from START to END.
  17403. Counting starts at 1."
  17404. (let (rtn (c start))
  17405. (setq list (nthcdr (1- start) list))
  17406. (while (and list (<= c end))
  17407. (push (pop list) rtn)
  17408. (setq c (1+ c)))
  17409. (nreverse rtn)))
  17410. (defun org-find-base-buffer-visiting (file)
  17411. "Like `find-buffer-visiting' but always return the base buffer and
  17412. not an indirect buffer."
  17413. (let ((buf (or (get-file-buffer file)
  17414. (find-buffer-visiting file))))
  17415. (if buf
  17416. (or (buffer-base-buffer buf) buf)
  17417. nil)))
  17418. (defun org-image-file-name-regexp (&optional extensions)
  17419. "Return regexp matching the file names of images.
  17420. If EXTENSIONS is given, only match these."
  17421. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17422. (image-file-name-regexp)
  17423. (let ((image-file-name-extensions
  17424. (or extensions
  17425. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17426. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17427. (concat "\\."
  17428. (regexp-opt (nconc (mapcar 'upcase
  17429. image-file-name-extensions)
  17430. image-file-name-extensions)
  17431. t)
  17432. "\\'"))))
  17433. (defun org-file-image-p (file &optional extensions)
  17434. "Return non-nil if FILE is an image."
  17435. (save-match-data
  17436. (string-match (org-image-file-name-regexp extensions) file)))
  17437. (defun org-get-cursor-date ()
  17438. "Return the date at cursor in as a time.
  17439. This works in the calendar and in the agenda, anywhere else it just
  17440. returns the current time."
  17441. (let (date day defd)
  17442. (cond
  17443. ((eq major-mode 'calendar-mode)
  17444. (setq date (calendar-cursor-to-date)
  17445. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17446. ((eq major-mode 'org-agenda-mode)
  17447. (setq day (get-text-property (point) 'day))
  17448. (if day
  17449. (setq date (calendar-gregorian-from-absolute day)
  17450. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17451. (nth 2 date))))))
  17452. (or defd (current-time))))
  17453. (defvar org-agenda-action-marker (make-marker)
  17454. "Marker pointing to the entry for the next agenda action.")
  17455. (defun org-mark-entry-for-agenda-action ()
  17456. "Mark the current entry as target of an agenda action.
  17457. Agenda actions are actions executed from the agenda with the key `k',
  17458. which make use of the date at the cursor."
  17459. (interactive)
  17460. (move-marker org-agenda-action-marker
  17461. (save-excursion (org-back-to-heading t) (point))
  17462. (current-buffer))
  17463. (message
  17464. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17465. (defun org-mark-subtree ()
  17466. "Mark the current subtree.
  17467. This puts point at the start of the current subtree, and mark at the end.
  17468. If point is in an inline task, mark that task instead."
  17469. (interactive)
  17470. (let ((inline-task-p
  17471. (and (featurep 'org-inlinetask)
  17472. (org-inlinetask-in-task-p)))
  17473. (beg))
  17474. ;; Get beginning of subtree
  17475. (cond
  17476. (inline-task-p (org-inlinetask-goto-beginning))
  17477. ((org-at-heading-p) (beginning-of-line))
  17478. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  17479. (setq beg (point))
  17480. ;; Get end of it
  17481. (if inline-task-p
  17482. (org-inlinetask-goto-end)
  17483. (org-end-of-subtree))
  17484. ;; Mark zone
  17485. (push-mark (point) nil t)
  17486. (goto-char beg)))
  17487. ;;; Paragraph filling stuff.
  17488. ;; We want this to be just right, so use the full arsenal.
  17489. (defun org-indent-line-function ()
  17490. "Indent line depending on context."
  17491. (interactive)
  17492. (let* ((pos (point))
  17493. (itemp (org-at-item-p))
  17494. (case-fold-search t)
  17495. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17496. (inline-task-p (and (featurep 'org-inlinetask)
  17497. (org-inlinetask-in-task-p)))
  17498. (inline-re (and inline-task-p
  17499. (org-inlinetask-outline-regexp)))
  17500. column)
  17501. (beginning-of-line 1)
  17502. (cond
  17503. ;; Comments
  17504. ((looking-at "# ") (setq column 0))
  17505. ;; Headings
  17506. ((looking-at "\\*+ ") (setq column 0))
  17507. ;; Included files
  17508. ((looking-at "#\\+include:") (setq column 0))
  17509. ;; Footnote definition
  17510. ((looking-at org-footnote-definition-re) (setq column 0))
  17511. ;; Literal examples
  17512. ((looking-at "[ \t]*:[ \t]")
  17513. (setq column (org-get-indentation))) ; do nothing
  17514. ;; Lists
  17515. ((ignore-errors (goto-char (org-in-item-p)))
  17516. (setq column (if itemp
  17517. (org-get-indentation)
  17518. (org-list-item-body-column (point))))
  17519. (goto-char pos))
  17520. ;; Drawers
  17521. ((and (looking-at "[ \t]*:END:")
  17522. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17523. (save-excursion
  17524. (goto-char (1- (match-beginning 1)))
  17525. (setq column (current-column))))
  17526. ;; Special blocks
  17527. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17528. (save-excursion
  17529. (re-search-backward
  17530. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17531. (setq column (org-get-indentation (match-string 0))))
  17532. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17533. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17534. (save-excursion
  17535. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17536. (setq column
  17537. (if (equal (downcase (match-string 1)) "src")
  17538. ;; src blocks: let `org-edit-src-exit' handle them
  17539. (org-get-indentation)
  17540. (org-get-indentation (match-string 0)))))
  17541. ;; This line has nothing special, look at the previous relevant
  17542. ;; line to compute indentation
  17543. (t
  17544. (beginning-of-line 0)
  17545. (while (and (not (bobp))
  17546. (not (looking-at org-drawer-regexp))
  17547. ;; When point started in an inline task, do not move
  17548. ;; above task starting line.
  17549. (not (and inline-task-p (looking-at inline-re)))
  17550. ;; Skip drawers, blocks, empty lines, verbatim,
  17551. ;; comments, tables, footnotes definitions, lists,
  17552. ;; inline tasks.
  17553. (or (and (looking-at "[ \t]*:END:")
  17554. (re-search-backward org-drawer-regexp nil t))
  17555. (and (looking-at "[ \t]*#\\+end_")
  17556. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17557. (looking-at "[ \t]*[\n:#|]")
  17558. (looking-at org-footnote-definition-re)
  17559. (and (ignore-errors (goto-char (org-in-item-p)))
  17560. (goto-char
  17561. (org-list-get-top-point (org-list-struct))))
  17562. (and (not inline-task-p)
  17563. (featurep 'org-inlinetask)
  17564. (org-inlinetask-in-task-p)
  17565. (or (org-inlinetask-goto-beginning) t))))
  17566. (beginning-of-line 0))
  17567. (cond
  17568. ;; There was an heading above.
  17569. ((looking-at "\\*+[ \t]+")
  17570. (if (not org-adapt-indentation)
  17571. (setq column 0)
  17572. (goto-char (match-end 0))
  17573. (setq column (current-column))))
  17574. ;; A drawer had started and is unfinished
  17575. ((looking-at org-drawer-regexp)
  17576. (goto-char (1- (match-beginning 1)))
  17577. (setq column (current-column)))
  17578. ;; Else, nothing noticeable found: get indentation and go on.
  17579. (t (setq column (org-get-indentation))))))
  17580. ;; Now apply indentation and move cursor accordingly
  17581. (goto-char pos)
  17582. (if (<= (current-column) (current-indentation))
  17583. (org-indent-line-to column)
  17584. (save-excursion (org-indent-line-to column)))
  17585. ;; Special polishing for properties, see `org-property-format'
  17586. (setq column (current-column))
  17587. (beginning-of-line 1)
  17588. (if (looking-at
  17589. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17590. (replace-match (concat (match-string 1)
  17591. (format org-property-format
  17592. (match-string 2) (match-string 3)))
  17593. t t))
  17594. (org-move-to-column column)))
  17595. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17596. "Variable to store copy of `adaptive-fill-regexp'.
  17597. Since `adaptive-fill-regexp' is set to never match, we need to
  17598. store a backup of its value before entering `org-mode' so that
  17599. the functionality can be provided as a fall-back.")
  17600. (defun org-set-autofill-regexps ()
  17601. (interactive)
  17602. ;; In the paragraph separator we include headlines, because filling
  17603. ;; text in a line directly attached to a headline would otherwise
  17604. ;; fill the headline as well.
  17605. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17606. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17607. ;; The paragraph starter includes hand-formatted lists.
  17608. (org-set-local
  17609. 'paragraph-start
  17610. (concat
  17611. "\f" "\\|"
  17612. "[ ]*$" "\\|"
  17613. "\\*+ " "\\|"
  17614. "[ \t]*#" "\\|"
  17615. (org-item-re) "\\|"
  17616. "[ \t]*[:|]" "\\|"
  17617. "\\$\\$" "\\|"
  17618. "\\\\\\(begin\\|end\\|[][]\\)"))
  17619. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17620. ;; But only if the user has not turned off tables or fixed-width regions
  17621. (org-set-local
  17622. 'auto-fill-inhibit-regexp
  17623. (concat "\\*+ \\|#\\+"
  17624. "\\|[ \t]*" org-keyword-time-regexp
  17625. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17626. (concat
  17627. "\\|[ \t]*["
  17628. (if org-enable-table-editor "|" "")
  17629. (if org-enable-fixed-width-editor ":" "")
  17630. "]"))))
  17631. ;; We use our own fill-paragraph function, to make sure that tables
  17632. ;; and fixed-width regions are not wrapped. That function will pass
  17633. ;; through to `fill-paragraph' when appropriate.
  17634. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17635. ;; Prevent auto-fill from inserting unwanted new items.
  17636. (org-set-local 'fill-nobreak-predicate
  17637. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  17638. fill-nobreak-predicate
  17639. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate)))
  17640. ;; Adaptive filling: To get full control, first make sure that
  17641. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17642. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17643. (org-set-local 'org-adaptive-fill-regexp-backup
  17644. adaptive-fill-regexp))
  17645. (org-set-local 'adaptive-fill-regexp "\000")
  17646. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  17647. (org-set-local 'adaptive-fill-function
  17648. 'org-adaptive-fill-function)
  17649. (org-set-local
  17650. 'align-mode-rules-list
  17651. '((org-in-buffer-settings
  17652. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17653. (modes . '(org-mode))))))
  17654. (defun org-fill-item-nobreak-p ()
  17655. "Non-nil when a line break at point would insert a new item."
  17656. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  17657. (defun org-fill-paragraph (&optional justify)
  17658. "Re-align a table, pass through to fill-paragraph if no table."
  17659. (let ((table-p (org-at-table-p))
  17660. (table.el-p (org-at-table.el-p))
  17661. (itemp (org-in-item-p)))
  17662. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17663. (save-excursion (goto-char (point-at-bol))
  17664. (looking-at outline-regexp)))
  17665. t) ; skip headlines
  17666. (table.el-p t) ; skip table.el tables
  17667. (table-p (org-table-align) t) ; align Org tables
  17668. (itemp ; align text in items
  17669. (let* ((struct (save-excursion (goto-char itemp)
  17670. (org-list-struct)))
  17671. (parents (org-list-parents-alist struct))
  17672. (children (org-list-get-children itemp struct parents))
  17673. beg end prev next prefix)
  17674. ;; Determine in which part of item point is: before
  17675. ;; first child, after last child, between two
  17676. ;; sub-lists, or simply in item if there's no child.
  17677. (cond
  17678. ((not children)
  17679. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17680. beg itemp
  17681. end (org-list-get-item-end itemp struct)))
  17682. ((< (point) (setq next (car children)))
  17683. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17684. beg itemp
  17685. end next))
  17686. ((> (point) (setq prev (car (last children))))
  17687. (setq beg (org-list-get-item-end prev struct)
  17688. end (org-list-get-item-end itemp struct)
  17689. prefix (save-excursion
  17690. (goto-char beg)
  17691. (skip-chars-forward " \t")
  17692. (make-string (current-column) ?\ ))))
  17693. (t (catch 'exit
  17694. (while (setq next (pop children))
  17695. (if (> (point) next)
  17696. (setq prev next)
  17697. (setq beg (org-list-get-item-end prev struct)
  17698. end next
  17699. prefix (save-excursion
  17700. (goto-char beg)
  17701. (skip-chars-forward " \t")
  17702. (make-string (current-column) ?\ )))
  17703. (throw 'exit nil))))))
  17704. ;; Use `fill-paragraph' with buffer narrowed to item
  17705. ;; without any child, and with our computed PREFIX.
  17706. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17707. (save-restriction
  17708. (narrow-to-region beg end)
  17709. (save-excursion (fill-paragraph justify)))) t))
  17710. ;; Special case where point is not in a list but is on
  17711. ;; a paragraph adjacent to a list: make sure this paragraph
  17712. ;; doesn't get merged with the end of the list by narrowing
  17713. ;; buffer first.
  17714. ((save-excursion (forward-paragraph -1)
  17715. (setq itemp (org-in-item-p)))
  17716. (let ((struct (save-excursion (goto-char itemp)
  17717. (org-list-struct))))
  17718. (save-restriction
  17719. (narrow-to-region (org-list-get-bottom-point struct)
  17720. (save-excursion (forward-paragraph 1)
  17721. (point)))
  17722. (fill-paragraph justify) t)))
  17723. ;; Else simply call `fill-paragraph'.
  17724. (t nil))))
  17725. ;; For reference, this is the default value of adaptive-fill-regexp
  17726. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17727. (defun org-adaptive-fill-function ()
  17728. "Return a fill prefix for org-mode files."
  17729. (let (itemp)
  17730. (save-excursion
  17731. (cond
  17732. ;; Comment line
  17733. ((looking-at "#[ \t]+")
  17734. (match-string-no-properties 0))
  17735. ;; Plain list item
  17736. ((org-at-item-p)
  17737. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  17738. ;; Point is in a list after `backward-paragraph': original
  17739. ;; point wasn't in the list, or filling would have been taken
  17740. ;; care of by `org-auto-fill-function', but the list and the
  17741. ;; real paragraph are not separated by a blank line. Thus, move
  17742. ;; point after the list to go back to real paragraph and
  17743. ;; determine fill-prefix.
  17744. ((setq itemp (org-in-item-p))
  17745. (goto-char itemp)
  17746. (let* ((struct (org-list-struct))
  17747. (bottom (org-list-get-bottom-point struct)))
  17748. (goto-char bottom)
  17749. (make-string (org-get-indentation) ?\ )))
  17750. ;; Other text
  17751. ((looking-at org-adaptive-fill-regexp-backup)
  17752. (match-string-no-properties 0))))))
  17753. (defun org-auto-fill-function ()
  17754. "Auto-fill function."
  17755. (let (itemp prefix)
  17756. ;; When in a list, compute an appropriate fill-prefix and make
  17757. ;; sure it will be used by `do-auto-fill'.
  17758. (if (setq itemp (org-in-item-p))
  17759. (progn
  17760. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  17761. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17762. (do-auto-fill)))
  17763. ;; Else just use `do-auto-fill'.
  17764. (do-auto-fill))))
  17765. ;;; Other stuff.
  17766. (defun org-toggle-fixed-width-section (arg)
  17767. "Toggle the fixed-width export.
  17768. If there is no active region, the QUOTE keyword at the current headline is
  17769. inserted or removed. When present, it causes the text between this headline
  17770. and the next to be exported as fixed-width text, and unmodified.
  17771. If there is an active region, this command adds or removes a colon as the
  17772. first character of this line. If the first character of a line is a colon,
  17773. this line is also exported in fixed-width font."
  17774. (interactive "P")
  17775. (let* ((cc 0)
  17776. (regionp (org-region-active-p))
  17777. (beg (if regionp (region-beginning) (point)))
  17778. (end (if regionp (region-end)))
  17779. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17780. (case-fold-search nil)
  17781. (re "[ \t]*\\(: \\)")
  17782. off)
  17783. (if regionp
  17784. (save-excursion
  17785. (goto-char beg)
  17786. (setq cc (current-column))
  17787. (beginning-of-line 1)
  17788. (setq off (looking-at re))
  17789. (while (> nlines 0)
  17790. (setq nlines (1- nlines))
  17791. (beginning-of-line 1)
  17792. (cond
  17793. (arg
  17794. (org-move-to-column cc t)
  17795. (insert ": \n")
  17796. (forward-line -1))
  17797. ((and off (looking-at re))
  17798. (replace-match "" t t nil 1))
  17799. ((not off) (org-move-to-column cc t) (insert ": ")))
  17800. (forward-line 1)))
  17801. (save-excursion
  17802. (org-back-to-heading)
  17803. (if (looking-at (concat outline-regexp
  17804. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17805. (replace-match "" t t nil 1)
  17806. (if (looking-at outline-regexp)
  17807. (progn
  17808. (goto-char (match-end 0))
  17809. (insert org-quote-string " "))))))))
  17810. (defun org-reftex-citation ()
  17811. "Use reftex-citation to insert a citation into the buffer.
  17812. This looks for a line like
  17813. #+BIBLIOGRAPHY: foo plain option:-d
  17814. and derives from it that foo.bib is the bibliography file relevant
  17815. for this document. It then installs the necessary environment for RefTeX
  17816. to work in this buffer and calls `reftex-citation' to insert a citation
  17817. into the buffer.
  17818. Export of such citations to both LaTeX and HTML is handled by the contributed
  17819. package org-exp-bibtex by Taru Karttunen."
  17820. (interactive)
  17821. (let ((reftex-docstruct-symbol 'rds)
  17822. (reftex-cite-format "\\cite{%l}")
  17823. rds bib)
  17824. (save-excursion
  17825. (save-restriction
  17826. (widen)
  17827. (let ((case-fold-search t)
  17828. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17829. (if (not (save-excursion
  17830. (or (re-search-forward re nil t)
  17831. (re-search-backward re nil t))))
  17832. (error "No bibliography defined in file")
  17833. (setq bib (concat (match-string 1) ".bib")
  17834. rds (list (list 'bib bib)))))))
  17835. (call-interactively 'reftex-citation)))
  17836. ;;;; Functions extending outline functionality
  17837. (defun org-beginning-of-line (&optional arg)
  17838. "Go to the beginning of the current line. If that is invisible, continue
  17839. to a visible line beginning. This makes the function of C-a more intuitive.
  17840. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17841. first attempt, and only move to after the tags when the cursor is already
  17842. beyond the end of the headline."
  17843. (interactive "P")
  17844. (let ((pos (point))
  17845. (special (if (consp org-special-ctrl-a/e)
  17846. (car org-special-ctrl-a/e)
  17847. org-special-ctrl-a/e))
  17848. refpos)
  17849. (if (org-bound-and-true-p line-move-visual)
  17850. (beginning-of-visual-line 1)
  17851. (beginning-of-line 1))
  17852. (if (and arg (fboundp 'move-beginning-of-line))
  17853. (call-interactively 'move-beginning-of-line)
  17854. (if (bobp)
  17855. nil
  17856. (backward-char 1)
  17857. (if (org-truely-invisible-p)
  17858. (while (and (not (bobp)) (org-truely-invisible-p))
  17859. (backward-char 1)
  17860. (beginning-of-line 1))
  17861. (forward-char 1))))
  17862. (when special
  17863. (cond
  17864. ((and (looking-at org-complex-heading-regexp)
  17865. (= (char-after (match-end 1)) ?\ ))
  17866. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17867. (point-at-eol)))
  17868. (goto-char
  17869. (if (eq special t)
  17870. (cond ((> pos refpos) refpos)
  17871. ((= pos (point)) refpos)
  17872. (t (point)))
  17873. (cond ((> pos (point)) (point))
  17874. ((not (eq last-command this-command)) (point))
  17875. (t refpos)))))
  17876. ((org-at-item-p)
  17877. (goto-char
  17878. (if (eq special t)
  17879. (cond ((> pos (match-end 0)) (match-end 0))
  17880. ((= pos (point)) (match-end 0))
  17881. (t (point)))
  17882. (cond ((> pos (point)) (point))
  17883. ((not (eq last-command this-command)) (point))
  17884. (t (match-end 0))))))))
  17885. (org-no-warnings
  17886. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17887. (defun org-end-of-line (&optional arg)
  17888. "Go to the end of the line.
  17889. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17890. first attempt, and only move to after the tags when the cursor is already
  17891. beyond the end of the headline."
  17892. (interactive "P")
  17893. (let ((special (if (consp org-special-ctrl-a/e)
  17894. (cdr org-special-ctrl-a/e)
  17895. org-special-ctrl-a/e)))
  17896. (if (or (not special)
  17897. (not (org-on-heading-p))
  17898. arg)
  17899. (call-interactively
  17900. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17901. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17902. (t 'end-of-line)))
  17903. (let ((pos (point)))
  17904. (beginning-of-line 1)
  17905. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  17906. (if (eq special t)
  17907. (if (or (< pos (match-beginning 1))
  17908. (= pos (match-end 0)))
  17909. (goto-char (match-beginning 1))
  17910. (goto-char (match-end 0)))
  17911. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17912. (goto-char (match-end 0))
  17913. (goto-char (match-beginning 1))))
  17914. (call-interactively (if (fboundp 'move-end-of-line)
  17915. 'move-end-of-line
  17916. 'end-of-line)))))
  17917. (org-no-warnings
  17918. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17919. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17920. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17921. (defun org-backward-sentence (&optional arg)
  17922. "Go to beginning of sentence, or beginning of table field.
  17923. This will call `backward-sentence' or `org-table-beginning-of-field',
  17924. depending on context."
  17925. (interactive "P")
  17926. (cond
  17927. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17928. (t (call-interactively 'backward-sentence))))
  17929. (defun org-forward-sentence (&optional arg)
  17930. "Go to end of sentence, or end of table field.
  17931. This will call `forward-sentence' or `org-table-end-of-field',
  17932. depending on context."
  17933. (interactive "P")
  17934. (cond
  17935. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17936. (t (call-interactively 'forward-sentence))))
  17937. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17938. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17939. (defun org-kill-line (&optional arg)
  17940. "Kill line, to tags or end of line."
  17941. (interactive "P")
  17942. (cond
  17943. ((or (not org-special-ctrl-k)
  17944. (bolp)
  17945. (not (org-on-heading-p)))
  17946. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17947. org-ctrl-k-protect-subtree)
  17948. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17949. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17950. (error "C-k aborted - would kill hidden subtree")))
  17951. (call-interactively 'kill-line))
  17952. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  17953. (kill-region (point) (match-beginning 1))
  17954. (org-set-tags nil t))
  17955. (t (kill-region (point) (point-at-eol)))))
  17956. (define-key org-mode-map "\C-k" 'org-kill-line)
  17957. (defun org-yank (&optional arg)
  17958. "Yank. If the kill is a subtree, treat it specially.
  17959. This command will look at the current kill and check if is a single
  17960. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17961. cursor is at the beginning of a line or after the stars of a currently
  17962. empty headline, then the yank is handled specially. How exactly depends
  17963. on the value of the following variables, both set by default.
  17964. org-yank-folded-subtrees
  17965. When set, the subtree(s) will be folded after insertion, but only
  17966. if doing so would now swallow text after the yanked text.
  17967. org-yank-adjusted-subtrees
  17968. When set, the subtree will be promoted or demoted in order to
  17969. fit into the local outline tree structure, which means that the level
  17970. will be adjusted so that it becomes the smaller one of the two
  17971. *visible* surrounding headings.
  17972. Any prefix to this command will cause `yank' to be called directly with
  17973. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17974. will just
  17975. plainly yank the text as it is.
  17976. \[1] The test checks if the first non-white line is a heading
  17977. and if there are no other headings with fewer stars."
  17978. (interactive "P")
  17979. (org-yank-generic 'yank arg))
  17980. (defun org-yank-generic (command arg)
  17981. "Perform some yank-like command.
  17982. This function implements the behavior described in the `org-yank'
  17983. documentation. However, it has been generalized to work for any
  17984. interactive command with similar behavior."
  17985. ;; pretend to be command COMMAND
  17986. (setq this-command command)
  17987. (if arg
  17988. (call-interactively command)
  17989. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17990. (and (org-kill-is-subtree-p)
  17991. (or (bolp)
  17992. (and (looking-at "[ \t]*$")
  17993. (string-match
  17994. "\\`\\*+\\'"
  17995. (buffer-substring (point-at-bol) (point)))))))
  17996. swallowp)
  17997. (cond
  17998. ((and subtreep org-yank-folded-subtrees)
  17999. (let ((beg (point))
  18000. end)
  18001. (if (and subtreep org-yank-adjusted-subtrees)
  18002. (org-paste-subtree nil nil 'for-yank)
  18003. (call-interactively command))
  18004. (setq end (point))
  18005. (goto-char beg)
  18006. (when (and (bolp) subtreep
  18007. (not (setq swallowp
  18008. (org-yank-folding-would-swallow-text beg end))))
  18009. (or (looking-at outline-regexp)
  18010. (re-search-forward (concat "^" outline-regexp) end t))
  18011. (while (and (< (point) end) (looking-at outline-regexp))
  18012. (hide-subtree)
  18013. (org-cycle-show-empty-lines 'folded)
  18014. (condition-case nil
  18015. (outline-forward-same-level 1)
  18016. (error (goto-char end)))))
  18017. (when swallowp
  18018. (message
  18019. "Inserted text not folded because that would swallow text"))
  18020. (goto-char end)
  18021. (skip-chars-forward " \t\n\r")
  18022. (beginning-of-line 1)
  18023. (push-mark beg 'nomsg)))
  18024. ((and subtreep org-yank-adjusted-subtrees)
  18025. (let ((beg (point-at-bol)))
  18026. (org-paste-subtree nil nil 'for-yank)
  18027. (push-mark beg 'nomsg)))
  18028. (t
  18029. (call-interactively command))))))
  18030. (defun org-yank-folding-would-swallow-text (beg end)
  18031. "Would hide-subtree at BEG swallow any text after END?"
  18032. (let (level)
  18033. (save-excursion
  18034. (goto-char beg)
  18035. (when (or (looking-at outline-regexp)
  18036. (re-search-forward (concat "^" outline-regexp) end t))
  18037. (setq level (org-outline-level)))
  18038. (goto-char end)
  18039. (skip-chars-forward " \t\r\n\v\f")
  18040. (if (or (eobp)
  18041. (and (bolp) (looking-at org-outline-regexp)
  18042. (<= (org-outline-level) level)))
  18043. nil ; Nothing would be swallowed
  18044. t)))) ; something would swallow
  18045. (define-key org-mode-map "\C-y" 'org-yank)
  18046. (defun org-truely-invisible-p ()
  18047. "Check if point is at a character currently not visible.
  18048. This version does not only check the character property, but also
  18049. `visible-mode'."
  18050. ;; Early versions of noutline don't have `outline-invisible-p'.
  18051. (if (org-bound-and-true-p visible-mode)
  18052. nil
  18053. (outline-invisible-p)))
  18054. (defun org-invisible-p2 ()
  18055. "Check if point is at a character currently not visible."
  18056. (save-excursion
  18057. (if (and (eolp) (not (bobp))) (backward-char 1))
  18058. ;; Early versions of noutline don't have `outline-invisible-p'.
  18059. (outline-invisible-p)))
  18060. (defun org-back-to-heading (&optional invisible-ok)
  18061. "Call `outline-back-to-heading', but provide a better error message."
  18062. (condition-case nil
  18063. (outline-back-to-heading invisible-ok)
  18064. (error (error "Before first headline at position %d in buffer %s"
  18065. (point) (current-buffer)))))
  18066. (defun org-beginning-of-defun ()
  18067. "Go to the beginning of the subtree, i.e. back to the heading."
  18068. (org-back-to-heading))
  18069. (defun org-end-of-defun ()
  18070. "Go to the end of the subtree."
  18071. (org-end-of-subtree nil t))
  18072. (defun org-before-first-heading-p ()
  18073. "Before first heading?"
  18074. (save-excursion
  18075. (end-of-line)
  18076. (null (re-search-backward "^\\*+ " nil t))))
  18077. (defun org-on-heading-p (&optional ignored)
  18078. (outline-on-heading-p t))
  18079. (defun org-at-heading-p (&optional ignored)
  18080. (outline-on-heading-p t))
  18081. (defun org-point-at-end-of-empty-headline ()
  18082. "If point is at the end of an empty headline, return t, else nil.
  18083. If the heading only contains a TODO keyword, it is still still considered
  18084. empty."
  18085. (and (looking-at "[ \t]*$")
  18086. (save-excursion
  18087. (beginning-of-line 1)
  18088. (let ((case-fold-search nil))
  18089. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  18090. "\\)?[ \t]*$"))))))
  18091. (defun org-at-heading-or-item-p ()
  18092. (or (org-on-heading-p) (org-at-item-p)))
  18093. (defun org-on-target-p ()
  18094. (or (org-in-regexp org-radio-target-regexp)
  18095. (org-in-regexp org-target-regexp)))
  18096. (defun org-up-heading-all (arg)
  18097. "Move to the heading line of which the present line is a subheading.
  18098. This function considers both visible and invisible heading lines.
  18099. With argument, move up ARG levels."
  18100. (if (fboundp 'outline-up-heading-all)
  18101. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18102. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18103. (defun org-up-heading-safe ()
  18104. "Move to the heading line of which the present line is a subheading.
  18105. This version will not throw an error. It will return the level of the
  18106. headline found, or nil if no higher level is found.
  18107. Also, this function will be a lot faster than `outline-up-heading',
  18108. because it relies on stars being the outline starters. This can really
  18109. make a significant difference in outlines with very many siblings."
  18110. (let (start-level re)
  18111. (org-back-to-heading t)
  18112. (setq start-level (funcall outline-level))
  18113. (if (equal start-level 1)
  18114. nil
  18115. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18116. (if (re-search-backward re nil t)
  18117. (funcall outline-level)))))
  18118. (defun org-first-sibling-p ()
  18119. "Is this heading the first child of its parents?"
  18120. (interactive)
  18121. (let ((re (concat "^" outline-regexp))
  18122. level l)
  18123. (unless (org-at-heading-p t)
  18124. (error "Not at a heading"))
  18125. (setq level (funcall outline-level))
  18126. (save-excursion
  18127. (if (not (re-search-backward re nil t))
  18128. t
  18129. (setq l (funcall outline-level))
  18130. (< l level)))))
  18131. (defun org-goto-sibling (&optional previous)
  18132. "Goto the next sibling, even if it is invisible.
  18133. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18134. when a sibling was found. When none is found, return nil and don't
  18135. move point."
  18136. (let ((fun (if previous 're-search-backward 're-search-forward))
  18137. (pos (point))
  18138. (re (concat "^" outline-regexp))
  18139. level l)
  18140. (when (condition-case nil (org-back-to-heading t) (error nil))
  18141. (setq level (funcall outline-level))
  18142. (catch 'exit
  18143. (or previous (forward-char 1))
  18144. (while (funcall fun re nil t)
  18145. (setq l (funcall outline-level))
  18146. (when (< l level) (goto-char pos) (throw 'exit nil))
  18147. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18148. (goto-char pos)
  18149. nil))))
  18150. (defun org-show-siblings ()
  18151. "Show all siblings of the current headline."
  18152. (save-excursion
  18153. (while (org-goto-sibling) (org-flag-heading nil)))
  18154. (save-excursion
  18155. (while (org-goto-sibling 'previous)
  18156. (org-flag-heading nil))))
  18157. (defun org-goto-first-child ()
  18158. "Goto the first child, even if it is invisible.
  18159. Return t when a child was found. Otherwise don't move point and
  18160. return nil."
  18161. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  18162. (when (condition-case nil (org-back-to-heading t) (error nil))
  18163. (setq level (outline-level))
  18164. (forward-char 1)
  18165. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18166. (progn (goto-char (match-beginning 0)) t)
  18167. (goto-char pos) nil))))
  18168. (defun org-show-hidden-entry ()
  18169. "Show an entry where even the heading is hidden."
  18170. (save-excursion
  18171. (org-show-entry)))
  18172. (defun org-flag-heading (flag &optional entry)
  18173. "Flag the current heading. FLAG non-nil means make invisible.
  18174. When ENTRY is non-nil, show the entire entry."
  18175. (save-excursion
  18176. (org-back-to-heading t)
  18177. ;; Check if we should show the entire entry
  18178. (if entry
  18179. (progn
  18180. (org-show-entry)
  18181. (save-excursion
  18182. (and (outline-next-heading)
  18183. (org-flag-heading nil))))
  18184. (outline-flag-region (max (point-min) (1- (point)))
  18185. (save-excursion (outline-end-of-heading) (point))
  18186. flag))))
  18187. (defun org-get-next-sibling ()
  18188. "Move to next heading of the same level, and return point.
  18189. If there is no such heading, return nil.
  18190. This is like outline-next-sibling, but invisible headings are ok."
  18191. (let ((level (funcall outline-level)))
  18192. (outline-next-heading)
  18193. (while (and (not (eobp)) (> (funcall outline-level) level))
  18194. (outline-next-heading))
  18195. (if (or (eobp) (< (funcall outline-level) level))
  18196. nil
  18197. (point))))
  18198. (defun org-get-last-sibling ()
  18199. "Move to previous heading of the same level, and return point.
  18200. If there is no such heading, return nil."
  18201. (let ((opoint (point))
  18202. (level (funcall outline-level)))
  18203. (outline-previous-heading)
  18204. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18205. (while (and (> (funcall outline-level) level)
  18206. (not (bobp)))
  18207. (outline-previous-heading))
  18208. (if (< (funcall outline-level) level)
  18209. nil
  18210. (point)))))
  18211. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18212. ;; This contains an exact copy of the original function, but it uses
  18213. ;; `org-back-to-heading', to make it work also in invisible
  18214. ;; trees. And is uses an invisible-OK argument.
  18215. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18216. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18217. ;; with many children and grandchildren etc, this can be much faster
  18218. ;; than the outline version.
  18219. (org-back-to-heading invisible-OK)
  18220. (let ((first t)
  18221. (level (funcall outline-level)))
  18222. (if (and (org-mode-p) (< level 1000))
  18223. ;; A true heading (not a plain list item), in Org-mode
  18224. ;; This means we can easily find the end by looking
  18225. ;; only for the right number of stars. Using a regexp to do
  18226. ;; this is so much faster than using a Lisp loop.
  18227. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  18228. (forward-char 1)
  18229. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  18230. ;; something else, do it the slow way
  18231. (while (and (not (eobp))
  18232. (or first (> (funcall outline-level) level)))
  18233. (setq first nil)
  18234. (outline-next-heading)))
  18235. (unless to-heading
  18236. (if (memq (preceding-char) '(?\n ?\^M))
  18237. (progn
  18238. ;; Go to end of line before heading
  18239. (forward-char -1)
  18240. (if (memq (preceding-char) '(?\n ?\^M))
  18241. ;; leave blank line before heading
  18242. (forward-char -1))))))
  18243. (point))
  18244. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  18245. "Use Org version in org-mode, for dramatic speed-up."
  18246. (if (org-mode-p)
  18247. (progn
  18248. (org-end-of-subtree nil t)
  18249. (unless (eobp) (backward-char 1)))
  18250. ad-do-it))
  18251. (defun org-end-of-meta-data-and-drawers ()
  18252. "Jump to the first text after meta data and drawers in the current entry.
  18253. This will move over empty lines, lines with planning time stamps,
  18254. clocking lines, and drawers."
  18255. (org-back-to-heading t)
  18256. (let ((end (save-excursion (outline-next-heading) (point)))
  18257. (re (concat "[ \t]*$"
  18258. "\\|"
  18259. "\\(" org-drawer-regexp "\\)" ; group 1 are drawers
  18260. "\\|"
  18261. "\\([ \t]*\\(" org-keyword-time-regexp "\\)\\)")))
  18262. (forward-line 1)
  18263. (while (looking-at (concat "[ \t]*\\(" org-keyword-time-regexp "\\)"))
  18264. (if (not (match-end 1))
  18265. ;; empty or planning line
  18266. (forward-line 1)
  18267. ;; a drawer, find the end
  18268. (re-search-forward "^[ \t]*:END:" end 'move)
  18269. (forward-line 1)))
  18270. (point)))
  18271. (defun org-forward-same-level (arg &optional invisible-ok)
  18272. "Move forward to the arg'th subheading at same level as this one.
  18273. Stop at the first and last subheadings of a superior heading.
  18274. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  18275. it wil also look at invisible ones."
  18276. (interactive "p")
  18277. (org-back-to-heading invisible-ok)
  18278. (org-on-heading-p)
  18279. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18280. (re (format "^\\*\\{1,%d\\} " level))
  18281. l)
  18282. (forward-char 1)
  18283. (while (> arg 0)
  18284. (while (and (re-search-forward re nil 'move)
  18285. (setq l (- (match-end 0) (match-beginning 0) 1))
  18286. (= l level)
  18287. (not invisible-ok)
  18288. (progn (backward-char 1) (outline-invisible-p)))
  18289. (if (< l level) (setq arg 1)))
  18290. (setq arg (1- arg)))
  18291. (beginning-of-line 1)))
  18292. (defun org-backward-same-level (arg &optional invisible-ok)
  18293. "Move backward to the arg'th subheading at same level as this one.
  18294. Stop at the first and last subheadings of a superior heading."
  18295. (interactive "p")
  18296. (org-back-to-heading)
  18297. (org-on-heading-p)
  18298. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18299. (re (format "^\\*\\{1,%d\\} " level))
  18300. l)
  18301. (while (> arg 0)
  18302. (while (and (re-search-backward re nil 'move)
  18303. (setq l (- (match-end 0) (match-beginning 0) 1))
  18304. (= l level)
  18305. (not invisible-ok)
  18306. (outline-invisible-p))
  18307. (if (< l level) (setq arg 1)))
  18308. (setq arg (1- arg)))))
  18309. (defun org-show-subtree ()
  18310. "Show everything after this heading at deeper levels."
  18311. (outline-flag-region
  18312. (point)
  18313. (save-excursion
  18314. (org-end-of-subtree t t))
  18315. nil))
  18316. (defun org-show-entry ()
  18317. "Show the body directly following this heading.
  18318. Show the heading too, if it is currently invisible."
  18319. (interactive)
  18320. (save-excursion
  18321. (condition-case nil
  18322. (progn
  18323. (org-back-to-heading t)
  18324. (outline-flag-region
  18325. (max (point-min) (1- (point)))
  18326. (save-excursion
  18327. (if (re-search-forward
  18328. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  18329. (match-beginning 1)
  18330. (point-max)))
  18331. nil)
  18332. (org-cycle-hide-drawers 'children))
  18333. (error nil))))
  18334. (defun org-make-options-regexp (kwds &optional extra)
  18335. "Make a regular expression for keyword lines."
  18336. (concat
  18337. "^"
  18338. "#?[ \t]*\\+\\("
  18339. (mapconcat 'regexp-quote kwds "\\|")
  18340. (if extra (concat "\\|" extra))
  18341. "\\):[ \t]*"
  18342. "\\(.*\\)"))
  18343. ;; Make isearch reveal the necessary context
  18344. (defun org-isearch-end ()
  18345. "Reveal context after isearch exits."
  18346. (when isearch-success ; only if search was successful
  18347. (if (featurep 'xemacs)
  18348. ;; Under XEmacs, the hook is run in the correct place,
  18349. ;; we directly show the context.
  18350. (org-show-context 'isearch)
  18351. ;; In Emacs the hook runs *before* restoring the overlays.
  18352. ;; So we have to use a one-time post-command-hook to do this.
  18353. ;; (Emacs 22 has a special variable, see function `org-mode')
  18354. (unless (and (boundp 'isearch-mode-end-hook-quit)
  18355. isearch-mode-end-hook-quit)
  18356. ;; Only when the isearch was not quitted.
  18357. (org-add-hook 'post-command-hook 'org-isearch-post-command
  18358. 'append 'local)))))
  18359. (defun org-isearch-post-command ()
  18360. "Remove self from hook, and show context."
  18361. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  18362. (org-show-context 'isearch))
  18363. ;;;; Integration with and fixes for other packages
  18364. ;;; Imenu support
  18365. (defvar org-imenu-markers nil
  18366. "All markers currently used by Imenu.")
  18367. (make-variable-buffer-local 'org-imenu-markers)
  18368. (defun org-imenu-new-marker (&optional pos)
  18369. "Return a new marker for use by Imenu, and remember the marker."
  18370. (let ((m (make-marker)))
  18371. (move-marker m (or pos (point)))
  18372. (push m org-imenu-markers)
  18373. m))
  18374. (defun org-imenu-get-tree ()
  18375. "Produce the index for Imenu."
  18376. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  18377. (setq org-imenu-markers nil)
  18378. (let* ((n org-imenu-depth)
  18379. (re (concat "^" outline-regexp))
  18380. (subs (make-vector (1+ n) nil))
  18381. (last-level 0)
  18382. m level head)
  18383. (save-excursion
  18384. (save-restriction
  18385. (widen)
  18386. (goto-char (point-max))
  18387. (while (re-search-backward re nil t)
  18388. (setq level (org-reduced-level (funcall outline-level)))
  18389. (when (<= level n)
  18390. (looking-at org-complex-heading-regexp)
  18391. (setq head (org-link-display-format
  18392. (org-match-string-no-properties 4))
  18393. m (org-imenu-new-marker))
  18394. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  18395. (if (>= level last-level)
  18396. (push (cons head m) (aref subs level))
  18397. (push (cons head (aref subs (1+ level))) (aref subs level))
  18398. (loop for i from (1+ level) to n do (aset subs i nil)))
  18399. (setq last-level level)))))
  18400. (aref subs 1)))
  18401. (eval-after-load "imenu"
  18402. '(progn
  18403. (add-hook 'imenu-after-jump-hook
  18404. (lambda ()
  18405. (if (org-mode-p)
  18406. (org-show-context 'org-goto))))))
  18407. (defun org-link-display-format (link)
  18408. "Replace a link with either the description, or the link target
  18409. if no description is present"
  18410. (save-match-data
  18411. (if (string-match org-bracket-link-analytic-regexp link)
  18412. (replace-match (if (match-end 5)
  18413. (match-string 5 link)
  18414. (concat (match-string 1 link)
  18415. (match-string 3 link)))
  18416. nil t link)
  18417. link)))
  18418. ;; Speedbar support
  18419. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  18420. "Overlay marking the agenda restriction line in speedbar.")
  18421. (overlay-put org-speedbar-restriction-lock-overlay
  18422. 'face 'org-agenda-restriction-lock)
  18423. (overlay-put org-speedbar-restriction-lock-overlay
  18424. 'help-echo "Agendas are currently limited to this item.")
  18425. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  18426. (defun org-speedbar-set-agenda-restriction ()
  18427. "Restrict future agenda commands to the location at point in speedbar.
  18428. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  18429. (interactive)
  18430. (require 'org-agenda)
  18431. (let (p m tp np dir txt)
  18432. (cond
  18433. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18434. 'org-imenu t))
  18435. (setq m (get-text-property p 'org-imenu-marker))
  18436. (with-current-buffer (marker-buffer m)
  18437. (goto-char m)
  18438. (org-agenda-set-restriction-lock 'subtree)))
  18439. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18440. 'speedbar-function 'speedbar-find-file))
  18441. (setq tp (previous-single-property-change
  18442. (1+ p) 'speedbar-function)
  18443. np (next-single-property-change
  18444. tp 'speedbar-function)
  18445. dir (speedbar-line-directory)
  18446. txt (buffer-substring-no-properties (or tp (point-min))
  18447. (or np (point-max))))
  18448. (with-current-buffer (find-file-noselect
  18449. (let ((default-directory dir))
  18450. (expand-file-name txt)))
  18451. (unless (org-mode-p)
  18452. (error "Cannot restrict to non-Org-mode file"))
  18453. (org-agenda-set-restriction-lock 'file)))
  18454. (t (error "Don't know how to restrict Org-mode's agenda")))
  18455. (move-overlay org-speedbar-restriction-lock-overlay
  18456. (point-at-bol) (point-at-eol))
  18457. (setq current-prefix-arg nil)
  18458. (org-agenda-maybe-redo)))
  18459. (eval-after-load "speedbar"
  18460. '(progn
  18461. (speedbar-add-supported-extension ".org")
  18462. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  18463. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  18464. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  18465. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18466. (add-hook 'speedbar-visiting-tag-hook
  18467. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  18468. ;;; Fixes and Hacks for problems with other packages
  18469. ;; Make flyspell not check words in links, to not mess up our keymap
  18470. (defun org-mode-flyspell-verify ()
  18471. "Don't let flyspell put overlays at active buttons."
  18472. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  18473. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  18474. (defun org-remove-flyspell-overlays-in (beg end)
  18475. "Remove flyspell overlays in region."
  18476. (and (org-bound-and-true-p flyspell-mode)
  18477. (fboundp 'flyspell-delete-region-overlays)
  18478. (flyspell-delete-region-overlays beg end))
  18479. (add-text-properties beg end '(org-no-flyspell t)))
  18480. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  18481. (eval-after-load "bookmark"
  18482. '(if (boundp 'bookmark-after-jump-hook)
  18483. ;; We can use the hook
  18484. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  18485. ;; Hook not available, use advice
  18486. (defadvice bookmark-jump (after org-make-visible activate)
  18487. "Make the position visible."
  18488. (org-bookmark-jump-unhide))))
  18489. ;; Make sure saveplace shows the location if it was hidden
  18490. (eval-after-load "saveplace"
  18491. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  18492. "Make the position visible."
  18493. (org-bookmark-jump-unhide)))
  18494. ;; Make sure ecb shows the location if it was hidden
  18495. (eval-after-load "ecb"
  18496. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  18497. "Make hierarchy visible when jumping into location from ECB tree buffer."
  18498. (if (org-mode-p)
  18499. (org-show-context))))
  18500. (defun org-bookmark-jump-unhide ()
  18501. "Unhide the current position, to show the bookmark location."
  18502. (and (org-mode-p)
  18503. (or (outline-invisible-p)
  18504. (save-excursion (goto-char (max (point-min) (1- (point))))
  18505. (outline-invisible-p)))
  18506. (org-show-context 'bookmark-jump)))
  18507. ;; Make session.el ignore our circular variable
  18508. (eval-after-load "session"
  18509. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18510. ;;;; Experimental code
  18511. (defun org-closed-in-range ()
  18512. "Sparse tree of items closed in a certain time range.
  18513. Still experimental, may disappear in the future."
  18514. (interactive)
  18515. ;; Get the time interval from the user.
  18516. (let* ((time1 (org-float-time
  18517. (org-read-date nil 'to-time nil "Starting date: ")))
  18518. (time2 (org-float-time
  18519. (org-read-date nil 'to-time nil "End date:")))
  18520. ;; callback function
  18521. (callback (lambda ()
  18522. (let ((time
  18523. (org-float-time
  18524. (apply 'encode-time
  18525. (org-parse-time-string
  18526. (match-string 1))))))
  18527. ;; check if time in interval
  18528. (and (>= time time1) (<= time time2))))))
  18529. ;; make tree, check each match with the callback
  18530. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18531. ;;;; Finish up
  18532. (provide 'org)
  18533. (run-hooks 'org-load-hook)
  18534. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  18535. ;;; org.el ends here