org.el 641 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193
  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
  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: 6.30trans
  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. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.30trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((version org-version)
  96. (git-version)
  97. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  98. (if (and (file-exists-p (expand-file-name ".git" dir))
  99. (executable-find "git"))
  100. (let ((pwd (substring (pwd) 10)))
  101. (cd dir)
  102. (if (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  103. (save-excursion
  104. (set-buffer "*Shell Command Output*")
  105. (goto-char (point-min))
  106. (re-search-forward "[^\n]+")
  107. (setq git-version (match-string 0))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (shell-command "git diff-index --name-only HEAD --")
  110. (unless (eql 1 (point-max))
  111. (setq git-version (concat git-version ".dirty")))
  112. (setq version (concat version " (" git-version ")")))
  113. (cd pwd))))
  114. (setq version (format "Org-mode version %s" version))
  115. (if here (insert version))
  116. (message version)
  117. version))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-load-hook nil
  127. "Hook that is run after org.el has been loaded."
  128. :group 'org
  129. :type 'hook)
  130. (defvar org-modules) ; defined below
  131. (defvar org-modules-loaded nil
  132. "Have the modules been loaded already?")
  133. (defun org-load-modules-maybe (&optional force)
  134. "Load all extensions listed in `org-modules'."
  135. (when (or force (not org-modules-loaded))
  136. (mapc (lambda (ext)
  137. (condition-case nil (require ext)
  138. (error (message "Problems while trying to load feature `%s'" ext))))
  139. org-modules)
  140. (setq org-modules-loaded t)))
  141. (defun org-set-modules (var value)
  142. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  143. (set var value)
  144. (when (featurep 'org)
  145. (org-load-modules-maybe 'force)))
  146. (when (org-bound-and-true-p org-modules)
  147. (let ((a (member 'org-infojs org-modules)))
  148. (and a (setcar a 'org-jsinfo))))
  149. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  150. "Modules that should always be loaded together with org.el.
  151. If a description starts with <C>, the file is not part of Emacs
  152. and loading it will require that you have downloaded and properly installed
  153. the org-mode distribution.
  154. You can also use this system to load external packages (i.e. neither Org
  155. core modules, nor modules from the CONTRIB directory). Just add symbols
  156. to the end of the list. If the package is called org-xyz.el, then you need
  157. to add the symbol `xyz', and the package must have a call to
  158. (provide 'org-xyz)"
  159. :group 'org
  160. :set 'org-set-modules
  161. :type
  162. '(set :greedy t
  163. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  164. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  165. (const :tag " crypt: Encryption of subtrees" org-crypt)
  166. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  167. (const :tag " id: Global IDs for identifying entries" org-id)
  168. (const :tag " info: Links to Info nodes" org-info)
  169. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  170. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  171. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  172. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  173. (const :tag " mew Links to Mew folders/messages" org-mew)
  174. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  175. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  176. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  177. (const :tag " vm: Links to VM folders/messages" org-vm)
  178. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  179. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  180. (const :tag " mouse: Additional mouse support" org-mouse)
  181. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  182. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  183. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  184. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  185. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  186. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  187. (const :tag "C collector: Collect properties into tables" org-collector)
  188. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  189. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  190. (const :tag "C eval: Include command output as text" org-eval)
  191. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  192. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  193. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  194. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  195. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  196. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  197. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  198. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  199. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  200. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  201. (const :tag "C R: Computation using the R language" org-R)
  202. (const :tag "C registry: A registry for Org links" org-registry)
  203. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  204. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  205. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  206. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  207. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  208. (const :tag "C track: Keep up with Org development" org-track)
  209. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  210. (defcustom org-support-shift-select nil
  211. "Non-nil means, make shift-cursor commands select text when possible.
  212. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  213. selecting a region, or enlarge thusly regions started in this way.
  214. In Org-mode, in special contexts, these same keys are used for other
  215. purposes, important enough to compete with shift selection. Org tries
  216. to balance these needs by supporting `shift-select-mode' outside these
  217. special contexts, under control of this variable.
  218. The default of this variable is nil, to avoid confusing behavior. Shifted
  219. cursor keys will then execute Org commands in the following contexts:
  220. - on a headline, changing TODO state (left/right) and priority (up/down)
  221. - on a time stamp, changing the time
  222. - in a plain list item, changing the bullet type
  223. - in a property definition line, switching between allowed values
  224. - in the BEGIN line of a clock table (changing the time block).
  225. Outside these contexts, the commands will throw an error.
  226. When this variable is t and the cursor is not in a special context,
  227. Org-mode will support shift-selection for making and enlarging regions.
  228. To make this more effective, the bullet cycling will no longer happen
  229. anywhere in an item line, but only if the cursor is exactly on the bullet.
  230. If you set this variable to the symbol `always', then the keys
  231. will not be special in headlines, property lines, and item lines, to make
  232. shift selection work there as well. If this is what you want, you can
  233. use the following alternative commands: `C-c C-t' and `C-c ,' to
  234. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  235. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  236. edited by hand or in column view.
  237. However, when the cursor is on a timestamp, shift-cursor commands
  238. will still edit the time stamp - this is just too good to give up.
  239. XEmacs user should have this variable set to nil, because shift-select-mode
  240. is Emacs 23 only."
  241. :group 'org
  242. :type '(choice
  243. (const :tag "Never" nil)
  244. (const :tag "When outside special context" t)
  245. (const :tag "Everywhere except timestamps" always)))
  246. (defgroup org-startup nil
  247. "Options concerning startup of Org-mode."
  248. :tag "Org Startup"
  249. :group 'org)
  250. (defcustom org-startup-folded t
  251. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  252. This can also be configured on a per-file basis by adding one of
  253. the following lines anywhere in the buffer:
  254. #+STARTUP: fold
  255. #+STARTUP: nofold
  256. #+STARTUP: content"
  257. :group 'org-startup
  258. :type '(choice
  259. (const :tag "nofold: show all" nil)
  260. (const :tag "fold: overview" t)
  261. (const :tag "content: all headlines" content)))
  262. (defcustom org-startup-truncated t
  263. "Non-nil means, entering Org-mode will set `truncate-lines'.
  264. This is useful since some lines containing links can be very long and
  265. uninteresting. Also tables look terrible when wrapped."
  266. :group 'org-startup
  267. :type 'boolean)
  268. (defcustom org-startup-indented nil
  269. "Non-nil means, turn on `org-indent-mode' on startup.
  270. This can also be configured on a per-file basis by adding one of
  271. the following lines anywhere in the buffer:
  272. #+STARTUP: indent
  273. #+STARTUP: noindent"
  274. :group 'org-structure
  275. :type '(choice
  276. (const :tag "Not" nil)
  277. (const :tag "Globally (slow on startup in large files)" t)))
  278. (defcustom org-startup-align-all-tables nil
  279. "Non-nil means, align all tables when visiting a file.
  280. This is useful when the column width in tables is forced with <N> cookies
  281. in table fields. Such tables will look correct only after the first re-align.
  282. This can also be configured on a per-file basis by adding one of
  283. the following lines anywhere in the buffer:
  284. #+STARTUP: align
  285. #+STARTUP: noalign"
  286. :group 'org-startup
  287. :type 'boolean)
  288. (defcustom org-insert-mode-line-in-empty-file nil
  289. "Non-nil means insert the first line setting Org-mode in empty files.
  290. When the function `org-mode' is called interactively in an empty file, this
  291. normally means that the file name does not automatically trigger Org-mode.
  292. To ensure that the file will always be in Org-mode in the future, a
  293. line enforcing Org-mode will be inserted into the buffer, if this option
  294. has been set."
  295. :group 'org-startup
  296. :type 'boolean)
  297. (defcustom org-replace-disputed-keys nil
  298. "Non-nil means use alternative key bindings for some keys.
  299. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  300. These keys are also used by other packages like shift-selection-mode'
  301. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  302. If you want to use Org-mode together with one of these other modes,
  303. or more generally if you would like to move some Org-mode commands to
  304. other keys, set this variable and configure the keys with the variable
  305. `org-disputed-keys'.
  306. This option is only relevant at load-time of Org-mode, and must be set
  307. *before* org.el is loaded. Changing it requires a restart of Emacs to
  308. become effective."
  309. :group 'org-startup
  310. :type 'boolean)
  311. (defcustom org-use-extra-keys nil
  312. "Non-nil means use extra key sequence definitions for certain
  313. commands. This happens automatically if you run XEmacs or if
  314. window-system is nil. This variable lets you do the same
  315. manually. You must set it before loading org.
  316. Example: on Carbon Emacs 22 running graphically, with an external
  317. keyboard on a Powerbook, the default way of setting M-left might
  318. not work for either Alt or ESC. Setting this variable will make
  319. it work for ESC."
  320. :group 'org-startup
  321. :type 'boolean)
  322. (if (fboundp 'defvaralias)
  323. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  324. (defcustom org-disputed-keys
  325. '(([(shift up)] . [(meta p)])
  326. ([(shift down)] . [(meta n)])
  327. ([(shift left)] . [(meta -)])
  328. ([(shift right)] . [(meta +)])
  329. ([(control shift right)] . [(meta shift +)])
  330. ([(control shift left)] . [(meta shift -)]))
  331. "Keys for which Org-mode and other modes compete.
  332. This is an alist, cars are the default keys, second element specifies
  333. the alternative to use when `org-replace-disputed-keys' is t.
  334. Keys can be specified in any syntax supported by `define-key'.
  335. The value of this option takes effect only at Org-mode's startup,
  336. therefore you'll have to restart Emacs to apply it after changing."
  337. :group 'org-startup
  338. :type 'alist)
  339. (defun org-key (key)
  340. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  341. Or return the original if not disputed."
  342. (if org-replace-disputed-keys
  343. (let* ((nkey (key-description key))
  344. (x (org-find-if (lambda (x)
  345. (equal (key-description (car x)) nkey))
  346. org-disputed-keys)))
  347. (if x (cdr x) key))
  348. key))
  349. (defun org-find-if (predicate seq)
  350. (catch 'exit
  351. (while seq
  352. (if (funcall predicate (car seq))
  353. (throw 'exit (car seq))
  354. (pop seq)))))
  355. (defun org-defkey (keymap key def)
  356. "Define a key, possibly translated, as returned by `org-key'."
  357. (define-key keymap (org-key key) def))
  358. (defcustom org-ellipsis nil
  359. "The ellipsis to use in the Org-mode outline.
  360. When nil, just use the standard three dots. When a string, use that instead,
  361. When a face, use the standard 3 dots, but with the specified face.
  362. The change affects only Org-mode (which will then use its own display table).
  363. Changing this requires executing `M-x org-mode' in a buffer to become
  364. effective."
  365. :group 'org-startup
  366. :type '(choice (const :tag "Default" nil)
  367. (face :tag "Face" :value org-warning)
  368. (string :tag "String" :value "...#")))
  369. (defvar org-display-table nil
  370. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  371. (defgroup org-keywords nil
  372. "Keywords in Org-mode."
  373. :tag "Org Keywords"
  374. :group 'org)
  375. (defcustom org-deadline-string "DEADLINE:"
  376. "String to mark deadline entries.
  377. A deadline is this string, followed by a time stamp. Should be a word,
  378. terminated by a colon. You can insert a schedule keyword and
  379. a timestamp with \\[org-deadline].
  380. Changes become only effective after restarting Emacs."
  381. :group 'org-keywords
  382. :type 'string)
  383. (defcustom org-scheduled-string "SCHEDULED:"
  384. "String to mark scheduled TODO entries.
  385. A schedule is this string, followed by a time stamp. Should be a word,
  386. terminated by a colon. You can insert a schedule keyword and
  387. a timestamp with \\[org-schedule].
  388. Changes become only effective after restarting Emacs."
  389. :group 'org-keywords
  390. :type 'string)
  391. (defcustom org-closed-string "CLOSED:"
  392. "String used as the prefix for timestamps logging closing a TODO entry."
  393. :group 'org-keywords
  394. :type 'string)
  395. (defcustom org-clock-string "CLOCK:"
  396. "String used as prefix for timestamps clocking work hours on an item."
  397. :group 'org-keywords
  398. :type 'string)
  399. (defcustom org-comment-string "COMMENT"
  400. "Entries starting with this keyword will never be exported.
  401. An entry can be toggled between COMMENT and normal with
  402. \\[org-toggle-comment].
  403. Changes become only effective after restarting Emacs."
  404. :group 'org-keywords
  405. :type 'string)
  406. (defcustom org-quote-string "QUOTE"
  407. "Entries starting with this keyword will be exported in fixed-width font.
  408. Quoting applies only to the text in the entry following the headline, and does
  409. not extend beyond the next headline, even if that is lower level.
  410. An entry can be toggled between QUOTE and normal with
  411. \\[org-toggle-fixed-width-section]."
  412. :group 'org-keywords
  413. :type 'string)
  414. (defconst org-repeat-re
  415. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  416. "Regular expression for specifying repeated events.
  417. After a match, group 1 contains the repeat expression.")
  418. (defgroup org-structure nil
  419. "Options concerning the general structure of Org-mode files."
  420. :tag "Org Structure"
  421. :group 'org)
  422. (defgroup org-reveal-location nil
  423. "Options about how to make context of a location visible."
  424. :tag "Org Reveal Location"
  425. :group 'org-structure)
  426. (defconst org-context-choice
  427. '(choice
  428. (const :tag "Always" t)
  429. (const :tag "Never" nil)
  430. (repeat :greedy t :tag "Individual contexts"
  431. (cons
  432. (choice :tag "Context"
  433. (const agenda)
  434. (const org-goto)
  435. (const occur-tree)
  436. (const tags-tree)
  437. (const link-search)
  438. (const mark-goto)
  439. (const bookmark-jump)
  440. (const isearch)
  441. (const default))
  442. (boolean))))
  443. "Contexts for the reveal options.")
  444. (defcustom org-show-hierarchy-above '((default . t))
  445. "Non-nil means, show full hierarchy when revealing a location.
  446. Org-mode often shows locations in an org-mode file which might have
  447. been invisible before. When this is set, the hierarchy of headings
  448. above the exposed location is shown.
  449. Turning this off for example for sparse trees makes them very compact.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. Valid contexts are
  452. agenda when exposing an entry from the agenda
  453. org-goto when using the command `org-goto' on key C-c C-j
  454. occur-tree when using the command `org-occur' on key C-c /
  455. tags-tree when constructing a sparse tree based on tags matches
  456. link-search when exposing search matches associated with a link
  457. mark-goto when exposing the jump goal of a mark
  458. bookmark-jump when exposing a bookmark location
  459. isearch when exiting from an incremental search
  460. default default for all contexts not set explicitly"
  461. :group 'org-reveal-location
  462. :type org-context-choice)
  463. (defcustom org-show-following-heading '((default . nil))
  464. "Non-nil means, show following heading when revealing a location.
  465. Org-mode often shows locations in an org-mode file which might have
  466. been invisible before. When this is set, the heading following the
  467. match is shown.
  468. Turning this off for example for sparse trees makes them very compact,
  469. but makes it harder to edit the location of the match. In such a case,
  470. use the command \\[org-reveal] to show more context.
  471. Instead of t, this can also be an alist specifying this option for different
  472. contexts. See `org-show-hierarchy-above' for valid contexts."
  473. :group 'org-reveal-location
  474. :type org-context-choice)
  475. (defcustom org-show-siblings '((default . nil) (isearch t))
  476. "Non-nil means, show all sibling heading when revealing a location.
  477. Org-mode often shows locations in an org-mode file which might have
  478. been invisible before. When this is set, the sibling of the current entry
  479. heading are all made visible. If `org-show-hierarchy-above' is t,
  480. the same happens on each level of the hierarchy above the current entry.
  481. By default this is on for the isearch context, off for all other contexts.
  482. Turning this off for example for sparse trees makes them very compact,
  483. but makes it harder to edit the location of the match. In such a case,
  484. use the command \\[org-reveal] to show more context.
  485. Instead of t, this can also be an alist specifying this option for different
  486. contexts. See `org-show-hierarchy-above' for valid contexts."
  487. :group 'org-reveal-location
  488. :type org-context-choice)
  489. (defcustom org-show-entry-below '((default . nil))
  490. "Non-nil means, show the entry below a headline when revealing a location.
  491. Org-mode often shows locations in an org-mode file which might have
  492. been invisible before. When this is set, the text below the headline that is
  493. exposed is also shown.
  494. By default this is off for all contexts.
  495. Instead of t, this can also be an alist specifying this option for different
  496. contexts. See `org-show-hierarchy-above' for valid contexts."
  497. :group 'org-reveal-location
  498. :type org-context-choice)
  499. (defcustom org-indirect-buffer-display 'other-window
  500. "How should indirect tree buffers be displayed?
  501. This applies to indirect buffers created with the commands
  502. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  503. Valid values are:
  504. current-window Display in the current window
  505. other-window Just display in another window.
  506. dedicated-frame Create one new frame, and re-use it each time.
  507. new-frame Make a new frame each time. Note that in this case
  508. previously-made indirect buffers are kept, and you need to
  509. kill these buffers yourself."
  510. :group 'org-structure
  511. :group 'org-agenda-windows
  512. :type '(choice
  513. (const :tag "In current window" current-window)
  514. (const :tag "In current frame, other window" other-window)
  515. (const :tag "Each time a new frame" new-frame)
  516. (const :tag "One dedicated frame" dedicated-frame)))
  517. (defgroup org-cycle nil
  518. "Options concerning visibility cycling in Org-mode."
  519. :tag "Org Cycle"
  520. :group 'org-structure)
  521. (defcustom org-cycle-skip-children-state-if-no-children t
  522. "Non-nil means, skip CHILDREN state in entries that don't have any."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-max-level nil
  526. "Maximum level which should still be subject to visibility cycling.
  527. Levels higher than this will, for cycling, be treated as text, not a headline.
  528. When `org-odd-levels-only' is set, a value of N in this variable actually
  529. means 2N-1 stars as the limiting headline.
  530. When nil, cycle all levels.
  531. Note that the limiting level of cycling is also influenced by
  532. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  533. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  534. than its value."
  535. :group 'org-cycle
  536. :type '(choice
  537. (const :tag "No limit" nil)
  538. (integer :tag "Maximum level")))
  539. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  540. "Names of drawers. Drawers are not opened by cycling on the headline above.
  541. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  542. this:
  543. :DRAWERNAME:
  544. .....
  545. :END:
  546. The drawer \"PROPERTIES\" is special for capturing properties through
  547. the property API.
  548. Drawers can be defined on the per-file basis with a line like:
  549. #+DRAWERS: HIDDEN STATE PROPERTIES"
  550. :group 'org-structure
  551. :group 'org-cycle
  552. :type '(repeat (string :tag "Drawer Name")))
  553. (defcustom org-hide-block-startup nil
  554. "Non-nil means, , entering Org-mode will fold all blocks.
  555. This can also be set in on a per-file basis with
  556. #+STARTUP: hideblocks
  557. #+STARTUP: showblocks"
  558. :group 'org-startup
  559. :group 'org-cycle
  560. :type 'boolean)
  561. (defcustom org-cycle-global-at-bob nil
  562. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  563. This makes it possible to do global cycling without having to use S-TAB or
  564. C-u TAB. For this special case to work, the first line of the buffer
  565. must not be a headline - it may be empty or some other text. When used in
  566. this way, `org-cycle-hook' is disables temporarily, to make sure the
  567. cursor stays at the beginning of the buffer.
  568. When this option is nil, don't do anything special at the beginning
  569. of the buffer."
  570. :group 'org-cycle
  571. :type 'boolean)
  572. (defcustom org-cycle-emulate-tab t
  573. "Where should `org-cycle' emulate TAB.
  574. nil Never
  575. white Only in completely white lines
  576. whitestart Only at the beginning of lines, before the first non-white char
  577. t Everywhere except in headlines
  578. exc-hl-bol Everywhere except at the start of a headline
  579. If TAB is used in a place where it does not emulate TAB, the current subtree
  580. visibility is cycled."
  581. :group 'org-cycle
  582. :type '(choice (const :tag "Never" nil)
  583. (const :tag "Only in completely white lines" white)
  584. (const :tag "Before first char in a line" whitestart)
  585. (const :tag "Everywhere except in headlines" t)
  586. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  587. ))
  588. (defcustom org-cycle-separator-lines 2
  589. "Number of empty lines needed to keep an empty line between collapsed trees.
  590. If you leave an empty line between the end of a subtree and the following
  591. headline, this empty line is hidden when the subtree is folded.
  592. Org-mode will leave (exactly) one empty line visible if the number of
  593. empty lines is equal or larger to the number given in this variable.
  594. So the default 2 means, at least 2 empty lines after the end of a subtree
  595. are needed to produce free space between a collapsed subtree and the
  596. following headline.
  597. If the number is negative, and the number of empty lines is at least -N,
  598. all empty lines are shown.
  599. Special case: when 0, never leave empty lines in collapsed view."
  600. :group 'org-cycle
  601. :type 'integer)
  602. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  603. (defcustom org-pre-cycle-hook nil
  604. "Hook that is run before visibility cycling is happening.
  605. The function(s) in this hook must accept a single argument which indicates
  606. the new state that will be set right after running this hook. The
  607. argument is a symbol. Before a global state change, it can have the values
  608. `overview', `content', or `all'. Before a local state change, it can have
  609. the values `folded', `children', or `subtree'."
  610. :group 'org-cycle
  611. :type 'hook)
  612. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  613. org-cycle-hide-drawers
  614. org-cycle-show-empty-lines
  615. org-optimize-window-after-visibility-change)
  616. "Hook that is run after `org-cycle' has changed the buffer visibility.
  617. The function(s) in this hook must accept a single argument which indicates
  618. the new state that was set by the most recent `org-cycle' command. The
  619. argument is a symbol. After a global state change, it can have the values
  620. `overview', `content', or `all'. After a local state change, it can have
  621. the values `folded', `children', or `subtree'."
  622. :group 'org-cycle
  623. :type 'hook)
  624. (defgroup org-edit-structure nil
  625. "Options concerning structure editing in Org-mode."
  626. :tag "Org Edit Structure"
  627. :group 'org-structure)
  628. (defcustom org-odd-levels-only nil
  629. "Non-nil means, skip even levels and only use odd levels for the outline.
  630. This has the effect that two stars are being added/taken away in
  631. promotion/demotion commands. It also influences how levels are
  632. handled by the exporters.
  633. Changing it requires restart of `font-lock-mode' to become effective
  634. for fontification also in regions already fontified.
  635. You may also set this on a per-file basis by adding one of the following
  636. lines to the buffer:
  637. #+STARTUP: odd
  638. #+STARTUP: oddeven"
  639. :group 'org-edit-structure
  640. :group 'org-font-lock
  641. :type 'boolean)
  642. (defcustom org-adapt-indentation t
  643. "Non-nil means, adapt indentation to outline node level.
  644. When this variable is set, Org assumes that you write outlines by
  645. indenting text in each node to align with the headline (after the stars).
  646. The following issues are influenced by this variable:
  647. - When this is set and the *entire* text in an entry is indented, the
  648. indentation is increased by one space in a demotion command, and
  649. decreased by one in a promotion command. If any line in the entry
  650. body starts with text at column 0, indentation is not changed at all.
  651. - Property drawers and planning information is inserted indented when
  652. this variable s set. When nil, they will not be indented.
  653. - TAB indents a line relative to context. The lines below a headline
  654. will be indented when this variable is set.
  655. Note that this is all about true indentation, by adding and removing
  656. space characters. See also `org-indent.el' which does level-dependent
  657. indentation in a virtual way, i.e. at display time in Emacs."
  658. :group 'org-edit-structure
  659. :type 'boolean)
  660. (defcustom org-special-ctrl-a/e nil
  661. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  662. When t, `C-a' will bring back the cursor to the beginning of the
  663. headline text, i.e. after the stars and after a possible TODO keyword.
  664. In an item, this will be the position after the bullet.
  665. When the cursor is already at that position, another `C-a' will bring
  666. it to the beginning of the line.
  667. `C-e' will jump to the end of the headline, ignoring the presence of tags
  668. in the headline. A second `C-e' will then jump to the true end of the
  669. line, after any tags.
  670. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  671. going to the true line boundary first. Only a directly following, identical
  672. keypress will bring the cursor to the special positions.
  673. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  674. set separately."
  675. :group 'org-edit-structure
  676. :type '(choice
  677. (const :tag "off" nil)
  678. (const :tag "after stars/bullet and before tags first" t)
  679. (const :tag "true line boundary first" reversed)
  680. (cons :tag "Set C-a and C-e separately"
  681. (choice :tag "Special C-a"
  682. (const :tag "off" nil)
  683. (const :tag "after stars/bullet first" t)
  684. (const :tag "before stars/bullet first" reversed))
  685. (choice :tag "Special C-e"
  686. (const :tag "off" nil)
  687. (const :tag "before tags first" t)
  688. (const :tag "after tags first" reversed)))))
  689. (if (fboundp 'defvaralias)
  690. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  691. (defcustom org-special-ctrl-k nil
  692. "Non-nil means `C-k' will behave specially in headlines.
  693. When nil, `C-k' will call the default `kill-line' command.
  694. When t, the following will happen while the cursor is in the headline:
  695. - When the cursor is at the beginning of a headline, kill the entire
  696. line and possible the folded subtree below the line.
  697. - When in the middle of the headline text, kill the headline up to the tags.
  698. - When after the headline text, kill the tags."
  699. :group 'org-edit-structure
  700. :type 'boolean)
  701. (defcustom org-yank-folded-subtrees t
  702. "Non-nil means, when yanking subtrees, fold them.
  703. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  704. it starts with a heading and all other headings in it are either children
  705. or siblings, then fold all the subtrees. However, do this only if no
  706. text after the yank would be swallowed into a folded tree by this action."
  707. :group 'org-edit-structure
  708. :type 'boolean)
  709. (defcustom org-yank-adjusted-subtrees nil
  710. "Non-nil means, when yanking subtrees, adjust the level.
  711. With this setting, `org-paste-subtree' is used to insert the subtree, see
  712. this function for details."
  713. :group 'org-edit-structure
  714. :type 'boolean)
  715. (defcustom org-M-RET-may-split-line '((default . t))
  716. "Non-nil means, M-RET will split the line at the cursor position.
  717. When nil, it will go to the end of the line before making a
  718. new line.
  719. You may also set this option in a different way for different
  720. contexts. Valid contexts are:
  721. headline when creating a new headline
  722. item when creating a new item
  723. table in a table field
  724. default the value to be used for all contexts not explicitly
  725. customized"
  726. :group 'org-structure
  727. :group 'org-table
  728. :type '(choice
  729. (const :tag "Always" t)
  730. (const :tag "Never" nil)
  731. (repeat :greedy t :tag "Individual contexts"
  732. (cons
  733. (choice :tag "Context"
  734. (const headline)
  735. (const item)
  736. (const table)
  737. (const default))
  738. (boolean)))))
  739. (defcustom org-insert-heading-respect-content nil
  740. "Non-nil means, insert new headings after the current subtree.
  741. When nil, the new heading is created directly after the current line.
  742. The commands \\[org-insert-heading-respect-content] and
  743. \\[org-insert-todo-heading-respect-content] turn this variable on
  744. for the duration of the command."
  745. :group 'org-structure
  746. :type 'boolean)
  747. (defcustom org-blank-before-new-entry '((heading . auto)
  748. (plain-list-item . auto))
  749. "Should `org-insert-heading' leave a blank line before new heading/item?
  750. The value is an alist, with `heading' and `plain-list-item' as car,
  751. and a boolean flag as cdr. For plain lists, if the variable
  752. `org-empty-line-terminates-plain-lists' is set, the setting here
  753. is ignored and no empty line is inserted, to keep the list in tact."
  754. :group 'org-edit-structure
  755. :type '(list
  756. (cons (const heading)
  757. (choice (const :tag "Never" nil)
  758. (const :tag "Always" t)
  759. (const :tag "Auto" auto)))
  760. (cons (const plain-list-item)
  761. (choice (const :tag "Never" nil)
  762. (const :tag "Always" t)
  763. (const :tag "Auto" auto)))))
  764. (defcustom org-insert-heading-hook nil
  765. "Hook being run after inserting a new heading."
  766. :group 'org-edit-structure
  767. :type 'hook)
  768. (defcustom org-enable-fixed-width-editor t
  769. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  770. This currently only means, they are never auto-wrapped.
  771. When nil, such lines will be treated like ordinary lines.
  772. See also the QUOTE keyword."
  773. :group 'org-edit-structure
  774. :type 'boolean)
  775. (defcustom org-goto-auto-isearch t
  776. "Non-nil means, typing characters in org-goto starts incremental search."
  777. :group 'org-edit-structure
  778. :type 'boolean)
  779. (defgroup org-sparse-trees nil
  780. "Options concerning sparse trees in Org-mode."
  781. :tag "Org Sparse Trees"
  782. :group 'org-structure)
  783. (defcustom org-highlight-sparse-tree-matches t
  784. "Non-nil means, highlight all matches that define a sparse tree.
  785. The highlights will automatically disappear the next time the buffer is
  786. changed by an edit command."
  787. :group 'org-sparse-trees
  788. :type 'boolean)
  789. (defcustom org-remove-highlights-with-change t
  790. "Non-nil means, any change to the buffer will remove temporary highlights.
  791. Such highlights are created by `org-occur' and `org-clock-display'.
  792. When nil, `C-c C-c needs to be used to get rid of the highlights.
  793. The highlights created by `org-preview-latex-fragment' always need
  794. `C-c C-c' to be removed."
  795. :group 'org-sparse-trees
  796. :group 'org-time
  797. :type 'boolean)
  798. (defcustom org-occur-hook '(org-first-headline-recenter)
  799. "Hook that is run after `org-occur' has constructed a sparse tree.
  800. This can be used to recenter the window to show as much of the structure
  801. as possible."
  802. :group 'org-sparse-trees
  803. :type 'hook)
  804. (defgroup org-imenu-and-speedbar nil
  805. "Options concerning imenu and speedbar in Org-mode."
  806. :tag "Org Imenu and Speedbar"
  807. :group 'org-structure)
  808. (defcustom org-imenu-depth 2
  809. "The maximum level for Imenu access to Org-mode headlines.
  810. This also applied for speedbar access."
  811. :group 'org-imenu-and-speedbar
  812. :type 'integer)
  813. (defgroup org-table nil
  814. "Options concerning tables in Org-mode."
  815. :tag "Org Table"
  816. :group 'org)
  817. (defcustom org-enable-table-editor 'optimized
  818. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  819. When nil, such lines will be treated like ordinary lines.
  820. When equal to the symbol `optimized', the table editor will be optimized to
  821. do the following:
  822. - Automatic overwrite mode in front of whitespace in table fields.
  823. This makes the structure of the table stay in tact as long as the edited
  824. field does not exceed the column width.
  825. - Minimize the number of realigns. Normally, the table is aligned each time
  826. TAB or RET are pressed to move to another field. With optimization this
  827. happens only if changes to a field might have changed the column width.
  828. Optimization requires replacing the functions `self-insert-command',
  829. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  830. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  831. very good at guessing when a re-align will be necessary, but you can always
  832. force one with \\[org-ctrl-c-ctrl-c].
  833. If you would like to use the optimized version in Org-mode, but the
  834. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  835. This variable can be used to turn on and off the table editor during a session,
  836. but in order to toggle optimization, a restart is required.
  837. See also the variable `org-table-auto-blank-field'."
  838. :group 'org-table
  839. :type '(choice
  840. (const :tag "off" nil)
  841. (const :tag "on" t)
  842. (const :tag "on, optimized" optimized)))
  843. (defcustom org-self-insert-cluster-for-undo t
  844. "Non-nil means cluster self-insert commands for undo when possible.
  845. If this is set, then, like in the Emacs command loop, 20 consequtive
  846. characters will be undone together.
  847. This is configurable, because there is some impact on typing performance."
  848. :group 'org-table
  849. :type 'boolean)
  850. (defcustom org-table-tab-recognizes-table.el t
  851. "Non-nil means, TAB will automatically notice a table.el table.
  852. When it sees such a table, it moves point into it and - if necessary -
  853. calls `table-recognize-table'."
  854. :group 'org-table-editing
  855. :type 'boolean)
  856. (defgroup org-link nil
  857. "Options concerning links in Org-mode."
  858. :tag "Org Link"
  859. :group 'org)
  860. (defvar org-link-abbrev-alist-local nil
  861. "Buffer-local version of `org-link-abbrev-alist', which see.
  862. The value of this is taken from the #+LINK lines.")
  863. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  864. (defcustom org-link-abbrev-alist nil
  865. "Alist of link abbreviations.
  866. The car of each element is a string, to be replaced at the start of a link.
  867. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  868. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  869. [[linkkey:tag][description]]
  870. The 'linkkey' must be a word word, starting with a letter, followed
  871. by letters, numbers, '-' or '_'.
  872. If REPLACE is a string, the tag will simply be appended to create the link.
  873. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  874. the placeholder \"%h\" will cause a url-encoded version of the tag to
  875. be inserted at that point (see the function `url-hexify-string').
  876. REPLACE may also be a function that will be called with the tag as the
  877. only argument to create the link, which should be returned as a string.
  878. See the manual for examples."
  879. :group 'org-link
  880. :type '(repeat
  881. (cons
  882. (string :tag "Protocol")
  883. (choice
  884. (string :tag "Format")
  885. (function)))))
  886. (defcustom org-descriptive-links t
  887. "Non-nil means, hide link part and only show description of bracket links.
  888. Bracket links are like [[link][description]]. This variable sets the initial
  889. state in new org-mode buffers. The setting can then be toggled on a
  890. per-buffer basis from the Org->Hyperlinks menu."
  891. :group 'org-link
  892. :type 'boolean)
  893. (defcustom org-link-file-path-type 'adaptive
  894. "How the path name in file links should be stored.
  895. Valid values are:
  896. relative Relative to the current directory, i.e. the directory of the file
  897. into which the link is being inserted.
  898. absolute Absolute path, if possible with ~ for home directory.
  899. noabbrev Absolute path, no abbreviation of home directory.
  900. adaptive Use relative path for files in the current directory and sub-
  901. directories of it. For other files, use an absolute path."
  902. :group 'org-link
  903. :type '(choice
  904. (const relative)
  905. (const absolute)
  906. (const noabbrev)
  907. (const adaptive)))
  908. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  909. "Types of links that should be activated in Org-mode files.
  910. This is a list of symbols, each leading to the activation of a certain link
  911. type. In principle, it does not hurt to turn on most link types - there may
  912. be a small gain when turning off unused link types. The types are:
  913. bracket The recommended [[link][description]] or [[link]] links with hiding.
  914. angular Links in angular brackets that may contain whitespace like
  915. <bbdb:Carsten Dominik>.
  916. plain Plain links in normal text, no whitespace, like http://google.com.
  917. radio Text that is matched by a radio target, see manual for details.
  918. tag Tag settings in a headline (link to tag search).
  919. date Time stamps (link to calendar).
  920. footnote Footnote labels.
  921. Changing this variable requires a restart of Emacs to become effective."
  922. :group 'org-link
  923. :type '(set :greedy t
  924. (const :tag "Double bracket links (new style)" bracket)
  925. (const :tag "Angular bracket links (old style)" angular)
  926. (const :tag "Plain text links" plain)
  927. (const :tag "Radio target matches" radio)
  928. (const :tag "Tags" tag)
  929. (const :tag "Timestamps" date)
  930. (const :tag "Footnotes" footnote)))
  931. (defcustom org-make-link-description-function nil
  932. "Function to use to generate link descriptions from links. If
  933. nil the link location will be used. This function must take two
  934. parameters; the first is the link and the second the description
  935. org-insert-link has generated, and should return the description
  936. to use."
  937. :group 'org-link
  938. :type 'function)
  939. (defgroup org-link-store nil
  940. "Options concerning storing links in Org-mode."
  941. :tag "Org Store Link"
  942. :group 'org-link)
  943. (defcustom org-email-link-description-format "Email %c: %.30s"
  944. "Format of the description part of a link to an email or usenet message.
  945. The following %-escapes will be replaced by corresponding information:
  946. %F full \"From\" field
  947. %f name, taken from \"From\" field, address if no name
  948. %T full \"To\" field
  949. %t first name in \"To\" field, address if no name
  950. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  951. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  952. %s subject
  953. %m message-id.
  954. You may use normal field width specification between the % and the letter.
  955. This is for example useful to limit the length of the subject.
  956. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  957. :group 'org-link-store
  958. :type 'string)
  959. (defcustom org-from-is-user-regexp
  960. (let (r1 r2)
  961. (when (and user-mail-address (not (string= user-mail-address "")))
  962. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  963. (when (and user-full-name (not (string= user-full-name "")))
  964. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  965. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  966. "Regexp matched against the \"From:\" header of an email or usenet message.
  967. It should match if the message is from the user him/herself."
  968. :group 'org-link-store
  969. :type 'regexp)
  970. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  971. "Non-nil means, storing a link to an Org file will use entry IDs.
  972. Note that before this variable is even considered, org-id must be loaded,
  973. so please customize `org-modules' and turn it on.
  974. The variable can have the following values:
  975. t Create an ID if needed to make a link to the current entry.
  976. create-if-interactive
  977. If `org-store-link' is called directly (interactively, as a user
  978. command), do create an ID to support the link. But when doing the
  979. job for remember, only use the ID if it already exists. The
  980. purpose of this setting is to avoid proliferation of unwanted
  981. IDs, just because you happen to be in an Org file when you
  982. call `org-remember' that automatically and preemptively
  983. creates a link. If you do want to get an ID link in a remember
  984. template to an entry not having an ID, create it first by
  985. explicitly creating a link to it, using `C-c C-l' first.
  986. create-if-interactive-and-no-custom-id
  987. Like create-if-interactive, but do not create an ID if there is
  988. a CUSTOM_ID property defined in the entry. This is the default.
  989. use-existing
  990. Use existing ID, do not create one.
  991. nil Never use an ID to make a link, instead link using a text search for
  992. the headline text."
  993. :group 'org-link-store
  994. :type '(choice
  995. (const :tag "Create ID to make link" t)
  996. (const :tag "Create if storing link interactively"
  997. create-if-interactive)
  998. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  999. create-if-interactive-and-no-custom-id)
  1000. (const :tag "Only use existing" use-existing)
  1001. (const :tag "Do not use ID to create link" nil)))
  1002. (defcustom org-context-in-file-links t
  1003. "Non-nil means, file links from `org-store-link' contain context.
  1004. A search string will be added to the file name with :: as separator and
  1005. used to find the context when the link is activated by the command
  1006. `org-open-at-point'.
  1007. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1008. negates this setting for the duration of the command."
  1009. :group 'org-link-store
  1010. :type 'boolean)
  1011. (defcustom org-keep-stored-link-after-insertion nil
  1012. "Non-nil means, keep link in list for entire session.
  1013. The command `org-store-link' adds a link pointing to the current
  1014. location to an internal list. These links accumulate during a session.
  1015. The command `org-insert-link' can be used to insert links into any
  1016. Org-mode file (offering completion for all stored links). When this
  1017. option is nil, every link which has been inserted once using \\[org-insert-link]
  1018. will be removed from the list, to make completing the unused links
  1019. more efficient."
  1020. :group 'org-link-store
  1021. :type 'boolean)
  1022. (defgroup org-link-follow nil
  1023. "Options concerning following links in Org-mode."
  1024. :tag "Org Follow Link"
  1025. :group 'org-link)
  1026. (defcustom org-link-translation-function nil
  1027. "Function to translate links with different syntax to Org syntax.
  1028. This can be used to translate links created for example by the Planner
  1029. or emacs-wiki packages to Org syntax.
  1030. The function must accept two parameters, a TYPE containing the link
  1031. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1032. which is everything after the link protocol. It should return a cons
  1033. with possibly modified values of type and path.
  1034. Org contains a function for this, so if you set this variable to
  1035. `org-translate-link-from-planner', you should be able follow many
  1036. links created by planner."
  1037. :group 'org-link-follow
  1038. :type 'function)
  1039. (defcustom org-follow-link-hook nil
  1040. "Hook that is run after a link has been followed."
  1041. :group 'org-link-follow
  1042. :type 'hook)
  1043. (defcustom org-tab-follows-link nil
  1044. "Non-nil means, on links TAB will follow the link.
  1045. Needs to be set before org.el is loaded.
  1046. This really should not be used, it does not make sense, and the
  1047. implementation is bad."
  1048. :group 'org-link-follow
  1049. :type 'boolean)
  1050. (defcustom org-return-follows-link nil
  1051. "Non-nil means, on links RET will follow the link.
  1052. Needs to be set before org.el is loaded."
  1053. :group 'org-link-follow
  1054. :type 'boolean)
  1055. (defcustom org-mouse-1-follows-link
  1056. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1057. "Non-nil means, mouse-1 on a link will follow the link.
  1058. A longer mouse click will still set point. Does not work on XEmacs.
  1059. Needs to be set before org.el is loaded."
  1060. :group 'org-link-follow
  1061. :type 'boolean)
  1062. (defcustom org-mark-ring-length 4
  1063. "Number of different positions to be recorded in the ring
  1064. Changing this requires a restart of Emacs to work correctly."
  1065. :group 'org-link-follow
  1066. :type 'integer)
  1067. (defcustom org-link-frame-setup
  1068. '((vm . vm-visit-folder-other-frame)
  1069. (gnus . gnus-other-frame)
  1070. (file . find-file-other-window))
  1071. "Setup the frame configuration for following links.
  1072. When following a link with Emacs, it may often be useful to display
  1073. this link in another window or frame. This variable can be used to
  1074. set this up for the different types of links.
  1075. For VM, use any of
  1076. `vm-visit-folder'
  1077. `vm-visit-folder-other-frame'
  1078. For Gnus, use any of
  1079. `gnus'
  1080. `gnus-other-frame'
  1081. `org-gnus-no-new-news'
  1082. For FILE, use any of
  1083. `find-file'
  1084. `find-file-other-window'
  1085. `find-file-other-frame'
  1086. For the calendar, use the variable `calendar-setup'.
  1087. For BBDB, it is currently only possible to display the matches in
  1088. another window."
  1089. :group 'org-link-follow
  1090. :type '(list
  1091. (cons (const vm)
  1092. (choice
  1093. (const vm-visit-folder)
  1094. (const vm-visit-folder-other-window)
  1095. (const vm-visit-folder-other-frame)))
  1096. (cons (const gnus)
  1097. (choice
  1098. (const gnus)
  1099. (const gnus-other-frame)
  1100. (const org-gnus-no-new-news)))
  1101. (cons (const file)
  1102. (choice
  1103. (const find-file)
  1104. (const find-file-other-window)
  1105. (const find-file-other-frame)))))
  1106. (defcustom org-display-internal-link-with-indirect-buffer nil
  1107. "Non-nil means, use indirect buffer to display infile links.
  1108. Activating internal links (from one location in a file to another location
  1109. in the same file) normally just jumps to the location. When the link is
  1110. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1111. another window. When this option is set, the other window actually displays
  1112. an indirect buffer clone of the current buffer, to avoid any visibility
  1113. changes to the current buffer."
  1114. :group 'org-link-follow
  1115. :type 'boolean)
  1116. (defcustom org-open-non-existing-files nil
  1117. "Non-nil means, `org-open-file' will open non-existing files.
  1118. When nil, an error will be generated.
  1119. This variable applies only to external applications because they
  1120. might choke on non-existing files. If the link is to a file that
  1121. will be openend in Emacs, the variable is ignored."
  1122. :group 'org-link-follow
  1123. :type 'boolean)
  1124. (defcustom org-open-directory-means-index-dot-org nil
  1125. "Non-nil means, a link to a directory really means to index.org.
  1126. When nil, following a directory link will run dired or open a finder/explorer
  1127. window on that directory."
  1128. :group 'org-link-follow
  1129. :type 'boolean)
  1130. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1131. "Function and arguments to call for following mailto links.
  1132. This is a list with the first element being a lisp function, and the
  1133. remaining elements being arguments to the function. In string arguments,
  1134. %a will be replaced by the address, and %s will be replaced by the subject
  1135. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1136. :group 'org-link-follow
  1137. :type '(choice
  1138. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1139. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1140. (const :tag "message-mail" (message-mail "%a" "%s"))
  1141. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1142. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1143. "Non-nil means, ask for confirmation before executing shell links.
  1144. Shell links can be dangerous: just think about a link
  1145. [[shell:rm -rf ~/*][Google Search]]
  1146. This link would show up in your Org-mode document as \"Google Search\",
  1147. but really it would remove your entire home directory.
  1148. Therefore we advise against setting this variable to nil.
  1149. Just change it to `y-or-n-p' if you want to confirm with a
  1150. single keystroke rather than having to type \"yes\"."
  1151. :group 'org-link-follow
  1152. :type '(choice
  1153. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1154. (const :tag "with y-or-n (faster)" y-or-n-p)
  1155. (const :tag "no confirmation (dangerous)" nil)))
  1156. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1157. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1158. Elisp links can be dangerous: just think about a link
  1159. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1160. This link would show up in your Org-mode document as \"Google Search\",
  1161. but really it would remove your entire home directory.
  1162. Therefore we advise against setting this variable to nil.
  1163. Just change it to `y-or-n-p' if you want to confirm with a
  1164. single keystroke rather than having to type \"yes\"."
  1165. :group 'org-link-follow
  1166. :type '(choice
  1167. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1168. (const :tag "with y-or-n (faster)" y-or-n-p)
  1169. (const :tag "no confirmation (dangerous)" nil)))
  1170. (defconst org-file-apps-defaults-gnu
  1171. '((remote . emacs)
  1172. (system . mailcap)
  1173. (t . mailcap))
  1174. "Default file applications on a UNIX or GNU/Linux system.
  1175. See `org-file-apps'.")
  1176. (defconst org-file-apps-defaults-macosx
  1177. '((remote . emacs)
  1178. (t . "open %s")
  1179. (system . "open %s")
  1180. ("ps.gz" . "gv %s")
  1181. ("eps.gz" . "gv %s")
  1182. ("dvi" . "xdvi %s")
  1183. ("fig" . "xfig %s"))
  1184. "Default file applications on a MacOS X system.
  1185. The system \"open\" is known as a default, but we use X11 applications
  1186. for some files for which the OS does not have a good default.
  1187. See `org-file-apps'.")
  1188. (defconst org-file-apps-defaults-windowsnt
  1189. (list
  1190. '(remote . emacs)
  1191. (cons t
  1192. (list (if (featurep 'xemacs)
  1193. 'mswindows-shell-execute
  1194. 'w32-shell-execute)
  1195. "open" 'file))
  1196. (cons 'system
  1197. (list (if (featurep 'xemacs)
  1198. 'mswindows-shell-execute
  1199. 'w32-shell-execute)
  1200. "open" 'file)))
  1201. "Default file applications on a Windows NT system.
  1202. The system \"open\" is used for most files.
  1203. See `org-file-apps'.")
  1204. (defcustom org-file-apps
  1205. '(
  1206. (auto-mode . emacs)
  1207. ("\\.x?html?\\'" . default)
  1208. ("\\.pdf\\'" . default)
  1209. )
  1210. "External applications for opening `file:path' items in a document.
  1211. Org-mode uses system defaults for different file types, but
  1212. you can use this variable to set the application for a given file
  1213. extension. The entries in this list are cons cells where the car identifies
  1214. files and the cdr the corresponding command. Possible values for the
  1215. file identifier are
  1216. \"regex\" Regular expression matched against the file name. For backward
  1217. compatibility, this can also be a string with only alphanumeric
  1218. characters, which is then interpreted as an extension.
  1219. `directory' Matches a directory
  1220. `remote' Matches a remote file, accessible through tramp or efs.
  1221. Remote files most likely should be visited through Emacs
  1222. because external applications cannot handle such paths.
  1223. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1224. so all files Emacs knows how to handle. Using this with
  1225. command `emacs' will open most files in Emacs. Beware that this
  1226. will also open html files inside Emacs, unless you add
  1227. (\"html\" . default) to the list as well.
  1228. t Default for files not matched by any of the other options.
  1229. `system' The system command to open files, like `open' on Windows
  1230. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1231. that will be selected if you call `C-c C-o' with a double
  1232. `C-u C-u' prefix.
  1233. Possible values for the command are:
  1234. `emacs' The file will be visited by the current Emacs process.
  1235. `default' Use the default application for this file type, which is the
  1236. association for t in the list, most likely in the system-specific
  1237. part.
  1238. This can be used to overrule an unwanted setting in the
  1239. system-specific variable.
  1240. `system' Use the system command for opening files, like \"open\".
  1241. This command is specified by the entry whose car is `system'.
  1242. Most likely, the system-specific version of this variable
  1243. does define this command, but you can overrule/replace it
  1244. here.
  1245. string A command to be executed by a shell; %s will be replaced
  1246. by the path to the file.
  1247. sexp A Lisp form which will be evaluated. The file path will
  1248. be available in the Lisp variable `file'.
  1249. For more examples, see the system specific constants
  1250. `org-file-apps-defaults-macosx'
  1251. `org-file-apps-defaults-windowsnt'
  1252. `org-file-apps-defaults-gnu'."
  1253. :group 'org-link-follow
  1254. :type '(repeat
  1255. (cons (choice :value ""
  1256. (string :tag "Extension")
  1257. (const :tag "System command to open files" system)
  1258. (const :tag "Default for unrecognized files" t)
  1259. (const :tag "Remote file" remote)
  1260. (const :tag "Links to a directory" directory)
  1261. (const :tag "Any files that have Emacs modes"
  1262. auto-mode))
  1263. (choice :value ""
  1264. (const :tag "Visit with Emacs" emacs)
  1265. (const :tag "Use default" default)
  1266. (const :tag "Use the system command" system)
  1267. (string :tag "Command")
  1268. (sexp :tag "Lisp form")))))
  1269. (defgroup org-refile nil
  1270. "Options concerning refiling entries in Org-mode."
  1271. :tag "Org Refile"
  1272. :group 'org)
  1273. (defcustom org-directory "~/org"
  1274. "Directory with org files.
  1275. This is just a default location to look for Org files. There is no need
  1276. at all to put your files into this directory. It is only used in the
  1277. following situations:
  1278. 1. When a remember template specifies a target file that is not an
  1279. absolute path. The path will then be interpreted relative to
  1280. `org-directory'
  1281. 2. When a remember note is filed away in an interactive way (when exiting the
  1282. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1283. with `org-directory' as the default path."
  1284. :group 'org-refile
  1285. :group 'org-remember
  1286. :type 'directory)
  1287. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1288. "Default target for storing notes.
  1289. Used by the hooks for remember.el. This can be a string, or nil to mean
  1290. the value of `remember-data-file'.
  1291. You can set this on a per-template basis with the variable
  1292. `org-remember-templates'."
  1293. :group 'org-refile
  1294. :group 'org-remember
  1295. :type '(choice
  1296. (const :tag "Default from remember-data-file" nil)
  1297. file))
  1298. (defcustom org-goto-interface 'outline
  1299. "The default interface to be used for `org-goto'.
  1300. Allowed values are:
  1301. outline The interface shows an outline of the relevant file
  1302. and the correct heading is found by moving through
  1303. the outline or by searching with incremental search.
  1304. outline-path-completion Headlines in the current buffer are offered via
  1305. completion. This is the interface also used by
  1306. the refile command."
  1307. :group 'org-refile
  1308. :type '(choice
  1309. (const :tag "Outline" outline)
  1310. (const :tag "Outline-path-completion" outline-path-completion)))
  1311. (defcustom org-goto-max-level 5
  1312. "Maximum level to be considered when running org-goto with refile interface."
  1313. :group 'org-refile
  1314. :type 'integer)
  1315. (defcustom org-reverse-note-order nil
  1316. "Non-nil means, store new notes at the beginning of a file or entry.
  1317. When nil, new notes will be filed to the end of a file or entry.
  1318. This can also be a list with cons cells of regular expressions that
  1319. are matched against file names, and values."
  1320. :group 'org-remember
  1321. :group 'org-refile
  1322. :type '(choice
  1323. (const :tag "Reverse always" t)
  1324. (const :tag "Reverse never" nil)
  1325. (repeat :tag "By file name regexp"
  1326. (cons regexp boolean))))
  1327. (defcustom org-refile-targets nil
  1328. "Targets for refiling entries with \\[org-refile].
  1329. This is list of cons cells. Each cell contains:
  1330. - a specification of the files to be considered, either a list of files,
  1331. or a symbol whose function or variable value will be used to retrieve
  1332. a file name or a list of file names. If you use `org-agenda-files' for
  1333. that, all agenda files will be scanned for targets. Nil means, consider
  1334. headings in the current buffer.
  1335. - A specification of how to find candidate refile targets. This may be
  1336. any of:
  1337. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1338. This tag has to be present in all target headlines, inheritance will
  1339. not be considered.
  1340. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1341. todo keyword.
  1342. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1343. headlines that are refiling targets.
  1344. - a cons cell (:level . N). Any headline of level N is considered a target.
  1345. Note that, when `org-odd-levels-only' is set, level corresponds to
  1346. order in hierarchy, not to the number of stars.
  1347. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1348. Note that, when `org-odd-levels-only' is set, level corresponds to
  1349. order in hierarchy, not to the number of stars.
  1350. You can set the variable `org-refile-target-verify-function' to a function
  1351. to verify each headline found by the simple critery above.
  1352. When this variable is nil, all top-level headlines in the current buffer
  1353. are used, equivalent to the value `((nil . (:level . 1))'."
  1354. :group 'org-refile
  1355. :type '(repeat
  1356. (cons
  1357. (choice :value org-agenda-files
  1358. (const :tag "All agenda files" org-agenda-files)
  1359. (const :tag "Current buffer" nil)
  1360. (function) (variable) (file))
  1361. (choice :tag "Identify target headline by"
  1362. (cons :tag "Specific tag" (const :value :tag) (string))
  1363. (cons :tag "TODO keyword" (const :value :todo) (string))
  1364. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1365. (cons :tag "Level number" (const :value :level) (integer))
  1366. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1367. (defcustom org-refile-target-verify-function nil
  1368. "Function to verify if the headline at point should be a refile target.
  1369. The function will be called without arguments, with point at the
  1370. beginning of the headline. It should return t and leave point
  1371. where it is if the headline is a valid target for refiling.
  1372. If the target should not be selected, the function must return nil.
  1373. In addition to this, it may move point to a place from where the search
  1374. should be continued. For example, the function may decide that the entire
  1375. subtree of the current entry should be excluded and move point to the end
  1376. of the subtree."
  1377. :group 'org-refile
  1378. :type 'function)
  1379. (defcustom org-refile-use-outline-path nil
  1380. "Non-nil means, provide refile targets as paths.
  1381. So a level 3 headline will be available as level1/level2/level3.
  1382. When the value is `file', also include the file name (without directory)
  1383. into the path. In this case, you can also stop the completion after
  1384. the file name, to get entries inserted as top level in the file.
  1385. When `full-file-path', include the full file path."
  1386. :group 'org-refile
  1387. :type '(choice
  1388. (const :tag "Not" nil)
  1389. (const :tag "Yes" t)
  1390. (const :tag "Start with file name" file)
  1391. (const :tag "Start with full file path" full-file-path)))
  1392. (defcustom org-outline-path-complete-in-steps t
  1393. "Non-nil means, complete the outline path in hierarchical steps.
  1394. When Org-mode uses the refile interface to select an outline path
  1395. \(see variable `org-refile-use-outline-path'), the completion of
  1396. the path can be done is a single go, or if can be done in steps down
  1397. the headline hierarchy. Going in steps is probably the best if you
  1398. do not use a special completion package like `ido' or `icicles'.
  1399. However, when using these packages, going in one step can be very
  1400. fast, while still showing the whole path to the entry."
  1401. :group 'org-refile
  1402. :type 'boolean)
  1403. (defcustom org-refile-allow-creating-parent-nodes nil
  1404. "Non-nil means, allow to create new nodes as refile targets.
  1405. New nodes are then created by adding \"/new node name\" to the completion
  1406. of an existing node. When the value of this variable is `confirm',
  1407. new node creation must be confirmed by the user (recommended)
  1408. When nil, the completion must match an existing entry.
  1409. Note that, if the new heading is not seen by the criteria
  1410. listed in `org-refile-targets', multiple instances of the same
  1411. heading would be created by trying again to file under the new
  1412. heading."
  1413. :group 'org-refile
  1414. :type '(choice
  1415. (const :tag "Never" nil)
  1416. (const :tag "Always" t)
  1417. (const :tag "Prompt for confirmation" confirm)))
  1418. (defgroup org-todo nil
  1419. "Options concerning TODO items in Org-mode."
  1420. :tag "Org TODO"
  1421. :group 'org)
  1422. (defgroup org-progress nil
  1423. "Options concerning Progress logging in Org-mode."
  1424. :tag "Org Progress"
  1425. :group 'org-time)
  1426. (defvar org-todo-interpretation-widgets
  1427. '(
  1428. (:tag "Sequence (cycling hits every state)" sequence)
  1429. (:tag "Type (cycling directly to DONE)" type))
  1430. "The available interpretation symbols for customizing
  1431. `org-todo-keywords'.
  1432. Interested libraries should add to this list.")
  1433. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1434. "List of TODO entry keyword sequences and their interpretation.
  1435. \\<org-mode-map>This is a list of sequences.
  1436. Each sequence starts with a symbol, either `sequence' or `type',
  1437. indicating if the keywords should be interpreted as a sequence of
  1438. action steps, or as different types of TODO items. The first
  1439. keywords are states requiring action - these states will select a headline
  1440. for inclusion into the global TODO list Org-mode produces. If one of
  1441. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1442. signify that no further action is necessary. If \"|\" is not found,
  1443. the last keyword is treated as the only DONE state of the sequence.
  1444. The command \\[org-todo] cycles an entry through these states, and one
  1445. additional state where no keyword is present. For details about this
  1446. cycling, see the manual.
  1447. TODO keywords and interpretation can also be set on a per-file basis with
  1448. the special #+SEQ_TODO and #+TYP_TODO lines.
  1449. Each keyword can optionally specify a character for fast state selection
  1450. \(in combination with the variable `org-use-fast-todo-selection')
  1451. and specifiers for state change logging, using the same syntax
  1452. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1453. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1454. indicates to record a time stamp each time this state is selected.
  1455. Each keyword may also specify if a timestamp or a note should be
  1456. recorded when entering or leaving the state, by adding additional
  1457. characters in the parenthesis after the keyword. This looks like this:
  1458. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1459. record only the time of the state change. With X and Y being either
  1460. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1461. Y when leaving the state if and only if the *target* state does not
  1462. define X. You may omit any of the fast-selection key or X or /Y,
  1463. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1464. For backward compatibility, this variable may also be just a list
  1465. of keywords - in this case the interpretation (sequence or type) will be
  1466. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1467. :group 'org-todo
  1468. :group 'org-keywords
  1469. :type '(choice
  1470. (repeat :tag "Old syntax, just keywords"
  1471. (string :tag "Keyword"))
  1472. (repeat :tag "New syntax"
  1473. (cons
  1474. (choice
  1475. :tag "Interpretation"
  1476. ;;Quick and dirty way to see
  1477. ;;`org-todo-interpretations'. This takes the
  1478. ;;place of item arguments
  1479. :convert-widget
  1480. (lambda (widget)
  1481. (widget-put widget
  1482. :args (mapcar
  1483. #'(lambda (x)
  1484. (widget-convert
  1485. (cons 'const x)))
  1486. org-todo-interpretation-widgets))
  1487. widget))
  1488. (repeat
  1489. (string :tag "Keyword"))))))
  1490. (defvar org-todo-keywords-1 nil
  1491. "All TODO and DONE keywords active in a buffer.")
  1492. (make-variable-buffer-local 'org-todo-keywords-1)
  1493. (defvar org-todo-keywords-for-agenda nil)
  1494. (defvar org-done-keywords-for-agenda nil)
  1495. (defvar org-todo-keyword-alist-for-agenda nil)
  1496. (defvar org-tag-alist-for-agenda nil)
  1497. (defvar org-agenda-contributing-files nil)
  1498. (defvar org-not-done-keywords nil)
  1499. (make-variable-buffer-local 'org-not-done-keywords)
  1500. (defvar org-done-keywords nil)
  1501. (make-variable-buffer-local 'org-done-keywords)
  1502. (defvar org-todo-heads nil)
  1503. (make-variable-buffer-local 'org-todo-heads)
  1504. (defvar org-todo-sets nil)
  1505. (make-variable-buffer-local 'org-todo-sets)
  1506. (defvar org-todo-log-states nil)
  1507. (make-variable-buffer-local 'org-todo-log-states)
  1508. (defvar org-todo-kwd-alist nil)
  1509. (make-variable-buffer-local 'org-todo-kwd-alist)
  1510. (defvar org-todo-key-alist nil)
  1511. (make-variable-buffer-local 'org-todo-key-alist)
  1512. (defvar org-todo-key-trigger nil)
  1513. (make-variable-buffer-local 'org-todo-key-trigger)
  1514. (defcustom org-todo-interpretation 'sequence
  1515. "Controls how TODO keywords are interpreted.
  1516. This variable is in principle obsolete and is only used for
  1517. backward compatibility, if the interpretation of todo keywords is
  1518. not given already in `org-todo-keywords'. See that variable for
  1519. more information."
  1520. :group 'org-todo
  1521. :group 'org-keywords
  1522. :type '(choice (const sequence)
  1523. (const type)))
  1524. (defcustom org-use-fast-todo-selection t
  1525. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1526. This variable describes if and under what circumstances the cycling
  1527. mechanism for TODO keywords will be replaced by a single-key, direct
  1528. selection scheme.
  1529. When nil, fast selection is never used.
  1530. When the symbol `prefix', it will be used when `org-todo' is called with
  1531. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1532. in an agenda buffer.
  1533. When t, fast selection is used by default. In this case, the prefix
  1534. argument forces cycling instead.
  1535. In all cases, the special interface is only used if access keys have actually
  1536. been assigned by the user, i.e. if keywords in the configuration are followed
  1537. by a letter in parenthesis, like TODO(t)."
  1538. :group 'org-todo
  1539. :type '(choice
  1540. (const :tag "Never" nil)
  1541. (const :tag "By default" t)
  1542. (const :tag "Only with C-u C-c C-t" prefix)))
  1543. (defcustom org-provide-todo-statistics t
  1544. "Non-nil means, update todo statistics after insert and toggle.
  1545. ALL-HEADLINES means update todo statistics by including headlines
  1546. with no TODO keyword as well, counting them as not done.
  1547. A list of TODO keywords means the same, but skip keywords that are
  1548. not in this list.
  1549. When this is set, todo statistics is updated in the parent of the
  1550. current entry each time a todo state is changed."
  1551. :group 'org-todo
  1552. :type '(choice
  1553. (const :tag "Yes, only for TODO entries" t)
  1554. (const :tag "Yes, including all entries" 'all-headlines)
  1555. (repeat :tag "Yes, for TODOs in this list"
  1556. (string :tag "TODO keyword"))
  1557. (other :tag "No TODO statistics" nil)))
  1558. (defcustom org-hierarchical-todo-statistics t
  1559. "Non-nil means, TODO statistics covers just direct children.
  1560. When nil, all entries in the subtree are considered.
  1561. This has only an effect if `org-provide-todo-statistics' is set.
  1562. To set this to nil for only a single subtree, use a COOKIE_DATA
  1563. property and include the word \"recursive\" into the value."
  1564. :group 'org-todo
  1565. :type 'boolean)
  1566. (defcustom org-after-todo-state-change-hook nil
  1567. "Hook which is run after the state of a TODO item was changed.
  1568. The new state (a string with a TODO keyword, or nil) is available in the
  1569. Lisp variable `state'."
  1570. :group 'org-todo
  1571. :type 'hook)
  1572. (defvar org-blocker-hook nil
  1573. "Hook for functions that are allowed to block a state change.
  1574. Each function gets as its single argument a property list, see
  1575. `org-trigger-hook' for more information about this list.
  1576. If any of the functions in this hook returns nil, the state change
  1577. is blocked.")
  1578. (defvar org-trigger-hook nil
  1579. "Hook for functions that are triggered by a state change.
  1580. Each function gets as its single argument a property list with at least
  1581. the following elements:
  1582. (:type type-of-change :position pos-at-entry-start
  1583. :from old-state :to new-state)
  1584. Depending on the type, more properties may be present.
  1585. This mechanism is currently implemented for:
  1586. TODO state changes
  1587. ------------------
  1588. :type todo-state-change
  1589. :from previous state (keyword as a string), or nil, or a symbol
  1590. 'todo' or 'done', to indicate the general type of state.
  1591. :to new state, like in :from")
  1592. (defcustom org-enforce-todo-dependencies nil
  1593. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1594. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1595. be blocked if any prior sibling is not yet done.
  1596. Finally, if the parent is blocked because of ordered siblings of its own,
  1597. the child will also be blocked.
  1598. This variable needs to be set before org.el is loaded, and you need to
  1599. restart Emacs after a change to make the change effective. The only way
  1600. to change is while Emacs is running is through the customize interface."
  1601. :set (lambda (var val)
  1602. (set var val)
  1603. (if val
  1604. (add-hook 'org-blocker-hook
  1605. 'org-block-todo-from-children-or-siblings-or-parent)
  1606. (remove-hook 'org-blocker-hook
  1607. 'org-block-todo-from-children-or-siblings-or-parent)))
  1608. :group 'org-todo
  1609. :type 'boolean)
  1610. (defcustom org-enforce-todo-checkbox-dependencies nil
  1611. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1612. When this is nil, checkboxes have no influence on switching TODO states.
  1613. When non-nil, you first need to check off all check boxes before the TODO
  1614. entry can be switched to DONE.
  1615. This variable needs to be set before org.el is loaded, and you need to
  1616. restart Emacs after a change to make the change effective. The only way
  1617. to change is while Emacs is running is through the customize interface."
  1618. :set (lambda (var val)
  1619. (set var val)
  1620. (if val
  1621. (add-hook 'org-blocker-hook
  1622. 'org-block-todo-from-checkboxes)
  1623. (remove-hook 'org-blocker-hook
  1624. 'org-block-todo-from-checkboxes)))
  1625. :group 'org-todo
  1626. :type 'boolean)
  1627. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1628. "Non-nil means, inserting a TODO heading is treated as state change.
  1629. So when the command \\[org-insert-todo-heading] is used, state change
  1630. logging will apply if appropriate. When nil, the new TODO item will
  1631. be inserted directly, and no logging will take place."
  1632. :group 'org-todo
  1633. :type 'boolean)
  1634. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1635. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1636. This is the default behavior. However, setting this to nil allows a
  1637. convenient way to select a TODO state and bypass any logging associated
  1638. with that."
  1639. :group 'org-todo
  1640. :type 'boolean)
  1641. (defcustom org-todo-state-tags-triggers nil
  1642. "Tag changes that should be triggered by TODO state changes.
  1643. This is a list. Each entry is
  1644. (state-change (tag . flag) .......)
  1645. State-change can be a string with a state, and empty string to indicate the
  1646. state that has no TODO keyword, or it can be one of the symbols `todo'
  1647. or `done', meaning any not-done or done state, respectively."
  1648. :group 'org-todo
  1649. :group 'org-tags
  1650. :type '(repeat
  1651. (cons (choice :tag "When changing to"
  1652. (const :tag "Not-done state" todo)
  1653. (const :tag "Done state" done)
  1654. (string :tag "State"))
  1655. (repeat
  1656. (cons :tag "Tag action"
  1657. (string :tag "Tag")
  1658. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1659. (defcustom org-log-done nil
  1660. "Information to record when a task moves to the DONE state.
  1661. Possible values are:
  1662. nil Don't add anything, just change the keyword
  1663. time Add a time stamp to the task
  1664. note Prompt a closing note and add it with template `org-log-note-headings'
  1665. This option can also be set with on a per-file-basis with
  1666. #+STARTUP: nologdone
  1667. #+STARTUP: logdone
  1668. #+STARTUP: lognotedone
  1669. You can have local logging settings for a subtree by setting the LOGGING
  1670. property to one or more of these keywords."
  1671. :group 'org-todo
  1672. :group 'org-progress
  1673. :type '(choice
  1674. (const :tag "No logging" nil)
  1675. (const :tag "Record CLOSED timestamp" time)
  1676. (const :tag "Record CLOSED timestamp with closing note." note)))
  1677. ;; Normalize old uses of org-log-done.
  1678. (cond
  1679. ((eq org-log-done t) (setq org-log-done 'time))
  1680. ((and (listp org-log-done) (memq 'done org-log-done))
  1681. (setq org-log-done 'note)))
  1682. (defcustom org-log-note-clock-out nil
  1683. "Non-nil means, record a note when clocking out of an item.
  1684. This can also be configured on a per-file basis by adding one of
  1685. the following lines anywhere in the buffer:
  1686. #+STARTUP: lognoteclock-out
  1687. #+STARTUP: nolognoteclock-out"
  1688. :group 'org-todo
  1689. :group 'org-progress
  1690. :type 'boolean)
  1691. (defcustom org-log-done-with-time t
  1692. "Non-nil means, the CLOSED time stamp will contain date and time.
  1693. When nil, only the date will be recorded."
  1694. :group 'org-progress
  1695. :type 'boolean)
  1696. (defcustom org-log-note-headings
  1697. '((done . "CLOSING NOTE %t")
  1698. (state . "State %-12s from %-12S %t")
  1699. (note . "Note taken on %t")
  1700. (clock-out . ""))
  1701. "Headings for notes added to entries.
  1702. The value is an alist, with the car being a symbol indicating the note
  1703. context, and the cdr is the heading to be used. The heading may also be the
  1704. empty string.
  1705. %t in the heading will be replaced by a time stamp.
  1706. %s will be replaced by the new TODO state, in double quotes.
  1707. %S will be replaced by the old TODO state, in double quotes.
  1708. %u will be replaced by the user name.
  1709. %U will be replaced by the full user name."
  1710. :group 'org-todo
  1711. :group 'org-progress
  1712. :type '(list :greedy t
  1713. (cons (const :tag "Heading when closing an item" done) string)
  1714. (cons (const :tag
  1715. "Heading when changing todo state (todo sequence only)"
  1716. state) string)
  1717. (cons (const :tag "Heading when just taking a note" note) string)
  1718. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1719. (unless (assq 'note org-log-note-headings)
  1720. (push '(note . "%t") org-log-note-headings))
  1721. (defcustom org-log-into-drawer nil
  1722. "Non-nil means, insert state change notes and time stamps into a drawer.
  1723. When nil, state changes notes will be inserted after the headline and
  1724. any scheduling and clock lines, but not inside a drawer.
  1725. The value of this variable should be the name of the drawer to use.
  1726. LOGBOOK is proposed at the default drawer for this purpose, you can
  1727. also set this to a string to define the drawer of your choice.
  1728. A value of t is also allowed, representing \"LOGBOOK\".
  1729. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1730. will be ignored.
  1731. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1732. a subtree."
  1733. :group 'org-todo
  1734. :group 'org-progress
  1735. :type '(choice
  1736. (const :tag "Not into a drawer" nil)
  1737. (const :tag "LOGBOOK" t)
  1738. (string :tag "Other")))
  1739. (if (fboundp 'defvaralias)
  1740. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1741. (defun org-log-into-drawer ()
  1742. "Return the value of `org-log-into-drawer', but let properties overrule.
  1743. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1744. used instead of the default value."
  1745. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1746. (cond
  1747. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1748. ((equal p "t") "LOGBOOK")
  1749. (t p))))
  1750. (defcustom org-log-state-notes-insert-after-drawers nil
  1751. "Non-nil means, insert state change notes after any drawers in entry.
  1752. Only the drawers that *immediately* follow the headline and the
  1753. deadline/scheduled line are skipped.
  1754. When nil, insert notes right after the heading and perhaps the line
  1755. with deadline/scheduling if present.
  1756. This variable will have no effect if `org-log-into-drawer' is
  1757. set."
  1758. :group 'org-todo
  1759. :group 'org-progress
  1760. :type 'boolean)
  1761. (defcustom org-log-states-order-reversed t
  1762. "Non-nil means, the latest state change note will be directly after heading.
  1763. When nil, the notes will be orderer according to time."
  1764. :group 'org-todo
  1765. :group 'org-progress
  1766. :type 'boolean)
  1767. (defcustom org-log-repeat 'time
  1768. "Non-nil means, record moving through the DONE state when triggering repeat.
  1769. An auto-repeating task is immediately switched back to TODO when
  1770. marked DONE. If you are not logging state changes (by adding \"@\"
  1771. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1772. record a closing note, there will be no record of the task moving
  1773. through DONE. This variable forces taking a note anyway.
  1774. nil Don't force a record
  1775. time Record a time stamp
  1776. note Record a note
  1777. This option can also be set with on a per-file-basis with
  1778. #+STARTUP: logrepeat
  1779. #+STARTUP: lognoterepeat
  1780. #+STARTUP: nologrepeat
  1781. You can have local logging settings for a subtree by setting the LOGGING
  1782. property to one or more of these keywords."
  1783. :group 'org-todo
  1784. :group 'org-progress
  1785. :type '(choice
  1786. (const :tag "Don't force a record" nil)
  1787. (const :tag "Force recording the DONE state" time)
  1788. (const :tag "Force recording a note with the DONE state" note)))
  1789. (defgroup org-priorities nil
  1790. "Priorities in Org-mode."
  1791. :tag "Org Priorities"
  1792. :group 'org-todo)
  1793. (defcustom org-enable-priority-commands t
  1794. "Non-nil means, priority commands are active.
  1795. When nil, these commands will be disabled, so that you never accidentally
  1796. set a priority."
  1797. :group 'org-priorities
  1798. :type 'boolean)
  1799. (defcustom org-highest-priority ?A
  1800. "The highest priority of TODO items. A character like ?A, ?B etc.
  1801. Must have a smaller ASCII number than `org-lowest-priority'."
  1802. :group 'org-priorities
  1803. :type 'character)
  1804. (defcustom org-lowest-priority ?C
  1805. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1806. Must have a larger ASCII number than `org-highest-priority'."
  1807. :group 'org-priorities
  1808. :type 'character)
  1809. (defcustom org-default-priority ?B
  1810. "The default priority of TODO items.
  1811. This is the priority an item get if no explicit priority is given."
  1812. :group 'org-priorities
  1813. :type 'character)
  1814. (defcustom org-priority-start-cycle-with-default t
  1815. "Non-nil means, start with default priority when starting to cycle.
  1816. When this is nil, the first step in the cycle will be (depending on the
  1817. command used) one higher or lower that the default priority."
  1818. :group 'org-priorities
  1819. :type 'boolean)
  1820. (defgroup org-time nil
  1821. "Options concerning time stamps and deadlines in Org-mode."
  1822. :tag "Org Time"
  1823. :group 'org)
  1824. (defcustom org-insert-labeled-timestamps-at-point nil
  1825. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1826. When nil, these labeled time stamps are forces into the second line of an
  1827. entry, just after the headline. When scheduling from the global TODO list,
  1828. the time stamp will always be forced into the second line."
  1829. :group 'org-time
  1830. :type 'boolean)
  1831. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1832. "Formats for `format-time-string' which are used for time stamps.
  1833. It is not recommended to change this constant.")
  1834. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1835. "Number of minutes to round time stamps to.
  1836. These are two values, the first applies when first creating a time stamp.
  1837. The second applies when changing it with the commands `S-up' and `S-down'.
  1838. When changing the time stamp, this means that it will change in steps
  1839. of N minutes, as given by the second value.
  1840. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1841. numbers should be factors of 60, so for example 5, 10, 15.
  1842. When this is larger than 1, you can still force an exact time-stamp by using
  1843. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1844. and by using a prefix arg to `S-up/down' to specify the exact number
  1845. of minutes to shift."
  1846. :group 'org-time
  1847. :get '(lambda (var) ; Make sure all entries have 5 elements
  1848. (if (integerp (default-value var))
  1849. (list (default-value var) 5)
  1850. (default-value var)))
  1851. :type '(list
  1852. (integer :tag "when inserting times")
  1853. (integer :tag "when modifying times")))
  1854. ;; Normalize old customizations of this variable.
  1855. (when (integerp org-time-stamp-rounding-minutes)
  1856. (setq org-time-stamp-rounding-minutes
  1857. (list org-time-stamp-rounding-minutes
  1858. org-time-stamp-rounding-minutes)))
  1859. (defcustom org-display-custom-times nil
  1860. "Non-nil means, overlay custom formats over all time stamps.
  1861. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1862. To turn this on on a per-file basis, insert anywhere in the file:
  1863. #+STARTUP: customtime"
  1864. :group 'org-time
  1865. :set 'set-default
  1866. :type 'sexp)
  1867. (make-variable-buffer-local 'org-display-custom-times)
  1868. (defcustom org-time-stamp-custom-formats
  1869. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1870. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1871. These are overlayed over the default ISO format if the variable
  1872. `org-display-custom-times' is set. Time like %H:%M should be at the
  1873. end of the second format. The custom formats are also honored by export
  1874. commands, if custom time display is turned on at the time of export."
  1875. :group 'org-time
  1876. :type 'sexp)
  1877. (defun org-time-stamp-format (&optional long inactive)
  1878. "Get the right format for a time string."
  1879. (let ((f (if long (cdr org-time-stamp-formats)
  1880. (car org-time-stamp-formats))))
  1881. (if inactive
  1882. (concat "[" (substring f 1 -1) "]")
  1883. f)))
  1884. (defcustom org-time-clocksum-format "%d:%02d"
  1885. "The format string used when creating CLOCKSUM lines, or when
  1886. org-mode generates a time duration."
  1887. :group 'org-time
  1888. :type 'string)
  1889. (defcustom org-deadline-warning-days 14
  1890. "No. of days before expiration during which a deadline becomes active.
  1891. This variable governs the display in sparse trees and in the agenda.
  1892. When 0 or negative, it means use this number (the absolute value of it)
  1893. even if a deadline has a different individual lead time specified.
  1894. Custom commands can set this variable in the options section."
  1895. :group 'org-time
  1896. :group 'org-agenda-daily/weekly
  1897. :type 'integer)
  1898. (defcustom org-read-date-prefer-future t
  1899. "Non-nil means, assume future for incomplete date input from user.
  1900. This affects the following situations:
  1901. 1. The user gives a day, but no month.
  1902. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1903. read this as the third of *next* month. However, if you enter \"17\",
  1904. it will be considered as *this* month.
  1905. 2. The user gives a month but not a year.
  1906. For example, if it is april and you enter \"feb 2\", this will be read
  1907. as feb 2, *next* year. \"May 5\", however, will be this year.
  1908. Currently this does not work for ISO week specifications.
  1909. When this option is nil, the current month and year will always be used
  1910. as defaults."
  1911. :group 'org-time
  1912. :type 'boolean)
  1913. (defcustom org-read-date-display-live t
  1914. "Non-nil means, display current interpretation of date prompt live.
  1915. This display will be in an overlay, in the minibuffer."
  1916. :group 'org-time
  1917. :type 'boolean)
  1918. (defcustom org-read-date-popup-calendar t
  1919. "Non-nil means, pop up a calendar when prompting for a date.
  1920. In the calendar, the date can be selected with mouse-1. However, the
  1921. minibuffer will also be active, and you can simply enter the date as well.
  1922. When nil, only the minibuffer will be available."
  1923. :group 'org-time
  1924. :type 'boolean)
  1925. (if (fboundp 'defvaralias)
  1926. (defvaralias 'org-popup-calendar-for-date-prompt
  1927. 'org-read-date-popup-calendar))
  1928. (defcustom org-read-date-minibuffer-setup-hook nil
  1929. "Hook to be used to set up keys for the date/time interface.
  1930. Add key definitions to `minibuffer-local-map', which will be a temporary
  1931. copy."
  1932. :group 'org-time
  1933. :type 'hook)
  1934. (defcustom org-extend-today-until 0
  1935. "The hour when your day really ends. Must be an integer.
  1936. This has influence for the following applications:
  1937. - When switching the agenda to \"today\". It it is still earlier than
  1938. the time given here, the day recognized as TODAY is actually yesterday.
  1939. - When a date is read from the user and it is still before the time given
  1940. here, the current date and time will be assumed to be yesterday, 23:59.
  1941. Also, timestamps inserted in remember templates follow this rule.
  1942. IMPORTANT: This is a feature whose implementation is and likely will
  1943. remain incomplete. Really, it is only here because past midnight seems to
  1944. be the favorite working time of John Wiegley :-)"
  1945. :group 'org-time
  1946. :type 'integer)
  1947. (defcustom org-edit-timestamp-down-means-later nil
  1948. "Non-nil means, S-down will increase the time in a time stamp.
  1949. When nil, S-up will increase."
  1950. :group 'org-time
  1951. :type 'boolean)
  1952. (defcustom org-calendar-follow-timestamp-change t
  1953. "Non-nil means, make the calendar window follow timestamp changes.
  1954. When a timestamp is modified and the calendar window is visible, it will be
  1955. moved to the new date."
  1956. :group 'org-time
  1957. :type 'boolean)
  1958. (defgroup org-tags nil
  1959. "Options concerning tags in Org-mode."
  1960. :tag "Org Tags"
  1961. :group 'org)
  1962. (defcustom org-tag-alist nil
  1963. "List of tags allowed in Org-mode files.
  1964. When this list is nil, Org-mode will base TAG input on what is already in the
  1965. buffer.
  1966. The value of this variable is an alist, the car of each entry must be a
  1967. keyword as a string, the cdr may be a character that is used to select
  1968. that tag through the fast-tag-selection interface.
  1969. See the manual for details."
  1970. :group 'org-tags
  1971. :type '(repeat
  1972. (choice
  1973. (cons (string :tag "Tag name")
  1974. (character :tag "Access char"))
  1975. (const :tag "Start radio group" (:startgroup))
  1976. (const :tag "End radio group" (:endgroup))
  1977. (const :tag "New line" (:newline)))))
  1978. (defcustom org-tag-persistent-alist nil
  1979. "List of tags that will always appear in all Org-mode files.
  1980. This is in addition to any in buffer settings or customizations
  1981. of `org-tag-alist'.
  1982. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1983. The value of this variable is an alist, the car of each entry must be a
  1984. keyword as a string, the cdr may be a character that is used to select
  1985. that tag through the fast-tag-selection interface.
  1986. See the manual for details.
  1987. To disable these tags on a per-file basis, insert anywhere in the file:
  1988. #+STARTUP: noptag"
  1989. :group 'org-tags
  1990. :type '(repeat
  1991. (choice
  1992. (cons (string :tag "Tag name")
  1993. (character :tag "Access char"))
  1994. (const :tag "Start radio group" (:startgroup))
  1995. (const :tag "End radio group" (:endgroup))
  1996. (const :tag "New line" (:newline)))))
  1997. (defvar org-file-tags nil
  1998. "List of tags that can be inherited by all entries in the file.
  1999. The tags will be inherited if the variable `org-use-tag-inheritance'
  2000. says they should be.
  2001. This variable is populated from #+TAG lines.")
  2002. (defcustom org-use-fast-tag-selection 'auto
  2003. "Non-nil means, use fast tag selection scheme.
  2004. This is a special interface to select and deselect tags with single keys.
  2005. When nil, fast selection is never used.
  2006. When the symbol `auto', fast selection is used if and only if selection
  2007. characters for tags have been configured, either through the variable
  2008. `org-tag-alist' or through a #+TAGS line in the buffer.
  2009. When t, fast selection is always used and selection keys are assigned
  2010. automatically if necessary."
  2011. :group 'org-tags
  2012. :type '(choice
  2013. (const :tag "Always" t)
  2014. (const :tag "Never" nil)
  2015. (const :tag "When selection characters are configured" 'auto)))
  2016. (defcustom org-fast-tag-selection-single-key nil
  2017. "Non-nil means, fast tag selection exits after first change.
  2018. When nil, you have to press RET to exit it.
  2019. During fast tag selection, you can toggle this flag with `C-c'.
  2020. This variable can also have the value `expert'. In this case, the window
  2021. displaying the tags menu is not even shown, until you press C-c again."
  2022. :group 'org-tags
  2023. :type '(choice
  2024. (const :tag "No" nil)
  2025. (const :tag "Yes" t)
  2026. (const :tag "Expert" expert)))
  2027. (defvar org-fast-tag-selection-include-todo nil
  2028. "Non-nil means, fast tags selection interface will also offer TODO states.
  2029. This is an undocumented feature, you should not rely on it.")
  2030. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2031. "The column to which tags should be indented in a headline.
  2032. If this number is positive, it specifies the column. If it is negative,
  2033. it means that the tags should be flushright to that column. For example,
  2034. -80 works well for a normal 80 character screen."
  2035. :group 'org-tags
  2036. :type 'integer)
  2037. (defcustom org-auto-align-tags t
  2038. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2039. These operations change the length of a headline and therefore shift
  2040. the tags around. With this options turned on, after each such operation
  2041. the tags are again aligned to `org-tags-column'."
  2042. :group 'org-tags
  2043. :type 'boolean)
  2044. (defcustom org-use-tag-inheritance t
  2045. "Non-nil means, tags in levels apply also for sublevels.
  2046. When nil, only the tags directly given in a specific line apply there.
  2047. This may also be a list of tags that should be inherited, or a regexp that
  2048. matches tags that should be inherited. Additional control is possible
  2049. with the variable `org-tags-exclude-from-inheritance' which gives an
  2050. explicit list of tags to be excluded from inheritance., even if the value of
  2051. `org-use-tag-inheritance' would select it for inheritance.
  2052. If this option is t, a match early-on in a tree can lead to a large
  2053. number of matches in the subtree when constructing the agenda or creating
  2054. a sparse tree. If you only want to see the first match in a tree during
  2055. a search, check out the variable `org-tags-match-list-sublevels'."
  2056. :group 'org-tags
  2057. :type '(choice
  2058. (const :tag "Not" nil)
  2059. (const :tag "Always" t)
  2060. (repeat :tag "Specific tags" (string :tag "Tag"))
  2061. (regexp :tag "Tags matched by regexp")))
  2062. (defcustom org-tags-exclude-from-inheritance nil
  2063. "List of tags that should never be inherited.
  2064. This is a way to exclude a few tags from inheritance. For way to do
  2065. the opposite, to actively allow inheritance for selected tags,
  2066. see the variable `org-use-tag-inheritance'."
  2067. :group 'org-tags
  2068. :type '(repeat (string :tag "Tag")))
  2069. (defun org-tag-inherit-p (tag)
  2070. "Check if TAG is one that should be inherited."
  2071. (cond
  2072. ((member tag org-tags-exclude-from-inheritance) nil)
  2073. ((eq org-use-tag-inheritance t) t)
  2074. ((not org-use-tag-inheritance) nil)
  2075. ((stringp org-use-tag-inheritance)
  2076. (string-match org-use-tag-inheritance tag))
  2077. ((listp org-use-tag-inheritance)
  2078. (member tag org-use-tag-inheritance))
  2079. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2080. (defcustom org-tags-match-list-sublevels t
  2081. "Non-nil means list also sublevels of headlines matching a search.
  2082. This variable applies to tags/property searches, and also to stuck
  2083. projects because this search is based on a tags match as well.
  2084. When set to the symbol `indented', sublevels are indented with
  2085. leading dots.
  2086. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2087. the sublevels of a headline matching a tag search often also match
  2088. the same search. Listing all of them can create very long lists.
  2089. Setting this variable to nil causes subtrees of a match to be skipped.
  2090. This variable is semi-obsolete and probably should always be true. It
  2091. is better to limit inheritance to certain tags using the variables
  2092. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2093. :group 'org-tags
  2094. :type '(choice
  2095. (const :tag "No, don't list them" nil)
  2096. (const :tag "Yes, do list them" t)
  2097. (const :tag "List them, indented with leading dots" indented)))
  2098. (defcustom org-tags-sort-function nil
  2099. "When set, tags are sorted using this function as a comparator"
  2100. :group 'org-tags
  2101. :type '(choice
  2102. (const :tag "No sorting" nil)
  2103. (const :tag "Alphabetical" string<)
  2104. (const :tag "Reverse alphabetical" string>)
  2105. (function :tag "Custom function" nil)))
  2106. (defvar org-tags-history nil
  2107. "History of minibuffer reads for tags.")
  2108. (defvar org-last-tags-completion-table nil
  2109. "The last used completion table for tags.")
  2110. (defvar org-after-tags-change-hook nil
  2111. "Hook that is run after the tags in a line have changed.")
  2112. (defgroup org-properties nil
  2113. "Options concerning properties in Org-mode."
  2114. :tag "Org Properties"
  2115. :group 'org)
  2116. (defcustom org-property-format "%-10s %s"
  2117. "How property key/value pairs should be formatted by `indent-line'.
  2118. When `indent-line' hits a property definition, it will format the line
  2119. according to this format, mainly to make sure that the values are
  2120. lined-up with respect to each other."
  2121. :group 'org-properties
  2122. :type 'string)
  2123. (defcustom org-use-property-inheritance nil
  2124. "Non-nil means, properties apply also for sublevels.
  2125. This setting is chiefly used during property searches. Turning it on can
  2126. cause significant overhead when doing a search, which is why it is not
  2127. on by default.
  2128. When nil, only the properties directly given in the current entry count.
  2129. When t, every property is inherited. The value may also be a list of
  2130. properties that should have inheritance, or a regular expression matching
  2131. properties that should be inherited.
  2132. However, note that some special properties use inheritance under special
  2133. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2134. and the properties ending in \"_ALL\" when they are used as descriptor
  2135. for valid values of a property.
  2136. Note for programmers:
  2137. When querying an entry with `org-entry-get', you can control if inheritance
  2138. should be used. By default, `org-entry-get' looks only at the local
  2139. properties. You can request inheritance by setting the inherit argument
  2140. to t (to force inheritance) or to `selective' (to respect the setting
  2141. in this variable)."
  2142. :group 'org-properties
  2143. :type '(choice
  2144. (const :tag "Not" nil)
  2145. (const :tag "Always" t)
  2146. (repeat :tag "Specific properties" (string :tag "Property"))
  2147. (regexp :tag "Properties matched by regexp")))
  2148. (defun org-property-inherit-p (property)
  2149. "Check if PROPERTY is one that should be inherited."
  2150. (cond
  2151. ((eq org-use-property-inheritance t) t)
  2152. ((not org-use-property-inheritance) nil)
  2153. ((stringp org-use-property-inheritance)
  2154. (string-match org-use-property-inheritance property))
  2155. ((listp org-use-property-inheritance)
  2156. (member property org-use-property-inheritance))
  2157. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2158. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2159. "The default column format, if no other format has been defined.
  2160. This variable can be set on the per-file basis by inserting a line
  2161. #+COLUMNS: %25ITEM ....."
  2162. :group 'org-properties
  2163. :type 'string)
  2164. (defcustom org-columns-ellipses ".."
  2165. "The ellipses to be used when a field in column view is truncated.
  2166. When this is the empty string, as many characters as possible are shown,
  2167. but then there will be no visual indication that the field has been truncated.
  2168. When this is a string of length N, the last N characters of a truncated
  2169. field are replaced by this string. If the column is narrower than the
  2170. ellipses string, only part of the ellipses string will be shown."
  2171. :group 'org-properties
  2172. :type 'string)
  2173. (defcustom org-columns-modify-value-for-display-function nil
  2174. "Function that modifies values for display in column view.
  2175. For example, it can be used to cut out a certain part from a time stamp.
  2176. The function must take 2 arguments:
  2177. column-title The title of the column (*not* the property name)
  2178. value The value that should be modified.
  2179. The function should return the value that should be displayed,
  2180. or nil if the normal value should be used."
  2181. :group 'org-properties
  2182. :type 'function)
  2183. (defcustom org-effort-property "Effort"
  2184. "The property that is being used to keep track of effort estimates.
  2185. Effort estimates given in this property need to have the format H:MM."
  2186. :group 'org-properties
  2187. :group 'org-progress
  2188. :type '(string :tag "Property"))
  2189. (defconst org-global-properties-fixed
  2190. '(("VISIBILITY_ALL" . "folded children content all")
  2191. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2192. "List of property/value pairs that can be inherited by any entry.
  2193. These are fixed values, for the preset properties. The user variable
  2194. that can be used to add to this list is `org-global-properties'.
  2195. The entries in this list are cons cells where the car is a property
  2196. name and cdr is a string with the value. If the value represents
  2197. multiple items like an \"_ALL\" property, separate the items by
  2198. spaces.")
  2199. (defcustom org-global-properties nil
  2200. "List of property/value pairs that can be inherited by any entry.
  2201. This list will be combined with the constant `org-global-properties-fixed'.
  2202. The entries in this list are cons cells where the car is a property
  2203. name and cdr is a string with the value.
  2204. You can set buffer-local values for the same purpose in the variable
  2205. `org-file-properties' this by adding lines like
  2206. #+PROPERTY: NAME VALUE"
  2207. :group 'org-properties
  2208. :type '(repeat
  2209. (cons (string :tag "Property")
  2210. (string :tag "Value"))))
  2211. (defvar org-file-properties nil
  2212. "List of property/value pairs that can be inherited by any entry.
  2213. Valid for the current buffer.
  2214. This variable is populated from #+PROPERTY lines.")
  2215. (make-variable-buffer-local 'org-file-properties)
  2216. (defgroup org-agenda nil
  2217. "Options concerning agenda views in Org-mode."
  2218. :tag "Org Agenda"
  2219. :group 'org)
  2220. (defvar org-category nil
  2221. "Variable used by org files to set a category for agenda display.
  2222. Such files should use a file variable to set it, for example
  2223. # -*- mode: org; org-category: \"ELisp\"
  2224. or contain a special line
  2225. #+CATEGORY: ELisp
  2226. If the file does not specify a category, then file's base name
  2227. is used instead.")
  2228. (make-variable-buffer-local 'org-category)
  2229. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2230. (defcustom org-agenda-files nil
  2231. "The files to be used for agenda display.
  2232. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2233. \\[org-remove-file]. You can also use customize to edit the list.
  2234. If an entry is a directory, all files in that directory that are matched by
  2235. `org-agenda-file-regexp' will be part of the file list.
  2236. If the value of the variable is not a list but a single file name, then
  2237. the list of agenda files is actually stored and maintained in that file, one
  2238. agenda file per line."
  2239. :group 'org-agenda
  2240. :type '(choice
  2241. (repeat :tag "List of files and directories" file)
  2242. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2243. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2244. "Regular expression to match files for `org-agenda-files'.
  2245. If any element in the list in that variable contains a directory instead
  2246. of a normal file, all files in that directory that are matched by this
  2247. regular expression will be included."
  2248. :group 'org-agenda
  2249. :type 'regexp)
  2250. (defcustom org-agenda-text-search-extra-files nil
  2251. "List of extra files to be searched by text search commands.
  2252. These files will be search in addition to the agenda files by the
  2253. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2254. Note that these files will only be searched for text search commands,
  2255. not for the other agenda views like todo lists, tag searches or the weekly
  2256. agenda. This variable is intended to list notes and possibly archive files
  2257. that should also be searched by these two commands.
  2258. In fact, if the first element in the list is the symbol `agenda-archives',
  2259. than all archive files of all agenda files will be added to the search
  2260. scope."
  2261. :group 'org-agenda
  2262. :type '(set :greedy t
  2263. (const :tag "Agenda Archives" agenda-archives)
  2264. (repeat :inline t (file))))
  2265. (if (fboundp 'defvaralias)
  2266. (defvaralias 'org-agenda-multi-occur-extra-files
  2267. 'org-agenda-text-search-extra-files))
  2268. (defcustom org-agenda-skip-unavailable-files nil
  2269. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2270. A nil value means to remove them, after a query, from the list."
  2271. :group 'org-agenda
  2272. :type 'boolean)
  2273. (defcustom org-calendar-to-agenda-key [?c]
  2274. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2275. The command `org-calendar-goto-agenda' will be bound to this key. The
  2276. default is the character `c' because then `c' can be used to switch back and
  2277. forth between agenda and calendar."
  2278. :group 'org-agenda
  2279. :type 'sexp)
  2280. (defcustom org-calendar-agenda-action-key [?k]
  2281. "The key to be installed in `calendar-mode-map' for agenda-action.
  2282. The command `org-agenda-action' will be bound to this key. The
  2283. default is the character `k' because we use the same key in the agenda."
  2284. :group 'org-agenda
  2285. :type 'sexp)
  2286. (eval-after-load "calendar"
  2287. '(progn
  2288. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2289. 'org-calendar-goto-agenda)
  2290. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2291. 'org-agenda-action)))
  2292. (defgroup org-latex nil
  2293. "Options for embedding LaTeX code into Org-mode."
  2294. :tag "Org LaTeX"
  2295. :group 'org)
  2296. (defcustom org-format-latex-options
  2297. '(:foreground default :background default :scale 1.0
  2298. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2299. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2300. "Options for creating images from LaTeX fragments.
  2301. This is a property list with the following properties:
  2302. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2303. `default' means use the foreground of the default face.
  2304. :background the background color, or \"Transparent\".
  2305. `default' means use the background of the default face.
  2306. :scale a scaling factor for the size of the images.
  2307. :html-foreground, :html-background, :html-scale
  2308. the same numbers for HTML export.
  2309. :matchers a list indicating which matchers should be used to
  2310. find LaTeX fragments. Valid members of this list are:
  2311. \"begin\" find environments
  2312. \"$1\" find single characters surrounded by $.$
  2313. \"$\" find math expressions surrounded by $...$
  2314. \"$$\" find math expressions surrounded by $$....$$
  2315. \"\\(\" find math expressions surrounded by \\(...\\)
  2316. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2317. :group 'org-latex
  2318. :type 'plist)
  2319. (defcustom org-format-latex-header "\\documentclass{article}
  2320. \\usepackage{fullpage} % do not remove
  2321. \\usepackage{amssymb}
  2322. \\usepackage[usenames]{color}
  2323. \\usepackage{amsmath}
  2324. \\usepackage{latexsym}
  2325. \\usepackage[mathscr]{eucal}
  2326. \\pagestyle{empty} % do not remove"
  2327. "The document header used for processing LaTeX fragments."
  2328. :group 'org-latex
  2329. :type 'string)
  2330. (defgroup org-font-lock nil
  2331. "Font-lock settings for highlighting in Org-mode."
  2332. :tag "Org Font Lock"
  2333. :group 'org)
  2334. (defcustom org-level-color-stars-only nil
  2335. "Non-nil means fontify only the stars in each headline.
  2336. When nil, the entire headline is fontified.
  2337. Changing it requires restart of `font-lock-mode' to become effective
  2338. also in regions already fontified."
  2339. :group 'org-font-lock
  2340. :type 'boolean)
  2341. (defcustom org-hide-leading-stars nil
  2342. "Non-nil means, hide the first N-1 stars in a headline.
  2343. This works by using the face `org-hide' for these stars. This
  2344. face is white for a light background, and black for a dark
  2345. background. You may have to customize the face `org-hide' to
  2346. make this work.
  2347. Changing it requires restart of `font-lock-mode' to become effective
  2348. also in regions already fontified.
  2349. You may also set this on a per-file basis by adding one of the following
  2350. lines to the buffer:
  2351. #+STARTUP: hidestars
  2352. #+STARTUP: showstars"
  2353. :group 'org-font-lock
  2354. :type 'boolean)
  2355. (defcustom org-fontify-done-headline nil
  2356. "Non-nil means, change the face of a headline if it is marked DONE.
  2357. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2358. When this is non-nil, the headline after the keyword is set to the
  2359. `org-headline-done' as an additional indication."
  2360. :group 'org-font-lock
  2361. :type 'boolean)
  2362. (defcustom org-fontify-emphasized-text t
  2363. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2364. Changing this variable requires a restart of Emacs to take effect."
  2365. :group 'org-font-lock
  2366. :type 'boolean)
  2367. (defcustom org-fontify-whole-heading-line nil
  2368. "Non-nil means fontify the whole line for headings.
  2369. This is useful when setting a background color for the
  2370. org-leve-* faces."
  2371. :group 'org-font-lock
  2372. :type 'boolean)
  2373. (defcustom org-highlight-latex-fragments-and-specials nil
  2374. "Non-nil means, fontify what is treated specially by the exporters."
  2375. :group 'org-font-lock
  2376. :type 'boolean)
  2377. (defcustom org-hide-emphasis-markers nil
  2378. "Non-nil mean font-lock should hide the emphasis marker characters."
  2379. :group 'org-font-lock
  2380. :type 'boolean)
  2381. (defvar org-emph-re nil
  2382. "Regular expression for matching emphasis.")
  2383. (defvar org-verbatim-re nil
  2384. "Regular expression for matching verbatim text.")
  2385. (defvar org-emphasis-regexp-components) ; defined just below
  2386. (defvar org-emphasis-alist) ; defined just below
  2387. (defun org-set-emph-re (var val)
  2388. "Set variable and compute the emphasis regular expression."
  2389. (set var val)
  2390. (when (and (boundp 'org-emphasis-alist)
  2391. (boundp 'org-emphasis-regexp-components)
  2392. org-emphasis-alist org-emphasis-regexp-components)
  2393. (let* ((e org-emphasis-regexp-components)
  2394. (pre (car e))
  2395. (post (nth 1 e))
  2396. (border (nth 2 e))
  2397. (body (nth 3 e))
  2398. (nl (nth 4 e))
  2399. (body1 (concat body "*?"))
  2400. (markers (mapconcat 'car org-emphasis-alist ""))
  2401. (vmarkers (mapconcat
  2402. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2403. org-emphasis-alist "")))
  2404. ;; make sure special characters appear at the right position in the class
  2405. (if (string-match "\\^" markers)
  2406. (setq markers (concat (replace-match "" t t markers) "^")))
  2407. (if (string-match "-" markers)
  2408. (setq markers (concat (replace-match "" t t markers) "-")))
  2409. (if (string-match "\\^" vmarkers)
  2410. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2411. (if (string-match "-" vmarkers)
  2412. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2413. (if (> nl 0)
  2414. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2415. (int-to-string nl) "\\}")))
  2416. ;; Make the regexp
  2417. (setq org-emph-re
  2418. (concat "\\([" pre "]\\|^\\)"
  2419. "\\("
  2420. "\\([" markers "]\\)"
  2421. "\\("
  2422. "[^" border "]\\|"
  2423. "[^" border "]"
  2424. body1
  2425. "[^" border "]"
  2426. "\\)"
  2427. "\\3\\)"
  2428. "\\([" post "]\\|$\\)"))
  2429. (setq org-verbatim-re
  2430. (concat "\\([" pre "]\\|^\\)"
  2431. "\\("
  2432. "\\([" vmarkers "]\\)"
  2433. "\\("
  2434. "[^" border "]\\|"
  2435. "[^" border "]"
  2436. body1
  2437. "[^" border "]"
  2438. "\\)"
  2439. "\\3\\)"
  2440. "\\([" post "]\\|$\\)")))))
  2441. (defcustom org-emphasis-regexp-components
  2442. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2443. "Components used to build the regular expression for emphasis.
  2444. This is a list with 6 entries. Terminology: In an emphasis string
  2445. like \" *strong word* \", we call the initial space PREMATCH, the final
  2446. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2447. and \"trong wor\" is the body. The different components in this variable
  2448. specify what is allowed/forbidden in each part:
  2449. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2450. post Chars allowed as postmatch. End of line will be allowed too.
  2451. border The chars *forbidden* as border characters.
  2452. body-regexp A regexp like \".\" to match a body character. Don't use
  2453. non-shy groups here, and don't allow newline here.
  2454. newline The maximum number of newlines allowed in an emphasis exp.
  2455. Use customize to modify this, or restart Emacs after changing it."
  2456. :group 'org-font-lock
  2457. :set 'org-set-emph-re
  2458. :type '(list
  2459. (sexp :tag "Allowed chars in pre ")
  2460. (sexp :tag "Allowed chars in post ")
  2461. (sexp :tag "Forbidden chars in border ")
  2462. (sexp :tag "Regexp for body ")
  2463. (integer :tag "number of newlines allowed")
  2464. (option (boolean :tag "Please ignore this button"))))
  2465. (defcustom org-emphasis-alist
  2466. `(("*" bold "<b>" "</b>")
  2467. ("/" italic "<i>" "</i>")
  2468. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2469. ("=" org-code "<code>" "</code>" verbatim)
  2470. ("~" org-verbatim "<code>" "</code>" verbatim)
  2471. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2472. "<del>" "</del>")
  2473. )
  2474. "Special syntax for emphasized text.
  2475. Text starting and ending with a special character will be emphasized, for
  2476. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2477. characters, the face to be used by font-lock for highlighting in Org-mode
  2478. Emacs buffers, and the HTML tags to be used for this.
  2479. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2480. Use customize to modify this, or restart Emacs after changing it."
  2481. :group 'org-font-lock
  2482. :set 'org-set-emph-re
  2483. :type '(repeat
  2484. (list
  2485. (string :tag "Marker character")
  2486. (choice
  2487. (face :tag "Font-lock-face")
  2488. (plist :tag "Face property list"))
  2489. (string :tag "HTML start tag")
  2490. (string :tag "HTML end tag")
  2491. (option (const verbatim)))))
  2492. (defvar org-protecting-blocks
  2493. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2494. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2495. This is needed for font-lock setup.")
  2496. ;;; Miscellaneous options
  2497. (defgroup org-completion nil
  2498. "Completion in Org-mode."
  2499. :tag "Org Completion"
  2500. :group 'org)
  2501. (defcustom org-completion-use-ido nil
  2502. "Non-nil means, use ido completion wherever possible.
  2503. Note that `ido-mode' must be active for this variable to be relevant.
  2504. If you decide to turn this variable on, you might well want to turn off
  2505. `org-outline-path-complete-in-steps'.
  2506. See also `org-completion-use-iswitchb'."
  2507. :group 'org-completion
  2508. :type 'boolean)
  2509. (defcustom org-completion-use-iswitchb nil
  2510. "Non-nil means, use iswitchb completion wherever possible.
  2511. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2512. If you decide to turn this variable on, you might well want to turn off
  2513. `org-outline-path-complete-in-steps'.
  2514. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2515. :group 'org-completion
  2516. :type 'boolean)
  2517. (defcustom org-completion-fallback-command 'hippie-expand
  2518. "The expansion command called by \\[org-complete] in normal context.
  2519. Normal means, no org-mode-specific context."
  2520. :group 'org-completion
  2521. :type 'function)
  2522. ;;; Functions and variables from ther packages
  2523. ;; Declared here to avoid compiler warnings
  2524. ;; XEmacs only
  2525. (defvar outline-mode-menu-heading)
  2526. (defvar outline-mode-menu-show)
  2527. (defvar outline-mode-menu-hide)
  2528. (defvar zmacs-regions) ; XEmacs regions
  2529. ;; Emacs only
  2530. (defvar mark-active)
  2531. ;; Various packages
  2532. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2533. (declare-function calendar-forward-day "cal-move" (arg))
  2534. (declare-function calendar-goto-date "cal-move" (date))
  2535. (declare-function calendar-goto-today "cal-move" ())
  2536. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2537. (defvar calc-embedded-close-formula)
  2538. (defvar calc-embedded-open-formula)
  2539. (declare-function cdlatex-tab "ext:cdlatex" ())
  2540. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2541. (defvar font-lock-unfontify-region-function)
  2542. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2543. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2544. (defvar iswitchb-temp-buflist)
  2545. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2546. (defvar org-agenda-tags-todo-honor-ignore-options)
  2547. (declare-function org-agenda-skip "org-agenda" ())
  2548. (declare-function org-format-agenda-item "org-agenda"
  2549. (extra txt &optional category tags dotime noprefix remove-re))
  2550. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2551. (declare-function org-agenda-change-all-lines "org-agenda"
  2552. (newhead hdmarker &optional fixface just-this))
  2553. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2554. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2555. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2556. (beg end))
  2557. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2558. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2559. "org-agenda" (&optional end))
  2560. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2561. (declare-function org-indent-mode "org-indent" (arg))
  2562. (declare-function parse-time-string "parse-time" (string))
  2563. (declare-function remember "remember" (&optional initial))
  2564. (declare-function remember-buffer-desc "remember" ())
  2565. (declare-function remember-finalize "remember" ())
  2566. (defvar remember-save-after-remembering)
  2567. (defvar remember-data-file)
  2568. (defvar remember-register)
  2569. (defvar remember-buffer)
  2570. (defvar remember-handler-functions)
  2571. (defvar remember-annotation-functions)
  2572. (defvar texmathp-why)
  2573. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2574. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2575. (defvar w3m-current-url)
  2576. (defvar w3m-current-title)
  2577. (defvar org-latex-regexps)
  2578. ;;; Autoload and prepare some org modules
  2579. ;; Some table stuff that needs to be defined here, because it is used
  2580. ;; by the functions setting up org-mode or checking for table context.
  2581. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2582. "Detects an org-type or table-type table.")
  2583. (defconst org-table-line-regexp "^[ \t]*|"
  2584. "Detects an org-type table line.")
  2585. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2586. "Detects an org-type table line.")
  2587. (defconst org-table-hline-regexp "^[ \t]*|-"
  2588. "Detects an org-type table hline.")
  2589. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2590. "Detects a table-type table hline.")
  2591. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2592. "Searching from within a table (any type) this finds the first line
  2593. outside the table.")
  2594. ;; Autoload the functions in org-table.el that are needed by functions here.
  2595. (eval-and-compile
  2596. (org-autoload "org-table"
  2597. '(org-table-align org-table-begin org-table-blank-field
  2598. org-table-convert org-table-convert-region org-table-copy-down
  2599. org-table-copy-region org-table-create
  2600. org-table-create-or-convert-from-region
  2601. org-table-create-with-table.el org-table-current-dline
  2602. org-table-cut-region org-table-delete-column org-table-edit-field
  2603. org-table-edit-formulas org-table-end org-table-eval-formula
  2604. org-table-export org-table-field-info
  2605. org-table-get-stored-formulas org-table-goto-column
  2606. org-table-hline-and-move org-table-import org-table-insert-column
  2607. org-table-insert-hline org-table-insert-row org-table-iterate
  2608. org-table-justify-field-maybe org-table-kill-row
  2609. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2610. org-table-move-column org-table-move-column-left
  2611. org-table-move-column-right org-table-move-row
  2612. org-table-move-row-down org-table-move-row-up
  2613. org-table-next-field org-table-next-row org-table-paste-rectangle
  2614. org-table-previous-field org-table-recalculate
  2615. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2616. org-table-toggle-coordinate-overlays
  2617. org-table-toggle-formula-debugger org-table-wrap-region
  2618. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2619. (defun org-at-table-p (&optional table-type)
  2620. "Return t if the cursor is inside an org-type table.
  2621. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2622. (if org-enable-table-editor
  2623. (save-excursion
  2624. (beginning-of-line 1)
  2625. (looking-at (if table-type org-table-any-line-regexp
  2626. org-table-line-regexp)))
  2627. nil))
  2628. (defsubst org-table-p () (org-at-table-p))
  2629. (defun org-at-table.el-p ()
  2630. "Return t if and only if we are at a table.el table."
  2631. (and (org-at-table-p 'any)
  2632. (save-excursion
  2633. (goto-char (org-table-begin 'any))
  2634. (looking-at org-table1-hline-regexp))))
  2635. (defun org-table-recognize-table.el ()
  2636. "If there is a table.el table nearby, recognize it and move into it."
  2637. (if org-table-tab-recognizes-table.el
  2638. (if (org-at-table.el-p)
  2639. (progn
  2640. (beginning-of-line 1)
  2641. (if (looking-at org-table-dataline-regexp)
  2642. nil
  2643. (if (looking-at org-table1-hline-regexp)
  2644. (progn
  2645. (beginning-of-line 2)
  2646. (if (looking-at org-table-any-border-regexp)
  2647. (beginning-of-line -1)))))
  2648. (if (re-search-forward "|" (org-table-end t) t)
  2649. (progn
  2650. (require 'table)
  2651. (if (table--at-cell-p (point))
  2652. t
  2653. (message "recognizing table.el table...")
  2654. (table-recognize-table)
  2655. (message "recognizing table.el table...done")))
  2656. (error "This should not happen..."))
  2657. t)
  2658. nil)
  2659. nil))
  2660. (defun org-at-table-hline-p ()
  2661. "Return t if the cursor is inside a hline in a table."
  2662. (if org-enable-table-editor
  2663. (save-excursion
  2664. (beginning-of-line 1)
  2665. (looking-at org-table-hline-regexp))
  2666. nil))
  2667. (defvar org-table-clean-did-remove-column nil)
  2668. (defun org-table-map-tables (function)
  2669. "Apply FUNCTION to the start of all tables in the buffer."
  2670. (save-excursion
  2671. (save-restriction
  2672. (widen)
  2673. (goto-char (point-min))
  2674. (while (re-search-forward org-table-any-line-regexp nil t)
  2675. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2676. (beginning-of-line 1)
  2677. (when (looking-at org-table-line-regexp)
  2678. (save-excursion (funcall function))
  2679. (or (looking-at org-table-line-regexp)
  2680. (forward-char 1)))
  2681. (re-search-forward org-table-any-border-regexp nil 1))))
  2682. (message "Mapping tables: done"))
  2683. ;; Declare and autoload functions from org-exp.el & Co
  2684. (declare-function org-default-export-plist "org-exp")
  2685. (declare-function org-infile-export-plist "org-exp")
  2686. (declare-function org-get-current-options "org-exp")
  2687. (eval-and-compile
  2688. (org-autoload "org-exp"
  2689. '(org-export org-export-visible
  2690. org-insert-export-options-template
  2691. org-table-clean-before-export))
  2692. (org-autoload "org-ascii"
  2693. '(org-export-as-ascii org-export-ascii-preprocess
  2694. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2695. org-export-region-as-ascii))
  2696. (org-autoload "org-html"
  2697. '(org-export-as-html-and-open
  2698. org-export-as-html-batch org-export-as-html-to-buffer
  2699. org-replace-region-by-html org-export-region-as-html
  2700. org-export-as-html))
  2701. (org-autoload "org-icalendar"
  2702. '(org-export-icalendar-this-file
  2703. org-export-icalendar-all-agenda-files
  2704. org-export-icalendar-combine-agenda-files))
  2705. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2706. ;; Declare and autoload functions from org-agenda.el
  2707. (eval-and-compile
  2708. (org-autoload "org-agenda"
  2709. '(org-agenda org-agenda-list org-search-view
  2710. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2711. org-diary org-agenda-to-appt
  2712. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2713. ;; Autoload org-remember
  2714. (eval-and-compile
  2715. (org-autoload "org-remember"
  2716. '(org-remember-insinuate org-remember-annotation
  2717. org-remember-apply-template org-remember org-remember-handler)))
  2718. ;; Autoload org-clock.el
  2719. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2720. (beg end))
  2721. (declare-function org-clock-update-mode-line "org-clock" ())
  2722. (defvar org-clock-start-time)
  2723. (defvar org-clock-marker (make-marker)
  2724. "Marker recording the last clock-in.")
  2725. (defvar org-clock-hd-marker (make-marker)
  2726. "Marker recording the last clock-in, but the headline position.")
  2727. (defun org-clock-is-active ()
  2728. "Return non-nil if clock is currently running.
  2729. The return value is actually the clock marker."
  2730. (marker-buffer org-clock-marker))
  2731. (eval-and-compile
  2732. (org-autoload
  2733. "org-clock"
  2734. '(org-clock-in org-clock-out org-clock-cancel
  2735. org-clock-goto org-clock-sum org-clock-display
  2736. org-clock-remove-overlays org-clock-report
  2737. org-clocktable-shift org-dblock-write:clocktable
  2738. org-get-clocktable)))
  2739. (defun org-clock-update-time-maybe ()
  2740. "If this is a CLOCK line, update it and return t.
  2741. Otherwise, return nil."
  2742. (interactive)
  2743. (save-excursion
  2744. (beginning-of-line 1)
  2745. (skip-chars-forward " \t")
  2746. (when (looking-at org-clock-string)
  2747. (let ((re (concat "[ \t]*" org-clock-string
  2748. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2749. "\\([ \t]*=>.*\\)?\\)?"))
  2750. ts te h m s neg)
  2751. (cond
  2752. ((not (looking-at re))
  2753. nil)
  2754. ((not (match-end 2))
  2755. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2756. (> org-clock-marker (point))
  2757. (<= org-clock-marker (point-at-eol)))
  2758. ;; The clock is running here
  2759. (setq org-clock-start-time
  2760. (apply 'encode-time
  2761. (org-parse-time-string (match-string 1))))
  2762. (org-clock-update-mode-line)))
  2763. (t
  2764. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2765. (end-of-line 1)
  2766. (setq ts (match-string 1)
  2767. te (match-string 3))
  2768. (setq s (- (org-float-time
  2769. (apply 'encode-time (org-parse-time-string te)))
  2770. (org-float-time
  2771. (apply 'encode-time (org-parse-time-string ts))))
  2772. neg (< s 0)
  2773. s (abs s)
  2774. h (floor (/ s 3600))
  2775. s (- s (* 3600 h))
  2776. m (floor (/ s 60))
  2777. s (- s (* 60 s)))
  2778. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2779. t))))))
  2780. (defun org-check-running-clock ()
  2781. "Check if the current buffer contains the running clock.
  2782. If yes, offer to stop it and to save the buffer with the changes."
  2783. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2784. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2785. (buffer-name))))
  2786. (org-clock-out)
  2787. (when (y-or-n-p "Save changed buffer?")
  2788. (save-buffer))))
  2789. (defun org-clocktable-try-shift (dir n)
  2790. "Check if this line starts a clock table, if yes, shift the time block."
  2791. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2792. (org-clocktable-shift dir n)))
  2793. ;; Autoload org-timer.el
  2794. (eval-and-compile
  2795. (org-autoload
  2796. "org-timer"
  2797. '(org-timer-start org-timer org-timer-item
  2798. org-timer-change-times-in-region
  2799. org-timer-set-timer
  2800. org-timer-reset-timers
  2801. org-timer-show-remaining-time)))
  2802. ;; Autoload org-feed.el
  2803. (eval-and-compile
  2804. (org-autoload
  2805. "org-feed"
  2806. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2807. ;; Autoload org-indent.el
  2808. (eval-and-compile
  2809. (org-autoload
  2810. "org-indent"
  2811. '(org-indent-mode)))
  2812. ;; Autoload archiving code
  2813. ;; The stuff that is needed for cycling and tags has to be defined here.
  2814. (defgroup org-archive nil
  2815. "Options concerning archiving in Org-mode."
  2816. :tag "Org Archive"
  2817. :group 'org-structure)
  2818. (defcustom org-archive-location "%s_archive::"
  2819. "The location where subtrees should be archived.
  2820. The value of this variable is a string, consisting of two parts,
  2821. separated by a double-colon. The first part is a filename and
  2822. the second part is a headline.
  2823. When the filename is omitted, archiving happens in the same file.
  2824. %s in the filename will be replaced by the current file
  2825. name (without the directory part). Archiving to a different file
  2826. is useful to keep archived entries from contributing to the
  2827. Org-mode Agenda.
  2828. The archived entries will be filed as subtrees of the specified
  2829. headline. When the headline is omitted, the subtrees are simply
  2830. filed away at the end of the file, as top-level entries. Also in
  2831. the heading you can use %s to represent the file name, this can be
  2832. useful when using the same archive for a number of different files.
  2833. Here are a few examples:
  2834. \"%s_archive::\"
  2835. If the current file is Projects.org, archive in file
  2836. Projects.org_archive, as top-level trees. This is the default.
  2837. \"::* Archived Tasks\"
  2838. Archive in the current file, under the top-level headline
  2839. \"* Archived Tasks\".
  2840. \"~/org/archive.org::\"
  2841. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2842. \"~/org/archive.org::From %s\"
  2843. Archive in file ~/org/archive.org (absolute path), und headlines
  2844. \"From FILENAME\" where file name is the current file name.
  2845. \"basement::** Finished Tasks\"
  2846. Archive in file ./basement (relative path), as level 3 trees
  2847. below the level 2 heading \"** Finished Tasks\".
  2848. You may set this option on a per-file basis by adding to the buffer a
  2849. line like
  2850. #+ARCHIVE: basement::** Finished Tasks
  2851. You may also define it locally for a subtree by setting an ARCHIVE property
  2852. in the entry. If such a property is found in an entry, or anywhere up
  2853. the hierarchy, it will be used."
  2854. :group 'org-archive
  2855. :type 'string)
  2856. (defcustom org-archive-tag "ARCHIVE"
  2857. "The tag that marks a subtree as archived.
  2858. An archived subtree does not open during visibility cycling, and does
  2859. not contribute to the agenda listings.
  2860. After changing this, font-lock must be restarted in the relevant buffers to
  2861. get the proper fontification."
  2862. :group 'org-archive
  2863. :group 'org-keywords
  2864. :type 'string)
  2865. (defcustom org-agenda-skip-archived-trees t
  2866. "Non-nil means, the agenda will skip any items located in archived trees.
  2867. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2868. variable is no longer recommended, you should leave it at the value t.
  2869. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2870. :group 'org-archive
  2871. :group 'org-agenda-skip
  2872. :type 'boolean)
  2873. (defcustom org-columns-skip-arrchived-trees t
  2874. "Non-nil means, irgnore archived trees when creating column view."
  2875. :group 'org-archive
  2876. :group 'org-properties
  2877. :type 'boolean)
  2878. (defcustom org-cycle-open-archived-trees nil
  2879. "Non-nil means, `org-cycle' will open archived trees.
  2880. An archived tree is a tree marked with the tag ARCHIVE.
  2881. When nil, archived trees will stay folded. You can still open them with
  2882. normal outline commands like `show-all', but not with the cycling commands."
  2883. :group 'org-archive
  2884. :group 'org-cycle
  2885. :type 'boolean)
  2886. (defcustom org-sparse-tree-open-archived-trees nil
  2887. "Non-nil means sparse tree construction shows matches in archived trees.
  2888. When nil, matches in these trees are highlighted, but the trees are kept in
  2889. collapsed state."
  2890. :group 'org-archive
  2891. :group 'org-sparse-trees
  2892. :type 'boolean)
  2893. (defun org-cycle-hide-archived-subtrees (state)
  2894. "Re-hide all archived subtrees after a visibility state change."
  2895. (when (and (not org-cycle-open-archived-trees)
  2896. (not (memq state '(overview folded))))
  2897. (save-excursion
  2898. (let* ((globalp (memq state '(contents all)))
  2899. (beg (if globalp (point-min) (point)))
  2900. (end (if globalp (point-max) (org-end-of-subtree t))))
  2901. (org-hide-archived-subtrees beg end)
  2902. (goto-char beg)
  2903. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2904. (message "%s" (substitute-command-keys
  2905. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2906. (defun org-force-cycle-archived ()
  2907. "Cycle subtree even if it is archived."
  2908. (interactive)
  2909. (setq this-command 'org-cycle)
  2910. (let ((org-cycle-open-archived-trees t))
  2911. (call-interactively 'org-cycle)))
  2912. (defun org-hide-archived-subtrees (beg end)
  2913. "Re-hide all archived subtrees after a visibility state change."
  2914. (save-excursion
  2915. (let* ((re (concat ":" org-archive-tag ":")))
  2916. (goto-char beg)
  2917. (while (re-search-forward re end t)
  2918. (and (org-on-heading-p) (hide-subtree))
  2919. (org-end-of-subtree t)))))
  2920. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2921. (eval-and-compile
  2922. (org-autoload "org-archive"
  2923. '(org-add-archive-files org-archive-subtree
  2924. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2925. ;; Autoload Column View Code
  2926. (declare-function org-columns-number-to-string "org-colview")
  2927. (declare-function org-columns-get-format-and-top-level "org-colview")
  2928. (declare-function org-columns-compute "org-colview")
  2929. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2930. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2931. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2932. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2933. ;; Autoload ID code
  2934. (declare-function org-id-store-link "org-id")
  2935. (declare-function org-id-locations-load "org-id")
  2936. (declare-function org-id-locations-save "org-id")
  2937. (defvar org-id-track-globally)
  2938. (org-autoload "org-id"
  2939. '(org-id-get-create org-id-new org-id-copy org-id-get
  2940. org-id-get-with-outline-path-completion
  2941. org-id-get-with-outline-drilling
  2942. org-id-goto org-id-find org-id-store-link))
  2943. ;; Autoload Plotting Code
  2944. (org-autoload "org-plot"
  2945. '(org-plot/gnuplot))
  2946. ;;; Variables for pre-computed regular expressions, all buffer local
  2947. (defvar org-drawer-regexp nil
  2948. "Matches first line of a hidden block.")
  2949. (make-variable-buffer-local 'org-drawer-regexp)
  2950. (defvar org-todo-regexp nil
  2951. "Matches any of the TODO state keywords.")
  2952. (make-variable-buffer-local 'org-todo-regexp)
  2953. (defvar org-not-done-regexp nil
  2954. "Matches any of the TODO state keywords except the last one.")
  2955. (make-variable-buffer-local 'org-not-done-regexp)
  2956. (defvar org-not-done-heading-regexp nil
  2957. "Matches a TODO headline that is not done.")
  2958. (make-variable-buffer-local 'org-not-done-regexp)
  2959. (defvar org-todo-line-regexp nil
  2960. "Matches a headline and puts TODO state into group 2 if present.")
  2961. (make-variable-buffer-local 'org-todo-line-regexp)
  2962. (defvar org-complex-heading-regexp nil
  2963. "Matches a headline and puts everything into groups:
  2964. group 1: the stars
  2965. group 2: The todo keyword, maybe
  2966. group 3: Priority cookie
  2967. group 4: True headline
  2968. group 5: Tags")
  2969. (make-variable-buffer-local 'org-complex-heading-regexp)
  2970. (defvar org-todo-line-tags-regexp nil
  2971. "Matches a headline and puts TODO state into group 2 if present.
  2972. Also put tags into group 4 if tags are present.")
  2973. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2974. (defvar org-nl-done-regexp nil
  2975. "Matches newline followed by a headline with the DONE keyword.")
  2976. (make-variable-buffer-local 'org-nl-done-regexp)
  2977. (defvar org-looking-at-done-regexp nil
  2978. "Matches the DONE keyword a point.")
  2979. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2980. (defvar org-ds-keyword-length 12
  2981. "Maximum length of the Deadline and SCHEDULED keywords.")
  2982. (make-variable-buffer-local 'org-ds-keyword-length)
  2983. (defvar org-deadline-regexp nil
  2984. "Matches the DEADLINE keyword.")
  2985. (make-variable-buffer-local 'org-deadline-regexp)
  2986. (defvar org-deadline-time-regexp nil
  2987. "Matches the DEADLINE keyword together with a time stamp.")
  2988. (make-variable-buffer-local 'org-deadline-time-regexp)
  2989. (defvar org-deadline-line-regexp nil
  2990. "Matches the DEADLINE keyword and the rest of the line.")
  2991. (make-variable-buffer-local 'org-deadline-line-regexp)
  2992. (defvar org-scheduled-regexp nil
  2993. "Matches the SCHEDULED keyword.")
  2994. (make-variable-buffer-local 'org-scheduled-regexp)
  2995. (defvar org-scheduled-time-regexp nil
  2996. "Matches the SCHEDULED keyword together with a time stamp.")
  2997. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2998. (defvar org-closed-time-regexp nil
  2999. "Matches the CLOSED keyword together with a time stamp.")
  3000. (make-variable-buffer-local 'org-closed-time-regexp)
  3001. (defvar org-keyword-time-regexp nil
  3002. "Matches any of the 4 keywords, together with the time stamp.")
  3003. (make-variable-buffer-local 'org-keyword-time-regexp)
  3004. (defvar org-keyword-time-not-clock-regexp nil
  3005. "Matches any of the 3 keywords, together with the time stamp.")
  3006. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3007. (defvar org-maybe-keyword-time-regexp nil
  3008. "Matches a timestamp, possibly preceeded by a keyword.")
  3009. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3010. (defvar org-planning-or-clock-line-re nil
  3011. "Matches a line with planning or clock info.")
  3012. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3013. (defconst org-plain-time-of-day-regexp
  3014. (concat
  3015. "\\(\\<[012]?[0-9]"
  3016. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3017. "\\(--?"
  3018. "\\(\\<[012]?[0-9]"
  3019. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3020. "\\)?")
  3021. "Regular expression to match a plain time or time range.
  3022. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3023. groups carry important information:
  3024. 0 the full match
  3025. 1 the first time, range or not
  3026. 8 the second time, if it is a range.")
  3027. (defconst org-plain-time-extension-regexp
  3028. (concat
  3029. "\\(\\<[012]?[0-9]"
  3030. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3031. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3032. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3033. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3034. groups carry important information:
  3035. 0 the full match
  3036. 7 hours of duration
  3037. 9 minutes of duration")
  3038. (defconst org-stamp-time-of-day-regexp
  3039. (concat
  3040. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3041. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3042. "\\(--?"
  3043. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3044. "Regular expression to match a timestamp time or time range.
  3045. After a match, the following groups carry important information:
  3046. 0 the full match
  3047. 1 date plus weekday, for backreferencing to make sure both times on same day
  3048. 2 the first time, range or not
  3049. 4 the second time, if it is a range.")
  3050. (defconst org-startup-options
  3051. '(("fold" org-startup-folded t)
  3052. ("overview" org-startup-folded t)
  3053. ("nofold" org-startup-folded nil)
  3054. ("showall" org-startup-folded nil)
  3055. ("content" org-startup-folded content)
  3056. ("indent" org-startup-indented t)
  3057. ("noindent" org-startup-indented nil)
  3058. ("hidestars" org-hide-leading-stars t)
  3059. ("showstars" org-hide-leading-stars nil)
  3060. ("odd" org-odd-levels-only t)
  3061. ("oddeven" org-odd-levels-only nil)
  3062. ("align" org-startup-align-all-tables t)
  3063. ("noalign" org-startup-align-all-tables nil)
  3064. ("customtime" org-display-custom-times t)
  3065. ("logdone" org-log-done time)
  3066. ("lognotedone" org-log-done note)
  3067. ("nologdone" org-log-done nil)
  3068. ("lognoteclock-out" org-log-note-clock-out t)
  3069. ("nolognoteclock-out" org-log-note-clock-out nil)
  3070. ("logrepeat" org-log-repeat state)
  3071. ("lognoterepeat" org-log-repeat note)
  3072. ("nologrepeat" org-log-repeat nil)
  3073. ("fninline" org-footnote-define-inline t)
  3074. ("nofninline" org-footnote-define-inline nil)
  3075. ("fnlocal" org-footnote-section nil)
  3076. ("fnauto" org-footnote-auto-label t)
  3077. ("fnprompt" org-footnote-auto-label nil)
  3078. ("fnconfirm" org-footnote-auto-label confirm)
  3079. ("fnplain" org-footnote-auto-label plain)
  3080. ("fnadjust" org-footnote-auto-adjust t)
  3081. ("nofnadjust" org-footnote-auto-adjust nil)
  3082. ("constcgs" constants-unit-system cgs)
  3083. ("constSI" constants-unit-system SI)
  3084. ("noptag" org-tag-persistent-alist nil)
  3085. ("hideblocks" org-hide-block-startup t)
  3086. ("nohideblocks" org-hide-block-startup nil))
  3087. "Variable associated with STARTUP options for org-mode.
  3088. Each element is a list of three items: The startup options as written
  3089. in the #+STARTUP line, the corresponding variable, and the value to
  3090. set this variable to if the option is found. An optional forth element PUSH
  3091. means to push this value onto the list in the variable.")
  3092. (defun org-set-regexps-and-options ()
  3093. "Precompute regular expressions for current buffer."
  3094. (when (org-mode-p)
  3095. (org-set-local 'org-todo-kwd-alist nil)
  3096. (org-set-local 'org-todo-key-alist nil)
  3097. (org-set-local 'org-todo-key-trigger nil)
  3098. (org-set-local 'org-todo-keywords-1 nil)
  3099. (org-set-local 'org-done-keywords nil)
  3100. (org-set-local 'org-todo-heads nil)
  3101. (org-set-local 'org-todo-sets nil)
  3102. (org-set-local 'org-todo-log-states nil)
  3103. (org-set-local 'org-file-properties nil)
  3104. (org-set-local 'org-file-tags nil)
  3105. (let ((re (org-make-options-regexp
  3106. '("CATEGORY" "TODO" "COLUMNS"
  3107. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3108. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3109. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3110. (splitre "[ \t]+")
  3111. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3112. tail sep kws1 prio props ftags drawers
  3113. ext-setup-or-nil setup-contents (start 0))
  3114. (save-excursion
  3115. (save-restriction
  3116. (widen)
  3117. (goto-char (point-min))
  3118. (while (or (and ext-setup-or-nil
  3119. (string-match re ext-setup-or-nil start)
  3120. (setq start (match-end 0)))
  3121. (and (setq ext-setup-or-nil nil start 0)
  3122. (re-search-forward re nil t)))
  3123. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3124. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3125. (cond
  3126. ((equal key "CATEGORY")
  3127. (if (string-match "[ \t]+$" value)
  3128. (setq value (replace-match "" t t value)))
  3129. (setq cat value))
  3130. ((member key '("SEQ_TODO" "TODO"))
  3131. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3132. ((equal key "TYP_TODO")
  3133. (push (cons 'type (org-split-string value splitre)) kwds))
  3134. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3135. ;; general TODO-like setup
  3136. (push (cons (intern (downcase (match-string 1 key)))
  3137. (org-split-string value splitre)) kwds))
  3138. ((equal key "TAGS")
  3139. (setq tags (append tags (if tags '("\\n") nil)
  3140. (org-split-string value splitre))))
  3141. ((equal key "COLUMNS")
  3142. (org-set-local 'org-columns-default-format value))
  3143. ((equal key "LINK")
  3144. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3145. (push (cons (match-string 1 value)
  3146. (org-trim (match-string 2 value)))
  3147. links)))
  3148. ((equal key "PRIORITIES")
  3149. (setq prio (org-split-string value " +")))
  3150. ((equal key "PROPERTY")
  3151. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3152. (push (cons (match-string 1 value) (match-string 2 value))
  3153. props)))
  3154. ((equal key "FILETAGS")
  3155. (when (string-match "\\S-" value)
  3156. (setq ftags
  3157. (append
  3158. ftags
  3159. (apply 'append
  3160. (mapcar (lambda (x) (org-split-string x ":"))
  3161. (org-split-string value)))))))
  3162. ((equal key "DRAWERS")
  3163. (setq drawers (org-split-string value splitre)))
  3164. ((equal key "CONSTANTS")
  3165. (setq const (append const (org-split-string value splitre))))
  3166. ((equal key "STARTUP")
  3167. (let ((opts (org-split-string value splitre))
  3168. l var val)
  3169. (while (setq l (pop opts))
  3170. (when (setq l (assoc l org-startup-options))
  3171. (setq var (nth 1 l) val (nth 2 l))
  3172. (if (not (nth 3 l))
  3173. (set (make-local-variable var) val)
  3174. (if (not (listp (symbol-value var)))
  3175. (set (make-local-variable var) nil))
  3176. (set (make-local-variable var) (symbol-value var))
  3177. (add-to-list var val))))))
  3178. ((equal key "ARCHIVE")
  3179. (string-match " *$" value)
  3180. (setq arch (replace-match "" t t value))
  3181. (remove-text-properties 0 (length arch)
  3182. '(face t fontified t) arch))
  3183. ((equal key "SETUPFILE")
  3184. (setq setup-contents (org-file-contents
  3185. (expand-file-name
  3186. (org-remove-double-quotes value))
  3187. 'noerror))
  3188. (if (not ext-setup-or-nil)
  3189. (setq ext-setup-or-nil setup-contents start 0)
  3190. (setq ext-setup-or-nil
  3191. (concat (substring ext-setup-or-nil 0 start)
  3192. "\n" setup-contents "\n"
  3193. (substring ext-setup-or-nil start)))))
  3194. ))))
  3195. (when cat
  3196. (org-set-local 'org-category (intern cat))
  3197. (push (cons "CATEGORY" cat) props))
  3198. (when prio
  3199. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3200. (setq prio (mapcar 'string-to-char prio))
  3201. (org-set-local 'org-highest-priority (nth 0 prio))
  3202. (org-set-local 'org-lowest-priority (nth 1 prio))
  3203. (org-set-local 'org-default-priority (nth 2 prio)))
  3204. (and props (org-set-local 'org-file-properties (nreverse props)))
  3205. (and ftags (org-set-local 'org-file-tags
  3206. (mapcar 'org-add-prop-inherited ftags)))
  3207. (and drawers (org-set-local 'org-drawers drawers))
  3208. (and arch (org-set-local 'org-archive-location arch))
  3209. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3210. ;; Process the TODO keywords
  3211. (unless kwds
  3212. ;; Use the global values as if they had been given locally.
  3213. (setq kwds (default-value 'org-todo-keywords))
  3214. (if (stringp (car kwds))
  3215. (setq kwds (list (cons org-todo-interpretation
  3216. (default-value 'org-todo-keywords)))))
  3217. (setq kwds (reverse kwds)))
  3218. (setq kwds (nreverse kwds))
  3219. (let (inter kws kw)
  3220. (while (setq kws (pop kwds))
  3221. (let ((kws (or
  3222. (run-hook-with-args-until-success
  3223. 'org-todo-setup-filter-hook kws)
  3224. kws)))
  3225. (setq inter (pop kws) sep (member "|" kws)
  3226. kws0 (delete "|" (copy-sequence kws))
  3227. kwsa nil
  3228. kws1 (mapcar
  3229. (lambda (x)
  3230. ;; 1 2
  3231. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3232. (progn
  3233. (setq kw (match-string 1 x)
  3234. key (and (match-end 2) (match-string 2 x))
  3235. log (org-extract-log-state-settings x))
  3236. (push (cons kw (and key (string-to-char key))) kwsa)
  3237. (and log (push log org-todo-log-states))
  3238. kw)
  3239. (error "Invalid TODO keyword %s" x)))
  3240. kws0)
  3241. kwsa (if kwsa (append '((:startgroup))
  3242. (nreverse kwsa)
  3243. '((:endgroup))))
  3244. hw (car kws1)
  3245. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3246. tail (list inter hw (car dws) (org-last dws))))
  3247. (add-to-list 'org-todo-heads hw 'append)
  3248. (push kws1 org-todo-sets)
  3249. (setq org-done-keywords (append org-done-keywords dws nil))
  3250. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3251. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3252. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3253. (setq org-todo-sets (nreverse org-todo-sets)
  3254. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3255. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3256. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3257. ;; Process the constants
  3258. (when const
  3259. (let (e cst)
  3260. (while (setq e (pop const))
  3261. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3262. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3263. (setq org-table-formula-constants-local cst)))
  3264. ;; Process the tags.
  3265. (when tags
  3266. (let (e tgs)
  3267. (while (setq e (pop tags))
  3268. (cond
  3269. ((equal e "{") (push '(:startgroup) tgs))
  3270. ((equal e "}") (push '(:endgroup) tgs))
  3271. ((equal e "\\n") (push '(:newline) tgs))
  3272. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3273. (push (cons (match-string 1 e)
  3274. (string-to-char (match-string 2 e)))
  3275. tgs))
  3276. (t (push (list e) tgs))))
  3277. (org-set-local 'org-tag-alist nil)
  3278. (while (setq e (pop tgs))
  3279. (or (and (stringp (car e))
  3280. (assoc (car e) org-tag-alist))
  3281. (push e org-tag-alist)))))
  3282. ;; Compute the regular expressions and other local variables
  3283. (if (not org-done-keywords)
  3284. (setq org-done-keywords (and org-todo-keywords-1
  3285. (list (org-last org-todo-keywords-1)))))
  3286. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3287. (length org-scheduled-string)
  3288. (length org-clock-string)
  3289. (length org-closed-string)))
  3290. org-drawer-regexp
  3291. (concat "^[ \t]*:\\("
  3292. (mapconcat 'regexp-quote org-drawers "\\|")
  3293. "\\):[ \t]*$")
  3294. org-not-done-keywords
  3295. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3296. org-todo-regexp
  3297. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3298. "\\|") "\\)\\>")
  3299. org-not-done-regexp
  3300. (concat "\\<\\("
  3301. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3302. "\\)\\>")
  3303. org-not-done-heading-regexp
  3304. (concat "^\\(\\*+\\)[ \t]+\\("
  3305. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3306. "\\)\\>")
  3307. org-todo-line-regexp
  3308. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3309. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3310. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3311. org-complex-heading-regexp
  3312. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3313. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3314. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3315. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3316. org-nl-done-regexp
  3317. (concat "\n\\*+[ \t]+"
  3318. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3319. "\\)" "\\>")
  3320. org-todo-line-tags-regexp
  3321. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3322. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3323. (org-re
  3324. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3325. org-looking-at-done-regexp
  3326. (concat "^" "\\(?:"
  3327. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3328. "\\>")
  3329. org-deadline-regexp (concat "\\<" org-deadline-string)
  3330. org-deadline-time-regexp
  3331. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3332. org-deadline-line-regexp
  3333. (concat "\\<\\(" org-deadline-string "\\).*")
  3334. org-scheduled-regexp
  3335. (concat "\\<" org-scheduled-string)
  3336. org-scheduled-time-regexp
  3337. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3338. org-closed-time-regexp
  3339. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3340. org-keyword-time-regexp
  3341. (concat "\\<\\(" org-scheduled-string
  3342. "\\|" org-deadline-string
  3343. "\\|" org-closed-string
  3344. "\\|" org-clock-string "\\)"
  3345. " *[[<]\\([^]>]+\\)[]>]")
  3346. org-keyword-time-not-clock-regexp
  3347. (concat "\\<\\(" org-scheduled-string
  3348. "\\|" org-deadline-string
  3349. "\\|" org-closed-string
  3350. "\\)"
  3351. " *[[<]\\([^]>]+\\)[]>]")
  3352. org-maybe-keyword-time-regexp
  3353. (concat "\\(\\<\\(" org-scheduled-string
  3354. "\\|" org-deadline-string
  3355. "\\|" org-closed-string
  3356. "\\|" org-clock-string "\\)\\)?"
  3357. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3358. org-planning-or-clock-line-re
  3359. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3360. "\\|" org-deadline-string
  3361. "\\|" org-closed-string "\\|" org-clock-string
  3362. "\\)\\>\\)")
  3363. )
  3364. (org-compute-latex-and-specials-regexp)
  3365. (org-set-font-lock-defaults))))
  3366. (defun org-file-contents (file &optional noerror)
  3367. "Return the contents of FILE, as a string."
  3368. (if (or (not file)
  3369. (not (file-readable-p file)))
  3370. (if noerror
  3371. (progn
  3372. (message "Cannot read file %s" file)
  3373. (ding) (sit-for 2)
  3374. "")
  3375. (error "Cannot read file %s" file))
  3376. (with-temp-buffer
  3377. (insert-file-contents file)
  3378. (buffer-string))))
  3379. (defun org-extract-log-state-settings (x)
  3380. "Extract the log state setting from a TODO keyword string.
  3381. This will extract info from a string like \"WAIT(w@/!)\"."
  3382. (let (kw key log1 log2)
  3383. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3384. (setq kw (match-string 1 x)
  3385. key (and (match-end 2) (match-string 2 x))
  3386. log1 (and (match-end 3) (match-string 3 x))
  3387. log2 (and (match-end 4) (match-string 4 x)))
  3388. (and (or log1 log2)
  3389. (list kw
  3390. (and log1 (if (equal log1 "!") 'time 'note))
  3391. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3392. (defun org-remove-keyword-keys (list)
  3393. "Remove a pair of parenthesis at the end of each string in LIST."
  3394. (mapcar (lambda (x)
  3395. (if (string-match "(.*)$" x)
  3396. (substring x 0 (match-beginning 0))
  3397. x))
  3398. list))
  3399. ;; FIXME: this could be done much better, using second characters etc.
  3400. (defun org-assign-fast-keys (alist)
  3401. "Assign fast keys to a keyword-key alist.
  3402. Respect keys that are already there."
  3403. (let (new e k c c1 c2 (char ?a))
  3404. (while (setq e (pop alist))
  3405. (cond
  3406. ((equal e '(:startgroup)) (push e new))
  3407. ((equal e '(:endgroup)) (push e new))
  3408. ((equal e '(:newline)) (push e new))
  3409. (t
  3410. (setq k (car e) c2 nil)
  3411. (if (cdr e)
  3412. (setq c (cdr e))
  3413. ;; automatically assign a character.
  3414. (setq c1 (string-to-char
  3415. (downcase (substring
  3416. k (if (= (string-to-char k) ?@) 1 0)))))
  3417. (if (or (rassoc c1 new) (rassoc c1 alist))
  3418. (while (or (rassoc char new) (rassoc char alist))
  3419. (setq char (1+ char)))
  3420. (setq c2 c1))
  3421. (setq c (or c2 char)))
  3422. (push (cons k c) new))))
  3423. (nreverse new)))
  3424. ;;; Some variables used in various places
  3425. (defvar org-window-configuration nil
  3426. "Used in various places to store a window configuration.")
  3427. (defvar org-selected-window nil
  3428. "Used in various places to store a window configuration.")
  3429. (defvar org-finish-function nil
  3430. "Function to be called when `C-c C-c' is used.
  3431. This is for getting out of special buffers like remember.")
  3432. ;; FIXME: Occasionally check by commenting these, to make sure
  3433. ;; no other functions uses these, forgetting to let-bind them.
  3434. (defvar entry)
  3435. (defvar last-state)
  3436. (defvar date)
  3437. ;; Defined somewhere in this file, but used before definition.
  3438. (defvar org-html-entities)
  3439. (defvar org-struct-menu)
  3440. (defvar org-org-menu)
  3441. (defvar org-tbl-menu)
  3442. (defvar org-agenda-keymap)
  3443. ;;;; Define the Org-mode
  3444. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3445. (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."))
  3446. ;; We use a before-change function to check if a table might need
  3447. ;; an update.
  3448. (defvar org-table-may-need-update t
  3449. "Indicates that a table might need an update.
  3450. This variable is set by `org-before-change-function'.
  3451. `org-table-align' sets it back to nil.")
  3452. (defun org-before-change-function (beg end)
  3453. "Every change indicates that a table might need an update."
  3454. (setq org-table-may-need-update t))
  3455. (defvar org-mode-map)
  3456. (defvar org-mode-hook nil
  3457. "Mode hook for Org-mode, run after the mode was turned on.")
  3458. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3459. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3460. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3461. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3462. (defvar org-table-buffer-is-an nil)
  3463. (defconst org-outline-regexp "\\*+ ")
  3464. ;;;###autoload
  3465. (define-derived-mode org-mode outline-mode "Org"
  3466. "Outline-based notes management and organizer, alias
  3467. \"Carsten's outline-mode for keeping track of everything.\"
  3468. Org-mode develops organizational tasks around a NOTES file which
  3469. contains information about projects as plain text. Org-mode is
  3470. implemented on top of outline-mode, which is ideal to keep the content
  3471. of large files well structured. It supports ToDo items, deadlines and
  3472. time stamps, which magically appear in the diary listing of the Emacs
  3473. calendar. Tables are easily created with a built-in table editor.
  3474. Plain text URL-like links connect to websites, emails (VM), Usenet
  3475. messages (Gnus), BBDB entries, and any files related to the project.
  3476. For printing and sharing of notes, an Org-mode file (or a part of it)
  3477. can be exported as a structured ASCII or HTML file.
  3478. The following commands are available:
  3479. \\{org-mode-map}"
  3480. ;; Get rid of Outline menus, they are not needed
  3481. ;; Need to do this here because define-derived-mode sets up
  3482. ;; the keymap so late. Still, it is a waste to call this each time
  3483. ;; we switch another buffer into org-mode.
  3484. (if (featurep 'xemacs)
  3485. (when (boundp 'outline-mode-menu-heading)
  3486. ;; Assume this is Greg's port, it used easymenu
  3487. (easy-menu-remove outline-mode-menu-heading)
  3488. (easy-menu-remove outline-mode-menu-show)
  3489. (easy-menu-remove outline-mode-menu-hide))
  3490. (define-key org-mode-map [menu-bar headings] 'undefined)
  3491. (define-key org-mode-map [menu-bar hide] 'undefined)
  3492. (define-key org-mode-map [menu-bar show] 'undefined))
  3493. (org-load-modules-maybe)
  3494. (easy-menu-add org-org-menu)
  3495. (easy-menu-add org-tbl-menu)
  3496. (org-install-agenda-files-menu)
  3497. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3498. (org-add-to-invisibility-spec '(org-cwidth))
  3499. (org-add-to-invisibility-spec '(org-hide-block . t))
  3500. (when (featurep 'xemacs)
  3501. (org-set-local 'line-move-ignore-invisible t))
  3502. (org-set-local 'outline-regexp org-outline-regexp)
  3503. (org-set-local 'outline-level 'org-outline-level)
  3504. (when (and org-ellipsis
  3505. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3506. (fboundp 'make-glyph-code))
  3507. (unless org-display-table
  3508. (setq org-display-table (make-display-table)))
  3509. (set-display-table-slot
  3510. org-display-table 4
  3511. (vconcat (mapcar
  3512. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3513. org-ellipsis)))
  3514. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3515. (setq buffer-display-table org-display-table))
  3516. (org-set-regexps-and-options)
  3517. (when (and org-tag-faces (not org-tags-special-faces-re))
  3518. ;; tag faces set outside customize.... force initialization.
  3519. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3520. ;; Calc embedded
  3521. (org-set-local 'calc-embedded-open-mode "# ")
  3522. (modify-syntax-entry ?# "<")
  3523. (modify-syntax-entry ?@ "w")
  3524. (if org-startup-truncated (setq truncate-lines t))
  3525. (org-set-local 'font-lock-unfontify-region-function
  3526. 'org-unfontify-region)
  3527. ;; Activate before-change-function
  3528. (org-set-local 'org-table-may-need-update t)
  3529. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3530. 'local)
  3531. ;; Check for running clock before killing a buffer
  3532. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3533. ;; Paragraphs and auto-filling
  3534. (org-set-autofill-regexps)
  3535. (setq indent-line-function 'org-indent-line-function)
  3536. (org-update-radio-target-regexp)
  3537. ;; Make sure dependence stuff works reliably, even for users who set it
  3538. ;; too late :-(
  3539. (if org-enforce-todo-dependencies
  3540. (add-hook 'org-blocker-hook
  3541. 'org-block-todo-from-children-or-siblings-or-parent)
  3542. (remove-hook 'org-blocker-hook
  3543. 'org-block-todo-from-children-or-siblings-or-parent))
  3544. (if org-enforce-todo-checkbox-dependencies
  3545. (add-hook 'org-blocker-hook
  3546. 'org-block-todo-from-checkboxes)
  3547. (remove-hook 'org-blocker-hook
  3548. 'org-block-todo-from-checkboxes))
  3549. ;; Comment characters
  3550. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3551. (org-set-local 'comment-padding " ")
  3552. ;; Align options lines
  3553. (org-set-local
  3554. 'align-mode-rules-list
  3555. '((org-in-buffer-settings
  3556. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3557. (modes . '(org-mode)))))
  3558. ;; Imenu
  3559. (org-set-local 'imenu-create-index-function
  3560. 'org-imenu-get-tree)
  3561. ;; Make isearch reveal context
  3562. (if (or (featurep 'xemacs)
  3563. (not (boundp 'outline-isearch-open-invisible-function)))
  3564. ;; Emacs 21 and XEmacs make use of the hook
  3565. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3566. ;; Emacs 22 deals with this through a special variable
  3567. (org-set-local 'outline-isearch-open-invisible-function
  3568. (lambda (&rest ignore) (org-show-context 'isearch))))
  3569. ;; If empty file that did not turn on org-mode automatically, make it to.
  3570. (if (and org-insert-mode-line-in-empty-file
  3571. (interactive-p)
  3572. (= (point-min) (point-max)))
  3573. (insert "# -*- mode: org -*-\n\n"))
  3574. (unless org-inhibit-startup
  3575. (when org-startup-align-all-tables
  3576. (let ((bmp (buffer-modified-p)))
  3577. (org-table-map-tables 'org-table-align)
  3578. (set-buffer-modified-p bmp)))
  3579. (when org-startup-indented
  3580. (require 'org-indent)
  3581. (org-indent-mode 1))
  3582. (org-set-startup-visibility)))
  3583. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3584. (defun org-current-time ()
  3585. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3586. (if (> (car org-time-stamp-rounding-minutes) 1)
  3587. (let ((r (car org-time-stamp-rounding-minutes))
  3588. (time (decode-time)))
  3589. (apply 'encode-time
  3590. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3591. (nthcdr 2 time))))
  3592. (current-time)))
  3593. ;;;; Font-Lock stuff, including the activators
  3594. (defvar org-mouse-map (make-sparse-keymap))
  3595. (org-defkey org-mouse-map
  3596. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3597. (org-defkey org-mouse-map
  3598. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3599. (when org-mouse-1-follows-link
  3600. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3601. (when org-tab-follows-link
  3602. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3603. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3604. (require 'font-lock)
  3605. (defconst org-non-link-chars "]\t\n\r<>")
  3606. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3607. "shell" "elisp"))
  3608. (defvar org-link-types-re nil
  3609. "Matches a link that has a url-like prefix like \"http:\"")
  3610. (defvar org-link-re-with-space nil
  3611. "Matches a link with spaces, optional angular brackets around it.")
  3612. (defvar org-link-re-with-space2 nil
  3613. "Matches a link with spaces, optional angular brackets around it.")
  3614. (defvar org-link-re-with-space3 nil
  3615. "Matches a link with spaces, only for internal part in bracket links.")
  3616. (defvar org-angle-link-re nil
  3617. "Matches link with angular brackets, spaces are allowed.")
  3618. (defvar org-plain-link-re nil
  3619. "Matches plain link, without spaces.")
  3620. (defvar org-bracket-link-regexp nil
  3621. "Matches a link in double brackets.")
  3622. (defvar org-bracket-link-analytic-regexp nil
  3623. "Regular expression used to analyze links.
  3624. Here is what the match groups contain after a match:
  3625. 1: http:
  3626. 2: http
  3627. 3: path
  3628. 4: [desc]
  3629. 5: desc")
  3630. (defvar org-bracket-link-analytic-regexp++ nil
  3631. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3632. (defvar org-any-link-re nil
  3633. "Regular expression matching any link.")
  3634. (defun org-make-link-regexps ()
  3635. "Update the link regular expressions.
  3636. This should be called after the variable `org-link-types' has changed."
  3637. (setq org-link-types-re
  3638. (concat
  3639. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3640. org-link-re-with-space
  3641. (concat
  3642. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3643. "\\([^" org-non-link-chars " ]"
  3644. "[^" org-non-link-chars "]*"
  3645. "[^" org-non-link-chars " ]\\)>?")
  3646. org-link-re-with-space2
  3647. (concat
  3648. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3649. "\\([^" org-non-link-chars " ]"
  3650. "[^\t\n\r]*"
  3651. "[^" org-non-link-chars " ]\\)>?")
  3652. org-link-re-with-space3
  3653. (concat
  3654. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3655. "\\([^" org-non-link-chars " ]"
  3656. "[^\t\n\r]*\\)")
  3657. org-angle-link-re
  3658. (concat
  3659. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3660. "\\([^" org-non-link-chars " ]"
  3661. "[^" org-non-link-chars "]*"
  3662. "\\)>")
  3663. org-plain-link-re
  3664. (concat
  3665. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3666. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3667. org-bracket-link-regexp
  3668. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3669. org-bracket-link-analytic-regexp
  3670. (concat
  3671. "\\[\\["
  3672. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3673. "\\([^]]+\\)"
  3674. "\\]"
  3675. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3676. "\\]")
  3677. org-bracket-link-analytic-regexp++
  3678. (concat
  3679. "\\[\\["
  3680. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3681. "\\([^]]+\\)"
  3682. "\\]"
  3683. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3684. "\\]")
  3685. org-any-link-re
  3686. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3687. org-angle-link-re "\\)\\|\\("
  3688. org-plain-link-re "\\)")))
  3689. (org-make-link-regexps)
  3690. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3691. "Regular expression for fast time stamp matching.")
  3692. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3693. "Regular expression for fast time stamp matching.")
  3694. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3695. "Regular expression matching time strings for analysis.
  3696. This one does not require the space after the date, so it can be used
  3697. on a string that terminates immediately after the date.")
  3698. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3699. "Regular expression matching time strings for analysis.")
  3700. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3701. "Regular expression matching time stamps, with groups.")
  3702. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3703. "Regular expression matching time stamps (also [..]), with groups.")
  3704. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3705. "Regular expression matching a time stamp range.")
  3706. (defconst org-tr-regexp-both
  3707. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3708. "Regular expression matching a time stamp range.")
  3709. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3710. org-ts-regexp "\\)?")
  3711. "Regular expression matching a time stamp or time stamp range.")
  3712. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3713. org-ts-regexp-both "\\)?")
  3714. "Regular expression matching a time stamp or time stamp range.
  3715. The time stamps may be either active or inactive.")
  3716. (defvar org-emph-face nil)
  3717. (defun org-do-emphasis-faces (limit)
  3718. "Run through the buffer and add overlays to links."
  3719. (let (rtn a)
  3720. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3721. (if (not (= (char-after (match-beginning 3))
  3722. (char-after (match-beginning 4))))
  3723. (progn
  3724. (setq rtn t)
  3725. (setq a (assoc (match-string 3) org-emphasis-alist))
  3726. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3727. 'face
  3728. (nth 1 a))
  3729. (and (nth 4 a)
  3730. (org-remove-flyspell-overlays-in
  3731. (match-beginning 0) (match-end 0)))
  3732. (add-text-properties (match-beginning 2) (match-end 2)
  3733. '(font-lock-multiline t))
  3734. (when org-hide-emphasis-markers
  3735. (add-text-properties (match-end 4) (match-beginning 5)
  3736. '(invisible org-link))
  3737. (add-text-properties (match-beginning 3) (match-end 3)
  3738. '(invisible org-link)))))
  3739. (backward-char 1))
  3740. rtn))
  3741. (defun org-emphasize (&optional char)
  3742. "Insert or change an emphasis, i.e. a font like bold or italic.
  3743. If there is an active region, change that region to a new emphasis.
  3744. If there is no region, just insert the marker characters and position
  3745. the cursor between them.
  3746. CHAR should be either the marker character, or the first character of the
  3747. HTML tag associated with that emphasis. If CHAR is a space, the means
  3748. to remove the emphasis of the selected region.
  3749. If char is not given (for example in an interactive call) it
  3750. will be prompted for."
  3751. (interactive)
  3752. (let ((eal org-emphasis-alist) e det
  3753. (erc org-emphasis-regexp-components)
  3754. (prompt "")
  3755. (string "") beg end move tag c s)
  3756. (if (org-region-active-p)
  3757. (setq beg (region-beginning) end (region-end)
  3758. string (buffer-substring beg end))
  3759. (setq move t))
  3760. (while (setq e (pop eal))
  3761. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3762. c (aref tag 0))
  3763. (push (cons c (string-to-char (car e))) det)
  3764. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3765. (substring tag 1)))))
  3766. (setq det (nreverse det))
  3767. (unless char
  3768. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3769. (setq char (read-char-exclusive)))
  3770. (setq char (or (cdr (assoc char det)) char))
  3771. (if (equal char ?\ )
  3772. (setq s "" move nil)
  3773. (unless (assoc (char-to-string char) org-emphasis-alist)
  3774. (error "No such emphasis marker: \"%c\"" char))
  3775. (setq s (char-to-string char)))
  3776. (while (and (> (length string) 1)
  3777. (equal (substring string 0 1) (substring string -1))
  3778. (assoc (substring string 0 1) org-emphasis-alist))
  3779. (setq string (substring string 1 -1)))
  3780. (setq string (concat s string s))
  3781. (if beg (delete-region beg end))
  3782. (unless (or (bolp)
  3783. (string-match (concat "[" (nth 0 erc) "\n]")
  3784. (char-to-string (char-before (point)))))
  3785. (insert " "))
  3786. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3787. (char-to-string (char-after (point))))
  3788. (insert " ") (backward-char 1))
  3789. (insert string)
  3790. (and move (backward-char 1))))
  3791. (defconst org-nonsticky-props
  3792. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3793. (defsubst org-rear-nonsticky-at (pos)
  3794. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3795. (defun org-activate-plain-links (limit)
  3796. "Run through the buffer and add overlays to links."
  3797. (catch 'exit
  3798. (let (f)
  3799. (if (re-search-forward org-plain-link-re limit t)
  3800. (progn
  3801. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3802. (setq f (get-text-property (match-beginning 0) 'face))
  3803. (if (or (eq f 'org-tag)
  3804. (and (listp f) (memq 'org-tag f)))
  3805. nil
  3806. (add-text-properties (match-beginning 0) (match-end 0)
  3807. (list 'mouse-face 'highlight
  3808. 'keymap org-mouse-map))
  3809. (org-rear-nonsticky-at (match-end 0)))
  3810. t)))))
  3811. (defun org-activate-code (limit)
  3812. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3813. (progn
  3814. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3815. (remove-text-properties (match-beginning 0) (match-end 0)
  3816. '(display t invisible t intangible t))
  3817. t)))
  3818. (defun org-fontify-meta-lines-and-blocks (limit)
  3819. "Fontify #+ lines and blocks, in the correct ways."
  3820. (let ((case-fold-search t))
  3821. (if (re-search-forward
  3822. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3823. limit t)
  3824. (let ((beg (match-beginning 0))
  3825. (beg1 (line-beginning-position 2))
  3826. (dc1 (downcase (match-string 2)))
  3827. (dc3 (downcase (match-string 3)))
  3828. end end1 quoting)
  3829. (cond
  3830. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3831. ;; a single line of backend-specific content
  3832. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3833. (remove-text-properties (match-beginning 0) (match-end 0)
  3834. '(display t invisible t intangible t))
  3835. (add-text-properties (match-beginning 1) (match-end 3)
  3836. '(font-lock-fontified t face org-meta-line))
  3837. (add-text-properties (match-beginning 6) (match-end 6)
  3838. '(font-lock-fontified t face org-block))
  3839. t)
  3840. ((and (match-end 4) (equal dc3 "begin"))
  3841. ;; Truely a block
  3842. (setq quoting (member (downcase (match-string 5))
  3843. org-protecting-blocks))
  3844. (when (re-search-forward
  3845. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3846. nil t) ;; on purpose, we look further than LIMIT
  3847. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3848. (when quoting
  3849. (remove-text-properties beg end
  3850. '(display t invisible t intangible t)))
  3851. (add-text-properties
  3852. beg end
  3853. '(font-lock-fontified t font-lock-multiline t))
  3854. (add-text-properties beg beg1 '(face org-meta-line))
  3855. (add-text-properties end1 end '(face org-meta-line))
  3856. (when quoting
  3857. (add-text-properties beg1 end1 '(face org-block)))
  3858. t))
  3859. ((not (member (char-after beg) '(?\ ?\t)))
  3860. ;; just any other in-buffer setting, but not indented
  3861. (add-text-properties
  3862. beg (match-end 0)
  3863. '(font-lock-fontified t face org-meta-line))
  3864. t)
  3865. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3866. (and (match-end 4) (equal dc3 "attr")))
  3867. (add-text-properties
  3868. beg (match-end 0)
  3869. '(font-lock-fontified t face org-meta-line))
  3870. t)
  3871. (t nil))))))
  3872. (defun org-activate-angle-links (limit)
  3873. "Run through the buffer and add overlays to links."
  3874. (if (re-search-forward org-angle-link-re limit t)
  3875. (progn
  3876. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3877. (add-text-properties (match-beginning 0) (match-end 0)
  3878. (list 'mouse-face 'highlight
  3879. 'keymap org-mouse-map))
  3880. (org-rear-nonsticky-at (match-end 0))
  3881. t)))
  3882. (defun org-activate-footnote-links (limit)
  3883. "Run through the buffer and add overlays to links."
  3884. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3885. limit t)
  3886. (progn
  3887. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3888. (add-text-properties (match-beginning 2) (match-end 2)
  3889. (list 'mouse-face 'highlight
  3890. 'keymap org-mouse-map
  3891. 'help-echo
  3892. (if (= (point-at-bol) (match-beginning 2))
  3893. "Footnote definition"
  3894. "Footnote reference")
  3895. ))
  3896. (org-rear-nonsticky-at (match-end 2))
  3897. t)))
  3898. (defun org-activate-bracket-links (limit)
  3899. "Run through the buffer and add overlays to bracketed links."
  3900. (if (re-search-forward org-bracket-link-regexp limit t)
  3901. (let* ((help (concat "LINK: "
  3902. (org-match-string-no-properties 1)))
  3903. ;; FIXME: above we should remove the escapes.
  3904. ;; but that requires another match, protecting match data,
  3905. ;; a lot of overhead for font-lock.
  3906. (ip (org-maybe-intangible
  3907. (list 'invisible 'org-link
  3908. 'keymap org-mouse-map 'mouse-face 'highlight
  3909. 'font-lock-multiline t 'help-echo help)))
  3910. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3911. 'font-lock-multiline t 'help-echo help)))
  3912. ;; We need to remove the invisible property here. Table narrowing
  3913. ;; may have made some of this invisible.
  3914. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3915. (remove-text-properties (match-beginning 0) (match-end 0)
  3916. '(invisible nil))
  3917. (if (match-end 3)
  3918. (progn
  3919. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3920. (org-rear-nonsticky-at (match-beginning 3))
  3921. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3922. (org-rear-nonsticky-at (match-end 3))
  3923. (add-text-properties (match-end 3) (match-end 0) ip)
  3924. (org-rear-nonsticky-at (match-end 0)))
  3925. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3926. (org-rear-nonsticky-at (match-beginning 1))
  3927. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3928. (org-rear-nonsticky-at (match-end 1))
  3929. (add-text-properties (match-end 1) (match-end 0) ip)
  3930. (org-rear-nonsticky-at (match-end 0)))
  3931. t)))
  3932. (defun org-activate-dates (limit)
  3933. "Run through the buffer and add overlays to dates."
  3934. (if (re-search-forward org-tsr-regexp-both limit t)
  3935. (progn
  3936. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3937. (add-text-properties (match-beginning 0) (match-end 0)
  3938. (list 'mouse-face 'highlight
  3939. 'keymap org-mouse-map))
  3940. (org-rear-nonsticky-at (match-end 0))
  3941. (when org-display-custom-times
  3942. (if (match-end 3)
  3943. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3944. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3945. t)))
  3946. (defvar org-target-link-regexp nil
  3947. "Regular expression matching radio targets in plain text.")
  3948. (make-variable-buffer-local 'org-target-link-regexp)
  3949. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3950. "Regular expression matching a link target.")
  3951. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3952. "Regular expression matching a radio target.")
  3953. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3954. "Regular expression matching any target.")
  3955. (defun org-activate-target-links (limit)
  3956. "Run through the buffer and add overlays to target matches."
  3957. (when org-target-link-regexp
  3958. (let ((case-fold-search t))
  3959. (if (re-search-forward org-target-link-regexp limit t)
  3960. (progn
  3961. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3962. (add-text-properties (match-beginning 0) (match-end 0)
  3963. (list 'mouse-face 'highlight
  3964. 'keymap org-mouse-map
  3965. 'help-echo "Radio target link"
  3966. 'org-linked-text t))
  3967. (org-rear-nonsticky-at (match-end 0))
  3968. t)))))
  3969. (defun org-update-radio-target-regexp ()
  3970. "Find all radio targets in this file and update the regular expression."
  3971. (interactive)
  3972. (when (memq 'radio org-activate-links)
  3973. (setq org-target-link-regexp
  3974. (org-make-target-link-regexp (org-all-targets 'radio)))
  3975. (org-restart-font-lock)))
  3976. (defun org-hide-wide-columns (limit)
  3977. (let (s e)
  3978. (setq s (text-property-any (point) (or limit (point-max))
  3979. 'org-cwidth t))
  3980. (when s
  3981. (setq e (next-single-property-change s 'org-cwidth))
  3982. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3983. (goto-char e)
  3984. t)))
  3985. (defvar org-latex-and-specials-regexp nil
  3986. "Regular expression for highlighting export special stuff.")
  3987. (defvar org-match-substring-regexp)
  3988. (defvar org-match-substring-with-braces-regexp)
  3989. ;; This should be with the exporter code, but we also use if for font-locking
  3990. (defconst org-export-html-special-string-regexps
  3991. '(("\\\\-" . "&shy;")
  3992. ("---\\([^-]\\)" . "&mdash;\\1")
  3993. ("--\\([^-]\\)" . "&ndash;\\1")
  3994. ("\\.\\.\\." . "&hellip;"))
  3995. "Regular expressions for special string conversion.")
  3996. (defun org-compute-latex-and-specials-regexp ()
  3997. "Compute regular expression for stuff treated specially by exporters."
  3998. (if (not org-highlight-latex-fragments-and-specials)
  3999. (org-set-local 'org-latex-and-specials-regexp nil)
  4000. (require 'org-exp)
  4001. (let*
  4002. ((matchers (plist-get org-format-latex-options :matchers))
  4003. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4004. org-latex-regexps)))
  4005. (options (org-combine-plists (org-default-export-plist)
  4006. (org-infile-export-plist)))
  4007. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4008. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4009. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4010. (org-export-html-expand (plist-get options :expand-quoted-html))
  4011. (org-export-with-special-strings (plist-get options :special-strings))
  4012. (re-sub
  4013. (cond
  4014. ((equal org-export-with-sub-superscripts '{})
  4015. (list org-match-substring-with-braces-regexp))
  4016. (org-export-with-sub-superscripts
  4017. (list org-match-substring-regexp))
  4018. (t nil)))
  4019. (re-latex
  4020. (if org-export-with-LaTeX-fragments
  4021. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4022. (re-macros
  4023. (if org-export-with-TeX-macros
  4024. (list (concat "\\\\"
  4025. (regexp-opt
  4026. (append (mapcar 'car org-html-entities)
  4027. (if (boundp 'org-latex-entities)
  4028. (mapcar (lambda (x)
  4029. (or (car-safe x) x))
  4030. org-latex-entities)
  4031. nil))
  4032. 'words))) ; FIXME
  4033. ))
  4034. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4035. (re-special (if org-export-with-special-strings
  4036. (mapcar (lambda (x) (car x))
  4037. org-export-html-special-string-regexps)))
  4038. (re-rest
  4039. (delq nil
  4040. (list
  4041. (if org-export-html-expand "@<[^>\n]+>")
  4042. ))))
  4043. (org-set-local
  4044. 'org-latex-and-specials-regexp
  4045. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4046. re-rest) "\\|")))))
  4047. (defun org-do-latex-and-special-faces (limit)
  4048. "Run through the buffer and add overlays to links."
  4049. (when org-latex-and-specials-regexp
  4050. (let (rtn d)
  4051. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4052. limit t))
  4053. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4054. 'face))
  4055. '(org-code org-verbatim underline)))
  4056. (progn
  4057. (setq rtn t
  4058. d (cond ((member (char-after (1+ (match-beginning 0)))
  4059. '(?_ ?^)) 1)
  4060. (t 0)))
  4061. (font-lock-prepend-text-property
  4062. (+ d (match-beginning 0)) (match-end 0)
  4063. 'face 'org-latex-and-export-specials)
  4064. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4065. '(font-lock-multiline t)))))
  4066. rtn)))
  4067. (defun org-restart-font-lock ()
  4068. "Restart font-lock-mode, to force refontification."
  4069. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4070. (font-lock-mode -1)
  4071. (font-lock-mode 1)))
  4072. (defun org-all-targets (&optional radio)
  4073. "Return a list of all targets in this file.
  4074. With optional argument RADIO, only find radio targets."
  4075. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4076. rtn)
  4077. (save-excursion
  4078. (goto-char (point-min))
  4079. (while (re-search-forward re nil t)
  4080. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4081. rtn)))
  4082. (defun org-make-target-link-regexp (targets)
  4083. "Make regular expression matching all strings in TARGETS.
  4084. The regular expression finds the targets also if there is a line break
  4085. between words."
  4086. (and targets
  4087. (concat
  4088. "\\<\\("
  4089. (mapconcat
  4090. (lambda (x)
  4091. (while (string-match " +" x)
  4092. (setq x (replace-match "\\s-+" t t x)))
  4093. x)
  4094. targets
  4095. "\\|")
  4096. "\\)\\>")))
  4097. (defun org-activate-tags (limit)
  4098. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4099. (progn
  4100. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4101. (add-text-properties (match-beginning 1) (match-end 1)
  4102. (list 'mouse-face 'highlight
  4103. 'keymap org-mouse-map))
  4104. (org-rear-nonsticky-at (match-end 1))
  4105. t)))
  4106. (defun org-outline-level ()
  4107. (save-excursion
  4108. (looking-at outline-regexp)
  4109. (if (match-beginning 1)
  4110. (+ (org-get-string-indentation (match-string 1)) 1000)
  4111. (1- (- (match-end 0) (match-beginning 0))))))
  4112. (defvar org-font-lock-keywords nil)
  4113. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4114. "Regular expression matching a property line.")
  4115. (defvar org-font-lock-hook nil
  4116. "Functions to be called for special font lock stuff.")
  4117. (defun org-font-lock-hook (limit)
  4118. (run-hook-with-args 'org-font-lock-hook limit))
  4119. (defun org-set-font-lock-defaults ()
  4120. (let* ((em org-fontify-emphasized-text)
  4121. (lk org-activate-links)
  4122. (org-font-lock-extra-keywords
  4123. (list
  4124. ;; Call the hook
  4125. '(org-font-lock-hook)
  4126. ;; Headlines
  4127. `(,(if org-fontify-whole-heading-line
  4128. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4129. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4130. (1 (org-get-level-face 1))
  4131. (2 (org-get-level-face 2))
  4132. (3 (org-get-level-face 3)))
  4133. ;; Table lines
  4134. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4135. (1 'org-table t))
  4136. ;; Table internals
  4137. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4138. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4139. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4140. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4141. ;; Drawers
  4142. (list org-drawer-regexp '(0 'org-special-keyword t))
  4143. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4144. ;; Properties
  4145. (list org-property-re
  4146. '(1 'org-special-keyword t)
  4147. '(3 'org-property-value t))
  4148. ;; Links
  4149. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4150. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4151. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4152. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4153. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4154. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4155. (if (memq 'footnote lk) '(org-activate-footnote-links
  4156. (2 'org-footnote t)))
  4157. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4158. '(org-hide-wide-columns (0 nil append))
  4159. ;; TODO lines
  4160. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4161. '(1 (org-get-todo-face 1) t))
  4162. ;; DONE
  4163. (if org-fontify-done-headline
  4164. (list (concat "^[*]+ +\\<\\("
  4165. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4166. "\\)\\(.*\\)")
  4167. '(2 'org-headline-done t))
  4168. nil)
  4169. ;; Priorities
  4170. '(org-font-lock-add-priority-faces)
  4171. ;; Tags
  4172. '(org-font-lock-add-tag-faces)
  4173. ;; Special keywords
  4174. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4175. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4176. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4177. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4178. ;; Emphasis
  4179. (if em
  4180. (if (featurep 'xemacs)
  4181. '(org-do-emphasis-faces (0 nil append))
  4182. '(org-do-emphasis-faces)))
  4183. ;; Checkboxes
  4184. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4185. 2 'org-checkbox prepend)
  4186. (if org-provide-checkbox-statistics
  4187. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4188. (0 (org-get-checkbox-statistics-face) t)))
  4189. ;; Description list items
  4190. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4191. 2 'bold prepend)
  4192. ;; ARCHIVEd headings
  4193. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4194. '(1 'org-archived prepend))
  4195. ;; Specials
  4196. '(org-do-latex-and-special-faces)
  4197. ;; Code
  4198. '(org-activate-code (1 'org-code t))
  4199. ;; COMMENT
  4200. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4201. "\\|" org-quote-string "\\)\\>")
  4202. '(1 'org-special-keyword t))
  4203. '("^#.*" (0 'font-lock-comment-face t))
  4204. ;; Blocks and meta lines
  4205. '(org-fontify-meta-lines-and-blocks)
  4206. )))
  4207. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4208. ;; Now set the full font-lock-keywords
  4209. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4210. (org-set-local 'font-lock-defaults
  4211. '(org-font-lock-keywords t nil nil backward-paragraph))
  4212. (kill-local-variable 'font-lock-keywords) nil))
  4213. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4214. "Fontify string S like in Org-mode"
  4215. (with-temp-buffer
  4216. (insert s)
  4217. (let ((org-odd-levels-only odd-levels))
  4218. (org-mode)
  4219. (font-lock-fontify-buffer)
  4220. (buffer-string))))
  4221. (defvar org-m nil)
  4222. (defvar org-l nil)
  4223. (defvar org-f nil)
  4224. (defun org-get-level-face (n)
  4225. "Get the right face for match N in font-lock matching of headlines."
  4226. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4227. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4228. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4229. (cond
  4230. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4231. ((eq n 2) org-f)
  4232. (t (if org-level-color-stars-only nil org-f))))
  4233. (defun org-get-todo-face (kwd)
  4234. "Get the right face for a TODO keyword KWD.
  4235. If KWD is a number, get the corresponding match group."
  4236. (if (numberp kwd) (setq kwd (match-string kwd)))
  4237. (or (cdr (assoc kwd org-todo-keyword-faces))
  4238. (and (member kwd org-done-keywords) 'org-done)
  4239. 'org-todo))
  4240. (defun org-font-lock-add-tag-faces (limit)
  4241. "Add the special tag faces."
  4242. (when (and org-tag-faces org-tags-special-faces-re)
  4243. (while (re-search-forward org-tags-special-faces-re limit t)
  4244. (add-text-properties (match-beginning 1) (match-end 1)
  4245. (list 'face (org-get-tag-face 1)
  4246. 'font-lock-fontified t))
  4247. (backward-char 1))))
  4248. (defun org-font-lock-add-priority-faces (limit)
  4249. "Add the special priority faces."
  4250. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4251. (add-text-properties
  4252. (match-beginning 0) (match-end 0)
  4253. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4254. org-priority-faces))
  4255. 'org-special-keyword)
  4256. 'font-lock-fontified t))))
  4257. (defun org-get-tag-face (kwd)
  4258. "Get the right face for a TODO keyword KWD.
  4259. If KWD is a number, get the corresponding match group."
  4260. (if (numberp kwd) (setq kwd (match-string kwd)))
  4261. (or (cdr (assoc kwd org-tag-faces))
  4262. 'org-tag))
  4263. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4264. "Remove fontification and activation overlays from links."
  4265. (font-lock-default-unfontify-region beg end)
  4266. (let* ((buffer-undo-list t)
  4267. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4268. (inhibit-modification-hooks t)
  4269. deactivate-mark buffer-file-name buffer-file-truename)
  4270. (remove-text-properties beg end
  4271. '(mouse-face t keymap t org-linked-text t
  4272. invisible t intangible t
  4273. line-prefix t wrap-prefix t
  4274. org-no-flyspell t))))
  4275. ;;;; Visibility cycling, including org-goto and indirect buffer
  4276. ;;; Cycling
  4277. (defvar org-cycle-global-status nil)
  4278. (make-variable-buffer-local 'org-cycle-global-status)
  4279. (defvar org-cycle-subtree-status nil)
  4280. (make-variable-buffer-local 'org-cycle-subtree-status)
  4281. ;;;###autoload
  4282. (defvar org-inlinetask-min-level)
  4283. (defun org-cycle (&optional arg)
  4284. "TAB-action and visibility cycling for Org-mode.
  4285. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4286. is outine visibility cycling, but it also invokes other actions
  4287. in special contexts.
  4288. - When this function is called with a prefix argument, rotate the entire
  4289. buffer through 3 states (global cycling)
  4290. 1. OVERVIEW: Show only top-level headlines.
  4291. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4292. 3. SHOW ALL: Show everything.
  4293. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4294. determined by the variable `org-startup-folded', and by any VISIBILITY
  4295. properties in the buffer.
  4296. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4297. including any drawers.
  4298. - When inside a table, re-align the table and move to the next field.
  4299. - When point is at the beginning of a headline, rotate the subtree started
  4300. by this line through 3 different states (local cycling)
  4301. 1. FOLDED: Only the main headline is shown.
  4302. 2. CHILDREN: The main headline and the direct children are shown.
  4303. From this state, you can move to one of the children
  4304. and zoom in further.
  4305. 3. SUBTREE: Show the entire subtree, including body text.
  4306. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4307. - When there is a numeric prefix, go up to a heading with level ARG, do
  4308. a `show-subtree' and return to the previous cursor position. If ARG
  4309. is negative, go up that many levels.
  4310. - When point is not at the beginning of a headline, execute the global
  4311. binding for TAB, which is re-indenting the line. See the option
  4312. `org-cycle-emulate-tab' for details.
  4313. - Special case: if point is at the beginning of the buffer and there is
  4314. no headline in line 1, this function will act as if called with prefix arg.
  4315. But only if also the variable `org-cycle-global-at-bob' is t."
  4316. (interactive "P")
  4317. (org-load-modules-maybe)
  4318. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4319. (let* ((limit-level
  4320. (or org-cycle-max-level
  4321. (and (boundp 'org-inlinetask-min-level)
  4322. org-inlinetask-min-level
  4323. (1- org-inlinetask-min-level))))
  4324. (nstars (and limit-level
  4325. (if org-odd-levels-only
  4326. (and limit-level (1- (* limit-level 2)))
  4327. limit-level)))
  4328. (outline-regexp
  4329. (cond
  4330. ((not (org-mode-p)) outline-regexp)
  4331. ((or (eq org-cycle-include-plain-lists 'integrate)
  4332. (and org-cycle-include-plain-lists (org-at-item-p)))
  4333. (concat "\\(?:\\*"
  4334. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4335. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4336. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4337. (bob-special (and org-cycle-global-at-bob (bobp)
  4338. (not (looking-at outline-regexp))))
  4339. (org-cycle-hook
  4340. (if bob-special
  4341. (delq 'org-optimize-window-after-visibility-change
  4342. (copy-sequence org-cycle-hook))
  4343. org-cycle-hook))
  4344. (pos (point)))
  4345. (if (or bob-special (equal arg '(4)))
  4346. ;; special case: use global cycling
  4347. (setq arg t))
  4348. (cond
  4349. ((equal arg '(16))
  4350. (org-set-startup-visibility)
  4351. (message "Startup visibility, plus VISIBILITY properties"))
  4352. ((equal arg '(64))
  4353. (show-all)
  4354. (message "Entire buffer visible, including drawers"))
  4355. ((org-at-table-p 'any)
  4356. ;; Enter the table or move to the next field in the table
  4357. (or (org-table-recognize-table.el)
  4358. (progn
  4359. (if arg (org-table-edit-field t)
  4360. (org-table-justify-field-maybe)
  4361. (call-interactively 'org-table-next-field)))))
  4362. ((run-hook-with-args-until-success
  4363. 'org-tab-after-check-for-table-hook))
  4364. ((eq arg t) ;; Global cycling
  4365. (org-cycle-internal-global))
  4366. ((and org-drawers org-drawer-regexp
  4367. (save-excursion
  4368. (beginning-of-line 1)
  4369. (looking-at org-drawer-regexp)))
  4370. ;; Toggle block visibility
  4371. (org-flag-drawer
  4372. (not (get-char-property (match-end 0) 'invisible))))
  4373. ((integerp arg)
  4374. ;; Show-subtree, ARG levels up from here.
  4375. (save-excursion
  4376. (org-back-to-heading)
  4377. (outline-up-heading (if (< arg 0) (- arg)
  4378. (- (funcall outline-level) arg)))
  4379. (org-show-subtree)))
  4380. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4381. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4382. (org-cycle-internal-local))
  4383. ;; TAB emulation and template completion
  4384. (buffer-read-only (org-back-to-heading))
  4385. ((run-hook-with-args-until-success
  4386. 'org-tab-after-check-for-cycling-hook))
  4387. ((org-try-structure-completion))
  4388. ((org-try-cdlatex-tab))
  4389. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4390. (or (not (bolp))
  4391. (not (looking-at outline-regexp))))
  4392. (call-interactively (global-key-binding "\t")))
  4393. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4394. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4395. (or (and (eq org-cycle-emulate-tab 'white)
  4396. (= (match-end 0) (point-at-eol)))
  4397. (and (eq org-cycle-emulate-tab 'whitestart)
  4398. (>= (match-end 0) pos))))
  4399. t
  4400. (eq org-cycle-emulate-tab t))
  4401. (call-interactively (global-key-binding "\t")))
  4402. (t (save-excursion
  4403. (org-back-to-heading)
  4404. (org-cycle)))))))
  4405. (defun org-cycle-internal-global ()
  4406. "Do the global cycling action."
  4407. (cond
  4408. ((and (eq last-command this-command)
  4409. (eq org-cycle-global-status 'overview))
  4410. ;; We just created the overview - now do table of contents
  4411. ;; This can be slow in very large buffers, so indicate action
  4412. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4413. (message "CONTENTS...")
  4414. (org-content)
  4415. (message "CONTENTS...done")
  4416. (setq org-cycle-global-status 'contents)
  4417. (run-hook-with-args 'org-cycle-hook 'contents))
  4418. ((and (eq last-command this-command)
  4419. (eq org-cycle-global-status 'contents))
  4420. ;; We just showed the table of contents - now show everything
  4421. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4422. (show-all)
  4423. (message "SHOW ALL")
  4424. (setq org-cycle-global-status 'all)
  4425. (run-hook-with-args 'org-cycle-hook 'all))
  4426. (t
  4427. ;; Default action: go to overview
  4428. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4429. (org-overview)
  4430. (message "OVERVIEW")
  4431. (setq org-cycle-global-status 'overview)
  4432. (run-hook-with-args 'org-cycle-hook 'overview))))
  4433. (defun org-cycle-internal-local ()
  4434. "Do the local cycling action."
  4435. (org-back-to-heading)
  4436. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4437. ;; First, some boundaries
  4438. (save-excursion
  4439. (org-back-to-heading)
  4440. (setq level (funcall outline-level))
  4441. (save-excursion
  4442. (beginning-of-line 2)
  4443. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4444. ; XEmacs does not have `next-single-char-property-change'
  4445. ; I'm not sure about Emacs 21.
  4446. (while (and (not (eobp)) ;; this is like `next-line'
  4447. (get-char-property (1- (point)) 'invisible))
  4448. (beginning-of-line 2))
  4449. (while (and (not (eobp)) ;; this is like `next-line'
  4450. (get-char-property (1- (point)) 'invisible))
  4451. (goto-char (next-single-char-property-change (point) 'invisible))
  4452. ;;;??? (or (bolp) (beginning-of-line 2))))
  4453. (and (eolp) (beginning-of-line 2))))
  4454. (setq eol (point)))
  4455. (outline-end-of-heading) (setq eoh (point))
  4456. (save-excursion
  4457. (outline-next-heading)
  4458. (setq has-children (and (org-at-heading-p t)
  4459. (> (funcall outline-level) level))))
  4460. (org-end-of-subtree t)
  4461. (unless (eobp)
  4462. (skip-chars-forward " \t\n")
  4463. (beginning-of-line 1) ; in case this is an item
  4464. )
  4465. (setq eos (if (eobp) (point) (1- (point)))))
  4466. ;; Find out what to do next and set `this-command'
  4467. (cond
  4468. ((= eos eoh)
  4469. ;; Nothing is hidden behind this heading
  4470. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4471. (message "EMPTY ENTRY")
  4472. (setq org-cycle-subtree-status nil)
  4473. (save-excursion
  4474. (goto-char eos)
  4475. (outline-next-heading)
  4476. (if (org-invisible-p) (org-flag-heading nil))))
  4477. ((and (or (>= eol eos)
  4478. (not (string-match "\\S-" (buffer-substring eol eos))))
  4479. (or has-children
  4480. (not (setq children-skipped
  4481. org-cycle-skip-children-state-if-no-children))))
  4482. ;; Entire subtree is hidden in one line: children view
  4483. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4484. (org-show-entry)
  4485. (show-children)
  4486. (message "CHILDREN")
  4487. (save-excursion
  4488. (goto-char eos)
  4489. (outline-next-heading)
  4490. (if (org-invisible-p) (org-flag-heading nil)))
  4491. (setq org-cycle-subtree-status 'children)
  4492. (run-hook-with-args 'org-cycle-hook 'children))
  4493. ((or children-skipped
  4494. (and (eq last-command this-command)
  4495. (eq org-cycle-subtree-status 'children)))
  4496. ;; We just showed the children, or no children are there,
  4497. ;; now show everything.
  4498. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4499. (org-show-subtree)
  4500. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4501. (setq org-cycle-subtree-status 'subtree)
  4502. (run-hook-with-args 'org-cycle-hook 'subtree))
  4503. (t
  4504. ;; Default action: hide the subtree.
  4505. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4506. (hide-subtree)
  4507. (message "FOLDED")
  4508. (setq org-cycle-subtree-status 'folded)
  4509. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4510. ;;;###autoload
  4511. (defun org-global-cycle (&optional arg)
  4512. "Cycle the global visibility. For details see `org-cycle'.
  4513. With C-u prefix arg, switch to startup visibility.
  4514. With a numeric prefix, show all headlines up to that level."
  4515. (interactive "P")
  4516. (let ((org-cycle-include-plain-lists
  4517. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4518. (cond
  4519. ((integerp arg)
  4520. (show-all)
  4521. (hide-sublevels arg)
  4522. (setq org-cycle-global-status 'contents))
  4523. ((equal arg '(4))
  4524. (org-set-startup-visibility)
  4525. (message "Startup visibility, plus VISIBILITY properties."))
  4526. (t
  4527. (org-cycle '(4))))))
  4528. (defun org-set-startup-visibility ()
  4529. "Set the visibility required by startup options and properties."
  4530. (cond
  4531. ((eq org-startup-folded t)
  4532. (org-cycle '(4)))
  4533. ((eq org-startup-folded 'content)
  4534. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4535. (org-cycle '(4)) (org-cycle '(4)))))
  4536. (if org-hide-block-startup (org-hide-block-all))
  4537. (org-set-visibility-according-to-property 'no-cleanup)
  4538. (org-cycle-hide-archived-subtrees 'all)
  4539. (org-cycle-hide-drawers 'all)
  4540. (org-cycle-show-empty-lines 'all))
  4541. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4542. "Switch subtree visibilities according to :VISIBILITY: property."
  4543. (interactive)
  4544. (let (org-show-entry-below state)
  4545. (save-excursion
  4546. (goto-char (point-min))
  4547. (while (re-search-forward
  4548. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4549. nil t)
  4550. (setq state (match-string 1))
  4551. (save-excursion
  4552. (org-back-to-heading t)
  4553. (hide-subtree)
  4554. (org-reveal)
  4555. (cond
  4556. ((equal state '("fold" "folded"))
  4557. (hide-subtree))
  4558. ((equal state "children")
  4559. (org-show-hidden-entry)
  4560. (show-children))
  4561. ((equal state "content")
  4562. (save-excursion
  4563. (save-restriction
  4564. (org-narrow-to-subtree)
  4565. (org-content))))
  4566. ((member state '("all" "showall"))
  4567. (show-subtree)))))
  4568. (unless no-cleanup
  4569. (org-cycle-hide-archived-subtrees 'all)
  4570. (org-cycle-hide-drawers 'all)
  4571. (org-cycle-show-empty-lines 'all)))))
  4572. (defun org-overview ()
  4573. "Switch to overview mode, showing only top-level headlines.
  4574. Really, this shows all headlines with level equal or greater than the level
  4575. of the first headline in the buffer. This is important, because if the
  4576. first headline is not level one, then (hide-sublevels 1) gives confusing
  4577. results."
  4578. (interactive)
  4579. (let ((level (save-excursion
  4580. (goto-char (point-min))
  4581. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4582. (progn
  4583. (goto-char (match-beginning 0))
  4584. (funcall outline-level))))))
  4585. (and level (hide-sublevels level))))
  4586. (defun org-content (&optional arg)
  4587. "Show all headlines in the buffer, like a table of contents.
  4588. With numerical argument N, show content up to level N."
  4589. (interactive "P")
  4590. (save-excursion
  4591. ;; Visit all headings and show their offspring
  4592. (and (integerp arg) (org-overview))
  4593. (goto-char (point-max))
  4594. (catch 'exit
  4595. (while (and (progn (condition-case nil
  4596. (outline-previous-visible-heading 1)
  4597. (error (goto-char (point-min))))
  4598. t)
  4599. (looking-at outline-regexp))
  4600. (if (integerp arg)
  4601. (show-children (1- arg))
  4602. (show-branches))
  4603. (if (bobp) (throw 'exit nil))))))
  4604. (defun org-optimize-window-after-visibility-change (state)
  4605. "Adjust the window after a change in outline visibility.
  4606. This function is the default value of the hook `org-cycle-hook'."
  4607. (when (get-buffer-window (current-buffer))
  4608. (cond
  4609. ((eq state 'content) nil)
  4610. ((eq state 'all) nil)
  4611. ((eq state 'folded) nil)
  4612. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4613. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4614. ;; FIXME: no longer in use
  4615. (defun org-compact-display-after-subtree-move ()
  4616. "Show a compacter version of the tree of the entry's parent."
  4617. (save-excursion
  4618. (if (org-up-heading-safe)
  4619. (progn
  4620. (hide-subtree)
  4621. (show-entry)
  4622. (show-children)
  4623. (org-cycle-show-empty-lines 'children)
  4624. (org-cycle-hide-drawers 'children))
  4625. (org-overview))))
  4626. (defun org-remove-empty-overlays-at (pos)
  4627. "Remove outline overlays that do not contain non-white stuff."
  4628. (mapc
  4629. (lambda (o)
  4630. (and (eq 'outline (org-overlay-get o 'invisible))
  4631. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4632. (org-overlay-end o))))
  4633. (org-delete-overlay o)))
  4634. (org-overlays-at pos)))
  4635. (defun org-clean-visibility-after-subtree-move ()
  4636. "Fix visibility issues after moving a subtree."
  4637. ;; First, find a reasonable region to look at:
  4638. ;; Start two siblings above, end three below
  4639. (let* ((beg (save-excursion
  4640. (and (org-get-last-sibling)
  4641. (org-get-last-sibling))
  4642. (point)))
  4643. (end (save-excursion
  4644. (and (org-get-next-sibling)
  4645. (org-get-next-sibling)
  4646. (org-get-next-sibling))
  4647. (if (org-at-heading-p)
  4648. (point-at-eol)
  4649. (point))))
  4650. (level (looking-at "\\*+"))
  4651. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4652. (save-excursion
  4653. (save-restriction
  4654. (narrow-to-region beg end)
  4655. (when re
  4656. ;; Properly fold already folded siblings
  4657. (goto-char (point-min))
  4658. (while (re-search-forward re nil t)
  4659. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4660. (hide-entry))))
  4661. (org-cycle-show-empty-lines 'overview)
  4662. (org-cycle-hide-drawers 'overview)))))
  4663. (defun org-cycle-show-empty-lines (state)
  4664. "Show empty lines above all visible headlines.
  4665. The region to be covered depends on STATE when called through
  4666. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4667. entire buffer covered. Note that an empty line is only shown if there
  4668. are at least `org-cycle-separator-lines' empty lines before the headline."
  4669. (when (not (= org-cycle-separator-lines 0))
  4670. (save-excursion
  4671. (let* ((n (abs org-cycle-separator-lines))
  4672. (re (cond
  4673. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4674. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4675. (t (let ((ns (number-to-string (- n 2))))
  4676. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4677. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4678. beg end b e)
  4679. (cond
  4680. ((memq state '(overview contents t))
  4681. (setq beg (point-min) end (point-max)))
  4682. ((memq state '(children folded))
  4683. (setq beg (point) end (progn (org-end-of-subtree t t)
  4684. (beginning-of-line 2)
  4685. (point)))))
  4686. (when beg
  4687. (goto-char beg)
  4688. (while (re-search-forward re end t)
  4689. (unless (get-char-property (match-end 1) 'invisible)
  4690. (setq e (match-end 1))
  4691. (if (< org-cycle-separator-lines 0)
  4692. (setq b (save-excursion
  4693. (goto-char (match-beginning 0))
  4694. (org-back-over-empty-lines)
  4695. (if (save-excursion
  4696. (goto-char (max (point-min) (1- (point))))
  4697. (org-on-heading-p))
  4698. (1- (point))
  4699. (point))))
  4700. (setq b (match-beginning 1)))
  4701. (outline-flag-region b e nil)))))))
  4702. ;; Never hide empty lines at the end of the file.
  4703. (save-excursion
  4704. (goto-char (point-max))
  4705. (outline-previous-heading)
  4706. (outline-end-of-heading)
  4707. (if (and (looking-at "[ \t\n]+")
  4708. (= (match-end 0) (point-max)))
  4709. (outline-flag-region (point) (match-end 0) nil))))
  4710. (defun org-show-empty-lines-in-parent ()
  4711. "Move to the parent and re-show empty lines before visible headlines."
  4712. (save-excursion
  4713. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4714. (org-cycle-show-empty-lines context))))
  4715. (defun org-cycle-hide-drawers (state)
  4716. "Re-hide all drawers after a visibility state change."
  4717. (when (and (org-mode-p)
  4718. (not (memq state '(overview folded contents))))
  4719. (save-excursion
  4720. (let* ((globalp (memq state '(contents all)))
  4721. (beg (if globalp (point-min) (point)))
  4722. (end (if globalp (point-max)
  4723. (if (eq state 'children)
  4724. (save-excursion (outline-next-heading) (point))
  4725. (org-end-of-subtree t)))))
  4726. (goto-char beg)
  4727. (while (re-search-forward org-drawer-regexp end t)
  4728. (org-flag-drawer t))))))
  4729. (defun org-flag-drawer (flag)
  4730. (save-excursion
  4731. (beginning-of-line 1)
  4732. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4733. (let ((b (match-end 0))
  4734. (outline-regexp org-outline-regexp))
  4735. (if (re-search-forward
  4736. "^[ \t]*:END:"
  4737. (save-excursion (outline-next-heading) (point)) t)
  4738. (outline-flag-region b (point-at-eol) flag)
  4739. (error ":END: line missing at position %s" b))))))
  4740. (defun org-subtree-end-visible-p ()
  4741. "Is the end of the current subtree visible?"
  4742. (pos-visible-in-window-p
  4743. (save-excursion (org-end-of-subtree t) (point))))
  4744. (defun org-first-headline-recenter (&optional N)
  4745. "Move cursor to the first headline and recenter the headline.
  4746. Optional argument N means, put the headline into the Nth line of the window."
  4747. (goto-char (point-min))
  4748. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4749. (beginning-of-line)
  4750. (recenter (prefix-numeric-value N))))
  4751. ;;; Folding of blocks
  4752. (defconst org-block-regexp
  4753. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4754. "Regular expression for hiding blocks.")
  4755. (defvar org-hide-block-overlays nil
  4756. "Overays hiding blocks.")
  4757. (make-variable-buffer-local 'org-hide-block-overlays)
  4758. (defun org-block-map (function &optional start end)
  4759. "Call func at the head of all source blocks in the current
  4760. buffer. Optional arguments START and END can be used to limit
  4761. the range."
  4762. (let ((start (or start (point-min)))
  4763. (end (or end (point-max))))
  4764. (save-excursion
  4765. (goto-char start)
  4766. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4767. (save-excursion
  4768. (save-match-data
  4769. (goto-char (match-beginning 0))
  4770. (funcall function)))))))
  4771. (defun org-hide-block-toggle-all ()
  4772. "Toggle the visibility of all blocks in the current buffer."
  4773. (org-block-map #'org-hide-block-toggle))
  4774. (defun org-hide-block-all ()
  4775. "Fold all blocks in the current buffer."
  4776. (interactive)
  4777. (org-show-block-all)
  4778. (org-block-map #'org-hide-block-toggle-maybe))
  4779. (defun org-show-block-all ()
  4780. "Unfold all blocks in the current buffer."
  4781. (mapc 'org-delete-overlay org-hide-block-overlays)
  4782. (setq org-hide-block-overlays nil))
  4783. (defun org-hide-block-toggle-maybe ()
  4784. "Toggle visibility of block at point."
  4785. (interactive)
  4786. (let ((case-fold-search t))
  4787. (if (save-excursion
  4788. (beginning-of-line 1)
  4789. (looking-at org-block-regexp))
  4790. (progn (org-hide-block-toggle)
  4791. t) ;; to signal that we took action
  4792. nil))) ;; to signal that we did not
  4793. (defun org-hide-block-toggle (&optional force)
  4794. "Toggle the visibility of the current block."
  4795. (interactive)
  4796. (save-excursion
  4797. (beginning-of-line)
  4798. (if (re-search-forward org-block-regexp nil t)
  4799. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4800. (end (match-end 0)) ;; end of entire body
  4801. ov)
  4802. (if (memq t (mapcar (lambda (overlay)
  4803. (eq (org-overlay-get overlay 'invisible)
  4804. 'org-hide-block))
  4805. (org-overlays-at start)))
  4806. (if (or (not force) (eq force 'off))
  4807. (mapc (lambda (ov)
  4808. (when (member ov org-hide-block-overlays)
  4809. (setq org-hide-block-overlays
  4810. (delq ov org-hide-block-overlays)))
  4811. (when (eq (org-overlay-get ov 'invisible)
  4812. 'org-hide-block)
  4813. (org-delete-overlay ov)))
  4814. (org-overlays-at start)))
  4815. (setq ov (org-make-overlay start end))
  4816. (org-overlay-put ov 'invisible 'org-hide-block)
  4817. ;; make the block accessible to isearch
  4818. (org-overlay-put
  4819. ov 'isearch-open-invisible
  4820. (lambda (ov)
  4821. (when (member ov org-hide-block-overlays)
  4822. (setq org-hide-block-overlays
  4823. (delq ov org-hide-block-overlays)))
  4824. (when (eq (org-overlay-get ov 'invisible)
  4825. 'org-hide-block)
  4826. (org-delete-overlay ov))))
  4827. (push ov org-hide-block-overlays)))
  4828. (error "Not looking at a source block"))))
  4829. ;; org-tab-after-check-for-cycling-hook
  4830. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4831. ;; Remove overlays when changing major mode
  4832. (add-hook 'org-mode-hook
  4833. (lambda () (org-add-hook 'change-major-mode-hook
  4834. 'org-show-block-all 'append 'local)))
  4835. ;;; Org-goto
  4836. (defvar org-goto-window-configuration nil)
  4837. (defvar org-goto-marker nil)
  4838. (defvar org-goto-map
  4839. (let ((map (make-sparse-keymap)))
  4840. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4841. (while (setq cmd (pop cmds))
  4842. (substitute-key-definition cmd cmd map global-map)))
  4843. (suppress-keymap map)
  4844. (org-defkey map "\C-m" 'org-goto-ret)
  4845. (org-defkey map [(return)] 'org-goto-ret)
  4846. (org-defkey map [(left)] 'org-goto-left)
  4847. (org-defkey map [(right)] 'org-goto-right)
  4848. (org-defkey map [(control ?g)] 'org-goto-quit)
  4849. (org-defkey map "\C-i" 'org-cycle)
  4850. (org-defkey map [(tab)] 'org-cycle)
  4851. (org-defkey map [(down)] 'outline-next-visible-heading)
  4852. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4853. (if org-goto-auto-isearch
  4854. (if (fboundp 'define-key-after)
  4855. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4856. nil)
  4857. (org-defkey map "q" 'org-goto-quit)
  4858. (org-defkey map "n" 'outline-next-visible-heading)
  4859. (org-defkey map "p" 'outline-previous-visible-heading)
  4860. (org-defkey map "f" 'outline-forward-same-level)
  4861. (org-defkey map "b" 'outline-backward-same-level)
  4862. (org-defkey map "u" 'outline-up-heading))
  4863. (org-defkey map "/" 'org-occur)
  4864. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4865. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4866. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4867. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4868. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4869. map))
  4870. (defconst org-goto-help
  4871. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4872. RET=jump to location [Q]uit and return to previous location
  4873. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4874. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4875. ;; FIXME: Docstring doe not mention both interfaces
  4876. (defun org-goto (&optional alternative-interface)
  4877. "Look up a different location in the current file, keeping current visibility.
  4878. When you want look-up or go to a different location in a document, the
  4879. fastest way is often to fold the entire buffer and then dive into the tree.
  4880. This method has the disadvantage, that the previous location will be folded,
  4881. which may not be what you want.
  4882. This command works around this by showing a copy of the current buffer
  4883. in an indirect buffer, in overview mode. You can dive into the tree in
  4884. that copy, use org-occur and incremental search to find a location.
  4885. When pressing RET or `Q', the command returns to the original buffer in
  4886. which the visibility is still unchanged. After RET is will also jump to
  4887. the location selected in the indirect buffer and expose the
  4888. the headline hierarchy above."
  4889. (interactive "P")
  4890. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4891. (org-refile-use-outline-path t)
  4892. (org-refile-target-verify-function nil)
  4893. (interface
  4894. (if (not alternative-interface)
  4895. org-goto-interface
  4896. (if (eq org-goto-interface 'outline)
  4897. 'outline-path-completion
  4898. 'outline)))
  4899. (org-goto-start-pos (point))
  4900. (selected-point
  4901. (if (eq interface 'outline)
  4902. (car (org-get-location (current-buffer) org-goto-help))
  4903. (nth 3 (org-refile-get-location "Goto: ")))))
  4904. (if selected-point
  4905. (progn
  4906. (org-mark-ring-push org-goto-start-pos)
  4907. (goto-char selected-point)
  4908. (if (or (org-invisible-p) (org-invisible-p2))
  4909. (org-show-context 'org-goto)))
  4910. (message "Quit"))))
  4911. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4912. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4913. (defvar org-goto-local-auto-isearch-map) ; defined below
  4914. (defun org-get-location (buf help)
  4915. "Let the user select a location in the Org-mode buffer BUF.
  4916. This function uses a recursive edit. It returns the selected position
  4917. or nil."
  4918. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4919. (isearch-hide-immediately nil)
  4920. (isearch-search-fun-function
  4921. (lambda () 'org-goto-local-search-headings))
  4922. (org-goto-selected-point org-goto-exit-command))
  4923. (save-excursion
  4924. (save-window-excursion
  4925. (delete-other-windows)
  4926. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4927. (switch-to-buffer
  4928. (condition-case nil
  4929. (make-indirect-buffer (current-buffer) "*org-goto*")
  4930. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4931. (with-output-to-temp-buffer "*Help*"
  4932. (princ help))
  4933. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4934. (setq buffer-read-only nil)
  4935. (let ((org-startup-truncated t)
  4936. (org-startup-folded nil)
  4937. (org-startup-align-all-tables nil))
  4938. (org-mode)
  4939. (org-overview))
  4940. (setq buffer-read-only t)
  4941. (if (and (boundp 'org-goto-start-pos)
  4942. (integer-or-marker-p org-goto-start-pos))
  4943. (let ((org-show-hierarchy-above t)
  4944. (org-show-siblings t)
  4945. (org-show-following-heading t))
  4946. (goto-char org-goto-start-pos)
  4947. (and (org-invisible-p) (org-show-context)))
  4948. (goto-char (point-min)))
  4949. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4950. (message "Select location and press RET")
  4951. (use-local-map org-goto-map)
  4952. (recursive-edit)
  4953. ))
  4954. (kill-buffer "*org-goto*")
  4955. (cons org-goto-selected-point org-goto-exit-command)))
  4956. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4957. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4958. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4959. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4960. (defun org-goto-local-search-headings (string bound noerror)
  4961. "Search and make sure that any matches are in headlines."
  4962. (catch 'return
  4963. (while (if isearch-forward
  4964. (search-forward string bound noerror)
  4965. (search-backward string bound noerror))
  4966. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4967. (and (member :headline context)
  4968. (not (member :tags context))))
  4969. (throw 'return (point))))))
  4970. (defun org-goto-local-auto-isearch ()
  4971. "Start isearch."
  4972. (interactive)
  4973. (goto-char (point-min))
  4974. (let ((keys (this-command-keys)))
  4975. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4976. (isearch-mode t)
  4977. (isearch-process-search-char (string-to-char keys)))))
  4978. (defun org-goto-ret (&optional arg)
  4979. "Finish `org-goto' by going to the new location."
  4980. (interactive "P")
  4981. (setq org-goto-selected-point (point)
  4982. org-goto-exit-command 'return)
  4983. (throw 'exit nil))
  4984. (defun org-goto-left ()
  4985. "Finish `org-goto' by going to the new location."
  4986. (interactive)
  4987. (if (org-on-heading-p)
  4988. (progn
  4989. (beginning-of-line 1)
  4990. (setq org-goto-selected-point (point)
  4991. org-goto-exit-command 'left)
  4992. (throw 'exit nil))
  4993. (error "Not on a heading")))
  4994. (defun org-goto-right ()
  4995. "Finish `org-goto' by going to the new location."
  4996. (interactive)
  4997. (if (org-on-heading-p)
  4998. (progn
  4999. (setq org-goto-selected-point (point)
  5000. org-goto-exit-command 'right)
  5001. (throw 'exit nil))
  5002. (error "Not on a heading")))
  5003. (defun org-goto-quit ()
  5004. "Finish `org-goto' without cursor motion."
  5005. (interactive)
  5006. (setq org-goto-selected-point nil)
  5007. (setq org-goto-exit-command 'quit)
  5008. (throw 'exit nil))
  5009. ;;; Indirect buffer display of subtrees
  5010. (defvar org-indirect-dedicated-frame nil
  5011. "This is the frame being used for indirect tree display.")
  5012. (defvar org-last-indirect-buffer nil)
  5013. (defun org-tree-to-indirect-buffer (&optional arg)
  5014. "Create indirect buffer and narrow it to current subtree.
  5015. With numerical prefix ARG, go up to this level and then take that tree.
  5016. If ARG is negative, go up that many levels.
  5017. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5018. indirect buffer previously made with this command, to avoid proliferation of
  5019. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5020. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5021. is kept so that you can work with several indirect buffers at the same time.
  5022. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5023. requests that a new frame be made for the new buffer, so that the dedicated
  5024. frame is not changed."
  5025. (interactive "P")
  5026. (let ((cbuf (current-buffer))
  5027. (cwin (selected-window))
  5028. (pos (point))
  5029. beg end level heading ibuf)
  5030. (save-excursion
  5031. (org-back-to-heading t)
  5032. (when (numberp arg)
  5033. (setq level (org-outline-level))
  5034. (if (< arg 0) (setq arg (+ level arg)))
  5035. (while (> (setq level (org-outline-level)) arg)
  5036. (outline-up-heading 1 t)))
  5037. (setq beg (point)
  5038. heading (org-get-heading))
  5039. (org-end-of-subtree t) (setq end (point)))
  5040. (if (and (buffer-live-p org-last-indirect-buffer)
  5041. (not (eq org-indirect-buffer-display 'new-frame))
  5042. (not arg))
  5043. (kill-buffer org-last-indirect-buffer))
  5044. (setq ibuf (org-get-indirect-buffer cbuf)
  5045. org-last-indirect-buffer ibuf)
  5046. (cond
  5047. ((or (eq org-indirect-buffer-display 'new-frame)
  5048. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5049. (select-frame (make-frame))
  5050. (delete-other-windows)
  5051. (switch-to-buffer ibuf)
  5052. (org-set-frame-title heading))
  5053. ((eq org-indirect-buffer-display 'dedicated-frame)
  5054. (raise-frame
  5055. (select-frame (or (and org-indirect-dedicated-frame
  5056. (frame-live-p org-indirect-dedicated-frame)
  5057. org-indirect-dedicated-frame)
  5058. (setq org-indirect-dedicated-frame (make-frame)))))
  5059. (delete-other-windows)
  5060. (switch-to-buffer ibuf)
  5061. (org-set-frame-title (concat "Indirect: " heading)))
  5062. ((eq org-indirect-buffer-display 'current-window)
  5063. (switch-to-buffer ibuf))
  5064. ((eq org-indirect-buffer-display 'other-window)
  5065. (pop-to-buffer ibuf))
  5066. (t (error "Invalid value.")))
  5067. (if (featurep 'xemacs)
  5068. (save-excursion (org-mode) (turn-on-font-lock)))
  5069. (narrow-to-region beg end)
  5070. (show-all)
  5071. (goto-char pos)
  5072. (and (window-live-p cwin) (select-window cwin))))
  5073. (defun org-get-indirect-buffer (&optional buffer)
  5074. (setq buffer (or buffer (current-buffer)))
  5075. (let ((n 1) (base (buffer-name buffer)) bname)
  5076. (while (buffer-live-p
  5077. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5078. (setq n (1+ n)))
  5079. (condition-case nil
  5080. (make-indirect-buffer buffer bname 'clone)
  5081. (error (make-indirect-buffer buffer bname)))))
  5082. (defun org-set-frame-title (title)
  5083. "Set the title of the current frame to the string TITLE."
  5084. ;; FIXME: how to name a single frame in XEmacs???
  5085. (unless (featurep 'xemacs)
  5086. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5087. ;;;; Structure editing
  5088. ;;; Inserting headlines
  5089. (defun org-previous-line-empty-p ()
  5090. (save-excursion
  5091. (and (not (bobp))
  5092. (or (beginning-of-line 0) t)
  5093. (save-match-data
  5094. (looking-at "[ \t]*$")))))
  5095. (defun org-insert-heading (&optional force-heading)
  5096. "Insert a new heading or item with same depth at point.
  5097. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5098. If point is at the beginning of a headline, insert a sibling before the
  5099. current headline. If point is not at the beginning, do not split the line,
  5100. but create the new headline after the current line."
  5101. (interactive "P")
  5102. (if (= (buffer-size) 0)
  5103. (insert "\n* ")
  5104. (when (or force-heading (not (org-insert-item)))
  5105. (let* ((empty-line-p nil)
  5106. (head (save-excursion
  5107. (condition-case nil
  5108. (progn
  5109. (org-back-to-heading)
  5110. (setq empty-line-p (org-previous-line-empty-p))
  5111. (match-string 0))
  5112. (error "*"))))
  5113. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5114. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5115. pos hide-previous previous-pos)
  5116. (cond
  5117. ((and (org-on-heading-p) (bolp)
  5118. (or (bobp)
  5119. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5120. ;; insert before the current line
  5121. (open-line (if blank 2 1)))
  5122. ((and (bolp)
  5123. (not org-insert-heading-respect-content)
  5124. (or (bobp)
  5125. (save-excursion
  5126. (backward-char 1) (not (org-invisible-p)))))
  5127. ;; insert right here
  5128. nil)
  5129. (t
  5130. ;; somewhere in the line
  5131. (save-excursion
  5132. (setq previous-pos (point-at-bol))
  5133. (end-of-line)
  5134. (setq hide-previous (org-invisible-p)))
  5135. (and org-insert-heading-respect-content (org-show-subtree))
  5136. (let ((split
  5137. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5138. (save-excursion
  5139. (let ((p (point)))
  5140. (goto-char (point-at-bol))
  5141. (and (looking-at org-complex-heading-regexp)
  5142. (> p (match-beginning 4)))))))
  5143. tags pos)
  5144. (cond
  5145. (org-insert-heading-respect-content
  5146. (org-end-of-subtree nil t)
  5147. (or (bolp) (newline))
  5148. (or (org-previous-line-empty-p)
  5149. (and blank (newline)))
  5150. (open-line 1))
  5151. ((org-on-heading-p)
  5152. (when hide-previous
  5153. (show-children)
  5154. (org-show-entry))
  5155. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5156. (setq tags (and (match-end 2) (match-string 2)))
  5157. (and (match-end 1)
  5158. (delete-region (match-beginning 1) (match-end 1)))
  5159. (setq pos (point-at-bol))
  5160. (or split (end-of-line 1))
  5161. (delete-horizontal-space)
  5162. (newline (if blank 2 1))
  5163. (when tags
  5164. (save-excursion
  5165. (goto-char pos)
  5166. (end-of-line 1)
  5167. (insert " " tags)
  5168. (org-set-tags nil 'align))))
  5169. (t
  5170. (or split (end-of-line 1))
  5171. (newline (if blank 2 1)))))))
  5172. (insert head) (just-one-space)
  5173. (setq pos (point))
  5174. (end-of-line 1)
  5175. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5176. (when (and org-insert-heading-respect-content hide-previous)
  5177. (save-excursion
  5178. (goto-char previous-pos)
  5179. (hide-subtree)))
  5180. (run-hooks 'org-insert-heading-hook)))))
  5181. (defun org-get-heading (&optional no-tags)
  5182. "Return the heading of the current entry, without the stars."
  5183. (save-excursion
  5184. (org-back-to-heading t)
  5185. (if (looking-at
  5186. (if no-tags
  5187. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5188. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5189. (match-string 1) "")))
  5190. (defun org-heading-components ()
  5191. "Return the components of the current heading.
  5192. This is a list with the following elements:
  5193. - the level as an integer
  5194. - the reduced level, different if `org-odd-levels-only' is set.
  5195. - the TODO keyword, or nil
  5196. - the priority character, like ?A, or nil if no priority is given
  5197. - the headline text itself, or the tags string if no headline text
  5198. - the tags string, or nil."
  5199. (save-excursion
  5200. (org-back-to-heading t)
  5201. (if (looking-at org-complex-heading-regexp)
  5202. (list (length (match-string 1))
  5203. (org-reduced-level (length (match-string 1)))
  5204. (org-match-string-no-properties 2)
  5205. (and (match-end 3) (aref (match-string 3) 2))
  5206. (org-match-string-no-properties 4)
  5207. (org-match-string-no-properties 5)))))
  5208. (defun org-get-entry ()
  5209. "Get the entry text, after heading, entire subtree."
  5210. (save-excursion
  5211. (org-back-to-heading t)
  5212. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5213. (defun org-insert-heading-after-current ()
  5214. "Insert a new heading with same level as current, after current subtree."
  5215. (interactive)
  5216. (org-back-to-heading)
  5217. (org-insert-heading)
  5218. (org-move-subtree-down)
  5219. (end-of-line 1))
  5220. (defun org-insert-heading-respect-content ()
  5221. (interactive)
  5222. (let ((org-insert-heading-respect-content t))
  5223. (org-insert-heading t)))
  5224. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5225. (interactive "P")
  5226. (let ((org-insert-heading-respect-content t))
  5227. (org-insert-todo-heading force-state t)))
  5228. (defun org-insert-todo-heading (arg &optional force-heading)
  5229. "Insert a new heading with the same level and TODO state as current heading.
  5230. If the heading has no TODO state, or if the state is DONE, use the first
  5231. state (TODO by default). Also with prefix arg, force first state."
  5232. (interactive "P")
  5233. (when (or force-heading (not (org-insert-item 'checkbox)))
  5234. (org-insert-heading force-heading)
  5235. (save-excursion
  5236. (org-back-to-heading)
  5237. (outline-previous-heading)
  5238. (looking-at org-todo-line-regexp))
  5239. (let*
  5240. ((new-mark-x
  5241. (if (or arg
  5242. (not (match-beginning 2))
  5243. (member (match-string 2) org-done-keywords))
  5244. (car org-todo-keywords-1)
  5245. (match-string 2)))
  5246. (new-mark
  5247. (or
  5248. (run-hook-with-args-until-success
  5249. 'org-todo-get-default-hook new-mark-x nil)
  5250. new-mark-x)))
  5251. (beginning-of-line 1)
  5252. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5253. (if org-treat-insert-todo-heading-as-state-change
  5254. (org-todo new-mark)
  5255. (insert new-mark " "))))
  5256. (when org-provide-todo-statistics
  5257. (org-update-parent-todo-statistics))))
  5258. (defun org-insert-subheading (arg)
  5259. "Insert a new subheading and demote it.
  5260. Works for outline headings and for plain lists alike."
  5261. (interactive "P")
  5262. (org-insert-heading arg)
  5263. (cond
  5264. ((org-on-heading-p) (org-do-demote))
  5265. ((org-at-item-p) (org-indent-item 1))))
  5266. (defun org-insert-todo-subheading (arg)
  5267. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5268. Works for outline headings and for plain lists alike."
  5269. (interactive "P")
  5270. (org-insert-todo-heading arg)
  5271. (cond
  5272. ((org-on-heading-p) (org-do-demote))
  5273. ((org-at-item-p) (org-indent-item 1))))
  5274. ;;; Promotion and Demotion
  5275. (defvar org-after-demote-entry-hook nil
  5276. "Hook run after an entry has been demoted.
  5277. The cursor will be at the beginning of the entry.
  5278. When a subtree is being demoted, the hook will be called for each node.")
  5279. (defvar org-after-promote-entry-hook nil
  5280. "Hook run after an entry has been promoted.
  5281. The cursor will be at the beginning of the entry.
  5282. When a subtree is being promoted, the hook will be called for each node.")
  5283. (defun org-promote-subtree ()
  5284. "Promote the entire subtree.
  5285. See also `org-promote'."
  5286. (interactive)
  5287. (save-excursion
  5288. (org-map-tree 'org-promote))
  5289. (org-fix-position-after-promote))
  5290. (defun org-demote-subtree ()
  5291. "Demote the entire subtree. See `org-demote'.
  5292. See also `org-promote'."
  5293. (interactive)
  5294. (save-excursion
  5295. (org-map-tree 'org-demote))
  5296. (org-fix-position-after-promote))
  5297. (defun org-do-promote ()
  5298. "Promote the current heading higher up the tree.
  5299. If the region is active in `transient-mark-mode', promote all headings
  5300. in the region."
  5301. (interactive)
  5302. (save-excursion
  5303. (if (org-region-active-p)
  5304. (org-map-region 'org-promote (region-beginning) (region-end))
  5305. (org-promote)))
  5306. (org-fix-position-after-promote))
  5307. (defun org-do-demote ()
  5308. "Demote the current heading lower down the tree.
  5309. If the region is active in `transient-mark-mode', demote all headings
  5310. in the region."
  5311. (interactive)
  5312. (save-excursion
  5313. (if (org-region-active-p)
  5314. (org-map-region 'org-demote (region-beginning) (region-end))
  5315. (org-demote)))
  5316. (org-fix-position-after-promote))
  5317. (defun org-fix-position-after-promote ()
  5318. "Make sure that after pro/demotion cursor position is right."
  5319. (let ((pos (point)))
  5320. (when (save-excursion
  5321. (beginning-of-line 1)
  5322. (looking-at org-todo-line-regexp)
  5323. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5324. (cond ((eobp) (insert " "))
  5325. ((eolp) (insert " "))
  5326. ((equal (char-after) ?\ ) (forward-char 1))))))
  5327. (defun org-reduced-level (l)
  5328. "Compute the effective level of a heading.
  5329. This takes into account the setting of `org-odd-levels-only'."
  5330. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5331. (defun org-get-valid-level (level &optional change)
  5332. "Rectify a level change under the influence of `org-odd-levels-only'
  5333. LEVEL is a current level, CHANGE is by how much the level should be
  5334. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5335. even level numbers will become the next higher odd number."
  5336. (if org-odd-levels-only
  5337. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5338. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5339. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5340. (max 1 (+ level (or change 0)))))
  5341. (if (boundp 'define-obsolete-function-alias)
  5342. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5343. (define-obsolete-function-alias 'org-get-legal-level
  5344. 'org-get-valid-level)
  5345. (define-obsolete-function-alias 'org-get-legal-level
  5346. 'org-get-valid-level "23.1")))
  5347. (defun org-promote ()
  5348. "Promote the current heading higher up the tree.
  5349. If the region is active in `transient-mark-mode', promote all headings
  5350. in the region."
  5351. (org-back-to-heading t)
  5352. (let* ((level (save-match-data (funcall outline-level)))
  5353. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5354. (diff (abs (- level (length up-head) -1))))
  5355. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5356. (replace-match up-head nil t)
  5357. ;; Fixup tag positioning
  5358. (and org-auto-align-tags (org-set-tags nil t))
  5359. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5360. (run-hooks 'org-after-promote-entry-hook)))
  5361. (defun org-demote ()
  5362. "Demote the current heading lower down the tree.
  5363. If the region is active in `transient-mark-mode', demote all headings
  5364. in the region."
  5365. (org-back-to-heading t)
  5366. (let* ((level (save-match-data (funcall outline-level)))
  5367. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5368. (diff (abs (- level (length down-head) -1))))
  5369. (replace-match down-head nil t)
  5370. ;; Fixup tag positioning
  5371. (and org-auto-align-tags (org-set-tags nil t))
  5372. (if org-adapt-indentation (org-fixup-indentation diff))
  5373. (run-hooks 'org-after-demote-entry-hook)))
  5374. (defun org-map-tree (fun)
  5375. "Call FUN for every heading underneath the current one."
  5376. (org-back-to-heading)
  5377. (let ((level (funcall outline-level)))
  5378. (save-excursion
  5379. (funcall fun)
  5380. (while (and (progn
  5381. (outline-next-heading)
  5382. (> (funcall outline-level) level))
  5383. (not (eobp)))
  5384. (funcall fun)))))
  5385. (defun org-map-region (fun beg end)
  5386. "Call FUN for every heading between BEG and END."
  5387. (let ((org-ignore-region t))
  5388. (save-excursion
  5389. (setq end (copy-marker end))
  5390. (goto-char beg)
  5391. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5392. (< (point) end))
  5393. (funcall fun))
  5394. (while (and (progn
  5395. (outline-next-heading)
  5396. (< (point) end))
  5397. (not (eobp)))
  5398. (funcall fun)))))
  5399. (defun org-fixup-indentation (diff)
  5400. "Change the indentation in the current entry by DIFF
  5401. However, if any line in the current entry has no indentation, or if it
  5402. would end up with no indentation after the change, nothing at all is done."
  5403. (save-excursion
  5404. (let ((end (save-excursion (outline-next-heading)
  5405. (point-marker)))
  5406. (prohibit (if (> diff 0)
  5407. "^\\S-"
  5408. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5409. col)
  5410. (unless (save-excursion (end-of-line 1)
  5411. (re-search-forward prohibit end t))
  5412. (while (and (< (point) end)
  5413. (re-search-forward "^[ \t]+" end t))
  5414. (goto-char (match-end 0))
  5415. (setq col (current-column))
  5416. (if (< diff 0) (replace-match ""))
  5417. (org-indent-to-column (+ diff col))))
  5418. (move-marker end nil))))
  5419. (defun org-convert-to-odd-levels ()
  5420. "Convert an org-mode file with all levels allowed to one with odd levels.
  5421. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5422. level 5 etc."
  5423. (interactive)
  5424. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5425. (let ((outline-regexp org-outline-regexp)
  5426. (outline-level 'org-outline-level)
  5427. (org-odd-levels-only nil) n)
  5428. (save-excursion
  5429. (goto-char (point-min))
  5430. (while (re-search-forward "^\\*\\*+ " nil t)
  5431. (setq n (- (length (match-string 0)) 2))
  5432. (while (>= (setq n (1- n)) 0)
  5433. (org-demote))
  5434. (end-of-line 1))))))
  5435. (defun org-convert-to-oddeven-levels ()
  5436. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5437. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5438. section with an even level, conversion would destroy the structure of the file. An error
  5439. is signaled in this case."
  5440. (interactive)
  5441. (goto-char (point-min))
  5442. ;; First check if there are no even levels
  5443. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5444. (org-show-context t)
  5445. (error "Not all levels are odd in this file. Conversion not possible."))
  5446. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5447. (let ((outline-regexp org-outline-regexp)
  5448. (outline-level 'org-outline-level)
  5449. (org-odd-levels-only nil) n)
  5450. (save-excursion
  5451. (goto-char (point-min))
  5452. (while (re-search-forward "^\\*\\*+ " nil t)
  5453. (setq n (/ (1- (length (match-string 0))) 2))
  5454. (while (>= (setq n (1- n)) 0)
  5455. (org-promote))
  5456. (end-of-line 1))))))
  5457. (defun org-tr-level (n)
  5458. "Make N odd if required."
  5459. (if org-odd-levels-only (1+ (/ n 2)) n))
  5460. ;;; Vertical tree motion, cutting and pasting of subtrees
  5461. (defun org-move-subtree-up (&optional arg)
  5462. "Move the current subtree up past ARG headlines of the same level."
  5463. (interactive "p")
  5464. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5465. (defun org-move-subtree-down (&optional arg)
  5466. "Move the current subtree down past ARG headlines of the same level."
  5467. (interactive "p")
  5468. (setq arg (prefix-numeric-value arg))
  5469. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5470. 'org-get-last-sibling))
  5471. (ins-point (make-marker))
  5472. (cnt (abs arg))
  5473. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5474. ;; Select the tree
  5475. (org-back-to-heading)
  5476. (setq beg0 (point))
  5477. (save-excursion
  5478. (setq ne-beg (org-back-over-empty-lines))
  5479. (setq beg (point)))
  5480. (save-match-data
  5481. (save-excursion (outline-end-of-heading)
  5482. (setq folded (org-invisible-p)))
  5483. (outline-end-of-subtree))
  5484. (outline-next-heading)
  5485. (setq ne-end (org-back-over-empty-lines))
  5486. (setq end (point))
  5487. (goto-char beg0)
  5488. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5489. ;; include less whitespace
  5490. (save-excursion
  5491. (goto-char beg)
  5492. (forward-line (- ne-beg ne-end))
  5493. (setq beg (point))))
  5494. ;; Find insertion point, with error handling
  5495. (while (> cnt 0)
  5496. (or (and (funcall movfunc) (looking-at outline-regexp))
  5497. (progn (goto-char beg0)
  5498. (error "Cannot move past superior level or buffer limit")))
  5499. (setq cnt (1- cnt)))
  5500. (if (> arg 0)
  5501. ;; Moving forward - still need to move over subtree
  5502. (progn (org-end-of-subtree t t)
  5503. (save-excursion
  5504. (org-back-over-empty-lines)
  5505. (or (bolp) (newline)))))
  5506. (setq ne-ins (org-back-over-empty-lines))
  5507. (move-marker ins-point (point))
  5508. (setq txt (buffer-substring beg end))
  5509. (org-save-markers-in-region beg end)
  5510. (delete-region beg end)
  5511. (org-remove-empty-overlays-at beg)
  5512. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5513. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5514. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5515. (let ((bbb (point)))
  5516. (insert-before-markers txt)
  5517. (org-reinstall-markers-in-region bbb)
  5518. (move-marker ins-point bbb))
  5519. (or (bolp) (insert "\n"))
  5520. (setq ins-end (point))
  5521. (goto-char ins-point)
  5522. (org-skip-whitespace)
  5523. (when (and (< arg 0)
  5524. (org-first-sibling-p)
  5525. (> ne-ins ne-beg))
  5526. ;; Move whitespace back to beginning
  5527. (save-excursion
  5528. (goto-char ins-end)
  5529. (let ((kill-whole-line t))
  5530. (kill-line (- ne-ins ne-beg)) (point)))
  5531. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5532. (move-marker ins-point nil)
  5533. (if folded
  5534. (hide-subtree)
  5535. (org-show-entry)
  5536. (show-children)
  5537. (org-cycle-hide-drawers 'children))
  5538. (org-clean-visibility-after-subtree-move)))
  5539. (defvar org-subtree-clip ""
  5540. "Clipboard for cut and paste of subtrees.
  5541. This is actually only a copy of the kill, because we use the normal kill
  5542. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5543. (defvar org-subtree-clip-folded nil
  5544. "Was the last copied subtree folded?
  5545. This is used to fold the tree back after pasting.")
  5546. (defun org-cut-subtree (&optional n)
  5547. "Cut the current subtree into the clipboard.
  5548. With prefix arg N, cut this many sequential subtrees.
  5549. This is a short-hand for marking the subtree and then cutting it."
  5550. (interactive "p")
  5551. (org-copy-subtree n 'cut))
  5552. (defun org-copy-subtree (&optional n cut force-store-markers)
  5553. "Cut the current subtree into the clipboard.
  5554. With prefix arg N, cut this many sequential subtrees.
  5555. This is a short-hand for marking the subtree and then copying it.
  5556. If CUT is non-nil, actually cut the subtree.
  5557. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5558. of some markers in the region, even if CUT is non-nil. This is
  5559. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5560. (interactive "p")
  5561. (let (beg end folded (beg0 (point)))
  5562. (if (interactive-p)
  5563. (org-back-to-heading nil) ; take what looks like a subtree
  5564. (org-back-to-heading t)) ; take what is really there
  5565. (org-back-over-empty-lines)
  5566. (setq beg (point))
  5567. (skip-chars-forward " \t\r\n")
  5568. (save-match-data
  5569. (save-excursion (outline-end-of-heading)
  5570. (setq folded (org-invisible-p)))
  5571. (condition-case nil
  5572. (org-forward-same-level (1- n) t)
  5573. (error nil))
  5574. (org-end-of-subtree t t))
  5575. (org-back-over-empty-lines)
  5576. (setq end (point))
  5577. (goto-char beg0)
  5578. (when (> end beg)
  5579. (setq org-subtree-clip-folded folded)
  5580. (when (or cut force-store-markers)
  5581. (org-save-markers-in-region beg end))
  5582. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5583. (setq org-subtree-clip (current-kill 0))
  5584. (message "%s: Subtree(s) with %d characters"
  5585. (if cut "Cut" "Copied")
  5586. (length org-subtree-clip)))))
  5587. (defun org-paste-subtree (&optional level tree for-yank)
  5588. "Paste the clipboard as a subtree, with modification of headline level.
  5589. The entire subtree is promoted or demoted in order to match a new headline
  5590. level.
  5591. If the cursor is at the beginning of a headline, the same level as
  5592. that headline is used to paste the tree
  5593. If not, the new level is derived from the *visible* headings
  5594. before and after the insertion point, and taken to be the inferior headline
  5595. level of the two. So if the previous visible heading is level 3 and the
  5596. next is level 4 (or vice versa), level 4 will be used for insertion.
  5597. This makes sure that the subtree remains an independent subtree and does
  5598. not swallow low level entries.
  5599. You can also force a different level, either by using a numeric prefix
  5600. argument, or by inserting the heading marker by hand. For example, if the
  5601. cursor is after \"*****\", then the tree will be shifted to level 5.
  5602. If optional TREE is given, use this text instead of the kill ring.
  5603. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5604. move back over whitespace before inserting, and move point to the end of
  5605. the inserted text when done."
  5606. (interactive "P")
  5607. (setq tree (or tree (and kill-ring (current-kill 0))))
  5608. (unless (org-kill-is-subtree-p tree)
  5609. (error "%s"
  5610. (substitute-command-keys
  5611. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5612. (let* ((visp (not (org-invisible-p)))
  5613. (txt tree)
  5614. (^re (concat "^\\(" outline-regexp "\\)"))
  5615. (re (concat "\\(" outline-regexp "\\)"))
  5616. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5617. (old-level (if (string-match ^re txt)
  5618. (- (match-end 0) (match-beginning 0) 1)
  5619. -1))
  5620. (force-level (cond (level (prefix-numeric-value level))
  5621. ((and (looking-at "[ \t]*$")
  5622. (string-match
  5623. ^re_ (buffer-substring
  5624. (point-at-bol) (point))))
  5625. (- (match-end 1) (match-beginning 1)))
  5626. ((and (bolp)
  5627. (looking-at org-outline-regexp))
  5628. (- (match-end 0) (point) 1))
  5629. (t nil)))
  5630. (previous-level (save-excursion
  5631. (condition-case nil
  5632. (progn
  5633. (outline-previous-visible-heading 1)
  5634. (if (looking-at re)
  5635. (- (match-end 0) (match-beginning 0) 1)
  5636. 1))
  5637. (error 1))))
  5638. (next-level (save-excursion
  5639. (condition-case nil
  5640. (progn
  5641. (or (looking-at outline-regexp)
  5642. (outline-next-visible-heading 1))
  5643. (if (looking-at re)
  5644. (- (match-end 0) (match-beginning 0) 1)
  5645. 1))
  5646. (error 1))))
  5647. (new-level (or force-level (max previous-level next-level)))
  5648. (shift (if (or (= old-level -1)
  5649. (= new-level -1)
  5650. (= old-level new-level))
  5651. 0
  5652. (- new-level old-level)))
  5653. (delta (if (> shift 0) -1 1))
  5654. (func (if (> shift 0) 'org-demote 'org-promote))
  5655. (org-odd-levels-only nil)
  5656. beg end newend)
  5657. ;; Remove the forced level indicator
  5658. (if force-level
  5659. (delete-region (point-at-bol) (point)))
  5660. ;; Paste
  5661. (beginning-of-line 1)
  5662. (unless for-yank (org-back-over-empty-lines))
  5663. (setq beg (point))
  5664. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5665. (insert-before-markers txt)
  5666. (unless (string-match "\n\\'" txt) (insert "\n"))
  5667. (setq newend (point))
  5668. (org-reinstall-markers-in-region beg)
  5669. (setq end (point))
  5670. (goto-char beg)
  5671. (skip-chars-forward " \t\n\r")
  5672. (setq beg (point))
  5673. (if (and (org-invisible-p) visp)
  5674. (save-excursion (outline-show-heading)))
  5675. ;; Shift if necessary
  5676. (unless (= shift 0)
  5677. (save-restriction
  5678. (narrow-to-region beg end)
  5679. (while (not (= shift 0))
  5680. (org-map-region func (point-min) (point-max))
  5681. (setq shift (+ delta shift)))
  5682. (goto-char (point-min))
  5683. (setq newend (point-max))))
  5684. (when (or (interactive-p) for-yank)
  5685. (message "Clipboard pasted as level %d subtree" new-level))
  5686. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5687. kill-ring
  5688. (eq org-subtree-clip (current-kill 0))
  5689. org-subtree-clip-folded)
  5690. ;; The tree was folded before it was killed/copied
  5691. (hide-subtree))
  5692. (and for-yank (goto-char newend))))
  5693. (defun org-kill-is-subtree-p (&optional txt)
  5694. "Check if the current kill is an outline subtree, or a set of trees.
  5695. Returns nil if kill does not start with a headline, or if the first
  5696. headline level is not the largest headline level in the tree.
  5697. So this will actually accept several entries of equal levels as well,
  5698. which is OK for `org-paste-subtree'.
  5699. If optional TXT is given, check this string instead of the current kill."
  5700. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5701. (start-level (and kill
  5702. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5703. org-outline-regexp "\\)")
  5704. kill)
  5705. (- (match-end 2) (match-beginning 2) 1)))
  5706. (re (concat "^" org-outline-regexp))
  5707. (start (1+ (or (match-beginning 2) -1))))
  5708. (if (not start-level)
  5709. (progn
  5710. nil) ;; does not even start with a heading
  5711. (catch 'exit
  5712. (while (setq start (string-match re kill (1+ start)))
  5713. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5714. (throw 'exit nil)))
  5715. t))))
  5716. (defvar org-markers-to-move nil
  5717. "Markers that should be moved with a cut-and-paste operation.
  5718. Those markers are stored together with their positions relative to
  5719. the start of the region.")
  5720. (defun org-save-markers-in-region (beg end)
  5721. "Check markers in region.
  5722. If these markers are between BEG and END, record their position relative
  5723. to BEG, so that after moving the block of text, we can put the markers back
  5724. into place.
  5725. This function gets called just before an entry or tree gets cut from the
  5726. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5727. called immediately, to move the markers with the entries."
  5728. (setq org-markers-to-move nil)
  5729. (when (featurep 'org-clock)
  5730. (org-clock-save-markers-for-cut-and-paste beg end))
  5731. (when (featurep 'org-agenda)
  5732. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5733. (defun org-check-and-save-marker (marker beg end)
  5734. "Check if MARKER is between BEG and END.
  5735. If yes, remember the marker and the distance to BEG."
  5736. (when (and (marker-buffer marker)
  5737. (equal (marker-buffer marker) (current-buffer)))
  5738. (if (and (>= marker beg) (< marker end))
  5739. (push (cons marker (- marker beg)) org-markers-to-move))))
  5740. (defun org-reinstall-markers-in-region (beg)
  5741. "Move all remembered markers to their position relative to BEG."
  5742. (mapc (lambda (x)
  5743. (move-marker (car x) (+ beg (cdr x))))
  5744. org-markers-to-move)
  5745. (setq org-markers-to-move nil))
  5746. (defun org-narrow-to-subtree ()
  5747. "Narrow buffer to the current subtree."
  5748. (interactive)
  5749. (save-excursion
  5750. (save-match-data
  5751. (narrow-to-region
  5752. (progn (org-back-to-heading t) (point))
  5753. (progn (org-end-of-subtree t) (point))))))
  5754. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5755. "Clone the task (subtree) at point N times.
  5756. The clones will be inserted as siblings.
  5757. In interactive use, the user will be prompted for the number of clones
  5758. to be produced, and for a time SHIFT, which may be a repeater as used
  5759. in time stamps, for example `+3d'.
  5760. When a valid repeater is given and the entry contains any time stamps,
  5761. the clones will become a sequence in time, with time stamps in the
  5762. subtree shifted for each clone produced. If SHIFT is nil or the
  5763. empty string, time stamps will be left alone.
  5764. If the original subtree did contain time stamps with a repeater,
  5765. the following will happen:
  5766. - the repeater will be removed in each clone
  5767. - an additional clone will be produced, with the current, unshifted
  5768. date(s) in the entry.
  5769. - the original entry will be placed *after* all the clones, with
  5770. repeater intact.
  5771. - the start days in the repeater in the original entry will be shifted
  5772. to past the last clone.
  5773. I this way you can spell out a number of instances of a repeating task,
  5774. and still retain the repeater to cover future instances of the task."
  5775. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5776. (let (beg end template task
  5777. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5778. (if (not (and (integerp n) (> n 0)))
  5779. (error "Invalid number of replications %s" n))
  5780. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5781. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5782. shift)))
  5783. (error "Invalid shift specification %s" shift))
  5784. (when doshift
  5785. (setq shift-n (string-to-number (match-string 1 shift))
  5786. shift-what (cdr (assoc (match-string 2 shift)
  5787. '(("d" . day) ("w" . week)
  5788. ("m" . month) ("y" . year))))))
  5789. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5790. (setq nmin 1 nmax n)
  5791. (org-back-to-heading t)
  5792. (setq beg (point))
  5793. (org-end-of-subtree t t)
  5794. (setq end (point))
  5795. (setq template (buffer-substring beg end))
  5796. (when (and doshift
  5797. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5798. (delete-region beg end)
  5799. (setq end beg)
  5800. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5801. (goto-char end)
  5802. (loop for n from nmin to nmax do
  5803. (if (not doshift)
  5804. (setq task template)
  5805. (with-temp-buffer
  5806. (insert template)
  5807. (org-mode)
  5808. (goto-char (point-min))
  5809. (while (re-search-forward org-ts-regexp-both nil t)
  5810. (org-timestamp-change (* n shift-n) shift-what))
  5811. (unless (= n n-no-remove)
  5812. (goto-char (point-min))
  5813. (while (re-search-forward org-ts-regexp nil t)
  5814. (save-excursion
  5815. (goto-char (match-beginning 0))
  5816. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5817. (delete-region (match-beginning 1) (match-end 1))))))
  5818. (setq task (buffer-string))))
  5819. (insert task))
  5820. (goto-char beg)))
  5821. ;;; Outline Sorting
  5822. (defun org-sort (with-case)
  5823. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5824. Optional argument WITH-CASE means sort case-sensitively.
  5825. With a double prefix argument, also remove duplicate entries."
  5826. (interactive "P")
  5827. (if (org-at-table-p)
  5828. (org-call-with-arg 'org-table-sort-lines with-case)
  5829. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5830. (defun org-sort-remove-invisible (s)
  5831. (remove-text-properties 0 (length s) org-rm-props s)
  5832. (while (string-match org-bracket-link-regexp s)
  5833. (setq s (replace-match (if (match-end 2)
  5834. (match-string 3 s)
  5835. (match-string 1 s)) t t s)))
  5836. s)
  5837. (defvar org-priority-regexp) ; defined later in the file
  5838. (defvar org-after-sorting-entries-or-items-hook nil
  5839. "Hook that is run after a bunch of entries or items have been sorted.
  5840. When children are sorted, the cursor is in the parent line when this
  5841. hook gets called. When a region or a plain list is sorted, the cursor
  5842. will be in the first entry of the sorted region/list.")
  5843. (defun org-sort-entries-or-items
  5844. (&optional with-case sorting-type getkey-func compare-func property)
  5845. "Sort entries on a certain level of an outline tree, or plain list items.
  5846. If there is an active region, the entries in the region are sorted.
  5847. Else, if the cursor is before the first entry, sort the top-level items.
  5848. Else, the children of the entry at point are sorted.
  5849. If the cursor is at the first item in a plain list, the list items will be
  5850. sorted.
  5851. Sorting can be alphabetically, numerically, by date/time as given by
  5852. a time stamp, by a property or by priority.
  5853. The command prompts for the sorting type unless it has been given to the
  5854. function through the SORTING-TYPE argument, which needs to a character,
  5855. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5856. precise meaning of each character:
  5857. n Numerically, by converting the beginning of the entry/item to a number.
  5858. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5859. t By date/time, either the first active time stamp in the entry, or, if
  5860. none exist, by the first inactive one.
  5861. In items, only the first line will be chekced.
  5862. s By the scheduled date/time.
  5863. d By deadline date/time.
  5864. c By creation time, which is assumed to be the first inactive time stamp
  5865. at the beginning of a line.
  5866. p By priority according to the cookie.
  5867. r By the value of a property.
  5868. Capital letters will reverse the sort order.
  5869. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5870. called with point at the beginning of the record. It must return either
  5871. a string or a number that should serve as the sorting key for that record.
  5872. Comparing entries ignores case by default. However, with an optional argument
  5873. WITH-CASE, the sorting considers case as well."
  5874. (interactive "P")
  5875. (let ((case-func (if with-case 'identity 'downcase))
  5876. start beg end stars re re2
  5877. txt what tmp plain-list-p)
  5878. ;; Find beginning and end of region to sort
  5879. (cond
  5880. ((org-region-active-p)
  5881. ;; we will sort the region
  5882. (setq end (region-end)
  5883. what "region")
  5884. (goto-char (region-beginning))
  5885. (if (not (org-on-heading-p)) (outline-next-heading))
  5886. (setq start (point)))
  5887. ((org-at-item-p)
  5888. ;; we will sort this plain list
  5889. (org-beginning-of-item-list) (setq start (point))
  5890. (org-end-of-item-list) (setq end (point))
  5891. (goto-char start)
  5892. (setq plain-list-p t
  5893. what "plain list"))
  5894. ((or (org-on-heading-p)
  5895. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5896. ;; we will sort the children of the current headline
  5897. (org-back-to-heading)
  5898. (setq start (point)
  5899. end (progn (org-end-of-subtree t t)
  5900. (org-back-over-empty-lines)
  5901. (point))
  5902. what "children")
  5903. (goto-char start)
  5904. (show-subtree)
  5905. (outline-next-heading))
  5906. (t
  5907. ;; we will sort the top-level entries in this file
  5908. (goto-char (point-min))
  5909. (or (org-on-heading-p) (outline-next-heading))
  5910. (setq start (point) end (point-max) what "top-level")
  5911. (goto-char start)
  5912. (show-all)))
  5913. (setq beg (point))
  5914. (if (>= beg end) (error "Nothing to sort"))
  5915. (unless plain-list-p
  5916. (looking-at "\\(\\*+\\)")
  5917. (setq stars (match-string 1)
  5918. re (concat "^" (regexp-quote stars) " +")
  5919. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5920. txt (buffer-substring beg end))
  5921. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5922. (if (and (not (equal stars "*")) (string-match re2 txt))
  5923. (error "Region to sort contains a level above the first entry")))
  5924. (unless sorting-type
  5925. (message
  5926. (if plain-list-p
  5927. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5928. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5929. [t]ime [s]cheduled [d]eadline [c]reated
  5930. A/N/T/S/D/C/P/O/F means reversed:")
  5931. what)
  5932. (setq sorting-type (read-char-exclusive))
  5933. (and (= (downcase sorting-type) ?f)
  5934. (setq getkey-func
  5935. (org-icompleting-read "Sort using function: "
  5936. obarray 'fboundp t nil nil))
  5937. (setq getkey-func (intern getkey-func)))
  5938. (and (= (downcase sorting-type) ?r)
  5939. (setq property
  5940. (org-icompleting-read "Property: "
  5941. (mapcar 'list (org-buffer-property-keys t))
  5942. nil t))))
  5943. (message "Sorting entries...")
  5944. (save-restriction
  5945. (narrow-to-region start end)
  5946. (let ((dcst (downcase sorting-type))
  5947. (case-fold-search nil)
  5948. (now (current-time)))
  5949. (sort-subr
  5950. (/= dcst sorting-type)
  5951. ;; This function moves to the beginning character of the "record" to
  5952. ;; be sorted.
  5953. (if plain-list-p
  5954. (lambda nil
  5955. (if (org-at-item-p) t (goto-char (point-max))))
  5956. (lambda nil
  5957. (if (re-search-forward re nil t)
  5958. (goto-char (match-beginning 0))
  5959. (goto-char (point-max)))))
  5960. ;; This function moves to the last character of the "record" being
  5961. ;; sorted.
  5962. (if plain-list-p
  5963. 'org-end-of-item
  5964. (lambda nil
  5965. (save-match-data
  5966. (condition-case nil
  5967. (outline-forward-same-level 1)
  5968. (error
  5969. (goto-char (point-max)))))))
  5970. ;; This function returns the value that gets sorted against.
  5971. (if plain-list-p
  5972. (lambda nil
  5973. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5974. (cond
  5975. ((= dcst ?n)
  5976. (string-to-number (buffer-substring (match-end 0)
  5977. (point-at-eol))))
  5978. ((= dcst ?a)
  5979. (buffer-substring (match-end 0) (point-at-eol)))
  5980. ((= dcst ?t)
  5981. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5982. (re-search-forward org-ts-regexp-both
  5983. (point-at-eol) t))
  5984. (org-time-string-to-seconds (match-string 0))
  5985. (org-float-time now)))
  5986. ((= dcst ?f)
  5987. (if getkey-func
  5988. (progn
  5989. (setq tmp (funcall getkey-func))
  5990. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5991. tmp)
  5992. (error "Invalid key function `%s'" getkey-func)))
  5993. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5994. (lambda nil
  5995. (cond
  5996. ((= dcst ?n)
  5997. (if (looking-at org-complex-heading-regexp)
  5998. (string-to-number (match-string 4))
  5999. nil))
  6000. ((= dcst ?a)
  6001. (if (looking-at org-complex-heading-regexp)
  6002. (funcall case-func (match-string 4))
  6003. nil))
  6004. ((= dcst ?t)
  6005. (let ((end (save-excursion (outline-next-heading) (point))))
  6006. (if (or (re-search-forward org-ts-regexp end t)
  6007. (re-search-forward org-ts-regexp-both end t))
  6008. (org-time-string-to-seconds (match-string 0))
  6009. (org-float-time now))))
  6010. ((= dcst ?c)
  6011. (let ((end (save-excursion (outline-next-heading) (point))))
  6012. (if (re-search-forward
  6013. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6014. end t)
  6015. (org-time-string-to-seconds (match-string 0))
  6016. (org-float-time now))))
  6017. ((= dcst ?s)
  6018. (let ((end (save-excursion (outline-next-heading) (point))))
  6019. (if (re-search-forward org-scheduled-time-regexp end t)
  6020. (org-time-string-to-seconds (match-string 1))
  6021. (org-float-time now))))
  6022. ((= dcst ?d)
  6023. (let ((end (save-excursion (outline-next-heading) (point))))
  6024. (if (re-search-forward org-deadline-time-regexp end t)
  6025. (org-time-string-to-seconds (match-string 1))
  6026. (org-float-time now))))
  6027. ((= dcst ?p)
  6028. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6029. (string-to-char (match-string 2))
  6030. org-default-priority))
  6031. ((= dcst ?r)
  6032. (or (org-entry-get nil property) ""))
  6033. ((= dcst ?o)
  6034. (if (looking-at org-complex-heading-regexp)
  6035. (- 9999 (length (member (match-string 2)
  6036. org-todo-keywords-1)))))
  6037. ((= dcst ?f)
  6038. (if getkey-func
  6039. (progn
  6040. (setq tmp (funcall getkey-func))
  6041. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6042. tmp)
  6043. (error "Invalid key function `%s'" getkey-func)))
  6044. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6045. nil
  6046. (cond
  6047. ((= dcst ?a) 'string<)
  6048. ((= dcst ?f) compare-func)
  6049. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6050. (t nil)))))
  6051. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6052. (message "Sorting entries...done")))
  6053. (defun org-do-sort (table what &optional with-case sorting-type)
  6054. "Sort TABLE of WHAT according to SORTING-TYPE.
  6055. The user will be prompted for the SORTING-TYPE if the call to this
  6056. function does not specify it. WHAT is only for the prompt, to indicate
  6057. what is being sorted. The sorting key will be extracted from
  6058. the car of the elements of the table.
  6059. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6060. (unless sorting-type
  6061. (message
  6062. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6063. what)
  6064. (setq sorting-type (read-char-exclusive)))
  6065. (let ((dcst (downcase sorting-type))
  6066. extractfun comparefun)
  6067. ;; Define the appropriate functions
  6068. (cond
  6069. ((= dcst ?n)
  6070. (setq extractfun 'string-to-number
  6071. comparefun (if (= dcst sorting-type) '< '>)))
  6072. ((= dcst ?a)
  6073. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6074. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6075. comparefun (if (= dcst sorting-type)
  6076. 'string<
  6077. (lambda (a b) (and (not (string< a b))
  6078. (not (string= a b)))))))
  6079. ((= dcst ?t)
  6080. (setq extractfun
  6081. (lambda (x)
  6082. (if (or (string-match org-ts-regexp x)
  6083. (string-match org-ts-regexp-both x))
  6084. (org-float-time
  6085. (org-time-string-to-time (match-string 0 x)))
  6086. 0))
  6087. comparefun (if (= dcst sorting-type) '< '>)))
  6088. (t (error "Invalid sorting type `%c'" sorting-type)))
  6089. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6090. table)
  6091. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6092. ;;; The orgstruct minor mode
  6093. ;; Define a minor mode which can be used in other modes in order to
  6094. ;; integrate the org-mode structure editing commands.
  6095. ;; This is really a hack, because the org-mode structure commands use
  6096. ;; keys which normally belong to the major mode. Here is how it
  6097. ;; works: The minor mode defines all the keys necessary to operate the
  6098. ;; structure commands, but wraps the commands into a function which
  6099. ;; tests if the cursor is currently at a headline or a plain list
  6100. ;; item. If that is the case, the structure command is used,
  6101. ;; temporarily setting many Org-mode variables like regular
  6102. ;; expressions for filling etc. However, when any of those keys is
  6103. ;; used at a different location, function uses `key-binding' to look
  6104. ;; up if the key has an associated command in another currently active
  6105. ;; keymap (minor modes, major mode, global), and executes that
  6106. ;; command. There might be problems if any of the keys is otherwise
  6107. ;; used as a prefix key.
  6108. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6109. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6110. ;; addresses this by checking explicitly for both bindings.
  6111. (defvar orgstruct-mode-map (make-sparse-keymap)
  6112. "Keymap for the minor `orgstruct-mode'.")
  6113. (defvar org-local-vars nil
  6114. "List of local variables, for use by `orgstruct-mode'")
  6115. ;;;###autoload
  6116. (define-minor-mode orgstruct-mode
  6117. "Toggle the minor more `orgstruct-mode'.
  6118. This mode is for using Org-mode structure commands in other modes.
  6119. The following key behave as if Org-mode was active, if the cursor
  6120. is on a headline, or on a plain list item (both in the definition
  6121. of Org-mode).
  6122. M-up Move entry/item up
  6123. M-down Move entry/item down
  6124. M-left Promote
  6125. M-right Demote
  6126. M-S-up Move entry/item up
  6127. M-S-down Move entry/item down
  6128. M-S-left Promote subtree
  6129. M-S-right Demote subtree
  6130. M-q Fill paragraph and items like in Org-mode
  6131. C-c ^ Sort entries
  6132. C-c - Cycle list bullet
  6133. TAB Cycle item visibility
  6134. M-RET Insert new heading/item
  6135. S-M-RET Insert new TODO heading / Checkbox item
  6136. C-c C-c Set tags / toggle checkbox"
  6137. nil " OrgStruct" nil
  6138. (org-load-modules-maybe)
  6139. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6140. ;;;###autoload
  6141. (defun turn-on-orgstruct ()
  6142. "Unconditionally turn on `orgstruct-mode'."
  6143. (orgstruct-mode 1))
  6144. (defun orgstruct++-mode (&optional arg)
  6145. "Toggle `orgstruct-mode', the enhanced version of it.
  6146. In addition to setting orgstruct-mode, this also exports all indentation
  6147. and autofilling variables from org-mode into the buffer. It will also
  6148. recognize item context in multiline items.
  6149. Note that turning off orgstruct-mode will *not* remove the
  6150. indentation/paragraph settings. This can only be done by refreshing the
  6151. major mode, for example with \\[normal-mode]."
  6152. (interactive "P")
  6153. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6154. (if (< arg 1)
  6155. (orgstruct-mode -1)
  6156. (orgstruct-mode 1)
  6157. (let (var val)
  6158. (mapc
  6159. (lambda (x)
  6160. (when (string-match
  6161. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6162. (symbol-name (car x)))
  6163. (setq var (car x) val (nth 1 x))
  6164. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6165. org-local-vars)
  6166. (org-set-local 'orgstruct-is-++ t))))
  6167. (defvar orgstruct-is-++ nil
  6168. "Is orgstruct-mode in ++ version in the current-buffer?")
  6169. (make-variable-buffer-local 'orgstruct-is-++)
  6170. ;;;###autoload
  6171. (defun turn-on-orgstruct++ ()
  6172. "Unconditionally turn on `orgstruct++-mode'."
  6173. (orgstruct++-mode 1))
  6174. (defun orgstruct-error ()
  6175. "Error when there is no default binding for a structure key."
  6176. (interactive)
  6177. (error "This key has no function outside structure elements"))
  6178. (defun orgstruct-setup ()
  6179. "Setup orgstruct keymaps."
  6180. (let ((nfunc 0)
  6181. (bindings
  6182. (list
  6183. '([(meta up)] org-metaup)
  6184. '([(meta down)] org-metadown)
  6185. '([(meta left)] org-metaleft)
  6186. '([(meta right)] org-metaright)
  6187. '([(meta shift up)] org-shiftmetaup)
  6188. '([(meta shift down)] org-shiftmetadown)
  6189. '([(meta shift left)] org-shiftmetaleft)
  6190. '([(meta shift right)] org-shiftmetaright)
  6191. '([?\e (up)] org-metaup)
  6192. '([?\e (down)] org-metadown)
  6193. '([?\e (left)] org-metaleft)
  6194. '([?\e (right)] org-metaright)
  6195. '([?\e (shift up)] org-shiftmetaup)
  6196. '([?\e (shift down)] org-shiftmetadown)
  6197. '([?\e (shift left)] org-shiftmetaleft)
  6198. '([?\e (shift right)] org-shiftmetaright)
  6199. '([(shift up)] org-shiftup)
  6200. '([(shift down)] org-shiftdown)
  6201. '([(shift left)] org-shiftleft)
  6202. '([(shift right)] org-shiftright)
  6203. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6204. '("\M-q" fill-paragraph)
  6205. '("\C-c^" org-sort)
  6206. '("\C-c-" org-cycle-list-bullet)))
  6207. elt key fun cmd)
  6208. (while (setq elt (pop bindings))
  6209. (setq nfunc (1+ nfunc))
  6210. (setq key (org-key (car elt))
  6211. fun (nth 1 elt)
  6212. cmd (orgstruct-make-binding fun nfunc key))
  6213. (org-defkey orgstruct-mode-map key cmd))
  6214. ;; Special treatment needed for TAB and RET
  6215. (org-defkey orgstruct-mode-map [(tab)]
  6216. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6217. (org-defkey orgstruct-mode-map "\C-i"
  6218. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6219. (org-defkey orgstruct-mode-map "\M-\C-m"
  6220. (orgstruct-make-binding 'org-insert-heading 105
  6221. "\M-\C-m" [(meta return)]))
  6222. (org-defkey orgstruct-mode-map [(meta return)]
  6223. (orgstruct-make-binding 'org-insert-heading 106
  6224. [(meta return)] "\M-\C-m"))
  6225. (org-defkey orgstruct-mode-map [(shift meta return)]
  6226. (orgstruct-make-binding 'org-insert-todo-heading 107
  6227. [(meta return)] "\M-\C-m"))
  6228. (org-defkey orgstruct-mode-map "\e\C-m"
  6229. (orgstruct-make-binding 'org-insert-heading 108
  6230. "\e\C-m" [?\e (return)]))
  6231. (org-defkey orgstruct-mode-map [?\e (return)]
  6232. (orgstruct-make-binding 'org-insert-heading 109
  6233. [?\e (return)] "\e\C-m"))
  6234. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6235. (orgstruct-make-binding 'org-insert-todo-heading 110
  6236. [?\e (return)] "\e\C-m"))
  6237. (unless org-local-vars
  6238. (setq org-local-vars (org-get-local-variables)))
  6239. t))
  6240. (defun orgstruct-make-binding (fun n &rest keys)
  6241. "Create a function for binding in the structure minor mode.
  6242. FUN is the command to call inside a table. N is used to create a unique
  6243. command name. KEYS are keys that should be checked in for a command
  6244. to execute outside of tables."
  6245. (eval
  6246. (list 'defun
  6247. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6248. '(arg)
  6249. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6250. "Outside of structure, run the binding of `"
  6251. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6252. "'.")
  6253. '(interactive "p")
  6254. (list 'if
  6255. `(org-context-p 'headline 'item
  6256. (and orgstruct-is-++
  6257. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6258. 'item-body))
  6259. (list 'org-run-like-in-org-mode (list 'quote fun))
  6260. (list 'let '(orgstruct-mode)
  6261. (list 'call-interactively
  6262. (append '(or)
  6263. (mapcar (lambda (k)
  6264. (list 'key-binding k))
  6265. keys)
  6266. '('orgstruct-error))))))))
  6267. (defun org-context-p (&rest contexts)
  6268. "Check if local context is any of CONTEXTS.
  6269. Possible values in the list of contexts are `table', `headline', and `item'."
  6270. (let ((pos (point)))
  6271. (goto-char (point-at-bol))
  6272. (prog1 (or (and (memq 'table contexts)
  6273. (looking-at "[ \t]*|"))
  6274. (and (memq 'headline contexts)
  6275. ;;????????? (looking-at "\\*+"))
  6276. (looking-at outline-regexp))
  6277. (and (memq 'item contexts)
  6278. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6279. (and (memq 'item-body contexts)
  6280. (org-in-item-p)))
  6281. (goto-char pos))))
  6282. (defun org-get-local-variables ()
  6283. "Return a list of all local variables in an org-mode buffer."
  6284. (let (varlist)
  6285. (with-current-buffer (get-buffer-create "*Org tmp*")
  6286. (erase-buffer)
  6287. (org-mode)
  6288. (setq varlist (buffer-local-variables)))
  6289. (kill-buffer "*Org tmp*")
  6290. (delq nil
  6291. (mapcar
  6292. (lambda (x)
  6293. (setq x
  6294. (if (symbolp x)
  6295. (list x)
  6296. (list (car x) (list 'quote (cdr x)))))
  6297. (if (string-match
  6298. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6299. (symbol-name (car x)))
  6300. x nil))
  6301. varlist))))
  6302. ;;;###autoload
  6303. (defun org-run-like-in-org-mode (cmd)
  6304. "Run a command, pretending that the current buffer is in Org-mode.
  6305. This will temporarily bind local variables that are typically bound in
  6306. Org-mode to the values they have in Org-mode, and then interactively
  6307. call CMD."
  6308. (org-load-modules-maybe)
  6309. (unless org-local-vars
  6310. (setq org-local-vars (org-get-local-variables)))
  6311. (eval (list 'let org-local-vars
  6312. (list 'call-interactively (list 'quote cmd)))))
  6313. ;;;; Archiving
  6314. (defun org-get-category (&optional pos)
  6315. "Get the category applying to position POS."
  6316. (get-text-property (or pos (point)) 'org-category))
  6317. (defun org-refresh-category-properties ()
  6318. "Refresh category text properties in the buffer."
  6319. (let ((def-cat (cond
  6320. ((null org-category)
  6321. (if buffer-file-name
  6322. (file-name-sans-extension
  6323. (file-name-nondirectory buffer-file-name))
  6324. "???"))
  6325. ((symbolp org-category) (symbol-name org-category))
  6326. (t org-category)))
  6327. beg end cat pos optionp)
  6328. (org-unmodified
  6329. (save-excursion
  6330. (save-restriction
  6331. (widen)
  6332. (goto-char (point-min))
  6333. (put-text-property (point) (point-max) 'org-category def-cat)
  6334. (while (re-search-forward
  6335. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6336. (setq pos (match-end 0)
  6337. optionp (equal (char-after (match-beginning 0)) ?#)
  6338. cat (org-trim (match-string 2)))
  6339. (if optionp
  6340. (setq beg (point-at-bol) end (point-max))
  6341. (org-back-to-heading t)
  6342. (setq beg (point) end (org-end-of-subtree t t)))
  6343. (put-text-property beg end 'org-category cat)
  6344. (goto-char pos)))))))
  6345. ;;;; Link Stuff
  6346. ;;; Link abbreviations
  6347. (defun org-link-expand-abbrev (link)
  6348. "Apply replacements as defined in `org-link-abbrev-alist."
  6349. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6350. (let* ((key (match-string 1 link))
  6351. (as (or (assoc key org-link-abbrev-alist-local)
  6352. (assoc key org-link-abbrev-alist)))
  6353. (tag (and (match-end 2) (match-string 3 link)))
  6354. rpl)
  6355. (if (not as)
  6356. link
  6357. (setq rpl (cdr as))
  6358. (cond
  6359. ((symbolp rpl) (funcall rpl tag))
  6360. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6361. ((string-match "%h" rpl)
  6362. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6363. (t (concat rpl tag)))))
  6364. link))
  6365. ;;; Storing and inserting links
  6366. (defvar org-insert-link-history nil
  6367. "Minibuffer history for links inserted with `org-insert-link'.")
  6368. (defvar org-stored-links nil
  6369. "Contains the links stored with `org-store-link'.")
  6370. (defvar org-store-link-plist nil
  6371. "Plist with info about the most recently link created with `org-store-link'.")
  6372. (defvar org-link-protocols nil
  6373. "Link protocols added to Org-mode using `org-add-link-type'.")
  6374. (defvar org-store-link-functions nil
  6375. "List of functions that are called to create and store a link.
  6376. Each function will be called in turn until one returns a non-nil
  6377. value. Each function should check if it is responsible for creating
  6378. this link (for example by looking at the major mode).
  6379. If not, it must exit and return nil.
  6380. If yes, it should return a non-nil value after a calling
  6381. `org-store-link-props' with a list of properties and values.
  6382. Special properties are:
  6383. :type The link prefix. like \"http\". This must be given.
  6384. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6385. This is obligatory as well.
  6386. :description Optional default description for the second pair
  6387. of brackets in an Org-mode link. The user can still change
  6388. this when inserting this link into an Org-mode buffer.
  6389. In addition to these, any additional properties can be specified
  6390. and then used in remember templates.")
  6391. (defun org-add-link-type (type &optional follow export)
  6392. "Add TYPE to the list of `org-link-types'.
  6393. Re-compute all regular expressions depending on `org-link-types'
  6394. FOLLOW and EXPORT are two functions.
  6395. FOLLOW should take the link path as the single argument and do whatever
  6396. is necessary to follow the link, for example find a file or display
  6397. a mail message.
  6398. EXPORT should format the link path for export to one of the export formats.
  6399. It should be a function accepting three arguments:
  6400. path the path of the link, the text after the prefix (like \"http:\")
  6401. desc the description of the link, if any, nil if there was no description
  6402. format the export format, a symbol like `html' or `latex'.
  6403. The function may use the FORMAT information to return different values
  6404. depending on the format. The return value will be put literally into
  6405. the exported file.
  6406. Org-mode has a built-in default for exporting links. If you are happy with
  6407. this default, there is no need to define an export function for the link
  6408. type. For a simple example of an export function, see `org-bbdb.el'."
  6409. (add-to-list 'org-link-types type t)
  6410. (org-make-link-regexps)
  6411. (if (assoc type org-link-protocols)
  6412. (setcdr (assoc type org-link-protocols) (list follow export))
  6413. (push (list type follow export) org-link-protocols)))
  6414. (defvar org-agenda-buffer-name)
  6415. ;;;###autoload
  6416. (defun org-store-link (arg)
  6417. "\\<org-mode-map>Store an org-link to the current location.
  6418. This link is added to `org-stored-links' and can later be inserted
  6419. into an org-buffer with \\[org-insert-link].
  6420. For some link types, a prefix arg is interpreted:
  6421. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6422. For file links, arg negates `org-context-in-file-links'."
  6423. (interactive "P")
  6424. (org-load-modules-maybe)
  6425. (setq org-store-link-plist nil) ; reset
  6426. (let ((outline-regexp (org-get-limited-outline-regexp))
  6427. link cpltxt desc description search txt custom-id)
  6428. (cond
  6429. ((run-hook-with-args-until-success 'org-store-link-functions)
  6430. (setq link (plist-get org-store-link-plist :link)
  6431. desc (or (plist-get org-store-link-plist :description) link)))
  6432. ((equal (buffer-name) "*Org Edit Src Example*")
  6433. (let (label gc)
  6434. (while (or (not label)
  6435. (save-excursion
  6436. (save-restriction
  6437. (widen)
  6438. (goto-char (point-min))
  6439. (re-search-forward
  6440. (regexp-quote (format org-coderef-label-format label))
  6441. nil t))))
  6442. (when label (message "Label exists already") (sit-for 2))
  6443. (setq label (read-string "Code line label: " label)))
  6444. (end-of-line 1)
  6445. (setq link (format org-coderef-label-format label))
  6446. (setq gc (- 79 (length link)))
  6447. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6448. (insert link)
  6449. (setq link (concat "(" label ")") desc nil)))
  6450. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6451. ;; We are in the agenda, link to referenced location
  6452. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6453. (get-text-property (point) 'org-marker))))
  6454. (unless m (error "Don't know what location to link to"))
  6455. (org-with-point-at m
  6456. (call-interactively 'org-store-link))))
  6457. ((eq major-mode 'calendar-mode)
  6458. (let ((cd (calendar-cursor-to-date)))
  6459. (setq link
  6460. (format-time-string
  6461. (car org-time-stamp-formats)
  6462. (apply 'encode-time
  6463. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6464. nil nil nil))))
  6465. (org-store-link-props :type "calendar" :date cd)))
  6466. ((eq major-mode 'w3-mode)
  6467. (setq cpltxt (if (and (buffer-name)
  6468. (not (string-match "Untitled" (buffer-name))))
  6469. (buffer-name)
  6470. (url-view-url t))
  6471. link (org-make-link (url-view-url t)))
  6472. (org-store-link-props :type "w3" :url (url-view-url t)))
  6473. ((eq major-mode 'w3m-mode)
  6474. (setq cpltxt (or w3m-current-title w3m-current-url)
  6475. link (org-make-link w3m-current-url))
  6476. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6477. ((setq search (run-hook-with-args-until-success
  6478. 'org-create-file-search-functions))
  6479. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6480. "::" search))
  6481. (setq cpltxt (or description link)))
  6482. ((eq major-mode 'image-mode)
  6483. (setq cpltxt (concat "file:"
  6484. (abbreviate-file-name buffer-file-name))
  6485. link (org-make-link cpltxt))
  6486. (org-store-link-props :type "image" :file buffer-file-name))
  6487. ((eq major-mode 'dired-mode)
  6488. ;; link to the file in the current line
  6489. (setq cpltxt (concat "file:"
  6490. (abbreviate-file-name
  6491. (expand-file-name
  6492. (dired-get-filename nil t))))
  6493. link (org-make-link cpltxt)))
  6494. ((and buffer-file-name (org-mode-p))
  6495. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6496. (cond
  6497. ((org-in-regexp "<<\\(.*?\\)>>")
  6498. (setq cpltxt
  6499. (concat "file:"
  6500. (abbreviate-file-name buffer-file-name)
  6501. "::" (match-string 1))
  6502. link (org-make-link cpltxt)))
  6503. ((and (featurep 'org-id)
  6504. (or (eq org-link-to-org-use-id t)
  6505. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6506. (interactive-p))
  6507. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6508. (interactive-p)
  6509. (not custom-id))
  6510. (and org-link-to-org-use-id
  6511. (condition-case nil
  6512. (org-entry-get nil "ID")
  6513. (error nil)))))
  6514. ;; We can make a link using the ID.
  6515. (setq link (condition-case nil
  6516. (prog1 (org-id-store-link)
  6517. (setq desc (plist-get org-store-link-plist
  6518. :description)))
  6519. (error
  6520. ;; probably before first headline, link to file only
  6521. (concat "file:"
  6522. (abbreviate-file-name buffer-file-name))))))
  6523. (t
  6524. ;; Just link to current headline
  6525. (setq cpltxt (concat "file:"
  6526. (abbreviate-file-name buffer-file-name)))
  6527. ;; Add a context search string
  6528. (when (org-xor org-context-in-file-links arg)
  6529. (setq txt (cond
  6530. ((org-on-heading-p) nil)
  6531. ((org-region-active-p)
  6532. (buffer-substring (region-beginning) (region-end)))
  6533. (t nil)))
  6534. (when (or (null txt) (string-match "\\S-" txt))
  6535. (setq cpltxt
  6536. (concat cpltxt "::"
  6537. (condition-case nil
  6538. (org-make-org-heading-search-string txt)
  6539. (error "")))
  6540. desc (or (nth 4 (ignore-errors
  6541. (org-heading-components))) "NONE"))))
  6542. (if (string-match "::\\'" cpltxt)
  6543. (setq cpltxt (substring cpltxt 0 -2)))
  6544. (setq link (org-make-link cpltxt)))))
  6545. ((buffer-file-name (buffer-base-buffer))
  6546. ;; Just link to this file here.
  6547. (setq cpltxt (concat "file:"
  6548. (abbreviate-file-name
  6549. (buffer-file-name (buffer-base-buffer)))))
  6550. ;; Add a context string
  6551. (when (org-xor org-context-in-file-links arg)
  6552. (setq txt (if (org-region-active-p)
  6553. (buffer-substring (region-beginning) (region-end))
  6554. (buffer-substring (point-at-bol) (point-at-eol))))
  6555. ;; Only use search option if there is some text.
  6556. (when (string-match "\\S-" txt)
  6557. (setq cpltxt
  6558. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6559. desc "NONE")))
  6560. (setq link (org-make-link cpltxt)))
  6561. ((interactive-p)
  6562. (error "Cannot link to a buffer which is not visiting a file"))
  6563. (t (setq link nil)))
  6564. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6565. (setq link (or link cpltxt)
  6566. desc (or desc cpltxt))
  6567. (if (equal desc "NONE") (setq desc nil))
  6568. (if (and (or (interactive-p) executing-kbd-macro) link)
  6569. (progn
  6570. (setq org-stored-links
  6571. (cons (list link desc) org-stored-links))
  6572. (message "Stored: %s" (or desc link))
  6573. (when custom-id
  6574. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6575. "::#" custom-id))
  6576. (setq org-stored-links
  6577. (cons (list link desc) org-stored-links))))
  6578. (and link (org-make-link-string link desc)))))
  6579. (defun org-store-link-props (&rest plist)
  6580. "Store link properties, extract names and addresses."
  6581. (let (x adr)
  6582. (when (setq x (plist-get plist :from))
  6583. (setq adr (mail-extract-address-components x))
  6584. (setq plist (plist-put plist :fromname (car adr)))
  6585. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6586. (when (setq x (plist-get plist :to))
  6587. (setq adr (mail-extract-address-components x))
  6588. (setq plist (plist-put plist :toname (car adr)))
  6589. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6590. (let ((from (plist-get plist :from))
  6591. (to (plist-get plist :to)))
  6592. (when (and from to org-from-is-user-regexp)
  6593. (setq plist
  6594. (plist-put plist :fromto
  6595. (if (string-match org-from-is-user-regexp from)
  6596. (concat "to %t")
  6597. (concat "from %f"))))))
  6598. (setq org-store-link-plist plist))
  6599. (defun org-add-link-props (&rest plist)
  6600. "Add these properties to the link property list."
  6601. (let (key value)
  6602. (while plist
  6603. (setq key (pop plist) value (pop plist))
  6604. (setq org-store-link-plist
  6605. (plist-put org-store-link-plist key value)))))
  6606. (defun org-email-link-description (&optional fmt)
  6607. "Return the description part of an email link.
  6608. This takes information from `org-store-link-plist' and formats it
  6609. according to FMT (default from `org-email-link-description-format')."
  6610. (setq fmt (or fmt org-email-link-description-format))
  6611. (let* ((p org-store-link-plist)
  6612. (to (plist-get p :toaddress))
  6613. (from (plist-get p :fromaddress))
  6614. (table
  6615. (list
  6616. (cons "%c" (plist-get p :fromto))
  6617. (cons "%F" (plist-get p :from))
  6618. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6619. (cons "%T" (plist-get p :to))
  6620. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6621. (cons "%s" (plist-get p :subject))
  6622. (cons "%m" (plist-get p :message-id)))))
  6623. (when (string-match "%c" fmt)
  6624. ;; Check if the user wrote this message
  6625. (if (and org-from-is-user-regexp from to
  6626. (save-match-data (string-match org-from-is-user-regexp from)))
  6627. (setq fmt (replace-match "to %t" t t fmt))
  6628. (setq fmt (replace-match "from %f" t t fmt))))
  6629. (org-replace-escapes fmt table)))
  6630. (defun org-make-org-heading-search-string (&optional string heading)
  6631. "Make search string for STRING or current headline."
  6632. (interactive)
  6633. (let ((s (or string (org-get-heading))))
  6634. (unless (and string (not heading))
  6635. ;; We are using a headline, clean up garbage in there.
  6636. (if (string-match org-todo-regexp s)
  6637. (setq s (replace-match "" t t s)))
  6638. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6639. (setq s (replace-match "" t t s)))
  6640. (setq s (org-trim s))
  6641. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6642. org-comment-string "\\)") s)
  6643. (setq s (replace-match "" t t s)))
  6644. (while (string-match org-ts-regexp s)
  6645. (setq s (replace-match "" t t s))))
  6646. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6647. (setq s (replace-match " " t t s)))
  6648. (or string (setq s (concat "*" s))) ; Add * for headlines
  6649. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6650. (defun org-make-link (&rest strings)
  6651. "Concatenate STRINGS."
  6652. (apply 'concat strings))
  6653. (defun org-make-link-string (link &optional description)
  6654. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6655. (unless (string-match "\\S-" link)
  6656. (error "Empty link"))
  6657. (when (stringp description)
  6658. ;; Remove brackets from the description, they are fatal.
  6659. (while (string-match "\\[" description)
  6660. (setq description (replace-match "{" t t description)))
  6661. (while (string-match "\\]" description)
  6662. (setq description (replace-match "}" t t description))))
  6663. (when (equal (org-link-escape link) description)
  6664. ;; No description needed, it is identical
  6665. (setq description nil))
  6666. (when (and (not description)
  6667. (not (equal link (org-link-escape link))))
  6668. (setq description (org-extract-attributes link)))
  6669. (concat "[[" (org-link-escape link) "]"
  6670. (if description (concat "[" description "]") "")
  6671. "]"))
  6672. (defconst org-link-escape-chars
  6673. '((?\ . "%20")
  6674. (?\[ . "%5B")
  6675. (?\] . "%5D")
  6676. (?\340 . "%E0") ; `a
  6677. (?\342 . "%E2") ; ^a
  6678. (?\347 . "%E7") ; ,c
  6679. (?\350 . "%E8") ; `e
  6680. (?\351 . "%E9") ; 'e
  6681. (?\352 . "%EA") ; ^e
  6682. (?\356 . "%EE") ; ^i
  6683. (?\364 . "%F4") ; ^o
  6684. (?\371 . "%F9") ; `u
  6685. (?\373 . "%FB") ; ^u
  6686. (?\; . "%3B")
  6687. (?? . "%3F")
  6688. (?= . "%3D")
  6689. (?+ . "%2B")
  6690. )
  6691. "Association list of escapes for some characters problematic in links.
  6692. This is the list that is used for internal purposes.")
  6693. (defvar org-url-encoding-use-url-hexify nil)
  6694. (defconst org-link-escape-chars-browser
  6695. '((?\ . "%20")) ; 32 for the SPC char
  6696. "Association list of escapes for some characters problematic in links.
  6697. This is the list that is used before handing over to the browser.")
  6698. (defun org-link-escape (text &optional table)
  6699. "Escape characters in TEXT that are problematic for links."
  6700. (if org-url-encoding-use-url-hexify
  6701. (url-hexify-string text)
  6702. (setq table (or table org-link-escape-chars))
  6703. (when text
  6704. (let ((re (mapconcat (lambda (x) (regexp-quote
  6705. (char-to-string (car x))))
  6706. table "\\|")))
  6707. (while (string-match re text)
  6708. (setq text
  6709. (replace-match
  6710. (cdr (assoc (string-to-char (match-string 0 text))
  6711. table))
  6712. t t text)))
  6713. text))))
  6714. (defun org-link-unescape (text &optional table)
  6715. "Reverse the action of `org-link-escape'."
  6716. (if org-url-encoding-use-url-hexify
  6717. (url-unhex-string text)
  6718. (setq table (or table org-link-escape-chars))
  6719. (when text
  6720. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6721. table "\\|")))
  6722. (while (string-match re text)
  6723. (setq text
  6724. (replace-match
  6725. (char-to-string (car (rassoc (match-string 0 text) table)))
  6726. t t text)))
  6727. text))))
  6728. (defun org-xor (a b)
  6729. "Exclusive or."
  6730. (if a (not b) b))
  6731. (defun org-fixup-message-id-for-http (s)
  6732. "Replace special characters in a message id, so it can be used in an http query."
  6733. (while (string-match "<" s)
  6734. (setq s (replace-match "%3C" t t s)))
  6735. (while (string-match ">" s)
  6736. (setq s (replace-match "%3E" t t s)))
  6737. (while (string-match "@" s)
  6738. (setq s (replace-match "%40" t t s)))
  6739. s)
  6740. ;;;###autoload
  6741. (defun org-insert-link-global ()
  6742. "Insert a link like Org-mode does.
  6743. This command can be called in any mode to insert a link in Org-mode syntax."
  6744. (interactive)
  6745. (org-load-modules-maybe)
  6746. (org-run-like-in-org-mode 'org-insert-link))
  6747. (defun org-insert-link (&optional complete-file link-location)
  6748. "Insert a link. At the prompt, enter the link.
  6749. Completion can be used to insert any of the link protocol prefixes like
  6750. http or ftp in use.
  6751. The history can be used to select a link previously stored with
  6752. `org-store-link'. When the empty string is entered (i.e. if you just
  6753. press RET at the prompt), the link defaults to the most recently
  6754. stored link. As SPC triggers completion in the minibuffer, you need to
  6755. use M-SPC or C-q SPC to force the insertion of a space character.
  6756. You will also be prompted for a description, and if one is given, it will
  6757. be displayed in the buffer instead of the link.
  6758. If there is already a link at point, this command will allow you to edit link
  6759. and description parts.
  6760. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6761. be selected using completion. The path to the file will be relative to the
  6762. current directory if the file is in the current directory or a subdirectory.
  6763. Otherwise, the link will be the absolute path as completed in the minibuffer
  6764. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6765. option `org-link-file-path-type'.
  6766. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6767. the current directory or below.
  6768. With three \\[universal-argument] prefixes, negate the meaning of
  6769. `org-keep-stored-link-after-insertion'.
  6770. If `org-make-link-description-function' is non-nil, this function will be
  6771. called with the link target, and the result will be the default
  6772. link description.
  6773. If the LINK-LOCATION parameter is non-nil, this value will be
  6774. used as the link location instead of reading one interactively."
  6775. (interactive "P")
  6776. (let* ((wcf (current-window-configuration))
  6777. (region (if (org-region-active-p)
  6778. (buffer-substring (region-beginning) (region-end))))
  6779. (remove (and region (list (region-beginning) (region-end))))
  6780. (desc region)
  6781. tmphist ; byte-compile incorrectly complains about this
  6782. (link link-location)
  6783. entry file all-prefixes)
  6784. (cond
  6785. (link-location) ; specified by arg, just use it.
  6786. ((org-in-regexp org-bracket-link-regexp 1)
  6787. ;; We do have a link at point, and we are going to edit it.
  6788. (setq remove (list (match-beginning 0) (match-end 0)))
  6789. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6790. (setq link (read-string "Link: "
  6791. (org-link-unescape
  6792. (org-match-string-no-properties 1)))))
  6793. ((or (org-in-regexp org-angle-link-re)
  6794. (org-in-regexp org-plain-link-re))
  6795. ;; Convert to bracket link
  6796. (setq remove (list (match-beginning 0) (match-end 0))
  6797. link (read-string "Link: "
  6798. (org-remove-angle-brackets (match-string 0)))))
  6799. ((member complete-file '((4) (16)))
  6800. ;; Completing read for file names.
  6801. (setq link (org-file-complete-link complete-file)))
  6802. (t
  6803. ;; Read link, with completion for stored links.
  6804. (with-output-to-temp-buffer "*Org Links*"
  6805. (princ "Insert a link.
  6806. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6807. (when org-stored-links
  6808. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6809. (princ (mapconcat
  6810. (lambda (x)
  6811. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6812. (reverse org-stored-links) "\n"))))
  6813. (let ((cw (selected-window)))
  6814. (select-window (get-buffer-window "*Org Links*"))
  6815. (setq truncate-lines t)
  6816. (unless (pos-visible-in-window-p (point-max))
  6817. (org-fit-window-to-buffer))
  6818. (and (window-live-p cw) (select-window cw)))
  6819. ;; Fake a link history, containing the stored links.
  6820. (setq tmphist (append (mapcar 'car org-stored-links)
  6821. org-insert-link-history))
  6822. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6823. (mapcar 'car org-link-abbrev-alist)
  6824. org-link-types))
  6825. (unwind-protect
  6826. (progn
  6827. (setq link
  6828. (let ((org-completion-use-ido nil)
  6829. (org-completion-use-iswitchb nil))
  6830. (org-completing-read
  6831. "Link: "
  6832. (append
  6833. (mapcar (lambda (x) (list (concat x ":")))
  6834. all-prefixes)
  6835. (mapcar 'car org-stored-links))
  6836. nil nil nil
  6837. 'tmphist
  6838. (car (car org-stored-links)))))
  6839. (if (or (member link all-prefixes)
  6840. (and (equal ":" (substring link -1))
  6841. (member (substring link 0 -1) all-prefixes)
  6842. (setq link (substring link 0 -1))))
  6843. (setq link (org-link-try-special-completion link))))
  6844. (set-window-configuration wcf)
  6845. (kill-buffer "*Org Links*"))
  6846. (setq entry (assoc link org-stored-links))
  6847. (or entry (push link org-insert-link-history))
  6848. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6849. (not org-keep-stored-link-after-insertion))
  6850. (setq org-stored-links (delq (assoc link org-stored-links)
  6851. org-stored-links)))
  6852. (setq desc (or desc (nth 1 entry)))))
  6853. (if (string-match org-plain-link-re link)
  6854. ;; URL-like link, normalize the use of angular brackets.
  6855. (setq link (org-make-link (org-remove-angle-brackets link))))
  6856. ;; Check if we are linking to the current file with a search option
  6857. ;; If yes, simplify the link by using only the search option.
  6858. (when (and buffer-file-name
  6859. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6860. (let* ((path (match-string 1 link))
  6861. (case-fold-search nil)
  6862. (search (match-string 2 link)))
  6863. (save-match-data
  6864. (if (equal (file-truename buffer-file-name) (file-truename path))
  6865. ;; We are linking to this same file, with a search option
  6866. (setq link search)))))
  6867. ;; Check if we can/should use a relative path. If yes, simplify the link
  6868. (when (string-match "^file:\\(.*\\)" link)
  6869. (let* ((path (match-string 1 link))
  6870. (origpath path)
  6871. (case-fold-search nil))
  6872. (cond
  6873. ((or (eq org-link-file-path-type 'absolute)
  6874. (equal complete-file '(16)))
  6875. (setq path (abbreviate-file-name (expand-file-name path))))
  6876. ((eq org-link-file-path-type 'noabbrev)
  6877. (setq path (expand-file-name path)))
  6878. ((eq org-link-file-path-type 'relative)
  6879. (setq path (file-relative-name path)))
  6880. (t
  6881. (save-match-data
  6882. (if (string-match (concat "^" (regexp-quote
  6883. (file-name-as-directory
  6884. (expand-file-name "."))))
  6885. (expand-file-name path))
  6886. ;; We are linking a file with relative path name.
  6887. (setq path (substring (expand-file-name path)
  6888. (match-end 0)))
  6889. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6890. (setq link (concat "file:" path))
  6891. (if (equal desc origpath)
  6892. (setq desc path))))
  6893. (if org-make-link-description-function
  6894. (setq desc (funcall org-make-link-description-function link desc)))
  6895. (setq desc (read-string "Description: " desc))
  6896. (unless (string-match "\\S-" desc) (setq desc nil))
  6897. (if remove (apply 'delete-region remove))
  6898. (insert (org-make-link-string link desc))))
  6899. (defun org-link-try-special-completion (type)
  6900. "If there is completion support for link type TYPE, offer it."
  6901. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6902. (if (functionp fun)
  6903. (funcall fun)
  6904. (read-string "Link (no completion support): " (concat type ":")))))
  6905. (defun org-file-complete-link (&optional arg)
  6906. "Create a file link using completion."
  6907. (let (file link)
  6908. (setq file (read-file-name "File: "))
  6909. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6910. (pwd1 (file-name-as-directory (abbreviate-file-name
  6911. (expand-file-name ".")))))
  6912. (cond
  6913. ((equal arg '(16))
  6914. (setq link (org-make-link
  6915. "file:"
  6916. (abbreviate-file-name (expand-file-name file)))))
  6917. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6918. (setq link (org-make-link "file:" (match-string 1 file))))
  6919. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6920. (expand-file-name file))
  6921. (setq link (org-make-link
  6922. "file:" (match-string 1 (expand-file-name file)))))
  6923. (t (setq link (org-make-link "file:" file)))))
  6924. link))
  6925. (defun org-completing-read (&rest args)
  6926. "Completing-read with SPACE being a normal character."
  6927. (let ((minibuffer-local-completion-map
  6928. (copy-keymap minibuffer-local-completion-map)))
  6929. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6930. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6931. (apply 'org-icompleting-read args)))
  6932. (defun org-completing-read-no-i (&rest args)
  6933. (let (org-completion-use-ido org-completion-use-iswitchb)
  6934. (apply 'org-completing-read args)))
  6935. (defun org-iswitchb-completing-read (prompt choices &rest args)
  6936. "Use iswitch as a completing-read replacement to choose from choices.
  6937. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  6938. from."
  6939. (let* ((switchb-use-virtual-buffers nil)
  6940. (iswitchb-make-buflist-hook
  6941. (lambda ()
  6942. (setq iswitchb-temp-buflist choices))))
  6943. (iswitchb-read-buffer prompt)))
  6944. (defun org-icompleting-read (&rest args)
  6945. "Completing-read using `ido-mode' or `iswitchb' speedups if available"
  6946. (if (and org-completion-use-ido
  6947. (fboundp 'ido-completing-read)
  6948. (boundp 'ido-mode) ido-mode
  6949. (listp (second args)))
  6950. (let ((ido-enter-matching-directory nil))
  6951. (apply 'ido-completing-read (concat (car args))
  6952. (if (consp (car (nth 1 args)))
  6953. (mapcar (lambda (x) (car x)) (nth 1 args))
  6954. (nth 1 args))
  6955. (cddr args)))
  6956. (if (and org-completion-use-iswitchb
  6957. (boundp 'iswitchb-mode) iswitchb-mode
  6958. (listp (second args)))
  6959. (apply 'org-iswitchb-completing-read (concat (car args))
  6960. (mapcar (lambda (x) (car x)) (nth 1 args))
  6961. (cddr args))
  6962. (apply 'completing-read args))))
  6963. (defun org-extract-attributes (s)
  6964. "Extract the attributes cookie from a string and set as text property."
  6965. (let (a attr (start 0) key value)
  6966. (save-match-data
  6967. (when (string-match "{{\\([^}]+\\)}}$" s)
  6968. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6969. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6970. (setq key (match-string 1 a) value (match-string 2 a)
  6971. start (match-end 0)
  6972. attr (plist-put attr (intern key) value))))
  6973. (org-add-props s nil 'org-attr attr))
  6974. s))
  6975. (defun org-extract-attributes-from-string (tag)
  6976. (let (key value attr)
  6977. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6978. (setq key (match-string 1 tag) value (match-string 2 tag)
  6979. tag (replace-match "" t t tag)
  6980. attr (plist-put attr (intern key) value)))
  6981. (cons tag attr)))
  6982. (defun org-attributes-to-string (plist)
  6983. "Format a property list into an HTML attribute list."
  6984. (let ((s "") key value)
  6985. (while plist
  6986. (setq key (pop plist) value (pop plist))
  6987. (and value
  6988. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6989. s))
  6990. ;;; Opening/following a link
  6991. (defvar org-link-search-failed nil)
  6992. (defun org-next-link ()
  6993. "Move forward to the next link.
  6994. If the link is in hidden text, expose it."
  6995. (interactive)
  6996. (when (and org-link-search-failed (eq this-command last-command))
  6997. (goto-char (point-min))
  6998. (message "Link search wrapped back to beginning of buffer"))
  6999. (setq org-link-search-failed nil)
  7000. (let* ((pos (point))
  7001. (ct (org-context))
  7002. (a (assoc :link ct)))
  7003. (if a (goto-char (nth 2 a)))
  7004. (if (re-search-forward org-any-link-re nil t)
  7005. (progn
  7006. (goto-char (match-beginning 0))
  7007. (if (org-invisible-p) (org-show-context)))
  7008. (goto-char pos)
  7009. (setq org-link-search-failed t)
  7010. (error "No further link found"))))
  7011. (defun org-previous-link ()
  7012. "Move backward to the previous link.
  7013. If the link is in hidden text, expose it."
  7014. (interactive)
  7015. (when (and org-link-search-failed (eq this-command last-command))
  7016. (goto-char (point-max))
  7017. (message "Link search wrapped back to end of buffer"))
  7018. (setq org-link-search-failed nil)
  7019. (let* ((pos (point))
  7020. (ct (org-context))
  7021. (a (assoc :link ct)))
  7022. (if a (goto-char (nth 1 a)))
  7023. (if (re-search-backward org-any-link-re nil t)
  7024. (progn
  7025. (goto-char (match-beginning 0))
  7026. (if (org-invisible-p) (org-show-context)))
  7027. (goto-char pos)
  7028. (setq org-link-search-failed t)
  7029. (error "No further link found"))))
  7030. (defun org-translate-link (s)
  7031. "Translate a link string if a translation function has been defined."
  7032. (if (and org-link-translation-function
  7033. (fboundp org-link-translation-function)
  7034. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7035. (progn
  7036. (setq s (funcall org-link-translation-function
  7037. (match-string 1) (match-string 2)))
  7038. (concat (car s) ":" (cdr s)))
  7039. s))
  7040. (defun org-translate-link-from-planner (type path)
  7041. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7042. This is still an experimental function, your mileage may vary."
  7043. (cond
  7044. ((member type '("http" "https" "news" "ftp"))
  7045. ;; standard Internet links are the same.
  7046. nil)
  7047. ((and (equal type "irc") (string-match "^//" path))
  7048. ;; Planner has two / at the beginning of an irc link, we have 1.
  7049. ;; We should have zero, actually....
  7050. (setq path (substring path 1)))
  7051. ((and (equal type "lisp") (string-match "^/" path))
  7052. ;; Planner has a slash, we do not.
  7053. (setq type "elisp" path (substring path 1)))
  7054. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7055. ;; A typical message link. Planner has the id after the fina slash,
  7056. ;; we separate it with a hash mark
  7057. (setq path (concat (match-string 1 path) "#"
  7058. (org-remove-angle-brackets (match-string 2 path)))))
  7059. )
  7060. (cons type path))
  7061. (defun org-find-file-at-mouse (ev)
  7062. "Open file link or URL at mouse."
  7063. (interactive "e")
  7064. (mouse-set-point ev)
  7065. (org-open-at-point 'in-emacs))
  7066. (defun org-open-at-mouse (ev)
  7067. "Open file link or URL at mouse."
  7068. (interactive "e")
  7069. (mouse-set-point ev)
  7070. (if (eq major-mode 'org-agenda-mode)
  7071. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7072. (org-open-at-point))
  7073. (defvar org-window-config-before-follow-link nil
  7074. "The window configuration before following a link.
  7075. This is saved in case the need arises to restore it.")
  7076. (defvar org-open-link-marker (make-marker)
  7077. "Marker pointing to the location where `org-open-at-point; was called.")
  7078. ;;;###autoload
  7079. (defun org-open-at-point-global ()
  7080. "Follow a link like Org-mode does.
  7081. This command can be called in any mode to follow a link that has
  7082. Org-mode syntax."
  7083. (interactive)
  7084. (org-run-like-in-org-mode 'org-open-at-point))
  7085. ;;;###autoload
  7086. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7087. "Open a link in the string S, as if it was in Org-mode."
  7088. (interactive "sLink: \nP")
  7089. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7090. (with-temp-buffer
  7091. (let ((org-inhibit-startup t))
  7092. (org-mode)
  7093. (insert s)
  7094. (goto-char (point-min))
  7095. (org-open-at-point arg reference-buffer)))))
  7096. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7097. "Open link at or after point.
  7098. If there is no link at point, this function will search forward up to
  7099. the end of the current line.
  7100. Normally, files will be opened by an appropriate application. If the
  7101. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7102. With a double prefix argument, try to open outside of Emacs, in the
  7103. application the system uses for this file type."
  7104. (interactive "P")
  7105. (org-load-modules-maybe)
  7106. (move-marker org-open-link-marker (point))
  7107. (setq org-window-config-before-follow-link (current-window-configuration))
  7108. (org-remove-occur-highlights nil nil t)
  7109. (cond
  7110. ((and (org-on-heading-p)
  7111. (not (org-in-regexp
  7112. (concat org-plain-link-re "\\|"
  7113. org-bracket-link-regexp "\\|"
  7114. org-angle-link-re "\\|"
  7115. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7116. (org-offer-links-in-entry in-emacs))
  7117. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7118. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7119. (org-footnote-action))
  7120. (t
  7121. (let (type path link line search (pos (point)))
  7122. (catch 'match
  7123. (save-excursion
  7124. (skip-chars-forward "^]\n\r")
  7125. (when (org-in-regexp org-bracket-link-regexp)
  7126. (setq link (org-extract-attributes
  7127. (org-link-unescape (org-match-string-no-properties 1))))
  7128. (while (string-match " *\n *" link)
  7129. (setq link (replace-match " " t t link)))
  7130. (setq link (org-link-expand-abbrev link))
  7131. (cond
  7132. ((or (file-name-absolute-p link)
  7133. (string-match "^\\.\\.?/" link))
  7134. (setq type "file" path link))
  7135. ((string-match org-link-re-with-space3 link)
  7136. (setq type (match-string 1 link) path (match-string 2 link)))
  7137. (t (setq type "thisfile" path link)))
  7138. (throw 'match t)))
  7139. (when (get-text-property (point) 'org-linked-text)
  7140. (setq type "thisfile"
  7141. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7142. (1+ (point)) (point))
  7143. path (buffer-substring
  7144. (previous-single-property-change pos 'org-linked-text)
  7145. (next-single-property-change pos 'org-linked-text)))
  7146. (throw 'match t))
  7147. (save-excursion
  7148. (when (or (org-in-regexp org-angle-link-re)
  7149. (org-in-regexp org-plain-link-re))
  7150. (setq type (match-string 1) path (match-string 2))
  7151. (throw 'match t)))
  7152. (save-excursion
  7153. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7154. (setq type "tags"
  7155. path (match-string 1))
  7156. (while (string-match ":" path)
  7157. (setq path (replace-match "+" t t path)))
  7158. (throw 'match t)))
  7159. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7160. (setq type "tree-match"
  7161. path (match-string 1))
  7162. (throw 'match t)))
  7163. (unless path
  7164. (error "No link found"))
  7165. ;; switch back to reference buffer
  7166. ;; needed when if called in a temporary buffer through
  7167. ;; org-open-link-from-string
  7168. (with-current-buffer (or reference-buffer (current-buffer))
  7169. ;; Remove any trailing spaces in path
  7170. (if (string-match " +\\'" path)
  7171. (setq path (replace-match "" t t path)))
  7172. (if (and org-link-translation-function
  7173. (fboundp org-link-translation-function))
  7174. ;; Check if we need to translate the link
  7175. (let ((tmp (funcall org-link-translation-function type path)))
  7176. (setq type (car tmp) path (cdr tmp))))
  7177. (cond
  7178. ((assoc type org-link-protocols)
  7179. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7180. ((equal type "mailto")
  7181. (let ((cmd (car org-link-mailto-program))
  7182. (args (cdr org-link-mailto-program)) args1
  7183. (address path) (subject "") a)
  7184. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7185. (setq address (match-string 1 path)
  7186. subject (org-link-escape (match-string 2 path))))
  7187. (while args
  7188. (cond
  7189. ((not (stringp (car args))) (push (pop args) args1))
  7190. (t (setq a (pop args))
  7191. (if (string-match "%a" a)
  7192. (setq a (replace-match address t t a)))
  7193. (if (string-match "%s" a)
  7194. (setq a (replace-match subject t t a)))
  7195. (push a args1))))
  7196. (apply cmd (nreverse args1))))
  7197. ((member type '("http" "https" "ftp" "news"))
  7198. (browse-url (concat type ":" (org-link-escape
  7199. path org-link-escape-chars-browser))))
  7200. ((member type '("message"))
  7201. (browse-url (concat type ":" path)))
  7202. ((string= type "tags")
  7203. (org-tags-view in-emacs path))
  7204. ((string= type "thisfile")
  7205. (if in-emacs
  7206. (switch-to-buffer-other-window
  7207. (org-get-buffer-for-internal-link (current-buffer)))
  7208. (org-mark-ring-push))
  7209. (let ((cmd `(org-link-search
  7210. ,path
  7211. ,(cond ((equal in-emacs '(4)) 'occur)
  7212. ((equal in-emacs '(16)) 'org-occur)
  7213. (t nil))
  7214. ,pos)))
  7215. (condition-case nil (eval cmd)
  7216. (error (progn (widen) (eval cmd))))))
  7217. ((string= type "tree-match")
  7218. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7219. ((string= type "file")
  7220. (if (string-match "::\\([0-9]+\\)\\'" path)
  7221. (setq line (string-to-number (match-string 1 path))
  7222. path (substring path 0 (match-beginning 0)))
  7223. (if (string-match "::\\(.+\\)\\'" path)
  7224. (setq search (match-string 1 path)
  7225. path (substring path 0 (match-beginning 0)))))
  7226. (if (string-match "[*?{]" (file-name-nondirectory path))
  7227. (dired path)
  7228. (org-open-file path in-emacs line search)))
  7229. ((string= type "news")
  7230. (require 'org-gnus)
  7231. (org-gnus-follow-link path))
  7232. ((string= type "shell")
  7233. (let ((cmd path))
  7234. (if (or (not org-confirm-shell-link-function)
  7235. (funcall org-confirm-shell-link-function
  7236. (format "Execute \"%s\" in shell? "
  7237. (org-add-props cmd nil
  7238. 'face 'org-warning))))
  7239. (progn
  7240. (message "Executing %s" cmd)
  7241. (shell-command cmd))
  7242. (error "Abort"))))
  7243. ((string= type "elisp")
  7244. (let ((cmd path))
  7245. (if (or (not org-confirm-elisp-link-function)
  7246. (funcall org-confirm-elisp-link-function
  7247. (format "Execute \"%s\" as elisp? "
  7248. (org-add-props cmd nil
  7249. 'face 'org-warning))))
  7250. (message "%s => %s" cmd
  7251. (if (equal (string-to-char cmd) ?\()
  7252. (eval (read cmd))
  7253. (call-interactively (read cmd))))
  7254. (error "Abort"))))
  7255. (t
  7256. (browse-url-at-point))))))
  7257. (move-marker org-open-link-marker nil)
  7258. (run-hook-with-args 'org-follow-link-hook)))
  7259. (defun org-offer-links-in-entry (&optional nth zero)
  7260. "Offer links in the curren entry and follow the selected link.
  7261. If there is only one link, follow it immediately as well.
  7262. If NTH is an integer, immediately pick the NTH link found.
  7263. If ZERO is a string, check also this string for a link, and if
  7264. there is one, offer it as link number zero."
  7265. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7266. "\\(" org-angle-link-re "\\)\\|"
  7267. "\\(" org-plain-link-re "\\)"))
  7268. (cnt ?0)
  7269. (in-emacs (if (integerp nth) nil nth))
  7270. have-zero end links link c)
  7271. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7272. (push (match-string 0 zero) links)
  7273. (setq cnt (1- cnt) have-zero t))
  7274. (save-excursion
  7275. (org-back-to-heading t)
  7276. (setq end (save-excursion (outline-next-heading) (point)))
  7277. (while (re-search-forward re end t)
  7278. (push (match-string 0) links))
  7279. (setq links (org-uniquify (reverse links))))
  7280. (cond
  7281. ((null links) (error "No links"))
  7282. ((equal (length links) 1)
  7283. (setq link (car links)))
  7284. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7285. (setq link (nth (if have-zero nth (1- nth)) links)))
  7286. (t ; we have to select a link
  7287. (save-excursion
  7288. (save-window-excursion
  7289. (delete-other-windows)
  7290. (with-output-to-temp-buffer "*Select Link*"
  7291. (mapc (lambda (l)
  7292. (if (not (string-match org-bracket-link-regexp l))
  7293. (princ (format "[%c] %s\n" (incf cnt)
  7294. (org-remove-angle-brackets l)))
  7295. (if (match-end 3)
  7296. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7297. (match-string 3 l) (match-string 1 l)))
  7298. (princ (format "[%c] %s\n" (incf cnt)
  7299. (match-string 1 l))))))
  7300. links))
  7301. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7302. (message "Select link to open:")
  7303. (setq c (read-char-exclusive))
  7304. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7305. (when (equal c ?q) (error "Abort"))
  7306. (setq nth (- c ?0))
  7307. (if have-zero (setq nth (1+ nth)))
  7308. (unless (and (integerp nth) (>= (length links) nth))
  7309. (error "Invalid link selection"))
  7310. (setq link (nth (1- nth) links))))
  7311. (org-open-link-from-string link in-emacs (current-buffer))))
  7312. ;;;; Time estimates
  7313. (defun org-get-effort (&optional pom)
  7314. "Get the effort estimate for the current entry."
  7315. (org-entry-get pom org-effort-property))
  7316. ;;; File search
  7317. (defvar org-create-file-search-functions nil
  7318. "List of functions to construct the right search string for a file link.
  7319. These functions are called in turn with point at the location to
  7320. which the link should point.
  7321. A function in the hook should first test if it would like to
  7322. handle this file type, for example by checking the major-mode or
  7323. the file extension. If it decides not to handle this file, it
  7324. should just return nil to give other functions a chance. If it
  7325. does handle the file, it must return the search string to be used
  7326. when following the link. The search string will be part of the
  7327. file link, given after a double colon, and `org-open-at-point'
  7328. will automatically search for it. If special measures must be
  7329. taken to make the search successful, another function should be
  7330. added to the companion hook `org-execute-file-search-functions',
  7331. which see.
  7332. A function in this hook may also use `setq' to set the variable
  7333. `description' to provide a suggestion for the descriptive text to
  7334. be used for this link when it gets inserted into an Org-mode
  7335. buffer with \\[org-insert-link].")
  7336. (defvar org-execute-file-search-functions nil
  7337. "List of functions to execute a file search triggered by a link.
  7338. Functions added to this hook must accept a single argument, the
  7339. search string that was part of the file link, the part after the
  7340. double colon. The function must first check if it would like to
  7341. handle this search, for example by checking the major-mode or the
  7342. file extension. If it decides not to handle this search, it
  7343. should just return nil to give other functions a chance. If it
  7344. does handle the search, it must return a non-nil value to keep
  7345. other functions from trying.
  7346. Each function can access the current prefix argument through the
  7347. variable `current-prefix-argument'. Note that a single prefix is
  7348. used to force opening a link in Emacs, so it may be good to only
  7349. use a numeric or double prefix to guide the search function.
  7350. In case this is needed, a function in this hook can also restore
  7351. the window configuration before `org-open-at-point' was called using:
  7352. (set-window-configuration org-window-config-before-follow-link)")
  7353. (defun org-link-search (s &optional type avoid-pos)
  7354. "Search for a link search option.
  7355. If S is surrounded by forward slashes, it is interpreted as a
  7356. regular expression. In org-mode files, this will create an `org-occur'
  7357. sparse tree. In ordinary files, `occur' will be used to list matches.
  7358. If the current buffer is in `dired-mode', grep will be used to search
  7359. in all files. If AVOID-POS is given, ignore matches near that position."
  7360. (let ((case-fold-search t)
  7361. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7362. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7363. (append '(("") (" ") ("\t") ("\n"))
  7364. org-emphasis-alist)
  7365. "\\|") "\\)"))
  7366. (pos (point))
  7367. (pre nil) (post nil)
  7368. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7369. (cond
  7370. ;; First check if there are any special
  7371. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7372. ;; Now try the builtin stuff
  7373. ((and (equal (string-to-char s0) ?#)
  7374. (> (length s0) 1)
  7375. (save-excursion
  7376. (goto-char (point-min))
  7377. (and
  7378. (re-search-forward
  7379. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7380. (setq type 'dedicated
  7381. pos (match-beginning 0))))
  7382. ;; There is an exact target for this
  7383. (goto-char pos)
  7384. (org-back-to-heading t)))
  7385. ((save-excursion
  7386. (goto-char (point-min))
  7387. (and
  7388. (re-search-forward
  7389. (concat "<<" (regexp-quote s0) ">>") nil t)
  7390. (setq type 'dedicated
  7391. pos (match-beginning 0))))
  7392. ;; There is an exact target for this
  7393. (goto-char pos))
  7394. ((and (string-match "^(\\(.*\\))$" s0)
  7395. (save-excursion
  7396. (goto-char (point-min))
  7397. (and
  7398. (re-search-forward
  7399. (concat "[^[]" (regexp-quote
  7400. (format org-coderef-label-format
  7401. (match-string 1 s0))))
  7402. nil t)
  7403. (setq type 'dedicated
  7404. pos (1+ (match-beginning 0))))))
  7405. ;; There is a coderef target for this
  7406. (goto-char pos))
  7407. ((string-match "^/\\(.*\\)/$" s)
  7408. ;; A regular expression
  7409. (cond
  7410. ((org-mode-p)
  7411. (org-occur (match-string 1 s)))
  7412. ;;((eq major-mode 'dired-mode)
  7413. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7414. (t (org-do-occur (match-string 1 s)))))
  7415. (t
  7416. ;; A normal search strings
  7417. (when (equal (string-to-char s) ?*)
  7418. ;; Anchor on headlines, post may include tags.
  7419. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7420. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7421. s (substring s 1)))
  7422. (remove-text-properties
  7423. 0 (length s)
  7424. '(face nil mouse-face nil keymap nil fontified nil) s)
  7425. ;; Make a series of regular expressions to find a match
  7426. (setq words (org-split-string s "[ \n\r\t]+")
  7427. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7428. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7429. "\\)" markers)
  7430. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7431. re2a (concat "[ \t\r\n]" re2a_)
  7432. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7433. re4 (concat "[^a-zA-Z_]" re4_)
  7434. re1 (concat pre re2 post)
  7435. re3 (concat pre (if pre re4_ re4) post)
  7436. re5 (concat pre ".*" re4)
  7437. re2 (concat pre re2)
  7438. re2a (concat pre (if pre re2a_ re2a))
  7439. re4 (concat pre (if pre re4_ re4))
  7440. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7441. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7442. re5 "\\)"
  7443. ))
  7444. (cond
  7445. ((eq type 'org-occur) (org-occur reall))
  7446. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7447. (t (goto-char (point-min))
  7448. (setq type 'fuzzy)
  7449. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7450. (org-search-not-self 1 re1 nil t)
  7451. (org-search-not-self 1 re2 nil t)
  7452. (org-search-not-self 1 re2a nil t)
  7453. (org-search-not-self 1 re3 nil t)
  7454. (org-search-not-self 1 re4 nil t)
  7455. (org-search-not-self 1 re5 nil t)
  7456. )
  7457. (goto-char (match-beginning 1))
  7458. (goto-char pos)
  7459. (error "No match")))))
  7460. (t
  7461. ;; Normal string-search
  7462. (goto-char (point-min))
  7463. (if (search-forward s nil t)
  7464. (goto-char (match-beginning 0))
  7465. (error "No match"))))
  7466. (and (org-mode-p) (org-show-context 'link-search))
  7467. type))
  7468. (defun org-search-not-self (group &rest args)
  7469. "Execute `re-search-forward', but only accept matches that do not
  7470. enclose the position of `org-open-link-marker'."
  7471. (let ((m org-open-link-marker))
  7472. (catch 'exit
  7473. (while (apply 're-search-forward args)
  7474. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7475. (goto-char (match-end group))
  7476. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7477. (> (match-beginning 0) (marker-position m))
  7478. (< (match-end 0) (marker-position m)))
  7479. (save-match-data
  7480. (or (not (org-in-regexp
  7481. org-bracket-link-analytic-regexp 1))
  7482. (not (match-end 4)) ; no description
  7483. (and (<= (match-beginning 4) (point))
  7484. (>= (match-end 4) (point))))))
  7485. (throw 'exit (point))))))))
  7486. (defun org-get-buffer-for-internal-link (buffer)
  7487. "Return a buffer to be used for displaying the link target of internal links."
  7488. (cond
  7489. ((not org-display-internal-link-with-indirect-buffer)
  7490. buffer)
  7491. ((string-match "(Clone)$" (buffer-name buffer))
  7492. (message "Buffer is already a clone, not making another one")
  7493. ;; we also do not modify visibility in this case
  7494. buffer)
  7495. (t ; make a new indirect buffer for displaying the link
  7496. (let* ((bn (buffer-name buffer))
  7497. (ibn (concat bn "(Clone)"))
  7498. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7499. (with-current-buffer ib (org-overview))
  7500. ib))))
  7501. (defun org-do-occur (regexp &optional cleanup)
  7502. "Call the Emacs command `occur'.
  7503. If CLEANUP is non-nil, remove the printout of the regular expression
  7504. in the *Occur* buffer. This is useful if the regex is long and not useful
  7505. to read."
  7506. (occur regexp)
  7507. (when cleanup
  7508. (let ((cwin (selected-window)) win beg end)
  7509. (when (setq win (get-buffer-window "*Occur*"))
  7510. (select-window win))
  7511. (goto-char (point-min))
  7512. (when (re-search-forward "match[a-z]+" nil t)
  7513. (setq beg (match-end 0))
  7514. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7515. (setq end (1- (match-beginning 0)))))
  7516. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7517. (goto-char (point-min))
  7518. (select-window cwin))))
  7519. ;;; The mark ring for links jumps
  7520. (defvar org-mark-ring nil
  7521. "Mark ring for positions before jumps in Org-mode.")
  7522. (defvar org-mark-ring-last-goto nil
  7523. "Last position in the mark ring used to go back.")
  7524. ;; Fill and close the ring
  7525. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7526. (loop for i from 1 to org-mark-ring-length do
  7527. (push (make-marker) org-mark-ring))
  7528. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7529. org-mark-ring)
  7530. (defun org-mark-ring-push (&optional pos buffer)
  7531. "Put the current position or POS into the mark ring and rotate it."
  7532. (interactive)
  7533. (setq pos (or pos (point)))
  7534. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7535. (move-marker (car org-mark-ring)
  7536. (or pos (point))
  7537. (or buffer (current-buffer)))
  7538. (message "%s"
  7539. (substitute-command-keys
  7540. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7541. (defun org-mark-ring-goto (&optional n)
  7542. "Jump to the previous position in the mark ring.
  7543. With prefix arg N, jump back that many stored positions. When
  7544. called several times in succession, walk through the entire ring.
  7545. Org-mode commands jumping to a different position in the current file,
  7546. or to another Org-mode file, automatically push the old position
  7547. onto the ring."
  7548. (interactive "p")
  7549. (let (p m)
  7550. (if (eq last-command this-command)
  7551. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7552. (setq p org-mark-ring))
  7553. (setq org-mark-ring-last-goto p)
  7554. (setq m (car p))
  7555. (switch-to-buffer (marker-buffer m))
  7556. (goto-char m)
  7557. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7558. (defun org-remove-angle-brackets (s)
  7559. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7560. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7561. s)
  7562. (defun org-add-angle-brackets (s)
  7563. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7564. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7565. s)
  7566. (defun org-remove-double-quotes (s)
  7567. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7568. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7569. s)
  7570. ;;; Following specific links
  7571. (defun org-follow-timestamp-link ()
  7572. (cond
  7573. ((org-at-date-range-p t)
  7574. (let ((org-agenda-start-on-weekday)
  7575. (t1 (match-string 1))
  7576. (t2 (match-string 2)))
  7577. (setq t1 (time-to-days (org-time-string-to-time t1))
  7578. t2 (time-to-days (org-time-string-to-time t2)))
  7579. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7580. ((org-at-timestamp-p t)
  7581. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7582. (substring (match-string 1) 0 10)))
  7583. 1))
  7584. (t (error "This should not happen"))))
  7585. ;;; Following file links
  7586. (defvar org-wait nil)
  7587. (defun org-open-file (path &optional in-emacs line search)
  7588. "Open the file at PATH.
  7589. First, this expands any special file name abbreviations. Then the
  7590. configuration variable `org-file-apps' is checked if it contains an
  7591. entry for this file type, and if yes, the corresponding command is launched.
  7592. If no application is found, Emacs simply visits the file.
  7593. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7594. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7595. and o use an external application to visit the file.
  7596. Optional LINE specifies a line to go to, optional SEARCH a string to
  7597. search for. If LINE or SEARCH is given, the file will always be
  7598. opened in Emacs.
  7599. If the file does not exist, an error is thrown."
  7600. (setq in-emacs (or in-emacs line search))
  7601. (let* ((file (if (equal path "")
  7602. buffer-file-name
  7603. (substitute-in-file-name (expand-file-name path))))
  7604. (apps (append org-file-apps (org-default-apps)))
  7605. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7606. (dirp (if remp nil (file-directory-p file)))
  7607. (file (if (and dirp org-open-directory-means-index-dot-org)
  7608. (concat (file-name-as-directory file) "index.org")
  7609. file))
  7610. (a-m-a-p (assq 'auto-mode apps))
  7611. (dfile (downcase file))
  7612. (old-buffer (current-buffer))
  7613. (old-pos (point))
  7614. (old-mode major-mode)
  7615. ext cmd)
  7616. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7617. (setq ext (match-string 1 dfile))
  7618. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7619. (setq ext (match-string 1 dfile))))
  7620. (cond
  7621. ((equal in-emacs '(16))
  7622. (setq cmd (cdr (assoc 'system apps))))
  7623. (in-emacs (setq cmd 'emacs))
  7624. (t
  7625. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7626. (and dirp (cdr (assoc 'directory apps)))
  7627. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7628. 'string-match)
  7629. (cdr (assoc ext apps))
  7630. (cdr (assoc t apps))))))
  7631. (when (eq cmd 'system)
  7632. (setq cmd (cdr (assoc 'system apps))))
  7633. (when (eq cmd 'default)
  7634. (setq cmd (cdr (assoc t apps))))
  7635. (when (eq cmd 'mailcap)
  7636. (require 'mailcap)
  7637. (mailcap-parse-mailcaps)
  7638. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7639. (command (mailcap-mime-info mime-type)))
  7640. (if (stringp command)
  7641. (setq cmd command)
  7642. (setq cmd 'emacs))))
  7643. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7644. (not (file-exists-p file))
  7645. (not org-open-non-existing-files))
  7646. (error "No such file: %s" file))
  7647. (cond
  7648. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7649. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7650. (while (string-match "['\"]%s['\"]" cmd)
  7651. (setq cmd (replace-match "%s" t t cmd)))
  7652. (while (string-match "%s" cmd)
  7653. (setq cmd (replace-match
  7654. (save-match-data
  7655. (shell-quote-argument
  7656. (convert-standard-filename file)))
  7657. t t cmd)))
  7658. (save-window-excursion
  7659. (start-process-shell-command cmd nil cmd)
  7660. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7661. ))
  7662. ((or (stringp cmd)
  7663. (eq cmd 'emacs))
  7664. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7665. (widen)
  7666. (if line (org-goto-line line)
  7667. (if search (org-link-search search))))
  7668. ((consp cmd)
  7669. (let ((file (convert-standard-filename file)))
  7670. (eval cmd)))
  7671. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7672. (and (org-mode-p) (eq old-mode 'org-mode)
  7673. (or (not (equal old-buffer (current-buffer)))
  7674. (not (equal old-pos (point))))
  7675. (org-mark-ring-push old-pos old-buffer))))
  7676. (defun org-default-apps ()
  7677. "Return the default applications for this operating system."
  7678. (cond
  7679. ((eq system-type 'darwin)
  7680. org-file-apps-defaults-macosx)
  7681. ((eq system-type 'windows-nt)
  7682. org-file-apps-defaults-windowsnt)
  7683. (t org-file-apps-defaults-gnu)))
  7684. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7685. "Convert extensions to regular expressions in the cars of LIST.
  7686. Also, weed out any non-string entries, because the return value is used
  7687. only for regexp matching.
  7688. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7689. point to the symbol `emacs', indicating that the file should
  7690. be opened in Emacs."
  7691. (append
  7692. (delq nil
  7693. (mapcar (lambda (x)
  7694. (if (not (stringp (car x)))
  7695. nil
  7696. (if (string-match "\\W" (car x))
  7697. x
  7698. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7699. list))
  7700. (if add-auto-mode
  7701. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7702. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7703. (defun org-file-remote-p (file)
  7704. "Test whether FILE specifies a location on a remote system.
  7705. Return non-nil if the location is indeed remote.
  7706. For example, the filename \"/user@host:/foo\" specifies a location
  7707. on the system \"/user@host:\"."
  7708. (cond ((fboundp 'file-remote-p)
  7709. (file-remote-p file))
  7710. ((fboundp 'tramp-handle-file-remote-p)
  7711. (tramp-handle-file-remote-p file))
  7712. ((and (boundp 'ange-ftp-name-format)
  7713. (string-match (car ange-ftp-name-format) file))
  7714. t)
  7715. (t nil)))
  7716. ;;;; Refiling
  7717. (defun org-get-org-file ()
  7718. "Read a filename, with default directory `org-directory'."
  7719. (let ((default (or org-default-notes-file remember-data-file)))
  7720. (read-file-name (format "File name [%s]: " default)
  7721. (file-name-as-directory org-directory)
  7722. default)))
  7723. (defun org-notes-order-reversed-p ()
  7724. "Check if the current file should receive notes in reversed order."
  7725. (cond
  7726. ((not org-reverse-note-order) nil)
  7727. ((eq t org-reverse-note-order) t)
  7728. ((not (listp org-reverse-note-order)) nil)
  7729. (t (catch 'exit
  7730. (let ((all org-reverse-note-order)
  7731. entry)
  7732. (while (setq entry (pop all))
  7733. (if (string-match (car entry) buffer-file-name)
  7734. (throw 'exit (cdr entry))))
  7735. nil)))))
  7736. (defvar org-refile-target-table nil
  7737. "The list of refile targets, created by `org-refile'.")
  7738. (defvar org-agenda-new-buffers nil
  7739. "Buffers created to visit agenda files.")
  7740. (defun org-get-refile-targets (&optional default-buffer)
  7741. "Produce a table with refile targets."
  7742. (let ((case-fold-search nil)
  7743. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7744. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7745. targets txt re files f desc descre fast-path-p level pos0)
  7746. (message "Getting targets...")
  7747. (with-current-buffer (or default-buffer (current-buffer))
  7748. (while (setq entry (pop entries))
  7749. (setq files (car entry) desc (cdr entry))
  7750. (setq fast-path-p nil)
  7751. (cond
  7752. ((null files) (setq files (list (current-buffer))))
  7753. ((eq files 'org-agenda-files)
  7754. (setq files (org-agenda-files 'unrestricted)))
  7755. ((and (symbolp files) (fboundp files))
  7756. (setq files (funcall files)))
  7757. ((and (symbolp files) (boundp files))
  7758. (setq files (symbol-value files))))
  7759. (if (stringp files) (setq files (list files)))
  7760. (cond
  7761. ((eq (car desc) :tag)
  7762. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7763. ((eq (car desc) :todo)
  7764. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7765. ((eq (car desc) :regexp)
  7766. (setq descre (cdr desc)))
  7767. ((eq (car desc) :level)
  7768. (setq descre (concat "^\\*\\{" (number-to-string
  7769. (if org-odd-levels-only
  7770. (1- (* 2 (cdr desc)))
  7771. (cdr desc)))
  7772. "\\}[ \t]")))
  7773. ((eq (car desc) :maxlevel)
  7774. (setq fast-path-p t)
  7775. (setq descre (concat "^\\*\\{1," (number-to-string
  7776. (if org-odd-levels-only
  7777. (1- (* 2 (cdr desc)))
  7778. (cdr desc)))
  7779. "\\}[ \t]")))
  7780. (t (error "Bad refiling target description %s" desc)))
  7781. (while (setq f (pop files))
  7782. (save-excursion
  7783. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7784. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7785. (setq f (expand-file-name f))
  7786. (if (eq org-refile-use-outline-path 'file)
  7787. (push (list (file-name-nondirectory f) f nil nil) targets))
  7788. (save-excursion
  7789. (save-restriction
  7790. (widen)
  7791. (goto-char (point-min))
  7792. (while (re-search-forward descre nil t)
  7793. (goto-char (setq pos0 (point-at-bol)))
  7794. (catch 'next
  7795. (when org-refile-target-verify-function
  7796. (save-match-data
  7797. (or (funcall org-refile-target-verify-function)
  7798. (throw 'next t))))
  7799. (when (looking-at org-complex-heading-regexp)
  7800. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7801. txt (org-link-display-format (match-string 4))
  7802. re (concat "^" (regexp-quote
  7803. (buffer-substring (match-beginning 1)
  7804. (match-end 4)))))
  7805. (if (match-end 5) (setq re (concat re "[ \t]+"
  7806. (regexp-quote
  7807. (match-string 5)))))
  7808. (setq re (concat re "[ \t]*$"))
  7809. (when org-refile-use-outline-path
  7810. (setq txt (mapconcat 'org-protect-slash
  7811. (append
  7812. (if (eq org-refile-use-outline-path 'file)
  7813. (list (file-name-nondirectory
  7814. (buffer-file-name (buffer-base-buffer))))
  7815. (if (eq org-refile-use-outline-path 'full-file-path)
  7816. (list (buffer-file-name (buffer-base-buffer)))))
  7817. (org-get-outline-path fast-path-p level txt)
  7818. (list txt))
  7819. "/")))
  7820. (push (list txt f re (point)) targets)))
  7821. (when (= (point) pos0)
  7822. ;; verification function has not moved point
  7823. (goto-char (point-at-eol))))))))))
  7824. (message "Getting targets...done")
  7825. (nreverse targets)))
  7826. (defun org-protect-slash (s)
  7827. (while (string-match "/" s)
  7828. (setq s (replace-match "\\" t t s)))
  7829. s)
  7830. (defvar org-olpa (make-vector 20 nil))
  7831. (defun org-get-outline-path (&optional fastp level heading)
  7832. "Return the outline path to the current entry, as a list."
  7833. (if fastp
  7834. (progn
  7835. (if (> level 19)
  7836. (error "Outline path failure, more than 19 levels."))
  7837. (loop for i from level upto 19 do
  7838. (aset org-olpa i nil))
  7839. (prog1
  7840. (delq nil (append org-olpa nil))
  7841. (aset org-olpa level heading)))
  7842. (let (rtn)
  7843. (save-excursion
  7844. (while (org-up-heading-safe)
  7845. (when (looking-at org-complex-heading-regexp)
  7846. (push (org-match-string-no-properties 4) rtn)))
  7847. rtn))))
  7848. (defvar org-refile-history nil
  7849. "History for refiling operations.")
  7850. (defvar org-after-refile-insert-hook nil
  7851. "Hook run after `org-refile' has inserted its stuff at the new location.
  7852. Note that this is still *before* the stuff will be removed from
  7853. the *old* location.")
  7854. (defun org-refile (&optional goto default-buffer rfloc)
  7855. "Move the entry at point to another heading.
  7856. The list of target headings is compiled using the information in
  7857. `org-refile-targets', which see. This list is created before each use
  7858. and will therefore always be up-to-date.
  7859. At the target location, the entry is filed as a subitem of the target heading.
  7860. Depending on `org-reverse-note-order', the new subitem will either be the
  7861. first or the last subitem.
  7862. If there is an active region, all entries in that region will be moved.
  7863. However, the region must fulfil the requirement that the first heading
  7864. is the first one sets the top-level of the moved text - at most siblings
  7865. below it are allowed.
  7866. With prefix arg GOTO, the command will only visit the target location,
  7867. not actually move anything.
  7868. With a double prefix `C-u C-u', go to the location where the last refiling
  7869. operation has put the subtree.
  7870. RFLOC can be a refile location obtained in a different way.
  7871. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7872. (interactive "P")
  7873. (let* ((cbuf (current-buffer))
  7874. (regionp (org-region-active-p))
  7875. (region-start (and regionp (region-beginning)))
  7876. (region-end (and regionp (region-end)))
  7877. (region-length (and regionp (- region-end region-start)))
  7878. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7879. pos it nbuf file re level reversed)
  7880. (when regionp
  7881. (goto-char region-start)
  7882. (or (bolp) (goto-char (point-at-bol)))
  7883. (setq region-start (point))
  7884. (unless (org-kill-is-subtree-p
  7885. (buffer-substring region-start region-end))
  7886. (error "The region is not a (sequence of) subtree(s)")))
  7887. (if (equal goto '(16))
  7888. (org-refile-goto-last-stored)
  7889. (when (setq it (or rfloc
  7890. (save-excursion
  7891. (org-refile-get-location
  7892. (if goto "Goto: " "Refile to: ") default-buffer
  7893. org-refile-allow-creating-parent-nodes))))
  7894. (setq file (nth 1 it)
  7895. re (nth 2 it)
  7896. pos (nth 3 it))
  7897. (if (and (not goto)
  7898. pos
  7899. (equal (buffer-file-name) file)
  7900. (if regionp
  7901. (and (>= pos region-start)
  7902. (<= pos region-end))
  7903. (and (>= pos (point))
  7904. (< pos (save-excursion
  7905. (org-end-of-subtree t t))))))
  7906. (error "Cannot refile to position inside the tree or region"))
  7907. (setq nbuf (or (find-buffer-visiting file)
  7908. (find-file-noselect file)))
  7909. (if goto
  7910. (progn
  7911. (switch-to-buffer nbuf)
  7912. (goto-char pos)
  7913. (org-show-context 'org-goto))
  7914. (if regionp
  7915. (progn
  7916. (org-kill-new (buffer-substring region-start region-end))
  7917. (org-save-markers-in-region region-start region-end))
  7918. (org-copy-subtree 1 nil t))
  7919. (save-excursion
  7920. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7921. (find-file-noselect file))))
  7922. (setq reversed (org-notes-order-reversed-p))
  7923. (save-excursion
  7924. (save-restriction
  7925. (widen)
  7926. (if pos
  7927. (progn
  7928. (goto-char pos)
  7929. (looking-at outline-regexp)
  7930. (setq level (org-get-valid-level (funcall outline-level) 1))
  7931. (goto-char
  7932. (if reversed
  7933. (or (outline-next-heading) (point-max))
  7934. (or (save-excursion (org-get-next-sibling))
  7935. (org-end-of-subtree t t)
  7936. (point-max)))))
  7937. (setq level 1)
  7938. (if (not reversed)
  7939. (goto-char (point-max))
  7940. (goto-char (point-min))
  7941. (or (outline-next-heading) (goto-char (point-max)))))
  7942. (if (not (bolp)) (newline))
  7943. (bookmark-set "org-refile-last-stored")
  7944. (org-paste-subtree level)
  7945. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7946. (run-hooks 'org-after-refile-insert-hook))))
  7947. (if regionp
  7948. (delete-region (point) (+ (point) region-length))
  7949. (org-cut-subtree))
  7950. (when (featurep 'org-inlinetask)
  7951. (org-inlinetask-remove-END-maybe))
  7952. (setq org-markers-to-move nil)
  7953. (message "Refiled to \"%s\"" (car it))))))
  7954. (org-reveal))
  7955. (defun org-refile-goto-last-stored ()
  7956. "Go to the location where the last refile was stored."
  7957. (interactive)
  7958. (bookmark-jump "org-refile-last-stored")
  7959. (message "This is the location of the last refile"))
  7960. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7961. "Prompt the user for a refile location, using PROMPT."
  7962. (let ((org-refile-targets org-refile-targets)
  7963. (org-refile-use-outline-path org-refile-use-outline-path))
  7964. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7965. (unless org-refile-target-table
  7966. (error "No refile targets"))
  7967. (let* ((cbuf (current-buffer))
  7968. (partial-completion-mode nil)
  7969. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7970. (cfunc (if (and org-refile-use-outline-path
  7971. org-outline-path-complete-in-steps)
  7972. 'org-olpath-completing-read
  7973. 'org-icompleting-read))
  7974. (extra (if org-refile-use-outline-path "/" ""))
  7975. (filename (and cfn (expand-file-name cfn)))
  7976. (tbl (mapcar
  7977. (lambda (x)
  7978. (if (and (not (member org-refile-use-outline-path
  7979. '(file full-file-path)))
  7980. (not (equal filename (nth 1 x))))
  7981. (cons (concat (car x) extra " ("
  7982. (file-name-nondirectory (nth 1 x)) ")")
  7983. (cdr x))
  7984. (cons (concat (car x) extra) (cdr x))))
  7985. org-refile-target-table))
  7986. (completion-ignore-case t)
  7987. pa answ parent-target child parent old-hist)
  7988. (setq old-hist org-refile-history)
  7989. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7990. nil 'org-refile-history))
  7991. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7992. (if pa
  7993. (progn
  7994. (when (or (not org-refile-history)
  7995. (not (eq old-hist org-refile-history))
  7996. (not (equal (car pa) (car org-refile-history))))
  7997. (setq org-refile-history
  7998. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  7999. org-refile-history
  8000. (cdr org-refile-history))))
  8001. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8002. (pop org-refile-history)))
  8003. pa)
  8004. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8005. (setq parent (match-string 1 answ)
  8006. child (match-string 2 answ))
  8007. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8008. (when (and parent-target
  8009. (or (eq new-nodes t)
  8010. (and (eq new-nodes 'confirm)
  8011. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8012. (org-refile-new-child parent-target child))))))
  8013. (defun org-refile-new-child (parent-target child)
  8014. "Use refile target PARENT-TARGET to add new CHILD below it."
  8015. (unless parent-target
  8016. (error "Cannot find parent for new node"))
  8017. (let ((file (nth 1 parent-target))
  8018. (pos (nth 3 parent-target))
  8019. level)
  8020. (with-current-buffer (or (find-buffer-visiting file)
  8021. (find-file-noselect file))
  8022. (save-excursion
  8023. (save-restriction
  8024. (widen)
  8025. (if pos
  8026. (goto-char pos)
  8027. (goto-char (point-max))
  8028. (if (not (bolp)) (newline)))
  8029. (when (looking-at outline-regexp)
  8030. (setq level (funcall outline-level))
  8031. (org-end-of-subtree t t))
  8032. (org-back-over-empty-lines)
  8033. (insert "\n" (make-string
  8034. (if pos (org-get-valid-level level 1) 1) ?*)
  8035. " " child "\n")
  8036. (beginning-of-line 0)
  8037. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8038. (defun org-olpath-completing-read (prompt collection &rest args)
  8039. "Read an outline path like a file name."
  8040. (let ((thetable collection)
  8041. (org-completion-use-ido nil) ; does not work with ido.
  8042. (org-completion-use-iswitchb nil)) ; or iswitchb
  8043. (apply
  8044. 'org-icompleting-read prompt
  8045. (lambda (string predicate &optional flag)
  8046. (let (rtn r f (l (length string)))
  8047. (cond
  8048. ((eq flag nil)
  8049. ;; try completion
  8050. (try-completion string thetable))
  8051. ((eq flag t)
  8052. ;; all-completions
  8053. (setq rtn (all-completions string thetable predicate))
  8054. (mapcar
  8055. (lambda (x)
  8056. (setq r (substring x l))
  8057. (if (string-match " ([^)]*)$" x)
  8058. (setq f (match-string 0 x))
  8059. (setq f ""))
  8060. (if (string-match "/" r)
  8061. (concat string (substring r 0 (match-end 0)) f)
  8062. x))
  8063. rtn))
  8064. ((eq flag 'lambda)
  8065. ;; exact match?
  8066. (assoc string thetable)))
  8067. ))
  8068. args)))
  8069. ;;;; Dynamic blocks
  8070. (defun org-find-dblock (name)
  8071. "Find the first dynamic block with name NAME in the buffer.
  8072. If not found, stay at current position and return nil."
  8073. (let (pos)
  8074. (save-excursion
  8075. (goto-char (point-min))
  8076. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8077. nil t)
  8078. (match-beginning 0))))
  8079. (if pos (goto-char pos))
  8080. pos))
  8081. (defconst org-dblock-start-re
  8082. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8083. "Matches the startline of a dynamic block, with parameters.")
  8084. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  8085. "Matches the end of a dynamic block.")
  8086. (defun org-create-dblock (plist)
  8087. "Create a dynamic block section, with parameters taken from PLIST.
  8088. PLIST must contain a :name entry which is used as name of the block."
  8089. (unless (bolp) (newline))
  8090. (let ((name (plist-get plist :name)))
  8091. (insert "#+BEGIN: " name)
  8092. (while plist
  8093. (if (eq (car plist) :name)
  8094. (setq plist (cddr plist))
  8095. (insert " " (prin1-to-string (pop plist)))))
  8096. (insert "\n\n#+END:\n")
  8097. (beginning-of-line -2)))
  8098. (defun org-prepare-dblock ()
  8099. "Prepare dynamic block for refresh.
  8100. This empties the block, puts the cursor at the insert position and returns
  8101. the property list including an extra property :name with the block name."
  8102. (unless (looking-at org-dblock-start-re)
  8103. (error "Not at a dynamic block"))
  8104. (let* ((begdel (1+ (match-end 0)))
  8105. (name (org-no-properties (match-string 1)))
  8106. (params (append (list :name name)
  8107. (read (concat "(" (match-string 3) ")")))))
  8108. (unless (re-search-forward org-dblock-end-re nil t)
  8109. (error "Dynamic block not terminated"))
  8110. (setq params
  8111. (append params
  8112. (list :content (buffer-substring
  8113. begdel (match-beginning 0)))))
  8114. (delete-region begdel (match-beginning 0))
  8115. (goto-char begdel)
  8116. (open-line 1)
  8117. params))
  8118. (defun org-map-dblocks (&optional command)
  8119. "Apply COMMAND to all dynamic blocks in the current buffer.
  8120. If COMMAND is not given, use `org-update-dblock'."
  8121. (let ((cmd (or command 'org-update-dblock))
  8122. pos)
  8123. (save-excursion
  8124. (goto-char (point-min))
  8125. (while (re-search-forward org-dblock-start-re nil t)
  8126. (goto-char (setq pos (match-beginning 0)))
  8127. (condition-case nil
  8128. (funcall cmd)
  8129. (error (message "Error during update of dynamic block")))
  8130. (goto-char pos)
  8131. (unless (re-search-forward org-dblock-end-re nil t)
  8132. (error "Dynamic block not terminated"))))))
  8133. (defun org-dblock-update (&optional arg)
  8134. "User command for updating dynamic blocks.
  8135. Update the dynamic block at point. With prefix ARG, update all dynamic
  8136. blocks in the buffer."
  8137. (interactive "P")
  8138. (if arg
  8139. (org-update-all-dblocks)
  8140. (or (looking-at org-dblock-start-re)
  8141. (org-beginning-of-dblock))
  8142. (org-update-dblock)))
  8143. (defun org-update-dblock ()
  8144. "Update the dynamic block at point
  8145. This means to empty the block, parse for parameters and then call
  8146. the correct writing function."
  8147. (save-window-excursion
  8148. (let* ((pos (point))
  8149. (line (org-current-line))
  8150. (params (org-prepare-dblock))
  8151. (name (plist-get params :name))
  8152. (cmd (intern (concat "org-dblock-write:" name))))
  8153. (message "Updating dynamic block `%s' at line %d..." name line)
  8154. (funcall cmd params)
  8155. (message "Updating dynamic block `%s' at line %d...done" name line)
  8156. (goto-char pos))))
  8157. (defun org-beginning-of-dblock ()
  8158. "Find the beginning of the dynamic block at point.
  8159. Error if there is no such block at point."
  8160. (let ((pos (point))
  8161. beg)
  8162. (end-of-line 1)
  8163. (if (and (re-search-backward org-dblock-start-re nil t)
  8164. (setq beg (match-beginning 0))
  8165. (re-search-forward org-dblock-end-re nil t)
  8166. (> (match-end 0) pos))
  8167. (goto-char beg)
  8168. (goto-char pos)
  8169. (error "Not in a dynamic block"))))
  8170. (defun org-update-all-dblocks ()
  8171. "Update all dynamic blocks in the buffer.
  8172. This function can be used in a hook."
  8173. (when (org-mode-p)
  8174. (org-map-dblocks 'org-update-dblock)))
  8175. ;;;; Completion
  8176. (defconst org-additional-option-like-keywords
  8177. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8178. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8179. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8180. "BEGIN:" "END:"
  8181. "ORGTBL" "TBLFM:" "TBLNAME:"
  8182. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8183. "BEGIN_QUOTE" "END_QUOTE"
  8184. "BEGIN_VERSE" "END_VERSE"
  8185. "BEGIN_CENTER" "END_CENTER"
  8186. "BEGIN_SRC" "END_SRC"
  8187. "CATEGORY" "COLUMNS"
  8188. "CAPTION" "LABEL"
  8189. "BIND"
  8190. "MACRO"))
  8191. (defcustom org-structure-template-alist
  8192. '(
  8193. ("s" "#+begin_src ?\n\n#+end_src"
  8194. "<src lang=\"?\">\n\n</src>")
  8195. ("e" "#+begin_example\n?\n#+end_example"
  8196. "<example>\n?\n</example>")
  8197. ("q" "#+begin_quote\n?\n#+end_quote"
  8198. "<quote>\n?\n</quote>")
  8199. ("v" "#+begin_verse\n?\n#+end_verse"
  8200. "<verse>\n?\n/verse>")
  8201. ("c" "#+begin_center\n?\n#+end_center"
  8202. "<center>\n?\n/center>")
  8203. ("l" "#+begin_latex\n?\n#+end_latex"
  8204. "<literal style=\"latex\">\n?\n</literal>")
  8205. ("L" "#+latex: "
  8206. "<literal style=\"latex\">?</literal>")
  8207. ("h" "#+begin_html\n?\n#+end_html"
  8208. "<literal style=\"html\">\n?\n</literal>")
  8209. ("H" "#+html: "
  8210. "<literal style=\"html\">?</literal>")
  8211. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8212. ("A" "#+ascii: ")
  8213. ("i" "#+include %file ?"
  8214. "<include file=%file markup=\"?\">")
  8215. )
  8216. "Structure completion elements.
  8217. This is a list of abbreviation keys and values. The value gets inserted
  8218. it you type @samp{.} followed by the key and then the completion key,
  8219. usually `M-TAB'. %file will be replaced by a file name after prompting
  8220. for the file using completion.
  8221. There are two templates for each key, the first uses the original Org syntax,
  8222. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8223. the default when the /org-mtags.el/ module has been loaded. See also the
  8224. variable `org-mtags-prefer-muse-templates'.
  8225. This is an experimental feature, it is undecided if it is going to stay in."
  8226. :group 'org-completion
  8227. :type '(repeat
  8228. (string :tag "Key")
  8229. (string :tag "Template")
  8230. (string :tag "Muse Template")))
  8231. (defun org-try-structure-completion ()
  8232. "Try to complete a structure template before point.
  8233. This looks for strings like \"<e\" on an otherwise empty line and
  8234. expands them."
  8235. (let ((l (buffer-substring (point-at-bol) (point)))
  8236. a)
  8237. (when (and (looking-at "[ \t]*$")
  8238. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8239. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8240. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8241. (match-beginning 1)) a)
  8242. t)))
  8243. (defun org-complete-expand-structure-template (start cell)
  8244. "Expand a structure template."
  8245. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8246. (rpl (nth (if musep 2 1) cell))
  8247. (ind ""))
  8248. (delete-region start (point))
  8249. (when (string-match "\\`#\\+" rpl)
  8250. (cond
  8251. ((bolp))
  8252. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8253. (setq ind (buffer-substring (point-at-bol) (point))))
  8254. (t (newline))))
  8255. (setq start (point))
  8256. (if (string-match "%file" rpl)
  8257. (setq rpl (replace-match
  8258. (concat
  8259. "\""
  8260. (save-match-data
  8261. (abbreviate-file-name (read-file-name "Include file: ")))
  8262. "\"")
  8263. t t rpl)))
  8264. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8265. (concat "\n" ind)))
  8266. (insert rpl)
  8267. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8268. (defun org-complete (&optional arg)
  8269. "Perform completion on word at point.
  8270. At the beginning of a headline, this completes TODO keywords as given in
  8271. `org-todo-keywords'.
  8272. If the current word is preceded by a backslash, completes the TeX symbols
  8273. that are supported for HTML support.
  8274. If the current word is preceded by \"#+\", completes special words for
  8275. setting file options.
  8276. In the line after \"#+STARTUP:, complete valid keywords.\"
  8277. At all other locations, this simply calls the value of
  8278. `org-completion-fallback-command'."
  8279. (interactive "P")
  8280. (org-without-partial-completion
  8281. (catch 'exit
  8282. (let* ((a nil)
  8283. (end (point))
  8284. (beg1 (save-excursion
  8285. (skip-chars-backward (org-re "[:alnum:]_@"))
  8286. (point)))
  8287. (beg (save-excursion
  8288. (skip-chars-backward "a-zA-Z0-9_:$")
  8289. (point)))
  8290. (confirm (lambda (x) (stringp (car x))))
  8291. (searchhead (equal (char-before beg) ?*))
  8292. (struct
  8293. (when (and (member (char-before beg1) '(?. ?<))
  8294. (setq a (assoc (buffer-substring beg1 (point))
  8295. org-structure-template-alist)))
  8296. (org-complete-expand-structure-template (1- beg1) a)
  8297. (throw 'exit t)))
  8298. (tag (and (equal (char-before beg1) ?:)
  8299. (equal (char-after (point-at-bol)) ?*)))
  8300. (prop (and (equal (char-before beg1) ?:)
  8301. (not (equal (char-after (point-at-bol)) ?*))))
  8302. (texp (equal (char-before beg) ?\\))
  8303. (link (equal (char-before beg) ?\[))
  8304. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8305. beg)
  8306. "#+"))
  8307. (startup (string-match "^#\\+STARTUP:.*"
  8308. (buffer-substring (point-at-bol) (point))))
  8309. (completion-ignore-case opt)
  8310. (type nil)
  8311. (tbl nil)
  8312. (table (cond
  8313. (opt
  8314. (setq type :opt)
  8315. (require 'org-exp)
  8316. (append
  8317. (delq nil
  8318. (mapcar
  8319. (lambda (x)
  8320. (if (string-match
  8321. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8322. (cons (match-string 2 x)
  8323. (match-string 1 x))))
  8324. (org-split-string (org-get-current-options) "\n")))
  8325. (mapcar 'list org-additional-option-like-keywords)))
  8326. (startup
  8327. (setq type :startup)
  8328. org-startup-options)
  8329. (link (append org-link-abbrev-alist-local
  8330. org-link-abbrev-alist))
  8331. (texp
  8332. (setq type :tex)
  8333. org-html-entities)
  8334. ((string-match "\\`\\*+[ \t]+\\'"
  8335. (buffer-substring (point-at-bol) beg))
  8336. (setq type :todo)
  8337. (mapcar 'list org-todo-keywords-1))
  8338. (searchhead
  8339. (setq type :searchhead)
  8340. (save-excursion
  8341. (goto-char (point-min))
  8342. (while (re-search-forward org-todo-line-regexp nil t)
  8343. (push (list
  8344. (org-make-org-heading-search-string
  8345. (match-string 3) t))
  8346. tbl)))
  8347. tbl)
  8348. (tag (setq type :tag beg beg1)
  8349. (or org-tag-alist (org-get-buffer-tags)))
  8350. (prop (setq type :prop beg beg1)
  8351. (mapcar 'list (org-buffer-property-keys nil t t)))
  8352. (t (progn
  8353. (call-interactively org-completion-fallback-command)
  8354. (throw 'exit nil)))))
  8355. (pattern (buffer-substring-no-properties beg end))
  8356. (completion (try-completion pattern table confirm)))
  8357. (cond ((eq completion t)
  8358. (if (not (assoc (upcase pattern) table))
  8359. (message "Already complete")
  8360. (if (and (equal type :opt)
  8361. (not (member (car (assoc (upcase pattern) table))
  8362. org-additional-option-like-keywords)))
  8363. (insert (substring (cdr (assoc (upcase pattern) table))
  8364. (length pattern)))
  8365. (if (memq type '(:tag :prop)) (insert ":")))))
  8366. ((null completion)
  8367. (message "Can't find completion for \"%s\"" pattern)
  8368. (ding))
  8369. ((not (string= pattern completion))
  8370. (delete-region beg end)
  8371. (if (string-match " +$" completion)
  8372. (setq completion (replace-match "" t t completion)))
  8373. (insert completion)
  8374. (if (get-buffer-window "*Completions*")
  8375. (delete-window (get-buffer-window "*Completions*")))
  8376. (if (assoc completion table)
  8377. (if (eq type :todo) (insert " ")
  8378. (if (memq type '(:tag :prop)) (insert ":"))))
  8379. (if (and (equal type :opt) (assoc completion table))
  8380. (message "%s" (substitute-command-keys
  8381. "Press \\[org-complete] again to insert example settings"))))
  8382. (t
  8383. (message "Making completion list...")
  8384. (let ((list (sort (all-completions pattern table confirm)
  8385. 'string<)))
  8386. (with-output-to-temp-buffer "*Completions*"
  8387. (condition-case nil
  8388. ;; Protection needed for XEmacs and emacs 21
  8389. (display-completion-list list pattern)
  8390. (error (display-completion-list list)))))
  8391. (message "Making completion list...%s" "done")))))))
  8392. ;;;; TODO, DEADLINE, Comments
  8393. (defun org-toggle-comment ()
  8394. "Change the COMMENT state of an entry."
  8395. (interactive)
  8396. (save-excursion
  8397. (org-back-to-heading)
  8398. (let (case-fold-search)
  8399. (if (looking-at (concat outline-regexp
  8400. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8401. (replace-match "" t t nil 1)
  8402. (if (looking-at outline-regexp)
  8403. (progn
  8404. (goto-char (match-end 0))
  8405. (insert org-comment-string " ")))))))
  8406. (defvar org-last-todo-state-is-todo nil
  8407. "This is non-nil when the last TODO state change led to a TODO state.
  8408. If the last change removed the TODO tag or switched to DONE, then
  8409. this is nil.")
  8410. (defvar org-setting-tags nil) ; dynamically skipped
  8411. (defun org-parse-local-options (string var)
  8412. "Parse STRING for startup setting relevant for variable VAR."
  8413. (let ((rtn (symbol-value var))
  8414. e opts)
  8415. (save-match-data
  8416. (if (or (not string) (not (string-match "\\S-" string)))
  8417. rtn
  8418. (setq opts (delq nil (mapcar (lambda (x)
  8419. (setq e (assoc x org-startup-options))
  8420. (if (eq (nth 1 e) var) e nil))
  8421. (org-split-string string "[ \t]+"))))
  8422. (if (not opts)
  8423. rtn
  8424. (setq rtn nil)
  8425. (while (setq e (pop opts))
  8426. (if (not (nth 3 e))
  8427. (setq rtn (nth 2 e))
  8428. (if (not (listp rtn)) (setq rtn nil))
  8429. (push (nth 2 e) rtn)))
  8430. rtn)))))
  8431. (defvar org-todo-setup-filter-hook nil
  8432. "Hook for functions that pre-filter todo specs.
  8433. Each function takes a todo spec and returns either `nil' or the spec
  8434. transformed into canonical form." )
  8435. (defvar org-todo-get-default-hook nil
  8436. "Hook for functions that get a default item for todo.
  8437. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8438. `nil' or a string to be used for the todo mark." )
  8439. (defvar org-agenda-headline-snapshot-before-repeat)
  8440. (defun org-todo (&optional arg)
  8441. "Change the TODO state of an item.
  8442. The state of an item is given by a keyword at the start of the heading,
  8443. like
  8444. *** TODO Write paper
  8445. *** DONE Call mom
  8446. The different keywords are specified in the variable `org-todo-keywords'.
  8447. By default the available states are \"TODO\" and \"DONE\".
  8448. So for this example: when the item starts with TODO, it is changed to DONE.
  8449. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8450. DONE are present, add TODO at the beginning of the heading.
  8451. With C-u prefix arg, use completion to determine the new state.
  8452. With numeric prefix arg, switch to that state.
  8453. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8454. With a tripple C-u prefix, circumvent any state blocking.
  8455. For calling through lisp, arg is also interpreted in the following way:
  8456. 'none -> empty state
  8457. \"\"(empty string) -> switch to empty state
  8458. 'done -> switch to DONE
  8459. 'nextset -> switch to the next set of keywords
  8460. 'previousset -> switch to the previous set of keywords
  8461. \"WAITING\" -> switch to the specified keyword, but only if it
  8462. really is a member of `org-todo-keywords'."
  8463. (interactive "P")
  8464. (if (equal arg '(16)) (setq arg 'nextset))
  8465. (let ((org-blocker-hook org-blocker-hook)
  8466. (case-fold-search nil))
  8467. (when (equal arg '(64))
  8468. (setq arg nil org-blocker-hook nil))
  8469. (when (and org-blocker-hook
  8470. (or org-inhibit-blocking
  8471. (org-entry-get nil "NOBLOCKING")))
  8472. (setq org-blocker-hook nil))
  8473. (save-excursion
  8474. (catch 'exit
  8475. (org-back-to-heading)
  8476. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8477. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8478. (looking-at " *"))
  8479. (let* ((match-data (match-data))
  8480. (startpos (point-at-bol))
  8481. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8482. (org-log-done org-log-done)
  8483. (org-log-repeat org-log-repeat)
  8484. (org-todo-log-states org-todo-log-states)
  8485. (this (match-string 1))
  8486. (hl-pos (match-beginning 0))
  8487. (head (org-get-todo-sequence-head this))
  8488. (ass (assoc head org-todo-kwd-alist))
  8489. (interpret (nth 1 ass))
  8490. (done-word (nth 3 ass))
  8491. (final-done-word (nth 4 ass))
  8492. (last-state (or this ""))
  8493. (completion-ignore-case t)
  8494. (member (member this org-todo-keywords-1))
  8495. (tail (cdr member))
  8496. (state (cond
  8497. ((and org-todo-key-trigger
  8498. (or (and (equal arg '(4))
  8499. (eq org-use-fast-todo-selection 'prefix))
  8500. (and (not arg) org-use-fast-todo-selection
  8501. (not (eq org-use-fast-todo-selection
  8502. 'prefix)))))
  8503. ;; Use fast selection
  8504. (org-fast-todo-selection))
  8505. ((and (equal arg '(4))
  8506. (or (not org-use-fast-todo-selection)
  8507. (not org-todo-key-trigger)))
  8508. ;; Read a state with completion
  8509. (org-icompleting-read
  8510. "State: " (mapcar (lambda(x) (list x))
  8511. org-todo-keywords-1)
  8512. nil t))
  8513. ((eq arg 'right)
  8514. (if this
  8515. (if tail (car tail) nil)
  8516. (car org-todo-keywords-1)))
  8517. ((eq arg 'left)
  8518. (if (equal member org-todo-keywords-1)
  8519. nil
  8520. (if this
  8521. (nth (- (length org-todo-keywords-1)
  8522. (length tail) 2)
  8523. org-todo-keywords-1)
  8524. (org-last org-todo-keywords-1))))
  8525. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8526. (setq arg nil))) ; hack to fall back to cycling
  8527. (arg
  8528. ;; user or caller requests a specific state
  8529. (cond
  8530. ((equal arg "") nil)
  8531. ((eq arg 'none) nil)
  8532. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8533. ((eq arg 'nextset)
  8534. (or (car (cdr (member head org-todo-heads)))
  8535. (car org-todo-heads)))
  8536. ((eq arg 'previousset)
  8537. (let ((org-todo-heads (reverse org-todo-heads)))
  8538. (or (car (cdr (member head org-todo-heads)))
  8539. (car org-todo-heads))))
  8540. ((car (member arg org-todo-keywords-1)))
  8541. ((nth (1- (prefix-numeric-value arg))
  8542. org-todo-keywords-1))))
  8543. ((null member) (or head (car org-todo-keywords-1)))
  8544. ((equal this final-done-word) nil) ;; -> make empty
  8545. ((null tail) nil) ;; -> first entry
  8546. ((memq interpret '(type priority))
  8547. (if (eq this-command last-command)
  8548. (car tail)
  8549. (if (> (length tail) 0)
  8550. (or done-word (car org-done-keywords))
  8551. nil)))
  8552. (t
  8553. (car tail))))
  8554. (state (or
  8555. (run-hook-with-args-until-success
  8556. 'org-todo-get-default-hook state last-state)
  8557. state))
  8558. (next (if state (concat " " state " ") " "))
  8559. (change-plist (list :type 'todo-state-change :from this :to state
  8560. :position startpos))
  8561. dolog now-done-p)
  8562. (when org-blocker-hook
  8563. (setq org-last-todo-state-is-todo
  8564. (not (member this org-done-keywords)))
  8565. (unless (save-excursion
  8566. (save-match-data
  8567. (run-hook-with-args-until-failure
  8568. 'org-blocker-hook change-plist)))
  8569. (if (interactive-p)
  8570. (error "TODO state change from %s to %s blocked" this state)
  8571. ;; fail silently
  8572. (message "TODO state change from %s to %s blocked" this state)
  8573. (throw 'exit nil))))
  8574. (store-match-data match-data)
  8575. (replace-match next t t)
  8576. (unless (pos-visible-in-window-p hl-pos)
  8577. (message "TODO state changed to %s" (org-trim next)))
  8578. (unless head
  8579. (setq head (org-get-todo-sequence-head state)
  8580. ass (assoc head org-todo-kwd-alist)
  8581. interpret (nth 1 ass)
  8582. done-word (nth 3 ass)
  8583. final-done-word (nth 4 ass)))
  8584. (when (memq arg '(nextset previousset))
  8585. (message "Keyword-Set %d/%d: %s"
  8586. (- (length org-todo-sets) -1
  8587. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8588. (length org-todo-sets)
  8589. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8590. (setq org-last-todo-state-is-todo
  8591. (not (member state org-done-keywords)))
  8592. (setq now-done-p (and (member state org-done-keywords)
  8593. (not (member this org-done-keywords))))
  8594. (and logging (org-local-logging logging))
  8595. (when (and (or org-todo-log-states org-log-done)
  8596. (not (eq org-inhibit-logging t))
  8597. (not (memq arg '(nextset previousset))))
  8598. ;; we need to look at recording a time and note
  8599. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8600. (nth 2 (assoc this org-todo-log-states))))
  8601. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8602. (setq dolog 'time))
  8603. (when (and state
  8604. (member state org-not-done-keywords)
  8605. (not (member this org-not-done-keywords)))
  8606. ;; This is now a todo state and was not one before
  8607. ;; If there was a CLOSED time stamp, get rid of it.
  8608. (org-add-planning-info nil nil 'closed))
  8609. (when (and now-done-p org-log-done)
  8610. ;; It is now done, and it was not done before
  8611. (org-add-planning-info 'closed (org-current-time))
  8612. (if (and (not dolog) (eq 'note org-log-done))
  8613. (org-add-log-setup 'done state this 'findpos 'note)))
  8614. (when (and state dolog)
  8615. ;; This is a non-nil state, and we need to log it
  8616. (org-add-log-setup 'state state this 'findpos dolog)))
  8617. ;; Fixup tag positioning
  8618. (org-todo-trigger-tag-changes state)
  8619. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8620. (when org-provide-todo-statistics
  8621. (org-update-parent-todo-statistics))
  8622. (run-hooks 'org-after-todo-state-change-hook)
  8623. (if (and arg (not (member state org-done-keywords)))
  8624. (setq head (org-get-todo-sequence-head state)))
  8625. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8626. ;; Do we need to trigger a repeat?
  8627. (when now-done-p
  8628. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8629. ;; This is for the agenda, take a snapshot of the headline.
  8630. (save-match-data
  8631. (setq org-agenda-headline-snapshot-before-repeat
  8632. (org-get-heading))))
  8633. (org-auto-repeat-maybe state))
  8634. ;; Fixup cursor location if close to the keyword
  8635. (if (and (outline-on-heading-p)
  8636. (not (bolp))
  8637. (save-excursion (beginning-of-line 1)
  8638. (looking-at org-todo-line-regexp))
  8639. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8640. (progn
  8641. (goto-char (or (match-end 2) (match-end 1)))
  8642. (and (looking-at " ") (just-one-space))))
  8643. (when org-trigger-hook
  8644. (save-excursion
  8645. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8646. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8647. "Block turning an entry into a TODO, using the hierarchy.
  8648. This checks whether the current task should be blocked from state
  8649. changes. Such blocking occurs when:
  8650. 1. The task has children which are not all in a completed state.
  8651. 2. A task has a parent with the property :ORDERED:, and there
  8652. are siblings prior to the current task with incomplete
  8653. status.
  8654. 3. The parent of the task is blocked because it has siblings that should
  8655. be done first, or is child of a block grandparent TODO entry."
  8656. (catch 'dont-block
  8657. ;; If this is not a todo state change, or if this entry is already DONE,
  8658. ;; do not block
  8659. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8660. (member (plist-get change-plist :from)
  8661. (cons 'done org-done-keywords))
  8662. (member (plist-get change-plist :to)
  8663. (cons 'todo org-not-done-keywords)))
  8664. (throw 'dont-block t))
  8665. ;; If this task has children, and any are undone, it's blocked
  8666. (save-excursion
  8667. (org-back-to-heading t)
  8668. (let ((this-level (funcall outline-level)))
  8669. (outline-next-heading)
  8670. (let ((child-level (funcall outline-level)))
  8671. (while (and (not (eobp))
  8672. (> child-level this-level))
  8673. ;; this todo has children, check whether they are all
  8674. ;; completed
  8675. (if (and (not (org-entry-is-done-p))
  8676. (org-entry-is-todo-p))
  8677. (throw 'dont-block nil))
  8678. (outline-next-heading)
  8679. (setq child-level (funcall outline-level))))))
  8680. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8681. ;; any previous siblings are undone, it's blocked
  8682. (save-excursion
  8683. (org-back-to-heading t)
  8684. (let* ((pos (point))
  8685. (parent-pos (and (org-up-heading-safe) (point))))
  8686. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8687. (when (and (org-entry-get (point) "ORDERED")
  8688. (forward-line 1)
  8689. (re-search-forward org-not-done-heading-regexp pos t))
  8690. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8691. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8692. (while t
  8693. (goto-char parent-pos)
  8694. (if (not (looking-at org-not-done-heading-regexp))
  8695. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8696. (setq pos (point))
  8697. (setq parent-pos (and (org-up-heading-safe) (point)))
  8698. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8699. (when (and (org-entry-get (point) "ORDERED")
  8700. (forward-line 1)
  8701. (re-search-forward org-not-done-heading-regexp pos t))
  8702. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8703. (defcustom org-track-ordered-property-with-tag nil
  8704. "Should the ORDERED property also be shown as a tag?
  8705. The ORDERED property decides if an entry should require subtasks to be
  8706. completed in sequence. Since a property is not very visible, setting
  8707. this option means that toggling the ORDERED property with the command
  8708. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8709. not relevant for the behavior, but it makes things more visible.
  8710. Note that toggling the tag with tags commands will not change the property
  8711. and therefore not influence behavior!
  8712. This can be t, meaning the tag ORDERED should be used, It can also be a
  8713. string to select a different tag for this task."
  8714. :group 'org-todo
  8715. :type '(choice
  8716. (const :tag "No tracking" nil)
  8717. (const :tag "Track with ORDERED tag" t)
  8718. (string :tag "Use other tag")))
  8719. (defun org-toggle-ordered-property ()
  8720. "Toggle the ORDERED property of the current entry.
  8721. For better visibility, you can track the value of this property with a tag.
  8722. See variable `org-track-ordered-property-with-tag'."
  8723. (interactive)
  8724. (let* ((t1 org-track-ordered-property-with-tag)
  8725. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8726. (save-excursion
  8727. (org-back-to-heading)
  8728. (if (org-entry-get nil "ORDERED")
  8729. (progn
  8730. (org-delete-property "ORDERED")
  8731. (and tag (org-toggle-tag tag 'off))
  8732. (message "Subtasks can be completed in arbitrary order"))
  8733. (org-entry-put nil "ORDERED" "t")
  8734. (and tag (org-toggle-tag tag 'on))
  8735. (message "Subtasks must be completed in sequence")))))
  8736. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8737. (defun org-block-todo-from-checkboxes (change-plist)
  8738. "Block turning an entry into a TODO, using checkboxes.
  8739. This checks whether the current task should be blocked from state
  8740. changes because there are uncheckd boxes in this entry."
  8741. (catch 'dont-block
  8742. ;; If this is not a todo state change, or if this entry is already DONE,
  8743. ;; do not block
  8744. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8745. (member (plist-get change-plist :from)
  8746. (cons 'done org-done-keywords))
  8747. (member (plist-get change-plist :to)
  8748. (cons 'todo org-not-done-keywords)))
  8749. (throw 'dont-block t))
  8750. ;; If this task has checkboxes that are not checked, it's blocked
  8751. (save-excursion
  8752. (org-back-to-heading t)
  8753. (let ((beg (point)) end)
  8754. (outline-next-heading)
  8755. (setq end (point))
  8756. (goto-char beg)
  8757. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8758. end t)
  8759. (progn
  8760. (if (boundp 'org-blocked-by-checkboxes)
  8761. (setq org-blocked-by-checkboxes t))
  8762. (throw 'dont-block nil)))))
  8763. t)) ; do not block
  8764. (defun org-update-statistics-cookies (all)
  8765. "Update the statistics cookie, either from TODO or from checkboxes.
  8766. This should be called with the cursor in a line with a statistics cookie."
  8767. (interactive "P")
  8768. (if all
  8769. (progn
  8770. (org-update-checkbox-count 'all)
  8771. (org-map-entries 'org-update-parent-todo-statistics))
  8772. (if (not (org-on-heading-p))
  8773. (org-update-checkbox-count)
  8774. (let ((pos (move-marker (make-marker) (point)))
  8775. end l1 l2)
  8776. (ignore-errors (org-back-to-heading t))
  8777. (if (not (org-on-heading-p))
  8778. (org-update-checkbox-count)
  8779. (setq l1 (org-outline-level))
  8780. (setq end (save-excursion
  8781. (outline-next-heading)
  8782. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  8783. (point)))
  8784. (if (and (save-excursion (re-search-forward
  8785. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  8786. (not (save-excursion (re-search-forward
  8787. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  8788. (org-update-checkbox-count)
  8789. (if (and l2 (> l2 l1))
  8790. (progn
  8791. (goto-char end)
  8792. (org-update-parent-todo-statistics))
  8793. (error "No data for statistics cookie"))))
  8794. (goto-char pos)
  8795. (move-marker pos nil)))))
  8796. (defvar org-entry-property-inherited-from) ;; defined below
  8797. (defun org-update-parent-todo-statistics ()
  8798. "Update any statistics cookie in the parent of the current headline.
  8799. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8800. the entire subtree and this will travel up the hierarchy and update
  8801. statistics everywhere."
  8802. (interactive)
  8803. (let* ((lim 0) prop
  8804. (recursive (or (not org-hierarchical-todo-statistics)
  8805. (string-match
  8806. "\\<recursive\\>"
  8807. (or (setq prop (org-entry-get
  8808. nil "COOKIE_DATA" 'inherit)) ""))))
  8809. (lim (or (and prop (marker-position
  8810. org-entry-property-inherited-from))
  8811. lim))
  8812. (first t)
  8813. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8814. level ltoggle l1
  8815. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8816. (catch 'exit
  8817. (save-excursion
  8818. (beginning-of-line 1)
  8819. (if (org-at-heading-p)
  8820. (setq ltoggle (funcall outline-level))
  8821. (error "This should not happen"))
  8822. (while (and (setq level (org-up-heading-safe))
  8823. (or recursive first)
  8824. (>= (point) lim))
  8825. (setq first nil)
  8826. (unless (and level
  8827. (not (string-match
  8828. "\\<checkbox\\>"
  8829. (downcase
  8830. (or (org-entry-get
  8831. nil "COOKIE_DATA")
  8832. "")))))
  8833. (throw 'exit nil))
  8834. (while (re-search-forward box-re (point-at-eol) t)
  8835. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8836. (setq is-percent (match-end 2))
  8837. (save-match-data
  8838. (unless (outline-next-heading) (throw 'exit nil))
  8839. (while (and (looking-at org-complex-heading-regexp)
  8840. (> (setq l1 (length (match-string 1))) level))
  8841. (setq kwd (and (or recursive (= l1 ltoggle))
  8842. (match-string 2)))
  8843. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8844. (and (listp org-provide-todo-statistics)
  8845. (or (member kwd org-provide-todo-statistics)
  8846. (member kwd org-done-keywords))))
  8847. (setq cnt-all (1+ cnt-all))
  8848. (if (eq org-provide-todo-statistics t)
  8849. (and kwd (setq cnt-all (1+ cnt-all)))))
  8850. (and (member kwd org-done-keywords)
  8851. (setq cnt-done (1+ cnt-done)))
  8852. (outline-next-heading)))
  8853. (replace-match
  8854. (if is-percent
  8855. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8856. (format "[%d/%d]" cnt-done cnt-all)))))
  8857. (when cookie-present
  8858. (run-hook-with-args 'org-after-todo-statistics-hook
  8859. cnt-done (- cnt-all cnt-done)))))
  8860. (run-hooks 'org-todo-statistics-hook)))
  8861. (defvar org-after-todo-statistics-hook nil
  8862. "Hook that is called after a TODO statistics cookie has been updated.
  8863. Each function is called with two arguments: the number of not-done entries
  8864. and the number of done entries.
  8865. For example, the following function, when added to this hook, will switch
  8866. an entry to DONE when all children are done, and back to TODO when new
  8867. entries are set to a TODO status. Note that this hook is only called
  8868. when there is a statistics cookie in the headline!
  8869. (defun org-summary-todo (n-done n-not-done)
  8870. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8871. (let (org-log-done org-log-states) ; turn off logging
  8872. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8873. ")
  8874. (defvar org-todo-statistics-hook nil
  8875. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8876. This hook runs even if there is no statisics cookie present, in which case
  8877. `org-after-todo-statistics-hook' would not run.")
  8878. (defun org-todo-trigger-tag-changes (state)
  8879. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8880. (let ((l org-todo-state-tags-triggers)
  8881. changes)
  8882. (when (or (not state) (equal state ""))
  8883. (setq changes (append changes (cdr (assoc "" l)))))
  8884. (when (and (stringp state) (> (length state) 0))
  8885. (setq changes (append changes (cdr (assoc state l)))))
  8886. (when (member state org-not-done-keywords)
  8887. (setq changes (append changes (cdr (assoc 'todo l)))))
  8888. (when (member state org-done-keywords)
  8889. (setq changes (append changes (cdr (assoc 'done l)))))
  8890. (dolist (c changes)
  8891. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8892. (defun org-local-logging (value)
  8893. "Get logging settings from a property VALUE."
  8894. (let* (words w a)
  8895. ;; directly set the variables, they are already local.
  8896. (setq org-log-done nil
  8897. org-log-repeat nil
  8898. org-todo-log-states nil)
  8899. (setq words (org-split-string value))
  8900. (while (setq w (pop words))
  8901. (cond
  8902. ((setq a (assoc w org-startup-options))
  8903. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8904. (set (nth 1 a) (nth 2 a))))
  8905. ((setq a (org-extract-log-state-settings w))
  8906. (and (member (car a) org-todo-keywords-1)
  8907. (push a org-todo-log-states)))))))
  8908. (defun org-get-todo-sequence-head (kwd)
  8909. "Return the head of the TODO sequence to which KWD belongs.
  8910. If KWD is not set, check if there is a text property remembering the
  8911. right sequence."
  8912. (let (p)
  8913. (cond
  8914. ((not kwd)
  8915. (or (get-text-property (point-at-bol) 'org-todo-head)
  8916. (progn
  8917. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8918. nil (point-at-eol)))
  8919. (get-text-property p 'org-todo-head))))
  8920. ((not (member kwd org-todo-keywords-1))
  8921. (car org-todo-keywords-1))
  8922. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8923. (defun org-fast-todo-selection ()
  8924. "Fast TODO keyword selection with single keys.
  8925. Returns the new TODO keyword, or nil if no state change should occur."
  8926. (let* ((fulltable org-todo-key-alist)
  8927. (done-keywords org-done-keywords) ;; needed for the faces.
  8928. (maxlen (apply 'max (mapcar
  8929. (lambda (x)
  8930. (if (stringp (car x)) (string-width (car x)) 0))
  8931. fulltable)))
  8932. (expert nil)
  8933. (fwidth (+ maxlen 3 1 3))
  8934. (ncol (/ (- (window-width) 4) fwidth))
  8935. tg cnt e c tbl
  8936. groups ingroup)
  8937. (save-excursion
  8938. (save-window-excursion
  8939. (if expert
  8940. (set-buffer (get-buffer-create " *Org todo*"))
  8941. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8942. (erase-buffer)
  8943. (org-set-local 'org-done-keywords done-keywords)
  8944. (setq tbl fulltable cnt 0)
  8945. (while (setq e (pop tbl))
  8946. (cond
  8947. ((equal e '(:startgroup))
  8948. (push '() groups) (setq ingroup t)
  8949. (when (not (= cnt 0))
  8950. (setq cnt 0)
  8951. (insert "\n"))
  8952. (insert "{ "))
  8953. ((equal e '(:endgroup))
  8954. (setq ingroup nil cnt 0)
  8955. (insert "}\n"))
  8956. ((equal e '(:newline))
  8957. (when (not (= cnt 0))
  8958. (setq cnt 0)
  8959. (insert "\n")
  8960. (setq e (car tbl))
  8961. (while (equal (car tbl) '(:newline))
  8962. (insert "\n")
  8963. (setq tbl (cdr tbl)))))
  8964. (t
  8965. (setq tg (car e) c (cdr e))
  8966. (if ingroup (push tg (car groups)))
  8967. (setq tg (org-add-props tg nil 'face
  8968. (org-get-todo-face tg)))
  8969. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8970. (insert "[" c "] " tg (make-string
  8971. (- fwidth 4 (length tg)) ?\ ))
  8972. (when (= (setq cnt (1+ cnt)) ncol)
  8973. (insert "\n")
  8974. (if ingroup (insert " "))
  8975. (setq cnt 0)))))
  8976. (insert "\n")
  8977. (goto-char (point-min))
  8978. (if (not expert) (org-fit-window-to-buffer))
  8979. (message "[a-z..]:Set [SPC]:clear")
  8980. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8981. (cond
  8982. ((or (= c ?\C-g)
  8983. (and (= c ?q) (not (rassoc c fulltable))))
  8984. (setq quit-flag t))
  8985. ((= c ?\ ) nil)
  8986. ((setq e (rassoc c fulltable) tg (car e))
  8987. tg)
  8988. (t (setq quit-flag t)))))))
  8989. (defun org-entry-is-todo-p ()
  8990. (member (org-get-todo-state) org-not-done-keywords))
  8991. (defun org-entry-is-done-p ()
  8992. (member (org-get-todo-state) org-done-keywords))
  8993. (defun org-get-todo-state ()
  8994. (save-excursion
  8995. (org-back-to-heading t)
  8996. (and (looking-at org-todo-line-regexp)
  8997. (match-end 2)
  8998. (match-string 2))))
  8999. (defun org-at-date-range-p (&optional inactive-ok)
  9000. "Is the cursor inside a date range?"
  9001. (interactive)
  9002. (save-excursion
  9003. (catch 'exit
  9004. (let ((pos (point)))
  9005. (skip-chars-backward "^[<\r\n")
  9006. (skip-chars-backward "<[")
  9007. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9008. (>= (match-end 0) pos)
  9009. (throw 'exit t))
  9010. (skip-chars-backward "^<[\r\n")
  9011. (skip-chars-backward "<[")
  9012. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9013. (>= (match-end 0) pos)
  9014. (throw 'exit t)))
  9015. nil)))
  9016. (defun org-get-repeat ()
  9017. "Check if there is a deadline/schedule with repeater in this entry."
  9018. (save-match-data
  9019. (save-excursion
  9020. (org-back-to-heading t)
  9021. (if (re-search-forward
  9022. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  9023. (match-string 1)))))
  9024. (defvar org-last-changed-timestamp)
  9025. (defvar org-last-inserted-timestamp)
  9026. (defvar org-log-post-message)
  9027. (defvar org-log-note-purpose)
  9028. (defvar org-log-note-how)
  9029. (defvar org-log-note-extra)
  9030. (defun org-auto-repeat-maybe (done-word)
  9031. "Check if the current headline contains a repeated deadline/schedule.
  9032. If yes, set TODO state back to what it was and change the base date
  9033. of repeating deadline/scheduled time stamps to new date.
  9034. This function is run automatically after each state change to a DONE state."
  9035. ;; last-state is dynamically scoped into this function
  9036. (let* ((repeat (org-get-repeat))
  9037. (aa (assoc last-state org-todo-kwd-alist))
  9038. (interpret (nth 1 aa))
  9039. (head (nth 2 aa))
  9040. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9041. (msg "Entry repeats: ")
  9042. (org-log-done nil)
  9043. (org-todo-log-states nil)
  9044. (nshiftmax 10) (nshift 0)
  9045. re type n what ts time)
  9046. (when repeat
  9047. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9048. (org-todo (if (eq interpret 'type) last-state head))
  9049. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9050. (org-time-stamp-format t t)))
  9051. (when org-log-repeat
  9052. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9053. (memq 'org-add-log-note post-command-hook))
  9054. ;; OK, we are already setup for some record
  9055. (if (eq org-log-repeat 'note)
  9056. ;; make sure we take a note, not only a time stamp
  9057. (setq org-log-note-how 'note))
  9058. ;; Set up for taking a record
  9059. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9060. last-state
  9061. 'findpos org-log-repeat)))
  9062. (org-back-to-heading t)
  9063. (org-add-planning-info nil nil 'closed)
  9064. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9065. org-deadline-time-regexp "\\)\\|\\("
  9066. org-ts-regexp "\\)"))
  9067. (while (re-search-forward
  9068. re (save-excursion (outline-next-heading) (point)) t)
  9069. (setq type (if (match-end 1) org-scheduled-string
  9070. (if (match-end 3) org-deadline-string "Plain:"))
  9071. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9072. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9073. (setq n (string-to-number (match-string 2 ts))
  9074. what (match-string 3 ts))
  9075. (if (equal what "w") (setq n (* n 7) what "d"))
  9076. ;; Preparation, see if we need to modify the start date for the change
  9077. (when (match-end 1)
  9078. (setq time (save-match-data (org-time-string-to-time ts)))
  9079. (cond
  9080. ((equal (match-string 1 ts) ".")
  9081. ;; Shift starting date to today
  9082. (org-timestamp-change
  9083. (- (time-to-days (current-time)) (time-to-days time))
  9084. 'day))
  9085. ((equal (match-string 1 ts) "+")
  9086. (while (or (= nshift 0)
  9087. (<= (time-to-days time) (time-to-days (current-time))))
  9088. (when (= (incf nshift) nshiftmax)
  9089. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9090. (error "Abort")))
  9091. (org-timestamp-change n (cdr (assoc what whata)))
  9092. (org-at-timestamp-p t)
  9093. (setq ts (match-string 1))
  9094. (setq time (save-match-data (org-time-string-to-time ts))))
  9095. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9096. ;; rematch, so that we have everything in place for the real shift
  9097. (org-at-timestamp-p t)
  9098. (setq ts (match-string 1))
  9099. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9100. (org-timestamp-change n (cdr (assoc what whata)))
  9101. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9102. (setq org-log-post-message msg)
  9103. (message "%s" msg))))
  9104. (defun org-show-todo-tree (arg)
  9105. "Make a compact tree which shows all headlines marked with TODO.
  9106. The tree will show the lines where the regexp matches, and all higher
  9107. headlines above the match.
  9108. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9109. With a numeric prefix N, construct a sparse tree for the Nth element
  9110. of `org-todo-keywords-1'."
  9111. (interactive "P")
  9112. (let ((case-fold-search nil)
  9113. (kwd-re
  9114. (cond ((null arg) org-not-done-regexp)
  9115. ((equal arg '(4))
  9116. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9117. (mapcar 'list org-todo-keywords-1))))
  9118. (concat "\\("
  9119. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9120. "\\)\\>")))
  9121. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9122. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9123. org-todo-keywords-1)))
  9124. (t (error "Invalid prefix argument: %s" arg)))))
  9125. (message "%d TODO entries found"
  9126. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9127. (defun org-deadline (&optional remove time)
  9128. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9129. With argument REMOVE, remove any deadline from the item.
  9130. When TIME is set, it should be an internal time specification, and the
  9131. scheduling will use the corresponding date."
  9132. (interactive "P")
  9133. (if remove
  9134. (progn
  9135. (org-remove-timestamp-with-keyword org-deadline-string)
  9136. (message "Item no longer has a deadline."))
  9137. (if (org-get-repeat)
  9138. (error "Cannot change deadline on task with repeater, please do that by hand")
  9139. (org-add-planning-info 'deadline time 'closed)
  9140. (message "Deadline on %s" org-last-inserted-timestamp))))
  9141. (defun org-schedule (&optional remove time)
  9142. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9143. With argument REMOVE, remove any scheduling date from the item.
  9144. When TIME is set, it should be an internal time specification, and the
  9145. scheduling will use the corresponding date."
  9146. (interactive "P")
  9147. (if remove
  9148. (progn
  9149. (org-remove-timestamp-with-keyword org-scheduled-string)
  9150. (message "Item is no longer scheduled."))
  9151. (if (org-get-repeat)
  9152. (error "Cannot reschedule task with repeater, please do that by hand")
  9153. (org-add-planning-info 'scheduled time 'closed)
  9154. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9155. (defun org-get-scheduled-time (pom &optional inherit)
  9156. "Get the scheduled time as a time tuple, of a format suitable
  9157. for calling org-schedule with, or if there is no scheduling,
  9158. returns nil."
  9159. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9160. (when time
  9161. (apply 'encode-time (org-parse-time-string time)))))
  9162. (defun org-get-deadline-time (pom &optional inherit)
  9163. "Get the deadine as a time tuple, of a format suitable for
  9164. calling org-deadlin with, or if there is no scheduling, returns
  9165. nil."
  9166. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9167. (when time
  9168. (apply 'encode-time (org-parse-time-string time)))))
  9169. (defun org-remove-timestamp-with-keyword (keyword)
  9170. "Remove all time stamps with KEYWORD in the current entry."
  9171. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9172. beg)
  9173. (save-excursion
  9174. (org-back-to-heading t)
  9175. (setq beg (point))
  9176. (outline-next-heading)
  9177. (while (re-search-backward re beg t)
  9178. (replace-match "")
  9179. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9180. (equal (char-before) ?\ ))
  9181. (backward-delete-char 1)
  9182. (if (string-match "^[ \t]*$" (buffer-substring
  9183. (point-at-bol) (point-at-eol)))
  9184. (delete-region (point-at-bol)
  9185. (min (point-max) (1+ (point-at-eol))))))))))
  9186. (defun org-add-planning-info (what &optional time &rest remove)
  9187. "Insert new timestamp with keyword in the line directly after the headline.
  9188. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9189. If non is given, the user is prompted for a date.
  9190. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9191. be removed."
  9192. (interactive)
  9193. (let (org-time-was-given org-end-time-was-given ts
  9194. end default-time default-input)
  9195. (catch 'exit
  9196. (when (and (not time) (memq what '(scheduled deadline)))
  9197. ;; Try to get a default date/time from existing timestamp
  9198. (save-excursion
  9199. (org-back-to-heading t)
  9200. (setq end (save-excursion (outline-next-heading) (point)))
  9201. (when (re-search-forward (if (eq what 'scheduled)
  9202. org-scheduled-time-regexp
  9203. org-deadline-time-regexp)
  9204. end t)
  9205. (setq ts (match-string 1)
  9206. default-time
  9207. (apply 'encode-time (org-parse-time-string ts))
  9208. default-input (and ts (org-get-compact-tod ts))))))
  9209. (when what
  9210. ;; If necessary, get the time from the user
  9211. (setq time (or time (org-read-date nil 'to-time nil nil
  9212. default-time default-input))))
  9213. (when (and org-insert-labeled-timestamps-at-point
  9214. (member what '(scheduled deadline)))
  9215. (insert
  9216. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9217. (org-insert-time-stamp time org-time-was-given
  9218. nil nil nil (list org-end-time-was-given))
  9219. (setq what nil))
  9220. (save-excursion
  9221. (save-restriction
  9222. (let (col list elt ts buffer-invisibility-spec)
  9223. (org-back-to-heading t)
  9224. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9225. (goto-char (match-end 1))
  9226. (setq col (current-column))
  9227. (goto-char (match-end 0))
  9228. (if (eobp) (insert "\n") (forward-char 1))
  9229. (when (and (not what)
  9230. (not (looking-at
  9231. (concat "[ \t]*"
  9232. org-keyword-time-not-clock-regexp))))
  9233. ;; Nothing to add, nothing to remove...... :-)
  9234. (throw 'exit nil))
  9235. (if (and (not (looking-at outline-regexp))
  9236. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9237. "[^\r\n]*"))
  9238. (not (equal (match-string 1) org-clock-string)))
  9239. (narrow-to-region (match-beginning 0) (match-end 0))
  9240. (insert-before-markers "\n")
  9241. (backward-char 1)
  9242. (narrow-to-region (point) (point))
  9243. (and org-adapt-indentation (org-indent-to-column col)))
  9244. ;; Check if we have to remove something.
  9245. (setq list (cons what remove))
  9246. (while list
  9247. (setq elt (pop list))
  9248. (goto-char (point-min))
  9249. (when (or (and (eq elt 'scheduled)
  9250. (re-search-forward org-scheduled-time-regexp nil t))
  9251. (and (eq elt 'deadline)
  9252. (re-search-forward org-deadline-time-regexp nil t))
  9253. (and (eq elt 'closed)
  9254. (re-search-forward org-closed-time-regexp nil t)))
  9255. (replace-match "")
  9256. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9257. (skip-chars-backward " ")
  9258. (if (looking-at " +") (replace-match ""))))
  9259. (goto-char (point-max))
  9260. (when what
  9261. (insert
  9262. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9263. (cond ((eq what 'scheduled) org-scheduled-string)
  9264. ((eq what 'deadline) org-deadline-string)
  9265. ((eq what 'closed) org-closed-string))
  9266. " ")
  9267. (setq ts (org-insert-time-stamp
  9268. time
  9269. (or org-time-was-given
  9270. (and (eq what 'closed) org-log-done-with-time))
  9271. (eq what 'closed)
  9272. nil nil (list org-end-time-was-given)))
  9273. (end-of-line 1))
  9274. (goto-char (point-min))
  9275. (widen)
  9276. (if (and (looking-at "[ \t]+\n")
  9277. (equal (char-before) ?\n))
  9278. (delete-region (1- (point)) (point-at-eol)))
  9279. ts))))))
  9280. (defvar org-log-note-marker (make-marker))
  9281. (defvar org-log-note-purpose nil)
  9282. (defvar org-log-note-state nil)
  9283. (defvar org-log-note-previous-state nil)
  9284. (defvar org-log-note-how nil)
  9285. (defvar org-log-note-extra nil)
  9286. (defvar org-log-note-window-configuration nil)
  9287. (defvar org-log-note-return-to (make-marker))
  9288. (defvar org-log-post-message nil
  9289. "Message to be displayed after a log note has been stored.
  9290. The auto-repeater uses this.")
  9291. (defun org-add-note ()
  9292. "Add a note to the current entry.
  9293. This is done in the same way as adding a state change note."
  9294. (interactive)
  9295. (org-add-log-setup 'note nil nil 'findpos nil))
  9296. (defvar org-property-end-re)
  9297. (defun org-add-log-setup (&optional purpose state prev-state
  9298. findpos how &optional extra)
  9299. "Set up the post command hook to take a note.
  9300. If this is about to TODO state change, the new state is expected in STATE.
  9301. When FINDPOS is non-nil, find the correct position for the note in
  9302. the current entry. If not, assume that it can be inserted at point.
  9303. HOW is an indicator what kind of note should be created.
  9304. EXTRA is additional text that will be inserted into the notes buffer."
  9305. (let* ((org-log-into-drawer (org-log-into-drawer))
  9306. (drawer (cond ((stringp org-log-into-drawer)
  9307. org-log-into-drawer)
  9308. (org-log-into-drawer "LOGBOOK")
  9309. (t nil))))
  9310. (save-restriction
  9311. (save-excursion
  9312. (when findpos
  9313. (org-back-to-heading t)
  9314. (narrow-to-region (point) (save-excursion
  9315. (outline-next-heading) (point)))
  9316. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9317. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9318. "[^\r\n]*\\)?"))
  9319. (goto-char (match-end 0))
  9320. (cond
  9321. (drawer
  9322. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9323. nil t)
  9324. (progn
  9325. (goto-char (match-end 0))
  9326. (or org-log-states-order-reversed
  9327. (and (re-search-forward org-property-end-re nil t)
  9328. (goto-char (1- (match-beginning 0))))))
  9329. (insert "\n:" drawer ":\n:END:")
  9330. (beginning-of-line 0)
  9331. (org-indent-line-function)
  9332. (beginning-of-line 2)
  9333. (org-indent-line-function)
  9334. (end-of-line 0)))
  9335. ((and org-log-state-notes-insert-after-drawers
  9336. (save-excursion
  9337. (forward-line) (looking-at org-drawer-regexp)))
  9338. (forward-line)
  9339. (while (looking-at org-drawer-regexp)
  9340. (goto-char (match-end 0))
  9341. (re-search-forward org-property-end-re (point-max) t)
  9342. (forward-line))
  9343. (forward-line -1)))
  9344. (unless org-log-states-order-reversed
  9345. (and (= (char-after) ?\n) (forward-char 1))
  9346. (org-skip-over-state-notes)
  9347. (skip-chars-backward " \t\n\r")))
  9348. (move-marker org-log-note-marker (point))
  9349. (setq org-log-note-purpose purpose
  9350. org-log-note-state state
  9351. org-log-note-previous-state prev-state
  9352. org-log-note-how how
  9353. org-log-note-extra extra)
  9354. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9355. (defun org-skip-over-state-notes ()
  9356. "Skip past the list of State notes in an entry."
  9357. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9358. (while (looking-at "[ \t]*- State")
  9359. (condition-case nil
  9360. (org-next-item)
  9361. (error (org-end-of-item)))))
  9362. (defun org-add-log-note (&optional purpose)
  9363. "Pop up a window for taking a note, and add this note later at point."
  9364. (remove-hook 'post-command-hook 'org-add-log-note)
  9365. (setq org-log-note-window-configuration (current-window-configuration))
  9366. (delete-other-windows)
  9367. (move-marker org-log-note-return-to (point))
  9368. (switch-to-buffer (marker-buffer org-log-note-marker))
  9369. (goto-char org-log-note-marker)
  9370. (org-switch-to-buffer-other-window "*Org Note*")
  9371. (erase-buffer)
  9372. (if (memq org-log-note-how '(time state))
  9373. (let (current-prefix-arg) (org-store-log-note))
  9374. (let ((org-inhibit-startup t)) (org-mode))
  9375. (insert (format "# Insert note for %s.
  9376. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9377. (cond
  9378. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9379. ((eq org-log-note-purpose 'done) "closed todo item")
  9380. ((eq org-log-note-purpose 'state)
  9381. (format "state change from \"%s\" to \"%s\""
  9382. (or org-log-note-previous-state "")
  9383. (or org-log-note-state "")))
  9384. ((eq org-log-note-purpose 'note)
  9385. "this entry")
  9386. (t (error "This should not happen")))))
  9387. (if org-log-note-extra (insert org-log-note-extra))
  9388. (org-set-local 'org-finish-function 'org-store-log-note)))
  9389. (defvar org-note-abort nil) ; dynamically scoped
  9390. (defun org-store-log-note ()
  9391. "Finish taking a log note, and insert it to where it belongs."
  9392. (let ((txt (buffer-string))
  9393. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9394. lines ind)
  9395. (kill-buffer (current-buffer))
  9396. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9397. (setq txt (replace-match "" t t txt)))
  9398. (if (string-match "\\s-+\\'" txt)
  9399. (setq txt (replace-match "" t t txt)))
  9400. (setq lines (org-split-string txt "\n"))
  9401. (when (and note (string-match "\\S-" note))
  9402. (setq note
  9403. (org-replace-escapes
  9404. note
  9405. (list (cons "%u" (user-login-name))
  9406. (cons "%U" user-full-name)
  9407. (cons "%t" (format-time-string
  9408. (org-time-stamp-format 'long 'inactive)
  9409. (current-time)))
  9410. (cons "%s" (if org-log-note-state
  9411. (concat "\"" org-log-note-state "\"")
  9412. ""))
  9413. (cons "%S" (if org-log-note-previous-state
  9414. (concat "\"" org-log-note-previous-state "\"")
  9415. "\"\"")))))
  9416. (if lines (setq note (concat note " \\\\")))
  9417. (push note lines))
  9418. (when (or current-prefix-arg org-note-abort)
  9419. (when org-log-into-drawer
  9420. (org-remove-empty-drawer-at
  9421. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9422. org-log-note-marker))
  9423. (setq lines nil))
  9424. (when lines
  9425. (save-excursion
  9426. (set-buffer (marker-buffer org-log-note-marker))
  9427. (save-excursion
  9428. (goto-char org-log-note-marker)
  9429. (move-marker org-log-note-marker nil)
  9430. (end-of-line 1)
  9431. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9432. (insert "- " (pop lines))
  9433. (org-indent-line-function)
  9434. (beginning-of-line 1)
  9435. (looking-at "[ \t]*")
  9436. (setq ind (concat (match-string 0) " "))
  9437. (end-of-line 1)
  9438. (while lines (insert "\n" ind (pop lines)))
  9439. (message "Note stored")
  9440. (org-back-to-heading t)
  9441. (org-cycle-hide-drawers 'children)))))
  9442. (set-window-configuration org-log-note-window-configuration)
  9443. (with-current-buffer (marker-buffer org-log-note-return-to)
  9444. (goto-char org-log-note-return-to))
  9445. (move-marker org-log-note-return-to nil)
  9446. (and org-log-post-message (message "%s" org-log-post-message)))
  9447. (defun org-remove-empty-drawer-at (drawer pos)
  9448. "Remove an emptyr DARWER drawer at position POS.
  9449. POS may also be a marker."
  9450. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9451. (save-excursion
  9452. (save-restriction
  9453. (widen)
  9454. (goto-char pos)
  9455. (if (org-in-regexp
  9456. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9457. (replace-match ""))))))
  9458. (defun org-sparse-tree (&optional arg)
  9459. "Create a sparse tree, prompt for the details.
  9460. This command can create sparse trees. You first need to select the type
  9461. of match used to create the tree:
  9462. t Show entries with a specific TODO keyword.
  9463. m Show entries selected by a tags/property match.
  9464. p Enter a property name and its value (both with completion on existing
  9465. names/values) and show entries with that property.
  9466. r Show entries matching a regular expression.
  9467. d Show deadlines due within `org-deadline-warning-days'.
  9468. b Show deadlines and scheduled items before a date.
  9469. a Show deadlines and scheduled items after a date."
  9470. (interactive "P")
  9471. (let (ans kwd value)
  9472. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9473. (setq ans (read-char-exclusive))
  9474. (cond
  9475. ((equal ans ?d)
  9476. (call-interactively 'org-check-deadlines))
  9477. ((equal ans ?b)
  9478. (call-interactively 'org-check-before-date))
  9479. ((equal ans ?a)
  9480. (call-interactively 'org-check-after-date))
  9481. ((equal ans ?t)
  9482. (org-show-todo-tree '(4)))
  9483. ((member ans '(?T ?m))
  9484. (call-interactively 'org-match-sparse-tree))
  9485. ((member ans '(?p ?P))
  9486. (setq kwd (org-icompleting-read "Property: "
  9487. (mapcar 'list (org-buffer-property-keys))))
  9488. (setq value (org-icompleting-read "Value: "
  9489. (mapcar 'list (org-property-values kwd))))
  9490. (unless (string-match "\\`{.*}\\'" value)
  9491. (setq value (concat "\"" value "\"")))
  9492. (org-match-sparse-tree arg (concat kwd "=" value)))
  9493. ((member ans '(?r ?R ?/))
  9494. (call-interactively 'org-occur))
  9495. (t (error "No such sparse tree command \"%c\"" ans)))))
  9496. (defvar org-occur-highlights nil
  9497. "List of overlays used for occur matches.")
  9498. (make-variable-buffer-local 'org-occur-highlights)
  9499. (defvar org-occur-parameters nil
  9500. "Parameters of the active org-occur calls.
  9501. This is a list, each call to org-occur pushes as cons cell,
  9502. containing the regular expression and the callback, onto the list.
  9503. The list can contain several entries if `org-occur' has been called
  9504. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9505. will only contain one set of parameters. When the highlights are
  9506. removed (for example with `C-c C-c', or with the next edit (depending
  9507. on `org-remove-highlights-with-change'), this variable is emptied
  9508. as well.")
  9509. (make-variable-buffer-local 'org-occur-parameters)
  9510. (defun org-occur (regexp &optional keep-previous callback)
  9511. "Make a compact tree which shows all matches of REGEXP.
  9512. The tree will show the lines where the regexp matches, and all higher
  9513. headlines above the match. It will also show the heading after the match,
  9514. to make sure editing the matching entry is easy.
  9515. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9516. call to `org-occur' will be kept, to allow stacking of calls to this
  9517. command.
  9518. If CALLBACK is non-nil, it is a function which is called to confirm
  9519. that the match should indeed be shown."
  9520. (interactive "sRegexp: \nP")
  9521. (when (equal regexp "")
  9522. (error "Regexp cannot be empty"))
  9523. (unless keep-previous
  9524. (org-remove-occur-highlights nil nil t))
  9525. (push (cons regexp callback) org-occur-parameters)
  9526. (let ((cnt 0))
  9527. (save-excursion
  9528. (goto-char (point-min))
  9529. (if (or (not keep-previous) ; do not want to keep
  9530. (not org-occur-highlights)) ; no previous matches
  9531. ;; hide everything
  9532. (org-overview))
  9533. (while (re-search-forward regexp nil t)
  9534. (when (or (not callback)
  9535. (save-match-data (funcall callback)))
  9536. (setq cnt (1+ cnt))
  9537. (when org-highlight-sparse-tree-matches
  9538. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9539. (org-show-context 'occur-tree))))
  9540. (when org-remove-highlights-with-change
  9541. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9542. nil 'local))
  9543. (unless org-sparse-tree-open-archived-trees
  9544. (org-hide-archived-subtrees (point-min) (point-max)))
  9545. (run-hooks 'org-occur-hook)
  9546. (if (interactive-p)
  9547. (message "%d match(es) for regexp %s" cnt regexp))
  9548. cnt))
  9549. (defun org-show-context (&optional key)
  9550. "Make sure point and context and visible.
  9551. How much context is shown depends upon the variables
  9552. `org-show-hierarchy-above', `org-show-following-heading'. and
  9553. `org-show-siblings'."
  9554. (let ((heading-p (org-on-heading-p t))
  9555. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9556. (following-p (org-get-alist-option org-show-following-heading key))
  9557. (entry-p (org-get-alist-option org-show-entry-below key))
  9558. (siblings-p (org-get-alist-option org-show-siblings key)))
  9559. (catch 'exit
  9560. ;; Show heading or entry text
  9561. (if (and heading-p (not entry-p))
  9562. (org-flag-heading nil) ; only show the heading
  9563. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9564. (org-show-hidden-entry))) ; show entire entry
  9565. (when following-p
  9566. ;; Show next sibling, or heading below text
  9567. (save-excursion
  9568. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9569. (org-flag-heading nil))))
  9570. (when siblings-p (org-show-siblings))
  9571. (when hierarchy-p
  9572. ;; show all higher headings, possibly with siblings
  9573. (save-excursion
  9574. (while (and (condition-case nil
  9575. (progn (org-up-heading-all 1) t)
  9576. (error nil))
  9577. (not (bobp)))
  9578. (org-flag-heading nil)
  9579. (when siblings-p (org-show-siblings))))))))
  9580. (defun org-reveal (&optional siblings)
  9581. "Show current entry, hierarchy above it, and the following headline.
  9582. This can be used to show a consistent set of context around locations
  9583. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9584. not t for the search context.
  9585. With optional argument SIBLINGS, on each level of the hierarchy all
  9586. siblings are shown. This repairs the tree structure to what it would
  9587. look like when opened with hierarchical calls to `org-cycle'."
  9588. (interactive "P")
  9589. (let ((org-show-hierarchy-above t)
  9590. (org-show-following-heading t)
  9591. (org-show-siblings (if siblings t org-show-siblings)))
  9592. (org-show-context nil)))
  9593. (defun org-highlight-new-match (beg end)
  9594. "Highlight from BEG to END and mark the highlight is an occur headline."
  9595. (let ((ov (org-make-overlay beg end)))
  9596. (org-overlay-put ov 'face 'secondary-selection)
  9597. (push ov org-occur-highlights)))
  9598. (defun org-remove-occur-highlights (&optional beg end noremove)
  9599. "Remove the occur highlights from the buffer.
  9600. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9601. from the `before-change-functions' in the current buffer."
  9602. (interactive)
  9603. (unless org-inhibit-highlight-removal
  9604. (mapc 'org-delete-overlay org-occur-highlights)
  9605. (setq org-occur-highlights nil)
  9606. (setq org-occur-parameters nil)
  9607. (unless noremove
  9608. (remove-hook 'before-change-functions
  9609. 'org-remove-occur-highlights 'local))))
  9610. ;;;; Priorities
  9611. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9612. "Regular expression matching the priority indicator.")
  9613. (defvar org-remove-priority-next-time nil)
  9614. (defun org-priority-up ()
  9615. "Increase the priority of the current item."
  9616. (interactive)
  9617. (org-priority 'up))
  9618. (defun org-priority-down ()
  9619. "Decrease the priority of the current item."
  9620. (interactive)
  9621. (org-priority 'down))
  9622. (defun org-priority (&optional action)
  9623. "Change the priority of an item by ARG.
  9624. ACTION can be `set', `up', `down', or a character."
  9625. (interactive)
  9626. (unless org-enable-priority-commands
  9627. (error "Priority commands are disabled"))
  9628. (setq action (or action 'set))
  9629. (let (current new news have remove)
  9630. (save-excursion
  9631. (org-back-to-heading t)
  9632. (if (looking-at org-priority-regexp)
  9633. (setq current (string-to-char (match-string 2))
  9634. have t)
  9635. (setq current org-default-priority))
  9636. (cond
  9637. ((or (eq action 'set)
  9638. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9639. (if (not (eq action 'set))
  9640. (setq new action)
  9641. (message "Priority %c-%c, SPC to remove: "
  9642. org-highest-priority org-lowest-priority)
  9643. (setq new (read-char-exclusive)))
  9644. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9645. (= (upcase org-lowest-priority) org-lowest-priority))
  9646. (setq new (upcase new)))
  9647. (cond ((equal new ?\ ) (setq remove t))
  9648. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9649. (error "Priority must be between `%c' and `%c'"
  9650. org-highest-priority org-lowest-priority))))
  9651. ((eq action 'up)
  9652. (if (and (not have) (eq last-command this-command))
  9653. (setq new org-lowest-priority)
  9654. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9655. org-default-priority (1- current)))))
  9656. ((eq action 'down)
  9657. (if (and (not have) (eq last-command this-command))
  9658. (setq new org-highest-priority)
  9659. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9660. org-default-priority (1+ current)))))
  9661. (t (error "Invalid action")))
  9662. (if (or (< (upcase new) org-highest-priority)
  9663. (> (upcase new) org-lowest-priority))
  9664. (setq remove t))
  9665. (setq news (format "%c" new))
  9666. (if have
  9667. (if remove
  9668. (replace-match "" t t nil 1)
  9669. (replace-match news t t nil 2))
  9670. (if remove
  9671. (error "No priority cookie found in line")
  9672. (let ((case-fold-search nil))
  9673. (looking-at org-todo-line-regexp))
  9674. (if (match-end 2)
  9675. (progn
  9676. (goto-char (match-end 2))
  9677. (insert " [#" news "]"))
  9678. (goto-char (match-beginning 3))
  9679. (insert "[#" news "] "))))
  9680. (org-preserve-lc (org-set-tags nil 'align)))
  9681. (if remove
  9682. (message "Priority removed")
  9683. (message "Priority of current item set to %s" news))))
  9684. (defun org-get-priority (s)
  9685. "Find priority cookie and return priority."
  9686. (save-match-data
  9687. (if (not (string-match org-priority-regexp s))
  9688. (* 1000 (- org-lowest-priority org-default-priority))
  9689. (* 1000 (- org-lowest-priority
  9690. (string-to-char (match-string 2 s)))))))
  9691. ;;;; Tags
  9692. (defvar org-agenda-archives-mode)
  9693. (defvar org-map-continue-from nil
  9694. "Position from where mapping should continue.
  9695. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9696. (defvar org-scanner-tags nil
  9697. "The current tag list while the tags scanner is running.")
  9698. (defvar org-trust-scanner-tags nil
  9699. "Should `org-get-tags-at' use the tags fro the scanner.
  9700. This is for internal dynamical scoping only.
  9701. When this is non-nil, the function `org-get-tags-at' will return the value
  9702. of `org-scanner-tags' instead of building the list by itself. This
  9703. can lead to large speed-ups when the tags scanner is used in a file with
  9704. many entries, and when the list of tags is retrieved, for example to
  9705. obtain a list of properties. Building the tags list for each entry in such
  9706. a file becomes an N^2 operation - but with this variable set, it scales
  9707. as N.")
  9708. (defun org-scan-tags (action matcher &optional todo-only)
  9709. "Scan headline tags with inheritance and produce output ACTION.
  9710. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9711. or `agenda' to produce an entry list for an agenda view. It can also be
  9712. a Lisp form or a function that should be called at each matched headline, in
  9713. this case the return value is a list of all return values from these calls.
  9714. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9715. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9716. only lines with a TODO keyword are included in the output."
  9717. (require 'org-agenda)
  9718. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9719. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9720. (org-re
  9721. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9722. (props (list 'face 'default
  9723. 'done-face 'org-agenda-done
  9724. 'undone-face 'default
  9725. 'mouse-face 'highlight
  9726. 'org-not-done-regexp org-not-done-regexp
  9727. 'org-todo-regexp org-todo-regexp
  9728. 'keymap org-agenda-keymap
  9729. 'help-echo
  9730. (format "mouse-2 or RET jump to org file %s"
  9731. (abbreviate-file-name
  9732. (or (buffer-file-name (buffer-base-buffer))
  9733. (buffer-name (buffer-base-buffer)))))))
  9734. (case-fold-search nil)
  9735. (org-map-continue-from nil)
  9736. lspos tags tags-list
  9737. (tags-alist (list (cons 0 org-file-tags)))
  9738. (llast 0) rtn rtn1 level category i txt
  9739. todo marker entry priority)
  9740. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9741. (setq action (list 'lambda nil action)))
  9742. (save-excursion
  9743. (goto-char (point-min))
  9744. (when (eq action 'sparse-tree)
  9745. (org-overview)
  9746. (org-remove-occur-highlights))
  9747. (while (re-search-forward re nil t)
  9748. (catch :skip
  9749. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9750. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9751. (goto-char (setq lspos (match-beginning 0)))
  9752. (setq level (org-reduced-level (funcall outline-level))
  9753. category (org-get-category))
  9754. (setq i llast llast level)
  9755. ;; remove tag lists from same and sublevels
  9756. (while (>= i level)
  9757. (when (setq entry (assoc i tags-alist))
  9758. (setq tags-alist (delete entry tags-alist)))
  9759. (setq i (1- i)))
  9760. ;; add the next tags
  9761. (when tags
  9762. (setq tags (org-split-string tags ":")
  9763. tags-alist
  9764. (cons (cons level tags) tags-alist)))
  9765. ;; compile tags for current headline
  9766. (setq tags-list
  9767. (if org-use-tag-inheritance
  9768. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9769. tags)
  9770. org-scanner-tags tags-list)
  9771. (when org-use-tag-inheritance
  9772. (setcdr (car tags-alist)
  9773. (mapcar (lambda (x)
  9774. (setq x (copy-sequence x))
  9775. (org-add-prop-inherited x))
  9776. (cdar tags-alist))))
  9777. (when (and tags org-use-tag-inheritance
  9778. (or (not (eq t org-use-tag-inheritance))
  9779. org-tags-exclude-from-inheritance))
  9780. ;; selective inheritance, remove uninherited ones
  9781. (setcdr (car tags-alist)
  9782. (org-remove-uniherited-tags (cdar tags-alist))))
  9783. (when (and (or (not todo-only)
  9784. (and (member todo org-not-done-keywords)
  9785. (or (not org-agenda-tags-todo-honor-ignore-options)
  9786. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9787. (let ((case-fold-search t)) (eval matcher))
  9788. (or
  9789. (not (member org-archive-tag tags-list))
  9790. ;; we have an archive tag, should we use this anyway?
  9791. (or (not org-agenda-skip-archived-trees)
  9792. (and (eq action 'agenda) org-agenda-archives-mode))))
  9793. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9794. ;; select this headline
  9795. (cond
  9796. ((eq action 'sparse-tree)
  9797. (and org-highlight-sparse-tree-matches
  9798. (org-get-heading) (match-end 0)
  9799. (org-highlight-new-match
  9800. (match-beginning 0) (match-beginning 1)))
  9801. (org-show-context 'tags-tree))
  9802. ((eq action 'agenda)
  9803. (setq txt (org-format-agenda-item
  9804. ""
  9805. (concat
  9806. (if (eq org-tags-match-list-sublevels 'indented)
  9807. (make-string (1- level) ?.) "")
  9808. (org-get-heading))
  9809. category
  9810. tags-list
  9811. )
  9812. priority (org-get-priority txt))
  9813. (goto-char lspos)
  9814. (setq marker (org-agenda-new-marker))
  9815. (org-add-props txt props
  9816. 'org-marker marker 'org-hd-marker marker 'org-category category
  9817. 'todo-state todo
  9818. 'priority priority 'type "tagsmatch")
  9819. (push txt rtn))
  9820. ((functionp action)
  9821. (setq org-map-continue-from nil)
  9822. (save-excursion
  9823. (setq rtn1 (funcall action))
  9824. (push rtn1 rtn)))
  9825. (t (error "Invalid action")))
  9826. ;; if we are to skip sublevels, jump to end of subtree
  9827. (unless org-tags-match-list-sublevels
  9828. (org-end-of-subtree t)
  9829. (backward-char 1))))
  9830. ;; Get the correct position from where to continue
  9831. (if org-map-continue-from
  9832. (goto-char org-map-continue-from)
  9833. (and (= (point) lspos) (end-of-line 1)))))
  9834. (when (and (eq action 'sparse-tree)
  9835. (not org-sparse-tree-open-archived-trees))
  9836. (org-hide-archived-subtrees (point-min) (point-max)))
  9837. (nreverse rtn)))
  9838. (defun org-remove-uniherited-tags (tags)
  9839. "Remove all tags that are not inherited from the list TAGS."
  9840. (cond
  9841. ((eq org-use-tag-inheritance t)
  9842. (if org-tags-exclude-from-inheritance
  9843. (org-delete-all org-tags-exclude-from-inheritance tags)
  9844. tags))
  9845. ((not org-use-tag-inheritance) nil)
  9846. ((stringp org-use-tag-inheritance)
  9847. (delq nil (mapcar
  9848. (lambda (x)
  9849. (if (and (string-match org-use-tag-inheritance x)
  9850. (not (member x org-tags-exclude-from-inheritance)))
  9851. x nil))
  9852. tags)))
  9853. ((listp org-use-tag-inheritance)
  9854. (delq nil (mapcar
  9855. (lambda (x)
  9856. (if (member x org-use-tag-inheritance) x nil))
  9857. tags)))))
  9858. (defvar todo-only) ;; dynamically scoped
  9859. (defun org-match-sparse-tree (&optional todo-only match)
  9860. "Create a sparse tree according to tags string MATCH.
  9861. MATCH can contain positive and negative selection of tags, like
  9862. \"+WORK+URGENT-WITHBOSS\".
  9863. If optional argument TODO-ONLY is non-nil, only select lines that are
  9864. also TODO lines."
  9865. (interactive "P")
  9866. (org-prepare-agenda-buffers (list (current-buffer)))
  9867. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9868. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9869. (defvar org-cached-props nil)
  9870. (defun org-cached-entry-get (pom property)
  9871. (if (or (eq t org-use-property-inheritance)
  9872. (and (stringp org-use-property-inheritance)
  9873. (string-match org-use-property-inheritance property))
  9874. (and (listp org-use-property-inheritance)
  9875. (member property org-use-property-inheritance)))
  9876. ;; Caching is not possible, check it directly
  9877. (org-entry-get pom property 'inherit)
  9878. ;; Get all properties, so that we can do complicated checks easily
  9879. (cdr (assoc property (or org-cached-props
  9880. (setq org-cached-props
  9881. (org-entry-properties pom)))))))
  9882. (defun org-global-tags-completion-table (&optional files)
  9883. "Return the list of all tags in all agenda buffer/files."
  9884. (save-excursion
  9885. (org-uniquify
  9886. (delq nil
  9887. (apply 'append
  9888. (mapcar
  9889. (lambda (file)
  9890. (set-buffer (find-file-noselect file))
  9891. (append (org-get-buffer-tags)
  9892. (mapcar (lambda (x) (if (stringp (car-safe x))
  9893. (list (car-safe x)) nil))
  9894. org-tag-alist)))
  9895. (if (and files (car files))
  9896. files
  9897. (org-agenda-files))))))))
  9898. (defun org-make-tags-matcher (match)
  9899. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9900. ;; todo-only is scoped dynamically into this function, and the function
  9901. ;; may change it if the matcher asks for it.
  9902. (unless match
  9903. ;; Get a new match request, with completion
  9904. (let ((org-last-tags-completion-table
  9905. (org-global-tags-completion-table)))
  9906. (setq match (org-completing-read-no-i
  9907. "Match: " 'org-tags-completion-function nil nil nil
  9908. 'org-tags-history))))
  9909. ;; Parse the string and create a lisp form
  9910. (let ((match0 match)
  9911. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9912. minus tag mm
  9913. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9914. orterms term orlist re-p str-p level-p level-op time-p
  9915. prop-p pn pv po cat-p gv rest)
  9916. (if (string-match "/+" match)
  9917. ;; match contains also a todo-matching request
  9918. (progn
  9919. (setq tagsmatch (substring match 0 (match-beginning 0))
  9920. todomatch (substring match (match-end 0)))
  9921. (if (string-match "^!" todomatch)
  9922. (setq todo-only t todomatch (substring todomatch 1)))
  9923. (if (string-match "^\\s-*$" todomatch)
  9924. (setq todomatch nil)))
  9925. ;; only matching tags
  9926. (setq tagsmatch match todomatch nil))
  9927. ;; Make the tags matcher
  9928. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9929. (setq tagsmatcher t)
  9930. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9931. (while (setq term (pop orterms))
  9932. (while (and (equal (substring term -1) "\\") orterms)
  9933. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9934. (while (string-match re term)
  9935. (setq rest (substring term (match-end 0))
  9936. minus (and (match-end 1)
  9937. (equal (match-string 1 term) "-"))
  9938. tag (match-string 2 term)
  9939. re-p (equal (string-to-char tag) ?{)
  9940. level-p (match-end 4)
  9941. prop-p (match-end 5)
  9942. mm (cond
  9943. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9944. (level-p
  9945. (setq level-op (org-op-to-function (match-string 3 term)))
  9946. `(,level-op level ,(string-to-number
  9947. (match-string 4 term))))
  9948. (prop-p
  9949. (setq pn (match-string 5 term)
  9950. po (match-string 6 term)
  9951. pv (match-string 7 term)
  9952. cat-p (equal pn "CATEGORY")
  9953. re-p (equal (string-to-char pv) ?{)
  9954. str-p (equal (string-to-char pv) ?\")
  9955. time-p (save-match-data
  9956. (string-match "^\"[[<].*[]>]\"$" pv))
  9957. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9958. (if time-p (setq pv (org-matcher-time pv)))
  9959. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9960. (cond
  9961. ((equal pn "CATEGORY")
  9962. (setq gv '(get-text-property (point) 'org-category)))
  9963. ((equal pn "TODO")
  9964. (setq gv 'todo))
  9965. (t
  9966. (setq gv `(org-cached-entry-get nil ,pn))))
  9967. (if re-p
  9968. (if (eq po 'org<>)
  9969. `(not (string-match ,pv (or ,gv "")))
  9970. `(string-match ,pv (or ,gv "")))
  9971. (if str-p
  9972. `(,po (or ,gv "") ,pv)
  9973. `(,po (string-to-number (or ,gv ""))
  9974. ,(string-to-number pv) ))))
  9975. (t `(member ,tag tags-list)))
  9976. mm (if minus (list 'not mm) mm)
  9977. term rest)
  9978. (push mm tagsmatcher))
  9979. (push (if (> (length tagsmatcher) 1)
  9980. (cons 'and tagsmatcher)
  9981. (car tagsmatcher))
  9982. orlist)
  9983. (setq tagsmatcher nil))
  9984. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9985. (setq tagsmatcher
  9986. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9987. ;; Make the todo matcher
  9988. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9989. (setq todomatcher t)
  9990. (setq orterms (org-split-string todomatch "|") orlist nil)
  9991. (while (setq term (pop orterms))
  9992. (while (string-match re term)
  9993. (setq minus (and (match-end 1)
  9994. (equal (match-string 1 term) "-"))
  9995. kwd (match-string 2 term)
  9996. re-p (equal (string-to-char kwd) ?{)
  9997. term (substring term (match-end 0))
  9998. mm (if re-p
  9999. `(string-match ,(substring kwd 1 -1) todo)
  10000. (list 'equal 'todo kwd))
  10001. mm (if minus (list 'not mm) mm))
  10002. (push mm todomatcher))
  10003. (push (if (> (length todomatcher) 1)
  10004. (cons 'and todomatcher)
  10005. (car todomatcher))
  10006. orlist)
  10007. (setq todomatcher nil))
  10008. (setq todomatcher (if (> (length orlist) 1)
  10009. (cons 'or orlist) (car orlist))))
  10010. ;; Return the string and lisp forms of the matcher
  10011. (setq matcher (if todomatcher
  10012. (list 'and tagsmatcher todomatcher)
  10013. tagsmatcher))
  10014. (cons match0 matcher)))
  10015. (defun org-op-to-function (op &optional stringp)
  10016. "Turn an operator into the appropriate function."
  10017. (setq op
  10018. (cond
  10019. ((equal op "<" ) '(< string< org-time<))
  10020. ((equal op ">" ) '(> org-string> org-time>))
  10021. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10022. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10023. ((member op '("=" "==")) '(= string= org-time=))
  10024. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10025. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10026. (defun org<> (a b) (not (= a b)))
  10027. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10028. (defun org-string>= (a b) (not (string< a b)))
  10029. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10030. (defun org-string<> (a b) (not (string= a b)))
  10031. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10032. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10033. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10034. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10035. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10036. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10037. (defun org-2ft (s)
  10038. "Convert S to a floating point time.
  10039. If S is already a number, just return it. If it is a string, parse
  10040. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10041. (cond
  10042. ((numberp s) s)
  10043. ((stringp s)
  10044. (condition-case nil
  10045. (float-time (apply 'encode-time (org-parse-time-string s)))
  10046. (error 0.)))
  10047. (t 0.)))
  10048. (defun org-time-today ()
  10049. "Time in seconds today at 0:00.
  10050. Returns the float number of seconds since the beginning of the
  10051. epoch to the beginning of today (00:00)."
  10052. (float-time (apply 'encode-time
  10053. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10054. (defun org-matcher-time (s)
  10055. "Interpret a time comparison value."
  10056. (save-match-data
  10057. (cond
  10058. ((string= s "<now>") (float-time))
  10059. ((string= s "<today>") (org-time-today))
  10060. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10061. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10062. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10063. (+ (org-time-today)
  10064. (* (string-to-number (match-string 1 s))
  10065. (cdr (assoc (match-string 2 s)
  10066. '(("d" . 86400.0) ("w" . 604800.0)
  10067. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10068. (t (org-2ft s)))))
  10069. (defun org-match-any-p (re list)
  10070. "Does re match any element of list?"
  10071. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10072. (delq nil list))
  10073. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10074. (defvar org-tags-overlay (org-make-overlay 1 1))
  10075. (org-detach-overlay org-tags-overlay)
  10076. (defun org-get-local-tags-at (&optional pos)
  10077. "Get a list of tags defined in the current headline."
  10078. (org-get-tags-at pos 'local))
  10079. (defun org-get-local-tags ()
  10080. "Get a list of tags defined in the current headline."
  10081. (org-get-tags-at nil 'local))
  10082. (defun org-get-tags-at (&optional pos local)
  10083. "Get a list of all headline tags applicable at POS.
  10084. POS defaults to point. If tags are inherited, the list contains
  10085. the targets in the same sequence as the headlines appear, i.e.
  10086. the tags of the current headline come last.
  10087. When LOCAL is non-nil, only return tags from the current headline,
  10088. ignore inherited ones."
  10089. (interactive)
  10090. (if (and org-trust-scanner-tags
  10091. (or (not pos) (equal pos (point)))
  10092. (not local))
  10093. org-scanner-tags
  10094. (let (tags ltags lastpos parent)
  10095. (save-excursion
  10096. (save-restriction
  10097. (widen)
  10098. (goto-char (or pos (point)))
  10099. (save-match-data
  10100. (catch 'done
  10101. (condition-case nil
  10102. (progn
  10103. (org-back-to-heading t)
  10104. (while (not (equal lastpos (point)))
  10105. (setq lastpos (point))
  10106. (when (looking-at
  10107. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10108. (setq ltags (org-split-string
  10109. (org-match-string-no-properties 1) ":"))
  10110. (when parent
  10111. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10112. (setq tags (append
  10113. (if parent
  10114. (org-remove-uniherited-tags ltags)
  10115. ltags)
  10116. tags)))
  10117. (or org-use-tag-inheritance (throw 'done t))
  10118. (if local (throw 'done t))
  10119. (or (org-up-heading-safe) (error nil))
  10120. (setq parent t)))
  10121. (error nil)))))
  10122. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10123. (defun org-add-prop-inherited (s)
  10124. (add-text-properties 0 (length s) '(inherited t) s)
  10125. s)
  10126. (defun org-toggle-tag (tag &optional onoff)
  10127. "Toggle the tag TAG for the current line.
  10128. If ONOFF is `on' or `off', don't toggle but set to this state."
  10129. (let (res current)
  10130. (save-excursion
  10131. (org-back-to-heading t)
  10132. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10133. (point-at-eol) t)
  10134. (progn
  10135. (setq current (match-string 1))
  10136. (replace-match ""))
  10137. (setq current ""))
  10138. (setq current (nreverse (org-split-string current ":")))
  10139. (cond
  10140. ((eq onoff 'on)
  10141. (setq res t)
  10142. (or (member tag current) (push tag current)))
  10143. ((eq onoff 'off)
  10144. (or (not (member tag current)) (setq current (delete tag current))))
  10145. (t (if (member tag current)
  10146. (setq current (delete tag current))
  10147. (setq res t)
  10148. (push tag current))))
  10149. (end-of-line 1)
  10150. (if current
  10151. (progn
  10152. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10153. (org-set-tags nil t))
  10154. (delete-horizontal-space))
  10155. (run-hooks 'org-after-tags-change-hook))
  10156. res))
  10157. (defun org-align-tags-here (to-col)
  10158. ;; Assumes that this is a headline
  10159. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10160. (beginning-of-line 1)
  10161. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10162. (< pos (match-beginning 2)))
  10163. (progn
  10164. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10165. (goto-char (match-beginning 1))
  10166. (insert " ")
  10167. (delete-region (point) (1+ (match-beginning 2)))
  10168. (setq ncol (max (1+ (current-column))
  10169. (1+ col)
  10170. (if (> to-col 0)
  10171. to-col
  10172. (- (abs to-col) tags-l))))
  10173. (setq p (point))
  10174. (insert (make-string (- ncol (current-column)) ?\ ))
  10175. (setq ncol (current-column))
  10176. (when indent-tabs-mode (tabify p (point-at-eol)))
  10177. (org-move-to-column (min ncol col) t))
  10178. (goto-char pos))))
  10179. (defun org-set-tags-command (&optional arg just-align)
  10180. "Call the set-tags command for the current entry."
  10181. (interactive "P")
  10182. (if (org-on-heading-p)
  10183. (org-set-tags arg just-align)
  10184. (save-excursion
  10185. (org-back-to-heading t)
  10186. (org-set-tags arg just-align))))
  10187. (defun org-set-tags (&optional arg just-align)
  10188. "Set the tags for the current headline.
  10189. With prefix ARG, realign all tags in headings in the current buffer."
  10190. (interactive "P")
  10191. (let* ((re (concat "^" outline-regexp))
  10192. (current (org-get-tags-string))
  10193. (col (current-column))
  10194. (org-setting-tags t)
  10195. table current-tags inherited-tags ; computed below when needed
  10196. tags p0 c0 c1 rpl)
  10197. (if arg
  10198. (save-excursion
  10199. (goto-char (point-min))
  10200. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10201. (while (re-search-forward re nil t)
  10202. (org-set-tags nil t)
  10203. (end-of-line 1)))
  10204. (message "All tags realigned to column %d" org-tags-column))
  10205. (if just-align
  10206. (setq tags current)
  10207. ;; Get a new set of tags from the user
  10208. (save-excursion
  10209. (setq table (append org-tag-persistent-alist
  10210. (or org-tag-alist (org-get-buffer-tags)))
  10211. org-last-tags-completion-table table
  10212. current-tags (org-split-string current ":")
  10213. inherited-tags (nreverse
  10214. (nthcdr (length current-tags)
  10215. (nreverse (org-get-tags-at))))
  10216. tags
  10217. (if (or (eq t org-use-fast-tag-selection)
  10218. (and org-use-fast-tag-selection
  10219. (delq nil (mapcar 'cdr table))))
  10220. (org-fast-tag-selection
  10221. current-tags inherited-tags table
  10222. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10223. (let ((org-add-colon-after-tag-completion t))
  10224. (org-trim
  10225. (org-without-partial-completion
  10226. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10227. nil nil current 'org-tags-history)))))))
  10228. (while (string-match "[-+&]+" tags)
  10229. ;; No boolean logic, just a list
  10230. (setq tags (replace-match ":" t t tags))))
  10231. (if org-tags-sort-function
  10232. (setq tags (mapconcat 'identity
  10233. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10234. org-tags-sort-function) ":")))
  10235. (if (string-match "\\`[\t ]*\\'" tags)
  10236. (setq tags "")
  10237. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10238. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10239. ;; Insert new tags at the correct column
  10240. (beginning-of-line 1)
  10241. (cond
  10242. ((and (equal current "") (equal tags "")))
  10243. ((re-search-forward
  10244. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10245. (point-at-eol) t)
  10246. (if (equal tags "")
  10247. (setq rpl "")
  10248. (goto-char (match-beginning 0))
  10249. (setq c0 (current-column) p0 (point)
  10250. c1 (max (1+ c0) (if (> org-tags-column 0)
  10251. org-tags-column
  10252. (- (- org-tags-column) (length tags))))
  10253. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10254. (replace-match rpl t t)
  10255. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10256. tags)
  10257. (t (error "Tags alignment failed")))
  10258. (org-move-to-column col)
  10259. (unless just-align
  10260. (run-hooks 'org-after-tags-change-hook)))))
  10261. (defun org-change-tag-in-region (beg end tag off)
  10262. "Add or remove TAG for each entry in the region.
  10263. This works in the agenda, and also in an org-mode buffer."
  10264. (interactive
  10265. (list (region-beginning) (region-end)
  10266. (let ((org-last-tags-completion-table
  10267. (if (org-mode-p)
  10268. (org-get-buffer-tags)
  10269. (org-global-tags-completion-table))))
  10270. (org-icompleting-read
  10271. "Tag: " 'org-tags-completion-function nil nil nil
  10272. 'org-tags-history))
  10273. (progn
  10274. (message "[s]et or [r]emove? ")
  10275. (equal (read-char-exclusive) ?r))))
  10276. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10277. (let ((agendap (equal major-mode 'org-agenda-mode))
  10278. l1 l2 m buf pos newhead (cnt 0))
  10279. (goto-char end)
  10280. (setq l2 (1- (org-current-line)))
  10281. (goto-char beg)
  10282. (setq l1 (org-current-line))
  10283. (loop for l from l1 to l2 do
  10284. (org-goto-line l)
  10285. (setq m (get-text-property (point) 'org-hd-marker))
  10286. (when (or (and (org-mode-p) (org-on-heading-p))
  10287. (and agendap m))
  10288. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10289. pos (if agendap m (point)))
  10290. (with-current-buffer buf
  10291. (save-excursion
  10292. (save-restriction
  10293. (goto-char pos)
  10294. (setq cnt (1+ cnt))
  10295. (org-toggle-tag tag (if off 'off 'on))
  10296. (setq newhead (org-get-heading)))))
  10297. (and agendap (org-agenda-change-all-lines newhead m))))
  10298. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10299. (defun org-tags-completion-function (string predicate &optional flag)
  10300. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10301. (confirm (lambda (x) (stringp (car x)))))
  10302. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10303. (setq s1 (match-string 1 string)
  10304. s2 (match-string 2 string))
  10305. (setq s1 "" s2 string))
  10306. (cond
  10307. ((eq flag nil)
  10308. ;; try completion
  10309. (setq rtn (try-completion s2 ctable confirm))
  10310. (if (stringp rtn)
  10311. (setq rtn
  10312. (concat s1 s2 (substring rtn (length s2))
  10313. (if (and org-add-colon-after-tag-completion
  10314. (assoc rtn ctable))
  10315. ":" ""))))
  10316. rtn)
  10317. ((eq flag t)
  10318. ;; all-completions
  10319. (all-completions s2 ctable confirm)
  10320. )
  10321. ((eq flag 'lambda)
  10322. ;; exact match?
  10323. (assoc s2 ctable)))
  10324. ))
  10325. (defun org-fast-tag-insert (kwd tags face &optional end)
  10326. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10327. (insert (format "%-12s" (concat kwd ":"))
  10328. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10329. (or end "")))
  10330. (defun org-fast-tag-show-exit (flag)
  10331. (save-excursion
  10332. (org-goto-line 3)
  10333. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10334. (replace-match ""))
  10335. (when flag
  10336. (end-of-line 1)
  10337. (org-move-to-column (- (window-width) 19) t)
  10338. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10339. (defun org-set-current-tags-overlay (current prefix)
  10340. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10341. (if (featurep 'xemacs)
  10342. (org-overlay-display org-tags-overlay (concat prefix s)
  10343. 'secondary-selection)
  10344. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10345. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10346. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10347. "Fast tag selection with single keys.
  10348. CURRENT is the current list of tags in the headline, INHERITED is the
  10349. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10350. possibly with grouping information. TODO-TABLE is a similar table with
  10351. TODO keywords, should these have keys assigned to them.
  10352. If the keys are nil, a-z are automatically assigned.
  10353. Returns the new tags string, or nil to not change the current settings."
  10354. (let* ((fulltable (append table todo-table))
  10355. (maxlen (apply 'max (mapcar
  10356. (lambda (x)
  10357. (if (stringp (car x)) (string-width (car x)) 0))
  10358. fulltable)))
  10359. (buf (current-buffer))
  10360. (expert (eq org-fast-tag-selection-single-key 'expert))
  10361. (buffer-tags nil)
  10362. (fwidth (+ maxlen 3 1 3))
  10363. (ncol (/ (- (window-width) 4) fwidth))
  10364. (i-face 'org-done)
  10365. (c-face 'org-todo)
  10366. tg cnt e c char c1 c2 ntable tbl rtn
  10367. ov-start ov-end ov-prefix
  10368. (exit-after-next org-fast-tag-selection-single-key)
  10369. (done-keywords org-done-keywords)
  10370. groups ingroup)
  10371. (save-excursion
  10372. (beginning-of-line 1)
  10373. (if (looking-at
  10374. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10375. (setq ov-start (match-beginning 1)
  10376. ov-end (match-end 1)
  10377. ov-prefix "")
  10378. (setq ov-start (1- (point-at-eol))
  10379. ov-end (1+ ov-start))
  10380. (skip-chars-forward "^\n\r")
  10381. (setq ov-prefix
  10382. (concat
  10383. (buffer-substring (1- (point)) (point))
  10384. (if (> (current-column) org-tags-column)
  10385. " "
  10386. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10387. (org-move-overlay org-tags-overlay ov-start ov-end)
  10388. (save-window-excursion
  10389. (if expert
  10390. (set-buffer (get-buffer-create " *Org tags*"))
  10391. (delete-other-windows)
  10392. (split-window-vertically)
  10393. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10394. (erase-buffer)
  10395. (org-set-local 'org-done-keywords done-keywords)
  10396. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10397. (org-fast-tag-insert "Current" current c-face "\n\n")
  10398. (org-fast-tag-show-exit exit-after-next)
  10399. (org-set-current-tags-overlay current ov-prefix)
  10400. (setq tbl fulltable char ?a cnt 0)
  10401. (while (setq e (pop tbl))
  10402. (cond
  10403. ((equal e '(:startgroup))
  10404. (push '() groups) (setq ingroup t)
  10405. (when (not (= cnt 0))
  10406. (setq cnt 0)
  10407. (insert "\n"))
  10408. (insert "{ "))
  10409. ((equal e '(:endgroup))
  10410. (setq ingroup nil cnt 0)
  10411. (insert "}\n"))
  10412. ((equal e '(:newline))
  10413. (when (not (= cnt 0))
  10414. (setq cnt 0)
  10415. (insert "\n")
  10416. (setq e (car tbl))
  10417. (while (equal (car tbl) '(:newline))
  10418. (insert "\n")
  10419. (setq tbl (cdr tbl)))))
  10420. (t
  10421. (setq tg (copy-sequence (car e)) c2 nil)
  10422. (if (cdr e)
  10423. (setq c (cdr e))
  10424. ;; automatically assign a character.
  10425. (setq c1 (string-to-char
  10426. (downcase (substring
  10427. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10428. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10429. (while (or (rassoc char ntable) (rassoc char table))
  10430. (setq char (1+ char)))
  10431. (setq c2 c1))
  10432. (setq c (or c2 char)))
  10433. (if ingroup (push tg (car groups)))
  10434. (setq tg (org-add-props tg nil 'face
  10435. (cond
  10436. ((not (assoc tg table))
  10437. (org-get-todo-face tg))
  10438. ((member tg current) c-face)
  10439. ((member tg inherited) i-face)
  10440. (t nil))))
  10441. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10442. (insert "[" c "] " tg (make-string
  10443. (- fwidth 4 (length tg)) ?\ ))
  10444. (push (cons tg c) ntable)
  10445. (when (= (setq cnt (1+ cnt)) ncol)
  10446. (insert "\n")
  10447. (if ingroup (insert " "))
  10448. (setq cnt 0)))))
  10449. (setq ntable (nreverse ntable))
  10450. (insert "\n")
  10451. (goto-char (point-min))
  10452. (if (not expert) (org-fit-window-to-buffer))
  10453. (setq rtn
  10454. (catch 'exit
  10455. (while t
  10456. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10457. (if groups " [!] no groups" " [!]groups")
  10458. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10459. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10460. (cond
  10461. ((= c ?\r) (throw 'exit t))
  10462. ((= c ?!)
  10463. (setq groups (not groups))
  10464. (goto-char (point-min))
  10465. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10466. ((= c ?\C-c)
  10467. (if (not expert)
  10468. (org-fast-tag-show-exit
  10469. (setq exit-after-next (not exit-after-next)))
  10470. (setq expert nil)
  10471. (delete-other-windows)
  10472. (split-window-vertically)
  10473. (org-switch-to-buffer-other-window " *Org tags*")
  10474. (org-fit-window-to-buffer)))
  10475. ((or (= c ?\C-g)
  10476. (and (= c ?q) (not (rassoc c ntable))))
  10477. (org-detach-overlay org-tags-overlay)
  10478. (setq quit-flag t))
  10479. ((= c ?\ )
  10480. (setq current nil)
  10481. (if exit-after-next (setq exit-after-next 'now)))
  10482. ((= c ?\t)
  10483. (condition-case nil
  10484. (setq tg (org-icompleting-read
  10485. "Tag: "
  10486. (or buffer-tags
  10487. (with-current-buffer buf
  10488. (org-get-buffer-tags)))))
  10489. (quit (setq tg "")))
  10490. (when (string-match "\\S-" tg)
  10491. (add-to-list 'buffer-tags (list tg))
  10492. (if (member tg current)
  10493. (setq current (delete tg current))
  10494. (push tg current)))
  10495. (if exit-after-next (setq exit-after-next 'now)))
  10496. ((setq e (rassoc c todo-table) tg (car e))
  10497. (with-current-buffer buf
  10498. (save-excursion (org-todo tg)))
  10499. (if exit-after-next (setq exit-after-next 'now)))
  10500. ((setq e (rassoc c ntable) tg (car e))
  10501. (if (member tg current)
  10502. (setq current (delete tg current))
  10503. (loop for g in groups do
  10504. (if (member tg g)
  10505. (mapc (lambda (x)
  10506. (setq current (delete x current)))
  10507. g)))
  10508. (push tg current))
  10509. (if exit-after-next (setq exit-after-next 'now))))
  10510. ;; Create a sorted list
  10511. (setq current
  10512. (sort current
  10513. (lambda (a b)
  10514. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10515. (if (eq exit-after-next 'now) (throw 'exit t))
  10516. (goto-char (point-min))
  10517. (beginning-of-line 2)
  10518. (delete-region (point) (point-at-eol))
  10519. (org-fast-tag-insert "Current" current c-face)
  10520. (org-set-current-tags-overlay current ov-prefix)
  10521. (while (re-search-forward
  10522. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10523. (setq tg (match-string 1))
  10524. (add-text-properties
  10525. (match-beginning 1) (match-end 1)
  10526. (list 'face
  10527. (cond
  10528. ((member tg current) c-face)
  10529. ((member tg inherited) i-face)
  10530. (t (get-text-property (match-beginning 1) 'face))))))
  10531. (goto-char (point-min)))))
  10532. (org-detach-overlay org-tags-overlay)
  10533. (if rtn
  10534. (mapconcat 'identity current ":")
  10535. nil))))
  10536. (defun org-get-tags-string ()
  10537. "Get the TAGS string in the current headline."
  10538. (unless (org-on-heading-p t)
  10539. (error "Not on a heading"))
  10540. (save-excursion
  10541. (beginning-of-line 1)
  10542. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10543. (org-match-string-no-properties 1)
  10544. "")))
  10545. (defun org-get-tags ()
  10546. "Get the list of tags specified in the current headline."
  10547. (org-split-string (org-get-tags-string) ":"))
  10548. (defun org-get-buffer-tags ()
  10549. "Get a table of all tags used in the buffer, for completion."
  10550. (let (tags)
  10551. (save-excursion
  10552. (goto-char (point-min))
  10553. (while (re-search-forward
  10554. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10555. (when (equal (char-after (point-at-bol 0)) ?*)
  10556. (mapc (lambda (x) (add-to-list 'tags x))
  10557. (org-split-string (org-match-string-no-properties 1) ":")))))
  10558. (mapcar 'list tags)))
  10559. ;;;; The mapping API
  10560. ;;;###autoload
  10561. (defun org-map-entries (func &optional match scope &rest skip)
  10562. "Call FUNC at each headline selected by MATCH in SCOPE.
  10563. FUNC is a function or a lisp form. The function will be called without
  10564. arguments, with the cursor positioned at the beginning of the headline.
  10565. The return values of all calls to the function will be collected and
  10566. returned as a list.
  10567. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10568. does not need to preserve point. After evaluation, the cursor will be
  10569. moved to the end of the line (presumably of the headline of the
  10570. processed entry) and search continues from there. Under some
  10571. circumstances, this may not produce the wanted results. For example,
  10572. if you have removed (e.g. archived) the current (sub)tree it could
  10573. mean that the next entry will be skipped entirely. In such cases, you
  10574. can specify the position from where search should continue by making
  10575. FUNC set the variable `org-map-continue-from' to the desired buffer
  10576. position.
  10577. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10578. Only headlines that are matched by this query will be considered during
  10579. the iteration. When MATCH is nil or t, all headlines will be
  10580. visited by the iteration.
  10581. SCOPE determines the scope of this command. It can be any of:
  10582. nil The current buffer, respecting the restriction if any
  10583. tree The subtree started with the entry at point
  10584. file The current buffer, without restriction
  10585. file-with-archives
  10586. The current buffer, and any archives associated with it
  10587. agenda All agenda files
  10588. agenda-with-archives
  10589. All agenda files with any archive files associated with them
  10590. \(file1 file2 ...)
  10591. If this is a list, all files in the list will be scanned
  10592. The remaining args are treated as settings for the skipping facilities of
  10593. the scanner. The following items can be given here:
  10594. archive skip trees with the archive tag.
  10595. comment skip trees with the COMMENT keyword
  10596. function or Emacs Lisp form:
  10597. will be used as value for `org-agenda-skip-function', so whenever
  10598. the the function returns t, FUNC will not be called for that
  10599. entry and search will continue from the point where the
  10600. function leaves it.
  10601. If your function needs to retrieve the tags including inherited tags
  10602. at the *current* entry, you can use the value of the variable
  10603. `org-scanner-tags' which will be much faster than getting the value
  10604. with `org-get-tags-at'. If your function gets properties with
  10605. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10606. to t around the call to `org-entry-properties' to get the same speedup.
  10607. Note that if your function moves around to retrieve tags and properties at
  10608. a *different* entry, you cannot use these techniques."
  10609. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10610. (org-agenda-skip-archived-trees (memq 'archive skip))
  10611. (org-agenda-skip-comment-trees (memq 'comment skip))
  10612. (org-agenda-skip-function
  10613. (car (org-delete-all '(comment archive) skip)))
  10614. (org-tags-match-list-sublevels t)
  10615. matcher file res
  10616. org-todo-keywords-for-agenda
  10617. org-done-keywords-for-agenda
  10618. org-todo-keyword-alist-for-agenda
  10619. org-tag-alist-for-agenda)
  10620. (cond
  10621. ((eq match t) (setq matcher t))
  10622. ((eq match nil) (setq matcher t))
  10623. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10624. (save-excursion
  10625. (save-restriction
  10626. (when (eq scope 'tree)
  10627. (org-back-to-heading t)
  10628. (org-narrow-to-subtree)
  10629. (setq scope nil))
  10630. (if (not scope)
  10631. (progn
  10632. (org-prepare-agenda-buffers
  10633. (list (buffer-file-name (current-buffer))))
  10634. (setq res (org-scan-tags func matcher)))
  10635. ;; Get the right scope
  10636. (cond
  10637. ((and scope (listp scope) (symbolp (car scope)))
  10638. (setq scope (eval scope)))
  10639. ((eq scope 'agenda)
  10640. (setq scope (org-agenda-files t)))
  10641. ((eq scope 'agenda-with-archives)
  10642. (setq scope (org-agenda-files t))
  10643. (setq scope (org-add-archive-files scope)))
  10644. ((eq scope 'file)
  10645. (setq scope (list (buffer-file-name))))
  10646. ((eq scope 'file-with-archives)
  10647. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10648. (org-prepare-agenda-buffers scope)
  10649. (while (setq file (pop scope))
  10650. (with-current-buffer (org-find-base-buffer-visiting file)
  10651. (save-excursion
  10652. (save-restriction
  10653. (widen)
  10654. (goto-char (point-min))
  10655. (setq res (append res (org-scan-tags func matcher))))))))))
  10656. res))
  10657. ;;;; Properties
  10658. ;;; Setting and retrieving properties
  10659. (defconst org-special-properties
  10660. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10661. "TIMESTAMP" "TIMESTAMP_IA")
  10662. "The special properties valid in Org-mode.
  10663. These are properties that are not defined in the property drawer,
  10664. but in some other way.")
  10665. (defconst org-default-properties
  10666. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10667. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10668. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10669. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10670. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10671. "CLOCK_MODELINE_TOTAL")
  10672. "Some properties that are used by Org-mode for various purposes.
  10673. Being in this list makes sure that they are offered for completion.")
  10674. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10675. "Regular expression matching the first line of a property drawer.")
  10676. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10677. "Regular expression matching the first line of a property drawer.")
  10678. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10679. "Regular expression matching the first line of a property drawer.")
  10680. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10681. "Regular expression matching the first line of a property drawer.")
  10682. (defconst org-property-drawer-re
  10683. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10684. org-property-end-re "\\)\n?")
  10685. "Matches an entire property drawer.")
  10686. (defconst org-clock-drawer-re
  10687. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10688. org-property-end-re "\\)\n?")
  10689. "Matches an entire clock drawer.")
  10690. (defun org-property-action ()
  10691. "Do an action on properties."
  10692. (interactive)
  10693. (let (c)
  10694. (org-at-property-p)
  10695. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10696. (setq c (read-char-exclusive))
  10697. (cond
  10698. ((equal c ?s)
  10699. (call-interactively 'org-set-property))
  10700. ((equal c ?d)
  10701. (call-interactively 'org-delete-property))
  10702. ((equal c ?D)
  10703. (call-interactively 'org-delete-property-globally))
  10704. ((equal c ?c)
  10705. (call-interactively 'org-compute-property-at-point))
  10706. (t (error "No such property action %c" c)))))
  10707. (defun org-set-effort (&optional value)
  10708. "Set the effort property of the current entry.
  10709. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  10710. allowed value."
  10711. (interactive "P")
  10712. (if (equal value 0) (setq value 10))
  10713. (let* ((completion-ignore-case t)
  10714. (prop org-effort-property)
  10715. (cur (org-entry-get nil prop))
  10716. (allowed (org-property-get-allowed-values nil prop 'table))
  10717. (existing (mapcar 'list (org-property-values prop)))
  10718. (val (cond
  10719. ((stringp value) value)
  10720. ((and allowed (integerp value))
  10721. (or (car (nth (1- value) allowed))
  10722. (car (org-last allowed))))
  10723. (allowed
  10724. (org-completing-read "Value: " allowed nil 'req-match))
  10725. (t
  10726. (let (org-completion-use-ido org-completion-use-iswitchb)
  10727. (org-completing-read
  10728. (concat "Value " (if (and cur (string-match "\\S-" cur))
  10729. (concat "[" cur "]") "")
  10730. ": ")
  10731. existing nil nil "" nil cur))))))
  10732. (unless (equal (org-entry-get nil prop) val)
  10733. (org-entry-put nil prop val))
  10734. (message "%s is now %s" prop val)))
  10735. (defun org-at-property-p ()
  10736. "Is the cursor in a property line?"
  10737. ;; FIXME: Does not check if we are actually in the drawer.
  10738. ;; FIXME: also returns true on any drawers.....
  10739. ;; This is used by C-c C-c for property action.
  10740. (save-excursion
  10741. (beginning-of-line 1)
  10742. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10743. (defun org-get-property-block (&optional beg end force)
  10744. "Return the (beg . end) range of the body of the property drawer.
  10745. BEG and END can be beginning and end of subtree, if not given
  10746. they will be found.
  10747. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10748. (catch 'exit
  10749. (save-excursion
  10750. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10751. (end (or end (progn (outline-next-heading) (point)))))
  10752. (goto-char beg)
  10753. (if (re-search-forward org-property-start-re end t)
  10754. (setq beg (1+ (match-end 0)))
  10755. (if force
  10756. (save-excursion
  10757. (org-insert-property-drawer)
  10758. (setq end (progn (outline-next-heading) (point))))
  10759. (throw 'exit nil))
  10760. (goto-char beg)
  10761. (if (re-search-forward org-property-start-re end t)
  10762. (setq beg (1+ (match-end 0)))))
  10763. (if (re-search-forward org-property-end-re end t)
  10764. (setq end (match-beginning 0))
  10765. (or force (throw 'exit nil))
  10766. (goto-char beg)
  10767. (setq end beg)
  10768. (org-indent-line-function)
  10769. (insert ":END:\n"))
  10770. (cons beg end)))))
  10771. (defun org-entry-properties (&optional pom which)
  10772. "Get all properties of the entry at point-or-marker POM.
  10773. This includes the TODO keyword, the tags, time strings for deadline,
  10774. scheduled, and clocking, and any additional properties defined in the
  10775. entry. The return value is an alist, keys may occur multiple times
  10776. if the property key was used several times.
  10777. POM may also be nil, in which case the current entry is used.
  10778. If WHICH is nil or `all', get all properties. If WHICH is
  10779. `special' or `standard', only get that subclass."
  10780. (setq which (or which 'all))
  10781. (org-with-point-at pom
  10782. (let ((clockstr (substring org-clock-string 0 -1))
  10783. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10784. beg end range props sum-props key value string clocksum)
  10785. (save-excursion
  10786. (when (condition-case nil
  10787. (and (org-mode-p) (org-back-to-heading t))
  10788. (error nil))
  10789. (setq beg (point))
  10790. (setq sum-props (get-text-property (point) 'org-summaries))
  10791. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10792. (outline-next-heading)
  10793. (setq end (point))
  10794. (when (memq which '(all special))
  10795. ;; Get the special properties, like TODO and tags
  10796. (goto-char beg)
  10797. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10798. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10799. (when (looking-at org-priority-regexp)
  10800. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10801. (when (and (setq value (org-get-tags-string))
  10802. (string-match "\\S-" value))
  10803. (push (cons "TAGS" value) props))
  10804. (when (setq value (org-get-tags-at))
  10805. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10806. props))
  10807. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10808. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10809. string (if (equal key clockstr)
  10810. (org-no-properties
  10811. (org-trim
  10812. (buffer-substring
  10813. (match-beginning 3) (goto-char (point-at-eol)))))
  10814. (substring (org-match-string-no-properties 3) 1 -1)))
  10815. (unless key
  10816. (if (= (char-after (match-beginning 3)) ?\[)
  10817. (setq key "TIMESTAMP_IA")
  10818. (setq key "TIMESTAMP")))
  10819. (when (or (equal key clockstr) (not (assoc key props)))
  10820. (push (cons key string) props)))
  10821. )
  10822. (when (memq which '(all standard))
  10823. ;; Get the standard properties, like :PROP: ...
  10824. (setq range (org-get-property-block beg end))
  10825. (when range
  10826. (goto-char (car range))
  10827. (while (re-search-forward
  10828. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10829. (cdr range) t)
  10830. (setq key (org-match-string-no-properties 1)
  10831. value (org-trim (or (org-match-string-no-properties 2) "")))
  10832. (unless (member key excluded)
  10833. (push (cons key (or value "")) props)))))
  10834. (if clocksum
  10835. (push (cons "CLOCKSUM"
  10836. (org-columns-number-to-string (/ (float clocksum) 60.)
  10837. 'add_times))
  10838. props))
  10839. (unless (assoc "CATEGORY" props)
  10840. (setq value (or (org-get-category)
  10841. (progn (org-refresh-category-properties)
  10842. (org-get-category))))
  10843. (push (cons "CATEGORY" value) props))
  10844. (append sum-props (nreverse props)))))))
  10845. (defun org-entry-get (pom property &optional inherit)
  10846. "Get value of PROPERTY for entry at point-or-marker POM.
  10847. If INHERIT is non-nil and the entry does not have the property,
  10848. then also check higher levels of the hierarchy.
  10849. If INHERIT is the symbol `selective', use inheritance only if the setting
  10850. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10851. If the property is present but empty, the return value is the empty string.
  10852. If the property is not present at all, nil is returned."
  10853. (org-with-point-at pom
  10854. (if (and inherit (if (eq inherit 'selective)
  10855. (org-property-inherit-p property)
  10856. t))
  10857. (org-entry-get-with-inheritance property)
  10858. (if (member property org-special-properties)
  10859. ;; We need a special property. Use brute force, get all properties.
  10860. (cdr (assoc property (org-entry-properties nil 'special)))
  10861. (let ((range (org-get-property-block)))
  10862. (if (and range
  10863. (goto-char (car range))
  10864. (re-search-forward
  10865. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10866. (cdr range) t))
  10867. ;; Found the property, return it.
  10868. (if (match-end 1)
  10869. (org-match-string-no-properties 1)
  10870. "")))))))
  10871. (defun org-property-or-variable-value (var &optional inherit)
  10872. "Check if there is a property fixing the value of VAR.
  10873. If yes, return this value. If not, return the current value of the variable."
  10874. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10875. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10876. (read prop)
  10877. (symbol-value var))))
  10878. (defun org-entry-delete (pom property)
  10879. "Delete the property PROPERTY from entry at point-or-marker POM."
  10880. (org-with-point-at pom
  10881. (if (member property org-special-properties)
  10882. nil ; cannot delete these properties.
  10883. (let ((range (org-get-property-block)))
  10884. (if (and range
  10885. (goto-char (car range))
  10886. (re-search-forward
  10887. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10888. (cdr range) t))
  10889. (progn
  10890. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10891. t)
  10892. nil)))))
  10893. ;; Multi-values properties are properties that contain multiple values
  10894. ;; These values are assumed to be single words, separated by whitespace.
  10895. (defun org-entry-add-to-multivalued-property (pom property value)
  10896. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10897. (let* ((old (org-entry-get pom property))
  10898. (values (and old (org-split-string old "[ \t]"))))
  10899. (setq value (org-entry-protect-space value))
  10900. (unless (member value values)
  10901. (setq values (cons value values))
  10902. (org-entry-put pom property
  10903. (mapconcat 'identity values " ")))))
  10904. (defun org-entry-remove-from-multivalued-property (pom property value)
  10905. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10906. (let* ((old (org-entry-get pom property))
  10907. (values (and old (org-split-string old "[ \t]"))))
  10908. (setq value (org-entry-protect-space value))
  10909. (when (member value values)
  10910. (setq values (delete value values))
  10911. (org-entry-put pom property
  10912. (mapconcat 'identity values " ")))))
  10913. (defun org-entry-member-in-multivalued-property (pom property value)
  10914. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10915. (let* ((old (org-entry-get pom property))
  10916. (values (and old (org-split-string old "[ \t]"))))
  10917. (setq value (org-entry-protect-space value))
  10918. (member value values)))
  10919. (defun org-entry-get-multivalued-property (pom property)
  10920. "Return a list of values in a multivalued property."
  10921. (let* ((value (org-entry-get pom property))
  10922. (values (and value (org-split-string value "[ \t]"))))
  10923. (mapcar 'org-entry-restore-space values)))
  10924. (defun org-entry-put-multivalued-property (pom property &rest values)
  10925. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10926. VALUES should be a list of strings. Spaces will be protected."
  10927. (org-entry-put pom property
  10928. (mapconcat 'org-entry-protect-space values " "))
  10929. (let* ((value (org-entry-get pom property))
  10930. (values (and value (org-split-string value "[ \t]"))))
  10931. (mapcar 'org-entry-restore-space values)))
  10932. (defun org-entry-protect-space (s)
  10933. "Protect spaces and newline in string S."
  10934. (while (string-match " " s)
  10935. (setq s (replace-match "%20" t t s)))
  10936. (while (string-match "\n" s)
  10937. (setq s (replace-match "%0A" t t s)))
  10938. s)
  10939. (defun org-entry-restore-space (s)
  10940. "Restore spaces and newline in string S."
  10941. (while (string-match "%20" s)
  10942. (setq s (replace-match " " t t s)))
  10943. (while (string-match "%0A" s)
  10944. (setq s (replace-match "\n" t t s)))
  10945. s)
  10946. (defvar org-entry-property-inherited-from (make-marker)
  10947. "Marker pointing to the entry from where a property was inherited.
  10948. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10949. location of the entry where the inheritance search matched. If there was
  10950. no match, the marker will point nowhere.
  10951. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10952. is set.")
  10953. (defun org-entry-get-with-inheritance (property)
  10954. "Get entry property, and search higher levels if not present."
  10955. (move-marker org-entry-property-inherited-from nil)
  10956. (let (tmp)
  10957. (save-excursion
  10958. (save-restriction
  10959. (widen)
  10960. (catch 'ex
  10961. (while t
  10962. (when (setq tmp (org-entry-get nil property))
  10963. (org-back-to-heading t)
  10964. (move-marker org-entry-property-inherited-from (point))
  10965. (throw 'ex tmp))
  10966. (or (org-up-heading-safe) (throw 'ex nil)))))
  10967. (or tmp
  10968. (cdr (assoc property org-file-properties))
  10969. (cdr (assoc property org-global-properties))
  10970. (cdr (assoc property org-global-properties-fixed))))))
  10971. (defun org-entry-put (pom property value)
  10972. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10973. (org-with-point-at pom
  10974. (org-back-to-heading t)
  10975. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10976. range)
  10977. (cond
  10978. ((equal property "TODO")
  10979. (when (and (stringp value) (string-match "\\S-" value)
  10980. (not (member value org-todo-keywords-1)))
  10981. (error "\"%s\" is not a valid TODO state" value))
  10982. (if (or (not value)
  10983. (not (string-match "\\S-" value)))
  10984. (setq value 'none))
  10985. (org-todo value)
  10986. (org-set-tags nil 'align))
  10987. ((equal property "PRIORITY")
  10988. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10989. (string-to-char value) ?\ ))
  10990. (org-set-tags nil 'align))
  10991. ((equal property "SCHEDULED")
  10992. (if (re-search-forward org-scheduled-time-regexp end t)
  10993. (cond
  10994. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10995. ((eq value 'later) (org-timestamp-change 1 'day))
  10996. (t (call-interactively 'org-schedule)))
  10997. (call-interactively 'org-schedule)))
  10998. ((equal property "DEADLINE")
  10999. (if (re-search-forward org-deadline-time-regexp end t)
  11000. (cond
  11001. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11002. ((eq value 'later) (org-timestamp-change 1 'day))
  11003. (t (call-interactively 'org-deadline)))
  11004. (call-interactively 'org-deadline)))
  11005. ((member property org-special-properties)
  11006. (error "The %s property can not yet be set with `org-entry-put'"
  11007. property))
  11008. (t ; a non-special property
  11009. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11010. (setq range (org-get-property-block beg end 'force))
  11011. (goto-char (car range))
  11012. (if (re-search-forward
  11013. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11014. (progn
  11015. (delete-region (match-beginning 1) (match-end 1))
  11016. (goto-char (match-beginning 1)))
  11017. (goto-char (cdr range))
  11018. (insert "\n")
  11019. (backward-char 1)
  11020. (org-indent-line-function)
  11021. (insert ":" property ":"))
  11022. (and value (insert " " value))
  11023. (org-indent-line-function)))))))
  11024. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11025. "Get all property keys in the current buffer.
  11026. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11027. like tags and TODO state.
  11028. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11029. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11030. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11031. formats in the current buffer."
  11032. (let (rtn range cfmt s p)
  11033. (save-excursion
  11034. (save-restriction
  11035. (widen)
  11036. (goto-char (point-min))
  11037. (while (re-search-forward org-property-start-re nil t)
  11038. (setq range (org-get-property-block))
  11039. (goto-char (car range))
  11040. (while (re-search-forward
  11041. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11042. (cdr range) t)
  11043. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11044. (outline-next-heading))))
  11045. (when include-specials
  11046. (setq rtn (append org-special-properties rtn)))
  11047. (when include-defaults
  11048. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11049. (add-to-list 'rtn org-effort-property))
  11050. (when include-columns
  11051. (save-excursion
  11052. (save-restriction
  11053. (widen)
  11054. (goto-char (point-min))
  11055. (while (re-search-forward
  11056. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11057. nil t)
  11058. (setq cfmt (match-string 2) s 0)
  11059. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11060. cfmt s)
  11061. (setq s (match-end 0)
  11062. p (match-string 1 cfmt))
  11063. (unless (or (equal p "ITEM")
  11064. (member p org-special-properties))
  11065. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11066. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11067. (defun org-property-values (key)
  11068. "Return a list of all values of property KEY."
  11069. (save-excursion
  11070. (save-restriction
  11071. (widen)
  11072. (goto-char (point-min))
  11073. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11074. values)
  11075. (while (re-search-forward re nil t)
  11076. (add-to-list 'values (org-trim (match-string 1))))
  11077. (delete "" values)))))
  11078. (defun org-insert-property-drawer ()
  11079. "Insert a property drawer into the current entry."
  11080. (interactive)
  11081. (org-back-to-heading t)
  11082. (looking-at outline-regexp)
  11083. (let ((indent (if org-adapt-indentation
  11084. (- (match-end 0)(match-beginning 0))
  11085. 0))
  11086. (beg (point))
  11087. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11088. end hiddenp)
  11089. (outline-next-heading)
  11090. (setq end (point))
  11091. (goto-char beg)
  11092. (while (re-search-forward re end t))
  11093. (setq hiddenp (org-invisible-p))
  11094. (end-of-line 1)
  11095. (and (equal (char-after) ?\n) (forward-char 1))
  11096. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11097. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11098. ;; just skip this line
  11099. (beginning-of-line 2)
  11100. ;; Drawer start, find the end
  11101. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11102. (beginning-of-line 1)))
  11103. (org-skip-over-state-notes)
  11104. (skip-chars-backward " \t\n\r")
  11105. (if (eq (char-before) ?*) (forward-char 1))
  11106. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11107. (beginning-of-line 0)
  11108. (org-indent-to-column indent)
  11109. (beginning-of-line 2)
  11110. (org-indent-to-column indent)
  11111. (beginning-of-line 0)
  11112. (if hiddenp
  11113. (save-excursion
  11114. (org-back-to-heading t)
  11115. (hide-entry))
  11116. (org-flag-drawer t))))
  11117. (defun org-set-property (property value)
  11118. "In the current entry, set PROPERTY to VALUE.
  11119. When called interactively, this will prompt for a property name, offering
  11120. completion on existing and default properties. And then it will prompt
  11121. for a value, offering completion either on allowed values (via an inherited
  11122. xxx_ALL property) or on existing values in other instances of this property
  11123. in the current file."
  11124. (interactive
  11125. (let* ((completion-ignore-case t)
  11126. (keys (org-buffer-property-keys nil t t))
  11127. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11128. (prop (if (member prop0 keys)
  11129. prop0
  11130. (or (cdr (assoc (downcase prop0)
  11131. (mapcar (lambda (x) (cons (downcase x) x))
  11132. keys)))
  11133. prop0)))
  11134. (cur (org-entry-get nil prop))
  11135. (allowed (org-property-get-allowed-values nil prop 'table))
  11136. (existing (mapcar 'list (org-property-values prop)))
  11137. (val (if allowed
  11138. (org-completing-read "Value: " allowed nil 'req-match)
  11139. (let (org-completion-use-ido org-completion-use-iswitchb)
  11140. (org-completing-read
  11141. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11142. (concat "[" cur "]") "")
  11143. ": ")
  11144. existing nil nil "" nil cur)))))
  11145. (list prop (if (equal val "") cur val))))
  11146. (unless (equal (org-entry-get nil property) value)
  11147. (org-entry-put nil property value)))
  11148. (defun org-delete-property (property)
  11149. "In the current entry, delete PROPERTY."
  11150. (interactive
  11151. (let* ((completion-ignore-case t)
  11152. (prop (org-icompleting-read
  11153. "Property: " (org-entry-properties nil 'standard))))
  11154. (list prop)))
  11155. (message "Property %s %s" property
  11156. (if (org-entry-delete nil property)
  11157. "deleted"
  11158. "was not present in the entry")))
  11159. (defun org-delete-property-globally (property)
  11160. "Remove PROPERTY globally, from all entries."
  11161. (interactive
  11162. (let* ((completion-ignore-case t)
  11163. (prop (org-icompleting-read
  11164. "Globally remove property: "
  11165. (mapcar 'list (org-buffer-property-keys)))))
  11166. (list prop)))
  11167. (save-excursion
  11168. (save-restriction
  11169. (widen)
  11170. (goto-char (point-min))
  11171. (let ((cnt 0))
  11172. (while (re-search-forward
  11173. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11174. nil t)
  11175. (setq cnt (1+ cnt))
  11176. (replace-match ""))
  11177. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11178. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11179. (defun org-compute-property-at-point ()
  11180. "Compute the property at point.
  11181. This looks for an enclosing column format, extracts the operator and
  11182. then applies it to the property in the column format's scope."
  11183. (interactive)
  11184. (unless (org-at-property-p)
  11185. (error "Not at a property"))
  11186. (let ((prop (org-match-string-no-properties 2)))
  11187. (org-columns-get-format-and-top-level)
  11188. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11189. (error "No operator defined for property %s" prop))
  11190. (org-columns-compute prop)))
  11191. (defun org-property-get-allowed-values (pom property &optional table)
  11192. "Get allowed values for the property PROPERTY.
  11193. When TABLE is non-nil, return an alist that can directly be used for
  11194. completion."
  11195. (let (vals)
  11196. (cond
  11197. ((equal property "TODO")
  11198. (setq vals (org-with-point-at pom
  11199. (append org-todo-keywords-1 '("")))))
  11200. ((equal property "PRIORITY")
  11201. (let ((n org-lowest-priority))
  11202. (while (>= n org-highest-priority)
  11203. (push (char-to-string n) vals)
  11204. (setq n (1- n)))))
  11205. ((member property org-special-properties))
  11206. (t
  11207. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11208. (when (and vals (string-match "\\S-" vals))
  11209. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11210. (setq vals (mapcar (lambda (x)
  11211. (cond ((stringp x) x)
  11212. ((numberp x) (number-to-string x))
  11213. ((symbolp x) (symbol-name x))
  11214. (t "???")))
  11215. vals)))))
  11216. (if table (mapcar 'list vals) vals)))
  11217. (defun org-property-previous-allowed-value (&optional previous)
  11218. "Switch to the next allowed value for this property."
  11219. (interactive)
  11220. (org-property-next-allowed-value t))
  11221. (defun org-property-next-allowed-value (&optional previous)
  11222. "Switch to the next allowed value for this property."
  11223. (interactive)
  11224. (unless (org-at-property-p)
  11225. (error "Not at a property"))
  11226. (let* ((key (match-string 2))
  11227. (value (match-string 3))
  11228. (allowed (or (org-property-get-allowed-values (point) key)
  11229. (and (member value '("[ ]" "[-]" "[X]"))
  11230. '("[ ]" "[X]"))))
  11231. nval)
  11232. (unless allowed
  11233. (error "Allowed values for this property have not been defined"))
  11234. (if previous (setq allowed (reverse allowed)))
  11235. (if (member value allowed)
  11236. (setq nval (car (cdr (member value allowed)))))
  11237. (setq nval (or nval (car allowed)))
  11238. (if (equal nval value)
  11239. (error "Only one allowed value for this property"))
  11240. (org-at-property-p)
  11241. (replace-match (concat " :" key ": " nval) t t)
  11242. (org-indent-line-function)
  11243. (beginning-of-line 1)
  11244. (skip-chars-forward " \t")))
  11245. (defun org-find-entry-with-id (ident)
  11246. "Locate the entry that contains the ID property with exact value IDENT.
  11247. IDENT can be a string, a symbol or a number, this function will search for
  11248. the string representation of it.
  11249. Return the position where this entry starts, or nil if there is no such entry."
  11250. (interactive "sID: ")
  11251. (let ((id (cond
  11252. ((stringp ident) ident)
  11253. ((symbol-name ident) (symbol-name ident))
  11254. ((numberp ident) (number-to-string ident))
  11255. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11256. (case-fold-search nil))
  11257. (save-excursion
  11258. (save-restriction
  11259. (widen)
  11260. (goto-char (point-min))
  11261. (when (re-search-forward
  11262. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11263. nil t)
  11264. (org-back-to-heading t)
  11265. (point))))))
  11266. ;;;; Timestamps
  11267. (defvar org-last-changed-timestamp nil)
  11268. (defvar org-last-inserted-timestamp nil
  11269. "The last time stamp inserted with `org-insert-time-stamp'.")
  11270. (defvar org-time-was-given) ; dynamically scoped parameter
  11271. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11272. (defvar org-ts-what) ; dynamically scoped parameter
  11273. (defun org-time-stamp (arg &optional inactive)
  11274. "Prompt for a date/time and insert a time stamp.
  11275. If the user specifies a time like HH:MM, or if this command is called
  11276. with a prefix argument, the time stamp will contain date and time.
  11277. Otherwise, only the date will be included. All parts of a date not
  11278. specified by the user will be filled in from the current date/time.
  11279. So if you press just return without typing anything, the time stamp
  11280. will represent the current date/time. If there is already a timestamp
  11281. at the cursor, it will be modified."
  11282. (interactive "P")
  11283. (let* ((ts nil)
  11284. (default-time
  11285. ;; Default time is either today, or, when entering a range,
  11286. ;; the range start.
  11287. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11288. (save-excursion
  11289. (re-search-backward
  11290. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11291. (- (point) 20) t)))
  11292. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11293. (current-time)))
  11294. (default-input (and ts (org-get-compact-tod ts)))
  11295. org-time-was-given org-end-time-was-given time)
  11296. (cond
  11297. ((and (org-at-timestamp-p t)
  11298. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11299. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11300. (insert "--")
  11301. (setq time (let ((this-command this-command))
  11302. (org-read-date arg 'totime nil nil
  11303. default-time default-input)))
  11304. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11305. ((org-at-timestamp-p t)
  11306. (setq time (let ((this-command this-command))
  11307. (org-read-date arg 'totime nil nil default-time default-input)))
  11308. (when (org-at-timestamp-p t) ; just to get the match data
  11309. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11310. (replace-match "")
  11311. (setq org-last-changed-timestamp
  11312. (org-insert-time-stamp
  11313. time (or org-time-was-given arg)
  11314. inactive nil nil (list org-end-time-was-given))))
  11315. (message "Timestamp updated"))
  11316. (t
  11317. (setq time (let ((this-command this-command))
  11318. (org-read-date arg 'totime nil nil default-time default-input)))
  11319. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11320. nil nil (list org-end-time-was-given))))))
  11321. ;; FIXME: can we use this for something else, like computing time differences?
  11322. (defun org-get-compact-tod (s)
  11323. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11324. (let* ((t1 (match-string 1 s))
  11325. (h1 (string-to-number (match-string 2 s)))
  11326. (m1 (string-to-number (match-string 3 s)))
  11327. (t2 (and (match-end 4) (match-string 5 s)))
  11328. (h2 (and t2 (string-to-number (match-string 6 s))))
  11329. (m2 (and t2 (string-to-number (match-string 7 s))))
  11330. dh dm)
  11331. (if (not t2)
  11332. t1
  11333. (setq dh (- h2 h1) dm (- m2 m1))
  11334. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11335. (concat t1 "+" (number-to-string dh)
  11336. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11337. (defun org-time-stamp-inactive (&optional arg)
  11338. "Insert an inactive time stamp.
  11339. An inactive time stamp is enclosed in square brackets instead of angle
  11340. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11341. does not link to the calendar and cannot be changed with the S-cursor keys.
  11342. So these are more for recording a certain time/date."
  11343. (interactive "P")
  11344. (org-time-stamp arg 'inactive))
  11345. (defvar org-date-ovl (org-make-overlay 1 1))
  11346. (org-overlay-put org-date-ovl 'face 'org-warning)
  11347. (org-detach-overlay org-date-ovl)
  11348. (defvar org-ans1) ; dynamically scoped parameter
  11349. (defvar org-ans2) ; dynamically scoped parameter
  11350. (defvar org-plain-time-of-day-regexp) ; defined below
  11351. (defvar org-overriding-default-time nil) ; dynamically scoped
  11352. (defvar org-read-date-overlay nil)
  11353. (defvar org-dcst nil) ; dynamically scoped
  11354. (defvar org-read-date-history nil)
  11355. (defvar org-read-date-final-answer nil)
  11356. (defun org-read-date (&optional with-time to-time from-string prompt
  11357. default-time default-input)
  11358. "Read a date, possibly a time, and make things smooth for the user.
  11359. The prompt will suggest to enter an ISO date, but you can also enter anything
  11360. which will at least partially be understood by `parse-time-string'.
  11361. Unrecognized parts of the date will default to the current day, month, year,
  11362. hour and minute. If this command is called to replace a timestamp at point,
  11363. of to enter the second timestamp of a range, the default time is taken from the
  11364. existing stamp. For example,
  11365. 3-2-5 --> 2003-02-05
  11366. feb 15 --> currentyear-02-15
  11367. sep 12 9 --> 2009-09-12
  11368. 12:45 --> today 12:45
  11369. 22 sept 0:34 --> currentyear-09-22 0:34
  11370. 12 --> currentyear-currentmonth-12
  11371. Fri --> nearest Friday (today or later)
  11372. etc.
  11373. Furthermore you can specify a relative date by giving, as the *first* thing
  11374. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11375. change in days weeks, months, years.
  11376. With a single plus or minus, the date is relative to today. With a double
  11377. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11378. +4d --> four days from today
  11379. +4 --> same as above
  11380. +2w --> two weeks from today
  11381. ++5 --> five days from default date
  11382. The function understands only English month and weekday abbreviations,
  11383. but this can be configured with the variables `parse-time-months' and
  11384. `parse-time-weekdays'.
  11385. While prompting, a calendar is popped up - you can also select the
  11386. date with the mouse (button 1). The calendar shows a period of three
  11387. months. To scroll it to other months, use the keys `>' and `<'.
  11388. If you don't like the calendar, turn it off with
  11389. \(setq org-read-date-popup-calendar nil)
  11390. With optional argument TO-TIME, the date will immediately be converted
  11391. to an internal time.
  11392. With an optional argument WITH-TIME, the prompt will suggest to also
  11393. insert a time. Note that when WITH-TIME is not set, you can still
  11394. enter a time, and this function will inform the calling routine about
  11395. this change. The calling routine may then choose to change the format
  11396. used to insert the time stamp into the buffer to include the time.
  11397. With optional argument FROM-STRING, read from this string instead from
  11398. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11399. the time/date that is used for everything that is not specified by the
  11400. user."
  11401. (require 'parse-time)
  11402. (let* ((org-time-stamp-rounding-minutes
  11403. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11404. (org-dcst org-display-custom-times)
  11405. (ct (org-current-time))
  11406. (def (or org-overriding-default-time default-time ct))
  11407. (defdecode (decode-time def))
  11408. (dummy (progn
  11409. (when (< (nth 2 defdecode) org-extend-today-until)
  11410. (setcar (nthcdr 2 defdecode) -1)
  11411. (setcar (nthcdr 1 defdecode) 59)
  11412. (setq def (apply 'encode-time defdecode)
  11413. defdecode (decode-time def)))))
  11414. (calendar-frame-setup nil)
  11415. (calendar-move-hook nil)
  11416. (calendar-view-diary-initially-flag nil)
  11417. (view-diary-entries-initially nil)
  11418. (calendar-view-holidays-initially-flag nil)
  11419. (view-calendar-holidays-initially nil)
  11420. (timestr (format-time-string
  11421. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11422. (prompt (concat (if prompt (concat prompt " ") "")
  11423. (format "Date+time [%s]: " timestr)))
  11424. ans (org-ans0 "") org-ans1 org-ans2 final)
  11425. (cond
  11426. (from-string (setq ans from-string))
  11427. (org-read-date-popup-calendar
  11428. (save-excursion
  11429. (save-window-excursion
  11430. (calendar)
  11431. (calendar-forward-day (- (time-to-days def)
  11432. (calendar-absolute-from-gregorian
  11433. (calendar-current-date))))
  11434. (org-eval-in-calendar nil t)
  11435. (let* ((old-map (current-local-map))
  11436. (map (copy-keymap calendar-mode-map))
  11437. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11438. (org-defkey map (kbd "RET") 'org-calendar-select)
  11439. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11440. 'org-calendar-select-mouse)
  11441. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11442. 'org-calendar-select-mouse)
  11443. (org-defkey minibuffer-local-map [(meta shift left)]
  11444. (lambda () (interactive)
  11445. (org-eval-in-calendar '(calendar-backward-month 1))))
  11446. (org-defkey minibuffer-local-map [(meta shift right)]
  11447. (lambda () (interactive)
  11448. (org-eval-in-calendar '(calendar-forward-month 1))))
  11449. (org-defkey minibuffer-local-map [(meta shift up)]
  11450. (lambda () (interactive)
  11451. (org-eval-in-calendar '(calendar-backward-year 1))))
  11452. (org-defkey minibuffer-local-map [(meta shift down)]
  11453. (lambda () (interactive)
  11454. (org-eval-in-calendar '(calendar-forward-year 1))))
  11455. (org-defkey minibuffer-local-map [?\e (shift left)]
  11456. (lambda () (interactive)
  11457. (org-eval-in-calendar '(calendar-backward-month 1))))
  11458. (org-defkey minibuffer-local-map [?\e (shift right)]
  11459. (lambda () (interactive)
  11460. (org-eval-in-calendar '(calendar-forward-month 1))))
  11461. (org-defkey minibuffer-local-map [?\e (shift up)]
  11462. (lambda () (interactive)
  11463. (org-eval-in-calendar '(calendar-backward-year 1))))
  11464. (org-defkey minibuffer-local-map [?\e (shift down)]
  11465. (lambda () (interactive)
  11466. (org-eval-in-calendar '(calendar-forward-year 1))))
  11467. (org-defkey minibuffer-local-map [(shift up)]
  11468. (lambda () (interactive)
  11469. (org-eval-in-calendar '(calendar-backward-week 1))))
  11470. (org-defkey minibuffer-local-map [(shift down)]
  11471. (lambda () (interactive)
  11472. (org-eval-in-calendar '(calendar-forward-week 1))))
  11473. (org-defkey minibuffer-local-map [(shift left)]
  11474. (lambda () (interactive)
  11475. (org-eval-in-calendar '(calendar-backward-day 1))))
  11476. (org-defkey minibuffer-local-map [(shift right)]
  11477. (lambda () (interactive)
  11478. (org-eval-in-calendar '(calendar-forward-day 1))))
  11479. (org-defkey minibuffer-local-map ">"
  11480. (lambda () (interactive)
  11481. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11482. (org-defkey minibuffer-local-map "<"
  11483. (lambda () (interactive)
  11484. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11485. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11486. (unwind-protect
  11487. (progn
  11488. (use-local-map map)
  11489. (add-hook 'post-command-hook 'org-read-date-display)
  11490. (setq org-ans0 (read-string prompt default-input
  11491. 'org-read-date-history nil))
  11492. ;; org-ans0: from prompt
  11493. ;; org-ans1: from mouse click
  11494. ;; org-ans2: from calendar motion
  11495. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11496. (remove-hook 'post-command-hook 'org-read-date-display)
  11497. (use-local-map old-map)
  11498. (when org-read-date-overlay
  11499. (org-delete-overlay org-read-date-overlay)
  11500. (setq org-read-date-overlay nil)))))))
  11501. (t ; Naked prompt only
  11502. (unwind-protect
  11503. (setq ans (read-string prompt default-input
  11504. 'org-read-date-history timestr))
  11505. (when org-read-date-overlay
  11506. (org-delete-overlay org-read-date-overlay)
  11507. (setq org-read-date-overlay nil)))))
  11508. (setq final (org-read-date-analyze ans def defdecode))
  11509. (setq org-read-date-final-answer ans)
  11510. (if to-time
  11511. (apply 'encode-time final)
  11512. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11513. (format "%04d-%02d-%02d %02d:%02d"
  11514. (nth 5 final) (nth 4 final) (nth 3 final)
  11515. (nth 2 final) (nth 1 final))
  11516. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11517. (defvar def)
  11518. (defvar defdecode)
  11519. (defvar with-time)
  11520. (defun org-read-date-display ()
  11521. "Display the current date prompt interpretation in the minibuffer."
  11522. (when org-read-date-display-live
  11523. (when org-read-date-overlay
  11524. (org-delete-overlay org-read-date-overlay))
  11525. (let ((p (point)))
  11526. (end-of-line 1)
  11527. (while (not (equal (buffer-substring
  11528. (max (point-min) (- (point) 4)) (point))
  11529. " "))
  11530. (insert " "))
  11531. (goto-char p))
  11532. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11533. " " (or org-ans1 org-ans2)))
  11534. (org-end-time-was-given nil)
  11535. (f (org-read-date-analyze ans def defdecode))
  11536. (fmts (if org-dcst
  11537. org-time-stamp-custom-formats
  11538. org-time-stamp-formats))
  11539. (fmt (if (or with-time
  11540. (and (boundp 'org-time-was-given) org-time-was-given))
  11541. (cdr fmts)
  11542. (car fmts)))
  11543. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11544. (when (and org-end-time-was-given
  11545. (string-match org-plain-time-of-day-regexp txt))
  11546. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11547. org-end-time-was-given
  11548. (substring txt (match-end 0)))))
  11549. (setq org-read-date-overlay
  11550. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11551. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11552. (defun org-read-date-analyze (ans def defdecode)
  11553. "Analyse the combined answer of the date prompt."
  11554. ;; FIXME: cleanup and comment
  11555. (let (delta deltan deltaw deltadef year month day
  11556. hour minute second wday pm h2 m2 tl wday1
  11557. iso-year iso-weekday iso-week iso-year iso-date)
  11558. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11559. (setq ans "+0"))
  11560. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11561. (setq ans (replace-match "" t t ans)
  11562. deltan (car delta)
  11563. deltaw (nth 1 delta)
  11564. deltadef (nth 2 delta)))
  11565. ;; Check if there is an iso week date in there
  11566. ;; If yes, sore the info and postpone interpreting it until the rest
  11567. ;; of the parsing is done
  11568. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11569. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11570. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11571. iso-week (string-to-number (match-string 2 ans)))
  11572. (setq ans (replace-match "" t t ans)))
  11573. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11574. (when (string-match
  11575. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11576. (setq year (if (match-end 2)
  11577. (string-to-number (match-string 2 ans))
  11578. (string-to-number (format-time-string "%Y")))
  11579. month (string-to-number (match-string 3 ans))
  11580. day (string-to-number (match-string 4 ans)))
  11581. (if (< year 100) (setq year (+ 2000 year)))
  11582. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11583. t nil ans)))
  11584. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11585. ;; If there is a time with am/pm, and *no* time without it, we convert
  11586. ;; so that matching will be successful.
  11587. (loop for i from 1 to 2 do ; twice, for end time as well
  11588. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11589. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11590. (setq hour (string-to-number (match-string 1 ans))
  11591. minute (if (match-end 3)
  11592. (string-to-number (match-string 3 ans))
  11593. 0)
  11594. pm (equal ?p
  11595. (string-to-char (downcase (match-string 4 ans)))))
  11596. (if (and (= hour 12) (not pm))
  11597. (setq hour 0)
  11598. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11599. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11600. t t ans))))
  11601. ;; Check if a time range is given as a duration
  11602. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11603. (setq hour (string-to-number (match-string 1 ans))
  11604. h2 (+ hour (string-to-number (match-string 3 ans)))
  11605. minute (string-to-number (match-string 2 ans))
  11606. m2 (+ minute (if (match-end 5) (string-to-number
  11607. (match-string 5 ans))0)))
  11608. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11609. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11610. t t ans)))
  11611. ;; Check if there is a time range
  11612. (when (boundp 'org-end-time-was-given)
  11613. (setq org-time-was-given nil)
  11614. (when (and (string-match org-plain-time-of-day-regexp ans)
  11615. (match-end 8))
  11616. (setq org-end-time-was-given (match-string 8 ans))
  11617. (setq ans (concat (substring ans 0 (match-beginning 7))
  11618. (substring ans (match-end 7))))))
  11619. (setq tl (parse-time-string ans)
  11620. day (or (nth 3 tl) (nth 3 defdecode))
  11621. month (or (nth 4 tl)
  11622. (if (and org-read-date-prefer-future
  11623. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11624. (1+ (nth 4 defdecode))
  11625. (nth 4 defdecode)))
  11626. year (or (nth 5 tl)
  11627. (if (and org-read-date-prefer-future
  11628. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11629. (1+ (nth 5 defdecode))
  11630. (nth 5 defdecode)))
  11631. hour (or (nth 2 tl) (nth 2 defdecode))
  11632. minute (or (nth 1 tl) (nth 1 defdecode))
  11633. second (or (nth 0 tl) 0)
  11634. wday (nth 6 tl))
  11635. ;; Special date definitions below
  11636. (cond
  11637. (iso-week
  11638. ;; There was an iso week
  11639. (setq year (or iso-year year)
  11640. day (or iso-weekday wday 1)
  11641. wday nil ; to make sure that the trigger below does not match
  11642. iso-date (calendar-gregorian-from-absolute
  11643. (calendar-absolute-from-iso
  11644. (list iso-week day year))))
  11645. ; FIXME: Should we also push ISO weeks into the future?
  11646. ; (when (and org-read-date-prefer-future
  11647. ; (not iso-year)
  11648. ; (< (calendar-absolute-from-gregorian iso-date)
  11649. ; (time-to-days (current-time))))
  11650. ; (setq year (1+ year)
  11651. ; iso-date (calendar-gregorian-from-absolute
  11652. ; (calendar-absolute-from-iso
  11653. ; (list iso-week day year)))))
  11654. (setq month (car iso-date)
  11655. year (nth 2 iso-date)
  11656. day (nth 1 iso-date)))
  11657. (deltan
  11658. (unless deltadef
  11659. (let ((now (decode-time (current-time))))
  11660. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11661. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11662. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11663. ((equal deltaw "m") (setq month (+ month deltan)))
  11664. ((equal deltaw "y") (setq year (+ year deltan)))))
  11665. ((and wday (not (nth 3 tl)))
  11666. ;; Weekday was given, but no day, so pick that day in the week
  11667. ;; on or after the derived date.
  11668. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11669. (unless (equal wday wday1)
  11670. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11671. (if (and (boundp 'org-time-was-given)
  11672. (nth 2 tl))
  11673. (setq org-time-was-given t))
  11674. (if (< year 100) (setq year (+ 2000 year)))
  11675. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11676. (list second minute hour day month year)))
  11677. (defvar parse-time-weekdays)
  11678. (defun org-read-date-get-relative (s today default)
  11679. "Check string S for special relative date string.
  11680. TODAY and DEFAULT are internal times, for today and for a default.
  11681. Return shift list (N what def-flag)
  11682. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11683. N is the number of WHATs to shift.
  11684. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11685. the DEFAULT date rather than TODAY."
  11686. (when (and
  11687. (string-match
  11688. (concat
  11689. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11690. "\\([0-9]+\\)?"
  11691. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11692. "\\([ \t]\\|$\\)") s)
  11693. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11694. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11695. (string-to-char (substring (match-string 1 s) -1))
  11696. ?+))
  11697. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11698. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11699. (what (if (match-end 3) (match-string 3 s) "d"))
  11700. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11701. (date (if rel default today))
  11702. (wday (nth 6 (decode-time date)))
  11703. delta)
  11704. (if wday1
  11705. (progn
  11706. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11707. (if (= dir ?-) (setq delta (- delta 7)))
  11708. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11709. (list delta "d" rel))
  11710. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11711. (defun org-eval-in-calendar (form &optional keepdate)
  11712. "Eval FORM in the calendar window and return to current window.
  11713. Also, store the cursor date in variable org-ans2."
  11714. (let ((sf (selected-frame))
  11715. (sw (selected-window)))
  11716. (select-window (get-buffer-window "*Calendar*" t))
  11717. (eval form)
  11718. (when (and (not keepdate) (calendar-cursor-to-date))
  11719. (let* ((date (calendar-cursor-to-date))
  11720. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11721. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11722. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11723. (select-window sw)
  11724. (org-select-frame-set-input-focus sf)))
  11725. (defun org-calendar-select ()
  11726. "Return to `org-read-date' with the date currently selected.
  11727. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11728. (interactive)
  11729. (when (calendar-cursor-to-date)
  11730. (let* ((date (calendar-cursor-to-date))
  11731. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11732. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11733. (if (active-minibuffer-window) (exit-minibuffer))))
  11734. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11735. "Insert a date stamp for the date given by the internal TIME.
  11736. WITH-HM means, use the stamp format that includes the time of the day.
  11737. INACTIVE means use square brackets instead of angular ones, so that the
  11738. stamp will not contribute to the agenda.
  11739. PRE and POST are optional strings to be inserted before and after the
  11740. stamp.
  11741. The command returns the inserted time stamp."
  11742. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11743. stamp)
  11744. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11745. (insert-before-markers (or pre ""))
  11746. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11747. (when (listp extra)
  11748. (setq extra (car extra))
  11749. (if (and (stringp extra)
  11750. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11751. (setq extra (format "-%02d:%02d"
  11752. (string-to-number (match-string 1 extra))
  11753. (string-to-number (match-string 2 extra))))
  11754. (setq extra nil)))
  11755. (when extra
  11756. (backward-char 1)
  11757. (insert-before-markers extra)
  11758. (forward-char 1))
  11759. (insert-before-markers (or post ""))
  11760. (setq org-last-inserted-timestamp stamp)))
  11761. (defun org-toggle-time-stamp-overlays ()
  11762. "Toggle the use of custom time stamp formats."
  11763. (interactive)
  11764. (setq org-display-custom-times (not org-display-custom-times))
  11765. (unless org-display-custom-times
  11766. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11767. (while (setq p (next-single-property-change p 'display))
  11768. (if (and (get-text-property p 'display)
  11769. (eq (get-text-property p 'face) 'org-date))
  11770. (remove-text-properties
  11771. p (setq p (next-single-property-change p 'display))
  11772. '(display t))))
  11773. (set-buffer-modified-p bmp)))
  11774. (if (featurep 'xemacs)
  11775. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11776. (org-restart-font-lock)
  11777. (setq org-table-may-need-update t)
  11778. (if org-display-custom-times
  11779. (message "Time stamps are overlayed with custom format")
  11780. (message "Time stamp overlays removed")))
  11781. (defun org-display-custom-time (beg end)
  11782. "Overlay modified time stamp format over timestamp between BEG and END."
  11783. (let* ((ts (buffer-substring beg end))
  11784. t1 w1 with-hm tf time str w2 (off 0))
  11785. (save-match-data
  11786. (setq t1 (org-parse-time-string ts t))
  11787. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11788. (setq off (- (match-end 0) (match-beginning 0)))))
  11789. (setq end (- end off))
  11790. (setq w1 (- end beg)
  11791. with-hm (and (nth 1 t1) (nth 2 t1))
  11792. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11793. time (org-fix-decoded-time t1)
  11794. str (org-add-props
  11795. (format-time-string
  11796. (substring tf 1 -1) (apply 'encode-time time))
  11797. nil 'mouse-face 'highlight)
  11798. w2 (length str))
  11799. (if (not (= w2 w1))
  11800. (add-text-properties (1+ beg) (+ 2 beg)
  11801. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11802. (if (featurep 'xemacs)
  11803. (progn
  11804. (put-text-property beg end 'invisible t)
  11805. (put-text-property beg end 'end-glyph (make-glyph str)))
  11806. (put-text-property beg end 'display str))))
  11807. (defun org-translate-time (string)
  11808. "Translate all timestamps in STRING to custom format.
  11809. But do this only if the variable `org-display-custom-times' is set."
  11810. (when org-display-custom-times
  11811. (save-match-data
  11812. (let* ((start 0)
  11813. (re org-ts-regexp-both)
  11814. t1 with-hm inactive tf time str beg end)
  11815. (while (setq start (string-match re string start))
  11816. (setq beg (match-beginning 0)
  11817. end (match-end 0)
  11818. t1 (save-match-data
  11819. (org-parse-time-string (substring string beg end) t))
  11820. with-hm (and (nth 1 t1) (nth 2 t1))
  11821. inactive (equal (substring string beg (1+ beg)) "[")
  11822. tf (funcall (if with-hm 'cdr 'car)
  11823. org-time-stamp-custom-formats)
  11824. time (org-fix-decoded-time t1)
  11825. str (format-time-string
  11826. (concat
  11827. (if inactive "[" "<") (substring tf 1 -1)
  11828. (if inactive "]" ">"))
  11829. (apply 'encode-time time))
  11830. string (replace-match str t t string)
  11831. start (+ start (length str)))))))
  11832. string)
  11833. (defun org-fix-decoded-time (time)
  11834. "Set 0 instead of nil for the first 6 elements of time.
  11835. Don't touch the rest."
  11836. (let ((n 0))
  11837. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11838. (defun org-days-to-time (timestamp-string)
  11839. "Difference between TIMESTAMP-STRING and now in days."
  11840. (- (time-to-days (org-time-string-to-time timestamp-string))
  11841. (time-to-days (current-time))))
  11842. (defun org-deadline-close (timestamp-string &optional ndays)
  11843. "Is the time in TIMESTAMP-STRING close to the current date?"
  11844. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11845. (and (< (org-days-to-time timestamp-string) ndays)
  11846. (not (org-entry-is-done-p))))
  11847. (defun org-get-wdays (ts)
  11848. "Get the deadline lead time appropriate for timestring TS."
  11849. (cond
  11850. ((<= org-deadline-warning-days 0)
  11851. ;; 0 or negative, enforce this value no matter what
  11852. (- org-deadline-warning-days))
  11853. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11854. ;; lead time is specified.
  11855. (floor (* (string-to-number (match-string 1 ts))
  11856. (cdr (assoc (match-string 2 ts)
  11857. '(("d" . 1) ("w" . 7)
  11858. ("m" . 30.4) ("y" . 365.25)))))))
  11859. ;; go for the default.
  11860. (t org-deadline-warning-days)))
  11861. (defun org-calendar-select-mouse (ev)
  11862. "Return to `org-read-date' with the date currently selected.
  11863. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11864. (interactive "e")
  11865. (mouse-set-point ev)
  11866. (when (calendar-cursor-to-date)
  11867. (let* ((date (calendar-cursor-to-date))
  11868. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11869. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11870. (if (active-minibuffer-window) (exit-minibuffer))))
  11871. (defun org-check-deadlines (ndays)
  11872. "Check if there are any deadlines due or past due.
  11873. A deadline is considered due if it happens within `org-deadline-warning-days'
  11874. days from today's date. If the deadline appears in an entry marked DONE,
  11875. it is not shown. The prefix arg NDAYS can be used to test that many
  11876. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11877. (interactive "P")
  11878. (let* ((org-warn-days
  11879. (cond
  11880. ((equal ndays '(4)) 100000)
  11881. (ndays (prefix-numeric-value ndays))
  11882. (t (abs org-deadline-warning-days))))
  11883. (case-fold-search nil)
  11884. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11885. (callback
  11886. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11887. (message "%d deadlines past-due or due within %d days"
  11888. (org-occur regexp nil callback)
  11889. org-warn-days)))
  11890. (defun org-check-before-date (date)
  11891. "Check if there are deadlines or scheduled entries before DATE."
  11892. (interactive (list (org-read-date)))
  11893. (let ((case-fold-search nil)
  11894. (regexp (concat "\\<\\(" org-deadline-string
  11895. "\\|" org-scheduled-string
  11896. "\\) *<\\([^>]+\\)>"))
  11897. (callback
  11898. (lambda () (time-less-p
  11899. (org-time-string-to-time (match-string 2))
  11900. (org-time-string-to-time date)))))
  11901. (message "%d entries before %s"
  11902. (org-occur regexp nil callback) date)))
  11903. (defun org-check-after-date (date)
  11904. "Check if there are deadlines or scheduled entries after DATE."
  11905. (interactive (list (org-read-date)))
  11906. (let ((case-fold-search nil)
  11907. (regexp (concat "\\<\\(" org-deadline-string
  11908. "\\|" org-scheduled-string
  11909. "\\) *<\\([^>]+\\)>"))
  11910. (callback
  11911. (lambda () (not
  11912. (time-less-p
  11913. (org-time-string-to-time (match-string 2))
  11914. (org-time-string-to-time date))))))
  11915. (message "%d entries after %s"
  11916. (org-occur regexp nil callback) date)))
  11917. (defun org-evaluate-time-range (&optional to-buffer)
  11918. "Evaluate a time range by computing the difference between start and end.
  11919. Normally the result is just printed in the echo area, but with prefix arg
  11920. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11921. If the time range is actually in a table, the result is inserted into the
  11922. next column.
  11923. For time difference computation, a year is assumed to be exactly 365
  11924. days in order to avoid rounding problems."
  11925. (interactive "P")
  11926. (or
  11927. (org-clock-update-time-maybe)
  11928. (save-excursion
  11929. (unless (org-at-date-range-p t)
  11930. (goto-char (point-at-bol))
  11931. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11932. (if (not (org-at-date-range-p t))
  11933. (error "Not at a time-stamp range, and none found in current line")))
  11934. (let* ((ts1 (match-string 1))
  11935. (ts2 (match-string 2))
  11936. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11937. (match-end (match-end 0))
  11938. (time1 (org-time-string-to-time ts1))
  11939. (time2 (org-time-string-to-time ts2))
  11940. (t1 (org-float-time time1))
  11941. (t2 (org-float-time time2))
  11942. (diff (abs (- t2 t1)))
  11943. (negative (< (- t2 t1) 0))
  11944. ;; (ys (floor (* 365 24 60 60)))
  11945. (ds (* 24 60 60))
  11946. (hs (* 60 60))
  11947. (fy "%dy %dd %02d:%02d")
  11948. (fy1 "%dy %dd")
  11949. (fd "%dd %02d:%02d")
  11950. (fd1 "%dd")
  11951. (fh "%02d:%02d")
  11952. y d h m align)
  11953. (if havetime
  11954. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11955. y 0
  11956. d (floor (/ diff ds)) diff (mod diff ds)
  11957. h (floor (/ diff hs)) diff (mod diff hs)
  11958. m (floor (/ diff 60)))
  11959. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11960. y 0
  11961. d (floor (+ (/ diff ds) 0.5))
  11962. h 0 m 0))
  11963. (if (not to-buffer)
  11964. (message "%s" (org-make-tdiff-string y d h m))
  11965. (if (org-at-table-p)
  11966. (progn
  11967. (goto-char match-end)
  11968. (setq align t)
  11969. (and (looking-at " *|") (goto-char (match-end 0))))
  11970. (goto-char match-end))
  11971. (if (looking-at
  11972. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11973. (replace-match ""))
  11974. (if negative (insert " -"))
  11975. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11976. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11977. (insert " " (format fh h m))))
  11978. (if align (org-table-align))
  11979. (message "Time difference inserted")))))
  11980. (defun org-make-tdiff-string (y d h m)
  11981. (let ((fmt "")
  11982. (l nil))
  11983. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11984. l (push y l)))
  11985. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11986. l (push d l)))
  11987. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11988. l (push h l)))
  11989. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11990. l (push m l)))
  11991. (apply 'format fmt (nreverse l))))
  11992. (defun org-time-string-to-time (s)
  11993. (apply 'encode-time (org-parse-time-string s)))
  11994. (defun org-time-string-to-seconds (s)
  11995. (org-float-time (org-time-string-to-time s)))
  11996. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11997. "Convert a time stamp to an absolute day number.
  11998. If there is a specifyer for a cyclic time stamp, get the closest date to
  11999. DAYNR.
  12000. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12001. the variable date is bound by the calendar when this is called."
  12002. (cond
  12003. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12004. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12005. daynr
  12006. (+ daynr 1000)))
  12007. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12008. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12009. (time-to-days (current-time))) (match-string 0 s)
  12010. prefer show-all))
  12011. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12012. (defun org-days-to-iso-week (days)
  12013. "Return the iso week number."
  12014. (require 'cal-iso)
  12015. (car (calendar-iso-from-absolute days)))
  12016. (defun org-small-year-to-year (year)
  12017. "Convert 2-digit years into 4-digit years.
  12018. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12019. The year 2000 cannot be abbreviated. Any year larger than 99
  12020. is returned unchanged."
  12021. (if (< year 38)
  12022. (setq year (+ 2000 year))
  12023. (if (< year 100)
  12024. (setq year (+ 1900 year))))
  12025. year)
  12026. (defun org-time-from-absolute (d)
  12027. "Return the time corresponding to date D.
  12028. D may be an absolute day number, or a calendar-type list (month day year)."
  12029. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12030. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12031. (defun org-calendar-holiday ()
  12032. "List of holidays, for Diary display in Org-mode."
  12033. (require 'holidays)
  12034. (let ((hl (funcall
  12035. (if (fboundp 'calendar-check-holidays)
  12036. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12037. (if hl (mapconcat 'identity hl "; "))))
  12038. (defun org-diary-sexp-entry (sexp entry date)
  12039. "Process a SEXP diary ENTRY for DATE."
  12040. (require 'diary-lib)
  12041. (let ((result (if calendar-debug-sexp
  12042. (let ((stack-trace-on-error t))
  12043. (eval (car (read-from-string sexp))))
  12044. (condition-case nil
  12045. (eval (car (read-from-string sexp)))
  12046. (error
  12047. (beep)
  12048. (message "Bad sexp at line %d in %s: %s"
  12049. (org-current-line)
  12050. (buffer-file-name) sexp)
  12051. (sleep-for 2))))))
  12052. (cond ((stringp result) result)
  12053. ((and (consp result)
  12054. (stringp (cdr result))) (cdr result))
  12055. (result entry)
  12056. (t nil))))
  12057. (defun org-diary-to-ical-string (frombuf)
  12058. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12059. This uses the icalendar.el library."
  12060. (let* ((tmpdir (if (featurep 'xemacs)
  12061. (temp-directory)
  12062. temporary-file-directory))
  12063. (tmpfile (make-temp-name
  12064. (expand-file-name "orgics" tmpdir)))
  12065. buf rtn b e)
  12066. (save-excursion
  12067. (set-buffer frombuf)
  12068. (icalendar-export-region (point-min) (point-max) tmpfile)
  12069. (setq buf (find-buffer-visiting tmpfile))
  12070. (set-buffer buf)
  12071. (goto-char (point-min))
  12072. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12073. (setq b (match-beginning 0)))
  12074. (goto-char (point-max))
  12075. (if (re-search-backward "^END:VEVENT" nil t)
  12076. (setq e (match-end 0)))
  12077. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12078. (kill-buffer buf)
  12079. (delete-file tmpfile)
  12080. rtn))
  12081. (defun org-closest-date (start current change prefer show-all)
  12082. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12083. When PREFER is `past' return a date that is either CURRENT or past.
  12084. When PREFER is `future', return a date that is either CURRENT or future.
  12085. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12086. ;; Make the proper lists from the dates
  12087. (catch 'exit
  12088. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12089. dn dw sday cday n1 n2 n0
  12090. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12091. (setq start (org-date-to-gregorian start)
  12092. current (org-date-to-gregorian
  12093. (if show-all
  12094. current
  12095. (time-to-days (current-time))))
  12096. sday (calendar-absolute-from-gregorian start)
  12097. cday (calendar-absolute-from-gregorian current))
  12098. (if (<= cday sday) (throw 'exit sday))
  12099. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12100. (setq dn (string-to-number (match-string 1 change))
  12101. dw (cdr (assoc (match-string 2 change) a1)))
  12102. (error "Invalid change specifyer: %s" change))
  12103. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12104. (cond
  12105. ((eq dw 'day)
  12106. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12107. n2 (+ n1 dn)))
  12108. ((eq dw 'year)
  12109. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12110. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12111. (setq date1 (list m d y1)
  12112. n1 (calendar-absolute-from-gregorian date1)
  12113. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12114. n2 (calendar-absolute-from-gregorian date2)))
  12115. ((eq dw 'month)
  12116. ;; approx number of month between the two dates
  12117. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12118. ;; How often does dn fit in there?
  12119. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12120. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12121. m (+ m nm)
  12122. ny (floor (/ m 12))
  12123. y (+ y ny)
  12124. m (- m (* ny 12)))
  12125. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12126. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12127. (setq m2 (+ m dn) y2 y)
  12128. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12129. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12130. (while (<= n2 cday)
  12131. (setq n1 n2 m m2 y y2)
  12132. (setq m2 (+ m dn) y2 y)
  12133. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12134. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12135. ;; Make sure n1 is the earlier date
  12136. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12137. (if show-all
  12138. (cond
  12139. ((eq prefer 'past) n1)
  12140. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12141. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12142. (cond
  12143. ((eq prefer 'past) n1)
  12144. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12145. (t (if (= cday n1) n1 n2)))))))
  12146. (defun org-date-to-gregorian (date)
  12147. "Turn any specification of DATE into a gregorian date for the calendar."
  12148. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12149. ((and (listp date) (= (length date) 3)) date)
  12150. ((stringp date)
  12151. (setq date (org-parse-time-string date))
  12152. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12153. ((listp date)
  12154. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12155. (defun org-parse-time-string (s &optional nodefault)
  12156. "Parse the standard Org-mode time string.
  12157. This should be a lot faster than the normal `parse-time-string'.
  12158. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12159. hour and minute fields will be nil if not given."
  12160. (if (string-match org-ts-regexp0 s)
  12161. (list 0
  12162. (if (or (match-beginning 8) (not nodefault))
  12163. (string-to-number (or (match-string 8 s) "0")))
  12164. (if (or (match-beginning 7) (not nodefault))
  12165. (string-to-number (or (match-string 7 s) "0")))
  12166. (string-to-number (match-string 4 s))
  12167. (string-to-number (match-string 3 s))
  12168. (string-to-number (match-string 2 s))
  12169. nil nil nil)
  12170. (error "Not a standard Org-mode time string: %s" s)))
  12171. (defun org-timestamp-up (&optional arg)
  12172. "Increase the date item at the cursor by one.
  12173. If the cursor is on the year, change the year. If it is on the month or
  12174. the day, change that.
  12175. With prefix ARG, change by that many units."
  12176. (interactive "p")
  12177. (org-timestamp-change (prefix-numeric-value arg)))
  12178. (defun org-timestamp-down (&optional arg)
  12179. "Decrease the date item at the cursor by one.
  12180. If the cursor is on the year, change the year. If it is on the month or
  12181. the day, change that.
  12182. With prefix ARG, change by that many units."
  12183. (interactive "p")
  12184. (org-timestamp-change (- (prefix-numeric-value arg))))
  12185. (defun org-timestamp-up-day (&optional arg)
  12186. "Increase the date in the time stamp by one day.
  12187. With prefix ARG, change that many days."
  12188. (interactive "p")
  12189. (if (and (not (org-at-timestamp-p t))
  12190. (org-on-heading-p))
  12191. (org-todo 'up)
  12192. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12193. (defun org-timestamp-down-day (&optional arg)
  12194. "Decrease the date in the time stamp by one day.
  12195. With prefix ARG, change that many days."
  12196. (interactive "p")
  12197. (if (and (not (org-at-timestamp-p t))
  12198. (org-on-heading-p))
  12199. (org-todo 'down)
  12200. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12201. (defun org-at-timestamp-p (&optional inactive-ok)
  12202. "Determine if the cursor is in or at a timestamp."
  12203. (interactive)
  12204. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12205. (pos (point))
  12206. (ans (or (looking-at tsr)
  12207. (save-excursion
  12208. (skip-chars-backward "^[<\n\r\t")
  12209. (if (> (point) (point-min)) (backward-char 1))
  12210. (and (looking-at tsr)
  12211. (> (- (match-end 0) pos) -1))))))
  12212. (and ans
  12213. (boundp 'org-ts-what)
  12214. (setq org-ts-what
  12215. (cond
  12216. ((= pos (match-beginning 0)) 'bracket)
  12217. ((= pos (1- (match-end 0))) 'bracket)
  12218. ((org-pos-in-match-range pos 2) 'year)
  12219. ((org-pos-in-match-range pos 3) 'month)
  12220. ((org-pos-in-match-range pos 7) 'hour)
  12221. ((org-pos-in-match-range pos 8) 'minute)
  12222. ((or (org-pos-in-match-range pos 4)
  12223. (org-pos-in-match-range pos 5)) 'day)
  12224. ((and (> pos (or (match-end 8) (match-end 5)))
  12225. (< pos (match-end 0)))
  12226. (- pos (or (match-end 8) (match-end 5))))
  12227. (t 'day))))
  12228. ans))
  12229. (defun org-toggle-timestamp-type ()
  12230. "Toggle the type (<active> or [inactive]) of a time stamp."
  12231. (interactive)
  12232. (when (org-at-timestamp-p t)
  12233. (let ((beg (match-beginning 0)) (end (match-end 0))
  12234. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12235. (save-excursion
  12236. (goto-char beg)
  12237. (while (re-search-forward "[][<>]" end t)
  12238. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12239. t t)))
  12240. (message "Timestamp is now %sactive"
  12241. (if (equal (char-after beg) ?<) "" "in")))))
  12242. (defun org-timestamp-change (n &optional what)
  12243. "Change the date in the time stamp at point.
  12244. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12245. `year', `minute', `second'. If WHAT is not given, the cursor position
  12246. in the timestamp determines what will be changed."
  12247. (let ((pos (point))
  12248. with-hm inactive
  12249. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12250. org-ts-what
  12251. extra rem
  12252. ts time time0)
  12253. (if (not (org-at-timestamp-p t))
  12254. (error "Not at a timestamp"))
  12255. (if (and (not what) (eq org-ts-what 'bracket))
  12256. (org-toggle-timestamp-type)
  12257. (if (and (not what) (not (eq org-ts-what 'day))
  12258. org-display-custom-times
  12259. (get-text-property (point) 'display)
  12260. (not (get-text-property (1- (point)) 'display)))
  12261. (setq org-ts-what 'day))
  12262. (setq org-ts-what (or what org-ts-what)
  12263. inactive (= (char-after (match-beginning 0)) ?\[)
  12264. ts (match-string 0))
  12265. (replace-match "")
  12266. (if (string-match
  12267. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12268. ts)
  12269. (setq extra (match-string 1 ts)))
  12270. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12271. (setq with-hm t))
  12272. (setq time0 (org-parse-time-string ts))
  12273. (when (and (eq org-ts-what 'minute)
  12274. (eq current-prefix-arg nil))
  12275. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12276. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12277. (setcar (cdr time0) (+ (nth 1 time0)
  12278. (if (> n 0) (- rem) (- dm rem))))))
  12279. (setq time
  12280. (encode-time (or (car time0) 0)
  12281. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12282. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12283. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12284. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12285. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12286. (nthcdr 6 time0)))
  12287. (when (and (member org-ts-what '(hour minute))
  12288. extra
  12289. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12290. (setq extra (org-modify-ts-extra
  12291. extra
  12292. (if (eq org-ts-what 'hour) 2 5)
  12293. n dm)))
  12294. (when (integerp org-ts-what)
  12295. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12296. (if (eq what 'calendar)
  12297. (let ((cal-date (org-get-date-from-calendar)))
  12298. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12299. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12300. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12301. (setcar time0 (or (car time0) 0))
  12302. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12303. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12304. (setq time (apply 'encode-time time0))))
  12305. (setq org-last-changed-timestamp
  12306. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12307. (org-clock-update-time-maybe)
  12308. (goto-char pos)
  12309. ;; Try to recenter the calendar window, if any
  12310. (if (and org-calendar-follow-timestamp-change
  12311. (get-buffer-window "*Calendar*" t)
  12312. (memq org-ts-what '(day month year)))
  12313. (org-recenter-calendar (time-to-days time))))))
  12314. (defun org-modify-ts-extra (s pos n dm)
  12315. "Change the different parts of the lead-time and repeat fields in timestamp."
  12316. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12317. ng h m new rem)
  12318. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12319. (cond
  12320. ((or (org-pos-in-match-range pos 2)
  12321. (org-pos-in-match-range pos 3))
  12322. (setq m (string-to-number (match-string 3 s))
  12323. h (string-to-number (match-string 2 s)))
  12324. (if (org-pos-in-match-range pos 2)
  12325. (setq h (+ h n))
  12326. (setq n (* dm (org-no-warnings (signum n))))
  12327. (when (not (= 0 (setq rem (% m dm))))
  12328. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12329. (setq m (+ m n)))
  12330. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12331. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12332. (setq h (min 24 (max 0 h)))
  12333. (setq ng 1 new (format "-%02d:%02d" h m)))
  12334. ((org-pos-in-match-range pos 6)
  12335. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12336. ((org-pos-in-match-range pos 5)
  12337. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12338. ((org-pos-in-match-range pos 9)
  12339. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12340. ((org-pos-in-match-range pos 8)
  12341. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12342. (when ng
  12343. (setq s (concat
  12344. (substring s 0 (match-beginning ng))
  12345. new
  12346. (substring s (match-end ng))))))
  12347. s))
  12348. (defun org-recenter-calendar (date)
  12349. "If the calendar is visible, recenter it to DATE."
  12350. (let* ((win (selected-window))
  12351. (cwin (get-buffer-window "*Calendar*" t))
  12352. (calendar-move-hook nil))
  12353. (when cwin
  12354. (select-window cwin)
  12355. (calendar-goto-date (if (listp date) date
  12356. (calendar-gregorian-from-absolute date)))
  12357. (select-window win))))
  12358. (defun org-goto-calendar (&optional arg)
  12359. "Go to the Emacs calendar at the current date.
  12360. If there is a time stamp in the current line, go to that date.
  12361. A prefix ARG can be used to force the current date."
  12362. (interactive "P")
  12363. (let ((tsr org-ts-regexp) diff
  12364. (calendar-move-hook nil)
  12365. (calendar-view-holidays-initially-flag nil)
  12366. (view-calendar-holidays-initially nil)
  12367. (calendar-view-diary-initially-flag nil)
  12368. (view-diary-entries-initially nil))
  12369. (if (or (org-at-timestamp-p)
  12370. (save-excursion
  12371. (beginning-of-line 1)
  12372. (looking-at (concat ".*" tsr))))
  12373. (let ((d1 (time-to-days (current-time)))
  12374. (d2 (time-to-days
  12375. (org-time-string-to-time (match-string 1)))))
  12376. (setq diff (- d2 d1))))
  12377. (calendar)
  12378. (calendar-goto-today)
  12379. (if (and diff (not arg)) (calendar-forward-day diff))))
  12380. (defun org-get-date-from-calendar ()
  12381. "Return a list (month day year) of date at point in calendar."
  12382. (with-current-buffer "*Calendar*"
  12383. (save-match-data
  12384. (calendar-cursor-to-date))))
  12385. (defun org-date-from-calendar ()
  12386. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12387. If there is already a time stamp at the cursor position, update it."
  12388. (interactive)
  12389. (if (org-at-timestamp-p t)
  12390. (org-timestamp-change 0 'calendar)
  12391. (let ((cal-date (org-get-date-from-calendar)))
  12392. (org-insert-time-stamp
  12393. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12394. (defun org-minutes-to-hh:mm-string (m)
  12395. "Compute H:MM from a number of minutes."
  12396. (let ((h (/ m 60)))
  12397. (setq m (- m (* 60 h)))
  12398. (format org-time-clocksum-format h m)))
  12399. (defun org-hh:mm-string-to-minutes (s)
  12400. "Convert a string H:MM to a number of minutes.
  12401. If the string is just a number, interprete it as minutes.
  12402. In fact, the first hh:mm or number in the string will be taken,
  12403. there can be extra stuff in the string.
  12404. If no number is found, the return value is 0."
  12405. (cond
  12406. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12407. (+ (* (string-to-number (match-string 1 s)) 60)
  12408. (string-to-number (match-string 2 s))))
  12409. ((string-match "\\([0-9]+\\)" s)
  12410. (string-to-number (match-string 1 s)))
  12411. (t 0)))
  12412. ;;;; Files
  12413. (defun org-save-all-org-buffers ()
  12414. "Save all Org-mode buffers without user confirmation."
  12415. (interactive)
  12416. (message "Saving all Org-mode buffers...")
  12417. (save-some-buffers t 'org-mode-p)
  12418. (when (featurep 'org-id) (org-id-locations-save))
  12419. (message "Saving all Org-mode buffers... done"))
  12420. (defun org-revert-all-org-buffers ()
  12421. "Revert all Org-mode buffers.
  12422. Prompt for confirmation when there are unsaved changes.
  12423. Be sure you know what you are doing before letting this function
  12424. overwrite your changes.
  12425. This function is useful in a setup where one tracks org files
  12426. with a version control system, to revert on one machine after pulling
  12427. changes from another. I believe the procedure must be like this:
  12428. 1. M-x org-save-all-org-buffers
  12429. 2. Pull changes from the other machine, resolve conflicts
  12430. 3. M-x org-revert-all-org-buffers"
  12431. (interactive)
  12432. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12433. (error "Abort"))
  12434. (save-excursion
  12435. (save-window-excursion
  12436. (mapc
  12437. (lambda (b)
  12438. (when (and (with-current-buffer b (org-mode-p))
  12439. (with-current-buffer b buffer-file-name))
  12440. (switch-to-buffer b)
  12441. (revert-buffer t 'no-confirm)))
  12442. (buffer-list))
  12443. (when (and (featurep 'org-id) org-id-track-globally)
  12444. (org-id-locations-load)))))
  12445. ;;;; Agenda files
  12446. ;;;###autoload
  12447. (defun org-iswitchb (&optional arg)
  12448. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12449. With a prefix argument, restrict available to files.
  12450. With two prefix arguments, restrict available buffers to agenda files."
  12451. (interactive "P")
  12452. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12453. ((equal arg '(16)) (org-buffer-list 'agenda))
  12454. (t (org-buffer-list)))))
  12455. (switch-to-buffer
  12456. (org-icompleting-read "Org buffer: "
  12457. (mapcar 'list (mapcar 'buffer-name blist))
  12458. nil t))))
  12459. ;;;###autoload
  12460. (defalias 'org-ido-switchb 'org-iswitchb)
  12461. (defun org-buffer-list (&optional predicate exclude-tmp)
  12462. "Return a list of Org buffers.
  12463. PREDICATE can be `export', `files' or `agenda'.
  12464. export restrict the list to Export buffers.
  12465. files restrict the list to buffers visiting Org files.
  12466. agenda restrict the list to buffers visiting agenda files.
  12467. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12468. (let* ((bfn nil)
  12469. (agenda-files (and (eq predicate 'agenda)
  12470. (mapcar 'file-truename (org-agenda-files t))))
  12471. (filter
  12472. (cond
  12473. ((eq predicate 'files)
  12474. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12475. ((eq predicate 'export)
  12476. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12477. ((eq predicate 'agenda)
  12478. (lambda (b)
  12479. (with-current-buffer b
  12480. (and (eq major-mode 'org-mode)
  12481. (setq bfn (buffer-file-name b))
  12482. (member (file-truename bfn) agenda-files)))))
  12483. (t (lambda (b) (with-current-buffer b
  12484. (or (eq major-mode 'org-mode)
  12485. (string-match "\*Org .*Export"
  12486. (buffer-name b)))))))))
  12487. (delq nil
  12488. (mapcar
  12489. (lambda(b)
  12490. (if (and (funcall filter b)
  12491. (or (not exclude-tmp)
  12492. (not (string-match "tmp" (buffer-name b)))))
  12493. b
  12494. nil))
  12495. (buffer-list)))))
  12496. (defun org-agenda-files (&optional unrestricted archives)
  12497. "Get the list of agenda files.
  12498. Optional UNRESTRICTED means return the full list even if a restriction
  12499. is currently in place.
  12500. When ARCHIVES is t, include all archive files hat are really being
  12501. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12502. `org-agenda-archives-mode' is t."
  12503. (let ((files
  12504. (cond
  12505. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12506. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12507. ((listp org-agenda-files) org-agenda-files)
  12508. (t (error "Invalid value of `org-agenda-files'")))))
  12509. (setq files (apply 'append
  12510. (mapcar (lambda (f)
  12511. (if (file-directory-p f)
  12512. (directory-files
  12513. f t org-agenda-file-regexp)
  12514. (list f)))
  12515. files)))
  12516. (when org-agenda-skip-unavailable-files
  12517. (setq files (delq nil
  12518. (mapcar (function
  12519. (lambda (file)
  12520. (and (file-readable-p file) file)))
  12521. files))))
  12522. (when (or (eq archives t)
  12523. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12524. (setq files (org-add-archive-files files)))
  12525. files))
  12526. (defun org-edit-agenda-file-list ()
  12527. "Edit the list of agenda files.
  12528. Depending on setup, this either uses customize to edit the variable
  12529. `org-agenda-files', or it visits the file that is holding the list. In the
  12530. latter case, the buffer is set up in a way that saving it automatically kills
  12531. the buffer and restores the previous window configuration."
  12532. (interactive)
  12533. (if (stringp org-agenda-files)
  12534. (let ((cw (current-window-configuration)))
  12535. (find-file org-agenda-files)
  12536. (org-set-local 'org-window-configuration cw)
  12537. (org-add-hook 'after-save-hook
  12538. (lambda ()
  12539. (set-window-configuration
  12540. (prog1 org-window-configuration
  12541. (kill-buffer (current-buffer))))
  12542. (org-install-agenda-files-menu)
  12543. (message "New agenda file list installed"))
  12544. nil 'local)
  12545. (message "%s" (substitute-command-keys
  12546. "Edit list and finish with \\[save-buffer]")))
  12547. (customize-variable 'org-agenda-files)))
  12548. (defun org-store-new-agenda-file-list (list)
  12549. "Set new value for the agenda file list and save it correctly."
  12550. (if (stringp org-agenda-files)
  12551. (let ((f org-agenda-files) b)
  12552. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12553. (with-temp-file f
  12554. (insert (mapconcat 'identity list "\n") "\n")))
  12555. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12556. (org-insert-mode-line-in-empty-file nil))
  12557. (setq org-agenda-files list)
  12558. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12559. (defun org-read-agenda-file-list ()
  12560. "Read the list of agenda files from a file."
  12561. (when (file-directory-p org-agenda-files)
  12562. (error "`org-agenda-files' cannot be a single directory"))
  12563. (when (stringp org-agenda-files)
  12564. (with-temp-buffer
  12565. (insert-file-contents org-agenda-files)
  12566. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12567. ;;;###autoload
  12568. (defun org-cycle-agenda-files ()
  12569. "Cycle through the files in `org-agenda-files'.
  12570. If the current buffer visits an agenda file, find the next one in the list.
  12571. If the current buffer does not, find the first agenda file."
  12572. (interactive)
  12573. (let* ((fs (org-agenda-files t))
  12574. (files (append fs (list (car fs))))
  12575. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12576. file)
  12577. (unless files (error "No agenda files"))
  12578. (catch 'exit
  12579. (while (setq file (pop files))
  12580. (if (equal (file-truename file) tcf)
  12581. (when (car files)
  12582. (find-file (car files))
  12583. (throw 'exit t))))
  12584. (find-file (car fs)))
  12585. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12586. (defun org-agenda-file-to-front (&optional to-end)
  12587. "Move/add the current file to the top of the agenda file list.
  12588. If the file is not present in the list, it is added to the front. If it is
  12589. present, it is moved there. With optional argument TO-END, add/move to the
  12590. end of the list."
  12591. (interactive "P")
  12592. (let ((org-agenda-skip-unavailable-files nil)
  12593. (file-alist (mapcar (lambda (x)
  12594. (cons (file-truename x) x))
  12595. (org-agenda-files t)))
  12596. (ctf (file-truename buffer-file-name))
  12597. x had)
  12598. (setq x (assoc ctf file-alist) had x)
  12599. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12600. (if to-end
  12601. (setq file-alist (append (delq x file-alist) (list x)))
  12602. (setq file-alist (cons x (delq x file-alist))))
  12603. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12604. (org-install-agenda-files-menu)
  12605. (message "File %s to %s of agenda file list"
  12606. (if had "moved" "added") (if to-end "end" "front"))))
  12607. (defun org-remove-file (&optional file)
  12608. "Remove current file from the list of files in variable `org-agenda-files'.
  12609. These are the files which are being checked for agenda entries.
  12610. Optional argument FILE means, use this file instead of the current."
  12611. (interactive)
  12612. (let* ((org-agenda-skip-unavailable-files nil)
  12613. (file (or file buffer-file-name))
  12614. (true-file (file-truename file))
  12615. (afile (abbreviate-file-name file))
  12616. (files (delq nil (mapcar
  12617. (lambda (x)
  12618. (if (equal true-file
  12619. (file-truename x))
  12620. nil x))
  12621. (org-agenda-files t)))))
  12622. (if (not (= (length files) (length (org-agenda-files t))))
  12623. (progn
  12624. (org-store-new-agenda-file-list files)
  12625. (org-install-agenda-files-menu)
  12626. (message "Removed file: %s" afile))
  12627. (message "File was not in list: %s (not removed)" afile))))
  12628. (defun org-file-menu-entry (file)
  12629. (vector file (list 'find-file file) t))
  12630. (defun org-check-agenda-file (file)
  12631. "Make sure FILE exists. If not, ask user what to do."
  12632. (when (not (file-exists-p file))
  12633. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  12634. (abbreviate-file-name file))
  12635. (let ((r (downcase (read-char-exclusive))))
  12636. (cond
  12637. ((equal r ?r)
  12638. (org-remove-file file)
  12639. (throw 'nextfile t))
  12640. (t (error "Abort"))))))
  12641. (defun org-get-agenda-file-buffer (file)
  12642. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12643. it to the list of buffers which might be released later."
  12644. (let ((buf (org-find-base-buffer-visiting file)))
  12645. (if buf
  12646. buf ; just return it
  12647. ;; Make a new buffer and remember it
  12648. (setq buf (find-file-noselect file))
  12649. (if buf (push buf org-agenda-new-buffers))
  12650. buf)))
  12651. (defun org-release-buffers (blist)
  12652. "Release all buffers in list, asking the user for confirmation when needed.
  12653. When a buffer is unmodified, it is just killed. When modified, it is saved
  12654. \(if the user agrees) and then killed."
  12655. (let (buf file)
  12656. (while (setq buf (pop blist))
  12657. (setq file (buffer-file-name buf))
  12658. (when (and (buffer-modified-p buf)
  12659. file
  12660. (y-or-n-p (format "Save file %s? " file)))
  12661. (with-current-buffer buf (save-buffer)))
  12662. (kill-buffer buf))))
  12663. (defun org-prepare-agenda-buffers (files)
  12664. "Create buffers for all agenda files, protect archived trees and comments."
  12665. (interactive)
  12666. (let ((pa '(:org-archived t))
  12667. (pc '(:org-comment t))
  12668. (pall '(:org-archived t :org-comment t))
  12669. (inhibit-read-only t)
  12670. (rea (concat ":" org-archive-tag ":"))
  12671. bmp file re)
  12672. (save-excursion
  12673. (save-restriction
  12674. (while (setq file (pop files))
  12675. (catch 'nextfile
  12676. (if (bufferp file)
  12677. (set-buffer file)
  12678. (org-check-agenda-file file)
  12679. (set-buffer (org-get-agenda-file-buffer file)))
  12680. (widen)
  12681. (setq bmp (buffer-modified-p))
  12682. (org-refresh-category-properties)
  12683. (setq org-todo-keywords-for-agenda
  12684. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12685. (setq org-done-keywords-for-agenda
  12686. (append org-done-keywords-for-agenda org-done-keywords))
  12687. (setq org-todo-keyword-alist-for-agenda
  12688. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12689. (setq org-tag-alist-for-agenda
  12690. (append org-tag-alist-for-agenda org-tag-alist))
  12691. (save-excursion
  12692. (remove-text-properties (point-min) (point-max) pall)
  12693. (when org-agenda-skip-archived-trees
  12694. (goto-char (point-min))
  12695. (while (re-search-forward rea nil t)
  12696. (if (org-on-heading-p t)
  12697. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12698. (goto-char (point-min))
  12699. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12700. (while (re-search-forward re nil t)
  12701. (add-text-properties
  12702. (match-beginning 0) (org-end-of-subtree t) pc)))
  12703. (set-buffer-modified-p bmp)))))
  12704. (setq org-todo-keyword-alist-for-agenda
  12705. (org-uniquify org-todo-keyword-alist-for-agenda)
  12706. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12707. ;;;; Embedded LaTeX
  12708. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12709. "Keymap for the minor `org-cdlatex-mode'.")
  12710. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12711. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12712. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12713. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12714. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12715. (defvar org-cdlatex-texmathp-advice-is-done nil
  12716. "Flag remembering if we have applied the advice to texmathp already.")
  12717. (define-minor-mode org-cdlatex-mode
  12718. "Toggle the minor `org-cdlatex-mode'.
  12719. This mode supports entering LaTeX environment and math in LaTeX fragments
  12720. in Org-mode.
  12721. \\{org-cdlatex-mode-map}"
  12722. nil " OCDL" nil
  12723. (when org-cdlatex-mode (require 'cdlatex))
  12724. (unless org-cdlatex-texmathp-advice-is-done
  12725. (setq org-cdlatex-texmathp-advice-is-done t)
  12726. (defadvice texmathp (around org-math-always-on activate)
  12727. "Always return t in org-mode buffers.
  12728. This is because we want to insert math symbols without dollars even outside
  12729. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12730. an embedded LaTeX fragment, let texmathp do its job.
  12731. \\[org-cdlatex-mode-map]"
  12732. (interactive)
  12733. (let (p)
  12734. (cond
  12735. ((not (org-mode-p)) ad-do-it)
  12736. ((eq this-command 'cdlatex-math-symbol)
  12737. (setq ad-return-value t
  12738. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12739. (t
  12740. (let ((p (org-inside-LaTeX-fragment-p)))
  12741. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12742. (setq ad-return-value t
  12743. texmathp-why '("Org-mode embedded math" . 0))
  12744. (if p ad-do-it)))))))))
  12745. (defun turn-on-org-cdlatex ()
  12746. "Unconditionally turn on `org-cdlatex-mode'."
  12747. (org-cdlatex-mode 1))
  12748. (defun org-inside-LaTeX-fragment-p ()
  12749. "Test if point is inside a LaTeX fragment.
  12750. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12751. sequence appearing also before point.
  12752. Even though the matchers for math are configurable, this function assumes
  12753. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12754. delimiters are skipped when they have been removed by customization.
  12755. The return value is nil, or a cons cell with the delimiter and
  12756. and the position of this delimiter.
  12757. This function does a reasonably good job, but can locally be fooled by
  12758. for example currency specifications. For example it will assume being in
  12759. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12760. fragments that are properly closed, but during editing, we have to live
  12761. with the uncertainty caused by missing closing delimiters. This function
  12762. looks only before point, not after."
  12763. (catch 'exit
  12764. (let ((pos (point))
  12765. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12766. (lim (progn
  12767. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12768. (point)))
  12769. dd-on str (start 0) m re)
  12770. (goto-char pos)
  12771. (when dodollar
  12772. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12773. re (nth 1 (assoc "$" org-latex-regexps)))
  12774. (while (string-match re str start)
  12775. (cond
  12776. ((= (match-end 0) (length str))
  12777. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12778. ((= (match-end 0) (- (length str) 5))
  12779. (throw 'exit nil))
  12780. (t (setq start (match-end 0))))))
  12781. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12782. (goto-char pos)
  12783. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12784. (and (match-beginning 2) (throw 'exit nil))
  12785. ;; count $$
  12786. (while (re-search-backward "\\$\\$" lim t)
  12787. (setq dd-on (not dd-on)))
  12788. (goto-char pos)
  12789. (if dd-on (cons "$$" m))))))
  12790. (defun org-try-cdlatex-tab ()
  12791. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12792. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12793. - inside a LaTeX fragment, or
  12794. - after the first word in a line, where an abbreviation expansion could
  12795. insert a LaTeX environment."
  12796. (when org-cdlatex-mode
  12797. (cond
  12798. ((save-excursion
  12799. (skip-chars-backward "a-zA-Z0-9*")
  12800. (skip-chars-backward " \t")
  12801. (bolp))
  12802. (cdlatex-tab) t)
  12803. ((org-inside-LaTeX-fragment-p)
  12804. (cdlatex-tab) t)
  12805. (t nil))))
  12806. (defun org-cdlatex-underscore-caret (&optional arg)
  12807. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12808. Revert to the normal definition outside of these fragments."
  12809. (interactive "P")
  12810. (if (org-inside-LaTeX-fragment-p)
  12811. (call-interactively 'cdlatex-sub-superscript)
  12812. (let (org-cdlatex-mode)
  12813. (call-interactively (key-binding (vector last-input-event))))))
  12814. (defun org-cdlatex-math-modify (&optional arg)
  12815. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12816. Revert to the normal definition outside of these fragments."
  12817. (interactive "P")
  12818. (if (org-inside-LaTeX-fragment-p)
  12819. (call-interactively 'cdlatex-math-modify)
  12820. (let (org-cdlatex-mode)
  12821. (call-interactively (key-binding (vector last-input-event))))))
  12822. (defvar org-latex-fragment-image-overlays nil
  12823. "List of overlays carrying the images of latex fragments.")
  12824. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12825. (defun org-remove-latex-fragment-image-overlays ()
  12826. "Remove all overlays with LaTeX fragment images in current buffer."
  12827. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12828. (setq org-latex-fragment-image-overlays nil))
  12829. (defun org-preview-latex-fragment (&optional subtree)
  12830. "Preview the LaTeX fragment at point, or all locally or globally.
  12831. If the cursor is in a LaTeX fragment, create the image and overlay
  12832. it over the source code. If there is no fragment at point, display
  12833. all fragments in the current text, from one headline to the next. With
  12834. prefix SUBTREE, display all fragments in the current subtree. With a
  12835. double prefix `C-u C-u', or when the cursor is before the first headline,
  12836. display all fragments in the buffer.
  12837. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12838. (interactive "P")
  12839. (org-remove-latex-fragment-image-overlays)
  12840. (save-excursion
  12841. (save-restriction
  12842. (let (beg end at msg)
  12843. (cond
  12844. ((or (equal subtree '(16))
  12845. (not (save-excursion
  12846. (re-search-backward (concat "^" outline-regexp) nil t))))
  12847. (setq beg (point-min) end (point-max)
  12848. msg "Creating images for buffer...%s"))
  12849. ((equal subtree '(4))
  12850. (org-back-to-heading)
  12851. (setq beg (point) end (org-end-of-subtree t)
  12852. msg "Creating images for subtree...%s"))
  12853. (t
  12854. (if (setq at (org-inside-LaTeX-fragment-p))
  12855. (goto-char (max (point-min) (- (cdr at) 2)))
  12856. (org-back-to-heading))
  12857. (setq beg (point) end (progn (outline-next-heading) (point))
  12858. msg (if at "Creating image...%s"
  12859. "Creating images for entry...%s"))))
  12860. (message msg "")
  12861. (narrow-to-region beg end)
  12862. (goto-char beg)
  12863. (org-format-latex
  12864. (concat "ltxpng/" (file-name-sans-extension
  12865. (file-name-nondirectory
  12866. buffer-file-name)))
  12867. default-directory 'overlays msg at 'forbuffer)
  12868. (message msg "done. Use `C-c C-c' to remove images.")))))
  12869. (defvar org-latex-regexps
  12870. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12871. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12872. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12873. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12874. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12875. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12876. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  12877. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  12878. "Regular expressions for matching embedded LaTeX.")
  12879. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12880. "Replace LaTeX fragments with links to an image, and produce images."
  12881. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12882. (let* ((prefixnodir (file-name-nondirectory prefix))
  12883. (absprefix (expand-file-name prefix dir))
  12884. (todir (file-name-directory absprefix))
  12885. (opt org-format-latex-options)
  12886. (matchers (plist-get opt :matchers))
  12887. (re-list org-latex-regexps)
  12888. (cnt 0) txt link beg end re e checkdir
  12889. executables-checked
  12890. m n block linkfile movefile ov)
  12891. ;; Check if there are old images files with this prefix, and remove them
  12892. (when (file-directory-p todir)
  12893. (mapc 'delete-file
  12894. (directory-files
  12895. todir 'full
  12896. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12897. ;; Check the different regular expressions
  12898. (while (setq e (pop re-list))
  12899. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12900. block (if (nth 3 e) "\n\n" ""))
  12901. (when (member m matchers)
  12902. (goto-char (point-min))
  12903. (while (re-search-forward re nil t)
  12904. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12905. (not (get-text-property (match-beginning n)
  12906. 'org-protected))
  12907. (or (not overlays)
  12908. (not (eq (get-char-property (match-beginning n)
  12909. 'org-overlay-type)
  12910. 'org-latex-overlay))))
  12911. (setq txt (match-string n)
  12912. beg (match-beginning n) end (match-end n)
  12913. cnt (1+ cnt)
  12914. linkfile (format "%s_%04d.png" prefix cnt)
  12915. movefile (format "%s_%04d.png" absprefix cnt)
  12916. link (concat block "[[file:" linkfile "]]" block))
  12917. (if msg (message msg cnt))
  12918. (goto-char beg)
  12919. (unless checkdir ; make sure the directory exists
  12920. (setq checkdir t)
  12921. (or (file-directory-p todir) (make-directory todir)))
  12922. (unless executables-checked
  12923. (org-check-external-command
  12924. "latex" "needed to convert LaTeX fragments to images")
  12925. (org-check-external-command
  12926. "dvipng" "needed to convert LaTeX fragments to images")
  12927. (setq executables-checked t))
  12928. (org-create-formula-image
  12929. txt movefile opt forbuffer)
  12930. (if overlays
  12931. (progn
  12932. (mapc (lambda (o)
  12933. (if (eq (org-overlay-get o 'org-overlay-type)
  12934. 'org-latex-overlay)
  12935. (org-delete-overlay o)))
  12936. (org-overlays-in beg end))
  12937. (setq ov (org-make-overlay beg end))
  12938. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  12939. (if (featurep 'xemacs)
  12940. (progn
  12941. (org-overlay-put ov 'invisible t)
  12942. (org-overlay-put
  12943. ov 'end-glyph
  12944. (make-glyph (vector 'png :file movefile))))
  12945. (org-overlay-put
  12946. ov 'display
  12947. (list 'image :type 'png :file movefile :ascent 'center)))
  12948. (push ov org-latex-fragment-image-overlays)
  12949. (goto-char end))
  12950. (delete-region beg end)
  12951. (insert link))))))))
  12952. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  12953. ;; This function borrows from Ganesh Swami's latex2png.el
  12954. (defun org-create-formula-image (string tofile options buffer)
  12955. (require 'org-latex)
  12956. (let* ((tmpdir (if (featurep 'xemacs)
  12957. (temp-directory)
  12958. temporary-file-directory))
  12959. (texfilebase (make-temp-name
  12960. (expand-file-name "orgtex" tmpdir)))
  12961. (texfile (concat texfilebase ".tex"))
  12962. (dvifile (concat texfilebase ".dvi"))
  12963. (pngfile (concat texfilebase ".png"))
  12964. (fnh (if (featurep 'xemacs)
  12965. (font-height (get-face-font 'default))
  12966. (face-attribute 'default :height nil)))
  12967. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12968. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12969. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12970. "Black"))
  12971. (bg (or (plist-get options (if buffer :background :html-background))
  12972. "Transparent")))
  12973. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12974. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12975. (with-temp-file texfile
  12976. (insert org-format-latex-header
  12977. (if org-export-latex-packages-alist
  12978. (concat "\n"
  12979. (mapconcat (lambda(p)
  12980. (if (equal "" (car p))
  12981. (format "\\usepackage{%s}" (cadr p))
  12982. (format "\\usepackage[%s]{%s}"
  12983. (car p) (cadr p))))
  12984. org-export-latex-packages-alist "\n"))
  12985. "")
  12986. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12987. (let ((dir default-directory))
  12988. (condition-case nil
  12989. (progn
  12990. (cd tmpdir)
  12991. (call-process "latex" nil nil nil texfile))
  12992. (error nil))
  12993. (cd dir))
  12994. (if (not (file-exists-p dvifile))
  12995. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12996. (condition-case nil
  12997. (call-process "dvipng" nil nil nil
  12998. "-fg" fg "-bg" bg
  12999. "-D" dpi
  13000. ;;"-x" scale "-y" scale
  13001. "-T" "tight"
  13002. "-o" pngfile
  13003. dvifile)
  13004. (error nil))
  13005. (if (not (file-exists-p pngfile))
  13006. (progn (message "Failed to create png file from %s" texfile) nil)
  13007. ;; Use the requested file name and clean up
  13008. (copy-file pngfile tofile 'replace)
  13009. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13010. (delete-file (concat texfilebase e)))
  13011. pngfile))))
  13012. (defun org-dvipng-color (attr)
  13013. "Return an rgb color specification for dvipng."
  13014. (apply 'format "rgb %s %s %s"
  13015. (mapcar 'org-normalize-color
  13016. (color-values (face-attribute 'default attr nil)))))
  13017. (defun org-normalize-color (value)
  13018. "Return string to be used as color value for an RGB component."
  13019. (format "%g" (/ value 65535.0)))
  13020. ;;;; Key bindings
  13021. ;; Make `C-c C-x' a prefix key
  13022. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13023. ;; TAB key with modifiers
  13024. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13025. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13026. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13027. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13028. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13029. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13030. ;; The following line is necessary under Suse GNU/Linux
  13031. (unless (featurep 'xemacs)
  13032. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13033. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13034. (define-key org-mode-map [backtab] 'org-shifttab)
  13035. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13036. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13037. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13038. ;; Cursor keys with modifiers
  13039. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13040. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13041. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13042. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13043. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13044. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13045. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13046. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13047. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13048. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13049. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13050. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13051. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13052. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13053. ;;; Extra keys for tty access.
  13054. ;; We only set them when really needed because otherwise the
  13055. ;; menus don't show the simple keys
  13056. (when (or org-use-extra-keys
  13057. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13058. (not window-system))
  13059. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13060. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13061. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13062. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13063. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13064. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13065. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13066. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13067. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13068. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13069. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13070. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13071. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13072. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13073. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13074. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13075. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13076. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13077. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13078. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13079. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13080. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13081. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13082. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13083. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13084. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13085. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13086. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13087. ;; All the other keys
  13088. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13089. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13090. (if (boundp 'narrow-map)
  13091. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13092. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13093. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13094. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13095. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13096. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13097. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  13098. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13099. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13100. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13101. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13102. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13103. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13104. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13105. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13106. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13107. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13108. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13109. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13110. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13111. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13112. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13113. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13114. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13115. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13116. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13117. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13118. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13119. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13120. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13121. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13122. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13123. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13124. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13125. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13126. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13127. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13128. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13129. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13130. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13131. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13132. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13133. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13134. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13135. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13136. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13137. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13138. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13139. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13140. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13141. (org-defkey org-mode-map "\C-m" 'org-return)
  13142. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13143. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13144. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13145. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13146. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13147. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13148. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13149. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13150. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13151. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13152. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13153. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13154. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13155. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13156. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13157. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13158. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13159. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13160. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13161. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13162. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13163. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13164. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13165. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13166. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13167. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13168. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13169. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13170. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13171. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13172. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13173. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13174. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13175. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13176. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13177. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13178. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13179. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13180. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13181. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13182. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13183. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13184. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13185. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13186. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13187. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13188. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13189. (when (featurep 'xemacs)
  13190. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13191. (defvar org-self-insert-command-undo-counter 0)
  13192. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13193. (defun org-self-insert-command (N)
  13194. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13195. If the cursor is in a table looking at whitespace, the whitespace is
  13196. overwritten, and the table is not marked as requiring realignment."
  13197. (interactive "p")
  13198. (if (and
  13199. (org-table-p)
  13200. (progn
  13201. ;; check if we blank the field, and if that triggers align
  13202. (and (featurep 'org-table) org-table-auto-blank-field
  13203. (member last-command
  13204. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13205. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13206. ;; got extra space, this field does not determine column width
  13207. (let (org-table-may-need-update) (org-table-blank-field))
  13208. ;; no extra space, this field may determine column width
  13209. (org-table-blank-field)))
  13210. t)
  13211. (eq N 1)
  13212. (looking-at "[^|\n]* |"))
  13213. (let (org-table-may-need-update)
  13214. (goto-char (1- (match-end 0)))
  13215. (delete-backward-char 1)
  13216. (goto-char (match-beginning 0))
  13217. (self-insert-command N))
  13218. (setq org-table-may-need-update t)
  13219. (self-insert-command N)
  13220. (org-fix-tags-on-the-fly)
  13221. (if org-self-insert-cluster-for-undo
  13222. (if (not (eq last-command 'org-self-insert-command))
  13223. (setq org-self-insert-command-undo-counter 1)
  13224. (if (>= org-self-insert-command-undo-counter 20)
  13225. (setq org-self-insert-command-undo-counter 1)
  13226. (and (> org-self-insert-command-undo-counter 0)
  13227. buffer-undo-list
  13228. (not (cadr buffer-undo-list)) ; remove nil entry
  13229. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13230. (setq org-self-insert-command-undo-counter
  13231. (1+ org-self-insert-command-undo-counter)))))))
  13232. (defun org-fix-tags-on-the-fly ()
  13233. (when (and (equal (char-after (point-at-bol)) ?*)
  13234. (org-on-heading-p))
  13235. (org-align-tags-here org-tags-column)))
  13236. (defun org-delete-backward-char (N)
  13237. "Like `delete-backward-char', insert whitespace at field end in tables.
  13238. When deleting backwards, in tables this function will insert whitespace in
  13239. front of the next \"|\" separator, to keep the table aligned. The table will
  13240. still be marked for re-alignment if the field did fill the entire column,
  13241. because, in this case the deletion might narrow the column."
  13242. (interactive "p")
  13243. (if (and (org-table-p)
  13244. (eq N 1)
  13245. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13246. (looking-at ".*?|"))
  13247. (let ((pos (point))
  13248. (noalign (looking-at "[^|\n\r]* |"))
  13249. (c org-table-may-need-update))
  13250. (backward-delete-char N)
  13251. (skip-chars-forward "^|")
  13252. (insert " ")
  13253. (goto-char (1- pos))
  13254. ;; noalign: if there were two spaces at the end, this field
  13255. ;; does not determine the width of the column.
  13256. (if noalign (setq org-table-may-need-update c)))
  13257. (backward-delete-char N)
  13258. (org-fix-tags-on-the-fly)))
  13259. (defun org-delete-char (N)
  13260. "Like `delete-char', but insert whitespace at field end in tables.
  13261. When deleting characters, in tables this function will insert whitespace in
  13262. front of the next \"|\" separator, to keep the table aligned. The table will
  13263. still be marked for re-alignment if the field did fill the entire column,
  13264. because, in this case the deletion might narrow the column."
  13265. (interactive "p")
  13266. (if (and (org-table-p)
  13267. (not (bolp))
  13268. (not (= (char-after) ?|))
  13269. (eq N 1))
  13270. (if (looking-at ".*?|")
  13271. (let ((pos (point))
  13272. (noalign (looking-at "[^|\n\r]* |"))
  13273. (c org-table-may-need-update))
  13274. (replace-match (concat
  13275. (substring (match-string 0) 1 -1)
  13276. " |"))
  13277. (goto-char pos)
  13278. ;; noalign: if there were two spaces at the end, this field
  13279. ;; does not determine the width of the column.
  13280. (if noalign (setq org-table-may-need-update c)))
  13281. (delete-char N))
  13282. (delete-char N)
  13283. (org-fix-tags-on-the-fly)))
  13284. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13285. (put 'org-self-insert-command 'delete-selection t)
  13286. (put 'orgtbl-self-insert-command 'delete-selection t)
  13287. (put 'org-delete-char 'delete-selection 'supersede)
  13288. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13289. (put 'org-yank 'delete-selection 'yank)
  13290. ;; Make `flyspell-mode' delay after some commands
  13291. (put 'org-self-insert-command 'flyspell-delayed t)
  13292. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13293. (put 'org-delete-char 'flyspell-delayed t)
  13294. (put 'org-delete-backward-char 'flyspell-delayed t)
  13295. ;; Make pabbrev-mode expand after org-mode commands
  13296. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13297. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13298. ;; How to do this: Measure non-white length of current string
  13299. ;; If equal to column width, we should realign.
  13300. (defun org-remap (map &rest commands)
  13301. "In MAP, remap the functions given in COMMANDS.
  13302. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13303. (let (new old)
  13304. (while commands
  13305. (setq old (pop commands) new (pop commands))
  13306. (if (fboundp 'command-remapping)
  13307. (org-defkey map (vector 'remap old) new)
  13308. (substitute-key-definition old new map global-map)))))
  13309. (when (eq org-enable-table-editor 'optimized)
  13310. ;; If the user wants maximum table support, we need to hijack
  13311. ;; some standard editing functions
  13312. (org-remap org-mode-map
  13313. 'self-insert-command 'org-self-insert-command
  13314. 'delete-char 'org-delete-char
  13315. 'delete-backward-char 'org-delete-backward-char)
  13316. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13317. (defvar org-ctrl-c-ctrl-c-hook nil
  13318. "Hook for functions attaching themselves to `C-c C-c'.
  13319. This can be used to add additional functionality to the C-c C-c key which
  13320. executes context-dependent commands.
  13321. Each function will be called with no arguments. The function must check
  13322. if the context is appropriate for it to act. If yes, it should do its
  13323. thing and then return a non-nil value. If the context is wrong,
  13324. just do nothing and return nil.")
  13325. (defvar org-tab-first-hook nil
  13326. "Hook for functions to attach themselves to TAB.
  13327. See `org-ctrl-c-ctrl-c-hook' for more information.
  13328. This hook runs as the first action when TAB is pressed, even before
  13329. `org-cycle' messes around with the `outline-regexp' to cater for
  13330. inline tasks and plain list item folding.
  13331. If any function in this hook returns t, not other actions like table
  13332. field motion visibility cycling will be done.")
  13333. (defvar org-tab-after-check-for-table-hook nil
  13334. "Hook for functions to attach themselves to TAB.
  13335. See `org-ctrl-c-ctrl-c-hook' for more information.
  13336. This hook runs after it has been established that the cursor is not in a
  13337. table, but before checking if the cursor is in a headline or if global cycling
  13338. should be done.
  13339. If any function in this hook returns t, not other actions like visibility
  13340. cycling will be done.")
  13341. (defvar org-tab-after-check-for-cycling-hook nil
  13342. "Hook for functions to attach themselves to TAB.
  13343. See `org-ctrl-c-ctrl-c-hook' for more information.
  13344. This hook runs after it has been established that not table field motion and
  13345. not visibility should be done because of current context. This is probably
  13346. the place where a package like yasnippets can hook in.")
  13347. (defvar org-metaleft-hook nil
  13348. "Hook for functions attaching themselves to `M-left'.
  13349. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13350. (defvar org-metaright-hook nil
  13351. "Hook for functions attaching themselves to `M-right'.
  13352. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13353. (defvar org-metaup-hook nil
  13354. "Hook for functions attaching themselves to `M-up'.
  13355. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13356. (defvar org-metadown-hook nil
  13357. "Hook for functions attaching themselves to `M-down'.
  13358. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13359. (defvar org-shiftmetaleft-hook nil
  13360. "Hook for functions attaching themselves to `M-S-left'.
  13361. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13362. (defvar org-shiftmetaright-hook nil
  13363. "Hook for functions attaching themselves to `M-S-right'.
  13364. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13365. (defvar org-shiftmetaup-hook nil
  13366. "Hook for functions attaching themselves to `M-S-up'.
  13367. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13368. (defvar org-shiftmetadown-hook nil
  13369. "Hook for functions attaching themselves to `M-S-down'.
  13370. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13371. (defvar org-metareturn-hook nil
  13372. "Hook for functions attaching themselves to `M-RET'.
  13373. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13374. (defun org-modifier-cursor-error ()
  13375. "Throw an error, a modified cursor command was applied in wrong context."
  13376. (error "This command is active in special context like tables, headlines or items"))
  13377. (defun org-shiftselect-error ()
  13378. "Throw an error because Shift-Cursor command was applied in wrong context."
  13379. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13380. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13381. (error "This command works only in special context like headlines or timestamps.")))
  13382. (defun org-call-for-shift-select (cmd)
  13383. (let ((this-command-keys-shift-translated t))
  13384. (call-interactively cmd)))
  13385. (defun org-shifttab (&optional arg)
  13386. "Global visibility cycling or move to previous table field.
  13387. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13388. on context.
  13389. See the individual commands for more information."
  13390. (interactive "P")
  13391. (cond
  13392. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13393. ((integerp arg)
  13394. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13395. (message "Content view to level: %d" arg)
  13396. (org-content (prefix-numeric-value arg2))
  13397. (setq org-cycle-global-status 'overview)))
  13398. (t (call-interactively 'org-global-cycle))))
  13399. (defun org-shiftmetaleft ()
  13400. "Promote subtree or delete table column.
  13401. Calls `org-promote-subtree', `org-outdent-item',
  13402. or `org-table-delete-column', depending on context.
  13403. See the individual commands for more information."
  13404. (interactive)
  13405. (cond
  13406. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13407. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13408. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13409. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13410. (t (org-modifier-cursor-error))))
  13411. (defun org-shiftmetaright ()
  13412. "Demote subtree or insert table column.
  13413. Calls `org-demote-subtree', `org-indent-item',
  13414. or `org-table-insert-column', depending on context.
  13415. See the individual commands for more information."
  13416. (interactive)
  13417. (cond
  13418. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13419. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13420. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13421. ((org-at-item-p) (call-interactively 'org-indent-item))
  13422. (t (org-modifier-cursor-error))))
  13423. (defun org-shiftmetaup (&optional arg)
  13424. "Move subtree up or kill table row.
  13425. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13426. `org-move-item-up' depending on context. See the individual commands
  13427. for more information."
  13428. (interactive "P")
  13429. (cond
  13430. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13431. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13432. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13433. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13434. (t (org-modifier-cursor-error))))
  13435. (defun org-shiftmetadown (&optional arg)
  13436. "Move subtree down or insert table row.
  13437. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13438. `org-move-item-down', depending on context. See the individual
  13439. commands for more information."
  13440. (interactive "P")
  13441. (cond
  13442. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13443. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13444. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13445. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13446. (t (org-modifier-cursor-error))))
  13447. (defun org-metaleft (&optional arg)
  13448. "Promote heading or move table column to left.
  13449. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13450. With no specific context, calls the Emacs default `backward-word'.
  13451. See the individual commands for more information."
  13452. (interactive "P")
  13453. (cond
  13454. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13455. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13456. ((or (org-on-heading-p)
  13457. (and (org-region-active-p)
  13458. (save-excursion
  13459. (goto-char (region-beginning))
  13460. (org-on-heading-p))))
  13461. (call-interactively 'org-do-promote))
  13462. ((or (org-at-item-p)
  13463. (and (org-region-active-p)
  13464. (save-excursion
  13465. (goto-char (region-beginning))
  13466. (org-at-item-p))))
  13467. (call-interactively 'org-outdent-item))
  13468. (t (call-interactively 'backward-word))))
  13469. (defun org-metaright (&optional arg)
  13470. "Demote subtree or move table column to right.
  13471. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13472. With no specific context, calls the Emacs default `forward-word'.
  13473. See the individual commands for more information."
  13474. (interactive "P")
  13475. (cond
  13476. ((run-hook-with-args-until-success 'org-metaright-hook))
  13477. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13478. ((or (org-on-heading-p)
  13479. (and (org-region-active-p)
  13480. (save-excursion
  13481. (goto-char (region-beginning))
  13482. (org-on-heading-p))))
  13483. (call-interactively 'org-do-demote))
  13484. ((or (org-at-item-p)
  13485. (and (org-region-active-p)
  13486. (save-excursion
  13487. (goto-char (region-beginning))
  13488. (org-at-item-p))))
  13489. (call-interactively 'org-indent-item))
  13490. (t (call-interactively 'forward-word))))
  13491. (defun org-metaup (&optional arg)
  13492. "Move subtree up or move table row up.
  13493. Calls `org-move-subtree-up' or `org-table-move-row' or
  13494. `org-move-item-up', depending on context. See the individual commands
  13495. for more information."
  13496. (interactive "P")
  13497. (cond
  13498. ((run-hook-with-args-until-success 'org-metaup-hook))
  13499. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13500. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13501. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13502. (t (transpose-lines 1) (beginning-of-line -1))))
  13503. (defun org-metadown (&optional arg)
  13504. "Move subtree down or move table row down.
  13505. Calls `org-move-subtree-down' or `org-table-move-row' or
  13506. `org-move-item-down', depending on context. See the individual
  13507. commands for more information."
  13508. (interactive "P")
  13509. (cond
  13510. ((run-hook-with-args-until-success 'org-metadown-hook))
  13511. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13512. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13513. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13514. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13515. (defun org-shiftup (&optional arg)
  13516. "Increase item in timestamp or increase priority of current headline.
  13517. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13518. depending on context. See the individual commands for more information."
  13519. (interactive "P")
  13520. (cond
  13521. ((and org-support-shift-select (org-region-active-p))
  13522. (org-call-for-shift-select 'previous-line))
  13523. ((org-at-timestamp-p t)
  13524. (call-interactively (if org-edit-timestamp-down-means-later
  13525. 'org-timestamp-down 'org-timestamp-up)))
  13526. ((and (not (eq org-support-shift-select 'always))
  13527. org-enable-priority-commands
  13528. (org-on-heading-p))
  13529. (call-interactively 'org-priority-up))
  13530. ((and (not org-support-shift-select) (org-at-item-p))
  13531. (call-interactively 'org-previous-item))
  13532. ((org-clocktable-try-shift 'up arg))
  13533. (org-support-shift-select
  13534. (org-call-for-shift-select 'previous-line))
  13535. (t (org-shiftselect-error))))
  13536. (defun org-shiftdown (&optional arg)
  13537. "Decrease item in timestamp or decrease priority of current headline.
  13538. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13539. depending on context. See the individual commands for more information."
  13540. (interactive "P")
  13541. (cond
  13542. ((and org-support-shift-select (org-region-active-p))
  13543. (org-call-for-shift-select 'next-line))
  13544. ((org-at-timestamp-p t)
  13545. (call-interactively (if org-edit-timestamp-down-means-later
  13546. 'org-timestamp-up 'org-timestamp-down)))
  13547. ((and (not (eq org-support-shift-select 'always))
  13548. org-enable-priority-commands
  13549. (org-on-heading-p))
  13550. (call-interactively 'org-priority-down))
  13551. ((and (not org-support-shift-select) (org-at-item-p))
  13552. (call-interactively 'org-next-item))
  13553. ((org-clocktable-try-shift 'down arg))
  13554. (org-support-shift-select
  13555. (org-call-for-shift-select 'next-line))
  13556. (t (org-shiftselect-error))))
  13557. (defun org-shiftright (&optional arg)
  13558. "Cycle the thing at point or in the current line, depending on context.
  13559. Depending on context, this does one of the following:
  13560. - switch a timestamp at point one day into the future
  13561. - on a headline, switch to the next TODO keyword.
  13562. - on an item, switch entire list to the next bullet type
  13563. - on a property line, switch to the next allowed value
  13564. - on a clocktable definition line, move time block into the future"
  13565. (interactive "P")
  13566. (cond
  13567. ((and org-support-shift-select (org-region-active-p))
  13568. (org-call-for-shift-select 'forward-char))
  13569. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13570. ((and (not (eq org-support-shift-select 'always))
  13571. (org-on-heading-p))
  13572. (let ((org-inhibit-logging
  13573. (not org-treat-S-cursor-todo-selection-as-state-change))
  13574. (org-inhibit-blocking
  13575. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13576. (org-call-with-arg 'org-todo 'right)))
  13577. ((or (and org-support-shift-select
  13578. (not (eq org-support-shift-select 'always))
  13579. (org-at-item-bullet-p))
  13580. (and (not org-support-shift-select) (org-at-item-p)))
  13581. (org-call-with-arg 'org-cycle-list-bullet nil))
  13582. ((and (not (eq org-support-shift-select 'always))
  13583. (org-at-property-p))
  13584. (call-interactively 'org-property-next-allowed-value))
  13585. ((org-clocktable-try-shift 'right arg))
  13586. (org-support-shift-select
  13587. (org-call-for-shift-select 'forward-char))
  13588. (t (org-shiftselect-error))))
  13589. (defun org-shiftleft (&optional arg)
  13590. "Cycle the thing at point or in the current line, depending on context.
  13591. Depending on context, this does one of the following:
  13592. - switch a timestamp at point one day into the past
  13593. - on a headline, switch to the previous TODO keyword.
  13594. - on an item, switch entire list to the previous bullet type
  13595. - on a property line, switch to the previous allowed value
  13596. - on a clocktable definition line, move time block into the past"
  13597. (interactive "P")
  13598. (cond
  13599. ((and org-support-shift-select (org-region-active-p))
  13600. (org-call-for-shift-select 'backward-char))
  13601. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13602. ((and (not (eq org-support-shift-select 'always))
  13603. (org-on-heading-p))
  13604. (let ((org-inhibit-logging
  13605. (not org-treat-S-cursor-todo-selection-as-state-change))
  13606. (org-inhibit-blocking
  13607. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13608. (org-call-with-arg 'org-todo 'left)))
  13609. ((or (and org-support-shift-select
  13610. (not (eq org-support-shift-select 'always))
  13611. (org-at-item-bullet-p))
  13612. (and (not org-support-shift-select) (org-at-item-p)))
  13613. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13614. ((and (not (eq org-support-shift-select 'always))
  13615. (org-at-property-p))
  13616. (call-interactively 'org-property-previous-allowed-value))
  13617. ((org-clocktable-try-shift 'left arg))
  13618. (org-support-shift-select
  13619. (org-call-for-shift-select 'backward-char))
  13620. (t (org-shiftselect-error))))
  13621. (defun org-shiftcontrolright ()
  13622. "Switch to next TODO set."
  13623. (interactive)
  13624. (cond
  13625. ((and org-support-shift-select (org-region-active-p))
  13626. (org-call-for-shift-select 'forward-word))
  13627. ((and (not (eq org-support-shift-select 'always))
  13628. (org-on-heading-p))
  13629. (org-call-with-arg 'org-todo 'nextset))
  13630. (org-support-shift-select
  13631. (org-call-for-shift-select 'forward-word))
  13632. (t (org-shiftselect-error))))
  13633. (defun org-shiftcontrolleft ()
  13634. "Switch to previous TODO set."
  13635. (interactive)
  13636. (cond
  13637. ((and org-support-shift-select (org-region-active-p))
  13638. (org-call-for-shift-select 'backward-word))
  13639. ((and (not (eq org-support-shift-select 'always))
  13640. (org-on-heading-p))
  13641. (org-call-with-arg 'org-todo 'previousset))
  13642. (org-support-shift-select
  13643. (org-call-for-shift-select 'backward-word))
  13644. (t (org-shiftselect-error))))
  13645. (defun org-ctrl-c-ret ()
  13646. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13647. (interactive)
  13648. (cond
  13649. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13650. (t (call-interactively 'org-insert-heading))))
  13651. (defun org-copy-special ()
  13652. "Copy region in table or copy current subtree.
  13653. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13654. See the individual commands for more information."
  13655. (interactive)
  13656. (call-interactively
  13657. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13658. (defun org-cut-special ()
  13659. "Cut region in table or cut current subtree.
  13660. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13661. See the individual commands for more information."
  13662. (interactive)
  13663. (call-interactively
  13664. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13665. (defun org-paste-special (arg)
  13666. "Paste rectangular region into table, or past subtree relative to level.
  13667. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13668. See the individual commands for more information."
  13669. (interactive "P")
  13670. (if (org-at-table-p)
  13671. (org-table-paste-rectangle)
  13672. (org-paste-subtree arg)))
  13673. (defun org-edit-special ()
  13674. "Call a special editor for the stuff at point.
  13675. When at a table, call the formula editor with `org-table-edit-formulas'.
  13676. When at the first line of an src example, call `org-edit-src-code'.
  13677. When in an #+include line, visit the include file. Otherwise call
  13678. `ffap' to visit the file at point."
  13679. (interactive)
  13680. (cond
  13681. ((org-at-table-p)
  13682. (call-interactively 'org-table-edit-formulas))
  13683. ((save-excursion
  13684. (beginning-of-line 1)
  13685. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13686. (find-file (org-trim (match-string 1))))
  13687. ((org-edit-src-code))
  13688. ((org-edit-fixed-width-region))
  13689. (t (call-interactively 'ffap))))
  13690. (defun org-ctrl-c-ctrl-c (&optional arg)
  13691. "Set tags in headline, or update according to changed information at point.
  13692. This command does many different things, depending on context:
  13693. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13694. this is what we do.
  13695. - If the cursor is on a statistics cookie, update it.
  13696. - If the cursor is in a headline, prompt for tags and insert them
  13697. into the current line, aligned to `org-tags-column'. When called
  13698. with prefix arg, realign all tags in the current buffer.
  13699. - If the cursor is in one of the special #+KEYWORD lines, this
  13700. triggers scanning the buffer for these lines and updating the
  13701. information.
  13702. - If the cursor is inside a table, realign the table. This command
  13703. works even if the automatic table editor has been turned off.
  13704. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13705. the entire table.
  13706. - If the cursor is at a footnote reference or definition, jump to
  13707. the corresponding definition or references, respectively.
  13708. - If the cursor is a the beginning of a dynamic block, update it.
  13709. - If the cursor is inside a table created by the table.el package,
  13710. activate that table.
  13711. - If the current buffer is a remember buffer, close note and file
  13712. it. A prefix argument of 1 files to the default location
  13713. without further interaction. A prefix argument of 2 files to
  13714. the currently clocking task.
  13715. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13716. links in this buffer.
  13717. - If the cursor is on a numbered item in a plain list, renumber the
  13718. ordered list.
  13719. - If the cursor is on a checkbox, toggle it."
  13720. (interactive "P")
  13721. (let ((org-enable-table-editor t))
  13722. (cond
  13723. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13724. org-occur-highlights
  13725. org-latex-fragment-image-overlays)
  13726. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13727. (org-remove-occur-highlights)
  13728. (org-remove-latex-fragment-image-overlays)
  13729. (message "Temporary highlights/overlays removed from current buffer"))
  13730. ((and (local-variable-p 'org-finish-function (current-buffer))
  13731. (fboundp org-finish-function))
  13732. (funcall org-finish-function))
  13733. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13734. ((org-at-property-p)
  13735. (call-interactively 'org-property-action))
  13736. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13737. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  13738. (or (org-on-heading-p) (org-at-item-p)))
  13739. (call-interactively 'org-update-statistics-cookies))
  13740. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13741. ((org-at-table.el-p)
  13742. (require 'table)
  13743. (beginning-of-line 1)
  13744. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13745. (call-interactively 'table-recognize-table))
  13746. ((org-at-table-p)
  13747. (org-table-maybe-eval-formula)
  13748. (if arg
  13749. (call-interactively 'org-table-recalculate)
  13750. (org-table-maybe-recalculate-line))
  13751. (call-interactively 'org-table-align))
  13752. ((or (org-footnote-at-reference-p)
  13753. (org-footnote-at-definition-p))
  13754. (call-interactively 'org-footnote-action))
  13755. ((org-at-item-checkbox-p)
  13756. (call-interactively 'org-toggle-checkbox))
  13757. ((org-at-item-p)
  13758. (if arg
  13759. (call-interactively 'org-toggle-checkbox)
  13760. (call-interactively 'org-maybe-renumber-ordered-list)))
  13761. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13762. ;; Dynamic block
  13763. (beginning-of-line 1)
  13764. (save-excursion (org-update-dblock)))
  13765. ((save-excursion
  13766. (beginning-of-line 1)
  13767. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13768. (cond
  13769. ((equal (match-string 1) "TBLFM")
  13770. ;; Recalculate the table before this line
  13771. (save-excursion
  13772. (beginning-of-line 1)
  13773. (skip-chars-backward " \r\n\t")
  13774. (if (org-at-table-p)
  13775. (org-call-with-arg 'org-table-recalculate t))))
  13776. (t
  13777. ; (org-set-regexps-and-options)
  13778. ; (org-restart-font-lock)
  13779. (let ((org-inhibit-startup t)) (org-mode-restart))
  13780. (message "Local setup has been refreshed"))))
  13781. ((org-clock-update-time-maybe))
  13782. (t (error "C-c C-c can do nothing useful at this location.")))))
  13783. (defun org-mode-restart ()
  13784. "Restart Org-mode, to scan again for special lines.
  13785. Also updates the keyword regular expressions."
  13786. (interactive)
  13787. (org-mode)
  13788. (message "Org-mode restarted"))
  13789. (defun org-kill-note-or-show-branches ()
  13790. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13791. (interactive)
  13792. (if (not org-finish-function)
  13793. (call-interactively 'show-branches)
  13794. (let ((org-note-abort t))
  13795. (funcall org-finish-function))))
  13796. (defun org-return (&optional indent)
  13797. "Goto next table row or insert a newline.
  13798. Calls `org-table-next-row' or `newline', depending on context.
  13799. See the individual commands for more information."
  13800. (interactive)
  13801. (cond
  13802. ((bobp) (if indent (newline-and-indent) (newline)))
  13803. ((org-at-table-p)
  13804. (org-table-justify-field-maybe)
  13805. (call-interactively 'org-table-next-row))
  13806. ((and org-return-follows-link
  13807. (eq (get-text-property (point) 'face) 'org-link))
  13808. (call-interactively 'org-open-at-point))
  13809. ((and (org-at-heading-p)
  13810. (looking-at
  13811. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13812. (org-show-entry)
  13813. (end-of-line 1)
  13814. (newline))
  13815. (t (if indent (newline-and-indent) (newline)))))
  13816. (defun org-return-indent ()
  13817. "Goto next table row or insert a newline and indent.
  13818. Calls `org-table-next-row' or `newline-and-indent', depending on
  13819. context. See the individual commands for more information."
  13820. (interactive)
  13821. (org-return t))
  13822. (defun org-ctrl-c-star ()
  13823. "Compute table, or change heading status of lines.
  13824. Calls `org-table-recalculate' or `org-toggle-heading',
  13825. depending on context."
  13826. (interactive)
  13827. (cond
  13828. ((org-at-table-p)
  13829. (call-interactively 'org-table-recalculate))
  13830. (t
  13831. ;; Convert all lines in region to list items
  13832. (call-interactively 'org-toggle-heading))))
  13833. (defun org-ctrl-c-minus ()
  13834. "Insert separator line in table or modify bullet status of line.
  13835. Also turns a plain line or a region of lines into list items.
  13836. Calls `org-table-insert-hline', `org-toggle-item', or
  13837. `org-cycle-list-bullet', depending on context."
  13838. (interactive)
  13839. (cond
  13840. ((org-at-table-p)
  13841. (call-interactively 'org-table-insert-hline))
  13842. ((org-region-active-p)
  13843. (call-interactively 'org-toggle-item))
  13844. ((org-in-item-p)
  13845. (call-interactively 'org-cycle-list-bullet))
  13846. (t
  13847. (call-interactively 'org-toggle-item))))
  13848. (defun org-toggle-item ()
  13849. "Convert headings or normal lines to items, items to normal lines.
  13850. If there is no active region, only the current line is considered.
  13851. If the first line in the region is a headline, convert all headlines to items.
  13852. If the first line in the region is an item, convert all items to normal lines.
  13853. If the first line is normal text, add an item bullet to each line."
  13854. (interactive)
  13855. (let (l2 l beg end)
  13856. (if (org-region-active-p)
  13857. (setq beg (region-beginning) end (region-end))
  13858. (setq beg (point-at-bol)
  13859. end (min (1+ (point-at-eol)) (point-max))))
  13860. (save-excursion
  13861. (goto-char end)
  13862. (setq l2 (org-current-line))
  13863. (goto-char beg)
  13864. (beginning-of-line 1)
  13865. (setq l (1- (org-current-line)))
  13866. (if (org-at-item-p)
  13867. ;; We already have items, de-itemize
  13868. (while (< (setq l (1+ l)) l2)
  13869. (when (org-at-item-p)
  13870. (goto-char (match-beginning 2))
  13871. (delete-region (match-beginning 2) (match-end 2))
  13872. (and (looking-at "[ \t]+") (replace-match "")))
  13873. (beginning-of-line 2))
  13874. (if (org-on-heading-p)
  13875. ;; Headings, convert to items
  13876. (while (< (setq l (1+ l)) l2)
  13877. (if (looking-at org-outline-regexp)
  13878. (replace-match "- " t t))
  13879. (beginning-of-line 2))
  13880. ;; normal lines, turn them into items
  13881. (while (< (setq l (1+ l)) l2)
  13882. (unless (org-at-item-p)
  13883. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13884. (replace-match "\\1- \\2")))
  13885. (beginning-of-line 2)))))))
  13886. (defun org-toggle-heading (&optional nstars)
  13887. "Convert headings to normal text, or items or text to headings.
  13888. If there is no active region, only the current line is considered.
  13889. If the first line is a heading, remove the stars from all headlines
  13890. in the region.
  13891. If the first line is a plain list item, turn all plain list items
  13892. into headings.
  13893. If the first line is a normal line, turn each and every line in the
  13894. region into a heading.
  13895. When converting a line into a heading, the number of stars is chosen
  13896. such that the lines become children of the current entry. However,
  13897. when a prefix argument is given, its value determines the number of
  13898. stars to add."
  13899. (interactive "P")
  13900. (let (l2 l itemp beg end)
  13901. (if (org-region-active-p)
  13902. (setq beg (region-beginning) end (region-end))
  13903. (setq beg (point-at-bol)
  13904. end (min (1+ (point-at-eol)) (point-max))))
  13905. (save-excursion
  13906. (goto-char end)
  13907. (setq l2 (org-current-line))
  13908. (goto-char beg)
  13909. (beginning-of-line 1)
  13910. (setq l (1- (org-current-line)))
  13911. (if (org-on-heading-p)
  13912. ;; We already have headlines, de-star them
  13913. (while (< (setq l (1+ l)) l2)
  13914. (when (org-on-heading-p t)
  13915. (and (looking-at outline-regexp) (replace-match "")))
  13916. (beginning-of-line 2))
  13917. (setq itemp (org-at-item-p))
  13918. (let* ((stars
  13919. (if nstars
  13920. (make-string (prefix-numeric-value current-prefix-arg)
  13921. ?*)
  13922. (save-excursion
  13923. (if (re-search-backward org-complex-heading-regexp nil t)
  13924. (match-string 1) ""))))
  13925. (add-stars (cond (nstars "")
  13926. ((equal stars "") "*")
  13927. (org-odd-levels-only "**")
  13928. (t "*")))
  13929. (rpl (concat stars add-stars " ")))
  13930. (while (< (setq l (1+ l)) l2)
  13931. (if itemp
  13932. (and (org-at-item-p) (replace-match rpl t t))
  13933. (unless (org-on-heading-p)
  13934. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13935. (replace-match (concat rpl (match-string 2))))))
  13936. (beginning-of-line 2)))))))
  13937. (defun org-meta-return (&optional arg)
  13938. "Insert a new heading or wrap a region in a table.
  13939. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13940. See the individual commands for more information."
  13941. (interactive "P")
  13942. (cond
  13943. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13944. ((org-at-table-p)
  13945. (call-interactively 'org-table-wrap-region))
  13946. (t (call-interactively 'org-insert-heading))))
  13947. ;;; Menu entries
  13948. ;; Define the Org-mode menus
  13949. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13950. '("Tbl"
  13951. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13952. ["Next Field" org-cycle (org-at-table-p)]
  13953. ["Previous Field" org-shifttab (org-at-table-p)]
  13954. ["Next Row" org-return (org-at-table-p)]
  13955. "--"
  13956. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13957. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13958. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13959. "--"
  13960. ("Column"
  13961. ["Move Column Left" org-metaleft (org-at-table-p)]
  13962. ["Move Column Right" org-metaright (org-at-table-p)]
  13963. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13964. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13965. ("Row"
  13966. ["Move Row Up" org-metaup (org-at-table-p)]
  13967. ["Move Row Down" org-metadown (org-at-table-p)]
  13968. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13969. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13970. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13971. "--"
  13972. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13973. ("Rectangle"
  13974. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13975. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13976. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13977. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13978. "--"
  13979. ("Calculate"
  13980. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13981. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13982. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13983. "--"
  13984. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13985. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13986. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13987. "--"
  13988. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13989. "--"
  13990. ["Sum Column/Rectangle" org-table-sum
  13991. (or (org-at-table-p) (org-region-active-p))]
  13992. ["Which Column?" org-table-current-column (org-at-table-p)])
  13993. ["Debug Formulas"
  13994. org-table-toggle-formula-debugger
  13995. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13996. ["Show Col/Row Numbers"
  13997. org-table-toggle-coordinate-overlays
  13998. :style toggle
  13999. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14000. "--"
  14001. ["Create" org-table-create (and (not (org-at-table-p))
  14002. org-enable-table-editor)]
  14003. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14004. ["Import from File" org-table-import (not (org-at-table-p))]
  14005. ["Export to File" org-table-export (org-at-table-p)]
  14006. "--"
  14007. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14008. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14009. '("Org"
  14010. ("Show/Hide"
  14011. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14012. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14013. ["Sparse Tree..." org-sparse-tree t]
  14014. ["Reveal Context" org-reveal t]
  14015. ["Show All" show-all t]
  14016. "--"
  14017. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14018. "--"
  14019. ["New Heading" org-insert-heading t]
  14020. ("Navigate Headings"
  14021. ["Up" outline-up-heading t]
  14022. ["Next" outline-next-visible-heading t]
  14023. ["Previous" outline-previous-visible-heading t]
  14024. ["Next Same Level" outline-forward-same-level t]
  14025. ["Previous Same Level" outline-backward-same-level t]
  14026. "--"
  14027. ["Jump" org-goto t])
  14028. ("Edit Structure"
  14029. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14030. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14031. "--"
  14032. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14033. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14034. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14035. "--"
  14036. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14037. "--"
  14038. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14039. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14040. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14041. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14042. "--"
  14043. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14044. "--"
  14045. ["Convert to odd levels" org-convert-to-odd-levels t]
  14046. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14047. ("Editing"
  14048. ["Emphasis..." org-emphasize t]
  14049. ["Edit Source Example" org-edit-special t]
  14050. "--"
  14051. ["Footnote new/jump" org-footnote-action t]
  14052. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14053. ("Archive"
  14054. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14055. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  14056. ; :active t :keys "C-u C-c C-x C-a"]
  14057. ["Sparse trees open ARCHIVE trees"
  14058. (setq org-sparse-tree-open-archived-trees
  14059. (not org-sparse-tree-open-archived-trees))
  14060. :style toggle :selected org-sparse-tree-open-archived-trees]
  14061. ["Cycling opens ARCHIVE trees"
  14062. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  14063. :style toggle :selected org-cycle-open-archived-trees]
  14064. "--"
  14065. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  14066. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  14067. ; ["Check and Move Children" (org-archive-subtree '(4))
  14068. ; :active t :keys "C-u C-c C-x C-s"]
  14069. )
  14070. "--"
  14071. ("Hyperlinks"
  14072. ["Store Link (Global)" org-store-link t]
  14073. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14074. ["Insert Link" org-insert-link t]
  14075. ["Follow Link" org-open-at-point t]
  14076. "--"
  14077. ["Next link" org-next-link t]
  14078. ["Previous link" org-previous-link t]
  14079. "--"
  14080. ["Descriptive Links"
  14081. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14082. :style radio
  14083. :selected (member '(org-link) buffer-invisibility-spec)]
  14084. ["Literal Links"
  14085. (progn
  14086. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14087. :style radio
  14088. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14089. "--"
  14090. ("TODO Lists"
  14091. ["TODO/DONE/-" org-todo t]
  14092. ("Select keyword"
  14093. ["Next keyword" org-shiftright (org-on-heading-p)]
  14094. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14095. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14096. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14097. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14098. ["Show TODO Tree" org-show-todo-tree t]
  14099. ["Global TODO list" org-todo-list t]
  14100. "--"
  14101. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14102. :selected org-enforce-todo-dependencies :style toggle :active t]
  14103. "Settings for tree at point"
  14104. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14105. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14106. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14107. ["Do Children parallel" org-toggle-ordered-property :style radio
  14108. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14109. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14110. "--"
  14111. ["Set Priority" org-priority t]
  14112. ["Priority Up" org-shiftup t]
  14113. ["Priority Down" org-shiftdown t]
  14114. "--"
  14115. ["Get news from all feeds" org-feed-update-all t]
  14116. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14117. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14118. ("TAGS and Properties"
  14119. ["Set Tags" org-set-tags-command t]
  14120. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14121. "--"
  14122. ["Set property" org-set-property t]
  14123. ["Column view of properties" org-columns t]
  14124. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14125. ("Dates and Scheduling"
  14126. ["Timestamp" org-time-stamp t]
  14127. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14128. ("Change Date"
  14129. ["1 Day Later" org-shiftright t]
  14130. ["1 Day Earlier" org-shiftleft t]
  14131. ["1 ... Later" org-shiftup t]
  14132. ["1 ... Earlier" org-shiftdown t])
  14133. ["Compute Time Range" org-evaluate-time-range t]
  14134. ["Schedule Item" org-schedule t]
  14135. ["Deadline" org-deadline t]
  14136. "--"
  14137. ["Custom time format" org-toggle-time-stamp-overlays
  14138. :style radio :selected org-display-custom-times]
  14139. "--"
  14140. ["Goto Calendar" org-goto-calendar t]
  14141. ["Date from Calendar" org-date-from-calendar t]
  14142. "--"
  14143. ["Start/Restart Timer" org-timer-start t]
  14144. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14145. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14146. ["Insert Timer String" org-timer t]
  14147. ["Insert Timer Item" org-timer-item t])
  14148. ("Logging work"
  14149. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14150. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14151. ["Clock out" org-clock-out t]
  14152. ["Clock cancel" org-clock-cancel t]
  14153. "--"
  14154. ["Mark as default task" org-clock-mark-default-task t]
  14155. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14156. ["Goto running clock" org-clock-goto t]
  14157. "--"
  14158. ["Display times" org-clock-display t]
  14159. ["Create clock table" org-clock-report t]
  14160. "--"
  14161. ["Record DONE time"
  14162. (progn (setq org-log-done (not org-log-done))
  14163. (message "Switching to %s will %s record a timestamp"
  14164. (car org-done-keywords)
  14165. (if org-log-done "automatically" "not")))
  14166. :style toggle :selected org-log-done])
  14167. "--"
  14168. ["Agenda Command..." org-agenda t]
  14169. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14170. ("File List for Agenda")
  14171. ("Special views current file"
  14172. ["TODO Tree" org-show-todo-tree t]
  14173. ["Check Deadlines" org-check-deadlines t]
  14174. ["Timeline" org-timeline t]
  14175. ["Tags/Property tree" org-match-sparse-tree t])
  14176. "--"
  14177. ["Export/Publish..." org-export t]
  14178. ("LaTeX"
  14179. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14180. :selected org-cdlatex-mode]
  14181. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14182. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14183. ["Modify math symbol" org-cdlatex-math-modify
  14184. (org-inside-LaTeX-fragment-p)]
  14185. ["Insert citation" org-reftex-citation t]
  14186. "--"
  14187. ["Export LaTeX fragments as images"
  14188. (if (featurep 'org-exp)
  14189. (setq org-export-with-LaTeX-fragments
  14190. (not org-export-with-LaTeX-fragments))
  14191. (require 'org-exp))
  14192. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14193. org-export-with-LaTeX-fragments)])
  14194. "--"
  14195. ("Documentation"
  14196. ["Show Version" org-version t]
  14197. ["Info Documentation" org-info t])
  14198. ("Customize"
  14199. ["Browse Org Group" org-customize t]
  14200. "--"
  14201. ["Expand This Menu" org-create-customize-menu
  14202. (fboundp 'customize-menu-create)])
  14203. ["Send bug report" org-submit-bug-report t]
  14204. "--"
  14205. ("Refresh/Reload"
  14206. ["Refresh setup current buffer" org-mode-restart t]
  14207. ["Reload Org (after update)" org-reload t]
  14208. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14209. ))
  14210. (defun org-info (&optional node)
  14211. "Read documentation for Org-mode in the info system.
  14212. With optional NODE, go directly to that node."
  14213. (interactive)
  14214. (info (format "(org)%s" (or node ""))))
  14215. ;;;###autoload
  14216. (defun org-submit-bug-report ()
  14217. "Submit a bug report on Org-mode via mail.
  14218. Don't hesitate to report any problems or inaccurate documentation.
  14219. If you don't have setup sending mail from (X)Emacs, please copy the
  14220. output buffer into your mail program, as it gives us important
  14221. information about your Org-mode version and configuration."
  14222. (interactive)
  14223. (require 'reporter)
  14224. (org-load-modules-maybe)
  14225. (org-require-autoloaded-modules)
  14226. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14227. (reporter-submit-bug-report
  14228. "emacs-orgmode@gnu.org"
  14229. (org-version)
  14230. (let (list)
  14231. (save-window-excursion
  14232. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14233. (delete-other-windows)
  14234. (erase-buffer)
  14235. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14236. We would like to add your full Org-mode and Outline configuration to the
  14237. bug report. This greatly simplifies the work of the maintainer and
  14238. other experts on the mailing list.
  14239. HOWEVER, some variables you have customized may contain private
  14240. information. The names of customers, colleagues, or friends, might
  14241. appear in the form of file names, tags, todo states, or search strings.
  14242. If you answer yes to the prompt, you might want to check and remove
  14243. such private information before sending the email.")
  14244. (add-text-properties (point-min) (point-max) '(face org-warning))
  14245. (when (yes-or-no-p "Include your Org-mode configuration ")
  14246. (mapatoms
  14247. (lambda (v)
  14248. (and (boundp v)
  14249. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14250. (or (and (symbol-value v)
  14251. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14252. (and
  14253. (get v 'custom-type) (get v 'standard-value)
  14254. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14255. (push v list)))))
  14256. (kill-buffer (get-buffer "*Warn about privacy*"))
  14257. list))
  14258. nil nil
  14259. "Remember to cover the basics, that is, what you expected to happen and
  14260. what in fact did happen. You don't know how to make a good report? See
  14261. http://orgmode.org/manual/Feedback.html#Feedback
  14262. Your bug report will be posted to the Org-mode mailing list.
  14263. ------------------------------------------------------------------------")))
  14264. (defun org-install-agenda-files-menu ()
  14265. (let ((bl (buffer-list)))
  14266. (save-excursion
  14267. (while bl
  14268. (set-buffer (pop bl))
  14269. (if (org-mode-p) (setq bl nil)))
  14270. (when (org-mode-p)
  14271. (easy-menu-change
  14272. '("Org") "File List for Agenda"
  14273. (append
  14274. (list
  14275. ["Edit File List" (org-edit-agenda-file-list) t]
  14276. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14277. ["Remove Current File from List" org-remove-file t]
  14278. ["Cycle through agenda files" org-cycle-agenda-files t]
  14279. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14280. "--")
  14281. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14282. ;;;; Documentation
  14283. ;;;###autoload
  14284. (defun org-require-autoloaded-modules ()
  14285. (interactive)
  14286. (mapc 'require
  14287. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14288. org-docbook org-exp org-html org-icalendar
  14289. org-id org-latex
  14290. org-publish org-remember org-table
  14291. org-timer org-xoxo)))
  14292. ;;;###autoload
  14293. (defun org-reload (&optional uncompiled)
  14294. "Reload all org lisp files.
  14295. With prefix arg UNCOMPILED, load the uncompiled versions."
  14296. (interactive "P")
  14297. (require 'find-func)
  14298. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14299. (dir-org (file-name-directory (org-find-library-name "org")))
  14300. (dir-org-contrib (ignore-errors
  14301. (file-name-directory
  14302. (org-find-library-name "org-contribdir"))))
  14303. (files
  14304. (append (directory-files dir-org t file-re)
  14305. (and dir-org-contrib
  14306. (directory-files dir-org-contrib t file-re))))
  14307. (remove-re (concat (if (featurep 'xemacs)
  14308. "org-colview" "org-colview-xemacs")
  14309. "\\'")))
  14310. (setq files (mapcar 'file-name-sans-extension files))
  14311. (setq files (mapcar
  14312. (lambda (x) (if (string-match remove-re x) nil x))
  14313. files))
  14314. (setq files (delq nil files))
  14315. (mapc
  14316. (lambda (f)
  14317. (when (featurep (intern (file-name-nondirectory f)))
  14318. (if (and (not uncompiled)
  14319. (file-exists-p (concat f ".elc")))
  14320. (load (concat f ".elc") nil nil t)
  14321. (load (concat f ".el") nil nil t))))
  14322. files))
  14323. (org-version))
  14324. ;;;###autoload
  14325. (defun org-customize ()
  14326. "Call the customize function with org as argument."
  14327. (interactive)
  14328. (org-load-modules-maybe)
  14329. (org-require-autoloaded-modules)
  14330. (customize-browse 'org))
  14331. (defun org-create-customize-menu ()
  14332. "Create a full customization menu for Org-mode, insert it into the menu."
  14333. (interactive)
  14334. (org-load-modules-maybe)
  14335. (org-require-autoloaded-modules)
  14336. (if (fboundp 'customize-menu-create)
  14337. (progn
  14338. (easy-menu-change
  14339. '("Org") "Customize"
  14340. `(["Browse Org group" org-customize t]
  14341. "--"
  14342. ,(customize-menu-create 'org)
  14343. ["Set" Custom-set t]
  14344. ["Save" Custom-save t]
  14345. ["Reset to Current" Custom-reset-current t]
  14346. ["Reset to Saved" Custom-reset-saved t]
  14347. ["Reset to Standard Settings" Custom-reset-standard t]))
  14348. (message "\"Org\"-menu now contains full customization menu"))
  14349. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14350. ;;;; Miscellaneous stuff
  14351. ;;; Generally useful functions
  14352. (defun org-find-text-property-in-string (prop s)
  14353. "Return the first non-nil value of property PROP in string S."
  14354. (or (get-text-property 0 prop s)
  14355. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14356. prop s)))
  14357. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14358. "Display the given MESSAGE as a warning."
  14359. (if (fboundp 'display-warning)
  14360. (display-warning 'org message
  14361. (if (featurep 'xemacs)
  14362. 'warning
  14363. :warning))
  14364. (let ((buf (get-buffer-create "*Org warnings*")))
  14365. (with-current-buffer buf
  14366. (goto-char (point-max))
  14367. (insert "Warning (Org): " message)
  14368. (unless (bolp)
  14369. (newline)))
  14370. (display-buffer buf)
  14371. (sit-for 0))))
  14372. (defun org-in-commented-line ()
  14373. "Is point in a line starting with `#'?"
  14374. (equal (char-after (point-at-bol)) ?#))
  14375. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14376. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14377. (if (and marker (marker-buffer marker)
  14378. (buffer-live-p (marker-buffer marker)))
  14379. (progn
  14380. (switch-to-buffer (marker-buffer marker))
  14381. (if (or (> marker (point-max)) (< marker (point-min)))
  14382. (widen))
  14383. (goto-char marker)
  14384. (org-show-context 'org-goto))
  14385. (if bookmark
  14386. (bookmark-jump bookmark)
  14387. (error "Cannot find location"))))
  14388. (defun org-quote-csv-field (s)
  14389. "Quote field for inclusion in CSV material."
  14390. (if (string-match "[\",]" s)
  14391. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14392. s))
  14393. (defun org-plist-delete (plist property)
  14394. "Delete PROPERTY from PLIST.
  14395. This is in contrast to merely setting it to 0."
  14396. (let (p)
  14397. (while plist
  14398. (if (not (eq property (car plist)))
  14399. (setq p (plist-put p (car plist) (nth 1 plist))))
  14400. (setq plist (cddr plist)))
  14401. p))
  14402. (defun org-force-self-insert (N)
  14403. "Needed to enforce self-insert under remapping."
  14404. (interactive "p")
  14405. (self-insert-command N))
  14406. (defun org-string-width (s)
  14407. "Compute width of string, ignoring invisible characters.
  14408. This ignores character with invisibility property `org-link', and also
  14409. characters with property `org-cwidth', because these will become invisible
  14410. upon the next fontification round."
  14411. (let (b l)
  14412. (when (or (eq t buffer-invisibility-spec)
  14413. (assq 'org-link buffer-invisibility-spec))
  14414. (while (setq b (text-property-any 0 (length s)
  14415. 'invisible 'org-link s))
  14416. (setq s (concat (substring s 0 b)
  14417. (substring s (or (next-single-property-change
  14418. b 'invisible s) (length s)))))))
  14419. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14420. (setq s (concat (substring s 0 b)
  14421. (substring s (or (next-single-property-change
  14422. b 'org-cwidth s) (length s))))))
  14423. (setq l (string-width s) b -1)
  14424. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14425. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14426. l))
  14427. (defun org-get-indentation (&optional line)
  14428. "Get the indentation of the current line, interpreting tabs.
  14429. When LINE is given, assume it represents a line and compute its indentation."
  14430. (if line
  14431. (if (string-match "^ *" (org-remove-tabs line))
  14432. (match-end 0))
  14433. (save-excursion
  14434. (beginning-of-line 1)
  14435. (skip-chars-forward " \t")
  14436. (current-column))))
  14437. (defun org-remove-tabs (s &optional width)
  14438. "Replace tabulators in S with spaces.
  14439. Assumes that s is a single line, starting in column 0."
  14440. (setq width (or width tab-width))
  14441. (while (string-match "\t" s)
  14442. (setq s (replace-match
  14443. (make-string
  14444. (- (* width (/ (+ (match-beginning 0) width) width))
  14445. (match-beginning 0)) ?\ )
  14446. t t s)))
  14447. s)
  14448. (defun org-fix-indentation (line ind)
  14449. "Fix indentation in LINE.
  14450. IND is a cons cell with target and minimum indentation.
  14451. If the current indentation in LINE is smaller than the minimum,
  14452. leave it alone. If it is larger than ind, set it to the target."
  14453. (let* ((l (org-remove-tabs line))
  14454. (i (org-get-indentation l))
  14455. (i1 (car ind)) (i2 (cdr ind)))
  14456. (if (>= i i2) (setq l (substring line i2)))
  14457. (if (> i1 0)
  14458. (concat (make-string i1 ?\ ) l)
  14459. l)))
  14460. (defun org-remove-indentation (code &optional n)
  14461. "Remove the maximum common indentation from the lines in CODE.
  14462. N may optionally be the number of spaces to remove."
  14463. (with-temp-buffer
  14464. (insert code)
  14465. (org-do-remove-indentation n)
  14466. (buffer-string)))
  14467. (defun org-do-remove-indentation (&optional n)
  14468. "Remove the maximum common indentation from the buffer."
  14469. (untabify (point-min) (point-max))
  14470. (let ((min 10000) re)
  14471. (if n
  14472. (setq min n)
  14473. (goto-char (point-min))
  14474. (while (re-search-forward "^ *[^ \n]" nil t)
  14475. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14476. (unless (or (= min 0) (= min 10000))
  14477. (setq re (format "^ \\{%d\\}" min))
  14478. (goto-char (point-min))
  14479. (while (re-search-forward re nil t)
  14480. (replace-match "")
  14481. (end-of-line 1))
  14482. min)))
  14483. (defun org-base-buffer (buffer)
  14484. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14485. (if (not buffer)
  14486. buffer
  14487. (or (buffer-base-buffer buffer)
  14488. buffer)))
  14489. (defun org-trim (s)
  14490. "Remove whitespace at beginning and end of string."
  14491. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14492. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14493. s)
  14494. (defun org-wrap (string &optional width lines)
  14495. "Wrap string to either a number of lines, or a width in characters.
  14496. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14497. that costs. If there is a word longer than WIDTH, the text is actually
  14498. wrapped to the length of that word.
  14499. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14500. many lines, whatever width that takes.
  14501. The return value is a list of lines, without newlines at the end."
  14502. (let* ((words (org-split-string string "[ \t\n]+"))
  14503. (maxword (apply 'max (mapcar 'org-string-width words)))
  14504. w ll)
  14505. (cond (width
  14506. (org-do-wrap words (max maxword width)))
  14507. (lines
  14508. (setq w maxword)
  14509. (setq ll (org-do-wrap words maxword))
  14510. (if (<= (length ll) lines)
  14511. ll
  14512. (setq ll words)
  14513. (while (> (length ll) lines)
  14514. (setq w (1+ w))
  14515. (setq ll (org-do-wrap words w)))
  14516. ll))
  14517. (t (error "Cannot wrap this")))))
  14518. (defun org-do-wrap (words width)
  14519. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14520. (let (lines line)
  14521. (while words
  14522. (setq line (pop words))
  14523. (while (and words (< (+ (length line) (length (car words))) width))
  14524. (setq line (concat line " " (pop words))))
  14525. (setq lines (push line lines)))
  14526. (nreverse lines)))
  14527. (defun org-split-string (string &optional separators)
  14528. "Splits STRING into substrings at SEPARATORS.
  14529. No empty strings are returned if there are matches at the beginning
  14530. and end of string."
  14531. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14532. (start 0)
  14533. notfirst
  14534. (list nil))
  14535. (while (and (string-match rexp string
  14536. (if (and notfirst
  14537. (= start (match-beginning 0))
  14538. (< start (length string)))
  14539. (1+ start) start))
  14540. (< (match-beginning 0) (length string)))
  14541. (setq notfirst t)
  14542. (or (eq (match-beginning 0) 0)
  14543. (and (eq (match-beginning 0) (match-end 0))
  14544. (eq (match-beginning 0) start))
  14545. (setq list
  14546. (cons (substring string start (match-beginning 0))
  14547. list)))
  14548. (setq start (match-end 0)))
  14549. (or (eq start (length string))
  14550. (setq list
  14551. (cons (substring string start)
  14552. list)))
  14553. (nreverse list)))
  14554. (defun org-quote-vert (s)
  14555. "Replace \"|\" with \"\\vert\"."
  14556. (while (string-match "|" s)
  14557. (setq s (replace-match "\\vert" t t s)))
  14558. s)
  14559. (defun org-uuidgen-p (s)
  14560. "Is S an ID created by UUIDGEN?"
  14561. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14562. (defun org-context ()
  14563. "Return a list of contexts of the current cursor position.
  14564. If several contexts apply, all are returned.
  14565. Each context entry is a list with a symbol naming the context, and
  14566. two positions indicating start and end of the context. Possible
  14567. contexts are:
  14568. :headline anywhere in a headline
  14569. :headline-stars on the leading stars in a headline
  14570. :todo-keyword on a TODO keyword (including DONE) in a headline
  14571. :tags on the TAGS in a headline
  14572. :priority on the priority cookie in a headline
  14573. :item on the first line of a plain list item
  14574. :item-bullet on the bullet/number of a plain list item
  14575. :checkbox on the checkbox in a plain list item
  14576. :table in an org-mode table
  14577. :table-special on a special filed in a table
  14578. :table-table in a table.el table
  14579. :link on a hyperlink
  14580. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14581. :target on a <<target>>
  14582. :radio-target on a <<<radio-target>>>
  14583. :latex-fragment on a LaTeX fragment
  14584. :latex-preview on a LaTeX fragment with overlayed preview image
  14585. This function expects the position to be visible because it uses font-lock
  14586. faces as a help to recognize the following contexts: :table-special, :link,
  14587. and :keyword."
  14588. (let* ((f (get-text-property (point) 'face))
  14589. (faces (if (listp f) f (list f)))
  14590. (p (point)) clist o)
  14591. ;; First the large context
  14592. (cond
  14593. ((org-on-heading-p t)
  14594. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14595. (when (progn
  14596. (beginning-of-line 1)
  14597. (looking-at org-todo-line-tags-regexp))
  14598. (push (org-point-in-group p 1 :headline-stars) clist)
  14599. (push (org-point-in-group p 2 :todo-keyword) clist)
  14600. (push (org-point-in-group p 4 :tags) clist))
  14601. (goto-char p)
  14602. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14603. (if (looking-at "\\[#[A-Z0-9]\\]")
  14604. (push (org-point-in-group p 0 :priority) clist)))
  14605. ((org-at-item-p)
  14606. (push (org-point-in-group p 2 :item-bullet) clist)
  14607. (push (list :item (point-at-bol)
  14608. (save-excursion (org-end-of-item) (point)))
  14609. clist)
  14610. (and (org-at-item-checkbox-p)
  14611. (push (org-point-in-group p 0 :checkbox) clist)))
  14612. ((org-at-table-p)
  14613. (push (list :table (org-table-begin) (org-table-end)) clist)
  14614. (if (memq 'org-formula faces)
  14615. (push (list :table-special
  14616. (previous-single-property-change p 'face)
  14617. (next-single-property-change p 'face)) clist)))
  14618. ((org-at-table-p 'any)
  14619. (push (list :table-table) clist)))
  14620. (goto-char p)
  14621. ;; Now the small context
  14622. (cond
  14623. ((org-at-timestamp-p)
  14624. (push (org-point-in-group p 0 :timestamp) clist))
  14625. ((memq 'org-link faces)
  14626. (push (list :link
  14627. (previous-single-property-change p 'face)
  14628. (next-single-property-change p 'face)) clist))
  14629. ((memq 'org-special-keyword faces)
  14630. (push (list :keyword
  14631. (previous-single-property-change p 'face)
  14632. (next-single-property-change p 'face)) clist))
  14633. ((org-on-target-p)
  14634. (push (org-point-in-group p 0 :target) clist)
  14635. (goto-char (1- (match-beginning 0)))
  14636. (if (looking-at org-radio-target-regexp)
  14637. (push (org-point-in-group p 0 :radio-target) clist))
  14638. (goto-char p))
  14639. ((setq o (car (delq nil
  14640. (mapcar
  14641. (lambda (x)
  14642. (if (memq x org-latex-fragment-image-overlays) x))
  14643. (org-overlays-at (point))))))
  14644. (push (list :latex-fragment
  14645. (org-overlay-start o) (org-overlay-end o)) clist)
  14646. (push (list :latex-preview
  14647. (org-overlay-start o) (org-overlay-end o)) clist))
  14648. ((org-inside-LaTeX-fragment-p)
  14649. ;; FIXME: positions wrong.
  14650. (push (list :latex-fragment (point) (point)) clist)))
  14651. (setq clist (nreverse (delq nil clist)))
  14652. clist))
  14653. ;; FIXME: Compare with at-regexp-p Do we need both?
  14654. (defun org-in-regexp (re &optional nlines visually)
  14655. "Check if point is inside a match of regexp.
  14656. Normally only the current line is checked, but you can include NLINES extra
  14657. lines both before and after point into the search.
  14658. If VISUALLY is set, require that the cursor is not after the match but
  14659. really on, so that the block visually is on the match."
  14660. (catch 'exit
  14661. (let ((pos (point))
  14662. (eol (point-at-eol (+ 1 (or nlines 0))))
  14663. (inc (if visually 1 0)))
  14664. (save-excursion
  14665. (beginning-of-line (- 1 (or nlines 0)))
  14666. (while (re-search-forward re eol t)
  14667. (if (and (<= (match-beginning 0) pos)
  14668. (>= (+ inc (match-end 0)) pos))
  14669. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14670. (defun org-at-regexp-p (regexp)
  14671. "Is point inside a match of REGEXP in the current line?"
  14672. (catch 'exit
  14673. (save-excursion
  14674. (let ((pos (point)) (end (point-at-eol)))
  14675. (beginning-of-line 1)
  14676. (while (re-search-forward regexp end t)
  14677. (if (and (<= (match-beginning 0) pos)
  14678. (>= (match-end 0) pos))
  14679. (throw 'exit t)))
  14680. nil))))
  14681. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14682. "Call `multi-occur' with buffers for all agenda files."
  14683. (interactive "sOrg-files matching: \np")
  14684. (let* ((files (org-agenda-files))
  14685. (tnames (mapcar 'file-truename files))
  14686. (extra org-agenda-text-search-extra-files)
  14687. f)
  14688. (when (eq (car extra) 'agenda-archives)
  14689. (setq extra (cdr extra))
  14690. (setq files (org-add-archive-files files)))
  14691. (while (setq f (pop extra))
  14692. (unless (member (file-truename f) tnames)
  14693. (add-to-list 'files f 'append)
  14694. (add-to-list 'tnames (file-truename f) 'append)))
  14695. (multi-occur
  14696. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14697. regexp)))
  14698. (if (boundp 'occur-mode-find-occurrence-hook)
  14699. ;; Emacs 23
  14700. (add-hook 'occur-mode-find-occurrence-hook
  14701. (lambda ()
  14702. (when (org-mode-p)
  14703. (org-reveal))))
  14704. ;; Emacs 22
  14705. (defadvice occur-mode-goto-occurrence
  14706. (after org-occur-reveal activate)
  14707. (and (org-mode-p) (org-reveal)))
  14708. (defadvice occur-mode-goto-occurrence-other-window
  14709. (after org-occur-reveal activate)
  14710. (and (org-mode-p) (org-reveal)))
  14711. (defadvice occur-mode-display-occurrence
  14712. (after org-occur-reveal activate)
  14713. (when (org-mode-p)
  14714. (let ((pos (occur-mode-find-occurrence)))
  14715. (with-current-buffer (marker-buffer pos)
  14716. (save-excursion
  14717. (goto-char pos)
  14718. (org-reveal)))))))
  14719. (defun org-occur-link-in-agenda-files ()
  14720. "Create a link and search for it in the agendas.
  14721. The link is not stored in `org-stored-links', it is just created
  14722. for the search purpose."
  14723. (interactive)
  14724. (let ((link (condition-case nil
  14725. (org-store-link nil)
  14726. (error "Unable to create a link to here"))))
  14727. (org-occur-in-agenda-files (regexp-quote link))))
  14728. (defun org-uniquify (list)
  14729. "Remove duplicate elements from LIST."
  14730. (let (res)
  14731. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14732. res))
  14733. (defun org-delete-all (elts list)
  14734. "Remove all elements in ELTS from LIST."
  14735. (while elts
  14736. (setq list (delete (pop elts) list)))
  14737. list)
  14738. (defun org-back-over-empty-lines ()
  14739. "Move backwards over whitespace, to the beginning of the first empty line.
  14740. Returns the number of empty lines passed."
  14741. (let ((pos (point)))
  14742. (skip-chars-backward " \t\n\r")
  14743. (beginning-of-line 2)
  14744. (goto-char (min (point) pos))
  14745. (count-lines (point) pos)))
  14746. (defun org-skip-whitespace ()
  14747. (skip-chars-forward " \t\n\r"))
  14748. (defun org-point-in-group (point group &optional context)
  14749. "Check if POINT is in match-group GROUP.
  14750. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14751. match. If the match group does ot exist or point is not inside it,
  14752. return nil."
  14753. (and (match-beginning group)
  14754. (>= point (match-beginning group))
  14755. (<= point (match-end group))
  14756. (if context
  14757. (list context (match-beginning group) (match-end group))
  14758. t)))
  14759. (defun org-switch-to-buffer-other-window (&rest args)
  14760. "Switch to buffer in a second window on the current frame.
  14761. In particular, do not allow pop-up frames."
  14762. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14763. special-display-function)
  14764. (apply 'switch-to-buffer-other-window args)))
  14765. (defun org-combine-plists (&rest plists)
  14766. "Create a single property list from all plists in PLISTS.
  14767. The process starts by copying the first list, and then setting properties
  14768. from the other lists. Settings in the last list are the most significant
  14769. ones and overrule settings in the other lists."
  14770. (let ((rtn (copy-sequence (pop plists)))
  14771. p v ls)
  14772. (while plists
  14773. (setq ls (pop plists))
  14774. (while ls
  14775. (setq p (pop ls) v (pop ls))
  14776. (setq rtn (plist-put rtn p v))))
  14777. rtn))
  14778. (defun org-move-line-down (arg)
  14779. "Move the current line down. With prefix argument, move it past ARG lines."
  14780. (interactive "p")
  14781. (let ((col (current-column))
  14782. beg end pos)
  14783. (beginning-of-line 1) (setq beg (point))
  14784. (beginning-of-line 2) (setq end (point))
  14785. (beginning-of-line (+ 1 arg))
  14786. (setq pos (move-marker (make-marker) (point)))
  14787. (insert (delete-and-extract-region beg end))
  14788. (goto-char pos)
  14789. (org-move-to-column col)))
  14790. (defun org-move-line-up (arg)
  14791. "Move the current line up. With prefix argument, move it past ARG lines."
  14792. (interactive "p")
  14793. (let ((col (current-column))
  14794. beg end pos)
  14795. (beginning-of-line 1) (setq beg (point))
  14796. (beginning-of-line 2) (setq end (point))
  14797. (beginning-of-line (- arg))
  14798. (setq pos (move-marker (make-marker) (point)))
  14799. (insert (delete-and-extract-region beg end))
  14800. (goto-char pos)
  14801. (org-move-to-column col)))
  14802. (defun org-replace-escapes (string table)
  14803. "Replace %-escapes in STRING with values in TABLE.
  14804. TABLE is an association list with keys like \"%a\" and string values.
  14805. The sequences in STRING may contain normal field width and padding information,
  14806. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14807. so values can contain further %-escapes if they are define later in TABLE."
  14808. (let ((case-fold-search nil)
  14809. e re rpl)
  14810. (while (setq e (pop table))
  14811. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14812. (while (string-match re string)
  14813. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14814. (cdr e)))
  14815. (setq string (replace-match rpl t t string))))
  14816. string))
  14817. (defun org-sublist (list start end)
  14818. "Return a section of LIST, from START to END.
  14819. Counting starts at 1."
  14820. (let (rtn (c start))
  14821. (setq list (nthcdr (1- start) list))
  14822. (while (and list (<= c end))
  14823. (push (pop list) rtn)
  14824. (setq c (1+ c)))
  14825. (nreverse rtn)))
  14826. (defun org-find-base-buffer-visiting (file)
  14827. "Like `find-buffer-visiting' but always return the base buffer and
  14828. not an indirect buffer."
  14829. (let ((buf (or (get-file-buffer file)
  14830. (find-buffer-visiting file))))
  14831. (if buf
  14832. (or (buffer-base-buffer buf) buf)
  14833. nil)))
  14834. (defun org-image-file-name-regexp (&optional extensions)
  14835. "Return regexp matching the file names of images.
  14836. If EXTENSIONS is given, only match these."
  14837. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14838. (image-file-name-regexp)
  14839. (let ((image-file-name-extensions
  14840. (or extensions
  14841. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14842. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14843. (concat "\\."
  14844. (regexp-opt (nconc (mapcar 'upcase
  14845. image-file-name-extensions)
  14846. image-file-name-extensions)
  14847. t)
  14848. "\\'"))))
  14849. (defun org-file-image-p (file &optional extensions)
  14850. "Return non-nil if FILE is an image."
  14851. (save-match-data
  14852. (string-match (org-image-file-name-regexp extensions) file)))
  14853. (defun org-get-cursor-date ()
  14854. "Return the date at cursor in as a time.
  14855. This works in the calendar and in the agenda, anywhere else it just
  14856. returns the current time."
  14857. (let (date day defd)
  14858. (cond
  14859. ((eq major-mode 'calendar-mode)
  14860. (setq date (calendar-cursor-to-date)
  14861. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14862. ((eq major-mode 'org-agenda-mode)
  14863. (setq day (get-text-property (point) 'day))
  14864. (if day
  14865. (setq date (calendar-gregorian-from-absolute day)
  14866. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14867. (nth 2 date))))))
  14868. (or defd (current-time))))
  14869. (defvar org-agenda-action-marker (make-marker)
  14870. "Marker pointing to the entry for the next agenda action.")
  14871. (defun org-mark-entry-for-agenda-action ()
  14872. "Mark the current entry as target of an agenda action.
  14873. Agenda actions are actions executed from the agenda with the key `k',
  14874. which make use of the date at the cursor."
  14875. (interactive)
  14876. (move-marker org-agenda-action-marker
  14877. (save-excursion (org-back-to-heading t) (point))
  14878. (current-buffer))
  14879. (message
  14880. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14881. ;;; Paragraph filling stuff.
  14882. ;; We want this to be just right, so use the full arsenal.
  14883. (defun org-indent-line-function ()
  14884. "Indent line like previous, but further if previous was headline or item."
  14885. (interactive)
  14886. (let* ((pos (point))
  14887. (itemp (org-at-item-p))
  14888. (case-fold-search t)
  14889. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14890. column bpos bcol tpos tcol bullet btype bullet-type)
  14891. ;; Find the previous relevant line
  14892. (beginning-of-line 1)
  14893. (cond
  14894. ((looking-at "#") (setq column 0))
  14895. ((looking-at "\\*+ ") (setq column 0))
  14896. ((and (looking-at "[ \t]*:END:")
  14897. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14898. (save-excursion
  14899. (goto-char (1- (match-beginning 1)))
  14900. (setq column (current-column))))
  14901. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14902. (save-excursion
  14903. (re-search-backward
  14904. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14905. (setq column (org-get-indentation (match-string 0))))
  14906. (t
  14907. (beginning-of-line 0)
  14908. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14909. (not (looking-at "[ \t]*:END:"))
  14910. (not (looking-at org-drawer-regexp)))
  14911. (beginning-of-line 0))
  14912. (cond
  14913. ((looking-at "\\*+[ \t]+")
  14914. (if (not org-adapt-indentation)
  14915. (setq column 0)
  14916. (goto-char (match-end 0))
  14917. (setq column (current-column))))
  14918. ((looking-at org-drawer-regexp)
  14919. (goto-char (1- (match-beginning 1)))
  14920. (setq column (current-column)))
  14921. ((looking-at "\\([ \t]*\\):END:")
  14922. (goto-char (match-end 1))
  14923. (setq column (current-column)))
  14924. ((org-in-item-p)
  14925. (org-beginning-of-item)
  14926. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14927. (setq bpos (match-beginning 1) tpos (match-end 0)
  14928. bcol (progn (goto-char bpos) (current-column))
  14929. tcol (progn (goto-char tpos) (current-column))
  14930. bullet (match-string 1)
  14931. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14932. (if (> tcol (+ bcol org-description-max-indent))
  14933. (setq tcol (+ bcol 5)))
  14934. (if (not itemp)
  14935. (setq column tcol)
  14936. (goto-char pos)
  14937. (beginning-of-line 1)
  14938. (if (looking-at "\\S-")
  14939. (progn
  14940. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14941. (setq bullet (match-string 1)
  14942. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14943. (setq column (if (equal btype bullet-type) bcol tcol)))
  14944. (setq column (org-get-indentation)))))
  14945. (t (setq column (org-get-indentation))))))
  14946. (goto-char pos)
  14947. (if (<= (current-column) (current-indentation))
  14948. (org-indent-line-to column)
  14949. (save-excursion (org-indent-line-to column)))
  14950. (setq column (current-column))
  14951. (beginning-of-line 1)
  14952. (if (looking-at
  14953. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14954. (replace-match (concat "\\1" (format org-property-format
  14955. (match-string 2) (match-string 3)))
  14956. t nil))
  14957. (org-move-to-column column)))
  14958. (defun org-set-autofill-regexps ()
  14959. (interactive)
  14960. ;; In the paragraph separator we include headlines, because filling
  14961. ;; text in a line directly attached to a headline would otherwise
  14962. ;; fill the headline as well.
  14963. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14964. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  14965. ;; The paragraph starter includes hand-formatted lists.
  14966. (org-set-local
  14967. 'paragraph-start
  14968. (concat
  14969. "\f" "\\|"
  14970. "[ ]*$" "\\|"
  14971. "\\*+ " "\\|"
  14972. "[ \t]*#" "\\|"
  14973. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14974. "[ \t]*[:|]" "\\|"
  14975. "\\$\\$" "\\|"
  14976. "\\\\\\(begin\\|end\\|[][]\\)"))
  14977. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14978. ;; But only if the user has not turned off tables or fixed-width regions
  14979. (org-set-local
  14980. 'auto-fill-inhibit-regexp
  14981. (concat "\\*+ \\|#\\+"
  14982. "\\|[ \t]*" org-keyword-time-regexp
  14983. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14984. (concat
  14985. "\\|[ \t]*["
  14986. (if org-enable-table-editor "|" "")
  14987. (if org-enable-fixed-width-editor ":" "")
  14988. "]"))))
  14989. ;; We use our own fill-paragraph function, to make sure that tables
  14990. ;; and fixed-width regions are not wrapped. That function will pass
  14991. ;; through to `fill-paragraph' when appropriate.
  14992. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14993. ; Adaptive filling: To get full control, first make sure that
  14994. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14995. (org-set-local 'adaptive-fill-regexp "\000")
  14996. (org-set-local 'adaptive-fill-function
  14997. 'org-adaptive-fill-function)
  14998. (org-set-local
  14999. 'align-mode-rules-list
  15000. '((org-in-buffer-settings
  15001. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15002. (modes . '(org-mode))))))
  15003. (defun org-fill-paragraph (&optional justify)
  15004. "Re-align a table, pass through to fill-paragraph if no table."
  15005. (let ((table-p (org-at-table-p))
  15006. (table.el-p (org-at-table.el-p)))
  15007. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15008. (save-excursion (goto-char (point-at-bol))
  15009. (looking-at outline-regexp)))
  15010. t) ; skip headlines
  15011. (table.el-p t) ; skip table.el tables
  15012. (table-p (org-table-align) t) ; align org-mode tables
  15013. (t nil)))) ; call paragraph-fill
  15014. ;; For reference, this is the default value of adaptive-fill-regexp
  15015. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15016. (defun org-adaptive-fill-function ()
  15017. "Return a fill prefix for org-mode files.
  15018. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15019. work correctly."
  15020. (cond ((looking-at "#[ \t]+")
  15021. (match-string 0))
  15022. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15023. (save-excursion
  15024. (if (> (match-end 1) (+ (match-beginning 1)
  15025. org-description-max-indent))
  15026. (goto-char (+ (match-beginning 1) 5))
  15027. (goto-char (match-end 0)))
  15028. (make-string (current-column) ?\ )))
  15029. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15030. (save-excursion
  15031. (goto-char (match-end 0))
  15032. (make-string (current-column) ?\ )))
  15033. (t nil)))
  15034. ;;; Other stuff.
  15035. (defun org-toggle-fixed-width-section (arg)
  15036. "Toggle the fixed-width export.
  15037. If there is no active region, the QUOTE keyword at the current headline is
  15038. inserted or removed. When present, it causes the text between this headline
  15039. and the next to be exported as fixed-width text, and unmodified.
  15040. If there is an active region, this command adds or removes a colon as the
  15041. first character of this line. If the first character of a line is a colon,
  15042. this line is also exported in fixed-width font."
  15043. (interactive "P")
  15044. (let* ((cc 0)
  15045. (regionp (org-region-active-p))
  15046. (beg (if regionp (region-beginning) (point)))
  15047. (end (if regionp (region-end)))
  15048. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15049. (case-fold-search nil)
  15050. (re "[ \t]*\\(: \\)")
  15051. off)
  15052. (if regionp
  15053. (save-excursion
  15054. (goto-char beg)
  15055. (setq cc (current-column))
  15056. (beginning-of-line 1)
  15057. (setq off (looking-at re))
  15058. (while (> nlines 0)
  15059. (setq nlines (1- nlines))
  15060. (beginning-of-line 1)
  15061. (cond
  15062. (arg
  15063. (org-move-to-column cc t)
  15064. (insert ": \n")
  15065. (forward-line -1))
  15066. ((and off (looking-at re))
  15067. (replace-match "" t t nil 1))
  15068. ((not off) (org-move-to-column cc t) (insert ": ")))
  15069. (forward-line 1)))
  15070. (save-excursion
  15071. (org-back-to-heading)
  15072. (if (looking-at (concat outline-regexp
  15073. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15074. (replace-match "" t t nil 1)
  15075. (if (looking-at outline-regexp)
  15076. (progn
  15077. (goto-char (match-end 0))
  15078. (insert org-quote-string " "))))))))
  15079. (defun org-reftex-citation ()
  15080. "Use reftex-citation to insert a citation into the buffer.
  15081. This looks for a line like
  15082. #+BIBLIOGRAPHY: foo plain option:-d
  15083. and derives from it that foo.bib is the bbliography file relevant
  15084. for this document. It then installs the necessary environment for RefTeX
  15085. to work in this buffer and calls `reftex-citation' to insert a citation
  15086. into the buffer.
  15087. Export of such citations to both LaTeX and HTML is handled by the contributed
  15088. package org-exp-bibtex by Taru Karttunen."
  15089. (interactive)
  15090. (let ((reftex-docstruct-symbol 'rds)
  15091. (reftex-cite-format "\\cite{%l}")
  15092. rds bib)
  15093. (save-excursion
  15094. (save-restriction
  15095. (widen)
  15096. (let ((case-fold-search t)
  15097. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15098. (if (not (save-excursion
  15099. (or (re-search-forward re nil t)
  15100. (re-search-backward re nil t))))
  15101. (error "No bibliography defined in file")
  15102. (setq bib (concat (match-string 1) ".bib")
  15103. rds (list (list 'bib bib)))))))
  15104. (call-interactively 'reftex-citation)))
  15105. ;;;; Functions extending outline functionality
  15106. (defun org-beginning-of-line (&optional arg)
  15107. "Go to the beginning of the current line. If that is invisible, continue
  15108. to a visible line beginning. This makes the function of C-a more intuitive.
  15109. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15110. first attempt, and only move to after the tags when the cursor is already
  15111. beyond the end of the headline."
  15112. (interactive "P")
  15113. (let ((pos (point))
  15114. (special (if (consp org-special-ctrl-a/e)
  15115. (car org-special-ctrl-a/e)
  15116. org-special-ctrl-a/e))
  15117. refpos)
  15118. (if (org-bound-and-true-p line-move-visual)
  15119. (beginning-of-visual-line 1)
  15120. (beginning-of-line 1))
  15121. (if (and arg (fboundp 'move-beginning-of-line))
  15122. (call-interactively 'move-beginning-of-line)
  15123. (if (bobp)
  15124. nil
  15125. (backward-char 1)
  15126. (if (org-invisible-p)
  15127. (while (and (not (bobp)) (org-invisible-p))
  15128. (backward-char 1)
  15129. (beginning-of-line 1))
  15130. (forward-char 1))))
  15131. (when special
  15132. (cond
  15133. ((and (looking-at org-complex-heading-regexp)
  15134. (= (char-after (match-end 1)) ?\ ))
  15135. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15136. (point-at-eol)))
  15137. (goto-char
  15138. (if (eq special t)
  15139. (cond ((> pos refpos) refpos)
  15140. ((= pos (point)) refpos)
  15141. (t (point)))
  15142. (cond ((> pos (point)) (point))
  15143. ((not (eq last-command this-command)) (point))
  15144. (t refpos)))))
  15145. ((org-at-item-p)
  15146. (goto-char
  15147. (if (eq special t)
  15148. (cond ((> pos (match-end 4)) (match-end 4))
  15149. ((= pos (point)) (match-end 4))
  15150. (t (point)))
  15151. (cond ((> pos (point)) (point))
  15152. ((not (eq last-command this-command)) (point))
  15153. (t (match-end 4))))))))
  15154. (org-no-warnings
  15155. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15156. (defun org-end-of-line (&optional arg)
  15157. "Go to the end of the line.
  15158. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15159. first attempt, and only move to after the tags when the cursor is already
  15160. beyond the end of the headline."
  15161. (interactive "P")
  15162. (let ((special (if (consp org-special-ctrl-a/e)
  15163. (cdr org-special-ctrl-a/e)
  15164. org-special-ctrl-a/e)))
  15165. (if (or (not special)
  15166. (not (org-on-heading-p))
  15167. arg)
  15168. (call-interactively
  15169. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15170. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15171. (t 'end-of-line)))
  15172. (let ((pos (point)))
  15173. (beginning-of-line 1)
  15174. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  15175. (if (eq special t)
  15176. (if (or (< pos (match-beginning 1))
  15177. (= pos (match-end 0)))
  15178. (goto-char (match-beginning 1))
  15179. (goto-char (match-end 0)))
  15180. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15181. (goto-char (match-end 0))
  15182. (goto-char (match-beginning 1))))
  15183. (call-interactively (if (fboundp 'move-end-of-line)
  15184. 'move-end-of-line
  15185. 'end-of-line)))))
  15186. (org-no-warnings
  15187. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15188. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15189. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15190. (defun org-backward-sentence (&optional arg)
  15191. "Go to beginning of sentence, or beginning of table field.
  15192. This will call `backward-sentence' or `org-table-beginning-of-field',
  15193. depending on context."
  15194. (interactive "P")
  15195. (cond
  15196. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15197. (t (call-interactively 'backward-sentence))))
  15198. (defun org-forward-sentence (&optional arg)
  15199. "Go to end of sentence, or end of table field.
  15200. This will call `forward-sentence' or `org-table-end-of-field',
  15201. depending on context."
  15202. (interactive "P")
  15203. (cond
  15204. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15205. (t (call-interactively 'forward-sentence))))
  15206. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15207. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15208. (defun org-kill-line (&optional arg)
  15209. "Kill line, to tags or end of line."
  15210. (interactive "P")
  15211. (cond
  15212. ((or (not org-special-ctrl-k)
  15213. (bolp)
  15214. (not (org-on-heading-p)))
  15215. (call-interactively 'kill-line))
  15216. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15217. (kill-region (point) (match-beginning 1))
  15218. (org-set-tags nil t))
  15219. (t (kill-region (point) (point-at-eol)))))
  15220. (define-key org-mode-map "\C-k" 'org-kill-line)
  15221. (defun org-yank (&optional arg)
  15222. "Yank. If the kill is a subtree, treat it specially.
  15223. This command will look at the current kill and check if is a single
  15224. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15225. cursor is at the beginning of a line or after the stars of a currently
  15226. empty headline, then the yank is handled specially. How exactly depends
  15227. on the value of the following variables, both set by default.
  15228. org-yank-folded-subtrees
  15229. When set, the subtree(s) will be folded after insertion, but only
  15230. if doing so would now swallow text after the yanked text.
  15231. org-yank-adjusted-subtrees
  15232. When set, the subtree will be promoted or demoted in order to
  15233. fit into the local outline tree structure, which means that the level
  15234. will be adjusted so that it becomes the smaller one of the two
  15235. *visible* surrounding headings.
  15236. Any prefix to this command will cause `yank' to be called directly with
  15237. no special treatment. In particular, a simple `C-u' prefix will just
  15238. plainly yank the text as it is.
  15239. \[1] The test checks if the first non-white line is a heading
  15240. and if there are no other headings with fewer stars."
  15241. (interactive "P")
  15242. (org-yank-generic 'yank arg))
  15243. (defun org-yank-generic (command arg)
  15244. "Perform some yank-like command.
  15245. This function implements the behavior described in the `org-yank'
  15246. documentation. However, it has been generalized to work for any
  15247. interactive command with similar behavior."
  15248. ;; pretend to be command COMMAND
  15249. (setq this-command command)
  15250. (if arg
  15251. (call-interactively command)
  15252. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15253. (and (org-kill-is-subtree-p)
  15254. (or (bolp)
  15255. (and (looking-at "[ \t]*$")
  15256. (string-match
  15257. "\\`\\*+\\'"
  15258. (buffer-substring (point-at-bol) (point)))))))
  15259. swallowp)
  15260. (cond
  15261. ((and subtreep org-yank-folded-subtrees)
  15262. (let ((beg (point))
  15263. end)
  15264. (if (and subtreep org-yank-adjusted-subtrees)
  15265. (org-paste-subtree nil nil 'for-yank)
  15266. (call-interactively command))
  15267. (setq end (point))
  15268. (goto-char beg)
  15269. (when (and (bolp) subtreep
  15270. (not (setq swallowp
  15271. (org-yank-folding-would-swallow-text beg end))))
  15272. (or (looking-at outline-regexp)
  15273. (re-search-forward (concat "^" outline-regexp) end t))
  15274. (while (and (< (point) end) (looking-at outline-regexp))
  15275. (hide-subtree)
  15276. (org-cycle-show-empty-lines 'folded)
  15277. (condition-case nil
  15278. (outline-forward-same-level 1)
  15279. (error (goto-char end)))))
  15280. (when swallowp
  15281. (message
  15282. "Inserted text not folded because that would swallow text"))
  15283. (goto-char end)
  15284. (skip-chars-forward " \t\n\r")
  15285. (beginning-of-line 1)
  15286. (push-mark beg 'nomsg)))
  15287. ((and subtreep org-yank-adjusted-subtrees)
  15288. (let ((beg (point-at-bol)))
  15289. (org-paste-subtree nil nil 'for-yank)
  15290. (push-mark beg 'nomsg)))
  15291. (t
  15292. (call-interactively command))))))
  15293. (defun org-yank-folding-would-swallow-text (beg end)
  15294. "Would hide-subtree at BEG swallow any text after END?"
  15295. (let (level)
  15296. (save-excursion
  15297. (goto-char beg)
  15298. (when (or (looking-at outline-regexp)
  15299. (re-search-forward (concat "^" outline-regexp) end t))
  15300. (setq level (org-outline-level)))
  15301. (goto-char end)
  15302. (skip-chars-forward " \t\r\n\v\f")
  15303. (if (or (eobp)
  15304. (and (bolp) (looking-at org-outline-regexp)
  15305. (<= (org-outline-level) level)))
  15306. nil ; Nothing would be swallowed
  15307. t)))) ; something would swallow
  15308. (define-key org-mode-map "\C-y" 'org-yank)
  15309. (defun org-invisible-p ()
  15310. "Check if point is at a character currently not visible."
  15311. ;; Early versions of noutline don't have `outline-invisible-p'.
  15312. (if (fboundp 'outline-invisible-p)
  15313. (outline-invisible-p)
  15314. (get-char-property (point) 'invisible)))
  15315. (defun org-invisible-p2 ()
  15316. "Check if point is at a character currently not visible."
  15317. (save-excursion
  15318. (if (and (eolp) (not (bobp))) (backward-char 1))
  15319. ;; Early versions of noutline don't have `outline-invisible-p'.
  15320. (if (fboundp 'outline-invisible-p)
  15321. (outline-invisible-p)
  15322. (get-char-property (point) 'invisible))))
  15323. (defun org-back-to-heading (&optional invisible-ok)
  15324. "Call `outline-back-to-heading', but provide a better error message."
  15325. (condition-case nil
  15326. (outline-back-to-heading invisible-ok)
  15327. (error (error "Before first headline at position %d in buffer %s"
  15328. (point) (current-buffer)))))
  15329. (defun org-before-first-heading-p ()
  15330. "Before first heading?"
  15331. (save-excursion
  15332. (null (re-search-backward "^\\*+ " nil t))))
  15333. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15334. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15335. (defun org-at-heading-or-item-p ()
  15336. (or (org-on-heading-p) (org-at-item-p)))
  15337. (defun org-on-target-p ()
  15338. (or (org-in-regexp org-radio-target-regexp)
  15339. (org-in-regexp org-target-regexp)))
  15340. (defun org-up-heading-all (arg)
  15341. "Move to the heading line of which the present line is a subheading.
  15342. This function considers both visible and invisible heading lines.
  15343. With argument, move up ARG levels."
  15344. (if (fboundp 'outline-up-heading-all)
  15345. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15346. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15347. (defun org-up-heading-safe ()
  15348. "Move to the heading line of which the present line is a subheading.
  15349. This version will not throw an error. It will return the level of the
  15350. headline found, or nil if no higher level is found.
  15351. Also, this function will be a lot faster than `outline-up-heading',
  15352. because it relies on stars being the outline starters. This can really
  15353. make a significant difference in outlines with very many siblings."
  15354. (let (start-level re)
  15355. (org-back-to-heading t)
  15356. (setq start-level (funcall outline-level))
  15357. (if (equal start-level 1)
  15358. nil
  15359. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15360. (if (re-search-backward re nil t)
  15361. (funcall outline-level)))))
  15362. (defun org-first-sibling-p ()
  15363. "Is this heading the first child of its parents?"
  15364. (interactive)
  15365. (let ((re (concat "^" outline-regexp))
  15366. level l)
  15367. (unless (org-at-heading-p t)
  15368. (error "Not at a heading"))
  15369. (setq level (funcall outline-level))
  15370. (save-excursion
  15371. (if (not (re-search-backward re nil t))
  15372. t
  15373. (setq l (funcall outline-level))
  15374. (< l level)))))
  15375. (defun org-goto-sibling (&optional previous)
  15376. "Goto the next sibling, even if it is invisible.
  15377. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15378. when a sibling was found. When none is found, return nil and don't
  15379. move point."
  15380. (let ((fun (if previous 're-search-backward 're-search-forward))
  15381. (pos (point))
  15382. (re (concat "^" outline-regexp))
  15383. level l)
  15384. (when (condition-case nil (org-back-to-heading t) (error nil))
  15385. (setq level (funcall outline-level))
  15386. (catch 'exit
  15387. (or previous (forward-char 1))
  15388. (while (funcall fun re nil t)
  15389. (setq l (funcall outline-level))
  15390. (when (< l level) (goto-char pos) (throw 'exit nil))
  15391. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15392. (goto-char pos)
  15393. nil))))
  15394. (defun org-show-siblings ()
  15395. "Show all siblings of the current headline."
  15396. (save-excursion
  15397. (while (org-goto-sibling) (org-flag-heading nil)))
  15398. (save-excursion
  15399. (while (org-goto-sibling 'previous)
  15400. (org-flag-heading nil))))
  15401. (defun org-show-hidden-entry ()
  15402. "Show an entry where even the heading is hidden."
  15403. (save-excursion
  15404. (org-show-entry)))
  15405. (defun org-flag-heading (flag &optional entry)
  15406. "Flag the current heading. FLAG non-nil means make invisible.
  15407. When ENTRY is non-nil, show the entire entry."
  15408. (save-excursion
  15409. (org-back-to-heading t)
  15410. ;; Check if we should show the entire entry
  15411. (if entry
  15412. (progn
  15413. (org-show-entry)
  15414. (save-excursion
  15415. (and (outline-next-heading)
  15416. (org-flag-heading nil))))
  15417. (outline-flag-region (max (point-min) (1- (point)))
  15418. (save-excursion (outline-end-of-heading) (point))
  15419. flag))))
  15420. (defun org-get-next-sibling ()
  15421. "Move to next heading of the same level, and return point.
  15422. If there is no such heading, return nil.
  15423. This is like outline-next-sibling, but invisible headings are ok."
  15424. (let ((level (funcall outline-level)))
  15425. (outline-next-heading)
  15426. (while (and (not (eobp)) (> (funcall outline-level) level))
  15427. (outline-next-heading))
  15428. (if (or (eobp) (< (funcall outline-level) level))
  15429. nil
  15430. (point))))
  15431. (defun org-get-last-sibling ()
  15432. "Move to previous heading of the same level, and return point.
  15433. If there is no such heading, return nil."
  15434. (let ((opoint (point))
  15435. (level (funcall outline-level)))
  15436. (outline-previous-heading)
  15437. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15438. (while (and (> (funcall outline-level) level)
  15439. (not (bobp)))
  15440. (outline-previous-heading))
  15441. (if (< (funcall outline-level) level)
  15442. nil
  15443. (point)))))
  15444. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15445. ;; This contains an exact copy of the original function, but it uses
  15446. ;; `org-back-to-heading', to make it work also in invisible
  15447. ;; trees. And is uses an invisible-OK argument.
  15448. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15449. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15450. ;; with many children and grandchildren etc, this can be much faster
  15451. ;; than the outline version.
  15452. (org-back-to-heading invisible-OK)
  15453. (let ((first t)
  15454. (level (funcall outline-level)))
  15455. (if (and (org-mode-p) (< level 1000))
  15456. ;; A true heading (not a plain list item), in Org-mode
  15457. ;; This means we can easily find the end by looking
  15458. ;; only for the right number of stars. Using a regexp to do
  15459. ;; this is so much faster than using a Lisp loop.
  15460. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15461. (forward-char 1)
  15462. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15463. ;; something else, do it the slow way
  15464. (while (and (not (eobp))
  15465. (or first (> (funcall outline-level) level)))
  15466. (setq first nil)
  15467. (outline-next-heading)))
  15468. (unless to-heading
  15469. (if (memq (preceding-char) '(?\n ?\^M))
  15470. (progn
  15471. ;; Go to end of line before heading
  15472. (forward-char -1)
  15473. (if (memq (preceding-char) '(?\n ?\^M))
  15474. ;; leave blank line before heading
  15475. (forward-char -1))))))
  15476. (point))
  15477. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15478. "Use Org version in org-mode, for dramatic speed-up."
  15479. (if (eq major-mode 'org-mode)
  15480. (progn
  15481. (org-end-of-subtree nil t)
  15482. (unless (eobp) (backward-char 1)))
  15483. ad-do-it))
  15484. (defun org-forward-same-level (arg &optional invisible-ok)
  15485. "Move forward to the arg'th subheading at same level as this one.
  15486. Stop at the first and last subheadings of a superior heading."
  15487. (interactive "p")
  15488. (org-back-to-heading invisible-ok)
  15489. (org-on-heading-p)
  15490. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15491. (re (format "^\\*\\{1,%d\\} " level))
  15492. l)
  15493. (forward-char 1)
  15494. (while (> arg 0)
  15495. (while (and (re-search-forward re nil 'move)
  15496. (setq l (- (match-end 0) (match-beginning 0) 1))
  15497. (= l level)
  15498. (not invisible-ok)
  15499. (org-invisible-p))
  15500. (if (< l level) (setq arg 1)))
  15501. (setq arg (1- arg)))
  15502. (beginning-of-line 1)))
  15503. (defun org-backward-same-level (arg &optional invisible-ok)
  15504. "Move backward to the arg'th subheading at same level as this one.
  15505. Stop at the first and last subheadings of a superior heading."
  15506. (interactive "p")
  15507. (org-back-to-heading)
  15508. (org-on-heading-p)
  15509. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15510. (re (format "^\\*\\{1,%d\\} " level))
  15511. l)
  15512. (while (> arg 0)
  15513. (while (and (re-search-backward re nil 'move)
  15514. (setq l (- (match-end 0) (match-beginning 0) 1))
  15515. (= l level)
  15516. (not invisible-ok)
  15517. (org-invisible-p))
  15518. (if (< l level) (setq arg 1)))
  15519. (setq arg (1- arg)))))
  15520. (defun org-show-subtree ()
  15521. "Show everything after this heading at deeper levels."
  15522. (outline-flag-region
  15523. (point)
  15524. (save-excursion
  15525. (org-end-of-subtree t t))
  15526. nil))
  15527. (defun org-show-entry ()
  15528. "Show the body directly following this heading.
  15529. Show the heading too, if it is currently invisible."
  15530. (interactive)
  15531. (save-excursion
  15532. (condition-case nil
  15533. (progn
  15534. (org-back-to-heading t)
  15535. (outline-flag-region
  15536. (max (point-min) (1- (point)))
  15537. (save-excursion
  15538. (if (re-search-forward
  15539. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15540. (match-beginning 1)
  15541. (point-max)))
  15542. nil)
  15543. (org-cycle-hide-drawers 'children))
  15544. (error nil))))
  15545. (defun org-make-options-regexp (kwds &optional extra)
  15546. "Make a regular expression for keyword lines."
  15547. (concat
  15548. "^"
  15549. "#?[ \t]*\\+\\("
  15550. (mapconcat 'regexp-quote kwds "\\|")
  15551. (if extra (concat "\\|" extra))
  15552. "\\):[ \t]*"
  15553. "\\(.*\\)"))
  15554. ;; Make isearch reveal the necessary context
  15555. (defun org-isearch-end ()
  15556. "Reveal context after isearch exits."
  15557. (when isearch-success ; only if search was successful
  15558. (if (featurep 'xemacs)
  15559. ;; Under XEmacs, the hook is run in the correct place,
  15560. ;; we directly show the context.
  15561. (org-show-context 'isearch)
  15562. ;; In Emacs the hook runs *before* restoring the overlays.
  15563. ;; So we have to use a one-time post-command-hook to do this.
  15564. ;; (Emacs 22 has a special variable, see function `org-mode')
  15565. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15566. isearch-mode-end-hook-quit)
  15567. ;; Only when the isearch was not quitted.
  15568. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15569. 'append 'local)))))
  15570. (defun org-isearch-post-command ()
  15571. "Remove self from hook, and show context."
  15572. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15573. (org-show-context 'isearch))
  15574. ;;;; Integration with and fixes for other packages
  15575. ;;; Imenu support
  15576. (defvar org-imenu-markers nil
  15577. "All markers currently used by Imenu.")
  15578. (make-variable-buffer-local 'org-imenu-markers)
  15579. (defun org-imenu-new-marker (&optional pos)
  15580. "Return a new marker for use by Imenu, and remember the marker."
  15581. (let ((m (make-marker)))
  15582. (move-marker m (or pos (point)))
  15583. (push m org-imenu-markers)
  15584. m))
  15585. (defun org-imenu-get-tree ()
  15586. "Produce the index for Imenu."
  15587. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15588. (setq org-imenu-markers nil)
  15589. (let* ((n org-imenu-depth)
  15590. (re (concat "^" outline-regexp))
  15591. (subs (make-vector (1+ n) nil))
  15592. (last-level 0)
  15593. m level head)
  15594. (save-excursion
  15595. (save-restriction
  15596. (widen)
  15597. (goto-char (point-max))
  15598. (while (re-search-backward re nil t)
  15599. (setq level (org-reduced-level (funcall outline-level)))
  15600. (when (<= level n)
  15601. (looking-at org-complex-heading-regexp)
  15602. (setq head (org-link-display-format
  15603. (org-match-string-no-properties 4))
  15604. m (org-imenu-new-marker))
  15605. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15606. (if (>= level last-level)
  15607. (push (cons head m) (aref subs level))
  15608. (push (cons head (aref subs (1+ level))) (aref subs level))
  15609. (loop for i from (1+ level) to n do (aset subs i nil)))
  15610. (setq last-level level)))))
  15611. (aref subs 1)))
  15612. (eval-after-load "imenu"
  15613. '(progn
  15614. (add-hook 'imenu-after-jump-hook
  15615. (lambda ()
  15616. (if (eq major-mode 'org-mode)
  15617. (org-show-context 'org-goto))))))
  15618. (defun org-link-display-format (link)
  15619. "Replace a link with either the description, or the link target
  15620. if no description is present"
  15621. (save-match-data
  15622. (if (string-match org-bracket-link-analytic-regexp link)
  15623. (replace-match (or (match-string 5 link)
  15624. (concat (match-string 1 link)
  15625. (match-string 3 link)))
  15626. nil nil link)
  15627. link)))
  15628. ;; Speedbar support
  15629. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15630. "Overlay marking the agenda restriction line in speedbar.")
  15631. (org-overlay-put org-speedbar-restriction-lock-overlay
  15632. 'face 'org-agenda-restriction-lock)
  15633. (org-overlay-put org-speedbar-restriction-lock-overlay
  15634. 'help-echo "Agendas are currently limited to this item.")
  15635. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15636. (defun org-speedbar-set-agenda-restriction ()
  15637. "Restrict future agenda commands to the location at point in speedbar.
  15638. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15639. (interactive)
  15640. (require 'org-agenda)
  15641. (let (p m tp np dir txt)
  15642. (cond
  15643. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15644. 'org-imenu t))
  15645. (setq m (get-text-property p 'org-imenu-marker))
  15646. (save-excursion
  15647. (save-restriction
  15648. (set-buffer (marker-buffer m))
  15649. (goto-char m)
  15650. (org-agenda-set-restriction-lock 'subtree))))
  15651. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15652. 'speedbar-function 'speedbar-find-file))
  15653. (setq tp (previous-single-property-change
  15654. (1+ p) 'speedbar-function)
  15655. np (next-single-property-change
  15656. tp 'speedbar-function)
  15657. dir (speedbar-line-directory)
  15658. txt (buffer-substring-no-properties (or tp (point-min))
  15659. (or np (point-max))))
  15660. (save-excursion
  15661. (save-restriction
  15662. (set-buffer (find-file-noselect
  15663. (let ((default-directory dir))
  15664. (expand-file-name txt))))
  15665. (unless (org-mode-p)
  15666. (error "Cannot restrict to non-Org-mode file"))
  15667. (org-agenda-set-restriction-lock 'file))))
  15668. (t (error "Don't know how to restrict Org-mode's agenda")))
  15669. (org-move-overlay org-speedbar-restriction-lock-overlay
  15670. (point-at-bol) (point-at-eol))
  15671. (setq current-prefix-arg nil)
  15672. (org-agenda-maybe-redo)))
  15673. (eval-after-load "speedbar"
  15674. '(progn
  15675. (speedbar-add-supported-extension ".org")
  15676. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15677. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15678. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15679. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15680. (add-hook 'speedbar-visiting-tag-hook
  15681. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15682. ;;; Fixes and Hacks for problems with other packages
  15683. ;; Make flyspell not check words in links, to not mess up our keymap
  15684. (defun org-mode-flyspell-verify ()
  15685. "Don't let flyspell put overlays at active buttons."
  15686. (and (not (get-text-property (point) 'keymap))
  15687. (not (get-text-property (point) 'org-no-flyspell))))
  15688. (defun org-remove-flyspell-overlays-in (beg end)
  15689. "Remove flyspell overlays in region."
  15690. (and (org-bound-and-true-p flyspell-mode)
  15691. (fboundp 'flyspell-delete-region-overlays)
  15692. (flyspell-delete-region-overlays beg end))
  15693. (add-text-properties beg end '(org-no-flyspell t)))
  15694. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15695. (eval-after-load "bookmark"
  15696. '(if (boundp 'bookmark-after-jump-hook)
  15697. ;; We can use the hook
  15698. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15699. ;; Hook not available, use advice
  15700. (defadvice bookmark-jump (after org-make-visible activate)
  15701. "Make the position visible."
  15702. (org-bookmark-jump-unhide))))
  15703. ;; Make sure saveplace show the location if it was hidden
  15704. (eval-after-load "saveplace"
  15705. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15706. "Make the position visible."
  15707. (org-bookmark-jump-unhide)))
  15708. (defun org-bookmark-jump-unhide ()
  15709. "Unhide the current position, to show the bookmark location."
  15710. (and (org-mode-p)
  15711. (or (org-invisible-p)
  15712. (save-excursion (goto-char (max (point-min) (1- (point))))
  15713. (org-invisible-p)))
  15714. (org-show-context 'bookmark-jump)))
  15715. ;; Make session.el ignore our circular variable
  15716. (eval-after-load "session"
  15717. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15718. ;;;; Experimental code
  15719. (defun org-closed-in-range ()
  15720. "Sparse tree of items closed in a certain time range.
  15721. Still experimental, may disappear in the future."
  15722. (interactive)
  15723. ;; Get the time interval from the user.
  15724. (let* ((time1 (org-float-time
  15725. (org-read-date nil 'to-time nil "Starting date: ")))
  15726. (time2 (org-float-time
  15727. (org-read-date nil 'to-time nil "End date:")))
  15728. ;; callback function
  15729. (callback (lambda ()
  15730. (let ((time
  15731. (org-float-time
  15732. (apply 'encode-time
  15733. (org-parse-time-string
  15734. (match-string 1))))))
  15735. ;; check if time in interval
  15736. (and (>= time time1) (<= time time2))))))
  15737. ;; make tree, check each match with the callback
  15738. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15739. ;;;; Finish up
  15740. (provide 'org)
  15741. (run-hooks 'org-load-hook)
  15742. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15743. ;;; org.el ends here