org.el 602 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186
  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.25trans
  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-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.25trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (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)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  185. (defcustom org-support-shift-select nil
  186. "Non-nil means, make shift-cursor commands select text when possible.
  187. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  188. selecting a region, or enlarge thusly regions started in this way.
  189. In Org-mode, in special contexts, these same keys are used for other
  190. purposes, important enough to compete with shift selection. Org tries
  191. to balance these needs by supporting `shift-select-mode' outside these
  192. special contexts, under control of this variable.
  193. The default of this variable is nil, to avoid confusing behavior. Shifted
  194. cursor keys will then execute Org commands in the following contexts:
  195. - on a headline, changing TODO state (left/right) and priority (up/down)
  196. - on a time stamp, changing the time
  197. - in a plain list item, changing the bullet type
  198. - in a property definition line, switching between allowed values
  199. - in the BEGIN line of a clock table (changing the time block).
  200. Outside these contexts, the commands will throw an error.
  201. When this variable is t and the cursor is not in a special context,
  202. Org-mode will support shift-selection for making and enlarging regions.
  203. To make this more effective, the bullet cycling will no longer happen
  204. anywhere in an item line, but only if the cursor is exactly on the bullet.
  205. If you set this variable to the symbol `always', then the keys
  206. will not be special in headlines, property lines, and item lines, to make
  207. shift selection work there as well. If this is what you want, you can
  208. use the following alternative commands: `C-c C-t' and `C-c ,' to
  209. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  210. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  211. edited by hand or in column view.
  212. However, when the cursor is on a timestamp, shift-cursor commands
  213. will still edit the time stamp - this is just too good to give up.
  214. XEmacs user should have this variable set to nil, because shift-select-mode
  215. is Emacs 23 only."
  216. :group 'org
  217. :type '(choice
  218. (const :tag "Never" nil)
  219. (const :tag "When outside special context" t)
  220. (const :tag "Everywhere except timestamps" always)))
  221. (defgroup org-startup nil
  222. "Options concerning startup of Org-mode."
  223. :tag "Org Startup"
  224. :group 'org)
  225. (defcustom org-startup-folded t
  226. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: fold
  230. #+STARTUP: nofold
  231. #+STARTUP: content"
  232. :group 'org-startup
  233. :type '(choice
  234. (const :tag "nofold: show all" nil)
  235. (const :tag "fold: overview" t)
  236. (const :tag "content: all headlines" content)))
  237. (defcustom org-startup-truncated t
  238. "Non-nil means, entering Org-mode will set `truncate-lines'.
  239. This is useful since some lines containing links can be very long and
  240. uninteresting. Also tables look terrible when wrapped."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-startup-align-all-tables nil
  244. "Non-nil means, align all tables when visiting a file.
  245. This is useful when the column width in tables is forced with <N> cookies
  246. in table fields. Such tables will look correct only after the first re-align.
  247. This can also be configured on a per-file basis by adding one of
  248. the following lines anywhere in the buffer:
  249. #+STARTUP: align
  250. #+STARTUP: noalign"
  251. :group 'org-startup
  252. :type 'boolean)
  253. (defcustom org-insert-mode-line-in-empty-file nil
  254. "Non-nil means insert the first line setting Org-mode in empty files.
  255. When the function `org-mode' is called interactively in an empty file, this
  256. normally means that the file name does not automatically trigger Org-mode.
  257. To ensure that the file will always be in Org-mode in the future, a
  258. line enforcing Org-mode will be inserted into the buffer, if this option
  259. has been set."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-replace-disputed-keys nil
  263. "Non-nil means use alternative key bindings for some keys.
  264. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  265. These keys are also used by other packages like shift-selection-mode'
  266. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  267. If you want to use Org-mode together with one of these other modes,
  268. or more generally if you would like to move some Org-mode commands to
  269. other keys, set this variable and configure the keys with the variable
  270. `org-disputed-keys'.
  271. This option is only relevant at load-time of Org-mode, and must be set
  272. *before* org.el is loaded. Changing it requires a restart of Emacs to
  273. become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-use-extra-keys nil
  277. "Non-nil means use extra key sequence definitions for certain
  278. commands. This happens automatically if you run XEmacs or if
  279. window-system is nil. This variable lets you do the same
  280. manually. You must set it before loading org.
  281. Example: on Carbon Emacs 22 running graphically, with an external
  282. keyboard on a Powerbook, the default way of setting M-left might
  283. not work for either Alt or ESC. Setting this variable will make
  284. it work for ESC."
  285. :group 'org-startup
  286. :type 'boolean)
  287. (if (fboundp 'defvaralias)
  288. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  289. (defcustom org-disputed-keys
  290. '(([(shift up)] . [(meta p)])
  291. ([(shift down)] . [(meta n)])
  292. ([(shift left)] . [(meta -)])
  293. ([(shift right)] . [(meta +)])
  294. ([(control shift right)] . [(meta shift +)])
  295. ([(control shift left)] . [(meta shift -)]))
  296. "Keys for which Org-mode and other modes compete.
  297. This is an alist, cars are the default keys, second element specifies
  298. the alternative to use when `org-replace-disputed-keys' is t.
  299. Keys can be specified in any syntax supported by `define-key'.
  300. The value of this option takes effect only at Org-mode's startup,
  301. therefore you'll have to restart Emacs to apply it after changing."
  302. :group 'org-startup
  303. :type 'alist)
  304. (defun org-key (key)
  305. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  306. Or return the original if not disputed."
  307. (if org-replace-disputed-keys
  308. (let* ((nkey (key-description key))
  309. (x (org-find-if (lambda (x)
  310. (equal (key-description (car x)) nkey))
  311. org-disputed-keys)))
  312. (if x (cdr x) key))
  313. key))
  314. (defun org-find-if (predicate seq)
  315. (catch 'exit
  316. (while seq
  317. (if (funcall predicate (car seq))
  318. (throw 'exit (car seq))
  319. (pop seq)))))
  320. (defun org-defkey (keymap key def)
  321. "Define a key, possibly translated, as returned by `org-key'."
  322. (define-key keymap (org-key key) def))
  323. (defcustom org-ellipsis nil
  324. "The ellipsis to use in the Org-mode outline.
  325. When nil, just use the standard three dots. When a string, use that instead,
  326. When a face, use the standard 3 dots, but with the specified face.
  327. The change affects only Org-mode (which will then use its own display table).
  328. Changing this requires executing `M-x org-mode' in a buffer to become
  329. effective."
  330. :group 'org-startup
  331. :type '(choice (const :tag "Default" nil)
  332. (face :tag "Face" :value org-warning)
  333. (string :tag "String" :value "...#")))
  334. (defvar org-display-table nil
  335. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  336. (defgroup org-keywords nil
  337. "Keywords in Org-mode."
  338. :tag "Org Keywords"
  339. :group 'org)
  340. (defcustom org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].
  345. Changes become only effective after restarting Emacs."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].
  353. Changes become only effective after restarting Emacs."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-closed-string "CLOSED:"
  357. "String used as the prefix for timestamps logging closing a TODO entry."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-clock-string "CLOCK:"
  361. "String used as prefix for timestamps clocking work hours on an item."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-comment-string "COMMENT"
  365. "Entries starting with this keyword will never be exported.
  366. An entry can be toggled between COMMENT and normal with
  367. \\[org-toggle-comment].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-quote-string "QUOTE"
  372. "Entries starting with this keyword will be exported in fixed-width font.
  373. Quoting applies only to the text in the entry following the headline, and does
  374. not extend beyond the next headline, even if that is lower level.
  375. An entry can be toggled between QUOTE and normal with
  376. \\[org-toggle-fixed-width-section]."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defconst org-repeat-re
  380. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  381. "Regular expression for specifying repeated events.
  382. After a match, group 1 contains the repeat expression.")
  383. (defgroup org-structure nil
  384. "Options concerning the general structure of Org-mode files."
  385. :tag "Org Structure"
  386. :group 'org)
  387. (defgroup org-reveal-location nil
  388. "Options about how to make context of a location visible."
  389. :tag "Org Reveal Location"
  390. :group 'org-structure)
  391. (defconst org-context-choice
  392. '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean))))
  408. "Contexts for the reveal options.")
  409. (defcustom org-show-hierarchy-above '((default . t))
  410. "Non-nil means, show full hierarchy when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the hierarchy of headings
  413. above the exposed location is shown.
  414. Turning this off for example for sparse trees makes them very compact.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. Valid contexts are
  417. agenda when exposing an entry from the agenda
  418. org-goto when using the command `org-goto' on key C-c C-j
  419. occur-tree when using the command `org-occur' on key C-c /
  420. tags-tree when constructing a sparse tree based on tags matches
  421. link-search when exposing search matches associated with a link
  422. mark-goto when exposing the jump goal of a mark
  423. bookmark-jump when exposing a bookmark location
  424. isearch when exiting from an incremental search
  425. default default for all contexts not set explicitly"
  426. :group 'org-reveal-location
  427. :type org-context-choice)
  428. (defcustom org-show-following-heading '((default . nil))
  429. "Non-nil means, show following heading when revealing a location.
  430. Org-mode often shows locations in an org-mode file which might have
  431. been invisible before. When this is set, the heading following the
  432. match is shown.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-siblings '((default . nil) (isearch t))
  441. "Non-nil means, show all sibling heading when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the sibling of the current entry
  444. heading are all made visible. If `org-show-hierarchy-above' is t,
  445. the same happens on each level of the hierarchy above the current entry.
  446. By default this is on for the isearch context, off for all other contexts.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-entry-below '((default . nil))
  455. "Non-nil means, show the entry below a headline when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the text below the headline that is
  458. exposed is also shown.
  459. By default this is off for all contexts.
  460. Instead of t, this can also be an alist specifying this option for different
  461. contexts. See `org-show-hierarchy-above' for valid contexts."
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-indirect-buffer-display 'other-window
  465. "How should indirect tree buffers be displayed?
  466. This applies to indirect buffers created with the commands
  467. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  468. Valid values are:
  469. current-window Display in the current window
  470. other-window Just display in another window.
  471. dedicated-frame Create one new frame, and re-use it each time.
  472. new-frame Make a new frame each time. Note that in this case
  473. previously-made indirect buffers are kept, and you need to
  474. kill these buffers yourself."
  475. :group 'org-structure
  476. :group 'org-agenda-windows
  477. :type '(choice
  478. (const :tag "In current window" current-window)
  479. (const :tag "In current frame, other window" other-window)
  480. (const :tag "Each time a new frame" new-frame)
  481. (const :tag "One dedicated frame" dedicated-frame)))
  482. (defgroup org-cycle nil
  483. "Options concerning visibility cycling in Org-mode."
  484. :tag "Org Cycle"
  485. :group 'org-structure)
  486. (defcustom org-cycle-max-level nil
  487. "Maximum level which should still be subject to visibility cycling.
  488. Levels higher than this will, for cycling, be treated as text, not a headline.
  489. When `org-odd-levels-only' is set, a value of N in this variable actually
  490. means 2N-1 stars as the limiting headline.
  491. When nil, cycle all levels.
  492. Note that the limiting level of cycling is also influenced by
  493. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  494. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  495. than its value."
  496. :group 'org-cycle
  497. :type '(choice
  498. (const :tag "No limit" nil)
  499. (integer :tag "Maximum level")))
  500. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  501. "Names of drawers. Drawers are not opened by cycling on the headline above.
  502. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  503. this:
  504. :DRAWERNAME:
  505. .....
  506. :END:
  507. The drawer \"PROPERTIES\" is special for capturing properties through
  508. the property API.
  509. Drawers can be defined on the per-file basis with a line like:
  510. #+DRAWERS: HIDDEN STATE PROPERTIES"
  511. :group 'org-structure
  512. :group 'org-cycle
  513. :type '(repeat (string :tag "Drawer Name")))
  514. (defcustom org-cycle-global-at-bob nil
  515. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  516. This makes it possible to do global cycling without having to use S-TAB or
  517. C-u TAB. For this special case to work, the first line of the buffer
  518. must not be a headline - it may be empty or some other text. When used in
  519. this way, `org-cycle-hook' is disables temporarily, to make sure the
  520. cursor stays at the beginning of the buffer.
  521. When this option is nil, don't do anything special at the beginning
  522. of the buffer."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-emulate-tab t
  526. "Where should `org-cycle' emulate TAB.
  527. nil Never
  528. white Only in completely white lines
  529. whitestart Only at the beginning of lines, before the first non-white char
  530. t Everywhere except in headlines
  531. exc-hl-bol Everywhere except at the start of a headline
  532. If TAB is used in a place where it does not emulate TAB, the current subtree
  533. visibility is cycled."
  534. :group 'org-cycle
  535. :type '(choice (const :tag "Never" nil)
  536. (const :tag "Only in completely white lines" white)
  537. (const :tag "Before first char in a line" whitestart)
  538. (const :tag "Everywhere except in headlines" t)
  539. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  540. ))
  541. (defcustom org-cycle-separator-lines 2
  542. "Number of empty lines needed to keep an empty line between collapsed trees.
  543. If you leave an empty line between the end of a subtree and the following
  544. headline, this empty line is hidden when the subtree is folded.
  545. Org-mode will leave (exactly) one empty line visible if the number of
  546. empty lines is equal or larger to the number given in this variable.
  547. So the default 2 means, at least 2 empty lines after the end of a subtree
  548. are needed to produce free space between a collapsed subtree and the
  549. following headline.
  550. Special case: when 0, never leave empty lines in collapsed view."
  551. :group 'org-cycle
  552. :type 'integer)
  553. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  554. (defcustom org-pre-cycle-hook nil
  555. "Hook that is run before visibility cycling is happening.
  556. The function(s) in this hook must accept a single argument which indicates
  557. the new state that will be set right after running this hook. The
  558. argument is a symbol. Before a global state change, it can have the values
  559. `overview', `content', or `all'. Before a local state change, it can have
  560. the values `folded', `children', or `subtree'."
  561. :group 'org-cycle
  562. :type 'hook)
  563. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  564. org-cycle-hide-drawers
  565. org-cycle-show-empty-lines
  566. org-optimize-window-after-visibility-change)
  567. "Hook that is run after `org-cycle' has changed the buffer visibility.
  568. The function(s) in this hook must accept a single argument which indicates
  569. the new state that was set by the most recent `org-cycle' command. The
  570. argument is a symbol. After a global state change, it can have the values
  571. `overview', `content', or `all'. After a local state change, it can have
  572. the values `folded', `children', or `subtree'."
  573. :group 'org-cycle
  574. :type 'hook)
  575. (defgroup org-edit-structure nil
  576. "Options concerning structure editing in Org-mode."
  577. :tag "Org Edit Structure"
  578. :group 'org-structure)
  579. (defcustom org-odd-levels-only nil
  580. "Non-nil means, skip even levels and only use odd levels for the outline.
  581. This has the effect that two stars are being added/taken away in
  582. promotion/demotion commands. It also influences how levels are
  583. handled by the exporters.
  584. Changing it requires restart of `font-lock-mode' to become effective
  585. for fontification also in regions already fontified.
  586. You may also set this on a per-file basis by adding one of the following
  587. lines to the buffer:
  588. #+STARTUP: odd
  589. #+STARTUP: oddeven"
  590. :group 'org-edit-structure
  591. :group 'org-font-lock
  592. :type 'boolean)
  593. (defcustom org-adapt-indentation t
  594. "Non-nil means, adapt indentation when promoting and demoting.
  595. When this is set and the *entire* text in an entry is indented, the
  596. indentation is increased by one space in a demotion command, and
  597. decreased by one in a promotion command. If any line in the entry
  598. body starts at column 0, indentation is not changed at all."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-special-ctrl-a/e nil
  602. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  603. When t, `C-a' will bring back the cursor to the beginning of the
  604. headline text, i.e. after the stars and after a possible TODO keyword.
  605. In an item, this will be the position after the bullet.
  606. When the cursor is already at that position, another `C-a' will bring
  607. it to the beginning of the line.
  608. `C-e' will jump to the end of the headline, ignoring the presence of tags
  609. in the headline. A second `C-e' will then jump to the true end of the
  610. line, after any tags.
  611. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  612. and only a directly following, identical keypress will bring the cursor
  613. to the special positions.
  614. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  615. set separately."
  616. :group 'org-edit-structure
  617. :type '(choice
  618. (const :tag "off" nil)
  619. (const :tag "after stars/bullet and before tags first" t)
  620. (const :tag "true line boundary first" reversed)
  621. (cons :tag "Set C-a and C-e separately"
  622. (choice :tag "Special C-a"
  623. (const :tag "off" nil)
  624. (const :tag "after stars/bullet first" t)
  625. (const :tag "before stars/bullet first" reversed))
  626. (choice :tag "Special C-e"
  627. (const :tag "off" nil)
  628. (const :tag "before tags first" t)
  629. (const :tag "after tags first" reversed)))))
  630. (if (fboundp 'defvaralias)
  631. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  632. (defcustom org-special-ctrl-k nil
  633. "Non-nil means `C-k' will behave specially in headlines.
  634. When nil, `C-k' will call the default `kill-line' command.
  635. When t, the following will happen while the cursor is in the headline:
  636. - When the cursor is at the beginning of a headline, kill the entire
  637. line and possible the folded subtree below the line.
  638. - When in the middle of the headline text, kill the headline up to the tags.
  639. - When after the headline text, kill the tags."
  640. :group 'org-edit-structure
  641. :type 'boolean)
  642. (defcustom org-yank-folded-subtrees t
  643. "Non-nil means, when yanking subtrees, fold them.
  644. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  645. it starts with a heading and all other headings in it are either children
  646. or siblings, then fold all the subtrees. However, do this only if no
  647. text after the yank would be swallowed into a folded tree by this action."
  648. :group 'org-edit-structure
  649. :type 'boolean)
  650. (defcustom org-yank-adjusted-subtrees nil
  651. "Non-nil means, when yanking subtrees, adjust the level.
  652. With this setting, `org-paste-subtree' is used to insert the subtree, see
  653. this function for details."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-M-RET-may-split-line '((default . t))
  657. "Non-nil means, M-RET will split the line at the cursor position.
  658. When nil, it will go to the end of the line before making a
  659. new line.
  660. You may also set this option in a different way for different
  661. contexts. Valid contexts are:
  662. headline when creating a new headline
  663. item when creating a new item
  664. table in a table field
  665. default the value to be used for all contexts not explicitly
  666. customized"
  667. :group 'org-structure
  668. :group 'org-table
  669. :type '(choice
  670. (const :tag "Always" t)
  671. (const :tag "Never" nil)
  672. (repeat :greedy t :tag "Individual contexts"
  673. (cons
  674. (choice :tag "Context"
  675. (const headline)
  676. (const item)
  677. (const table)
  678. (const default))
  679. (boolean)))))
  680. (defcustom org-insert-heading-respect-content nil
  681. "Non-nil means, insert new headings after the current subtree.
  682. When nil, the new heading is created directly after the current line.
  683. The commands \\[org-insert-heading-respect-content] and
  684. \\[org-insert-todo-heading-respect-content] turn this variable on
  685. for the duration of the command."
  686. :group 'org-structure
  687. :type 'boolean)
  688. (defcustom org-blank-before-new-entry '((heading . auto)
  689. (plain-list-item . auto))
  690. "Should `org-insert-heading' leave a blank line before new heading/item?
  691. The value is an alist, with `heading' and `plain-list-item' as car,
  692. and a boolean flag as cdr. For plain lists, if the variable
  693. `org-empty-line-terminates-plain-lists' is set, the setting here
  694. is ignored and no empty line is inserted, to keep the list in tact."
  695. :group 'org-edit-structure
  696. :type '(list
  697. (cons (const heading)
  698. (choice (const :tag "Never" nil)
  699. (const :tag "Always" t)
  700. (const :tag "Auto" auto)))
  701. (cons (const plain-list-item)
  702. (choice (const :tag "Never" nil)
  703. (const :tag "Always" t)
  704. (const :tag "Auto" auto)))))
  705. (defcustom org-insert-heading-hook nil
  706. "Hook being run after inserting a new heading."
  707. :group 'org-edit-structure
  708. :type 'hook)
  709. (defcustom org-enable-fixed-width-editor t
  710. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  711. This currently only means, they are never auto-wrapped.
  712. When nil, such lines will be treated like ordinary lines.
  713. See also the QUOTE keyword."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defcustom org-edit-src-region-extra nil
  717. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  718. For examples see the function `org-edit-src-find-region-and-lang'.
  719. The regular expression identifying the begin marker should end with a newline,
  720. and the regexp marking the end line should start with a newline, to make sure
  721. there are kept outside the narrowed region."
  722. :group 'org-edit-structure
  723. :type '(repeat
  724. (list
  725. (regexp :tag "begin regexp")
  726. (regexp :tag "end regexp")
  727. (choice :tag "language"
  728. (string :tag "specify")
  729. (integer :tag "from match group")
  730. (const :tag "from `lang' element")
  731. (const :tag "from `style' element")))))
  732. (defcustom org-coderef-label-format "(ref:%s)"
  733. "The default coderef format.
  734. This format string will be used to search for coderef labels in literal
  735. examples (EXAMPLE and SRC blocks). The format can be overwritten
  736. an individual literal example with the -f option, like
  737. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  738. ...
  739. #+END_SRC
  740. If you want to use this for HTML export, make sure that the format does
  741. not introduce special font-locking, and avoid the HTML special
  742. characters `<', `>', and `&'. The reason for this restriction is that
  743. the labels are searched for only after htmlize has done its job."
  744. :group 'org-edit-structure ; FIXME this is not in the right group
  745. :type 'string)
  746. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  747. "The mode that should be used to edit fixed-width regions.
  748. These are the regions where each line starts with a colon."
  749. :group 'org-edit-structure
  750. :type '(choice
  751. (const artist-mode)
  752. (const picture-mode)
  753. (const fundamental-mode)
  754. (function :tag "Other (specify)")))
  755. (defcustom org-goto-auto-isearch t
  756. "Non-nil means, typing characters in org-goto starts incremental search."
  757. :group 'org-edit-structure
  758. :type 'boolean)
  759. (defgroup org-sparse-trees nil
  760. "Options concerning sparse trees in Org-mode."
  761. :tag "Org Sparse Trees"
  762. :group 'org-structure)
  763. (defcustom org-highlight-sparse-tree-matches t
  764. "Non-nil means, highlight all matches that define a sparse tree.
  765. The highlights will automatically disappear the next time the buffer is
  766. changed by an edit command."
  767. :group 'org-sparse-trees
  768. :type 'boolean)
  769. (defcustom org-remove-highlights-with-change t
  770. "Non-nil means, any change to the buffer will remove temporary highlights.
  771. Such highlights are created by `org-occur' and `org-clock-display'.
  772. When nil, `C-c C-c needs to be used to get rid of the highlights.
  773. The highlights created by `org-preview-latex-fragment' always need
  774. `C-c C-c' to be removed."
  775. :group 'org-sparse-trees
  776. :group 'org-time
  777. :type 'boolean)
  778. (defcustom org-occur-hook '(org-first-headline-recenter)
  779. "Hook that is run after `org-occur' has constructed a sparse tree.
  780. This can be used to recenter the window to show as much of the structure
  781. as possible."
  782. :group 'org-sparse-trees
  783. :type 'hook)
  784. (defgroup org-imenu-and-speedbar nil
  785. "Options concerning imenu and speedbar in Org-mode."
  786. :tag "Org Imenu and Speedbar"
  787. :group 'org-structure)
  788. (defcustom org-imenu-depth 2
  789. "The maximum level for Imenu access to Org-mode headlines.
  790. This also applied for speedbar access."
  791. :group 'org-imenu-and-speedbar
  792. :type 'integer)
  793. (defgroup org-table nil
  794. "Options concerning tables in Org-mode."
  795. :tag "Org Table"
  796. :group 'org)
  797. (defcustom org-enable-table-editor 'optimized
  798. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  799. When nil, such lines will be treated like ordinary lines.
  800. When equal to the symbol `optimized', the table editor will be optimized to
  801. do the following:
  802. - Automatic overwrite mode in front of whitespace in table fields.
  803. This makes the structure of the table stay in tact as long as the edited
  804. field does not exceed the column width.
  805. - Minimize the number of realigns. Normally, the table is aligned each time
  806. TAB or RET are pressed to move to another field. With optimization this
  807. happens only if changes to a field might have changed the column width.
  808. Optimization requires replacing the functions `self-insert-command',
  809. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  810. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  811. very good at guessing when a re-align will be necessary, but you can always
  812. force one with \\[org-ctrl-c-ctrl-c].
  813. If you would like to use the optimized version in Org-mode, but the
  814. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  815. This variable can be used to turn on and off the table editor during a session,
  816. but in order to toggle optimization, a restart is required.
  817. See also the variable `org-table-auto-blank-field'."
  818. :group 'org-table
  819. :type '(choice
  820. (const :tag "off" nil)
  821. (const :tag "on" t)
  822. (const :tag "on, optimized" optimized)))
  823. (defcustom org-self-insert-cluster-for-undo t
  824. "Non-nil means cluster self-insert commands for undo when possible.
  825. If this is set, then, like in the Emacs command loop, 20 consequtive
  826. characters will be undone together.
  827. This is configurable, because there is some impact on typing performance."
  828. :group 'org-table
  829. :type 'boolean)
  830. (defcustom org-table-tab-recognizes-table.el t
  831. "Non-nil means, TAB will automatically notice a table.el table.
  832. When it sees such a table, it moves point into it and - if necessary -
  833. calls `table-recognize-table'."
  834. :group 'org-table-editing
  835. :type 'boolean)
  836. (defgroup org-link nil
  837. "Options concerning links in Org-mode."
  838. :tag "Org Link"
  839. :group 'org)
  840. (defvar org-link-abbrev-alist-local nil
  841. "Buffer-local version of `org-link-abbrev-alist', which see.
  842. The value of this is taken from the #+LINK lines.")
  843. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  844. (defcustom org-link-abbrev-alist nil
  845. "Alist of link abbreviations.
  846. The car of each element is a string, to be replaced at the start of a link.
  847. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  848. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  849. [[linkkey:tag][description]]
  850. The 'linkkey' must be a word word, starting with a letter, followed
  851. by letters, numbers, '-' or '_'.
  852. If REPLACE is a string, the tag will simply be appended to create the link.
  853. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  854. the placeholder \"%h\" will cause a url-encoded version of the tag to
  855. be inserted at that point (see the function `url-hexify-string').
  856. REPLACE may also be a function that will be called with the tag as the
  857. only argument to create the link, which should be returned as a string.
  858. See the manual for examples."
  859. :group 'org-link
  860. :type '(repeat
  861. (cons
  862. (string :tag "Protocol")
  863. (choice
  864. (string :tag "Format")
  865. (function)))))
  866. (defcustom org-descriptive-links t
  867. "Non-nil means, hide link part and only show description of bracket links.
  868. Bracket links are like [[link][description]]. This variable sets the initial
  869. state in new org-mode buffers. The setting can then be toggled on a
  870. per-buffer basis from the Org->Hyperlinks menu."
  871. :group 'org-link
  872. :type 'boolean)
  873. (defcustom org-link-file-path-type 'adaptive
  874. "How the path name in file links should be stored.
  875. Valid values are:
  876. relative Relative to the current directory, i.e. the directory of the file
  877. into which the link is being inserted.
  878. absolute Absolute path, if possible with ~ for home directory.
  879. noabbrev Absolute path, no abbreviation of home directory.
  880. adaptive Use relative path for files in the current directory and sub-
  881. directories of it. For other files, use an absolute path."
  882. :group 'org-link
  883. :type '(choice
  884. (const relative)
  885. (const absolute)
  886. (const noabbrev)
  887. (const adaptive)))
  888. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  889. "Types of links that should be activated in Org-mode files.
  890. This is a list of symbols, each leading to the activation of a certain link
  891. type. In principle, it does not hurt to turn on most link types - there may
  892. be a small gain when turning off unused link types. The types are:
  893. bracket The recommended [[link][description]] or [[link]] links with hiding.
  894. angular Links in angular brackets that may contain whitespace like
  895. <bbdb:Carsten Dominik>.
  896. plain Plain links in normal text, no whitespace, like http://google.com.
  897. radio Text that is matched by a radio target, see manual for details.
  898. tag Tag settings in a headline (link to tag search).
  899. date Time stamps (link to calendar).
  900. footnote Footnote labels.
  901. Changing this variable requires a restart of Emacs to become effective."
  902. :group 'org-link
  903. :type '(set :greedy t
  904. (const :tag "Double bracket links (new style)" bracket)
  905. (const :tag "Angular bracket links (old style)" angular)
  906. (const :tag "Plain text links" plain)
  907. (const :tag "Radio target matches" radio)
  908. (const :tag "Tags" tag)
  909. (const :tag "Timestamps" date)
  910. (const :tag "Footnotes" footnote)))
  911. (defcustom org-make-link-description-function nil
  912. "Function to use to generate link descriptions from links. If
  913. nil the link location will be used. This function must take two
  914. parameters; the first is the link and the second the description
  915. org-insert-link has generated, and should return the description
  916. to use."
  917. :group 'org-link
  918. :type 'function)
  919. (defgroup org-link-store nil
  920. "Options concerning storing links in Org-mode."
  921. :tag "Org Store Link"
  922. :group 'org-link)
  923. (defcustom org-email-link-description-format "Email %c: %.30s"
  924. "Format of the description part of a link to an email or usenet message.
  925. The following %-escapes will be replaced by corresponding information:
  926. %F full \"From\" field
  927. %f name, taken from \"From\" field, address if no name
  928. %T full \"To\" field
  929. %t first name in \"To\" field, address if no name
  930. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  931. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  932. %s subject
  933. %m message-id.
  934. You may use normal field width specification between the % and the letter.
  935. This is for example useful to limit the length of the subject.
  936. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  937. :group 'org-link-store
  938. :type 'string)
  939. (defcustom org-from-is-user-regexp
  940. (let (r1 r2)
  941. (when (and user-mail-address (not (string= user-mail-address "")))
  942. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  943. (when (and user-full-name (not (string= user-full-name "")))
  944. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  945. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  946. "Regexp matched against the \"From:\" header of an email or usenet message.
  947. It should match if the message is from the user him/herself."
  948. :group 'org-link-store
  949. :type 'regexp)
  950. (defcustom org-link-to-org-use-id 'create-if-interactive
  951. "Non-nil means, storing a link to an Org file will use entry IDs.
  952. Note that before this variable is even considered, org-id must be loaded,
  953. to please customize `org-modules' and turn it on.
  954. The variable can have the following values:
  955. t Create an ID if needed to make a link to the current entry.
  956. create-if-interactive
  957. If `org-store-link' is called directly (interactively, as a user
  958. command), do create an ID to support the link. But when doing the
  959. job for remember, only use the ID if it already exists. The
  960. purpose of this setting is to avoid proliferation of unwanted
  961. IDs, just because you happen to be in an Org file when you
  962. call `org-remember' that automatically and preemptively
  963. creates a link. If you do want to get an ID link in a remember
  964. template to an entry not having an ID, create it first by
  965. explicitly creating a link to it, using `C-c C-l' first.
  966. use-existing
  967. Use existing ID, do not create one.
  968. nil Never use an ID to make a link, instead link using a text search for
  969. the headline text."
  970. :group 'org-link-store
  971. :type '(choice
  972. (const :tag "Create ID to make link" t)
  973. (const :tag "Create if string link interactively"
  974. 'create-if-interactive)
  975. (const :tag "Only use existing" 'use-existing)
  976. (const :tag "Do not use ID to create link" nil)))
  977. (defcustom org-context-in-file-links t
  978. "Non-nil means, file links from `org-store-link' contain context.
  979. A search string will be added to the file name with :: as separator and
  980. used to find the context when the link is activated by the command
  981. `org-open-at-point'.
  982. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  983. negates this setting for the duration of the command."
  984. :group 'org-link-store
  985. :type 'boolean)
  986. (defcustom org-keep-stored-link-after-insertion nil
  987. "Non-nil means, keep link in list for entire session.
  988. The command `org-store-link' adds a link pointing to the current
  989. location to an internal list. These links accumulate during a session.
  990. The command `org-insert-link' can be used to insert links into any
  991. Org-mode file (offering completion for all stored links). When this
  992. option is nil, every link which has been inserted once using \\[org-insert-link]
  993. will be removed from the list, to make completing the unused links
  994. more efficient."
  995. :group 'org-link-store
  996. :type 'boolean)
  997. (defgroup org-link-follow nil
  998. "Options concerning following links in Org-mode."
  999. :tag "Org Follow Link"
  1000. :group 'org-link)
  1001. (defcustom org-link-translation-function nil
  1002. "Function to translate links with different syntax to Org syntax.
  1003. This can be used to translate links created for example by the Planner
  1004. or emacs-wiki packages to Org syntax.
  1005. The function must accept two parameters, a TYPE containing the link
  1006. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1007. which is everything after the link protocol. It should return a cons
  1008. with possibly modified values of type and path.
  1009. Org contains a function for this, so if you set this variable to
  1010. `org-translate-link-from-planner', you should be able follow many
  1011. links created by planner."
  1012. :group 'org-link-follow
  1013. :type 'function)
  1014. (defcustom org-follow-link-hook nil
  1015. "Hook that is run after a link has been followed."
  1016. :group 'org-link-follow
  1017. :type 'hook)
  1018. (defcustom org-tab-follows-link nil
  1019. "Non-nil means, on links TAB will follow the link.
  1020. Needs to be set before org.el is loaded.
  1021. This really should not be used, it does not make sense, and the
  1022. implementation is bad."
  1023. :group 'org-link-follow
  1024. :type 'boolean)
  1025. (defcustom org-return-follows-link nil
  1026. "Non-nil means, on links RET will follow the link.
  1027. Needs to be set before org.el is loaded."
  1028. :group 'org-link-follow
  1029. :type 'boolean)
  1030. (defcustom org-mouse-1-follows-link
  1031. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1032. "Non-nil means, mouse-1 on a link will follow the link.
  1033. A longer mouse click will still set point. Does not work on XEmacs.
  1034. Needs to be set before org.el is loaded."
  1035. :group 'org-link-follow
  1036. :type 'boolean)
  1037. (defcustom org-mark-ring-length 4
  1038. "Number of different positions to be recorded in the ring
  1039. Changing this requires a restart of Emacs to work correctly."
  1040. :group 'org-link-follow
  1041. :type 'integer)
  1042. (defcustom org-link-frame-setup
  1043. '((vm . vm-visit-folder-other-frame)
  1044. (gnus . gnus-other-frame)
  1045. (file . find-file-other-window))
  1046. "Setup the frame configuration for following links.
  1047. When following a link with Emacs, it may often be useful to display
  1048. this link in another window or frame. This variable can be used to
  1049. set this up for the different types of links.
  1050. For VM, use any of
  1051. `vm-visit-folder'
  1052. `vm-visit-folder-other-frame'
  1053. For Gnus, use any of
  1054. `gnus'
  1055. `gnus-other-frame'
  1056. `org-gnus-no-new-news'
  1057. For FILE, use any of
  1058. `find-file'
  1059. `find-file-other-window'
  1060. `find-file-other-frame'
  1061. For the calendar, use the variable `calendar-setup'.
  1062. For BBDB, it is currently only possible to display the matches in
  1063. another window."
  1064. :group 'org-link-follow
  1065. :type '(list
  1066. (cons (const vm)
  1067. (choice
  1068. (const vm-visit-folder)
  1069. (const vm-visit-folder-other-window)
  1070. (const vm-visit-folder-other-frame)))
  1071. (cons (const gnus)
  1072. (choice
  1073. (const gnus)
  1074. (const gnus-other-frame)
  1075. (const org-gnus-no-new-news)))
  1076. (cons (const file)
  1077. (choice
  1078. (const find-file)
  1079. (const find-file-other-window)
  1080. (const find-file-other-frame)))))
  1081. (defcustom org-display-internal-link-with-indirect-buffer nil
  1082. "Non-nil means, use indirect buffer to display infile links.
  1083. Activating internal links (from one location in a file to another location
  1084. in the same file) normally just jumps to the location. When the link is
  1085. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1086. another window. When this option is set, the other window actually displays
  1087. an indirect buffer clone of the current buffer, to avoid any visibility
  1088. changes to the current buffer."
  1089. :group 'org-link-follow
  1090. :type 'boolean)
  1091. (defcustom org-open-non-existing-files nil
  1092. "Non-nil means, `org-open-file' will open non-existing files.
  1093. When nil, an error will be generated."
  1094. :group 'org-link-follow
  1095. :type 'boolean)
  1096. (defcustom org-open-directory-means-index-dot-org nil
  1097. "Non-nil means, a link to a directory really means to index.org.
  1098. When nil, following a directory link will run dired or open a finder/explorer
  1099. window on that directory."
  1100. :group 'org-link-follow
  1101. :type 'boolean)
  1102. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1103. "Function and arguments to call for following mailto links.
  1104. This is a list with the first element being a lisp function, and the
  1105. remaining elements being arguments to the function. In string arguments,
  1106. %a will be replaced by the address, and %s will be replaced by the subject
  1107. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1108. :group 'org-link-follow
  1109. :type '(choice
  1110. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1111. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1112. (const :tag "message-mail" (message-mail "%a" "%s"))
  1113. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1114. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1115. "Non-nil means, ask for confirmation before executing shell links.
  1116. Shell links can be dangerous: just think about a link
  1117. [[shell:rm -rf ~/*][Google Search]]
  1118. This link would show up in your Org-mode document as \"Google Search\",
  1119. but really it would remove your entire home directory.
  1120. Therefore we advise against setting this variable to nil.
  1121. Just change it to `y-or-n-p' if you want to confirm with a
  1122. single keystroke rather than having to type \"yes\"."
  1123. :group 'org-link-follow
  1124. :type '(choice
  1125. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1126. (const :tag "with y-or-n (faster)" y-or-n-p)
  1127. (const :tag "no confirmation (dangerous)" nil)))
  1128. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1129. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1130. Elisp links can be dangerous: just think about a link
  1131. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1132. This link would show up in your Org-mode document as \"Google Search\",
  1133. but really it would remove your entire home directory.
  1134. Therefore we advise against setting this variable to nil.
  1135. Just change it to `y-or-n-p' if you want to confirm with a
  1136. single keystroke rather than having to type \"yes\"."
  1137. :group 'org-link-follow
  1138. :type '(choice
  1139. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1140. (const :tag "with y-or-n (faster)" y-or-n-p)
  1141. (const :tag "no confirmation (dangerous)" nil)))
  1142. (defconst org-file-apps-defaults-gnu
  1143. '((remote . emacs)
  1144. (system . mailcap)
  1145. (t . mailcap))
  1146. "Default file applications on a UNIX or GNU/Linux system.
  1147. See `org-file-apps'.")
  1148. (defconst org-file-apps-defaults-macosx
  1149. '((remote . emacs)
  1150. (t . "open %s")
  1151. (system . "open %s")
  1152. ("ps.gz" . "gv %s")
  1153. ("eps.gz" . "gv %s")
  1154. ("dvi" . "xdvi %s")
  1155. ("fig" . "xfig %s"))
  1156. "Default file applications on a MacOS X system.
  1157. The system \"open\" is known as a default, but we use X11 applications
  1158. for some files for which the OS does not have a good default.
  1159. See `org-file-apps'.")
  1160. (defconst org-file-apps-defaults-windowsnt
  1161. (list
  1162. '(remote . emacs)
  1163. (cons t
  1164. (list (if (featurep 'xemacs)
  1165. 'mswindows-shell-execute
  1166. 'w32-shell-execute)
  1167. "open" 'file))
  1168. (cons 'system
  1169. (list (if (featurep 'xemacs)
  1170. 'mswindows-shell-execute
  1171. 'w32-shell-execute)
  1172. "open" 'file)))
  1173. "Default file applications on a Windows NT system.
  1174. The system \"open\" is used for most files.
  1175. See `org-file-apps'.")
  1176. (defcustom org-file-apps
  1177. '(
  1178. (auto-mode . emacs)
  1179. ("\\.x?html?\\'" . default)
  1180. ("\\.pdf\\'" . default)
  1181. )
  1182. "External applications for opening `file:path' items in a document.
  1183. Org-mode uses system defaults for different file types, but
  1184. you can use this variable to set the application for a given file
  1185. extension. The entries in this list are cons cells where the car identifies
  1186. files and the cdr the corresponding command. Possible values for the
  1187. file identifier are
  1188. \"regex\" Regular expression matched against the file name. For backward
  1189. compatibility, this can also be a string with only alphanumeric
  1190. characters, which is then interpreted as an extension.
  1191. `directory' Matches a directory
  1192. `remote' Matches a remote file, accessible through tramp or efs.
  1193. Remote files most likely should be visited through Emacs
  1194. because external applications cannot handle such paths.
  1195. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1196. so all files Emacs knows how to handle. Using this with
  1197. command `emacs' will open most files in Emacs. Beware that this
  1198. will also open html files inside Emacs, unless you add
  1199. (\"html\" . default) to the list as well.
  1200. t Default for files not matched by any of the other options.
  1201. `system' The system command to open files, like `open' on Windows
  1202. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1203. that will be selected if you call `C-c C-o' with a double
  1204. `C-u C-u' prefix.
  1205. Possible values for the command are:
  1206. `emacs' The file will be visited by the current Emacs process.
  1207. `default' Use the default application for this file type, which is the
  1208. association for t in the list, most likely in the system-specific
  1209. part.
  1210. This can be used to overrule an unwanted setting in the
  1211. system-specific variable.
  1212. `system' Use the system command for opening files, like \"open\".
  1213. This command is specified by the entry whose car is `system'.
  1214. Most likely, the system-specific version of this variable
  1215. does define this command, but you can overrule/replace it
  1216. here.
  1217. string A command to be executed by a shell; %s will be replaced
  1218. by the path to the file.
  1219. sexp A Lisp form which will be evaluated. The file path will
  1220. be available in the Lisp variable `file'.
  1221. For more examples, see the system specific constants
  1222. `org-file-apps-defaults-macosx'
  1223. `org-file-apps-defaults-windowsnt'
  1224. `org-file-apps-defaults-gnu'."
  1225. :group 'org-link-follow
  1226. :type '(repeat
  1227. (cons (choice :value ""
  1228. (string :tag "Extension")
  1229. (const :tag "System command to open files" system)
  1230. (const :tag "Default for unrecognized files" t)
  1231. (const :tag "Remote file" remote)
  1232. (const :tag "Links to a directory" directory)
  1233. (const :tag "Any files that have Emacs modes"
  1234. auto-mode))
  1235. (choice :value ""
  1236. (const :tag "Visit with Emacs" emacs)
  1237. (const :tag "Use default" default)
  1238. (const :tag "Use the system command" system)
  1239. (string :tag "Command")
  1240. (sexp :tag "Lisp form")))))
  1241. (defgroup org-refile nil
  1242. "Options concerning refiling entries in Org-mode."
  1243. :tag "Org Refile"
  1244. :group 'org)
  1245. (defcustom org-directory "~/org"
  1246. "Directory with org files.
  1247. This is just a default location to look for Org files. There is no need
  1248. at all to put your files into this directory. It is only used in the
  1249. following situations:
  1250. 1. When a remember template specifies a target file that is not an
  1251. absolute path. The path will then be interpreted relative to
  1252. `org-directory'
  1253. 2. When a remember note is filed away in an interactive way (when exiting the
  1254. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1255. with `org-directory' as the default path."
  1256. :group 'org-refile
  1257. :group 'org-remember
  1258. :type 'directory)
  1259. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1260. "Default target for storing notes.
  1261. Used by the hooks for remember.el. This can be a string, or nil to mean
  1262. the value of `remember-data-file'.
  1263. You can set this on a per-template basis with the variable
  1264. `org-remember-templates'."
  1265. :group 'org-refile
  1266. :group 'org-remember
  1267. :type '(choice
  1268. (const :tag "Default from remember-data-file" nil)
  1269. file))
  1270. (defcustom org-goto-interface 'outline
  1271. "The default interface to be used for `org-goto'.
  1272. Allowed values are:
  1273. outline The interface shows an outline of the relevant file
  1274. and the correct heading is found by moving through
  1275. the outline or by searching with incremental search.
  1276. outline-path-completion Headlines in the current buffer are offered via
  1277. completion. This is the interface also used by
  1278. the refile command."
  1279. :group 'org-refile
  1280. :type '(choice
  1281. (const :tag "Outline" outline)
  1282. (const :tag "Outline-path-completion" outline-path-completion)))
  1283. (defcustom org-goto-max-level 5
  1284. "Maximum level to be considered when running org-goto with refile interface."
  1285. :group 'org-refile
  1286. :type 'integer)
  1287. (defcustom org-reverse-note-order nil
  1288. "Non-nil means, store new notes at the beginning of a file or entry.
  1289. When nil, new notes will be filed to the end of a file or entry.
  1290. This can also be a list with cons cells of regular expressions that
  1291. are matched against file names, and values."
  1292. :group 'org-remember
  1293. :group 'org-refile
  1294. :type '(choice
  1295. (const :tag "Reverse always" t)
  1296. (const :tag "Reverse never" nil)
  1297. (repeat :tag "By file name regexp"
  1298. (cons regexp boolean))))
  1299. (defcustom org-refile-targets nil
  1300. "Targets for refiling entries with \\[org-refile].
  1301. This is list of cons cells. Each cell contains:
  1302. - a specification of the files to be considered, either a list of files,
  1303. or a symbol whose function or variable value will be used to retrieve
  1304. a file name or a list of file names. If you use `org-agenda-files' for
  1305. that, all agenda files will be scanned for targets. Nil means, consider
  1306. headings in the current buffer.
  1307. - A specification of how to find candidate refile targets. This may be
  1308. any of:
  1309. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1310. This tag has to be present in all target headlines, inheritance will
  1311. not be considered.
  1312. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1313. todo keyword.
  1314. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1315. headlines that are refiling targets.
  1316. - a cons cell (:level . N). Any headline of level N is considered a target.
  1317. Note that, when `org-odd-levels-only' is set, level corresponds to
  1318. order in hierarchy, not to the number of stars.
  1319. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1320. Note that, when `org-odd-levels-only' is set, level corresponds to
  1321. order in hierarchy, not to the number of stars.
  1322. When this variable is nil, all top-level headlines in the current buffer
  1323. are used, equivalent to the value `((nil . (:level . 1))'."
  1324. :group 'org-refile
  1325. :type '(repeat
  1326. (cons
  1327. (choice :value org-agenda-files
  1328. (const :tag "All agenda files" org-agenda-files)
  1329. (const :tag "Current buffer" nil)
  1330. (function) (variable) (file))
  1331. (choice :tag "Identify target headline by"
  1332. (cons :tag "Specific tag" (const :value :tag) (string))
  1333. (cons :tag "TODO keyword" (const :value :todo) (string))
  1334. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1335. (cons :tag "Level number" (const :value :level) (integer))
  1336. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1337. (defcustom org-refile-use-outline-path nil
  1338. "Non-nil means, provide refile targets as paths.
  1339. So a level 3 headline will be available as level1/level2/level3.
  1340. When the value is `file', also include the file name (without directory)
  1341. into the path. In this case, you can also stop the completion after
  1342. the file name, to get entries inserted as top level in the file.
  1343. When `full-file-path', include the full file path."
  1344. :group 'org-refile
  1345. :type '(choice
  1346. (const :tag "Not" nil)
  1347. (const :tag "Yes" t)
  1348. (const :tag "Start with file name" file)
  1349. (const :tag "Start with full file path" full-file-path)))
  1350. (defcustom org-outline-path-complete-in-steps t
  1351. "Non-nil means, complete the outline path in hierarchical steps.
  1352. When Org-mode uses the refile interface to select an outline path
  1353. \(see variable `org-refile-use-outline-path'), the completion of
  1354. the path can be done is a single go, or if can be done in steps down
  1355. the headline hierarchy. Going in steps is probably the best if you
  1356. do not use a special completion package like `ido' or `icicles'.
  1357. However, when using these packages, going in one step can be very
  1358. fast, while still showing the whole path to the entry."
  1359. :group 'org-refile
  1360. :type 'boolean)
  1361. (defgroup org-todo nil
  1362. "Options concerning TODO items in Org-mode."
  1363. :tag "Org TODO"
  1364. :group 'org)
  1365. (defgroup org-progress nil
  1366. "Options concerning Progress logging in Org-mode."
  1367. :tag "Org Progress"
  1368. :group 'org-time)
  1369. (defvar org-todo-interpretation-widgets
  1370. '(
  1371. (:tag "Sequence (cycling hits every state)" sequence)
  1372. (:tag "Type (cycling directly to DONE)" type))
  1373. "The available interpretation symbols for customizing
  1374. `org-todo-keywords'.
  1375. Interested libraries should add to this list.")
  1376. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1377. "List of TODO entry keyword sequences and their interpretation.
  1378. \\<org-mode-map>This is a list of sequences.
  1379. Each sequence starts with a symbol, either `sequence' or `type',
  1380. indicating if the keywords should be interpreted as a sequence of
  1381. action steps, or as different types of TODO items. The first
  1382. keywords are states requiring action - these states will select a headline
  1383. for inclusion into the global TODO list Org-mode produces. If one of
  1384. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1385. signify that no further action is necessary. If \"|\" is not found,
  1386. the last keyword is treated as the only DONE state of the sequence.
  1387. The command \\[org-todo] cycles an entry through these states, and one
  1388. additional state where no keyword is present. For details about this
  1389. cycling, see the manual.
  1390. TODO keywords and interpretation can also be set on a per-file basis with
  1391. the special #+SEQ_TODO and #+TYP_TODO lines.
  1392. Each keyword can optionally specify a character for fast state selection
  1393. \(in combination with the variable `org-use-fast-todo-selection')
  1394. and specifiers for state change logging, using the same syntax
  1395. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1396. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1397. indicates to record a time stamp each time this state is selected.
  1398. Each keyword may also specify if a timestamp or a note should be
  1399. recorded when entering or leaving the state, by adding additional
  1400. characters in the parenthesis after the keyword. This looks like this:
  1401. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1402. record only the time of the state change. With X and Y being either
  1403. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1404. Y when leaving the state if and only if the *target* state does not
  1405. define X. You may omit any of the fast-selection key or X or /Y,
  1406. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1407. For backward compatibility, this variable may also be just a list
  1408. of keywords - in this case the interpretation (sequence or type) will be
  1409. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1410. :group 'org-todo
  1411. :group 'org-keywords
  1412. :type '(choice
  1413. (repeat :tag "Old syntax, just keywords"
  1414. (string :tag "Keyword"))
  1415. (repeat :tag "New syntax"
  1416. (cons
  1417. (choice
  1418. :tag "Interpretation"
  1419. ;;Quick and dirty way to see
  1420. ;;`org-todo-interpretations'. This takes the
  1421. ;;place of item arguments
  1422. :convert-widget
  1423. (lambda (widget)
  1424. (widget-put widget
  1425. :args (mapcar
  1426. #'(lambda (x)
  1427. (widget-convert
  1428. (cons 'const x)))
  1429. org-todo-interpretation-widgets))
  1430. widget))
  1431. (repeat
  1432. (string :tag "Keyword"))))))
  1433. (defvar org-todo-keywords-1 nil
  1434. "All TODO and DONE keywords active in a buffer.")
  1435. (make-variable-buffer-local 'org-todo-keywords-1)
  1436. (defvar org-todo-keywords-for-agenda nil)
  1437. (defvar org-done-keywords-for-agenda nil)
  1438. (defvar org-todo-keyword-alist-for-agenda nil)
  1439. (defvar org-tag-alist-for-agenda nil)
  1440. (defvar org-agenda-contributing-files nil)
  1441. (defvar org-not-done-keywords nil)
  1442. (make-variable-buffer-local 'org-not-done-keywords)
  1443. (defvar org-done-keywords nil)
  1444. (make-variable-buffer-local 'org-done-keywords)
  1445. (defvar org-todo-heads nil)
  1446. (make-variable-buffer-local 'org-todo-heads)
  1447. (defvar org-todo-sets nil)
  1448. (make-variable-buffer-local 'org-todo-sets)
  1449. (defvar org-todo-log-states nil)
  1450. (make-variable-buffer-local 'org-todo-log-states)
  1451. (defvar org-todo-kwd-alist nil)
  1452. (make-variable-buffer-local 'org-todo-kwd-alist)
  1453. (defvar org-todo-key-alist nil)
  1454. (make-variable-buffer-local 'org-todo-key-alist)
  1455. (defvar org-todo-key-trigger nil)
  1456. (make-variable-buffer-local 'org-todo-key-trigger)
  1457. (defcustom org-todo-interpretation 'sequence
  1458. "Controls how TODO keywords are interpreted.
  1459. This variable is in principle obsolete and is only used for
  1460. backward compatibility, if the interpretation of todo keywords is
  1461. not given already in `org-todo-keywords'. See that variable for
  1462. more information."
  1463. :group 'org-todo
  1464. :group 'org-keywords
  1465. :type '(choice (const sequence)
  1466. (const type)))
  1467. (defcustom org-use-fast-todo-selection t
  1468. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1469. This variable describes if and under what circumstances the cycling
  1470. mechanism for TODO keywords will be replaced by a single-key, direct
  1471. selection scheme.
  1472. When nil, fast selection is never used.
  1473. When the symbol `prefix', it will be used when `org-todo' is called with
  1474. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1475. in an agenda buffer.
  1476. When t, fast selection is used by default. In this case, the prefix
  1477. argument forces cycling instead.
  1478. In all cases, the special interface is only used if access keys have actually
  1479. been assigned by the user, i.e. if keywords in the configuration are followed
  1480. by a letter in parenthesis, like TODO(t)."
  1481. :group 'org-todo
  1482. :type '(choice
  1483. (const :tag "Never" nil)
  1484. (const :tag "By default" t)
  1485. (const :tag "Only with C-u C-c C-t" prefix)))
  1486. (defcustom org-provide-todo-statistics t
  1487. "Non-nil means, update todo statistics after insert and toggle.
  1488. When this is set, todo statistics is updated in the parent of the current
  1489. entry each time a todo state is changed."
  1490. :group 'org-todo
  1491. :type 'boolean)
  1492. (defcustom org-after-todo-state-change-hook nil
  1493. "Hook which is run after the state of a TODO item was changed.
  1494. The new state (a string with a TODO keyword, or nil) is available in the
  1495. Lisp variable `state'."
  1496. :group 'org-todo
  1497. :type 'hook)
  1498. (defvar org-blocker-hook nil
  1499. "Hook for functions that are allowed to block a state change.
  1500. Each function gets as its single argument a property list, see
  1501. `org-trigger-hook' for more information about this list.
  1502. If any of the functions in this hook returns nil, the state change
  1503. is blocked.")
  1504. (defvar org-trigger-hook nil
  1505. "Hook for functions that are triggered by a state change.
  1506. Each function gets as its single argument a property list with at least
  1507. the following elements:
  1508. (:type type-of-change :position pos-at-entry-start
  1509. :from old-state :to new-state)
  1510. Depending on the type, more properties may be present.
  1511. This mechanism is currently implemented for:
  1512. TODO state changes
  1513. ------------------
  1514. :type todo-state-change
  1515. :from previous state (keyword as a string), or nil, or a symbol
  1516. 'todo' or 'done', to indicate the general type of state.
  1517. :to new state, like in :from")
  1518. (defcustom org-enforce-todo-dependencies nil
  1519. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1520. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1521. be blocked if any prior sibling is not yet done.
  1522. Finally, if the parent is blocked because of ordered siblings of its own,
  1523. the child will also be blocked.
  1524. This variable needs to be set before org.el is loaded, and you need to
  1525. restart Emacs after a change to make the change effective. The only way
  1526. to change is while Emacs is running is through the customize interface."
  1527. :set (lambda (var val)
  1528. (set var val)
  1529. (if val
  1530. (add-hook 'org-blocker-hook
  1531. 'org-block-todo-from-children-or-siblings-or-parent)
  1532. (remove-hook 'org-blocker-hook
  1533. 'org-block-todo-from-children-or-siblings-or-parent)))
  1534. :group 'org-todo
  1535. :type 'boolean)
  1536. (defcustom org-enforce-todo-checkbox-dependencies nil
  1537. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1538. When this is nil, checkboxes have no influence on switching TODO states.
  1539. When non-nil, you first need to check off all check boxes before the TODO
  1540. entry can be switched to DONE.
  1541. This variable needs to be set before org.el is loaded, and you need to
  1542. restart Emacs after a change to make the change effective. The only way
  1543. to change is while Emacs is running is through the customize interface."
  1544. :set (lambda (var val)
  1545. (set var val)
  1546. (if val
  1547. (add-hook 'org-blocker-hook
  1548. 'org-block-todo-from-checkboxes)
  1549. (remove-hook 'org-blocker-hook
  1550. 'org-block-todo-from-checkboxes)))
  1551. :group 'org-todo
  1552. :type 'boolean)
  1553. (defcustom org-todo-state-tags-triggers nil
  1554. "Tag changes that should be triggered by TODO state changes.
  1555. This is a list. Each entry is
  1556. (state-change (tag . flag) .......)
  1557. State-change can be a string with a state, and empty string to indicate the
  1558. state that has no TODO keyword, or it can be one of the symbols `todo'
  1559. or `done', meaning any not-done or done state, respectively."
  1560. :group 'org-todo
  1561. :group 'org-tags
  1562. :type '(repeat
  1563. (cons (choice :tag "When changing to"
  1564. (const :tag "Not-done state" todo)
  1565. (const :tag "Done state" done)
  1566. (string :tag "State"))
  1567. (repeat
  1568. (cons :tag "Tag action"
  1569. (string :tag "Tag")
  1570. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1571. (defcustom org-log-done nil
  1572. "Information to record when a task moves to the DONE state.
  1573. Possible values are:
  1574. nil Don't add anything, just change the keyword
  1575. time Add a time stamp to the task
  1576. note Prompt a closing note and add it with template `org-log-note-headings'
  1577. This option can also be set with on a per-file-basis with
  1578. #+STARTUP: nologdone
  1579. #+STARTUP: logdone
  1580. #+STARTUP: lognotedone
  1581. You can have local logging settings for a subtree by setting the LOGGING
  1582. property to one or more of these keywords."
  1583. :group 'org-todo
  1584. :group 'org-progress
  1585. :type '(choice
  1586. (const :tag "No logging" nil)
  1587. (const :tag "Record CLOSED timestamp" time)
  1588. (const :tag "Record CLOSED timestamp with closing note." note)))
  1589. ;; Normalize old uses of org-log-done.
  1590. (cond
  1591. ((eq org-log-done t) (setq org-log-done 'time))
  1592. ((and (listp org-log-done) (memq 'done org-log-done))
  1593. (setq org-log-done 'note)))
  1594. (defcustom org-log-note-clock-out nil
  1595. "Non-nil means, record a note when clocking out of an item.
  1596. This can also be configured on a per-file basis by adding one of
  1597. the following lines anywhere in the buffer:
  1598. #+STARTUP: lognoteclock-out
  1599. #+STARTUP: nolognoteclock-out"
  1600. :group 'org-todo
  1601. :group 'org-progress
  1602. :type 'boolean)
  1603. (defcustom org-log-done-with-time t
  1604. "Non-nil means, the CLOSED time stamp will contain date and time.
  1605. When nil, only the date will be recorded."
  1606. :group 'org-progress
  1607. :type 'boolean)
  1608. (defcustom org-log-note-headings
  1609. '((done . "CLOSING NOTE %t")
  1610. (state . "State %-12s from %-12S %t")
  1611. (note . "Note taken on %t")
  1612. (clock-out . ""))
  1613. "Headings for notes added to entries.
  1614. The value is an alist, with the car being a symbol indicating the note
  1615. context, and the cdr is the heading to be used. The heading may also be the
  1616. empty string.
  1617. %t in the heading will be replaced by a time stamp.
  1618. %s will be replaced by the new TODO state, in double quotes.
  1619. %S will be replaced by the old TODO state, in double quotes.
  1620. %u will be replaced by the user name.
  1621. %U will be replaced by the full user name."
  1622. :group 'org-todo
  1623. :group 'org-progress
  1624. :type '(list :greedy t
  1625. (cons (const :tag "Heading when closing an item" done) string)
  1626. (cons (const :tag
  1627. "Heading when changing todo state (todo sequence only)"
  1628. state) string)
  1629. (cons (const :tag "Heading when just taking a note" note) string)
  1630. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1631. (unless (assq 'note org-log-note-headings)
  1632. (push '(note . "%t") org-log-note-headings))
  1633. (defcustom org-log-into-drawer nil
  1634. "Non-nil means, insert state change notes and time stamps into a drawer.
  1635. When nil, state changes notes will be inserted after the headline and
  1636. any scheduling and clock lines, but not inside a drawer.
  1637. The value of this variable should be the name of the drawer to use.
  1638. LOGBOOK is proposed at the default drawer for this purpose, you can
  1639. also set this to a string to define the drawer of your choice.
  1640. A value of t is also allowed, representing \"LOGBOOK\".
  1641. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1642. will be ignored."
  1643. :group 'org-todo
  1644. :group 'org-progress
  1645. :type '(choice
  1646. (const :tag "Not into a drawer" nil)
  1647. (const :tag "LOGBOOK" t)
  1648. (string :tag "Other")))
  1649. (if (fboundp 'defvaralias)
  1650. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1651. (defcustom org-log-state-notes-insert-after-drawers nil
  1652. "Non-nil means, insert state change notes after any drawers in entry.
  1653. Only the drawers that *immediately* follow the headline and the
  1654. deadline/scheduled line are skipped.
  1655. When nil, insert notes right after the heading and perhaps the line
  1656. with deadline/scheduling if present.
  1657. This variable will have no effect if `org-log-into-drawer' is
  1658. set."
  1659. :group 'org-todo
  1660. :group 'org-progress
  1661. :type 'boolean)
  1662. (defcustom org-log-states-order-reversed t
  1663. "Non-nil means, the latest state change note will be directly after heading.
  1664. When nil, the notes will be orderer according to time."
  1665. :group 'org-todo
  1666. :group 'org-progress
  1667. :type 'boolean)
  1668. (defcustom org-log-repeat 'time
  1669. "Non-nil means, record moving through the DONE state when triggering repeat.
  1670. An auto-repeating tasks is immediately switched back to TODO when marked
  1671. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1672. the TODO keyword definition, or recording a closing note by setting
  1673. `org-log-done', there will be no record of the task moving through DONE.
  1674. This variable forces taking a note anyway. Possible values are:
  1675. nil Don't force a record
  1676. time Record a time stamp
  1677. note Record a note
  1678. This option can also be set with on a per-file-basis with
  1679. #+STARTUP: logrepeat
  1680. #+STARTUP: lognoterepeat
  1681. #+STARTUP: nologrepeat
  1682. You can have local logging settings for a subtree by setting the LOGGING
  1683. property to one or more of these keywords."
  1684. :group 'org-todo
  1685. :group 'org-progress
  1686. :type '(choice
  1687. (const :tag "Don't force a record" nil)
  1688. (const :tag "Force recording the DONE state" time)
  1689. (const :tag "Force recording a note with the DONE state" note)))
  1690. (defgroup org-priorities nil
  1691. "Priorities in Org-mode."
  1692. :tag "Org Priorities"
  1693. :group 'org-todo)
  1694. (defcustom org-highest-priority ?A
  1695. "The highest priority of TODO items. A character like ?A, ?B etc.
  1696. Must have a smaller ASCII number than `org-lowest-priority'."
  1697. :group 'org-priorities
  1698. :type 'character)
  1699. (defcustom org-lowest-priority ?C
  1700. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1701. Must have a larger ASCII number than `org-highest-priority'."
  1702. :group 'org-priorities
  1703. :type 'character)
  1704. (defcustom org-default-priority ?B
  1705. "The default priority of TODO items.
  1706. This is the priority an item get if no explicit priority is given."
  1707. :group 'org-priorities
  1708. :type 'character)
  1709. (defcustom org-priority-start-cycle-with-default t
  1710. "Non-nil means, start with default priority when starting to cycle.
  1711. When this is nil, the first step in the cycle will be (depending on the
  1712. command used) one higher or lower that the default priority."
  1713. :group 'org-priorities
  1714. :type 'boolean)
  1715. (defgroup org-time nil
  1716. "Options concerning time stamps and deadlines in Org-mode."
  1717. :tag "Org Time"
  1718. :group 'org)
  1719. (defcustom org-insert-labeled-timestamps-at-point nil
  1720. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1721. When nil, these labeled time stamps are forces into the second line of an
  1722. entry, just after the headline. When scheduling from the global TODO list,
  1723. the time stamp will always be forced into the second line."
  1724. :group 'org-time
  1725. :type 'boolean)
  1726. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1727. "Formats for `format-time-string' which are used for time stamps.
  1728. It is not recommended to change this constant.")
  1729. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1730. "Number of minutes to round time stamps to.
  1731. These are two values, the first applies when first creating a time stamp.
  1732. The second applies when changing it with the commands `S-up' and `S-down'.
  1733. When changing the time stamp, this means that it will change in steps
  1734. of N minutes, as given by the second value.
  1735. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1736. numbers should be factors of 60, so for example 5, 10, 15.
  1737. When this is larger than 1, you can still force an exact time-stamp by using
  1738. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1739. and by using a prefix arg to `S-up/down' to specify the exact number
  1740. of minutes to shift."
  1741. :group 'org-time
  1742. :get '(lambda (var) ; Make sure all entries have 5 elements
  1743. (if (integerp (default-value var))
  1744. (list (default-value var) 5)
  1745. (default-value var)))
  1746. :type '(list
  1747. (integer :tag "when inserting times")
  1748. (integer :tag "when modifying times")))
  1749. ;; Normalize old customizations of this variable.
  1750. (when (integerp org-time-stamp-rounding-minutes)
  1751. (setq org-time-stamp-rounding-minutes
  1752. (list org-time-stamp-rounding-minutes
  1753. org-time-stamp-rounding-minutes)))
  1754. (defcustom org-display-custom-times nil
  1755. "Non-nil means, overlay custom formats over all time stamps.
  1756. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1757. To turn this on on a per-file basis, insert anywhere in the file:
  1758. #+STARTUP: customtime"
  1759. :group 'org-time
  1760. :set 'set-default
  1761. :type 'sexp)
  1762. (make-variable-buffer-local 'org-display-custom-times)
  1763. (defcustom org-time-stamp-custom-formats
  1764. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1765. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1766. These are overlayed over the default ISO format if the variable
  1767. `org-display-custom-times' is set. Time like %H:%M should be at the
  1768. end of the second format."
  1769. :group 'org-time
  1770. :type 'sexp)
  1771. (defun org-time-stamp-format (&optional long inactive)
  1772. "Get the right format for a time string."
  1773. (let ((f (if long (cdr org-time-stamp-formats)
  1774. (car org-time-stamp-formats))))
  1775. (if inactive
  1776. (concat "[" (substring f 1 -1) "]")
  1777. f)))
  1778. (defcustom org-time-clocksum-format "%d:%02d"
  1779. "The format string used when creating CLOCKSUM lines, or when
  1780. org-mode generates a time duration."
  1781. :group 'org-time
  1782. :type 'string)
  1783. (defcustom org-deadline-warning-days 14
  1784. "No. of days before expiration during which a deadline becomes active.
  1785. This variable governs the display in sparse trees and in the agenda.
  1786. When 0 or negative, it means use this number (the absolute value of it)
  1787. even if a deadline has a different individual lead time specified.
  1788. Custom commands can set this variable in the options section."
  1789. :group 'org-time
  1790. :group 'org-agenda-daily/weekly
  1791. :type 'integer)
  1792. (defcustom org-read-date-prefer-future t
  1793. "Non-nil means, assume future for incomplete date input from user.
  1794. This affects the following situations:
  1795. 1. The user gives a day, but no month.
  1796. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1797. read this as the third of *next* month. However, if you enter \"17\",
  1798. it will be considered as *this* month.
  1799. 2. The user gives a month but not a year.
  1800. For example, if it is april and you enter \"feb 2\", this will be read
  1801. as feb 2, *next* year. \"May 5\", however, will be this year.
  1802. Currently this does not work for ISO week specifications.
  1803. When this option is nil, the current month and year will always be used
  1804. as defaults."
  1805. :group 'org-time
  1806. :type 'boolean)
  1807. (defcustom org-read-date-display-live t
  1808. "Non-nil means, display current interpretation of date prompt live.
  1809. This display will be in an overlay, in the minibuffer."
  1810. :group 'org-time
  1811. :type 'boolean)
  1812. (defcustom org-read-date-popup-calendar t
  1813. "Non-nil means, pop up a calendar when prompting for a date.
  1814. In the calendar, the date can be selected with mouse-1. However, the
  1815. minibuffer will also be active, and you can simply enter the date as well.
  1816. When nil, only the minibuffer will be available."
  1817. :group 'org-time
  1818. :type 'boolean)
  1819. (if (fboundp 'defvaralias)
  1820. (defvaralias 'org-popup-calendar-for-date-prompt
  1821. 'org-read-date-popup-calendar))
  1822. (defcustom org-read-date-minibuffer-setup-hook nil
  1823. "Hook to be used to set up keys for the date/time interface.
  1824. Add key definitions to `minibuffer-local-map', which will be a temporary
  1825. copy."
  1826. :group 'org-time
  1827. :type 'hook)
  1828. (defcustom org-extend-today-until 0
  1829. "The hour when your day really ends. Must be an integer.
  1830. This has influence for the following applications:
  1831. - When switching the agenda to \"today\". It it is still earlier than
  1832. the time given here, the day recognized as TODAY is actually yesterday.
  1833. - When a date is read from the user and it is still before the time given
  1834. here, the current date and time will be assumed to be yesterday, 23:59.
  1835. Also, timestamps inserted in remember templates follow this rule.
  1836. IMPORTANT: This is a feature whose implementation is and likely will
  1837. remain incomplete. Really, it is only here because past midnight seems to
  1838. be the favorite working time of John Wiegley :-)"
  1839. :group 'org-time
  1840. :type 'integer)
  1841. (defcustom org-edit-timestamp-down-means-later nil
  1842. "Non-nil means, S-down will increase the time in a time stamp.
  1843. When nil, S-up will increase."
  1844. :group 'org-time
  1845. :type 'boolean)
  1846. (defcustom org-calendar-follow-timestamp-change t
  1847. "Non-nil means, make the calendar window follow timestamp changes.
  1848. When a timestamp is modified and the calendar window is visible, it will be
  1849. moved to the new date."
  1850. :group 'org-time
  1851. :type 'boolean)
  1852. (defgroup org-tags nil
  1853. "Options concerning tags in Org-mode."
  1854. :tag "Org Tags"
  1855. :group 'org)
  1856. (defcustom org-tag-alist nil
  1857. "List of tags allowed in Org-mode files.
  1858. When this list is nil, Org-mode will base TAG input on what is already in the
  1859. buffer.
  1860. The value of this variable is an alist, the car of each entry must be a
  1861. keyword as a string, the cdr may be a character that is used to select
  1862. that tag through the fast-tag-selection interface.
  1863. See the manual for details."
  1864. :group 'org-tags
  1865. :type '(repeat
  1866. (choice
  1867. (cons (string :tag "Tag name")
  1868. (character :tag "Access char"))
  1869. (const :tag "Start radio group" (:startgroup))
  1870. (const :tag "End radio group" (:endgroup))
  1871. (const :tag "New line" (:newline)))))
  1872. (defcustom org-tag-persistent-alist nil
  1873. "List of tags that will always appear in all Org-mode files.
  1874. This is in addition to any in buffer settings or customizations
  1875. of `org-tag-alist'.
  1876. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1877. The value of this variable is an alist, the car of each entry must be a
  1878. keyword as a string, the cdr may be a character that is used to select
  1879. that tag through the fast-tag-selection interface.
  1880. See the manual for details.
  1881. To disable these tags on a per-file basis, insert anywhere in the file:
  1882. #+STARTUP: noptag"
  1883. :group 'org-tags
  1884. :type '(repeat
  1885. (choice
  1886. (cons (string :tag "Tag name")
  1887. (character :tag "Access char"))
  1888. (const :tag "Start radio group" (:startgroup))
  1889. (const :tag "End radio group" (:endgroup))
  1890. (const :tag "New line" (:newline)))))
  1891. (defvar org-file-tags nil
  1892. "List of tags that can be inherited by all entries in the file.
  1893. The tags will be inherited if the variable `org-use-tag-inheritance'
  1894. says they should be.
  1895. This variable is populated from #+TAG lines.")
  1896. (defcustom org-use-fast-tag-selection 'auto
  1897. "Non-nil means, use fast tag selection scheme.
  1898. This is a special interface to select and deselect tags with single keys.
  1899. When nil, fast selection is never used.
  1900. When the symbol `auto', fast selection is used if and only if selection
  1901. characters for tags have been configured, either through the variable
  1902. `org-tag-alist' or through a #+TAGS line in the buffer.
  1903. When t, fast selection is always used and selection keys are assigned
  1904. automatically if necessary."
  1905. :group 'org-tags
  1906. :type '(choice
  1907. (const :tag "Always" t)
  1908. (const :tag "Never" nil)
  1909. (const :tag "When selection characters are configured" 'auto)))
  1910. (defcustom org-fast-tag-selection-single-key nil
  1911. "Non-nil means, fast tag selection exits after first change.
  1912. When nil, you have to press RET to exit it.
  1913. During fast tag selection, you can toggle this flag with `C-c'.
  1914. This variable can also have the value `expert'. In this case, the window
  1915. displaying the tags menu is not even shown, until you press C-c again."
  1916. :group 'org-tags
  1917. :type '(choice
  1918. (const :tag "No" nil)
  1919. (const :tag "Yes" t)
  1920. (const :tag "Expert" expert)))
  1921. (defvar org-fast-tag-selection-include-todo nil
  1922. "Non-nil means, fast tags selection interface will also offer TODO states.
  1923. This is an undocumented feature, you should not rely on it.")
  1924. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1925. "The column to which tags should be indented in a headline.
  1926. If this number is positive, it specifies the column. If it is negative,
  1927. it means that the tags should be flushright to that column. For example,
  1928. -80 works well for a normal 80 character screen."
  1929. :group 'org-tags
  1930. :type 'integer)
  1931. (defcustom org-auto-align-tags t
  1932. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1933. These operations change the length of a headline and therefore shift
  1934. the tags around. With this options turned on, after each such operation
  1935. the tags are again aligned to `org-tags-column'."
  1936. :group 'org-tags
  1937. :type 'boolean)
  1938. (defcustom org-use-tag-inheritance t
  1939. "Non-nil means, tags in levels apply also for sublevels.
  1940. When nil, only the tags directly given in a specific line apply there.
  1941. This may also be a list of tags that should be inherited, or a regexp that
  1942. matches tags that should be inherited. Additional control is possible
  1943. with the variable `org-tags-exclude-from-inheritance' which gives an
  1944. explicit list of tags to be excluded from inheritance., even if the value of
  1945. `org-use-tag-inheritance' would select it for inheritance.
  1946. If this option is t, a match early-on in a tree can lead to a large
  1947. number of matches in the subtree when constructing the agenda or creating
  1948. a sparse tree. If you only want to see the first match in a tree during
  1949. a search, check out the variable `org-tags-match-list-sublevels'."
  1950. :group 'org-tags
  1951. :type '(choice
  1952. (const :tag "Not" nil)
  1953. (const :tag "Always" t)
  1954. (repeat :tag "Specific tags" (string :tag "Tag"))
  1955. (regexp :tag "Tags matched by regexp")))
  1956. (defcustom org-tags-exclude-from-inheritance nil
  1957. "List of tags that should never be inherited.
  1958. This is a way to exclude a few tags from inheritance. For way to do
  1959. the opposite, to actively allow inheritance for selected tags,
  1960. see the variable `org-use-tag-inheritance'."
  1961. :group 'org-tags
  1962. :type '(repeat (string :tag "Tag")))
  1963. (defun org-tag-inherit-p (tag)
  1964. "Check if TAG is one that should be inherited."
  1965. (cond
  1966. ((member tag org-tags-exclude-from-inheritance) nil)
  1967. ((eq org-use-tag-inheritance t) t)
  1968. ((not org-use-tag-inheritance) nil)
  1969. ((stringp org-use-tag-inheritance)
  1970. (string-match org-use-tag-inheritance tag))
  1971. ((listp org-use-tag-inheritance)
  1972. (member tag org-use-tag-inheritance))
  1973. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1974. (defcustom org-tags-match-list-sublevels t
  1975. "Non-nil means list also sublevels of headlines matching a search.
  1976. This variable applies to tags/property searches, and also to stuck
  1977. projects because this search is based on a tags match as well.
  1978. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1979. the sublevels of a headline matching a tag search often also match
  1980. the same search. Listing all of them can create very long lists.
  1981. Setting this variable to nil causes subtrees of a match to be skipped.
  1982. As a special case, if the tag search is restricted to TODO items, the
  1983. value of this variable is ignored and sublevels are always checked, to
  1984. make sure all corresponding TODO items find their way into the list.
  1985. This variable is semi-obsolete and probably should always be true. It
  1986. is better to limit inheritance to certain tags using the variables
  1987. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1988. :group 'org-tags
  1989. :type '(choice
  1990. (const :tag "No, don't list them" nil)
  1991. (const :tag "Yes, do list them" t)
  1992. (const :tag "List them, indented with leading dots" indented)))
  1993. (defvar org-tags-history nil
  1994. "History of minibuffer reads for tags.")
  1995. (defvar org-last-tags-completion-table nil
  1996. "The last used completion table for tags.")
  1997. (defvar org-after-tags-change-hook nil
  1998. "Hook that is run after the tags in a line have changed.")
  1999. (defgroup org-properties nil
  2000. "Options concerning properties in Org-mode."
  2001. :tag "Org Properties"
  2002. :group 'org)
  2003. (defcustom org-property-format "%-10s %s"
  2004. "How property key/value pairs should be formatted by `indent-line'.
  2005. When `indent-line' hits a property definition, it will format the line
  2006. according to this format, mainly to make sure that the values are
  2007. lined-up with respect to each other."
  2008. :group 'org-properties
  2009. :type 'string)
  2010. (defcustom org-use-property-inheritance nil
  2011. "Non-nil means, properties apply also for sublevels.
  2012. This setting is chiefly used during property searches. Turning it on can
  2013. cause significant overhead when doing a search, which is why it is not
  2014. on by default.
  2015. When nil, only the properties directly given in the current entry count.
  2016. When t, every property is inherited. The value may also be a list of
  2017. properties that should have inheritance, or a regular expression matching
  2018. properties that should be inherited.
  2019. However, note that some special properties use inheritance under special
  2020. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2021. and the properties ending in \"_ALL\" when they are used as descriptor
  2022. for valid values of a property.
  2023. Note for programmers:
  2024. When querying an entry with `org-entry-get', you can control if inheritance
  2025. should be used. By default, `org-entry-get' looks only at the local
  2026. properties. You can request inheritance by setting the inherit argument
  2027. to t (to force inheritance) or to `selective' (to respect the setting
  2028. in this variable)."
  2029. :group 'org-properties
  2030. :type '(choice
  2031. (const :tag "Not" nil)
  2032. (const :tag "Always" t)
  2033. (repeat :tag "Specific properties" (string :tag "Property"))
  2034. (regexp :tag "Properties matched by regexp")))
  2035. (defun org-property-inherit-p (property)
  2036. "Check if PROPERTY is one that should be inherited."
  2037. (cond
  2038. ((eq org-use-property-inheritance t) t)
  2039. ((not org-use-property-inheritance) nil)
  2040. ((stringp org-use-property-inheritance)
  2041. (string-match org-use-property-inheritance property))
  2042. ((listp org-use-property-inheritance)
  2043. (member property org-use-property-inheritance))
  2044. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2045. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2046. "The default column format, if no other format has been defined.
  2047. This variable can be set on the per-file basis by inserting a line
  2048. #+COLUMNS: %25ITEM ....."
  2049. :group 'org-properties
  2050. :type 'string)
  2051. (defcustom org-columns-ellipses ".."
  2052. "The ellipses to be used when a field in column view is truncated.
  2053. When this is the empty string, as many characters as possible are shown,
  2054. but then there will be no visual indication that the field has been truncated.
  2055. When this is a string of length N, the last N characters of a truncated
  2056. field are replaced by this string. If the column is narrower than the
  2057. ellipses string, only part of the ellipses string will be shown."
  2058. :group 'org-properties
  2059. :type 'string)
  2060. (defcustom org-columns-modify-value-for-display-function nil
  2061. "Function that modifies values for display in column view.
  2062. For example, it can be used to cut out a certain part from a time stamp.
  2063. The function must take 2 arguments:
  2064. column-title The title of the column (*not* the property name)
  2065. value The value that should be modified.
  2066. The function should return the value that should be displayed,
  2067. or nil if the normal value should be used."
  2068. :group 'org-properties
  2069. :type 'function)
  2070. (defcustom org-effort-property "Effort"
  2071. "The property that is being used to keep track of effort estimates.
  2072. Effort estimates given in this property need to have the format H:MM."
  2073. :group 'org-properties
  2074. :group 'org-progress
  2075. :type '(string :tag "Property"))
  2076. (defconst org-global-properties-fixed
  2077. '(("VISIBILITY_ALL" . "folded children content all"))
  2078. "List of property/value pairs that can be inherited by any entry.
  2079. These are fixed values, for the preset properties. The user variable
  2080. that can be used to add to this list is `org-global-properties'.
  2081. The entries in this list are cons cells where the car is a property
  2082. name and cdr is a string with the value. If the value represents
  2083. multiple items like an \"_ALL\" property, separate the items by
  2084. spaces.")
  2085. (defcustom org-global-properties nil
  2086. "List of property/value pairs that can be inherited by any entry.
  2087. This list will be combined with the constant `org-global-properties-fixed'.
  2088. The entries in this list are cons cells where the car is a property
  2089. name and cdr is a string with the value.
  2090. You can set buffer-local values for the same purpose in the variable
  2091. `org-file-properties' this by adding lines like
  2092. #+PROPERTY: NAME VALUE"
  2093. :group 'org-properties
  2094. :type '(repeat
  2095. (cons (string :tag "Property")
  2096. (string :tag "Value"))))
  2097. (defvar org-file-properties nil
  2098. "List of property/value pairs that can be inherited by any entry.
  2099. Valid for the current buffer.
  2100. This variable is populated from #+PROPERTY lines.")
  2101. (make-variable-buffer-local 'org-file-properties)
  2102. (defgroup org-agenda nil
  2103. "Options concerning agenda views in Org-mode."
  2104. :tag "Org Agenda"
  2105. :group 'org)
  2106. (defvar org-category nil
  2107. "Variable used by org files to set a category for agenda display.
  2108. Such files should use a file variable to set it, for example
  2109. # -*- mode: org; org-category: \"ELisp\"
  2110. or contain a special line
  2111. #+CATEGORY: ELisp
  2112. If the file does not specify a category, then file's base name
  2113. is used instead.")
  2114. (make-variable-buffer-local 'org-category)
  2115. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2116. (defcustom org-agenda-files nil
  2117. "The files to be used for agenda display.
  2118. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2119. \\[org-remove-file]. You can also use customize to edit the list.
  2120. If an entry is a directory, all files in that directory that are matched by
  2121. `org-agenda-file-regexp' will be part of the file list.
  2122. If the value of the variable is not a list but a single file name, then
  2123. the list of agenda files is actually stored and maintained in that file, one
  2124. agenda file per line."
  2125. :group 'org-agenda
  2126. :type '(choice
  2127. (repeat :tag "List of files and directories" file)
  2128. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2129. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2130. "Regular expression to match files for `org-agenda-files'.
  2131. If any element in the list in that variable contains a directory instead
  2132. of a normal file, all files in that directory that are matched by this
  2133. regular expression will be included."
  2134. :group 'org-agenda
  2135. :type 'regexp)
  2136. (defcustom org-agenda-text-search-extra-files nil
  2137. "List of extra files to be searched by text search commands.
  2138. These files will be search in addition to the agenda files by the
  2139. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2140. Note that these files will only be searched for text search commands,
  2141. not for the other agenda views like todo lists, tag searches or the weekly
  2142. agenda. This variable is intended to list notes and possibly archive files
  2143. that should also be searched by these two commands.
  2144. In fact, if the first element in the list is the symbol `agenda-archives',
  2145. than all archive files of all agenda files will be added to the search
  2146. scope."
  2147. :group 'org-agenda
  2148. :type '(set :greedy t
  2149. (const :tag "Agenda Archives" agenda-archives)
  2150. (repeat :inline t (file))))
  2151. (if (fboundp 'defvaralias)
  2152. (defvaralias 'org-agenda-multi-occur-extra-files
  2153. 'org-agenda-text-search-extra-files))
  2154. (defcustom org-agenda-skip-unavailable-files nil
  2155. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2156. A nil value means to remove them, after a query, from the list."
  2157. :group 'org-agenda
  2158. :type 'boolean)
  2159. (defcustom org-calendar-to-agenda-key [?c]
  2160. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2161. The command `org-calendar-goto-agenda' will be bound to this key. The
  2162. default is the character `c' because then `c' can be used to switch back and
  2163. forth between agenda and calendar."
  2164. :group 'org-agenda
  2165. :type 'sexp)
  2166. (defcustom org-calendar-agenda-action-key [?k]
  2167. "The key to be installed in `calendar-mode-map' for agenda-action.
  2168. The command `org-agenda-action' will be bound to this key. The
  2169. default is the character `k' because we use the same key in the agenda."
  2170. :group 'org-agenda
  2171. :type 'sexp)
  2172. (eval-after-load "calendar"
  2173. '(progn
  2174. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2175. 'org-calendar-goto-agenda)
  2176. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2177. 'org-agenda-action)))
  2178. (defgroup org-latex nil
  2179. "Options for embedding LaTeX code into Org-mode."
  2180. :tag "Org LaTeX"
  2181. :group 'org)
  2182. (defcustom org-format-latex-options
  2183. '(:foreground default :background default :scale 1.0
  2184. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2185. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2186. "Options for creating images from LaTeX fragments.
  2187. This is a property list with the following properties:
  2188. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2189. `default' means use the foreground of the default face.
  2190. :background the background color, or \"Transparent\".
  2191. `default' means use the background of the default face.
  2192. :scale a scaling factor for the size of the images.
  2193. :html-foreground, :html-background, :html-scale
  2194. the same numbers for HTML export.
  2195. :matchers a list indicating which matchers should be used to
  2196. find LaTeX fragments. Valid members of this list are:
  2197. \"begin\" find environments
  2198. \"$1\" find single characters surrounded by $.$
  2199. \"$\" find math expressions surrounded by $...$
  2200. \"$$\" find math expressions surrounded by $$....$$
  2201. \"\\(\" find math expressions surrounded by \\(...\\)
  2202. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2203. :group 'org-latex
  2204. :type 'plist)
  2205. (defcustom org-format-latex-header "\\documentclass{article}
  2206. \\usepackage{fullpage} % do not remove
  2207. \\usepackage{amssymb}
  2208. \\usepackage[usenames]{color}
  2209. \\usepackage{amsmath}
  2210. \\usepackage{latexsym}
  2211. \\usepackage[mathscr]{eucal}
  2212. \\pagestyle{empty} % do not remove"
  2213. "The document header used for processing LaTeX fragments."
  2214. :group 'org-latex
  2215. :type 'string)
  2216. (defgroup org-font-lock nil
  2217. "Font-lock settings for highlighting in Org-mode."
  2218. :tag "Org Font Lock"
  2219. :group 'org)
  2220. (defcustom org-level-color-stars-only nil
  2221. "Non-nil means fontify only the stars in each headline.
  2222. When nil, the entire headline is fontified.
  2223. Changing it requires restart of `font-lock-mode' to become effective
  2224. also in regions already fontified."
  2225. :group 'org-font-lock
  2226. :type 'boolean)
  2227. (defcustom org-hide-leading-stars nil
  2228. "Non-nil means, hide the first N-1 stars in a headline.
  2229. This works by using the face `org-hide' for these stars. This
  2230. face is white for a light background, and black for a dark
  2231. background. You may have to customize the face `org-hide' to
  2232. make this work.
  2233. Changing it requires restart of `font-lock-mode' to become effective
  2234. also in regions already fontified.
  2235. You may also set this on a per-file basis by adding one of the following
  2236. lines to the buffer:
  2237. #+STARTUP: hidestars
  2238. #+STARTUP: showstars"
  2239. :group 'org-font-lock
  2240. :type 'boolean)
  2241. (defcustom org-fontify-done-headline nil
  2242. "Non-nil means, change the face of a headline if it is marked DONE.
  2243. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2244. When this is non-nil, the headline after the keyword is set to the
  2245. `org-headline-done' as an additional indication."
  2246. :group 'org-font-lock
  2247. :type 'boolean)
  2248. (defcustom org-fontify-emphasized-text t
  2249. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2250. Changing this variable requires a restart of Emacs to take effect."
  2251. :group 'org-font-lock
  2252. :type 'boolean)
  2253. (defcustom org-highlight-latex-fragments-and-specials nil
  2254. "Non-nil means, fontify what is treated specially by the exporters."
  2255. :group 'org-font-lock
  2256. :type 'boolean)
  2257. (defcustom org-hide-emphasis-markers nil
  2258. "Non-nil mean font-lock should hide the emphasis marker characters."
  2259. :group 'org-font-lock
  2260. :type 'boolean)
  2261. (defvar org-emph-re nil
  2262. "Regular expression for matching emphasis.")
  2263. (defvar org-verbatim-re nil
  2264. "Regular expression for matching verbatim text.")
  2265. (defvar org-emphasis-regexp-components) ; defined just below
  2266. (defvar org-emphasis-alist) ; defined just below
  2267. (defun org-set-emph-re (var val)
  2268. "Set variable and compute the emphasis regular expression."
  2269. (set var val)
  2270. (when (and (boundp 'org-emphasis-alist)
  2271. (boundp 'org-emphasis-regexp-components)
  2272. org-emphasis-alist org-emphasis-regexp-components)
  2273. (let* ((e org-emphasis-regexp-components)
  2274. (pre (car e))
  2275. (post (nth 1 e))
  2276. (border (nth 2 e))
  2277. (body (nth 3 e))
  2278. (nl (nth 4 e))
  2279. (body1 (concat body "*?"))
  2280. (markers (mapconcat 'car org-emphasis-alist ""))
  2281. (vmarkers (mapconcat
  2282. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2283. org-emphasis-alist "")))
  2284. ;; make sure special characters appear at the right position in the class
  2285. (if (string-match "\\^" markers)
  2286. (setq markers (concat (replace-match "" t t markers) "^")))
  2287. (if (string-match "-" markers)
  2288. (setq markers (concat (replace-match "" t t markers) "-")))
  2289. (if (string-match "\\^" vmarkers)
  2290. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2291. (if (string-match "-" vmarkers)
  2292. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2293. (if (> nl 0)
  2294. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2295. (int-to-string nl) "\\}")))
  2296. ;; Make the regexp
  2297. (setq org-emph-re
  2298. (concat "\\([" pre "]\\|^\\)"
  2299. "\\("
  2300. "\\([" markers "]\\)"
  2301. "\\("
  2302. "[^" border "]\\|"
  2303. "[^" border "]"
  2304. body1
  2305. "[^" border "]"
  2306. "\\)"
  2307. "\\3\\)"
  2308. "\\([" post "]\\|$\\)"))
  2309. (setq org-verbatim-re
  2310. (concat "\\([" pre "]\\|^\\)"
  2311. "\\("
  2312. "\\([" vmarkers "]\\)"
  2313. "\\("
  2314. "[^" border "]\\|"
  2315. "[^" border "]"
  2316. body1
  2317. "[^" border "]"
  2318. "\\)"
  2319. "\\3\\)"
  2320. "\\([" post "]\\|$\\)")))))
  2321. (defcustom org-emphasis-regexp-components
  2322. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2323. "Components used to build the regular expression for emphasis.
  2324. This is a list with 6 entries. Terminology: In an emphasis string
  2325. like \" *strong word* \", we call the initial space PREMATCH, the final
  2326. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2327. and \"trong wor\" is the body. The different components in this variable
  2328. specify what is allowed/forbidden in each part:
  2329. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2330. post Chars allowed as postmatch. End of line will be allowed too.
  2331. border The chars *forbidden* as border characters.
  2332. body-regexp A regexp like \".\" to match a body character. Don't use
  2333. non-shy groups here, and don't allow newline here.
  2334. newline The maximum number of newlines allowed in an emphasis exp.
  2335. Use customize to modify this, or restart Emacs after changing it."
  2336. :group 'org-font-lock
  2337. :set 'org-set-emph-re
  2338. :type '(list
  2339. (sexp :tag "Allowed chars in pre ")
  2340. (sexp :tag "Allowed chars in post ")
  2341. (sexp :tag "Forbidden chars in border ")
  2342. (sexp :tag "Regexp for body ")
  2343. (integer :tag "number of newlines allowed")
  2344. (option (boolean :tag "Please ignore this button"))))
  2345. (defcustom org-emphasis-alist
  2346. `(("*" bold "<b>" "</b>")
  2347. ("/" italic "<i>" "</i>")
  2348. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2349. ("=" org-code "<code>" "</code>" verbatim)
  2350. ("~" org-verbatim "<code>" "</code>" verbatim)
  2351. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2352. "<del>" "</del>")
  2353. )
  2354. "Special syntax for emphasized text.
  2355. Text starting and ending with a special character will be emphasized, for
  2356. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2357. characters, the face to be used by font-lock for highlighting in Org-mode
  2358. Emacs buffers, and the HTML tags to be used for this.
  2359. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2360. Use customize to modify this, or restart Emacs after changing it."
  2361. :group 'org-font-lock
  2362. :set 'org-set-emph-re
  2363. :type '(repeat
  2364. (list
  2365. (string :tag "Marker character")
  2366. (choice
  2367. (face :tag "Font-lock-face")
  2368. (plist :tag "Face property list"))
  2369. (string :tag "HTML start tag")
  2370. (string :tag "HTML end tag")
  2371. (option (const verbatim)))))
  2372. ;;; Miscellaneous options
  2373. (defgroup org-completion nil
  2374. "Completion in Org-mode."
  2375. :tag "Org Completion"
  2376. :group 'org)
  2377. (defcustom org-completion-use-ido nil
  2378. "Non-nil means, use ido completion wherever possible.
  2379. Note that `ido-mode' must be active for this variable to be relevant.
  2380. If you decide to turn this variable on, you might well want to turn off
  2381. `org-outline-path-complete-in-steps'."
  2382. :group 'org-completion
  2383. :type 'boolean)
  2384. (defcustom org-completion-fallback-command 'hippie-expand
  2385. "The expansion command called by \\[org-complete] in normal context.
  2386. Normal means, no org-mode-specific context."
  2387. :group 'org-completion
  2388. :type 'function)
  2389. ;;; Functions and variables from ther packages
  2390. ;; Declared here to avoid compiler warnings
  2391. ;; XEmacs only
  2392. (defvar outline-mode-menu-heading)
  2393. (defvar outline-mode-menu-show)
  2394. (defvar outline-mode-menu-hide)
  2395. (defvar zmacs-regions) ; XEmacs regions
  2396. ;; Emacs only
  2397. (defvar mark-active)
  2398. ;; Various packages
  2399. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2400. (declare-function calendar-forward-day "cal-move" (arg))
  2401. (declare-function calendar-goto-date "cal-move" (date))
  2402. (declare-function calendar-goto-today "cal-move" ())
  2403. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2404. (defvar calc-embedded-close-formula)
  2405. (defvar calc-embedded-open-formula)
  2406. (declare-function cdlatex-tab "ext:cdlatex" ())
  2407. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2408. (defvar font-lock-unfontify-region-function)
  2409. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2410. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2411. (defvar iswitchb-temp-buflist)
  2412. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2413. (defvar org-agenda-tags-todo-honor-ignore-options)
  2414. (declare-function org-agenda-skip "org-agenda" ())
  2415. (declare-function org-format-agenda-item "org-agenda"
  2416. (extra txt &optional category tags dotime noprefix remove-re))
  2417. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2418. (declare-function org-agenda-change-all-lines "org-agenda"
  2419. (newhead hdmarker &optional fixface just-this))
  2420. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2421. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2422. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2423. (beg end))
  2424. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2425. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2426. "org-agenda" (&optional end))
  2427. (declare-function parse-time-string "parse-time" (string))
  2428. (declare-function remember "remember" (&optional initial))
  2429. (declare-function remember-buffer-desc "remember" ())
  2430. (declare-function remember-finalize "remember" ())
  2431. (defvar remember-save-after-remembering)
  2432. (defvar remember-data-file)
  2433. (defvar remember-register)
  2434. (defvar remember-buffer)
  2435. (defvar remember-handler-functions)
  2436. (defvar remember-annotation-functions)
  2437. (defvar texmathp-why)
  2438. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2439. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2440. (defvar w3m-current-url)
  2441. (defvar w3m-current-title)
  2442. (defvar org-latex-regexps)
  2443. ;;; Autoload and prepare some org modules
  2444. ;; Some table stuff that needs to be defined here, because it is used
  2445. ;; by the functions setting up org-mode or checking for table context.
  2446. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2447. "Detects an org-type or table-type table.")
  2448. (defconst org-table-line-regexp "^[ \t]*|"
  2449. "Detects an org-type table line.")
  2450. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2451. "Detects an org-type table line.")
  2452. (defconst org-table-hline-regexp "^[ \t]*|-"
  2453. "Detects an org-type table hline.")
  2454. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2455. "Detects a table-type table hline.")
  2456. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2457. "Searching from within a table (any type) this finds the first line
  2458. outside the table.")
  2459. ;; Autoload the functions in org-table.el that are needed by functions here.
  2460. (eval-and-compile
  2461. (org-autoload "org-table"
  2462. '(org-table-align org-table-begin org-table-blank-field
  2463. org-table-convert org-table-convert-region org-table-copy-down
  2464. org-table-copy-region org-table-create
  2465. org-table-create-or-convert-from-region
  2466. org-table-create-with-table.el org-table-current-dline
  2467. org-table-cut-region org-table-delete-column org-table-edit-field
  2468. org-table-edit-formulas org-table-end org-table-eval-formula
  2469. org-table-export org-table-field-info
  2470. org-table-get-stored-formulas org-table-goto-column
  2471. org-table-hline-and-move org-table-import org-table-insert-column
  2472. org-table-insert-hline org-table-insert-row org-table-iterate
  2473. org-table-justify-field-maybe org-table-kill-row
  2474. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2475. org-table-move-column org-table-move-column-left
  2476. org-table-move-column-right org-table-move-row
  2477. org-table-move-row-down org-table-move-row-up
  2478. org-table-next-field org-table-next-row org-table-paste-rectangle
  2479. org-table-previous-field org-table-recalculate
  2480. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2481. org-table-toggle-coordinate-overlays
  2482. org-table-toggle-formula-debugger org-table-wrap-region
  2483. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2484. (defun org-at-table-p (&optional table-type)
  2485. "Return t if the cursor is inside an org-type table.
  2486. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2487. (if org-enable-table-editor
  2488. (save-excursion
  2489. (beginning-of-line 1)
  2490. (looking-at (if table-type org-table-any-line-regexp
  2491. org-table-line-regexp)))
  2492. nil))
  2493. (defsubst org-table-p () (org-at-table-p))
  2494. (defun org-at-table.el-p ()
  2495. "Return t if and only if we are at a table.el table."
  2496. (and (org-at-table-p 'any)
  2497. (save-excursion
  2498. (goto-char (org-table-begin 'any))
  2499. (looking-at org-table1-hline-regexp))))
  2500. (defun org-table-recognize-table.el ()
  2501. "If there is a table.el table nearby, recognize it and move into it."
  2502. (if org-table-tab-recognizes-table.el
  2503. (if (org-at-table.el-p)
  2504. (progn
  2505. (beginning-of-line 1)
  2506. (if (looking-at org-table-dataline-regexp)
  2507. nil
  2508. (if (looking-at org-table1-hline-regexp)
  2509. (progn
  2510. (beginning-of-line 2)
  2511. (if (looking-at org-table-any-border-regexp)
  2512. (beginning-of-line -1)))))
  2513. (if (re-search-forward "|" (org-table-end t) t)
  2514. (progn
  2515. (require 'table)
  2516. (if (table--at-cell-p (point))
  2517. t
  2518. (message "recognizing table.el table...")
  2519. (table-recognize-table)
  2520. (message "recognizing table.el table...done")))
  2521. (error "This should not happen..."))
  2522. t)
  2523. nil)
  2524. nil))
  2525. (defun org-at-table-hline-p ()
  2526. "Return t if the cursor is inside a hline in a table."
  2527. (if org-enable-table-editor
  2528. (save-excursion
  2529. (beginning-of-line 1)
  2530. (looking-at org-table-hline-regexp))
  2531. nil))
  2532. (defvar org-table-clean-did-remove-column nil)
  2533. (defun org-table-map-tables (function)
  2534. "Apply FUNCTION to the start of all tables in the buffer."
  2535. (save-excursion
  2536. (save-restriction
  2537. (widen)
  2538. (goto-char (point-min))
  2539. (while (re-search-forward org-table-any-line-regexp nil t)
  2540. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2541. (beginning-of-line 1)
  2542. (if (looking-at org-table-line-regexp)
  2543. (save-excursion (funcall function)))
  2544. (re-search-forward org-table-any-border-regexp nil 1))))
  2545. (message "Mapping tables: done"))
  2546. ;; Declare and autoload functions from org-exp.el & Co
  2547. (declare-function org-default-export-plist "org-exp")
  2548. (declare-function org-infile-export-plist "org-exp")
  2549. (declare-function org-get-current-options "org-exp")
  2550. (eval-and-compile
  2551. (org-autoload "org-exp"
  2552. '(org-export org-export-visible
  2553. org-insert-export-options-template
  2554. org-table-clean-before-export))
  2555. (org-autoload "org-ascii"
  2556. '(org-export-as-ascii org-export-ascii-preprocess))
  2557. (org-autoload "org-html"
  2558. '(org-export-as-html-and-open
  2559. org-export-as-html-batch org-export-as-html-to-buffer
  2560. org-replace-region-by-html org-export-region-as-html
  2561. org-export-as-html))
  2562. (org-autoload "org-icalendar"
  2563. '(org-export-icalendar-this-file
  2564. org-export-icalendar-all-agenda-files
  2565. org-export-icalendar-combine-agenda-files))
  2566. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2567. ;; Declare and autoload functions from org-agenda.el
  2568. (eval-and-compile
  2569. (org-autoload "org-agenda"
  2570. '(org-agenda org-agenda-list org-search-view
  2571. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2572. org-diary org-agenda-to-appt
  2573. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2574. ;; Autoload org-remember
  2575. (eval-and-compile
  2576. (org-autoload "org-remember"
  2577. '(org-remember-insinuate org-remember-annotation
  2578. org-remember-apply-template org-remember org-remember-handler)))
  2579. ;; Autoload org-clock.el
  2580. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2581. (beg end))
  2582. (declare-function org-clock-update-mode-line "org-clock" ())
  2583. (defvar org-clock-start-time)
  2584. (defvar org-clock-marker (make-marker)
  2585. "Marker recording the last clock-in.")
  2586. (eval-and-compile
  2587. (org-autoload
  2588. "org-clock"
  2589. '(org-clock-in org-clock-out org-clock-cancel
  2590. org-clock-goto org-clock-sum org-clock-display
  2591. org-clock-remove-overlays org-clock-report
  2592. org-clocktable-shift org-dblock-write:clocktable
  2593. org-get-clocktable)))
  2594. (defun org-clock-update-time-maybe ()
  2595. "If this is a CLOCK line, update it and return t.
  2596. Otherwise, return nil."
  2597. (interactive)
  2598. (save-excursion
  2599. (beginning-of-line 1)
  2600. (skip-chars-forward " \t")
  2601. (when (looking-at org-clock-string)
  2602. (let ((re (concat "[ \t]*" org-clock-string
  2603. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2604. "\\([ \t]*=>.*\\)?\\)?"))
  2605. ts te h m s neg)
  2606. (cond
  2607. ((not (looking-at re))
  2608. nil)
  2609. ((not (match-end 2))
  2610. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2611. (> org-clock-marker (point))
  2612. (<= org-clock-marker (point-at-eol)))
  2613. ;; The clock is running here
  2614. (setq org-clock-start-time
  2615. (apply 'encode-time
  2616. (org-parse-time-string (match-string 1))))
  2617. (org-clock-update-mode-line)))
  2618. (t
  2619. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2620. (end-of-line 1)
  2621. (setq ts (match-string 1)
  2622. te (match-string 3))
  2623. (setq s (- (time-to-seconds
  2624. (apply 'encode-time (org-parse-time-string te)))
  2625. (time-to-seconds
  2626. (apply 'encode-time (org-parse-time-string ts))))
  2627. neg (< s 0)
  2628. s (abs s)
  2629. h (floor (/ s 3600))
  2630. s (- s (* 3600 h))
  2631. m (floor (/ s 60))
  2632. s (- s (* 60 s)))
  2633. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2634. t))))))
  2635. (defun org-check-running-clock ()
  2636. "Check if the current buffer contains the running clock.
  2637. If yes, offer to stop it and to save the buffer with the changes."
  2638. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2639. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2640. (buffer-name))))
  2641. (org-clock-out)
  2642. (when (y-or-n-p "Save changed buffer?")
  2643. (save-buffer))))
  2644. (defun org-clocktable-try-shift (dir n)
  2645. "Check if this line starts a clock table, if yes, shift the time block."
  2646. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2647. (org-clocktable-shift dir n)))
  2648. ;; Autoload org-timer.el
  2649. (eval-and-compile
  2650. (org-autoload
  2651. "org-timer"
  2652. '(org-timer-start org-timer org-timer-item
  2653. org-timer-change-times-in-region)))
  2654. ;; Autoload org-feed.el
  2655. (eval-and-compile
  2656. (org-autoload
  2657. "org-feed"
  2658. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2659. ;; Autoload archiving code
  2660. ;; The stuff that is needed for cycling and tags has to be defined here.
  2661. (defgroup org-archive nil
  2662. "Options concerning archiving in Org-mode."
  2663. :tag "Org Archive"
  2664. :group 'org-structure)
  2665. (defcustom org-archive-location "%s_archive::"
  2666. "The location where subtrees should be archived.
  2667. The value of this variable is a string, consisting of two parts,
  2668. separated by a double-colon. The first part is a filename and
  2669. the second part is a headline.
  2670. When the filename is omitted, archiving happens in the same file.
  2671. %s in the filename will be replaced by the current file
  2672. name (without the directory part). Archiving to a different file
  2673. is useful to keep archived entries from contributing to the
  2674. Org-mode Agenda.
  2675. The archived entries will be filed as subtrees of the specified
  2676. headline. When the headline is omitted, the subtrees are simply
  2677. filed away at the end of the file, as top-level entries. Also in
  2678. the heading you can use %s to represent the file name, this can be
  2679. useful when using the same archive for a number of different files.
  2680. Here are a few examples:
  2681. \"%s_archive::\"
  2682. If the current file is Projects.org, archive in file
  2683. Projects.org_archive, as top-level trees. This is the default.
  2684. \"::* Archived Tasks\"
  2685. Archive in the current file, under the top-level headline
  2686. \"* Archived Tasks\".
  2687. \"~/org/archive.org::\"
  2688. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2689. \"~/org/archive.org::From %s\"
  2690. Archive in file ~/org/archive.org (absolute path), und headlines
  2691. \"From FILENAME\" where file name is the current file name.
  2692. \"basement::** Finished Tasks\"
  2693. Archive in file ./basement (relative path), as level 3 trees
  2694. below the level 2 heading \"** Finished Tasks\".
  2695. You may set this option on a per-file basis by adding to the buffer a
  2696. line like
  2697. #+ARCHIVE: basement::** Finished Tasks
  2698. You may also define it locally for a subtree by setting an ARCHIVE property
  2699. in the entry. If such a property is found in an entry, or anywhere up
  2700. the hierarchy, it will be used."
  2701. :group 'org-archive
  2702. :type 'string)
  2703. (defcustom org-archive-tag "ARCHIVE"
  2704. "The tag that marks a subtree as archived.
  2705. An archived subtree does not open during visibility cycling, and does
  2706. not contribute to the agenda listings.
  2707. After changing this, font-lock must be restarted in the relevant buffers to
  2708. get the proper fontification."
  2709. :group 'org-archive
  2710. :group 'org-keywords
  2711. :type 'string)
  2712. (defcustom org-agenda-skip-archived-trees t
  2713. "Non-nil means, the agenda will skip any items located in archived trees.
  2714. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2715. variable is no longer recommended, you should leave it at the value t.
  2716. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2717. :group 'org-archive
  2718. :group 'org-agenda-skip
  2719. :type 'boolean)
  2720. (defcustom org-columns-skip-arrchived-trees t
  2721. "Non-nil means, irgnore archived trees when creating column view."
  2722. :group 'org-archive
  2723. :group 'org-properties
  2724. :type 'boolean)
  2725. (defcustom org-cycle-open-archived-trees nil
  2726. "Non-nil means, `org-cycle' will open archived trees.
  2727. An archived tree is a tree marked with the tag ARCHIVE.
  2728. When nil, archived trees will stay folded. You can still open them with
  2729. normal outline commands like `show-all', but not with the cycling commands."
  2730. :group 'org-archive
  2731. :group 'org-cycle
  2732. :type 'boolean)
  2733. (defcustom org-sparse-tree-open-archived-trees nil
  2734. "Non-nil means sparse tree construction shows matches in archived trees.
  2735. When nil, matches in these trees are highlighted, but the trees are kept in
  2736. collapsed state."
  2737. :group 'org-archive
  2738. :group 'org-sparse-trees
  2739. :type 'boolean)
  2740. (defun org-cycle-hide-archived-subtrees (state)
  2741. "Re-hide all archived subtrees after a visibility state change."
  2742. (when (and (not org-cycle-open-archived-trees)
  2743. (not (memq state '(overview folded))))
  2744. (save-excursion
  2745. (let* ((globalp (memq state '(contents all)))
  2746. (beg (if globalp (point-min) (point)))
  2747. (end (if globalp (point-max) (org-end-of-subtree t))))
  2748. (org-hide-archived-subtrees beg end)
  2749. (goto-char beg)
  2750. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2751. (message "%s" (substitute-command-keys
  2752. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2753. (defun org-force-cycle-archived ()
  2754. "Cycle subtree even if it is archived."
  2755. (interactive)
  2756. (setq this-command 'org-cycle)
  2757. (let ((org-cycle-open-archived-trees t))
  2758. (call-interactively 'org-cycle)))
  2759. (defun org-hide-archived-subtrees (beg end)
  2760. "Re-hide all archived subtrees after a visibility state change."
  2761. (save-excursion
  2762. (let* ((re (concat ":" org-archive-tag ":")))
  2763. (goto-char beg)
  2764. (while (re-search-forward re end t)
  2765. (and (org-on-heading-p) (hide-subtree))
  2766. (org-end-of-subtree t)))))
  2767. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2768. (eval-and-compile
  2769. (org-autoload "org-archive"
  2770. '(org-add-archive-files org-archive-subtree
  2771. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2772. ;; Autoload Column View Code
  2773. (declare-function org-columns-number-to-string "org-colview")
  2774. (declare-function org-columns-get-format-and-top-level "org-colview")
  2775. (declare-function org-columns-compute "org-colview")
  2776. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2777. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2778. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2779. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2780. ;; Autoload ID code
  2781. (declare-function org-id-store-link "org-id")
  2782. (declare-function org-id-locations-load "org-id")
  2783. (declare-function org-id-locations-save "org-id")
  2784. (defvar org-id-track-globally)
  2785. (org-autoload "org-id"
  2786. '(org-id-get-create org-id-new org-id-copy org-id-get
  2787. org-id-get-with-outline-path-completion
  2788. org-id-get-with-outline-drilling
  2789. org-id-goto org-id-find org-id-store-link))
  2790. ;; Autoload Plotting Code
  2791. (org-autoload "org-plot"
  2792. '(org-plot/gnuplot))
  2793. ;;; Variables for pre-computed regular expressions, all buffer local
  2794. (defvar org-drawer-regexp nil
  2795. "Matches first line of a hidden block.")
  2796. (make-variable-buffer-local 'org-drawer-regexp)
  2797. (defvar org-todo-regexp nil
  2798. "Matches any of the TODO state keywords.")
  2799. (make-variable-buffer-local 'org-todo-regexp)
  2800. (defvar org-not-done-regexp nil
  2801. "Matches any of the TODO state keywords except the last one.")
  2802. (make-variable-buffer-local 'org-not-done-regexp)
  2803. (defvar org-not-done-heading-regexp nil
  2804. "Matches a TODO headline that is not done.")
  2805. (make-variable-buffer-local 'org-not-done-regexp)
  2806. (defvar org-todo-line-regexp nil
  2807. "Matches a headline and puts TODO state into group 2 if present.")
  2808. (make-variable-buffer-local 'org-todo-line-regexp)
  2809. (defvar org-complex-heading-regexp nil
  2810. "Matches a headline and puts everything into groups:
  2811. group 1: the stars
  2812. group 2: The todo keyword, maybe
  2813. group 3: Priority cookie
  2814. group 4: True headline
  2815. group 5: Tags")
  2816. (make-variable-buffer-local 'org-complex-heading-regexp)
  2817. (defvar org-todo-line-tags-regexp nil
  2818. "Matches a headline and puts TODO state into group 2 if present.
  2819. Also put tags into group 4 if tags are present.")
  2820. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2821. (defvar org-nl-done-regexp nil
  2822. "Matches newline followed by a headline with the DONE keyword.")
  2823. (make-variable-buffer-local 'org-nl-done-regexp)
  2824. (defvar org-looking-at-done-regexp nil
  2825. "Matches the DONE keyword a point.")
  2826. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2827. (defvar org-ds-keyword-length 12
  2828. "Maximum length of the Deadline and SCHEDULED keywords.")
  2829. (make-variable-buffer-local 'org-ds-keyword-length)
  2830. (defvar org-deadline-regexp nil
  2831. "Matches the DEADLINE keyword.")
  2832. (make-variable-buffer-local 'org-deadline-regexp)
  2833. (defvar org-deadline-time-regexp nil
  2834. "Matches the DEADLINE keyword together with a time stamp.")
  2835. (make-variable-buffer-local 'org-deadline-time-regexp)
  2836. (defvar org-deadline-line-regexp nil
  2837. "Matches the DEADLINE keyword and the rest of the line.")
  2838. (make-variable-buffer-local 'org-deadline-line-regexp)
  2839. (defvar org-scheduled-regexp nil
  2840. "Matches the SCHEDULED keyword.")
  2841. (make-variable-buffer-local 'org-scheduled-regexp)
  2842. (defvar org-scheduled-time-regexp nil
  2843. "Matches the SCHEDULED keyword together with a time stamp.")
  2844. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2845. (defvar org-closed-time-regexp nil
  2846. "Matches the CLOSED keyword together with a time stamp.")
  2847. (make-variable-buffer-local 'org-closed-time-regexp)
  2848. (defvar org-keyword-time-regexp nil
  2849. "Matches any of the 4 keywords, together with the time stamp.")
  2850. (make-variable-buffer-local 'org-keyword-time-regexp)
  2851. (defvar org-keyword-time-not-clock-regexp nil
  2852. "Matches any of the 3 keywords, together with the time stamp.")
  2853. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2854. (defvar org-maybe-keyword-time-regexp nil
  2855. "Matches a timestamp, possibly preceeded by a keyword.")
  2856. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2857. (defvar org-planning-or-clock-line-re nil
  2858. "Matches a line with planning or clock info.")
  2859. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2860. (defconst org-plain-time-of-day-regexp
  2861. (concat
  2862. "\\(\\<[012]?[0-9]"
  2863. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2864. "\\(--?"
  2865. "\\(\\<[012]?[0-9]"
  2866. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2867. "\\)?")
  2868. "Regular expression to match a plain time or time range.
  2869. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2870. groups carry important information:
  2871. 0 the full match
  2872. 1 the first time, range or not
  2873. 8 the second time, if it is a range.")
  2874. (defconst org-plain-time-extension-regexp
  2875. (concat
  2876. "\\(\\<[012]?[0-9]"
  2877. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2878. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2879. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2880. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2881. groups carry important information:
  2882. 0 the full match
  2883. 7 hours of duration
  2884. 9 minutes of duration")
  2885. (defconst org-stamp-time-of-day-regexp
  2886. (concat
  2887. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2888. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2889. "\\(--?"
  2890. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2891. "Regular expression to match a timestamp time or time range.
  2892. After a match, the following groups carry important information:
  2893. 0 the full match
  2894. 1 date plus weekday, for backreferencing to make sure both times on same day
  2895. 2 the first time, range or not
  2896. 4 the second time, if it is a range.")
  2897. (defconst org-startup-options
  2898. '(("fold" org-startup-folded t)
  2899. ("overview" org-startup-folded t)
  2900. ("nofold" org-startup-folded nil)
  2901. ("showall" org-startup-folded nil)
  2902. ("content" org-startup-folded content)
  2903. ("hidestars" org-hide-leading-stars t)
  2904. ("showstars" org-hide-leading-stars nil)
  2905. ("odd" org-odd-levels-only t)
  2906. ("oddeven" org-odd-levels-only nil)
  2907. ("align" org-startup-align-all-tables t)
  2908. ("noalign" org-startup-align-all-tables nil)
  2909. ("customtime" org-display-custom-times t)
  2910. ("logdone" org-log-done time)
  2911. ("lognotedone" org-log-done note)
  2912. ("nologdone" org-log-done nil)
  2913. ("lognoteclock-out" org-log-note-clock-out t)
  2914. ("nolognoteclock-out" org-log-note-clock-out nil)
  2915. ("logrepeat" org-log-repeat state)
  2916. ("lognoterepeat" org-log-repeat note)
  2917. ("nologrepeat" org-log-repeat nil)
  2918. ("fninline" org-footnote-define-inline t)
  2919. ("nofninline" org-footnote-define-inline nil)
  2920. ("fnlocal" org-footnote-section nil)
  2921. ("fnauto" org-footnote-auto-label t)
  2922. ("fnprompt" org-footnote-auto-label nil)
  2923. ("fnconfirm" org-footnote-auto-label confirm)
  2924. ("fnplain" org-footnote-auto-label plain)
  2925. ("constcgs" constants-unit-system cgs)
  2926. ("constSI" constants-unit-system SI)
  2927. ("noptag" org-tag-persistent-alist nil))
  2928. "Variable associated with STARTUP options for org-mode.
  2929. Each element is a list of three items: The startup options as written
  2930. in the #+STARTUP line, the corresponding variable, and the value to
  2931. set this variable to if the option is found. An optional forth element PUSH
  2932. means to push this value onto the list in the variable.")
  2933. (defun org-set-regexps-and-options ()
  2934. "Precompute regular expressions for current buffer."
  2935. (when (org-mode-p)
  2936. (org-set-local 'org-todo-kwd-alist nil)
  2937. (org-set-local 'org-todo-key-alist nil)
  2938. (org-set-local 'org-todo-key-trigger nil)
  2939. (org-set-local 'org-todo-keywords-1 nil)
  2940. (org-set-local 'org-done-keywords nil)
  2941. (org-set-local 'org-todo-heads nil)
  2942. (org-set-local 'org-todo-sets nil)
  2943. (org-set-local 'org-todo-log-states nil)
  2944. (org-set-local 'org-file-properties nil)
  2945. (org-set-local 'org-file-tags nil)
  2946. (let ((re (org-make-options-regexp
  2947. '("CATEGORY" "TODO" "COLUMNS"
  2948. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2949. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2950. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2951. (splitre "[ \t]+")
  2952. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2953. tail sep kws1 prio props ftags drawers
  2954. ext-setup-or-nil setup-contents (start 0))
  2955. (save-excursion
  2956. (save-restriction
  2957. (widen)
  2958. (goto-char (point-min))
  2959. (while (or (and ext-setup-or-nil
  2960. (string-match re ext-setup-or-nil start)
  2961. (setq start (match-end 0)))
  2962. (and (setq ext-setup-or-nil nil start 0)
  2963. (re-search-forward re nil t)))
  2964. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2965. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2966. (cond
  2967. ((equal key "CATEGORY")
  2968. (if (string-match "[ \t]+$" value)
  2969. (setq value (replace-match "" t t value)))
  2970. (setq cat value))
  2971. ((member key '("SEQ_TODO" "TODO"))
  2972. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2973. ((equal key "TYP_TODO")
  2974. (push (cons 'type (org-split-string value splitre)) kwds))
  2975. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2976. ;; general TODO-like setup
  2977. (push (cons (intern (downcase (match-string 1 key)))
  2978. (org-split-string value splitre)) kwds))
  2979. ((equal key "TAGS")
  2980. (setq tags (append tags (if tags '("\\n") nil)
  2981. (org-split-string value splitre))))
  2982. ((equal key "COLUMNS")
  2983. (org-set-local 'org-columns-default-format value))
  2984. ((equal key "LINK")
  2985. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2986. (push (cons (match-string 1 value)
  2987. (org-trim (match-string 2 value)))
  2988. links)))
  2989. ((equal key "PRIORITIES")
  2990. (setq prio (org-split-string value " +")))
  2991. ((equal key "PROPERTY")
  2992. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2993. (push (cons (match-string 1 value) (match-string 2 value))
  2994. props)))
  2995. ((equal key "FILETAGS")
  2996. (when (string-match "\\S-" value)
  2997. (setq ftags
  2998. (append
  2999. ftags
  3000. (apply 'append
  3001. (mapcar (lambda (x) (org-split-string x ":"))
  3002. (org-split-string value)))))))
  3003. ((equal key "DRAWERS")
  3004. (setq drawers (org-split-string value splitre)))
  3005. ((equal key "CONSTANTS")
  3006. (setq const (append const (org-split-string value splitre))))
  3007. ((equal key "STARTUP")
  3008. (let ((opts (org-split-string value splitre))
  3009. l var val)
  3010. (while (setq l (pop opts))
  3011. (when (setq l (assoc l org-startup-options))
  3012. (setq var (nth 1 l) val (nth 2 l))
  3013. (if (not (nth 3 l))
  3014. (set (make-local-variable var) val)
  3015. (if (not (listp (symbol-value var)))
  3016. (set (make-local-variable var) nil))
  3017. (set (make-local-variable var) (symbol-value var))
  3018. (add-to-list var val))))))
  3019. ((equal key "ARCHIVE")
  3020. (string-match " *$" value)
  3021. (setq arch (replace-match "" t t value))
  3022. (remove-text-properties 0 (length arch)
  3023. '(face t fontified t) arch))
  3024. ((equal key "SETUPFILE")
  3025. (setq setup-contents (org-file-contents
  3026. (expand-file-name
  3027. (org-remove-double-quotes value))
  3028. 'noerror))
  3029. (if (not ext-setup-or-nil)
  3030. (setq ext-setup-or-nil setup-contents start 0)
  3031. (setq ext-setup-or-nil
  3032. (concat (substring ext-setup-or-nil 0 start)
  3033. "\n" setup-contents "\n"
  3034. (substring ext-setup-or-nil start)))))
  3035. ))))
  3036. (when cat
  3037. (org-set-local 'org-category (intern cat))
  3038. (push (cons "CATEGORY" cat) props))
  3039. (when prio
  3040. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3041. (setq prio (mapcar 'string-to-char prio))
  3042. (org-set-local 'org-highest-priority (nth 0 prio))
  3043. (org-set-local 'org-lowest-priority (nth 1 prio))
  3044. (org-set-local 'org-default-priority (nth 2 prio)))
  3045. (and props (org-set-local 'org-file-properties (nreverse props)))
  3046. (and ftags (org-set-local 'org-file-tags
  3047. (mapcar 'org-add-prop-inherited ftags)))
  3048. (and drawers (org-set-local 'org-drawers drawers))
  3049. (and arch (org-set-local 'org-archive-location arch))
  3050. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3051. ;; Process the TODO keywords
  3052. (unless kwds
  3053. ;; Use the global values as if they had been given locally.
  3054. (setq kwds (default-value 'org-todo-keywords))
  3055. (if (stringp (car kwds))
  3056. (setq kwds (list (cons org-todo-interpretation
  3057. (default-value 'org-todo-keywords)))))
  3058. (setq kwds (reverse kwds)))
  3059. (setq kwds (nreverse kwds))
  3060. (let (inter kws kw)
  3061. (while (setq kws (pop kwds))
  3062. (let ((kws (or
  3063. (run-hook-with-args-until-success
  3064. 'org-todo-setup-filter-hook kws)
  3065. kws)))
  3066. (setq inter (pop kws) sep (member "|" kws)
  3067. kws0 (delete "|" (copy-sequence kws))
  3068. kwsa nil
  3069. kws1 (mapcar
  3070. (lambda (x)
  3071. ;; 1 2
  3072. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3073. (progn
  3074. (setq kw (match-string 1 x)
  3075. key (and (match-end 2) (match-string 2 x))
  3076. log (org-extract-log-state-settings x))
  3077. (push (cons kw (and key (string-to-char key))) kwsa)
  3078. (and log (push log org-todo-log-states))
  3079. kw)
  3080. (error "Invalid TODO keyword %s" x)))
  3081. kws0)
  3082. kwsa (if kwsa (append '((:startgroup))
  3083. (nreverse kwsa)
  3084. '((:endgroup))))
  3085. hw (car kws1)
  3086. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3087. tail (list inter hw (car dws) (org-last dws))))
  3088. (add-to-list 'org-todo-heads hw 'append)
  3089. (push kws1 org-todo-sets)
  3090. (setq org-done-keywords (append org-done-keywords dws nil))
  3091. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3092. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3093. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3094. (setq org-todo-sets (nreverse org-todo-sets)
  3095. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3096. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3097. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3098. ;; Process the constants
  3099. (when const
  3100. (let (e cst)
  3101. (while (setq e (pop const))
  3102. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3103. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3104. (setq org-table-formula-constants-local cst)))
  3105. ;; Process the tags.
  3106. (when tags
  3107. (let (e tgs)
  3108. (while (setq e (pop tags))
  3109. (cond
  3110. ((equal e "{") (push '(:startgroup) tgs))
  3111. ((equal e "}") (push '(:endgroup) tgs))
  3112. ((equal e "\\n") (push '(:newline) tgs))
  3113. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3114. (push (cons (match-string 1 e)
  3115. (string-to-char (match-string 2 e)))
  3116. tgs))
  3117. (t (push (list e) tgs))))
  3118. (org-set-local 'org-tag-alist nil)
  3119. (while (setq e (pop tgs))
  3120. (or (and (stringp (car e))
  3121. (assoc (car e) org-tag-alist))
  3122. (push e org-tag-alist)))))
  3123. ;; Compute the regular expressions and other local variables
  3124. (if (not org-done-keywords)
  3125. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3126. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3127. (length org-scheduled-string)
  3128. (length org-clock-string)
  3129. (length org-closed-string)))
  3130. org-drawer-regexp
  3131. (concat "^[ \t]*:\\("
  3132. (mapconcat 'regexp-quote org-drawers "\\|")
  3133. "\\):[ \t]*$")
  3134. org-not-done-keywords
  3135. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3136. org-todo-regexp
  3137. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3138. "\\|") "\\)\\>")
  3139. org-not-done-regexp
  3140. (concat "\\<\\("
  3141. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3142. "\\)\\>")
  3143. org-not-done-heading-regexp
  3144. (concat "^\\(\\*+\\)[ \t]+\\("
  3145. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3146. "\\)\\>")
  3147. org-todo-line-regexp
  3148. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3149. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3150. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3151. org-complex-heading-regexp
  3152. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3153. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3154. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3155. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3156. org-nl-done-regexp
  3157. (concat "\n\\*+[ \t]+"
  3158. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3159. "\\)" "\\>")
  3160. org-todo-line-tags-regexp
  3161. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3162. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3163. (org-re
  3164. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3165. org-looking-at-done-regexp
  3166. (concat "^" "\\(?:"
  3167. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3168. "\\>")
  3169. org-deadline-regexp (concat "\\<" org-deadline-string)
  3170. org-deadline-time-regexp
  3171. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3172. org-deadline-line-regexp
  3173. (concat "\\<\\(" org-deadline-string "\\).*")
  3174. org-scheduled-regexp
  3175. (concat "\\<" org-scheduled-string)
  3176. org-scheduled-time-regexp
  3177. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3178. org-closed-time-regexp
  3179. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3180. org-keyword-time-regexp
  3181. (concat "\\<\\(" org-scheduled-string
  3182. "\\|" org-deadline-string
  3183. "\\|" org-closed-string
  3184. "\\|" org-clock-string "\\)"
  3185. " *[[<]\\([^]>]+\\)[]>]")
  3186. org-keyword-time-not-clock-regexp
  3187. (concat "\\<\\(" org-scheduled-string
  3188. "\\|" org-deadline-string
  3189. "\\|" org-closed-string
  3190. "\\)"
  3191. " *[[<]\\([^]>]+\\)[]>]")
  3192. org-maybe-keyword-time-regexp
  3193. (concat "\\(\\<\\(" org-scheduled-string
  3194. "\\|" org-deadline-string
  3195. "\\|" org-closed-string
  3196. "\\|" org-clock-string "\\)\\)?"
  3197. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3198. org-planning-or-clock-line-re
  3199. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3200. "\\|" org-deadline-string
  3201. "\\|" org-closed-string "\\|" org-clock-string
  3202. "\\)\\>\\)")
  3203. )
  3204. (org-compute-latex-and-specials-regexp)
  3205. (org-set-font-lock-defaults))))
  3206. (defun org-file-contents (file &optional noerror)
  3207. "Return the contents of FILE, as a string."
  3208. (if (or (not file)
  3209. (not (file-readable-p file)))
  3210. (if noerror
  3211. (progn
  3212. (message "Cannot read file %s" file)
  3213. (ding) (sit-for 2)
  3214. "")
  3215. (error "Cannot read file %s" file))
  3216. (with-temp-buffer
  3217. (insert-file-contents file)
  3218. (buffer-string))))
  3219. (defun org-extract-log-state-settings (x)
  3220. "Extract the log state setting from a TODO keyword string.
  3221. This will extract info from a string like \"WAIT(w@/!)\"."
  3222. (let (kw key log1 log2)
  3223. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3224. (setq kw (match-string 1 x)
  3225. key (and (match-end 2) (match-string 2 x))
  3226. log1 (and (match-end 3) (match-string 3 x))
  3227. log2 (and (match-end 4) (match-string 4 x)))
  3228. (and (or log1 log2)
  3229. (list kw
  3230. (and log1 (if (equal log1 "!") 'time 'note))
  3231. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3232. (defun org-remove-keyword-keys (list)
  3233. "Remove a pair of parenthesis at the end of each string in LIST."
  3234. (mapcar (lambda (x)
  3235. (if (string-match "(.*)$" x)
  3236. (substring x 0 (match-beginning 0))
  3237. x))
  3238. list))
  3239. ;; FIXME: this could be done much better, using second characters etc.
  3240. (defun org-assign-fast-keys (alist)
  3241. "Assign fast keys to a keyword-key alist.
  3242. Respect keys that are already there."
  3243. (let (new e k c c1 c2 (char ?a))
  3244. (while (setq e (pop alist))
  3245. (cond
  3246. ((equal e '(:startgroup)) (push e new))
  3247. ((equal e '(:endgroup)) (push e new))
  3248. ((equal e '(:newline)) (push e new))
  3249. (t
  3250. (setq k (car e) c2 nil)
  3251. (if (cdr e)
  3252. (setq c (cdr e))
  3253. ;; automatically assign a character.
  3254. (setq c1 (string-to-char
  3255. (downcase (substring
  3256. k (if (= (string-to-char k) ?@) 1 0)))))
  3257. (if (or (rassoc c1 new) (rassoc c1 alist))
  3258. (while (or (rassoc char new) (rassoc char alist))
  3259. (setq char (1+ char)))
  3260. (setq c2 c1))
  3261. (setq c (or c2 char)))
  3262. (push (cons k c) new))))
  3263. (nreverse new)))
  3264. ;;; Some variables used in various places
  3265. (defvar org-window-configuration nil
  3266. "Used in various places to store a window configuration.")
  3267. (defvar org-finish-function nil
  3268. "Function to be called when `C-c C-c' is used.
  3269. This is for getting out of special buffers like remember.")
  3270. ;; FIXME: Occasionally check by commenting these, to make sure
  3271. ;; no other functions uses these, forgetting to let-bind them.
  3272. (defvar entry)
  3273. (defvar state)
  3274. (defvar last-state)
  3275. (defvar date)
  3276. (defvar description)
  3277. ;; Defined somewhere in this file, but used before definition.
  3278. (defvar org-html-entities)
  3279. (defvar org-struct-menu)
  3280. (defvar org-org-menu)
  3281. (defvar org-tbl-menu)
  3282. (defvar org-agenda-keymap)
  3283. ;;;; Define the Org-mode
  3284. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3285. (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."))
  3286. ;; We use a before-change function to check if a table might need
  3287. ;; an update.
  3288. (defvar org-table-may-need-update t
  3289. "Indicates that a table might need an update.
  3290. This variable is set by `org-before-change-function'.
  3291. `org-table-align' sets it back to nil.")
  3292. (defun org-before-change-function (beg end)
  3293. "Every change indicates that a table might need an update."
  3294. (setq org-table-may-need-update t))
  3295. (defvar org-mode-map)
  3296. (defvar org-mode-hook nil
  3297. "Mode hook for Org-mode, run after the mode was turned on.")
  3298. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3299. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3300. (defvar org-table-buffer-is-an nil)
  3301. (defconst org-outline-regexp "\\*+ ")
  3302. ;;;###autoload
  3303. (define-derived-mode org-mode outline-mode "Org"
  3304. "Outline-based notes management and organizer, alias
  3305. \"Carsten's outline-mode for keeping track of everything.\"
  3306. Org-mode develops organizational tasks around a NOTES file which
  3307. contains information about projects as plain text. Org-mode is
  3308. implemented on top of outline-mode, which is ideal to keep the content
  3309. of large files well structured. It supports ToDo items, deadlines and
  3310. time stamps, which magically appear in the diary listing of the Emacs
  3311. calendar. Tables are easily created with a built-in table editor.
  3312. Plain text URL-like links connect to websites, emails (VM), Usenet
  3313. messages (Gnus), BBDB entries, and any files related to the project.
  3314. For printing and sharing of notes, an Org-mode file (or a part of it)
  3315. can be exported as a structured ASCII or HTML file.
  3316. The following commands are available:
  3317. \\{org-mode-map}"
  3318. ;; Get rid of Outline menus, they are not needed
  3319. ;; Need to do this here because define-derived-mode sets up
  3320. ;; the keymap so late. Still, it is a waste to call this each time
  3321. ;; we switch another buffer into org-mode.
  3322. (if (featurep 'xemacs)
  3323. (when (boundp 'outline-mode-menu-heading)
  3324. ;; Assume this is Greg's port, it used easymenu
  3325. (easy-menu-remove outline-mode-menu-heading)
  3326. (easy-menu-remove outline-mode-menu-show)
  3327. (easy-menu-remove outline-mode-menu-hide))
  3328. (define-key org-mode-map [menu-bar headings] 'undefined)
  3329. (define-key org-mode-map [menu-bar hide] 'undefined)
  3330. (define-key org-mode-map [menu-bar show] 'undefined))
  3331. (org-load-modules-maybe)
  3332. (easy-menu-add org-org-menu)
  3333. (easy-menu-add org-tbl-menu)
  3334. (org-install-agenda-files-menu)
  3335. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3336. (org-add-to-invisibility-spec '(org-cwidth))
  3337. (when (featurep 'xemacs)
  3338. (org-set-local 'line-move-ignore-invisible t))
  3339. (org-set-local 'outline-regexp org-outline-regexp)
  3340. (org-set-local 'outline-level 'org-outline-level)
  3341. (when (and org-ellipsis
  3342. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3343. (fboundp 'make-glyph-code))
  3344. (unless org-display-table
  3345. (setq org-display-table (make-display-table)))
  3346. (set-display-table-slot
  3347. org-display-table 4
  3348. (vconcat (mapcar
  3349. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3350. org-ellipsis)))
  3351. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3352. (setq buffer-display-table org-display-table))
  3353. (org-set-regexps-and-options)
  3354. (when (and org-tag-faces (not org-tags-special-faces-re))
  3355. ;; tag faces set outside customize.... force initialization.
  3356. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3357. ;; Calc embedded
  3358. (org-set-local 'calc-embedded-open-mode "# ")
  3359. (modify-syntax-entry ?# "<")
  3360. (modify-syntax-entry ?@ "w")
  3361. (if org-startup-truncated (setq truncate-lines t))
  3362. (org-set-local 'font-lock-unfontify-region-function
  3363. 'org-unfontify-region)
  3364. ;; Activate before-change-function
  3365. (org-set-local 'org-table-may-need-update t)
  3366. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3367. 'local)
  3368. ;; Check for running clock before killing a buffer
  3369. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3370. ;; Paragraphs and auto-filling
  3371. (org-set-autofill-regexps)
  3372. (setq indent-line-function 'org-indent-line-function)
  3373. (org-update-radio-target-regexp)
  3374. ;; Make sure dependence stuff works reliably, even for users who set it
  3375. ;; too late :-(
  3376. (if org-enforce-todo-dependencies
  3377. (add-hook 'org-blocker-hook
  3378. 'org-block-todo-from-children-or-siblings-or-parent)
  3379. (remove-hook 'org-blocker-hook
  3380. 'org-block-todo-from-children-or-siblings-or-parent))
  3381. (if org-enforce-todo-checkbox-dependencies
  3382. (add-hook 'org-blocker-hook
  3383. 'org-block-todo-from-checkboxes)
  3384. (remove-hook 'org-blocker-hook
  3385. 'org-block-todo-from-checkboxes))
  3386. ;; Comment characters
  3387. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3388. (org-set-local 'comment-padding " ")
  3389. ;; Align options lines
  3390. (org-set-local
  3391. 'align-mode-rules-list
  3392. '((org-in-buffer-settings
  3393. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3394. (modes . '(org-mode)))))
  3395. ;; Imenu
  3396. (org-set-local 'imenu-create-index-function
  3397. 'org-imenu-get-tree)
  3398. ;; Make isearch reveal context
  3399. (if (or (featurep 'xemacs)
  3400. (not (boundp 'outline-isearch-open-invisible-function)))
  3401. ;; Emacs 21 and XEmacs make use of the hook
  3402. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3403. ;; Emacs 22 deals with this through a special variable
  3404. (org-set-local 'outline-isearch-open-invisible-function
  3405. (lambda (&rest ignore) (org-show-context 'isearch))))
  3406. ;; If empty file that did not turn on org-mode automatically, make it to.
  3407. (if (and org-insert-mode-line-in-empty-file
  3408. (interactive-p)
  3409. (= (point-min) (point-max)))
  3410. (insert "# -*- mode: org -*-\n\n"))
  3411. (unless org-inhibit-startup
  3412. (when org-startup-align-all-tables
  3413. (let ((bmp (buffer-modified-p)))
  3414. (org-table-map-tables 'org-table-align)
  3415. (set-buffer-modified-p bmp)))
  3416. (org-set-startup-visibility)))
  3417. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3418. (defun org-current-time ()
  3419. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3420. (if (> (car org-time-stamp-rounding-minutes) 1)
  3421. (let ((r (car org-time-stamp-rounding-minutes))
  3422. (time (decode-time)))
  3423. (apply 'encode-time
  3424. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3425. (nthcdr 2 time))))
  3426. (current-time)))
  3427. ;;;; Font-Lock stuff, including the activators
  3428. (defvar org-mouse-map (make-sparse-keymap))
  3429. (org-defkey org-mouse-map
  3430. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3431. (org-defkey org-mouse-map
  3432. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3433. (when org-mouse-1-follows-link
  3434. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3435. (when org-tab-follows-link
  3436. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3437. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3438. (require 'font-lock)
  3439. (defconst org-non-link-chars "]\t\n\r<>")
  3440. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3441. "shell" "elisp"))
  3442. (defvar org-link-types-re nil
  3443. "Matches a link that has a url-like prefix like \"http:\"")
  3444. (defvar org-link-re-with-space nil
  3445. "Matches a link with spaces, optional angular brackets around it.")
  3446. (defvar org-link-re-with-space2 nil
  3447. "Matches a link with spaces, optional angular brackets around it.")
  3448. (defvar org-link-re-with-space3 nil
  3449. "Matches a link with spaces, only for internal part in bracket links.")
  3450. (defvar org-angle-link-re nil
  3451. "Matches link with angular brackets, spaces are allowed.")
  3452. (defvar org-plain-link-re nil
  3453. "Matches plain link, without spaces.")
  3454. (defvar org-bracket-link-regexp nil
  3455. "Matches a link in double brackets.")
  3456. (defvar org-bracket-link-analytic-regexp nil
  3457. "Regular expression used to analyze links.
  3458. Here is what the match groups contain after a match:
  3459. 1: http:
  3460. 2: http
  3461. 3: path
  3462. 4: [desc]
  3463. 5: desc")
  3464. (defvar org-bracket-link-analytic-regexp++ nil
  3465. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3466. (defvar org-any-link-re nil
  3467. "Regular expression matching any link.")
  3468. (defun org-make-link-regexps ()
  3469. "Update the link regular expressions.
  3470. This should be called after the variable `org-link-types' has changed."
  3471. (setq org-link-types-re
  3472. (concat
  3473. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3474. org-link-re-with-space
  3475. (concat
  3476. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3477. "\\([^" org-non-link-chars " ]"
  3478. "[^" org-non-link-chars "]*"
  3479. "[^" org-non-link-chars " ]\\)>?")
  3480. org-link-re-with-space2
  3481. (concat
  3482. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3483. "\\([^" org-non-link-chars " ]"
  3484. "[^\t\n\r]*"
  3485. "[^" org-non-link-chars " ]\\)>?")
  3486. org-link-re-with-space3
  3487. (concat
  3488. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3489. "\\([^" org-non-link-chars " ]"
  3490. "[^\t\n\r]*\\)")
  3491. org-angle-link-re
  3492. (concat
  3493. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3494. "\\([^" org-non-link-chars " ]"
  3495. "[^" org-non-link-chars "]*"
  3496. "\\)>")
  3497. org-plain-link-re
  3498. (concat
  3499. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3500. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3501. org-bracket-link-regexp
  3502. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3503. org-bracket-link-analytic-regexp
  3504. (concat
  3505. "\\[\\["
  3506. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3507. "\\([^]]+\\)"
  3508. "\\]"
  3509. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3510. "\\]")
  3511. org-bracket-link-analytic-regexp++
  3512. (concat
  3513. "\\[\\["
  3514. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3515. "\\([^]]+\\)"
  3516. "\\]"
  3517. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3518. "\\]")
  3519. org-any-link-re
  3520. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3521. org-angle-link-re "\\)\\|\\("
  3522. org-plain-link-re "\\)")))
  3523. (org-make-link-regexps)
  3524. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3525. "Regular expression for fast time stamp matching.")
  3526. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3527. "Regular expression for fast time stamp matching.")
  3528. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3529. "Regular expression matching time strings for analysis.
  3530. This one does not require the space after the date, so it can be used
  3531. on a string that terminates immediately after the date.")
  3532. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3533. "Regular expression matching time strings for analysis.")
  3534. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3535. "Regular expression matching time stamps, with groups.")
  3536. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3537. "Regular expression matching time stamps (also [..]), with groups.")
  3538. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3539. "Regular expression matching a time stamp range.")
  3540. (defconst org-tr-regexp-both
  3541. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3542. "Regular expression matching a time stamp range.")
  3543. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3544. org-ts-regexp "\\)?")
  3545. "Regular expression matching a time stamp or time stamp range.")
  3546. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3547. org-ts-regexp-both "\\)?")
  3548. "Regular expression matching a time stamp or time stamp range.
  3549. The time stamps may be either active or inactive.")
  3550. (defvar org-emph-face nil)
  3551. (defun org-do-emphasis-faces (limit)
  3552. "Run through the buffer and add overlays to links."
  3553. (let (rtn)
  3554. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3555. (if (not (= (char-after (match-beginning 3))
  3556. (char-after (match-beginning 4))))
  3557. (progn
  3558. (setq rtn t)
  3559. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3560. 'face
  3561. (nth 1 (assoc (match-string 3)
  3562. org-emphasis-alist)))
  3563. (add-text-properties (match-beginning 2) (match-end 2)
  3564. '(font-lock-multiline t))
  3565. (when org-hide-emphasis-markers
  3566. (add-text-properties (match-end 4) (match-beginning 5)
  3567. '(invisible org-link))
  3568. (add-text-properties (match-beginning 3) (match-end 3)
  3569. '(invisible org-link)))))
  3570. (backward-char 1))
  3571. rtn))
  3572. (defun org-emphasize (&optional char)
  3573. "Insert or change an emphasis, i.e. a font like bold or italic.
  3574. If there is an active region, change that region to a new emphasis.
  3575. If there is no region, just insert the marker characters and position
  3576. the cursor between them.
  3577. CHAR should be either the marker character, or the first character of the
  3578. HTML tag associated with that emphasis. If CHAR is a space, the means
  3579. to remove the emphasis of the selected region.
  3580. If char is not given (for example in an interactive call) it
  3581. will be prompted for."
  3582. (interactive)
  3583. (let ((eal org-emphasis-alist) e det
  3584. (erc org-emphasis-regexp-components)
  3585. (prompt "")
  3586. (string "") beg end move tag c s)
  3587. (if (org-region-active-p)
  3588. (setq beg (region-beginning) end (region-end)
  3589. string (buffer-substring beg end))
  3590. (setq move t))
  3591. (while (setq e (pop eal))
  3592. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3593. c (aref tag 0))
  3594. (push (cons c (string-to-char (car e))) det)
  3595. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3596. (substring tag 1)))))
  3597. (setq det (nreverse det))
  3598. (unless char
  3599. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3600. (setq char (read-char-exclusive)))
  3601. (setq char (or (cdr (assoc char det)) char))
  3602. (if (equal char ?\ )
  3603. (setq s "" move nil)
  3604. (unless (assoc (char-to-string char) org-emphasis-alist)
  3605. (error "No such emphasis marker: \"%c\"" char))
  3606. (setq s (char-to-string char)))
  3607. (while (and (> (length string) 1)
  3608. (equal (substring string 0 1) (substring string -1))
  3609. (assoc (substring string 0 1) org-emphasis-alist))
  3610. (setq string (substring string 1 -1)))
  3611. (setq string (concat s string s))
  3612. (if beg (delete-region beg end))
  3613. (unless (or (bolp)
  3614. (string-match (concat "[" (nth 0 erc) "\n]")
  3615. (char-to-string (char-before (point)))))
  3616. (insert " "))
  3617. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3618. (char-to-string (char-after (point))))
  3619. (insert " ") (backward-char 1))
  3620. (insert string)
  3621. (and move (backward-char 1))))
  3622. (defconst org-nonsticky-props
  3623. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3624. (defun org-activate-plain-links (limit)
  3625. "Run through the buffer and add overlays to links."
  3626. (catch 'exit
  3627. (let (f)
  3628. (while (re-search-forward org-plain-link-re limit t)
  3629. (setq f (get-text-property (match-beginning 0) 'face))
  3630. (if (or (eq f 'org-tag)
  3631. (and (listp f) (memq 'org-tag f)))
  3632. nil
  3633. (add-text-properties (match-beginning 0) (match-end 0)
  3634. (list 'mouse-face 'highlight
  3635. 'rear-nonsticky org-nonsticky-props
  3636. 'keymap org-mouse-map
  3637. ))
  3638. (throw 'exit t))))))
  3639. (defun org-activate-code (limit)
  3640. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3641. (progn
  3642. (remove-text-properties (match-beginning 0) (match-end 0)
  3643. '(display t invisible t intangible t))
  3644. t)))
  3645. (defun org-activate-angle-links (limit)
  3646. "Run through the buffer and add overlays to links."
  3647. (if (re-search-forward org-angle-link-re limit t)
  3648. (progn
  3649. (add-text-properties (match-beginning 0) (match-end 0)
  3650. (list 'mouse-face 'highlight
  3651. 'rear-nonsticky org-nonsticky-props
  3652. 'keymap org-mouse-map
  3653. ))
  3654. t)))
  3655. (defun org-activate-footnote-links (limit)
  3656. "Run through the buffer and add overlays to links."
  3657. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3658. limit t)
  3659. (progn
  3660. (add-text-properties (match-beginning 2) (match-end 2)
  3661. (list 'mouse-face 'highlight
  3662. 'rear-nonsticky org-nonsticky-props
  3663. 'keymap org-mouse-map
  3664. 'help-echo
  3665. (if (= (point-at-bol) (match-beginning 2))
  3666. "Footnote definition"
  3667. "Footnote reference")
  3668. ))
  3669. t)))
  3670. (defun org-activate-bracket-links (limit)
  3671. "Run through the buffer and add overlays to bracketed links."
  3672. (if (re-search-forward org-bracket-link-regexp limit t)
  3673. (let* ((help (concat "LINK: "
  3674. (org-match-string-no-properties 1)))
  3675. ;; FIXME: above we should remove the escapes.
  3676. ;; but that requires another match, protecting match data,
  3677. ;; a lot of overhead for font-lock.
  3678. (ip (org-maybe-intangible
  3679. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3680. 'keymap org-mouse-map 'mouse-face 'highlight
  3681. 'font-lock-multiline t 'help-echo help)))
  3682. (vp (list 'rear-nonsticky org-nonsticky-props
  3683. 'keymap org-mouse-map 'mouse-face 'highlight
  3684. 'font-lock-multiline t 'help-echo help)))
  3685. ;; We need to remove the invisible property here. Table narrowing
  3686. ;; may have made some of this invisible.
  3687. (remove-text-properties (match-beginning 0) (match-end 0)
  3688. '(invisible nil))
  3689. (if (match-end 3)
  3690. (progn
  3691. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3692. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3693. (add-text-properties (match-end 3) (match-end 0) ip))
  3694. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3695. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3696. (add-text-properties (match-end 1) (match-end 0) ip))
  3697. t)))
  3698. (defun org-activate-dates (limit)
  3699. "Run through the buffer and add overlays to dates."
  3700. (if (re-search-forward org-tsr-regexp-both limit t)
  3701. (progn
  3702. (add-text-properties (match-beginning 0) (match-end 0)
  3703. (list 'mouse-face 'highlight
  3704. 'rear-nonsticky org-nonsticky-props
  3705. 'keymap org-mouse-map))
  3706. (when org-display-custom-times
  3707. (if (match-end 3)
  3708. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3709. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3710. t)))
  3711. (defvar org-target-link-regexp nil
  3712. "Regular expression matching radio targets in plain text.")
  3713. (make-variable-buffer-local 'org-target-link-regexp)
  3714. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3715. "Regular expression matching a link target.")
  3716. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3717. "Regular expression matching a radio target.")
  3718. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3719. "Regular expression matching any target.")
  3720. (defun org-activate-target-links (limit)
  3721. "Run through the buffer and add overlays to target matches."
  3722. (when org-target-link-regexp
  3723. (let ((case-fold-search t))
  3724. (if (re-search-forward org-target-link-regexp limit t)
  3725. (progn
  3726. (add-text-properties (match-beginning 0) (match-end 0)
  3727. (list 'mouse-face 'highlight
  3728. 'rear-nonsticky org-nonsticky-props
  3729. 'keymap org-mouse-map
  3730. 'help-echo "Radio target link"
  3731. 'org-linked-text t))
  3732. t)))))
  3733. (defun org-update-radio-target-regexp ()
  3734. "Find all radio targets in this file and update the regular expression."
  3735. (interactive)
  3736. (when (memq 'radio org-activate-links)
  3737. (setq org-target-link-regexp
  3738. (org-make-target-link-regexp (org-all-targets 'radio)))
  3739. (org-restart-font-lock)))
  3740. (defun org-hide-wide-columns (limit)
  3741. (let (s e)
  3742. (setq s (text-property-any (point) (or limit (point-max))
  3743. 'org-cwidth t))
  3744. (when s
  3745. (setq e (next-single-property-change s 'org-cwidth))
  3746. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3747. (goto-char e)
  3748. t)))
  3749. (defvar org-latex-and-specials-regexp nil
  3750. "Regular expression for highlighting export special stuff.")
  3751. (defvar org-match-substring-regexp)
  3752. (defvar org-match-substring-with-braces-regexp)
  3753. (defvar org-export-html-special-string-regexps)
  3754. (defun org-compute-latex-and-specials-regexp ()
  3755. "Compute regular expression for stuff treated specially by exporters."
  3756. (if (not org-highlight-latex-fragments-and-specials)
  3757. (org-set-local 'org-latex-and-specials-regexp nil)
  3758. (require 'org-exp)
  3759. (let*
  3760. ((matchers (plist-get org-format-latex-options :matchers))
  3761. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3762. org-latex-regexps)))
  3763. (options (org-combine-plists (org-default-export-plist)
  3764. (org-infile-export-plist)))
  3765. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3766. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3767. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3768. (org-export-html-expand (plist-get options :expand-quoted-html))
  3769. (org-export-with-special-strings (plist-get options :special-strings))
  3770. (re-sub
  3771. (cond
  3772. ((equal org-export-with-sub-superscripts '{})
  3773. (list org-match-substring-with-braces-regexp))
  3774. (org-export-with-sub-superscripts
  3775. (list org-match-substring-regexp))
  3776. (t nil)))
  3777. (re-latex
  3778. (if org-export-with-LaTeX-fragments
  3779. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3780. (re-macros
  3781. (if org-export-with-TeX-macros
  3782. (list (concat "\\\\"
  3783. (regexp-opt
  3784. (append (mapcar 'car org-html-entities)
  3785. (if (boundp 'org-latex-entities)
  3786. org-latex-entities nil))
  3787. 'words))) ; FIXME
  3788. ))
  3789. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3790. (re-special (if org-export-with-special-strings
  3791. (mapcar (lambda (x) (car x))
  3792. org-export-html-special-string-regexps)))
  3793. (re-rest
  3794. (delq nil
  3795. (list
  3796. (if org-export-html-expand "@<[^>\n]+>")
  3797. ))))
  3798. (org-set-local
  3799. 'org-latex-and-specials-regexp
  3800. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3801. re-rest) "\\|")))))
  3802. (defun org-do-latex-and-special-faces (limit)
  3803. "Run through the buffer and add overlays to links."
  3804. (when org-latex-and-specials-regexp
  3805. (let (rtn d)
  3806. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3807. limit t))
  3808. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3809. 'face))
  3810. '(org-code org-verbatim underline)))
  3811. (progn
  3812. (setq rtn t
  3813. d (cond ((member (char-after (1+ (match-beginning 0)))
  3814. '(?_ ?^)) 1)
  3815. (t 0)))
  3816. (font-lock-prepend-text-property
  3817. (+ d (match-beginning 0)) (match-end 0)
  3818. 'face 'org-latex-and-export-specials)
  3819. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3820. '(font-lock-multiline t)))))
  3821. rtn)))
  3822. (defun org-restart-font-lock ()
  3823. "Restart font-lock-mode, to force refontification."
  3824. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3825. (font-lock-mode -1)
  3826. (font-lock-mode 1)))
  3827. (defun org-all-targets (&optional radio)
  3828. "Return a list of all targets in this file.
  3829. With optional argument RADIO, only find radio targets."
  3830. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3831. rtn)
  3832. (save-excursion
  3833. (goto-char (point-min))
  3834. (while (re-search-forward re nil t)
  3835. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3836. rtn)))
  3837. (defun org-make-target-link-regexp (targets)
  3838. "Make regular expression matching all strings in TARGETS.
  3839. The regular expression finds the targets also if there is a line break
  3840. between words."
  3841. (and targets
  3842. (concat
  3843. "\\<\\("
  3844. (mapconcat
  3845. (lambda (x)
  3846. (while (string-match " +" x)
  3847. (setq x (replace-match "\\s-+" t t x)))
  3848. x)
  3849. targets
  3850. "\\|")
  3851. "\\)\\>")))
  3852. (defun org-activate-tags (limit)
  3853. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3854. (progn
  3855. (add-text-properties (match-beginning 1) (match-end 1)
  3856. (list 'mouse-face 'highlight
  3857. 'rear-nonsticky org-nonsticky-props
  3858. 'keymap org-mouse-map))
  3859. t)))
  3860. (defun org-outline-level ()
  3861. (save-excursion
  3862. (looking-at outline-regexp)
  3863. (if (match-beginning 1)
  3864. (+ (org-get-string-indentation (match-string 1)) 1000)
  3865. (1- (- (match-end 0) (match-beginning 0))))))
  3866. (defvar org-font-lock-keywords nil)
  3867. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3868. "Regular expression matching a property line.")
  3869. (defvar org-font-lock-hook nil
  3870. "Functions to be called for special font lock stuff.")
  3871. (defun org-font-lock-hook (limit)
  3872. (run-hook-with-args 'org-font-lock-hook limit))
  3873. (defun org-set-font-lock-defaults ()
  3874. (let* ((em org-fontify-emphasized-text)
  3875. (lk org-activate-links)
  3876. (org-font-lock-extra-keywords
  3877. (list
  3878. ;; Call the hook
  3879. '(org-font-lock-hook)
  3880. ;; Headlines
  3881. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3882. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3883. ;; Table lines
  3884. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3885. (1 'org-table t))
  3886. ;; Table internals
  3887. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3888. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3889. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3890. ;; Drawers
  3891. (list org-drawer-regexp '(0 'org-special-keyword t))
  3892. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3893. ;; Properties
  3894. (list org-property-re
  3895. '(1 'org-special-keyword t)
  3896. '(3 'org-property-value t))
  3897. (if org-format-transports-properties-p
  3898. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3899. ;; Links
  3900. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3901. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3902. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3903. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3904. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3905. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3906. (if (memq 'footnote lk) '(org-activate-footnote-links
  3907. (2 'org-footnote t)))
  3908. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3909. '(org-hide-wide-columns (0 nil append))
  3910. ;; TODO lines
  3911. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3912. '(1 (org-get-todo-face 1) t))
  3913. ;; DONE
  3914. (if org-fontify-done-headline
  3915. (list (concat "^[*]+ +\\<\\("
  3916. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3917. "\\)\\(.*\\)")
  3918. '(2 'org-headline-done t))
  3919. nil)
  3920. ;; Priorities
  3921. '(org-font-lock-add-priority-faces)
  3922. ;; Tags
  3923. '(org-font-lock-add-tag-faces)
  3924. ;; Special keywords
  3925. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3926. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3927. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3928. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3929. ;; Emphasis
  3930. (if em
  3931. (if (featurep 'xemacs)
  3932. '(org-do-emphasis-faces (0 nil append))
  3933. '(org-do-emphasis-faces)))
  3934. ;; Checkboxes
  3935. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3936. 2 'org-checkbox prepend)
  3937. (if org-provide-checkbox-statistics
  3938. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3939. (0 (org-get-checkbox-statistics-face) t)))
  3940. ;; Description list items
  3941. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3942. 2 'bold prepend)
  3943. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3944. '(1 'org-archived prepend))
  3945. ;; Specials
  3946. '(org-do-latex-and-special-faces)
  3947. ;; Code
  3948. '(org-activate-code (1 'org-code t))
  3949. ;; COMMENT
  3950. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3951. "\\|" org-quote-string "\\)\\>")
  3952. '(1 'org-special-keyword t))
  3953. '("^#.*" (0 'font-lock-comment-face t))
  3954. )))
  3955. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3956. ;; Now set the full font-lock-keywords
  3957. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3958. (org-set-local 'font-lock-defaults
  3959. '(org-font-lock-keywords t nil nil backward-paragraph))
  3960. (kill-local-variable 'font-lock-keywords) nil))
  3961. (defvar org-m nil)
  3962. (defvar org-l nil)
  3963. (defvar org-f nil)
  3964. (defun org-get-level-face (n)
  3965. "Get the right face for match N in font-lock matching of headlines."
  3966. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3967. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3968. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3969. (cond
  3970. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3971. ((eq n 2) org-f)
  3972. (t (if org-level-color-stars-only nil org-f))))
  3973. (defun org-get-todo-face (kwd)
  3974. "Get the right face for a TODO keyword KWD.
  3975. If KWD is a number, get the corresponding match group."
  3976. (if (numberp kwd) (setq kwd (match-string kwd)))
  3977. (or (cdr (assoc kwd org-todo-keyword-faces))
  3978. (and (member kwd org-done-keywords) 'org-done)
  3979. 'org-todo))
  3980. (defun org-font-lock-add-tag-faces (limit)
  3981. "Add the special tag faces."
  3982. (when (and org-tag-faces org-tags-special-faces-re)
  3983. (while (re-search-forward org-tags-special-faces-re limit t)
  3984. (add-text-properties (match-beginning 1) (match-end 1)
  3985. (list 'face (org-get-tag-face 1)
  3986. 'font-lock-fontified t))
  3987. (backward-char 1))))
  3988. (defun org-font-lock-add-priority-faces (limit)
  3989. "Add the special priority faces."
  3990. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  3991. (add-text-properties
  3992. (match-beginning 0) (match-end 0)
  3993. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  3994. org-priority-faces))
  3995. 'org-special-keyword)
  3996. 'font-lock-fontified t))))
  3997. (defun org-get-tag-face (kwd)
  3998. "Get the right face for a TODO keyword KWD.
  3999. If KWD is a number, get the corresponding match group."
  4000. (if (numberp kwd) (setq kwd (match-string kwd)))
  4001. (or (cdr (assoc kwd org-tag-faces))
  4002. 'org-tag))
  4003. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4004. "Remove fontification and activation overlays from links."
  4005. (font-lock-default-unfontify-region beg end)
  4006. (let* ((buffer-undo-list t)
  4007. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4008. (inhibit-modification-hooks t)
  4009. deactivate-mark buffer-file-name buffer-file-truename)
  4010. (remove-text-properties beg end
  4011. '(mouse-face t keymap t org-linked-text t
  4012. invisible t intangible t))))
  4013. ;;;; Visibility cycling, including org-goto and indirect buffer
  4014. ;;; Cycling
  4015. (defvar org-cycle-global-status nil)
  4016. (make-variable-buffer-local 'org-cycle-global-status)
  4017. (defvar org-cycle-subtree-status nil)
  4018. (make-variable-buffer-local 'org-cycle-subtree-status)
  4019. ;;;###autoload
  4020. (defvar org-inlinetask-min-level)
  4021. (defun org-cycle (&optional arg)
  4022. "Visibility cycling for Org-mode.
  4023. - When this function is called with a prefix argument, rotate the entire
  4024. buffer through 3 states (global cycling)
  4025. 1. OVERVIEW: Show only top-level headlines.
  4026. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4027. 3. SHOW ALL: Show everything.
  4028. When called with two C-u C-u prefixes, switch to the startup visibility,
  4029. determined by the variable `org-startup-folded', and by any VISIBILITY
  4030. properties in the buffer.
  4031. When called with three C-u C-u C-u prefixed, show the entire buffer,
  4032. including drawers.
  4033. - When point is at the beginning of a headline, rotate the subtree started
  4034. by this line through 3 different states (local cycling)
  4035. 1. FOLDED: Only the main headline is shown.
  4036. 2. CHILDREN: The main headline and the direct children are shown.
  4037. From this state, you can move to one of the children
  4038. and zoom in further.
  4039. 3. SUBTREE: Show the entire subtree, including body text.
  4040. - When there is a numeric prefix, go up to a heading with level ARG, do
  4041. a `show-subtree' and return to the previous cursor position. If ARG
  4042. is negative, go up that many levels.
  4043. - When point is not at the beginning of a headline, execute the global
  4044. binding for TAB, which is re-indenting the line. See the option
  4045. `org-cycle-emulate-tab' for details.
  4046. - Special case: if point is at the beginning of the buffer and there is
  4047. no headline in line 1, this function will act as if called with prefix arg.
  4048. But only if also the variable `org-cycle-global-at-bob' is t."
  4049. (interactive "P")
  4050. (org-load-modules-maybe)
  4051. (let* ((limit-level
  4052. (or org-cycle-max-level
  4053. (and (boundp 'org-inlinetask-min-level)
  4054. org-inlinetask-min-level
  4055. (1- org-inlinetask-min-level))))
  4056. (nstars (and limit-level
  4057. (if org-odd-levels-only
  4058. (and limit-level (1- (* limit-level 2)))
  4059. limit-level)))
  4060. (outline-regexp
  4061. (cond
  4062. ((not (org-mode-p)) outline-regexp)
  4063. (org-cycle-include-plain-lists
  4064. (concat "\\(?:\\*"
  4065. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4066. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4067. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4068. (bob-special (and org-cycle-global-at-bob (bobp)
  4069. (not (looking-at outline-regexp))))
  4070. (org-cycle-hook
  4071. (if bob-special
  4072. (delq 'org-optimize-window-after-visibility-change
  4073. (copy-sequence org-cycle-hook))
  4074. org-cycle-hook))
  4075. (pos (point)))
  4076. (if (or bob-special (equal arg '(4)))
  4077. ;; special case: use global cycling
  4078. (setq arg t))
  4079. (cond
  4080. ((equal arg '(16))
  4081. (org-set-startup-visibility)
  4082. (message "Startup visibility, plus VISIBILITY properties"))
  4083. ((equal arg '(64))
  4084. (show-all)
  4085. (message "Entire buffer visible, including drawers"))
  4086. ((org-at-table-p 'any)
  4087. ;; Enter the table or move to the next field in the table
  4088. (or (org-table-recognize-table.el)
  4089. (progn
  4090. (if arg (org-table-edit-field t)
  4091. (org-table-justify-field-maybe)
  4092. (call-interactively 'org-table-next-field)))))
  4093. ((eq arg t) ;; Global cycling
  4094. (cond
  4095. ((and (eq last-command this-command)
  4096. (eq org-cycle-global-status 'overview))
  4097. ;; We just created the overview - now do table of contents
  4098. ;; This can be slow in very large buffers, so indicate action
  4099. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4100. (message "CONTENTS...")
  4101. (org-content)
  4102. (message "CONTENTS...done")
  4103. (setq org-cycle-global-status 'contents)
  4104. (run-hook-with-args 'org-cycle-hook 'contents))
  4105. ((and (eq last-command this-command)
  4106. (eq org-cycle-global-status 'contents))
  4107. ;; We just showed the table of contents - now show everything
  4108. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4109. (show-all)
  4110. (message "SHOW ALL")
  4111. (setq org-cycle-global-status 'all)
  4112. (run-hook-with-args 'org-cycle-hook 'all))
  4113. (t
  4114. ;; Default action: go to overview
  4115. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4116. (org-overview)
  4117. (message "OVERVIEW")
  4118. (setq org-cycle-global-status 'overview)
  4119. (run-hook-with-args 'org-cycle-hook 'overview))))
  4120. ((and org-drawers org-drawer-regexp
  4121. (save-excursion
  4122. (beginning-of-line 1)
  4123. (looking-at org-drawer-regexp)))
  4124. ;; Toggle block visibility
  4125. (org-flag-drawer
  4126. (not (get-char-property (match-end 0) 'invisible))))
  4127. ((integerp arg)
  4128. ;; Show-subtree, ARG levels up from here.
  4129. (save-excursion
  4130. (org-back-to-heading)
  4131. (outline-up-heading (if (< arg 0) (- arg)
  4132. (- (funcall outline-level) arg)))
  4133. (org-show-subtree)))
  4134. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4135. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4136. ;; At a heading: rotate between three different views
  4137. (org-back-to-heading)
  4138. (let ((goal-column 0) eoh eol eos)
  4139. ;; First, some boundaries
  4140. (save-excursion
  4141. (org-back-to-heading)
  4142. (save-excursion
  4143. (beginning-of-line 2)
  4144. (while (and (not (eobp)) ;; this is like `next-line'
  4145. (get-char-property (1- (point)) 'invisible))
  4146. (beginning-of-line 2)) (setq eol (point)))
  4147. (outline-end-of-heading) (setq eoh (point))
  4148. (org-end-of-subtree t)
  4149. (unless (eobp)
  4150. (skip-chars-forward " \t\n")
  4151. (beginning-of-line 1) ; in case this is an item
  4152. )
  4153. (setq eos (1- (point))))
  4154. ;; Find out what to do next and set `this-command'
  4155. (cond
  4156. ((= eos eoh)
  4157. ;; Nothing is hidden behind this heading
  4158. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4159. (message "EMPTY ENTRY")
  4160. (setq org-cycle-subtree-status nil)
  4161. (save-excursion
  4162. (goto-char eos)
  4163. (outline-next-heading)
  4164. (if (org-invisible-p) (org-flag-heading nil))))
  4165. ((or (>= eol eos)
  4166. (not (string-match "\\S-" (buffer-substring eol eos))))
  4167. ;; Entire subtree is hidden in one line: open it
  4168. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4169. (org-show-entry)
  4170. (show-children)
  4171. (message "CHILDREN")
  4172. (save-excursion
  4173. (goto-char eos)
  4174. (outline-next-heading)
  4175. (if (org-invisible-p) (org-flag-heading nil)))
  4176. (setq org-cycle-subtree-status 'children)
  4177. (run-hook-with-args 'org-cycle-hook 'children))
  4178. ((and (eq last-command this-command)
  4179. (eq org-cycle-subtree-status 'children))
  4180. ;; We just showed the children, now show everything.
  4181. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4182. (org-show-subtree)
  4183. (message "SUBTREE")
  4184. (setq org-cycle-subtree-status 'subtree)
  4185. (run-hook-with-args 'org-cycle-hook 'subtree))
  4186. (t
  4187. ;; Default action: hide the subtree.
  4188. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4189. (hide-subtree)
  4190. (message "FOLDED")
  4191. (setq org-cycle-subtree-status 'folded)
  4192. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4193. ;; TAB emulation and template completion
  4194. (buffer-read-only (org-back-to-heading))
  4195. ((org-try-structure-completion))
  4196. ((org-try-cdlatex-tab))
  4197. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4198. (or (not (bolp))
  4199. (not (looking-at outline-regexp))))
  4200. (call-interactively (global-key-binding "\t")))
  4201. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4202. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4203. (or (and (eq org-cycle-emulate-tab 'white)
  4204. (= (match-end 0) (point-at-eol)))
  4205. (and (eq org-cycle-emulate-tab 'whitestart)
  4206. (>= (match-end 0) pos))))
  4207. t
  4208. (eq org-cycle-emulate-tab t))
  4209. (call-interactively (global-key-binding "\t")))
  4210. (t (save-excursion
  4211. (org-back-to-heading)
  4212. (org-cycle))))))
  4213. ;;;###autoload
  4214. (defun org-global-cycle (&optional arg)
  4215. "Cycle the global visibility. For details see `org-cycle'.
  4216. With C-u prefix arg, switch to startup visibility.
  4217. With a numeric prefix, show all headlines up to that level."
  4218. (interactive "P")
  4219. (let ((org-cycle-include-plain-lists
  4220. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4221. (cond
  4222. ((integerp arg)
  4223. (show-all)
  4224. (hide-sublevels arg)
  4225. (setq org-cycle-global-status 'contents))
  4226. ((equal arg '(4))
  4227. (org-set-startup-visibility)
  4228. (message "Startup visibility, plus VISIBILITY properties."))
  4229. (t
  4230. (org-cycle '(4))))))
  4231. (defun org-set-startup-visibility ()
  4232. "Set the visibility required by startup options and properties."
  4233. (cond
  4234. ((eq org-startup-folded t)
  4235. (org-cycle '(4)))
  4236. ((eq org-startup-folded 'content)
  4237. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4238. (org-cycle '(4)) (org-cycle '(4)))))
  4239. (org-set-visibility-according-to-property 'no-cleanup)
  4240. (org-cycle-hide-archived-subtrees 'all)
  4241. (org-cycle-hide-drawers 'all)
  4242. (org-cycle-show-empty-lines 'all))
  4243. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4244. "Switch subtree visibilities according to :VISIBILITY: property."
  4245. (interactive)
  4246. (let (org-show-entry-below state)
  4247. (save-excursion
  4248. (goto-char (point-min))
  4249. (while (re-search-forward
  4250. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4251. nil t)
  4252. (setq state (match-string 1))
  4253. (save-excursion
  4254. (org-back-to-heading t)
  4255. (hide-subtree)
  4256. (org-reveal)
  4257. (cond
  4258. ((equal state '("fold" "folded"))
  4259. (hide-subtree))
  4260. ((equal state "children")
  4261. (org-show-hidden-entry)
  4262. (show-children))
  4263. ((equal state "content")
  4264. (save-excursion
  4265. (save-restriction
  4266. (org-narrow-to-subtree)
  4267. (org-content))))
  4268. ((member state '("all" "showall"))
  4269. (show-subtree)))))
  4270. (unless no-cleanup
  4271. (org-cycle-hide-archived-subtrees 'all)
  4272. (org-cycle-hide-drawers 'all)
  4273. (org-cycle-show-empty-lines 'all)))))
  4274. (defun org-overview ()
  4275. "Switch to overview mode, showing only top-level headlines.
  4276. Really, this shows all headlines with level equal or greater than the level
  4277. of the first headline in the buffer. This is important, because if the
  4278. first headline is not level one, then (hide-sublevels 1) gives confusing
  4279. results."
  4280. (interactive)
  4281. (let ((level (save-excursion
  4282. (goto-char (point-min))
  4283. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4284. (progn
  4285. (goto-char (match-beginning 0))
  4286. (funcall outline-level))))))
  4287. (and level (hide-sublevels level))))
  4288. (defun org-content (&optional arg)
  4289. "Show all headlines in the buffer, like a table of contents.
  4290. With numerical argument N, show content up to level N."
  4291. (interactive "P")
  4292. (save-excursion
  4293. ;; Visit all headings and show their offspring
  4294. (and (integerp arg) (org-overview))
  4295. (goto-char (point-max))
  4296. (catch 'exit
  4297. (while (and (progn (condition-case nil
  4298. (outline-previous-visible-heading 1)
  4299. (error (goto-char (point-min))))
  4300. t)
  4301. (looking-at outline-regexp))
  4302. (if (integerp arg)
  4303. (show-children (1- arg))
  4304. (show-branches))
  4305. (if (bobp) (throw 'exit nil))))))
  4306. (defun org-optimize-window-after-visibility-change (state)
  4307. "Adjust the window after a change in outline visibility.
  4308. This function is the default value of the hook `org-cycle-hook'."
  4309. (when (get-buffer-window (current-buffer))
  4310. (cond
  4311. ((eq state 'content) nil)
  4312. ((eq state 'all) nil)
  4313. ((eq state 'folded) nil)
  4314. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4315. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4316. (defun org-compact-display-after-subtree-move ()
  4317. "Show a compacter version of the tree of the entry's parent."
  4318. (save-excursion
  4319. (if (org-up-heading-safe)
  4320. (progn
  4321. (hide-subtree)
  4322. (show-entry)
  4323. (show-children)
  4324. (org-cycle-show-empty-lines 'children)
  4325. (org-cycle-hide-drawers 'children))
  4326. (org-overview))))
  4327. (defun org-cycle-show-empty-lines (state)
  4328. "Show empty lines above all visible headlines.
  4329. The region to be covered depends on STATE when called through
  4330. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4331. entire buffer covered. Note that an empty line is only shown if there
  4332. are at least `org-cycle-separator-lines' empty lines before the headline."
  4333. (when (> org-cycle-separator-lines 0)
  4334. (save-excursion
  4335. (let* ((n org-cycle-separator-lines)
  4336. (re (cond
  4337. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4338. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4339. (t (let ((ns (number-to-string (- n 2))))
  4340. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4341. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4342. beg end)
  4343. (cond
  4344. ((memq state '(overview contents t))
  4345. (setq beg (point-min) end (point-max)))
  4346. ((memq state '(children folded))
  4347. (setq beg (point) end (progn (org-end-of-subtree t t)
  4348. (beginning-of-line 2)
  4349. (point)))))
  4350. (when beg
  4351. (goto-char beg)
  4352. (while (re-search-forward re end t)
  4353. (if (not (get-char-property (match-end 1) 'invisible))
  4354. (outline-flag-region
  4355. (match-beginning 1) (match-end 1) nil)))))))
  4356. ;; Never hide empty lines at the end of the file.
  4357. (save-excursion
  4358. (goto-char (point-max))
  4359. (outline-previous-heading)
  4360. (outline-end-of-heading)
  4361. (if (and (looking-at "[ \t\n]+")
  4362. (= (match-end 0) (point-max)))
  4363. (outline-flag-region (point) (match-end 0) nil))))
  4364. (defun org-show-empty-lines-in-parent ()
  4365. "Move to the parent and re-show empty lines before visible headlines."
  4366. (save-excursion
  4367. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4368. (org-cycle-show-empty-lines context))))
  4369. (defun org-cycle-hide-drawers (state)
  4370. "Re-hide all drawers after a visibility state change."
  4371. (when (and (org-mode-p)
  4372. (not (memq state '(overview folded))))
  4373. (save-excursion
  4374. (let* ((globalp (memq state '(contents all)))
  4375. (beg (if globalp (point-min) (point)))
  4376. (end (if globalp (point-max) (org-end-of-subtree t))))
  4377. (goto-char beg)
  4378. (while (re-search-forward org-drawer-regexp end t)
  4379. (org-flag-drawer t))))))
  4380. (defun org-flag-drawer (flag)
  4381. (save-excursion
  4382. (beginning-of-line 1)
  4383. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4384. (let ((b (match-end 0))
  4385. (outline-regexp org-outline-regexp))
  4386. (if (re-search-forward
  4387. "^[ \t]*:END:"
  4388. (save-excursion (outline-next-heading) (point)) t)
  4389. (outline-flag-region b (point-at-eol) flag)
  4390. (error ":END: line missing"))))))
  4391. (defun org-subtree-end-visible-p ()
  4392. "Is the end of the current subtree visible?"
  4393. (pos-visible-in-window-p
  4394. (save-excursion (org-end-of-subtree t) (point))))
  4395. (defun org-first-headline-recenter (&optional N)
  4396. "Move cursor to the first headline and recenter the headline.
  4397. Optional argument N means, put the headline into the Nth line of the window."
  4398. (goto-char (point-min))
  4399. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4400. (beginning-of-line)
  4401. (recenter (prefix-numeric-value N))))
  4402. ;;; Org-goto
  4403. (defvar org-goto-window-configuration nil)
  4404. (defvar org-goto-marker nil)
  4405. (defvar org-goto-map
  4406. (let ((map (make-sparse-keymap)))
  4407. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4408. (while (setq cmd (pop cmds))
  4409. (substitute-key-definition cmd cmd map global-map)))
  4410. (suppress-keymap map)
  4411. (org-defkey map "\C-m" 'org-goto-ret)
  4412. (org-defkey map [(return)] 'org-goto-ret)
  4413. (org-defkey map [(left)] 'org-goto-left)
  4414. (org-defkey map [(right)] 'org-goto-right)
  4415. (org-defkey map [(control ?g)] 'org-goto-quit)
  4416. (org-defkey map "\C-i" 'org-cycle)
  4417. (org-defkey map [(tab)] 'org-cycle)
  4418. (org-defkey map [(down)] 'outline-next-visible-heading)
  4419. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4420. (if org-goto-auto-isearch
  4421. (if (fboundp 'define-key-after)
  4422. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4423. nil)
  4424. (org-defkey map "q" 'org-goto-quit)
  4425. (org-defkey map "n" 'outline-next-visible-heading)
  4426. (org-defkey map "p" 'outline-previous-visible-heading)
  4427. (org-defkey map "f" 'outline-forward-same-level)
  4428. (org-defkey map "b" 'outline-backward-same-level)
  4429. (org-defkey map "u" 'outline-up-heading))
  4430. (org-defkey map "/" 'org-occur)
  4431. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4432. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4433. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4434. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4435. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4436. map))
  4437. (defconst org-goto-help
  4438. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4439. RET=jump to location [Q]uit and return to previous location
  4440. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4441. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4442. ;; FIXME: Docstring doe not mention both interfaces
  4443. (defun org-goto (&optional alternative-interface)
  4444. "Look up a different location in the current file, keeping current visibility.
  4445. When you want look-up or go to a different location in a document, the
  4446. fastest way is often to fold the entire buffer and then dive into the tree.
  4447. This method has the disadvantage, that the previous location will be folded,
  4448. which may not be what you want.
  4449. This command works around this by showing a copy of the current buffer
  4450. in an indirect buffer, in overview mode. You can dive into the tree in
  4451. that copy, use org-occur and incremental search to find a location.
  4452. When pressing RET or `Q', the command returns to the original buffer in
  4453. which the visibility is still unchanged. After RET is will also jump to
  4454. the location selected in the indirect buffer and expose the
  4455. the headline hierarchy above."
  4456. (interactive "P")
  4457. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4458. (org-refile-use-outline-path t)
  4459. (interface
  4460. (if (not alternative-interface)
  4461. org-goto-interface
  4462. (if (eq org-goto-interface 'outline)
  4463. 'outline-path-completion
  4464. 'outline)))
  4465. (org-goto-start-pos (point))
  4466. (selected-point
  4467. (if (eq interface 'outline)
  4468. (car (org-get-location (current-buffer) org-goto-help))
  4469. (nth 3 (org-refile-get-location "Goto: ")))))
  4470. (if selected-point
  4471. (progn
  4472. (org-mark-ring-push org-goto-start-pos)
  4473. (goto-char selected-point)
  4474. (if (or (org-invisible-p) (org-invisible-p2))
  4475. (org-show-context 'org-goto)))
  4476. (message "Quit"))))
  4477. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4478. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4479. (defvar org-goto-local-auto-isearch-map) ; defined below
  4480. (defun org-get-location (buf help)
  4481. "Let the user select a location in the Org-mode buffer BUF.
  4482. This function uses a recursive edit. It returns the selected position
  4483. or nil."
  4484. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4485. (isearch-hide-immediately nil)
  4486. (isearch-search-fun-function
  4487. (lambda () 'org-goto-local-search-headings))
  4488. (org-goto-selected-point org-goto-exit-command))
  4489. (save-excursion
  4490. (save-window-excursion
  4491. (delete-other-windows)
  4492. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4493. (switch-to-buffer
  4494. (condition-case nil
  4495. (make-indirect-buffer (current-buffer) "*org-goto*")
  4496. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4497. (with-output-to-temp-buffer "*Help*"
  4498. (princ help))
  4499. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4500. (setq buffer-read-only nil)
  4501. (let ((org-startup-truncated t)
  4502. (org-startup-folded nil)
  4503. (org-startup-align-all-tables nil))
  4504. (org-mode)
  4505. (org-overview))
  4506. (setq buffer-read-only t)
  4507. (if (and (boundp 'org-goto-start-pos)
  4508. (integer-or-marker-p org-goto-start-pos))
  4509. (let ((org-show-hierarchy-above t)
  4510. (org-show-siblings t)
  4511. (org-show-following-heading t))
  4512. (goto-char org-goto-start-pos)
  4513. (and (org-invisible-p) (org-show-context)))
  4514. (goto-char (point-min)))
  4515. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4516. (message "Select location and press RET")
  4517. (use-local-map org-goto-map)
  4518. (recursive-edit)
  4519. ))
  4520. (kill-buffer "*org-goto*")
  4521. (cons org-goto-selected-point org-goto-exit-command)))
  4522. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4523. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4524. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4525. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4526. (defun org-goto-local-search-headings (string bound noerror)
  4527. "Search and make sure that any matches are in headlines."
  4528. (catch 'return
  4529. (while (if isearch-forward
  4530. (search-forward string bound noerror)
  4531. (search-backward string bound noerror))
  4532. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4533. (and (member :headline context)
  4534. (not (member :tags context))))
  4535. (throw 'return (point))))))
  4536. (defun org-goto-local-auto-isearch ()
  4537. "Start isearch."
  4538. (interactive)
  4539. (goto-char (point-min))
  4540. (let ((keys (this-command-keys)))
  4541. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4542. (isearch-mode t)
  4543. (isearch-process-search-char (string-to-char keys)))))
  4544. (defun org-goto-ret (&optional arg)
  4545. "Finish `org-goto' by going to the new location."
  4546. (interactive "P")
  4547. (setq org-goto-selected-point (point)
  4548. org-goto-exit-command 'return)
  4549. (throw 'exit nil))
  4550. (defun org-goto-left ()
  4551. "Finish `org-goto' by going to the new location."
  4552. (interactive)
  4553. (if (org-on-heading-p)
  4554. (progn
  4555. (beginning-of-line 1)
  4556. (setq org-goto-selected-point (point)
  4557. org-goto-exit-command 'left)
  4558. (throw 'exit nil))
  4559. (error "Not on a heading")))
  4560. (defun org-goto-right ()
  4561. "Finish `org-goto' by going to the new location."
  4562. (interactive)
  4563. (if (org-on-heading-p)
  4564. (progn
  4565. (setq org-goto-selected-point (point)
  4566. org-goto-exit-command 'right)
  4567. (throw 'exit nil))
  4568. (error "Not on a heading")))
  4569. (defun org-goto-quit ()
  4570. "Finish `org-goto' without cursor motion."
  4571. (interactive)
  4572. (setq org-goto-selected-point nil)
  4573. (setq org-goto-exit-command 'quit)
  4574. (throw 'exit nil))
  4575. ;;; Indirect buffer display of subtrees
  4576. (defvar org-indirect-dedicated-frame nil
  4577. "This is the frame being used for indirect tree display.")
  4578. (defvar org-last-indirect-buffer nil)
  4579. (defun org-tree-to-indirect-buffer (&optional arg)
  4580. "Create indirect buffer and narrow it to current subtree.
  4581. With numerical prefix ARG, go up to this level and then take that tree.
  4582. If ARG is negative, go up that many levels.
  4583. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4584. indirect buffer previously made with this command, to avoid proliferation of
  4585. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4586. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4587. is kept so that you can work with several indirect buffers at the same time.
  4588. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4589. requests that a new frame be made for the new buffer, so that the dedicated
  4590. frame is not changed."
  4591. (interactive "P")
  4592. (let ((cbuf (current-buffer))
  4593. (cwin (selected-window))
  4594. (pos (point))
  4595. beg end level heading ibuf)
  4596. (save-excursion
  4597. (org-back-to-heading t)
  4598. (when (numberp arg)
  4599. (setq level (org-outline-level))
  4600. (if (< arg 0) (setq arg (+ level arg)))
  4601. (while (> (setq level (org-outline-level)) arg)
  4602. (outline-up-heading 1 t)))
  4603. (setq beg (point)
  4604. heading (org-get-heading))
  4605. (org-end-of-subtree t) (setq end (point)))
  4606. (if (and (buffer-live-p org-last-indirect-buffer)
  4607. (not (eq org-indirect-buffer-display 'new-frame))
  4608. (not arg))
  4609. (kill-buffer org-last-indirect-buffer))
  4610. (setq ibuf (org-get-indirect-buffer cbuf)
  4611. org-last-indirect-buffer ibuf)
  4612. (cond
  4613. ((or (eq org-indirect-buffer-display 'new-frame)
  4614. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4615. (select-frame (make-frame))
  4616. (delete-other-windows)
  4617. (switch-to-buffer ibuf)
  4618. (org-set-frame-title heading))
  4619. ((eq org-indirect-buffer-display 'dedicated-frame)
  4620. (raise-frame
  4621. (select-frame (or (and org-indirect-dedicated-frame
  4622. (frame-live-p org-indirect-dedicated-frame)
  4623. org-indirect-dedicated-frame)
  4624. (setq org-indirect-dedicated-frame (make-frame)))))
  4625. (delete-other-windows)
  4626. (switch-to-buffer ibuf)
  4627. (org-set-frame-title (concat "Indirect: " heading)))
  4628. ((eq org-indirect-buffer-display 'current-window)
  4629. (switch-to-buffer ibuf))
  4630. ((eq org-indirect-buffer-display 'other-window)
  4631. (pop-to-buffer ibuf))
  4632. (t (error "Invalid value.")))
  4633. (if (featurep 'xemacs)
  4634. (save-excursion (org-mode) (turn-on-font-lock)))
  4635. (narrow-to-region beg end)
  4636. (show-all)
  4637. (goto-char pos)
  4638. (and (window-live-p cwin) (select-window cwin))))
  4639. (defun org-get-indirect-buffer (&optional buffer)
  4640. (setq buffer (or buffer (current-buffer)))
  4641. (let ((n 1) (base (buffer-name buffer)) bname)
  4642. (while (buffer-live-p
  4643. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4644. (setq n (1+ n)))
  4645. (condition-case nil
  4646. (make-indirect-buffer buffer bname 'clone)
  4647. (error (make-indirect-buffer buffer bname)))))
  4648. (defun org-set-frame-title (title)
  4649. "Set the title of the current frame to the string TITLE."
  4650. ;; FIXME: how to name a single frame in XEmacs???
  4651. (unless (featurep 'xemacs)
  4652. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4653. ;;;; Structure editing
  4654. ;;; Inserting headlines
  4655. (defun org-previous-line-empty-p ()
  4656. (save-excursion
  4657. (and (not (bobp))
  4658. (or (beginning-of-line 0) t)
  4659. (save-match-data
  4660. (looking-at "[ \t]*$")))))
  4661. (defun org-insert-heading (&optional force-heading)
  4662. "Insert a new heading or item with same depth at point.
  4663. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4664. If point is at the beginning of a headline, insert a sibling before the
  4665. current headline. If point is not at the beginning, do not split the line,
  4666. but create the new headline after the current line."
  4667. (interactive "P")
  4668. (if (= (buffer-size) 0)
  4669. (insert "\n* ")
  4670. (when (or force-heading (not (org-insert-item)))
  4671. (let* ((empty-line-p nil)
  4672. (head (save-excursion
  4673. (condition-case nil
  4674. (progn
  4675. (org-back-to-heading)
  4676. (setq empty-line-p (org-previous-line-empty-p))
  4677. (match-string 0))
  4678. (error "*"))))
  4679. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4680. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4681. pos hide-previous previous-pos)
  4682. (cond
  4683. ((and (org-on-heading-p) (bolp)
  4684. (or (bobp)
  4685. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4686. ;; insert before the current line
  4687. (open-line (if blank 2 1)))
  4688. ((and (bolp)
  4689. (or (bobp)
  4690. (save-excursion
  4691. (backward-char 1) (not (org-invisible-p)))))
  4692. ;; insert right here
  4693. nil)
  4694. (t
  4695. ;; somewhere in the line
  4696. (save-excursion
  4697. (setq previous-pos (point-at-bol))
  4698. (end-of-line)
  4699. (setq hide-previous (org-invisible-p)))
  4700. (and org-insert-heading-respect-content (org-show-subtree))
  4701. (let ((split
  4702. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4703. (save-excursion
  4704. (let ((p (point)))
  4705. (goto-char (point-at-bol))
  4706. (and (looking-at org-complex-heading-regexp)
  4707. (> p (match-beginning 4)))))))
  4708. tags pos)
  4709. (cond
  4710. (org-insert-heading-respect-content
  4711. (org-end-of-subtree nil t)
  4712. (or (bolp) (newline))
  4713. (or (org-previous-line-empty-p)
  4714. (and blank (newline)))
  4715. (open-line 1))
  4716. ((org-on-heading-p)
  4717. (when hide-previous
  4718. (show-children)
  4719. (org-show-entry))
  4720. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4721. (setq tags (and (match-end 2) (match-string 2)))
  4722. (and (match-end 1)
  4723. (delete-region (match-beginning 1) (match-end 1)))
  4724. (setq pos (point-at-bol))
  4725. (or split (end-of-line 1))
  4726. (delete-horizontal-space)
  4727. (newline (if blank 2 1))
  4728. (when tags
  4729. (save-excursion
  4730. (goto-char pos)
  4731. (end-of-line 1)
  4732. (insert " " tags)
  4733. (org-set-tags nil 'align))))
  4734. (t
  4735. (or split (end-of-line 1))
  4736. (newline (if blank 2 1)))))))
  4737. (insert head) (just-one-space)
  4738. (setq pos (point))
  4739. (end-of-line 1)
  4740. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4741. (when (and org-insert-heading-respect-content hide-previous)
  4742. (save-excursion
  4743. (goto-char previous-pos)
  4744. (hide-subtree)))
  4745. (run-hooks 'org-insert-heading-hook)))))
  4746. (defun org-get-heading (&optional no-tags)
  4747. "Return the heading of the current entry, without the stars."
  4748. (save-excursion
  4749. (org-back-to-heading t)
  4750. (if (looking-at
  4751. (if no-tags
  4752. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4753. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4754. (match-string 1) "")))
  4755. (defun org-heading-components ()
  4756. "Return the components of the current heading.
  4757. This is a list with the following elements:
  4758. - the level as an integer
  4759. - the reduced level, different if `org-odd-levels-only' is set.
  4760. - the TODO keyword, or nil
  4761. - the priority character, like ?A, or nil if no priority is given
  4762. - the headline text itself, or the tags string if no headline text
  4763. - the tags string, or nil."
  4764. (save-excursion
  4765. (org-back-to-heading t)
  4766. (if (looking-at org-complex-heading-regexp)
  4767. (list (length (match-string 1))
  4768. (org-reduced-level (length (match-string 1)))
  4769. (org-match-string-no-properties 2)
  4770. (and (match-end 3) (aref (match-string 3) 2))
  4771. (org-match-string-no-properties 4)
  4772. (org-match-string-no-properties 5)))))
  4773. (defun org-get-entry ()
  4774. "Get the entry text, after heading, entire subtree."
  4775. (save-excursion
  4776. (org-back-to-heading t)
  4777. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4778. (defun org-insert-heading-after-current ()
  4779. "Insert a new heading with same level as current, after current subtree."
  4780. (interactive)
  4781. (org-back-to-heading)
  4782. (org-insert-heading)
  4783. (org-move-subtree-down)
  4784. (end-of-line 1))
  4785. (defun org-insert-heading-respect-content ()
  4786. (interactive)
  4787. (let ((org-insert-heading-respect-content t))
  4788. (org-insert-heading t)))
  4789. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4790. (interactive "P")
  4791. (let ((org-insert-heading-respect-content t))
  4792. (org-insert-todo-heading force-state t)))
  4793. (defun org-insert-todo-heading (arg &optional force-heading)
  4794. "Insert a new heading with the same level and TODO state as current heading.
  4795. If the heading has no TODO state, or if the state is DONE, use the first
  4796. state (TODO by default). Also with prefix arg, force first state."
  4797. (interactive "P")
  4798. (when (or force-heading (not (org-insert-item 'checkbox)))
  4799. (org-insert-heading force-heading)
  4800. (save-excursion
  4801. (org-back-to-heading)
  4802. (outline-previous-heading)
  4803. (looking-at org-todo-line-regexp))
  4804. (let*
  4805. ((new-mark-x
  4806. (if (or arg
  4807. (not (match-beginning 2))
  4808. (member (match-string 2) org-done-keywords))
  4809. (car org-todo-keywords-1)
  4810. (match-string 2)))
  4811. (new-mark
  4812. (or
  4813. (run-hook-with-args-until-success
  4814. 'org-todo-get-default-hook new-mark-x nil)
  4815. new-mark-x)))
  4816. (beginning-of-line 1)
  4817. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4818. (insert new-mark " ")))
  4819. (when org-provide-todo-statistics
  4820. (org-update-parent-todo-statistics))))
  4821. (defun org-insert-subheading (arg)
  4822. "Insert a new subheading and demote it.
  4823. Works for outline headings and for plain lists alike."
  4824. (interactive "P")
  4825. (org-insert-heading arg)
  4826. (cond
  4827. ((org-on-heading-p) (org-do-demote))
  4828. ((org-at-item-p) (org-indent-item 1))))
  4829. (defun org-insert-todo-subheading (arg)
  4830. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4831. Works for outline headings and for plain lists alike."
  4832. (interactive "P")
  4833. (org-insert-todo-heading arg)
  4834. (cond
  4835. ((org-on-heading-p) (org-do-demote))
  4836. ((org-at-item-p) (org-indent-item 1))))
  4837. ;;; Promotion and Demotion
  4838. (defun org-promote-subtree ()
  4839. "Promote the entire subtree.
  4840. See also `org-promote'."
  4841. (interactive)
  4842. (save-excursion
  4843. (org-map-tree 'org-promote))
  4844. (org-fix-position-after-promote))
  4845. (defun org-demote-subtree ()
  4846. "Demote the entire subtree. See `org-demote'.
  4847. See also `org-promote'."
  4848. (interactive)
  4849. (save-excursion
  4850. (org-map-tree 'org-demote))
  4851. (org-fix-position-after-promote))
  4852. (defun org-do-promote ()
  4853. "Promote the current heading higher up the tree.
  4854. If the region is active in `transient-mark-mode', promote all headings
  4855. in the region."
  4856. (interactive)
  4857. (save-excursion
  4858. (if (org-region-active-p)
  4859. (org-map-region 'org-promote (region-beginning) (region-end))
  4860. (org-promote)))
  4861. (org-fix-position-after-promote))
  4862. (defun org-do-demote ()
  4863. "Demote the current heading lower down the tree.
  4864. If the region is active in `transient-mark-mode', demote all headings
  4865. in the region."
  4866. (interactive)
  4867. (save-excursion
  4868. (if (org-region-active-p)
  4869. (org-map-region 'org-demote (region-beginning) (region-end))
  4870. (org-demote)))
  4871. (org-fix-position-after-promote))
  4872. (defun org-fix-position-after-promote ()
  4873. "Make sure that after pro/demotion cursor position is right."
  4874. (let ((pos (point)))
  4875. (when (save-excursion
  4876. (beginning-of-line 1)
  4877. (looking-at org-todo-line-regexp)
  4878. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4879. (cond ((eobp) (insert " "))
  4880. ((eolp) (insert " "))
  4881. ((equal (char-after) ?\ ) (forward-char 1))))))
  4882. (defun org-reduced-level (l)
  4883. "Compute the effective level of a heading.
  4884. This takes into account the setting of `org-odd-levels-only'."
  4885. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4886. (defun org-get-valid-level (level &optional change)
  4887. "Rectify a level change under the influence of `org-odd-levels-only'
  4888. LEVEL is a current level, CHANGE is by how much the level should be
  4889. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4890. even level numbers will become the next higher odd number."
  4891. (if org-odd-levels-only
  4892. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4893. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4894. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4895. (max 1 (+ level (or change 0)))))
  4896. (if (boundp 'define-obsolete-function-alias)
  4897. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4898. (define-obsolete-function-alias 'org-get-legal-level
  4899. 'org-get-valid-level)
  4900. (define-obsolete-function-alias 'org-get-legal-level
  4901. 'org-get-valid-level "23.1")))
  4902. (defun org-promote ()
  4903. "Promote the current heading higher up the tree.
  4904. If the region is active in `transient-mark-mode', promote all headings
  4905. in the region."
  4906. (org-back-to-heading t)
  4907. (let* ((level (save-match-data (funcall outline-level)))
  4908. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4909. (diff (abs (- level (length up-head) -1))))
  4910. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4911. (replace-match up-head nil t)
  4912. ;; Fixup tag positioning
  4913. (and org-auto-align-tags (org-set-tags nil t))
  4914. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4915. (defun org-demote ()
  4916. "Demote the current heading lower down the tree.
  4917. If the region is active in `transient-mark-mode', demote all headings
  4918. in the region."
  4919. (org-back-to-heading t)
  4920. (let* ((level (save-match-data (funcall outline-level)))
  4921. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4922. (diff (abs (- level (length down-head) -1))))
  4923. (replace-match down-head nil t)
  4924. ;; Fixup tag positioning
  4925. (and org-auto-align-tags (org-set-tags nil t))
  4926. (if org-adapt-indentation (org-fixup-indentation diff))))
  4927. (defun org-map-tree (fun)
  4928. "Call FUN for every heading underneath the current one."
  4929. (org-back-to-heading)
  4930. (let ((level (funcall outline-level)))
  4931. (save-excursion
  4932. (funcall fun)
  4933. (while (and (progn
  4934. (outline-next-heading)
  4935. (> (funcall outline-level) level))
  4936. (not (eobp)))
  4937. (funcall fun)))))
  4938. (defun org-map-region (fun beg end)
  4939. "Call FUN for every heading between BEG and END."
  4940. (let ((org-ignore-region t))
  4941. (save-excursion
  4942. (setq end (copy-marker end))
  4943. (goto-char beg)
  4944. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4945. (< (point) end))
  4946. (funcall fun))
  4947. (while (and (progn
  4948. (outline-next-heading)
  4949. (< (point) end))
  4950. (not (eobp)))
  4951. (funcall fun)))))
  4952. (defun org-fixup-indentation (diff)
  4953. "Change the indentation in the current entry by DIFF
  4954. However, if any line in the current entry has no indentation, or if it
  4955. would end up with no indentation after the change, nothing at all is done."
  4956. (save-excursion
  4957. (let ((end (save-excursion (outline-next-heading)
  4958. (point-marker)))
  4959. (prohibit (if (> diff 0)
  4960. "^\\S-"
  4961. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4962. col)
  4963. (unless (save-excursion (end-of-line 1)
  4964. (re-search-forward prohibit end t))
  4965. (while (and (< (point) end)
  4966. (re-search-forward "^[ \t]+" end t))
  4967. (goto-char (match-end 0))
  4968. (setq col (current-column))
  4969. (if (< diff 0) (replace-match ""))
  4970. (org-indent-to-column (+ diff col))))
  4971. (move-marker end nil))))
  4972. (defun org-convert-to-odd-levels ()
  4973. "Convert an org-mode file with all levels allowed to one with odd levels.
  4974. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4975. level 5 etc."
  4976. (interactive)
  4977. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4978. (let ((org-odd-levels-only nil) n)
  4979. (save-excursion
  4980. (goto-char (point-min))
  4981. (while (re-search-forward "^\\*\\*+ " nil t)
  4982. (setq n (- (length (match-string 0)) 2))
  4983. (while (>= (setq n (1- n)) 0)
  4984. (org-demote))
  4985. (end-of-line 1))))))
  4986. (defun org-convert-to-oddeven-levels ()
  4987. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4988. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4989. section with an even level, conversion would destroy the structure of the file. An error
  4990. is signaled in this case."
  4991. (interactive)
  4992. (goto-char (point-min))
  4993. ;; First check if there are no even levels
  4994. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4995. (org-show-context t)
  4996. (error "Not all levels are odd in this file. Conversion not possible."))
  4997. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4998. (let ((org-odd-levels-only nil) n)
  4999. (save-excursion
  5000. (goto-char (point-min))
  5001. (while (re-search-forward "^\\*\\*+ " nil t)
  5002. (setq n (/ (1- (length (match-string 0))) 2))
  5003. (while (>= (setq n (1- n)) 0)
  5004. (org-promote))
  5005. (end-of-line 1))))))
  5006. (defun org-tr-level (n)
  5007. "Make N odd if required."
  5008. (if org-odd-levels-only (1+ (/ n 2)) n))
  5009. ;;; Vertical tree motion, cutting and pasting of subtrees
  5010. (defun org-move-subtree-up (&optional arg)
  5011. "Move the current subtree up past ARG headlines of the same level."
  5012. (interactive "p")
  5013. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5014. (defun org-move-subtree-down (&optional arg)
  5015. "Move the current subtree down past ARG headlines of the same level."
  5016. (interactive "p")
  5017. (setq arg (prefix-numeric-value arg))
  5018. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5019. 'outline-get-last-sibling))
  5020. (ins-point (make-marker))
  5021. (cnt (abs arg))
  5022. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5023. ;; Select the tree
  5024. (org-back-to-heading)
  5025. (setq beg0 (point))
  5026. (save-excursion
  5027. (setq ne-beg (org-back-over-empty-lines))
  5028. (setq beg (point)))
  5029. (save-match-data
  5030. (save-excursion (outline-end-of-heading)
  5031. (setq folded (org-invisible-p)))
  5032. (outline-end-of-subtree))
  5033. (outline-next-heading)
  5034. (setq ne-end (org-back-over-empty-lines))
  5035. (setq end (point))
  5036. (goto-char beg0)
  5037. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5038. ;; include less whitespace
  5039. (save-excursion
  5040. (goto-char beg)
  5041. (forward-line (- ne-beg ne-end))
  5042. (setq beg (point))))
  5043. ;; Find insertion point, with error handling
  5044. (while (> cnt 0)
  5045. (or (and (funcall movfunc) (looking-at outline-regexp))
  5046. (progn (goto-char beg0)
  5047. (error "Cannot move past superior level or buffer limit")))
  5048. (setq cnt (1- cnt)))
  5049. (if (> arg 0)
  5050. ;; Moving forward - still need to move over subtree
  5051. (progn (org-end-of-subtree t t)
  5052. (save-excursion
  5053. (org-back-over-empty-lines)
  5054. (or (bolp) (newline)))))
  5055. (setq ne-ins (org-back-over-empty-lines))
  5056. (move-marker ins-point (point))
  5057. (setq txt (buffer-substring beg end))
  5058. (org-save-markers-in-region beg end)
  5059. (delete-region beg end)
  5060. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5061. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5062. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5063. (let ((bbb (point)))
  5064. (insert-before-markers txt)
  5065. (org-reinstall-markers-in-region bbb)
  5066. (move-marker ins-point bbb))
  5067. (or (bolp) (insert "\n"))
  5068. (setq ins-end (point))
  5069. (goto-char ins-point)
  5070. (org-skip-whitespace)
  5071. (when (and (< arg 0)
  5072. (org-first-sibling-p)
  5073. (> ne-ins ne-beg))
  5074. ;; Move whitespace back to beginning
  5075. (save-excursion
  5076. (goto-char ins-end)
  5077. (let ((kill-whole-line t))
  5078. (kill-line (- ne-ins ne-beg)) (point)))
  5079. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5080. (move-marker ins-point nil)
  5081. (org-compact-display-after-subtree-move)
  5082. (org-show-empty-lines-in-parent)
  5083. (unless folded
  5084. (org-show-entry)
  5085. (show-children)
  5086. (org-cycle-hide-drawers 'children))))
  5087. (defvar org-subtree-clip ""
  5088. "Clipboard for cut and paste of subtrees.
  5089. This is actually only a copy of the kill, because we use the normal kill
  5090. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5091. (defvar org-subtree-clip-folded nil
  5092. "Was the last copied subtree folded?
  5093. This is used to fold the tree back after pasting.")
  5094. (defun org-cut-subtree (&optional n)
  5095. "Cut the current subtree into the clipboard.
  5096. With prefix arg N, cut this many sequential subtrees.
  5097. This is a short-hand for marking the subtree and then cutting it."
  5098. (interactive "p")
  5099. (org-copy-subtree n 'cut))
  5100. (defun org-copy-subtree (&optional n cut force-store-markers)
  5101. "Cut the current subtree into the clipboard.
  5102. With prefix arg N, cut this many sequential subtrees.
  5103. This is a short-hand for marking the subtree and then copying it.
  5104. If CUT is non-nil, actually cut the subtree.
  5105. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5106. of some markers in the region, even if CUT is non-nil. This is
  5107. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5108. (interactive "p")
  5109. (let (beg end folded (beg0 (point)))
  5110. (if (interactive-p)
  5111. (org-back-to-heading nil) ; take what looks like a subtree
  5112. (org-back-to-heading t)) ; take what is really there
  5113. (org-back-over-empty-lines)
  5114. (setq beg (point))
  5115. (skip-chars-forward " \t\r\n")
  5116. (save-match-data
  5117. (save-excursion (outline-end-of-heading)
  5118. (setq folded (org-invisible-p)))
  5119. (condition-case nil
  5120. (outline-forward-same-level (1- n))
  5121. (error nil))
  5122. (org-end-of-subtree t t))
  5123. (org-back-over-empty-lines)
  5124. (setq end (point))
  5125. (goto-char beg0)
  5126. (when (> end beg)
  5127. (setq org-subtree-clip-folded folded)
  5128. (when (or cut force-store-markers)
  5129. (org-save-markers-in-region beg end))
  5130. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5131. (setq org-subtree-clip (current-kill 0))
  5132. (message "%s: Subtree(s) with %d characters"
  5133. (if cut "Cut" "Copied")
  5134. (length org-subtree-clip)))))
  5135. (defun org-paste-subtree (&optional level tree for-yank)
  5136. "Paste the clipboard as a subtree, with modification of headline level.
  5137. The entire subtree is promoted or demoted in order to match a new headline
  5138. level.
  5139. If the cursor is at the beginning of a headline, the same level as
  5140. that headline is used to paste the tree
  5141. If not, the new level is derived from the *visible* headings
  5142. before and after the insertion point, and taken to be the inferior headline
  5143. level of the two. So if the previous visible heading is level 3 and the
  5144. next is level 4 (or vice versa), level 4 will be used for insertion.
  5145. This makes sure that the subtree remains an independent subtree and does
  5146. not swallow low level entries.
  5147. You can also force a different level, either by using a numeric prefix
  5148. argument, or by inserting the heading marker by hand. For example, if the
  5149. cursor is after \"*****\", then the tree will be shifted to level 5.
  5150. If optional TREE is given, use this text instead of the kill ring.
  5151. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5152. move back over whitespace before inserting, and move point to the end of
  5153. the inserted text when done."
  5154. (interactive "P")
  5155. (unless (org-kill-is-subtree-p tree)
  5156. (error "%s"
  5157. (substitute-command-keys
  5158. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5159. (let* ((visp (not (org-invisible-p)))
  5160. (txt (or tree (and kill-ring (current-kill 0))))
  5161. (^re (concat "^\\(" outline-regexp "\\)"))
  5162. (re (concat "\\(" outline-regexp "\\)"))
  5163. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5164. (old-level (if (string-match ^re txt)
  5165. (- (match-end 0) (match-beginning 0) 1)
  5166. -1))
  5167. (force-level (cond (level (prefix-numeric-value level))
  5168. ((and (looking-at "[ \t]*$")
  5169. (string-match
  5170. ^re_ (buffer-substring
  5171. (point-at-bol) (point))))
  5172. (- (match-end 1) (match-beginning 1)))
  5173. ((and (bolp)
  5174. (looking-at org-outline-regexp))
  5175. (- (match-end 0) (point) 1))
  5176. (t nil)))
  5177. (previous-level (save-excursion
  5178. (condition-case nil
  5179. (progn
  5180. (outline-previous-visible-heading 1)
  5181. (if (looking-at re)
  5182. (- (match-end 0) (match-beginning 0) 1)
  5183. 1))
  5184. (error 1))))
  5185. (next-level (save-excursion
  5186. (condition-case nil
  5187. (progn
  5188. (or (looking-at outline-regexp)
  5189. (outline-next-visible-heading 1))
  5190. (if (looking-at re)
  5191. (- (match-end 0) (match-beginning 0) 1)
  5192. 1))
  5193. (error 1))))
  5194. (new-level (or force-level (max previous-level next-level)))
  5195. (shift (if (or (= old-level -1)
  5196. (= new-level -1)
  5197. (= old-level new-level))
  5198. 0
  5199. (- new-level old-level)))
  5200. (delta (if (> shift 0) -1 1))
  5201. (func (if (> shift 0) 'org-demote 'org-promote))
  5202. (org-odd-levels-only nil)
  5203. beg end newend)
  5204. ;; Remove the forced level indicator
  5205. (if force-level
  5206. (delete-region (point-at-bol) (point)))
  5207. ;; Paste
  5208. (beginning-of-line 1)
  5209. (unless for-yank (org-back-over-empty-lines))
  5210. (setq beg (point))
  5211. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5212. (insert-before-markers txt)
  5213. (unless (string-match "\n\\'" txt) (insert "\n"))
  5214. (setq newend (point))
  5215. (org-reinstall-markers-in-region beg)
  5216. (setq end (point))
  5217. (goto-char beg)
  5218. (skip-chars-forward " \t\n\r")
  5219. (setq beg (point))
  5220. (if (and (org-invisible-p) visp)
  5221. (save-excursion (outline-show-heading)))
  5222. ;; Shift if necessary
  5223. (unless (= shift 0)
  5224. (save-restriction
  5225. (narrow-to-region beg end)
  5226. (while (not (= shift 0))
  5227. (org-map-region func (point-min) (point-max))
  5228. (setq shift (+ delta shift)))
  5229. (goto-char (point-min))
  5230. (setq newend (point-max))))
  5231. (when (or (interactive-p) for-yank)
  5232. (message "Clipboard pasted as level %d subtree" new-level))
  5233. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5234. kill-ring
  5235. (eq org-subtree-clip (current-kill 0))
  5236. org-subtree-clip-folded)
  5237. ;; The tree was folded before it was killed/copied
  5238. (hide-subtree))
  5239. (and for-yank (goto-char newend))))
  5240. (defun org-kill-is-subtree-p (&optional txt)
  5241. "Check if the current kill is an outline subtree, or a set of trees.
  5242. Returns nil if kill does not start with a headline, or if the first
  5243. headline level is not the largest headline level in the tree.
  5244. So this will actually accept several entries of equal levels as well,
  5245. which is OK for `org-paste-subtree'.
  5246. If optional TXT is given, check this string instead of the current kill."
  5247. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5248. (start-level (and kill
  5249. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5250. org-outline-regexp "\\)")
  5251. kill)
  5252. (- (match-end 2) (match-beginning 2) 1)))
  5253. (re (concat "^" org-outline-regexp))
  5254. (start (1+ (or (match-beginning 2) -1))))
  5255. (if (not start-level)
  5256. (progn
  5257. nil) ;; does not even start with a heading
  5258. (catch 'exit
  5259. (while (setq start (string-match re kill (1+ start)))
  5260. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5261. (throw 'exit nil)))
  5262. t))))
  5263. (defvar org-markers-to-move nil
  5264. "Markers that should be moved with a cut-and-paste operation.
  5265. Those markers are stored together with their positions relative to
  5266. the start of the region.")
  5267. (defun org-save-markers-in-region (beg end)
  5268. "Check markers in region.
  5269. If these markers are between BEG and END, record their position relative
  5270. to BEG, so that after moving the block of text, we can put the markers back
  5271. into place.
  5272. This function gets called just before an entry or tree gets cut from the
  5273. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5274. called immediately, to move the markers with the entries."
  5275. (setq org-markers-to-move nil)
  5276. (when (featurep 'org-clock)
  5277. (org-clock-save-markers-for-cut-and-paste beg end))
  5278. (when (featurep 'org-agenda)
  5279. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5280. (defun org-check-and-save-marker (marker beg end)
  5281. "Check if MARKER is between BEG and END.
  5282. If yes, remember the marker and the distance to BEG."
  5283. (when (and (marker-buffer marker)
  5284. (equal (marker-buffer marker) (current-buffer)))
  5285. (if (and (>= marker beg) (< marker end))
  5286. (push (cons marker (- marker beg)) org-markers-to-move))))
  5287. (defun org-reinstall-markers-in-region (beg)
  5288. "Move all remembered markers to their position relative to BEG."
  5289. (mapc (lambda (x)
  5290. (move-marker (car x) (+ beg (cdr x))))
  5291. org-markers-to-move)
  5292. (setq org-markers-to-move nil))
  5293. (defun org-narrow-to-subtree ()
  5294. "Narrow buffer to the current subtree."
  5295. (interactive)
  5296. (save-excursion
  5297. (save-match-data
  5298. (narrow-to-region
  5299. (progn (org-back-to-heading) (point))
  5300. (progn (org-end-of-subtree t) (point))))))
  5301. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5302. "Clone the task (subtree) at point N times.
  5303. The clones will be inserted as siblings.
  5304. In interactive use, the user will be prompted for the number of clones
  5305. to be produced, and for a time SHIFT, which may be a repeater as used
  5306. in time stamps, for example `+3d'.
  5307. When a valid repeater is given and the entry contains any time stamps,
  5308. the clones will become a sequence in time, with time stamps in the
  5309. subtree shifted for each clone produced. If SHIFT is nil or the
  5310. empty string, time stamps will be left alone.
  5311. If the original subtree did contain time stamps with a repeater,
  5312. the following will happen:
  5313. - the repeater will be removed in each clone
  5314. - an additional clone will be produced, with the current, unshifted
  5315. date(s) in the entry.
  5316. - the original entry will be placed *after* all the clones, with
  5317. repeater intact.
  5318. - the start days in the repeater in the original entry will be shifted
  5319. to past the last clone.
  5320. I this way you can spell out a number of instances of a repeating task,
  5321. and still retain the repeater to cover future instances of the task."
  5322. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5323. (let (beg end template task
  5324. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5325. (if (not (and (integerp n) (> n 0)))
  5326. (error "Invalid number of replications %s" n))
  5327. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5328. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5329. shift)))
  5330. (error "Invalid shift specification %s" shift))
  5331. (when doshift
  5332. (setq shift-n (string-to-number (match-string 1 shift))
  5333. shift-what (cdr (assoc (match-string 2 shift)
  5334. '(("d" . day) ("w" . week)
  5335. ("m" . month) ("y" . year))))))
  5336. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5337. (setq nmin 1 nmax n)
  5338. (org-back-to-heading t)
  5339. (setq beg (point))
  5340. (org-end-of-subtree t t)
  5341. (setq end (point))
  5342. (setq template (buffer-substring beg end))
  5343. (when (and doshift
  5344. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5345. (delete-region beg end)
  5346. (setq end beg)
  5347. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5348. (goto-char end)
  5349. (loop for n from nmin to nmax do
  5350. (if (not doshift)
  5351. (setq task template)
  5352. (with-temp-buffer
  5353. (insert template)
  5354. (org-mode)
  5355. (goto-char (point-min))
  5356. (while (re-search-forward org-ts-regexp nil t)
  5357. (org-timestamp-change (* n shift-n) shift-what))
  5358. (unless (= n n-no-remove)
  5359. (goto-char (point-min))
  5360. (while (re-search-forward org-ts-regexp nil t)
  5361. (save-excursion
  5362. (goto-char (match-beginning 0))
  5363. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5364. (delete-region (match-beginning 1) (match-end 1))))))
  5365. (setq task (buffer-string))))
  5366. (insert task))
  5367. (goto-char beg)))
  5368. ;;; Outline Sorting
  5369. (defun org-sort (with-case)
  5370. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5371. Optional argument WITH-CASE means sort case-sensitively.
  5372. With a double prefix argument, also remove duplicate entries."
  5373. (interactive "P")
  5374. (if (org-at-table-p)
  5375. (org-call-with-arg 'org-table-sort-lines with-case)
  5376. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5377. (defun org-sort-remove-invisible (s)
  5378. (remove-text-properties 0 (length s) org-rm-props s)
  5379. (while (string-match org-bracket-link-regexp s)
  5380. (setq s (replace-match (if (match-end 2)
  5381. (match-string 3 s)
  5382. (match-string 1 s)) t t s)))
  5383. s)
  5384. (defvar org-priority-regexp) ; defined later in the file
  5385. (defun org-sort-entries-or-items
  5386. (&optional with-case sorting-type getkey-func compare-func property)
  5387. "Sort entries on a certain level of an outline tree.
  5388. If there is an active region, the entries in the region are sorted.
  5389. Else, if the cursor is before the first entry, sort the top-level items.
  5390. Else, the children of the entry at point are sorted.
  5391. Sorting can be alphabetically, numerically, and by date/time as given by
  5392. the first time stamp in the entry. The command prompts for the sorting
  5393. type unless it has been given to the function through the SORTING-TYPE
  5394. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5395. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5396. called with point at the beginning of the record. It must return either
  5397. a string or a number that should serve as the sorting key for that record.
  5398. Comparing entries ignores case by default. However, with an optional argument
  5399. WITH-CASE, the sorting considers case as well."
  5400. (interactive "P")
  5401. (let ((case-func (if with-case 'identity 'downcase))
  5402. start beg end stars re re2
  5403. txt what tmp plain-list-p)
  5404. ;; Find beginning and end of region to sort
  5405. (cond
  5406. ((org-region-active-p)
  5407. ;; we will sort the region
  5408. (setq end (region-end)
  5409. what "region")
  5410. (goto-char (region-beginning))
  5411. (if (not (org-on-heading-p)) (outline-next-heading))
  5412. (setq start (point)))
  5413. ((org-at-item-p)
  5414. ;; we will sort this plain list
  5415. (org-beginning-of-item-list) (setq start (point))
  5416. (org-end-of-item-list) (setq end (point))
  5417. (goto-char start)
  5418. (setq plain-list-p t
  5419. what "plain list"))
  5420. ((or (org-on-heading-p)
  5421. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5422. ;; we will sort the children of the current headline
  5423. (org-back-to-heading)
  5424. (setq start (point)
  5425. end (progn (org-end-of-subtree t t)
  5426. (org-back-over-empty-lines)
  5427. (point))
  5428. what "children")
  5429. (goto-char start)
  5430. (show-subtree)
  5431. (outline-next-heading))
  5432. (t
  5433. ;; we will sort the top-level entries in this file
  5434. (goto-char (point-min))
  5435. (or (org-on-heading-p) (outline-next-heading))
  5436. (setq start (point) end (point-max) what "top-level")
  5437. (goto-char start)
  5438. (show-all)))
  5439. (setq beg (point))
  5440. (if (>= beg end) (error "Nothing to sort"))
  5441. (unless plain-list-p
  5442. (looking-at "\\(\\*+\\)")
  5443. (setq stars (match-string 1)
  5444. re (concat "^" (regexp-quote stars) " +")
  5445. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5446. txt (buffer-substring beg end))
  5447. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5448. (if (and (not (equal stars "*")) (string-match re2 txt))
  5449. (error "Region to sort contains a level above the first entry")))
  5450. (unless sorting-type
  5451. (message
  5452. (if plain-list-p
  5453. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5454. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5455. what)
  5456. (setq sorting-type (read-char-exclusive))
  5457. (and (= (downcase sorting-type) ?f)
  5458. (setq getkey-func
  5459. (org-ido-completing-read "Sort using function: "
  5460. obarray 'fboundp t nil nil))
  5461. (setq getkey-func (intern getkey-func)))
  5462. (and (= (downcase sorting-type) ?r)
  5463. (setq property
  5464. (org-ido-completing-read "Property: "
  5465. (mapcar 'list (org-buffer-property-keys t))
  5466. nil t))))
  5467. (message "Sorting entries...")
  5468. (save-restriction
  5469. (narrow-to-region start end)
  5470. (let ((dcst (downcase sorting-type))
  5471. (now (current-time)))
  5472. (sort-subr
  5473. (/= dcst sorting-type)
  5474. ;; This function moves to the beginning character of the "record" to
  5475. ;; be sorted.
  5476. (if plain-list-p
  5477. (lambda nil
  5478. (if (org-at-item-p) t (goto-char (point-max))))
  5479. (lambda nil
  5480. (if (re-search-forward re nil t)
  5481. (goto-char (match-beginning 0))
  5482. (goto-char (point-max)))))
  5483. ;; This function moves to the last character of the "record" being
  5484. ;; sorted.
  5485. (if plain-list-p
  5486. 'org-end-of-item
  5487. (lambda nil
  5488. (save-match-data
  5489. (condition-case nil
  5490. (outline-forward-same-level 1)
  5491. (error
  5492. (goto-char (point-max)))))))
  5493. ;; This function returns the value that gets sorted against.
  5494. (if plain-list-p
  5495. (lambda nil
  5496. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5497. (cond
  5498. ((= dcst ?n)
  5499. (string-to-number (buffer-substring (match-end 0)
  5500. (point-at-eol))))
  5501. ((= dcst ?a)
  5502. (buffer-substring (match-end 0) (point-at-eol)))
  5503. ((= dcst ?t)
  5504. (if (re-search-forward org-ts-regexp
  5505. (point-at-eol) t)
  5506. (org-time-string-to-time (match-string 0))
  5507. now))
  5508. ((= dcst ?f)
  5509. (if getkey-func
  5510. (progn
  5511. (setq tmp (funcall getkey-func))
  5512. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5513. tmp)
  5514. (error "Invalid key function `%s'" getkey-func)))
  5515. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5516. (lambda nil
  5517. (cond
  5518. ((= dcst ?n)
  5519. (if (looking-at org-complex-heading-regexp)
  5520. (string-to-number (match-string 4))
  5521. nil))
  5522. ((= dcst ?a)
  5523. (if (looking-at org-complex-heading-regexp)
  5524. (funcall case-func (match-string 4))
  5525. nil))
  5526. ((= dcst ?t)
  5527. (if (re-search-forward org-ts-regexp
  5528. (save-excursion
  5529. (forward-line 2)
  5530. (point)) t)
  5531. (org-time-string-to-time (match-string 0))
  5532. now))
  5533. ((= dcst ?p)
  5534. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5535. (string-to-char (match-string 2))
  5536. org-default-priority))
  5537. ((= dcst ?r)
  5538. (or (org-entry-get nil property) ""))
  5539. ((= dcst ?o)
  5540. (if (looking-at org-complex-heading-regexp)
  5541. (- 9999 (length (member (match-string 2)
  5542. org-todo-keywords-1)))))
  5543. ((= dcst ?f)
  5544. (if getkey-func
  5545. (progn
  5546. (setq tmp (funcall getkey-func))
  5547. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5548. tmp)
  5549. (error "Invalid key function `%s'" getkey-func)))
  5550. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5551. nil
  5552. (cond
  5553. ((= dcst ?a) 'string<)
  5554. ((= dcst ?t) 'time-less-p)
  5555. ((= dcst ?f) compare-func)
  5556. (t nil)))))
  5557. (message "Sorting entries...done")))
  5558. (defun org-do-sort (table what &optional with-case sorting-type)
  5559. "Sort TABLE of WHAT according to SORTING-TYPE.
  5560. The user will be prompted for the SORTING-TYPE if the call to this
  5561. function does not specify it. WHAT is only for the prompt, to indicate
  5562. what is being sorted. The sorting key will be extracted from
  5563. the car of the elements of the table.
  5564. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5565. (unless sorting-type
  5566. (message
  5567. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5568. what)
  5569. (setq sorting-type (read-char-exclusive)))
  5570. (let ((dcst (downcase sorting-type))
  5571. extractfun comparefun)
  5572. ;; Define the appropriate functions
  5573. (cond
  5574. ((= dcst ?n)
  5575. (setq extractfun 'string-to-number
  5576. comparefun (if (= dcst sorting-type) '< '>)))
  5577. ((= dcst ?a)
  5578. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5579. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5580. comparefun (if (= dcst sorting-type)
  5581. 'string<
  5582. (lambda (a b) (and (not (string< a b))
  5583. (not (string= a b)))))))
  5584. ((= dcst ?t)
  5585. (setq extractfun
  5586. (lambda (x)
  5587. (if (or (string-match org-ts-regexp x)
  5588. (string-match org-ts-regexp-both x))
  5589. (time-to-seconds
  5590. (org-time-string-to-time (match-string 0 x)))
  5591. 0))
  5592. comparefun (if (= dcst sorting-type) '< '>)))
  5593. (t (error "Invalid sorting type `%c'" sorting-type)))
  5594. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5595. table)
  5596. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5597. ;;; Editing source examples
  5598. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5599. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5600. (defvar org-edit-src-force-single-line nil)
  5601. (defvar org-edit-src-from-org-mode nil)
  5602. (defvar org-edit-src-picture nil)
  5603. (define-minor-mode org-exit-edit-mode
  5604. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5605. (defun org-edit-src-code ()
  5606. "Edit the source code example at point.
  5607. An indirect buffer is created, and that buffer is then narrowed to the
  5608. example at point and switched to the correct language mode. When done,
  5609. exit by killing the buffer with \\[org-edit-src-exit]."
  5610. (interactive)
  5611. (let ((line (org-current-line))
  5612. (case-fold-search t)
  5613. (msg (substitute-command-keys
  5614. "Edit, then exit with C-c ' (C-c and single quote)"))
  5615. (info (org-edit-src-find-region-and-lang))
  5616. (org-mode-p (eq major-mode 'org-mode))
  5617. beg end lang lang-f single lfmt)
  5618. (if (not info)
  5619. nil
  5620. (setq beg (nth 0 info)
  5621. end (nth 1 info)
  5622. lang (nth 2 info)
  5623. single (nth 3 info)
  5624. lfmt (nth 4 info)
  5625. lang-f (intern (concat lang "-mode")))
  5626. (unless (functionp lang-f)
  5627. (error "No such language mode: %s" lang-f))
  5628. (goto-line line)
  5629. (if (get-buffer "*Org Edit Src Example*")
  5630. (kill-buffer "*Org Edit Src Example*"))
  5631. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5632. "*Org Edit Src Example*"))
  5633. (narrow-to-region beg end)
  5634. (remove-text-properties beg end '(display nil invisible nil
  5635. intangible nil))
  5636. (let ((org-inhibit-startup t))
  5637. (funcall lang-f))
  5638. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5639. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5640. (when lfmt
  5641. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5642. (when org-mode-p
  5643. (goto-char (point-min))
  5644. (while (re-search-forward "^," nil t)
  5645. (replace-match "")))
  5646. (goto-line line)
  5647. (org-exit-edit-mode)
  5648. (org-set-local 'header-line-format msg)
  5649. (message "%s" msg)
  5650. t)))
  5651. (defun org-edit-fixed-width-region ()
  5652. "Edit the fixed-width ascii drawing at point.
  5653. This must be a region where each line starts with a colon followed by
  5654. a space character.
  5655. An indirect buffer is created, and that buffer is then narrowed to the
  5656. example at point and switched to artist-mode. When done,
  5657. exit by killing the buffer with \\[org-edit-src-exit]."
  5658. (interactive)
  5659. (let ((line (org-current-line))
  5660. (case-fold-search t)
  5661. (msg (substitute-command-keys
  5662. "Edit, then exit with C-c ' (C-c and single quote)"))
  5663. (org-mode-p (eq major-mode 'org-mode))
  5664. beg end)
  5665. (beginning-of-line 1)
  5666. (if (looking-at "[ \t]*[^:\n \t]")
  5667. nil
  5668. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5669. (setq beg (point) end beg)
  5670. (save-excursion
  5671. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5672. (setq beg (point-at-bol 2))
  5673. (setq beg (point))))
  5674. (save-excursion
  5675. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5676. (setq end (1- (match-beginning 0)))
  5677. (setq end (point))))
  5678. (goto-line line))
  5679. (if (get-buffer "*Org Edit Picture*")
  5680. (kill-buffer "*Org Edit Picture*"))
  5681. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5682. "*Org Edit Picture*"))
  5683. (narrow-to-region beg end)
  5684. (remove-text-properties beg end '(display nil invisible nil
  5685. intangible nil))
  5686. (when (fboundp 'font-lock-unfontify-region)
  5687. (font-lock-unfontify-region (point-min) (point-max)))
  5688. (cond
  5689. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5690. (fundamental-mode)
  5691. (artist-mode 1))
  5692. (t (funcall org-edit-fixed-width-region-mode)))
  5693. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5694. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5695. (set (make-local-variable 'org-edit-src-picture) t)
  5696. (goto-char (point-min))
  5697. (while (re-search-forward "^[ \t]*: ?" nil t)
  5698. (replace-match ""))
  5699. (goto-line line)
  5700. (org-exit-edit-mode)
  5701. (org-set-local 'header-line-format msg)
  5702. (message "%s" msg)
  5703. t)))
  5704. (defun org-edit-src-find-region-and-lang ()
  5705. "Find the region and language for a local edit.
  5706. Return a list with beginning and end of the region, a string representing
  5707. the language, a switch telling of the content should be in a single line."
  5708. (let ((re-list
  5709. (append
  5710. org-edit-src-region-extra
  5711. '(
  5712. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5713. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5714. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5715. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5716. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5717. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5718. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5719. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5720. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5721. ("^#\\+html:" "\n" "html" single-line)
  5722. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5723. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5724. ("^#\\+latex:" "\n" "latex" single-line)
  5725. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5726. ("^#\\+ascii:" "\n" "ascii" single-line)
  5727. )))
  5728. (pos (point))
  5729. re1 re2 single beg end lang lfmt match-re1)
  5730. (catch 'exit
  5731. (while (setq entry (pop re-list))
  5732. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5733. single (nth 3 entry))
  5734. (save-excursion
  5735. (if (or (looking-at re1)
  5736. (re-search-backward re1 nil t))
  5737. (progn
  5738. (setq match-re1 (match-string 0))
  5739. (setq beg (match-end 0)
  5740. lang (org-edit-src-get-lang lang)
  5741. lfmt (org-edit-src-get-label-format match-re1))
  5742. (if (and (re-search-forward re2 nil t)
  5743. (>= (match-end 0) pos))
  5744. (throw 'exit (list beg (match-beginning 0)
  5745. lang single lfmt))))
  5746. (if (or (looking-at re2)
  5747. (re-search-forward re2 nil t))
  5748. (progn
  5749. (setq end (match-beginning 0))
  5750. (if (and (re-search-backward re1 nil t)
  5751. (<= (match-beginning 0) pos))
  5752. (progn
  5753. (setq lfmt (org-edit-src-get-label-format
  5754. (match-string 0)))
  5755. (throw 'exit
  5756. (list (match-end 0) end
  5757. (org-edit-src-get-lang lang)
  5758. single lfmt))))))))))))
  5759. (defun org-edit-src-get-lang (lang)
  5760. "Extract the src language."
  5761. (let ((m (match-string 0)))
  5762. (cond
  5763. ((stringp lang) lang)
  5764. ((integerp lang) (match-string lang))
  5765. ((and (eq lang 'lang)
  5766. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5767. (match-string 1 m))
  5768. ((and (eq lang 'style)
  5769. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5770. (match-string 1 m))
  5771. (t "fundamental"))))
  5772. (defun org-edit-src-get-label-format (s)
  5773. "Extract the label format."
  5774. (save-match-data
  5775. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5776. (match-string 1 s))))
  5777. (defun org-edit-src-exit ()
  5778. "Exit special edit and protect problematic lines."
  5779. (interactive)
  5780. (unless (buffer-base-buffer (current-buffer))
  5781. (error "This is not an indirect buffer, something is wrong..."))
  5782. (unless (> (point-min) 1)
  5783. (error "This buffer is not narrowed, something is wrong..."))
  5784. (goto-char (point-min))
  5785. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5786. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5787. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5788. (goto-char (point-min))
  5789. (while (re-search-forward "\n" nil t)
  5790. (replace-match " "))
  5791. (goto-char (point-min))
  5792. (if (looking-at "\\s-*") (replace-match " "))
  5793. (if (re-search-forward "\\s-+\\'" nil t)
  5794. (replace-match "")))
  5795. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5796. (goto-char (point-min))
  5797. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5798. (replace-match ",\\1"))
  5799. (when font-lock-mode
  5800. (font-lock-unfontify-region (point-min) (point-max)))
  5801. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5802. (when (org-bound-and-true-p org-edit-src-picture)
  5803. (untabify (point-min) (point-max))
  5804. (goto-char (point-min))
  5805. (while (re-search-forward "^" nil t)
  5806. (replace-match ": "))
  5807. (when font-lock-mode
  5808. (font-lock-unfontify-region (point-min) (point-max)))
  5809. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5810. (kill-buffer (current-buffer))
  5811. (and (org-mode-p) (org-restart-font-lock)))
  5812. ;;; The orgstruct minor mode
  5813. ;; Define a minor mode which can be used in other modes in order to
  5814. ;; integrate the org-mode structure editing commands.
  5815. ;; This is really a hack, because the org-mode structure commands use
  5816. ;; keys which normally belong to the major mode. Here is how it
  5817. ;; works: The minor mode defines all the keys necessary to operate the
  5818. ;; structure commands, but wraps the commands into a function which
  5819. ;; tests if the cursor is currently at a headline or a plain list
  5820. ;; item. If that is the case, the structure command is used,
  5821. ;; temporarily setting many Org-mode variables like regular
  5822. ;; expressions for filling etc. However, when any of those keys is
  5823. ;; used at a different location, function uses `key-binding' to look
  5824. ;; up if the key has an associated command in another currently active
  5825. ;; keymap (minor modes, major mode, global), and executes that
  5826. ;; command. There might be problems if any of the keys is otherwise
  5827. ;; used as a prefix key.
  5828. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5829. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5830. ;; addresses this by checking explicitly for both bindings.
  5831. (defvar orgstruct-mode-map (make-sparse-keymap)
  5832. "Keymap for the minor `orgstruct-mode'.")
  5833. (defvar org-local-vars nil
  5834. "List of local variables, for use by `orgstruct-mode'")
  5835. ;;;###autoload
  5836. (define-minor-mode orgstruct-mode
  5837. "Toggle the minor more `orgstruct-mode'.
  5838. This mode is for using Org-mode structure commands in other modes.
  5839. The following key behave as if Org-mode was active, if the cursor
  5840. is on a headline, or on a plain list item (both in the definition
  5841. of Org-mode).
  5842. M-up Move entry/item up
  5843. M-down Move entry/item down
  5844. M-left Promote
  5845. M-right Demote
  5846. M-S-up Move entry/item up
  5847. M-S-down Move entry/item down
  5848. M-S-left Promote subtree
  5849. M-S-right Demote subtree
  5850. M-q Fill paragraph and items like in Org-mode
  5851. C-c ^ Sort entries
  5852. C-c - Cycle list bullet
  5853. TAB Cycle item visibility
  5854. M-RET Insert new heading/item
  5855. S-M-RET Insert new TODO heading / Checkbox item
  5856. C-c C-c Set tags / toggle checkbox"
  5857. nil " OrgStruct" nil
  5858. (org-load-modules-maybe)
  5859. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5860. ;;;###autoload
  5861. (defun turn-on-orgstruct ()
  5862. "Unconditionally turn on `orgstruct-mode'."
  5863. (orgstruct-mode 1))
  5864. (defun orgstruct++-mode (&optional arg)
  5865. "Toggle `orgstruct-mode', the enhanced version of it.
  5866. In addition to setting orgstruct-mode, this also exports all indentation and
  5867. autofilling variables from org-mode into the buffer. It will also
  5868. recognize item context in multiline items.
  5869. Note that turning off orgstruct-mode will *not* remove the
  5870. indentation/paragraph settings. This can only be done by refreshing the
  5871. major mode, for example with \[normal-mode]."
  5872. (interactive "P")
  5873. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5874. (if (< arg 1)
  5875. (orgstruct-mode -1)
  5876. (orgstruct-mode 1)
  5877. (let (var val)
  5878. (mapc
  5879. (lambda (x)
  5880. (when (string-match
  5881. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5882. (symbol-name (car x)))
  5883. (setq var (car x) val (nth 1 x))
  5884. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5885. org-local-vars)
  5886. (org-set-local 'orgstruct-is-++ t))))
  5887. (defvar orgstruct-is-++ nil
  5888. "Is orgstruct-mode in ++ version in the current-buffer?")
  5889. (make-variable-buffer-local 'orgstruct-is-++)
  5890. ;;;###autoload
  5891. (defun turn-on-orgstruct++ ()
  5892. "Unconditionally turn on `orgstruct++-mode'."
  5893. (orgstruct++-mode 1))
  5894. (defun orgstruct-error ()
  5895. "Error when there is no default binding for a structure key."
  5896. (interactive)
  5897. (error "This key has no function outside structure elements"))
  5898. (defun orgstruct-setup ()
  5899. "Setup orgstruct keymaps."
  5900. (let ((nfunc 0)
  5901. (bindings
  5902. (list
  5903. '([(meta up)] org-metaup)
  5904. '([(meta down)] org-metadown)
  5905. '([(meta left)] org-metaleft)
  5906. '([(meta right)] org-metaright)
  5907. '([(meta shift up)] org-shiftmetaup)
  5908. '([(meta shift down)] org-shiftmetadown)
  5909. '([(meta shift left)] org-shiftmetaleft)
  5910. '([(meta shift right)] org-shiftmetaright)
  5911. '([(shift up)] org-shiftup)
  5912. '([(shift down)] org-shiftdown)
  5913. '([(shift left)] org-shiftleft)
  5914. '([(shift right)] org-shiftright)
  5915. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5916. '("\M-q" fill-paragraph)
  5917. '("\C-c^" org-sort)
  5918. '("\C-c-" org-cycle-list-bullet)))
  5919. elt key fun cmd)
  5920. (while (setq elt (pop bindings))
  5921. (setq nfunc (1+ nfunc))
  5922. (setq key (org-key (car elt))
  5923. fun (nth 1 elt)
  5924. cmd (orgstruct-make-binding fun nfunc key))
  5925. (org-defkey orgstruct-mode-map key cmd))
  5926. ;; Special treatment needed for TAB and RET
  5927. (org-defkey orgstruct-mode-map [(tab)]
  5928. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5929. (org-defkey orgstruct-mode-map "\C-i"
  5930. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5931. (org-defkey orgstruct-mode-map "\M-\C-m"
  5932. (orgstruct-make-binding 'org-insert-heading 105
  5933. "\M-\C-m" [(meta return)]))
  5934. (org-defkey orgstruct-mode-map [(meta return)]
  5935. (orgstruct-make-binding 'org-insert-heading 106
  5936. [(meta return)] "\M-\C-m"))
  5937. (org-defkey orgstruct-mode-map [(shift meta return)]
  5938. (orgstruct-make-binding 'org-insert-todo-heading 107
  5939. [(meta return)] "\M-\C-m"))
  5940. (unless org-local-vars
  5941. (setq org-local-vars (org-get-local-variables)))
  5942. t))
  5943. (defun orgstruct-make-binding (fun n &rest keys)
  5944. "Create a function for binding in the structure minor mode.
  5945. FUN is the command to call inside a table. N is used to create a unique
  5946. command name. KEYS are keys that should be checked in for a command
  5947. to execute outside of tables."
  5948. (eval
  5949. (list 'defun
  5950. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5951. '(arg)
  5952. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5953. "Outside of structure, run the binding of `"
  5954. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5955. "'.")
  5956. '(interactive "p")
  5957. (list 'if
  5958. `(org-context-p 'headline 'item
  5959. (and orgstruct-is-++
  5960. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5961. 'item-body))
  5962. (list 'org-run-like-in-org-mode (list 'quote fun))
  5963. (list 'let '(orgstruct-mode)
  5964. (list 'call-interactively
  5965. (append '(or)
  5966. (mapcar (lambda (k)
  5967. (list 'key-binding k))
  5968. keys)
  5969. '('orgstruct-error))))))))
  5970. (defun org-context-p (&rest contexts)
  5971. "Check if local context is any of CONTEXTS.
  5972. Possible values in the list of contexts are `table', `headline', and `item'."
  5973. (let ((pos (point)))
  5974. (goto-char (point-at-bol))
  5975. (prog1 (or (and (memq 'table contexts)
  5976. (looking-at "[ \t]*|"))
  5977. (and (memq 'headline contexts)
  5978. ;;????????? (looking-at "\\*+"))
  5979. (looking-at outline-regexp))
  5980. (and (memq 'item contexts)
  5981. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5982. (and (memq 'item-body contexts)
  5983. (org-in-item-p)))
  5984. (goto-char pos))))
  5985. (defun org-get-local-variables ()
  5986. "Return a list of all local variables in an org-mode buffer."
  5987. (let (varlist)
  5988. (with-current-buffer (get-buffer-create "*Org tmp*")
  5989. (erase-buffer)
  5990. (org-mode)
  5991. (setq varlist (buffer-local-variables)))
  5992. (kill-buffer "*Org tmp*")
  5993. (delq nil
  5994. (mapcar
  5995. (lambda (x)
  5996. (setq x
  5997. (if (symbolp x)
  5998. (list x)
  5999. (list (car x) (list 'quote (cdr x)))))
  6000. (if (string-match
  6001. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6002. (symbol-name (car x)))
  6003. x nil))
  6004. varlist))))
  6005. ;;;###autoload
  6006. (defun org-run-like-in-org-mode (cmd)
  6007. "Run a command, pretending that the current buffer is in Org-mode.
  6008. This will temporarily bind local variables that are typically bound in
  6009. Org-mode to the values they have in Org-mode, and then interactively
  6010. call CMD."
  6011. (org-load-modules-maybe)
  6012. (unless org-local-vars
  6013. (setq org-local-vars (org-get-local-variables)))
  6014. (eval (list 'let org-local-vars
  6015. (list 'call-interactively (list 'quote cmd)))))
  6016. ;;;; Archiving
  6017. (defun org-get-category (&optional pos)
  6018. "Get the category applying to position POS."
  6019. (get-text-property (or pos (point)) 'org-category))
  6020. (defun org-refresh-category-properties ()
  6021. "Refresh category text properties in the buffer."
  6022. (let ((def-cat (cond
  6023. ((null org-category)
  6024. (if buffer-file-name
  6025. (file-name-sans-extension
  6026. (file-name-nondirectory buffer-file-name))
  6027. "???"))
  6028. ((symbolp org-category) (symbol-name org-category))
  6029. (t org-category)))
  6030. beg end cat pos optionp)
  6031. (org-unmodified
  6032. (save-excursion
  6033. (save-restriction
  6034. (widen)
  6035. (goto-char (point-min))
  6036. (put-text-property (point) (point-max) 'org-category def-cat)
  6037. (while (re-search-forward
  6038. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6039. (setq pos (match-end 0)
  6040. optionp (equal (char-after (match-beginning 0)) ?#)
  6041. cat (org-trim (match-string 2)))
  6042. (if optionp
  6043. (setq beg (point-at-bol) end (point-max))
  6044. (org-back-to-heading t)
  6045. (setq beg (point) end (org-end-of-subtree t t)))
  6046. (put-text-property beg end 'org-category cat)
  6047. (goto-char pos)))))))
  6048. ;;;; Link Stuff
  6049. ;;; Link abbreviations
  6050. (defun org-link-expand-abbrev (link)
  6051. "Apply replacements as defined in `org-link-abbrev-alist."
  6052. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6053. (let* ((key (match-string 1 link))
  6054. (as (or (assoc key org-link-abbrev-alist-local)
  6055. (assoc key org-link-abbrev-alist)))
  6056. (tag (and (match-end 2) (match-string 3 link)))
  6057. rpl)
  6058. (if (not as)
  6059. link
  6060. (setq rpl (cdr as))
  6061. (cond
  6062. ((symbolp rpl) (funcall rpl tag))
  6063. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6064. ((string-match "%h" rpl)
  6065. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6066. (t (concat rpl tag)))))
  6067. link))
  6068. ;;; Storing and inserting links
  6069. (defvar org-insert-link-history nil
  6070. "Minibuffer history for links inserted with `org-insert-link'.")
  6071. (defvar org-stored-links nil
  6072. "Contains the links stored with `org-store-link'.")
  6073. (defvar org-store-link-plist nil
  6074. "Plist with info about the most recently link created with `org-store-link'.")
  6075. (defvar org-link-protocols nil
  6076. "Link protocols added to Org-mode using `org-add-link-type'.")
  6077. (defvar org-store-link-functions nil
  6078. "List of functions that are called to create and store a link.
  6079. Each function will be called in turn until one returns a non-nil
  6080. value. Each function should check if it is responsible for creating
  6081. this link (for example by looking at the major mode).
  6082. If not, it must exit and return nil.
  6083. If yes, it should return a non-nil value after a calling
  6084. `org-store-link-props' with a list of properties and values.
  6085. Special properties are:
  6086. :type The link prefix. like \"http\". This must be given.
  6087. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6088. This is obligatory as well.
  6089. :description Optional default description for the second pair
  6090. of brackets in an Org-mode link. The user can still change
  6091. this when inserting this link into an Org-mode buffer.
  6092. In addition to these, any additional properties can be specified
  6093. and then used in remember templates.")
  6094. (defun org-add-link-type (type &optional follow export)
  6095. "Add TYPE to the list of `org-link-types'.
  6096. Re-compute all regular expressions depending on `org-link-types'
  6097. FOLLOW and EXPORT are two functions.
  6098. FOLLOW should take the link path as the single argument and do whatever
  6099. is necessary to follow the link, for example find a file or display
  6100. a mail message.
  6101. EXPORT should format the link path for export to one of the export formats.
  6102. It should be a function accepting three arguments:
  6103. path the path of the link, the text after the prefix (like \"http:\")
  6104. desc the description of the link, if any, nil if there was no description
  6105. format the export format, a symbol like `html' or `latex'.
  6106. The function may use the FORMAT information to return different values
  6107. depending on the format. The return value will be put literally into
  6108. the exported file.
  6109. Org-mode has a built-in default for exporting links. If you are happy with
  6110. this default, there is no need to define an export function for the link
  6111. type. For a simple example of an export function, see `org-bbdb.el'."
  6112. (add-to-list 'org-link-types type t)
  6113. (org-make-link-regexps)
  6114. (if (assoc type org-link-protocols)
  6115. (setcdr (assoc type org-link-protocols) (list follow export))
  6116. (push (list type follow export) org-link-protocols)))
  6117. ;;;###autoload
  6118. (defun org-store-link (arg)
  6119. "\\<org-mode-map>Store an org-link to the current location.
  6120. This link is added to `org-stored-links' and can later be inserted
  6121. into an org-buffer with \\[org-insert-link].
  6122. For some link types, a prefix arg is interpreted:
  6123. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6124. For file links, arg negates `org-context-in-file-links'."
  6125. (interactive "P")
  6126. (org-load-modules-maybe)
  6127. (setq org-store-link-plist nil) ; reset
  6128. (let (link cpltxt desc description search txt)
  6129. (cond
  6130. ((run-hook-with-args-until-success 'org-store-link-functions)
  6131. (setq link (plist-get org-store-link-plist :link)
  6132. desc (or (plist-get org-store-link-plist :description) link)))
  6133. ((equal (buffer-name) "*Org Edit Src Example*")
  6134. (let (label gc)
  6135. (while (or (not label)
  6136. (save-excursion
  6137. (save-restriction
  6138. (widen)
  6139. (goto-char (point-min))
  6140. (re-search-forward
  6141. (regexp-quote (format org-coderef-label-format label))
  6142. nil t))))
  6143. (when label (message "Label exists already") (sit-for 2))
  6144. (setq label (read-string "Code line label: " label)))
  6145. (end-of-line 1)
  6146. (setq link (format org-coderef-label-format label))
  6147. (setq gc (- 79 (length link)))
  6148. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6149. (insert link)
  6150. (setq link (concat "(" label ")") desc nil)))
  6151. ((eq major-mode 'calendar-mode)
  6152. (let ((cd (calendar-cursor-to-date)))
  6153. (setq link
  6154. (format-time-string
  6155. (car org-time-stamp-formats)
  6156. (apply 'encode-time
  6157. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6158. nil nil nil))))
  6159. (org-store-link-props :type "calendar" :date cd)))
  6160. ((eq major-mode 'w3-mode)
  6161. (setq cpltxt (if (and (buffer-name)
  6162. (not (string-match "Untitled" (buffer-name))))
  6163. (buffer-name)
  6164. (url-view-url t))
  6165. link (org-make-link (url-view-url t)))
  6166. (org-store-link-props :type "w3" :url (url-view-url t)))
  6167. ((eq major-mode 'w3m-mode)
  6168. (setq cpltxt (or w3m-current-title w3m-current-url)
  6169. link (org-make-link w3m-current-url))
  6170. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6171. ((setq search (run-hook-with-args-until-success
  6172. 'org-create-file-search-functions))
  6173. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6174. "::" search))
  6175. (setq cpltxt (or description link)))
  6176. ((eq major-mode 'image-mode)
  6177. (setq cpltxt (concat "file:"
  6178. (abbreviate-file-name buffer-file-name))
  6179. link (org-make-link cpltxt))
  6180. (org-store-link-props :type "image" :file buffer-file-name))
  6181. ((eq major-mode 'dired-mode)
  6182. ;; link to the file in the current line
  6183. (setq cpltxt (concat "file:"
  6184. (abbreviate-file-name
  6185. (expand-file-name
  6186. (dired-get-filename nil t))))
  6187. link (org-make-link cpltxt)))
  6188. ((and buffer-file-name (org-mode-p))
  6189. (cond
  6190. ((org-in-regexp "<<\\(.*?\\)>>")
  6191. (setq cpltxt
  6192. (concat "file:"
  6193. (abbreviate-file-name buffer-file-name)
  6194. "::" (match-string 1))
  6195. link (org-make-link cpltxt)))
  6196. ((and (featurep 'org-id)
  6197. (or (eq org-link-to-org-use-id t)
  6198. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6199. (interactive-p))
  6200. (and org-link-to-org-use-id
  6201. (condition-case nil
  6202. (org-entry-get nil "ID")
  6203. (error nil)))))
  6204. ;; We can make a link using the ID.
  6205. (setq link (condition-case nil
  6206. (prog1 (org-id-store-link)
  6207. (setq desc (plist-get org-store-link-plist
  6208. :description)))
  6209. (error
  6210. ;; probably before first headline, link to file only
  6211. (concat "file:"
  6212. (abbreviate-file-name buffer-file-name))))))
  6213. (t
  6214. ;; Just link to current headline
  6215. (setq cpltxt (concat "file:"
  6216. (abbreviate-file-name buffer-file-name)))
  6217. ;; Add a context search string
  6218. (when (org-xor org-context-in-file-links arg)
  6219. (setq txt (cond
  6220. ((org-on-heading-p) nil)
  6221. ((org-region-active-p)
  6222. (buffer-substring (region-beginning) (region-end)))
  6223. (t nil)))
  6224. (when (or (null txt) (string-match "\\S-" txt))
  6225. (setq cpltxt
  6226. (concat cpltxt "::"
  6227. (condition-case nil
  6228. (org-make-org-heading-search-string txt)
  6229. (error "")))
  6230. desc "NONE")))
  6231. (if (string-match "::\\'" cpltxt)
  6232. (setq cpltxt (substring cpltxt 0 -2)))
  6233. (setq link (org-make-link cpltxt)))))
  6234. ((buffer-file-name (buffer-base-buffer))
  6235. ;; Just link to this file here.
  6236. (setq cpltxt (concat "file:"
  6237. (abbreviate-file-name
  6238. (buffer-file-name (buffer-base-buffer)))))
  6239. ;; Add a context string
  6240. (when (org-xor org-context-in-file-links arg)
  6241. (setq txt (if (org-region-active-p)
  6242. (buffer-substring (region-beginning) (region-end))
  6243. (buffer-substring (point-at-bol) (point-at-eol))))
  6244. ;; Only use search option if there is some text.
  6245. (when (string-match "\\S-" txt)
  6246. (setq cpltxt
  6247. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6248. desc "NONE")))
  6249. (setq link (org-make-link cpltxt)))
  6250. ((interactive-p)
  6251. (error "Cannot link to a buffer which is not visiting a file"))
  6252. (t (setq link nil)))
  6253. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6254. (setq link (or link cpltxt)
  6255. desc (or desc cpltxt))
  6256. (if (equal desc "NONE") (setq desc nil))
  6257. (if (and (interactive-p) link)
  6258. (progn
  6259. (setq org-stored-links
  6260. (cons (list link desc) org-stored-links))
  6261. (message "Stored: %s" (or desc link)))
  6262. (and link (org-make-link-string link desc)))))
  6263. (defun org-store-link-props (&rest plist)
  6264. "Store link properties, extract names and addresses."
  6265. (let (x adr)
  6266. (when (setq x (plist-get plist :from))
  6267. (setq adr (mail-extract-address-components x))
  6268. (setq plist (plist-put plist :fromname (car adr)))
  6269. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6270. (when (setq x (plist-get plist :to))
  6271. (setq adr (mail-extract-address-components x))
  6272. (setq plist (plist-put plist :toname (car adr)))
  6273. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6274. (let ((from (plist-get plist :from))
  6275. (to (plist-get plist :to)))
  6276. (when (and from to org-from-is-user-regexp)
  6277. (setq plist
  6278. (plist-put plist :fromto
  6279. (if (string-match org-from-is-user-regexp from)
  6280. (concat "to %t")
  6281. (concat "from %f"))))))
  6282. (setq org-store-link-plist plist))
  6283. (defun org-add-link-props (&rest plist)
  6284. "Add these properties to the link property list."
  6285. (let (key value)
  6286. (while plist
  6287. (setq key (pop plist) value (pop plist))
  6288. (setq org-store-link-plist
  6289. (plist-put org-store-link-plist key value)))))
  6290. (defun org-email-link-description (&optional fmt)
  6291. "Return the description part of an email link.
  6292. This takes information from `org-store-link-plist' and formats it
  6293. according to FMT (default from `org-email-link-description-format')."
  6294. (setq fmt (or fmt org-email-link-description-format))
  6295. (let* ((p org-store-link-plist)
  6296. (to (plist-get p :toaddress))
  6297. (from (plist-get p :fromaddress))
  6298. (table
  6299. (list
  6300. (cons "%c" (plist-get p :fromto))
  6301. (cons "%F" (plist-get p :from))
  6302. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6303. (cons "%T" (plist-get p :to))
  6304. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6305. (cons "%s" (plist-get p :subject))
  6306. (cons "%m" (plist-get p :message-id)))))
  6307. (when (string-match "%c" fmt)
  6308. ;; Check if the user wrote this message
  6309. (if (and org-from-is-user-regexp from to
  6310. (save-match-data (string-match org-from-is-user-regexp from)))
  6311. (setq fmt (replace-match "to %t" t t fmt))
  6312. (setq fmt (replace-match "from %f" t t fmt))))
  6313. (org-replace-escapes fmt table)))
  6314. (defun org-make-org-heading-search-string (&optional string heading)
  6315. "Make search string for STRING or current headline."
  6316. (interactive)
  6317. (let ((s (or string (org-get-heading))))
  6318. (unless (and string (not heading))
  6319. ;; We are using a headline, clean up garbage in there.
  6320. (if (string-match org-todo-regexp s)
  6321. (setq s (replace-match "" t t s)))
  6322. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6323. (setq s (replace-match "" t t s)))
  6324. (setq s (org-trim s))
  6325. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6326. org-comment-string "\\)") s)
  6327. (setq s (replace-match "" t t s)))
  6328. (while (string-match org-ts-regexp s)
  6329. (setq s (replace-match "" t t s))))
  6330. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6331. (setq s (replace-match " " t t s)))
  6332. (or string (setq s (concat "*" s))) ; Add * for headlines
  6333. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6334. (defun org-make-link (&rest strings)
  6335. "Concatenate STRINGS."
  6336. (apply 'concat strings))
  6337. (defun org-make-link-string (link &optional description)
  6338. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6339. (unless (string-match "\\S-" link)
  6340. (error "Empty link"))
  6341. (when (stringp description)
  6342. ;; Remove brackets from the description, they are fatal.
  6343. (while (string-match "\\[" description)
  6344. (setq description (replace-match "{" t t description)))
  6345. (while (string-match "\\]" description)
  6346. (setq description (replace-match "}" t t description))))
  6347. (when (equal (org-link-escape link) description)
  6348. ;; No description needed, it is identical
  6349. (setq description nil))
  6350. (when (and (not description)
  6351. (not (equal link (org-link-escape link))))
  6352. (setq description (org-extract-attributes link)))
  6353. (concat "[[" (org-link-escape link) "]"
  6354. (if description (concat "[" description "]") "")
  6355. "]"))
  6356. (defconst org-link-escape-chars
  6357. '((?\ . "%20")
  6358. (?\[ . "%5B")
  6359. (?\] . "%5D")
  6360. (?\340 . "%E0") ; `a
  6361. (?\342 . "%E2") ; ^a
  6362. (?\347 . "%E7") ; ,c
  6363. (?\350 . "%E8") ; `e
  6364. (?\351 . "%E9") ; 'e
  6365. (?\352 . "%EA") ; ^e
  6366. (?\356 . "%EE") ; ^i
  6367. (?\364 . "%F4") ; ^o
  6368. (?\371 . "%F9") ; `u
  6369. (?\373 . "%FB") ; ^u
  6370. (?\; . "%3B")
  6371. (?? . "%3F")
  6372. (?= . "%3D")
  6373. (?+ . "%2B")
  6374. )
  6375. "Association list of escapes for some characters problematic in links.
  6376. This is the list that is used for internal purposes.")
  6377. (defconst org-link-escape-chars-browser
  6378. '((?\ . "%20")) ; 32 for the SPC char
  6379. "Association list of escapes for some characters problematic in links.
  6380. This is the list that is used before handing over to the browser.")
  6381. (defun org-link-escape (text &optional table)
  6382. "Escape characters in TEXT that are problematic for links."
  6383. (setq table (or table org-link-escape-chars))
  6384. (when text
  6385. (let ((re (mapconcat (lambda (x) (regexp-quote
  6386. (char-to-string (car x))))
  6387. table "\\|")))
  6388. (while (string-match re text)
  6389. (setq text
  6390. (replace-match
  6391. (cdr (assoc (string-to-char (match-string 0 text))
  6392. table))
  6393. t t text)))
  6394. text)))
  6395. (defun org-link-unescape (text &optional table)
  6396. "Reverse the action of `org-link-escape'."
  6397. (setq table (or table org-link-escape-chars))
  6398. (when text
  6399. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6400. table "\\|")))
  6401. (while (string-match re text)
  6402. (setq text
  6403. (replace-match
  6404. (char-to-string (car (rassoc (match-string 0 text) table)))
  6405. t t text)))
  6406. text)))
  6407. (defun org-xor (a b)
  6408. "Exclusive or."
  6409. (if a (not b) b))
  6410. (defun org-fixup-message-id-for-http (s)
  6411. "Replace special characters in a message id, so it can be used in an http query."
  6412. (while (string-match "<" s)
  6413. (setq s (replace-match "%3C" t t s)))
  6414. (while (string-match ">" s)
  6415. (setq s (replace-match "%3E" t t s)))
  6416. (while (string-match "@" s)
  6417. (setq s (replace-match "%40" t t s)))
  6418. s)
  6419. ;;;###autoload
  6420. (defun org-insert-link-global ()
  6421. "Insert a link like Org-mode does.
  6422. This command can be called in any mode to insert a link in Org-mode syntax."
  6423. (interactive)
  6424. (org-load-modules-maybe)
  6425. (org-run-like-in-org-mode 'org-insert-link))
  6426. (defun org-insert-link (&optional complete-file link-location)
  6427. "Insert a link. At the prompt, enter the link.
  6428. Completion can be used to insert any of the link protocol prefixes like
  6429. http or ftp in use.
  6430. The history can be used to select a link previously stored with
  6431. `org-store-link'. When the empty string is entered (i.e. if you just
  6432. press RET at the prompt), the link defaults to the most recently
  6433. stored link. As SPC triggers completion in the minibuffer, you need to
  6434. use M-SPC or C-q SPC to force the insertion of a space character.
  6435. You will also be prompted for a description, and if one is given, it will
  6436. be displayed in the buffer instead of the link.
  6437. If there is already a link at point, this command will allow you to edit link
  6438. and description parts.
  6439. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6440. be selected using completion. The path to the file will be relative to the
  6441. current directory if the file is in the current directory or a subdirectory.
  6442. Otherwise, the link will be the absolute path as completed in the minibuffer
  6443. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6444. option `org-link-file-path-type'.
  6445. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6446. the current directory or below.
  6447. With three \\[universal-argument] prefixes, negate the meaning of
  6448. `org-keep-stored-link-after-insertion'.
  6449. If `org-make-link-description-function' is non-nil, this function will be
  6450. called with the link target, and the result will be the default
  6451. link description.
  6452. If the LINK-LOCATION parameter is non-nil, this value will be
  6453. used as the link location instead of reading one interactively."
  6454. (interactive "P")
  6455. (let* ((wcf (current-window-configuration))
  6456. (region (if (org-region-active-p)
  6457. (buffer-substring (region-beginning) (region-end))))
  6458. (remove (and region (list (region-beginning) (region-end))))
  6459. (desc region)
  6460. tmphist ; byte-compile incorrectly complains about this
  6461. (link link-location)
  6462. entry file)
  6463. (cond
  6464. (link-location) ; specified by arg, just use it.
  6465. ((org-in-regexp org-bracket-link-regexp 1)
  6466. ;; We do have a link at point, and we are going to edit it.
  6467. (setq remove (list (match-beginning 0) (match-end 0)))
  6468. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6469. (setq link (read-string "Link: "
  6470. (org-link-unescape
  6471. (org-match-string-no-properties 1)))))
  6472. ((or (org-in-regexp org-angle-link-re)
  6473. (org-in-regexp org-plain-link-re))
  6474. ;; Convert to bracket link
  6475. (setq remove (list (match-beginning 0) (match-end 0))
  6476. link (read-string "Link: "
  6477. (org-remove-angle-brackets (match-string 0)))))
  6478. ((member complete-file '((4) (16)))
  6479. ;; Completing read for file names.
  6480. (setq file (read-file-name "File: "))
  6481. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6482. (pwd1 (file-name-as-directory (abbreviate-file-name
  6483. (expand-file-name ".")))))
  6484. (cond
  6485. ((equal complete-file '(16))
  6486. (setq link (org-make-link
  6487. "file:"
  6488. (abbreviate-file-name (expand-file-name file)))))
  6489. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6490. (setq link (org-make-link "file:" (match-string 1 file))))
  6491. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6492. (expand-file-name file))
  6493. (setq link (org-make-link
  6494. "file:" (match-string 1 (expand-file-name file)))))
  6495. (t (setq link (org-make-link "file:" file))))))
  6496. (t
  6497. ;; Read link, with completion for stored links.
  6498. (with-output-to-temp-buffer "*Org Links*"
  6499. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6500. (when org-stored-links
  6501. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6502. (princ (mapconcat
  6503. (lambda (x)
  6504. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6505. (reverse org-stored-links) "\n"))))
  6506. (let ((cw (selected-window)))
  6507. (select-window (get-buffer-window "*Org Links*"))
  6508. (setq truncate-lines t)
  6509. (org-fit-window-to-buffer)
  6510. (select-window cw))
  6511. ;; Fake a link history, containing the stored links.
  6512. (setq tmphist (append (mapcar 'car org-stored-links)
  6513. org-insert-link-history))
  6514. (unwind-protect
  6515. (setq link
  6516. (let ((org-completion-use-ido nil))
  6517. (org-completing-read
  6518. "Link: "
  6519. (append
  6520. (mapcar (lambda (x) (list (concat (car x) ":")))
  6521. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6522. (mapcar (lambda (x) (list (concat x ":")))
  6523. org-link-types))
  6524. nil nil nil
  6525. 'tmphist
  6526. (or (car (car org-stored-links))))))
  6527. (set-window-configuration wcf)
  6528. (kill-buffer "*Org Links*"))
  6529. (setq entry (assoc link org-stored-links))
  6530. (or entry (push link org-insert-link-history))
  6531. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6532. (not org-keep-stored-link-after-insertion))
  6533. (setq org-stored-links (delq (assoc link org-stored-links)
  6534. org-stored-links)))
  6535. (setq desc (or desc (nth 1 entry)))))
  6536. (if (string-match org-plain-link-re link)
  6537. ;; URL-like link, normalize the use of angular brackets.
  6538. (setq link (org-make-link (org-remove-angle-brackets link))))
  6539. ;; Check if we are linking to the current file with a search option
  6540. ;; If yes, simplify the link by using only the search option.
  6541. (when (and buffer-file-name
  6542. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6543. (let* ((path (match-string 1 link))
  6544. (case-fold-search nil)
  6545. (search (match-string 2 link)))
  6546. (save-match-data
  6547. (if (equal (file-truename buffer-file-name) (file-truename path))
  6548. ;; We are linking to this same file, with a search option
  6549. (setq link search)))))
  6550. ;; Check if we can/should use a relative path. If yes, simplify the link
  6551. (when (string-match "^file:\\(.*\\)" link)
  6552. (let* ((path (match-string 1 link))
  6553. (origpath path)
  6554. (case-fold-search nil))
  6555. (cond
  6556. ((or (eq org-link-file-path-type 'absolute)
  6557. (equal complete-file '(16)))
  6558. (setq path (abbreviate-file-name (expand-file-name path))))
  6559. ((eq org-link-file-path-type 'noabbrev)
  6560. (setq path (expand-file-name path)))
  6561. ((eq org-link-file-path-type 'relative)
  6562. (setq path (file-relative-name path)))
  6563. (t
  6564. (save-match-data
  6565. (if (string-match (concat "^" (regexp-quote
  6566. (file-name-as-directory
  6567. (expand-file-name "."))))
  6568. (expand-file-name path))
  6569. ;; We are linking a file with relative path name.
  6570. (setq path (substring (expand-file-name path)
  6571. (match-end 0)))
  6572. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6573. (setq link (concat "file:" path))
  6574. (if (equal desc origpath)
  6575. (setq desc path))))
  6576. (if org-make-link-description-function
  6577. (setq desc (funcall org-make-link-description-function link desc)))
  6578. (setq desc (read-string "Description: " desc))
  6579. (unless (string-match "\\S-" desc) (setq desc nil))
  6580. (if remove (apply 'delete-region remove))
  6581. (insert (org-make-link-string link desc))))
  6582. (defun org-completing-read (&rest args)
  6583. "Completing-read with SPACE being a normal character."
  6584. (let ((minibuffer-local-completion-map
  6585. (copy-keymap minibuffer-local-completion-map)))
  6586. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6587. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6588. (apply 'org-ido-completing-read args)))
  6589. (defun org-completing-read-no-ido (&rest args)
  6590. (let (org-completion-use-ido)
  6591. (apply 'org-completing-read args)))
  6592. (defun org-ido-completing-read (&rest args)
  6593. "Completing-read using `ido-mode' speedups if available"
  6594. (if (and org-completion-use-ido
  6595. (fboundp 'ido-completing-read)
  6596. (boundp 'ido-mode) ido-mode
  6597. (listp (second args)))
  6598. (let ((ido-enter-matching-directory nil))
  6599. (apply 'ido-completing-read (concat (car args))
  6600. (mapcar (lambda (x) (car x)) (nth 1 args))
  6601. (cddr args)))
  6602. (apply 'completing-read args)))
  6603. (defun org-extract-attributes (s)
  6604. "Extract the attributes cookie from a string and set as text property."
  6605. (let (a attr (start 0) key value)
  6606. (save-match-data
  6607. (when (string-match "{{\\([^}]+\\)}}$" s)
  6608. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6609. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6610. (setq key (match-string 1 a) value (match-string 2 a)
  6611. start (match-end 0)
  6612. attr (plist-put attr (intern key) value))))
  6613. (org-add-props s nil 'org-attr attr))
  6614. s))
  6615. (defun org-extract-attributes-from-string (tag)
  6616. (let (key value attr)
  6617. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6618. (setq key (match-string 1 tag) value (match-string 2 tag)
  6619. tag (replace-match "" t t tag)
  6620. attr (plist-put attr (intern key) value)))
  6621. (cons tag attr)))
  6622. (defun org-attributes-to-string (plist)
  6623. "Format a property list into an HTML attribute list."
  6624. (let ((s "") key value)
  6625. (while plist
  6626. (setq key (pop plist) value (pop plist))
  6627. (and value
  6628. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6629. s))
  6630. ;;; Opening/following a link
  6631. (defvar org-link-search-failed nil)
  6632. (defun org-next-link ()
  6633. "Move forward to the next link.
  6634. If the link is in hidden text, expose it."
  6635. (interactive)
  6636. (when (and org-link-search-failed (eq this-command last-command))
  6637. (goto-char (point-min))
  6638. (message "Link search wrapped back to beginning of buffer"))
  6639. (setq org-link-search-failed nil)
  6640. (let* ((pos (point))
  6641. (ct (org-context))
  6642. (a (assoc :link ct)))
  6643. (if a (goto-char (nth 2 a)))
  6644. (if (re-search-forward org-any-link-re nil t)
  6645. (progn
  6646. (goto-char (match-beginning 0))
  6647. (if (org-invisible-p) (org-show-context)))
  6648. (goto-char pos)
  6649. (setq org-link-search-failed t)
  6650. (error "No further link found"))))
  6651. (defun org-previous-link ()
  6652. "Move backward to the previous link.
  6653. If the link is in hidden text, expose it."
  6654. (interactive)
  6655. (when (and org-link-search-failed (eq this-command last-command))
  6656. (goto-char (point-max))
  6657. (message "Link search wrapped back to end of buffer"))
  6658. (setq org-link-search-failed nil)
  6659. (let* ((pos (point))
  6660. (ct (org-context))
  6661. (a (assoc :link ct)))
  6662. (if a (goto-char (nth 1 a)))
  6663. (if (re-search-backward org-any-link-re nil t)
  6664. (progn
  6665. (goto-char (match-beginning 0))
  6666. (if (org-invisible-p) (org-show-context)))
  6667. (goto-char pos)
  6668. (setq org-link-search-failed t)
  6669. (error "No further link found"))))
  6670. (defun org-translate-link (s)
  6671. "Translate a link string if a translation function has been defined."
  6672. (if (and org-link-translation-function
  6673. (fboundp org-link-translation-function)
  6674. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6675. (progn
  6676. (setq s (funcall org-link-translation-function
  6677. (match-string 1) (match-string 2)))
  6678. (concat (car s) ":" (cdr s)))
  6679. s))
  6680. (defun org-translate-link-from-planner (type path)
  6681. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6682. This is still an experimental function, your mileage may vary."
  6683. (cond
  6684. ((member type '("http" "https" "news" "ftp"))
  6685. ;; standard Internet links are the same.
  6686. nil)
  6687. ((and (equal type "irc") (string-match "^//" path))
  6688. ;; Planner has two / at the beginning of an irc link, we have 1.
  6689. ;; We should have zero, actually....
  6690. (setq path (substring path 1)))
  6691. ((and (equal type "lisp") (string-match "^/" path))
  6692. ;; Planner has a slash, we do not.
  6693. (setq type "elisp" path (substring path 1)))
  6694. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6695. ;; A typical message link. Planner has the id after the fina slash,
  6696. ;; we separate it with a hash mark
  6697. (setq path (concat (match-string 1 path) "#"
  6698. (org-remove-angle-brackets (match-string 2 path)))))
  6699. )
  6700. (cons type path))
  6701. (defun org-find-file-at-mouse (ev)
  6702. "Open file link or URL at mouse."
  6703. (interactive "e")
  6704. (mouse-set-point ev)
  6705. (org-open-at-point 'in-emacs))
  6706. (defun org-open-at-mouse (ev)
  6707. "Open file link or URL at mouse."
  6708. (interactive "e")
  6709. (mouse-set-point ev)
  6710. (if (eq major-mode 'org-agenda-mode)
  6711. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6712. (org-open-at-point))
  6713. (defvar org-window-config-before-follow-link nil
  6714. "The window configuration before following a link.
  6715. This is saved in case the need arises to restore it.")
  6716. (defvar org-open-link-marker (make-marker)
  6717. "Marker pointing to the location where `org-open-at-point; was called.")
  6718. ;;;###autoload
  6719. (defun org-open-at-point-global ()
  6720. "Follow a link like Org-mode does.
  6721. This command can be called in any mode to follow a link that has
  6722. Org-mode syntax."
  6723. (interactive)
  6724. (org-run-like-in-org-mode 'org-open-at-point))
  6725. ;;;###autoload
  6726. (defun org-open-link-from-string (s &optional arg)
  6727. "Open a link in the string S, as if it was in Org-mode."
  6728. (interactive "sLink: \nP")
  6729. (let ((reference-buffer (current-buffer)))
  6730. (with-temp-buffer
  6731. (let ((org-inhibit-startup t))
  6732. (org-mode)
  6733. (insert s)
  6734. (goto-char (point-min))
  6735. (org-open-at-point arg reference-buffer)))))
  6736. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6737. "Open link at or after point.
  6738. If there is no link at point, this function will search forward up to
  6739. the end of the current line.
  6740. Normally, files will be opened by an appropriate application. If the
  6741. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6742. With a double prefix argument, try to open outside of Emacs, in the
  6743. application the system uses for this file type."
  6744. (interactive "P")
  6745. (org-load-modules-maybe)
  6746. (move-marker org-open-link-marker (point))
  6747. (setq org-window-config-before-follow-link (current-window-configuration))
  6748. (org-remove-occur-highlights nil nil t)
  6749. (cond
  6750. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6751. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6752. (org-footnote-action))
  6753. (t
  6754. (let (type path link line search (pos (point)))
  6755. (catch 'match
  6756. (save-excursion
  6757. (skip-chars-forward "^]\n\r")
  6758. (when (org-in-regexp org-bracket-link-regexp)
  6759. (setq link (org-extract-attributes
  6760. (org-link-unescape (org-match-string-no-properties 1))))
  6761. (while (string-match " *\n *" link)
  6762. (setq link (replace-match " " t t link)))
  6763. (setq link (org-link-expand-abbrev link))
  6764. (cond
  6765. ((or (file-name-absolute-p link)
  6766. (string-match "^\\.\\.?/" link))
  6767. (setq type "file" path link))
  6768. ((string-match org-link-re-with-space3 link)
  6769. (setq type (match-string 1 link) path (match-string 2 link)))
  6770. (t (setq type "thisfile" path link)))
  6771. (throw 'match t)))
  6772. (when (get-text-property (point) 'org-linked-text)
  6773. (setq type "thisfile"
  6774. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6775. (1+ (point)) (point))
  6776. path (buffer-substring
  6777. (previous-single-property-change pos 'org-linked-text)
  6778. (next-single-property-change pos 'org-linked-text)))
  6779. (throw 'match t))
  6780. (save-excursion
  6781. (when (or (org-in-regexp org-angle-link-re)
  6782. (org-in-regexp org-plain-link-re))
  6783. (setq type (match-string 1) path (match-string 2))
  6784. (throw 'match t)))
  6785. (save-excursion
  6786. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6787. (setq type "tags"
  6788. path (match-string 1))
  6789. (while (string-match ":" path)
  6790. (setq path (replace-match "+" t t path)))
  6791. (throw 'match t)))
  6792. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6793. (setq type "tree-match"
  6794. path (match-string 1))
  6795. (throw 'match t)))
  6796. (unless path
  6797. (error "No link found"))
  6798. ;; switch back to reference buffer
  6799. ;; needed when if called in a temporary buffer through
  6800. ;; org-open-link-from-string
  6801. (and reference-buffer (switch-to-buffer reference-buffer))
  6802. ;; Remove any trailing spaces in path
  6803. (if (string-match " +\\'" path)
  6804. (setq path (replace-match "" t t path)))
  6805. (if (and org-link-translation-function
  6806. (fboundp org-link-translation-function))
  6807. ;; Check if we need to translate the link
  6808. (let ((tmp (funcall org-link-translation-function type path)))
  6809. (setq type (car tmp) path (cdr tmp))))
  6810. (cond
  6811. ((assoc type org-link-protocols)
  6812. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6813. ((equal type "mailto")
  6814. (let ((cmd (car org-link-mailto-program))
  6815. (args (cdr org-link-mailto-program)) args1
  6816. (address path) (subject "") a)
  6817. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6818. (setq address (match-string 1 path)
  6819. subject (org-link-escape (match-string 2 path))))
  6820. (while args
  6821. (cond
  6822. ((not (stringp (car args))) (push (pop args) args1))
  6823. (t (setq a (pop args))
  6824. (if (string-match "%a" a)
  6825. (setq a (replace-match address t t a)))
  6826. (if (string-match "%s" a)
  6827. (setq a (replace-match subject t t a)))
  6828. (push a args1))))
  6829. (apply cmd (nreverse args1))))
  6830. ((member type '("http" "https" "ftp" "news"))
  6831. (browse-url (concat type ":" (org-link-escape
  6832. path org-link-escape-chars-browser))))
  6833. ((member type '("message"))
  6834. (browse-url (concat type ":" path)))
  6835. ((string= type "tags")
  6836. (org-tags-view in-emacs path))
  6837. ((string= type "thisfile")
  6838. (if in-emacs
  6839. (switch-to-buffer-other-window
  6840. (org-get-buffer-for-internal-link (current-buffer)))
  6841. (org-mark-ring-push))
  6842. (let ((cmd `(org-link-search
  6843. ,path
  6844. ,(cond ((equal in-emacs '(4)) 'occur)
  6845. ((equal in-emacs '(16)) 'org-occur)
  6846. (t nil))
  6847. ,pos)))
  6848. (condition-case nil (eval cmd)
  6849. (error (progn (widen) (eval cmd))))))
  6850. ((string= type "tree-match")
  6851. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6852. ((string= type "file")
  6853. (if (string-match "::\\([0-9]+\\)\\'" path)
  6854. (setq line (string-to-number (match-string 1 path))
  6855. path (substring path 0 (match-beginning 0)))
  6856. (if (string-match "::\\(.+\\)\\'" path)
  6857. (setq search (match-string 1 path)
  6858. path (substring path 0 (match-beginning 0)))))
  6859. (if (string-match "[*?{]" (file-name-nondirectory path))
  6860. (dired path)
  6861. (org-open-file path in-emacs line search)))
  6862. ((string= type "news")
  6863. (require 'org-gnus)
  6864. (org-gnus-follow-link path))
  6865. ((string= type "shell")
  6866. (let ((cmd path))
  6867. (if (or (not org-confirm-shell-link-function)
  6868. (funcall org-confirm-shell-link-function
  6869. (format "Execute \"%s\" in shell? "
  6870. (org-add-props cmd nil
  6871. 'face 'org-warning))))
  6872. (progn
  6873. (message "Executing %s" cmd)
  6874. (shell-command cmd))
  6875. (error "Abort"))))
  6876. ((string= type "elisp")
  6877. (let ((cmd path))
  6878. (if (or (not org-confirm-elisp-link-function)
  6879. (funcall org-confirm-elisp-link-function
  6880. (format "Execute \"%s\" as elisp? "
  6881. (org-add-props cmd nil
  6882. 'face 'org-warning))))
  6883. (message "%s => %s" cmd
  6884. (if (equal (string-to-char cmd) ?\()
  6885. (eval (read cmd))
  6886. (call-interactively (read cmd))))
  6887. (error "Abort"))))
  6888. (t
  6889. (browse-url-at-point))))))
  6890. (move-marker org-open-link-marker nil)
  6891. (run-hook-with-args 'org-follow-link-hook))
  6892. ;;;; Time estimates
  6893. (defun org-get-effort (&optional pom)
  6894. "Get the effort estimate for the current entry."
  6895. (org-entry-get pom org-effort-property))
  6896. ;;; File search
  6897. (defvar org-create-file-search-functions nil
  6898. "List of functions to construct the right search string for a file link.
  6899. These functions are called in turn with point at the location to
  6900. which the link should point.
  6901. A function in the hook should first test if it would like to
  6902. handle this file type, for example by checking the major-mode or
  6903. the file extension. If it decides not to handle this file, it
  6904. should just return nil to give other functions a chance. If it
  6905. does handle the file, it must return the search string to be used
  6906. when following the link. The search string will be part of the
  6907. file link, given after a double colon, and `org-open-at-point'
  6908. will automatically search for it. If special measures must be
  6909. taken to make the search successful, another function should be
  6910. added to the companion hook `org-execute-file-search-functions',
  6911. which see.
  6912. A function in this hook may also use `setq' to set the variable
  6913. `description' to provide a suggestion for the descriptive text to
  6914. be used for this link when it gets inserted into an Org-mode
  6915. buffer with \\[org-insert-link].")
  6916. (defvar org-execute-file-search-functions nil
  6917. "List of functions to execute a file search triggered by a link.
  6918. Functions added to this hook must accept a single argument, the
  6919. search string that was part of the file link, the part after the
  6920. double colon. The function must first check if it would like to
  6921. handle this search, for example by checking the major-mode or the
  6922. file extension. If it decides not to handle this search, it
  6923. should just return nil to give other functions a chance. If it
  6924. does handle the search, it must return a non-nil value to keep
  6925. other functions from trying.
  6926. Each function can access the current prefix argument through the
  6927. variable `current-prefix-argument'. Note that a single prefix is
  6928. used to force opening a link in Emacs, so it may be good to only
  6929. use a numeric or double prefix to guide the search function.
  6930. In case this is needed, a function in this hook can also restore
  6931. the window configuration before `org-open-at-point' was called using:
  6932. (set-window-configuration org-window-config-before-follow-link)")
  6933. (defun org-link-search (s &optional type avoid-pos)
  6934. "Search for a link search option.
  6935. If S is surrounded by forward slashes, it is interpreted as a
  6936. regular expression. In org-mode files, this will create an `org-occur'
  6937. sparse tree. In ordinary files, `occur' will be used to list matches.
  6938. If the current buffer is in `dired-mode', grep will be used to search
  6939. in all files. If AVOID-POS is given, ignore matches near that position."
  6940. (let ((case-fold-search t)
  6941. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6942. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6943. (append '(("") (" ") ("\t") ("\n"))
  6944. org-emphasis-alist)
  6945. "\\|") "\\)"))
  6946. (pos (point))
  6947. (pre nil) (post nil)
  6948. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6949. (cond
  6950. ;; First check if there are any special
  6951. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6952. ;; Now try the builtin stuff
  6953. ((save-excursion
  6954. (goto-char (point-min))
  6955. (and
  6956. (re-search-forward
  6957. (concat "<<" (regexp-quote s0) ">>") nil t)
  6958. (setq type 'dedicated
  6959. pos (match-beginning 0))))
  6960. ;; There is an exact target for this
  6961. (goto-char pos))
  6962. ((and (string-match "^(\\(.*\\))$" s0)
  6963. (save-excursion
  6964. (goto-char (point-min))
  6965. (and
  6966. (re-search-forward
  6967. (concat "[^[]" (regexp-quote
  6968. (format org-coderef-label-format
  6969. (match-string 1 s0))))
  6970. nil t)
  6971. (setq type 'dedicated
  6972. pos (1+ (match-beginning 0))))))
  6973. ;; There is a coderef target for this
  6974. (goto-char pos))
  6975. ((string-match "^/\\(.*\\)/$" s)
  6976. ;; A regular expression
  6977. (cond
  6978. ((org-mode-p)
  6979. (org-occur (match-string 1 s)))
  6980. ;;((eq major-mode 'dired-mode)
  6981. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6982. (t (org-do-occur (match-string 1 s)))))
  6983. (t
  6984. ;; A normal search strings
  6985. (when (equal (string-to-char s) ?*)
  6986. ;; Anchor on headlines, post may include tags.
  6987. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6988. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6989. s (substring s 1)))
  6990. (remove-text-properties
  6991. 0 (length s)
  6992. '(face nil mouse-face nil keymap nil fontified nil) s)
  6993. ;; Make a series of regular expressions to find a match
  6994. (setq words (org-split-string s "[ \n\r\t]+")
  6995. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6996. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6997. "\\)" markers)
  6998. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6999. re2a (concat "[ \t\r\n]" re2a_)
  7000. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7001. re4 (concat "[^a-zA-Z_]" re4_)
  7002. re1 (concat pre re2 post)
  7003. re3 (concat pre (if pre re4_ re4) post)
  7004. re5 (concat pre ".*" re4)
  7005. re2 (concat pre re2)
  7006. re2a (concat pre (if pre re2a_ re2a))
  7007. re4 (concat pre (if pre re4_ re4))
  7008. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7009. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7010. re5 "\\)"
  7011. ))
  7012. (cond
  7013. ((eq type 'org-occur) (org-occur reall))
  7014. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7015. (t (goto-char (point-min))
  7016. (setq type 'fuzzy)
  7017. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7018. (org-search-not-self 1 re1 nil t)
  7019. (org-search-not-self 1 re2 nil t)
  7020. (org-search-not-self 1 re2a nil t)
  7021. (org-search-not-self 1 re3 nil t)
  7022. (org-search-not-self 1 re4 nil t)
  7023. (org-search-not-self 1 re5 nil t)
  7024. )
  7025. (goto-char (match-beginning 1))
  7026. (goto-char pos)
  7027. (error "No match")))))
  7028. (t
  7029. ;; Normal string-search
  7030. (goto-char (point-min))
  7031. (if (search-forward s nil t)
  7032. (goto-char (match-beginning 0))
  7033. (error "No match"))))
  7034. (and (org-mode-p) (org-show-context 'link-search))
  7035. type))
  7036. (defun org-search-not-self (group &rest args)
  7037. "Execute `re-search-forward', but only accept matches that do not
  7038. enclose the position of `org-open-link-marker'."
  7039. (let ((m org-open-link-marker))
  7040. (catch 'exit
  7041. (while (apply 're-search-forward args)
  7042. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7043. (goto-char (match-end group))
  7044. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7045. (> (match-beginning 0) (marker-position m))
  7046. (< (match-end 0) (marker-position m)))
  7047. (save-match-data
  7048. (or (not (org-in-regexp
  7049. org-bracket-link-analytic-regexp 1))
  7050. (not (match-end 4)) ; no description
  7051. (and (<= (match-beginning 4) (point))
  7052. (>= (match-end 4) (point))))))
  7053. (throw 'exit (point))))))))
  7054. (defun org-get-buffer-for-internal-link (buffer)
  7055. "Return a buffer to be used for displaying the link target of internal links."
  7056. (cond
  7057. ((not org-display-internal-link-with-indirect-buffer)
  7058. buffer)
  7059. ((string-match "(Clone)$" (buffer-name buffer))
  7060. (message "Buffer is already a clone, not making another one")
  7061. ;; we also do not modify visibility in this case
  7062. buffer)
  7063. (t ; make a new indirect buffer for displaying the link
  7064. (let* ((bn (buffer-name buffer))
  7065. (ibn (concat bn "(Clone)"))
  7066. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7067. (with-current-buffer ib (org-overview))
  7068. ib))))
  7069. (defun org-do-occur (regexp &optional cleanup)
  7070. "Call the Emacs command `occur'.
  7071. If CLEANUP is non-nil, remove the printout of the regular expression
  7072. in the *Occur* buffer. This is useful if the regex is long and not useful
  7073. to read."
  7074. (occur regexp)
  7075. (when cleanup
  7076. (let ((cwin (selected-window)) win beg end)
  7077. (when (setq win (get-buffer-window "*Occur*"))
  7078. (select-window win))
  7079. (goto-char (point-min))
  7080. (when (re-search-forward "match[a-z]+" nil t)
  7081. (setq beg (match-end 0))
  7082. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7083. (setq end (1- (match-beginning 0)))))
  7084. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7085. (goto-char (point-min))
  7086. (select-window cwin))))
  7087. ;;; The mark ring for links jumps
  7088. (defvar org-mark-ring nil
  7089. "Mark ring for positions before jumps in Org-mode.")
  7090. (defvar org-mark-ring-last-goto nil
  7091. "Last position in the mark ring used to go back.")
  7092. ;; Fill and close the ring
  7093. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7094. (loop for i from 1 to org-mark-ring-length do
  7095. (push (make-marker) org-mark-ring))
  7096. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7097. org-mark-ring)
  7098. (defun org-mark-ring-push (&optional pos buffer)
  7099. "Put the current position or POS into the mark ring and rotate it."
  7100. (interactive)
  7101. (setq pos (or pos (point)))
  7102. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7103. (move-marker (car org-mark-ring)
  7104. (or pos (point))
  7105. (or buffer (current-buffer)))
  7106. (message "%s"
  7107. (substitute-command-keys
  7108. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7109. (defun org-mark-ring-goto (&optional n)
  7110. "Jump to the previous position in the mark ring.
  7111. With prefix arg N, jump back that many stored positions. When
  7112. called several times in succession, walk through the entire ring.
  7113. Org-mode commands jumping to a different position in the current file,
  7114. or to another Org-mode file, automatically push the old position
  7115. onto the ring."
  7116. (interactive "p")
  7117. (let (p m)
  7118. (if (eq last-command this-command)
  7119. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7120. (setq p org-mark-ring))
  7121. (setq org-mark-ring-last-goto p)
  7122. (setq m (car p))
  7123. (switch-to-buffer (marker-buffer m))
  7124. (goto-char m)
  7125. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7126. (defun org-remove-angle-brackets (s)
  7127. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7128. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7129. s)
  7130. (defun org-add-angle-brackets (s)
  7131. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7132. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7133. s)
  7134. (defun org-remove-double-quotes (s)
  7135. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7136. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7137. s)
  7138. ;;; Following specific links
  7139. (defun org-follow-timestamp-link ()
  7140. (cond
  7141. ((org-at-date-range-p t)
  7142. (let ((org-agenda-start-on-weekday)
  7143. (t1 (match-string 1))
  7144. (t2 (match-string 2)))
  7145. (setq t1 (time-to-days (org-time-string-to-time t1))
  7146. t2 (time-to-days (org-time-string-to-time t2)))
  7147. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7148. ((org-at-timestamp-p t)
  7149. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7150. (substring (match-string 1) 0 10)))
  7151. 1))
  7152. (t (error "This should not happen"))))
  7153. ;;; Following file links
  7154. (defvar org-wait nil)
  7155. (defun org-open-file (path &optional in-emacs line search)
  7156. "Open the file at PATH.
  7157. First, this expands any special file name abbreviations. Then the
  7158. configuration variable `org-file-apps' is checked if it contains an
  7159. entry for this file type, and if yes, the corresponding command is launched.
  7160. If no application is found, Emacs simply visits the file.
  7161. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7162. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7163. and o use an external application to visit the file.
  7164. Optional LINE specifies a line to go to, optional SEARCH a string to
  7165. search for. If LINE or SEARCH is given, the file will always be
  7166. opened in Emacs.
  7167. If the file does not exist, an error is thrown."
  7168. (setq in-emacs (or in-emacs line search))
  7169. (let* ((file (if (equal path "")
  7170. buffer-file-name
  7171. (substitute-in-file-name (expand-file-name path))))
  7172. (apps (append org-file-apps (org-default-apps)))
  7173. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7174. (dirp (if remp nil (file-directory-p file)))
  7175. (file (if (and dirp org-open-directory-means-index-dot-org)
  7176. (concat (file-name-as-directory file) "index.org")
  7177. file))
  7178. (a-m-a-p (assq 'auto-mode apps))
  7179. (dfile (downcase file))
  7180. (old-buffer (current-buffer))
  7181. (old-pos (point))
  7182. (old-mode major-mode)
  7183. ext cmd)
  7184. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7185. (setq ext (match-string 1 dfile))
  7186. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7187. (setq ext (match-string 1 dfile))))
  7188. (cond
  7189. ((equal in-emacs '(16))
  7190. (setq cmd (cdr (assoc 'system apps))))
  7191. (in-emacs (setq cmd 'emacs))
  7192. (t
  7193. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7194. (and dirp (cdr (assoc 'directory apps)))
  7195. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7196. 'string-match)
  7197. (cdr (assoc ext apps))
  7198. (cdr (assoc t apps))))))
  7199. (when (eq cmd 'system)
  7200. (setq cmd (cdr (assoc 'system apps))))
  7201. (when (eq cmd 'default)
  7202. (setq cmd (cdr (assoc t apps))))
  7203. (when (eq cmd 'mailcap)
  7204. (require 'mailcap)
  7205. (mailcap-parse-mailcaps)
  7206. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7207. (command (mailcap-mime-info mime-type)))
  7208. (if (stringp command)
  7209. (setq cmd command)
  7210. (setq cmd 'emacs))))
  7211. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7212. (not (file-exists-p file))
  7213. (not org-open-non-existing-files))
  7214. (error "No such file: %s" file))
  7215. (cond
  7216. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7217. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7218. (while (string-match "['\"]%s['\"]" cmd)
  7219. (setq cmd (replace-match "%s" t t cmd)))
  7220. (while (string-match "%s" cmd)
  7221. (setq cmd (replace-match
  7222. (save-match-data
  7223. (shell-quote-argument
  7224. (convert-standard-filename file)))
  7225. t t cmd)))
  7226. (save-window-excursion
  7227. (start-process-shell-command cmd nil cmd)
  7228. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7229. ))
  7230. ((or (stringp cmd)
  7231. (eq cmd 'emacs))
  7232. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7233. (widen)
  7234. (if line (goto-line line)
  7235. (if search (org-link-search search))))
  7236. ((consp cmd)
  7237. (let ((file (convert-standard-filename file)))
  7238. (eval cmd)))
  7239. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7240. (and (org-mode-p) (eq old-mode 'org-mode)
  7241. (or (not (equal old-buffer (current-buffer)))
  7242. (not (equal old-pos (point))))
  7243. (org-mark-ring-push old-pos old-buffer))))
  7244. (defun org-default-apps ()
  7245. "Return the default applications for this operating system."
  7246. (cond
  7247. ((eq system-type 'darwin)
  7248. org-file-apps-defaults-macosx)
  7249. ((eq system-type 'windows-nt)
  7250. org-file-apps-defaults-windowsnt)
  7251. (t org-file-apps-defaults-gnu)))
  7252. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7253. "Convert extensions to regular expressions in the cars of LIST.
  7254. Also, weed out any non-string entries, because the return value is used
  7255. only for regexp matching.
  7256. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7257. point to the symbol `emacs', indicating that the file should
  7258. be opened in Emacs."
  7259. (append
  7260. (delq nil
  7261. (mapcar (lambda (x)
  7262. (if (not (stringp (car x)))
  7263. nil
  7264. (if (string-match "\\W" (car x))
  7265. x
  7266. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7267. list))
  7268. (if add-auto-mode
  7269. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7270. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7271. (defun org-file-remote-p (file)
  7272. "Test whether FILE specifies a location on a remote system.
  7273. Return non-nil if the location is indeed remote.
  7274. For example, the filename \"/user@host:/foo\" specifies a location
  7275. on the system \"/user@host:\"."
  7276. (cond ((fboundp 'file-remote-p)
  7277. (file-remote-p file))
  7278. ((fboundp 'tramp-handle-file-remote-p)
  7279. (tramp-handle-file-remote-p file))
  7280. ((and (boundp 'ange-ftp-name-format)
  7281. (string-match (car ange-ftp-name-format) file))
  7282. t)
  7283. (t nil)))
  7284. ;;;; Refiling
  7285. (defun org-get-org-file ()
  7286. "Read a filename, with default directory `org-directory'."
  7287. (let ((default (or org-default-notes-file remember-data-file)))
  7288. (read-file-name (format "File name [%s]: " default)
  7289. (file-name-as-directory org-directory)
  7290. default)))
  7291. (defun org-notes-order-reversed-p ()
  7292. "Check if the current file should receive notes in reversed order."
  7293. (cond
  7294. ((not org-reverse-note-order) nil)
  7295. ((eq t org-reverse-note-order) t)
  7296. ((not (listp org-reverse-note-order)) nil)
  7297. (t (catch 'exit
  7298. (let ((all org-reverse-note-order)
  7299. entry)
  7300. (while (setq entry (pop all))
  7301. (if (string-match (car entry) buffer-file-name)
  7302. (throw 'exit (cdr entry))))
  7303. nil)))))
  7304. (defvar org-refile-target-table nil
  7305. "The list of refile targets, created by `org-refile'.")
  7306. (defvar org-agenda-new-buffers nil
  7307. "Buffers created to visit agenda files.")
  7308. (defun org-get-refile-targets (&optional default-buffer)
  7309. "Produce a table with refile targets."
  7310. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7311. targets txt re files f desc descre fast-path-p level)
  7312. (message "Getting targets...")
  7313. (with-current-buffer (or default-buffer (current-buffer))
  7314. (while (setq entry (pop entries))
  7315. (setq files (car entry) desc (cdr entry))
  7316. (setq fast-path-p nil)
  7317. (cond
  7318. ((null files) (setq files (list (current-buffer))))
  7319. ((eq files 'org-agenda-files)
  7320. (setq files (org-agenda-files 'unrestricted)))
  7321. ((and (symbolp files) (fboundp files))
  7322. (setq files (funcall files)))
  7323. ((and (symbolp files) (boundp files))
  7324. (setq files (symbol-value files))))
  7325. (if (stringp files) (setq files (list files)))
  7326. (cond
  7327. ((eq (car desc) :tag)
  7328. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7329. ((eq (car desc) :todo)
  7330. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7331. ((eq (car desc) :regexp)
  7332. (setq descre (cdr desc)))
  7333. ((eq (car desc) :level)
  7334. (setq descre (concat "^\\*\\{" (number-to-string
  7335. (if org-odd-levels-only
  7336. (1- (* 2 (cdr desc)))
  7337. (cdr desc)))
  7338. "\\}[ \t]")))
  7339. ((eq (car desc) :maxlevel)
  7340. (setq fast-path-p t)
  7341. (setq descre (concat "^\\*\\{1," (number-to-string
  7342. (if org-odd-levels-only
  7343. (1- (* 2 (cdr desc)))
  7344. (cdr desc)))
  7345. "\\}[ \t]")))
  7346. (t (error "Bad refiling target description %s" desc)))
  7347. (while (setq f (pop files))
  7348. (save-excursion
  7349. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7350. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7351. (setq f (expand-file-name f))
  7352. (if (eq org-refile-use-outline-path 'file)
  7353. (push (list (file-name-nondirectory f) f nil nil) targets))
  7354. (save-excursion
  7355. (save-restriction
  7356. (widen)
  7357. (goto-char (point-min))
  7358. (while (re-search-forward descre nil t)
  7359. (goto-char (point-at-bol))
  7360. (when (looking-at org-complex-heading-regexp)
  7361. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7362. txt (org-link-display-format (match-string 4))
  7363. re (concat "^" (regexp-quote
  7364. (buffer-substring (match-beginning 1)
  7365. (match-end 4)))))
  7366. (if (match-end 5) (setq re (concat re "[ \t]+"
  7367. (regexp-quote
  7368. (match-string 5)))))
  7369. (setq re (concat re "[ \t]*$"))
  7370. (when org-refile-use-outline-path
  7371. (setq txt (mapconcat 'org-protect-slash
  7372. (append
  7373. (if (eq org-refile-use-outline-path 'file)
  7374. (list (file-name-nondirectory
  7375. (buffer-file-name (buffer-base-buffer))))
  7376. (if (eq org-refile-use-outline-path 'full-file-path)
  7377. (list (buffer-file-name (buffer-base-buffer)))))
  7378. (org-get-outline-path fast-path-p level txt)
  7379. (list txt))
  7380. "/")))
  7381. (push (list txt f re (point)) targets))
  7382. (goto-char (point-at-eol))))))))
  7383. (message "Getting targets...done")
  7384. (nreverse targets))))
  7385. (defun org-protect-slash (s)
  7386. (while (string-match "/" s)
  7387. (setq s (replace-match "\\" t t s)))
  7388. s)
  7389. (defvar org-olpa (make-vector 20 nil))
  7390. (defun org-get-outline-path (&optional fastp level heading)
  7391. "Return the outline path to the current entry, as a list."
  7392. (if fastp
  7393. (progn
  7394. (if (> level 19)
  7395. (error "Outline path failure, more than 19 levels."))
  7396. (loop for i from level upto 19 do
  7397. (aset org-olpa i nil))
  7398. (prog1
  7399. (delq nil (append org-olpa nil))
  7400. (aset org-olpa level heading)))
  7401. (let (rtn)
  7402. (save-excursion
  7403. (while (org-up-heading-safe)
  7404. (when (looking-at org-complex-heading-regexp)
  7405. (push (org-match-string-no-properties 4) rtn)))
  7406. rtn))))
  7407. (defvar org-refile-history nil
  7408. "History for refiling operations.")
  7409. (defun org-refile (&optional goto default-buffer)
  7410. "Move the entry at point to another heading.
  7411. The list of target headings is compiled using the information in
  7412. `org-refile-targets', which see. This list is created before each use
  7413. and will therefore always be up-to-date.
  7414. At the target location, the entry is filed as a subitem of the target heading.
  7415. Depending on `org-reverse-note-order', the new subitem will either be the
  7416. first or the last subitem.
  7417. If there is an active region, all entries in that region will be moved.
  7418. However, the region must fulfil the requirement that the first heading
  7419. is the first one sets the top-level of the moved text - at most siblings
  7420. below it are allowed.
  7421. With prefix arg GOTO, the command will only visit the target location,
  7422. not actually move anything.
  7423. With a double prefix `C-u C-u', go to the location where the last refiling
  7424. operation has put the subtree."
  7425. (interactive "P")
  7426. (let* ((cbuf (current-buffer))
  7427. (regionp (org-region-active-p))
  7428. (region-start (and regionp (region-beginning)))
  7429. (region-end (and regionp (region-end)))
  7430. (region-length (and regionp (- region-end region-start)))
  7431. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7432. pos it nbuf file re level reversed)
  7433. (when regionp (goto-char region-start)
  7434. (unless (org-kill-is-subtree-p
  7435. (buffer-substring region-start region-end))
  7436. (error "The region is not a (sequence of) subtree(s)")))
  7437. (if (equal goto '(16))
  7438. (org-refile-goto-last-stored)
  7439. (when (setq it (org-refile-get-location
  7440. (if goto "Goto: " "Refile to: ") default-buffer))
  7441. (setq file (nth 1 it)
  7442. re (nth 2 it)
  7443. pos (nth 3 it))
  7444. (if (and (not goto)
  7445. pos
  7446. (equal (buffer-file-name) file)
  7447. (if regionp
  7448. (and (>= pos region-start)
  7449. (<= pos region-end))
  7450. (and (>= pos (point))
  7451. (< pos (save-excursion
  7452. (org-end-of-subtree t t))))))
  7453. (error "Cannot refile to position inside the tree or region"))
  7454. (setq nbuf (or (find-buffer-visiting file)
  7455. (find-file-noselect file)))
  7456. (if goto
  7457. (progn
  7458. (switch-to-buffer nbuf)
  7459. (goto-char pos)
  7460. (org-show-context 'org-goto))
  7461. (if regionp
  7462. (progn
  7463. (kill-new (buffer-substring region-start region-end))
  7464. (org-save-markers-in-region region-start region-end))
  7465. (org-copy-subtree 1 nil t))
  7466. (save-excursion
  7467. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7468. (find-file-noselect file))))
  7469. (setq reversed (org-notes-order-reversed-p))
  7470. (save-excursion
  7471. (save-restriction
  7472. (widen)
  7473. (if pos
  7474. (progn
  7475. (goto-char pos)
  7476. (looking-at outline-regexp)
  7477. (setq level (org-get-valid-level (funcall outline-level) 1))
  7478. (goto-char
  7479. (if reversed
  7480. (or (outline-next-heading) (point-max))
  7481. (or (save-excursion (outline-get-next-sibling))
  7482. (org-end-of-subtree t t)
  7483. (point-max)))))
  7484. (setq level 1)
  7485. (if (not reversed)
  7486. (goto-char (point-max))
  7487. (goto-char (point-min))
  7488. (or (outline-next-heading) (goto-char (point-max)))))
  7489. (if (not (bolp)) (newline))
  7490. (bookmark-set "org-refile-last-stored")
  7491. (org-paste-subtree level))))
  7492. (if regionp
  7493. (delete-region (point) (+ (point) region-length))
  7494. (org-cut-subtree))
  7495. (setq org-markers-to-move nil)
  7496. (message "Refiled to \"%s\"" (car it)))))))
  7497. (defun org-refile-goto-last-stored ()
  7498. "Go to the location where the last refile was stored."
  7499. (interactive)
  7500. (bookmark-jump "org-refile-last-stored")
  7501. (message "This is the location of the last refile"))
  7502. (defun org-refile-get-location (&optional prompt default-buffer)
  7503. "Prompt the user for a refile location, using PROMPT."
  7504. (let ((org-refile-targets org-refile-targets)
  7505. (org-refile-use-outline-path org-refile-use-outline-path))
  7506. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7507. (unless org-refile-target-table
  7508. (error "No refile targets"))
  7509. (let* ((cbuf (current-buffer))
  7510. (partial-completion-mode nil)
  7511. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7512. (cfunc (if (and org-refile-use-outline-path
  7513. org-outline-path-complete-in-steps)
  7514. 'org-olpath-completing-read
  7515. 'org-ido-completing-read))
  7516. (extra (if org-refile-use-outline-path "/" ""))
  7517. (filename (and cfn (expand-file-name cfn)))
  7518. (tbl (mapcar
  7519. (lambda (x)
  7520. (if (and (not (member org-refile-use-outline-path
  7521. '(file full-file-path)))
  7522. (not (equal filename (nth 1 x))))
  7523. (cons (concat (car x) extra " ("
  7524. (file-name-nondirectory (nth 1 x)) ")")
  7525. (cdr x))
  7526. (cons (concat (car x) extra) (cdr x))))
  7527. org-refile-target-table))
  7528. (completion-ignore-case t))
  7529. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7530. tbl)))
  7531. (defun org-olpath-completing-read (prompt collection &rest args)
  7532. "Read an outline path like a file name."
  7533. (let ((thetable collection)
  7534. (org-completion-use-ido nil)) ; does not work with ido.
  7535. (apply
  7536. 'org-ido-completing-read prompt
  7537. (lambda (string predicate &optional flag)
  7538. (let (rtn r f (l (length string)))
  7539. (cond
  7540. ((eq flag nil)
  7541. ;; try completion
  7542. (try-completion string thetable))
  7543. ((eq flag t)
  7544. ;; all-completions
  7545. (setq rtn (all-completions string thetable predicate))
  7546. (mapcar
  7547. (lambda (x)
  7548. (setq r (substring x l))
  7549. (if (string-match " ([^)]*)$" x)
  7550. (setq f (match-string 0 x))
  7551. (setq f ""))
  7552. (if (string-match "/" r)
  7553. (concat string (substring r 0 (match-end 0)) f)
  7554. x))
  7555. rtn))
  7556. ((eq flag 'lambda)
  7557. ;; exact match?
  7558. (assoc string thetable)))
  7559. ))
  7560. args)))
  7561. ;;;; Dynamic blocks
  7562. (defun org-find-dblock (name)
  7563. "Find the first dynamic block with name NAME in the buffer.
  7564. If not found, stay at current position and return nil."
  7565. (let (pos)
  7566. (save-excursion
  7567. (goto-char (point-min))
  7568. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7569. nil t)
  7570. (match-beginning 0))))
  7571. (if pos (goto-char pos))
  7572. pos))
  7573. (defconst org-dblock-start-re
  7574. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7575. "Matches the startline of a dynamic block, with parameters.")
  7576. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7577. "Matches the end of a dynamic block.")
  7578. (defun org-create-dblock (plist)
  7579. "Create a dynamic block section, with parameters taken from PLIST.
  7580. PLIST must contain a :name entry which is used as name of the block."
  7581. (unless (bolp) (newline))
  7582. (let ((name (plist-get plist :name)))
  7583. (insert "#+BEGIN: " name)
  7584. (while plist
  7585. (if (eq (car plist) :name)
  7586. (setq plist (cddr plist))
  7587. (insert " " (prin1-to-string (pop plist)))))
  7588. (insert "\n\n#+END:\n")
  7589. (beginning-of-line -2)))
  7590. (defun org-prepare-dblock ()
  7591. "Prepare dynamic block for refresh.
  7592. This empties the block, puts the cursor at the insert position and returns
  7593. the property list including an extra property :name with the block name."
  7594. (unless (looking-at org-dblock-start-re)
  7595. (error "Not at a dynamic block"))
  7596. (let* ((begdel (1+ (match-end 0)))
  7597. (name (org-no-properties (match-string 1)))
  7598. (params (append (list :name name)
  7599. (read (concat "(" (match-string 3) ")")))))
  7600. (unless (re-search-forward org-dblock-end-re nil t)
  7601. (error "Dynamic block not terminated"))
  7602. (setq params
  7603. (append params
  7604. (list :content (buffer-substring
  7605. begdel (match-beginning 0)))))
  7606. (delete-region begdel (match-beginning 0))
  7607. (goto-char begdel)
  7608. (open-line 1)
  7609. params))
  7610. (defun org-map-dblocks (&optional command)
  7611. "Apply COMMAND to all dynamic blocks in the current buffer.
  7612. If COMMAND is not given, use `org-update-dblock'."
  7613. (let ((cmd (or command 'org-update-dblock))
  7614. pos)
  7615. (save-excursion
  7616. (goto-char (point-min))
  7617. (while (re-search-forward org-dblock-start-re nil t)
  7618. (goto-char (setq pos (match-beginning 0)))
  7619. (condition-case nil
  7620. (funcall cmd)
  7621. (error (message "Error during update of dynamic block")))
  7622. (goto-char pos)
  7623. (unless (re-search-forward org-dblock-end-re nil t)
  7624. (error "Dynamic block not terminated"))))))
  7625. (defun org-dblock-update (&optional arg)
  7626. "User command for updating dynamic blocks.
  7627. Update the dynamic block at point. With prefix ARG, update all dynamic
  7628. blocks in the buffer."
  7629. (interactive "P")
  7630. (if arg
  7631. (org-update-all-dblocks)
  7632. (or (looking-at org-dblock-start-re)
  7633. (org-beginning-of-dblock))
  7634. (org-update-dblock)))
  7635. (defun org-update-dblock ()
  7636. "Update the dynamic block at point
  7637. This means to empty the block, parse for parameters and then call
  7638. the correct writing function."
  7639. (save-window-excursion
  7640. (let* ((pos (point))
  7641. (line (org-current-line))
  7642. (params (org-prepare-dblock))
  7643. (name (plist-get params :name))
  7644. (cmd (intern (concat "org-dblock-write:" name))))
  7645. (message "Updating dynamic block `%s' at line %d..." name line)
  7646. (funcall cmd params)
  7647. (message "Updating dynamic block `%s' at line %d...done" name line)
  7648. (goto-char pos))))
  7649. (defun org-beginning-of-dblock ()
  7650. "Find the beginning of the dynamic block at point.
  7651. Error if there is no such block at point."
  7652. (let ((pos (point))
  7653. beg)
  7654. (end-of-line 1)
  7655. (if (and (re-search-backward org-dblock-start-re nil t)
  7656. (setq beg (match-beginning 0))
  7657. (re-search-forward org-dblock-end-re nil t)
  7658. (> (match-end 0) pos))
  7659. (goto-char beg)
  7660. (goto-char pos)
  7661. (error "Not in a dynamic block"))))
  7662. (defun org-update-all-dblocks ()
  7663. "Update all dynamic blocks in the buffer.
  7664. This function can be used in a hook."
  7665. (when (org-mode-p)
  7666. (org-map-dblocks 'org-update-dblock)))
  7667. ;;;; Completion
  7668. (defconst org-additional-option-like-keywords
  7669. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7670. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7671. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7672. "BEGIN_QUOTE" "END_QUOTE"
  7673. "BEGIN_VERSE" "END_VERSE"
  7674. "BEGIN_CENTER" "END_CENTER"
  7675. "BEGIN_SRC" "END_SRC"
  7676. "CATEGORY" "COLUMNS"
  7677. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7678. (defcustom org-structure-template-alist
  7679. '(
  7680. ("s" "#+begin_src ?\n\n#+end_src"
  7681. "<src lang=\"?\">\n\n</src>")
  7682. ("e" "#+begin_example\n?\n#+end_example"
  7683. "<example>\n?\n</example>")
  7684. ("q" "#+begin_quote\n?\n#+end_quote"
  7685. "<quote>\n?\n</quote>")
  7686. ("v" "#+begin_verse\n?\n#+end_verse"
  7687. "<verse>\n?\n/verse>")
  7688. ("c" "#+begin_center\n?\n#+end_center"
  7689. "<center>\n?\n/center>")
  7690. ("l" "#+begin_latex\n?\n#+end_latex"
  7691. "<literal style=\"latex\">\n?\n</literal>")
  7692. ("L" "#+latex: "
  7693. "<literal style=\"latex\">?</literal>")
  7694. ("h" "#+begin_html\n?\n#+end_html"
  7695. "<literal style=\"html\">\n?\n</literal>")
  7696. ("H" "#+html: "
  7697. "<literal style=\"html\">?</literal>")
  7698. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7699. ("A" "#+ascii: ")
  7700. ("i" "#+include %file ?"
  7701. "<include file=%file markup=\"?\">")
  7702. )
  7703. "Structure completion elements.
  7704. This is a list of abbreviation keys and values. The value gets inserted
  7705. it you type @samp{.} followed by the key and then the completion key,
  7706. usually `M-TAB'. %file will be replaced by a file name after prompting
  7707. for the file using completion.
  7708. There are two templates for each key, the first uses the original Org syntax,
  7709. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7710. the default when the /org-mtags.el/ module has been loaded. See also the
  7711. variable `org-mtags-prefer-muse-templates'.
  7712. This is an experimental feature, it is undecided if it is going to stay in."
  7713. :group 'org-completion
  7714. :type '(repeat
  7715. (string :tag "Key")
  7716. (string :tag "Template")
  7717. (string :tag "Muse Template")))
  7718. (defun org-try-structure-completion ()
  7719. "Try to complete a structure template before point.
  7720. This looks for strings like \"<e\" on an otherwise empty line and
  7721. expands them."
  7722. (let ((l (buffer-substring (point-at-bol) (point)))
  7723. a)
  7724. (when (and (looking-at "[ \t]*$")
  7725. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7726. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7727. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7728. (match-beginning 1)) a)
  7729. t)))
  7730. (defun org-complete-expand-structure-template (start cell)
  7731. "Expand a structure template."
  7732. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7733. (rpl (nth (if musep 2 1) cell)))
  7734. (delete-region start (point))
  7735. (when (string-match "\\`#\\+" rpl)
  7736. (cond
  7737. ((bolp))
  7738. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7739. (delete-region (point-at-bol) (point)))
  7740. (t (newline))))
  7741. (setq start (point))
  7742. (if (string-match "%file" rpl)
  7743. (setq rpl (replace-match
  7744. (concat
  7745. "\""
  7746. (save-match-data
  7747. (abbreviate-file-name (read-file-name "Include file: ")))
  7748. "\"")
  7749. t t rpl)))
  7750. (insert rpl)
  7751. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7752. (defun org-complete (&optional arg)
  7753. "Perform completion on word at point.
  7754. At the beginning of a headline, this completes TODO keywords as given in
  7755. `org-todo-keywords'.
  7756. If the current word is preceded by a backslash, completes the TeX symbols
  7757. that are supported for HTML support.
  7758. If the current word is preceded by \"#+\", completes special words for
  7759. setting file options.
  7760. In the line after \"#+STARTUP:, complete valid keywords.\"
  7761. At all other locations, this simply calls the value of
  7762. `org-completion-fallback-command'."
  7763. (interactive "P")
  7764. (org-without-partial-completion
  7765. (catch 'exit
  7766. (let* ((a nil)
  7767. (end (point))
  7768. (beg1 (save-excursion
  7769. (skip-chars-backward (org-re "[:alnum:]_@"))
  7770. (point)))
  7771. (beg (save-excursion
  7772. (skip-chars-backward "a-zA-Z0-9_:$")
  7773. (point)))
  7774. (confirm (lambda (x) (stringp (car x))))
  7775. (searchhead (equal (char-before beg) ?*))
  7776. (struct
  7777. (when (and (member (char-before beg1) '(?. ?<))
  7778. (setq a (assoc (buffer-substring beg1 (point))
  7779. org-structure-template-alist)))
  7780. (org-complete-expand-structure-template (1- beg1) a)
  7781. (throw 'exit t)))
  7782. (tag (and (equal (char-before beg1) ?:)
  7783. (equal (char-after (point-at-bol)) ?*)))
  7784. (prop (and (equal (char-before beg1) ?:)
  7785. (not (equal (char-after (point-at-bol)) ?*))))
  7786. (texp (equal (char-before beg) ?\\))
  7787. (link (equal (char-before beg) ?\[))
  7788. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7789. beg)
  7790. "#+"))
  7791. (startup (string-match "^#\\+STARTUP:.*"
  7792. (buffer-substring (point-at-bol) (point))))
  7793. (completion-ignore-case opt)
  7794. (type nil)
  7795. (tbl nil)
  7796. (table (cond
  7797. (opt
  7798. (setq type :opt)
  7799. (require 'org-exp)
  7800. (append
  7801. (mapcar
  7802. (lambda (x)
  7803. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7804. (cons (match-string 2 x) (match-string 1 x)))
  7805. (org-split-string (org-get-current-options) "\n"))
  7806. (mapcar 'list org-additional-option-like-keywords)))
  7807. (startup
  7808. (setq type :startup)
  7809. org-startup-options)
  7810. (link (append org-link-abbrev-alist-local
  7811. org-link-abbrev-alist))
  7812. (texp
  7813. (setq type :tex)
  7814. org-html-entities)
  7815. ((string-match "\\`\\*+[ \t]+\\'"
  7816. (buffer-substring (point-at-bol) beg))
  7817. (setq type :todo)
  7818. (mapcar 'list org-todo-keywords-1))
  7819. (searchhead
  7820. (setq type :searchhead)
  7821. (save-excursion
  7822. (goto-char (point-min))
  7823. (while (re-search-forward org-todo-line-regexp nil t)
  7824. (push (list
  7825. (org-make-org-heading-search-string
  7826. (match-string 3) t))
  7827. tbl)))
  7828. tbl)
  7829. (tag (setq type :tag beg beg1)
  7830. (or org-tag-alist (org-get-buffer-tags)))
  7831. (prop (setq type :prop beg beg1)
  7832. (mapcar 'list (org-buffer-property-keys nil t t)))
  7833. (t (progn
  7834. (call-interactively org-completion-fallback-command)
  7835. (throw 'exit nil)))))
  7836. (pattern (buffer-substring-no-properties beg end))
  7837. (completion (try-completion pattern table confirm)))
  7838. (cond ((eq completion t)
  7839. (if (not (assoc (upcase pattern) table))
  7840. (message "Already complete")
  7841. (if (and (equal type :opt)
  7842. (not (member (car (assoc (upcase pattern) table))
  7843. org-additional-option-like-keywords)))
  7844. (insert (substring (cdr (assoc (upcase pattern) table))
  7845. (length pattern)))
  7846. (if (memq type '(:tag :prop)) (insert ":")))))
  7847. ((null completion)
  7848. (message "Can't find completion for \"%s\"" pattern)
  7849. (ding))
  7850. ((not (string= pattern completion))
  7851. (delete-region beg end)
  7852. (if (string-match " +$" completion)
  7853. (setq completion (replace-match "" t t completion)))
  7854. (insert completion)
  7855. (if (get-buffer-window "*Completions*")
  7856. (delete-window (get-buffer-window "*Completions*")))
  7857. (if (assoc completion table)
  7858. (if (eq type :todo) (insert " ")
  7859. (if (memq type '(:tag :prop)) (insert ":"))))
  7860. (if (and (equal type :opt) (assoc completion table))
  7861. (message "%s" (substitute-command-keys
  7862. "Press \\[org-complete] again to insert example settings"))))
  7863. (t
  7864. (message "Making completion list...")
  7865. (let ((list (sort (all-completions pattern table confirm)
  7866. 'string<)))
  7867. (with-output-to-temp-buffer "*Completions*"
  7868. (condition-case nil
  7869. ;; Protection needed for XEmacs and emacs 21
  7870. (display-completion-list list pattern)
  7871. (error (display-completion-list list)))))
  7872. (message "Making completion list...%s" "done")))))))
  7873. ;;;; TODO, DEADLINE, Comments
  7874. (defun org-toggle-comment ()
  7875. "Change the COMMENT state of an entry."
  7876. (interactive)
  7877. (save-excursion
  7878. (org-back-to-heading)
  7879. (let (case-fold-search)
  7880. (if (looking-at (concat outline-regexp
  7881. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7882. (replace-match "" t t nil 1)
  7883. (if (looking-at outline-regexp)
  7884. (progn
  7885. (goto-char (match-end 0))
  7886. (insert org-comment-string " ")))))))
  7887. (defvar org-last-todo-state-is-todo nil
  7888. "This is non-nil when the last TODO state change led to a TODO state.
  7889. If the last change removed the TODO tag or switched to DONE, then
  7890. this is nil.")
  7891. (defvar org-setting-tags nil) ; dynamically skipped
  7892. (defun org-parse-local-options (string var)
  7893. "Parse STRING for startup setting relevant for variable VAR."
  7894. (let ((rtn (symbol-value var))
  7895. e opts)
  7896. (save-match-data
  7897. (if (or (not string) (not (string-match "\\S-" string)))
  7898. rtn
  7899. (setq opts (delq nil (mapcar (lambda (x)
  7900. (setq e (assoc x org-startup-options))
  7901. (if (eq (nth 1 e) var) e nil))
  7902. (org-split-string string "[ \t]+"))))
  7903. (if (not opts)
  7904. rtn
  7905. (setq rtn nil)
  7906. (while (setq e (pop opts))
  7907. (if (not (nth 3 e))
  7908. (setq rtn (nth 2 e))
  7909. (if (not (listp rtn)) (setq rtn nil))
  7910. (push (nth 2 e) rtn)))
  7911. rtn)))))
  7912. (defvar org-todo-setup-filter-hook nil
  7913. "Hook for functions that pre-filter todo specs.
  7914. Each function takes a todo spec and returns either `nil' or the spec
  7915. transformed into canonical form." )
  7916. (defvar org-todo-get-default-hook nil
  7917. "Hook for functions that get a default item for todo.
  7918. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7919. `nil' or a string to be used for the todo mark." )
  7920. (defvar org-agenda-headline-snapshot-before-repeat)
  7921. (defun org-todo (&optional arg)
  7922. "Change the TODO state of an item.
  7923. The state of an item is given by a keyword at the start of the heading,
  7924. like
  7925. *** TODO Write paper
  7926. *** DONE Call mom
  7927. The different keywords are specified in the variable `org-todo-keywords'.
  7928. By default the available states are \"TODO\" and \"DONE\".
  7929. So for this example: when the item starts with TODO, it is changed to DONE.
  7930. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7931. DONE are present, add TODO at the beginning of the heading.
  7932. With C-u prefix arg, use completion to determine the new state.
  7933. With numeric prefix arg, switch to that state.
  7934. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7935. With a tripple C-u prefix, circumvent any state blocking.
  7936. For calling through lisp, arg is also interpreted in the following way:
  7937. 'none -> empty state
  7938. \"\"(empty string) -> switch to empty state
  7939. 'done -> switch to DONE
  7940. 'nextset -> switch to the next set of keywords
  7941. 'previousset -> switch to the previous set of keywords
  7942. \"WAITING\" -> switch to the specified keyword, but only if it
  7943. really is a member of `org-todo-keywords'."
  7944. (interactive "P")
  7945. (if (equal arg '(16)) (setq arg 'nextset))
  7946. (let ((org-blocker-hook org-blocker-hook)
  7947. (case-fold-search nil))
  7948. (when (equal arg '(64))
  7949. (setq arg nil org-blocker-hook nil))
  7950. (save-excursion
  7951. (catch 'exit
  7952. (org-back-to-heading)
  7953. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7954. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  7955. (looking-at " *"))
  7956. (let* ((match-data (match-data))
  7957. (startpos (point-at-bol))
  7958. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7959. (org-log-done org-log-done)
  7960. (org-log-repeat org-log-repeat)
  7961. (org-todo-log-states org-todo-log-states)
  7962. (this (match-string 1))
  7963. (hl-pos (match-beginning 0))
  7964. (head (org-get-todo-sequence-head this))
  7965. (ass (assoc head org-todo-kwd-alist))
  7966. (interpret (nth 1 ass))
  7967. (done-word (nth 3 ass))
  7968. (final-done-word (nth 4 ass))
  7969. (last-state (or this ""))
  7970. (completion-ignore-case t)
  7971. (member (member this org-todo-keywords-1))
  7972. (tail (cdr member))
  7973. (state (cond
  7974. ((and org-todo-key-trigger
  7975. (or (and (equal arg '(4))
  7976. (eq org-use-fast-todo-selection 'prefix))
  7977. (and (not arg) org-use-fast-todo-selection
  7978. (not (eq org-use-fast-todo-selection
  7979. 'prefix)))))
  7980. ;; Use fast selection
  7981. (org-fast-todo-selection))
  7982. ((and (equal arg '(4))
  7983. (or (not org-use-fast-todo-selection)
  7984. (not org-todo-key-trigger)))
  7985. ;; Read a state with completion
  7986. (org-ido-completing-read
  7987. "State: " (mapcar (lambda(x) (list x))
  7988. org-todo-keywords-1)
  7989. nil t))
  7990. ((eq arg 'right)
  7991. (if this
  7992. (if tail (car tail) nil)
  7993. (car org-todo-keywords-1)))
  7994. ((eq arg 'left)
  7995. (if (equal member org-todo-keywords-1)
  7996. nil
  7997. (if this
  7998. (nth (- (length org-todo-keywords-1)
  7999. (length tail) 2)
  8000. org-todo-keywords-1)
  8001. (org-last org-todo-keywords-1))))
  8002. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8003. (setq arg nil))) ; hack to fall back to cycling
  8004. (arg
  8005. ;; user or caller requests a specific state
  8006. (cond
  8007. ((equal arg "") nil)
  8008. ((eq arg 'none) nil)
  8009. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8010. ((eq arg 'nextset)
  8011. (or (car (cdr (member head org-todo-heads)))
  8012. (car org-todo-heads)))
  8013. ((eq arg 'previousset)
  8014. (let ((org-todo-heads (reverse org-todo-heads)))
  8015. (or (car (cdr (member head org-todo-heads)))
  8016. (car org-todo-heads))))
  8017. ((car (member arg org-todo-keywords-1)))
  8018. ((nth (1- (prefix-numeric-value arg))
  8019. org-todo-keywords-1))))
  8020. ((null member) (or head (car org-todo-keywords-1)))
  8021. ((equal this final-done-word) nil) ;; -> make empty
  8022. ((null tail) nil) ;; -> first entry
  8023. ((memq interpret '(type priority))
  8024. (if (eq this-command last-command)
  8025. (car tail)
  8026. (if (> (length tail) 0)
  8027. (or done-word (car org-done-keywords))
  8028. nil)))
  8029. (t
  8030. (car tail))))
  8031. (state (or
  8032. (run-hook-with-args-until-success
  8033. 'org-todo-get-default-hook state last-state)
  8034. state))
  8035. (next (if state (concat " " state " ") " "))
  8036. (change-plist (list :type 'todo-state-change :from this :to state
  8037. :position startpos))
  8038. dolog now-done-p)
  8039. (when org-blocker-hook
  8040. (setq org-last-todo-state-is-todo
  8041. (not (member this org-done-keywords)))
  8042. (unless (save-excursion
  8043. (save-match-data
  8044. (run-hook-with-args-until-failure
  8045. 'org-blocker-hook change-plist)))
  8046. (if (interactive-p)
  8047. (error "TODO state change from %s to %s blocked" this state)
  8048. ;; fail silently
  8049. (message "TODO state change from %s to %s blocked" this state)
  8050. (throw 'exit nil))))
  8051. (store-match-data match-data)
  8052. (replace-match next t t)
  8053. (unless (pos-visible-in-window-p hl-pos)
  8054. (message "TODO state changed to %s" (org-trim next)))
  8055. (unless head
  8056. (setq head (org-get-todo-sequence-head state)
  8057. ass (assoc head org-todo-kwd-alist)
  8058. interpret (nth 1 ass)
  8059. done-word (nth 3 ass)
  8060. final-done-word (nth 4 ass)))
  8061. (when (memq arg '(nextset previousset))
  8062. (message "Keyword-Set %d/%d: %s"
  8063. (- (length org-todo-sets) -1
  8064. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8065. (length org-todo-sets)
  8066. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8067. (setq org-last-todo-state-is-todo
  8068. (not (member state org-done-keywords)))
  8069. (setq now-done-p (and (member state org-done-keywords)
  8070. (not (member this org-done-keywords))))
  8071. (and logging (org-local-logging logging))
  8072. (when (and (or org-todo-log-states org-log-done)
  8073. (not (memq arg '(nextset previousset))))
  8074. ;; we need to look at recording a time and note
  8075. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8076. (nth 2 (assoc this org-todo-log-states))))
  8077. (when (and state
  8078. (member state org-not-done-keywords)
  8079. (not (member this org-not-done-keywords)))
  8080. ;; This is now a todo state and was not one before
  8081. ;; If there was a CLOSED time stamp, get rid of it.
  8082. (org-add-planning-info nil nil 'closed))
  8083. (when (and now-done-p org-log-done)
  8084. ;; It is now done, and it was not done before
  8085. (org-add-planning-info 'closed (org-current-time))
  8086. (if (and (not dolog) (eq 'note org-log-done))
  8087. (org-add-log-setup 'done state this 'findpos 'note)))
  8088. (when (and state dolog)
  8089. ;; This is a non-nil state, and we need to log it
  8090. (org-add-log-setup 'state state this 'findpos dolog)))
  8091. ;; Fixup tag positioning
  8092. (org-todo-trigger-tag-changes state)
  8093. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8094. (when org-provide-todo-statistics
  8095. (org-update-parent-todo-statistics))
  8096. (run-hooks 'org-after-todo-state-change-hook)
  8097. (if (and arg (not (member state org-done-keywords)))
  8098. (setq head (org-get-todo-sequence-head state)))
  8099. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8100. ;; Do we need to trigger a repeat?
  8101. (when now-done-p
  8102. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8103. ;; This is for the agenda, take a snapshot of the headline.
  8104. (save-match-data
  8105. (setq org-agenda-headline-snapshot-before-repeat
  8106. (org-get-heading))))
  8107. (org-auto-repeat-maybe state))
  8108. ;; Fixup cursor location if close to the keyword
  8109. (if (and (outline-on-heading-p)
  8110. (not (bolp))
  8111. (save-excursion (beginning-of-line 1)
  8112. (looking-at org-todo-line-regexp))
  8113. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8114. (progn
  8115. (goto-char (or (match-end 2) (match-end 1)))
  8116. (and (looking-at " ") (just-one-space))))
  8117. (when org-trigger-hook
  8118. (save-excursion
  8119. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8120. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8121. "Block turning an entry into a TODO, using the hierarchy.
  8122. This checks whether the current task should be blocked from state
  8123. changes. Such blocking occurs when:
  8124. 1. The task has children which are not all in a completed state.
  8125. 2. A task has a parent with the property :ORDERED:, and there
  8126. are siblings prior to the current task with incomplete
  8127. status.
  8128. 3. The parent of the task is blocked because it has siblings that should
  8129. be done first, or is child of a block grandparent TODO entry."
  8130. (catch 'dont-block
  8131. ;; If this is not a todo state change, or if this entry is already DONE,
  8132. ;; do not block
  8133. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8134. (member (plist-get change-plist :from)
  8135. (cons 'done org-done-keywords))
  8136. (member (plist-get change-plist :to)
  8137. (cons 'todo org-not-done-keywords)))
  8138. (throw 'dont-block t))
  8139. ;; If this task has children, and any are undone, it's blocked
  8140. (save-excursion
  8141. (org-back-to-heading t)
  8142. (let ((this-level (funcall outline-level)))
  8143. (outline-next-heading)
  8144. (let ((child-level (funcall outline-level)))
  8145. (while (and (not (eobp))
  8146. (> child-level this-level))
  8147. ;; this todo has children, check whether they are all
  8148. ;; completed
  8149. (if (and (not (org-entry-is-done-p))
  8150. (org-entry-is-todo-p))
  8151. (throw 'dont-block nil))
  8152. (outline-next-heading)
  8153. (setq child-level (funcall outline-level))))))
  8154. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8155. ;; any previous siblings are undone, it's blocked
  8156. (save-excursion
  8157. (org-back-to-heading t)
  8158. (let* ((pos (point))
  8159. (parent-pos (and (org-up-heading-safe) (point))))
  8160. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8161. (when (and (org-entry-get (point) "ORDERED")
  8162. (forward-line 1)
  8163. (re-search-forward org-not-done-heading-regexp pos t))
  8164. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8165. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8166. (while t
  8167. (goto-char parent-pos)
  8168. (if (not (looking-at org-not-done-heading-regexp))
  8169. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8170. (setq pos (point))
  8171. (setq parent-pos (and (org-up-heading-safe) (point)))
  8172. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8173. (when (and (org-entry-get (point) "ORDERED")
  8174. (forward-line 1)
  8175. (re-search-forward org-not-done-heading-regexp pos t))
  8176. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8177. (defcustom org-track-ordered-property-with-tag nil
  8178. "Should the ORDERED property also be shown as a tag?
  8179. The ORDERED property decides if an entry should require subtasks to be
  8180. completed in sequence. Since a property is not very visible, setting
  8181. this option means that toggling the ORDERED property with the command
  8182. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8183. not relevant for the behavior, but it makes things more visible.
  8184. Note that toggling the tag with tags commands will not change the property
  8185. and therefore not influence behavior!
  8186. This can be t, meaning the tag ORDERED should be used, It can also be a
  8187. string to select a different tag for this task."
  8188. :group 'org-todo
  8189. :type '(choice
  8190. (const :tag "No tracking" nil)
  8191. (const :tag "Track with ORDERED tag" t)
  8192. (string :tag "Use other tag")))
  8193. (defun org-toggle-ordered-property ()
  8194. "Toggle the ORDERED property of the current entry.
  8195. For better visibility, you can track the value of this property with a tag.
  8196. See variable `org-track-ordered-property-with-tag'."
  8197. (interactive)
  8198. (let* ((t1 org-track-ordered-property-with-tag)
  8199. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8200. (save-excursion
  8201. (org-back-to-heading)
  8202. (if (org-entry-get nil "ORDERED")
  8203. (progn
  8204. (org-delete-property "ORDERED")
  8205. (and tag (org-toggle-tag tag 'off))
  8206. (message "Subtasks can be completed in arbitrary order"))
  8207. (org-entry-put nil "ORDERED" "t")
  8208. (and tag (org-toggle-tag tag 'on))
  8209. (message "Subtasks must be completed in sequence")))))
  8210. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8211. (defun org-block-todo-from-checkboxes (change-plist)
  8212. "Block turning an entry into a TODO, using checkboxes.
  8213. This checks whether the current task should be blocked from state
  8214. changes because there are uncheckd boxes in this entry."
  8215. (catch 'dont-block
  8216. ;; If this is not a todo state change, or if this entry is already DONE,
  8217. ;; do not block
  8218. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8219. (member (plist-get change-plist :from)
  8220. (cons 'done org-done-keywords))
  8221. (member (plist-get change-plist :to)
  8222. (cons 'todo org-not-done-keywords)))
  8223. (throw 'dont-block t))
  8224. ;; If this task has checkboxes that are not checked, it's blocked
  8225. (save-excursion
  8226. (org-back-to-heading t)
  8227. (let ((beg (point)) end)
  8228. (outline-next-heading)
  8229. (setq end (point))
  8230. (goto-char beg)
  8231. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8232. end t)
  8233. (progn
  8234. (if (boundp 'org-blocked-by-checkboxes)
  8235. (setq org-blocked-by-checkboxes t))
  8236. (throw 'dont-block nil)))))
  8237. t)) ; do not block
  8238. (defun org-update-parent-todo-statistics ()
  8239. "Update any statistics cookie in the parent of the current headline."
  8240. (interactive)
  8241. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8242. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8243. (catch 'exit
  8244. (save-excursion
  8245. (setq level (org-up-heading-safe))
  8246. (unless level
  8247. (throw 'exit nil))
  8248. (while (re-search-forward box-re (point-at-eol) t)
  8249. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8250. (setq is-percent (match-end 2))
  8251. (save-match-data
  8252. (unless (outline-next-heading) (throw 'exit nil))
  8253. (while (looking-at org-todo-line-regexp)
  8254. (setq kwd (match-string 2))
  8255. (and kwd (setq cnt-all (1+ cnt-all)))
  8256. (and (member kwd org-done-keywords)
  8257. (setq cnt-done (1+ cnt-done)))
  8258. (condition-case nil
  8259. (org-forward-same-level 1)
  8260. (error (end-of-line 1)))))
  8261. (replace-match
  8262. (if is-percent
  8263. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8264. (format "[%d/%d]" cnt-done cnt-all))))
  8265. (when cookie-present
  8266. (run-hook-with-args 'org-after-todo-statistics-hook
  8267. cnt-done (- cnt-all cnt-done)))))))
  8268. (defvar org-after-todo-statistics-hook nil
  8269. "Hook that is called after a TODO statistics cookie has been updated.
  8270. Each function is called with two arguments: the number of not-done entries
  8271. and the number of done entries.
  8272. For example, the following function, when added to this hook, will switch
  8273. an entry to DONE when all children are done, and back to TODO when new
  8274. entries are set to a TODO status. Note that this hook is only called
  8275. when there is a statistics cookie in the headline!
  8276. (defun org-summary-todo (n-done n-not-done)
  8277. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8278. (let (org-log-done org-log-states) ; turn off logging
  8279. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8280. ")
  8281. (defun org-todo-trigger-tag-changes (state)
  8282. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8283. (let ((l org-todo-state-tags-triggers)
  8284. changes)
  8285. (when (or (not state) (equal state ""))
  8286. (setq changes (append changes (cdr (assoc "" l)))))
  8287. (when (and (stringp state) (> (length state) 0))
  8288. (setq changes (append changes (cdr (assoc state l)))))
  8289. (when (member state org-not-done-keywords)
  8290. (setq changes (append changes (cdr (assoc 'todo l)))))
  8291. (when (member state org-done-keywords)
  8292. (setq changes (append changes (cdr (assoc 'done l)))))
  8293. (dolist (c changes)
  8294. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8295. (defun org-local-logging (value)
  8296. "Get logging settings from a property VALUE."
  8297. (let* (words w a)
  8298. ;; directly set the variables, they are already local.
  8299. (setq org-log-done nil
  8300. org-log-repeat nil
  8301. org-todo-log-states nil)
  8302. (setq words (org-split-string value))
  8303. (while (setq w (pop words))
  8304. (cond
  8305. ((setq a (assoc w org-startup-options))
  8306. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8307. (set (nth 1 a) (nth 2 a))))
  8308. ((setq a (org-extract-log-state-settings w))
  8309. (and (member (car a) org-todo-keywords-1)
  8310. (push a org-todo-log-states)))))))
  8311. (defun org-get-todo-sequence-head (kwd)
  8312. "Return the head of the TODO sequence to which KWD belongs.
  8313. If KWD is not set, check if there is a text property remembering the
  8314. right sequence."
  8315. (let (p)
  8316. (cond
  8317. ((not kwd)
  8318. (or (get-text-property (point-at-bol) 'org-todo-head)
  8319. (progn
  8320. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8321. nil (point-at-eol)))
  8322. (get-text-property p 'org-todo-head))))
  8323. ((not (member kwd org-todo-keywords-1))
  8324. (car org-todo-keywords-1))
  8325. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8326. (defun org-fast-todo-selection ()
  8327. "Fast TODO keyword selection with single keys.
  8328. Returns the new TODO keyword, or nil if no state change should occur."
  8329. (let* ((fulltable org-todo-key-alist)
  8330. (done-keywords org-done-keywords) ;; needed for the faces.
  8331. (maxlen (apply 'max (mapcar
  8332. (lambda (x)
  8333. (if (stringp (car x)) (string-width (car x)) 0))
  8334. fulltable)))
  8335. (expert nil)
  8336. (fwidth (+ maxlen 3 1 3))
  8337. (ncol (/ (- (window-width) 4) fwidth))
  8338. tg cnt e c tbl
  8339. groups ingroup)
  8340. (save-excursion
  8341. (save-window-excursion
  8342. (if expert
  8343. (set-buffer (get-buffer-create " *Org todo*"))
  8344. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8345. (erase-buffer)
  8346. (org-set-local 'org-done-keywords done-keywords)
  8347. (setq tbl fulltable cnt 0)
  8348. (while (setq e (pop tbl))
  8349. (cond
  8350. ((equal e '(:startgroup))
  8351. (push '() groups) (setq ingroup t)
  8352. (when (not (= cnt 0))
  8353. (setq cnt 0)
  8354. (insert "\n"))
  8355. (insert "{ "))
  8356. ((equal e '(:endgroup))
  8357. (setq ingroup nil cnt 0)
  8358. (insert "}\n"))
  8359. ((equal e '(:newline))
  8360. (when (not (= cnt 0))
  8361. (setq cnt 0)
  8362. (insert "\n")
  8363. (setq e (car tbl))
  8364. (while (equal (car tbl) '(:newline))
  8365. (insert "\n")
  8366. (setq tbl (cdr tbl)))))
  8367. (t
  8368. (setq tg (car e) c (cdr e))
  8369. (if ingroup (push tg (car groups)))
  8370. (setq tg (org-add-props tg nil 'face
  8371. (org-get-todo-face tg)))
  8372. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8373. (insert "[" c "] " tg (make-string
  8374. (- fwidth 4 (length tg)) ?\ ))
  8375. (when (= (setq cnt (1+ cnt)) ncol)
  8376. (insert "\n")
  8377. (if ingroup (insert " "))
  8378. (setq cnt 0)))))
  8379. (insert "\n")
  8380. (goto-char (point-min))
  8381. (if (not expert) (org-fit-window-to-buffer))
  8382. (message "[a-z..]:Set [SPC]:clear")
  8383. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8384. (cond
  8385. ((or (= c ?\C-g)
  8386. (and (= c ?q) (not (rassoc c fulltable))))
  8387. (setq quit-flag t))
  8388. ((= c ?\ ) nil)
  8389. ((setq e (rassoc c fulltable) tg (car e))
  8390. tg)
  8391. (t (setq quit-flag t)))))))
  8392. (defun org-entry-is-todo-p ()
  8393. (member (org-get-todo-state) org-not-done-keywords))
  8394. (defun org-entry-is-done-p ()
  8395. (member (org-get-todo-state) org-done-keywords))
  8396. (defun org-get-todo-state ()
  8397. (save-excursion
  8398. (org-back-to-heading t)
  8399. (and (looking-at org-todo-line-regexp)
  8400. (match-end 2)
  8401. (match-string 2))))
  8402. (defun org-at-date-range-p (&optional inactive-ok)
  8403. "Is the cursor inside a date range?"
  8404. (interactive)
  8405. (save-excursion
  8406. (catch 'exit
  8407. (let ((pos (point)))
  8408. (skip-chars-backward "^[<\r\n")
  8409. (skip-chars-backward "<[")
  8410. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8411. (>= (match-end 0) pos)
  8412. (throw 'exit t))
  8413. (skip-chars-backward "^<[\r\n")
  8414. (skip-chars-backward "<[")
  8415. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8416. (>= (match-end 0) pos)
  8417. (throw 'exit t)))
  8418. nil)))
  8419. (defun org-get-repeat ()
  8420. "Check if there is a deadline/schedule with repeater in this entry."
  8421. (save-match-data
  8422. (save-excursion
  8423. (org-back-to-heading t)
  8424. (if (re-search-forward
  8425. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8426. (match-string 1)))))
  8427. (defvar org-last-changed-timestamp)
  8428. (defvar org-last-inserted-timestamp)
  8429. (defvar org-log-post-message)
  8430. (defvar org-log-note-purpose)
  8431. (defvar org-log-note-how)
  8432. (defvar org-log-note-extra)
  8433. (defun org-auto-repeat-maybe (done-word)
  8434. "Check if the current headline contains a repeated deadline/schedule.
  8435. If yes, set TODO state back to what it was and change the base date
  8436. of repeating deadline/scheduled time stamps to new date.
  8437. This function is run automatically after each state change to a DONE state."
  8438. ;; last-state is dynamically scoped into this function
  8439. (let* ((repeat (org-get-repeat))
  8440. (aa (assoc last-state org-todo-kwd-alist))
  8441. (interpret (nth 1 aa))
  8442. (head (nth 2 aa))
  8443. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8444. (msg "Entry repeats: ")
  8445. (org-log-done nil)
  8446. (org-todo-log-states nil)
  8447. (nshiftmax 10) (nshift 0)
  8448. re type n what ts time)
  8449. (when repeat
  8450. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8451. (org-todo (if (eq interpret 'type) last-state head))
  8452. (when org-log-repeat
  8453. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8454. (memq 'org-add-log-note post-command-hook))
  8455. ;; OK, we are already setup for some record
  8456. (if (eq org-log-repeat 'note)
  8457. ;; make sure we take a note, not only a time stamp
  8458. (setq org-log-note-how 'note))
  8459. ;; Set up for taking a record
  8460. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8461. last-state
  8462. 'findpos org-log-repeat)))
  8463. (org-back-to-heading t)
  8464. (org-add-planning-info nil nil 'closed)
  8465. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8466. org-deadline-time-regexp "\\)\\|\\("
  8467. org-ts-regexp "\\)"))
  8468. (while (re-search-forward
  8469. re (save-excursion (outline-next-heading) (point)) t)
  8470. (setq type (if (match-end 1) org-scheduled-string
  8471. (if (match-end 3) org-deadline-string "Plain:"))
  8472. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8473. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8474. (setq n (string-to-number (match-string 2 ts))
  8475. what (match-string 3 ts))
  8476. (if (equal what "w") (setq n (* n 7) what "d"))
  8477. ;; Preparation, see if we need to modify the start date for the change
  8478. (when (match-end 1)
  8479. (setq time (save-match-data (org-time-string-to-time ts)))
  8480. (cond
  8481. ((equal (match-string 1 ts) ".")
  8482. ;; Shift starting date to today
  8483. (org-timestamp-change
  8484. (- (time-to-days (current-time)) (time-to-days time))
  8485. 'day))
  8486. ((equal (match-string 1 ts) "+")
  8487. (while (or (= nshift 0)
  8488. (<= (time-to-days time) (time-to-days (current-time))))
  8489. (when (= (incf nshift) nshiftmax)
  8490. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8491. (error "Abort")))
  8492. (org-timestamp-change n (cdr (assoc what whata)))
  8493. (org-at-timestamp-p t)
  8494. (setq ts (match-string 1))
  8495. (setq time (save-match-data (org-time-string-to-time ts))))
  8496. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8497. ;; rematch, so that we have everything in place for the real shift
  8498. (org-at-timestamp-p t)
  8499. (setq ts (match-string 1))
  8500. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8501. (org-timestamp-change n (cdr (assoc what whata)))
  8502. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8503. (setq org-log-post-message msg)
  8504. (message "%s" msg))))
  8505. (defun org-show-todo-tree (arg)
  8506. "Make a compact tree which shows all headlines marked with TODO.
  8507. The tree will show the lines where the regexp matches, and all higher
  8508. headlines above the match.
  8509. With a \\[universal-argument] prefix, also show the DONE entries.
  8510. With a numeric prefix N, construct a sparse tree for the Nth element
  8511. of `org-todo-keywords-1'."
  8512. (interactive "P")
  8513. (let ((case-fold-search nil)
  8514. (kwd-re
  8515. (cond ((null arg) org-not-done-regexp)
  8516. ((equal arg '(4))
  8517. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8518. (mapcar 'list org-todo-keywords-1))))
  8519. (concat "\\("
  8520. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8521. "\\)\\>")))
  8522. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8523. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8524. org-todo-keywords-1)))
  8525. (t (error "Invalid prefix argument: %s" arg)))))
  8526. (message "%d TODO entries found"
  8527. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8528. (defun org-deadline (&optional remove time)
  8529. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8530. With argument REMOVE, remove any deadline from the item.
  8531. When TIME is set, it should be an internal time specification, and the
  8532. scheduling will use the corresponding date."
  8533. (interactive "P")
  8534. (if remove
  8535. (progn
  8536. (org-remove-timestamp-with-keyword org-deadline-string)
  8537. (message "Item no longer has a deadline."))
  8538. (if (org-get-repeat)
  8539. (error "Cannot change deadline on task with repeater, please do that by hand")
  8540. (org-add-planning-info 'deadline time 'closed)
  8541. (message "Deadline on %s" org-last-inserted-timestamp))))
  8542. (defun org-schedule (&optional remove time)
  8543. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8544. With argument REMOVE, remove any scheduling date from the item.
  8545. When TIME is set, it should be an internal time specification, and the
  8546. scheduling will use the corresponding date."
  8547. (interactive "P")
  8548. (if remove
  8549. (progn
  8550. (org-remove-timestamp-with-keyword org-scheduled-string)
  8551. (message "Item is no longer scheduled."))
  8552. (if (org-get-repeat)
  8553. (error "Cannot reschedule task with repeater, please do that by hand")
  8554. (org-add-planning-info 'scheduled time 'closed)
  8555. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8556. (defun org-get-scheduled-time (pom &optional inherit)
  8557. "Get the scheduled time as a time tuple, of a format suitable
  8558. for calling org-schedule with, or if there is no scheduling,
  8559. returns nil."
  8560. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8561. (when time
  8562. (apply 'encode-time (org-parse-time-string time)))))
  8563. (defun org-get-deadline-time (pom &optional inherit)
  8564. "Get the deadine as a time tuple, of a format suitable for
  8565. calling org-deadlin with, or if there is no scheduling, returns
  8566. nil."
  8567. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8568. (when time
  8569. (apply 'encode-time (org-parse-time-string time)))))
  8570. (defun org-remove-timestamp-with-keyword (keyword)
  8571. "Remove all time stamps with KEYWORD in the current entry."
  8572. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8573. beg)
  8574. (save-excursion
  8575. (org-back-to-heading t)
  8576. (setq beg (point))
  8577. (org-end-of-subtree t t)
  8578. (while (re-search-backward re beg t)
  8579. (replace-match "")
  8580. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8581. (equal (char-before) ?\ ))
  8582. (backward-delete-char 1)
  8583. (if (string-match "^[ \t]*$" (buffer-substring
  8584. (point-at-bol) (point-at-eol)))
  8585. (delete-region (point-at-bol)
  8586. (min (point-max) (1+ (point-at-eol))))))))))
  8587. (defun org-add-planning-info (what &optional time &rest remove)
  8588. "Insert new timestamp with keyword in the line directly after the headline.
  8589. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8590. If non is given, the user is prompted for a date.
  8591. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8592. be removed."
  8593. (interactive)
  8594. (let (org-time-was-given org-end-time-was-given ts
  8595. end default-time default-input)
  8596. (catch 'exit
  8597. (when (and (not time) (memq what '(scheduled deadline)))
  8598. ;; Try to get a default date/time from existing timestamp
  8599. (save-excursion
  8600. (org-back-to-heading t)
  8601. (setq end (save-excursion (outline-next-heading) (point)))
  8602. (when (re-search-forward (if (eq what 'scheduled)
  8603. org-scheduled-time-regexp
  8604. org-deadline-time-regexp)
  8605. end t)
  8606. (setq ts (match-string 1)
  8607. default-time
  8608. (apply 'encode-time (org-parse-time-string ts))
  8609. default-input (and ts (org-get-compact-tod ts))))))
  8610. (when what
  8611. ;; If necessary, get the time from the user
  8612. (setq time (or time (org-read-date nil 'to-time nil nil
  8613. default-time default-input))))
  8614. (when (and org-insert-labeled-timestamps-at-point
  8615. (member what '(scheduled deadline)))
  8616. (insert
  8617. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8618. (org-insert-time-stamp time org-time-was-given
  8619. nil nil nil (list org-end-time-was-given))
  8620. (setq what nil))
  8621. (save-excursion
  8622. (save-restriction
  8623. (let (col list elt ts buffer-invisibility-spec)
  8624. (org-back-to-heading t)
  8625. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8626. (goto-char (match-end 1))
  8627. (setq col (current-column))
  8628. (goto-char (match-end 0))
  8629. (if (eobp) (insert "\n") (forward-char 1))
  8630. (when (and (not what)
  8631. (not (looking-at
  8632. (concat "[ \t]*"
  8633. org-keyword-time-not-clock-regexp))))
  8634. ;; Nothing to add, nothing to remove...... :-)
  8635. (throw 'exit nil))
  8636. (if (and (not (looking-at outline-regexp))
  8637. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8638. "[^\r\n]*"))
  8639. (not (equal (match-string 1) org-clock-string)))
  8640. (narrow-to-region (match-beginning 0) (match-end 0))
  8641. (insert-before-markers "\n")
  8642. (backward-char 1)
  8643. (narrow-to-region (point) (point))
  8644. (and org-adapt-indentation (org-indent-to-column col)))
  8645. ;; Check if we have to remove something.
  8646. (setq list (cons what remove))
  8647. (while list
  8648. (setq elt (pop list))
  8649. (goto-char (point-min))
  8650. (when (or (and (eq elt 'scheduled)
  8651. (re-search-forward org-scheduled-time-regexp nil t))
  8652. (and (eq elt 'deadline)
  8653. (re-search-forward org-deadline-time-regexp nil t))
  8654. (and (eq elt 'closed)
  8655. (re-search-forward org-closed-time-regexp nil t)))
  8656. (replace-match "")
  8657. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8658. (if (looking-at " +") (replace-match ""))))
  8659. (goto-char (point-max))
  8660. (when what
  8661. (insert
  8662. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8663. (cond ((eq what 'scheduled) org-scheduled-string)
  8664. ((eq what 'deadline) org-deadline-string)
  8665. ((eq what 'closed) org-closed-string))
  8666. " ")
  8667. (setq ts (org-insert-time-stamp
  8668. time
  8669. (or org-time-was-given
  8670. (and (eq what 'closed) org-log-done-with-time))
  8671. (eq what 'closed)
  8672. nil nil (list org-end-time-was-given)))
  8673. (end-of-line 1))
  8674. (goto-char (point-min))
  8675. (widen)
  8676. (if (and (looking-at "[ \t]+\n")
  8677. (equal (char-before) ?\n))
  8678. (delete-region (1- (point)) (point-at-eol)))
  8679. ts))))))
  8680. (defvar org-log-note-marker (make-marker))
  8681. (defvar org-log-note-purpose nil)
  8682. (defvar org-log-note-state nil)
  8683. (defvar org-log-note-previous-state nil)
  8684. (defvar org-log-note-how nil)
  8685. (defvar org-log-note-extra nil)
  8686. (defvar org-log-note-window-configuration nil)
  8687. (defvar org-log-note-return-to (make-marker))
  8688. (defvar org-log-post-message nil
  8689. "Message to be displayed after a log note has been stored.
  8690. The auto-repeater uses this.")
  8691. (defun org-add-note ()
  8692. "Add a note to the current entry.
  8693. This is done in the same way as adding a state change note."
  8694. (interactive)
  8695. (org-add-log-setup 'note nil nil 'findpos nil))
  8696. (defvar org-property-end-re)
  8697. (defun org-add-log-setup (&optional purpose state prev-state
  8698. findpos how &optional extra)
  8699. "Set up the post command hook to take a note.
  8700. If this is about to TODO state change, the new state is expected in STATE.
  8701. When FINDPOS is non-nil, find the correct position for the note in
  8702. the current entry. If not, assume that it can be inserted at point.
  8703. HOW is an indicator what kind of note should be created.
  8704. EXTRA is additional text that will be inserted into the notes buffer."
  8705. (let ((drawer (cond ((stringp org-log-into-drawer)
  8706. org-log-into-drawer)
  8707. (org-log-into-drawer "LOGBOOK")
  8708. (t nil))))
  8709. (save-restriction
  8710. (save-excursion
  8711. (when findpos
  8712. (org-back-to-heading t)
  8713. (narrow-to-region (point) (save-excursion
  8714. (outline-next-heading) (point)))
  8715. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8716. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8717. "[^\r\n]*\\)?"))
  8718. (goto-char (match-end 0))
  8719. (cond
  8720. (drawer
  8721. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8722. nil t)
  8723. (progn
  8724. (goto-char (match-end 0))
  8725. (or org-log-states-order-reversed
  8726. (and (re-search-forward org-property-end-re nil t)
  8727. (goto-char (1- (match-beginning 0))))))
  8728. (insert "\n:" drawer ":\n:END:")
  8729. (beginning-of-line 0)
  8730. (org-indent-line-function)
  8731. (beginning-of-line 2)
  8732. (org-indent-line-function)
  8733. (end-of-line 0)))
  8734. ((and org-log-state-notes-insert-after-drawers
  8735. (save-excursion
  8736. (forward-line) (looking-at org-drawer-regexp)))
  8737. (forward-line)
  8738. (while (looking-at org-drawer-regexp)
  8739. (goto-char (match-end 0))
  8740. (re-search-forward org-property-end-re (point-max) t)
  8741. (forward-line))
  8742. (forward-line -1)))
  8743. (unless org-log-states-order-reversed
  8744. (and (= (char-after) ?\n) (forward-char 1))
  8745. (org-skip-over-state-notes)
  8746. (skip-chars-backward " \t\n\r")))
  8747. (move-marker org-log-note-marker (point))
  8748. (setq org-log-note-purpose purpose
  8749. org-log-note-state state
  8750. org-log-note-previous-state prev-state
  8751. org-log-note-how how
  8752. org-log-note-extra extra)
  8753. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8754. (defun org-skip-over-state-notes ()
  8755. "Skip past the list of State notes in an entry."
  8756. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8757. (while (looking-at "[ \t]*- State")
  8758. (condition-case nil
  8759. (org-next-item)
  8760. (error (org-end-of-item)))))
  8761. (defun org-add-log-note (&optional purpose)
  8762. "Pop up a window for taking a note, and add this note later at point."
  8763. (remove-hook 'post-command-hook 'org-add-log-note)
  8764. (setq org-log-note-window-configuration (current-window-configuration))
  8765. (delete-other-windows)
  8766. (move-marker org-log-note-return-to (point))
  8767. (switch-to-buffer (marker-buffer org-log-note-marker))
  8768. (goto-char org-log-note-marker)
  8769. (org-switch-to-buffer-other-window "*Org Note*")
  8770. (erase-buffer)
  8771. (if (memq org-log-note-how '(time state))
  8772. (let (current-prefix-arg) (org-store-log-note))
  8773. (let ((org-inhibit-startup t)) (org-mode))
  8774. (insert (format "# Insert note for %s.
  8775. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8776. (cond
  8777. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8778. ((eq org-log-note-purpose 'done) "closed todo item")
  8779. ((eq org-log-note-purpose 'state)
  8780. (format "state change from \"%s\" to \"%s\""
  8781. (or org-log-note-previous-state "")
  8782. (or org-log-note-state "")))
  8783. ((eq org-log-note-purpose 'note)
  8784. "this entry")
  8785. (t (error "This should not happen")))))
  8786. (if org-log-note-extra (insert org-log-note-extra))
  8787. (org-set-local 'org-finish-function 'org-store-log-note)))
  8788. (defvar org-note-abort nil) ; dynamically scoped
  8789. (defun org-store-log-note ()
  8790. "Finish taking a log note, and insert it to where it belongs."
  8791. (let ((txt (buffer-string))
  8792. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8793. lines ind)
  8794. (kill-buffer (current-buffer))
  8795. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8796. (setq txt (replace-match "" t t txt)))
  8797. (if (string-match "\\s-+\\'" txt)
  8798. (setq txt (replace-match "" t t txt)))
  8799. (setq lines (org-split-string txt "\n"))
  8800. (when (and note (string-match "\\S-" note))
  8801. (setq note
  8802. (org-replace-escapes
  8803. note
  8804. (list (cons "%u" (user-login-name))
  8805. (cons "%U" user-full-name)
  8806. (cons "%t" (format-time-string
  8807. (org-time-stamp-format 'long 'inactive)
  8808. (current-time)))
  8809. (cons "%s" (if org-log-note-state
  8810. (concat "\"" org-log-note-state "\"")
  8811. ""))
  8812. (cons "%S" (if org-log-note-previous-state
  8813. (concat "\"" org-log-note-previous-state "\"")
  8814. "\"\"")))))
  8815. (if lines (setq note (concat note " \\\\")))
  8816. (push note lines))
  8817. (when (or current-prefix-arg org-note-abort)
  8818. (when org-log-into-drawer
  8819. (org-remove-empty-drawer-at
  8820. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8821. org-log-note-marker))
  8822. (setq lines nil))
  8823. (when lines
  8824. (save-excursion
  8825. (set-buffer (marker-buffer org-log-note-marker))
  8826. (save-excursion
  8827. (goto-char org-log-note-marker)
  8828. (move-marker org-log-note-marker nil)
  8829. (end-of-line 1)
  8830. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8831. (insert "- " (pop lines))
  8832. (org-indent-line-function)
  8833. (beginning-of-line 1)
  8834. (looking-at "[ \t]*")
  8835. (setq ind (concat (match-string 0) " "))
  8836. (end-of-line 1)
  8837. (while lines (insert "\n" ind (pop lines)))
  8838. (message "Note stored")
  8839. (org-back-to-heading t)
  8840. (org-cycle-hide-drawers 'children)))))
  8841. (set-window-configuration org-log-note-window-configuration)
  8842. (with-current-buffer (marker-buffer org-log-note-return-to)
  8843. (goto-char org-log-note-return-to))
  8844. (move-marker org-log-note-return-to nil)
  8845. (and org-log-post-message (message "%s" org-log-post-message)))
  8846. (defun org-remove-empty-drawer-at (drawer pos)
  8847. "Remove an emptyr DARWER drawer at position POS.
  8848. POS may also be a marker."
  8849. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8850. (save-excursion
  8851. (save-restriction
  8852. (widen)
  8853. (goto-char pos)
  8854. (if (org-in-regexp
  8855. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8856. (replace-match ""))))))
  8857. (defun org-sparse-tree (&optional arg)
  8858. "Create a sparse tree, prompt for the details.
  8859. This command can create sparse trees. You first need to select the type
  8860. of match used to create the tree:
  8861. t Show entries with a specific TODO keyword.
  8862. m Show entries selected by a tags/property match.
  8863. p Enter a property name and its value (both with completion on existing
  8864. names/values) and show entries with that property.
  8865. r Show entries matching a regular expression.
  8866. d Show deadlines due within `org-deadline-warning-days'.
  8867. b Show deadlines and scheduled items before a date.
  8868. a Show deadlines and scheduled items after a date."
  8869. (interactive "P")
  8870. (let (ans kwd value)
  8871. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8872. (setq ans (read-char-exclusive))
  8873. (cond
  8874. ((equal ans ?d)
  8875. (call-interactively 'org-check-deadlines))
  8876. ((equal ans ?b)
  8877. (call-interactively 'org-check-before-date))
  8878. ((equal ans ?a)
  8879. (call-interactively 'org-check-after-date))
  8880. ((equal ans ?t)
  8881. (org-show-todo-tree '(4)))
  8882. ((member ans '(?T ?m))
  8883. (call-interactively 'org-match-sparse-tree))
  8884. ((member ans '(?p ?P))
  8885. (setq kwd (org-ido-completing-read "Property: "
  8886. (mapcar 'list (org-buffer-property-keys))))
  8887. (setq value (org-ido-completing-read "Value: "
  8888. (mapcar 'list (org-property-values kwd))))
  8889. (unless (string-match "\\`{.*}\\'" value)
  8890. (setq value (concat "\"" value "\"")))
  8891. (org-match-sparse-tree arg (concat kwd "=" value)))
  8892. ((member ans '(?r ?R ?/))
  8893. (call-interactively 'org-occur))
  8894. (t (error "No such sparse tree command \"%c\"" ans)))))
  8895. (defvar org-occur-highlights nil
  8896. "List of overlays used for occur matches.")
  8897. (make-variable-buffer-local 'org-occur-highlights)
  8898. (defvar org-occur-parameters nil
  8899. "Parameters of the active org-occur calls.
  8900. This is a list, each call to org-occur pushes as cons cell,
  8901. containing the regular expression and the callback, onto the list.
  8902. The list can contain several entries if `org-occur' has been called
  8903. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8904. will only contain one set of parameters. When the highlights are
  8905. removed (for example with `C-c C-c', or with the next edit (depending
  8906. on `org-remove-highlights-with-change'), this variable is emptied
  8907. as well.")
  8908. (make-variable-buffer-local 'org-occur-parameters)
  8909. (defun org-occur (regexp &optional keep-previous callback)
  8910. "Make a compact tree which shows all matches of REGEXP.
  8911. The tree will show the lines where the regexp matches, and all higher
  8912. headlines above the match. It will also show the heading after the match,
  8913. to make sure editing the matching entry is easy.
  8914. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8915. call to `org-occur' will be kept, to allow stacking of calls to this
  8916. command.
  8917. If CALLBACK is non-nil, it is a function which is called to confirm
  8918. that the match should indeed be shown."
  8919. (interactive "sRegexp: \nP")
  8920. (unless keep-previous
  8921. (org-remove-occur-highlights nil nil t))
  8922. (push (cons regexp callback) org-occur-parameters)
  8923. (let ((cnt 0))
  8924. (save-excursion
  8925. (goto-char (point-min))
  8926. (if (or (not keep-previous) ; do not want to keep
  8927. (not org-occur-highlights)) ; no previous matches
  8928. ;; hide everything
  8929. (org-overview))
  8930. (while (re-search-forward regexp nil t)
  8931. (when (or (not callback)
  8932. (save-match-data (funcall callback)))
  8933. (setq cnt (1+ cnt))
  8934. (when org-highlight-sparse-tree-matches
  8935. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8936. (org-show-context 'occur-tree))))
  8937. (when org-remove-highlights-with-change
  8938. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8939. nil 'local))
  8940. (unless org-sparse-tree-open-archived-trees
  8941. (org-hide-archived-subtrees (point-min) (point-max)))
  8942. (run-hooks 'org-occur-hook)
  8943. (if (interactive-p)
  8944. (message "%d match(es) for regexp %s" cnt regexp))
  8945. cnt))
  8946. (defun org-show-context (&optional key)
  8947. "Make sure point and context and visible.
  8948. How much context is shown depends upon the variables
  8949. `org-show-hierarchy-above', `org-show-following-heading'. and
  8950. `org-show-siblings'."
  8951. (let ((heading-p (org-on-heading-p t))
  8952. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8953. (following-p (org-get-alist-option org-show-following-heading key))
  8954. (entry-p (org-get-alist-option org-show-entry-below key))
  8955. (siblings-p (org-get-alist-option org-show-siblings key)))
  8956. (catch 'exit
  8957. ;; Show heading or entry text
  8958. (if (and heading-p (not entry-p))
  8959. (org-flag-heading nil) ; only show the heading
  8960. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8961. (org-show-hidden-entry))) ; show entire entry
  8962. (when following-p
  8963. ;; Show next sibling, or heading below text
  8964. (save-excursion
  8965. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8966. (org-flag-heading nil))))
  8967. (when siblings-p (org-show-siblings))
  8968. (when hierarchy-p
  8969. ;; show all higher headings, possibly with siblings
  8970. (save-excursion
  8971. (while (and (condition-case nil
  8972. (progn (org-up-heading-all 1) t)
  8973. (error nil))
  8974. (not (bobp)))
  8975. (org-flag-heading nil)
  8976. (when siblings-p (org-show-siblings))))))))
  8977. (defun org-reveal (&optional siblings)
  8978. "Show current entry, hierarchy above it, and the following headline.
  8979. This can be used to show a consistent set of context around locations
  8980. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8981. not t for the search context.
  8982. With optional argument SIBLINGS, on each level of the hierarchy all
  8983. siblings are shown. This repairs the tree structure to what it would
  8984. look like when opened with hierarchical calls to `org-cycle'."
  8985. (interactive "P")
  8986. (let ((org-show-hierarchy-above t)
  8987. (org-show-following-heading t)
  8988. (org-show-siblings (if siblings t org-show-siblings)))
  8989. (org-show-context nil)))
  8990. (defun org-highlight-new-match (beg end)
  8991. "Highlight from BEG to END and mark the highlight is an occur headline."
  8992. (let ((ov (org-make-overlay beg end)))
  8993. (org-overlay-put ov 'face 'secondary-selection)
  8994. (push ov org-occur-highlights)))
  8995. (defun org-remove-occur-highlights (&optional beg end noremove)
  8996. "Remove the occur highlights from the buffer.
  8997. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8998. from the `before-change-functions' in the current buffer."
  8999. (interactive)
  9000. (unless org-inhibit-highlight-removal
  9001. (mapc 'org-delete-overlay org-occur-highlights)
  9002. (setq org-occur-highlights nil)
  9003. (setq org-occur-parameters nil)
  9004. (unless noremove
  9005. (remove-hook 'before-change-functions
  9006. 'org-remove-occur-highlights 'local))))
  9007. ;;;; Priorities
  9008. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9009. "Regular expression matching the priority indicator.")
  9010. (defvar org-remove-priority-next-time nil)
  9011. (defun org-priority-up ()
  9012. "Increase the priority of the current item."
  9013. (interactive)
  9014. (org-priority 'up))
  9015. (defun org-priority-down ()
  9016. "Decrease the priority of the current item."
  9017. (interactive)
  9018. (org-priority 'down))
  9019. (defun org-priority (&optional action)
  9020. "Change the priority of an item by ARG.
  9021. ACTION can be `set', `up', `down', or a character."
  9022. (interactive)
  9023. (setq action (or action 'set))
  9024. (let (current new news have remove)
  9025. (save-excursion
  9026. (org-back-to-heading t)
  9027. (if (looking-at org-priority-regexp)
  9028. (setq current (string-to-char (match-string 2))
  9029. have t)
  9030. (setq current org-default-priority))
  9031. (cond
  9032. ((or (eq action 'set)
  9033. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9034. (if (not (eq action 'set))
  9035. (setq new action)
  9036. (message "Priority %c-%c, SPC to remove: "
  9037. org-highest-priority org-lowest-priority)
  9038. (setq new (read-char-exclusive)))
  9039. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9040. (= (upcase org-lowest-priority) org-lowest-priority))
  9041. (setq new (upcase new)))
  9042. (cond ((equal new ?\ ) (setq remove t))
  9043. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9044. (error "Priority must be between `%c' and `%c'"
  9045. org-highest-priority org-lowest-priority))))
  9046. ((eq action 'up)
  9047. (if (and (not have) (eq last-command this-command))
  9048. (setq new org-lowest-priority)
  9049. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9050. org-default-priority (1- current)))))
  9051. ((eq action 'down)
  9052. (if (and (not have) (eq last-command this-command))
  9053. (setq new org-highest-priority)
  9054. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9055. org-default-priority (1+ current)))))
  9056. (t (error "Invalid action")))
  9057. (if (or (< (upcase new) org-highest-priority)
  9058. (> (upcase new) org-lowest-priority))
  9059. (setq remove t))
  9060. (setq news (format "%c" new))
  9061. (if have
  9062. (if remove
  9063. (replace-match "" t t nil 1)
  9064. (replace-match news t t nil 2))
  9065. (if remove
  9066. (error "No priority cookie found in line")
  9067. (looking-at org-todo-line-regexp)
  9068. (if (match-end 2)
  9069. (progn
  9070. (goto-char (match-end 2))
  9071. (insert " [#" news "]"))
  9072. (goto-char (match-beginning 3))
  9073. (insert "[#" news "] ")))))
  9074. (org-preserve-lc (org-set-tags nil 'align))
  9075. (if remove
  9076. (message "Priority removed")
  9077. (message "Priority of current item set to %s" news))))
  9078. (defun org-get-priority (s)
  9079. "Find priority cookie and return priority."
  9080. (save-match-data
  9081. (if (not (string-match org-priority-regexp s))
  9082. (* 1000 (- org-lowest-priority org-default-priority))
  9083. (* 1000 (- org-lowest-priority
  9084. (string-to-char (match-string 2 s)))))))
  9085. ;;;; Tags
  9086. (defvar org-agenda-archives-mode)
  9087. (defvar org-map-continue-from nil
  9088. "Position from where mapping should continue.
  9089. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9090. (defvar org-scanner-tags nil
  9091. "The current tag list while the tags scanner is running.")
  9092. (defvar org-trust-scanner-tags nil
  9093. "Should `org-get-tags-at' use the tags fro the scanner.
  9094. This is for internal dynamical scoping only.
  9095. When this is non-nil, the function `org-get-tags-at' will return the value
  9096. of `org-scanner-tags' instead of building the list by itself. This
  9097. can lead to large speed-ups when the tags scanner is used in a file with
  9098. many entries, and when the list of tags is retrieved, for example to
  9099. obtain a list of properties. Building the tags list for each entry in such
  9100. a file becomes an N^2 operation - but with this variable set, it scales
  9101. as N.")
  9102. (defun org-scan-tags (action matcher &optional todo-only)
  9103. "Scan headline tags with inheritance and produce output ACTION.
  9104. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9105. or `agenda' to produce an entry list for an agenda view. It can also be
  9106. a Lisp form or a function that should be called at each matched headline, in
  9107. this case the return value is a list of all return values from these calls.
  9108. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9109. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9110. only lines with a TODO keyword are included in the output."
  9111. (require 'org-agenda)
  9112. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9113. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9114. (org-re
  9115. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9116. (props (list 'face 'default
  9117. 'done-face 'org-agenda-done
  9118. 'undone-face 'default
  9119. 'mouse-face 'highlight
  9120. 'org-not-done-regexp org-not-done-regexp
  9121. 'org-todo-regexp org-todo-regexp
  9122. 'keymap org-agenda-keymap
  9123. 'help-echo
  9124. (format "mouse-2 or RET jump to org file %s"
  9125. (abbreviate-file-name
  9126. (or (buffer-file-name (buffer-base-buffer))
  9127. (buffer-name (buffer-base-buffer)))))))
  9128. (case-fold-search nil)
  9129. (org-map-continue-from nil)
  9130. lspos tags tags-list
  9131. (tags-alist (list (cons 0 org-file-tags)))
  9132. (llast 0) rtn rtn1 level category i txt
  9133. todo marker entry priority)
  9134. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9135. (setq action (list 'lambda nil action)))
  9136. (save-excursion
  9137. (goto-char (point-min))
  9138. (when (eq action 'sparse-tree)
  9139. (org-overview)
  9140. (org-remove-occur-highlights))
  9141. (while (re-search-forward re nil t)
  9142. (catch :skip
  9143. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9144. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9145. (goto-char (setq lspos (1+ (match-beginning 0))))
  9146. (setq level (org-reduced-level (funcall outline-level))
  9147. category (org-get-category))
  9148. (setq i llast llast level)
  9149. ;; remove tag lists from same and sublevels
  9150. (while (>= i level)
  9151. (when (setq entry (assoc i tags-alist))
  9152. (setq tags-alist (delete entry tags-alist)))
  9153. (setq i (1- i)))
  9154. ;; add the next tags
  9155. (when tags
  9156. (setq tags (org-split-string tags ":")
  9157. tags-alist
  9158. (cons (cons level tags) tags-alist)))
  9159. ;; compile tags for current headline
  9160. (setq tags-list
  9161. (if org-use-tag-inheritance
  9162. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9163. tags)
  9164. org-scanner-tags tags-list)
  9165. (when org-use-tag-inheritance
  9166. (setcdr (car tags-alist)
  9167. (mapcar (lambda (x)
  9168. (setq x (copy-sequence x))
  9169. (org-add-prop-inherited x))
  9170. (cdar tags-alist))))
  9171. (when (and tags org-use-tag-inheritance
  9172. (or (not (eq t org-use-tag-inheritance))
  9173. org-tags-exclude-from-inheritance))
  9174. ;; selective inheritance, remove uninherited ones
  9175. (setcdr (car tags-alist)
  9176. (org-remove-uniherited-tags (cdar tags-alist))))
  9177. (when (and (or (not todo-only)
  9178. (and (member todo org-not-done-keywords)
  9179. (or (not org-agenda-tags-todo-honor-ignore-options)
  9180. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9181. (let ((case-fold-search t)) (eval matcher))
  9182. (or
  9183. (not (member org-archive-tag tags-list))
  9184. ;; we have an archive tag, should we use this anyway?
  9185. (or (not org-agenda-skip-archived-trees)
  9186. (and (eq action 'agenda) org-agenda-archives-mode))))
  9187. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9188. ;; select this headline
  9189. (cond
  9190. ((eq action 'sparse-tree)
  9191. (and org-highlight-sparse-tree-matches
  9192. (org-get-heading) (match-end 0)
  9193. (org-highlight-new-match
  9194. (match-beginning 0) (match-beginning 1)))
  9195. (org-show-context 'tags-tree))
  9196. ((eq action 'agenda)
  9197. (setq txt (org-format-agenda-item
  9198. ""
  9199. (concat
  9200. (if (eq org-tags-match-list-sublevels 'indented)
  9201. (make-string (1- level) ?.) "")
  9202. (org-get-heading))
  9203. category
  9204. tags-list
  9205. )
  9206. priority (org-get-priority txt))
  9207. (goto-char lspos)
  9208. (setq marker (org-agenda-new-marker))
  9209. (org-add-props txt props
  9210. 'org-marker marker 'org-hd-marker marker 'org-category category
  9211. 'todo-state todo
  9212. 'priority priority 'type "tagsmatch")
  9213. (push txt rtn))
  9214. ((functionp action)
  9215. (setq org-map-continue-from nil)
  9216. (save-excursion
  9217. (setq rtn1 (funcall action))
  9218. (push rtn1 rtn)))
  9219. (t (error "Invalid action")))
  9220. ;; if we are to skip sublevels, jump to end of subtree
  9221. (unless org-tags-match-list-sublevels
  9222. (org-end-of-subtree t)
  9223. (backward-char 1))))
  9224. ;; Get the correct position from where to continue
  9225. (if org-map-continue-from
  9226. (goto-char org-map-continue-from)
  9227. (and (= (point) lspos) (end-of-line 1)))))
  9228. (when (and (eq action 'sparse-tree)
  9229. (not org-sparse-tree-open-archived-trees))
  9230. (org-hide-archived-subtrees (point-min) (point-max)))
  9231. (nreverse rtn)))
  9232. (defun org-remove-uniherited-tags (tags)
  9233. "Remove all tags that are not inherited from the list TAGS."
  9234. (cond
  9235. ((eq org-use-tag-inheritance t)
  9236. (if org-tags-exclude-from-inheritance
  9237. (org-delete-all org-tags-exclude-from-inheritance tags)
  9238. tags))
  9239. ((not org-use-tag-inheritance) nil)
  9240. ((stringp org-use-tag-inheritance)
  9241. (delq nil (mapcar
  9242. (lambda (x)
  9243. (if (and (string-match org-use-tag-inheritance x)
  9244. (not (member x org-tags-exclude-from-inheritance)))
  9245. x nil))
  9246. tags)))
  9247. ((listp org-use-tag-inheritance)
  9248. (delq nil (mapcar
  9249. (lambda (x)
  9250. (if (member x org-use-tag-inheritance) x nil))
  9251. tags)))))
  9252. (defvar todo-only) ;; dynamically scoped
  9253. (defun org-match-sparse-tree (&optional todo-only match)
  9254. "Create a sparse tree according to tags string MATCH.
  9255. MATCH can contain positive and negative selection of tags, like
  9256. \"+WORK+URGENT-WITHBOSS\".
  9257. If optional argument TODO-ONLY is non-nil, only select lines that are
  9258. also TODO lines."
  9259. (interactive "P")
  9260. (org-prepare-agenda-buffers (list (current-buffer)))
  9261. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9262. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9263. (defvar org-cached-props nil)
  9264. (defun org-cached-entry-get (pom property)
  9265. (if (or (eq t org-use-property-inheritance)
  9266. (and (stringp org-use-property-inheritance)
  9267. (string-match org-use-property-inheritance property))
  9268. (and (listp org-use-property-inheritance)
  9269. (member property org-use-property-inheritance)))
  9270. ;; Caching is not possible, check it directly
  9271. (org-entry-get pom property 'inherit)
  9272. ;; Get all properties, so that we can do complicated checks easily
  9273. (cdr (assoc property (or org-cached-props
  9274. (setq org-cached-props
  9275. (org-entry-properties pom)))))))
  9276. (defun org-global-tags-completion-table (&optional files)
  9277. "Return the list of all tags in all agenda buffer/files."
  9278. (save-excursion
  9279. (org-uniquify
  9280. (delq nil
  9281. (apply 'append
  9282. (mapcar
  9283. (lambda (file)
  9284. (set-buffer (find-file-noselect file))
  9285. (append (org-get-buffer-tags)
  9286. (mapcar (lambda (x) (if (stringp (car-safe x))
  9287. (list (car-safe x)) nil))
  9288. org-tag-alist)))
  9289. (if (and files (car files))
  9290. files
  9291. (org-agenda-files))))))))
  9292. (defun org-make-tags-matcher (match)
  9293. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9294. ;; todo-only is scoped dynamically into this function, and the function
  9295. ;; may change it if the matcher asks for it.
  9296. (unless match
  9297. ;; Get a new match request, with completion
  9298. (let ((org-last-tags-completion-table
  9299. (org-global-tags-completion-table)))
  9300. (setq match (org-completing-read-no-ido
  9301. "Match: " 'org-tags-completion-function nil nil nil
  9302. 'org-tags-history))))
  9303. ;; Parse the string and create a lisp form
  9304. (let ((match0 match)
  9305. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9306. minus tag mm
  9307. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9308. orterms term orlist re-p str-p level-p level-op time-p
  9309. prop-p pn pv po cat-p gv rest)
  9310. (if (string-match "/+" match)
  9311. ;; match contains also a todo-matching request
  9312. (progn
  9313. (setq tagsmatch (substring match 0 (match-beginning 0))
  9314. todomatch (substring match (match-end 0)))
  9315. (if (string-match "^!" todomatch)
  9316. (setq todo-only t todomatch (substring todomatch 1)))
  9317. (if (string-match "^\\s-*$" todomatch)
  9318. (setq todomatch nil)))
  9319. ;; only matching tags
  9320. (setq tagsmatch match todomatch nil))
  9321. ;; Make the tags matcher
  9322. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9323. (setq tagsmatcher t)
  9324. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9325. (while (setq term (pop orterms))
  9326. (while (and (equal (substring term -1) "\\") orterms)
  9327. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9328. (while (string-match re term)
  9329. (setq rest (substring term (match-end 0))
  9330. minus (and (match-end 1)
  9331. (equal (match-string 1 term) "-"))
  9332. tag (match-string 2 term)
  9333. re-p (equal (string-to-char tag) ?{)
  9334. level-p (match-end 4)
  9335. prop-p (match-end 5)
  9336. mm (cond
  9337. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9338. (level-p
  9339. (setq level-op (org-op-to-function (match-string 3 term)))
  9340. `(,level-op level ,(string-to-number
  9341. (match-string 4 term))))
  9342. (prop-p
  9343. (setq pn (match-string 5 term)
  9344. po (match-string 6 term)
  9345. pv (match-string 7 term)
  9346. cat-p (equal pn "CATEGORY")
  9347. re-p (equal (string-to-char pv) ?{)
  9348. str-p (equal (string-to-char pv) ?\")
  9349. time-p (save-match-data
  9350. (string-match "^\"[[<].*[]>]\"$" pv))
  9351. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9352. (if time-p (setq pv (org-matcher-time pv)))
  9353. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9354. (cond
  9355. ((equal pn "CATEGORY")
  9356. (setq gv '(get-text-property (point) 'org-category)))
  9357. ((equal pn "TODO")
  9358. (setq gv 'todo))
  9359. (t
  9360. (setq gv `(org-cached-entry-get nil ,pn))))
  9361. (if re-p
  9362. (if (eq po 'org<>)
  9363. `(not (string-match ,pv (or ,gv "")))
  9364. `(string-match ,pv (or ,gv "")))
  9365. (if str-p
  9366. `(,po (or ,gv "") ,pv)
  9367. `(,po (string-to-number (or ,gv ""))
  9368. ,(string-to-number pv) ))))
  9369. (t `(member ,tag tags-list)))
  9370. mm (if minus (list 'not mm) mm)
  9371. term rest)
  9372. (push mm tagsmatcher))
  9373. (push (if (> (length tagsmatcher) 1)
  9374. (cons 'and tagsmatcher)
  9375. (car tagsmatcher))
  9376. orlist)
  9377. (setq tagsmatcher nil))
  9378. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9379. (setq tagsmatcher
  9380. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9381. ;; Make the todo matcher
  9382. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9383. (setq todomatcher t)
  9384. (setq orterms (org-split-string todomatch "|") orlist nil)
  9385. (while (setq term (pop orterms))
  9386. (while (string-match re term)
  9387. (setq minus (and (match-end 1)
  9388. (equal (match-string 1 term) "-"))
  9389. kwd (match-string 2 term)
  9390. re-p (equal (string-to-char kwd) ?{)
  9391. term (substring term (match-end 0))
  9392. mm (if re-p
  9393. `(string-match ,(substring kwd 1 -1) todo)
  9394. (list 'equal 'todo kwd))
  9395. mm (if minus (list 'not mm) mm))
  9396. (push mm todomatcher))
  9397. (push (if (> (length todomatcher) 1)
  9398. (cons 'and todomatcher)
  9399. (car todomatcher))
  9400. orlist)
  9401. (setq todomatcher nil))
  9402. (setq todomatcher (if (> (length orlist) 1)
  9403. (cons 'or orlist) (car orlist))))
  9404. ;; Return the string and lisp forms of the matcher
  9405. (setq matcher (if todomatcher
  9406. (list 'and tagsmatcher todomatcher)
  9407. tagsmatcher))
  9408. (cons match0 matcher)))
  9409. (defun org-op-to-function (op &optional stringp)
  9410. "Turn an operator into the appropriate function."
  9411. (setq op
  9412. (cond
  9413. ((equal op "<" ) '(< string< org-time<))
  9414. ((equal op ">" ) '(> org-string> org-time>))
  9415. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9416. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9417. ((member op '("=" "==")) '(= string= org-time=))
  9418. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9419. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9420. (defun org<> (a b) (not (= a b)))
  9421. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9422. (defun org-string>= (a b) (not (string< a b)))
  9423. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9424. (defun org-string<> (a b) (not (string= a b)))
  9425. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9426. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9427. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9428. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9429. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9430. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9431. (defun org-2ft (s)
  9432. "Convert S to a floating point time.
  9433. If S is already a number, just return it. If it is a string, parse
  9434. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9435. (cond
  9436. ((numberp s) s)
  9437. ((stringp s)
  9438. (condition-case nil
  9439. (float-time (apply 'encode-time (org-parse-time-string s)))
  9440. (error 0.)))
  9441. (t 0.)))
  9442. (defun org-time-today ()
  9443. "Time in seconds today at 0:00.
  9444. Returns the float number of seconds since the beginning of the
  9445. epoch to the beginning of today (00:00)."
  9446. (float-time (apply 'encode-time
  9447. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9448. (defun org-matcher-time (s)
  9449. "Interpret a time comparison value."
  9450. (save-match-data
  9451. (cond
  9452. ((string= s "<now>") (float-time))
  9453. ((string= s "<today>") (org-time-today))
  9454. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9455. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9456. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9457. (+ (org-time-today)
  9458. (* (string-to-number (match-string 1 s))
  9459. (cdr (assoc (match-string 2 s)
  9460. '(("d" . 86400.0) ("w" . 604800.0)
  9461. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9462. (t (org-2ft s)))))
  9463. (defun org-match-any-p (re list)
  9464. "Does re match any element of list?"
  9465. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9466. (delq nil list))
  9467. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9468. (defvar org-tags-overlay (org-make-overlay 1 1))
  9469. (org-detach-overlay org-tags-overlay)
  9470. (defun org-get-local-tags-at (&optional pos)
  9471. "Get a list of tags defined in the current headline."
  9472. (org-get-tags-at pos 'local))
  9473. (defun org-get-local-tags ()
  9474. "Get a list of tags defined in the current headline."
  9475. (org-get-tags-at nil 'local))
  9476. (defun org-get-tags-at (&optional pos local)
  9477. "Get a list of all headline tags applicable at POS.
  9478. POS defaults to point. If tags are inherited, the list contains
  9479. the targets in the same sequence as the headlines appear, i.e.
  9480. the tags of the current headline come last.
  9481. When LOCAL is non-nil, only return tags from the current headline,
  9482. ignore inherited ones."
  9483. (interactive)
  9484. (if (and org-trust-scanner-tags
  9485. (or (not pos) (equal pos (point)))
  9486. (not local))
  9487. org-scanner-tags
  9488. (let (tags ltags lastpos parent)
  9489. (save-excursion
  9490. (save-restriction
  9491. (widen)
  9492. (goto-char (or pos (point)))
  9493. (save-match-data
  9494. (catch 'done
  9495. (condition-case nil
  9496. (progn
  9497. (org-back-to-heading t)
  9498. (while (not (equal lastpos (point)))
  9499. (setq lastpos (point))
  9500. (when (looking-at
  9501. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9502. (setq ltags (org-split-string
  9503. (org-match-string-no-properties 1) ":"))
  9504. (when parent
  9505. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9506. (setq tags (append
  9507. (if parent
  9508. (org-remove-uniherited-tags ltags)
  9509. ltags)
  9510. tags)))
  9511. (or org-use-tag-inheritance (throw 'done t))
  9512. (if local (throw 'done t))
  9513. (org-up-heading-all 1)
  9514. (setq parent t)))
  9515. (error nil)))))
  9516. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9517. (defun org-add-prop-inherited (s)
  9518. (add-text-properties 0 (length s) '(inherited t) s)
  9519. s)
  9520. (defun org-toggle-tag (tag &optional onoff)
  9521. "Toggle the tag TAG for the current line.
  9522. If ONOFF is `on' or `off', don't toggle but set to this state."
  9523. (let (res current)
  9524. (save-excursion
  9525. (org-back-to-heading t)
  9526. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9527. (point-at-eol) t)
  9528. (progn
  9529. (setq current (match-string 1))
  9530. (replace-match ""))
  9531. (setq current ""))
  9532. (setq current (nreverse (org-split-string current ":")))
  9533. (cond
  9534. ((eq onoff 'on)
  9535. (setq res t)
  9536. (or (member tag current) (push tag current)))
  9537. ((eq onoff 'off)
  9538. (or (not (member tag current)) (setq current (delete tag current))))
  9539. (t (if (member tag current)
  9540. (setq current (delete tag current))
  9541. (setq res t)
  9542. (push tag current))))
  9543. (end-of-line 1)
  9544. (if current
  9545. (progn
  9546. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9547. (org-set-tags nil t))
  9548. (delete-horizontal-space))
  9549. (run-hooks 'org-after-tags-change-hook))
  9550. res))
  9551. (defun org-align-tags-here (to-col)
  9552. ;; Assumes that this is a headline
  9553. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9554. (beginning-of-line 1)
  9555. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9556. (< pos (match-beginning 2)))
  9557. (progn
  9558. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9559. (goto-char (match-beginning 1))
  9560. (insert " ")
  9561. (delete-region (point) (1+ (match-beginning 2)))
  9562. (setq ncol (max (1+ (current-column))
  9563. (1+ col)
  9564. (if (> to-col 0)
  9565. to-col
  9566. (- (abs to-col) tags-l))))
  9567. (setq p (point))
  9568. (insert (make-string (- ncol (current-column)) ?\ ))
  9569. (setq ncol (current-column))
  9570. (when indent-tabs-mode (tabify p (point-at-eol)))
  9571. (org-move-to-column (min ncol col) t))
  9572. (goto-char pos))))
  9573. (defun org-set-tags-command (&optional arg just-align)
  9574. "Call the set-tags command for the current entry."
  9575. (interactive "P")
  9576. (if (org-on-heading-p)
  9577. (org-set-tags arg just-align)
  9578. (save-excursion
  9579. (org-back-to-heading t)
  9580. (org-set-tags arg just-align))))
  9581. (defun org-set-tags (&optional arg just-align)
  9582. "Set the tags for the current headline.
  9583. With prefix ARG, realign all tags in headings in the current buffer."
  9584. (interactive "P")
  9585. (let* ((re (concat "^" outline-regexp))
  9586. (current (org-get-tags-string))
  9587. (col (current-column))
  9588. (org-setting-tags t)
  9589. table current-tags inherited-tags ; computed below when needed
  9590. tags p0 c0 c1 rpl)
  9591. (if arg
  9592. (save-excursion
  9593. (goto-char (point-min))
  9594. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9595. (while (re-search-forward re nil t)
  9596. (org-set-tags nil t)
  9597. (end-of-line 1)))
  9598. (message "All tags realigned to column %d" org-tags-column))
  9599. (if just-align
  9600. (setq tags current)
  9601. ;; Get a new set of tags from the user
  9602. (save-excursion
  9603. (setq table (append org-tag-persistent-alist
  9604. (or org-tag-alist (org-get-buffer-tags)))
  9605. org-last-tags-completion-table table
  9606. current-tags (org-split-string current ":")
  9607. inherited-tags (nreverse
  9608. (nthcdr (length current-tags)
  9609. (nreverse (org-get-tags-at))))
  9610. tags
  9611. (if (or (eq t org-use-fast-tag-selection)
  9612. (and org-use-fast-tag-selection
  9613. (delq nil (mapcar 'cdr table))))
  9614. (org-fast-tag-selection
  9615. current-tags inherited-tags table
  9616. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9617. (let ((org-add-colon-after-tag-completion t))
  9618. (org-trim
  9619. (org-without-partial-completion
  9620. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9621. nil nil current 'org-tags-history)))))))
  9622. (while (string-match "[-+&]+" tags)
  9623. ;; No boolean logic, just a list
  9624. (setq tags (replace-match ":" t t tags))))
  9625. (if (string-match "\\`[\t ]*\\'" tags)
  9626. (setq tags "")
  9627. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9628. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9629. ;; Insert new tags at the correct column
  9630. (beginning-of-line 1)
  9631. (cond
  9632. ((and (equal current "") (equal tags "")))
  9633. ((re-search-forward
  9634. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9635. (point-at-eol) t)
  9636. (if (equal tags "")
  9637. (setq rpl "")
  9638. (goto-char (match-beginning 0))
  9639. (setq c0 (current-column) p0 (point)
  9640. c1 (max (1+ c0) (if (> org-tags-column 0)
  9641. org-tags-column
  9642. (- (- org-tags-column) (length tags))))
  9643. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9644. (replace-match rpl t t)
  9645. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9646. tags)
  9647. (t (error "Tags alignment failed")))
  9648. (org-move-to-column col)
  9649. (unless just-align
  9650. (run-hooks 'org-after-tags-change-hook)))))
  9651. (defun org-change-tag-in-region (beg end tag off)
  9652. "Add or remove TAG for each entry in the region.
  9653. This works in the agenda, and also in an org-mode buffer."
  9654. (interactive
  9655. (list (region-beginning) (region-end)
  9656. (let ((org-last-tags-completion-table
  9657. (if (org-mode-p)
  9658. (org-get-buffer-tags)
  9659. (org-global-tags-completion-table))))
  9660. (org-ido-completing-read
  9661. "Tag: " 'org-tags-completion-function nil nil nil
  9662. 'org-tags-history))
  9663. (progn
  9664. (message "[s]et or [r]emove? ")
  9665. (equal (read-char-exclusive) ?r))))
  9666. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9667. (let ((agendap (equal major-mode 'org-agenda-mode))
  9668. l1 l2 m buf pos newhead (cnt 0))
  9669. (goto-char end)
  9670. (setq l2 (1- (org-current-line)))
  9671. (goto-char beg)
  9672. (setq l1 (org-current-line))
  9673. (loop for l from l1 to l2 do
  9674. (goto-line l)
  9675. (setq m (get-text-property (point) 'org-hd-marker))
  9676. (when (or (and (org-mode-p) (org-on-heading-p))
  9677. (and agendap m))
  9678. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9679. pos (if agendap m (point)))
  9680. (with-current-buffer buf
  9681. (save-excursion
  9682. (save-restriction
  9683. (goto-char pos)
  9684. (setq cnt (1+ cnt))
  9685. (org-toggle-tag tag (if off 'off 'on))
  9686. (setq newhead (org-get-heading)))))
  9687. (and agendap (org-agenda-change-all-lines newhead m))))
  9688. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9689. (defun org-tags-completion-function (string predicate &optional flag)
  9690. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9691. (confirm (lambda (x) (stringp (car x)))))
  9692. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9693. (setq s1 (match-string 1 string)
  9694. s2 (match-string 2 string))
  9695. (setq s1 "" s2 string))
  9696. (cond
  9697. ((eq flag nil)
  9698. ;; try completion
  9699. (setq rtn (try-completion s2 ctable confirm))
  9700. (if (stringp rtn)
  9701. (setq rtn
  9702. (concat s1 s2 (substring rtn (length s2))
  9703. (if (and org-add-colon-after-tag-completion
  9704. (assoc rtn ctable))
  9705. ":" ""))))
  9706. rtn)
  9707. ((eq flag t)
  9708. ;; all-completions
  9709. (all-completions s2 ctable confirm)
  9710. )
  9711. ((eq flag 'lambda)
  9712. ;; exact match?
  9713. (assoc s2 ctable)))
  9714. ))
  9715. (defun org-fast-tag-insert (kwd tags face &optional end)
  9716. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9717. (insert (format "%-12s" (concat kwd ":"))
  9718. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9719. (or end "")))
  9720. (defun org-fast-tag-show-exit (flag)
  9721. (save-excursion
  9722. (goto-line 3)
  9723. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9724. (replace-match ""))
  9725. (when flag
  9726. (end-of-line 1)
  9727. (org-move-to-column (- (window-width) 19) t)
  9728. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9729. (defun org-set-current-tags-overlay (current prefix)
  9730. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9731. (if (featurep 'xemacs)
  9732. (org-overlay-display org-tags-overlay (concat prefix s)
  9733. 'secondary-selection)
  9734. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9735. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9736. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9737. "Fast tag selection with single keys.
  9738. CURRENT is the current list of tags in the headline, INHERITED is the
  9739. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9740. possibly with grouping information. TODO-TABLE is a similar table with
  9741. TODO keywords, should these have keys assigned to them.
  9742. If the keys are nil, a-z are automatically assigned.
  9743. Returns the new tags string, or nil to not change the current settings."
  9744. (let* ((fulltable (append table todo-table))
  9745. (maxlen (apply 'max (mapcar
  9746. (lambda (x)
  9747. (if (stringp (car x)) (string-width (car x)) 0))
  9748. fulltable)))
  9749. (buf (current-buffer))
  9750. (expert (eq org-fast-tag-selection-single-key 'expert))
  9751. (buffer-tags nil)
  9752. (fwidth (+ maxlen 3 1 3))
  9753. (ncol (/ (- (window-width) 4) fwidth))
  9754. (i-face 'org-done)
  9755. (c-face 'org-todo)
  9756. tg cnt e c char c1 c2 ntable tbl rtn
  9757. ov-start ov-end ov-prefix
  9758. (exit-after-next org-fast-tag-selection-single-key)
  9759. (done-keywords org-done-keywords)
  9760. groups ingroup)
  9761. (save-excursion
  9762. (beginning-of-line 1)
  9763. (if (looking-at
  9764. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9765. (setq ov-start (match-beginning 1)
  9766. ov-end (match-end 1)
  9767. ov-prefix "")
  9768. (setq ov-start (1- (point-at-eol))
  9769. ov-end (1+ ov-start))
  9770. (skip-chars-forward "^\n\r")
  9771. (setq ov-prefix
  9772. (concat
  9773. (buffer-substring (1- (point)) (point))
  9774. (if (> (current-column) org-tags-column)
  9775. " "
  9776. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9777. (org-move-overlay org-tags-overlay ov-start ov-end)
  9778. (save-window-excursion
  9779. (if expert
  9780. (set-buffer (get-buffer-create " *Org tags*"))
  9781. (delete-other-windows)
  9782. (split-window-vertically)
  9783. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9784. (erase-buffer)
  9785. (org-set-local 'org-done-keywords done-keywords)
  9786. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9787. (org-fast-tag-insert "Current" current c-face "\n\n")
  9788. (org-fast-tag-show-exit exit-after-next)
  9789. (org-set-current-tags-overlay current ov-prefix)
  9790. (setq tbl fulltable char ?a cnt 0)
  9791. (while (setq e (pop tbl))
  9792. (cond
  9793. ((equal e '(:startgroup))
  9794. (push '() groups) (setq ingroup t)
  9795. (when (not (= cnt 0))
  9796. (setq cnt 0)
  9797. (insert "\n"))
  9798. (insert "{ "))
  9799. ((equal e '(:endgroup))
  9800. (setq ingroup nil cnt 0)
  9801. (insert "}\n"))
  9802. ((equal e '(:newline))
  9803. (when (not (= cnt 0))
  9804. (setq cnt 0)
  9805. (insert "\n")
  9806. (setq e (car tbl))
  9807. (while (equal (car tbl) '(:newline))
  9808. (insert "\n")
  9809. (setq tbl (cdr tbl)))))
  9810. (t
  9811. (setq tg (car e) c2 nil)
  9812. (if (cdr e)
  9813. (setq c (cdr e))
  9814. ;; automatically assign a character.
  9815. (setq c1 (string-to-char
  9816. (downcase (substring
  9817. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9818. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9819. (while (or (rassoc char ntable) (rassoc char table))
  9820. (setq char (1+ char)))
  9821. (setq c2 c1))
  9822. (setq c (or c2 char)))
  9823. (if ingroup (push tg (car groups)))
  9824. (setq tg (org-add-props tg nil 'face
  9825. (cond
  9826. ((not (assoc tg table))
  9827. (org-get-todo-face tg))
  9828. ((member tg current) c-face)
  9829. ((member tg inherited) i-face)
  9830. (t nil))))
  9831. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9832. (insert "[" c "] " tg (make-string
  9833. (- fwidth 4 (length tg)) ?\ ))
  9834. (push (cons tg c) ntable)
  9835. (when (= (setq cnt (1+ cnt)) ncol)
  9836. (insert "\n")
  9837. (if ingroup (insert " "))
  9838. (setq cnt 0)))))
  9839. (setq ntable (nreverse ntable))
  9840. (insert "\n")
  9841. (goto-char (point-min))
  9842. (if (not expert) (org-fit-window-to-buffer))
  9843. (setq rtn
  9844. (catch 'exit
  9845. (while t
  9846. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9847. (if groups " [!] no groups" " [!]groups")
  9848. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9849. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9850. (cond
  9851. ((= c ?\r) (throw 'exit t))
  9852. ((= c ?!)
  9853. (setq groups (not groups))
  9854. (goto-char (point-min))
  9855. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9856. ((= c ?\C-c)
  9857. (if (not expert)
  9858. (org-fast-tag-show-exit
  9859. (setq exit-after-next (not exit-after-next)))
  9860. (setq expert nil)
  9861. (delete-other-windows)
  9862. (split-window-vertically)
  9863. (org-switch-to-buffer-other-window " *Org tags*")
  9864. (org-fit-window-to-buffer)))
  9865. ((or (= c ?\C-g)
  9866. (and (= c ?q) (not (rassoc c ntable))))
  9867. (org-detach-overlay org-tags-overlay)
  9868. (setq quit-flag t))
  9869. ((= c ?\ )
  9870. (setq current nil)
  9871. (if exit-after-next (setq exit-after-next 'now)))
  9872. ((= c ?\t)
  9873. (condition-case nil
  9874. (setq tg (org-ido-completing-read
  9875. "Tag: "
  9876. (or buffer-tags
  9877. (with-current-buffer buf
  9878. (org-get-buffer-tags)))))
  9879. (quit (setq tg "")))
  9880. (when (string-match "\\S-" tg)
  9881. (add-to-list 'buffer-tags (list tg))
  9882. (if (member tg current)
  9883. (setq current (delete tg current))
  9884. (push tg current)))
  9885. (if exit-after-next (setq exit-after-next 'now)))
  9886. ((setq e (rassoc c todo-table) tg (car e))
  9887. (with-current-buffer buf
  9888. (save-excursion (org-todo tg)))
  9889. (if exit-after-next (setq exit-after-next 'now)))
  9890. ((setq e (rassoc c ntable) tg (car e))
  9891. (if (member tg current)
  9892. (setq current (delete tg current))
  9893. (loop for g in groups do
  9894. (if (member tg g)
  9895. (mapc (lambda (x)
  9896. (setq current (delete x current)))
  9897. g)))
  9898. (push tg current))
  9899. (if exit-after-next (setq exit-after-next 'now))))
  9900. ;; Create a sorted list
  9901. (setq current
  9902. (sort current
  9903. (lambda (a b)
  9904. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9905. (if (eq exit-after-next 'now) (throw 'exit t))
  9906. (goto-char (point-min))
  9907. (beginning-of-line 2)
  9908. (delete-region (point) (point-at-eol))
  9909. (org-fast-tag-insert "Current" current c-face)
  9910. (org-set-current-tags-overlay current ov-prefix)
  9911. (while (re-search-forward
  9912. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9913. (setq tg (match-string 1))
  9914. (add-text-properties
  9915. (match-beginning 1) (match-end 1)
  9916. (list 'face
  9917. (cond
  9918. ((member tg current) c-face)
  9919. ((member tg inherited) i-face)
  9920. (t (get-text-property (match-beginning 1) 'face))))))
  9921. (goto-char (point-min)))))
  9922. (org-detach-overlay org-tags-overlay)
  9923. (if rtn
  9924. (mapconcat 'identity current ":")
  9925. nil))))
  9926. (defun org-get-tags-string ()
  9927. "Get the TAGS string in the current headline."
  9928. (unless (org-on-heading-p t)
  9929. (error "Not on a heading"))
  9930. (save-excursion
  9931. (beginning-of-line 1)
  9932. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9933. (org-match-string-no-properties 1)
  9934. "")))
  9935. (defun org-get-tags ()
  9936. "Get the list of tags specified in the current headline."
  9937. (org-split-string (org-get-tags-string) ":"))
  9938. (defun org-get-buffer-tags ()
  9939. "Get a table of all tags used in the buffer, for completion."
  9940. (let (tags)
  9941. (save-excursion
  9942. (goto-char (point-min))
  9943. (while (re-search-forward
  9944. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9945. (when (equal (char-after (point-at-bol 0)) ?*)
  9946. (mapc (lambda (x) (add-to-list 'tags x))
  9947. (org-split-string (org-match-string-no-properties 1) ":")))))
  9948. (mapcar 'list tags)))
  9949. ;;;; The mapping API
  9950. ;;;###autoload
  9951. (defun org-map-entries (func &optional match scope &rest skip)
  9952. "Call FUNC at each headline selected by MATCH in SCOPE.
  9953. FUNC is a function or a lisp form. The function will be called without
  9954. arguments, with the cursor positioned at the beginning of the headline.
  9955. The return values of all calls to the function will be collected and
  9956. returned as a list.
  9957. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  9958. does not need to preserve point. After evaluaton, the cursor will be
  9959. moved to the end of the line (presumably of the headline of the
  9960. processed entry) and search continues from there. Under some
  9961. circumstances, this may not produce the wanted results. For example,
  9962. if you have removed (e.g. archived) the current (sub)tree it could
  9963. mean that the next entry will be skipped entirely. In such cases, you
  9964. can specify the position from where search should continue by making
  9965. FUNC set the variable `org-map-continue-from' to the desired buffer
  9966. position.
  9967. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9968. Only headlines that are matched by this query will be considered during
  9969. the iteration. When MATCH is nil or t, all headlines will be
  9970. visited by the iteration.
  9971. SCOPE determines the scope of this command. It can be any of:
  9972. nil The current buffer, respecting the restriction if any
  9973. tree The subtree started with the entry at point
  9974. file The current buffer, without restriction
  9975. file-with-archives
  9976. The current buffer, and any archives associated with it
  9977. agenda All agenda files
  9978. agenda-with-archives
  9979. All agenda files with any archive files associated with them
  9980. \(file1 file2 ...)
  9981. If this is a list, all files in the list will be scanned
  9982. The remaining args are treated as settings for the skipping facilities of
  9983. the scanner. The following items can be given here:
  9984. archive skip trees with the archive tag.
  9985. comment skip trees with the COMMENT keyword
  9986. function or Emacs Lisp form:
  9987. will be used as value for `org-agenda-skip-function', so whenever
  9988. the the function returns t, FUNC will not be called for that
  9989. entry and search will continue from the point where the
  9990. function leaves it.
  9991. If your function needs to retrieve the tags including inherited tags
  9992. at the *current* entry, you can use the value of the variable
  9993. `org-scanner-tags' which will be much faster than getting the value
  9994. with `org-get-tags-at'. If your function gets properties with
  9995. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  9996. to t around the call to `org-entry-properties' to get the same speedup.
  9997. Note that if your function moves around to retrieve tags and properties at
  9998. a *different* entry, you cannot use these techniques."
  9999. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10000. (org-agenda-skip-archived-trees (memq 'archive skip))
  10001. (org-agenda-skip-comment-trees (memq 'comment skip))
  10002. (org-agenda-skip-function
  10003. (car (org-delete-all '(comment archive) skip)))
  10004. (org-tags-match-list-sublevels t)
  10005. matcher file res
  10006. org-todo-keywords-for-agenda
  10007. org-done-keywords-for-agenda
  10008. org-todo-keyword-alist-for-agenda
  10009. org-tag-alist-for-agenda)
  10010. (cond
  10011. ((eq match t) (setq matcher t))
  10012. ((eq match nil) (setq matcher t))
  10013. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10014. (save-excursion
  10015. (save-restriction
  10016. (when (eq scope 'tree)
  10017. (org-back-to-heading t)
  10018. (org-narrow-to-subtree)
  10019. (setq scope nil))
  10020. (if (not scope)
  10021. (progn
  10022. (org-prepare-agenda-buffers
  10023. (list (buffer-file-name (current-buffer))))
  10024. (setq res (org-scan-tags func matcher)))
  10025. ;; Get the right scope
  10026. (cond
  10027. ((and scope (listp scope) (symbolp (car scope)))
  10028. (setq scope (eval scope)))
  10029. ((eq scope 'agenda)
  10030. (setq scope (org-agenda-files t)))
  10031. ((eq scope 'agenda-with-archives)
  10032. (setq scope (org-agenda-files t))
  10033. (setq scope (org-add-archive-files scope)))
  10034. ((eq scope 'file)
  10035. (setq scope (list (buffer-file-name))))
  10036. ((eq scope 'file-with-archives)
  10037. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10038. (org-prepare-agenda-buffers scope)
  10039. (while (setq file (pop scope))
  10040. (with-current-buffer (org-find-base-buffer-visiting file)
  10041. (save-excursion
  10042. (save-restriction
  10043. (widen)
  10044. (goto-char (point-min))
  10045. (setq res (append res (org-scan-tags func matcher))))))))))
  10046. res))
  10047. ;;;; Properties
  10048. ;;; Setting and retrieving properties
  10049. (defconst org-special-properties
  10050. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10051. "TIMESTAMP" "TIMESTAMP_IA")
  10052. "The special properties valid in Org-mode.
  10053. These are properties that are not defined in the property drawer,
  10054. but in some other way.")
  10055. (defconst org-default-properties
  10056. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  10057. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10058. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10059. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  10060. "Some properties that are used by Org-mode for various purposes.
  10061. Being in this list makes sure that they are offered for completion.")
  10062. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10063. "Regular expression matching the first line of a property drawer.")
  10064. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10065. "Regular expression matching the first line of a property drawer.")
  10066. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10067. "Regular expression matching the first line of a property drawer.")
  10068. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10069. "Regular expression matching the first line of a property drawer.")
  10070. (defconst org-property-drawer-re
  10071. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10072. org-property-end-re "\\)\n?")
  10073. "Matches an entire property drawer.")
  10074. (defconst org-clock-drawer-re
  10075. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10076. org-property-end-re "\\)\n?")
  10077. "Matches an entire clock drawer.")
  10078. (defun org-property-action ()
  10079. "Do an action on properties."
  10080. (interactive)
  10081. (let (c)
  10082. (org-at-property-p)
  10083. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10084. (setq c (read-char-exclusive))
  10085. (cond
  10086. ((equal c ?s)
  10087. (call-interactively 'org-set-property))
  10088. ((equal c ?d)
  10089. (call-interactively 'org-delete-property))
  10090. ((equal c ?D)
  10091. (call-interactively 'org-delete-property-globally))
  10092. ((equal c ?c)
  10093. (call-interactively 'org-compute-property-at-point))
  10094. (t (error "No such property action %c" c)))))
  10095. (defun org-at-property-p ()
  10096. "Is the cursor in a property line?"
  10097. ;; FIXME: Does not check if we are actually in the drawer.
  10098. ;; FIXME: also returns true on any drawers.....
  10099. ;; This is used by C-c C-c for property action.
  10100. (save-excursion
  10101. (beginning-of-line 1)
  10102. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10103. (defun org-get-property-block (&optional beg end force)
  10104. "Return the (beg . end) range of the body of the property drawer.
  10105. BEG and END can be beginning and end of subtree, if not given
  10106. they will be found.
  10107. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10108. (catch 'exit
  10109. (save-excursion
  10110. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10111. (end (or end (progn (outline-next-heading) (point)))))
  10112. (goto-char beg)
  10113. (if (re-search-forward org-property-start-re end t)
  10114. (setq beg (1+ (match-end 0)))
  10115. (if force
  10116. (save-excursion
  10117. (org-insert-property-drawer)
  10118. (setq end (progn (outline-next-heading) (point))))
  10119. (throw 'exit nil))
  10120. (goto-char beg)
  10121. (if (re-search-forward org-property-start-re end t)
  10122. (setq beg (1+ (match-end 0)))))
  10123. (if (re-search-forward org-property-end-re end t)
  10124. (setq end (match-beginning 0))
  10125. (or force (throw 'exit nil))
  10126. (goto-char beg)
  10127. (setq end beg)
  10128. (org-indent-line-function)
  10129. (insert ":END:\n"))
  10130. (cons beg end)))))
  10131. (defun org-entry-properties (&optional pom which)
  10132. "Get all properties of the entry at point-or-marker POM.
  10133. This includes the TODO keyword, the tags, time strings for deadline,
  10134. scheduled, and clocking, and any additional properties defined in the
  10135. entry. The return value is an alist, keys may occur multiple times
  10136. if the property key was used several times.
  10137. POM may also be nil, in which case the current entry is used.
  10138. If WHICH is nil or `all', get all properties. If WHICH is
  10139. `special' or `standard', only get that subclass."
  10140. (setq which (or which 'all))
  10141. (org-with-point-at pom
  10142. (let ((clockstr (substring org-clock-string 0 -1))
  10143. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10144. beg end range props sum-props key value string clocksum)
  10145. (save-excursion
  10146. (when (condition-case nil
  10147. (and (org-mode-p) (org-back-to-heading t))
  10148. (error nil))
  10149. (setq beg (point))
  10150. (setq sum-props (get-text-property (point) 'org-summaries))
  10151. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10152. (outline-next-heading)
  10153. (setq end (point))
  10154. (when (memq which '(all special))
  10155. ;; Get the special properties, like TODO and tags
  10156. (goto-char beg)
  10157. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10158. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10159. (when (looking-at org-priority-regexp)
  10160. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10161. (when (and (setq value (org-get-tags-string))
  10162. (string-match "\\S-" value))
  10163. (push (cons "TAGS" value) props))
  10164. (when (setq value (org-get-tags-at))
  10165. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10166. props))
  10167. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10168. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10169. string (if (equal key clockstr)
  10170. (org-no-properties
  10171. (org-trim
  10172. (buffer-substring
  10173. (match-beginning 3) (goto-char (point-at-eol)))))
  10174. (substring (org-match-string-no-properties 3) 1 -1)))
  10175. (unless key
  10176. (if (= (char-after (match-beginning 3)) ?\[)
  10177. (setq key "TIMESTAMP_IA")
  10178. (setq key "TIMESTAMP")))
  10179. (when (or (equal key clockstr) (not (assoc key props)))
  10180. (push (cons key string) props)))
  10181. )
  10182. (when (memq which '(all standard))
  10183. ;; Get the standard properties, like :PROP: ...
  10184. (setq range (org-get-property-block beg end))
  10185. (when range
  10186. (goto-char (car range))
  10187. (while (re-search-forward
  10188. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10189. (cdr range) t)
  10190. (setq key (org-match-string-no-properties 1)
  10191. value (org-trim (or (org-match-string-no-properties 2) "")))
  10192. (unless (member key excluded)
  10193. (push (cons key (or value "")) props)))))
  10194. (if clocksum
  10195. (push (cons "CLOCKSUM"
  10196. (org-columns-number-to-string (/ (float clocksum) 60.)
  10197. 'add_times))
  10198. props))
  10199. (unless (assoc "CATEGORY" props)
  10200. (setq value (or (org-get-category)
  10201. (progn (org-refresh-category-properties)
  10202. (org-get-category))))
  10203. (push (cons "CATEGORY" value) props))
  10204. (append sum-props (nreverse props)))))))
  10205. (defun org-entry-get (pom property &optional inherit)
  10206. "Get value of PROPERTY for entry at point-or-marker POM.
  10207. If INHERIT is non-nil and the entry does not have the property,
  10208. then also check higher levels of the hierarchy.
  10209. If INHERIT is the symbol `selective', use inheritance only if the setting
  10210. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10211. If the property is present but empty, the return value is the empty string.
  10212. If the property is not present at all, nil is returned."
  10213. (org-with-point-at pom
  10214. (if (and inherit (if (eq inherit 'selective)
  10215. (org-property-inherit-p property)
  10216. t))
  10217. (org-entry-get-with-inheritance property)
  10218. (if (member property org-special-properties)
  10219. ;; We need a special property. Use brute force, get all properties.
  10220. (cdr (assoc property (org-entry-properties nil 'special)))
  10221. (let ((range (org-get-property-block)))
  10222. (if (and range
  10223. (goto-char (car range))
  10224. (re-search-forward
  10225. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10226. (cdr range) t))
  10227. ;; Found the property, return it.
  10228. (if (match-end 1)
  10229. (org-match-string-no-properties 1)
  10230. "")))))))
  10231. (defun org-property-or-variable-value (var &optional inherit)
  10232. "Check if there is a property fixing the value of VAR.
  10233. If yes, return this value. If not, return the current value of the variable."
  10234. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10235. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10236. (read prop)
  10237. (symbol-value var))))
  10238. (defun org-entry-delete (pom property)
  10239. "Delete the property PROPERTY from entry at point-or-marker POM."
  10240. (org-with-point-at pom
  10241. (if (member property org-special-properties)
  10242. nil ; cannot delete these properties.
  10243. (let ((range (org-get-property-block)))
  10244. (if (and range
  10245. (goto-char (car range))
  10246. (re-search-forward
  10247. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10248. (cdr range) t))
  10249. (progn
  10250. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10251. t)
  10252. nil)))))
  10253. ;; Multi-values properties are properties that contain multiple values
  10254. ;; These values are assumed to be single words, separated by whitespace.
  10255. (defun org-entry-add-to-multivalued-property (pom property value)
  10256. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10257. (let* ((old (org-entry-get pom property))
  10258. (values (and old (org-split-string old "[ \t]"))))
  10259. (setq value (org-entry-protect-space value))
  10260. (unless (member value values)
  10261. (setq values (cons value values))
  10262. (org-entry-put pom property
  10263. (mapconcat 'identity values " ")))))
  10264. (defun org-entry-remove-from-multivalued-property (pom property value)
  10265. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10266. (let* ((old (org-entry-get pom property))
  10267. (values (and old (org-split-string old "[ \t]"))))
  10268. (setq value (org-entry-protect-space value))
  10269. (when (member value values)
  10270. (setq values (delete value values))
  10271. (org-entry-put pom property
  10272. (mapconcat 'identity values " ")))))
  10273. (defun org-entry-member-in-multivalued-property (pom property value)
  10274. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10275. (let* ((old (org-entry-get pom property))
  10276. (values (and old (org-split-string old "[ \t]"))))
  10277. (setq value (org-entry-protect-space value))
  10278. (member value values)))
  10279. (defun org-entry-get-multivalued-property (pom property)
  10280. "Return a list of values in a multivalued property."
  10281. (let* ((value (org-entry-get pom property))
  10282. (values (and value (org-split-string value "[ \t]"))))
  10283. (mapcar 'org-entry-restore-space values)))
  10284. (defun org-entry-put-multivalued-property (pom property &rest values)
  10285. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10286. VALUES should be a list of strings. Spaces will be protected."
  10287. (org-entry-put pom property
  10288. (mapconcat 'org-entry-protect-space values " "))
  10289. (let* ((value (org-entry-get pom property))
  10290. (values (and value (org-split-string value "[ \t]"))))
  10291. (mapcar 'org-entry-restore-space values)))
  10292. (defun org-entry-protect-space (s)
  10293. "Protect spaces and newline in string S."
  10294. (while (string-match " " s)
  10295. (setq s (replace-match "%20" t t s)))
  10296. (while (string-match "\n" s)
  10297. (setq s (replace-match "%0A" t t s)))
  10298. s)
  10299. (defun org-entry-restore-space (s)
  10300. "Restore spaces and newline in string S."
  10301. (while (string-match "%20" s)
  10302. (setq s (replace-match " " t t s)))
  10303. (while (string-match "%0A" s)
  10304. (setq s (replace-match "\n" t t s)))
  10305. s)
  10306. (defvar org-entry-property-inherited-from (make-marker)
  10307. "Marker pointing to the entry from where a property was inherited.
  10308. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10309. location of the entry where the inheritance search matched. If there was
  10310. no match, the marker will point nowhere.
  10311. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10312. is set.")
  10313. (defun org-entry-get-with-inheritance (property)
  10314. "Get entry property, and search higher levels if not present."
  10315. (move-marker org-entry-property-inherited-from nil)
  10316. (let (tmp)
  10317. (save-excursion
  10318. (save-restriction
  10319. (widen)
  10320. (catch 'ex
  10321. (while t
  10322. (when (setq tmp (org-entry-get nil property))
  10323. (org-back-to-heading t)
  10324. (move-marker org-entry-property-inherited-from (point))
  10325. (throw 'ex tmp))
  10326. (or (org-up-heading-safe) (throw 'ex nil)))))
  10327. (or tmp
  10328. (cdr (assoc property org-file-properties))
  10329. (cdr (assoc property org-global-properties))
  10330. (cdr (assoc property org-global-properties-fixed))))))
  10331. (defun org-entry-put (pom property value)
  10332. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10333. (org-with-point-at pom
  10334. (org-back-to-heading t)
  10335. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10336. range)
  10337. (cond
  10338. ((equal property "TODO")
  10339. (when (and (stringp value) (string-match "\\S-" value)
  10340. (not (member value org-todo-keywords-1)))
  10341. (error "\"%s\" is not a valid TODO state" value))
  10342. (if (or (not value)
  10343. (not (string-match "\\S-" value)))
  10344. (setq value 'none))
  10345. (org-todo value)
  10346. (org-set-tags nil 'align))
  10347. ((equal property "PRIORITY")
  10348. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10349. (string-to-char value) ?\ ))
  10350. (org-set-tags nil 'align))
  10351. ((equal property "SCHEDULED")
  10352. (if (re-search-forward org-scheduled-time-regexp end t)
  10353. (cond
  10354. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10355. ((eq value 'later) (org-timestamp-change 1 'day))
  10356. (t (call-interactively 'org-schedule)))
  10357. (call-interactively 'org-schedule)))
  10358. ((equal property "DEADLINE")
  10359. (if (re-search-forward org-deadline-time-regexp end t)
  10360. (cond
  10361. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10362. ((eq value 'later) (org-timestamp-change 1 'day))
  10363. (t (call-interactively 'org-deadline)))
  10364. (call-interactively 'org-deadline)))
  10365. ((member property org-special-properties)
  10366. (error "The %s property can not yet be set with `org-entry-put'"
  10367. property))
  10368. (t ; a non-special property
  10369. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10370. (setq range (org-get-property-block beg end 'force))
  10371. (goto-char (car range))
  10372. (if (re-search-forward
  10373. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10374. (progn
  10375. (delete-region (match-beginning 1) (match-end 1))
  10376. (goto-char (match-beginning 1)))
  10377. (goto-char (cdr range))
  10378. (insert "\n")
  10379. (backward-char 1)
  10380. (org-indent-line-function)
  10381. (insert ":" property ":"))
  10382. (and value (insert " " value))
  10383. (org-indent-line-function)))))))
  10384. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10385. "Get all property keys in the current buffer.
  10386. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10387. like tags and TODO state.
  10388. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10389. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10390. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10391. formats in the current buffer."
  10392. (let (rtn range cfmt s p)
  10393. (save-excursion
  10394. (save-restriction
  10395. (widen)
  10396. (goto-char (point-min))
  10397. (while (re-search-forward org-property-start-re nil t)
  10398. (setq range (org-get-property-block))
  10399. (goto-char (car range))
  10400. (while (re-search-forward
  10401. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10402. (cdr range) t)
  10403. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10404. (outline-next-heading))))
  10405. (when include-specials
  10406. (setq rtn (append org-special-properties rtn)))
  10407. (when include-defaults
  10408. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10409. (when include-columns
  10410. (save-excursion
  10411. (save-restriction
  10412. (widen)
  10413. (goto-char (point-min))
  10414. (while (re-search-forward
  10415. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10416. nil t)
  10417. (setq cfmt (match-string 2) s 0)
  10418. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10419. cfmt s)
  10420. (setq s (match-end 0)
  10421. p (match-string 1 cfmt))
  10422. (unless (or (equal p "ITEM")
  10423. (member p org-special-properties))
  10424. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10425. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10426. (defun org-property-values (key)
  10427. "Return a list of all values of property KEY."
  10428. (save-excursion
  10429. (save-restriction
  10430. (widen)
  10431. (goto-char (point-min))
  10432. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10433. values)
  10434. (while (re-search-forward re nil t)
  10435. (add-to-list 'values (org-trim (match-string 1))))
  10436. (delete "" values)))))
  10437. (defun org-insert-property-drawer ()
  10438. "Insert a property drawer into the current entry."
  10439. (interactive)
  10440. (org-back-to-heading t)
  10441. (looking-at outline-regexp)
  10442. (let ((indent (- (match-end 0)(match-beginning 0)))
  10443. (beg (point))
  10444. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10445. end hiddenp)
  10446. (outline-next-heading)
  10447. (setq end (point))
  10448. (goto-char beg)
  10449. (while (re-search-forward re end t))
  10450. (setq hiddenp (org-invisible-p))
  10451. (end-of-line 1)
  10452. (and (equal (char-after) ?\n) (forward-char 1))
  10453. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10454. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10455. ;; just skip this line
  10456. (beginning-of-line 2)
  10457. ;; Drawer start, find the end
  10458. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10459. (beginning-of-line 1)))
  10460. (org-skip-over-state-notes)
  10461. (skip-chars-backward " \t\n\r")
  10462. (if (eq (char-before) ?*) (forward-char 1))
  10463. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10464. (beginning-of-line 0)
  10465. (org-indent-to-column indent)
  10466. (beginning-of-line 2)
  10467. (org-indent-to-column indent)
  10468. (beginning-of-line 0)
  10469. (if hiddenp
  10470. (save-excursion
  10471. (org-back-to-heading t)
  10472. (hide-entry))
  10473. (org-flag-drawer t))))
  10474. (defun org-set-property (property value)
  10475. "In the current entry, set PROPERTY to VALUE.
  10476. When called interactively, this will prompt for a property name, offering
  10477. completion on existing and default properties. And then it will prompt
  10478. for a value, offering completion either on allowed values (via an inherited
  10479. xxx_ALL property) or on existing values in other instances of this property
  10480. in the current file."
  10481. (interactive
  10482. (let* ((completion-ignore-case t)
  10483. (keys (org-buffer-property-keys nil t t))
  10484. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10485. (prop (if (member prop0 keys)
  10486. prop0
  10487. (or (cdr (assoc (downcase prop0)
  10488. (mapcar (lambda (x) (cons (downcase x) x))
  10489. keys)))
  10490. prop0)))
  10491. (cur (org-entry-get nil prop))
  10492. (allowed (org-property-get-allowed-values nil prop 'table))
  10493. (existing (mapcar 'list (org-property-values prop)))
  10494. (val (if allowed
  10495. (org-completing-read "Value: " allowed nil 'req-match)
  10496. (let (org-completion-use-ido)
  10497. (org-completing-read
  10498. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10499. (concat "[" cur "]") "")
  10500. ": ")
  10501. existing nil nil "" nil cur)))))
  10502. (list prop (if (equal val "") cur val))))
  10503. (unless (equal (org-entry-get nil property) value)
  10504. (org-entry-put nil property value)))
  10505. (defun org-delete-property (property)
  10506. "In the current entry, delete PROPERTY."
  10507. (interactive
  10508. (let* ((completion-ignore-case t)
  10509. (prop (org-ido-completing-read
  10510. "Property: " (org-entry-properties nil 'standard))))
  10511. (list prop)))
  10512. (message "Property %s %s" property
  10513. (if (org-entry-delete nil property)
  10514. "deleted"
  10515. "was not present in the entry")))
  10516. (defun org-delete-property-globally (property)
  10517. "Remove PROPERTY globally, from all entries."
  10518. (interactive
  10519. (let* ((completion-ignore-case t)
  10520. (prop (org-ido-completing-read
  10521. "Globally remove property: "
  10522. (mapcar 'list (org-buffer-property-keys)))))
  10523. (list prop)))
  10524. (save-excursion
  10525. (save-restriction
  10526. (widen)
  10527. (goto-char (point-min))
  10528. (let ((cnt 0))
  10529. (while (re-search-forward
  10530. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10531. nil t)
  10532. (setq cnt (1+ cnt))
  10533. (replace-match ""))
  10534. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10535. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10536. (defun org-compute-property-at-point ()
  10537. "Compute the property at point.
  10538. This looks for an enclosing column format, extracts the operator and
  10539. then applies it to the property in the column format's scope."
  10540. (interactive)
  10541. (unless (org-at-property-p)
  10542. (error "Not at a property"))
  10543. (let ((prop (org-match-string-no-properties 2)))
  10544. (org-columns-get-format-and-top-level)
  10545. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10546. (error "No operator defined for property %s" prop))
  10547. (org-columns-compute prop)))
  10548. (defun org-property-get-allowed-values (pom property &optional table)
  10549. "Get allowed values for the property PROPERTY.
  10550. When TABLE is non-nil, return an alist that can directly be used for
  10551. completion."
  10552. (let (vals)
  10553. (cond
  10554. ((equal property "TODO")
  10555. (setq vals (org-with-point-at pom
  10556. (append org-todo-keywords-1 '("")))))
  10557. ((equal property "PRIORITY")
  10558. (let ((n org-lowest-priority))
  10559. (while (>= n org-highest-priority)
  10560. (push (char-to-string n) vals)
  10561. (setq n (1- n)))))
  10562. ((member property org-special-properties))
  10563. (t
  10564. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10565. (when (and vals (string-match "\\S-" vals))
  10566. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10567. (setq vals (mapcar (lambda (x)
  10568. (cond ((stringp x) x)
  10569. ((numberp x) (number-to-string x))
  10570. ((symbolp x) (symbol-name x))
  10571. (t "???")))
  10572. vals)))))
  10573. (if table (mapcar 'list vals) vals)))
  10574. (defun org-property-previous-allowed-value (&optional previous)
  10575. "Switch to the next allowed value for this property."
  10576. (interactive)
  10577. (org-property-next-allowed-value t))
  10578. (defun org-property-next-allowed-value (&optional previous)
  10579. "Switch to the next allowed value for this property."
  10580. (interactive)
  10581. (unless (org-at-property-p)
  10582. (error "Not at a property"))
  10583. (let* ((key (match-string 2))
  10584. (value (match-string 3))
  10585. (allowed (or (org-property-get-allowed-values (point) key)
  10586. (and (member value '("[ ]" "[-]" "[X]"))
  10587. '("[ ]" "[X]"))))
  10588. nval)
  10589. (unless allowed
  10590. (error "Allowed values for this property have not been defined"))
  10591. (if previous (setq allowed (reverse allowed)))
  10592. (if (member value allowed)
  10593. (setq nval (car (cdr (member value allowed)))))
  10594. (setq nval (or nval (car allowed)))
  10595. (if (equal nval value)
  10596. (error "Only one allowed value for this property"))
  10597. (org-at-property-p)
  10598. (replace-match (concat " :" key ": " nval) t t)
  10599. (org-indent-line-function)
  10600. (beginning-of-line 1)
  10601. (skip-chars-forward " \t")))
  10602. (defun org-find-entry-with-id (ident)
  10603. "Locate the entry that contains the ID property with exact value IDENT.
  10604. IDENT can be a string, a symbol or a number, this function will search for
  10605. the string representation of it.
  10606. Return the position where this entry starts, or nil if there is no such entry."
  10607. (interactive "sID: ")
  10608. (let ((id (cond
  10609. ((stringp ident) ident)
  10610. ((symbol-name ident) (symbol-name ident))
  10611. ((numberp ident) (number-to-string ident))
  10612. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10613. (case-fold-search nil))
  10614. (save-excursion
  10615. (save-restriction
  10616. (widen)
  10617. (goto-char (point-min))
  10618. (when (re-search-forward
  10619. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10620. nil t)
  10621. (org-back-to-heading)
  10622. (point))))))
  10623. ;;;; Timestamps
  10624. (defvar org-last-changed-timestamp nil)
  10625. (defvar org-last-inserted-timestamp nil
  10626. "The last time stamp inserted with `org-insert-time-stamp'.")
  10627. (defvar org-time-was-given) ; dynamically scoped parameter
  10628. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10629. (defvar org-ts-what) ; dynamically scoped parameter
  10630. (defun org-time-stamp (arg &optional inactive)
  10631. "Prompt for a date/time and insert a time stamp.
  10632. If the user specifies a time like HH:MM, or if this command is called
  10633. with a prefix argument, the time stamp will contain date and time.
  10634. Otherwise, only the date will be included. All parts of a date not
  10635. specified by the user will be filled in from the current date/time.
  10636. So if you press just return without typing anything, the time stamp
  10637. will represent the current date/time. If there is already a timestamp
  10638. at the cursor, it will be modified."
  10639. (interactive "P")
  10640. (let* ((ts nil)
  10641. (default-time
  10642. ;; Default time is either today, or, when entering a range,
  10643. ;; the range start.
  10644. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10645. (save-excursion
  10646. (re-search-backward
  10647. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10648. (- (point) 20) t)))
  10649. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10650. (current-time)))
  10651. (default-input (and ts (org-get-compact-tod ts)))
  10652. org-time-was-given org-end-time-was-given time)
  10653. (cond
  10654. ((and (org-at-timestamp-p t)
  10655. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10656. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10657. (insert "--")
  10658. (setq time (let ((this-command this-command))
  10659. (org-read-date arg 'totime nil nil
  10660. default-time default-input)))
  10661. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10662. ((org-at-timestamp-p t)
  10663. (setq time (let ((this-command this-command))
  10664. (org-read-date arg 'totime nil nil default-time default-input)))
  10665. (when (org-at-timestamp-p t) ; just to get the match data
  10666. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10667. (replace-match "")
  10668. (setq org-last-changed-timestamp
  10669. (org-insert-time-stamp
  10670. time (or org-time-was-given arg)
  10671. inactive nil nil (list org-end-time-was-given))))
  10672. (message "Timestamp updated"))
  10673. (t
  10674. (setq time (let ((this-command this-command))
  10675. (org-read-date arg 'totime nil nil default-time default-input)))
  10676. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10677. nil nil (list org-end-time-was-given))))))
  10678. ;; FIXME: can we use this for something else, like computing time differences?
  10679. (defun org-get-compact-tod (s)
  10680. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10681. (let* ((t1 (match-string 1 s))
  10682. (h1 (string-to-number (match-string 2 s)))
  10683. (m1 (string-to-number (match-string 3 s)))
  10684. (t2 (and (match-end 4) (match-string 5 s)))
  10685. (h2 (and t2 (string-to-number (match-string 6 s))))
  10686. (m2 (and t2 (string-to-number (match-string 7 s))))
  10687. dh dm)
  10688. (if (not t2)
  10689. t1
  10690. (setq dh (- h2 h1) dm (- m2 m1))
  10691. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10692. (concat t1 "+" (number-to-string dh)
  10693. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10694. (defun org-time-stamp-inactive (&optional arg)
  10695. "Insert an inactive time stamp.
  10696. An inactive time stamp is enclosed in square brackets instead of angle
  10697. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10698. does not link to the calendar and cannot be changed with the S-cursor keys.
  10699. So these are more for recording a certain time/date."
  10700. (interactive "P")
  10701. (org-time-stamp arg 'inactive))
  10702. (defvar org-date-ovl (org-make-overlay 1 1))
  10703. (org-overlay-put org-date-ovl 'face 'org-warning)
  10704. (org-detach-overlay org-date-ovl)
  10705. (defvar org-ans1) ; dynamically scoped parameter
  10706. (defvar org-ans2) ; dynamically scoped parameter
  10707. (defvar org-plain-time-of-day-regexp) ; defined below
  10708. (defvar org-overriding-default-time nil) ; dynamically scoped
  10709. (defvar org-read-date-overlay nil)
  10710. (defvar org-dcst nil) ; dynamically scoped
  10711. (defvar org-read-date-history nil)
  10712. (defun org-read-date (&optional with-time to-time from-string prompt
  10713. default-time default-input)
  10714. "Read a date, possibly a time, and make things smooth for the user.
  10715. The prompt will suggest to enter an ISO date, but you can also enter anything
  10716. which will at least partially be understood by `parse-time-string'.
  10717. Unrecognized parts of the date will default to the current day, month, year,
  10718. hour and minute. If this command is called to replace a timestamp at point,
  10719. of to enter the second timestamp of a range, the default time is taken from the
  10720. existing stamp. For example,
  10721. 3-2-5 --> 2003-02-05
  10722. feb 15 --> currentyear-02-15
  10723. sep 12 9 --> 2009-09-12
  10724. 12:45 --> today 12:45
  10725. 22 sept 0:34 --> currentyear-09-22 0:34
  10726. 12 --> currentyear-currentmonth-12
  10727. Fri --> nearest Friday (today or later)
  10728. etc.
  10729. Furthermore you can specify a relative date by giving, as the *first* thing
  10730. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10731. change in days weeks, months, years.
  10732. With a single plus or minus, the date is relative to today. With a double
  10733. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10734. +4d --> four days from today
  10735. +4 --> same as above
  10736. +2w --> two weeks from today
  10737. ++5 --> five days from default date
  10738. The function understands only English month and weekday abbreviations,
  10739. but this can be configured with the variables `parse-time-months' and
  10740. `parse-time-weekdays'.
  10741. While prompting, a calendar is popped up - you can also select the
  10742. date with the mouse (button 1). The calendar shows a period of three
  10743. months. To scroll it to other months, use the keys `>' and `<'.
  10744. If you don't like the calendar, turn it off with
  10745. \(setq org-read-date-popup-calendar nil)
  10746. With optional argument TO-TIME, the date will immediately be converted
  10747. to an internal time.
  10748. With an optional argument WITH-TIME, the prompt will suggest to also
  10749. insert a time. Note that when WITH-TIME is not set, you can still
  10750. enter a time, and this function will inform the calling routine about
  10751. this change. The calling routine may then choose to change the format
  10752. used to insert the time stamp into the buffer to include the time.
  10753. With optional argument FROM-STRING, read from this string instead from
  10754. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10755. the time/date that is used for everything that is not specified by the
  10756. user."
  10757. (require 'parse-time)
  10758. (let* ((org-time-stamp-rounding-minutes
  10759. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10760. (org-dcst org-display-custom-times)
  10761. (ct (org-current-time))
  10762. (def (or org-overriding-default-time default-time ct))
  10763. (defdecode (decode-time def))
  10764. (dummy (progn
  10765. (when (< (nth 2 defdecode) org-extend-today-until)
  10766. (setcar (nthcdr 2 defdecode) -1)
  10767. (setcar (nthcdr 1 defdecode) 59)
  10768. (setq def (apply 'encode-time defdecode)
  10769. defdecode (decode-time def)))))
  10770. (calendar-move-hook nil)
  10771. (calendar-view-diary-initially-flag nil)
  10772. (view-diary-entries-initially nil)
  10773. (calendar-view-holidays-initially-flag nil)
  10774. (view-calendar-holidays-initially nil)
  10775. (timestr (format-time-string
  10776. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10777. (prompt (concat (if prompt (concat prompt " ") "")
  10778. (format "Date+time [%s]: " timestr)))
  10779. ans (org-ans0 "") org-ans1 org-ans2 final)
  10780. (cond
  10781. (from-string (setq ans from-string))
  10782. (org-read-date-popup-calendar
  10783. (save-excursion
  10784. (save-window-excursion
  10785. (calendar)
  10786. (calendar-forward-day (- (time-to-days def)
  10787. (calendar-absolute-from-gregorian
  10788. (calendar-current-date))))
  10789. (org-eval-in-calendar nil t)
  10790. (let* ((old-map (current-local-map))
  10791. (map (copy-keymap calendar-mode-map))
  10792. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10793. (org-defkey map (kbd "RET") 'org-calendar-select)
  10794. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10795. 'org-calendar-select-mouse)
  10796. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10797. 'org-calendar-select-mouse)
  10798. (org-defkey minibuffer-local-map [(meta shift left)]
  10799. (lambda () (interactive)
  10800. (org-eval-in-calendar '(calendar-backward-month 1))))
  10801. (org-defkey minibuffer-local-map [(meta shift right)]
  10802. (lambda () (interactive)
  10803. (org-eval-in-calendar '(calendar-forward-month 1))))
  10804. (org-defkey minibuffer-local-map [(meta shift up)]
  10805. (lambda () (interactive)
  10806. (org-eval-in-calendar '(calendar-backward-year 1))))
  10807. (org-defkey minibuffer-local-map [(meta shift down)]
  10808. (lambda () (interactive)
  10809. (org-eval-in-calendar '(calendar-forward-year 1))))
  10810. (org-defkey minibuffer-local-map [(shift up)]
  10811. (lambda () (interactive)
  10812. (org-eval-in-calendar '(calendar-backward-week 1))))
  10813. (org-defkey minibuffer-local-map [(shift down)]
  10814. (lambda () (interactive)
  10815. (org-eval-in-calendar '(calendar-forward-week 1))))
  10816. (org-defkey minibuffer-local-map [(shift left)]
  10817. (lambda () (interactive)
  10818. (org-eval-in-calendar '(calendar-backward-day 1))))
  10819. (org-defkey minibuffer-local-map [(shift right)]
  10820. (lambda () (interactive)
  10821. (org-eval-in-calendar '(calendar-forward-day 1))))
  10822. (org-defkey minibuffer-local-map ">"
  10823. (lambda () (interactive)
  10824. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10825. (org-defkey minibuffer-local-map "<"
  10826. (lambda () (interactive)
  10827. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10828. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10829. (unwind-protect
  10830. (progn
  10831. (use-local-map map)
  10832. (add-hook 'post-command-hook 'org-read-date-display)
  10833. (setq org-ans0 (read-string prompt default-input
  10834. 'org-read-date-history nil))
  10835. ;; org-ans0: from prompt
  10836. ;; org-ans1: from mouse click
  10837. ;; org-ans2: from calendar motion
  10838. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10839. (remove-hook 'post-command-hook 'org-read-date-display)
  10840. (use-local-map old-map)
  10841. (when org-read-date-overlay
  10842. (org-delete-overlay org-read-date-overlay)
  10843. (setq org-read-date-overlay nil)))))))
  10844. (t ; Naked prompt only
  10845. (unwind-protect
  10846. (setq ans (read-string prompt default-input
  10847. 'org-read-date-history timestr))
  10848. (when org-read-date-overlay
  10849. (org-delete-overlay org-read-date-overlay)
  10850. (setq org-read-date-overlay nil)))))
  10851. (setq final (org-read-date-analyze ans def defdecode))
  10852. (if to-time
  10853. (apply 'encode-time final)
  10854. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10855. (format "%04d-%02d-%02d %02d:%02d"
  10856. (nth 5 final) (nth 4 final) (nth 3 final)
  10857. (nth 2 final) (nth 1 final))
  10858. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10859. (defvar def)
  10860. (defvar defdecode)
  10861. (defvar with-time)
  10862. (defun org-read-date-display ()
  10863. "Display the current date prompt interpretation in the minibuffer."
  10864. (when org-read-date-display-live
  10865. (when org-read-date-overlay
  10866. (org-delete-overlay org-read-date-overlay))
  10867. (let ((p (point)))
  10868. (end-of-line 1)
  10869. (while (not (equal (buffer-substring
  10870. (max (point-min) (- (point) 4)) (point))
  10871. " "))
  10872. (insert " "))
  10873. (goto-char p))
  10874. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10875. " " (or org-ans1 org-ans2)))
  10876. (org-end-time-was-given nil)
  10877. (f (org-read-date-analyze ans def defdecode))
  10878. (fmts (if org-dcst
  10879. org-time-stamp-custom-formats
  10880. org-time-stamp-formats))
  10881. (fmt (if (or with-time
  10882. (and (boundp 'org-time-was-given) org-time-was-given))
  10883. (cdr fmts)
  10884. (car fmts)))
  10885. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10886. (when (and org-end-time-was-given
  10887. (string-match org-plain-time-of-day-regexp txt))
  10888. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10889. org-end-time-was-given
  10890. (substring txt (match-end 0)))))
  10891. (setq org-read-date-overlay
  10892. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10893. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10894. (defun org-read-date-analyze (ans def defdecode)
  10895. "Analyse the combined answer of the date prompt."
  10896. ;; FIXME: cleanup and comment
  10897. (let (delta deltan deltaw deltadef year month day
  10898. hour minute second wday pm h2 m2 tl wday1
  10899. iso-year iso-weekday iso-week iso-year iso-date)
  10900. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10901. (setq ans "+0"))
  10902. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10903. (setq ans (replace-match "" t t ans)
  10904. deltan (car delta)
  10905. deltaw (nth 1 delta)
  10906. deltadef (nth 2 delta)))
  10907. ;; Check if there is an iso week date in there
  10908. ;; If yes, sore the info and postpone interpreting it until the rest
  10909. ;; of the parsing is done
  10910. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10911. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10912. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10913. iso-week (string-to-number (match-string 2 ans)))
  10914. (setq ans (replace-match "" t t ans)))
  10915. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10916. (when (string-match
  10917. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10918. (setq year (if (match-end 2)
  10919. (string-to-number (match-string 2 ans))
  10920. (string-to-number (format-time-string "%Y")))
  10921. month (string-to-number (match-string 3 ans))
  10922. day (string-to-number (match-string 4 ans)))
  10923. (if (< year 100) (setq year (+ 2000 year)))
  10924. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10925. t nil ans)))
  10926. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10927. ;; If there is a time with am/pm, and *no* time without it, we convert
  10928. ;; so that matching will be successful.
  10929. (loop for i from 1 to 2 do ; twice, for end time as well
  10930. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10931. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10932. (setq hour (string-to-number (match-string 1 ans))
  10933. minute (if (match-end 3)
  10934. (string-to-number (match-string 3 ans))
  10935. 0)
  10936. pm (equal ?p
  10937. (string-to-char (downcase (match-string 4 ans)))))
  10938. (if (and (= hour 12) (not pm))
  10939. (setq hour 0)
  10940. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10941. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10942. t t ans))))
  10943. ;; Check if a time range is given as a duration
  10944. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10945. (setq hour (string-to-number (match-string 1 ans))
  10946. h2 (+ hour (string-to-number (match-string 3 ans)))
  10947. minute (string-to-number (match-string 2 ans))
  10948. m2 (+ minute (if (match-end 5) (string-to-number
  10949. (match-string 5 ans))0)))
  10950. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10951. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10952. t t ans)))
  10953. ;; Check if there is a time range
  10954. (when (boundp 'org-end-time-was-given)
  10955. (setq org-time-was-given nil)
  10956. (when (and (string-match org-plain-time-of-day-regexp ans)
  10957. (match-end 8))
  10958. (setq org-end-time-was-given (match-string 8 ans))
  10959. (setq ans (concat (substring ans 0 (match-beginning 7))
  10960. (substring ans (match-end 7))))))
  10961. (setq tl (parse-time-string ans)
  10962. day (or (nth 3 tl) (nth 3 defdecode))
  10963. month (or (nth 4 tl)
  10964. (if (and org-read-date-prefer-future
  10965. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10966. (1+ (nth 4 defdecode))
  10967. (nth 4 defdecode)))
  10968. year (or (nth 5 tl)
  10969. (if (and org-read-date-prefer-future
  10970. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10971. (1+ (nth 5 defdecode))
  10972. (nth 5 defdecode)))
  10973. hour (or (nth 2 tl) (nth 2 defdecode))
  10974. minute (or (nth 1 tl) (nth 1 defdecode))
  10975. second (or (nth 0 tl) 0)
  10976. wday (nth 6 tl))
  10977. ;; Special date definitions below
  10978. (cond
  10979. (iso-week
  10980. ;; There was an iso week
  10981. (setq year (or iso-year year)
  10982. day (or iso-weekday wday 1)
  10983. wday nil ; to make sure that the trigger below does not match
  10984. iso-date (calendar-gregorian-from-absolute
  10985. (calendar-absolute-from-iso
  10986. (list iso-week day year))))
  10987. ; FIXME: Should we also push ISO weeks into the future?
  10988. ; (when (and org-read-date-prefer-future
  10989. ; (not iso-year)
  10990. ; (< (calendar-absolute-from-gregorian iso-date)
  10991. ; (time-to-days (current-time))))
  10992. ; (setq year (1+ year)
  10993. ; iso-date (calendar-gregorian-from-absolute
  10994. ; (calendar-absolute-from-iso
  10995. ; (list iso-week day year)))))
  10996. (setq month (car iso-date)
  10997. year (nth 2 iso-date)
  10998. day (nth 1 iso-date)))
  10999. (deltan
  11000. (unless deltadef
  11001. (let ((now (decode-time (current-time))))
  11002. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11003. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11004. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11005. ((equal deltaw "m") (setq month (+ month deltan)))
  11006. ((equal deltaw "y") (setq year (+ year deltan)))))
  11007. ((and wday (not (nth 3 tl)))
  11008. ;; Weekday was given, but no day, so pick that day in the week
  11009. ;; on or after the derived date.
  11010. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11011. (unless (equal wday wday1)
  11012. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11013. (if (and (boundp 'org-time-was-given)
  11014. (nth 2 tl))
  11015. (setq org-time-was-given t))
  11016. (if (< year 100) (setq year (+ 2000 year)))
  11017. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11018. (list second minute hour day month year)))
  11019. (defvar parse-time-weekdays)
  11020. (defun org-read-date-get-relative (s today default)
  11021. "Check string S for special relative date string.
  11022. TODAY and DEFAULT are internal times, for today and for a default.
  11023. Return shift list (N what def-flag)
  11024. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11025. N is the number of WHATs to shift.
  11026. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11027. the DEFAULT date rather than TODAY."
  11028. (when (and
  11029. (string-match
  11030. (concat
  11031. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11032. "\\([0-9]+\\)?"
  11033. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11034. "\\([ \t]\\|$\\)") s)
  11035. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11036. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11037. (string-to-char (substring (match-string 1 s) -1))
  11038. ?+))
  11039. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11040. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11041. (what (if (match-end 3) (match-string 3 s) "d"))
  11042. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11043. (date (if rel default today))
  11044. (wday (nth 6 (decode-time date)))
  11045. delta)
  11046. (if wday1
  11047. (progn
  11048. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11049. (if (= dir ?-) (setq delta (- delta 7)))
  11050. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11051. (list delta "d" rel))
  11052. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11053. (defun org-eval-in-calendar (form &optional keepdate)
  11054. "Eval FORM in the calendar window and return to current window.
  11055. Also, store the cursor date in variable org-ans2."
  11056. (let ((sw (selected-window)))
  11057. (select-window (get-buffer-window "*Calendar*"))
  11058. (eval form)
  11059. (when (and (not keepdate) (calendar-cursor-to-date))
  11060. (let* ((date (calendar-cursor-to-date))
  11061. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11062. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11063. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11064. (select-window sw)))
  11065. (defun org-calendar-select ()
  11066. "Return to `org-read-date' with the date currently selected.
  11067. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11068. (interactive)
  11069. (when (calendar-cursor-to-date)
  11070. (let* ((date (calendar-cursor-to-date))
  11071. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11072. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11073. (if (active-minibuffer-window) (exit-minibuffer))))
  11074. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11075. "Insert a date stamp for the date given by the internal TIME.
  11076. WITH-HM means, use the stamp format that includes the time of the day.
  11077. INACTIVE means use square brackets instead of angular ones, so that the
  11078. stamp will not contribute to the agenda.
  11079. PRE and POST are optional strings to be inserted before and after the
  11080. stamp.
  11081. The command returns the inserted time stamp."
  11082. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11083. stamp)
  11084. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11085. (insert-before-markers (or pre ""))
  11086. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11087. (when (listp extra)
  11088. (setq extra (car extra))
  11089. (if (and (stringp extra)
  11090. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11091. (setq extra (format "-%02d:%02d"
  11092. (string-to-number (match-string 1 extra))
  11093. (string-to-number (match-string 2 extra))))
  11094. (setq extra nil)))
  11095. (when extra
  11096. (backward-char 1)
  11097. (insert-before-markers extra)
  11098. (forward-char 1))
  11099. (insert-before-markers (or post ""))
  11100. (setq org-last-inserted-timestamp stamp)))
  11101. (defun org-toggle-time-stamp-overlays ()
  11102. "Toggle the use of custom time stamp formats."
  11103. (interactive)
  11104. (setq org-display-custom-times (not org-display-custom-times))
  11105. (unless org-display-custom-times
  11106. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11107. (while (setq p (next-single-property-change p 'display))
  11108. (if (and (get-text-property p 'display)
  11109. (eq (get-text-property p 'face) 'org-date))
  11110. (remove-text-properties
  11111. p (setq p (next-single-property-change p 'display))
  11112. '(display t))))
  11113. (set-buffer-modified-p bmp)))
  11114. (if (featurep 'xemacs)
  11115. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11116. (org-restart-font-lock)
  11117. (setq org-table-may-need-update t)
  11118. (if org-display-custom-times
  11119. (message "Time stamps are overlayed with custom format")
  11120. (message "Time stamp overlays removed")))
  11121. (defun org-display-custom-time (beg end)
  11122. "Overlay modified time stamp format over timestamp between BEG and END."
  11123. (let* ((ts (buffer-substring beg end))
  11124. t1 w1 with-hm tf time str w2 (off 0))
  11125. (save-match-data
  11126. (setq t1 (org-parse-time-string ts t))
  11127. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11128. (setq off (- (match-end 0) (match-beginning 0)))))
  11129. (setq end (- end off))
  11130. (setq w1 (- end beg)
  11131. with-hm (and (nth 1 t1) (nth 2 t1))
  11132. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11133. time (org-fix-decoded-time t1)
  11134. str (org-add-props
  11135. (format-time-string
  11136. (substring tf 1 -1) (apply 'encode-time time))
  11137. nil 'mouse-face 'highlight)
  11138. w2 (length str))
  11139. (if (not (= w2 w1))
  11140. (add-text-properties (1+ beg) (+ 2 beg)
  11141. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11142. (if (featurep 'xemacs)
  11143. (progn
  11144. (put-text-property beg end 'invisible t)
  11145. (put-text-property beg end 'end-glyph (make-glyph str)))
  11146. (put-text-property beg end 'display str))))
  11147. (defun org-translate-time (string)
  11148. "Translate all timestamps in STRING to custom format.
  11149. But do this only if the variable `org-display-custom-times' is set."
  11150. (when org-display-custom-times
  11151. (save-match-data
  11152. (let* ((start 0)
  11153. (re org-ts-regexp-both)
  11154. t1 with-hm inactive tf time str beg end)
  11155. (while (setq start (string-match re string start))
  11156. (setq beg (match-beginning 0)
  11157. end (match-end 0)
  11158. t1 (save-match-data
  11159. (org-parse-time-string (substring string beg end) t))
  11160. with-hm (and (nth 1 t1) (nth 2 t1))
  11161. inactive (equal (substring string beg (1+ beg)) "[")
  11162. tf (funcall (if with-hm 'cdr 'car)
  11163. org-time-stamp-custom-formats)
  11164. time (org-fix-decoded-time t1)
  11165. str (format-time-string
  11166. (concat
  11167. (if inactive "[" "<") (substring tf 1 -1)
  11168. (if inactive "]" ">"))
  11169. (apply 'encode-time time))
  11170. string (replace-match str t t string)
  11171. start (+ start (length str)))))))
  11172. string)
  11173. (defun org-fix-decoded-time (time)
  11174. "Set 0 instead of nil for the first 6 elements of time.
  11175. Don't touch the rest."
  11176. (let ((n 0))
  11177. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11178. (defun org-days-to-time (timestamp-string)
  11179. "Difference between TIMESTAMP-STRING and now in days."
  11180. (- (time-to-days (org-time-string-to-time timestamp-string))
  11181. (time-to-days (current-time))))
  11182. (defun org-deadline-close (timestamp-string &optional ndays)
  11183. "Is the time in TIMESTAMP-STRING close to the current date?"
  11184. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11185. (and (< (org-days-to-time timestamp-string) ndays)
  11186. (not (org-entry-is-done-p))))
  11187. (defun org-get-wdays (ts)
  11188. "Get the deadline lead time appropriate for timestring TS."
  11189. (cond
  11190. ((<= org-deadline-warning-days 0)
  11191. ;; 0 or negative, enforce this value no matter what
  11192. (- org-deadline-warning-days))
  11193. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11194. ;; lead time is specified.
  11195. (floor (* (string-to-number (match-string 1 ts))
  11196. (cdr (assoc (match-string 2 ts)
  11197. '(("d" . 1) ("w" . 7)
  11198. ("m" . 30.4) ("y" . 365.25)))))))
  11199. ;; go for the default.
  11200. (t org-deadline-warning-days)))
  11201. (defun org-calendar-select-mouse (ev)
  11202. "Return to `org-read-date' with the date currently selected.
  11203. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11204. (interactive "e")
  11205. (mouse-set-point ev)
  11206. (when (calendar-cursor-to-date)
  11207. (let* ((date (calendar-cursor-to-date))
  11208. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11209. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11210. (if (active-minibuffer-window) (exit-minibuffer))))
  11211. (defun org-check-deadlines (ndays)
  11212. "Check if there are any deadlines due or past due.
  11213. A deadline is considered due if it happens within `org-deadline-warning-days'
  11214. days from today's date. If the deadline appears in an entry marked DONE,
  11215. it is not shown. The prefix arg NDAYS can be used to test that many
  11216. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11217. (interactive "P")
  11218. (let* ((org-warn-days
  11219. (cond
  11220. ((equal ndays '(4)) 100000)
  11221. (ndays (prefix-numeric-value ndays))
  11222. (t (abs org-deadline-warning-days))))
  11223. (case-fold-search nil)
  11224. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11225. (callback
  11226. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11227. (message "%d deadlines past-due or due within %d days"
  11228. (org-occur regexp nil callback)
  11229. org-warn-days)))
  11230. (defun org-check-before-date (date)
  11231. "Check if there are deadlines or scheduled entries before DATE."
  11232. (interactive (list (org-read-date)))
  11233. (let ((case-fold-search nil)
  11234. (regexp (concat "\\<\\(" org-deadline-string
  11235. "\\|" org-scheduled-string
  11236. "\\) *<\\([^>]+\\)>"))
  11237. (callback
  11238. (lambda () (time-less-p
  11239. (org-time-string-to-time (match-string 2))
  11240. (org-time-string-to-time date)))))
  11241. (message "%d entries before %s"
  11242. (org-occur regexp nil callback) date)))
  11243. (defun org-check-after-date (date)
  11244. "Check if there are deadlines or scheduled entries after DATE."
  11245. (interactive (list (org-read-date)))
  11246. (let ((case-fold-search nil)
  11247. (regexp (concat "\\<\\(" org-deadline-string
  11248. "\\|" org-scheduled-string
  11249. "\\) *<\\([^>]+\\)>"))
  11250. (callback
  11251. (lambda () (not
  11252. (time-less-p
  11253. (org-time-string-to-time (match-string 2))
  11254. (org-time-string-to-time date))))))
  11255. (message "%d entries after %s"
  11256. (org-occur regexp nil callback) date)))
  11257. (defun org-evaluate-time-range (&optional to-buffer)
  11258. "Evaluate a time range by computing the difference between start and end.
  11259. Normally the result is just printed in the echo area, but with prefix arg
  11260. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11261. If the time range is actually in a table, the result is inserted into the
  11262. next column.
  11263. For time difference computation, a year is assumed to be exactly 365
  11264. days in order to avoid rounding problems."
  11265. (interactive "P")
  11266. (or
  11267. (org-clock-update-time-maybe)
  11268. (save-excursion
  11269. (unless (org-at-date-range-p t)
  11270. (goto-char (point-at-bol))
  11271. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11272. (if (not (org-at-date-range-p t))
  11273. (error "Not at a time-stamp range, and none found in current line")))
  11274. (let* ((ts1 (match-string 1))
  11275. (ts2 (match-string 2))
  11276. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11277. (match-end (match-end 0))
  11278. (time1 (org-time-string-to-time ts1))
  11279. (time2 (org-time-string-to-time ts2))
  11280. (t1 (time-to-seconds time1))
  11281. (t2 (time-to-seconds time2))
  11282. (diff (abs (- t2 t1)))
  11283. (negative (< (- t2 t1) 0))
  11284. ;; (ys (floor (* 365 24 60 60)))
  11285. (ds (* 24 60 60))
  11286. (hs (* 60 60))
  11287. (fy "%dy %dd %02d:%02d")
  11288. (fy1 "%dy %dd")
  11289. (fd "%dd %02d:%02d")
  11290. (fd1 "%dd")
  11291. (fh "%02d:%02d")
  11292. y d h m align)
  11293. (if havetime
  11294. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11295. y 0
  11296. d (floor (/ diff ds)) diff (mod diff ds)
  11297. h (floor (/ diff hs)) diff (mod diff hs)
  11298. m (floor (/ diff 60)))
  11299. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11300. y 0
  11301. d (floor (+ (/ diff ds) 0.5))
  11302. h 0 m 0))
  11303. (if (not to-buffer)
  11304. (message "%s" (org-make-tdiff-string y d h m))
  11305. (if (org-at-table-p)
  11306. (progn
  11307. (goto-char match-end)
  11308. (setq align t)
  11309. (and (looking-at " *|") (goto-char (match-end 0))))
  11310. (goto-char match-end))
  11311. (if (looking-at
  11312. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11313. (replace-match ""))
  11314. (if negative (insert " -"))
  11315. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11316. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11317. (insert " " (format fh h m))))
  11318. (if align (org-table-align))
  11319. (message "Time difference inserted")))))
  11320. (defun org-make-tdiff-string (y d h m)
  11321. (let ((fmt "")
  11322. (l nil))
  11323. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11324. l (push y l)))
  11325. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11326. l (push d l)))
  11327. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11328. l (push h l)))
  11329. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11330. l (push m l)))
  11331. (apply 'format fmt (nreverse l))))
  11332. (defun org-time-string-to-time (s)
  11333. (apply 'encode-time (org-parse-time-string s)))
  11334. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11335. "Convert a time stamp to an absolute day number.
  11336. If there is a specifyer for a cyclic time stamp, get the closest date to
  11337. DAYNR.
  11338. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11339. (cond
  11340. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11341. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11342. daynr
  11343. (+ daynr 1000)))
  11344. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11345. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11346. (time-to-days (current-time))) (match-string 0 s)
  11347. prefer show-all))
  11348. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11349. (defun org-days-to-iso-week (days)
  11350. "Return the iso week number."
  11351. (require 'cal-iso)
  11352. (car (calendar-iso-from-absolute days)))
  11353. (defun org-small-year-to-year (year)
  11354. "Convert 2-digit years into 4-digit years.
  11355. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11356. The year 2000 cannot be abbreviated. Any year larger than 99
  11357. is returned unchanged."
  11358. (if (< year 38)
  11359. (setq year (+ 2000 year))
  11360. (if (< year 100)
  11361. (setq year (+ 1900 year))))
  11362. year)
  11363. (defun org-time-from-absolute (d)
  11364. "Return the time corresponding to date D.
  11365. D may be an absolute day number, or a calendar-type list (month day year)."
  11366. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11367. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11368. (defun org-calendar-holiday ()
  11369. "List of holidays, for Diary display in Org-mode."
  11370. (require 'holidays)
  11371. (let ((hl (funcall
  11372. (if (fboundp 'calendar-check-holidays)
  11373. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11374. (if hl (mapconcat 'identity hl "; "))))
  11375. (defun org-diary-sexp-entry (sexp entry date)
  11376. "Process a SEXP diary ENTRY for DATE."
  11377. (require 'diary-lib)
  11378. (let ((result (if calendar-debug-sexp
  11379. (let ((stack-trace-on-error t))
  11380. (eval (car (read-from-string sexp))))
  11381. (condition-case nil
  11382. (eval (car (read-from-string sexp)))
  11383. (error
  11384. (beep)
  11385. (message "Bad sexp at line %d in %s: %s"
  11386. (org-current-line)
  11387. (buffer-file-name) sexp)
  11388. (sleep-for 2))))))
  11389. (cond ((stringp result) result)
  11390. ((and (consp result)
  11391. (stringp (cdr result))) (cdr result))
  11392. (result entry)
  11393. (t nil))))
  11394. (defun org-diary-to-ical-string (frombuf)
  11395. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11396. This uses the icalendar.el library."
  11397. (let* ((tmpdir (if (featurep 'xemacs)
  11398. (temp-directory)
  11399. temporary-file-directory))
  11400. (tmpfile (make-temp-name
  11401. (expand-file-name "orgics" tmpdir)))
  11402. buf rtn b e)
  11403. (save-excursion
  11404. (set-buffer frombuf)
  11405. (icalendar-export-region (point-min) (point-max) tmpfile)
  11406. (setq buf (find-buffer-visiting tmpfile))
  11407. (set-buffer buf)
  11408. (goto-char (point-min))
  11409. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11410. (setq b (match-beginning 0)))
  11411. (goto-char (point-max))
  11412. (if (re-search-backward "^END:VEVENT" nil t)
  11413. (setq e (match-end 0)))
  11414. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11415. (kill-buffer buf)
  11416. (delete-file tmpfile)
  11417. rtn))
  11418. (defun org-closest-date (start current change prefer show-all)
  11419. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11420. When PREFER is `past' return a date that is either CURRENT or past.
  11421. When PREFER is `future', return a date that is either CURRENT or future.
  11422. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11423. ;; Make the proper lists from the dates
  11424. (catch 'exit
  11425. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11426. dn dw sday cday n1 n2 n0
  11427. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11428. (setq start (org-date-to-gregorian start)
  11429. current (org-date-to-gregorian
  11430. (if show-all
  11431. current
  11432. (time-to-days (current-time))))
  11433. sday (calendar-absolute-from-gregorian start)
  11434. cday (calendar-absolute-from-gregorian current))
  11435. (if (<= cday sday) (throw 'exit sday))
  11436. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11437. (setq dn (string-to-number (match-string 1 change))
  11438. dw (cdr (assoc (match-string 2 change) a1)))
  11439. (error "Invalid change specifyer: %s" change))
  11440. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11441. (cond
  11442. ((eq dw 'day)
  11443. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11444. n2 (+ n1 dn)))
  11445. ((eq dw 'year)
  11446. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11447. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11448. (setq date1 (list m d y1)
  11449. n1 (calendar-absolute-from-gregorian date1)
  11450. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11451. n2 (calendar-absolute-from-gregorian date2)))
  11452. ((eq dw 'month)
  11453. ;; approx number of month between the two dates
  11454. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11455. ;; How often does dn fit in there?
  11456. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11457. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11458. m (+ m nm)
  11459. ny (floor (/ m 12))
  11460. y (+ y ny)
  11461. m (- m (* ny 12)))
  11462. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11463. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11464. (setq m2 (+ m dn) y2 y)
  11465. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11466. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11467. (while (<= n2 cday)
  11468. (setq n1 n2 m m2 y y2)
  11469. (setq m2 (+ m dn) y2 y)
  11470. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11471. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11472. ;; Make sure n1 is the earlier date
  11473. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11474. (if show-all
  11475. (cond
  11476. ((eq prefer 'past) n1)
  11477. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11478. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11479. (cond
  11480. ((eq prefer 'past) n1)
  11481. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11482. (t (if (= cday n1) n1 n2)))))))
  11483. (defun org-date-to-gregorian (date)
  11484. "Turn any specification of DATE into a gregorian date for the calendar."
  11485. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11486. ((and (listp date) (= (length date) 3)) date)
  11487. ((stringp date)
  11488. (setq date (org-parse-time-string date))
  11489. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11490. ((listp date)
  11491. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11492. (defun org-parse-time-string (s &optional nodefault)
  11493. "Parse the standard Org-mode time string.
  11494. This should be a lot faster than the normal `parse-time-string'.
  11495. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11496. hour and minute fields will be nil if not given."
  11497. (if (string-match org-ts-regexp0 s)
  11498. (list 0
  11499. (if (or (match-beginning 8) (not nodefault))
  11500. (string-to-number (or (match-string 8 s) "0")))
  11501. (if (or (match-beginning 7) (not nodefault))
  11502. (string-to-number (or (match-string 7 s) "0")))
  11503. (string-to-number (match-string 4 s))
  11504. (string-to-number (match-string 3 s))
  11505. (string-to-number (match-string 2 s))
  11506. nil nil nil)
  11507. (make-list 9 0)))
  11508. (defun org-timestamp-up (&optional arg)
  11509. "Increase the date item at the cursor by one.
  11510. If the cursor is on the year, change the year. If it is on the month or
  11511. the day, change that.
  11512. With prefix ARG, change by that many units."
  11513. (interactive "p")
  11514. (org-timestamp-change (prefix-numeric-value arg)))
  11515. (defun org-timestamp-down (&optional arg)
  11516. "Decrease the date item at the cursor by one.
  11517. If the cursor is on the year, change the year. If it is on the month or
  11518. the day, change that.
  11519. With prefix ARG, change by that many units."
  11520. (interactive "p")
  11521. (org-timestamp-change (- (prefix-numeric-value arg))))
  11522. (defun org-timestamp-up-day (&optional arg)
  11523. "Increase the date in the time stamp by one day.
  11524. With prefix ARG, change that many days."
  11525. (interactive "p")
  11526. (if (and (not (org-at-timestamp-p t))
  11527. (org-on-heading-p))
  11528. (org-todo 'up)
  11529. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11530. (defun org-timestamp-down-day (&optional arg)
  11531. "Decrease the date in the time stamp by one day.
  11532. With prefix ARG, change that many days."
  11533. (interactive "p")
  11534. (if (and (not (org-at-timestamp-p t))
  11535. (org-on-heading-p))
  11536. (org-todo 'down)
  11537. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11538. (defun org-at-timestamp-p (&optional inactive-ok)
  11539. "Determine if the cursor is in or at a timestamp."
  11540. (interactive)
  11541. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11542. (pos (point))
  11543. (ans (or (looking-at tsr)
  11544. (save-excursion
  11545. (skip-chars-backward "^[<\n\r\t")
  11546. (if (> (point) (point-min)) (backward-char 1))
  11547. (and (looking-at tsr)
  11548. (> (- (match-end 0) pos) -1))))))
  11549. (and ans
  11550. (boundp 'org-ts-what)
  11551. (setq org-ts-what
  11552. (cond
  11553. ((= pos (match-beginning 0)) 'bracket)
  11554. ((= pos (1- (match-end 0))) 'bracket)
  11555. ((org-pos-in-match-range pos 2) 'year)
  11556. ((org-pos-in-match-range pos 3) 'month)
  11557. ((org-pos-in-match-range pos 7) 'hour)
  11558. ((org-pos-in-match-range pos 8) 'minute)
  11559. ((or (org-pos-in-match-range pos 4)
  11560. (org-pos-in-match-range pos 5)) 'day)
  11561. ((and (> pos (or (match-end 8) (match-end 5)))
  11562. (< pos (match-end 0)))
  11563. (- pos (or (match-end 8) (match-end 5))))
  11564. (t 'day))))
  11565. ans))
  11566. (defun org-toggle-timestamp-type ()
  11567. "Toggle the type (<active> or [inactive]) of a time stamp."
  11568. (interactive)
  11569. (when (org-at-timestamp-p t)
  11570. (let ((beg (match-beginning 0)) (end (match-end 0))
  11571. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11572. (save-excursion
  11573. (goto-char beg)
  11574. (while (re-search-forward "[][<>]" end t)
  11575. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11576. t t)))
  11577. (message "Timestamp is now %sactive"
  11578. (if (equal (char-after beg) ?<) "" "in")))))
  11579. (defun org-timestamp-change (n &optional what)
  11580. "Change the date in the time stamp at point.
  11581. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11582. `year', `minute', `second'. If WHAT is not given, the cursor position
  11583. in the timestamp determines what will be changed."
  11584. (let ((pos (point))
  11585. with-hm inactive
  11586. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11587. org-ts-what
  11588. extra rem
  11589. ts time time0)
  11590. (if (not (org-at-timestamp-p t))
  11591. (error "Not at a timestamp"))
  11592. (if (and (not what) (eq org-ts-what 'bracket))
  11593. (org-toggle-timestamp-type)
  11594. (if (and (not what) (not (eq org-ts-what 'day))
  11595. org-display-custom-times
  11596. (get-text-property (point) 'display)
  11597. (not (get-text-property (1- (point)) 'display)))
  11598. (setq org-ts-what 'day))
  11599. (setq org-ts-what (or what org-ts-what)
  11600. inactive (= (char-after (match-beginning 0)) ?\[)
  11601. ts (match-string 0))
  11602. (replace-match "")
  11603. (if (string-match
  11604. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11605. ts)
  11606. (setq extra (match-string 1 ts)))
  11607. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11608. (setq with-hm t))
  11609. (setq time0 (org-parse-time-string ts))
  11610. (when (and (eq org-ts-what 'minute)
  11611. (eq current-prefix-arg nil))
  11612. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11613. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11614. (setcar (cdr time0) (+ (nth 1 time0)
  11615. (if (> n 0) (- rem) (- dm rem))))))
  11616. (setq time
  11617. (encode-time (or (car time0) 0)
  11618. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11619. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11620. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11621. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11622. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11623. (nthcdr 6 time0)))
  11624. (when (and (member org-ts-what '(hour minute))
  11625. extra
  11626. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11627. (setq extra (org-modify-ts-extra
  11628. extra
  11629. (if (eq org-ts-what 'hour) 2 5)
  11630. n dm)))
  11631. (when (integerp org-ts-what)
  11632. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11633. (if (eq what 'calendar)
  11634. (let ((cal-date (org-get-date-from-calendar)))
  11635. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11636. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11637. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11638. (setcar time0 (or (car time0) 0))
  11639. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11640. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11641. (setq time (apply 'encode-time time0))))
  11642. (setq org-last-changed-timestamp
  11643. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11644. (org-clock-update-time-maybe)
  11645. (goto-char pos)
  11646. ;; Try to recenter the calendar window, if any
  11647. (if (and org-calendar-follow-timestamp-change
  11648. (get-buffer-window "*Calendar*" t)
  11649. (memq org-ts-what '(day month year)))
  11650. (org-recenter-calendar (time-to-days time))))))
  11651. (defun org-modify-ts-extra (s pos n dm)
  11652. "Change the different parts of the lead-time and repeat fields in timestamp."
  11653. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11654. ng h m new rem)
  11655. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11656. (cond
  11657. ((or (org-pos-in-match-range pos 2)
  11658. (org-pos-in-match-range pos 3))
  11659. (setq m (string-to-number (match-string 3 s))
  11660. h (string-to-number (match-string 2 s)))
  11661. (if (org-pos-in-match-range pos 2)
  11662. (setq h (+ h n))
  11663. (setq n (* dm (org-no-warnings (signum n))))
  11664. (when (not (= 0 (setq rem (% m dm))))
  11665. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11666. (setq m (+ m n)))
  11667. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11668. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11669. (setq h (min 24 (max 0 h)))
  11670. (setq ng 1 new (format "-%02d:%02d" h m)))
  11671. ((org-pos-in-match-range pos 6)
  11672. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11673. ((org-pos-in-match-range pos 5)
  11674. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11675. ((org-pos-in-match-range pos 9)
  11676. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11677. ((org-pos-in-match-range pos 8)
  11678. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11679. (when ng
  11680. (setq s (concat
  11681. (substring s 0 (match-beginning ng))
  11682. new
  11683. (substring s (match-end ng))))))
  11684. s))
  11685. (defun org-recenter-calendar (date)
  11686. "If the calendar is visible, recenter it to DATE."
  11687. (let* ((win (selected-window))
  11688. (cwin (get-buffer-window "*Calendar*" t))
  11689. (calendar-move-hook nil))
  11690. (when cwin
  11691. (select-window cwin)
  11692. (calendar-goto-date (if (listp date) date
  11693. (calendar-gregorian-from-absolute date)))
  11694. (select-window win))))
  11695. (defun org-goto-calendar (&optional arg)
  11696. "Go to the Emacs calendar at the current date.
  11697. If there is a time stamp in the current line, go to that date.
  11698. A prefix ARG can be used to force the current date."
  11699. (interactive "P")
  11700. (let ((tsr org-ts-regexp) diff
  11701. (calendar-move-hook nil)
  11702. (calendar-view-holidays-initially-flag nil)
  11703. (view-calendar-holidays-initially nil)
  11704. (calendar-view-diary-initially-flag nil)
  11705. (view-diary-entries-initially nil))
  11706. (if (or (org-at-timestamp-p)
  11707. (save-excursion
  11708. (beginning-of-line 1)
  11709. (looking-at (concat ".*" tsr))))
  11710. (let ((d1 (time-to-days (current-time)))
  11711. (d2 (time-to-days
  11712. (org-time-string-to-time (match-string 1)))))
  11713. (setq diff (- d2 d1))))
  11714. (calendar)
  11715. (calendar-goto-today)
  11716. (if (and diff (not arg)) (calendar-forward-day diff))))
  11717. (defun org-get-date-from-calendar ()
  11718. "Return a list (month day year) of date at point in calendar."
  11719. (with-current-buffer "*Calendar*"
  11720. (save-match-data
  11721. (calendar-cursor-to-date))))
  11722. (defun org-date-from-calendar ()
  11723. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11724. If there is already a time stamp at the cursor position, update it."
  11725. (interactive)
  11726. (if (org-at-timestamp-p t)
  11727. (org-timestamp-change 0 'calendar)
  11728. (let ((cal-date (org-get-date-from-calendar)))
  11729. (org-insert-time-stamp
  11730. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11731. (defun org-minutes-to-hh:mm-string (m)
  11732. "Compute H:MM from a number of minutes."
  11733. (let ((h (/ m 60)))
  11734. (setq m (- m (* 60 h)))
  11735. (format org-time-clocksum-format h m)))
  11736. (defun org-hh:mm-string-to-minutes (s)
  11737. "Convert a string H:MM to a number of minutes."
  11738. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11739. (+ (* (string-to-number (match-string 1 s)) 60)
  11740. (string-to-number (match-string 2 s)))
  11741. 0))
  11742. ;;;; Files
  11743. (defun org-save-all-org-buffers ()
  11744. "Save all Org-mode buffers without user confirmation."
  11745. (interactive)
  11746. (message "Saving all Org-mode buffers...")
  11747. (save-some-buffers t 'org-mode-p)
  11748. (when (featurep 'org-id) (org-id-locations-save))
  11749. (message "Saving all Org-mode buffers... done"))
  11750. (defun org-revert-all-org-buffers ()
  11751. "Revert all Org-mode buffers.
  11752. Prompt for confirmation when there are unsaved changes.
  11753. Be sure you know what you are doing before letting this function
  11754. overwrite your changes.
  11755. This function is useful in a setup where one tracks org files
  11756. with a version control system, to revert on one machine after pulling
  11757. changes from another. I believe the procedure must be like this:
  11758. 1. M-x org-save-all-org-buffers
  11759. 2. Pull changes from the other machine, resolve conflicts
  11760. 3. M-x org-revert-all-org-buffers"
  11761. (interactive)
  11762. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11763. (error "Abort"))
  11764. (save-excursion
  11765. (save-window-excursion
  11766. (mapc
  11767. (lambda (b)
  11768. (when (and (with-current-buffer b (org-mode-p))
  11769. (with-current-buffer b buffer-file-name))
  11770. (switch-to-buffer b)
  11771. (revert-buffer t 'no-confirm)))
  11772. (buffer-list))
  11773. (when (and (featurep 'org-id) org-id-track-globally)
  11774. (org-id-locations-load)))))
  11775. ;;;; Agenda files
  11776. ;;;###autoload
  11777. (defun org-iswitchb (&optional arg)
  11778. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11779. With a prefix argument, restrict available to files.
  11780. With two prefix arguments, restrict available buffers to agenda files.
  11781. Due to some yet unresolved reason, the global function
  11782. `iswitchb-mode' needs to be active for this function to work."
  11783. (interactive "P")
  11784. (require 'iswitchb)
  11785. (let ((enabled iswitchb-mode) blist)
  11786. (or enabled (iswitchb-mode 1))
  11787. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11788. ((equal arg '(16)) (org-buffer-list 'agenda))
  11789. (t (org-buffer-list))))
  11790. (unwind-protect
  11791. (let ((iswitchb-make-buflist-hook
  11792. (lambda ()
  11793. (setq iswitchb-temp-buflist
  11794. (mapcar 'buffer-name blist)))))
  11795. (switch-to-buffer
  11796. (iswitchb-read-buffer
  11797. "Switch-to: " nil t))
  11798. (or enabled (iswitchb-mode -1))))))
  11799. ;;;###autoload
  11800. (defun org-ido-switchb (&optional arg)
  11801. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11802. With a prefix argument, restrict available to files.
  11803. With two prefix arguments, restrict available buffers to agenda files."
  11804. (interactive "P")
  11805. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11806. ((equal arg '(16)) (org-buffer-list 'agenda))
  11807. (t (org-buffer-list)))))
  11808. (switch-to-buffer
  11809. (org-ido-completing-read "Org buffer: "
  11810. (mapcar 'list (mapcar 'buffer-name blist))
  11811. nil t))))
  11812. (defun org-buffer-list (&optional predicate exclude-tmp)
  11813. "Return a list of Org buffers.
  11814. PREDICATE can be `export', `files' or `agenda'.
  11815. export restrict the list to Export buffers.
  11816. files restrict the list to buffers visiting Org files.
  11817. agenda restrict the list to buffers visiting agenda files.
  11818. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11819. (let* ((bfn nil)
  11820. (agenda-files (and (eq predicate 'agenda)
  11821. (mapcar 'file-truename (org-agenda-files t))))
  11822. (filter
  11823. (cond
  11824. ((eq predicate 'files)
  11825. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11826. ((eq predicate 'export)
  11827. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11828. ((eq predicate 'agenda)
  11829. (lambda (b)
  11830. (with-current-buffer b
  11831. (and (eq major-mode 'org-mode)
  11832. (setq bfn (buffer-file-name b))
  11833. (member (file-truename bfn) agenda-files)))))
  11834. (t (lambda (b) (with-current-buffer b
  11835. (or (eq major-mode 'org-mode)
  11836. (string-match "\*Org .*Export"
  11837. (buffer-name b)))))))))
  11838. (delq nil
  11839. (mapcar
  11840. (lambda(b)
  11841. (if (and (funcall filter b)
  11842. (or (not exclude-tmp)
  11843. (not (string-match "tmp" (buffer-name b)))))
  11844. b
  11845. nil))
  11846. (buffer-list)))))
  11847. (defun org-agenda-files (&optional unrestricted archives)
  11848. "Get the list of agenda files.
  11849. Optional UNRESTRICTED means return the full list even if a restriction
  11850. is currently in place.
  11851. When ARCHIVES is t, include all archive files hat are really being
  11852. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11853. `org-agenda-archives-mode' is t."
  11854. (let ((files
  11855. (cond
  11856. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11857. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11858. ((listp org-agenda-files) org-agenda-files)
  11859. (t (error "Invalid value of `org-agenda-files'")))))
  11860. (setq files (apply 'append
  11861. (mapcar (lambda (f)
  11862. (if (file-directory-p f)
  11863. (directory-files
  11864. f t org-agenda-file-regexp)
  11865. (list f)))
  11866. files)))
  11867. (when org-agenda-skip-unavailable-files
  11868. (setq files (delq nil
  11869. (mapcar (function
  11870. (lambda (file)
  11871. (and (file-readable-p file) file)))
  11872. files))))
  11873. (when (or (eq archives t)
  11874. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11875. (setq files (org-add-archive-files files)))
  11876. files))
  11877. (defun org-edit-agenda-file-list ()
  11878. "Edit the list of agenda files.
  11879. Depending on setup, this either uses customize to edit the variable
  11880. `org-agenda-files', or it visits the file that is holding the list. In the
  11881. latter case, the buffer is set up in a way that saving it automatically kills
  11882. the buffer and restores the previous window configuration."
  11883. (interactive)
  11884. (if (stringp org-agenda-files)
  11885. (let ((cw (current-window-configuration)))
  11886. (find-file org-agenda-files)
  11887. (org-set-local 'org-window-configuration cw)
  11888. (org-add-hook 'after-save-hook
  11889. (lambda ()
  11890. (set-window-configuration
  11891. (prog1 org-window-configuration
  11892. (kill-buffer (current-buffer))))
  11893. (org-install-agenda-files-menu)
  11894. (message "New agenda file list installed"))
  11895. nil 'local)
  11896. (message "%s" (substitute-command-keys
  11897. "Edit list and finish with \\[save-buffer]")))
  11898. (customize-variable 'org-agenda-files)))
  11899. (defun org-store-new-agenda-file-list (list)
  11900. "Set new value for the agenda file list and save it correctly."
  11901. (if (stringp org-agenda-files)
  11902. (let ((f org-agenda-files) b)
  11903. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11904. (with-temp-file f
  11905. (insert (mapconcat 'identity list "\n") "\n")))
  11906. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11907. (setq org-agenda-files list)
  11908. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11909. (defun org-read-agenda-file-list ()
  11910. "Read the list of agenda files from a file."
  11911. (when (file-directory-p org-agenda-files)
  11912. (error "`org-agenda-files' cannot be a single directory"))
  11913. (when (stringp org-agenda-files)
  11914. (with-temp-buffer
  11915. (insert-file-contents org-agenda-files)
  11916. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11917. ;;;###autoload
  11918. (defun org-cycle-agenda-files ()
  11919. "Cycle through the files in `org-agenda-files'.
  11920. If the current buffer visits an agenda file, find the next one in the list.
  11921. If the current buffer does not, find the first agenda file."
  11922. (interactive)
  11923. (let* ((fs (org-agenda-files t))
  11924. (files (append fs (list (car fs))))
  11925. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11926. file)
  11927. (unless files (error "No agenda files"))
  11928. (catch 'exit
  11929. (while (setq file (pop files))
  11930. (if (equal (file-truename file) tcf)
  11931. (when (car files)
  11932. (find-file (car files))
  11933. (throw 'exit t))))
  11934. (find-file (car fs)))
  11935. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11936. (defun org-agenda-file-to-front (&optional to-end)
  11937. "Move/add the current file to the top of the agenda file list.
  11938. If the file is not present in the list, it is added to the front. If it is
  11939. present, it is moved there. With optional argument TO-END, add/move to the
  11940. end of the list."
  11941. (interactive "P")
  11942. (let ((org-agenda-skip-unavailable-files nil)
  11943. (file-alist (mapcar (lambda (x)
  11944. (cons (file-truename x) x))
  11945. (org-agenda-files t)))
  11946. (ctf (file-truename buffer-file-name))
  11947. x had)
  11948. (setq x (assoc ctf file-alist) had x)
  11949. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11950. (if to-end
  11951. (setq file-alist (append (delq x file-alist) (list x)))
  11952. (setq file-alist (cons x (delq x file-alist))))
  11953. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11954. (org-install-agenda-files-menu)
  11955. (message "File %s to %s of agenda file list"
  11956. (if had "moved" "added") (if to-end "end" "front"))))
  11957. (defun org-remove-file (&optional file)
  11958. "Remove current file from the list of files in variable `org-agenda-files'.
  11959. These are the files which are being checked for agenda entries.
  11960. Optional argument FILE means, use this file instead of the current."
  11961. (interactive)
  11962. (let* ((org-agenda-skip-unavailable-files nil)
  11963. (file (or file buffer-file-name))
  11964. (true-file (file-truename file))
  11965. (afile (abbreviate-file-name file))
  11966. (files (delq nil (mapcar
  11967. (lambda (x)
  11968. (if (equal true-file
  11969. (file-truename x))
  11970. nil x))
  11971. (org-agenda-files t)))))
  11972. (if (not (= (length files) (length (org-agenda-files t))))
  11973. (progn
  11974. (org-store-new-agenda-file-list files)
  11975. (org-install-agenda-files-menu)
  11976. (message "Removed file: %s" afile))
  11977. (message "File was not in list: %s (not removed)" afile))))
  11978. (defun org-file-menu-entry (file)
  11979. (vector file (list 'find-file file) t))
  11980. (defun org-check-agenda-file (file)
  11981. "Make sure FILE exists. If not, ask user what to do."
  11982. (when (not (file-exists-p file))
  11983. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11984. (abbreviate-file-name file))
  11985. (let ((r (downcase (read-char-exclusive))))
  11986. (cond
  11987. ((equal r ?r)
  11988. (org-remove-file file)
  11989. (throw 'nextfile t))
  11990. (t (error "Abort"))))))
  11991. (defun org-get-agenda-file-buffer (file)
  11992. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11993. it to the list of buffers which might be released later."
  11994. (let ((buf (org-find-base-buffer-visiting file)))
  11995. (if buf
  11996. buf ; just return it
  11997. ;; Make a new buffer and remember it
  11998. (setq buf (find-file-noselect file))
  11999. (if buf (push buf org-agenda-new-buffers))
  12000. buf)))
  12001. (defun org-release-buffers (blist)
  12002. "Release all buffers in list, asking the user for confirmation when needed.
  12003. When a buffer is unmodified, it is just killed. When modified, it is saved
  12004. \(if the user agrees) and then killed."
  12005. (let (buf file)
  12006. (while (setq buf (pop blist))
  12007. (setq file (buffer-file-name buf))
  12008. (when (and (buffer-modified-p buf)
  12009. file
  12010. (y-or-n-p (format "Save file %s? " file)))
  12011. (with-current-buffer buf (save-buffer)))
  12012. (kill-buffer buf))))
  12013. (defun org-prepare-agenda-buffers (files)
  12014. "Create buffers for all agenda files, protect archived trees and comments."
  12015. (interactive)
  12016. (let ((pa '(:org-archived t))
  12017. (pc '(:org-comment t))
  12018. (pall '(:org-archived t :org-comment t))
  12019. (inhibit-read-only t)
  12020. (rea (concat ":" org-archive-tag ":"))
  12021. bmp file re)
  12022. (save-excursion
  12023. (save-restriction
  12024. (while (setq file (pop files))
  12025. (if (bufferp file)
  12026. (set-buffer file)
  12027. (org-check-agenda-file file)
  12028. (set-buffer (org-get-agenda-file-buffer file)))
  12029. (widen)
  12030. (setq bmp (buffer-modified-p))
  12031. (org-refresh-category-properties)
  12032. (setq org-todo-keywords-for-agenda
  12033. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12034. (setq org-done-keywords-for-agenda
  12035. (append org-done-keywords-for-agenda org-done-keywords))
  12036. (setq org-todo-keyword-alist-for-agenda
  12037. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12038. (setq org-tag-alist-for-agenda
  12039. (append org-tag-alist-for-agenda org-tag-alist))
  12040. (save-excursion
  12041. (remove-text-properties (point-min) (point-max) pall)
  12042. (when org-agenda-skip-archived-trees
  12043. (goto-char (point-min))
  12044. (while (re-search-forward rea nil t)
  12045. (if (org-on-heading-p t)
  12046. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12047. (goto-char (point-min))
  12048. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12049. (while (re-search-forward re nil t)
  12050. (add-text-properties
  12051. (match-beginning 0) (org-end-of-subtree t) pc)))
  12052. (set-buffer-modified-p bmp))))
  12053. (setq org-todo-keyword-alist-for-agenda
  12054. (org-uniquify org-todo-keyword-alist-for-agenda)
  12055. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12056. ;;;; Embedded LaTeX
  12057. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12058. "Keymap for the minor `org-cdlatex-mode'.")
  12059. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12060. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12061. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12062. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12063. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12064. (defvar org-cdlatex-texmathp-advice-is-done nil
  12065. "Flag remembering if we have applied the advice to texmathp already.")
  12066. (define-minor-mode org-cdlatex-mode
  12067. "Toggle the minor `org-cdlatex-mode'.
  12068. This mode supports entering LaTeX environment and math in LaTeX fragments
  12069. in Org-mode.
  12070. \\{org-cdlatex-mode-map}"
  12071. nil " OCDL" nil
  12072. (when org-cdlatex-mode (require 'cdlatex))
  12073. (unless org-cdlatex-texmathp-advice-is-done
  12074. (setq org-cdlatex-texmathp-advice-is-done t)
  12075. (defadvice texmathp (around org-math-always-on activate)
  12076. "Always return t in org-mode buffers.
  12077. This is because we want to insert math symbols without dollars even outside
  12078. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12079. an embedded LaTeX fragment, let texmathp do its job.
  12080. \\[org-cdlatex-mode-map]"
  12081. (interactive)
  12082. (let (p)
  12083. (cond
  12084. ((not (org-mode-p)) ad-do-it)
  12085. ((eq this-command 'cdlatex-math-symbol)
  12086. (setq ad-return-value t
  12087. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12088. (t
  12089. (let ((p (org-inside-LaTeX-fragment-p)))
  12090. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12091. (setq ad-return-value t
  12092. texmathp-why '("Org-mode embedded math" . 0))
  12093. (if p ad-do-it)))))))))
  12094. (defun turn-on-org-cdlatex ()
  12095. "Unconditionally turn on `org-cdlatex-mode'."
  12096. (org-cdlatex-mode 1))
  12097. (defun org-inside-LaTeX-fragment-p ()
  12098. "Test if point is inside a LaTeX fragment.
  12099. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12100. sequence appearing also before point.
  12101. Even though the matchers for math are configurable, this function assumes
  12102. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12103. delimiters are skipped when they have been removed by customization.
  12104. The return value is nil, or a cons cell with the delimiter and
  12105. and the position of this delimiter.
  12106. This function does a reasonably good job, but can locally be fooled by
  12107. for example currency specifications. For example it will assume being in
  12108. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12109. fragments that are properly closed, but during editing, we have to live
  12110. with the uncertainty caused by missing closing delimiters. This function
  12111. looks only before point, not after."
  12112. (catch 'exit
  12113. (let ((pos (point))
  12114. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12115. (lim (progn
  12116. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12117. (point)))
  12118. dd-on str (start 0) m re)
  12119. (goto-char pos)
  12120. (when dodollar
  12121. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12122. re (nth 1 (assoc "$" org-latex-regexps)))
  12123. (while (string-match re str start)
  12124. (cond
  12125. ((= (match-end 0) (length str))
  12126. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12127. ((= (match-end 0) (- (length str) 5))
  12128. (throw 'exit nil))
  12129. (t (setq start (match-end 0))))))
  12130. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12131. (goto-char pos)
  12132. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12133. (and (match-beginning 2) (throw 'exit nil))
  12134. ;; count $$
  12135. (while (re-search-backward "\\$\\$" lim t)
  12136. (setq dd-on (not dd-on)))
  12137. (goto-char pos)
  12138. (if dd-on (cons "$$" m))))))
  12139. (defun org-try-cdlatex-tab ()
  12140. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12141. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12142. - inside a LaTeX fragment, or
  12143. - after the first word in a line, where an abbreviation expansion could
  12144. insert a LaTeX environment."
  12145. (when org-cdlatex-mode
  12146. (cond
  12147. ((save-excursion
  12148. (skip-chars-backward "a-zA-Z0-9*")
  12149. (skip-chars-backward " \t")
  12150. (bolp))
  12151. (cdlatex-tab) t)
  12152. ((org-inside-LaTeX-fragment-p)
  12153. (cdlatex-tab) t)
  12154. (t nil))))
  12155. (defun org-cdlatex-underscore-caret (&optional arg)
  12156. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12157. Revert to the normal definition outside of these fragments."
  12158. (interactive "P")
  12159. (if (org-inside-LaTeX-fragment-p)
  12160. (call-interactively 'cdlatex-sub-superscript)
  12161. (let (org-cdlatex-mode)
  12162. (call-interactively (key-binding (vector last-input-event))))))
  12163. (defun org-cdlatex-math-modify (&optional arg)
  12164. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12165. Revert to the normal definition outside of these fragments."
  12166. (interactive "P")
  12167. (if (org-inside-LaTeX-fragment-p)
  12168. (call-interactively 'cdlatex-math-modify)
  12169. (let (org-cdlatex-mode)
  12170. (call-interactively (key-binding (vector last-input-event))))))
  12171. (defvar org-latex-fragment-image-overlays nil
  12172. "List of overlays carrying the images of latex fragments.")
  12173. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12174. (defun org-remove-latex-fragment-image-overlays ()
  12175. "Remove all overlays with LaTeX fragment images in current buffer."
  12176. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12177. (setq org-latex-fragment-image-overlays nil))
  12178. (defun org-preview-latex-fragment (&optional subtree)
  12179. "Preview the LaTeX fragment at point, or all locally or globally.
  12180. If the cursor is in a LaTeX fragment, create the image and overlay
  12181. it over the source code. If there is no fragment at point, display
  12182. all fragments in the current text, from one headline to the next. With
  12183. prefix SUBTREE, display all fragments in the current subtree. With a
  12184. double prefix `C-u C-u', or when the cursor is before the first headline,
  12185. display all fragments in the buffer.
  12186. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12187. (interactive "P")
  12188. (org-remove-latex-fragment-image-overlays)
  12189. (save-excursion
  12190. (save-restriction
  12191. (let (beg end at msg)
  12192. (cond
  12193. ((or (equal subtree '(16))
  12194. (not (save-excursion
  12195. (re-search-backward (concat "^" outline-regexp) nil t))))
  12196. (setq beg (point-min) end (point-max)
  12197. msg "Creating images for buffer...%s"))
  12198. ((equal subtree '(4))
  12199. (org-back-to-heading)
  12200. (setq beg (point) end (org-end-of-subtree t)
  12201. msg "Creating images for subtree...%s"))
  12202. (t
  12203. (if (setq at (org-inside-LaTeX-fragment-p))
  12204. (goto-char (max (point-min) (- (cdr at) 2)))
  12205. (org-back-to-heading))
  12206. (setq beg (point) end (progn (outline-next-heading) (point))
  12207. msg (if at "Creating image...%s"
  12208. "Creating images for entry...%s"))))
  12209. (message msg "")
  12210. (narrow-to-region beg end)
  12211. (goto-char beg)
  12212. (org-format-latex
  12213. (concat "ltxpng/" (file-name-sans-extension
  12214. (file-name-nondirectory
  12215. buffer-file-name)))
  12216. default-directory 'overlays msg at 'forbuffer)
  12217. (message msg "done. Use `C-c C-c' to remove images.")))))
  12218. (defvar org-latex-regexps
  12219. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12220. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12221. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12222. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12223. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12224. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12225. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12226. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12227. "Regular expressions for matching embedded LaTeX.")
  12228. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12229. "Replace LaTeX fragments with links to an image, and produce images."
  12230. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12231. (let* ((prefixnodir (file-name-nondirectory prefix))
  12232. (absprefix (expand-file-name prefix dir))
  12233. (todir (file-name-directory absprefix))
  12234. (opt org-format-latex-options)
  12235. (matchers (plist-get opt :matchers))
  12236. (re-list org-latex-regexps)
  12237. (cnt 0) txt link beg end re e checkdir
  12238. m n block linkfile movefile ov)
  12239. ;; Check if there are old images files with this prefix, and remove them
  12240. (when (file-directory-p todir)
  12241. (mapc 'delete-file
  12242. (directory-files
  12243. todir 'full
  12244. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12245. ;; Check the different regular expressions
  12246. (while (setq e (pop re-list))
  12247. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12248. block (if (nth 3 e) "\n\n" ""))
  12249. (when (member m matchers)
  12250. (goto-char (point-min))
  12251. (while (re-search-forward re nil t)
  12252. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12253. (not (get-text-property (match-beginning n)
  12254. 'org-protected)))
  12255. (setq txt (match-string n)
  12256. beg (match-beginning n) end (match-end n)
  12257. cnt (1+ cnt)
  12258. linkfile (format "%s_%04d.png" prefix cnt)
  12259. movefile (format "%s_%04d.png" absprefix cnt)
  12260. link (concat block "[[file:" linkfile "]]" block))
  12261. (if msg (message msg cnt))
  12262. (goto-char beg)
  12263. (unless checkdir ; make sure the directory exists
  12264. (setq checkdir t)
  12265. (or (file-directory-p todir) (make-directory todir)))
  12266. (org-create-formula-image
  12267. txt movefile opt forbuffer)
  12268. (if overlays
  12269. (progn
  12270. (setq ov (org-make-overlay beg end))
  12271. (if (featurep 'xemacs)
  12272. (progn
  12273. (org-overlay-put ov 'invisible t)
  12274. (org-overlay-put
  12275. ov 'end-glyph
  12276. (make-glyph (vector 'png :file movefile))))
  12277. (org-overlay-put
  12278. ov 'display
  12279. (list 'image :type 'png :file movefile :ascent 'center)))
  12280. (push ov org-latex-fragment-image-overlays)
  12281. (goto-char end))
  12282. (delete-region beg end)
  12283. (insert link))))))))
  12284. ;; This function borrows from Ganesh Swami's latex2png.el
  12285. (defun org-create-formula-image (string tofile options buffer)
  12286. (let* ((tmpdir (if (featurep 'xemacs)
  12287. (temp-directory)
  12288. temporary-file-directory))
  12289. (texfilebase (make-temp-name
  12290. (expand-file-name "orgtex" tmpdir)))
  12291. (texfile (concat texfilebase ".tex"))
  12292. (dvifile (concat texfilebase ".dvi"))
  12293. (pngfile (concat texfilebase ".png"))
  12294. (fnh (if (featurep 'xemacs)
  12295. (font-height (get-face-font 'default))
  12296. (face-attribute 'default :height nil)))
  12297. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12298. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12299. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12300. "Black"))
  12301. (bg (or (plist-get options (if buffer :background :html-background))
  12302. "Transparent")))
  12303. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12304. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12305. (with-temp-file texfile
  12306. (insert org-format-latex-header
  12307. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12308. (let ((dir default-directory))
  12309. (condition-case nil
  12310. (progn
  12311. (cd tmpdir)
  12312. (call-process "latex" nil nil nil texfile))
  12313. (error nil))
  12314. (cd dir))
  12315. (if (not (file-exists-p dvifile))
  12316. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12317. (condition-case nil
  12318. (call-process "dvipng" nil nil nil
  12319. "-E" "-fg" fg "-bg" bg
  12320. "-D" dpi
  12321. ;;"-x" scale "-y" scale
  12322. "-T" "tight"
  12323. "-o" pngfile
  12324. dvifile)
  12325. (error nil))
  12326. (if (not (file-exists-p pngfile))
  12327. (progn (message "Failed to create png file from %s" texfile) nil)
  12328. ;; Use the requested file name and clean up
  12329. (copy-file pngfile tofile 'replace)
  12330. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12331. (delete-file (concat texfilebase e)))
  12332. pngfile))))
  12333. (defun org-dvipng-color (attr)
  12334. "Return an rgb color specification for dvipng."
  12335. (apply 'format "rgb %s %s %s"
  12336. (mapcar 'org-normalize-color
  12337. (color-values (face-attribute 'default attr nil)))))
  12338. (defun org-normalize-color (value)
  12339. "Return string to be used as color value for an RGB component."
  12340. (format "%g" (/ value 65535.0)))
  12341. ;;;; Key bindings
  12342. ;; Make `C-c C-x' a prefix key
  12343. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12344. ;; TAB key with modifiers
  12345. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12346. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12347. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12348. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12349. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12350. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12351. ;; The following line is necessary under Suse GNU/Linux
  12352. (unless (featurep 'xemacs)
  12353. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12354. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12355. (define-key org-mode-map [backtab] 'org-shifttab)
  12356. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12357. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12358. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12359. ;; Cursor keys with modifiers
  12360. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12361. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12362. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12363. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12364. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12365. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12366. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12367. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12368. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12369. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12370. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12371. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12372. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12373. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12374. ;;; Extra keys for tty access.
  12375. ;; We only set them when really needed because otherwise the
  12376. ;; menus don't show the simple keys
  12377. (when (or org-use-extra-keys
  12378. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12379. (not window-system))
  12380. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12381. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12382. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12383. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12384. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12385. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12386. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12387. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12388. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12389. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12390. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12391. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12392. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12393. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12394. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12395. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12396. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12397. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12398. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12399. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12400. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12401. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12402. ;; All the other keys
  12403. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12404. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12405. (if (boundp 'narrow-map)
  12406. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12407. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12408. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12409. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12410. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12411. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12412. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12413. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12414. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12415. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12416. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12417. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12418. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12419. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12420. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12421. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12422. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12423. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12424. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12425. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12426. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12427. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12428. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12429. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12430. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12431. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12432. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12433. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12434. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12435. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12436. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12437. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12438. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12439. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12440. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12441. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12442. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12443. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12444. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12445. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12446. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12447. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12448. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12449. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12450. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12451. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12452. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12453. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12454. (org-defkey org-mode-map "\C-m" 'org-return)
  12455. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12456. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12457. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12458. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12459. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12460. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12461. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12462. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12463. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12464. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12465. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12466. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12467. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12468. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12469. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12470. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12471. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12472. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12473. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12474. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12475. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12476. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12477. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12478. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12479. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12480. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12481. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12482. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12483. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12484. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12485. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12486. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12487. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12488. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12489. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12490. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12491. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12492. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12493. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12494. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12495. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12496. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12497. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12498. (when (featurep 'xemacs)
  12499. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12500. (defvar org-self-insert-command-undo-counter 0)
  12501. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12502. (defun org-self-insert-command (N)
  12503. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12504. If the cursor is in a table looking at whitespace, the whitespace is
  12505. overwritten, and the table is not marked as requiring realignment."
  12506. (interactive "p")
  12507. (if (and
  12508. (org-table-p)
  12509. (progn
  12510. ;; check if we blank the field, and if that triggers align
  12511. (and (featurep 'org-table) org-table-auto-blank-field
  12512. (member last-command
  12513. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12514. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12515. ;; got extra space, this field does not determine column width
  12516. (let (org-table-may-need-update) (org-table-blank-field))
  12517. ;; no extra space, this field may determine column width
  12518. (org-table-blank-field)))
  12519. t)
  12520. (eq N 1)
  12521. (looking-at "[^|\n]* |"))
  12522. (let (org-table-may-need-update)
  12523. (goto-char (1- (match-end 0)))
  12524. (delete-backward-char 1)
  12525. (goto-char (match-beginning 0))
  12526. (self-insert-command N))
  12527. (setq org-table-may-need-update t)
  12528. (self-insert-command N)
  12529. (org-fix-tags-on-the-fly)
  12530. (if org-self-insert-cluster-for-undo
  12531. (if (not (eq last-command 'org-self-insert-command))
  12532. (setq org-self-insert-command-undo-counter 1)
  12533. (if (>= org-self-insert-command-undo-counter 20)
  12534. (setq org-self-insert-command-undo-counter 1)
  12535. (and (> org-self-insert-command-undo-counter 0)
  12536. buffer-undo-list
  12537. (not (cadr buffer-undo-list)) ; remove nil entry
  12538. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12539. (setq org-self-insert-command-undo-counter
  12540. (1+ org-self-insert-command-undo-counter)))))))
  12541. (defun org-fix-tags-on-the-fly ()
  12542. (when (and (equal (char-after (point-at-bol)) ?*)
  12543. (org-on-heading-p))
  12544. (org-align-tags-here org-tags-column)))
  12545. (defun org-delete-backward-char (N)
  12546. "Like `delete-backward-char', insert whitespace at field end in tables.
  12547. When deleting backwards, in tables this function will insert whitespace in
  12548. front of the next \"|\" separator, to keep the table aligned. The table will
  12549. still be marked for re-alignment if the field did fill the entire column,
  12550. because, in this case the deletion might narrow the column."
  12551. (interactive "p")
  12552. (if (and (org-table-p)
  12553. (eq N 1)
  12554. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12555. (looking-at ".*?|"))
  12556. (let ((pos (point))
  12557. (noalign (looking-at "[^|\n\r]* |"))
  12558. (c org-table-may-need-update))
  12559. (backward-delete-char N)
  12560. (skip-chars-forward "^|")
  12561. (insert " ")
  12562. (goto-char (1- pos))
  12563. ;; noalign: if there were two spaces at the end, this field
  12564. ;; does not determine the width of the column.
  12565. (if noalign (setq org-table-may-need-update c)))
  12566. (backward-delete-char N)
  12567. (org-fix-tags-on-the-fly)))
  12568. (defun org-delete-char (N)
  12569. "Like `delete-char', but insert whitespace at field end in tables.
  12570. When deleting characters, in tables this function will insert whitespace in
  12571. front of the next \"|\" separator, to keep the table aligned. The table will
  12572. still be marked for re-alignment if the field did fill the entire column,
  12573. because, in this case the deletion might narrow the column."
  12574. (interactive "p")
  12575. (if (and (org-table-p)
  12576. (not (bolp))
  12577. (not (= (char-after) ?|))
  12578. (eq N 1))
  12579. (if (looking-at ".*?|")
  12580. (let ((pos (point))
  12581. (noalign (looking-at "[^|\n\r]* |"))
  12582. (c org-table-may-need-update))
  12583. (replace-match (concat
  12584. (substring (match-string 0) 1 -1)
  12585. " |"))
  12586. (goto-char pos)
  12587. ;; noalign: if there were two spaces at the end, this field
  12588. ;; does not determine the width of the column.
  12589. (if noalign (setq org-table-may-need-update c)))
  12590. (delete-char N))
  12591. (delete-char N)
  12592. (org-fix-tags-on-the-fly)))
  12593. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12594. (put 'org-self-insert-command 'delete-selection t)
  12595. (put 'orgtbl-self-insert-command 'delete-selection t)
  12596. (put 'org-delete-char 'delete-selection 'supersede)
  12597. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12598. (put 'org-yank 'delete-selection 'yank)
  12599. ;; Make `flyspell-mode' delay after some commands
  12600. (put 'org-self-insert-command 'flyspell-delayed t)
  12601. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12602. (put 'org-delete-char 'flyspell-delayed t)
  12603. (put 'org-delete-backward-char 'flyspell-delayed t)
  12604. ;; Make pabbrev-mode expand after org-mode commands
  12605. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12606. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12607. ;; How to do this: Measure non-white length of current string
  12608. ;; If equal to column width, we should realign.
  12609. (defun org-remap (map &rest commands)
  12610. "In MAP, remap the functions given in COMMANDS.
  12611. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12612. (let (new old)
  12613. (while commands
  12614. (setq old (pop commands) new (pop commands))
  12615. (if (fboundp 'command-remapping)
  12616. (org-defkey map (vector 'remap old) new)
  12617. (substitute-key-definition old new map global-map)))))
  12618. (when (eq org-enable-table-editor 'optimized)
  12619. ;; If the user wants maximum table support, we need to hijack
  12620. ;; some standard editing functions
  12621. (org-remap org-mode-map
  12622. 'self-insert-command 'org-self-insert-command
  12623. 'delete-char 'org-delete-char
  12624. 'delete-backward-char 'org-delete-backward-char)
  12625. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12626. (defvar org-ctrl-c-ctrl-c-hook nil
  12627. "Hook for functions attaching themselves to `C-c C-c'.
  12628. This can be used to add additional functionality to the C-c C-c key which
  12629. executes context-dependent commands.
  12630. Each function will be called with no arguments. The function must check
  12631. if the context is appropriate for it to act. If yes, it should do its
  12632. thing and then return a non-nil value. If the context is wrong,
  12633. just do nothing.")
  12634. (defvar org-metaleft-hook nil
  12635. "Hook for functions attaching themselves to `M-left'.
  12636. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12637. (defvar org-metaright-hook nil
  12638. "Hook for functions attaching themselves to `M-right'.
  12639. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12640. (defvar org-metaup-hook nil
  12641. "Hook for functions attaching themselves to `M-up'.
  12642. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12643. (defvar org-metadown-hook nil
  12644. "Hook for functions attaching themselves to `M-down'.
  12645. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12646. (defvar org-shiftmetaleft-hook nil
  12647. "Hook for functions attaching themselves to `M-S-left'.
  12648. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12649. (defvar org-shiftmetaright-hook nil
  12650. "Hook for functions attaching themselves to `M-S-right'.
  12651. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12652. (defvar org-shiftmetaup-hook nil
  12653. "Hook for functions attaching themselves to `M-S-up'.
  12654. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12655. (defvar org-shiftmetadown-hook nil
  12656. "Hook for functions attaching themselves to `M-S-down'.
  12657. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12658. (defvar org-metareturn-hook nil
  12659. "Hook for functions attaching themselves to `M-RET'.
  12660. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12661. (defun org-modifier-cursor-error ()
  12662. "Throw an error, a modified cursor command was applied in wrong context."
  12663. (error "This command is active in special context like tables, headlines or items"))
  12664. (defun org-shiftselect-error ()
  12665. "Throw an error because Shift-Cursor command was applied in wrong context."
  12666. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12667. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12668. (error "This command works only in special context like headlines or timestamps.")))
  12669. (defun org-call-for-shift-select (cmd)
  12670. (let ((this-command-keys-shift-translated t))
  12671. (call-interactively cmd)))
  12672. (defun org-shifttab (&optional arg)
  12673. "Global visibility cycling or move to previous table field.
  12674. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12675. on context.
  12676. See the individual commands for more information."
  12677. (interactive "P")
  12678. (cond
  12679. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12680. ((integerp arg)
  12681. (message "Content view to level: %d" arg)
  12682. (org-content (prefix-numeric-value arg))
  12683. (setq org-cycle-global-status 'overview))
  12684. (t (call-interactively 'org-global-cycle))))
  12685. (defun org-shiftmetaleft ()
  12686. "Promote subtree or delete table column.
  12687. Calls `org-promote-subtree', `org-outdent-item',
  12688. or `org-table-delete-column', depending on context.
  12689. See the individual commands for more information."
  12690. (interactive)
  12691. (cond
  12692. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12693. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12694. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12695. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12696. (t (org-modifier-cursor-error))))
  12697. (defun org-shiftmetaright ()
  12698. "Demote subtree or insert table column.
  12699. Calls `org-demote-subtree', `org-indent-item',
  12700. or `org-table-insert-column', depending on context.
  12701. See the individual commands for more information."
  12702. (interactive)
  12703. (cond
  12704. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12705. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12706. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12707. ((org-at-item-p) (call-interactively 'org-indent-item))
  12708. (t (org-modifier-cursor-error))))
  12709. (defun org-shiftmetaup (&optional arg)
  12710. "Move subtree up or kill table row.
  12711. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12712. `org-move-item-up' depending on context. See the individual commands
  12713. for more information."
  12714. (interactive "P")
  12715. (cond
  12716. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12717. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12718. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12719. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12720. (t (org-modifier-cursor-error))))
  12721. (defun org-shiftmetadown (&optional arg)
  12722. "Move subtree down or insert table row.
  12723. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12724. `org-move-item-down', depending on context. See the individual
  12725. commands for more information."
  12726. (interactive "P")
  12727. (cond
  12728. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12729. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12730. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12731. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12732. (t (org-modifier-cursor-error))))
  12733. (defun org-metaleft (&optional arg)
  12734. "Promote heading or move table column to left.
  12735. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12736. With no specific context, calls the Emacs default `backward-word'.
  12737. See the individual commands for more information."
  12738. (interactive "P")
  12739. (cond
  12740. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12741. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12742. ((or (org-on-heading-p)
  12743. (and (org-region-active-p)
  12744. (save-excursion
  12745. (goto-char (region-beginning))
  12746. (org-on-heading-p))))
  12747. (call-interactively 'org-do-promote))
  12748. ((or (org-at-item-p)
  12749. (and (org-region-active-p)
  12750. (save-excursion
  12751. (goto-char (region-beginning))
  12752. (org-at-item-p))))
  12753. (call-interactively 'org-outdent-item))
  12754. (t (call-interactively 'backward-word))))
  12755. (defun org-metaright (&optional arg)
  12756. "Demote subtree or move table column to right.
  12757. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12758. With no specific context, calls the Emacs default `forward-word'.
  12759. See the individual commands for more information."
  12760. (interactive "P")
  12761. (cond
  12762. ((run-hook-with-args-until-success 'org-metaright-hook))
  12763. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12764. ((or (org-on-heading-p)
  12765. (and (org-region-active-p)
  12766. (save-excursion
  12767. (goto-char (region-beginning))
  12768. (org-on-heading-p))))
  12769. (call-interactively 'org-do-demote))
  12770. ((or (org-at-item-p)
  12771. (and (org-region-active-p)
  12772. (save-excursion
  12773. (goto-char (region-beginning))
  12774. (org-at-item-p))))
  12775. (call-interactively 'org-indent-item))
  12776. (t (call-interactively 'forward-word))))
  12777. (defun org-metaup (&optional arg)
  12778. "Move subtree up or move table row up.
  12779. Calls `org-move-subtree-up' or `org-table-move-row' or
  12780. `org-move-item-up', depending on context. See the individual commands
  12781. for more information."
  12782. (interactive "P")
  12783. (cond
  12784. ((run-hook-with-args-until-success 'org-metaup-hook))
  12785. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12786. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12787. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12788. (t (transpose-lines 1) (beginning-of-line -1))))
  12789. (defun org-metadown (&optional arg)
  12790. "Move subtree down or move table row down.
  12791. Calls `org-move-subtree-down' or `org-table-move-row' or
  12792. `org-move-item-down', depending on context. See the individual
  12793. commands for more information."
  12794. (interactive "P")
  12795. (cond
  12796. ((run-hook-with-args-until-success 'org-metadown-hook))
  12797. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12798. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12799. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12800. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12801. (defun org-shiftup (&optional arg)
  12802. "Increase item in timestamp or increase priority of current headline.
  12803. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12804. depending on context. See the individual commands for more information."
  12805. (interactive "P")
  12806. (cond
  12807. ((and org-support-shift-select (org-region-active-p))
  12808. (org-call-for-shift-select 'previous-line))
  12809. ((org-at-timestamp-p t)
  12810. (call-interactively (if org-edit-timestamp-down-means-later
  12811. 'org-timestamp-down 'org-timestamp-up)))
  12812. ((and (not (eq org-support-shift-select 'always))
  12813. (org-on-heading-p))
  12814. (call-interactively 'org-priority-up))
  12815. ((and (not org-support-shift-select) (org-at-item-p))
  12816. (call-interactively 'org-previous-item))
  12817. ((org-clocktable-try-shift 'up arg))
  12818. (org-support-shift-select
  12819. (org-call-for-shift-select 'previous-line))
  12820. (t (org-shiftselect-error))))
  12821. (defun org-shiftdown (&optional arg)
  12822. "Decrease item in timestamp or decrease priority of current headline.
  12823. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12824. depending on context. See the individual commands for more information."
  12825. (interactive "P")
  12826. (cond
  12827. ((and org-support-shift-select (org-region-active-p))
  12828. (org-call-for-shift-select 'next-line))
  12829. ((org-at-timestamp-p t)
  12830. (call-interactively (if org-edit-timestamp-down-means-later
  12831. 'org-timestamp-up 'org-timestamp-down)))
  12832. ((and (not (eq org-support-shift-select 'always))
  12833. (org-on-heading-p))
  12834. (call-interactively 'org-priority-down))
  12835. ((and (not org-support-shift-select) (org-at-item-p))
  12836. (call-interactively 'org-next-item))
  12837. ((org-clocktable-try-shift 'down arg))
  12838. (org-support-shift-select
  12839. (org-call-for-shift-select 'next-line))
  12840. (t (org-shiftselect-error))))
  12841. (defun org-shiftright (&optional arg)
  12842. "Cycle the thing at point or in the current line, depending on context.
  12843. Depending on context, this does one of the following:
  12844. - switch a timestamp at point one day into the future
  12845. - on a headline, switch to the next TODO keyword.
  12846. - on an item, switch entire list to the next bullet type
  12847. - on a property line, switch to the next allowed value
  12848. - on a clocktable definition line, move time block into the future"
  12849. (interactive "P")
  12850. (cond
  12851. ((and org-support-shift-select (org-region-active-p))
  12852. (org-call-for-shift-select 'forward-char))
  12853. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12854. ((and (not (eq org-support-shift-select 'always))
  12855. (org-on-heading-p))
  12856. (org-call-with-arg 'org-todo 'right))
  12857. ((or (and org-support-shift-select
  12858. (not (eq org-support-shift-select 'always))
  12859. (org-at-item-bullet-p))
  12860. (and (not org-support-shift-select) (org-at-item-p)))
  12861. (org-call-with-arg 'org-cycle-list-bullet nil))
  12862. ((and (not (eq org-support-shift-select 'always))
  12863. (org-at-property-p))
  12864. (call-interactively 'org-property-next-allowed-value))
  12865. ((org-clocktable-try-shift 'right arg))
  12866. (org-support-shift-select
  12867. (org-call-for-shift-select 'forward-char))
  12868. (t (org-shiftselect-error))))
  12869. (defun org-shiftleft (&optional arg)
  12870. "Cycle the thing at point or in the current line, depending on context.
  12871. Depending on context, this does one of the following:
  12872. - switch a timestamp at point one day into the past
  12873. - on a headline, switch to the previous TODO keyword.
  12874. - on an item, switch entire list to the previous bullet type
  12875. - on a property line, switch to the previous allowed value
  12876. - on a clocktable definition line, move time block into the past"
  12877. (interactive "P")
  12878. (cond
  12879. ((and org-support-shift-select (org-region-active-p))
  12880. (org-call-for-shift-select 'backward-char))
  12881. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12882. ((and (not (eq org-support-shift-select 'always))
  12883. (org-on-heading-p))
  12884. (org-call-with-arg 'org-todo 'left))
  12885. ((or (and org-support-shift-select
  12886. (not (eq org-support-shift-select 'always))
  12887. (org-at-item-bullet-p))
  12888. (and (not org-support-shift-select) (org-at-item-p)))
  12889. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12890. ((and (not (eq org-support-shift-select 'always))
  12891. (org-at-property-p))
  12892. (call-interactively 'org-property-previous-allowed-value))
  12893. ((org-clocktable-try-shift 'left arg))
  12894. (org-support-shift-select
  12895. (org-call-for-shift-select 'backward-char))
  12896. (t (org-shiftselect-error))))
  12897. (defun org-shiftcontrolright ()
  12898. "Switch to next TODO set."
  12899. (interactive)
  12900. (cond
  12901. ((and org-support-shift-select (org-region-active-p))
  12902. (org-call-for-shift-select 'forward-word))
  12903. ((and (not (eq org-support-shift-select 'always))
  12904. (org-on-heading-p))
  12905. (org-call-with-arg 'org-todo 'nextset))
  12906. (org-support-shift-select
  12907. (org-call-for-shift-select 'forward-word))
  12908. (t (org-shiftselect-error))))
  12909. (defun org-shiftcontrolleft ()
  12910. "Switch to previous TODO set."
  12911. (interactive)
  12912. (cond
  12913. ((and org-support-shift-select (org-region-active-p))
  12914. (org-call-for-shift-select 'backward-word))
  12915. ((and (not (eq org-support-shift-select 'always))
  12916. (org-on-heading-p))
  12917. (org-call-with-arg 'org-todo 'previousset))
  12918. (org-support-shift-select
  12919. (org-call-for-shift-select 'backward-word))
  12920. (t (org-shiftselect-error))))
  12921. (defun org-ctrl-c-ret ()
  12922. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12923. (interactive)
  12924. (cond
  12925. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12926. (t (call-interactively 'org-insert-heading))))
  12927. (defun org-copy-special ()
  12928. "Copy region in table or copy current subtree.
  12929. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12930. See the individual commands for more information."
  12931. (interactive)
  12932. (call-interactively
  12933. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12934. (defun org-cut-special ()
  12935. "Cut region in table or cut current subtree.
  12936. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12937. See the individual commands for more information."
  12938. (interactive)
  12939. (call-interactively
  12940. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12941. (defun org-paste-special (arg)
  12942. "Paste rectangular region into table, or past subtree relative to level.
  12943. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12944. See the individual commands for more information."
  12945. (interactive "P")
  12946. (if (org-at-table-p)
  12947. (org-table-paste-rectangle)
  12948. (org-paste-subtree arg)))
  12949. (defun org-edit-special ()
  12950. "Call a special editor for the stuff at point.
  12951. When at a table, call the formula editor with `org-table-edit-formulas'.
  12952. When at the first line of an src example, call `org-edit-src-code'.
  12953. When in an #+include line, visit the include file. Otherwise call
  12954. `ffap' to visit the file at point."
  12955. (interactive)
  12956. (cond
  12957. ((org-at-table-p)
  12958. (call-interactively 'org-table-edit-formulas))
  12959. ((save-excursion
  12960. (beginning-of-line 1)
  12961. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12962. (find-file (org-trim (match-string 1))))
  12963. ((org-edit-src-code))
  12964. ((org-edit-fixed-width-region))
  12965. (t (call-interactively 'ffap))))
  12966. (defun org-ctrl-c-ctrl-c (&optional arg)
  12967. "Set tags in headline, or update according to changed information at point.
  12968. This command does many different things, depending on context:
  12969. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12970. this is what we do.
  12971. - If the cursor is in a headline, prompt for tags and insert them
  12972. into the current line, aligned to `org-tags-column'. When called
  12973. with prefix arg, realign all tags in the current buffer.
  12974. - If the cursor is in one of the special #+KEYWORD lines, this
  12975. triggers scanning the buffer for these lines and updating the
  12976. information.
  12977. - If the cursor is inside a table, realign the table. This command
  12978. works even if the automatic table editor has been turned off.
  12979. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12980. the entire table.
  12981. - If the cursor is at a footnote reference or definition, jump to
  12982. the corresponding definition or references, respectively.
  12983. - If the cursor is a the beginning of a dynamic block, update it.
  12984. - If the cursor is inside a table created by the table.el package,
  12985. activate that table.
  12986. - If the current buffer is a remember buffer, close note and file
  12987. it. A prefix argument of 1 files to the default location
  12988. without further interaction. A prefix argument of 2 files to
  12989. the currently clocking task.
  12990. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12991. links in this buffer.
  12992. - If the cursor is on a numbered item in a plain list, renumber the
  12993. ordered list.
  12994. - If the cursor is on a checkbox, toggle it."
  12995. (interactive "P")
  12996. (let ((org-enable-table-editor t))
  12997. (cond
  12998. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12999. org-occur-highlights
  13000. org-latex-fragment-image-overlays)
  13001. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13002. (org-remove-occur-highlights)
  13003. (org-remove-latex-fragment-image-overlays)
  13004. (message "Temporary highlights/overlays removed from current buffer"))
  13005. ((and (local-variable-p 'org-finish-function (current-buffer))
  13006. (fboundp org-finish-function))
  13007. (funcall org-finish-function))
  13008. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13009. ((org-at-property-p)
  13010. (call-interactively 'org-property-action))
  13011. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13012. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13013. ((org-at-table.el-p)
  13014. (require 'table)
  13015. (beginning-of-line 1)
  13016. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13017. (call-interactively 'table-recognize-table))
  13018. ((org-at-table-p)
  13019. (org-table-maybe-eval-formula)
  13020. (if arg
  13021. (call-interactively 'org-table-recalculate)
  13022. (org-table-maybe-recalculate-line))
  13023. (call-interactively 'org-table-align))
  13024. ((or (org-footnote-at-reference-p)
  13025. (org-footnote-at-definition-p))
  13026. (call-interactively 'org-footnote-action))
  13027. ((org-at-item-checkbox-p)
  13028. (call-interactively 'org-toggle-checkbox))
  13029. ((org-at-item-p)
  13030. (if arg
  13031. (call-interactively 'org-toggle-checkbox)
  13032. (call-interactively 'org-maybe-renumber-ordered-list)))
  13033. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13034. ;; Dynamic block
  13035. (beginning-of-line 1)
  13036. (save-excursion (org-update-dblock)))
  13037. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13038. (cond
  13039. ((equal (match-string 1) "TBLFM")
  13040. ;; Recalculate the table before this line
  13041. (save-excursion
  13042. (beginning-of-line 1)
  13043. (skip-chars-backward " \r\n\t")
  13044. (if (org-at-table-p)
  13045. (org-call-with-arg 'org-table-recalculate t))))
  13046. (t
  13047. ; (org-set-regexps-and-options)
  13048. ; (org-restart-font-lock)
  13049. (let ((org-inhibit-startup t)) (org-mode-restart))
  13050. (message "Local setup has been refreshed"))))
  13051. (t (error "C-c C-c can do nothing useful at this location.")))))
  13052. (defun org-mode-restart ()
  13053. "Restart Org-mode, to scan again for special lines.
  13054. Also updates the keyword regular expressions."
  13055. (interactive)
  13056. (org-mode)
  13057. (message "Org-mode restarted"))
  13058. (defun org-kill-note-or-show-branches ()
  13059. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13060. (interactive)
  13061. (if (not org-finish-function)
  13062. (call-interactively 'show-branches)
  13063. (let ((org-note-abort t))
  13064. (funcall org-finish-function))))
  13065. (defun org-return (&optional indent)
  13066. "Goto next table row or insert a newline.
  13067. Calls `org-table-next-row' or `newline', depending on context.
  13068. See the individual commands for more information."
  13069. (interactive)
  13070. (cond
  13071. ((bobp) (if indent (newline-and-indent) (newline)))
  13072. ((org-at-table-p)
  13073. (org-table-justify-field-maybe)
  13074. (call-interactively 'org-table-next-row))
  13075. ((and org-return-follows-link
  13076. (eq (get-text-property (point) 'face) 'org-link))
  13077. (call-interactively 'org-open-at-point))
  13078. ((and (org-at-heading-p)
  13079. (looking-at
  13080. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13081. (org-show-entry)
  13082. (end-of-line 1)
  13083. (newline))
  13084. (t (if indent (newline-and-indent) (newline)))))
  13085. (defun org-return-indent ()
  13086. "Goto next table row or insert a newline and indent.
  13087. Calls `org-table-next-row' or `newline-and-indent', depending on
  13088. context. See the individual commands for more information."
  13089. (interactive)
  13090. (org-return t))
  13091. (defun org-ctrl-c-star ()
  13092. "Compute table, or change heading status of lines.
  13093. Calls `org-table-recalculate' or `org-toggle-heading',
  13094. depending on context."
  13095. (interactive)
  13096. (cond
  13097. ((org-at-table-p)
  13098. (call-interactively 'org-table-recalculate))
  13099. (t
  13100. ;; Convert all lines in region to list items
  13101. (call-interactively 'org-toggle-heading))))
  13102. (defun org-ctrl-c-minus ()
  13103. "Insert separator line in table or modify bullet status of line.
  13104. Also turns a plain line or a region of lines into list items.
  13105. Calls `org-table-insert-hline', `org-toggle-item', or
  13106. `org-cycle-list-bullet', depending on context."
  13107. (interactive)
  13108. (cond
  13109. ((org-at-table-p)
  13110. (call-interactively 'org-table-insert-hline))
  13111. ((org-region-active-p)
  13112. (call-interactively 'org-toggle-item))
  13113. ((org-in-item-p)
  13114. (call-interactively 'org-cycle-list-bullet))
  13115. (t
  13116. (call-interactively 'org-toggle-item))))
  13117. (defun org-toggle-item ()
  13118. "Convert headings or normal lines to items, items to normal lines.
  13119. If there is no active region, only the current line is considered.
  13120. If the first line in the region is a headline, convert all headlines to items.
  13121. If the first line in the region is an item, convert all items to normal lines.
  13122. If the first line is normal text, add an item bullet to each line."
  13123. (interactive)
  13124. (let (l2 l beg end)
  13125. (if (org-region-active-p)
  13126. (setq beg (region-beginning) end (region-end))
  13127. (setq beg (point-at-bol)
  13128. end (min (1+ (point-at-eol)) (point-max))))
  13129. (save-excursion
  13130. (goto-char end)
  13131. (setq l2 (org-current-line))
  13132. (goto-char beg)
  13133. (beginning-of-line 1)
  13134. (setq l (1- (org-current-line)))
  13135. (if (org-at-item-p)
  13136. ;; We already have items, de-itemize
  13137. (while (< (setq l (1+ l)) l2)
  13138. (when (org-at-item-p)
  13139. (goto-char (match-beginning 2))
  13140. (delete-region (match-beginning 2) (match-end 2))
  13141. (and (looking-at "[ \t]+") (replace-match "")))
  13142. (beginning-of-line 2))
  13143. (if (org-on-heading-p)
  13144. ;; Headings, convert to items
  13145. (while (< (setq l (1+ l)) l2)
  13146. (if (looking-at org-outline-regexp)
  13147. (replace-match "- " t t))
  13148. (beginning-of-line 2))
  13149. ;; normal lines, turn them into items
  13150. (while (< (setq l (1+ l)) l2)
  13151. (unless (org-at-item-p)
  13152. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13153. (replace-match "\\1- \\2")))
  13154. (beginning-of-line 2)))))))
  13155. (defun org-toggle-heading (&optional nstars)
  13156. "Convert headings to normal text, or items or text to headings.
  13157. If there is no active region, only the current line is considered.
  13158. If the first line is a heading, remove the stars from all headlines
  13159. in the region.
  13160. If the first line is a plain list item, turn all plain list items into
  13161. headings.
  13162. If the first line is a normal line, turn each and every line in the region
  13163. into a heading.
  13164. When converting a line into a heading, the number of stars is chosen
  13165. such that the lines become children of the current entry. However, when
  13166. a prefix argument is given, its value determines the number of stars to add."
  13167. (interactive "P")
  13168. (let (l2 l itemp beg end)
  13169. (if (org-region-active-p)
  13170. (setq beg (region-beginning) end (region-end))
  13171. (setq beg (point-at-bol)
  13172. end (min (1+ (point-at-eol)) (point-max))))
  13173. (save-excursion
  13174. (goto-char end)
  13175. (setq l2 (org-current-line))
  13176. (goto-char beg)
  13177. (beginning-of-line 1)
  13178. (setq l (1- (org-current-line)))
  13179. (if (org-on-heading-p)
  13180. ;; We already have headlines, de-star them
  13181. (while (< (setq l (1+ l)) l2)
  13182. (when (org-on-heading-p t)
  13183. (and (looking-at outline-regexp) (replace-match "")))
  13184. (beginning-of-line 2))
  13185. (setq itemp (org-at-item-p))
  13186. (let* ((stars
  13187. (if nstars
  13188. (make-string (prefix-numeric-value current-prefix-arg)
  13189. ?*)
  13190. (save-excursion
  13191. (re-search-backward org-complex-heading-regexp nil t)
  13192. (or (match-string 1) ""))))
  13193. (add-stars (cond (nstars "")
  13194. ((equal stars "") "*")
  13195. (org-odd-levels-only "**")
  13196. (t "*")))
  13197. (rpl (concat stars add-stars " ")))
  13198. (while (< (setq l (1+ l)) l2)
  13199. (if itemp
  13200. (and (org-at-item-p) (replace-match rpl t t))
  13201. (unless (org-on-heading-p)
  13202. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13203. (replace-match (concat rpl (match-string 2))))))
  13204. (beginning-of-line 2)))))))
  13205. (defun org-meta-return (&optional arg)
  13206. "Insert a new heading or wrap a region in a table.
  13207. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13208. See the individual commands for more information."
  13209. (interactive "P")
  13210. (cond
  13211. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13212. ((org-at-table-p)
  13213. (call-interactively 'org-table-wrap-region))
  13214. (t (call-interactively 'org-insert-heading))))
  13215. ;;; Menu entries
  13216. ;; Define the Org-mode menus
  13217. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13218. '("Tbl"
  13219. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13220. ["Next Field" org-cycle (org-at-table-p)]
  13221. ["Previous Field" org-shifttab (org-at-table-p)]
  13222. ["Next Row" org-return (org-at-table-p)]
  13223. "--"
  13224. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13225. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13226. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13227. "--"
  13228. ("Column"
  13229. ["Move Column Left" org-metaleft (org-at-table-p)]
  13230. ["Move Column Right" org-metaright (org-at-table-p)]
  13231. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13232. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13233. ("Row"
  13234. ["Move Row Up" org-metaup (org-at-table-p)]
  13235. ["Move Row Down" org-metadown (org-at-table-p)]
  13236. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13237. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13238. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13239. "--"
  13240. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13241. ("Rectangle"
  13242. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13243. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13244. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13245. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13246. "--"
  13247. ("Calculate"
  13248. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13249. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13250. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13251. "--"
  13252. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13253. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13254. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13255. "--"
  13256. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13257. "--"
  13258. ["Sum Column/Rectangle" org-table-sum
  13259. (or (org-at-table-p) (org-region-active-p))]
  13260. ["Which Column?" org-table-current-column (org-at-table-p)])
  13261. ["Debug Formulas"
  13262. org-table-toggle-formula-debugger
  13263. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13264. ["Show Col/Row Numbers"
  13265. org-table-toggle-coordinate-overlays
  13266. :style toggle
  13267. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13268. "--"
  13269. ["Create" org-table-create (and (not (org-at-table-p))
  13270. org-enable-table-editor)]
  13271. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13272. ["Import from File" org-table-import (not (org-at-table-p))]
  13273. ["Export to File" org-table-export (org-at-table-p)]
  13274. "--"
  13275. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13276. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13277. '("Org"
  13278. ("Show/Hide"
  13279. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13280. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13281. ["Sparse Tree..." org-sparse-tree t]
  13282. ["Reveal Context" org-reveal t]
  13283. ["Show All" show-all t]
  13284. "--"
  13285. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13286. "--"
  13287. ["New Heading" org-insert-heading t]
  13288. ("Navigate Headings"
  13289. ["Up" outline-up-heading t]
  13290. ["Next" outline-next-visible-heading t]
  13291. ["Previous" outline-previous-visible-heading t]
  13292. ["Next Same Level" outline-forward-same-level t]
  13293. ["Previous Same Level" outline-backward-same-level t]
  13294. "--"
  13295. ["Jump" org-goto t])
  13296. ("Edit Structure"
  13297. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13298. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13299. "--"
  13300. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13301. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13302. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13303. "--"
  13304. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13305. "--"
  13306. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13307. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13308. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13309. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13310. "--"
  13311. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13312. "--"
  13313. ["Convert to odd levels" org-convert-to-odd-levels t]
  13314. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13315. ("Editing"
  13316. ["Emphasis..." org-emphasize t]
  13317. ["Edit Source Example" org-edit-special t]
  13318. "--"
  13319. ["Footnote new/jump" org-footnote-action t]
  13320. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13321. ("Archive"
  13322. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13323. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13324. ; :active t :keys "C-u C-c C-x C-a"]
  13325. ["Sparse trees open ARCHIVE trees"
  13326. (setq org-sparse-tree-open-archived-trees
  13327. (not org-sparse-tree-open-archived-trees))
  13328. :style toggle :selected org-sparse-tree-open-archived-trees]
  13329. ["Cycling opens ARCHIVE trees"
  13330. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13331. :style toggle :selected org-cycle-open-archived-trees]
  13332. "--"
  13333. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13334. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13335. ; ["Check and Move Children" (org-archive-subtree '(4))
  13336. ; :active t :keys "C-u C-c C-x C-s"]
  13337. )
  13338. "--"
  13339. ("Hyperlinks"
  13340. ["Store Link (Global)" org-store-link t]
  13341. ["Insert Link" org-insert-link t]
  13342. ["Follow Link" org-open-at-point t]
  13343. "--"
  13344. ["Next link" org-next-link t]
  13345. ["Previous link" org-previous-link t]
  13346. "--"
  13347. ["Descriptive Links"
  13348. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13349. :style radio
  13350. :selected (member '(org-link) buffer-invisibility-spec)]
  13351. ["Literal Links"
  13352. (progn
  13353. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13354. :style radio
  13355. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13356. "--"
  13357. ("TODO Lists"
  13358. ["TODO/DONE/-" org-todo t]
  13359. ("Select keyword"
  13360. ["Next keyword" org-shiftright (org-on-heading-p)]
  13361. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13362. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13363. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13364. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13365. ["Show TODO Tree" org-show-todo-tree t]
  13366. ["Global TODO list" org-todo-list t]
  13367. "--"
  13368. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13369. :selected org-enforce-todo-dependencies :style toggle :active t]
  13370. "Settings for tree at point"
  13371. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13372. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13373. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13374. ["Do Children parallel" org-toggle-ordered-property :style radio
  13375. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13376. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13377. "--"
  13378. ["Set Priority" org-priority t]
  13379. ["Priority Up" org-shiftup t]
  13380. ["Priority Down" org-shiftdown t]
  13381. "--"
  13382. ["Get news from all feeds" org-feed-update-all t]
  13383. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13384. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13385. ("TAGS and Properties"
  13386. ["Set Tags" org-set-tags-command t]
  13387. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13388. "--"
  13389. ["Set property" org-set-property t]
  13390. ["Column view of properties" org-columns t]
  13391. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13392. ("Dates and Scheduling"
  13393. ["Timestamp" org-time-stamp t]
  13394. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13395. ("Change Date"
  13396. ["1 Day Later" org-shiftright t]
  13397. ["1 Day Earlier" org-shiftleft t]
  13398. ["1 ... Later" org-shiftup t]
  13399. ["1 ... Earlier" org-shiftdown t])
  13400. ["Compute Time Range" org-evaluate-time-range t]
  13401. ["Schedule Item" org-schedule t]
  13402. ["Deadline" org-deadline t]
  13403. "--"
  13404. ["Custom time format" org-toggle-time-stamp-overlays
  13405. :style radio :selected org-display-custom-times]
  13406. "--"
  13407. ["Goto Calendar" org-goto-calendar t]
  13408. ["Date from Calendar" org-date-from-calendar t]
  13409. "--"
  13410. ["Start/Restart Timer" org-timer-start t]
  13411. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13412. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13413. ["Insert Timer String" org-timer t]
  13414. ["Insert Timer Item" org-timer-item t])
  13415. ("Logging work"
  13416. ["Clock in" org-clock-in t]
  13417. ["Clock out" org-clock-out t]
  13418. ["Clock cancel" org-clock-cancel t]
  13419. ["Goto running clock" org-clock-goto t]
  13420. ["Display times" org-clock-display t]
  13421. ["Create clock table" org-clock-report t]
  13422. "--"
  13423. ["Record DONE time"
  13424. (progn (setq org-log-done (not org-log-done))
  13425. (message "Switching to %s will %s record a timestamp"
  13426. (car org-done-keywords)
  13427. (if org-log-done "automatically" "not")))
  13428. :style toggle :selected org-log-done])
  13429. "--"
  13430. ["Agenda Command..." org-agenda t]
  13431. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13432. ("File List for Agenda")
  13433. ("Special views current file"
  13434. ["TODO Tree" org-show-todo-tree t]
  13435. ["Check Deadlines" org-check-deadlines t]
  13436. ["Timeline" org-timeline t]
  13437. ["Tags/Property tree" org-match-sparse-tree t])
  13438. "--"
  13439. ["Export/Publish..." org-export t]
  13440. ("LaTeX"
  13441. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13442. :selected org-cdlatex-mode]
  13443. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13444. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13445. ["Modify math symbol" org-cdlatex-math-modify
  13446. (org-inside-LaTeX-fragment-p)]
  13447. ["Insert citation" org-reftex-citation t]
  13448. "--"
  13449. ["Export LaTeX fragments as images"
  13450. (if (featurep 'org-exp)
  13451. (setq org-export-with-LaTeX-fragments
  13452. (not org-export-with-LaTeX-fragments))
  13453. (require 'org-exp))
  13454. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13455. org-export-with-LaTeX-fragments)])
  13456. "--"
  13457. ("Documentation"
  13458. ["Show Version" org-version t]
  13459. ["Info Documentation" org-info t])
  13460. ("Customize"
  13461. ["Browse Org Group" org-customize t]
  13462. "--"
  13463. ["Expand This Menu" org-create-customize-menu
  13464. (fboundp 'customize-menu-create)])
  13465. "--"
  13466. ("Refresh/Reload"
  13467. ["Refresh setup current buffer" org-mode-restart t]
  13468. ["Reload Org (after update)" org-reload t]
  13469. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13470. ))
  13471. (defun org-info (&optional node)
  13472. "Read documentation for Org-mode in the info system.
  13473. With optional NODE, go directly to that node."
  13474. (interactive)
  13475. (info (format "(org)%s" (or node ""))))
  13476. (defun org-install-agenda-files-menu ()
  13477. (let ((bl (buffer-list)))
  13478. (save-excursion
  13479. (while bl
  13480. (set-buffer (pop bl))
  13481. (if (org-mode-p) (setq bl nil)))
  13482. (when (org-mode-p)
  13483. (easy-menu-change
  13484. '("Org") "File List for Agenda"
  13485. (append
  13486. (list
  13487. ["Edit File List" (org-edit-agenda-file-list) t]
  13488. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13489. ["Remove Current File from List" org-remove-file t]
  13490. ["Cycle through agenda files" org-cycle-agenda-files t]
  13491. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13492. "--")
  13493. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13494. ;;;; Documentation
  13495. ;;;###autoload
  13496. (defun org-require-autoloaded-modules ()
  13497. (interactive)
  13498. (mapc 'require
  13499. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13500. org-docbook org-exp org-export-latex org-html org-icalendar
  13501. org-id org-publish org-remember org-table
  13502. org-timer org-xoxo)))
  13503. ;;;###autoload
  13504. (defun org-reload (&optional uncompiled)
  13505. "Reload all org lisp files.
  13506. With prefix arg UNCOMPILED, load the uncompiled versions."
  13507. (interactive "P")
  13508. (require 'find-func)
  13509. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13510. (dir-org (file-name-directory (org-find-library-name "org")))
  13511. (dir-org-contrib (ignore-errors
  13512. (file-name-directory
  13513. (org-find-library-name "org-contribdir"))))
  13514. (files
  13515. (append (directory-files dir-org t file-re)
  13516. (and dir-org-contrib
  13517. (directory-files dir-org-contrib t file-re))))
  13518. (remove-re (concat (if (featurep 'xemacs)
  13519. "org-colview" "org-colview-xemacs")
  13520. "\\'")))
  13521. (setq files (mapcar 'file-name-sans-extension files))
  13522. (setq files (mapcar
  13523. (lambda (x) (if (string-match remove-re x) nil x))
  13524. files))
  13525. (setq files (delq nil files))
  13526. (mapc
  13527. (lambda (f)
  13528. (when (featurep (intern (file-name-nondirectory f)))
  13529. (if (and (not uncompiled)
  13530. (file-exists-p (concat f ".elc")))
  13531. (load (concat f ".elc") nil nil t)
  13532. (load (concat f ".el") nil nil t))))
  13533. files)))
  13534. ;;;###autoload
  13535. (defun org-customize ()
  13536. "Call the customize function with org as argument."
  13537. (interactive)
  13538. (org-load-modules-maybe)
  13539. (org-require-autoloaded-modules)
  13540. (customize-browse 'org))
  13541. (defun org-create-customize-menu ()
  13542. "Create a full customization menu for Org-mode, insert it into the menu."
  13543. (interactive)
  13544. (org-load-modules-maybe)
  13545. (org-require-autoloaded-modules)
  13546. (if (fboundp 'customize-menu-create)
  13547. (progn
  13548. (easy-menu-change
  13549. '("Org") "Customize"
  13550. `(["Browse Org group" org-customize t]
  13551. "--"
  13552. ,(customize-menu-create 'org)
  13553. ["Set" Custom-set t]
  13554. ["Save" Custom-save t]
  13555. ["Reset to Current" Custom-reset-current t]
  13556. ["Reset to Saved" Custom-reset-saved t]
  13557. ["Reset to Standard Settings" Custom-reset-standard t]))
  13558. (message "\"Org\"-menu now contains full customization menu"))
  13559. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13560. ;;;; Miscellaneous stuff
  13561. ;;; Generally useful functions
  13562. (defun org-find-text-property-in-string (prop s)
  13563. "Return the first non-nil value of property PROP in string S."
  13564. (or (get-text-property 0 prop s)
  13565. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13566. prop s)))
  13567. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13568. "Display the given MESSAGE as a warning."
  13569. (if (fboundp 'display-warning)
  13570. (display-warning 'org message
  13571. (if (featurep 'xemacs)
  13572. 'warning
  13573. :warning))
  13574. (let ((buf (get-buffer-create "*Org warnings*")))
  13575. (with-current-buffer buf
  13576. (goto-char (point-max))
  13577. (insert "Warning (Org): " message)
  13578. (unless (bolp)
  13579. (newline)))
  13580. (display-buffer buf)
  13581. (sit-for 0))))
  13582. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13583. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13584. (if (and marker (marker-buffer marker)
  13585. (buffer-live-p (marker-buffer marker)))
  13586. (progn
  13587. (switch-to-buffer (marker-buffer marker))
  13588. (if (or (> marker (point-max)) (< marker (point-min)))
  13589. (widen))
  13590. (goto-char marker)
  13591. (org-show-context 'org-goto))
  13592. (if bookmark
  13593. (bookmark-jump bookmark)
  13594. (error "Cannot find location"))))
  13595. (defun org-quote-csv-field (s)
  13596. "Quote field for inclusion in CSV material."
  13597. (if (string-match "[\",]" s)
  13598. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13599. s))
  13600. (defun org-plist-delete (plist property)
  13601. "Delete PROPERTY from PLIST.
  13602. This is in contrast to merely setting it to 0."
  13603. (let (p)
  13604. (while plist
  13605. (if (not (eq property (car plist)))
  13606. (setq p (plist-put p (car plist) (nth 1 plist))))
  13607. (setq plist (cddr plist)))
  13608. p))
  13609. (defun org-force-self-insert (N)
  13610. "Needed to enforce self-insert under remapping."
  13611. (interactive "p")
  13612. (self-insert-command N))
  13613. (defun org-string-width (s)
  13614. "Compute width of string, ignoring invisible characters.
  13615. This ignores character with invisibility property `org-link', and also
  13616. characters with property `org-cwidth', because these will become invisible
  13617. upon the next fontification round."
  13618. (let (b l)
  13619. (when (or (eq t buffer-invisibility-spec)
  13620. (assq 'org-link buffer-invisibility-spec))
  13621. (while (setq b (text-property-any 0 (length s)
  13622. 'invisible 'org-link s))
  13623. (setq s (concat (substring s 0 b)
  13624. (substring s (or (next-single-property-change
  13625. b 'invisible s) (length s)))))))
  13626. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13627. (setq s (concat (substring s 0 b)
  13628. (substring s (or (next-single-property-change
  13629. b 'org-cwidth s) (length s))))))
  13630. (setq l (string-width s) b -1)
  13631. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13632. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13633. l))
  13634. (defun org-get-indentation (&optional line)
  13635. "Get the indentation of the current line, interpreting tabs.
  13636. When LINE is given, assume it represents a line and compute its indentation."
  13637. (if line
  13638. (if (string-match "^ *" (org-remove-tabs line))
  13639. (match-end 0))
  13640. (save-excursion
  13641. (beginning-of-line 1)
  13642. (skip-chars-forward " \t")
  13643. (current-column))))
  13644. (defun org-remove-tabs (s &optional width)
  13645. "Replace tabulators in S with spaces.
  13646. Assumes that s is a single line, starting in column 0."
  13647. (setq width (or width tab-width))
  13648. (while (string-match "\t" s)
  13649. (setq s (replace-match
  13650. (make-string
  13651. (- (* width (/ (+ (match-beginning 0) width) width))
  13652. (match-beginning 0)) ?\ )
  13653. t t s)))
  13654. s)
  13655. (defun org-fix-indentation (line ind)
  13656. "Fix indentation in LINE.
  13657. IND is a cons cell with target and minimum indentation.
  13658. If the current indentation in LINE is smaller than the minimum,
  13659. leave it alone. If it is larger than ind, set it to the target."
  13660. (let* ((l (org-remove-tabs line))
  13661. (i (org-get-indentation l))
  13662. (i1 (car ind)) (i2 (cdr ind)))
  13663. (if (>= i i2) (setq l (substring line i2)))
  13664. (if (> i1 0)
  13665. (concat (make-string i1 ?\ ) l)
  13666. l)))
  13667. (defun org-base-buffer (buffer)
  13668. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13669. (if (not buffer)
  13670. buffer
  13671. (or (buffer-base-buffer buffer)
  13672. buffer)))
  13673. (defun org-trim (s)
  13674. "Remove whitespace at beginning and end of string."
  13675. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13676. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13677. s)
  13678. (defun org-wrap (string &optional width lines)
  13679. "Wrap string to either a number of lines, or a width in characters.
  13680. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13681. that costs. If there is a word longer than WIDTH, the text is actually
  13682. wrapped to the length of that word.
  13683. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13684. many lines, whatever width that takes.
  13685. The return value is a list of lines, without newlines at the end."
  13686. (let* ((words (org-split-string string "[ \t\n]+"))
  13687. (maxword (apply 'max (mapcar 'org-string-width words)))
  13688. w ll)
  13689. (cond (width
  13690. (org-do-wrap words (max maxword width)))
  13691. (lines
  13692. (setq w maxword)
  13693. (setq ll (org-do-wrap words maxword))
  13694. (if (<= (length ll) lines)
  13695. ll
  13696. (setq ll words)
  13697. (while (> (length ll) lines)
  13698. (setq w (1+ w))
  13699. (setq ll (org-do-wrap words w)))
  13700. ll))
  13701. (t (error "Cannot wrap this")))))
  13702. (defun org-do-wrap (words width)
  13703. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13704. (let (lines line)
  13705. (while words
  13706. (setq line (pop words))
  13707. (while (and words (< (+ (length line) (length (car words))) width))
  13708. (setq line (concat line " " (pop words))))
  13709. (setq lines (push line lines)))
  13710. (nreverse lines)))
  13711. (defun org-split-string (string &optional separators)
  13712. "Splits STRING into substrings at SEPARATORS.
  13713. No empty strings are returned if there are matches at the beginning
  13714. and end of string."
  13715. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13716. (start 0)
  13717. notfirst
  13718. (list nil))
  13719. (while (and (string-match rexp string
  13720. (if (and notfirst
  13721. (= start (match-beginning 0))
  13722. (< start (length string)))
  13723. (1+ start) start))
  13724. (< (match-beginning 0) (length string)))
  13725. (setq notfirst t)
  13726. (or (eq (match-beginning 0) 0)
  13727. (and (eq (match-beginning 0) (match-end 0))
  13728. (eq (match-beginning 0) start))
  13729. (setq list
  13730. (cons (substring string start (match-beginning 0))
  13731. list)))
  13732. (setq start (match-end 0)))
  13733. (or (eq start (length string))
  13734. (setq list
  13735. (cons (substring string start)
  13736. list)))
  13737. (nreverse list)))
  13738. (defun org-uuidgen-p (s)
  13739. "Is S an ID created by UUIDGEN?"
  13740. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13741. (defun org-context ()
  13742. "Return a list of contexts of the current cursor position.
  13743. If several contexts apply, all are returned.
  13744. Each context entry is a list with a symbol naming the context, and
  13745. two positions indicating start and end of the context. Possible
  13746. contexts are:
  13747. :headline anywhere in a headline
  13748. :headline-stars on the leading stars in a headline
  13749. :todo-keyword on a TODO keyword (including DONE) in a headline
  13750. :tags on the TAGS in a headline
  13751. :priority on the priority cookie in a headline
  13752. :item on the first line of a plain list item
  13753. :item-bullet on the bullet/number of a plain list item
  13754. :checkbox on the checkbox in a plain list item
  13755. :table in an org-mode table
  13756. :table-special on a special filed in a table
  13757. :table-table in a table.el table
  13758. :link on a hyperlink
  13759. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13760. :target on a <<target>>
  13761. :radio-target on a <<<radio-target>>>
  13762. :latex-fragment on a LaTeX fragment
  13763. :latex-preview on a LaTeX fragment with overlayed preview image
  13764. This function expects the position to be visible because it uses font-lock
  13765. faces as a help to recognize the following contexts: :table-special, :link,
  13766. and :keyword."
  13767. (let* ((f (get-text-property (point) 'face))
  13768. (faces (if (listp f) f (list f)))
  13769. (p (point)) clist o)
  13770. ;; First the large context
  13771. (cond
  13772. ((org-on-heading-p t)
  13773. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13774. (when (progn
  13775. (beginning-of-line 1)
  13776. (looking-at org-todo-line-tags-regexp))
  13777. (push (org-point-in-group p 1 :headline-stars) clist)
  13778. (push (org-point-in-group p 2 :todo-keyword) clist)
  13779. (push (org-point-in-group p 4 :tags) clist))
  13780. (goto-char p)
  13781. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13782. (if (looking-at "\\[#[A-Z0-9]\\]")
  13783. (push (org-point-in-group p 0 :priority) clist)))
  13784. ((org-at-item-p)
  13785. (push (org-point-in-group p 2 :item-bullet) clist)
  13786. (push (list :item (point-at-bol)
  13787. (save-excursion (org-end-of-item) (point)))
  13788. clist)
  13789. (and (org-at-item-checkbox-p)
  13790. (push (org-point-in-group p 0 :checkbox) clist)))
  13791. ((org-at-table-p)
  13792. (push (list :table (org-table-begin) (org-table-end)) clist)
  13793. (if (memq 'org-formula faces)
  13794. (push (list :table-special
  13795. (previous-single-property-change p 'face)
  13796. (next-single-property-change p 'face)) clist)))
  13797. ((org-at-table-p 'any)
  13798. (push (list :table-table) clist)))
  13799. (goto-char p)
  13800. ;; Now the small context
  13801. (cond
  13802. ((org-at-timestamp-p)
  13803. (push (org-point-in-group p 0 :timestamp) clist))
  13804. ((memq 'org-link faces)
  13805. (push (list :link
  13806. (previous-single-property-change p 'face)
  13807. (next-single-property-change p 'face)) clist))
  13808. ((memq 'org-special-keyword faces)
  13809. (push (list :keyword
  13810. (previous-single-property-change p 'face)
  13811. (next-single-property-change p 'face)) clist))
  13812. ((org-on-target-p)
  13813. (push (org-point-in-group p 0 :target) clist)
  13814. (goto-char (1- (match-beginning 0)))
  13815. (if (looking-at org-radio-target-regexp)
  13816. (push (org-point-in-group p 0 :radio-target) clist))
  13817. (goto-char p))
  13818. ((setq o (car (delq nil
  13819. (mapcar
  13820. (lambda (x)
  13821. (if (memq x org-latex-fragment-image-overlays) x))
  13822. (org-overlays-at (point))))))
  13823. (push (list :latex-fragment
  13824. (org-overlay-start o) (org-overlay-end o)) clist)
  13825. (push (list :latex-preview
  13826. (org-overlay-start o) (org-overlay-end o)) clist))
  13827. ((org-inside-LaTeX-fragment-p)
  13828. ;; FIXME: positions wrong.
  13829. (push (list :latex-fragment (point) (point)) clist)))
  13830. (setq clist (nreverse (delq nil clist)))
  13831. clist))
  13832. ;; FIXME: Compare with at-regexp-p Do we need both?
  13833. (defun org-in-regexp (re &optional nlines visually)
  13834. "Check if point is inside a match of regexp.
  13835. Normally only the current line is checked, but you can include NLINES extra
  13836. lines both before and after point into the search.
  13837. If VISUALLY is set, require that the cursor is not after the match but
  13838. really on, so that the block visually is on the match."
  13839. (catch 'exit
  13840. (let ((pos (point))
  13841. (eol (point-at-eol (+ 1 (or nlines 0))))
  13842. (inc (if visually 1 0)))
  13843. (save-excursion
  13844. (beginning-of-line (- 1 (or nlines 0)))
  13845. (while (re-search-forward re eol t)
  13846. (if (and (<= (match-beginning 0) pos)
  13847. (>= (+ inc (match-end 0)) pos))
  13848. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13849. (defun org-at-regexp-p (regexp)
  13850. "Is point inside a match of REGEXP in the current line?"
  13851. (catch 'exit
  13852. (save-excursion
  13853. (let ((pos (point)) (end (point-at-eol)))
  13854. (beginning-of-line 1)
  13855. (while (re-search-forward regexp end t)
  13856. (if (and (<= (match-beginning 0) pos)
  13857. (>= (match-end 0) pos))
  13858. (throw 'exit t)))
  13859. nil))))
  13860. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13861. "Call `multi-occur' with buffers for all agenda files."
  13862. (interactive "sOrg-files matching: \np")
  13863. (let* ((files (org-agenda-files))
  13864. (tnames (mapcar 'file-truename files))
  13865. (extra org-agenda-text-search-extra-files)
  13866. f)
  13867. (when (eq (car extra) 'agenda-archives)
  13868. (setq extra (cdr extra))
  13869. (setq files (org-add-archive-files files)))
  13870. (while (setq f (pop extra))
  13871. (unless (member (file-truename f) tnames)
  13872. (add-to-list 'files f 'append)
  13873. (add-to-list 'tnames (file-truename f) 'append)))
  13874. (multi-occur
  13875. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13876. regexp)))
  13877. (if (boundp 'occur-mode-find-occurrence-hook)
  13878. ;; Emacs 23
  13879. (add-hook 'occur-mode-find-occurrence-hook
  13880. (lambda ()
  13881. (when (org-mode-p)
  13882. (org-reveal))))
  13883. ;; Emacs 22
  13884. (defadvice occur-mode-goto-occurrence
  13885. (after org-occur-reveal activate)
  13886. (and (org-mode-p) (org-reveal)))
  13887. (defadvice occur-mode-goto-occurrence-other-window
  13888. (after org-occur-reveal activate)
  13889. (and (org-mode-p) (org-reveal)))
  13890. (defadvice occur-mode-display-occurrence
  13891. (after org-occur-reveal activate)
  13892. (when (org-mode-p)
  13893. (let ((pos (occur-mode-find-occurrence)))
  13894. (with-current-buffer (marker-buffer pos)
  13895. (save-excursion
  13896. (goto-char pos)
  13897. (org-reveal)))))))
  13898. (defun org-uniquify (list)
  13899. "Remove duplicate elements from LIST."
  13900. (let (res)
  13901. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13902. res))
  13903. (defun org-delete-all (elts list)
  13904. "Remove all elements in ELTS from LIST."
  13905. (while elts
  13906. (setq list (delete (pop elts) list)))
  13907. list)
  13908. (defun org-back-over-empty-lines ()
  13909. "Move backwards over whitespace, to the beginning of the first empty line.
  13910. Returns the number of empty lines passed."
  13911. (let ((pos (point)))
  13912. (skip-chars-backward " \t\n\r")
  13913. (beginning-of-line 2)
  13914. (goto-char (min (point) pos))
  13915. (count-lines (point) pos)))
  13916. (defun org-skip-whitespace ()
  13917. (skip-chars-forward " \t\n\r"))
  13918. (defun org-point-in-group (point group &optional context)
  13919. "Check if POINT is in match-group GROUP.
  13920. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13921. match. If the match group does ot exist or point is not inside it,
  13922. return nil."
  13923. (and (match-beginning group)
  13924. (>= point (match-beginning group))
  13925. (<= point (match-end group))
  13926. (if context
  13927. (list context (match-beginning group) (match-end group))
  13928. t)))
  13929. (defun org-switch-to-buffer-other-window (&rest args)
  13930. "Switch to buffer in a second window on the current frame.
  13931. In particular, do not allow pop-up frames."
  13932. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13933. special-display-function)
  13934. (apply 'switch-to-buffer-other-window args)))
  13935. (defun org-combine-plists (&rest plists)
  13936. "Create a single property list from all plists in PLISTS.
  13937. The process starts by copying the first list, and then setting properties
  13938. from the other lists. Settings in the last list are the most significant
  13939. ones and overrule settings in the other lists."
  13940. (let ((rtn (copy-sequence (pop plists)))
  13941. p v ls)
  13942. (while plists
  13943. (setq ls (pop plists))
  13944. (while ls
  13945. (setq p (pop ls) v (pop ls))
  13946. (setq rtn (plist-put rtn p v))))
  13947. rtn))
  13948. (defun org-move-line-down (arg)
  13949. "Move the current line down. With prefix argument, move it past ARG lines."
  13950. (interactive "p")
  13951. (let ((col (current-column))
  13952. beg end pos)
  13953. (beginning-of-line 1) (setq beg (point))
  13954. (beginning-of-line 2) (setq end (point))
  13955. (beginning-of-line (+ 1 arg))
  13956. (setq pos (move-marker (make-marker) (point)))
  13957. (insert (delete-and-extract-region beg end))
  13958. (goto-char pos)
  13959. (org-move-to-column col)))
  13960. (defun org-move-line-up (arg)
  13961. "Move the current line up. With prefix argument, move it past ARG lines."
  13962. (interactive "p")
  13963. (let ((col (current-column))
  13964. beg end pos)
  13965. (beginning-of-line 1) (setq beg (point))
  13966. (beginning-of-line 2) (setq end (point))
  13967. (beginning-of-line (- arg))
  13968. (setq pos (move-marker (make-marker) (point)))
  13969. (insert (delete-and-extract-region beg end))
  13970. (goto-char pos)
  13971. (org-move-to-column col)))
  13972. (defun org-replace-escapes (string table)
  13973. "Replace %-escapes in STRING with values in TABLE.
  13974. TABLE is an association list with keys like \"%a\" and string values.
  13975. The sequences in STRING may contain normal field width and padding information,
  13976. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13977. so values can contain further %-escapes if they are define later in TABLE."
  13978. (let ((case-fold-search nil)
  13979. e re rpl)
  13980. (while (setq e (pop table))
  13981. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13982. (while (string-match re string)
  13983. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13984. (cdr e)))
  13985. (setq string (replace-match rpl t t string))))
  13986. string))
  13987. (defun org-sublist (list start end)
  13988. "Return a section of LIST, from START to END.
  13989. Counting starts at 1."
  13990. (let (rtn (c start))
  13991. (setq list (nthcdr (1- start) list))
  13992. (while (and list (<= c end))
  13993. (push (pop list) rtn)
  13994. (setq c (1+ c)))
  13995. (nreverse rtn)))
  13996. (defun org-find-base-buffer-visiting (file)
  13997. "Like `find-buffer-visiting' but alway return the base buffer and
  13998. not an indirect buffer."
  13999. (let ((buf (find-buffer-visiting file)))
  14000. (if buf
  14001. (or (buffer-base-buffer buf) buf)
  14002. nil)))
  14003. (defun org-image-file-name-regexp (&optional extensions)
  14004. "Return regexp matching the file names of images.
  14005. If EXTENSIONS is given, only match these."
  14006. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14007. (image-file-name-regexp)
  14008. (let ((image-file-name-extensions
  14009. (or extensions
  14010. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14011. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14012. (concat "\\."
  14013. (regexp-opt (nconc (mapcar 'upcase
  14014. image-file-name-extensions)
  14015. image-file-name-extensions)
  14016. t)
  14017. "\\'"))))
  14018. (defun org-file-image-p (file &optional extensions)
  14019. "Return non-nil if FILE is an image."
  14020. (save-match-data
  14021. (string-match (org-image-file-name-regexp extensions) file)))
  14022. (defun org-get-cursor-date ()
  14023. "Return the date at cursor in as a time.
  14024. This works in the calendar and in the agenda, anywhere else it just
  14025. returns the current time."
  14026. (let (date day defd)
  14027. (cond
  14028. ((eq major-mode 'calendar-mode)
  14029. (setq date (calendar-cursor-to-date)
  14030. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14031. ((eq major-mode 'org-agenda-mode)
  14032. (setq day (get-text-property (point) 'day))
  14033. (if day
  14034. (setq date (calendar-gregorian-from-absolute day)
  14035. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14036. (nth 2 date))))))
  14037. (or defd (current-time))))
  14038. (defvar org-agenda-action-marker (make-marker)
  14039. "Marker pointing to the entry for the next agenda action.")
  14040. (defun org-mark-entry-for-agenda-action ()
  14041. "Mark the current entry as target of an agenda action.
  14042. Agenda actions are actions executed from the agenda with the key `k',
  14043. which make use of the date at the cursor."
  14044. (interactive)
  14045. (move-marker org-agenda-action-marker
  14046. (save-excursion (org-back-to-heading t) (point))
  14047. (current-buffer))
  14048. (message
  14049. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14050. ;;; Paragraph filling stuff.
  14051. ;; We want this to be just right, so use the full arsenal.
  14052. (defun org-indent-line-function ()
  14053. "Indent line like previous, but further if previous was headline or item."
  14054. (interactive)
  14055. (let* ((pos (point))
  14056. (itemp (org-at-item-p))
  14057. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14058. column bpos bcol tpos tcol bullet btype bullet-type)
  14059. ;; Find the previous relevant line
  14060. (beginning-of-line 1)
  14061. (cond
  14062. ((looking-at "#") (setq column 0))
  14063. ((looking-at "\\*+ ") (setq column 0))
  14064. ((and (looking-at "[ \t]*:END:")
  14065. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14066. (save-excursion
  14067. (goto-char (1- (match-beginning 1)))
  14068. (setq column (current-column))))
  14069. (t
  14070. (beginning-of-line 0)
  14071. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14072. (not (looking-at "[ \t]*:END:"))
  14073. (not (looking-at org-drawer-regexp)))
  14074. (beginning-of-line 0))
  14075. (cond
  14076. ((looking-at "\\*+[ \t]+")
  14077. (if (not org-adapt-indentation)
  14078. (setq column 0)
  14079. (goto-char (match-end 0))
  14080. (setq column (current-column))))
  14081. ((looking-at org-drawer-regexp)
  14082. (goto-char (1- (match-beginning 1)))
  14083. (setq column (current-column)))
  14084. ((looking-at "\\([ \t]*\\):END:")
  14085. (goto-char (match-end 1))
  14086. (setq column (current-column)))
  14087. ((org-in-item-p)
  14088. (org-beginning-of-item)
  14089. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14090. (setq bpos (match-beginning 1) tpos (match-end 0)
  14091. bcol (progn (goto-char bpos) (current-column))
  14092. tcol (progn (goto-char tpos) (current-column))
  14093. bullet (match-string 1)
  14094. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14095. (if (> tcol (+ bcol org-description-max-indent))
  14096. (setq tcol (+ bcol 5)))
  14097. (if (not itemp)
  14098. (setq column tcol)
  14099. (goto-char pos)
  14100. (beginning-of-line 1)
  14101. (if (looking-at "\\S-")
  14102. (progn
  14103. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14104. (setq bullet (match-string 1)
  14105. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14106. (setq column (if (equal btype bullet-type) bcol tcol)))
  14107. (setq column (org-get-indentation)))))
  14108. (t (setq column (org-get-indentation))))))
  14109. (goto-char pos)
  14110. (if (<= (current-column) (current-indentation))
  14111. (org-indent-line-to column)
  14112. (save-excursion (org-indent-line-to column)))
  14113. (setq column (current-column))
  14114. (beginning-of-line 1)
  14115. (if (looking-at
  14116. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14117. (replace-match (concat "\\1" (format org-property-format
  14118. (match-string 2) (match-string 3)))
  14119. t nil))
  14120. (org-move-to-column column)))
  14121. (defun org-set-autofill-regexps ()
  14122. (interactive)
  14123. ;; In the paragraph separator we include headlines, because filling
  14124. ;; text in a line directly attached to a headline would otherwise
  14125. ;; fill the headline as well.
  14126. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14127. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14128. ;; The paragraph starter includes hand-formatted lists.
  14129. (org-set-local 'paragraph-start
  14130. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14131. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14132. ;; But only if the user has not turned off tables or fixed-width regions
  14133. (org-set-local
  14134. 'auto-fill-inhibit-regexp
  14135. (concat "\\*+ \\|#\\+"
  14136. "\\|[ \t]*" org-keyword-time-regexp
  14137. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14138. (concat
  14139. "\\|[ \t]*["
  14140. (if org-enable-table-editor "|" "")
  14141. (if org-enable-fixed-width-editor ":" "")
  14142. "]"))))
  14143. ;; We use our own fill-paragraph function, to make sure that tables
  14144. ;; and fixed-width regions are not wrapped. That function will pass
  14145. ;; through to `fill-paragraph' when appropriate.
  14146. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14147. ; Adaptive filling: To get full control, first make sure that
  14148. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14149. (org-set-local 'adaptive-fill-regexp "\000")
  14150. (org-set-local 'adaptive-fill-function
  14151. 'org-adaptive-fill-function)
  14152. (org-set-local
  14153. 'align-mode-rules-list
  14154. '((org-in-buffer-settings
  14155. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14156. (modes . '(org-mode))))))
  14157. (defun org-fill-paragraph (&optional justify)
  14158. "Re-align a table, pass through to fill-paragraph if no table."
  14159. (let ((table-p (org-at-table-p))
  14160. (table.el-p (org-at-table.el-p)))
  14161. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14162. (save-excursion (goto-char (point-at-bol))
  14163. (looking-at outline-regexp)))
  14164. t) ; skip headlines
  14165. (table.el-p t) ; skip table.el tables
  14166. (table-p (org-table-align) t) ; align org-mode tables
  14167. (t nil)))) ; call paragraph-fill
  14168. ;; For reference, this is the default value of adaptive-fill-regexp
  14169. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14170. (defun org-adaptive-fill-function ()
  14171. "Return a fill prefix for org-mode files.
  14172. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14173. work correctly."
  14174. (cond ((looking-at "#[ \t]+")
  14175. (match-string 0))
  14176. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14177. (save-excursion
  14178. (if (> (match-end 1) (+ (match-beginning 1)
  14179. org-description-max-indent))
  14180. (goto-char (+ (match-beginning 1) 5))
  14181. (goto-char (match-end 0)))
  14182. (make-string (current-column) ?\ )))
  14183. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14184. (save-excursion
  14185. (goto-char (match-end 0))
  14186. (make-string (current-column) ?\ )))
  14187. (t nil)))
  14188. ;;; Other stuff.
  14189. (defun org-toggle-fixed-width-section (arg)
  14190. "Toggle the fixed-width export.
  14191. If there is no active region, the QUOTE keyword at the current headline is
  14192. inserted or removed. When present, it causes the text between this headline
  14193. and the next to be exported as fixed-width text, and unmodified.
  14194. If there is an active region, this command adds or removes a colon as the
  14195. first character of this line. If the first character of a line is a colon,
  14196. this line is also exported in fixed-width font."
  14197. (interactive "P")
  14198. (let* ((cc 0)
  14199. (regionp (org-region-active-p))
  14200. (beg (if regionp (region-beginning) (point)))
  14201. (end (if regionp (region-end)))
  14202. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14203. (case-fold-search nil)
  14204. (re "[ \t]*\\(: \\)")
  14205. off)
  14206. (if regionp
  14207. (save-excursion
  14208. (goto-char beg)
  14209. (setq cc (current-column))
  14210. (beginning-of-line 1)
  14211. (setq off (looking-at re))
  14212. (while (> nlines 0)
  14213. (setq nlines (1- nlines))
  14214. (beginning-of-line 1)
  14215. (cond
  14216. (arg
  14217. (org-move-to-column cc t)
  14218. (insert ": \n")
  14219. (forward-line -1))
  14220. ((and off (looking-at re))
  14221. (replace-match "" t t nil 1))
  14222. ((not off) (org-move-to-column cc t) (insert ": ")))
  14223. (forward-line 1)))
  14224. (save-excursion
  14225. (org-back-to-heading)
  14226. (if (looking-at (concat outline-regexp
  14227. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14228. (replace-match "" t t nil 1)
  14229. (if (looking-at outline-regexp)
  14230. (progn
  14231. (goto-char (match-end 0))
  14232. (insert org-quote-string " "))))))))
  14233. (defun org-reftex-citation ()
  14234. "Use reftex-citation to insert a citation into the buffer.
  14235. This looks for a line like
  14236. #+BIBLIOGRAPHY: foo plain option:-d
  14237. and derives from it that foo.bib is the bbliography file relevant
  14238. for this document. It then installs the necessary environment for RefTeX
  14239. to work in this buffer and calls `reftex-citation' to insert a citation
  14240. into the buffer.
  14241. Export of such citations to both LaTeX and HTML is handled by the contributed
  14242. package org-exp-bibtex by Taru Karttunen."
  14243. (interactive)
  14244. (let ((reftex-docstruct-symbol 'rds)
  14245. (reftex-cite-format "\\cite{%l}")
  14246. rds bib)
  14247. (save-excursion
  14248. (save-restriction
  14249. (widen)
  14250. (let ((case-fold-search t)
  14251. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14252. (if (not (save-excursion
  14253. (or (re-search-forward re nil t)
  14254. (re-search-backward re nil t))))
  14255. (error "No bibliography defined in file")
  14256. (setq bib (concat (match-string 1) ".bib")
  14257. rds (list (list 'bib bib)))))))
  14258. (call-interactively 'reftex-citation)))
  14259. ;;;; Functions extending outline functionality
  14260. (defun org-beginning-of-line (&optional arg)
  14261. "Go to the beginning of the current line. If that is invisible, continue
  14262. to a visible line beginning. This makes the function of C-a more intuitive.
  14263. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14264. first attempt, and only move to after the tags when the cursor is already
  14265. beyond the end of the headline."
  14266. (interactive "P")
  14267. (let ((pos (point))
  14268. (special (if (consp org-special-ctrl-a/e)
  14269. (car org-special-ctrl-a/e)
  14270. org-special-ctrl-a/e))
  14271. refpos)
  14272. (beginning-of-line 1)
  14273. (if (and arg (fboundp 'move-beginning-of-line))
  14274. (call-interactively 'move-beginning-of-line)
  14275. (if (bobp)
  14276. nil
  14277. (backward-char 1)
  14278. (if (org-invisible-p)
  14279. (while (and (not (bobp)) (org-invisible-p))
  14280. (backward-char 1)
  14281. (beginning-of-line 1))
  14282. (forward-char 1))))
  14283. (when special
  14284. (cond
  14285. ((and (looking-at org-complex-heading-regexp)
  14286. (= (char-after (match-end 1)) ?\ ))
  14287. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14288. (point-at-eol)))
  14289. (goto-char
  14290. (if (eq special t)
  14291. (cond ((> pos refpos) refpos)
  14292. ((= pos (point)) refpos)
  14293. (t (point)))
  14294. (cond ((> pos (point)) (point))
  14295. ((not (eq last-command this-command)) (point))
  14296. (t refpos)))))
  14297. ((org-at-item-p)
  14298. (goto-char
  14299. (if (eq special t)
  14300. (cond ((> pos (match-end 4)) (match-end 4))
  14301. ((= pos (point)) (match-end 4))
  14302. (t (point)))
  14303. (cond ((> pos (point)) (point))
  14304. ((not (eq last-command this-command)) (point))
  14305. (t (match-end 4))))))))
  14306. (org-no-warnings
  14307. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14308. (defun org-end-of-line (&optional arg)
  14309. "Go to the end of the line.
  14310. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14311. first attempt, and only move to after the tags when the cursor is already
  14312. beyond the end of the headline."
  14313. (interactive "P")
  14314. (let ((special (if (consp org-special-ctrl-a/e)
  14315. (cdr org-special-ctrl-a/e)
  14316. org-special-ctrl-a/e)))
  14317. (if (or (not special)
  14318. (not (org-on-heading-p))
  14319. arg)
  14320. (call-interactively (if (fboundp 'move-end-of-line)
  14321. 'move-end-of-line
  14322. 'end-of-line))
  14323. (let ((pos (point)))
  14324. (beginning-of-line 1)
  14325. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14326. (if (eq special t)
  14327. (if (or (< pos (match-beginning 1))
  14328. (= pos (match-end 0)))
  14329. (goto-char (match-beginning 1))
  14330. (goto-char (match-end 0)))
  14331. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14332. (goto-char (match-end 0))
  14333. (goto-char (match-beginning 1))))
  14334. (call-interactively (if (fboundp 'move-end-of-line)
  14335. 'move-end-of-line
  14336. 'end-of-line)))))
  14337. (org-no-warnings
  14338. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14339. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14340. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14341. (defun org-backward-sentence (&optional arg)
  14342. "Go to beginning of sentence, or beginning of table field.
  14343. This will call `backward-sentence' or `org-table-beginning-of-field',
  14344. depending on context."
  14345. (interactive "P")
  14346. (cond
  14347. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14348. (t (call-interactively 'backward-sentence))))
  14349. (defun org-forward-sentence (&optional arg)
  14350. "Go to end of sentence, or end of table field.
  14351. This will call `forward-sentence' or `org-table-end-of-field',
  14352. depending on context."
  14353. (interactive "P")
  14354. (cond
  14355. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14356. (t (call-interactively 'forward-sentence))))
  14357. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14358. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14359. (defun org-kill-line (&optional arg)
  14360. "Kill line, to tags or end of line."
  14361. (interactive "P")
  14362. (cond
  14363. ((or (not org-special-ctrl-k)
  14364. (bolp)
  14365. (not (org-on-heading-p)))
  14366. (call-interactively 'kill-line))
  14367. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14368. (kill-region (point) (match-beginning 1))
  14369. (org-set-tags nil t))
  14370. (t (kill-region (point) (point-at-eol)))))
  14371. (define-key org-mode-map "\C-k" 'org-kill-line)
  14372. (defun org-yank (&optional arg)
  14373. "Yank. If the kill is a subtree, treat it specially.
  14374. This command will look at the current kill and check if is a single
  14375. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14376. cursor is at the beginning of a line or after the stars of a currently
  14377. empty headline, then the yank is handled specially. How exactly depends
  14378. on the value of the following variables, both set by default.
  14379. org-yank-folded-subtrees
  14380. When set, the subtree(s) will be folded after insertion, but only
  14381. if doing so would now swallow text after the yanked text.
  14382. org-yank-adjusted-subtrees
  14383. When set, the subtree will be promoted or demoted in order to
  14384. fit into the local outline tree structure, which means that the level
  14385. will be adjusted so that it becomes the smaller one of the two
  14386. *visible* surrounding headings.
  14387. Any prefix to this command will cause `yank' to be called directly with
  14388. no special treatment. In particular, a simple `C-u' prefix will just
  14389. plainly yank the text as it is.
  14390. \[1] Basically, the test checks if the first non-white line is a heading
  14391. and if there are no other headings with fewer stars."
  14392. (interactive "P")
  14393. (setq this-command 'yank)
  14394. (if arg
  14395. (call-interactively 'yank)
  14396. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14397. (and (org-kill-is-subtree-p)
  14398. (or (bolp)
  14399. (and (looking-at "[ \t]*$")
  14400. (string-match
  14401. "\\`\\*+\\'"
  14402. (buffer-substring (point-at-bol) (point)))))))
  14403. swallowp)
  14404. (cond
  14405. ((and subtreep org-yank-folded-subtrees)
  14406. (let ((beg (point))
  14407. end)
  14408. (if (and subtreep org-yank-adjusted-subtrees)
  14409. (org-paste-subtree nil nil 'for-yank)
  14410. (call-interactively 'yank))
  14411. (setq end (point))
  14412. (goto-char beg)
  14413. (when (and (bolp) subtreep
  14414. (not (setq swallowp
  14415. (org-yank-folding-would-swallow-text beg end))))
  14416. (or (looking-at outline-regexp)
  14417. (re-search-forward (concat "^" outline-regexp) end t))
  14418. (while (and (< (point) end) (looking-at outline-regexp))
  14419. (hide-subtree)
  14420. (org-cycle-show-empty-lines 'folded)
  14421. (condition-case nil
  14422. (outline-forward-same-level 1)
  14423. (error (goto-char end)))))
  14424. (when swallowp
  14425. (message
  14426. "Yanked text not folded because that would swallow text"))
  14427. (goto-char end)
  14428. (skip-chars-forward " \t\n\r")
  14429. (beginning-of-line 1)
  14430. (push-mark beg 'nomsg)))
  14431. ((and subtreep org-yank-adjusted-subtrees)
  14432. (let ((beg (point-at-bol)))
  14433. (org-paste-subtree nil nil 'for-yank)
  14434. (push-mark beg 'nomsg)))
  14435. (t
  14436. (call-interactively 'yank))))))
  14437. (defun org-yank-folding-would-swallow-text (beg end)
  14438. "Would hide-subtree at BEG swallow any text after END?"
  14439. (let (level)
  14440. (save-excursion
  14441. (goto-char beg)
  14442. (when (or (looking-at outline-regexp)
  14443. (re-search-forward (concat "^" outline-regexp) end t))
  14444. (setq level (org-outline-level)))
  14445. (goto-char end)
  14446. (skip-chars-forward " \t\r\n\v\f")
  14447. (if (or (eobp)
  14448. (and (bolp) (looking-at org-outline-regexp)
  14449. (<= (org-outline-level) level)))
  14450. nil ; Nothing would be swallowed
  14451. t)))) ; something would swallow
  14452. (define-key org-mode-map "\C-y" 'org-yank)
  14453. (defun org-invisible-p ()
  14454. "Check if point is at a character currently not visible."
  14455. ;; Early versions of noutline don't have `outline-invisible-p'.
  14456. (if (fboundp 'outline-invisible-p)
  14457. (outline-invisible-p)
  14458. (get-char-property (point) 'invisible)))
  14459. (defun org-invisible-p2 ()
  14460. "Check if point is at a character currently not visible."
  14461. (save-excursion
  14462. (if (and (eolp) (not (bobp))) (backward-char 1))
  14463. ;; Early versions of noutline don't have `outline-invisible-p'.
  14464. (if (fboundp 'outline-invisible-p)
  14465. (outline-invisible-p)
  14466. (get-char-property (point) 'invisible))))
  14467. (defun org-back-to-heading (&optional invisible-ok)
  14468. "Call `outline-back-to-heading', but provide a better error message."
  14469. (condition-case nil
  14470. (outline-back-to-heading invisible-ok)
  14471. (error (error "Before first headline at position %d in buffer %s"
  14472. (point) (current-buffer)))))
  14473. (defun org-before-first-heading-p ()
  14474. "Before first heading?"
  14475. (save-excursion
  14476. (null (re-search-backward "^\\*+ " nil t))))
  14477. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14478. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14479. (defun org-at-heading-or-item-p ()
  14480. (or (org-on-heading-p) (org-at-item-p)))
  14481. (defun org-on-target-p ()
  14482. (or (org-in-regexp org-radio-target-regexp)
  14483. (org-in-regexp org-target-regexp)))
  14484. (defun org-up-heading-all (arg)
  14485. "Move to the heading line of which the present line is a subheading.
  14486. This function considers both visible and invisible heading lines.
  14487. With argument, move up ARG levels."
  14488. (if (fboundp 'outline-up-heading-all)
  14489. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14490. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14491. (defun org-up-heading-safe ()
  14492. "Move to the heading line of which the present line is a subheading.
  14493. This version will not throw an error. It will return the level of the
  14494. headline found, or nil if no higher level is found.
  14495. Also, this function will be a lot faster than `outline-up-heading',
  14496. because it relies on stars being the outline starters. This can really
  14497. make a significant difference in outlines with very many siblings."
  14498. (let (start-level re)
  14499. (org-back-to-heading t)
  14500. (setq start-level (funcall outline-level))
  14501. (if (equal start-level 1)
  14502. nil
  14503. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14504. (if (re-search-backward re nil t)
  14505. (funcall outline-level)))))
  14506. (defun org-first-sibling-p ()
  14507. "Is this heading the first child of its parents?"
  14508. (interactive)
  14509. (let ((re (concat "^" outline-regexp))
  14510. level l)
  14511. (unless (org-at-heading-p t)
  14512. (error "Not at a heading"))
  14513. (setq level (funcall outline-level))
  14514. (save-excursion
  14515. (if (not (re-search-backward re nil t))
  14516. t
  14517. (setq l (funcall outline-level))
  14518. (< l level)))))
  14519. (defun org-goto-sibling (&optional previous)
  14520. "Goto the next sibling, even if it is invisible.
  14521. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14522. when a sibling was found. When none is found, return nil and don't
  14523. move point."
  14524. (let ((fun (if previous 're-search-backward 're-search-forward))
  14525. (pos (point))
  14526. (re (concat "^" outline-regexp))
  14527. level l)
  14528. (when (condition-case nil (org-back-to-heading t) (error nil))
  14529. (setq level (funcall outline-level))
  14530. (catch 'exit
  14531. (or previous (forward-char 1))
  14532. (while (funcall fun re nil t)
  14533. (setq l (funcall outline-level))
  14534. (when (< l level) (goto-char pos) (throw 'exit nil))
  14535. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14536. (goto-char pos)
  14537. nil))))
  14538. (defun org-show-siblings ()
  14539. "Show all siblings of the current headline."
  14540. (save-excursion
  14541. (while (org-goto-sibling) (org-flag-heading nil)))
  14542. (save-excursion
  14543. (while (org-goto-sibling 'previous)
  14544. (org-flag-heading nil))))
  14545. (defun org-show-hidden-entry ()
  14546. "Show an entry where even the heading is hidden."
  14547. (save-excursion
  14548. (org-show-entry)))
  14549. (defun org-flag-heading (flag &optional entry)
  14550. "Flag the current heading. FLAG non-nil means make invisible.
  14551. When ENTRY is non-nil, show the entire entry."
  14552. (save-excursion
  14553. (org-back-to-heading t)
  14554. ;; Check if we should show the entire entry
  14555. (if entry
  14556. (progn
  14557. (org-show-entry)
  14558. (save-excursion
  14559. (and (outline-next-heading)
  14560. (org-flag-heading nil))))
  14561. (outline-flag-region (max (point-min) (1- (point)))
  14562. (save-excursion (outline-end-of-heading) (point))
  14563. flag))))
  14564. (defun org-forward-same-level (arg)
  14565. "Move forward to the ARG'th subheading at same level as this one.
  14566. Stop at the first and last subheadings of a superior heading.
  14567. This is like outline-forward-same-level, but invisible headings are ok."
  14568. (interactive "p")
  14569. (org-back-to-heading t)
  14570. (while (> arg 0)
  14571. (let ((point-to-move-to (save-excursion
  14572. (org-get-next-sibling))))
  14573. (if point-to-move-to
  14574. (progn
  14575. (goto-char point-to-move-to)
  14576. (setq arg (1- arg)))
  14577. (progn
  14578. (setq arg 0)
  14579. (error "No following same-level heading"))))))
  14580. (defun org-get-next-sibling ()
  14581. "Move to next heading of the same level, and return point.
  14582. If there is no such heading, return nil.
  14583. This is like outline-next-sibling, but invisible headings are ok."
  14584. (let ((level (funcall outline-level)))
  14585. (outline-next-heading)
  14586. (while (and (not (eobp)) (> (funcall outline-level) level))
  14587. (outline-next-heading))
  14588. (if (or (eobp) (< (funcall outline-level) level))
  14589. nil
  14590. (point))))
  14591. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14592. ;; This is an exact copy of the original function, but it uses
  14593. ;; `org-back-to-heading', to make it work also in invisible
  14594. ;; trees. And is uses an invisible-OK argument.
  14595. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14596. (org-back-to-heading invisible-OK)
  14597. (let ((first t)
  14598. (level (funcall outline-level)))
  14599. (while (and (not (eobp))
  14600. (or first (> (funcall outline-level) level)))
  14601. (setq first nil)
  14602. (outline-next-heading))
  14603. (unless to-heading
  14604. (if (memq (preceding-char) '(?\n ?\^M))
  14605. (progn
  14606. ;; Go to end of line before heading
  14607. (forward-char -1)
  14608. (if (memq (preceding-char) '(?\n ?\^M))
  14609. ;; leave blank line before heading
  14610. (forward-char -1))))))
  14611. (point))
  14612. (defun org-show-subtree ()
  14613. "Show everything after this heading at deeper levels."
  14614. (outline-flag-region
  14615. (point)
  14616. (save-excursion
  14617. (outline-end-of-subtree) (outline-next-heading) (point))
  14618. nil))
  14619. (defun org-show-entry ()
  14620. "Show the body directly following this heading.
  14621. Show the heading too, if it is currently invisible."
  14622. (interactive)
  14623. (save-excursion
  14624. (condition-case nil
  14625. (progn
  14626. (org-back-to-heading t)
  14627. (outline-flag-region
  14628. (max (point-min) (1- (point)))
  14629. (save-excursion
  14630. (if (re-search-forward
  14631. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14632. (match-beginning 1)
  14633. (point-max)))
  14634. nil))
  14635. (error nil))))
  14636. (defun org-make-options-regexp (kwds &optional extra)
  14637. "Make a regular expression for keyword lines."
  14638. (concat
  14639. "^"
  14640. "#?[ \t]*\\+\\("
  14641. (mapconcat 'regexp-quote kwds "\\|")
  14642. (if extra (concat "\\|" extra))
  14643. "\\):[ \t]*"
  14644. "\\(.+\\)"))
  14645. ;; Make isearch reveal the necessary context
  14646. (defun org-isearch-end ()
  14647. "Reveal context after isearch exits."
  14648. (when isearch-success ; only if search was successful
  14649. (if (featurep 'xemacs)
  14650. ;; Under XEmacs, the hook is run in the correct place,
  14651. ;; we directly show the context.
  14652. (org-show-context 'isearch)
  14653. ;; In Emacs the hook runs *before* restoring the overlays.
  14654. ;; So we have to use a one-time post-command-hook to do this.
  14655. ;; (Emacs 22 has a special variable, see function `org-mode')
  14656. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14657. isearch-mode-end-hook-quit)
  14658. ;; Only when the isearch was not quitted.
  14659. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14660. 'append 'local)))))
  14661. (defun org-isearch-post-command ()
  14662. "Remove self from hook, and show context."
  14663. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14664. (org-show-context 'isearch))
  14665. ;;;; Integration with and fixes for other packages
  14666. ;;; Imenu support
  14667. (defvar org-imenu-markers nil
  14668. "All markers currently used by Imenu.")
  14669. (make-variable-buffer-local 'org-imenu-markers)
  14670. (defun org-imenu-new-marker (&optional pos)
  14671. "Return a new marker for use by Imenu, and remember the marker."
  14672. (let ((m (make-marker)))
  14673. (move-marker m (or pos (point)))
  14674. (push m org-imenu-markers)
  14675. m))
  14676. (defun org-imenu-get-tree ()
  14677. "Produce the index for Imenu."
  14678. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14679. (setq org-imenu-markers nil)
  14680. (let* ((n org-imenu-depth)
  14681. (re (concat "^" outline-regexp))
  14682. (subs (make-vector (1+ n) nil))
  14683. (last-level 0)
  14684. m level head)
  14685. (save-excursion
  14686. (save-restriction
  14687. (widen)
  14688. (goto-char (point-max))
  14689. (while (re-search-backward re nil t)
  14690. (setq level (org-reduced-level (funcall outline-level)))
  14691. (when (<= level n)
  14692. (looking-at org-complex-heading-regexp)
  14693. (setq head (org-link-display-format
  14694. (org-match-string-no-properties 4))
  14695. m (org-imenu-new-marker))
  14696. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14697. (if (>= level last-level)
  14698. (push (cons head m) (aref subs level))
  14699. (push (cons head (aref subs (1+ level))) (aref subs level))
  14700. (loop for i from (1+ level) to n do (aset subs i nil)))
  14701. (setq last-level level)))))
  14702. (aref subs 1)))
  14703. (eval-after-load "imenu"
  14704. '(progn
  14705. (add-hook 'imenu-after-jump-hook
  14706. (lambda ()
  14707. (if (eq major-mode 'org-mode)
  14708. (org-show-context 'org-goto))))))
  14709. (defun org-link-display-format (link)
  14710. "Replace a link with either the description, or the link target
  14711. if no description is present"
  14712. (save-match-data
  14713. (if (string-match org-bracket-link-analytic-regexp link)
  14714. (replace-match (or (match-string 5 link)
  14715. (concat (match-string 1 link)
  14716. (match-string 3 link)))
  14717. nil nil link)
  14718. link)))
  14719. ;; Speedbar support
  14720. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14721. "Overlay marking the agenda restriction line in speedbar.")
  14722. (org-overlay-put org-speedbar-restriction-lock-overlay
  14723. 'face 'org-agenda-restriction-lock)
  14724. (org-overlay-put org-speedbar-restriction-lock-overlay
  14725. 'help-echo "Agendas are currently limited to this item.")
  14726. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14727. (defun org-speedbar-set-agenda-restriction ()
  14728. "Restrict future agenda commands to the location at point in speedbar.
  14729. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14730. (interactive)
  14731. (require 'org-agenda)
  14732. (let (p m tp np dir txt)
  14733. (cond
  14734. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14735. 'org-imenu t))
  14736. (setq m (get-text-property p 'org-imenu-marker))
  14737. (save-excursion
  14738. (save-restriction
  14739. (set-buffer (marker-buffer m))
  14740. (goto-char m)
  14741. (org-agenda-set-restriction-lock 'subtree))))
  14742. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14743. 'speedbar-function 'speedbar-find-file))
  14744. (setq tp (previous-single-property-change
  14745. (1+ p) 'speedbar-function)
  14746. np (next-single-property-change
  14747. tp 'speedbar-function)
  14748. dir (speedbar-line-directory)
  14749. txt (buffer-substring-no-properties (or tp (point-min))
  14750. (or np (point-max))))
  14751. (save-excursion
  14752. (save-restriction
  14753. (set-buffer (find-file-noselect
  14754. (let ((default-directory dir))
  14755. (expand-file-name txt))))
  14756. (unless (org-mode-p)
  14757. (error "Cannot restrict to non-Org-mode file"))
  14758. (org-agenda-set-restriction-lock 'file))))
  14759. (t (error "Don't know how to restrict Org-mode's agenda")))
  14760. (org-move-overlay org-speedbar-restriction-lock-overlay
  14761. (point-at-bol) (point-at-eol))
  14762. (setq current-prefix-arg nil)
  14763. (org-agenda-maybe-redo)))
  14764. (eval-after-load "speedbar"
  14765. '(progn
  14766. (speedbar-add-supported-extension ".org")
  14767. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14768. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14769. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14770. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14771. (add-hook 'speedbar-visiting-tag-hook
  14772. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14773. ;;; Fixes and Hacks for problems with other packages
  14774. ;; Make flyspell not check words in links, to not mess up our keymap
  14775. (defun org-mode-flyspell-verify ()
  14776. "Don't let flyspell put overlays at active buttons."
  14777. (not (get-text-property (point) 'keymap)))
  14778. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14779. (eval-after-load "bookmark"
  14780. '(if (boundp 'bookmark-after-jump-hook)
  14781. ;; We can use the hook
  14782. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14783. ;; Hook not available, use advice
  14784. (defadvice bookmark-jump (after org-make-visible activate)
  14785. "Make the position visible."
  14786. (org-bookmark-jump-unhide))))
  14787. ;; Make sure saveplace show the location if it was hidden
  14788. (eval-after-load "saveplace"
  14789. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14790. "Make the position visible."
  14791. (org-bookmark-jump-unhide)))
  14792. (defun org-bookmark-jump-unhide ()
  14793. "Unhide the current position, to show the bookmark location."
  14794. (and (org-mode-p)
  14795. (or (org-invisible-p)
  14796. (save-excursion (goto-char (max (point-min) (1- (point))))
  14797. (org-invisible-p)))
  14798. (org-show-context 'bookmark-jump)))
  14799. ;; Make session.el ignore our circular variable
  14800. (eval-after-load "session"
  14801. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14802. ;;;; Experimental code
  14803. (defun org-closed-in-range ()
  14804. "Sparse tree of items closed in a certain time range.
  14805. Still experimental, may disappear in the future."
  14806. (interactive)
  14807. ;; Get the time interval from the user.
  14808. (let* ((time1 (time-to-seconds
  14809. (org-read-date nil 'to-time nil "Starting date: ")))
  14810. (time2 (time-to-seconds
  14811. (org-read-date nil 'to-time nil "End date:")))
  14812. ;; callback function
  14813. (callback (lambda ()
  14814. (let ((time
  14815. (time-to-seconds
  14816. (apply 'encode-time
  14817. (org-parse-time-string
  14818. (match-string 1))))))
  14819. ;; check if time in interval
  14820. (and (>= time time1) (<= time time2))))))
  14821. ;; make tree, check each match with the callback
  14822. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14823. ;;;; Finish up
  14824. (provide 'org)
  14825. (run-hooks 'org-load-hook)
  14826. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14827. ;;; org.el ends here