org.el 607 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314
  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.26a
  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.26a"
  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/paste 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 (OBSOLETE, use org-protocol)" 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 (OBSOLETE, use org-protocol)" 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 (PARTIALLY OBSOLETE, see built-in dependency support))" 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 (PARTIALLY OBSOLETE, see secondary filtering)" 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-and-no-custom-id
  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 storing link interactively"
  974. create-if-interactive)
  975. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  976. create-if-interactive-and-no-custom-id)
  977. (const :tag "Only use existing" use-existing)
  978. (const :tag "Do not use ID to create link" nil)))
  979. (defcustom org-context-in-file-links t
  980. "Non-nil means, file links from `org-store-link' contain context.
  981. A search string will be added to the file name with :: as separator and
  982. used to find the context when the link is activated by the command
  983. `org-open-at-point'.
  984. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  985. negates this setting for the duration of the command."
  986. :group 'org-link-store
  987. :type 'boolean)
  988. (defcustom org-keep-stored-link-after-insertion nil
  989. "Non-nil means, keep link in list for entire session.
  990. The command `org-store-link' adds a link pointing to the current
  991. location to an internal list. These links accumulate during a session.
  992. The command `org-insert-link' can be used to insert links into any
  993. Org-mode file (offering completion for all stored links). When this
  994. option is nil, every link which has been inserted once using \\[org-insert-link]
  995. will be removed from the list, to make completing the unused links
  996. more efficient."
  997. :group 'org-link-store
  998. :type 'boolean)
  999. (defgroup org-link-follow nil
  1000. "Options concerning following links in Org-mode."
  1001. :tag "Org Follow Link"
  1002. :group 'org-link)
  1003. (defcustom org-link-translation-function nil
  1004. "Function to translate links with different syntax to Org syntax.
  1005. This can be used to translate links created for example by the Planner
  1006. or emacs-wiki packages to Org syntax.
  1007. The function must accept two parameters, a TYPE containing the link
  1008. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1009. which is everything after the link protocol. It should return a cons
  1010. with possibly modified values of type and path.
  1011. Org contains a function for this, so if you set this variable to
  1012. `org-translate-link-from-planner', you should be able follow many
  1013. links created by planner."
  1014. :group 'org-link-follow
  1015. :type 'function)
  1016. (defcustom org-follow-link-hook nil
  1017. "Hook that is run after a link has been followed."
  1018. :group 'org-link-follow
  1019. :type 'hook)
  1020. (defcustom org-tab-follows-link nil
  1021. "Non-nil means, on links TAB will follow the link.
  1022. Needs to be set before org.el is loaded.
  1023. This really should not be used, it does not make sense, and the
  1024. implementation is bad."
  1025. :group 'org-link-follow
  1026. :type 'boolean)
  1027. (defcustom org-return-follows-link nil
  1028. "Non-nil means, on links RET will follow the link.
  1029. Needs to be set before org.el is loaded."
  1030. :group 'org-link-follow
  1031. :type 'boolean)
  1032. (defcustom org-mouse-1-follows-link
  1033. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1034. "Non-nil means, mouse-1 on a link will follow the link.
  1035. A longer mouse click will still set point. Does not work on XEmacs.
  1036. Needs to be set before org.el is loaded."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-mark-ring-length 4
  1040. "Number of different positions to be recorded in the ring
  1041. Changing this requires a restart of Emacs to work correctly."
  1042. :group 'org-link-follow
  1043. :type 'integer)
  1044. (defcustom org-link-frame-setup
  1045. '((vm . vm-visit-folder-other-frame)
  1046. (gnus . gnus-other-frame)
  1047. (file . find-file-other-window))
  1048. "Setup the frame configuration for following links.
  1049. When following a link with Emacs, it may often be useful to display
  1050. this link in another window or frame. This variable can be used to
  1051. set this up for the different types of links.
  1052. For VM, use any of
  1053. `vm-visit-folder'
  1054. `vm-visit-folder-other-frame'
  1055. For Gnus, use any of
  1056. `gnus'
  1057. `gnus-other-frame'
  1058. `org-gnus-no-new-news'
  1059. For FILE, use any of
  1060. `find-file'
  1061. `find-file-other-window'
  1062. `find-file-other-frame'
  1063. For the calendar, use the variable `calendar-setup'.
  1064. For BBDB, it is currently only possible to display the matches in
  1065. another window."
  1066. :group 'org-link-follow
  1067. :type '(list
  1068. (cons (const vm)
  1069. (choice
  1070. (const vm-visit-folder)
  1071. (const vm-visit-folder-other-window)
  1072. (const vm-visit-folder-other-frame)))
  1073. (cons (const gnus)
  1074. (choice
  1075. (const gnus)
  1076. (const gnus-other-frame)
  1077. (const org-gnus-no-new-news)))
  1078. (cons (const file)
  1079. (choice
  1080. (const find-file)
  1081. (const find-file-other-window)
  1082. (const find-file-other-frame)))))
  1083. (defcustom org-display-internal-link-with-indirect-buffer nil
  1084. "Non-nil means, use indirect buffer to display infile links.
  1085. Activating internal links (from one location in a file to another location
  1086. in the same file) normally just jumps to the location. When the link is
  1087. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1088. another window. When this option is set, the other window actually displays
  1089. an indirect buffer clone of the current buffer, to avoid any visibility
  1090. changes to the current buffer."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-open-non-existing-files nil
  1094. "Non-nil means, `org-open-file' will open non-existing files.
  1095. When nil, an error will be generated."
  1096. :group 'org-link-follow
  1097. :type 'boolean)
  1098. (defcustom org-open-directory-means-index-dot-org nil
  1099. "Non-nil means, a link to a directory really means to index.org.
  1100. When nil, following a directory link will run dired or open a finder/explorer
  1101. window on that directory."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1105. "Function and arguments to call for following mailto links.
  1106. This is a list with the first element being a lisp function, and the
  1107. remaining elements being arguments to the function. In string arguments,
  1108. %a will be replaced by the address, and %s will be replaced by the subject
  1109. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1110. :group 'org-link-follow
  1111. :type '(choice
  1112. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1113. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1114. (const :tag "message-mail" (message-mail "%a" "%s"))
  1115. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1116. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1117. "Non-nil means, ask for confirmation before executing shell links.
  1118. Shell links can be dangerous: just think about a link
  1119. [[shell:rm -rf ~/*][Google Search]]
  1120. This link would show up in your Org-mode document as \"Google Search\",
  1121. but really it would remove your entire home directory.
  1122. Therefore we advise against setting this variable to nil.
  1123. Just change it to `y-or-n-p' if you want to confirm with a
  1124. single keystroke rather than having to type \"yes\"."
  1125. :group 'org-link-follow
  1126. :type '(choice
  1127. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1128. (const :tag "with y-or-n (faster)" y-or-n-p)
  1129. (const :tag "no confirmation (dangerous)" nil)))
  1130. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1131. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1132. Elisp links can be dangerous: just think about a link
  1133. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1134. This link would show up in your Org-mode document as \"Google Search\",
  1135. but really it would remove your entire home directory.
  1136. Therefore we advise against setting this variable to nil.
  1137. Just change it to `y-or-n-p' if you want to confirm with a
  1138. single keystroke rather than having to type \"yes\"."
  1139. :group 'org-link-follow
  1140. :type '(choice
  1141. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1142. (const :tag "with y-or-n (faster)" y-or-n-p)
  1143. (const :tag "no confirmation (dangerous)" nil)))
  1144. (defconst org-file-apps-defaults-gnu
  1145. '((remote . emacs)
  1146. (system . mailcap)
  1147. (t . mailcap))
  1148. "Default file applications on a UNIX or GNU/Linux system.
  1149. See `org-file-apps'.")
  1150. (defconst org-file-apps-defaults-macosx
  1151. '((remote . emacs)
  1152. (t . "open %s")
  1153. (system . "open %s")
  1154. ("ps.gz" . "gv %s")
  1155. ("eps.gz" . "gv %s")
  1156. ("dvi" . "xdvi %s")
  1157. ("fig" . "xfig %s"))
  1158. "Default file applications on a MacOS X system.
  1159. The system \"open\" is known as a default, but we use X11 applications
  1160. for some files for which the OS does not have a good default.
  1161. See `org-file-apps'.")
  1162. (defconst org-file-apps-defaults-windowsnt
  1163. (list
  1164. '(remote . emacs)
  1165. (cons t
  1166. (list (if (featurep 'xemacs)
  1167. 'mswindows-shell-execute
  1168. 'w32-shell-execute)
  1169. "open" 'file))
  1170. (cons 'system
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. (auto-mode . emacs)
  1181. ("\\.x?html?\\'" . default)
  1182. ("\\.pdf\\'" . default)
  1183. )
  1184. "External applications for opening `file:path' items in a document.
  1185. Org-mode uses system defaults for different file types, but
  1186. you can use this variable to set the application for a given file
  1187. extension. The entries in this list are cons cells where the car identifies
  1188. files and the cdr the corresponding command. Possible values for the
  1189. file identifier are
  1190. \"regex\" Regular expression matched against the file name. For backward
  1191. compatibility, this can also be a string with only alphanumeric
  1192. characters, which is then interpreted as an extension.
  1193. `directory' Matches a directory
  1194. `remote' Matches a remote file, accessible through tramp or efs.
  1195. Remote files most likely should be visited through Emacs
  1196. because external applications cannot handle such paths.
  1197. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1198. so all files Emacs knows how to handle. Using this with
  1199. command `emacs' will open most files in Emacs. Beware that this
  1200. will also open html files inside Emacs, unless you add
  1201. (\"html\" . default) to the list as well.
  1202. t Default for files not matched by any of the other options.
  1203. `system' The system command to open files, like `open' on Windows
  1204. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1205. that will be selected if you call `C-c C-o' with a double
  1206. `C-u C-u' prefix.
  1207. Possible values for the command are:
  1208. `emacs' The file will be visited by the current Emacs process.
  1209. `default' Use the default application for this file type, which is the
  1210. association for t in the list, most likely in the system-specific
  1211. part.
  1212. This can be used to overrule an unwanted setting in the
  1213. system-specific variable.
  1214. `system' Use the system command for opening files, like \"open\".
  1215. This command is specified by the entry whose car is `system'.
  1216. Most likely, the system-specific version of this variable
  1217. does define this command, but you can overrule/replace it
  1218. here.
  1219. string A command to be executed by a shell; %s will be replaced
  1220. by the path to the file.
  1221. sexp A Lisp form which will be evaluated. The file path will
  1222. be available in the Lisp variable `file'.
  1223. For more examples, see the system specific constants
  1224. `org-file-apps-defaults-macosx'
  1225. `org-file-apps-defaults-windowsnt'
  1226. `org-file-apps-defaults-gnu'."
  1227. :group 'org-link-follow
  1228. :type '(repeat
  1229. (cons (choice :value ""
  1230. (string :tag "Extension")
  1231. (const :tag "System command to open files" system)
  1232. (const :tag "Default for unrecognized files" t)
  1233. (const :tag "Remote file" remote)
  1234. (const :tag "Links to a directory" directory)
  1235. (const :tag "Any files that have Emacs modes"
  1236. auto-mode))
  1237. (choice :value ""
  1238. (const :tag "Visit with Emacs" emacs)
  1239. (const :tag "Use default" default)
  1240. (const :tag "Use the system command" system)
  1241. (string :tag "Command")
  1242. (sexp :tag "Lisp form")))))
  1243. (defgroup org-refile nil
  1244. "Options concerning refiling entries in Org-mode."
  1245. :tag "Org Refile"
  1246. :group 'org)
  1247. (defcustom org-directory "~/org"
  1248. "Directory with org files.
  1249. This is just a default location to look for Org files. There is no need
  1250. at all to put your files into this directory. It is only used in the
  1251. following situations:
  1252. 1. When a remember template specifies a target file that is not an
  1253. absolute path. The path will then be interpreted relative to
  1254. `org-directory'
  1255. 2. When a remember note is filed away in an interactive way (when exiting the
  1256. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1257. with `org-directory' as the default path."
  1258. :group 'org-refile
  1259. :group 'org-remember
  1260. :type 'directory)
  1261. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1262. "Default target for storing notes.
  1263. Used by the hooks for remember.el. This can be a string, or nil to mean
  1264. the value of `remember-data-file'.
  1265. You can set this on a per-template basis with the variable
  1266. `org-remember-templates'."
  1267. :group 'org-refile
  1268. :group 'org-remember
  1269. :type '(choice
  1270. (const :tag "Default from remember-data-file" nil)
  1271. file))
  1272. (defcustom org-goto-interface 'outline
  1273. "The default interface to be used for `org-goto'.
  1274. Allowed values are:
  1275. outline The interface shows an outline of the relevant file
  1276. and the correct heading is found by moving through
  1277. the outline or by searching with incremental search.
  1278. outline-path-completion Headlines in the current buffer are offered via
  1279. completion. This is the interface also used by
  1280. the refile command."
  1281. :group 'org-refile
  1282. :type '(choice
  1283. (const :tag "Outline" outline)
  1284. (const :tag "Outline-path-completion" outline-path-completion)))
  1285. (defcustom org-goto-max-level 5
  1286. "Maximum level to be considered when running org-goto with refile interface."
  1287. :group 'org-refile
  1288. :type 'integer)
  1289. (defcustom org-reverse-note-order nil
  1290. "Non-nil means, store new notes at the beginning of a file or entry.
  1291. When nil, new notes will be filed to the end of a file or entry.
  1292. This can also be a list with cons cells of regular expressions that
  1293. are matched against file names, and values."
  1294. :group 'org-remember
  1295. :group 'org-refile
  1296. :type '(choice
  1297. (const :tag "Reverse always" t)
  1298. (const :tag "Reverse never" nil)
  1299. (repeat :tag "By file name regexp"
  1300. (cons regexp boolean))))
  1301. (defcustom org-refile-targets nil
  1302. "Targets for refiling entries with \\[org-refile].
  1303. This is list of cons cells. Each cell contains:
  1304. - a specification of the files to be considered, either a list of files,
  1305. or a symbol whose function or variable value will be used to retrieve
  1306. a file name or a list of file names. If you use `org-agenda-files' for
  1307. that, all agenda files will be scanned for targets. Nil means, consider
  1308. headings in the current buffer.
  1309. - A specification of how to find candidate refile targets. This may be
  1310. any of:
  1311. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1312. This tag has to be present in all target headlines, inheritance will
  1313. not be considered.
  1314. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1315. todo keyword.
  1316. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1317. headlines that are refiling targets.
  1318. - a cons cell (:level . N). Any headline of level N is considered a target.
  1319. Note that, when `org-odd-levels-only' is set, level corresponds to
  1320. order in hierarchy, not to the number of stars.
  1321. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1322. Note that, when `org-odd-levels-only' is set, level corresponds to
  1323. order in hierarchy, not to the number of stars.
  1324. You can set the variable `org-refile-target-verify-function' to a function
  1325. to verify each headline found by the simple critery above.
  1326. When this variable is nil, all top-level headlines in the current buffer
  1327. are used, equivalent to the value `((nil . (:level . 1))'."
  1328. :group 'org-refile
  1329. :type '(repeat
  1330. (cons
  1331. (choice :value org-agenda-files
  1332. (const :tag "All agenda files" org-agenda-files)
  1333. (const :tag "Current buffer" nil)
  1334. (function) (variable) (file))
  1335. (choice :tag "Identify target headline by"
  1336. (cons :tag "Specific tag" (const :value :tag) (string))
  1337. (cons :tag "TODO keyword" (const :value :todo) (string))
  1338. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1339. (cons :tag "Level number" (const :value :level) (integer))
  1340. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1341. (defcustom org-refile-target-verify-function nil
  1342. "Function to verify if the headline at point should be a refile target.
  1343. The function will be called without arguments, with point at the
  1344. beginning of the headline. It should return t and leave point
  1345. where it is if the headline is a valid target for refiling.
  1346. If the target should not be selected, the function must return nil.
  1347. In addition to this, it may move point to a place from where the search
  1348. should be continued. For example, the function may decide that the entire
  1349. subtree of the current entry should be excluded and move point to the end
  1350. of the subtree."
  1351. :group 'org-refile
  1352. :type 'function)
  1353. (defcustom org-refile-use-outline-path nil
  1354. "Non-nil means, provide refile targets as paths.
  1355. So a level 3 headline will be available as level1/level2/level3.
  1356. When the value is `file', also include the file name (without directory)
  1357. into the path. In this case, you can also stop the completion after
  1358. the file name, to get entries inserted as top level in the file.
  1359. When `full-file-path', include the full file path."
  1360. :group 'org-refile
  1361. :type '(choice
  1362. (const :tag "Not" nil)
  1363. (const :tag "Yes" t)
  1364. (const :tag "Start with file name" file)
  1365. (const :tag "Start with full file path" full-file-path)))
  1366. (defcustom org-outline-path-complete-in-steps t
  1367. "Non-nil means, complete the outline path in hierarchical steps.
  1368. When Org-mode uses the refile interface to select an outline path
  1369. \(see variable `org-refile-use-outline-path'), the completion of
  1370. the path can be done is a single go, or if can be done in steps down
  1371. the headline hierarchy. Going in steps is probably the best if you
  1372. do not use a special completion package like `ido' or `icicles'.
  1373. However, when using these packages, going in one step can be very
  1374. fast, while still showing the whole path to the entry."
  1375. :group 'org-refile
  1376. :type 'boolean)
  1377. (defcustom org-refile-allow-creating-parent-nodes nil
  1378. "Non-nil means, allow to create new nodes as refile targets.
  1379. New nodes are then created by adding \"/new node name\" to the completion
  1380. of an existing node. When the value of this variable is `confirm',
  1381. new node creation must be confirmed by the user (recommended)
  1382. When nil, the completion must match an existing entry.
  1383. Note that, if the new heading is not seen by the criteria
  1384. listed in `org-refile-targets', multiple instances of the same
  1385. heading would be created by trying again to file under the new
  1386. heading."
  1387. :group 'org-refile
  1388. :type '(choice
  1389. (const :tag "Never" nil)
  1390. (const :tag "Always" t)
  1391. (const :tag "Prompt for confirmation" confirm)))
  1392. (defgroup org-todo nil
  1393. "Options concerning TODO items in Org-mode."
  1394. :tag "Org TODO"
  1395. :group 'org)
  1396. (defgroup org-progress nil
  1397. "Options concerning Progress logging in Org-mode."
  1398. :tag "Org Progress"
  1399. :group 'org-time)
  1400. (defvar org-todo-interpretation-widgets
  1401. '(
  1402. (:tag "Sequence (cycling hits every state)" sequence)
  1403. (:tag "Type (cycling directly to DONE)" type))
  1404. "The available interpretation symbols for customizing
  1405. `org-todo-keywords'.
  1406. Interested libraries should add to this list.")
  1407. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1408. "List of TODO entry keyword sequences and their interpretation.
  1409. \\<org-mode-map>This is a list of sequences.
  1410. Each sequence starts with a symbol, either `sequence' or `type',
  1411. indicating if the keywords should be interpreted as a sequence of
  1412. action steps, or as different types of TODO items. The first
  1413. keywords are states requiring action - these states will select a headline
  1414. for inclusion into the global TODO list Org-mode produces. If one of
  1415. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1416. signify that no further action is necessary. If \"|\" is not found,
  1417. the last keyword is treated as the only DONE state of the sequence.
  1418. The command \\[org-todo] cycles an entry through these states, and one
  1419. additional state where no keyword is present. For details about this
  1420. cycling, see the manual.
  1421. TODO keywords and interpretation can also be set on a per-file basis with
  1422. the special #+SEQ_TODO and #+TYP_TODO lines.
  1423. Each keyword can optionally specify a character for fast state selection
  1424. \(in combination with the variable `org-use-fast-todo-selection')
  1425. and specifiers for state change logging, using the same syntax
  1426. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1427. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1428. indicates to record a time stamp each time this state is selected.
  1429. Each keyword may also specify if a timestamp or a note should be
  1430. recorded when entering or leaving the state, by adding additional
  1431. characters in the parenthesis after the keyword. This looks like this:
  1432. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1433. record only the time of the state change. With X and Y being either
  1434. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1435. Y when leaving the state if and only if the *target* state does not
  1436. define X. You may omit any of the fast-selection key or X or /Y,
  1437. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1438. For backward compatibility, this variable may also be just a list
  1439. of keywords - in this case the interpretation (sequence or type) will be
  1440. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1441. :group 'org-todo
  1442. :group 'org-keywords
  1443. :type '(choice
  1444. (repeat :tag "Old syntax, just keywords"
  1445. (string :tag "Keyword"))
  1446. (repeat :tag "New syntax"
  1447. (cons
  1448. (choice
  1449. :tag "Interpretation"
  1450. ;;Quick and dirty way to see
  1451. ;;`org-todo-interpretations'. This takes the
  1452. ;;place of item arguments
  1453. :convert-widget
  1454. (lambda (widget)
  1455. (widget-put widget
  1456. :args (mapcar
  1457. #'(lambda (x)
  1458. (widget-convert
  1459. (cons 'const x)))
  1460. org-todo-interpretation-widgets))
  1461. widget))
  1462. (repeat
  1463. (string :tag "Keyword"))))))
  1464. (defvar org-todo-keywords-1 nil
  1465. "All TODO and DONE keywords active in a buffer.")
  1466. (make-variable-buffer-local 'org-todo-keywords-1)
  1467. (defvar org-todo-keywords-for-agenda nil)
  1468. (defvar org-done-keywords-for-agenda nil)
  1469. (defvar org-todo-keyword-alist-for-agenda nil)
  1470. (defvar org-tag-alist-for-agenda nil)
  1471. (defvar org-agenda-contributing-files nil)
  1472. (defvar org-not-done-keywords nil)
  1473. (make-variable-buffer-local 'org-not-done-keywords)
  1474. (defvar org-done-keywords nil)
  1475. (make-variable-buffer-local 'org-done-keywords)
  1476. (defvar org-todo-heads nil)
  1477. (make-variable-buffer-local 'org-todo-heads)
  1478. (defvar org-todo-sets nil)
  1479. (make-variable-buffer-local 'org-todo-sets)
  1480. (defvar org-todo-log-states nil)
  1481. (make-variable-buffer-local 'org-todo-log-states)
  1482. (defvar org-todo-kwd-alist nil)
  1483. (make-variable-buffer-local 'org-todo-kwd-alist)
  1484. (defvar org-todo-key-alist nil)
  1485. (make-variable-buffer-local 'org-todo-key-alist)
  1486. (defvar org-todo-key-trigger nil)
  1487. (make-variable-buffer-local 'org-todo-key-trigger)
  1488. (defcustom org-todo-interpretation 'sequence
  1489. "Controls how TODO keywords are interpreted.
  1490. This variable is in principle obsolete and is only used for
  1491. backward compatibility, if the interpretation of todo keywords is
  1492. not given already in `org-todo-keywords'. See that variable for
  1493. more information."
  1494. :group 'org-todo
  1495. :group 'org-keywords
  1496. :type '(choice (const sequence)
  1497. (const type)))
  1498. (defcustom org-use-fast-todo-selection t
  1499. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1500. This variable describes if and under what circumstances the cycling
  1501. mechanism for TODO keywords will be replaced by a single-key, direct
  1502. selection scheme.
  1503. When nil, fast selection is never used.
  1504. When the symbol `prefix', it will be used when `org-todo' is called with
  1505. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1506. in an agenda buffer.
  1507. When t, fast selection is used by default. In this case, the prefix
  1508. argument forces cycling instead.
  1509. In all cases, the special interface is only used if access keys have actually
  1510. been assigned by the user, i.e. if keywords in the configuration are followed
  1511. by a letter in parenthesis, like TODO(t)."
  1512. :group 'org-todo
  1513. :type '(choice
  1514. (const :tag "Never" nil)
  1515. (const :tag "By default" t)
  1516. (const :tag "Only with C-u C-c C-t" prefix)))
  1517. (defcustom org-provide-todo-statistics t
  1518. "Non-nil means, update todo statistics after insert and toggle.
  1519. When this is set, todo statistics is updated in the parent of the current
  1520. entry each time a todo state is changed."
  1521. :group 'org-todo
  1522. :type 'boolean)
  1523. (defcustom org-after-todo-state-change-hook nil
  1524. "Hook which is run after the state of a TODO item was changed.
  1525. The new state (a string with a TODO keyword, or nil) is available in the
  1526. Lisp variable `state'."
  1527. :group 'org-todo
  1528. :type 'hook)
  1529. (defvar org-blocker-hook nil
  1530. "Hook for functions that are allowed to block a state change.
  1531. Each function gets as its single argument a property list, see
  1532. `org-trigger-hook' for more information about this list.
  1533. If any of the functions in this hook returns nil, the state change
  1534. is blocked.")
  1535. (defvar org-trigger-hook nil
  1536. "Hook for functions that are triggered by a state change.
  1537. Each function gets as its single argument a property list with at least
  1538. the following elements:
  1539. (:type type-of-change :position pos-at-entry-start
  1540. :from old-state :to new-state)
  1541. Depending on the type, more properties may be present.
  1542. This mechanism is currently implemented for:
  1543. TODO state changes
  1544. ------------------
  1545. :type todo-state-change
  1546. :from previous state (keyword as a string), or nil, or a symbol
  1547. 'todo' or 'done', to indicate the general type of state.
  1548. :to new state, like in :from")
  1549. (defcustom org-enforce-todo-dependencies nil
  1550. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1551. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1552. be blocked if any prior sibling is not yet done.
  1553. Finally, if the parent is blocked because of ordered siblings of its own,
  1554. the child will also be blocked.
  1555. This variable needs to be set before org.el is loaded, and you need to
  1556. restart Emacs after a change to make the change effective. The only way
  1557. to change is while Emacs is running is through the customize interface."
  1558. :set (lambda (var val)
  1559. (set var val)
  1560. (if val
  1561. (add-hook 'org-blocker-hook
  1562. 'org-block-todo-from-children-or-siblings-or-parent)
  1563. (remove-hook 'org-blocker-hook
  1564. 'org-block-todo-from-children-or-siblings-or-parent)))
  1565. :group 'org-todo
  1566. :type 'boolean)
  1567. (defcustom org-enforce-todo-checkbox-dependencies nil
  1568. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1569. When this is nil, checkboxes have no influence on switching TODO states.
  1570. When non-nil, you first need to check off all check boxes before the TODO
  1571. entry can be switched to DONE.
  1572. This variable needs to be set before org.el is loaded, and you need to
  1573. restart Emacs after a change to make the change effective. The only way
  1574. to change is while Emacs is running is through the customize interface."
  1575. :set (lambda (var val)
  1576. (set var val)
  1577. (if val
  1578. (add-hook 'org-blocker-hook
  1579. 'org-block-todo-from-checkboxes)
  1580. (remove-hook 'org-blocker-hook
  1581. 'org-block-todo-from-checkboxes)))
  1582. :group 'org-todo
  1583. :type 'boolean)
  1584. (defcustom org-todo-state-tags-triggers nil
  1585. "Tag changes that should be triggered by TODO state changes.
  1586. This is a list. Each entry is
  1587. (state-change (tag . flag) .......)
  1588. State-change can be a string with a state, and empty string to indicate the
  1589. state that has no TODO keyword, or it can be one of the symbols `todo'
  1590. or `done', meaning any not-done or done state, respectively."
  1591. :group 'org-todo
  1592. :group 'org-tags
  1593. :type '(repeat
  1594. (cons (choice :tag "When changing to"
  1595. (const :tag "Not-done state" todo)
  1596. (const :tag "Done state" done)
  1597. (string :tag "State"))
  1598. (repeat
  1599. (cons :tag "Tag action"
  1600. (string :tag "Tag")
  1601. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1602. (defcustom org-log-done nil
  1603. "Information to record when a task moves to the DONE state.
  1604. Possible values are:
  1605. nil Don't add anything, just change the keyword
  1606. time Add a time stamp to the task
  1607. note Prompt a closing note and add it with template `org-log-note-headings'
  1608. This option can also be set with on a per-file-basis with
  1609. #+STARTUP: nologdone
  1610. #+STARTUP: logdone
  1611. #+STARTUP: lognotedone
  1612. You can have local logging settings for a subtree by setting the LOGGING
  1613. property to one or more of these keywords."
  1614. :group 'org-todo
  1615. :group 'org-progress
  1616. :type '(choice
  1617. (const :tag "No logging" nil)
  1618. (const :tag "Record CLOSED timestamp" time)
  1619. (const :tag "Record CLOSED timestamp with closing note." note)))
  1620. ;; Normalize old uses of org-log-done.
  1621. (cond
  1622. ((eq org-log-done t) (setq org-log-done 'time))
  1623. ((and (listp org-log-done) (memq 'done org-log-done))
  1624. (setq org-log-done 'note)))
  1625. (defcustom org-log-note-clock-out nil
  1626. "Non-nil means, record a note when clocking out of an item.
  1627. This can also be configured on a per-file basis by adding one of
  1628. the following lines anywhere in the buffer:
  1629. #+STARTUP: lognoteclock-out
  1630. #+STARTUP: nolognoteclock-out"
  1631. :group 'org-todo
  1632. :group 'org-progress
  1633. :type 'boolean)
  1634. (defcustom org-log-done-with-time t
  1635. "Non-nil means, the CLOSED time stamp will contain date and time.
  1636. When nil, only the date will be recorded."
  1637. :group 'org-progress
  1638. :type 'boolean)
  1639. (defcustom org-log-note-headings
  1640. '((done . "CLOSING NOTE %t")
  1641. (state . "State %-12s from %-12S %t")
  1642. (note . "Note taken on %t")
  1643. (clock-out . ""))
  1644. "Headings for notes added to entries.
  1645. The value is an alist, with the car being a symbol indicating the note
  1646. context, and the cdr is the heading to be used. The heading may also be the
  1647. empty string.
  1648. %t in the heading will be replaced by a time stamp.
  1649. %s will be replaced by the new TODO state, in double quotes.
  1650. %S will be replaced by the old TODO state, in double quotes.
  1651. %u will be replaced by the user name.
  1652. %U will be replaced by the full user name."
  1653. :group 'org-todo
  1654. :group 'org-progress
  1655. :type '(list :greedy t
  1656. (cons (const :tag "Heading when closing an item" done) string)
  1657. (cons (const :tag
  1658. "Heading when changing todo state (todo sequence only)"
  1659. state) string)
  1660. (cons (const :tag "Heading when just taking a note" note) string)
  1661. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1662. (unless (assq 'note org-log-note-headings)
  1663. (push '(note . "%t") org-log-note-headings))
  1664. (defcustom org-log-into-drawer nil
  1665. "Non-nil means, insert state change notes and time stamps into a drawer.
  1666. When nil, state changes notes will be inserted after the headline and
  1667. any scheduling and clock lines, but not inside a drawer.
  1668. The value of this variable should be the name of the drawer to use.
  1669. LOGBOOK is proposed at the default drawer for this purpose, you can
  1670. also set this to a string to define the drawer of your choice.
  1671. A value of t is also allowed, representing \"LOGBOOK\".
  1672. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1673. will be ignored."
  1674. :group 'org-todo
  1675. :group 'org-progress
  1676. :type '(choice
  1677. (const :tag "Not into a drawer" nil)
  1678. (const :tag "LOGBOOK" t)
  1679. (string :tag "Other")))
  1680. (if (fboundp 'defvaralias)
  1681. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1682. (defcustom org-log-state-notes-insert-after-drawers nil
  1683. "Non-nil means, insert state change notes after any drawers in entry.
  1684. Only the drawers that *immediately* follow the headline and the
  1685. deadline/scheduled line are skipped.
  1686. When nil, insert notes right after the heading and perhaps the line
  1687. with deadline/scheduling if present.
  1688. This variable will have no effect if `org-log-into-drawer' is
  1689. set."
  1690. :group 'org-todo
  1691. :group 'org-progress
  1692. :type 'boolean)
  1693. (defcustom org-log-states-order-reversed t
  1694. "Non-nil means, the latest state change note will be directly after heading.
  1695. When nil, the notes will be orderer according to time."
  1696. :group 'org-todo
  1697. :group 'org-progress
  1698. :type 'boolean)
  1699. (defcustom org-log-repeat 'time
  1700. "Non-nil means, record moving through the DONE state when triggering repeat.
  1701. An auto-repeating tasks is immediately switched back to TODO when marked
  1702. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1703. the TODO keyword definition, or recording a closing note by setting
  1704. `org-log-done', there will be no record of the task moving through DONE.
  1705. This variable forces taking a note anyway. Possible values are:
  1706. nil Don't force a record
  1707. time Record a time stamp
  1708. note Record a note
  1709. This option can also be set with on a per-file-basis with
  1710. #+STARTUP: logrepeat
  1711. #+STARTUP: lognoterepeat
  1712. #+STARTUP: nologrepeat
  1713. You can have local logging settings for a subtree by setting the LOGGING
  1714. property to one or more of these keywords."
  1715. :group 'org-todo
  1716. :group 'org-progress
  1717. :type '(choice
  1718. (const :tag "Don't force a record" nil)
  1719. (const :tag "Force recording the DONE state" time)
  1720. (const :tag "Force recording a note with the DONE state" note)))
  1721. (defgroup org-priorities nil
  1722. "Priorities in Org-mode."
  1723. :tag "Org Priorities"
  1724. :group 'org-todo)
  1725. (defcustom org-highest-priority ?A
  1726. "The highest priority of TODO items. A character like ?A, ?B etc.
  1727. Must have a smaller ASCII number than `org-lowest-priority'."
  1728. :group 'org-priorities
  1729. :type 'character)
  1730. (defcustom org-lowest-priority ?C
  1731. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1732. Must have a larger ASCII number than `org-highest-priority'."
  1733. :group 'org-priorities
  1734. :type 'character)
  1735. (defcustom org-default-priority ?B
  1736. "The default priority of TODO items.
  1737. This is the priority an item get if no explicit priority is given."
  1738. :group 'org-priorities
  1739. :type 'character)
  1740. (defcustom org-priority-start-cycle-with-default t
  1741. "Non-nil means, start with default priority when starting to cycle.
  1742. When this is nil, the first step in the cycle will be (depending on the
  1743. command used) one higher or lower that the default priority."
  1744. :group 'org-priorities
  1745. :type 'boolean)
  1746. (defgroup org-time nil
  1747. "Options concerning time stamps and deadlines in Org-mode."
  1748. :tag "Org Time"
  1749. :group 'org)
  1750. (defcustom org-insert-labeled-timestamps-at-point nil
  1751. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1752. When nil, these labeled time stamps are forces into the second line of an
  1753. entry, just after the headline. When scheduling from the global TODO list,
  1754. the time stamp will always be forced into the second line."
  1755. :group 'org-time
  1756. :type 'boolean)
  1757. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1758. "Formats for `format-time-string' which are used for time stamps.
  1759. It is not recommended to change this constant.")
  1760. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1761. "Number of minutes to round time stamps to.
  1762. These are two values, the first applies when first creating a time stamp.
  1763. The second applies when changing it with the commands `S-up' and `S-down'.
  1764. When changing the time stamp, this means that it will change in steps
  1765. of N minutes, as given by the second value.
  1766. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1767. numbers should be factors of 60, so for example 5, 10, 15.
  1768. When this is larger than 1, you can still force an exact time-stamp by using
  1769. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1770. and by using a prefix arg to `S-up/down' to specify the exact number
  1771. of minutes to shift."
  1772. :group 'org-time
  1773. :get '(lambda (var) ; Make sure all entries have 5 elements
  1774. (if (integerp (default-value var))
  1775. (list (default-value var) 5)
  1776. (default-value var)))
  1777. :type '(list
  1778. (integer :tag "when inserting times")
  1779. (integer :tag "when modifying times")))
  1780. ;; Normalize old customizations of this variable.
  1781. (when (integerp org-time-stamp-rounding-minutes)
  1782. (setq org-time-stamp-rounding-minutes
  1783. (list org-time-stamp-rounding-minutes
  1784. org-time-stamp-rounding-minutes)))
  1785. (defcustom org-display-custom-times nil
  1786. "Non-nil means, overlay custom formats over all time stamps.
  1787. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1788. To turn this on on a per-file basis, insert anywhere in the file:
  1789. #+STARTUP: customtime"
  1790. :group 'org-time
  1791. :set 'set-default
  1792. :type 'sexp)
  1793. (make-variable-buffer-local 'org-display-custom-times)
  1794. (defcustom org-time-stamp-custom-formats
  1795. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1796. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1797. These are overlayed over the default ISO format if the variable
  1798. `org-display-custom-times' is set. Time like %H:%M should be at the
  1799. end of the second format. The custom formats are also honored by export
  1800. commands, if custom time display is turned on at the time of export."
  1801. :group 'org-time
  1802. :type 'sexp)
  1803. (defun org-time-stamp-format (&optional long inactive)
  1804. "Get the right format for a time string."
  1805. (let ((f (if long (cdr org-time-stamp-formats)
  1806. (car org-time-stamp-formats))))
  1807. (if inactive
  1808. (concat "[" (substring f 1 -1) "]")
  1809. f)))
  1810. (defcustom org-time-clocksum-format "%d:%02d"
  1811. "The format string used when creating CLOCKSUM lines, or when
  1812. org-mode generates a time duration."
  1813. :group 'org-time
  1814. :type 'string)
  1815. (defcustom org-deadline-warning-days 14
  1816. "No. of days before expiration during which a deadline becomes active.
  1817. This variable governs the display in sparse trees and in the agenda.
  1818. When 0 or negative, it means use this number (the absolute value of it)
  1819. even if a deadline has a different individual lead time specified.
  1820. Custom commands can set this variable in the options section."
  1821. :group 'org-time
  1822. :group 'org-agenda-daily/weekly
  1823. :type 'integer)
  1824. (defcustom org-read-date-prefer-future t
  1825. "Non-nil means, assume future for incomplete date input from user.
  1826. This affects the following situations:
  1827. 1. The user gives a day, but no month.
  1828. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1829. read this as the third of *next* month. However, if you enter \"17\",
  1830. it will be considered as *this* month.
  1831. 2. The user gives a month but not a year.
  1832. For example, if it is april and you enter \"feb 2\", this will be read
  1833. as feb 2, *next* year. \"May 5\", however, will be this year.
  1834. Currently this does not work for ISO week specifications.
  1835. When this option is nil, the current month and year will always be used
  1836. as defaults."
  1837. :group 'org-time
  1838. :type 'boolean)
  1839. (defcustom org-read-date-display-live t
  1840. "Non-nil means, display current interpretation of date prompt live.
  1841. This display will be in an overlay, in the minibuffer."
  1842. :group 'org-time
  1843. :type 'boolean)
  1844. (defcustom org-read-date-popup-calendar t
  1845. "Non-nil means, pop up a calendar when prompting for a date.
  1846. In the calendar, the date can be selected with mouse-1. However, the
  1847. minibuffer will also be active, and you can simply enter the date as well.
  1848. When nil, only the minibuffer will be available."
  1849. :group 'org-time
  1850. :type 'boolean)
  1851. (if (fboundp 'defvaralias)
  1852. (defvaralias 'org-popup-calendar-for-date-prompt
  1853. 'org-read-date-popup-calendar))
  1854. (defcustom org-read-date-minibuffer-setup-hook nil
  1855. "Hook to be used to set up keys for the date/time interface.
  1856. Add key definitions to `minibuffer-local-map', which will be a temporary
  1857. copy."
  1858. :group 'org-time
  1859. :type 'hook)
  1860. (defcustom org-extend-today-until 0
  1861. "The hour when your day really ends. Must be an integer.
  1862. This has influence for the following applications:
  1863. - When switching the agenda to \"today\". It it is still earlier than
  1864. the time given here, the day recognized as TODAY is actually yesterday.
  1865. - When a date is read from the user and it is still before the time given
  1866. here, the current date and time will be assumed to be yesterday, 23:59.
  1867. Also, timestamps inserted in remember templates follow this rule.
  1868. IMPORTANT: This is a feature whose implementation is and likely will
  1869. remain incomplete. Really, it is only here because past midnight seems to
  1870. be the favorite working time of John Wiegley :-)"
  1871. :group 'org-time
  1872. :type 'integer)
  1873. (defcustom org-edit-timestamp-down-means-later nil
  1874. "Non-nil means, S-down will increase the time in a time stamp.
  1875. When nil, S-up will increase."
  1876. :group 'org-time
  1877. :type 'boolean)
  1878. (defcustom org-calendar-follow-timestamp-change t
  1879. "Non-nil means, make the calendar window follow timestamp changes.
  1880. When a timestamp is modified and the calendar window is visible, it will be
  1881. moved to the new date."
  1882. :group 'org-time
  1883. :type 'boolean)
  1884. (defgroup org-tags nil
  1885. "Options concerning tags in Org-mode."
  1886. :tag "Org Tags"
  1887. :group 'org)
  1888. (defcustom org-tag-alist nil
  1889. "List of tags allowed in Org-mode files.
  1890. When this list is nil, Org-mode will base TAG input on what is already in the
  1891. buffer.
  1892. The value of this variable is an alist, the car of each entry must be a
  1893. keyword as a string, the cdr may be a character that is used to select
  1894. that tag through the fast-tag-selection interface.
  1895. See the manual for details."
  1896. :group 'org-tags
  1897. :type '(repeat
  1898. (choice
  1899. (cons (string :tag "Tag name")
  1900. (character :tag "Access char"))
  1901. (const :tag "Start radio group" (:startgroup))
  1902. (const :tag "End radio group" (:endgroup))
  1903. (const :tag "New line" (:newline)))))
  1904. (defcustom org-tag-persistent-alist nil
  1905. "List of tags that will always appear in all Org-mode files.
  1906. This is in addition to any in buffer settings or customizations
  1907. of `org-tag-alist'.
  1908. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1909. The value of this variable is an alist, the car of each entry must be a
  1910. keyword as a string, the cdr may be a character that is used to select
  1911. that tag through the fast-tag-selection interface.
  1912. See the manual for details.
  1913. To disable these tags on a per-file basis, insert anywhere in the file:
  1914. #+STARTUP: noptag"
  1915. :group 'org-tags
  1916. :type '(repeat
  1917. (choice
  1918. (cons (string :tag "Tag name")
  1919. (character :tag "Access char"))
  1920. (const :tag "Start radio group" (:startgroup))
  1921. (const :tag "End radio group" (:endgroup))
  1922. (const :tag "New line" (:newline)))))
  1923. (defvar org-file-tags nil
  1924. "List of tags that can be inherited by all entries in the file.
  1925. The tags will be inherited if the variable `org-use-tag-inheritance'
  1926. says they should be.
  1927. This variable is populated from #+TAG lines.")
  1928. (defcustom org-use-fast-tag-selection 'auto
  1929. "Non-nil means, use fast tag selection scheme.
  1930. This is a special interface to select and deselect tags with single keys.
  1931. When nil, fast selection is never used.
  1932. When the symbol `auto', fast selection is used if and only if selection
  1933. characters for tags have been configured, either through the variable
  1934. `org-tag-alist' or through a #+TAGS line in the buffer.
  1935. When t, fast selection is always used and selection keys are assigned
  1936. automatically if necessary."
  1937. :group 'org-tags
  1938. :type '(choice
  1939. (const :tag "Always" t)
  1940. (const :tag "Never" nil)
  1941. (const :tag "When selection characters are configured" 'auto)))
  1942. (defcustom org-fast-tag-selection-single-key nil
  1943. "Non-nil means, fast tag selection exits after first change.
  1944. When nil, you have to press RET to exit it.
  1945. During fast tag selection, you can toggle this flag with `C-c'.
  1946. This variable can also have the value `expert'. In this case, the window
  1947. displaying the tags menu is not even shown, until you press C-c again."
  1948. :group 'org-tags
  1949. :type '(choice
  1950. (const :tag "No" nil)
  1951. (const :tag "Yes" t)
  1952. (const :tag "Expert" expert)))
  1953. (defvar org-fast-tag-selection-include-todo nil
  1954. "Non-nil means, fast tags selection interface will also offer TODO states.
  1955. This is an undocumented feature, you should not rely on it.")
  1956. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1957. "The column to which tags should be indented in a headline.
  1958. If this number is positive, it specifies the column. If it is negative,
  1959. it means that the tags should be flushright to that column. For example,
  1960. -80 works well for a normal 80 character screen."
  1961. :group 'org-tags
  1962. :type 'integer)
  1963. (defcustom org-auto-align-tags t
  1964. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1965. These operations change the length of a headline and therefore shift
  1966. the tags around. With this options turned on, after each such operation
  1967. the tags are again aligned to `org-tags-column'."
  1968. :group 'org-tags
  1969. :type 'boolean)
  1970. (defcustom org-use-tag-inheritance t
  1971. "Non-nil means, tags in levels apply also for sublevels.
  1972. When nil, only the tags directly given in a specific line apply there.
  1973. This may also be a list of tags that should be inherited, or a regexp that
  1974. matches tags that should be inherited. Additional control is possible
  1975. with the variable `org-tags-exclude-from-inheritance' which gives an
  1976. explicit list of tags to be excluded from inheritance., even if the value of
  1977. `org-use-tag-inheritance' would select it for inheritance.
  1978. If this option is t, a match early-on in a tree can lead to a large
  1979. number of matches in the subtree when constructing the agenda or creating
  1980. a sparse tree. If you only want to see the first match in a tree during
  1981. a search, check out the variable `org-tags-match-list-sublevels'."
  1982. :group 'org-tags
  1983. :type '(choice
  1984. (const :tag "Not" nil)
  1985. (const :tag "Always" t)
  1986. (repeat :tag "Specific tags" (string :tag "Tag"))
  1987. (regexp :tag "Tags matched by regexp")))
  1988. (defcustom org-tags-exclude-from-inheritance nil
  1989. "List of tags that should never be inherited.
  1990. This is a way to exclude a few tags from inheritance. For way to do
  1991. the opposite, to actively allow inheritance for selected tags,
  1992. see the variable `org-use-tag-inheritance'."
  1993. :group 'org-tags
  1994. :type '(repeat (string :tag "Tag")))
  1995. (defun org-tag-inherit-p (tag)
  1996. "Check if TAG is one that should be inherited."
  1997. (cond
  1998. ((member tag org-tags-exclude-from-inheritance) nil)
  1999. ((eq org-use-tag-inheritance t) t)
  2000. ((not org-use-tag-inheritance) nil)
  2001. ((stringp org-use-tag-inheritance)
  2002. (string-match org-use-tag-inheritance tag))
  2003. ((listp org-use-tag-inheritance)
  2004. (member tag org-use-tag-inheritance))
  2005. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2006. (defcustom org-tags-match-list-sublevels t
  2007. "Non-nil means list also sublevels of headlines matching a search.
  2008. This variable applies to tags/property searches, and also to stuck
  2009. projects because this search is based on a tags match as well.
  2010. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2011. the sublevels of a headline matching a tag search often also match
  2012. the same search. Listing all of them can create very long lists.
  2013. Setting this variable to nil causes subtrees of a match to be skipped.
  2014. As a special case, if the tag search is restricted to TODO items, the
  2015. value of this variable is ignored and sublevels are always checked, to
  2016. make sure all corresponding TODO items find their way into the list.
  2017. This variable is semi-obsolete and probably should always be true. It
  2018. is better to limit inheritance to certain tags using the variables
  2019. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2020. :group 'org-tags
  2021. :type '(choice
  2022. (const :tag "No, don't list them" nil)
  2023. (const :tag "Yes, do list them" t)
  2024. (const :tag "List them, indented with leading dots" indented)))
  2025. (defvar org-tags-history nil
  2026. "History of minibuffer reads for tags.")
  2027. (defvar org-last-tags-completion-table nil
  2028. "The last used completion table for tags.")
  2029. (defvar org-after-tags-change-hook nil
  2030. "Hook that is run after the tags in a line have changed.")
  2031. (defgroup org-properties nil
  2032. "Options concerning properties in Org-mode."
  2033. :tag "Org Properties"
  2034. :group 'org)
  2035. (defcustom org-property-format "%-10s %s"
  2036. "How property key/value pairs should be formatted by `indent-line'.
  2037. When `indent-line' hits a property definition, it will format the line
  2038. according to this format, mainly to make sure that the values are
  2039. lined-up with respect to each other."
  2040. :group 'org-properties
  2041. :type 'string)
  2042. (defcustom org-use-property-inheritance nil
  2043. "Non-nil means, properties apply also for sublevels.
  2044. This setting is chiefly used during property searches. Turning it on can
  2045. cause significant overhead when doing a search, which is why it is not
  2046. on by default.
  2047. When nil, only the properties directly given in the current entry count.
  2048. When t, every property is inherited. The value may also be a list of
  2049. properties that should have inheritance, or a regular expression matching
  2050. properties that should be inherited.
  2051. However, note that some special properties use inheritance under special
  2052. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2053. and the properties ending in \"_ALL\" when they are used as descriptor
  2054. for valid values of a property.
  2055. Note for programmers:
  2056. When querying an entry with `org-entry-get', you can control if inheritance
  2057. should be used. By default, `org-entry-get' looks only at the local
  2058. properties. You can request inheritance by setting the inherit argument
  2059. to t (to force inheritance) or to `selective' (to respect the setting
  2060. in this variable)."
  2061. :group 'org-properties
  2062. :type '(choice
  2063. (const :tag "Not" nil)
  2064. (const :tag "Always" t)
  2065. (repeat :tag "Specific properties" (string :tag "Property"))
  2066. (regexp :tag "Properties matched by regexp")))
  2067. (defun org-property-inherit-p (property)
  2068. "Check if PROPERTY is one that should be inherited."
  2069. (cond
  2070. ((eq org-use-property-inheritance t) t)
  2071. ((not org-use-property-inheritance) nil)
  2072. ((stringp org-use-property-inheritance)
  2073. (string-match org-use-property-inheritance property))
  2074. ((listp org-use-property-inheritance)
  2075. (member property org-use-property-inheritance))
  2076. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2077. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2078. "The default column format, if no other format has been defined.
  2079. This variable can be set on the per-file basis by inserting a line
  2080. #+COLUMNS: %25ITEM ....."
  2081. :group 'org-properties
  2082. :type 'string)
  2083. (defcustom org-columns-ellipses ".."
  2084. "The ellipses to be used when a field in column view is truncated.
  2085. When this is the empty string, as many characters as possible are shown,
  2086. but then there will be no visual indication that the field has been truncated.
  2087. When this is a string of length N, the last N characters of a truncated
  2088. field are replaced by this string. If the column is narrower than the
  2089. ellipses string, only part of the ellipses string will be shown."
  2090. :group 'org-properties
  2091. :type 'string)
  2092. (defcustom org-columns-modify-value-for-display-function nil
  2093. "Function that modifies values for display in column view.
  2094. For example, it can be used to cut out a certain part from a time stamp.
  2095. The function must take 2 arguments:
  2096. column-title The title of the column (*not* the property name)
  2097. value The value that should be modified.
  2098. The function should return the value that should be displayed,
  2099. or nil if the normal value should be used."
  2100. :group 'org-properties
  2101. :type 'function)
  2102. (defcustom org-effort-property "Effort"
  2103. "The property that is being used to keep track of effort estimates.
  2104. Effort estimates given in this property need to have the format H:MM."
  2105. :group 'org-properties
  2106. :group 'org-progress
  2107. :type '(string :tag "Property"))
  2108. (defconst org-global-properties-fixed
  2109. '(("VISIBILITY_ALL" . "folded children content all"))
  2110. "List of property/value pairs that can be inherited by any entry.
  2111. These are fixed values, for the preset properties. The user variable
  2112. that can be used to add to this list is `org-global-properties'.
  2113. The entries in this list are cons cells where the car is a property
  2114. name and cdr is a string with the value. If the value represents
  2115. multiple items like an \"_ALL\" property, separate the items by
  2116. spaces.")
  2117. (defcustom org-global-properties nil
  2118. "List of property/value pairs that can be inherited by any entry.
  2119. This list will be combined with the constant `org-global-properties-fixed'.
  2120. The entries in this list are cons cells where the car is a property
  2121. name and cdr is a string with the value.
  2122. You can set buffer-local values for the same purpose in the variable
  2123. `org-file-properties' this by adding lines like
  2124. #+PROPERTY: NAME VALUE"
  2125. :group 'org-properties
  2126. :type '(repeat
  2127. (cons (string :tag "Property")
  2128. (string :tag "Value"))))
  2129. (defvar org-file-properties nil
  2130. "List of property/value pairs that can be inherited by any entry.
  2131. Valid for the current buffer.
  2132. This variable is populated from #+PROPERTY lines.")
  2133. (make-variable-buffer-local 'org-file-properties)
  2134. (defgroup org-agenda nil
  2135. "Options concerning agenda views in Org-mode."
  2136. :tag "Org Agenda"
  2137. :group 'org)
  2138. (defvar org-category nil
  2139. "Variable used by org files to set a category for agenda display.
  2140. Such files should use a file variable to set it, for example
  2141. # -*- mode: org; org-category: \"ELisp\"
  2142. or contain a special line
  2143. #+CATEGORY: ELisp
  2144. If the file does not specify a category, then file's base name
  2145. is used instead.")
  2146. (make-variable-buffer-local 'org-category)
  2147. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2148. (defcustom org-agenda-files nil
  2149. "The files to be used for agenda display.
  2150. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2151. \\[org-remove-file]. You can also use customize to edit the list.
  2152. If an entry is a directory, all files in that directory that are matched by
  2153. `org-agenda-file-regexp' will be part of the file list.
  2154. If the value of the variable is not a list but a single file name, then
  2155. the list of agenda files is actually stored and maintained in that file, one
  2156. agenda file per line."
  2157. :group 'org-agenda
  2158. :type '(choice
  2159. (repeat :tag "List of files and directories" file)
  2160. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2161. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2162. "Regular expression to match files for `org-agenda-files'.
  2163. If any element in the list in that variable contains a directory instead
  2164. of a normal file, all files in that directory that are matched by this
  2165. regular expression will be included."
  2166. :group 'org-agenda
  2167. :type 'regexp)
  2168. (defcustom org-agenda-text-search-extra-files nil
  2169. "List of extra files to be searched by text search commands.
  2170. These files will be search in addition to the agenda files by the
  2171. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2172. Note that these files will only be searched for text search commands,
  2173. not for the other agenda views like todo lists, tag searches or the weekly
  2174. agenda. This variable is intended to list notes and possibly archive files
  2175. that should also be searched by these two commands.
  2176. In fact, if the first element in the list is the symbol `agenda-archives',
  2177. than all archive files of all agenda files will be added to the search
  2178. scope."
  2179. :group 'org-agenda
  2180. :type '(set :greedy t
  2181. (const :tag "Agenda Archives" agenda-archives)
  2182. (repeat :inline t (file))))
  2183. (if (fboundp 'defvaralias)
  2184. (defvaralias 'org-agenda-multi-occur-extra-files
  2185. 'org-agenda-text-search-extra-files))
  2186. (defcustom org-agenda-skip-unavailable-files nil
  2187. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2188. A nil value means to remove them, after a query, from the list."
  2189. :group 'org-agenda
  2190. :type 'boolean)
  2191. (defcustom org-calendar-to-agenda-key [?c]
  2192. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2193. The command `org-calendar-goto-agenda' will be bound to this key. The
  2194. default is the character `c' because then `c' can be used to switch back and
  2195. forth between agenda and calendar."
  2196. :group 'org-agenda
  2197. :type 'sexp)
  2198. (defcustom org-calendar-agenda-action-key [?k]
  2199. "The key to be installed in `calendar-mode-map' for agenda-action.
  2200. The command `org-agenda-action' will be bound to this key. The
  2201. default is the character `k' because we use the same key in the agenda."
  2202. :group 'org-agenda
  2203. :type 'sexp)
  2204. (eval-after-load "calendar"
  2205. '(progn
  2206. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2207. 'org-calendar-goto-agenda)
  2208. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2209. 'org-agenda-action)))
  2210. (defgroup org-latex nil
  2211. "Options for embedding LaTeX code into Org-mode."
  2212. :tag "Org LaTeX"
  2213. :group 'org)
  2214. (defcustom org-format-latex-options
  2215. '(:foreground default :background default :scale 1.0
  2216. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2217. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2218. "Options for creating images from LaTeX fragments.
  2219. This is a property list with the following properties:
  2220. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2221. `default' means use the foreground of the default face.
  2222. :background the background color, or \"Transparent\".
  2223. `default' means use the background of the default face.
  2224. :scale a scaling factor for the size of the images.
  2225. :html-foreground, :html-background, :html-scale
  2226. the same numbers for HTML export.
  2227. :matchers a list indicating which matchers should be used to
  2228. find LaTeX fragments. Valid members of this list are:
  2229. \"begin\" find environments
  2230. \"$1\" find single characters surrounded by $.$
  2231. \"$\" find math expressions surrounded by $...$
  2232. \"$$\" find math expressions surrounded by $$....$$
  2233. \"\\(\" find math expressions surrounded by \\(...\\)
  2234. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2235. :group 'org-latex
  2236. :type 'plist)
  2237. (defcustom org-format-latex-header "\\documentclass{article}
  2238. \\usepackage{fullpage} % do not remove
  2239. \\usepackage{amssymb}
  2240. \\usepackage[usenames]{color}
  2241. \\usepackage{amsmath}
  2242. \\usepackage{latexsym}
  2243. \\usepackage[mathscr]{eucal}
  2244. \\pagestyle{empty} % do not remove"
  2245. "The document header used for processing LaTeX fragments."
  2246. :group 'org-latex
  2247. :type 'string)
  2248. (defgroup org-font-lock nil
  2249. "Font-lock settings for highlighting in Org-mode."
  2250. :tag "Org Font Lock"
  2251. :group 'org)
  2252. (defcustom org-level-color-stars-only nil
  2253. "Non-nil means fontify only the stars in each headline.
  2254. When nil, the entire headline is fontified.
  2255. Changing it requires restart of `font-lock-mode' to become effective
  2256. also in regions already fontified."
  2257. :group 'org-font-lock
  2258. :type 'boolean)
  2259. (defcustom org-hide-leading-stars nil
  2260. "Non-nil means, hide the first N-1 stars in a headline.
  2261. This works by using the face `org-hide' for these stars. This
  2262. face is white for a light background, and black for a dark
  2263. background. You may have to customize the face `org-hide' to
  2264. make this work.
  2265. Changing it requires restart of `font-lock-mode' to become effective
  2266. also in regions already fontified.
  2267. You may also set this on a per-file basis by adding one of the following
  2268. lines to the buffer:
  2269. #+STARTUP: hidestars
  2270. #+STARTUP: showstars"
  2271. :group 'org-font-lock
  2272. :type 'boolean)
  2273. (defcustom org-fontify-done-headline nil
  2274. "Non-nil means, change the face of a headline if it is marked DONE.
  2275. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2276. When this is non-nil, the headline after the keyword is set to the
  2277. `org-headline-done' as an additional indication."
  2278. :group 'org-font-lock
  2279. :type 'boolean)
  2280. (defcustom org-fontify-emphasized-text t
  2281. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2282. Changing this variable requires a restart of Emacs to take effect."
  2283. :group 'org-font-lock
  2284. :type 'boolean)
  2285. (defcustom org-highlight-latex-fragments-and-specials nil
  2286. "Non-nil means, fontify what is treated specially by the exporters."
  2287. :group 'org-font-lock
  2288. :type 'boolean)
  2289. (defcustom org-hide-emphasis-markers nil
  2290. "Non-nil mean font-lock should hide the emphasis marker characters."
  2291. :group 'org-font-lock
  2292. :type 'boolean)
  2293. (defvar org-emph-re nil
  2294. "Regular expression for matching emphasis.")
  2295. (defvar org-verbatim-re nil
  2296. "Regular expression for matching verbatim text.")
  2297. (defvar org-emphasis-regexp-components) ; defined just below
  2298. (defvar org-emphasis-alist) ; defined just below
  2299. (defun org-set-emph-re (var val)
  2300. "Set variable and compute the emphasis regular expression."
  2301. (set var val)
  2302. (when (and (boundp 'org-emphasis-alist)
  2303. (boundp 'org-emphasis-regexp-components)
  2304. org-emphasis-alist org-emphasis-regexp-components)
  2305. (let* ((e org-emphasis-regexp-components)
  2306. (pre (car e))
  2307. (post (nth 1 e))
  2308. (border (nth 2 e))
  2309. (body (nth 3 e))
  2310. (nl (nth 4 e))
  2311. (body1 (concat body "*?"))
  2312. (markers (mapconcat 'car org-emphasis-alist ""))
  2313. (vmarkers (mapconcat
  2314. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2315. org-emphasis-alist "")))
  2316. ;; make sure special characters appear at the right position in the class
  2317. (if (string-match "\\^" markers)
  2318. (setq markers (concat (replace-match "" t t markers) "^")))
  2319. (if (string-match "-" markers)
  2320. (setq markers (concat (replace-match "" t t markers) "-")))
  2321. (if (string-match "\\^" vmarkers)
  2322. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2323. (if (string-match "-" vmarkers)
  2324. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2325. (if (> nl 0)
  2326. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2327. (int-to-string nl) "\\}")))
  2328. ;; Make the regexp
  2329. (setq org-emph-re
  2330. (concat "\\([" pre "]\\|^\\)"
  2331. "\\("
  2332. "\\([" markers "]\\)"
  2333. "\\("
  2334. "[^" border "]\\|"
  2335. "[^" border "]"
  2336. body1
  2337. "[^" border "]"
  2338. "\\)"
  2339. "\\3\\)"
  2340. "\\([" post "]\\|$\\)"))
  2341. (setq org-verbatim-re
  2342. (concat "\\([" pre "]\\|^\\)"
  2343. "\\("
  2344. "\\([" vmarkers "]\\)"
  2345. "\\("
  2346. "[^" border "]\\|"
  2347. "[^" border "]"
  2348. body1
  2349. "[^" border "]"
  2350. "\\)"
  2351. "\\3\\)"
  2352. "\\([" post "]\\|$\\)")))))
  2353. (defcustom org-emphasis-regexp-components
  2354. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2355. "Components used to build the regular expression for emphasis.
  2356. This is a list with 6 entries. Terminology: In an emphasis string
  2357. like \" *strong word* \", we call the initial space PREMATCH, the final
  2358. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2359. and \"trong wor\" is the body. The different components in this variable
  2360. specify what is allowed/forbidden in each part:
  2361. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2362. post Chars allowed as postmatch. End of line will be allowed too.
  2363. border The chars *forbidden* as border characters.
  2364. body-regexp A regexp like \".\" to match a body character. Don't use
  2365. non-shy groups here, and don't allow newline here.
  2366. newline The maximum number of newlines allowed in an emphasis exp.
  2367. Use customize to modify this, or restart Emacs after changing it."
  2368. :group 'org-font-lock
  2369. :set 'org-set-emph-re
  2370. :type '(list
  2371. (sexp :tag "Allowed chars in pre ")
  2372. (sexp :tag "Allowed chars in post ")
  2373. (sexp :tag "Forbidden chars in border ")
  2374. (sexp :tag "Regexp for body ")
  2375. (integer :tag "number of newlines allowed")
  2376. (option (boolean :tag "Please ignore this button"))))
  2377. (defcustom org-emphasis-alist
  2378. `(("*" bold "<b>" "</b>")
  2379. ("/" italic "<i>" "</i>")
  2380. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2381. ("=" org-code "<code>" "</code>" verbatim)
  2382. ("~" org-verbatim "<code>" "</code>" verbatim)
  2383. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2384. "<del>" "</del>")
  2385. )
  2386. "Special syntax for emphasized text.
  2387. Text starting and ending with a special character will be emphasized, for
  2388. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2389. characters, the face to be used by font-lock for highlighting in Org-mode
  2390. Emacs buffers, and the HTML tags to be used for this.
  2391. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2392. Use customize to modify this, or restart Emacs after changing it."
  2393. :group 'org-font-lock
  2394. :set 'org-set-emph-re
  2395. :type '(repeat
  2396. (list
  2397. (string :tag "Marker character")
  2398. (choice
  2399. (face :tag "Font-lock-face")
  2400. (plist :tag "Face property list"))
  2401. (string :tag "HTML start tag")
  2402. (string :tag "HTML end tag")
  2403. (option (const verbatim)))))
  2404. ;;; Miscellaneous options
  2405. (defgroup org-completion nil
  2406. "Completion in Org-mode."
  2407. :tag "Org Completion"
  2408. :group 'org)
  2409. (defcustom org-completion-use-ido nil
  2410. "Non-nil means, use ido completion wherever possible.
  2411. Note that `ido-mode' must be active for this variable to be relevant.
  2412. If you decide to turn this variable on, you might well want to turn off
  2413. `org-outline-path-complete-in-steps'."
  2414. :group 'org-completion
  2415. :type 'boolean)
  2416. (defcustom org-completion-fallback-command 'hippie-expand
  2417. "The expansion command called by \\[org-complete] in normal context.
  2418. Normal means, no org-mode-specific context."
  2419. :group 'org-completion
  2420. :type 'function)
  2421. ;;; Functions and variables from ther packages
  2422. ;; Declared here to avoid compiler warnings
  2423. ;; XEmacs only
  2424. (defvar outline-mode-menu-heading)
  2425. (defvar outline-mode-menu-show)
  2426. (defvar outline-mode-menu-hide)
  2427. (defvar zmacs-regions) ; XEmacs regions
  2428. ;; Emacs only
  2429. (defvar mark-active)
  2430. ;; Various packages
  2431. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2432. (declare-function calendar-forward-day "cal-move" (arg))
  2433. (declare-function calendar-goto-date "cal-move" (date))
  2434. (declare-function calendar-goto-today "cal-move" ())
  2435. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2436. (defvar calc-embedded-close-formula)
  2437. (defvar calc-embedded-open-formula)
  2438. (declare-function cdlatex-tab "ext:cdlatex" ())
  2439. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2440. (defvar font-lock-unfontify-region-function)
  2441. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2442. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2443. (defvar iswitchb-temp-buflist)
  2444. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2445. (defvar org-agenda-tags-todo-honor-ignore-options)
  2446. (declare-function org-agenda-skip "org-agenda" ())
  2447. (declare-function org-format-agenda-item "org-agenda"
  2448. (extra txt &optional category tags dotime noprefix remove-re))
  2449. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2450. (declare-function org-agenda-change-all-lines "org-agenda"
  2451. (newhead hdmarker &optional fixface just-this))
  2452. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2453. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2454. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2455. (beg end))
  2456. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2457. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2458. "org-agenda" (&optional end))
  2459. (declare-function parse-time-string "parse-time" (string))
  2460. (declare-function remember "remember" (&optional initial))
  2461. (declare-function remember-buffer-desc "remember" ())
  2462. (declare-function remember-finalize "remember" ())
  2463. (defvar remember-save-after-remembering)
  2464. (defvar remember-data-file)
  2465. (defvar remember-register)
  2466. (defvar remember-buffer)
  2467. (defvar remember-handler-functions)
  2468. (defvar remember-annotation-functions)
  2469. (defvar texmathp-why)
  2470. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2471. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2472. (defvar w3m-current-url)
  2473. (defvar w3m-current-title)
  2474. (defvar org-latex-regexps)
  2475. ;;; Autoload and prepare some org modules
  2476. ;; Some table stuff that needs to be defined here, because it is used
  2477. ;; by the functions setting up org-mode or checking for table context.
  2478. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2479. "Detects an org-type or table-type table.")
  2480. (defconst org-table-line-regexp "^[ \t]*|"
  2481. "Detects an org-type table line.")
  2482. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2483. "Detects an org-type table line.")
  2484. (defconst org-table-hline-regexp "^[ \t]*|-"
  2485. "Detects an org-type table hline.")
  2486. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2487. "Detects a table-type table hline.")
  2488. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2489. "Searching from within a table (any type) this finds the first line
  2490. outside the table.")
  2491. ;; Autoload the functions in org-table.el that are needed by functions here.
  2492. (eval-and-compile
  2493. (org-autoload "org-table"
  2494. '(org-table-align org-table-begin org-table-blank-field
  2495. org-table-convert org-table-convert-region org-table-copy-down
  2496. org-table-copy-region org-table-create
  2497. org-table-create-or-convert-from-region
  2498. org-table-create-with-table.el org-table-current-dline
  2499. org-table-cut-region org-table-delete-column org-table-edit-field
  2500. org-table-edit-formulas org-table-end org-table-eval-formula
  2501. org-table-export org-table-field-info
  2502. org-table-get-stored-formulas org-table-goto-column
  2503. org-table-hline-and-move org-table-import org-table-insert-column
  2504. org-table-insert-hline org-table-insert-row org-table-iterate
  2505. org-table-justify-field-maybe org-table-kill-row
  2506. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2507. org-table-move-column org-table-move-column-left
  2508. org-table-move-column-right org-table-move-row
  2509. org-table-move-row-down org-table-move-row-up
  2510. org-table-next-field org-table-next-row org-table-paste-rectangle
  2511. org-table-previous-field org-table-recalculate
  2512. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2513. org-table-toggle-coordinate-overlays
  2514. org-table-toggle-formula-debugger org-table-wrap-region
  2515. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2516. (defun org-at-table-p (&optional table-type)
  2517. "Return t if the cursor is inside an org-type table.
  2518. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2519. (if org-enable-table-editor
  2520. (save-excursion
  2521. (beginning-of-line 1)
  2522. (looking-at (if table-type org-table-any-line-regexp
  2523. org-table-line-regexp)))
  2524. nil))
  2525. (defsubst org-table-p () (org-at-table-p))
  2526. (defun org-at-table.el-p ()
  2527. "Return t if and only if we are at a table.el table."
  2528. (and (org-at-table-p 'any)
  2529. (save-excursion
  2530. (goto-char (org-table-begin 'any))
  2531. (looking-at org-table1-hline-regexp))))
  2532. (defun org-table-recognize-table.el ()
  2533. "If there is a table.el table nearby, recognize it and move into it."
  2534. (if org-table-tab-recognizes-table.el
  2535. (if (org-at-table.el-p)
  2536. (progn
  2537. (beginning-of-line 1)
  2538. (if (looking-at org-table-dataline-regexp)
  2539. nil
  2540. (if (looking-at org-table1-hline-regexp)
  2541. (progn
  2542. (beginning-of-line 2)
  2543. (if (looking-at org-table-any-border-regexp)
  2544. (beginning-of-line -1)))))
  2545. (if (re-search-forward "|" (org-table-end t) t)
  2546. (progn
  2547. (require 'table)
  2548. (if (table--at-cell-p (point))
  2549. t
  2550. (message "recognizing table.el table...")
  2551. (table-recognize-table)
  2552. (message "recognizing table.el table...done")))
  2553. (error "This should not happen..."))
  2554. t)
  2555. nil)
  2556. nil))
  2557. (defun org-at-table-hline-p ()
  2558. "Return t if the cursor is inside a hline in a table."
  2559. (if org-enable-table-editor
  2560. (save-excursion
  2561. (beginning-of-line 1)
  2562. (looking-at org-table-hline-regexp))
  2563. nil))
  2564. (defvar org-table-clean-did-remove-column nil)
  2565. (defun org-table-map-tables (function)
  2566. "Apply FUNCTION to the start of all tables in the buffer."
  2567. (save-excursion
  2568. (save-restriction
  2569. (widen)
  2570. (goto-char (point-min))
  2571. (while (re-search-forward org-table-any-line-regexp nil t)
  2572. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2573. (beginning-of-line 1)
  2574. (if (looking-at org-table-line-regexp)
  2575. (save-excursion (funcall function)))
  2576. (re-search-forward org-table-any-border-regexp nil 1))))
  2577. (message "Mapping tables: done"))
  2578. ;; Declare and autoload functions from org-exp.el & Co
  2579. (declare-function org-default-export-plist "org-exp")
  2580. (declare-function org-infile-export-plist "org-exp")
  2581. (declare-function org-get-current-options "org-exp")
  2582. (eval-and-compile
  2583. (org-autoload "org-exp"
  2584. '(org-export org-export-visible
  2585. org-insert-export-options-template
  2586. org-table-clean-before-export))
  2587. (org-autoload "org-ascii"
  2588. '(org-export-as-ascii org-export-ascii-preprocess))
  2589. (org-autoload "org-html"
  2590. '(org-export-as-html-and-open
  2591. org-export-as-html-batch org-export-as-html-to-buffer
  2592. org-replace-region-by-html org-export-region-as-html
  2593. org-export-as-html))
  2594. (org-autoload "org-icalendar"
  2595. '(org-export-icalendar-this-file
  2596. org-export-icalendar-all-agenda-files
  2597. org-export-icalendar-combine-agenda-files))
  2598. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2599. ;; Declare and autoload functions from org-agenda.el
  2600. (eval-and-compile
  2601. (org-autoload "org-agenda"
  2602. '(org-agenda org-agenda-list org-search-view
  2603. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2604. org-diary org-agenda-to-appt
  2605. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2606. ;; Autoload org-remember
  2607. (eval-and-compile
  2608. (org-autoload "org-remember"
  2609. '(org-remember-insinuate org-remember-annotation
  2610. org-remember-apply-template org-remember org-remember-handler)))
  2611. ;; Autoload org-clock.el
  2612. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2613. (beg end))
  2614. (declare-function org-clock-update-mode-line "org-clock" ())
  2615. (defvar org-clock-start-time)
  2616. (defvar org-clock-marker (make-marker)
  2617. "Marker recording the last clock-in.")
  2618. (eval-and-compile
  2619. (org-autoload
  2620. "org-clock"
  2621. '(org-clock-in org-clock-out org-clock-cancel
  2622. org-clock-goto org-clock-sum org-clock-display
  2623. org-clock-remove-overlays org-clock-report
  2624. org-clocktable-shift org-dblock-write:clocktable
  2625. org-get-clocktable)))
  2626. (defun org-clock-update-time-maybe ()
  2627. "If this is a CLOCK line, update it and return t.
  2628. Otherwise, return nil."
  2629. (interactive)
  2630. (save-excursion
  2631. (beginning-of-line 1)
  2632. (skip-chars-forward " \t")
  2633. (when (looking-at org-clock-string)
  2634. (let ((re (concat "[ \t]*" org-clock-string
  2635. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2636. "\\([ \t]*=>.*\\)?\\)?"))
  2637. ts te h m s neg)
  2638. (cond
  2639. ((not (looking-at re))
  2640. nil)
  2641. ((not (match-end 2))
  2642. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2643. (> org-clock-marker (point))
  2644. (<= org-clock-marker (point-at-eol)))
  2645. ;; The clock is running here
  2646. (setq org-clock-start-time
  2647. (apply 'encode-time
  2648. (org-parse-time-string (match-string 1))))
  2649. (org-clock-update-mode-line)))
  2650. (t
  2651. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2652. (end-of-line 1)
  2653. (setq ts (match-string 1)
  2654. te (match-string 3))
  2655. (setq s (- (time-to-seconds
  2656. (apply 'encode-time (org-parse-time-string te)))
  2657. (time-to-seconds
  2658. (apply 'encode-time (org-parse-time-string ts))))
  2659. neg (< s 0)
  2660. s (abs s)
  2661. h (floor (/ s 3600))
  2662. s (- s (* 3600 h))
  2663. m (floor (/ s 60))
  2664. s (- s (* 60 s)))
  2665. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2666. t))))))
  2667. (defun org-check-running-clock ()
  2668. "Check if the current buffer contains the running clock.
  2669. If yes, offer to stop it and to save the buffer with the changes."
  2670. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2671. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2672. (buffer-name))))
  2673. (org-clock-out)
  2674. (when (y-or-n-p "Save changed buffer?")
  2675. (save-buffer))))
  2676. (defun org-clocktable-try-shift (dir n)
  2677. "Check if this line starts a clock table, if yes, shift the time block."
  2678. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2679. (org-clocktable-shift dir n)))
  2680. ;; Autoload org-timer.el
  2681. (eval-and-compile
  2682. (org-autoload
  2683. "org-timer"
  2684. '(org-timer-start org-timer org-timer-item
  2685. org-timer-change-times-in-region)))
  2686. ;; Autoload org-feed.el
  2687. (eval-and-compile
  2688. (org-autoload
  2689. "org-feed"
  2690. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2691. ;; Autoload archiving code
  2692. ;; The stuff that is needed for cycling and tags has to be defined here.
  2693. (defgroup org-archive nil
  2694. "Options concerning archiving in Org-mode."
  2695. :tag "Org Archive"
  2696. :group 'org-structure)
  2697. (defcustom org-archive-location "%s_archive::"
  2698. "The location where subtrees should be archived.
  2699. The value of this variable is a string, consisting of two parts,
  2700. separated by a double-colon. The first part is a filename and
  2701. the second part is a headline.
  2702. When the filename is omitted, archiving happens in the same file.
  2703. %s in the filename will be replaced by the current file
  2704. name (without the directory part). Archiving to a different file
  2705. is useful to keep archived entries from contributing to the
  2706. Org-mode Agenda.
  2707. The archived entries will be filed as subtrees of the specified
  2708. headline. When the headline is omitted, the subtrees are simply
  2709. filed away at the end of the file, as top-level entries. Also in
  2710. the heading you can use %s to represent the file name, this can be
  2711. useful when using the same archive for a number of different files.
  2712. Here are a few examples:
  2713. \"%s_archive::\"
  2714. If the current file is Projects.org, archive in file
  2715. Projects.org_archive, as top-level trees. This is the default.
  2716. \"::* Archived Tasks\"
  2717. Archive in the current file, under the top-level headline
  2718. \"* Archived Tasks\".
  2719. \"~/org/archive.org::\"
  2720. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2721. \"~/org/archive.org::From %s\"
  2722. Archive in file ~/org/archive.org (absolute path), und headlines
  2723. \"From FILENAME\" where file name is the current file name.
  2724. \"basement::** Finished Tasks\"
  2725. Archive in file ./basement (relative path), as level 3 trees
  2726. below the level 2 heading \"** Finished Tasks\".
  2727. You may set this option on a per-file basis by adding to the buffer a
  2728. line like
  2729. #+ARCHIVE: basement::** Finished Tasks
  2730. You may also define it locally for a subtree by setting an ARCHIVE property
  2731. in the entry. If such a property is found in an entry, or anywhere up
  2732. the hierarchy, it will be used."
  2733. :group 'org-archive
  2734. :type 'string)
  2735. (defcustom org-archive-tag "ARCHIVE"
  2736. "The tag that marks a subtree as archived.
  2737. An archived subtree does not open during visibility cycling, and does
  2738. not contribute to the agenda listings.
  2739. After changing this, font-lock must be restarted in the relevant buffers to
  2740. get the proper fontification."
  2741. :group 'org-archive
  2742. :group 'org-keywords
  2743. :type 'string)
  2744. (defcustom org-agenda-skip-archived-trees t
  2745. "Non-nil means, the agenda will skip any items located in archived trees.
  2746. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2747. variable is no longer recommended, you should leave it at the value t.
  2748. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2749. :group 'org-archive
  2750. :group 'org-agenda-skip
  2751. :type 'boolean)
  2752. (defcustom org-columns-skip-arrchived-trees t
  2753. "Non-nil means, irgnore archived trees when creating column view."
  2754. :group 'org-archive
  2755. :group 'org-properties
  2756. :type 'boolean)
  2757. (defcustom org-cycle-open-archived-trees nil
  2758. "Non-nil means, `org-cycle' will open archived trees.
  2759. An archived tree is a tree marked with the tag ARCHIVE.
  2760. When nil, archived trees will stay folded. You can still open them with
  2761. normal outline commands like `show-all', but not with the cycling commands."
  2762. :group 'org-archive
  2763. :group 'org-cycle
  2764. :type 'boolean)
  2765. (defcustom org-sparse-tree-open-archived-trees nil
  2766. "Non-nil means sparse tree construction shows matches in archived trees.
  2767. When nil, matches in these trees are highlighted, but the trees are kept in
  2768. collapsed state."
  2769. :group 'org-archive
  2770. :group 'org-sparse-trees
  2771. :type 'boolean)
  2772. (defun org-cycle-hide-archived-subtrees (state)
  2773. "Re-hide all archived subtrees after a visibility state change."
  2774. (when (and (not org-cycle-open-archived-trees)
  2775. (not (memq state '(overview folded))))
  2776. (save-excursion
  2777. (let* ((globalp (memq state '(contents all)))
  2778. (beg (if globalp (point-min) (point)))
  2779. (end (if globalp (point-max) (org-end-of-subtree t))))
  2780. (org-hide-archived-subtrees beg end)
  2781. (goto-char beg)
  2782. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2783. (message "%s" (substitute-command-keys
  2784. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2785. (defun org-force-cycle-archived ()
  2786. "Cycle subtree even if it is archived."
  2787. (interactive)
  2788. (setq this-command 'org-cycle)
  2789. (let ((org-cycle-open-archived-trees t))
  2790. (call-interactively 'org-cycle)))
  2791. (defun org-hide-archived-subtrees (beg end)
  2792. "Re-hide all archived subtrees after a visibility state change."
  2793. (save-excursion
  2794. (let* ((re (concat ":" org-archive-tag ":")))
  2795. (goto-char beg)
  2796. (while (re-search-forward re end t)
  2797. (and (org-on-heading-p) (hide-subtree))
  2798. (org-end-of-subtree t)))))
  2799. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2800. (eval-and-compile
  2801. (org-autoload "org-archive"
  2802. '(org-add-archive-files org-archive-subtree
  2803. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2804. ;; Autoload Column View Code
  2805. (declare-function org-columns-number-to-string "org-colview")
  2806. (declare-function org-columns-get-format-and-top-level "org-colview")
  2807. (declare-function org-columns-compute "org-colview")
  2808. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2809. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2810. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2811. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2812. ;; Autoload ID code
  2813. (declare-function org-id-store-link "org-id")
  2814. (declare-function org-id-locations-load "org-id")
  2815. (declare-function org-id-locations-save "org-id")
  2816. (defvar org-id-track-globally)
  2817. (org-autoload "org-id"
  2818. '(org-id-get-create org-id-new org-id-copy org-id-get
  2819. org-id-get-with-outline-path-completion
  2820. org-id-get-with-outline-drilling
  2821. org-id-goto org-id-find org-id-store-link))
  2822. ;; Autoload Plotting Code
  2823. (org-autoload "org-plot"
  2824. '(org-plot/gnuplot))
  2825. ;;; Variables for pre-computed regular expressions, all buffer local
  2826. (defvar org-drawer-regexp nil
  2827. "Matches first line of a hidden block.")
  2828. (make-variable-buffer-local 'org-drawer-regexp)
  2829. (defvar org-todo-regexp nil
  2830. "Matches any of the TODO state keywords.")
  2831. (make-variable-buffer-local 'org-todo-regexp)
  2832. (defvar org-not-done-regexp nil
  2833. "Matches any of the TODO state keywords except the last one.")
  2834. (make-variable-buffer-local 'org-not-done-regexp)
  2835. (defvar org-not-done-heading-regexp nil
  2836. "Matches a TODO headline that is not done.")
  2837. (make-variable-buffer-local 'org-not-done-regexp)
  2838. (defvar org-todo-line-regexp nil
  2839. "Matches a headline and puts TODO state into group 2 if present.")
  2840. (make-variable-buffer-local 'org-todo-line-regexp)
  2841. (defvar org-complex-heading-regexp nil
  2842. "Matches a headline and puts everything into groups:
  2843. group 1: the stars
  2844. group 2: The todo keyword, maybe
  2845. group 3: Priority cookie
  2846. group 4: True headline
  2847. group 5: Tags")
  2848. (make-variable-buffer-local 'org-complex-heading-regexp)
  2849. (defvar org-todo-line-tags-regexp nil
  2850. "Matches a headline and puts TODO state into group 2 if present.
  2851. Also put tags into group 4 if tags are present.")
  2852. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2853. (defvar org-nl-done-regexp nil
  2854. "Matches newline followed by a headline with the DONE keyword.")
  2855. (make-variable-buffer-local 'org-nl-done-regexp)
  2856. (defvar org-looking-at-done-regexp nil
  2857. "Matches the DONE keyword a point.")
  2858. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2859. (defvar org-ds-keyword-length 12
  2860. "Maximum length of the Deadline and SCHEDULED keywords.")
  2861. (make-variable-buffer-local 'org-ds-keyword-length)
  2862. (defvar org-deadline-regexp nil
  2863. "Matches the DEADLINE keyword.")
  2864. (make-variable-buffer-local 'org-deadline-regexp)
  2865. (defvar org-deadline-time-regexp nil
  2866. "Matches the DEADLINE keyword together with a time stamp.")
  2867. (make-variable-buffer-local 'org-deadline-time-regexp)
  2868. (defvar org-deadline-line-regexp nil
  2869. "Matches the DEADLINE keyword and the rest of the line.")
  2870. (make-variable-buffer-local 'org-deadline-line-regexp)
  2871. (defvar org-scheduled-regexp nil
  2872. "Matches the SCHEDULED keyword.")
  2873. (make-variable-buffer-local 'org-scheduled-regexp)
  2874. (defvar org-scheduled-time-regexp nil
  2875. "Matches the SCHEDULED keyword together with a time stamp.")
  2876. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2877. (defvar org-closed-time-regexp nil
  2878. "Matches the CLOSED keyword together with a time stamp.")
  2879. (make-variable-buffer-local 'org-closed-time-regexp)
  2880. (defvar org-keyword-time-regexp nil
  2881. "Matches any of the 4 keywords, together with the time stamp.")
  2882. (make-variable-buffer-local 'org-keyword-time-regexp)
  2883. (defvar org-keyword-time-not-clock-regexp nil
  2884. "Matches any of the 3 keywords, together with the time stamp.")
  2885. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2886. (defvar org-maybe-keyword-time-regexp nil
  2887. "Matches a timestamp, possibly preceeded by a keyword.")
  2888. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2889. (defvar org-planning-or-clock-line-re nil
  2890. "Matches a line with planning or clock info.")
  2891. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2892. (defconst org-plain-time-of-day-regexp
  2893. (concat
  2894. "\\(\\<[012]?[0-9]"
  2895. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2896. "\\(--?"
  2897. "\\(\\<[012]?[0-9]"
  2898. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2899. "\\)?")
  2900. "Regular expression to match a plain time or time range.
  2901. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2902. groups carry important information:
  2903. 0 the full match
  2904. 1 the first time, range or not
  2905. 8 the second time, if it is a range.")
  2906. (defconst org-plain-time-extension-regexp
  2907. (concat
  2908. "\\(\\<[012]?[0-9]"
  2909. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2910. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2911. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2912. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2913. groups carry important information:
  2914. 0 the full match
  2915. 7 hours of duration
  2916. 9 minutes of duration")
  2917. (defconst org-stamp-time-of-day-regexp
  2918. (concat
  2919. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2920. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2921. "\\(--?"
  2922. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2923. "Regular expression to match a timestamp time or time range.
  2924. After a match, the following groups carry important information:
  2925. 0 the full match
  2926. 1 date plus weekday, for backreferencing to make sure both times on same day
  2927. 2 the first time, range or not
  2928. 4 the second time, if it is a range.")
  2929. (defconst org-startup-options
  2930. '(("fold" org-startup-folded t)
  2931. ("overview" org-startup-folded t)
  2932. ("nofold" org-startup-folded nil)
  2933. ("showall" org-startup-folded nil)
  2934. ("content" org-startup-folded content)
  2935. ("hidestars" org-hide-leading-stars t)
  2936. ("showstars" org-hide-leading-stars nil)
  2937. ("odd" org-odd-levels-only t)
  2938. ("oddeven" org-odd-levels-only nil)
  2939. ("align" org-startup-align-all-tables t)
  2940. ("noalign" org-startup-align-all-tables nil)
  2941. ("customtime" org-display-custom-times t)
  2942. ("logdone" org-log-done time)
  2943. ("lognotedone" org-log-done note)
  2944. ("nologdone" org-log-done nil)
  2945. ("lognoteclock-out" org-log-note-clock-out t)
  2946. ("nolognoteclock-out" org-log-note-clock-out nil)
  2947. ("logrepeat" org-log-repeat state)
  2948. ("lognoterepeat" org-log-repeat note)
  2949. ("nologrepeat" org-log-repeat nil)
  2950. ("fninline" org-footnote-define-inline t)
  2951. ("nofninline" org-footnote-define-inline nil)
  2952. ("fnlocal" org-footnote-section nil)
  2953. ("fnauto" org-footnote-auto-label t)
  2954. ("fnprompt" org-footnote-auto-label nil)
  2955. ("fnconfirm" org-footnote-auto-label confirm)
  2956. ("fnplain" org-footnote-auto-label plain)
  2957. ("constcgs" constants-unit-system cgs)
  2958. ("constSI" constants-unit-system SI)
  2959. ("noptag" org-tag-persistent-alist nil))
  2960. "Variable associated with STARTUP options for org-mode.
  2961. Each element is a list of three items: The startup options as written
  2962. in the #+STARTUP line, the corresponding variable, and the value to
  2963. set this variable to if the option is found. An optional forth element PUSH
  2964. means to push this value onto the list in the variable.")
  2965. (defun org-set-regexps-and-options ()
  2966. "Precompute regular expressions for current buffer."
  2967. (when (org-mode-p)
  2968. (org-set-local 'org-todo-kwd-alist nil)
  2969. (org-set-local 'org-todo-key-alist nil)
  2970. (org-set-local 'org-todo-key-trigger nil)
  2971. (org-set-local 'org-todo-keywords-1 nil)
  2972. (org-set-local 'org-done-keywords nil)
  2973. (org-set-local 'org-todo-heads nil)
  2974. (org-set-local 'org-todo-sets nil)
  2975. (org-set-local 'org-todo-log-states nil)
  2976. (org-set-local 'org-file-properties nil)
  2977. (org-set-local 'org-file-tags nil)
  2978. (let ((re (org-make-options-regexp
  2979. '("CATEGORY" "TODO" "COLUMNS"
  2980. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2981. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2982. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2983. (splitre "[ \t]+")
  2984. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2985. tail sep kws1 prio props ftags drawers
  2986. ext-setup-or-nil setup-contents (start 0))
  2987. (save-excursion
  2988. (save-restriction
  2989. (widen)
  2990. (goto-char (point-min))
  2991. (while (or (and ext-setup-or-nil
  2992. (string-match re ext-setup-or-nil start)
  2993. (setq start (match-end 0)))
  2994. (and (setq ext-setup-or-nil nil start 0)
  2995. (re-search-forward re nil t)))
  2996. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2997. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2998. (cond
  2999. ((equal key "CATEGORY")
  3000. (if (string-match "[ \t]+$" value)
  3001. (setq value (replace-match "" t t value)))
  3002. (setq cat value))
  3003. ((member key '("SEQ_TODO" "TODO"))
  3004. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3005. ((equal key "TYP_TODO")
  3006. (push (cons 'type (org-split-string value splitre)) kwds))
  3007. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3008. ;; general TODO-like setup
  3009. (push (cons (intern (downcase (match-string 1 key)))
  3010. (org-split-string value splitre)) kwds))
  3011. ((equal key "TAGS")
  3012. (setq tags (append tags (if tags '("\\n") nil)
  3013. (org-split-string value splitre))))
  3014. ((equal key "COLUMNS")
  3015. (org-set-local 'org-columns-default-format value))
  3016. ((equal key "LINK")
  3017. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3018. (push (cons (match-string 1 value)
  3019. (org-trim (match-string 2 value)))
  3020. links)))
  3021. ((equal key "PRIORITIES")
  3022. (setq prio (org-split-string value " +")))
  3023. ((equal key "PROPERTY")
  3024. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3025. (push (cons (match-string 1 value) (match-string 2 value))
  3026. props)))
  3027. ((equal key "FILETAGS")
  3028. (when (string-match "\\S-" value)
  3029. (setq ftags
  3030. (append
  3031. ftags
  3032. (apply 'append
  3033. (mapcar (lambda (x) (org-split-string x ":"))
  3034. (org-split-string value)))))))
  3035. ((equal key "DRAWERS")
  3036. (setq drawers (org-split-string value splitre)))
  3037. ((equal key "CONSTANTS")
  3038. (setq const (append const (org-split-string value splitre))))
  3039. ((equal key "STARTUP")
  3040. (let ((opts (org-split-string value splitre))
  3041. l var val)
  3042. (while (setq l (pop opts))
  3043. (when (setq l (assoc l org-startup-options))
  3044. (setq var (nth 1 l) val (nth 2 l))
  3045. (if (not (nth 3 l))
  3046. (set (make-local-variable var) val)
  3047. (if (not (listp (symbol-value var)))
  3048. (set (make-local-variable var) nil))
  3049. (set (make-local-variable var) (symbol-value var))
  3050. (add-to-list var val))))))
  3051. ((equal key "ARCHIVE")
  3052. (string-match " *$" value)
  3053. (setq arch (replace-match "" t t value))
  3054. (remove-text-properties 0 (length arch)
  3055. '(face t fontified t) arch))
  3056. ((equal key "SETUPFILE")
  3057. (setq setup-contents (org-file-contents
  3058. (expand-file-name
  3059. (org-remove-double-quotes value))
  3060. 'noerror))
  3061. (if (not ext-setup-or-nil)
  3062. (setq ext-setup-or-nil setup-contents start 0)
  3063. (setq ext-setup-or-nil
  3064. (concat (substring ext-setup-or-nil 0 start)
  3065. "\n" setup-contents "\n"
  3066. (substring ext-setup-or-nil start)))))
  3067. ))))
  3068. (when cat
  3069. (org-set-local 'org-category (intern cat))
  3070. (push (cons "CATEGORY" cat) props))
  3071. (when prio
  3072. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3073. (setq prio (mapcar 'string-to-char prio))
  3074. (org-set-local 'org-highest-priority (nth 0 prio))
  3075. (org-set-local 'org-lowest-priority (nth 1 prio))
  3076. (org-set-local 'org-default-priority (nth 2 prio)))
  3077. (and props (org-set-local 'org-file-properties (nreverse props)))
  3078. (and ftags (org-set-local 'org-file-tags
  3079. (mapcar 'org-add-prop-inherited ftags)))
  3080. (and drawers (org-set-local 'org-drawers drawers))
  3081. (and arch (org-set-local 'org-archive-location arch))
  3082. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3083. ;; Process the TODO keywords
  3084. (unless kwds
  3085. ;; Use the global values as if they had been given locally.
  3086. (setq kwds (default-value 'org-todo-keywords))
  3087. (if (stringp (car kwds))
  3088. (setq kwds (list (cons org-todo-interpretation
  3089. (default-value 'org-todo-keywords)))))
  3090. (setq kwds (reverse kwds)))
  3091. (setq kwds (nreverse kwds))
  3092. (let (inter kws kw)
  3093. (while (setq kws (pop kwds))
  3094. (let ((kws (or
  3095. (run-hook-with-args-until-success
  3096. 'org-todo-setup-filter-hook kws)
  3097. kws)))
  3098. (setq inter (pop kws) sep (member "|" kws)
  3099. kws0 (delete "|" (copy-sequence kws))
  3100. kwsa nil
  3101. kws1 (mapcar
  3102. (lambda (x)
  3103. ;; 1 2
  3104. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3105. (progn
  3106. (setq kw (match-string 1 x)
  3107. key (and (match-end 2) (match-string 2 x))
  3108. log (org-extract-log-state-settings x))
  3109. (push (cons kw (and key (string-to-char key))) kwsa)
  3110. (and log (push log org-todo-log-states))
  3111. kw)
  3112. (error "Invalid TODO keyword %s" x)))
  3113. kws0)
  3114. kwsa (if kwsa (append '((:startgroup))
  3115. (nreverse kwsa)
  3116. '((:endgroup))))
  3117. hw (car kws1)
  3118. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3119. tail (list inter hw (car dws) (org-last dws))))
  3120. (add-to-list 'org-todo-heads hw 'append)
  3121. (push kws1 org-todo-sets)
  3122. (setq org-done-keywords (append org-done-keywords dws nil))
  3123. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3124. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3125. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3126. (setq org-todo-sets (nreverse org-todo-sets)
  3127. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3128. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3129. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3130. ;; Process the constants
  3131. (when const
  3132. (let (e cst)
  3133. (while (setq e (pop const))
  3134. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3135. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3136. (setq org-table-formula-constants-local cst)))
  3137. ;; Process the tags.
  3138. (when tags
  3139. (let (e tgs)
  3140. (while (setq e (pop tags))
  3141. (cond
  3142. ((equal e "{") (push '(:startgroup) tgs))
  3143. ((equal e "}") (push '(:endgroup) tgs))
  3144. ((equal e "\\n") (push '(:newline) tgs))
  3145. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3146. (push (cons (match-string 1 e)
  3147. (string-to-char (match-string 2 e)))
  3148. tgs))
  3149. (t (push (list e) tgs))))
  3150. (org-set-local 'org-tag-alist nil)
  3151. (while (setq e (pop tgs))
  3152. (or (and (stringp (car e))
  3153. (assoc (car e) org-tag-alist))
  3154. (push e org-tag-alist)))))
  3155. ;; Compute the regular expressions and other local variables
  3156. (if (not org-done-keywords)
  3157. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3158. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3159. (length org-scheduled-string)
  3160. (length org-clock-string)
  3161. (length org-closed-string)))
  3162. org-drawer-regexp
  3163. (concat "^[ \t]*:\\("
  3164. (mapconcat 'regexp-quote org-drawers "\\|")
  3165. "\\):[ \t]*$")
  3166. org-not-done-keywords
  3167. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3168. org-todo-regexp
  3169. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3170. "\\|") "\\)\\>")
  3171. org-not-done-regexp
  3172. (concat "\\<\\("
  3173. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3174. "\\)\\>")
  3175. org-not-done-heading-regexp
  3176. (concat "^\\(\\*+\\)[ \t]+\\("
  3177. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3178. "\\)\\>")
  3179. org-todo-line-regexp
  3180. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3181. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3182. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3183. org-complex-heading-regexp
  3184. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3185. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3186. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3187. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3188. org-nl-done-regexp
  3189. (concat "\n\\*+[ \t]+"
  3190. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3191. "\\)" "\\>")
  3192. org-todo-line-tags-regexp
  3193. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3194. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3195. (org-re
  3196. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3197. org-looking-at-done-regexp
  3198. (concat "^" "\\(?:"
  3199. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3200. "\\>")
  3201. org-deadline-regexp (concat "\\<" org-deadline-string)
  3202. org-deadline-time-regexp
  3203. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3204. org-deadline-line-regexp
  3205. (concat "\\<\\(" org-deadline-string "\\).*")
  3206. org-scheduled-regexp
  3207. (concat "\\<" org-scheduled-string)
  3208. org-scheduled-time-regexp
  3209. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3210. org-closed-time-regexp
  3211. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3212. org-keyword-time-regexp
  3213. (concat "\\<\\(" org-scheduled-string
  3214. "\\|" org-deadline-string
  3215. "\\|" org-closed-string
  3216. "\\|" org-clock-string "\\)"
  3217. " *[[<]\\([^]>]+\\)[]>]")
  3218. org-keyword-time-not-clock-regexp
  3219. (concat "\\<\\(" org-scheduled-string
  3220. "\\|" org-deadline-string
  3221. "\\|" org-closed-string
  3222. "\\)"
  3223. " *[[<]\\([^]>]+\\)[]>]")
  3224. org-maybe-keyword-time-regexp
  3225. (concat "\\(\\<\\(" org-scheduled-string
  3226. "\\|" org-deadline-string
  3227. "\\|" org-closed-string
  3228. "\\|" org-clock-string "\\)\\)?"
  3229. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3230. org-planning-or-clock-line-re
  3231. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3232. "\\|" org-deadline-string
  3233. "\\|" org-closed-string "\\|" org-clock-string
  3234. "\\)\\>\\)")
  3235. )
  3236. (org-compute-latex-and-specials-regexp)
  3237. (org-set-font-lock-defaults))))
  3238. (defun org-file-contents (file &optional noerror)
  3239. "Return the contents of FILE, as a string."
  3240. (if (or (not file)
  3241. (not (file-readable-p file)))
  3242. (if noerror
  3243. (progn
  3244. (message "Cannot read file %s" file)
  3245. (ding) (sit-for 2)
  3246. "")
  3247. (error "Cannot read file %s" file))
  3248. (with-temp-buffer
  3249. (insert-file-contents file)
  3250. (buffer-string))))
  3251. (defun org-extract-log-state-settings (x)
  3252. "Extract the log state setting from a TODO keyword string.
  3253. This will extract info from a string like \"WAIT(w@/!)\"."
  3254. (let (kw key log1 log2)
  3255. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3256. (setq kw (match-string 1 x)
  3257. key (and (match-end 2) (match-string 2 x))
  3258. log1 (and (match-end 3) (match-string 3 x))
  3259. log2 (and (match-end 4) (match-string 4 x)))
  3260. (and (or log1 log2)
  3261. (list kw
  3262. (and log1 (if (equal log1 "!") 'time 'note))
  3263. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3264. (defun org-remove-keyword-keys (list)
  3265. "Remove a pair of parenthesis at the end of each string in LIST."
  3266. (mapcar (lambda (x)
  3267. (if (string-match "(.*)$" x)
  3268. (substring x 0 (match-beginning 0))
  3269. x))
  3270. list))
  3271. ;; FIXME: this could be done much better, using second characters etc.
  3272. (defun org-assign-fast-keys (alist)
  3273. "Assign fast keys to a keyword-key alist.
  3274. Respect keys that are already there."
  3275. (let (new e k c c1 c2 (char ?a))
  3276. (while (setq e (pop alist))
  3277. (cond
  3278. ((equal e '(:startgroup)) (push e new))
  3279. ((equal e '(:endgroup)) (push e new))
  3280. ((equal e '(:newline)) (push e new))
  3281. (t
  3282. (setq k (car e) c2 nil)
  3283. (if (cdr e)
  3284. (setq c (cdr e))
  3285. ;; automatically assign a character.
  3286. (setq c1 (string-to-char
  3287. (downcase (substring
  3288. k (if (= (string-to-char k) ?@) 1 0)))))
  3289. (if (or (rassoc c1 new) (rassoc c1 alist))
  3290. (while (or (rassoc char new) (rassoc char alist))
  3291. (setq char (1+ char)))
  3292. (setq c2 c1))
  3293. (setq c (or c2 char)))
  3294. (push (cons k c) new))))
  3295. (nreverse new)))
  3296. ;;; Some variables used in various places
  3297. (defvar org-window-configuration nil
  3298. "Used in various places to store a window configuration.")
  3299. (defvar org-finish-function nil
  3300. "Function to be called when `C-c C-c' is used.
  3301. This is for getting out of special buffers like remember.")
  3302. ;; FIXME: Occasionally check by commenting these, to make sure
  3303. ;; no other functions uses these, forgetting to let-bind them.
  3304. (defvar entry)
  3305. (defvar state)
  3306. (defvar last-state)
  3307. (defvar date)
  3308. (defvar description)
  3309. ;; Defined somewhere in this file, but used before definition.
  3310. (defvar org-html-entities)
  3311. (defvar org-struct-menu)
  3312. (defvar org-org-menu)
  3313. (defvar org-tbl-menu)
  3314. (defvar org-agenda-keymap)
  3315. ;;;; Define the Org-mode
  3316. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3317. (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."))
  3318. ;; We use a before-change function to check if a table might need
  3319. ;; an update.
  3320. (defvar org-table-may-need-update t
  3321. "Indicates that a table might need an update.
  3322. This variable is set by `org-before-change-function'.
  3323. `org-table-align' sets it back to nil.")
  3324. (defun org-before-change-function (beg end)
  3325. "Every change indicates that a table might need an update."
  3326. (setq org-table-may-need-update t))
  3327. (defvar org-mode-map)
  3328. (defvar org-mode-hook nil
  3329. "Mode hook for Org-mode, run after the mode was turned on.")
  3330. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3331. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3332. (defvar org-table-buffer-is-an nil)
  3333. (defconst org-outline-regexp "\\*+ ")
  3334. ;;;###autoload
  3335. (define-derived-mode org-mode outline-mode "Org"
  3336. "Outline-based notes management and organizer, alias
  3337. \"Carsten's outline-mode for keeping track of everything.\"
  3338. Org-mode develops organizational tasks around a NOTES file which
  3339. contains information about projects as plain text. Org-mode is
  3340. implemented on top of outline-mode, which is ideal to keep the content
  3341. of large files well structured. It supports ToDo items, deadlines and
  3342. time stamps, which magically appear in the diary listing of the Emacs
  3343. calendar. Tables are easily created with a built-in table editor.
  3344. Plain text URL-like links connect to websites, emails (VM), Usenet
  3345. messages (Gnus), BBDB entries, and any files related to the project.
  3346. For printing and sharing of notes, an Org-mode file (or a part of it)
  3347. can be exported as a structured ASCII or HTML file.
  3348. The following commands are available:
  3349. \\{org-mode-map}"
  3350. ;; Get rid of Outline menus, they are not needed
  3351. ;; Need to do this here because define-derived-mode sets up
  3352. ;; the keymap so late. Still, it is a waste to call this each time
  3353. ;; we switch another buffer into org-mode.
  3354. (if (featurep 'xemacs)
  3355. (when (boundp 'outline-mode-menu-heading)
  3356. ;; Assume this is Greg's port, it used easymenu
  3357. (easy-menu-remove outline-mode-menu-heading)
  3358. (easy-menu-remove outline-mode-menu-show)
  3359. (easy-menu-remove outline-mode-menu-hide))
  3360. (define-key org-mode-map [menu-bar headings] 'undefined)
  3361. (define-key org-mode-map [menu-bar hide] 'undefined)
  3362. (define-key org-mode-map [menu-bar show] 'undefined))
  3363. (org-load-modules-maybe)
  3364. (easy-menu-add org-org-menu)
  3365. (easy-menu-add org-tbl-menu)
  3366. (org-install-agenda-files-menu)
  3367. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3368. (org-add-to-invisibility-spec '(org-cwidth))
  3369. (when (featurep 'xemacs)
  3370. (org-set-local 'line-move-ignore-invisible t))
  3371. (org-set-local 'outline-regexp org-outline-regexp)
  3372. (org-set-local 'outline-level 'org-outline-level)
  3373. (when (and org-ellipsis
  3374. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3375. (fboundp 'make-glyph-code))
  3376. (unless org-display-table
  3377. (setq org-display-table (make-display-table)))
  3378. (set-display-table-slot
  3379. org-display-table 4
  3380. (vconcat (mapcar
  3381. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3382. org-ellipsis)))
  3383. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3384. (setq buffer-display-table org-display-table))
  3385. (org-set-regexps-and-options)
  3386. (when (and org-tag-faces (not org-tags-special-faces-re))
  3387. ;; tag faces set outside customize.... force initialization.
  3388. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3389. ;; Calc embedded
  3390. (org-set-local 'calc-embedded-open-mode "# ")
  3391. (modify-syntax-entry ?# "<")
  3392. (modify-syntax-entry ?@ "w")
  3393. (if org-startup-truncated (setq truncate-lines t))
  3394. (org-set-local 'font-lock-unfontify-region-function
  3395. 'org-unfontify-region)
  3396. ;; Activate before-change-function
  3397. (org-set-local 'org-table-may-need-update t)
  3398. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3399. 'local)
  3400. ;; Check for running clock before killing a buffer
  3401. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3402. ;; Paragraphs and auto-filling
  3403. (org-set-autofill-regexps)
  3404. (setq indent-line-function 'org-indent-line-function)
  3405. (org-update-radio-target-regexp)
  3406. ;; Make sure dependence stuff works reliably, even for users who set it
  3407. ;; too late :-(
  3408. (if org-enforce-todo-dependencies
  3409. (add-hook 'org-blocker-hook
  3410. 'org-block-todo-from-children-or-siblings-or-parent)
  3411. (remove-hook 'org-blocker-hook
  3412. 'org-block-todo-from-children-or-siblings-or-parent))
  3413. (if org-enforce-todo-checkbox-dependencies
  3414. (add-hook 'org-blocker-hook
  3415. 'org-block-todo-from-checkboxes)
  3416. (remove-hook 'org-blocker-hook
  3417. 'org-block-todo-from-checkboxes))
  3418. ;; Comment characters
  3419. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3420. (org-set-local 'comment-padding " ")
  3421. ;; Align options lines
  3422. (org-set-local
  3423. 'align-mode-rules-list
  3424. '((org-in-buffer-settings
  3425. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3426. (modes . '(org-mode)))))
  3427. ;; Imenu
  3428. (org-set-local 'imenu-create-index-function
  3429. 'org-imenu-get-tree)
  3430. ;; Make isearch reveal context
  3431. (if (or (featurep 'xemacs)
  3432. (not (boundp 'outline-isearch-open-invisible-function)))
  3433. ;; Emacs 21 and XEmacs make use of the hook
  3434. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3435. ;; Emacs 22 deals with this through a special variable
  3436. (org-set-local 'outline-isearch-open-invisible-function
  3437. (lambda (&rest ignore) (org-show-context 'isearch))))
  3438. ;; If empty file that did not turn on org-mode automatically, make it to.
  3439. (if (and org-insert-mode-line-in-empty-file
  3440. (interactive-p)
  3441. (= (point-min) (point-max)))
  3442. (insert "# -*- mode: org -*-\n\n"))
  3443. (unless org-inhibit-startup
  3444. (when org-startup-align-all-tables
  3445. (let ((bmp (buffer-modified-p)))
  3446. (org-table-map-tables 'org-table-align)
  3447. (set-buffer-modified-p bmp)))
  3448. (org-set-startup-visibility)))
  3449. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3450. (defun org-current-time ()
  3451. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3452. (if (> (car org-time-stamp-rounding-minutes) 1)
  3453. (let ((r (car org-time-stamp-rounding-minutes))
  3454. (time (decode-time)))
  3455. (apply 'encode-time
  3456. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3457. (nthcdr 2 time))))
  3458. (current-time)))
  3459. ;;;; Font-Lock stuff, including the activators
  3460. (defvar org-mouse-map (make-sparse-keymap))
  3461. (org-defkey org-mouse-map
  3462. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3463. (org-defkey org-mouse-map
  3464. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3465. (when org-mouse-1-follows-link
  3466. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3467. (when org-tab-follows-link
  3468. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3469. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3470. (require 'font-lock)
  3471. (defconst org-non-link-chars "]\t\n\r<>")
  3472. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3473. "shell" "elisp"))
  3474. (defvar org-link-types-re nil
  3475. "Matches a link that has a url-like prefix like \"http:\"")
  3476. (defvar org-link-re-with-space nil
  3477. "Matches a link with spaces, optional angular brackets around it.")
  3478. (defvar org-link-re-with-space2 nil
  3479. "Matches a link with spaces, optional angular brackets around it.")
  3480. (defvar org-link-re-with-space3 nil
  3481. "Matches a link with spaces, only for internal part in bracket links.")
  3482. (defvar org-angle-link-re nil
  3483. "Matches link with angular brackets, spaces are allowed.")
  3484. (defvar org-plain-link-re nil
  3485. "Matches plain link, without spaces.")
  3486. (defvar org-bracket-link-regexp nil
  3487. "Matches a link in double brackets.")
  3488. (defvar org-bracket-link-analytic-regexp nil
  3489. "Regular expression used to analyze links.
  3490. Here is what the match groups contain after a match:
  3491. 1: http:
  3492. 2: http
  3493. 3: path
  3494. 4: [desc]
  3495. 5: desc")
  3496. (defvar org-bracket-link-analytic-regexp++ nil
  3497. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3498. (defvar org-any-link-re nil
  3499. "Regular expression matching any link.")
  3500. (defun org-make-link-regexps ()
  3501. "Update the link regular expressions.
  3502. This should be called after the variable `org-link-types' has changed."
  3503. (setq org-link-types-re
  3504. (concat
  3505. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3506. org-link-re-with-space
  3507. (concat
  3508. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3509. "\\([^" org-non-link-chars " ]"
  3510. "[^" org-non-link-chars "]*"
  3511. "[^" org-non-link-chars " ]\\)>?")
  3512. org-link-re-with-space2
  3513. (concat
  3514. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3515. "\\([^" org-non-link-chars " ]"
  3516. "[^\t\n\r]*"
  3517. "[^" org-non-link-chars " ]\\)>?")
  3518. org-link-re-with-space3
  3519. (concat
  3520. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3521. "\\([^" org-non-link-chars " ]"
  3522. "[^\t\n\r]*\\)")
  3523. org-angle-link-re
  3524. (concat
  3525. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3526. "\\([^" org-non-link-chars " ]"
  3527. "[^" org-non-link-chars "]*"
  3528. "\\)>")
  3529. org-plain-link-re
  3530. (concat
  3531. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3532. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3533. org-bracket-link-regexp
  3534. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3535. org-bracket-link-analytic-regexp
  3536. (concat
  3537. "\\[\\["
  3538. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3539. "\\([^]]+\\)"
  3540. "\\]"
  3541. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3542. "\\]")
  3543. org-bracket-link-analytic-regexp++
  3544. (concat
  3545. "\\[\\["
  3546. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3547. "\\([^]]+\\)"
  3548. "\\]"
  3549. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3550. "\\]")
  3551. org-any-link-re
  3552. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3553. org-angle-link-re "\\)\\|\\("
  3554. org-plain-link-re "\\)")))
  3555. (org-make-link-regexps)
  3556. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3557. "Regular expression for fast time stamp matching.")
  3558. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3559. "Regular expression for fast time stamp matching.")
  3560. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3561. "Regular expression matching time strings for analysis.
  3562. This one does not require the space after the date, so it can be used
  3563. on a string that terminates immediately after the date.")
  3564. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3565. "Regular expression matching time strings for analysis.")
  3566. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3567. "Regular expression matching time stamps, with groups.")
  3568. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3569. "Regular expression matching time stamps (also [..]), with groups.")
  3570. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3571. "Regular expression matching a time stamp range.")
  3572. (defconst org-tr-regexp-both
  3573. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3574. "Regular expression matching a time stamp range.")
  3575. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3576. org-ts-regexp "\\)?")
  3577. "Regular expression matching a time stamp or time stamp range.")
  3578. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3579. org-ts-regexp-both "\\)?")
  3580. "Regular expression matching a time stamp or time stamp range.
  3581. The time stamps may be either active or inactive.")
  3582. (defvar org-emph-face nil)
  3583. (defun org-do-emphasis-faces (limit)
  3584. "Run through the buffer and add overlays to links."
  3585. (let (rtn)
  3586. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3587. (if (not (= (char-after (match-beginning 3))
  3588. (char-after (match-beginning 4))))
  3589. (progn
  3590. (setq rtn t)
  3591. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3592. 'face
  3593. (nth 1 (assoc (match-string 3)
  3594. org-emphasis-alist)))
  3595. (add-text-properties (match-beginning 2) (match-end 2)
  3596. '(font-lock-multiline t))
  3597. (when org-hide-emphasis-markers
  3598. (add-text-properties (match-end 4) (match-beginning 5)
  3599. '(invisible org-link))
  3600. (add-text-properties (match-beginning 3) (match-end 3)
  3601. '(invisible org-link)))))
  3602. (backward-char 1))
  3603. rtn))
  3604. (defun org-emphasize (&optional char)
  3605. "Insert or change an emphasis, i.e. a font like bold or italic.
  3606. If there is an active region, change that region to a new emphasis.
  3607. If there is no region, just insert the marker characters and position
  3608. the cursor between them.
  3609. CHAR should be either the marker character, or the first character of the
  3610. HTML tag associated with that emphasis. If CHAR is a space, the means
  3611. to remove the emphasis of the selected region.
  3612. If char is not given (for example in an interactive call) it
  3613. will be prompted for."
  3614. (interactive)
  3615. (let ((eal org-emphasis-alist) e det
  3616. (erc org-emphasis-regexp-components)
  3617. (prompt "")
  3618. (string "") beg end move tag c s)
  3619. (if (org-region-active-p)
  3620. (setq beg (region-beginning) end (region-end)
  3621. string (buffer-substring beg end))
  3622. (setq move t))
  3623. (while (setq e (pop eal))
  3624. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3625. c (aref tag 0))
  3626. (push (cons c (string-to-char (car e))) det)
  3627. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3628. (substring tag 1)))))
  3629. (setq det (nreverse det))
  3630. (unless char
  3631. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3632. (setq char (read-char-exclusive)))
  3633. (setq char (or (cdr (assoc char det)) char))
  3634. (if (equal char ?\ )
  3635. (setq s "" move nil)
  3636. (unless (assoc (char-to-string char) org-emphasis-alist)
  3637. (error "No such emphasis marker: \"%c\"" char))
  3638. (setq s (char-to-string char)))
  3639. (while (and (> (length string) 1)
  3640. (equal (substring string 0 1) (substring string -1))
  3641. (assoc (substring string 0 1) org-emphasis-alist))
  3642. (setq string (substring string 1 -1)))
  3643. (setq string (concat s string s))
  3644. (if beg (delete-region beg end))
  3645. (unless (or (bolp)
  3646. (string-match (concat "[" (nth 0 erc) "\n]")
  3647. (char-to-string (char-before (point)))))
  3648. (insert " "))
  3649. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3650. (char-to-string (char-after (point))))
  3651. (insert " ") (backward-char 1))
  3652. (insert string)
  3653. (and move (backward-char 1))))
  3654. (defconst org-nonsticky-props
  3655. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3656. (defsubst org-rear-nonsticky-at (pos)
  3657. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3658. (defun org-activate-plain-links (limit)
  3659. "Run through the buffer and add overlays to links."
  3660. (catch 'exit
  3661. (let (f)
  3662. (while (re-search-forward org-plain-link-re limit t)
  3663. (setq f (get-text-property (match-beginning 0) 'face))
  3664. (if (or (eq f 'org-tag)
  3665. (and (listp f) (memq 'org-tag f)))
  3666. nil
  3667. (add-text-properties (match-beginning 0) (match-end 0)
  3668. (list 'mouse-face 'highlight
  3669. 'keymap org-mouse-map))
  3670. (org-rear-nonsticky-at (match-end 0))
  3671. (throw 'exit t))))))
  3672. (defun org-activate-code (limit)
  3673. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3674. (progn
  3675. (remove-text-properties (match-beginning 0) (match-end 0)
  3676. '(display t invisible t intangible t))
  3677. t)))
  3678. (defun org-activate-angle-links (limit)
  3679. "Run through the buffer and add overlays to links."
  3680. (if (re-search-forward org-angle-link-re limit t)
  3681. (progn
  3682. (add-text-properties (match-beginning 0) (match-end 0)
  3683. (list 'mouse-face 'highlight
  3684. 'keymap org-mouse-map))
  3685. (org-rear-nonsticky-at (match-end 0))
  3686. t)))
  3687. (defun org-activate-footnote-links (limit)
  3688. "Run through the buffer and add overlays to links."
  3689. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3690. limit t)
  3691. (progn
  3692. (add-text-properties (match-beginning 2) (match-end 2)
  3693. (list 'mouse-face 'highlight
  3694. 'keymap org-mouse-map
  3695. 'help-echo
  3696. (if (= (point-at-bol) (match-beginning 2))
  3697. "Footnote definition"
  3698. "Footnote reference")
  3699. ))
  3700. (org-rear-nonsticky-at (match-end 2))
  3701. t)))
  3702. (defun org-activate-bracket-links (limit)
  3703. "Run through the buffer and add overlays to bracketed links."
  3704. (if (re-search-forward org-bracket-link-regexp limit t)
  3705. (let* ((help (concat "LINK: "
  3706. (org-match-string-no-properties 1)))
  3707. ;; FIXME: above we should remove the escapes.
  3708. ;; but that requires another match, protecting match data,
  3709. ;; a lot of overhead for font-lock.
  3710. (ip (org-maybe-intangible
  3711. (list 'invisible 'org-link
  3712. 'keymap org-mouse-map 'mouse-face 'highlight
  3713. 'font-lock-multiline t 'help-echo help)))
  3714. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3715. 'font-lock-multiline t 'help-echo help)))
  3716. ;; We need to remove the invisible property here. Table narrowing
  3717. ;; may have made some of this invisible.
  3718. (remove-text-properties (match-beginning 0) (match-end 0)
  3719. '(invisible nil))
  3720. (if (match-end 3)
  3721. (progn
  3722. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3723. (org-rear-nonsticky-at (match-beginning 3))
  3724. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3725. (org-rear-nonsticky-at (match-end 3))
  3726. (add-text-properties (match-end 3) (match-end 0) ip)
  3727. (org-rear-nonsticky-at (match-end 0)))
  3728. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3729. (org-rear-nonsticky-at (match-beginning 1))
  3730. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3731. (org-rear-nonsticky-at (match-end 1))
  3732. (add-text-properties (match-end 1) (match-end 0) ip)
  3733. (org-rear-nonsticky-at (match-end 0)))
  3734. t)))
  3735. (defun org-activate-dates (limit)
  3736. "Run through the buffer and add overlays to dates."
  3737. (if (re-search-forward org-tsr-regexp-both limit t)
  3738. (progn
  3739. (add-text-properties (match-beginning 0) (match-end 0)
  3740. (list 'mouse-face 'highlight
  3741. 'keymap org-mouse-map))
  3742. (org-rear-nonsticky-at (match-end 0))
  3743. (when org-display-custom-times
  3744. (if (match-end 3)
  3745. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3746. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3747. t)))
  3748. (defvar org-target-link-regexp nil
  3749. "Regular expression matching radio targets in plain text.")
  3750. (make-variable-buffer-local 'org-target-link-regexp)
  3751. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3752. "Regular expression matching a link target.")
  3753. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3754. "Regular expression matching a radio target.")
  3755. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3756. "Regular expression matching any target.")
  3757. (defun org-activate-target-links (limit)
  3758. "Run through the buffer and add overlays to target matches."
  3759. (when org-target-link-regexp
  3760. (let ((case-fold-search t))
  3761. (if (re-search-forward org-target-link-regexp limit t)
  3762. (progn
  3763. (add-text-properties (match-beginning 0) (match-end 0)
  3764. (list 'mouse-face 'highlight
  3765. 'keymap org-mouse-map
  3766. 'help-echo "Radio target link"
  3767. 'org-linked-text t))
  3768. (org-rear-nonsticky-at (match-end 0))
  3769. t)))))
  3770. (defun org-update-radio-target-regexp ()
  3771. "Find all radio targets in this file and update the regular expression."
  3772. (interactive)
  3773. (when (memq 'radio org-activate-links)
  3774. (setq org-target-link-regexp
  3775. (org-make-target-link-regexp (org-all-targets 'radio)))
  3776. (org-restart-font-lock)))
  3777. (defun org-hide-wide-columns (limit)
  3778. (let (s e)
  3779. (setq s (text-property-any (point) (or limit (point-max))
  3780. 'org-cwidth t))
  3781. (when s
  3782. (setq e (next-single-property-change s 'org-cwidth))
  3783. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3784. (goto-char e)
  3785. t)))
  3786. (defvar org-latex-and-specials-regexp nil
  3787. "Regular expression for highlighting export special stuff.")
  3788. (defvar org-match-substring-regexp)
  3789. (defvar org-match-substring-with-braces-regexp)
  3790. (defvar org-export-html-special-string-regexps)
  3791. (defun org-compute-latex-and-specials-regexp ()
  3792. "Compute regular expression for stuff treated specially by exporters."
  3793. (if (not org-highlight-latex-fragments-and-specials)
  3794. (org-set-local 'org-latex-and-specials-regexp nil)
  3795. (require 'org-exp)
  3796. (let*
  3797. ((matchers (plist-get org-format-latex-options :matchers))
  3798. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3799. org-latex-regexps)))
  3800. (options (org-combine-plists (org-default-export-plist)
  3801. (org-infile-export-plist)))
  3802. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3803. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3804. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3805. (org-export-html-expand (plist-get options :expand-quoted-html))
  3806. (org-export-with-special-strings (plist-get options :special-strings))
  3807. (re-sub
  3808. (cond
  3809. ((equal org-export-with-sub-superscripts '{})
  3810. (list org-match-substring-with-braces-regexp))
  3811. (org-export-with-sub-superscripts
  3812. (list org-match-substring-regexp))
  3813. (t nil)))
  3814. (re-latex
  3815. (if org-export-with-LaTeX-fragments
  3816. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3817. (re-macros
  3818. (if org-export-with-TeX-macros
  3819. (list (concat "\\\\"
  3820. (regexp-opt
  3821. (append (mapcar 'car org-html-entities)
  3822. (if (boundp 'org-latex-entities)
  3823. org-latex-entities nil))
  3824. 'words))) ; FIXME
  3825. ))
  3826. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3827. (re-special (if org-export-with-special-strings
  3828. (mapcar (lambda (x) (car x))
  3829. org-export-html-special-string-regexps)))
  3830. (re-rest
  3831. (delq nil
  3832. (list
  3833. (if org-export-html-expand "@<[^>\n]+>")
  3834. ))))
  3835. (org-set-local
  3836. 'org-latex-and-specials-regexp
  3837. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3838. re-rest) "\\|")))))
  3839. (defun org-do-latex-and-special-faces (limit)
  3840. "Run through the buffer and add overlays to links."
  3841. (when org-latex-and-specials-regexp
  3842. (let (rtn d)
  3843. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3844. limit t))
  3845. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3846. 'face))
  3847. '(org-code org-verbatim underline)))
  3848. (progn
  3849. (setq rtn t
  3850. d (cond ((member (char-after (1+ (match-beginning 0)))
  3851. '(?_ ?^)) 1)
  3852. (t 0)))
  3853. (font-lock-prepend-text-property
  3854. (+ d (match-beginning 0)) (match-end 0)
  3855. 'face 'org-latex-and-export-specials)
  3856. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3857. '(font-lock-multiline t)))))
  3858. rtn)))
  3859. (defun org-restart-font-lock ()
  3860. "Restart font-lock-mode, to force refontification."
  3861. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3862. (font-lock-mode -1)
  3863. (font-lock-mode 1)))
  3864. (defun org-all-targets (&optional radio)
  3865. "Return a list of all targets in this file.
  3866. With optional argument RADIO, only find radio targets."
  3867. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3868. rtn)
  3869. (save-excursion
  3870. (goto-char (point-min))
  3871. (while (re-search-forward re nil t)
  3872. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3873. rtn)))
  3874. (defun org-make-target-link-regexp (targets)
  3875. "Make regular expression matching all strings in TARGETS.
  3876. The regular expression finds the targets also if there is a line break
  3877. between words."
  3878. (and targets
  3879. (concat
  3880. "\\<\\("
  3881. (mapconcat
  3882. (lambda (x)
  3883. (while (string-match " +" x)
  3884. (setq x (replace-match "\\s-+" t t x)))
  3885. x)
  3886. targets
  3887. "\\|")
  3888. "\\)\\>")))
  3889. (defun org-activate-tags (limit)
  3890. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3891. (progn
  3892. (add-text-properties (match-beginning 1) (match-end 1)
  3893. (list 'mouse-face 'highlight
  3894. 'keymap org-mouse-map))
  3895. (org-rear-nonsticky-at (match-end 1))
  3896. t)))
  3897. (defun org-outline-level ()
  3898. (save-excursion
  3899. (looking-at outline-regexp)
  3900. (if (match-beginning 1)
  3901. (+ (org-get-string-indentation (match-string 1)) 1000)
  3902. (1- (- (match-end 0) (match-beginning 0))))))
  3903. (defvar org-font-lock-keywords nil)
  3904. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3905. "Regular expression matching a property line.")
  3906. (defvar org-font-lock-hook nil
  3907. "Functions to be called for special font lock stuff.")
  3908. (defun org-font-lock-hook (limit)
  3909. (run-hook-with-args 'org-font-lock-hook limit))
  3910. (defun org-set-font-lock-defaults ()
  3911. (let* ((em org-fontify-emphasized-text)
  3912. (lk org-activate-links)
  3913. (org-font-lock-extra-keywords
  3914. (list
  3915. ;; Call the hook
  3916. '(org-font-lock-hook)
  3917. ;; Headlines
  3918. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3919. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3920. ;; Table lines
  3921. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3922. (1 'org-table t))
  3923. ;; Table internals
  3924. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3925. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3926. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3927. ;; Drawers
  3928. (list org-drawer-regexp '(0 'org-special-keyword t))
  3929. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3930. ;; Properties
  3931. (list org-property-re
  3932. '(1 'org-special-keyword t)
  3933. '(3 'org-property-value t))
  3934. (if org-format-transports-properties-p
  3935. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3936. ;; Links
  3937. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3938. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3939. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3940. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3941. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3942. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3943. (if (memq 'footnote lk) '(org-activate-footnote-links
  3944. (2 'org-footnote t)))
  3945. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3946. '(org-hide-wide-columns (0 nil append))
  3947. ;; TODO lines
  3948. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3949. '(1 (org-get-todo-face 1) t))
  3950. ;; DONE
  3951. (if org-fontify-done-headline
  3952. (list (concat "^[*]+ +\\<\\("
  3953. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3954. "\\)\\(.*\\)")
  3955. '(2 'org-headline-done t))
  3956. nil)
  3957. ;; Priorities
  3958. '(org-font-lock-add-priority-faces)
  3959. ;; Tags
  3960. '(org-font-lock-add-tag-faces)
  3961. ;; Special keywords
  3962. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3963. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3964. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3965. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3966. ;; Emphasis
  3967. (if em
  3968. (if (featurep 'xemacs)
  3969. '(org-do-emphasis-faces (0 nil append))
  3970. '(org-do-emphasis-faces)))
  3971. ;; Checkboxes
  3972. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3973. 2 'org-checkbox prepend)
  3974. (if org-provide-checkbox-statistics
  3975. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3976. (0 (org-get-checkbox-statistics-face) t)))
  3977. ;; Description list items
  3978. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3979. 2 'bold prepend)
  3980. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3981. '(1 'org-archived prepend))
  3982. ;; Specials
  3983. '(org-do-latex-and-special-faces)
  3984. ;; Code
  3985. '(org-activate-code (1 'org-code t))
  3986. ;; COMMENT
  3987. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3988. "\\|" org-quote-string "\\)\\>")
  3989. '(1 'org-special-keyword t))
  3990. '("^#.*" (0 'font-lock-comment-face t))
  3991. )))
  3992. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3993. ;; Now set the full font-lock-keywords
  3994. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3995. (org-set-local 'font-lock-defaults
  3996. '(org-font-lock-keywords t nil nil backward-paragraph))
  3997. (kill-local-variable 'font-lock-keywords) nil))
  3998. (defvar org-m nil)
  3999. (defvar org-l nil)
  4000. (defvar org-f nil)
  4001. (defun org-get-level-face (n)
  4002. "Get the right face for match N in font-lock matching of headlines."
  4003. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4004. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4005. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4006. (cond
  4007. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4008. ((eq n 2) org-f)
  4009. (t (if org-level-color-stars-only nil org-f))))
  4010. (defun org-get-todo-face (kwd)
  4011. "Get the right face for a TODO keyword KWD.
  4012. If KWD is a number, get the corresponding match group."
  4013. (if (numberp kwd) (setq kwd (match-string kwd)))
  4014. (or (cdr (assoc kwd org-todo-keyword-faces))
  4015. (and (member kwd org-done-keywords) 'org-done)
  4016. 'org-todo))
  4017. (defun org-font-lock-add-tag-faces (limit)
  4018. "Add the special tag faces."
  4019. (when (and org-tag-faces org-tags-special-faces-re)
  4020. (while (re-search-forward org-tags-special-faces-re limit t)
  4021. (add-text-properties (match-beginning 1) (match-end 1)
  4022. (list 'face (org-get-tag-face 1)
  4023. 'font-lock-fontified t))
  4024. (backward-char 1))))
  4025. (defun org-font-lock-add-priority-faces (limit)
  4026. "Add the special priority faces."
  4027. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4028. (add-text-properties
  4029. (match-beginning 0) (match-end 0)
  4030. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4031. org-priority-faces))
  4032. 'org-special-keyword)
  4033. 'font-lock-fontified t))))
  4034. (defun org-get-tag-face (kwd)
  4035. "Get the right face for a TODO keyword KWD.
  4036. If KWD is a number, get the corresponding match group."
  4037. (if (numberp kwd) (setq kwd (match-string kwd)))
  4038. (or (cdr (assoc kwd org-tag-faces))
  4039. 'org-tag))
  4040. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4041. "Remove fontification and activation overlays from links."
  4042. (font-lock-default-unfontify-region beg end)
  4043. (let* ((buffer-undo-list t)
  4044. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4045. (inhibit-modification-hooks t)
  4046. deactivate-mark buffer-file-name buffer-file-truename)
  4047. (remove-text-properties beg end
  4048. '(mouse-face t keymap t org-linked-text t
  4049. invisible t intangible t))))
  4050. ;;;; Visibility cycling, including org-goto and indirect buffer
  4051. ;;; Cycling
  4052. (defvar org-cycle-global-status nil)
  4053. (make-variable-buffer-local 'org-cycle-global-status)
  4054. (defvar org-cycle-subtree-status nil)
  4055. (make-variable-buffer-local 'org-cycle-subtree-status)
  4056. ;;;###autoload
  4057. (defvar org-inlinetask-min-level)
  4058. (defun org-cycle (&optional arg)
  4059. "Visibility cycling for Org-mode.
  4060. - When this function is called with a prefix argument, rotate the entire
  4061. buffer through 3 states (global cycling)
  4062. 1. OVERVIEW: Show only top-level headlines.
  4063. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4064. 3. SHOW ALL: Show everything.
  4065. When called with two C-u C-u prefixes, switch to the startup visibility,
  4066. determined by the variable `org-startup-folded', and by any VISIBILITY
  4067. properties in the buffer.
  4068. When called with three C-u C-u C-u prefixed, show the entire buffer,
  4069. including drawers.
  4070. - When point is at the beginning of a headline, rotate the subtree started
  4071. by this line through 3 different states (local cycling)
  4072. 1. FOLDED: Only the main headline is shown.
  4073. 2. CHILDREN: The main headline and the direct children are shown.
  4074. From this state, you can move to one of the children
  4075. and zoom in further.
  4076. 3. SUBTREE: Show the entire subtree, including body text.
  4077. - When there is a numeric prefix, go up to a heading with level ARG, do
  4078. a `show-subtree' and return to the previous cursor position. If ARG
  4079. is negative, go up that many levels.
  4080. - When point is not at the beginning of a headline, execute the global
  4081. binding for TAB, which is re-indenting the line. See the option
  4082. `org-cycle-emulate-tab' for details.
  4083. - Special case: if point is at the beginning of the buffer and there is
  4084. no headline in line 1, this function will act as if called with prefix arg.
  4085. But only if also the variable `org-cycle-global-at-bob' is t."
  4086. (interactive "P")
  4087. (org-load-modules-maybe)
  4088. (let* ((limit-level
  4089. (or org-cycle-max-level
  4090. (and (boundp 'org-inlinetask-min-level)
  4091. org-inlinetask-min-level
  4092. (1- org-inlinetask-min-level))))
  4093. (nstars (and limit-level
  4094. (if org-odd-levels-only
  4095. (and limit-level (1- (* limit-level 2)))
  4096. limit-level)))
  4097. (outline-regexp
  4098. (cond
  4099. ((not (org-mode-p)) outline-regexp)
  4100. (org-cycle-include-plain-lists
  4101. (concat "\\(?:\\*"
  4102. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4103. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4104. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4105. (bob-special (and org-cycle-global-at-bob (bobp)
  4106. (not (looking-at outline-regexp))))
  4107. (org-cycle-hook
  4108. (if bob-special
  4109. (delq 'org-optimize-window-after-visibility-change
  4110. (copy-sequence org-cycle-hook))
  4111. org-cycle-hook))
  4112. (pos (point)))
  4113. (if (or bob-special (equal arg '(4)))
  4114. ;; special case: use global cycling
  4115. (setq arg t))
  4116. (cond
  4117. ((equal arg '(16))
  4118. (org-set-startup-visibility)
  4119. (message "Startup visibility, plus VISIBILITY properties"))
  4120. ((equal arg '(64))
  4121. (show-all)
  4122. (message "Entire buffer visible, including drawers"))
  4123. ((org-at-table-p 'any)
  4124. ;; Enter the table or move to the next field in the table
  4125. (or (org-table-recognize-table.el)
  4126. (progn
  4127. (if arg (org-table-edit-field t)
  4128. (org-table-justify-field-maybe)
  4129. (call-interactively 'org-table-next-field)))))
  4130. ((eq arg t) ;; Global cycling
  4131. (cond
  4132. ((and (eq last-command this-command)
  4133. (eq org-cycle-global-status 'overview))
  4134. ;; We just created the overview - now do table of contents
  4135. ;; This can be slow in very large buffers, so indicate action
  4136. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4137. (message "CONTENTS...")
  4138. (org-content)
  4139. (message "CONTENTS...done")
  4140. (setq org-cycle-global-status 'contents)
  4141. (run-hook-with-args 'org-cycle-hook 'contents))
  4142. ((and (eq last-command this-command)
  4143. (eq org-cycle-global-status 'contents))
  4144. ;; We just showed the table of contents - now show everything
  4145. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4146. (show-all)
  4147. (message "SHOW ALL")
  4148. (setq org-cycle-global-status 'all)
  4149. (run-hook-with-args 'org-cycle-hook 'all))
  4150. (t
  4151. ;; Default action: go to overview
  4152. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4153. (org-overview)
  4154. (message "OVERVIEW")
  4155. (setq org-cycle-global-status 'overview)
  4156. (run-hook-with-args 'org-cycle-hook 'overview))))
  4157. ((and org-drawers org-drawer-regexp
  4158. (save-excursion
  4159. (beginning-of-line 1)
  4160. (looking-at org-drawer-regexp)))
  4161. ;; Toggle block visibility
  4162. (org-flag-drawer
  4163. (not (get-char-property (match-end 0) 'invisible))))
  4164. ((integerp arg)
  4165. ;; Show-subtree, ARG levels up from here.
  4166. (save-excursion
  4167. (org-back-to-heading)
  4168. (outline-up-heading (if (< arg 0) (- arg)
  4169. (- (funcall outline-level) arg)))
  4170. (org-show-subtree)))
  4171. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4172. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4173. ;; At a heading: rotate between three different views
  4174. (org-back-to-heading)
  4175. (let ((goal-column 0) eoh eol eos)
  4176. ;; First, some boundaries
  4177. (save-excursion
  4178. (org-back-to-heading)
  4179. (save-excursion
  4180. (beginning-of-line 2)
  4181. (while (and (not (eobp)) ;; this is like `next-line'
  4182. (get-char-property (1- (point)) 'invisible))
  4183. (beginning-of-line 2)) (setq eol (point)))
  4184. (outline-end-of-heading) (setq eoh (point))
  4185. (org-end-of-subtree t)
  4186. (unless (eobp)
  4187. (skip-chars-forward " \t\n")
  4188. (beginning-of-line 1) ; in case this is an item
  4189. )
  4190. (setq eos (1- (point))))
  4191. ;; Find out what to do next and set `this-command'
  4192. (cond
  4193. ((= eos eoh)
  4194. ;; Nothing is hidden behind this heading
  4195. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4196. (message "EMPTY ENTRY")
  4197. (setq org-cycle-subtree-status nil)
  4198. (save-excursion
  4199. (goto-char eos)
  4200. (outline-next-heading)
  4201. (if (org-invisible-p) (org-flag-heading nil))))
  4202. ((or (>= eol eos)
  4203. (not (string-match "\\S-" (buffer-substring eol eos))))
  4204. ;; Entire subtree is hidden in one line: open it
  4205. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4206. (org-show-entry)
  4207. (show-children)
  4208. (message "CHILDREN")
  4209. (save-excursion
  4210. (goto-char eos)
  4211. (outline-next-heading)
  4212. (if (org-invisible-p) (org-flag-heading nil)))
  4213. (setq org-cycle-subtree-status 'children)
  4214. (run-hook-with-args 'org-cycle-hook 'children))
  4215. ((and (eq last-command this-command)
  4216. (eq org-cycle-subtree-status 'children))
  4217. ;; We just showed the children, now show everything.
  4218. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4219. (org-show-subtree)
  4220. (message "SUBTREE")
  4221. (setq org-cycle-subtree-status 'subtree)
  4222. (run-hook-with-args 'org-cycle-hook 'subtree))
  4223. (t
  4224. ;; Default action: hide the subtree.
  4225. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4226. (hide-subtree)
  4227. (message "FOLDED")
  4228. (setq org-cycle-subtree-status 'folded)
  4229. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4230. ;; TAB emulation and template completion
  4231. (buffer-read-only (org-back-to-heading))
  4232. ((org-try-structure-completion))
  4233. ((org-try-cdlatex-tab))
  4234. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4235. (or (not (bolp))
  4236. (not (looking-at outline-regexp))))
  4237. (call-interactively (global-key-binding "\t")))
  4238. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4239. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4240. (or (and (eq org-cycle-emulate-tab 'white)
  4241. (= (match-end 0) (point-at-eol)))
  4242. (and (eq org-cycle-emulate-tab 'whitestart)
  4243. (>= (match-end 0) pos))))
  4244. t
  4245. (eq org-cycle-emulate-tab t))
  4246. (call-interactively (global-key-binding "\t")))
  4247. (t (save-excursion
  4248. (org-back-to-heading)
  4249. (org-cycle))))))
  4250. ;;;###autoload
  4251. (defun org-global-cycle (&optional arg)
  4252. "Cycle the global visibility. For details see `org-cycle'.
  4253. With C-u prefix arg, switch to startup visibility.
  4254. With a numeric prefix, show all headlines up to that level."
  4255. (interactive "P")
  4256. (let ((org-cycle-include-plain-lists
  4257. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4258. (cond
  4259. ((integerp arg)
  4260. (show-all)
  4261. (hide-sublevels arg)
  4262. (setq org-cycle-global-status 'contents))
  4263. ((equal arg '(4))
  4264. (org-set-startup-visibility)
  4265. (message "Startup visibility, plus VISIBILITY properties."))
  4266. (t
  4267. (org-cycle '(4))))))
  4268. (defun org-set-startup-visibility ()
  4269. "Set the visibility required by startup options and properties."
  4270. (cond
  4271. ((eq org-startup-folded t)
  4272. (org-cycle '(4)))
  4273. ((eq org-startup-folded 'content)
  4274. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4275. (org-cycle '(4)) (org-cycle '(4)))))
  4276. (org-set-visibility-according-to-property 'no-cleanup)
  4277. (org-cycle-hide-archived-subtrees 'all)
  4278. (org-cycle-hide-drawers 'all)
  4279. (org-cycle-show-empty-lines 'all))
  4280. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4281. "Switch subtree visibilities according to :VISIBILITY: property."
  4282. (interactive)
  4283. (let (org-show-entry-below state)
  4284. (save-excursion
  4285. (goto-char (point-min))
  4286. (while (re-search-forward
  4287. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4288. nil t)
  4289. (setq state (match-string 1))
  4290. (save-excursion
  4291. (org-back-to-heading t)
  4292. (hide-subtree)
  4293. (org-reveal)
  4294. (cond
  4295. ((equal state '("fold" "folded"))
  4296. (hide-subtree))
  4297. ((equal state "children")
  4298. (org-show-hidden-entry)
  4299. (show-children))
  4300. ((equal state "content")
  4301. (save-excursion
  4302. (save-restriction
  4303. (org-narrow-to-subtree)
  4304. (org-content))))
  4305. ((member state '("all" "showall"))
  4306. (show-subtree)))))
  4307. (unless no-cleanup
  4308. (org-cycle-hide-archived-subtrees 'all)
  4309. (org-cycle-hide-drawers 'all)
  4310. (org-cycle-show-empty-lines 'all)))))
  4311. (defun org-overview ()
  4312. "Switch to overview mode, showing only top-level headlines.
  4313. Really, this shows all headlines with level equal or greater than the level
  4314. of the first headline in the buffer. This is important, because if the
  4315. first headline is not level one, then (hide-sublevels 1) gives confusing
  4316. results."
  4317. (interactive)
  4318. (let ((level (save-excursion
  4319. (goto-char (point-min))
  4320. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4321. (progn
  4322. (goto-char (match-beginning 0))
  4323. (funcall outline-level))))))
  4324. (and level (hide-sublevels level))))
  4325. (defun org-content (&optional arg)
  4326. "Show all headlines in the buffer, like a table of contents.
  4327. With numerical argument N, show content up to level N."
  4328. (interactive "P")
  4329. (save-excursion
  4330. ;; Visit all headings and show their offspring
  4331. (and (integerp arg) (org-overview))
  4332. (goto-char (point-max))
  4333. (catch 'exit
  4334. (while (and (progn (condition-case nil
  4335. (outline-previous-visible-heading 1)
  4336. (error (goto-char (point-min))))
  4337. t)
  4338. (looking-at outline-regexp))
  4339. (if (integerp arg)
  4340. (show-children (1- arg))
  4341. (show-branches))
  4342. (if (bobp) (throw 'exit nil))))))
  4343. (defun org-optimize-window-after-visibility-change (state)
  4344. "Adjust the window after a change in outline visibility.
  4345. This function is the default value of the hook `org-cycle-hook'."
  4346. (when (get-buffer-window (current-buffer))
  4347. (cond
  4348. ((eq state 'content) nil)
  4349. ((eq state 'all) nil)
  4350. ((eq state 'folded) nil)
  4351. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4352. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4353. (defun org-compact-display-after-subtree-move ()
  4354. "Show a compacter version of the tree of the entry's parent."
  4355. (save-excursion
  4356. (if (org-up-heading-safe)
  4357. (progn
  4358. (hide-subtree)
  4359. (show-entry)
  4360. (show-children)
  4361. (org-cycle-show-empty-lines 'children)
  4362. (org-cycle-hide-drawers 'children))
  4363. (org-overview))))
  4364. (defun org-cycle-show-empty-lines (state)
  4365. "Show empty lines above all visible headlines.
  4366. The region to be covered depends on STATE when called through
  4367. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4368. entire buffer covered. Note that an empty line is only shown if there
  4369. are at least `org-cycle-separator-lines' empty lines before the headline."
  4370. (when (> org-cycle-separator-lines 0)
  4371. (save-excursion
  4372. (let* ((n org-cycle-separator-lines)
  4373. (re (cond
  4374. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4375. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4376. (t (let ((ns (number-to-string (- n 2))))
  4377. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4378. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4379. beg end)
  4380. (cond
  4381. ((memq state '(overview contents t))
  4382. (setq beg (point-min) end (point-max)))
  4383. ((memq state '(children folded))
  4384. (setq beg (point) end (progn (org-end-of-subtree t t)
  4385. (beginning-of-line 2)
  4386. (point)))))
  4387. (when beg
  4388. (goto-char beg)
  4389. (while (re-search-forward re end t)
  4390. (if (not (get-char-property (match-end 1) 'invisible))
  4391. (outline-flag-region
  4392. (match-beginning 1) (match-end 1) nil)))))))
  4393. ;; Never hide empty lines at the end of the file.
  4394. (save-excursion
  4395. (goto-char (point-max))
  4396. (outline-previous-heading)
  4397. (outline-end-of-heading)
  4398. (if (and (looking-at "[ \t\n]+")
  4399. (= (match-end 0) (point-max)))
  4400. (outline-flag-region (point) (match-end 0) nil))))
  4401. (defun org-show-empty-lines-in-parent ()
  4402. "Move to the parent and re-show empty lines before visible headlines."
  4403. (save-excursion
  4404. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4405. (org-cycle-show-empty-lines context))))
  4406. (defun org-cycle-hide-drawers (state)
  4407. "Re-hide all drawers after a visibility state change."
  4408. (when (and (org-mode-p)
  4409. (not (memq state '(overview folded))))
  4410. (save-excursion
  4411. (let* ((globalp (memq state '(contents all)))
  4412. (beg (if globalp (point-min) (point)))
  4413. (end (if globalp (point-max) (org-end-of-subtree t))))
  4414. (goto-char beg)
  4415. (while (re-search-forward org-drawer-regexp end t)
  4416. (org-flag-drawer t))))))
  4417. (defun org-flag-drawer (flag)
  4418. (save-excursion
  4419. (beginning-of-line 1)
  4420. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4421. (let ((b (match-end 0))
  4422. (outline-regexp org-outline-regexp))
  4423. (if (re-search-forward
  4424. "^[ \t]*:END:"
  4425. (save-excursion (outline-next-heading) (point)) t)
  4426. (outline-flag-region b (point-at-eol) flag)
  4427. (error ":END: line missing"))))))
  4428. (defun org-subtree-end-visible-p ()
  4429. "Is the end of the current subtree visible?"
  4430. (pos-visible-in-window-p
  4431. (save-excursion (org-end-of-subtree t) (point))))
  4432. (defun org-first-headline-recenter (&optional N)
  4433. "Move cursor to the first headline and recenter the headline.
  4434. Optional argument N means, put the headline into the Nth line of the window."
  4435. (goto-char (point-min))
  4436. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4437. (beginning-of-line)
  4438. (recenter (prefix-numeric-value N))))
  4439. ;;; Org-goto
  4440. (defvar org-goto-window-configuration nil)
  4441. (defvar org-goto-marker nil)
  4442. (defvar org-goto-map
  4443. (let ((map (make-sparse-keymap)))
  4444. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4445. (while (setq cmd (pop cmds))
  4446. (substitute-key-definition cmd cmd map global-map)))
  4447. (suppress-keymap map)
  4448. (org-defkey map "\C-m" 'org-goto-ret)
  4449. (org-defkey map [(return)] 'org-goto-ret)
  4450. (org-defkey map [(left)] 'org-goto-left)
  4451. (org-defkey map [(right)] 'org-goto-right)
  4452. (org-defkey map [(control ?g)] 'org-goto-quit)
  4453. (org-defkey map "\C-i" 'org-cycle)
  4454. (org-defkey map [(tab)] 'org-cycle)
  4455. (org-defkey map [(down)] 'outline-next-visible-heading)
  4456. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4457. (if org-goto-auto-isearch
  4458. (if (fboundp 'define-key-after)
  4459. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4460. nil)
  4461. (org-defkey map "q" 'org-goto-quit)
  4462. (org-defkey map "n" 'outline-next-visible-heading)
  4463. (org-defkey map "p" 'outline-previous-visible-heading)
  4464. (org-defkey map "f" 'outline-forward-same-level)
  4465. (org-defkey map "b" 'outline-backward-same-level)
  4466. (org-defkey map "u" 'outline-up-heading))
  4467. (org-defkey map "/" 'org-occur)
  4468. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4469. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4470. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4471. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4472. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4473. map))
  4474. (defconst org-goto-help
  4475. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4476. RET=jump to location [Q]uit and return to previous location
  4477. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4478. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4479. ;; FIXME: Docstring doe not mention both interfaces
  4480. (defun org-goto (&optional alternative-interface)
  4481. "Look up a different location in the current file, keeping current visibility.
  4482. When you want look-up or go to a different location in a document, the
  4483. fastest way is often to fold the entire buffer and then dive into the tree.
  4484. This method has the disadvantage, that the previous location will be folded,
  4485. which may not be what you want.
  4486. This command works around this by showing a copy of the current buffer
  4487. in an indirect buffer, in overview mode. You can dive into the tree in
  4488. that copy, use org-occur and incremental search to find a location.
  4489. When pressing RET or `Q', the command returns to the original buffer in
  4490. which the visibility is still unchanged. After RET is will also jump to
  4491. the location selected in the indirect buffer and expose the
  4492. the headline hierarchy above."
  4493. (interactive "P")
  4494. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4495. (org-refile-use-outline-path t)
  4496. (org-refile-target-verify-function nil)
  4497. (interface
  4498. (if (not alternative-interface)
  4499. org-goto-interface
  4500. (if (eq org-goto-interface 'outline)
  4501. 'outline-path-completion
  4502. 'outline)))
  4503. (org-goto-start-pos (point))
  4504. (selected-point
  4505. (if (eq interface 'outline)
  4506. (car (org-get-location (current-buffer) org-goto-help))
  4507. (nth 3 (org-refile-get-location "Goto: ")))))
  4508. (if selected-point
  4509. (progn
  4510. (org-mark-ring-push org-goto-start-pos)
  4511. (goto-char selected-point)
  4512. (if (or (org-invisible-p) (org-invisible-p2))
  4513. (org-show-context 'org-goto)))
  4514. (message "Quit"))))
  4515. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4516. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4517. (defvar org-goto-local-auto-isearch-map) ; defined below
  4518. (defun org-get-location (buf help)
  4519. "Let the user select a location in the Org-mode buffer BUF.
  4520. This function uses a recursive edit. It returns the selected position
  4521. or nil."
  4522. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4523. (isearch-hide-immediately nil)
  4524. (isearch-search-fun-function
  4525. (lambda () 'org-goto-local-search-headings))
  4526. (org-goto-selected-point org-goto-exit-command))
  4527. (save-excursion
  4528. (save-window-excursion
  4529. (delete-other-windows)
  4530. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4531. (switch-to-buffer
  4532. (condition-case nil
  4533. (make-indirect-buffer (current-buffer) "*org-goto*")
  4534. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4535. (with-output-to-temp-buffer "*Help*"
  4536. (princ help))
  4537. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4538. (setq buffer-read-only nil)
  4539. (let ((org-startup-truncated t)
  4540. (org-startup-folded nil)
  4541. (org-startup-align-all-tables nil))
  4542. (org-mode)
  4543. (org-overview))
  4544. (setq buffer-read-only t)
  4545. (if (and (boundp 'org-goto-start-pos)
  4546. (integer-or-marker-p org-goto-start-pos))
  4547. (let ((org-show-hierarchy-above t)
  4548. (org-show-siblings t)
  4549. (org-show-following-heading t))
  4550. (goto-char org-goto-start-pos)
  4551. (and (org-invisible-p) (org-show-context)))
  4552. (goto-char (point-min)))
  4553. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4554. (message "Select location and press RET")
  4555. (use-local-map org-goto-map)
  4556. (recursive-edit)
  4557. ))
  4558. (kill-buffer "*org-goto*")
  4559. (cons org-goto-selected-point org-goto-exit-command)))
  4560. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4561. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4562. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4563. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4564. (defun org-goto-local-search-headings (string bound noerror)
  4565. "Search and make sure that any matches are in headlines."
  4566. (catch 'return
  4567. (while (if isearch-forward
  4568. (search-forward string bound noerror)
  4569. (search-backward string bound noerror))
  4570. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4571. (and (member :headline context)
  4572. (not (member :tags context))))
  4573. (throw 'return (point))))))
  4574. (defun org-goto-local-auto-isearch ()
  4575. "Start isearch."
  4576. (interactive)
  4577. (goto-char (point-min))
  4578. (let ((keys (this-command-keys)))
  4579. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4580. (isearch-mode t)
  4581. (isearch-process-search-char (string-to-char keys)))))
  4582. (defun org-goto-ret (&optional arg)
  4583. "Finish `org-goto' by going to the new location."
  4584. (interactive "P")
  4585. (setq org-goto-selected-point (point)
  4586. org-goto-exit-command 'return)
  4587. (throw 'exit nil))
  4588. (defun org-goto-left ()
  4589. "Finish `org-goto' by going to the new location."
  4590. (interactive)
  4591. (if (org-on-heading-p)
  4592. (progn
  4593. (beginning-of-line 1)
  4594. (setq org-goto-selected-point (point)
  4595. org-goto-exit-command 'left)
  4596. (throw 'exit nil))
  4597. (error "Not on a heading")))
  4598. (defun org-goto-right ()
  4599. "Finish `org-goto' by going to the new location."
  4600. (interactive)
  4601. (if (org-on-heading-p)
  4602. (progn
  4603. (setq org-goto-selected-point (point)
  4604. org-goto-exit-command 'right)
  4605. (throw 'exit nil))
  4606. (error "Not on a heading")))
  4607. (defun org-goto-quit ()
  4608. "Finish `org-goto' without cursor motion."
  4609. (interactive)
  4610. (setq org-goto-selected-point nil)
  4611. (setq org-goto-exit-command 'quit)
  4612. (throw 'exit nil))
  4613. ;;; Indirect buffer display of subtrees
  4614. (defvar org-indirect-dedicated-frame nil
  4615. "This is the frame being used for indirect tree display.")
  4616. (defvar org-last-indirect-buffer nil)
  4617. (defun org-tree-to-indirect-buffer (&optional arg)
  4618. "Create indirect buffer and narrow it to current subtree.
  4619. With numerical prefix ARG, go up to this level and then take that tree.
  4620. If ARG is negative, go up that many levels.
  4621. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4622. indirect buffer previously made with this command, to avoid proliferation of
  4623. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4624. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4625. is kept so that you can work with several indirect buffers at the same time.
  4626. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4627. requests that a new frame be made for the new buffer, so that the dedicated
  4628. frame is not changed."
  4629. (interactive "P")
  4630. (let ((cbuf (current-buffer))
  4631. (cwin (selected-window))
  4632. (pos (point))
  4633. beg end level heading ibuf)
  4634. (save-excursion
  4635. (org-back-to-heading t)
  4636. (when (numberp arg)
  4637. (setq level (org-outline-level))
  4638. (if (< arg 0) (setq arg (+ level arg)))
  4639. (while (> (setq level (org-outline-level)) arg)
  4640. (outline-up-heading 1 t)))
  4641. (setq beg (point)
  4642. heading (org-get-heading))
  4643. (org-end-of-subtree t) (setq end (point)))
  4644. (if (and (buffer-live-p org-last-indirect-buffer)
  4645. (not (eq org-indirect-buffer-display 'new-frame))
  4646. (not arg))
  4647. (kill-buffer org-last-indirect-buffer))
  4648. (setq ibuf (org-get-indirect-buffer cbuf)
  4649. org-last-indirect-buffer ibuf)
  4650. (cond
  4651. ((or (eq org-indirect-buffer-display 'new-frame)
  4652. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4653. (select-frame (make-frame))
  4654. (delete-other-windows)
  4655. (switch-to-buffer ibuf)
  4656. (org-set-frame-title heading))
  4657. ((eq org-indirect-buffer-display 'dedicated-frame)
  4658. (raise-frame
  4659. (select-frame (or (and org-indirect-dedicated-frame
  4660. (frame-live-p org-indirect-dedicated-frame)
  4661. org-indirect-dedicated-frame)
  4662. (setq org-indirect-dedicated-frame (make-frame)))))
  4663. (delete-other-windows)
  4664. (switch-to-buffer ibuf)
  4665. (org-set-frame-title (concat "Indirect: " heading)))
  4666. ((eq org-indirect-buffer-display 'current-window)
  4667. (switch-to-buffer ibuf))
  4668. ((eq org-indirect-buffer-display 'other-window)
  4669. (pop-to-buffer ibuf))
  4670. (t (error "Invalid value.")))
  4671. (if (featurep 'xemacs)
  4672. (save-excursion (org-mode) (turn-on-font-lock)))
  4673. (narrow-to-region beg end)
  4674. (show-all)
  4675. (goto-char pos)
  4676. (and (window-live-p cwin) (select-window cwin))))
  4677. (defun org-get-indirect-buffer (&optional buffer)
  4678. (setq buffer (or buffer (current-buffer)))
  4679. (let ((n 1) (base (buffer-name buffer)) bname)
  4680. (while (buffer-live-p
  4681. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4682. (setq n (1+ n)))
  4683. (condition-case nil
  4684. (make-indirect-buffer buffer bname 'clone)
  4685. (error (make-indirect-buffer buffer bname)))))
  4686. (defun org-set-frame-title (title)
  4687. "Set the title of the current frame to the string TITLE."
  4688. ;; FIXME: how to name a single frame in XEmacs???
  4689. (unless (featurep 'xemacs)
  4690. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4691. ;;;; Structure editing
  4692. ;;; Inserting headlines
  4693. (defun org-previous-line-empty-p ()
  4694. (save-excursion
  4695. (and (not (bobp))
  4696. (or (beginning-of-line 0) t)
  4697. (save-match-data
  4698. (looking-at "[ \t]*$")))))
  4699. (defun org-insert-heading (&optional force-heading)
  4700. "Insert a new heading or item with same depth at point.
  4701. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4702. If point is at the beginning of a headline, insert a sibling before the
  4703. current headline. If point is not at the beginning, do not split the line,
  4704. but create the new headline after the current line."
  4705. (interactive "P")
  4706. (if (= (buffer-size) 0)
  4707. (insert "\n* ")
  4708. (when (or force-heading (not (org-insert-item)))
  4709. (let* ((empty-line-p nil)
  4710. (head (save-excursion
  4711. (condition-case nil
  4712. (progn
  4713. (org-back-to-heading)
  4714. (setq empty-line-p (org-previous-line-empty-p))
  4715. (match-string 0))
  4716. (error "*"))))
  4717. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4718. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4719. pos hide-previous previous-pos)
  4720. (cond
  4721. ((and (org-on-heading-p) (bolp)
  4722. (or (bobp)
  4723. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4724. ;; insert before the current line
  4725. (open-line (if blank 2 1)))
  4726. ((and (bolp)
  4727. (or (bobp)
  4728. (save-excursion
  4729. (backward-char 1) (not (org-invisible-p)))))
  4730. ;; insert right here
  4731. nil)
  4732. (t
  4733. ;; somewhere in the line
  4734. (save-excursion
  4735. (setq previous-pos (point-at-bol))
  4736. (end-of-line)
  4737. (setq hide-previous (org-invisible-p)))
  4738. (and org-insert-heading-respect-content (org-show-subtree))
  4739. (let ((split
  4740. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4741. (save-excursion
  4742. (let ((p (point)))
  4743. (goto-char (point-at-bol))
  4744. (and (looking-at org-complex-heading-regexp)
  4745. (> p (match-beginning 4)))))))
  4746. tags pos)
  4747. (cond
  4748. (org-insert-heading-respect-content
  4749. (org-end-of-subtree nil t)
  4750. (or (bolp) (newline))
  4751. (or (org-previous-line-empty-p)
  4752. (and blank (newline)))
  4753. (open-line 1))
  4754. ((org-on-heading-p)
  4755. (when hide-previous
  4756. (show-children)
  4757. (org-show-entry))
  4758. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4759. (setq tags (and (match-end 2) (match-string 2)))
  4760. (and (match-end 1)
  4761. (delete-region (match-beginning 1) (match-end 1)))
  4762. (setq pos (point-at-bol))
  4763. (or split (end-of-line 1))
  4764. (delete-horizontal-space)
  4765. (newline (if blank 2 1))
  4766. (when tags
  4767. (save-excursion
  4768. (goto-char pos)
  4769. (end-of-line 1)
  4770. (insert " " tags)
  4771. (org-set-tags nil 'align))))
  4772. (t
  4773. (or split (end-of-line 1))
  4774. (newline (if blank 2 1)))))))
  4775. (insert head) (just-one-space)
  4776. (setq pos (point))
  4777. (end-of-line 1)
  4778. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4779. (when (and org-insert-heading-respect-content hide-previous)
  4780. (save-excursion
  4781. (goto-char previous-pos)
  4782. (hide-subtree)))
  4783. (run-hooks 'org-insert-heading-hook)))))
  4784. (defun org-get-heading (&optional no-tags)
  4785. "Return the heading of the current entry, without the stars."
  4786. (save-excursion
  4787. (org-back-to-heading t)
  4788. (if (looking-at
  4789. (if no-tags
  4790. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4791. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4792. (match-string 1) "")))
  4793. (defun org-heading-components ()
  4794. "Return the components of the current heading.
  4795. This is a list with the following elements:
  4796. - the level as an integer
  4797. - the reduced level, different if `org-odd-levels-only' is set.
  4798. - the TODO keyword, or nil
  4799. - the priority character, like ?A, or nil if no priority is given
  4800. - the headline text itself, or the tags string if no headline text
  4801. - the tags string, or nil."
  4802. (save-excursion
  4803. (org-back-to-heading t)
  4804. (if (looking-at org-complex-heading-regexp)
  4805. (list (length (match-string 1))
  4806. (org-reduced-level (length (match-string 1)))
  4807. (org-match-string-no-properties 2)
  4808. (and (match-end 3) (aref (match-string 3) 2))
  4809. (org-match-string-no-properties 4)
  4810. (org-match-string-no-properties 5)))))
  4811. (defun org-get-entry ()
  4812. "Get the entry text, after heading, entire subtree."
  4813. (save-excursion
  4814. (org-back-to-heading t)
  4815. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4816. (defun org-insert-heading-after-current ()
  4817. "Insert a new heading with same level as current, after current subtree."
  4818. (interactive)
  4819. (org-back-to-heading)
  4820. (org-insert-heading)
  4821. (org-move-subtree-down)
  4822. (end-of-line 1))
  4823. (defun org-insert-heading-respect-content ()
  4824. (interactive)
  4825. (let ((org-insert-heading-respect-content t))
  4826. (org-insert-heading t)))
  4827. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4828. (interactive "P")
  4829. (let ((org-insert-heading-respect-content t))
  4830. (org-insert-todo-heading force-state t)))
  4831. (defun org-insert-todo-heading (arg &optional force-heading)
  4832. "Insert a new heading with the same level and TODO state as current heading.
  4833. If the heading has no TODO state, or if the state is DONE, use the first
  4834. state (TODO by default). Also with prefix arg, force first state."
  4835. (interactive "P")
  4836. (when (or force-heading (not (org-insert-item 'checkbox)))
  4837. (org-insert-heading force-heading)
  4838. (save-excursion
  4839. (org-back-to-heading)
  4840. (outline-previous-heading)
  4841. (looking-at org-todo-line-regexp))
  4842. (let*
  4843. ((new-mark-x
  4844. (if (or arg
  4845. (not (match-beginning 2))
  4846. (member (match-string 2) org-done-keywords))
  4847. (car org-todo-keywords-1)
  4848. (match-string 2)))
  4849. (new-mark
  4850. (or
  4851. (run-hook-with-args-until-success
  4852. 'org-todo-get-default-hook new-mark-x nil)
  4853. new-mark-x)))
  4854. (beginning-of-line 1)
  4855. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4856. (insert new-mark " ")))
  4857. (when org-provide-todo-statistics
  4858. (org-update-parent-todo-statistics))))
  4859. (defun org-insert-subheading (arg)
  4860. "Insert a new subheading and demote it.
  4861. Works for outline headings and for plain lists alike."
  4862. (interactive "P")
  4863. (org-insert-heading arg)
  4864. (cond
  4865. ((org-on-heading-p) (org-do-demote))
  4866. ((org-at-item-p) (org-indent-item 1))))
  4867. (defun org-insert-todo-subheading (arg)
  4868. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4869. Works for outline headings and for plain lists alike."
  4870. (interactive "P")
  4871. (org-insert-todo-heading arg)
  4872. (cond
  4873. ((org-on-heading-p) (org-do-demote))
  4874. ((org-at-item-p) (org-indent-item 1))))
  4875. ;;; Promotion and Demotion
  4876. (defun org-promote-subtree ()
  4877. "Promote the entire subtree.
  4878. See also `org-promote'."
  4879. (interactive)
  4880. (save-excursion
  4881. (org-map-tree 'org-promote))
  4882. (org-fix-position-after-promote))
  4883. (defun org-demote-subtree ()
  4884. "Demote the entire subtree. See `org-demote'.
  4885. See also `org-promote'."
  4886. (interactive)
  4887. (save-excursion
  4888. (org-map-tree 'org-demote))
  4889. (org-fix-position-after-promote))
  4890. (defun org-do-promote ()
  4891. "Promote the current heading higher up the tree.
  4892. If the region is active in `transient-mark-mode', promote all headings
  4893. in the region."
  4894. (interactive)
  4895. (save-excursion
  4896. (if (org-region-active-p)
  4897. (org-map-region 'org-promote (region-beginning) (region-end))
  4898. (org-promote)))
  4899. (org-fix-position-after-promote))
  4900. (defun org-do-demote ()
  4901. "Demote the current heading lower down the tree.
  4902. If the region is active in `transient-mark-mode', demote all headings
  4903. in the region."
  4904. (interactive)
  4905. (save-excursion
  4906. (if (org-region-active-p)
  4907. (org-map-region 'org-demote (region-beginning) (region-end))
  4908. (org-demote)))
  4909. (org-fix-position-after-promote))
  4910. (defun org-fix-position-after-promote ()
  4911. "Make sure that after pro/demotion cursor position is right."
  4912. (let ((pos (point)))
  4913. (when (save-excursion
  4914. (beginning-of-line 1)
  4915. (looking-at org-todo-line-regexp)
  4916. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4917. (cond ((eobp) (insert " "))
  4918. ((eolp) (insert " "))
  4919. ((equal (char-after) ?\ ) (forward-char 1))))))
  4920. (defun org-reduced-level (l)
  4921. "Compute the effective level of a heading.
  4922. This takes into account the setting of `org-odd-levels-only'."
  4923. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4924. (defun org-get-valid-level (level &optional change)
  4925. "Rectify a level change under the influence of `org-odd-levels-only'
  4926. LEVEL is a current level, CHANGE is by how much the level should be
  4927. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4928. even level numbers will become the next higher odd number."
  4929. (if org-odd-levels-only
  4930. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4931. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4932. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4933. (max 1 (+ level (or change 0)))))
  4934. (if (boundp 'define-obsolete-function-alias)
  4935. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4936. (define-obsolete-function-alias 'org-get-legal-level
  4937. 'org-get-valid-level)
  4938. (define-obsolete-function-alias 'org-get-legal-level
  4939. 'org-get-valid-level "23.1")))
  4940. (defun org-promote ()
  4941. "Promote the current heading higher up the tree.
  4942. If the region is active in `transient-mark-mode', promote all headings
  4943. in the region."
  4944. (org-back-to-heading t)
  4945. (let* ((level (save-match-data (funcall outline-level)))
  4946. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4947. (diff (abs (- level (length up-head) -1))))
  4948. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4949. (replace-match up-head nil t)
  4950. ;; Fixup tag positioning
  4951. (and org-auto-align-tags (org-set-tags nil t))
  4952. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4953. (defun org-demote ()
  4954. "Demote the current heading lower down the tree.
  4955. If the region is active in `transient-mark-mode', demote all headings
  4956. in the region."
  4957. (org-back-to-heading t)
  4958. (let* ((level (save-match-data (funcall outline-level)))
  4959. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4960. (diff (abs (- level (length down-head) -1))))
  4961. (replace-match down-head nil t)
  4962. ;; Fixup tag positioning
  4963. (and org-auto-align-tags (org-set-tags nil t))
  4964. (if org-adapt-indentation (org-fixup-indentation diff))))
  4965. (defun org-map-tree (fun)
  4966. "Call FUN for every heading underneath the current one."
  4967. (org-back-to-heading)
  4968. (let ((level (funcall outline-level)))
  4969. (save-excursion
  4970. (funcall fun)
  4971. (while (and (progn
  4972. (outline-next-heading)
  4973. (> (funcall outline-level) level))
  4974. (not (eobp)))
  4975. (funcall fun)))))
  4976. (defun org-map-region (fun beg end)
  4977. "Call FUN for every heading between BEG and END."
  4978. (let ((org-ignore-region t))
  4979. (save-excursion
  4980. (setq end (copy-marker end))
  4981. (goto-char beg)
  4982. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4983. (< (point) end))
  4984. (funcall fun))
  4985. (while (and (progn
  4986. (outline-next-heading)
  4987. (< (point) end))
  4988. (not (eobp)))
  4989. (funcall fun)))))
  4990. (defun org-fixup-indentation (diff)
  4991. "Change the indentation in the current entry by DIFF
  4992. However, if any line in the current entry has no indentation, or if it
  4993. would end up with no indentation after the change, nothing at all is done."
  4994. (save-excursion
  4995. (let ((end (save-excursion (outline-next-heading)
  4996. (point-marker)))
  4997. (prohibit (if (> diff 0)
  4998. "^\\S-"
  4999. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5000. col)
  5001. (unless (save-excursion (end-of-line 1)
  5002. (re-search-forward prohibit end t))
  5003. (while (and (< (point) end)
  5004. (re-search-forward "^[ \t]+" end t))
  5005. (goto-char (match-end 0))
  5006. (setq col (current-column))
  5007. (if (< diff 0) (replace-match ""))
  5008. (org-indent-to-column (+ diff col))))
  5009. (move-marker end nil))))
  5010. (defun org-convert-to-odd-levels ()
  5011. "Convert an org-mode file with all levels allowed to one with odd levels.
  5012. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5013. level 5 etc."
  5014. (interactive)
  5015. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5016. (let ((org-odd-levels-only nil) n)
  5017. (save-excursion
  5018. (goto-char (point-min))
  5019. (while (re-search-forward "^\\*\\*+ " nil t)
  5020. (setq n (- (length (match-string 0)) 2))
  5021. (while (>= (setq n (1- n)) 0)
  5022. (org-demote))
  5023. (end-of-line 1))))))
  5024. (defun org-convert-to-oddeven-levels ()
  5025. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5026. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5027. section with an even level, conversion would destroy the structure of the file. An error
  5028. is signaled in this case."
  5029. (interactive)
  5030. (goto-char (point-min))
  5031. ;; First check if there are no even levels
  5032. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5033. (org-show-context t)
  5034. (error "Not all levels are odd in this file. Conversion not possible."))
  5035. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5036. (let ((org-odd-levels-only nil) n)
  5037. (save-excursion
  5038. (goto-char (point-min))
  5039. (while (re-search-forward "^\\*\\*+ " nil t)
  5040. (setq n (/ (1- (length (match-string 0))) 2))
  5041. (while (>= (setq n (1- n)) 0)
  5042. (org-promote))
  5043. (end-of-line 1))))))
  5044. (defun org-tr-level (n)
  5045. "Make N odd if required."
  5046. (if org-odd-levels-only (1+ (/ n 2)) n))
  5047. ;;; Vertical tree motion, cutting and pasting of subtrees
  5048. (defun org-move-subtree-up (&optional arg)
  5049. "Move the current subtree up past ARG headlines of the same level."
  5050. (interactive "p")
  5051. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5052. (defun org-move-subtree-down (&optional arg)
  5053. "Move the current subtree down past ARG headlines of the same level."
  5054. (interactive "p")
  5055. (setq arg (prefix-numeric-value arg))
  5056. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5057. 'outline-get-last-sibling))
  5058. (ins-point (make-marker))
  5059. (cnt (abs arg))
  5060. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5061. ;; Select the tree
  5062. (org-back-to-heading)
  5063. (setq beg0 (point))
  5064. (save-excursion
  5065. (setq ne-beg (org-back-over-empty-lines))
  5066. (setq beg (point)))
  5067. (save-match-data
  5068. (save-excursion (outline-end-of-heading)
  5069. (setq folded (org-invisible-p)))
  5070. (outline-end-of-subtree))
  5071. (outline-next-heading)
  5072. (setq ne-end (org-back-over-empty-lines))
  5073. (setq end (point))
  5074. (goto-char beg0)
  5075. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5076. ;; include less whitespace
  5077. (save-excursion
  5078. (goto-char beg)
  5079. (forward-line (- ne-beg ne-end))
  5080. (setq beg (point))))
  5081. ;; Find insertion point, with error handling
  5082. (while (> cnt 0)
  5083. (or (and (funcall movfunc) (looking-at outline-regexp))
  5084. (progn (goto-char beg0)
  5085. (error "Cannot move past superior level or buffer limit")))
  5086. (setq cnt (1- cnt)))
  5087. (if (> arg 0)
  5088. ;; Moving forward - still need to move over subtree
  5089. (progn (org-end-of-subtree t t)
  5090. (save-excursion
  5091. (org-back-over-empty-lines)
  5092. (or (bolp) (newline)))))
  5093. (setq ne-ins (org-back-over-empty-lines))
  5094. (move-marker ins-point (point))
  5095. (setq txt (buffer-substring beg end))
  5096. (org-save-markers-in-region beg end)
  5097. (delete-region beg end)
  5098. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5099. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5100. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5101. (let ((bbb (point)))
  5102. (insert-before-markers txt)
  5103. (org-reinstall-markers-in-region bbb)
  5104. (move-marker ins-point bbb))
  5105. (or (bolp) (insert "\n"))
  5106. (setq ins-end (point))
  5107. (goto-char ins-point)
  5108. (org-skip-whitespace)
  5109. (when (and (< arg 0)
  5110. (org-first-sibling-p)
  5111. (> ne-ins ne-beg))
  5112. ;; Move whitespace back to beginning
  5113. (save-excursion
  5114. (goto-char ins-end)
  5115. (let ((kill-whole-line t))
  5116. (kill-line (- ne-ins ne-beg)) (point)))
  5117. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5118. (move-marker ins-point nil)
  5119. (org-compact-display-after-subtree-move)
  5120. (org-show-empty-lines-in-parent)
  5121. (unless folded
  5122. (org-show-entry)
  5123. (show-children)
  5124. (org-cycle-hide-drawers 'children))))
  5125. (defvar org-subtree-clip ""
  5126. "Clipboard for cut and paste of subtrees.
  5127. This is actually only a copy of the kill, because we use the normal kill
  5128. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5129. (defvar org-subtree-clip-folded nil
  5130. "Was the last copied subtree folded?
  5131. This is used to fold the tree back after pasting.")
  5132. (defun org-cut-subtree (&optional n)
  5133. "Cut the current subtree into the clipboard.
  5134. With prefix arg N, cut this many sequential subtrees.
  5135. This is a short-hand for marking the subtree and then cutting it."
  5136. (interactive "p")
  5137. (org-copy-subtree n 'cut))
  5138. (defun org-copy-subtree (&optional n cut force-store-markers)
  5139. "Cut the current subtree into the clipboard.
  5140. With prefix arg N, cut this many sequential subtrees.
  5141. This is a short-hand for marking the subtree and then copying it.
  5142. If CUT is non-nil, actually cut the subtree.
  5143. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5144. of some markers in the region, even if CUT is non-nil. This is
  5145. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5146. (interactive "p")
  5147. (let (beg end folded (beg0 (point)))
  5148. (if (interactive-p)
  5149. (org-back-to-heading nil) ; take what looks like a subtree
  5150. (org-back-to-heading t)) ; take what is really there
  5151. (org-back-over-empty-lines)
  5152. (setq beg (point))
  5153. (skip-chars-forward " \t\r\n")
  5154. (save-match-data
  5155. (save-excursion (outline-end-of-heading)
  5156. (setq folded (org-invisible-p)))
  5157. (condition-case nil
  5158. (outline-forward-same-level (1- n))
  5159. (error nil))
  5160. (org-end-of-subtree t t))
  5161. (org-back-over-empty-lines)
  5162. (setq end (point))
  5163. (goto-char beg0)
  5164. (when (> end beg)
  5165. (setq org-subtree-clip-folded folded)
  5166. (when (or cut force-store-markers)
  5167. (org-save-markers-in-region beg end))
  5168. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5169. (setq org-subtree-clip (current-kill 0))
  5170. (message "%s: Subtree(s) with %d characters"
  5171. (if cut "Cut" "Copied")
  5172. (length org-subtree-clip)))))
  5173. (defun org-paste-subtree (&optional level tree for-yank)
  5174. "Paste the clipboard as a subtree, with modification of headline level.
  5175. The entire subtree is promoted or demoted in order to match a new headline
  5176. level.
  5177. If the cursor is at the beginning of a headline, the same level as
  5178. that headline is used to paste the tree
  5179. If not, the new level is derived from the *visible* headings
  5180. before and after the insertion point, and taken to be the inferior headline
  5181. level of the two. So if the previous visible heading is level 3 and the
  5182. next is level 4 (or vice versa), level 4 will be used for insertion.
  5183. This makes sure that the subtree remains an independent subtree and does
  5184. not swallow low level entries.
  5185. You can also force a different level, either by using a numeric prefix
  5186. argument, or by inserting the heading marker by hand. For example, if the
  5187. cursor is after \"*****\", then the tree will be shifted to level 5.
  5188. If optional TREE is given, use this text instead of the kill ring.
  5189. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5190. move back over whitespace before inserting, and move point to the end of
  5191. the inserted text when done."
  5192. (interactive "P")
  5193. (unless (org-kill-is-subtree-p tree)
  5194. (error "%s"
  5195. (substitute-command-keys
  5196. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5197. (let* ((visp (not (org-invisible-p)))
  5198. (txt (or tree (and kill-ring (current-kill 0))))
  5199. (^re (concat "^\\(" outline-regexp "\\)"))
  5200. (re (concat "\\(" outline-regexp "\\)"))
  5201. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5202. (old-level (if (string-match ^re txt)
  5203. (- (match-end 0) (match-beginning 0) 1)
  5204. -1))
  5205. (force-level (cond (level (prefix-numeric-value level))
  5206. ((and (looking-at "[ \t]*$")
  5207. (string-match
  5208. ^re_ (buffer-substring
  5209. (point-at-bol) (point))))
  5210. (- (match-end 1) (match-beginning 1)))
  5211. ((and (bolp)
  5212. (looking-at org-outline-regexp))
  5213. (- (match-end 0) (point) 1))
  5214. (t nil)))
  5215. (previous-level (save-excursion
  5216. (condition-case nil
  5217. (progn
  5218. (outline-previous-visible-heading 1)
  5219. (if (looking-at re)
  5220. (- (match-end 0) (match-beginning 0) 1)
  5221. 1))
  5222. (error 1))))
  5223. (next-level (save-excursion
  5224. (condition-case nil
  5225. (progn
  5226. (or (looking-at outline-regexp)
  5227. (outline-next-visible-heading 1))
  5228. (if (looking-at re)
  5229. (- (match-end 0) (match-beginning 0) 1)
  5230. 1))
  5231. (error 1))))
  5232. (new-level (or force-level (max previous-level next-level)))
  5233. (shift (if (or (= old-level -1)
  5234. (= new-level -1)
  5235. (= old-level new-level))
  5236. 0
  5237. (- new-level old-level)))
  5238. (delta (if (> shift 0) -1 1))
  5239. (func (if (> shift 0) 'org-demote 'org-promote))
  5240. (org-odd-levels-only nil)
  5241. beg end newend)
  5242. ;; Remove the forced level indicator
  5243. (if force-level
  5244. (delete-region (point-at-bol) (point)))
  5245. ;; Paste
  5246. (beginning-of-line 1)
  5247. (unless for-yank (org-back-over-empty-lines))
  5248. (setq beg (point))
  5249. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5250. (insert-before-markers txt)
  5251. (unless (string-match "\n\\'" txt) (insert "\n"))
  5252. (setq newend (point))
  5253. (org-reinstall-markers-in-region beg)
  5254. (setq end (point))
  5255. (goto-char beg)
  5256. (skip-chars-forward " \t\n\r")
  5257. (setq beg (point))
  5258. (if (and (org-invisible-p) visp)
  5259. (save-excursion (outline-show-heading)))
  5260. ;; Shift if necessary
  5261. (unless (= shift 0)
  5262. (save-restriction
  5263. (narrow-to-region beg end)
  5264. (while (not (= shift 0))
  5265. (org-map-region func (point-min) (point-max))
  5266. (setq shift (+ delta shift)))
  5267. (goto-char (point-min))
  5268. (setq newend (point-max))))
  5269. (when (or (interactive-p) for-yank)
  5270. (message "Clipboard pasted as level %d subtree" new-level))
  5271. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5272. kill-ring
  5273. (eq org-subtree-clip (current-kill 0))
  5274. org-subtree-clip-folded)
  5275. ;; The tree was folded before it was killed/copied
  5276. (hide-subtree))
  5277. (and for-yank (goto-char newend))))
  5278. (defun org-kill-is-subtree-p (&optional txt)
  5279. "Check if the current kill is an outline subtree, or a set of trees.
  5280. Returns nil if kill does not start with a headline, or if the first
  5281. headline level is not the largest headline level in the tree.
  5282. So this will actually accept several entries of equal levels as well,
  5283. which is OK for `org-paste-subtree'.
  5284. If optional TXT is given, check this string instead of the current kill."
  5285. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5286. (start-level (and kill
  5287. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5288. org-outline-regexp "\\)")
  5289. kill)
  5290. (- (match-end 2) (match-beginning 2) 1)))
  5291. (re (concat "^" org-outline-regexp))
  5292. (start (1+ (or (match-beginning 2) -1))))
  5293. (if (not start-level)
  5294. (progn
  5295. nil) ;; does not even start with a heading
  5296. (catch 'exit
  5297. (while (setq start (string-match re kill (1+ start)))
  5298. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5299. (throw 'exit nil)))
  5300. t))))
  5301. (defvar org-markers-to-move nil
  5302. "Markers that should be moved with a cut-and-paste operation.
  5303. Those markers are stored together with their positions relative to
  5304. the start of the region.")
  5305. (defun org-save-markers-in-region (beg end)
  5306. "Check markers in region.
  5307. If these markers are between BEG and END, record their position relative
  5308. to BEG, so that after moving the block of text, we can put the markers back
  5309. into place.
  5310. This function gets called just before an entry or tree gets cut from the
  5311. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5312. called immediately, to move the markers with the entries."
  5313. (setq org-markers-to-move nil)
  5314. (when (featurep 'org-clock)
  5315. (org-clock-save-markers-for-cut-and-paste beg end))
  5316. (when (featurep 'org-agenda)
  5317. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5318. (defun org-check-and-save-marker (marker beg end)
  5319. "Check if MARKER is between BEG and END.
  5320. If yes, remember the marker and the distance to BEG."
  5321. (when (and (marker-buffer marker)
  5322. (equal (marker-buffer marker) (current-buffer)))
  5323. (if (and (>= marker beg) (< marker end))
  5324. (push (cons marker (- marker beg)) org-markers-to-move))))
  5325. (defun org-reinstall-markers-in-region (beg)
  5326. "Move all remembered markers to their position relative to BEG."
  5327. (mapc (lambda (x)
  5328. (move-marker (car x) (+ beg (cdr x))))
  5329. org-markers-to-move)
  5330. (setq org-markers-to-move nil))
  5331. (defun org-narrow-to-subtree ()
  5332. "Narrow buffer to the current subtree."
  5333. (interactive)
  5334. (save-excursion
  5335. (save-match-data
  5336. (narrow-to-region
  5337. (progn (org-back-to-heading) (point))
  5338. (progn (org-end-of-subtree t) (point))))))
  5339. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5340. "Clone the task (subtree) at point N times.
  5341. The clones will be inserted as siblings.
  5342. In interactive use, the user will be prompted for the number of clones
  5343. to be produced, and for a time SHIFT, which may be a repeater as used
  5344. in time stamps, for example `+3d'.
  5345. When a valid repeater is given and the entry contains any time stamps,
  5346. the clones will become a sequence in time, with time stamps in the
  5347. subtree shifted for each clone produced. If SHIFT is nil or the
  5348. empty string, time stamps will be left alone.
  5349. If the original subtree did contain time stamps with a repeater,
  5350. the following will happen:
  5351. - the repeater will be removed in each clone
  5352. - an additional clone will be produced, with the current, unshifted
  5353. date(s) in the entry.
  5354. - the original entry will be placed *after* all the clones, with
  5355. repeater intact.
  5356. - the start days in the repeater in the original entry will be shifted
  5357. to past the last clone.
  5358. I this way you can spell out a number of instances of a repeating task,
  5359. and still retain the repeater to cover future instances of the task."
  5360. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5361. (let (beg end template task
  5362. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5363. (if (not (and (integerp n) (> n 0)))
  5364. (error "Invalid number of replications %s" n))
  5365. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5366. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5367. shift)))
  5368. (error "Invalid shift specification %s" shift))
  5369. (when doshift
  5370. (setq shift-n (string-to-number (match-string 1 shift))
  5371. shift-what (cdr (assoc (match-string 2 shift)
  5372. '(("d" . day) ("w" . week)
  5373. ("m" . month) ("y" . year))))))
  5374. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5375. (setq nmin 1 nmax n)
  5376. (org-back-to-heading t)
  5377. (setq beg (point))
  5378. (org-end-of-subtree t t)
  5379. (setq end (point))
  5380. (setq template (buffer-substring beg end))
  5381. (when (and doshift
  5382. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5383. (delete-region beg end)
  5384. (setq end beg)
  5385. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5386. (goto-char end)
  5387. (loop for n from nmin to nmax do
  5388. (if (not doshift)
  5389. (setq task template)
  5390. (with-temp-buffer
  5391. (insert template)
  5392. (org-mode)
  5393. (goto-char (point-min))
  5394. (while (re-search-forward org-ts-regexp nil t)
  5395. (org-timestamp-change (* n shift-n) shift-what))
  5396. (unless (= n n-no-remove)
  5397. (goto-char (point-min))
  5398. (while (re-search-forward org-ts-regexp nil t)
  5399. (save-excursion
  5400. (goto-char (match-beginning 0))
  5401. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5402. (delete-region (match-beginning 1) (match-end 1))))))
  5403. (setq task (buffer-string))))
  5404. (insert task))
  5405. (goto-char beg)))
  5406. ;;; Outline Sorting
  5407. (defun org-sort (with-case)
  5408. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5409. Optional argument WITH-CASE means sort case-sensitively.
  5410. With a double prefix argument, also remove duplicate entries."
  5411. (interactive "P")
  5412. (if (org-at-table-p)
  5413. (org-call-with-arg 'org-table-sort-lines with-case)
  5414. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5415. (defun org-sort-remove-invisible (s)
  5416. (remove-text-properties 0 (length s) org-rm-props s)
  5417. (while (string-match org-bracket-link-regexp s)
  5418. (setq s (replace-match (if (match-end 2)
  5419. (match-string 3 s)
  5420. (match-string 1 s)) t t s)))
  5421. s)
  5422. (defvar org-priority-regexp) ; defined later in the file
  5423. (defun org-sort-entries-or-items
  5424. (&optional with-case sorting-type getkey-func compare-func property)
  5425. "Sort entries on a certain level of an outline tree.
  5426. If there is an active region, the entries in the region are sorted.
  5427. Else, if the cursor is before the first entry, sort the top-level items.
  5428. Else, the children of the entry at point are sorted.
  5429. Sorting can be alphabetically, numerically, and by date/time as given by
  5430. the first time stamp in the entry. The command prompts for the sorting
  5431. type unless it has been given to the function through the SORTING-TYPE
  5432. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5433. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5434. called with point at the beginning of the record. It must return either
  5435. a string or a number that should serve as the sorting key for that record.
  5436. Comparing entries ignores case by default. However, with an optional argument
  5437. WITH-CASE, the sorting considers case as well."
  5438. (interactive "P")
  5439. (let ((case-func (if with-case 'identity 'downcase))
  5440. start beg end stars re re2
  5441. txt what tmp plain-list-p)
  5442. ;; Find beginning and end of region to sort
  5443. (cond
  5444. ((org-region-active-p)
  5445. ;; we will sort the region
  5446. (setq end (region-end)
  5447. what "region")
  5448. (goto-char (region-beginning))
  5449. (if (not (org-on-heading-p)) (outline-next-heading))
  5450. (setq start (point)))
  5451. ((org-at-item-p)
  5452. ;; we will sort this plain list
  5453. (org-beginning-of-item-list) (setq start (point))
  5454. (org-end-of-item-list) (setq end (point))
  5455. (goto-char start)
  5456. (setq plain-list-p t
  5457. what "plain list"))
  5458. ((or (org-on-heading-p)
  5459. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5460. ;; we will sort the children of the current headline
  5461. (org-back-to-heading)
  5462. (setq start (point)
  5463. end (progn (org-end-of-subtree t t)
  5464. (org-back-over-empty-lines)
  5465. (point))
  5466. what "children")
  5467. (goto-char start)
  5468. (show-subtree)
  5469. (outline-next-heading))
  5470. (t
  5471. ;; we will sort the top-level entries in this file
  5472. (goto-char (point-min))
  5473. (or (org-on-heading-p) (outline-next-heading))
  5474. (setq start (point) end (point-max) what "top-level")
  5475. (goto-char start)
  5476. (show-all)))
  5477. (setq beg (point))
  5478. (if (>= beg end) (error "Nothing to sort"))
  5479. (unless plain-list-p
  5480. (looking-at "\\(\\*+\\)")
  5481. (setq stars (match-string 1)
  5482. re (concat "^" (regexp-quote stars) " +")
  5483. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5484. txt (buffer-substring beg end))
  5485. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5486. (if (and (not (equal stars "*")) (string-match re2 txt))
  5487. (error "Region to sort contains a level above the first entry")))
  5488. (unless sorting-type
  5489. (message
  5490. (if plain-list-p
  5491. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5492. "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:")
  5493. what)
  5494. (setq sorting-type (read-char-exclusive))
  5495. (and (= (downcase sorting-type) ?f)
  5496. (setq getkey-func
  5497. (org-ido-completing-read "Sort using function: "
  5498. obarray 'fboundp t nil nil))
  5499. (setq getkey-func (intern getkey-func)))
  5500. (and (= (downcase sorting-type) ?r)
  5501. (setq property
  5502. (org-ido-completing-read "Property: "
  5503. (mapcar 'list (org-buffer-property-keys t))
  5504. nil t))))
  5505. (message "Sorting entries...")
  5506. (save-restriction
  5507. (narrow-to-region start end)
  5508. (let ((dcst (downcase sorting-type))
  5509. (case-fold-search nil)
  5510. (now (current-time)))
  5511. (sort-subr
  5512. (/= dcst sorting-type)
  5513. ;; This function moves to the beginning character of the "record" to
  5514. ;; be sorted.
  5515. (if plain-list-p
  5516. (lambda nil
  5517. (if (org-at-item-p) t (goto-char (point-max))))
  5518. (lambda nil
  5519. (if (re-search-forward re nil t)
  5520. (goto-char (match-beginning 0))
  5521. (goto-char (point-max)))))
  5522. ;; This function moves to the last character of the "record" being
  5523. ;; sorted.
  5524. (if plain-list-p
  5525. 'org-end-of-item
  5526. (lambda nil
  5527. (save-match-data
  5528. (condition-case nil
  5529. (outline-forward-same-level 1)
  5530. (error
  5531. (goto-char (point-max)))))))
  5532. ;; This function returns the value that gets sorted against.
  5533. (if plain-list-p
  5534. (lambda nil
  5535. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5536. (cond
  5537. ((= dcst ?n)
  5538. (string-to-number (buffer-substring (match-end 0)
  5539. (point-at-eol))))
  5540. ((= dcst ?a)
  5541. (buffer-substring (match-end 0) (point-at-eol)))
  5542. ((= dcst ?t)
  5543. (if (re-search-forward org-ts-regexp
  5544. (point-at-eol) t)
  5545. (org-time-string-to-time (match-string 0))
  5546. now))
  5547. ((= dcst ?f)
  5548. (if getkey-func
  5549. (progn
  5550. (setq tmp (funcall getkey-func))
  5551. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5552. tmp)
  5553. (error "Invalid key function `%s'" getkey-func)))
  5554. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5555. (lambda nil
  5556. (cond
  5557. ((= dcst ?n)
  5558. (if (looking-at org-complex-heading-regexp)
  5559. (string-to-number (match-string 4))
  5560. nil))
  5561. ((= dcst ?a)
  5562. (if (looking-at org-complex-heading-regexp)
  5563. (funcall case-func (match-string 4))
  5564. nil))
  5565. ((= dcst ?t)
  5566. (if (re-search-forward org-ts-regexp
  5567. (save-excursion
  5568. (forward-line 2)
  5569. (point)) t)
  5570. (org-time-string-to-time (match-string 0))
  5571. now))
  5572. ((= dcst ?p)
  5573. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5574. (string-to-char (match-string 2))
  5575. org-default-priority))
  5576. ((= dcst ?r)
  5577. (or (org-entry-get nil property) ""))
  5578. ((= dcst ?o)
  5579. (if (looking-at org-complex-heading-regexp)
  5580. (- 9999 (length (member (match-string 2)
  5581. org-todo-keywords-1)))))
  5582. ((= dcst ?f)
  5583. (if getkey-func
  5584. (progn
  5585. (setq tmp (funcall getkey-func))
  5586. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5587. tmp)
  5588. (error "Invalid key function `%s'" getkey-func)))
  5589. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5590. nil
  5591. (cond
  5592. ((= dcst ?a) 'string<)
  5593. ((= dcst ?t) 'time-less-p)
  5594. ((= dcst ?f) compare-func)
  5595. (t nil)))))
  5596. (message "Sorting entries...done")))
  5597. (defun org-do-sort (table what &optional with-case sorting-type)
  5598. "Sort TABLE of WHAT according to SORTING-TYPE.
  5599. The user will be prompted for the SORTING-TYPE if the call to this
  5600. function does not specify it. WHAT is only for the prompt, to indicate
  5601. what is being sorted. The sorting key will be extracted from
  5602. the car of the elements of the table.
  5603. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5604. (unless sorting-type
  5605. (message
  5606. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5607. what)
  5608. (setq sorting-type (read-char-exclusive)))
  5609. (let ((dcst (downcase sorting-type))
  5610. extractfun comparefun)
  5611. ;; Define the appropriate functions
  5612. (cond
  5613. ((= dcst ?n)
  5614. (setq extractfun 'string-to-number
  5615. comparefun (if (= dcst sorting-type) '< '>)))
  5616. ((= dcst ?a)
  5617. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5618. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5619. comparefun (if (= dcst sorting-type)
  5620. 'string<
  5621. (lambda (a b) (and (not (string< a b))
  5622. (not (string= a b)))))))
  5623. ((= dcst ?t)
  5624. (setq extractfun
  5625. (lambda (x)
  5626. (if (or (string-match org-ts-regexp x)
  5627. (string-match org-ts-regexp-both x))
  5628. (time-to-seconds
  5629. (org-time-string-to-time (match-string 0 x)))
  5630. 0))
  5631. comparefun (if (= dcst sorting-type) '< '>)))
  5632. (t (error "Invalid sorting type `%c'" sorting-type)))
  5633. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5634. table)
  5635. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5636. ;;; Editing source examples
  5637. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5638. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5639. (defvar org-edit-src-force-single-line nil)
  5640. (defvar org-edit-src-from-org-mode nil)
  5641. (defvar org-edit-src-picture nil)
  5642. (define-minor-mode org-exit-edit-mode
  5643. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5644. (defun org-edit-src-code ()
  5645. "Edit the source code example at point.
  5646. An indirect buffer is created, and that buffer is then narrowed to the
  5647. example at point and switched to the correct language mode. When done,
  5648. exit by killing the buffer with \\[org-edit-src-exit]."
  5649. (interactive)
  5650. (let ((line (org-current-line))
  5651. (case-fold-search t)
  5652. (msg (substitute-command-keys
  5653. "Edit, then exit with C-c ' (C-c and single quote)"))
  5654. (info (org-edit-src-find-region-and-lang))
  5655. (org-mode-p (eq major-mode 'org-mode))
  5656. beg end lang lang-f single lfmt)
  5657. (if (not info)
  5658. nil
  5659. (setq beg (nth 0 info)
  5660. end (nth 1 info)
  5661. lang (nth 2 info)
  5662. single (nth 3 info)
  5663. lfmt (nth 4 info)
  5664. lang-f (intern (concat lang "-mode")))
  5665. (unless (functionp lang-f)
  5666. (error "No such language mode: %s" lang-f))
  5667. (goto-line line)
  5668. (if (get-buffer "*Org Edit Src Example*")
  5669. (kill-buffer "*Org Edit Src Example*"))
  5670. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5671. "*Org Edit Src Example*"))
  5672. (narrow-to-region beg end)
  5673. (remove-text-properties beg end '(display nil invisible nil
  5674. intangible nil))
  5675. (let ((org-inhibit-startup t))
  5676. (funcall lang-f))
  5677. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5678. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5679. (when lfmt
  5680. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5681. (when org-mode-p
  5682. (goto-char (point-min))
  5683. (while (re-search-forward "^," nil t)
  5684. (replace-match "")))
  5685. (goto-line line)
  5686. (org-exit-edit-mode)
  5687. (org-set-local 'header-line-format msg)
  5688. (message "%s" msg)
  5689. t)))
  5690. (defun org-edit-fixed-width-region ()
  5691. "Edit the fixed-width ascii drawing at point.
  5692. This must be a region where each line starts with a colon followed by
  5693. a space character.
  5694. An indirect buffer is created, and that buffer is then narrowed to the
  5695. example at point and switched to artist-mode. When done,
  5696. exit by killing the buffer with \\[org-edit-src-exit]."
  5697. (interactive)
  5698. (let ((line (org-current-line))
  5699. (case-fold-search t)
  5700. (msg (substitute-command-keys
  5701. "Edit, then exit with C-c ' (C-c and single quote)"))
  5702. (org-mode-p (eq major-mode 'org-mode))
  5703. beg end)
  5704. (beginning-of-line 1)
  5705. (if (looking-at "[ \t]*[^:\n \t]")
  5706. nil
  5707. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5708. (setq beg (point) end beg)
  5709. (save-excursion
  5710. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5711. (setq beg (point-at-bol 2))
  5712. (setq beg (point))))
  5713. (save-excursion
  5714. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5715. (setq end (1- (match-beginning 0)))
  5716. (setq end (point))))
  5717. (goto-line line))
  5718. (if (get-buffer "*Org Edit Picture*")
  5719. (kill-buffer "*Org Edit Picture*"))
  5720. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5721. "*Org Edit Picture*"))
  5722. (narrow-to-region beg end)
  5723. (remove-text-properties beg end '(display nil invisible nil
  5724. intangible nil))
  5725. (when (fboundp 'font-lock-unfontify-region)
  5726. (font-lock-unfontify-region (point-min) (point-max)))
  5727. (cond
  5728. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5729. (fundamental-mode)
  5730. (artist-mode 1))
  5731. (t (funcall org-edit-fixed-width-region-mode)))
  5732. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5733. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5734. (set (make-local-variable 'org-edit-src-picture) t)
  5735. (goto-char (point-min))
  5736. (while (re-search-forward "^[ \t]*: ?" nil t)
  5737. (replace-match ""))
  5738. (goto-line line)
  5739. (org-exit-edit-mode)
  5740. (org-set-local 'header-line-format msg)
  5741. (message "%s" msg)
  5742. t)))
  5743. (defun org-edit-src-find-region-and-lang ()
  5744. "Find the region and language for a local edit.
  5745. Return a list with beginning and end of the region, a string representing
  5746. the language, a switch telling of the content should be in a single line."
  5747. (let ((re-list
  5748. (append
  5749. org-edit-src-region-extra
  5750. '(
  5751. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5752. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5753. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5754. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5755. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5756. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5757. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5758. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5759. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5760. ("^#\\+html:" "\n" "html" single-line)
  5761. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5762. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5763. ("^#\\+latex:" "\n" "latex" single-line)
  5764. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5765. ("^#\\+ascii:" "\n" "ascii" single-line)
  5766. )))
  5767. (pos (point))
  5768. re1 re2 single beg end lang lfmt match-re1)
  5769. (catch 'exit
  5770. (while (setq entry (pop re-list))
  5771. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5772. single (nth 3 entry))
  5773. (save-excursion
  5774. (if (or (looking-at re1)
  5775. (re-search-backward re1 nil t))
  5776. (progn
  5777. (setq match-re1 (match-string 0))
  5778. (setq beg (match-end 0)
  5779. lang (org-edit-src-get-lang lang)
  5780. lfmt (org-edit-src-get-label-format match-re1))
  5781. (if (and (re-search-forward re2 nil t)
  5782. (>= (match-end 0) pos))
  5783. (throw 'exit (list beg (match-beginning 0)
  5784. lang single lfmt))))
  5785. (if (or (looking-at re2)
  5786. (re-search-forward re2 nil t))
  5787. (progn
  5788. (setq end (match-beginning 0))
  5789. (if (and (re-search-backward re1 nil t)
  5790. (<= (match-beginning 0) pos))
  5791. (progn
  5792. (setq lfmt (org-edit-src-get-label-format
  5793. (match-string 0)))
  5794. (throw 'exit
  5795. (list (match-end 0) end
  5796. (org-edit-src-get-lang lang)
  5797. single lfmt))))))))))))
  5798. (defun org-edit-src-get-lang (lang)
  5799. "Extract the src language."
  5800. (let ((m (match-string 0)))
  5801. (cond
  5802. ((stringp lang) lang)
  5803. ((integerp lang) (match-string lang))
  5804. ((and (eq lang 'lang)
  5805. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5806. (match-string 1 m))
  5807. ((and (eq lang 'style)
  5808. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5809. (match-string 1 m))
  5810. (t "fundamental"))))
  5811. (defun org-edit-src-get-label-format (s)
  5812. "Extract the label format."
  5813. (save-match-data
  5814. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5815. (match-string 1 s))))
  5816. (defun org-edit-src-exit ()
  5817. "Exit special edit and protect problematic lines."
  5818. (interactive)
  5819. (unless (buffer-base-buffer (current-buffer))
  5820. (error "This is not an indirect buffer, something is wrong..."))
  5821. (unless (> (point-min) 1)
  5822. (error "This buffer is not narrowed, something is wrong..."))
  5823. (goto-char (point-min))
  5824. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5825. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5826. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5827. (goto-char (point-min))
  5828. (while (re-search-forward "\n" nil t)
  5829. (replace-match " "))
  5830. (goto-char (point-min))
  5831. (if (looking-at "\\s-*") (replace-match " "))
  5832. (if (re-search-forward "\\s-+\\'" nil t)
  5833. (replace-match "")))
  5834. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5835. (goto-char (point-min))
  5836. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5837. (replace-match ",\\1"))
  5838. (when font-lock-mode
  5839. (font-lock-unfontify-region (point-min) (point-max)))
  5840. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5841. (when (org-bound-and-true-p org-edit-src-picture)
  5842. (untabify (point-min) (point-max))
  5843. (goto-char (point-min))
  5844. (while (re-search-forward "^" nil t)
  5845. (replace-match ": "))
  5846. (when font-lock-mode
  5847. (font-lock-unfontify-region (point-min) (point-max)))
  5848. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5849. (kill-buffer (current-buffer))
  5850. (and (org-mode-p) (org-restart-font-lock)))
  5851. ;;; The orgstruct minor mode
  5852. ;; Define a minor mode which can be used in other modes in order to
  5853. ;; integrate the org-mode structure editing commands.
  5854. ;; This is really a hack, because the org-mode structure commands use
  5855. ;; keys which normally belong to the major mode. Here is how it
  5856. ;; works: The minor mode defines all the keys necessary to operate the
  5857. ;; structure commands, but wraps the commands into a function which
  5858. ;; tests if the cursor is currently at a headline or a plain list
  5859. ;; item. If that is the case, the structure command is used,
  5860. ;; temporarily setting many Org-mode variables like regular
  5861. ;; expressions for filling etc. However, when any of those keys is
  5862. ;; used at a different location, function uses `key-binding' to look
  5863. ;; up if the key has an associated command in another currently active
  5864. ;; keymap (minor modes, major mode, global), and executes that
  5865. ;; command. There might be problems if any of the keys is otherwise
  5866. ;; used as a prefix key.
  5867. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5868. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5869. ;; addresses this by checking explicitly for both bindings.
  5870. (defvar orgstruct-mode-map (make-sparse-keymap)
  5871. "Keymap for the minor `orgstruct-mode'.")
  5872. (defvar org-local-vars nil
  5873. "List of local variables, for use by `orgstruct-mode'")
  5874. ;;;###autoload
  5875. (define-minor-mode orgstruct-mode
  5876. "Toggle the minor more `orgstruct-mode'.
  5877. This mode is for using Org-mode structure commands in other modes.
  5878. The following key behave as if Org-mode was active, if the cursor
  5879. is on a headline, or on a plain list item (both in the definition
  5880. of Org-mode).
  5881. M-up Move entry/item up
  5882. M-down Move entry/item down
  5883. M-left Promote
  5884. M-right Demote
  5885. M-S-up Move entry/item up
  5886. M-S-down Move entry/item down
  5887. M-S-left Promote subtree
  5888. M-S-right Demote subtree
  5889. M-q Fill paragraph and items like in Org-mode
  5890. C-c ^ Sort entries
  5891. C-c - Cycle list bullet
  5892. TAB Cycle item visibility
  5893. M-RET Insert new heading/item
  5894. S-M-RET Insert new TODO heading / Checkbox item
  5895. C-c C-c Set tags / toggle checkbox"
  5896. nil " OrgStruct" nil
  5897. (org-load-modules-maybe)
  5898. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5899. ;;;###autoload
  5900. (defun turn-on-orgstruct ()
  5901. "Unconditionally turn on `orgstruct-mode'."
  5902. (orgstruct-mode 1))
  5903. (defun orgstruct++-mode (&optional arg)
  5904. "Toggle `orgstruct-mode', the enhanced version of it.
  5905. In addition to setting orgstruct-mode, this also exports all indentation and
  5906. autofilling variables from org-mode into the buffer. It will also
  5907. recognize item context in multiline items.
  5908. Note that turning off orgstruct-mode will *not* remove the
  5909. indentation/paragraph settings. This can only be done by refreshing the
  5910. major mode, for example with \[normal-mode]."
  5911. (interactive "P")
  5912. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5913. (if (< arg 1)
  5914. (orgstruct-mode -1)
  5915. (orgstruct-mode 1)
  5916. (let (var val)
  5917. (mapc
  5918. (lambda (x)
  5919. (when (string-match
  5920. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5921. (symbol-name (car x)))
  5922. (setq var (car x) val (nth 1 x))
  5923. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5924. org-local-vars)
  5925. (org-set-local 'orgstruct-is-++ t))))
  5926. (defvar orgstruct-is-++ nil
  5927. "Is orgstruct-mode in ++ version in the current-buffer?")
  5928. (make-variable-buffer-local 'orgstruct-is-++)
  5929. ;;;###autoload
  5930. (defun turn-on-orgstruct++ ()
  5931. "Unconditionally turn on `orgstruct++-mode'."
  5932. (orgstruct++-mode 1))
  5933. (defun orgstruct-error ()
  5934. "Error when there is no default binding for a structure key."
  5935. (interactive)
  5936. (error "This key has no function outside structure elements"))
  5937. (defun orgstruct-setup ()
  5938. "Setup orgstruct keymaps."
  5939. (let ((nfunc 0)
  5940. (bindings
  5941. (list
  5942. '([(meta up)] org-metaup)
  5943. '([(meta down)] org-metadown)
  5944. '([(meta left)] org-metaleft)
  5945. '([(meta right)] org-metaright)
  5946. '([(meta shift up)] org-shiftmetaup)
  5947. '([(meta shift down)] org-shiftmetadown)
  5948. '([(meta shift left)] org-shiftmetaleft)
  5949. '([(meta shift right)] org-shiftmetaright)
  5950. '([(shift up)] org-shiftup)
  5951. '([(shift down)] org-shiftdown)
  5952. '([(shift left)] org-shiftleft)
  5953. '([(shift right)] org-shiftright)
  5954. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5955. '("\M-q" fill-paragraph)
  5956. '("\C-c^" org-sort)
  5957. '("\C-c-" org-cycle-list-bullet)))
  5958. elt key fun cmd)
  5959. (while (setq elt (pop bindings))
  5960. (setq nfunc (1+ nfunc))
  5961. (setq key (org-key (car elt))
  5962. fun (nth 1 elt)
  5963. cmd (orgstruct-make-binding fun nfunc key))
  5964. (org-defkey orgstruct-mode-map key cmd))
  5965. ;; Special treatment needed for TAB and RET
  5966. (org-defkey orgstruct-mode-map [(tab)]
  5967. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5968. (org-defkey orgstruct-mode-map "\C-i"
  5969. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5970. (org-defkey orgstruct-mode-map "\M-\C-m"
  5971. (orgstruct-make-binding 'org-insert-heading 105
  5972. "\M-\C-m" [(meta return)]))
  5973. (org-defkey orgstruct-mode-map [(meta return)]
  5974. (orgstruct-make-binding 'org-insert-heading 106
  5975. [(meta return)] "\M-\C-m"))
  5976. (org-defkey orgstruct-mode-map [(shift meta return)]
  5977. (orgstruct-make-binding 'org-insert-todo-heading 107
  5978. [(meta return)] "\M-\C-m"))
  5979. (unless org-local-vars
  5980. (setq org-local-vars (org-get-local-variables)))
  5981. t))
  5982. (defun orgstruct-make-binding (fun n &rest keys)
  5983. "Create a function for binding in the structure minor mode.
  5984. FUN is the command to call inside a table. N is used to create a unique
  5985. command name. KEYS are keys that should be checked in for a command
  5986. to execute outside of tables."
  5987. (eval
  5988. (list 'defun
  5989. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5990. '(arg)
  5991. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5992. "Outside of structure, run the binding of `"
  5993. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5994. "'.")
  5995. '(interactive "p")
  5996. (list 'if
  5997. `(org-context-p 'headline 'item
  5998. (and orgstruct-is-++
  5999. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6000. 'item-body))
  6001. (list 'org-run-like-in-org-mode (list 'quote fun))
  6002. (list 'let '(orgstruct-mode)
  6003. (list 'call-interactively
  6004. (append '(or)
  6005. (mapcar (lambda (k)
  6006. (list 'key-binding k))
  6007. keys)
  6008. '('orgstruct-error))))))))
  6009. (defun org-context-p (&rest contexts)
  6010. "Check if local context is any of CONTEXTS.
  6011. Possible values in the list of contexts are `table', `headline', and `item'."
  6012. (let ((pos (point)))
  6013. (goto-char (point-at-bol))
  6014. (prog1 (or (and (memq 'table contexts)
  6015. (looking-at "[ \t]*|"))
  6016. (and (memq 'headline contexts)
  6017. ;;????????? (looking-at "\\*+"))
  6018. (looking-at outline-regexp))
  6019. (and (memq 'item contexts)
  6020. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6021. (and (memq 'item-body contexts)
  6022. (org-in-item-p)))
  6023. (goto-char pos))))
  6024. (defun org-get-local-variables ()
  6025. "Return a list of all local variables in an org-mode buffer."
  6026. (let (varlist)
  6027. (with-current-buffer (get-buffer-create "*Org tmp*")
  6028. (erase-buffer)
  6029. (org-mode)
  6030. (setq varlist (buffer-local-variables)))
  6031. (kill-buffer "*Org tmp*")
  6032. (delq nil
  6033. (mapcar
  6034. (lambda (x)
  6035. (setq x
  6036. (if (symbolp x)
  6037. (list x)
  6038. (list (car x) (list 'quote (cdr x)))))
  6039. (if (string-match
  6040. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6041. (symbol-name (car x)))
  6042. x nil))
  6043. varlist))))
  6044. ;;;###autoload
  6045. (defun org-run-like-in-org-mode (cmd)
  6046. "Run a command, pretending that the current buffer is in Org-mode.
  6047. This will temporarily bind local variables that are typically bound in
  6048. Org-mode to the values they have in Org-mode, and then interactively
  6049. call CMD."
  6050. (org-load-modules-maybe)
  6051. (unless org-local-vars
  6052. (setq org-local-vars (org-get-local-variables)))
  6053. (eval (list 'let org-local-vars
  6054. (list 'call-interactively (list 'quote cmd)))))
  6055. ;;;; Archiving
  6056. (defun org-get-category (&optional pos)
  6057. "Get the category applying to position POS."
  6058. (get-text-property (or pos (point)) 'org-category))
  6059. (defun org-refresh-category-properties ()
  6060. "Refresh category text properties in the buffer."
  6061. (let ((def-cat (cond
  6062. ((null org-category)
  6063. (if buffer-file-name
  6064. (file-name-sans-extension
  6065. (file-name-nondirectory buffer-file-name))
  6066. "???"))
  6067. ((symbolp org-category) (symbol-name org-category))
  6068. (t org-category)))
  6069. beg end cat pos optionp)
  6070. (org-unmodified
  6071. (save-excursion
  6072. (save-restriction
  6073. (widen)
  6074. (goto-char (point-min))
  6075. (put-text-property (point) (point-max) 'org-category def-cat)
  6076. (while (re-search-forward
  6077. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6078. (setq pos (match-end 0)
  6079. optionp (equal (char-after (match-beginning 0)) ?#)
  6080. cat (org-trim (match-string 2)))
  6081. (if optionp
  6082. (setq beg (point-at-bol) end (point-max))
  6083. (org-back-to-heading t)
  6084. (setq beg (point) end (org-end-of-subtree t t)))
  6085. (put-text-property beg end 'org-category cat)
  6086. (goto-char pos)))))))
  6087. ;;;; Link Stuff
  6088. ;;; Link abbreviations
  6089. (defun org-link-expand-abbrev (link)
  6090. "Apply replacements as defined in `org-link-abbrev-alist."
  6091. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6092. (let* ((key (match-string 1 link))
  6093. (as (or (assoc key org-link-abbrev-alist-local)
  6094. (assoc key org-link-abbrev-alist)))
  6095. (tag (and (match-end 2) (match-string 3 link)))
  6096. rpl)
  6097. (if (not as)
  6098. link
  6099. (setq rpl (cdr as))
  6100. (cond
  6101. ((symbolp rpl) (funcall rpl tag))
  6102. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6103. ((string-match "%h" rpl)
  6104. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6105. (t (concat rpl tag)))))
  6106. link))
  6107. ;;; Storing and inserting links
  6108. (defvar org-insert-link-history nil
  6109. "Minibuffer history for links inserted with `org-insert-link'.")
  6110. (defvar org-stored-links nil
  6111. "Contains the links stored with `org-store-link'.")
  6112. (defvar org-store-link-plist nil
  6113. "Plist with info about the most recently link created with `org-store-link'.")
  6114. (defvar org-link-protocols nil
  6115. "Link protocols added to Org-mode using `org-add-link-type'.")
  6116. (defvar org-store-link-functions nil
  6117. "List of functions that are called to create and store a link.
  6118. Each function will be called in turn until one returns a non-nil
  6119. value. Each function should check if it is responsible for creating
  6120. this link (for example by looking at the major mode).
  6121. If not, it must exit and return nil.
  6122. If yes, it should return a non-nil value after a calling
  6123. `org-store-link-props' with a list of properties and values.
  6124. Special properties are:
  6125. :type The link prefix. like \"http\". This must be given.
  6126. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6127. This is obligatory as well.
  6128. :description Optional default description for the second pair
  6129. of brackets in an Org-mode link. The user can still change
  6130. this when inserting this link into an Org-mode buffer.
  6131. In addition to these, any additional properties can be specified
  6132. and then used in remember templates.")
  6133. (defun org-add-link-type (type &optional follow export)
  6134. "Add TYPE to the list of `org-link-types'.
  6135. Re-compute all regular expressions depending on `org-link-types'
  6136. FOLLOW and EXPORT are two functions.
  6137. FOLLOW should take the link path as the single argument and do whatever
  6138. is necessary to follow the link, for example find a file or display
  6139. a mail message.
  6140. EXPORT should format the link path for export to one of the export formats.
  6141. It should be a function accepting three arguments:
  6142. path the path of the link, the text after the prefix (like \"http:\")
  6143. desc the description of the link, if any, nil if there was no description
  6144. format the export format, a symbol like `html' or `latex'.
  6145. The function may use the FORMAT information to return different values
  6146. depending on the format. The return value will be put literally into
  6147. the exported file.
  6148. Org-mode has a built-in default for exporting links. If you are happy with
  6149. this default, there is no need to define an export function for the link
  6150. type. For a simple example of an export function, see `org-bbdb.el'."
  6151. (add-to-list 'org-link-types type t)
  6152. (org-make-link-regexps)
  6153. (if (assoc type org-link-protocols)
  6154. (setcdr (assoc type org-link-protocols) (list follow export))
  6155. (push (list type follow export) org-link-protocols)))
  6156. ;;;###autoload
  6157. (defun org-store-link (arg)
  6158. "\\<org-mode-map>Store an org-link to the current location.
  6159. This link is added to `org-stored-links' and can later be inserted
  6160. into an org-buffer with \\[org-insert-link].
  6161. For some link types, a prefix arg is interpreted:
  6162. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6163. For file links, arg negates `org-context-in-file-links'."
  6164. (interactive "P")
  6165. (org-load-modules-maybe)
  6166. (setq org-store-link-plist nil) ; reset
  6167. (let (link cpltxt desc description search txt custom-id)
  6168. (cond
  6169. ((run-hook-with-args-until-success 'org-store-link-functions)
  6170. (setq link (plist-get org-store-link-plist :link)
  6171. desc (or (plist-get org-store-link-plist :description) link)))
  6172. ((equal (buffer-name) "*Org Edit Src Example*")
  6173. (let (label gc)
  6174. (while (or (not label)
  6175. (save-excursion
  6176. (save-restriction
  6177. (widen)
  6178. (goto-char (point-min))
  6179. (re-search-forward
  6180. (regexp-quote (format org-coderef-label-format label))
  6181. nil t))))
  6182. (when label (message "Label exists already") (sit-for 2))
  6183. (setq label (read-string "Code line label: " label)))
  6184. (end-of-line 1)
  6185. (setq link (format org-coderef-label-format label))
  6186. (setq gc (- 79 (length link)))
  6187. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6188. (insert link)
  6189. (setq link (concat "(" label ")") desc nil)))
  6190. ((eq major-mode 'calendar-mode)
  6191. (let ((cd (calendar-cursor-to-date)))
  6192. (setq link
  6193. (format-time-string
  6194. (car org-time-stamp-formats)
  6195. (apply 'encode-time
  6196. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6197. nil nil nil))))
  6198. (org-store-link-props :type "calendar" :date cd)))
  6199. ((eq major-mode 'w3-mode)
  6200. (setq cpltxt (if (and (buffer-name)
  6201. (not (string-match "Untitled" (buffer-name))))
  6202. (buffer-name)
  6203. (url-view-url t))
  6204. link (org-make-link (url-view-url t)))
  6205. (org-store-link-props :type "w3" :url (url-view-url t)))
  6206. ((eq major-mode 'w3m-mode)
  6207. (setq cpltxt (or w3m-current-title w3m-current-url)
  6208. link (org-make-link w3m-current-url))
  6209. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6210. ((setq search (run-hook-with-args-until-success
  6211. 'org-create-file-search-functions))
  6212. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6213. "::" search))
  6214. (setq cpltxt (or description link)))
  6215. ((eq major-mode 'image-mode)
  6216. (setq cpltxt (concat "file:"
  6217. (abbreviate-file-name buffer-file-name))
  6218. link (org-make-link cpltxt))
  6219. (org-store-link-props :type "image" :file buffer-file-name))
  6220. ((eq major-mode 'dired-mode)
  6221. ;; link to the file in the current line
  6222. (setq cpltxt (concat "file:"
  6223. (abbreviate-file-name
  6224. (expand-file-name
  6225. (dired-get-filename nil t))))
  6226. link (org-make-link cpltxt)))
  6227. ((and buffer-file-name (org-mode-p))
  6228. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  6229. (cond
  6230. ((org-in-regexp "<<\\(.*?\\)>>")
  6231. (setq cpltxt
  6232. (concat "file:"
  6233. (abbreviate-file-name buffer-file-name)
  6234. "::" (match-string 1))
  6235. link (org-make-link cpltxt)))
  6236. ((and (featurep 'org-id)
  6237. (or (eq org-link-to-org-use-id t)
  6238. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6239. (interactive-p))
  6240. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6241. (interactive-p)
  6242. (not custom-id))
  6243. (and org-link-to-org-use-id
  6244. (condition-case nil
  6245. (org-entry-get nil "ID")
  6246. (error nil)))))
  6247. ;; We can make a link using the ID.
  6248. (setq link (condition-case nil
  6249. (prog1 (org-id-store-link)
  6250. (setq desc (plist-get org-store-link-plist
  6251. :description)))
  6252. (error
  6253. ;; probably before first headline, link to file only
  6254. (concat "file:"
  6255. (abbreviate-file-name buffer-file-name))))))
  6256. (t
  6257. ;; Just link to current headline
  6258. (setq cpltxt (concat "file:"
  6259. (abbreviate-file-name buffer-file-name)))
  6260. ;; Add a context search string
  6261. (when (org-xor org-context-in-file-links arg)
  6262. (setq txt (cond
  6263. ((org-on-heading-p) nil)
  6264. ((org-region-active-p)
  6265. (buffer-substring (region-beginning) (region-end)))
  6266. (t nil)))
  6267. (when (or (null txt) (string-match "\\S-" txt))
  6268. (setq cpltxt
  6269. (concat cpltxt "::"
  6270. (condition-case nil
  6271. (org-make-org-heading-search-string txt)
  6272. (error "")))
  6273. desc "NONE")))
  6274. (if (string-match "::\\'" cpltxt)
  6275. (setq cpltxt (substring cpltxt 0 -2)))
  6276. (setq link (org-make-link cpltxt)))))
  6277. ((buffer-file-name (buffer-base-buffer))
  6278. ;; Just link to this file here.
  6279. (setq cpltxt (concat "file:"
  6280. (abbreviate-file-name
  6281. (buffer-file-name (buffer-base-buffer)))))
  6282. ;; Add a context string
  6283. (when (org-xor org-context-in-file-links arg)
  6284. (setq txt (if (org-region-active-p)
  6285. (buffer-substring (region-beginning) (region-end))
  6286. (buffer-substring (point-at-bol) (point-at-eol))))
  6287. ;; Only use search option if there is some text.
  6288. (when (string-match "\\S-" txt)
  6289. (setq cpltxt
  6290. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6291. desc "NONE")))
  6292. (setq link (org-make-link cpltxt)))
  6293. ((interactive-p)
  6294. (error "Cannot link to a buffer which is not visiting a file"))
  6295. (t (setq link nil)))
  6296. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6297. (setq link (or link cpltxt)
  6298. desc (or desc cpltxt))
  6299. (if (equal desc "NONE") (setq desc nil))
  6300. (if (and (interactive-p) link)
  6301. (progn
  6302. (setq org-stored-links
  6303. (cons (list link desc) org-stored-links))
  6304. (message "Stored: %s" (or desc link))
  6305. (when custom-id
  6306. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6307. "::#" custom-id))
  6308. (setq org-stored-links
  6309. (cons (list link desc) org-stored-links))))
  6310. (and link (org-make-link-string link desc)))))
  6311. (defun org-store-link-props (&rest plist)
  6312. "Store link properties, extract names and addresses."
  6313. (let (x adr)
  6314. (when (setq x (plist-get plist :from))
  6315. (setq adr (mail-extract-address-components x))
  6316. (setq plist (plist-put plist :fromname (car adr)))
  6317. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6318. (when (setq x (plist-get plist :to))
  6319. (setq adr (mail-extract-address-components x))
  6320. (setq plist (plist-put plist :toname (car adr)))
  6321. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6322. (let ((from (plist-get plist :from))
  6323. (to (plist-get plist :to)))
  6324. (when (and from to org-from-is-user-regexp)
  6325. (setq plist
  6326. (plist-put plist :fromto
  6327. (if (string-match org-from-is-user-regexp from)
  6328. (concat "to %t")
  6329. (concat "from %f"))))))
  6330. (setq org-store-link-plist plist))
  6331. (defun org-add-link-props (&rest plist)
  6332. "Add these properties to the link property list."
  6333. (let (key value)
  6334. (while plist
  6335. (setq key (pop plist) value (pop plist))
  6336. (setq org-store-link-plist
  6337. (plist-put org-store-link-plist key value)))))
  6338. (defun org-email-link-description (&optional fmt)
  6339. "Return the description part of an email link.
  6340. This takes information from `org-store-link-plist' and formats it
  6341. according to FMT (default from `org-email-link-description-format')."
  6342. (setq fmt (or fmt org-email-link-description-format))
  6343. (let* ((p org-store-link-plist)
  6344. (to (plist-get p :toaddress))
  6345. (from (plist-get p :fromaddress))
  6346. (table
  6347. (list
  6348. (cons "%c" (plist-get p :fromto))
  6349. (cons "%F" (plist-get p :from))
  6350. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6351. (cons "%T" (plist-get p :to))
  6352. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6353. (cons "%s" (plist-get p :subject))
  6354. (cons "%m" (plist-get p :message-id)))))
  6355. (when (string-match "%c" fmt)
  6356. ;; Check if the user wrote this message
  6357. (if (and org-from-is-user-regexp from to
  6358. (save-match-data (string-match org-from-is-user-regexp from)))
  6359. (setq fmt (replace-match "to %t" t t fmt))
  6360. (setq fmt (replace-match "from %f" t t fmt))))
  6361. (org-replace-escapes fmt table)))
  6362. (defun org-make-org-heading-search-string (&optional string heading)
  6363. "Make search string for STRING or current headline."
  6364. (interactive)
  6365. (let ((s (or string (org-get-heading))))
  6366. (unless (and string (not heading))
  6367. ;; We are using a headline, clean up garbage in there.
  6368. (if (string-match org-todo-regexp s)
  6369. (setq s (replace-match "" t t s)))
  6370. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6371. (setq s (replace-match "" t t s)))
  6372. (setq s (org-trim s))
  6373. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6374. org-comment-string "\\)") s)
  6375. (setq s (replace-match "" t t s)))
  6376. (while (string-match org-ts-regexp s)
  6377. (setq s (replace-match "" t t s))))
  6378. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6379. (setq s (replace-match " " t t s)))
  6380. (or string (setq s (concat "*" s))) ; Add * for headlines
  6381. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6382. (defun org-make-link (&rest strings)
  6383. "Concatenate STRINGS."
  6384. (apply 'concat strings))
  6385. (defun org-make-link-string (link &optional description)
  6386. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6387. (unless (string-match "\\S-" link)
  6388. (error "Empty link"))
  6389. (when (stringp description)
  6390. ;; Remove brackets from the description, they are fatal.
  6391. (while (string-match "\\[" description)
  6392. (setq description (replace-match "{" t t description)))
  6393. (while (string-match "\\]" description)
  6394. (setq description (replace-match "}" t t description))))
  6395. (when (equal (org-link-escape link) description)
  6396. ;; No description needed, it is identical
  6397. (setq description nil))
  6398. (when (and (not description)
  6399. (not (equal link (org-link-escape link))))
  6400. (setq description (org-extract-attributes link)))
  6401. (concat "[[" (org-link-escape link) "]"
  6402. (if description (concat "[" description "]") "")
  6403. "]"))
  6404. (defconst org-link-escape-chars
  6405. '((?\ . "%20")
  6406. (?\[ . "%5B")
  6407. (?\] . "%5D")
  6408. (?\340 . "%E0") ; `a
  6409. (?\342 . "%E2") ; ^a
  6410. (?\347 . "%E7") ; ,c
  6411. (?\350 . "%E8") ; `e
  6412. (?\351 . "%E9") ; 'e
  6413. (?\352 . "%EA") ; ^e
  6414. (?\356 . "%EE") ; ^i
  6415. (?\364 . "%F4") ; ^o
  6416. (?\371 . "%F9") ; `u
  6417. (?\373 . "%FB") ; ^u
  6418. (?\; . "%3B")
  6419. (?? . "%3F")
  6420. (?= . "%3D")
  6421. (?+ . "%2B")
  6422. )
  6423. "Association list of escapes for some characters problematic in links.
  6424. This is the list that is used for internal purposes.")
  6425. (defvar org-url-encoding-use-url-hexify nil)
  6426. (defconst org-link-escape-chars-browser
  6427. '((?\ . "%20")) ; 32 for the SPC char
  6428. "Association list of escapes for some characters problematic in links.
  6429. This is the list that is used before handing over to the browser.")
  6430. (defun org-link-escape (text &optional table)
  6431. "Escape characters in TEXT that are problematic for links."
  6432. (if org-url-encoding-use-url-hexify
  6433. (url-hexify-string text)
  6434. (setq table (or table org-link-escape-chars))
  6435. (when text
  6436. (let ((re (mapconcat (lambda (x) (regexp-quote
  6437. (char-to-string (car x))))
  6438. table "\\|")))
  6439. (while (string-match re text)
  6440. (setq text
  6441. (replace-match
  6442. (cdr (assoc (string-to-char (match-string 0 text))
  6443. table))
  6444. t t text)))
  6445. text))))
  6446. (defun org-link-unescape (text &optional table)
  6447. "Reverse the action of `org-link-escape'."
  6448. (if org-url-encoding-use-url-hexify
  6449. (url-unhex-string text)
  6450. (setq table (or table org-link-escape-chars))
  6451. (when text
  6452. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6453. table "\\|")))
  6454. (while (string-match re text)
  6455. (setq text
  6456. (replace-match
  6457. (char-to-string (car (rassoc (match-string 0 text) table)))
  6458. t t text)))
  6459. text))))
  6460. (defun org-xor (a b)
  6461. "Exclusive or."
  6462. (if a (not b) b))
  6463. (defun org-fixup-message-id-for-http (s)
  6464. "Replace special characters in a message id, so it can be used in an http query."
  6465. (while (string-match "<" s)
  6466. (setq s (replace-match "%3C" t t s)))
  6467. (while (string-match ">" s)
  6468. (setq s (replace-match "%3E" t t s)))
  6469. (while (string-match "@" s)
  6470. (setq s (replace-match "%40" t t s)))
  6471. s)
  6472. ;;;###autoload
  6473. (defun org-insert-link-global ()
  6474. "Insert a link like Org-mode does.
  6475. This command can be called in any mode to insert a link in Org-mode syntax."
  6476. (interactive)
  6477. (org-load-modules-maybe)
  6478. (org-run-like-in-org-mode 'org-insert-link))
  6479. (defun org-insert-link (&optional complete-file link-location)
  6480. "Insert a link. At the prompt, enter the link.
  6481. Completion can be used to insert any of the link protocol prefixes like
  6482. http or ftp in use.
  6483. The history can be used to select a link previously stored with
  6484. `org-store-link'. When the empty string is entered (i.e. if you just
  6485. press RET at the prompt), the link defaults to the most recently
  6486. stored link. As SPC triggers completion in the minibuffer, you need to
  6487. use M-SPC or C-q SPC to force the insertion of a space character.
  6488. You will also be prompted for a description, and if one is given, it will
  6489. be displayed in the buffer instead of the link.
  6490. If there is already a link at point, this command will allow you to edit link
  6491. and description parts.
  6492. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6493. be selected using completion. The path to the file will be relative to the
  6494. current directory if the file is in the current directory or a subdirectory.
  6495. Otherwise, the link will be the absolute path as completed in the minibuffer
  6496. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6497. option `org-link-file-path-type'.
  6498. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6499. the current directory or below.
  6500. With three \\[universal-argument] prefixes, negate the meaning of
  6501. `org-keep-stored-link-after-insertion'.
  6502. If `org-make-link-description-function' is non-nil, this function will be
  6503. called with the link target, and the result will be the default
  6504. link description.
  6505. If the LINK-LOCATION parameter is non-nil, this value will be
  6506. used as the link location instead of reading one interactively."
  6507. (interactive "P")
  6508. (let* ((wcf (current-window-configuration))
  6509. (region (if (org-region-active-p)
  6510. (buffer-substring (region-beginning) (region-end))))
  6511. (remove (and region (list (region-beginning) (region-end))))
  6512. (desc region)
  6513. tmphist ; byte-compile incorrectly complains about this
  6514. (link link-location)
  6515. entry file)
  6516. (cond
  6517. (link-location) ; specified by arg, just use it.
  6518. ((org-in-regexp org-bracket-link-regexp 1)
  6519. ;; We do have a link at point, and we are going to edit it.
  6520. (setq remove (list (match-beginning 0) (match-end 0)))
  6521. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6522. (setq link (read-string "Link: "
  6523. (org-link-unescape
  6524. (org-match-string-no-properties 1)))))
  6525. ((or (org-in-regexp org-angle-link-re)
  6526. (org-in-regexp org-plain-link-re))
  6527. ;; Convert to bracket link
  6528. (setq remove (list (match-beginning 0) (match-end 0))
  6529. link (read-string "Link: "
  6530. (org-remove-angle-brackets (match-string 0)))))
  6531. ((member complete-file '((4) (16)))
  6532. ;; Completing read for file names.
  6533. (setq file (read-file-name "File: "))
  6534. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6535. (pwd1 (file-name-as-directory (abbreviate-file-name
  6536. (expand-file-name ".")))))
  6537. (cond
  6538. ((equal complete-file '(16))
  6539. (setq link (org-make-link
  6540. "file:"
  6541. (abbreviate-file-name (expand-file-name file)))))
  6542. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6543. (setq link (org-make-link "file:" (match-string 1 file))))
  6544. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6545. (expand-file-name file))
  6546. (setq link (org-make-link
  6547. "file:" (match-string 1 (expand-file-name file)))))
  6548. (t (setq link (org-make-link "file:" file))))))
  6549. (t
  6550. ;; Read link, with completion for stored links.
  6551. (with-output-to-temp-buffer "*Org Links*"
  6552. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6553. (when org-stored-links
  6554. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6555. (princ (mapconcat
  6556. (lambda (x)
  6557. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6558. (reverse org-stored-links) "\n"))))
  6559. (let ((cw (selected-window)))
  6560. (select-window (get-buffer-window "*Org Links*"))
  6561. (setq truncate-lines t)
  6562. (org-fit-window-to-buffer)
  6563. (select-window cw))
  6564. ;; Fake a link history, containing the stored links.
  6565. (setq tmphist (append (mapcar 'car org-stored-links)
  6566. org-insert-link-history))
  6567. (unwind-protect
  6568. (setq link
  6569. (let ((org-completion-use-ido nil))
  6570. (org-completing-read
  6571. "Link: "
  6572. (append
  6573. (mapcar (lambda (x) (list (concat (car x) ":")))
  6574. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6575. (mapcar (lambda (x) (list (concat x ":")))
  6576. org-link-types))
  6577. nil nil nil
  6578. 'tmphist
  6579. (or (car (car org-stored-links))))))
  6580. (set-window-configuration wcf)
  6581. (kill-buffer "*Org Links*"))
  6582. (setq entry (assoc link org-stored-links))
  6583. (or entry (push link org-insert-link-history))
  6584. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6585. (not org-keep-stored-link-after-insertion))
  6586. (setq org-stored-links (delq (assoc link org-stored-links)
  6587. org-stored-links)))
  6588. (setq desc (or desc (nth 1 entry)))))
  6589. (if (string-match org-plain-link-re link)
  6590. ;; URL-like link, normalize the use of angular brackets.
  6591. (setq link (org-make-link (org-remove-angle-brackets link))))
  6592. ;; Check if we are linking to the current file with a search option
  6593. ;; If yes, simplify the link by using only the search option.
  6594. (when (and buffer-file-name
  6595. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6596. (let* ((path (match-string 1 link))
  6597. (case-fold-search nil)
  6598. (search (match-string 2 link)))
  6599. (save-match-data
  6600. (if (equal (file-truename buffer-file-name) (file-truename path))
  6601. ;; We are linking to this same file, with a search option
  6602. (setq link search)))))
  6603. ;; Check if we can/should use a relative path. If yes, simplify the link
  6604. (when (string-match "^file:\\(.*\\)" link)
  6605. (let* ((path (match-string 1 link))
  6606. (origpath path)
  6607. (case-fold-search nil))
  6608. (cond
  6609. ((or (eq org-link-file-path-type 'absolute)
  6610. (equal complete-file '(16)))
  6611. (setq path (abbreviate-file-name (expand-file-name path))))
  6612. ((eq org-link-file-path-type 'noabbrev)
  6613. (setq path (expand-file-name path)))
  6614. ((eq org-link-file-path-type 'relative)
  6615. (setq path (file-relative-name path)))
  6616. (t
  6617. (save-match-data
  6618. (if (string-match (concat "^" (regexp-quote
  6619. (file-name-as-directory
  6620. (expand-file-name "."))))
  6621. (expand-file-name path))
  6622. ;; We are linking a file with relative path name.
  6623. (setq path (substring (expand-file-name path)
  6624. (match-end 0)))
  6625. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6626. (setq link (concat "file:" path))
  6627. (if (equal desc origpath)
  6628. (setq desc path))))
  6629. (if org-make-link-description-function
  6630. (setq desc (funcall org-make-link-description-function link desc)))
  6631. (setq desc (read-string "Description: " desc))
  6632. (unless (string-match "\\S-" desc) (setq desc nil))
  6633. (if remove (apply 'delete-region remove))
  6634. (insert (org-make-link-string link desc))))
  6635. (defun org-completing-read (&rest args)
  6636. "Completing-read with SPACE being a normal character."
  6637. (let ((minibuffer-local-completion-map
  6638. (copy-keymap minibuffer-local-completion-map)))
  6639. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6640. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6641. (apply 'org-ido-completing-read args)))
  6642. (defun org-completing-read-no-ido (&rest args)
  6643. (let (org-completion-use-ido)
  6644. (apply 'org-completing-read args)))
  6645. (defun org-ido-completing-read (&rest args)
  6646. "Completing-read using `ido-mode' speedups if available"
  6647. (if (and org-completion-use-ido
  6648. (fboundp 'ido-completing-read)
  6649. (boundp 'ido-mode) ido-mode
  6650. (listp (second args)))
  6651. (let ((ido-enter-matching-directory nil))
  6652. (apply 'ido-completing-read (concat (car args))
  6653. (mapcar (lambda (x) (car x)) (nth 1 args))
  6654. (cddr args)))
  6655. (apply 'completing-read args)))
  6656. (defun org-extract-attributes (s)
  6657. "Extract the attributes cookie from a string and set as text property."
  6658. (let (a attr (start 0) key value)
  6659. (save-match-data
  6660. (when (string-match "{{\\([^}]+\\)}}$" s)
  6661. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6662. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6663. (setq key (match-string 1 a) value (match-string 2 a)
  6664. start (match-end 0)
  6665. attr (plist-put attr (intern key) value))))
  6666. (org-add-props s nil 'org-attr attr))
  6667. s))
  6668. (defun org-extract-attributes-from-string (tag)
  6669. (let (key value attr)
  6670. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6671. (setq key (match-string 1 tag) value (match-string 2 tag)
  6672. tag (replace-match "" t t tag)
  6673. attr (plist-put attr (intern key) value)))
  6674. (cons tag attr)))
  6675. (defun org-attributes-to-string (plist)
  6676. "Format a property list into an HTML attribute list."
  6677. (let ((s "") key value)
  6678. (while plist
  6679. (setq key (pop plist) value (pop plist))
  6680. (and value
  6681. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6682. s))
  6683. ;;; Opening/following a link
  6684. (defvar org-link-search-failed nil)
  6685. (defun org-next-link ()
  6686. "Move forward to the next link.
  6687. If the link is in hidden text, expose it."
  6688. (interactive)
  6689. (when (and org-link-search-failed (eq this-command last-command))
  6690. (goto-char (point-min))
  6691. (message "Link search wrapped back to beginning of buffer"))
  6692. (setq org-link-search-failed nil)
  6693. (let* ((pos (point))
  6694. (ct (org-context))
  6695. (a (assoc :link ct)))
  6696. (if a (goto-char (nth 2 a)))
  6697. (if (re-search-forward org-any-link-re nil t)
  6698. (progn
  6699. (goto-char (match-beginning 0))
  6700. (if (org-invisible-p) (org-show-context)))
  6701. (goto-char pos)
  6702. (setq org-link-search-failed t)
  6703. (error "No further link found"))))
  6704. (defun org-previous-link ()
  6705. "Move backward to the previous link.
  6706. If the link is in hidden text, expose it."
  6707. (interactive)
  6708. (when (and org-link-search-failed (eq this-command last-command))
  6709. (goto-char (point-max))
  6710. (message "Link search wrapped back to end of buffer"))
  6711. (setq org-link-search-failed nil)
  6712. (let* ((pos (point))
  6713. (ct (org-context))
  6714. (a (assoc :link ct)))
  6715. (if a (goto-char (nth 1 a)))
  6716. (if (re-search-backward org-any-link-re nil t)
  6717. (progn
  6718. (goto-char (match-beginning 0))
  6719. (if (org-invisible-p) (org-show-context)))
  6720. (goto-char pos)
  6721. (setq org-link-search-failed t)
  6722. (error "No further link found"))))
  6723. (defun org-translate-link (s)
  6724. "Translate a link string if a translation function has been defined."
  6725. (if (and org-link-translation-function
  6726. (fboundp org-link-translation-function)
  6727. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6728. (progn
  6729. (setq s (funcall org-link-translation-function
  6730. (match-string 1) (match-string 2)))
  6731. (concat (car s) ":" (cdr s)))
  6732. s))
  6733. (defun org-translate-link-from-planner (type path)
  6734. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6735. This is still an experimental function, your mileage may vary."
  6736. (cond
  6737. ((member type '("http" "https" "news" "ftp"))
  6738. ;; standard Internet links are the same.
  6739. nil)
  6740. ((and (equal type "irc") (string-match "^//" path))
  6741. ;; Planner has two / at the beginning of an irc link, we have 1.
  6742. ;; We should have zero, actually....
  6743. (setq path (substring path 1)))
  6744. ((and (equal type "lisp") (string-match "^/" path))
  6745. ;; Planner has a slash, we do not.
  6746. (setq type "elisp" path (substring path 1)))
  6747. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6748. ;; A typical message link. Planner has the id after the fina slash,
  6749. ;; we separate it with a hash mark
  6750. (setq path (concat (match-string 1 path) "#"
  6751. (org-remove-angle-brackets (match-string 2 path)))))
  6752. )
  6753. (cons type path))
  6754. (defun org-find-file-at-mouse (ev)
  6755. "Open file link or URL at mouse."
  6756. (interactive "e")
  6757. (mouse-set-point ev)
  6758. (org-open-at-point 'in-emacs))
  6759. (defun org-open-at-mouse (ev)
  6760. "Open file link or URL at mouse."
  6761. (interactive "e")
  6762. (mouse-set-point ev)
  6763. (if (eq major-mode 'org-agenda-mode)
  6764. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6765. (org-open-at-point))
  6766. (defvar org-window-config-before-follow-link nil
  6767. "The window configuration before following a link.
  6768. This is saved in case the need arises to restore it.")
  6769. (defvar org-open-link-marker (make-marker)
  6770. "Marker pointing to the location where `org-open-at-point; was called.")
  6771. ;;;###autoload
  6772. (defun org-open-at-point-global ()
  6773. "Follow a link like Org-mode does.
  6774. This command can be called in any mode to follow a link that has
  6775. Org-mode syntax."
  6776. (interactive)
  6777. (org-run-like-in-org-mode 'org-open-at-point))
  6778. ;;;###autoload
  6779. (defun org-open-link-from-string (s &optional arg)
  6780. "Open a link in the string S, as if it was in Org-mode."
  6781. (interactive "sLink: \nP")
  6782. (let ((reference-buffer (current-buffer)))
  6783. (with-temp-buffer
  6784. (let ((org-inhibit-startup t))
  6785. (org-mode)
  6786. (insert s)
  6787. (goto-char (point-min))
  6788. (org-open-at-point arg reference-buffer)))))
  6789. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6790. "Open link at or after point.
  6791. If there is no link at point, this function will search forward up to
  6792. the end of the current line.
  6793. Normally, files will be opened by an appropriate application. If the
  6794. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6795. With a double prefix argument, try to open outside of Emacs, in the
  6796. application the system uses for this file type."
  6797. (interactive "P")
  6798. (org-load-modules-maybe)
  6799. (move-marker org-open-link-marker (point))
  6800. (setq org-window-config-before-follow-link (current-window-configuration))
  6801. (org-remove-occur-highlights nil nil t)
  6802. (cond
  6803. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6804. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6805. (org-footnote-action))
  6806. (t
  6807. (let (type path link line search (pos (point)))
  6808. (catch 'match
  6809. (save-excursion
  6810. (skip-chars-forward "^]\n\r")
  6811. (when (org-in-regexp org-bracket-link-regexp)
  6812. (setq link (org-extract-attributes
  6813. (org-link-unescape (org-match-string-no-properties 1))))
  6814. (while (string-match " *\n *" link)
  6815. (setq link (replace-match " " t t link)))
  6816. (setq link (org-link-expand-abbrev link))
  6817. (cond
  6818. ((or (file-name-absolute-p link)
  6819. (string-match "^\\.\\.?/" link))
  6820. (setq type "file" path link))
  6821. ((string-match org-link-re-with-space3 link)
  6822. (setq type (match-string 1 link) path (match-string 2 link)))
  6823. (t (setq type "thisfile" path link)))
  6824. (throw 'match t)))
  6825. (when (get-text-property (point) 'org-linked-text)
  6826. (setq type "thisfile"
  6827. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6828. (1+ (point)) (point))
  6829. path (buffer-substring
  6830. (previous-single-property-change pos 'org-linked-text)
  6831. (next-single-property-change pos 'org-linked-text)))
  6832. (throw 'match t))
  6833. (save-excursion
  6834. (when (or (org-in-regexp org-angle-link-re)
  6835. (org-in-regexp org-plain-link-re))
  6836. (setq type (match-string 1) path (match-string 2))
  6837. (throw 'match t)))
  6838. (save-excursion
  6839. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6840. (setq type "tags"
  6841. path (match-string 1))
  6842. (while (string-match ":" path)
  6843. (setq path (replace-match "+" t t path)))
  6844. (throw 'match t)))
  6845. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6846. (setq type "tree-match"
  6847. path (match-string 1))
  6848. (throw 'match t)))
  6849. (unless path
  6850. (error "No link found"))
  6851. ;; switch back to reference buffer
  6852. ;; needed when if called in a temporary buffer through
  6853. ;; org-open-link-from-string
  6854. (and reference-buffer (switch-to-buffer reference-buffer))
  6855. ;; Remove any trailing spaces in path
  6856. (if (string-match " +\\'" path)
  6857. (setq path (replace-match "" t t path)))
  6858. (if (and org-link-translation-function
  6859. (fboundp org-link-translation-function))
  6860. ;; Check if we need to translate the link
  6861. (let ((tmp (funcall org-link-translation-function type path)))
  6862. (setq type (car tmp) path (cdr tmp))))
  6863. (cond
  6864. ((assoc type org-link-protocols)
  6865. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6866. ((equal type "mailto")
  6867. (let ((cmd (car org-link-mailto-program))
  6868. (args (cdr org-link-mailto-program)) args1
  6869. (address path) (subject "") a)
  6870. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6871. (setq address (match-string 1 path)
  6872. subject (org-link-escape (match-string 2 path))))
  6873. (while args
  6874. (cond
  6875. ((not (stringp (car args))) (push (pop args) args1))
  6876. (t (setq a (pop args))
  6877. (if (string-match "%a" a)
  6878. (setq a (replace-match address t t a)))
  6879. (if (string-match "%s" a)
  6880. (setq a (replace-match subject t t a)))
  6881. (push a args1))))
  6882. (apply cmd (nreverse args1))))
  6883. ((member type '("http" "https" "ftp" "news"))
  6884. (browse-url (concat type ":" (org-link-escape
  6885. path org-link-escape-chars-browser))))
  6886. ((member type '("message"))
  6887. (browse-url (concat type ":" path)))
  6888. ((string= type "tags")
  6889. (org-tags-view in-emacs path))
  6890. ((string= type "thisfile")
  6891. (if in-emacs
  6892. (switch-to-buffer-other-window
  6893. (org-get-buffer-for-internal-link (current-buffer)))
  6894. (org-mark-ring-push))
  6895. (let ((cmd `(org-link-search
  6896. ,path
  6897. ,(cond ((equal in-emacs '(4)) 'occur)
  6898. ((equal in-emacs '(16)) 'org-occur)
  6899. (t nil))
  6900. ,pos)))
  6901. (condition-case nil (eval cmd)
  6902. (error (progn (widen) (eval cmd))))))
  6903. ((string= type "tree-match")
  6904. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6905. ((string= type "file")
  6906. (if (string-match "::\\([0-9]+\\)\\'" path)
  6907. (setq line (string-to-number (match-string 1 path))
  6908. path (substring path 0 (match-beginning 0)))
  6909. (if (string-match "::\\(.+\\)\\'" path)
  6910. (setq search (match-string 1 path)
  6911. path (substring path 0 (match-beginning 0)))))
  6912. (if (string-match "[*?{]" (file-name-nondirectory path))
  6913. (dired path)
  6914. (org-open-file path in-emacs line search)))
  6915. ((string= type "news")
  6916. (require 'org-gnus)
  6917. (org-gnus-follow-link path))
  6918. ((string= type "shell")
  6919. (let ((cmd path))
  6920. (if (or (not org-confirm-shell-link-function)
  6921. (funcall org-confirm-shell-link-function
  6922. (format "Execute \"%s\" in shell? "
  6923. (org-add-props cmd nil
  6924. 'face 'org-warning))))
  6925. (progn
  6926. (message "Executing %s" cmd)
  6927. (shell-command cmd))
  6928. (error "Abort"))))
  6929. ((string= type "elisp")
  6930. (let ((cmd path))
  6931. (if (or (not org-confirm-elisp-link-function)
  6932. (funcall org-confirm-elisp-link-function
  6933. (format "Execute \"%s\" as elisp? "
  6934. (org-add-props cmd nil
  6935. 'face 'org-warning))))
  6936. (message "%s => %s" cmd
  6937. (if (equal (string-to-char cmd) ?\()
  6938. (eval (read cmd))
  6939. (call-interactively (read cmd))))
  6940. (error "Abort"))))
  6941. (t
  6942. (browse-url-at-point))))))
  6943. (move-marker org-open-link-marker nil)
  6944. (run-hook-with-args 'org-follow-link-hook))
  6945. ;;;; Time estimates
  6946. (defun org-get-effort (&optional pom)
  6947. "Get the effort estimate for the current entry."
  6948. (org-entry-get pom org-effort-property))
  6949. ;;; File search
  6950. (defvar org-create-file-search-functions nil
  6951. "List of functions to construct the right search string for a file link.
  6952. These functions are called in turn with point at the location to
  6953. which the link should point.
  6954. A function in the hook should first test if it would like to
  6955. handle this file type, for example by checking the major-mode or
  6956. the file extension. If it decides not to handle this file, it
  6957. should just return nil to give other functions a chance. If it
  6958. does handle the file, it must return the search string to be used
  6959. when following the link. The search string will be part of the
  6960. file link, given after a double colon, and `org-open-at-point'
  6961. will automatically search for it. If special measures must be
  6962. taken to make the search successful, another function should be
  6963. added to the companion hook `org-execute-file-search-functions',
  6964. which see.
  6965. A function in this hook may also use `setq' to set the variable
  6966. `description' to provide a suggestion for the descriptive text to
  6967. be used for this link when it gets inserted into an Org-mode
  6968. buffer with \\[org-insert-link].")
  6969. (defvar org-execute-file-search-functions nil
  6970. "List of functions to execute a file search triggered by a link.
  6971. Functions added to this hook must accept a single argument, the
  6972. search string that was part of the file link, the part after the
  6973. double colon. The function must first check if it would like to
  6974. handle this search, for example by checking the major-mode or the
  6975. file extension. If it decides not to handle this search, it
  6976. should just return nil to give other functions a chance. If it
  6977. does handle the search, it must return a non-nil value to keep
  6978. other functions from trying.
  6979. Each function can access the current prefix argument through the
  6980. variable `current-prefix-argument'. Note that a single prefix is
  6981. used to force opening a link in Emacs, so it may be good to only
  6982. use a numeric or double prefix to guide the search function.
  6983. In case this is needed, a function in this hook can also restore
  6984. the window configuration before `org-open-at-point' was called using:
  6985. (set-window-configuration org-window-config-before-follow-link)")
  6986. (defun org-link-search (s &optional type avoid-pos)
  6987. "Search for a link search option.
  6988. If S is surrounded by forward slashes, it is interpreted as a
  6989. regular expression. In org-mode files, this will create an `org-occur'
  6990. sparse tree. In ordinary files, `occur' will be used to list matches.
  6991. If the current buffer is in `dired-mode', grep will be used to search
  6992. in all files. If AVOID-POS is given, ignore matches near that position."
  6993. (let ((case-fold-search t)
  6994. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6995. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6996. (append '(("") (" ") ("\t") ("\n"))
  6997. org-emphasis-alist)
  6998. "\\|") "\\)"))
  6999. (pos (point))
  7000. (pre nil) (post nil)
  7001. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7002. (cond
  7003. ;; First check if there are any special
  7004. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7005. ;; Now try the builtin stuff
  7006. ((and (equal (string-to-char s0) ?#)
  7007. (> (length s0) 1)
  7008. (save-excursion
  7009. (goto-char (point-min))
  7010. (and
  7011. (re-search-forward
  7012. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7013. (setq type 'dedicated
  7014. pos (match-beginning 0))))
  7015. ;; There is an exact target for this
  7016. (goto-char pos)
  7017. (org-back-to-heading t)))
  7018. ((save-excursion
  7019. (goto-char (point-min))
  7020. (and
  7021. (re-search-forward
  7022. (concat "<<" (regexp-quote s0) ">>") nil t)
  7023. (setq type 'dedicated
  7024. pos (match-beginning 0))))
  7025. ;; There is an exact target for this
  7026. (goto-char pos))
  7027. ((and (string-match "^(\\(.*\\))$" s0)
  7028. (save-excursion
  7029. (goto-char (point-min))
  7030. (and
  7031. (re-search-forward
  7032. (concat "[^[]" (regexp-quote
  7033. (format org-coderef-label-format
  7034. (match-string 1 s0))))
  7035. nil t)
  7036. (setq type 'dedicated
  7037. pos (1+ (match-beginning 0))))))
  7038. ;; There is a coderef target for this
  7039. (goto-char pos))
  7040. ((string-match "^/\\(.*\\)/$" s)
  7041. ;; A regular expression
  7042. (cond
  7043. ((org-mode-p)
  7044. (org-occur (match-string 1 s)))
  7045. ;;((eq major-mode 'dired-mode)
  7046. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7047. (t (org-do-occur (match-string 1 s)))))
  7048. (t
  7049. ;; A normal search strings
  7050. (when (equal (string-to-char s) ?*)
  7051. ;; Anchor on headlines, post may include tags.
  7052. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7053. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7054. s (substring s 1)))
  7055. (remove-text-properties
  7056. 0 (length s)
  7057. '(face nil mouse-face nil keymap nil fontified nil) s)
  7058. ;; Make a series of regular expressions to find a match
  7059. (setq words (org-split-string s "[ \n\r\t]+")
  7060. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7061. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7062. "\\)" markers)
  7063. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7064. re2a (concat "[ \t\r\n]" re2a_)
  7065. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7066. re4 (concat "[^a-zA-Z_]" re4_)
  7067. re1 (concat pre re2 post)
  7068. re3 (concat pre (if pre re4_ re4) post)
  7069. re5 (concat pre ".*" re4)
  7070. re2 (concat pre re2)
  7071. re2a (concat pre (if pre re2a_ re2a))
  7072. re4 (concat pre (if pre re4_ re4))
  7073. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7074. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7075. re5 "\\)"
  7076. ))
  7077. (cond
  7078. ((eq type 'org-occur) (org-occur reall))
  7079. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7080. (t (goto-char (point-min))
  7081. (setq type 'fuzzy)
  7082. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7083. (org-search-not-self 1 re1 nil t)
  7084. (org-search-not-self 1 re2 nil t)
  7085. (org-search-not-self 1 re2a nil t)
  7086. (org-search-not-self 1 re3 nil t)
  7087. (org-search-not-self 1 re4 nil t)
  7088. (org-search-not-self 1 re5 nil t)
  7089. )
  7090. (goto-char (match-beginning 1))
  7091. (goto-char pos)
  7092. (error "No match")))))
  7093. (t
  7094. ;; Normal string-search
  7095. (goto-char (point-min))
  7096. (if (search-forward s nil t)
  7097. (goto-char (match-beginning 0))
  7098. (error "No match"))))
  7099. (and (org-mode-p) (org-show-context 'link-search))
  7100. type))
  7101. (defun org-search-not-self (group &rest args)
  7102. "Execute `re-search-forward', but only accept matches that do not
  7103. enclose the position of `org-open-link-marker'."
  7104. (let ((m org-open-link-marker))
  7105. (catch 'exit
  7106. (while (apply 're-search-forward args)
  7107. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7108. (goto-char (match-end group))
  7109. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7110. (> (match-beginning 0) (marker-position m))
  7111. (< (match-end 0) (marker-position m)))
  7112. (save-match-data
  7113. (or (not (org-in-regexp
  7114. org-bracket-link-analytic-regexp 1))
  7115. (not (match-end 4)) ; no description
  7116. (and (<= (match-beginning 4) (point))
  7117. (>= (match-end 4) (point))))))
  7118. (throw 'exit (point))))))))
  7119. (defun org-get-buffer-for-internal-link (buffer)
  7120. "Return a buffer to be used for displaying the link target of internal links."
  7121. (cond
  7122. ((not org-display-internal-link-with-indirect-buffer)
  7123. buffer)
  7124. ((string-match "(Clone)$" (buffer-name buffer))
  7125. (message "Buffer is already a clone, not making another one")
  7126. ;; we also do not modify visibility in this case
  7127. buffer)
  7128. (t ; make a new indirect buffer for displaying the link
  7129. (let* ((bn (buffer-name buffer))
  7130. (ibn (concat bn "(Clone)"))
  7131. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7132. (with-current-buffer ib (org-overview))
  7133. ib))))
  7134. (defun org-do-occur (regexp &optional cleanup)
  7135. "Call the Emacs command `occur'.
  7136. If CLEANUP is non-nil, remove the printout of the regular expression
  7137. in the *Occur* buffer. This is useful if the regex is long and not useful
  7138. to read."
  7139. (occur regexp)
  7140. (when cleanup
  7141. (let ((cwin (selected-window)) win beg end)
  7142. (when (setq win (get-buffer-window "*Occur*"))
  7143. (select-window win))
  7144. (goto-char (point-min))
  7145. (when (re-search-forward "match[a-z]+" nil t)
  7146. (setq beg (match-end 0))
  7147. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7148. (setq end (1- (match-beginning 0)))))
  7149. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7150. (goto-char (point-min))
  7151. (select-window cwin))))
  7152. ;;; The mark ring for links jumps
  7153. (defvar org-mark-ring nil
  7154. "Mark ring for positions before jumps in Org-mode.")
  7155. (defvar org-mark-ring-last-goto nil
  7156. "Last position in the mark ring used to go back.")
  7157. ;; Fill and close the ring
  7158. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7159. (loop for i from 1 to org-mark-ring-length do
  7160. (push (make-marker) org-mark-ring))
  7161. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7162. org-mark-ring)
  7163. (defun org-mark-ring-push (&optional pos buffer)
  7164. "Put the current position or POS into the mark ring and rotate it."
  7165. (interactive)
  7166. (setq pos (or pos (point)))
  7167. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7168. (move-marker (car org-mark-ring)
  7169. (or pos (point))
  7170. (or buffer (current-buffer)))
  7171. (message "%s"
  7172. (substitute-command-keys
  7173. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7174. (defun org-mark-ring-goto (&optional n)
  7175. "Jump to the previous position in the mark ring.
  7176. With prefix arg N, jump back that many stored positions. When
  7177. called several times in succession, walk through the entire ring.
  7178. Org-mode commands jumping to a different position in the current file,
  7179. or to another Org-mode file, automatically push the old position
  7180. onto the ring."
  7181. (interactive "p")
  7182. (let (p m)
  7183. (if (eq last-command this-command)
  7184. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7185. (setq p org-mark-ring))
  7186. (setq org-mark-ring-last-goto p)
  7187. (setq m (car p))
  7188. (switch-to-buffer (marker-buffer m))
  7189. (goto-char m)
  7190. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7191. (defun org-remove-angle-brackets (s)
  7192. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7193. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7194. s)
  7195. (defun org-add-angle-brackets (s)
  7196. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7197. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7198. s)
  7199. (defun org-remove-double-quotes (s)
  7200. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7201. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7202. s)
  7203. ;;; Following specific links
  7204. (defun org-follow-timestamp-link ()
  7205. (cond
  7206. ((org-at-date-range-p t)
  7207. (let ((org-agenda-start-on-weekday)
  7208. (t1 (match-string 1))
  7209. (t2 (match-string 2)))
  7210. (setq t1 (time-to-days (org-time-string-to-time t1))
  7211. t2 (time-to-days (org-time-string-to-time t2)))
  7212. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7213. ((org-at-timestamp-p t)
  7214. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7215. (substring (match-string 1) 0 10)))
  7216. 1))
  7217. (t (error "This should not happen"))))
  7218. ;;; Following file links
  7219. (defvar org-wait nil)
  7220. (defun org-open-file (path &optional in-emacs line search)
  7221. "Open the file at PATH.
  7222. First, this expands any special file name abbreviations. Then the
  7223. configuration variable `org-file-apps' is checked if it contains an
  7224. entry for this file type, and if yes, the corresponding command is launched.
  7225. If no application is found, Emacs simply visits the file.
  7226. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7227. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7228. and o use an external application to visit the file.
  7229. Optional LINE specifies a line to go to, optional SEARCH a string to
  7230. search for. If LINE or SEARCH is given, the file will always be
  7231. opened in Emacs.
  7232. If the file does not exist, an error is thrown."
  7233. (setq in-emacs (or in-emacs line search))
  7234. (let* ((file (if (equal path "")
  7235. buffer-file-name
  7236. (substitute-in-file-name (expand-file-name path))))
  7237. (apps (append org-file-apps (org-default-apps)))
  7238. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7239. (dirp (if remp nil (file-directory-p file)))
  7240. (file (if (and dirp org-open-directory-means-index-dot-org)
  7241. (concat (file-name-as-directory file) "index.org")
  7242. file))
  7243. (a-m-a-p (assq 'auto-mode apps))
  7244. (dfile (downcase file))
  7245. (old-buffer (current-buffer))
  7246. (old-pos (point))
  7247. (old-mode major-mode)
  7248. ext cmd)
  7249. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7250. (setq ext (match-string 1 dfile))
  7251. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7252. (setq ext (match-string 1 dfile))))
  7253. (cond
  7254. ((equal in-emacs '(16))
  7255. (setq cmd (cdr (assoc 'system apps))))
  7256. (in-emacs (setq cmd 'emacs))
  7257. (t
  7258. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7259. (and dirp (cdr (assoc 'directory apps)))
  7260. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7261. 'string-match)
  7262. (cdr (assoc ext apps))
  7263. (cdr (assoc t apps))))))
  7264. (when (eq cmd 'system)
  7265. (setq cmd (cdr (assoc 'system apps))))
  7266. (when (eq cmd 'default)
  7267. (setq cmd (cdr (assoc t apps))))
  7268. (when (eq cmd 'mailcap)
  7269. (require 'mailcap)
  7270. (mailcap-parse-mailcaps)
  7271. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7272. (command (mailcap-mime-info mime-type)))
  7273. (if (stringp command)
  7274. (setq cmd command)
  7275. (setq cmd 'emacs))))
  7276. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7277. (not (file-exists-p file))
  7278. (not org-open-non-existing-files))
  7279. (error "No such file: %s" file))
  7280. (cond
  7281. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7282. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7283. (while (string-match "['\"]%s['\"]" cmd)
  7284. (setq cmd (replace-match "%s" t t cmd)))
  7285. (while (string-match "%s" cmd)
  7286. (setq cmd (replace-match
  7287. (save-match-data
  7288. (shell-quote-argument
  7289. (convert-standard-filename file)))
  7290. t t cmd)))
  7291. (save-window-excursion
  7292. (start-process-shell-command cmd nil cmd)
  7293. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7294. ))
  7295. ((or (stringp cmd)
  7296. (eq cmd 'emacs))
  7297. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7298. (widen)
  7299. (if line (goto-line line)
  7300. (if search (org-link-search search))))
  7301. ((consp cmd)
  7302. (let ((file (convert-standard-filename file)))
  7303. (eval cmd)))
  7304. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7305. (and (org-mode-p) (eq old-mode 'org-mode)
  7306. (or (not (equal old-buffer (current-buffer)))
  7307. (not (equal old-pos (point))))
  7308. (org-mark-ring-push old-pos old-buffer))))
  7309. (defun org-default-apps ()
  7310. "Return the default applications for this operating system."
  7311. (cond
  7312. ((eq system-type 'darwin)
  7313. org-file-apps-defaults-macosx)
  7314. ((eq system-type 'windows-nt)
  7315. org-file-apps-defaults-windowsnt)
  7316. (t org-file-apps-defaults-gnu)))
  7317. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7318. "Convert extensions to regular expressions in the cars of LIST.
  7319. Also, weed out any non-string entries, because the return value is used
  7320. only for regexp matching.
  7321. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7322. point to the symbol `emacs', indicating that the file should
  7323. be opened in Emacs."
  7324. (append
  7325. (delq nil
  7326. (mapcar (lambda (x)
  7327. (if (not (stringp (car x)))
  7328. nil
  7329. (if (string-match "\\W" (car x))
  7330. x
  7331. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7332. list))
  7333. (if add-auto-mode
  7334. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7335. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7336. (defun org-file-remote-p (file)
  7337. "Test whether FILE specifies a location on a remote system.
  7338. Return non-nil if the location is indeed remote.
  7339. For example, the filename \"/user@host:/foo\" specifies a location
  7340. on the system \"/user@host:\"."
  7341. (cond ((fboundp 'file-remote-p)
  7342. (file-remote-p file))
  7343. ((fboundp 'tramp-handle-file-remote-p)
  7344. (tramp-handle-file-remote-p file))
  7345. ((and (boundp 'ange-ftp-name-format)
  7346. (string-match (car ange-ftp-name-format) file))
  7347. t)
  7348. (t nil)))
  7349. ;;;; Refiling
  7350. (defun org-get-org-file ()
  7351. "Read a filename, with default directory `org-directory'."
  7352. (let ((default (or org-default-notes-file remember-data-file)))
  7353. (read-file-name (format "File name [%s]: " default)
  7354. (file-name-as-directory org-directory)
  7355. default)))
  7356. (defun org-notes-order-reversed-p ()
  7357. "Check if the current file should receive notes in reversed order."
  7358. (cond
  7359. ((not org-reverse-note-order) nil)
  7360. ((eq t org-reverse-note-order) t)
  7361. ((not (listp org-reverse-note-order)) nil)
  7362. (t (catch 'exit
  7363. (let ((all org-reverse-note-order)
  7364. entry)
  7365. (while (setq entry (pop all))
  7366. (if (string-match (car entry) buffer-file-name)
  7367. (throw 'exit (cdr entry))))
  7368. nil)))))
  7369. (defvar org-refile-target-table nil
  7370. "The list of refile targets, created by `org-refile'.")
  7371. (defvar org-agenda-new-buffers nil
  7372. "Buffers created to visit agenda files.")
  7373. (defun org-get-refile-targets (&optional default-buffer)
  7374. "Produce a table with refile targets."
  7375. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7376. targets txt re files f desc descre fast-path-p level pos0)
  7377. (message "Getting targets...")
  7378. (with-current-buffer (or default-buffer (current-buffer))
  7379. (while (setq entry (pop entries))
  7380. (setq files (car entry) desc (cdr entry))
  7381. (setq fast-path-p nil)
  7382. (cond
  7383. ((null files) (setq files (list (current-buffer))))
  7384. ((eq files 'org-agenda-files)
  7385. (setq files (org-agenda-files 'unrestricted)))
  7386. ((and (symbolp files) (fboundp files))
  7387. (setq files (funcall files)))
  7388. ((and (symbolp files) (boundp files))
  7389. (setq files (symbol-value files))))
  7390. (if (stringp files) (setq files (list files)))
  7391. (cond
  7392. ((eq (car desc) :tag)
  7393. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7394. ((eq (car desc) :todo)
  7395. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7396. ((eq (car desc) :regexp)
  7397. (setq descre (cdr desc)))
  7398. ((eq (car desc) :level)
  7399. (setq descre (concat "^\\*\\{" (number-to-string
  7400. (if org-odd-levels-only
  7401. (1- (* 2 (cdr desc)))
  7402. (cdr desc)))
  7403. "\\}[ \t]")))
  7404. ((eq (car desc) :maxlevel)
  7405. (setq fast-path-p t)
  7406. (setq descre (concat "^\\*\\{1," (number-to-string
  7407. (if org-odd-levels-only
  7408. (1- (* 2 (cdr desc)))
  7409. (cdr desc)))
  7410. "\\}[ \t]")))
  7411. (t (error "Bad refiling target description %s" desc)))
  7412. (while (setq f (pop files))
  7413. (save-excursion
  7414. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7415. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7416. (setq f (expand-file-name f))
  7417. (if (eq org-refile-use-outline-path 'file)
  7418. (push (list (file-name-nondirectory f) f nil nil) targets))
  7419. (save-excursion
  7420. (save-restriction
  7421. (widen)
  7422. (goto-char (point-min))
  7423. (while (re-search-forward descre nil t)
  7424. (goto-char (setq pos0 (point-at-bol)))
  7425. (catch 'next
  7426. (when org-refile-target-verify-function
  7427. (save-match-data
  7428. (or (funcall org-refile-target-verify-function)
  7429. (throw 'next t))))
  7430. (when (looking-at org-complex-heading-regexp)
  7431. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7432. txt (org-link-display-format (match-string 4))
  7433. re (concat "^" (regexp-quote
  7434. (buffer-substring (match-beginning 1)
  7435. (match-end 4)))))
  7436. (if (match-end 5) (setq re (concat re "[ \t]+"
  7437. (regexp-quote
  7438. (match-string 5)))))
  7439. (setq re (concat re "[ \t]*$"))
  7440. (when org-refile-use-outline-path
  7441. (setq txt (mapconcat 'org-protect-slash
  7442. (append
  7443. (if (eq org-refile-use-outline-path 'file)
  7444. (list (file-name-nondirectory
  7445. (buffer-file-name (buffer-base-buffer))))
  7446. (if (eq org-refile-use-outline-path 'full-file-path)
  7447. (list (buffer-file-name (buffer-base-buffer)))))
  7448. (org-get-outline-path fast-path-p level txt)
  7449. (list txt))
  7450. "/")))
  7451. (push (list txt f re (point)) targets)))
  7452. (when (= (point) pos0)
  7453. ;; verification function has not moved point
  7454. (goto-char (point-at-eol))))))))))
  7455. (message "Getting targets...done")
  7456. (nreverse targets)))
  7457. (defun org-protect-slash (s)
  7458. (while (string-match "/" s)
  7459. (setq s (replace-match "\\" t t s)))
  7460. s)
  7461. (defvar org-olpa (make-vector 20 nil))
  7462. (defun org-get-outline-path (&optional fastp level heading)
  7463. "Return the outline path to the current entry, as a list."
  7464. (if fastp
  7465. (progn
  7466. (if (> level 19)
  7467. (error "Outline path failure, more than 19 levels."))
  7468. (loop for i from level upto 19 do
  7469. (aset org-olpa i nil))
  7470. (prog1
  7471. (delq nil (append org-olpa nil))
  7472. (aset org-olpa level heading)))
  7473. (let (rtn)
  7474. (save-excursion
  7475. (while (org-up-heading-safe)
  7476. (when (looking-at org-complex-heading-regexp)
  7477. (push (org-match-string-no-properties 4) rtn)))
  7478. rtn))))
  7479. (defvar org-refile-history nil
  7480. "History for refiling operations.")
  7481. (defun org-refile (&optional goto default-buffer)
  7482. "Move the entry at point to another heading.
  7483. The list of target headings is compiled using the information in
  7484. `org-refile-targets', which see. This list is created before each use
  7485. and will therefore always be up-to-date.
  7486. At the target location, the entry is filed as a subitem of the target heading.
  7487. Depending on `org-reverse-note-order', the new subitem will either be the
  7488. first or the last subitem.
  7489. If there is an active region, all entries in that region will be moved.
  7490. However, the region must fulfil the requirement that the first heading
  7491. is the first one sets the top-level of the moved text - at most siblings
  7492. below it are allowed.
  7493. With prefix arg GOTO, the command will only visit the target location,
  7494. not actually move anything.
  7495. With a double prefix `C-u C-u', go to the location where the last refiling
  7496. operation has put the subtree.
  7497. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7498. (interactive "P")
  7499. (let* ((cbuf (current-buffer))
  7500. (regionp (org-region-active-p))
  7501. (region-start (and regionp (region-beginning)))
  7502. (region-end (and regionp (region-end)))
  7503. (region-length (and regionp (- region-end region-start)))
  7504. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7505. pos it nbuf file re level reversed)
  7506. (when regionp (goto-char region-start)
  7507. (unless (org-kill-is-subtree-p
  7508. (buffer-substring region-start region-end))
  7509. (error "The region is not a (sequence of) subtree(s)")))
  7510. (if (equal goto '(16))
  7511. (org-refile-goto-last-stored)
  7512. (when (setq it (org-refile-get-location
  7513. (if goto "Goto: " "Refile to: ") default-buffer
  7514. org-refile-allow-creating-parent-nodes))
  7515. (setq file (nth 1 it)
  7516. re (nth 2 it)
  7517. pos (nth 3 it))
  7518. (if (and (not goto)
  7519. pos
  7520. (equal (buffer-file-name) file)
  7521. (if regionp
  7522. (and (>= pos region-start)
  7523. (<= pos region-end))
  7524. (and (>= pos (point))
  7525. (< pos (save-excursion
  7526. (org-end-of-subtree t t))))))
  7527. (error "Cannot refile to position inside the tree or region"))
  7528. (setq nbuf (or (find-buffer-visiting file)
  7529. (find-file-noselect file)))
  7530. (if goto
  7531. (progn
  7532. (switch-to-buffer nbuf)
  7533. (goto-char pos)
  7534. (org-show-context 'org-goto))
  7535. (if regionp
  7536. (progn
  7537. (kill-new (buffer-substring region-start region-end))
  7538. (org-save-markers-in-region region-start region-end))
  7539. (org-copy-subtree 1 nil t))
  7540. (save-excursion
  7541. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7542. (find-file-noselect file))))
  7543. (setq reversed (org-notes-order-reversed-p))
  7544. (save-excursion
  7545. (save-restriction
  7546. (widen)
  7547. (if pos
  7548. (progn
  7549. (goto-char pos)
  7550. (looking-at outline-regexp)
  7551. (setq level (org-get-valid-level (funcall outline-level) 1))
  7552. (goto-char
  7553. (if reversed
  7554. (or (outline-next-heading) (point-max))
  7555. (or (save-excursion (outline-get-next-sibling))
  7556. (org-end-of-subtree t t)
  7557. (point-max)))))
  7558. (setq level 1)
  7559. (if (not reversed)
  7560. (goto-char (point-max))
  7561. (goto-char (point-min))
  7562. (or (outline-next-heading) (goto-char (point-max)))))
  7563. (if (not (bolp)) (newline))
  7564. (bookmark-set "org-refile-last-stored")
  7565. (org-paste-subtree level))))
  7566. (if regionp
  7567. (delete-region (point) (+ (point) region-length))
  7568. (org-cut-subtree))
  7569. (setq org-markers-to-move nil)
  7570. (message "Refiled to \"%s\"" (car it))))))
  7571. (org-reveal))
  7572. (defun org-refile-goto-last-stored ()
  7573. "Go to the location where the last refile was stored."
  7574. (interactive)
  7575. (bookmark-jump "org-refile-last-stored")
  7576. (message "This is the location of the last refile"))
  7577. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7578. "Prompt the user for a refile location, using PROMPT."
  7579. (let ((org-refile-targets org-refile-targets)
  7580. (org-refile-use-outline-path org-refile-use-outline-path))
  7581. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7582. (unless org-refile-target-table
  7583. (error "No refile targets"))
  7584. (let* ((cbuf (current-buffer))
  7585. (partial-completion-mode nil)
  7586. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7587. (cfunc (if (and org-refile-use-outline-path
  7588. org-outline-path-complete-in-steps)
  7589. 'org-olpath-completing-read
  7590. 'org-ido-completing-read))
  7591. (extra (if org-refile-use-outline-path "/" ""))
  7592. (filename (and cfn (expand-file-name cfn)))
  7593. (tbl (mapcar
  7594. (lambda (x)
  7595. (if (and (not (member org-refile-use-outline-path
  7596. '(file full-file-path)))
  7597. (not (equal filename (nth 1 x))))
  7598. (cons (concat (car x) extra " ("
  7599. (file-name-nondirectory (nth 1 x)) ")")
  7600. (cdr x))
  7601. (cons (concat (car x) extra) (cdr x))))
  7602. org-refile-target-table))
  7603. (completion-ignore-case t)
  7604. pa answ parent-target child parent)
  7605. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7606. nil 'org-refile-history))
  7607. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7608. (if pa
  7609. pa
  7610. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7611. (setq parent (match-string 1 answ)
  7612. child (match-string 2 answ))
  7613. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7614. (when (and parent-target
  7615. (or (eq new-nodes t)
  7616. (and (eq new-nodes 'confirm)
  7617. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7618. (org-refile-new-child parent-target child))))))
  7619. (defun org-refile-new-child (parent-target child)
  7620. "Use refile target PARENT-TARGET to add new CHILD below it."
  7621. (unless parent-target
  7622. (error "Cannot find parent for new node"))
  7623. (let ((file (nth 1 parent-target))
  7624. (pos (nth 3 parent-target))
  7625. level)
  7626. (with-current-buffer (or (find-buffer-visiting file)
  7627. (find-file-noselect file))
  7628. (save-excursion
  7629. (save-restriction
  7630. (widen)
  7631. (if pos
  7632. (goto-char pos)
  7633. (goto-char (point-max))
  7634. (if (not (bolp)) (newline)))
  7635. (when (looking-at outline-regexp)
  7636. (setq level (funcall outline-level))
  7637. (org-end-of-subtree t t))
  7638. (org-back-over-empty-lines)
  7639. (insert "\n" (make-string
  7640. (if pos (org-get-valid-level level 1) 1) ?*)
  7641. " " child "\n")
  7642. (beginning-of-line 0)
  7643. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7644. (defun org-olpath-completing-read (prompt collection &rest args)
  7645. "Read an outline path like a file name."
  7646. (let ((thetable collection)
  7647. (org-completion-use-ido nil)) ; does not work with ido.
  7648. (apply
  7649. 'org-ido-completing-read prompt
  7650. (lambda (string predicate &optional flag)
  7651. (let (rtn r f (l (length string)))
  7652. (cond
  7653. ((eq flag nil)
  7654. ;; try completion
  7655. (try-completion string thetable))
  7656. ((eq flag t)
  7657. ;; all-completions
  7658. (setq rtn (all-completions string thetable predicate))
  7659. (mapcar
  7660. (lambda (x)
  7661. (setq r (substring x l))
  7662. (if (string-match " ([^)]*)$" x)
  7663. (setq f (match-string 0 x))
  7664. (setq f ""))
  7665. (if (string-match "/" r)
  7666. (concat string (substring r 0 (match-end 0)) f)
  7667. x))
  7668. rtn))
  7669. ((eq flag 'lambda)
  7670. ;; exact match?
  7671. (assoc string thetable)))
  7672. ))
  7673. args)))
  7674. ;;;; Dynamic blocks
  7675. (defun org-find-dblock (name)
  7676. "Find the first dynamic block with name NAME in the buffer.
  7677. If not found, stay at current position and return nil."
  7678. (let (pos)
  7679. (save-excursion
  7680. (goto-char (point-min))
  7681. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7682. nil t)
  7683. (match-beginning 0))))
  7684. (if pos (goto-char pos))
  7685. pos))
  7686. (defconst org-dblock-start-re
  7687. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7688. "Matches the startline of a dynamic block, with parameters.")
  7689. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7690. "Matches the end of a dynamic block.")
  7691. (defun org-create-dblock (plist)
  7692. "Create a dynamic block section, with parameters taken from PLIST.
  7693. PLIST must contain a :name entry which is used as name of the block."
  7694. (unless (bolp) (newline))
  7695. (let ((name (plist-get plist :name)))
  7696. (insert "#+BEGIN: " name)
  7697. (while plist
  7698. (if (eq (car plist) :name)
  7699. (setq plist (cddr plist))
  7700. (insert " " (prin1-to-string (pop plist)))))
  7701. (insert "\n\n#+END:\n")
  7702. (beginning-of-line -2)))
  7703. (defun org-prepare-dblock ()
  7704. "Prepare dynamic block for refresh.
  7705. This empties the block, puts the cursor at the insert position and returns
  7706. the property list including an extra property :name with the block name."
  7707. (unless (looking-at org-dblock-start-re)
  7708. (error "Not at a dynamic block"))
  7709. (let* ((begdel (1+ (match-end 0)))
  7710. (name (org-no-properties (match-string 1)))
  7711. (params (append (list :name name)
  7712. (read (concat "(" (match-string 3) ")")))))
  7713. (unless (re-search-forward org-dblock-end-re nil t)
  7714. (error "Dynamic block not terminated"))
  7715. (setq params
  7716. (append params
  7717. (list :content (buffer-substring
  7718. begdel (match-beginning 0)))))
  7719. (delete-region begdel (match-beginning 0))
  7720. (goto-char begdel)
  7721. (open-line 1)
  7722. params))
  7723. (defun org-map-dblocks (&optional command)
  7724. "Apply COMMAND to all dynamic blocks in the current buffer.
  7725. If COMMAND is not given, use `org-update-dblock'."
  7726. (let ((cmd (or command 'org-update-dblock))
  7727. pos)
  7728. (save-excursion
  7729. (goto-char (point-min))
  7730. (while (re-search-forward org-dblock-start-re nil t)
  7731. (goto-char (setq pos (match-beginning 0)))
  7732. (condition-case nil
  7733. (funcall cmd)
  7734. (error (message "Error during update of dynamic block")))
  7735. (goto-char pos)
  7736. (unless (re-search-forward org-dblock-end-re nil t)
  7737. (error "Dynamic block not terminated"))))))
  7738. (defun org-dblock-update (&optional arg)
  7739. "User command for updating dynamic blocks.
  7740. Update the dynamic block at point. With prefix ARG, update all dynamic
  7741. blocks in the buffer."
  7742. (interactive "P")
  7743. (if arg
  7744. (org-update-all-dblocks)
  7745. (or (looking-at org-dblock-start-re)
  7746. (org-beginning-of-dblock))
  7747. (org-update-dblock)))
  7748. (defun org-update-dblock ()
  7749. "Update the dynamic block at point
  7750. This means to empty the block, parse for parameters and then call
  7751. the correct writing function."
  7752. (save-window-excursion
  7753. (let* ((pos (point))
  7754. (line (org-current-line))
  7755. (params (org-prepare-dblock))
  7756. (name (plist-get params :name))
  7757. (cmd (intern (concat "org-dblock-write:" name))))
  7758. (message "Updating dynamic block `%s' at line %d..." name line)
  7759. (funcall cmd params)
  7760. (message "Updating dynamic block `%s' at line %d...done" name line)
  7761. (goto-char pos))))
  7762. (defun org-beginning-of-dblock ()
  7763. "Find the beginning of the dynamic block at point.
  7764. Error if there is no such block at point."
  7765. (let ((pos (point))
  7766. beg)
  7767. (end-of-line 1)
  7768. (if (and (re-search-backward org-dblock-start-re nil t)
  7769. (setq beg (match-beginning 0))
  7770. (re-search-forward org-dblock-end-re nil t)
  7771. (> (match-end 0) pos))
  7772. (goto-char beg)
  7773. (goto-char pos)
  7774. (error "Not in a dynamic block"))))
  7775. (defun org-update-all-dblocks ()
  7776. "Update all dynamic blocks in the buffer.
  7777. This function can be used in a hook."
  7778. (when (org-mode-p)
  7779. (org-map-dblocks 'org-update-dblock)))
  7780. ;;;; Completion
  7781. (defconst org-additional-option-like-keywords
  7782. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7783. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7784. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7785. "BEGIN_QUOTE" "END_QUOTE"
  7786. "BEGIN_VERSE" "END_VERSE"
  7787. "BEGIN_CENTER" "END_CENTER"
  7788. "BEGIN_SRC" "END_SRC"
  7789. "CATEGORY" "COLUMNS"
  7790. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7791. (defcustom org-structure-template-alist
  7792. '(
  7793. ("s" "#+begin_src ?\n\n#+end_src"
  7794. "<src lang=\"?\">\n\n</src>")
  7795. ("e" "#+begin_example\n?\n#+end_example"
  7796. "<example>\n?\n</example>")
  7797. ("q" "#+begin_quote\n?\n#+end_quote"
  7798. "<quote>\n?\n</quote>")
  7799. ("v" "#+begin_verse\n?\n#+end_verse"
  7800. "<verse>\n?\n/verse>")
  7801. ("c" "#+begin_center\n?\n#+end_center"
  7802. "<center>\n?\n/center>")
  7803. ("l" "#+begin_latex\n?\n#+end_latex"
  7804. "<literal style=\"latex\">\n?\n</literal>")
  7805. ("L" "#+latex: "
  7806. "<literal style=\"latex\">?</literal>")
  7807. ("h" "#+begin_html\n?\n#+end_html"
  7808. "<literal style=\"html\">\n?\n</literal>")
  7809. ("H" "#+html: "
  7810. "<literal style=\"html\">?</literal>")
  7811. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7812. ("A" "#+ascii: ")
  7813. ("i" "#+include %file ?"
  7814. "<include file=%file markup=\"?\">")
  7815. )
  7816. "Structure completion elements.
  7817. This is a list of abbreviation keys and values. The value gets inserted
  7818. it you type @samp{.} followed by the key and then the completion key,
  7819. usually `M-TAB'. %file will be replaced by a file name after prompting
  7820. for the file using completion.
  7821. There are two templates for each key, the first uses the original Org syntax,
  7822. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7823. the default when the /org-mtags.el/ module has been loaded. See also the
  7824. variable `org-mtags-prefer-muse-templates'.
  7825. This is an experimental feature, it is undecided if it is going to stay in."
  7826. :group 'org-completion
  7827. :type '(repeat
  7828. (string :tag "Key")
  7829. (string :tag "Template")
  7830. (string :tag "Muse Template")))
  7831. (defun org-try-structure-completion ()
  7832. "Try to complete a structure template before point.
  7833. This looks for strings like \"<e\" on an otherwise empty line and
  7834. expands them."
  7835. (let ((l (buffer-substring (point-at-bol) (point)))
  7836. a)
  7837. (when (and (looking-at "[ \t]*$")
  7838. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7839. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7840. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7841. (match-beginning 1)) a)
  7842. t)))
  7843. (defun org-complete-expand-structure-template (start cell)
  7844. "Expand a structure template."
  7845. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7846. (rpl (nth (if musep 2 1) cell)))
  7847. (delete-region start (point))
  7848. (when (string-match "\\`#\\+" rpl)
  7849. (cond
  7850. ((bolp))
  7851. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7852. (delete-region (point-at-bol) (point)))
  7853. (t (newline))))
  7854. (setq start (point))
  7855. (if (string-match "%file" rpl)
  7856. (setq rpl (replace-match
  7857. (concat
  7858. "\""
  7859. (save-match-data
  7860. (abbreviate-file-name (read-file-name "Include file: ")))
  7861. "\"")
  7862. t t rpl)))
  7863. (insert rpl)
  7864. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7865. (defun org-complete (&optional arg)
  7866. "Perform completion on word at point.
  7867. At the beginning of a headline, this completes TODO keywords as given in
  7868. `org-todo-keywords'.
  7869. If the current word is preceded by a backslash, completes the TeX symbols
  7870. that are supported for HTML support.
  7871. If the current word is preceded by \"#+\", completes special words for
  7872. setting file options.
  7873. In the line after \"#+STARTUP:, complete valid keywords.\"
  7874. At all other locations, this simply calls the value of
  7875. `org-completion-fallback-command'."
  7876. (interactive "P")
  7877. (org-without-partial-completion
  7878. (catch 'exit
  7879. (let* ((a nil)
  7880. (end (point))
  7881. (beg1 (save-excursion
  7882. (skip-chars-backward (org-re "[:alnum:]_@"))
  7883. (point)))
  7884. (beg (save-excursion
  7885. (skip-chars-backward "a-zA-Z0-9_:$")
  7886. (point)))
  7887. (confirm (lambda (x) (stringp (car x))))
  7888. (searchhead (equal (char-before beg) ?*))
  7889. (struct
  7890. (when (and (member (char-before beg1) '(?. ?<))
  7891. (setq a (assoc (buffer-substring beg1 (point))
  7892. org-structure-template-alist)))
  7893. (org-complete-expand-structure-template (1- beg1) a)
  7894. (throw 'exit t)))
  7895. (tag (and (equal (char-before beg1) ?:)
  7896. (equal (char-after (point-at-bol)) ?*)))
  7897. (prop (and (equal (char-before beg1) ?:)
  7898. (not (equal (char-after (point-at-bol)) ?*))))
  7899. (texp (equal (char-before beg) ?\\))
  7900. (link (equal (char-before beg) ?\[))
  7901. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7902. beg)
  7903. "#+"))
  7904. (startup (string-match "^#\\+STARTUP:.*"
  7905. (buffer-substring (point-at-bol) (point))))
  7906. (completion-ignore-case opt)
  7907. (type nil)
  7908. (tbl nil)
  7909. (table (cond
  7910. (opt
  7911. (setq type :opt)
  7912. (require 'org-exp)
  7913. (append
  7914. (mapcar
  7915. (lambda (x)
  7916. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7917. (cons (match-string 2 x) (match-string 1 x)))
  7918. (org-split-string (org-get-current-options) "\n"))
  7919. (mapcar 'list org-additional-option-like-keywords)))
  7920. (startup
  7921. (setq type :startup)
  7922. org-startup-options)
  7923. (link (append org-link-abbrev-alist-local
  7924. org-link-abbrev-alist))
  7925. (texp
  7926. (setq type :tex)
  7927. org-html-entities)
  7928. ((string-match "\\`\\*+[ \t]+\\'"
  7929. (buffer-substring (point-at-bol) beg))
  7930. (setq type :todo)
  7931. (mapcar 'list org-todo-keywords-1))
  7932. (searchhead
  7933. (setq type :searchhead)
  7934. (save-excursion
  7935. (goto-char (point-min))
  7936. (while (re-search-forward org-todo-line-regexp nil t)
  7937. (push (list
  7938. (org-make-org-heading-search-string
  7939. (match-string 3) t))
  7940. tbl)))
  7941. tbl)
  7942. (tag (setq type :tag beg beg1)
  7943. (or org-tag-alist (org-get-buffer-tags)))
  7944. (prop (setq type :prop beg beg1)
  7945. (mapcar 'list (org-buffer-property-keys nil t t)))
  7946. (t (progn
  7947. (call-interactively org-completion-fallback-command)
  7948. (throw 'exit nil)))))
  7949. (pattern (buffer-substring-no-properties beg end))
  7950. (completion (try-completion pattern table confirm)))
  7951. (cond ((eq completion t)
  7952. (if (not (assoc (upcase pattern) table))
  7953. (message "Already complete")
  7954. (if (and (equal type :opt)
  7955. (not (member (car (assoc (upcase pattern) table))
  7956. org-additional-option-like-keywords)))
  7957. (insert (substring (cdr (assoc (upcase pattern) table))
  7958. (length pattern)))
  7959. (if (memq type '(:tag :prop)) (insert ":")))))
  7960. ((null completion)
  7961. (message "Can't find completion for \"%s\"" pattern)
  7962. (ding))
  7963. ((not (string= pattern completion))
  7964. (delete-region beg end)
  7965. (if (string-match " +$" completion)
  7966. (setq completion (replace-match "" t t completion)))
  7967. (insert completion)
  7968. (if (get-buffer-window "*Completions*")
  7969. (delete-window (get-buffer-window "*Completions*")))
  7970. (if (assoc completion table)
  7971. (if (eq type :todo) (insert " ")
  7972. (if (memq type '(:tag :prop)) (insert ":"))))
  7973. (if (and (equal type :opt) (assoc completion table))
  7974. (message "%s" (substitute-command-keys
  7975. "Press \\[org-complete] again to insert example settings"))))
  7976. (t
  7977. (message "Making completion list...")
  7978. (let ((list (sort (all-completions pattern table confirm)
  7979. 'string<)))
  7980. (with-output-to-temp-buffer "*Completions*"
  7981. (condition-case nil
  7982. ;; Protection needed for XEmacs and emacs 21
  7983. (display-completion-list list pattern)
  7984. (error (display-completion-list list)))))
  7985. (message "Making completion list...%s" "done")))))))
  7986. ;;;; TODO, DEADLINE, Comments
  7987. (defun org-toggle-comment ()
  7988. "Change the COMMENT state of an entry."
  7989. (interactive)
  7990. (save-excursion
  7991. (org-back-to-heading)
  7992. (let (case-fold-search)
  7993. (if (looking-at (concat outline-regexp
  7994. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7995. (replace-match "" t t nil 1)
  7996. (if (looking-at outline-regexp)
  7997. (progn
  7998. (goto-char (match-end 0))
  7999. (insert org-comment-string " ")))))))
  8000. (defvar org-last-todo-state-is-todo nil
  8001. "This is non-nil when the last TODO state change led to a TODO state.
  8002. If the last change removed the TODO tag or switched to DONE, then
  8003. this is nil.")
  8004. (defvar org-setting-tags nil) ; dynamically skipped
  8005. (defun org-parse-local-options (string var)
  8006. "Parse STRING for startup setting relevant for variable VAR."
  8007. (let ((rtn (symbol-value var))
  8008. e opts)
  8009. (save-match-data
  8010. (if (or (not string) (not (string-match "\\S-" string)))
  8011. rtn
  8012. (setq opts (delq nil (mapcar (lambda (x)
  8013. (setq e (assoc x org-startup-options))
  8014. (if (eq (nth 1 e) var) e nil))
  8015. (org-split-string string "[ \t]+"))))
  8016. (if (not opts)
  8017. rtn
  8018. (setq rtn nil)
  8019. (while (setq e (pop opts))
  8020. (if (not (nth 3 e))
  8021. (setq rtn (nth 2 e))
  8022. (if (not (listp rtn)) (setq rtn nil))
  8023. (push (nth 2 e) rtn)))
  8024. rtn)))))
  8025. (defvar org-todo-setup-filter-hook nil
  8026. "Hook for functions that pre-filter todo specs.
  8027. Each function takes a todo spec and returns either `nil' or the spec
  8028. transformed into canonical form." )
  8029. (defvar org-todo-get-default-hook nil
  8030. "Hook for functions that get a default item for todo.
  8031. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8032. `nil' or a string to be used for the todo mark." )
  8033. (defvar org-agenda-headline-snapshot-before-repeat)
  8034. (defun org-todo (&optional arg)
  8035. "Change the TODO state of an item.
  8036. The state of an item is given by a keyword at the start of the heading,
  8037. like
  8038. *** TODO Write paper
  8039. *** DONE Call mom
  8040. The different keywords are specified in the variable `org-todo-keywords'.
  8041. By default the available states are \"TODO\" and \"DONE\".
  8042. So for this example: when the item starts with TODO, it is changed to DONE.
  8043. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8044. DONE are present, add TODO at the beginning of the heading.
  8045. With C-u prefix arg, use completion to determine the new state.
  8046. With numeric prefix arg, switch to that state.
  8047. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8048. With a tripple C-u prefix, circumvent any state blocking.
  8049. For calling through lisp, arg is also interpreted in the following way:
  8050. 'none -> empty state
  8051. \"\"(empty string) -> switch to empty state
  8052. 'done -> switch to DONE
  8053. 'nextset -> switch to the next set of keywords
  8054. 'previousset -> switch to the previous set of keywords
  8055. \"WAITING\" -> switch to the specified keyword, but only if it
  8056. really is a member of `org-todo-keywords'."
  8057. (interactive "P")
  8058. (if (equal arg '(16)) (setq arg 'nextset))
  8059. (let ((org-blocker-hook org-blocker-hook)
  8060. (case-fold-search nil))
  8061. (when (equal arg '(64))
  8062. (setq arg nil org-blocker-hook nil))
  8063. (when (and org-blocker-hook
  8064. (org-entry-get nil "NOBLOCKING"))
  8065. (setq org-blocker-hook nil))
  8066. (save-excursion
  8067. (catch 'exit
  8068. (org-back-to-heading)
  8069. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8070. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8071. (looking-at " *"))
  8072. (let* ((match-data (match-data))
  8073. (startpos (point-at-bol))
  8074. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8075. (org-log-done org-log-done)
  8076. (org-log-repeat org-log-repeat)
  8077. (org-todo-log-states org-todo-log-states)
  8078. (this (match-string 1))
  8079. (hl-pos (match-beginning 0))
  8080. (head (org-get-todo-sequence-head this))
  8081. (ass (assoc head org-todo-kwd-alist))
  8082. (interpret (nth 1 ass))
  8083. (done-word (nth 3 ass))
  8084. (final-done-word (nth 4 ass))
  8085. (last-state (or this ""))
  8086. (completion-ignore-case t)
  8087. (member (member this org-todo-keywords-1))
  8088. (tail (cdr member))
  8089. (state (cond
  8090. ((and org-todo-key-trigger
  8091. (or (and (equal arg '(4))
  8092. (eq org-use-fast-todo-selection 'prefix))
  8093. (and (not arg) org-use-fast-todo-selection
  8094. (not (eq org-use-fast-todo-selection
  8095. 'prefix)))))
  8096. ;; Use fast selection
  8097. (org-fast-todo-selection))
  8098. ((and (equal arg '(4))
  8099. (or (not org-use-fast-todo-selection)
  8100. (not org-todo-key-trigger)))
  8101. ;; Read a state with completion
  8102. (org-ido-completing-read
  8103. "State: " (mapcar (lambda(x) (list x))
  8104. org-todo-keywords-1)
  8105. nil t))
  8106. ((eq arg 'right)
  8107. (if this
  8108. (if tail (car tail) nil)
  8109. (car org-todo-keywords-1)))
  8110. ((eq arg 'left)
  8111. (if (equal member org-todo-keywords-1)
  8112. nil
  8113. (if this
  8114. (nth (- (length org-todo-keywords-1)
  8115. (length tail) 2)
  8116. org-todo-keywords-1)
  8117. (org-last org-todo-keywords-1))))
  8118. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8119. (setq arg nil))) ; hack to fall back to cycling
  8120. (arg
  8121. ;; user or caller requests a specific state
  8122. (cond
  8123. ((equal arg "") nil)
  8124. ((eq arg 'none) nil)
  8125. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8126. ((eq arg 'nextset)
  8127. (or (car (cdr (member head org-todo-heads)))
  8128. (car org-todo-heads)))
  8129. ((eq arg 'previousset)
  8130. (let ((org-todo-heads (reverse org-todo-heads)))
  8131. (or (car (cdr (member head org-todo-heads)))
  8132. (car org-todo-heads))))
  8133. ((car (member arg org-todo-keywords-1)))
  8134. ((nth (1- (prefix-numeric-value arg))
  8135. org-todo-keywords-1))))
  8136. ((null member) (or head (car org-todo-keywords-1)))
  8137. ((equal this final-done-word) nil) ;; -> make empty
  8138. ((null tail) nil) ;; -> first entry
  8139. ((memq interpret '(type priority))
  8140. (if (eq this-command last-command)
  8141. (car tail)
  8142. (if (> (length tail) 0)
  8143. (or done-word (car org-done-keywords))
  8144. nil)))
  8145. (t
  8146. (car tail))))
  8147. (state (or
  8148. (run-hook-with-args-until-success
  8149. 'org-todo-get-default-hook state last-state)
  8150. state))
  8151. (next (if state (concat " " state " ") " "))
  8152. (change-plist (list :type 'todo-state-change :from this :to state
  8153. :position startpos))
  8154. dolog now-done-p)
  8155. (when org-blocker-hook
  8156. (setq org-last-todo-state-is-todo
  8157. (not (member this org-done-keywords)))
  8158. (unless (save-excursion
  8159. (save-match-data
  8160. (run-hook-with-args-until-failure
  8161. 'org-blocker-hook change-plist)))
  8162. (if (interactive-p)
  8163. (error "TODO state change from %s to %s blocked" this state)
  8164. ;; fail silently
  8165. (message "TODO state change from %s to %s blocked" this state)
  8166. (throw 'exit nil))))
  8167. (store-match-data match-data)
  8168. (replace-match next t t)
  8169. (unless (pos-visible-in-window-p hl-pos)
  8170. (message "TODO state changed to %s" (org-trim next)))
  8171. (unless head
  8172. (setq head (org-get-todo-sequence-head state)
  8173. ass (assoc head org-todo-kwd-alist)
  8174. interpret (nth 1 ass)
  8175. done-word (nth 3 ass)
  8176. final-done-word (nth 4 ass)))
  8177. (when (memq arg '(nextset previousset))
  8178. (message "Keyword-Set %d/%d: %s"
  8179. (- (length org-todo-sets) -1
  8180. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8181. (length org-todo-sets)
  8182. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8183. (setq org-last-todo-state-is-todo
  8184. (not (member state org-done-keywords)))
  8185. (setq now-done-p (and (member state org-done-keywords)
  8186. (not (member this org-done-keywords))))
  8187. (and logging (org-local-logging logging))
  8188. (when (and (or org-todo-log-states org-log-done)
  8189. (not (memq arg '(nextset previousset))))
  8190. ;; we need to look at recording a time and note
  8191. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8192. (nth 2 (assoc this org-todo-log-states))))
  8193. (when (and state
  8194. (member state org-not-done-keywords)
  8195. (not (member this org-not-done-keywords)))
  8196. ;; This is now a todo state and was not one before
  8197. ;; If there was a CLOSED time stamp, get rid of it.
  8198. (org-add-planning-info nil nil 'closed))
  8199. (when (and now-done-p org-log-done)
  8200. ;; It is now done, and it was not done before
  8201. (org-add-planning-info 'closed (org-current-time))
  8202. (if (and (not dolog) (eq 'note org-log-done))
  8203. (org-add-log-setup 'done state this 'findpos 'note)))
  8204. (when (and state dolog)
  8205. ;; This is a non-nil state, and we need to log it
  8206. (org-add-log-setup 'state state this 'findpos dolog)))
  8207. ;; Fixup tag positioning
  8208. (org-todo-trigger-tag-changes state)
  8209. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8210. (when org-provide-todo-statistics
  8211. (org-update-parent-todo-statistics))
  8212. (run-hooks 'org-after-todo-state-change-hook)
  8213. (if (and arg (not (member state org-done-keywords)))
  8214. (setq head (org-get-todo-sequence-head state)))
  8215. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8216. ;; Do we need to trigger a repeat?
  8217. (when now-done-p
  8218. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8219. ;; This is for the agenda, take a snapshot of the headline.
  8220. (save-match-data
  8221. (setq org-agenda-headline-snapshot-before-repeat
  8222. (org-get-heading))))
  8223. (org-auto-repeat-maybe state))
  8224. ;; Fixup cursor location if close to the keyword
  8225. (if (and (outline-on-heading-p)
  8226. (not (bolp))
  8227. (save-excursion (beginning-of-line 1)
  8228. (looking-at org-todo-line-regexp))
  8229. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8230. (progn
  8231. (goto-char (or (match-end 2) (match-end 1)))
  8232. (and (looking-at " ") (just-one-space))))
  8233. (when org-trigger-hook
  8234. (save-excursion
  8235. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8236. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8237. "Block turning an entry into a TODO, using the hierarchy.
  8238. This checks whether the current task should be blocked from state
  8239. changes. Such blocking occurs when:
  8240. 1. The task has children which are not all in a completed state.
  8241. 2. A task has a parent with the property :ORDERED:, and there
  8242. are siblings prior to the current task with incomplete
  8243. status.
  8244. 3. The parent of the task is blocked because it has siblings that should
  8245. be done first, or is child of a block grandparent TODO entry."
  8246. (catch 'dont-block
  8247. ;; If this is not a todo state change, or if this entry is already DONE,
  8248. ;; do not block
  8249. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8250. (member (plist-get change-plist :from)
  8251. (cons 'done org-done-keywords))
  8252. (member (plist-get change-plist :to)
  8253. (cons 'todo org-not-done-keywords)))
  8254. (throw 'dont-block t))
  8255. ;; If this task has children, and any are undone, it's blocked
  8256. (save-excursion
  8257. (org-back-to-heading t)
  8258. (let ((this-level (funcall outline-level)))
  8259. (outline-next-heading)
  8260. (let ((child-level (funcall outline-level)))
  8261. (while (and (not (eobp))
  8262. (> child-level this-level))
  8263. ;; this todo has children, check whether they are all
  8264. ;; completed
  8265. (if (and (not (org-entry-is-done-p))
  8266. (org-entry-is-todo-p))
  8267. (throw 'dont-block nil))
  8268. (outline-next-heading)
  8269. (setq child-level (funcall outline-level))))))
  8270. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8271. ;; any previous siblings are undone, it's blocked
  8272. (save-excursion
  8273. (org-back-to-heading t)
  8274. (let* ((pos (point))
  8275. (parent-pos (and (org-up-heading-safe) (point))))
  8276. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8277. (when (and (org-entry-get (point) "ORDERED")
  8278. (forward-line 1)
  8279. (re-search-forward org-not-done-heading-regexp pos t))
  8280. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8281. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8282. (while t
  8283. (goto-char parent-pos)
  8284. (if (not (looking-at org-not-done-heading-regexp))
  8285. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8286. (setq pos (point))
  8287. (setq parent-pos (and (org-up-heading-safe) (point)))
  8288. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8289. (when (and (org-entry-get (point) "ORDERED")
  8290. (forward-line 1)
  8291. (re-search-forward org-not-done-heading-regexp pos t))
  8292. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8293. (defcustom org-track-ordered-property-with-tag nil
  8294. "Should the ORDERED property also be shown as a tag?
  8295. The ORDERED property decides if an entry should require subtasks to be
  8296. completed in sequence. Since a property is not very visible, setting
  8297. this option means that toggling the ORDERED property with the command
  8298. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8299. not relevant for the behavior, but it makes things more visible.
  8300. Note that toggling the tag with tags commands will not change the property
  8301. and therefore not influence behavior!
  8302. This can be t, meaning the tag ORDERED should be used, It can also be a
  8303. string to select a different tag for this task."
  8304. :group 'org-todo
  8305. :type '(choice
  8306. (const :tag "No tracking" nil)
  8307. (const :tag "Track with ORDERED tag" t)
  8308. (string :tag "Use other tag")))
  8309. (defun org-toggle-ordered-property ()
  8310. "Toggle the ORDERED property of the current entry.
  8311. For better visibility, you can track the value of this property with a tag.
  8312. See variable `org-track-ordered-property-with-tag'."
  8313. (interactive)
  8314. (let* ((t1 org-track-ordered-property-with-tag)
  8315. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8316. (save-excursion
  8317. (org-back-to-heading)
  8318. (if (org-entry-get nil "ORDERED")
  8319. (progn
  8320. (org-delete-property "ORDERED")
  8321. (and tag (org-toggle-tag tag 'off))
  8322. (message "Subtasks can be completed in arbitrary order"))
  8323. (org-entry-put nil "ORDERED" "t")
  8324. (and tag (org-toggle-tag tag 'on))
  8325. (message "Subtasks must be completed in sequence")))))
  8326. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8327. (defun org-block-todo-from-checkboxes (change-plist)
  8328. "Block turning an entry into a TODO, using checkboxes.
  8329. This checks whether the current task should be blocked from state
  8330. changes because there are uncheckd boxes in this entry."
  8331. (catch 'dont-block
  8332. ;; If this is not a todo state change, or if this entry is already DONE,
  8333. ;; do not block
  8334. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8335. (member (plist-get change-plist :from)
  8336. (cons 'done org-done-keywords))
  8337. (member (plist-get change-plist :to)
  8338. (cons 'todo org-not-done-keywords)))
  8339. (throw 'dont-block t))
  8340. ;; If this task has checkboxes that are not checked, it's blocked
  8341. (save-excursion
  8342. (org-back-to-heading t)
  8343. (let ((beg (point)) end)
  8344. (outline-next-heading)
  8345. (setq end (point))
  8346. (goto-char beg)
  8347. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8348. end t)
  8349. (progn
  8350. (if (boundp 'org-blocked-by-checkboxes)
  8351. (setq org-blocked-by-checkboxes t))
  8352. (throw 'dont-block nil)))))
  8353. t)) ; do not block
  8354. (defun org-update-parent-todo-statistics ()
  8355. "Update any statistics cookie in the parent of the current headline."
  8356. (interactive)
  8357. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8358. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8359. (catch 'exit
  8360. (save-excursion
  8361. (setq level (org-up-heading-safe))
  8362. (unless level
  8363. (throw 'exit nil))
  8364. (while (re-search-forward box-re (point-at-eol) t)
  8365. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8366. (setq is-percent (match-end 2))
  8367. (save-match-data
  8368. (unless (outline-next-heading) (throw 'exit nil))
  8369. (while (looking-at org-todo-line-regexp)
  8370. (setq kwd (match-string 2))
  8371. (and kwd (setq cnt-all (1+ cnt-all)))
  8372. (and (member kwd org-done-keywords)
  8373. (setq cnt-done (1+ cnt-done)))
  8374. (condition-case nil
  8375. (org-forward-same-level 1)
  8376. (error (end-of-line 1)))))
  8377. (replace-match
  8378. (if is-percent
  8379. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8380. (format "[%d/%d]" cnt-done cnt-all))))
  8381. (when cookie-present
  8382. (run-hook-with-args 'org-after-todo-statistics-hook
  8383. cnt-done (- cnt-all cnt-done)))))))
  8384. (defvar org-after-todo-statistics-hook nil
  8385. "Hook that is called after a TODO statistics cookie has been updated.
  8386. Each function is called with two arguments: the number of not-done entries
  8387. and the number of done entries.
  8388. For example, the following function, when added to this hook, will switch
  8389. an entry to DONE when all children are done, and back to TODO when new
  8390. entries are set to a TODO status. Note that this hook is only called
  8391. when there is a statistics cookie in the headline!
  8392. (defun org-summary-todo (n-done n-not-done)
  8393. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8394. (let (org-log-done org-log-states) ; turn off logging
  8395. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8396. ")
  8397. (defun org-todo-trigger-tag-changes (state)
  8398. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8399. (let ((l org-todo-state-tags-triggers)
  8400. changes)
  8401. (when (or (not state) (equal state ""))
  8402. (setq changes (append changes (cdr (assoc "" l)))))
  8403. (when (and (stringp state) (> (length state) 0))
  8404. (setq changes (append changes (cdr (assoc state l)))))
  8405. (when (member state org-not-done-keywords)
  8406. (setq changes (append changes (cdr (assoc 'todo l)))))
  8407. (when (member state org-done-keywords)
  8408. (setq changes (append changes (cdr (assoc 'done l)))))
  8409. (dolist (c changes)
  8410. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8411. (defun org-local-logging (value)
  8412. "Get logging settings from a property VALUE."
  8413. (let* (words w a)
  8414. ;; directly set the variables, they are already local.
  8415. (setq org-log-done nil
  8416. org-log-repeat nil
  8417. org-todo-log-states nil)
  8418. (setq words (org-split-string value))
  8419. (while (setq w (pop words))
  8420. (cond
  8421. ((setq a (assoc w org-startup-options))
  8422. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8423. (set (nth 1 a) (nth 2 a))))
  8424. ((setq a (org-extract-log-state-settings w))
  8425. (and (member (car a) org-todo-keywords-1)
  8426. (push a org-todo-log-states)))))))
  8427. (defun org-get-todo-sequence-head (kwd)
  8428. "Return the head of the TODO sequence to which KWD belongs.
  8429. If KWD is not set, check if there is a text property remembering the
  8430. right sequence."
  8431. (let (p)
  8432. (cond
  8433. ((not kwd)
  8434. (or (get-text-property (point-at-bol) 'org-todo-head)
  8435. (progn
  8436. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8437. nil (point-at-eol)))
  8438. (get-text-property p 'org-todo-head))))
  8439. ((not (member kwd org-todo-keywords-1))
  8440. (car org-todo-keywords-1))
  8441. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8442. (defun org-fast-todo-selection ()
  8443. "Fast TODO keyword selection with single keys.
  8444. Returns the new TODO keyword, or nil if no state change should occur."
  8445. (let* ((fulltable org-todo-key-alist)
  8446. (done-keywords org-done-keywords) ;; needed for the faces.
  8447. (maxlen (apply 'max (mapcar
  8448. (lambda (x)
  8449. (if (stringp (car x)) (string-width (car x)) 0))
  8450. fulltable)))
  8451. (expert nil)
  8452. (fwidth (+ maxlen 3 1 3))
  8453. (ncol (/ (- (window-width) 4) fwidth))
  8454. tg cnt e c tbl
  8455. groups ingroup)
  8456. (save-excursion
  8457. (save-window-excursion
  8458. (if expert
  8459. (set-buffer (get-buffer-create " *Org todo*"))
  8460. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8461. (erase-buffer)
  8462. (org-set-local 'org-done-keywords done-keywords)
  8463. (setq tbl fulltable cnt 0)
  8464. (while (setq e (pop tbl))
  8465. (cond
  8466. ((equal e '(:startgroup))
  8467. (push '() groups) (setq ingroup t)
  8468. (when (not (= cnt 0))
  8469. (setq cnt 0)
  8470. (insert "\n"))
  8471. (insert "{ "))
  8472. ((equal e '(:endgroup))
  8473. (setq ingroup nil cnt 0)
  8474. (insert "}\n"))
  8475. ((equal e '(:newline))
  8476. (when (not (= cnt 0))
  8477. (setq cnt 0)
  8478. (insert "\n")
  8479. (setq e (car tbl))
  8480. (while (equal (car tbl) '(:newline))
  8481. (insert "\n")
  8482. (setq tbl (cdr tbl)))))
  8483. (t
  8484. (setq tg (car e) c (cdr e))
  8485. (if ingroup (push tg (car groups)))
  8486. (setq tg (org-add-props tg nil 'face
  8487. (org-get-todo-face tg)))
  8488. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8489. (insert "[" c "] " tg (make-string
  8490. (- fwidth 4 (length tg)) ?\ ))
  8491. (when (= (setq cnt (1+ cnt)) ncol)
  8492. (insert "\n")
  8493. (if ingroup (insert " "))
  8494. (setq cnt 0)))))
  8495. (insert "\n")
  8496. (goto-char (point-min))
  8497. (if (not expert) (org-fit-window-to-buffer))
  8498. (message "[a-z..]:Set [SPC]:clear")
  8499. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8500. (cond
  8501. ((or (= c ?\C-g)
  8502. (and (= c ?q) (not (rassoc c fulltable))))
  8503. (setq quit-flag t))
  8504. ((= c ?\ ) nil)
  8505. ((setq e (rassoc c fulltable) tg (car e))
  8506. tg)
  8507. (t (setq quit-flag t)))))))
  8508. (defun org-entry-is-todo-p ()
  8509. (member (org-get-todo-state) org-not-done-keywords))
  8510. (defun org-entry-is-done-p ()
  8511. (member (org-get-todo-state) org-done-keywords))
  8512. (defun org-get-todo-state ()
  8513. (save-excursion
  8514. (org-back-to-heading t)
  8515. (and (looking-at org-todo-line-regexp)
  8516. (match-end 2)
  8517. (match-string 2))))
  8518. (defun org-at-date-range-p (&optional inactive-ok)
  8519. "Is the cursor inside a date range?"
  8520. (interactive)
  8521. (save-excursion
  8522. (catch 'exit
  8523. (let ((pos (point)))
  8524. (skip-chars-backward "^[<\r\n")
  8525. (skip-chars-backward "<[")
  8526. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8527. (>= (match-end 0) pos)
  8528. (throw 'exit t))
  8529. (skip-chars-backward "^<[\r\n")
  8530. (skip-chars-backward "<[")
  8531. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8532. (>= (match-end 0) pos)
  8533. (throw 'exit t)))
  8534. nil)))
  8535. (defun org-get-repeat ()
  8536. "Check if there is a deadline/schedule with repeater in this entry."
  8537. (save-match-data
  8538. (save-excursion
  8539. (org-back-to-heading t)
  8540. (if (re-search-forward
  8541. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8542. (match-string 1)))))
  8543. (defvar org-last-changed-timestamp)
  8544. (defvar org-last-inserted-timestamp)
  8545. (defvar org-log-post-message)
  8546. (defvar org-log-note-purpose)
  8547. (defvar org-log-note-how)
  8548. (defvar org-log-note-extra)
  8549. (defun org-auto-repeat-maybe (done-word)
  8550. "Check if the current headline contains a repeated deadline/schedule.
  8551. If yes, set TODO state back to what it was and change the base date
  8552. of repeating deadline/scheduled time stamps to new date.
  8553. This function is run automatically after each state change to a DONE state."
  8554. ;; last-state is dynamically scoped into this function
  8555. (let* ((repeat (org-get-repeat))
  8556. (aa (assoc last-state org-todo-kwd-alist))
  8557. (interpret (nth 1 aa))
  8558. (head (nth 2 aa))
  8559. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8560. (msg "Entry repeats: ")
  8561. (org-log-done nil)
  8562. (org-todo-log-states nil)
  8563. (nshiftmax 10) (nshift 0)
  8564. re type n what ts time)
  8565. (when repeat
  8566. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8567. (org-todo (if (eq interpret 'type) last-state head))
  8568. (when org-log-repeat
  8569. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8570. (memq 'org-add-log-note post-command-hook))
  8571. ;; OK, we are already setup for some record
  8572. (if (eq org-log-repeat 'note)
  8573. ;; make sure we take a note, not only a time stamp
  8574. (setq org-log-note-how 'note))
  8575. ;; Set up for taking a record
  8576. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8577. last-state
  8578. 'findpos org-log-repeat)))
  8579. (org-back-to-heading t)
  8580. (org-add-planning-info nil nil 'closed)
  8581. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8582. org-deadline-time-regexp "\\)\\|\\("
  8583. org-ts-regexp "\\)"))
  8584. (while (re-search-forward
  8585. re (save-excursion (outline-next-heading) (point)) t)
  8586. (setq type (if (match-end 1) org-scheduled-string
  8587. (if (match-end 3) org-deadline-string "Plain:"))
  8588. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8589. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8590. (setq n (string-to-number (match-string 2 ts))
  8591. what (match-string 3 ts))
  8592. (if (equal what "w") (setq n (* n 7) what "d"))
  8593. ;; Preparation, see if we need to modify the start date for the change
  8594. (when (match-end 1)
  8595. (setq time (save-match-data (org-time-string-to-time ts)))
  8596. (cond
  8597. ((equal (match-string 1 ts) ".")
  8598. ;; Shift starting date to today
  8599. (org-timestamp-change
  8600. (- (time-to-days (current-time)) (time-to-days time))
  8601. 'day))
  8602. ((equal (match-string 1 ts) "+")
  8603. (while (or (= nshift 0)
  8604. (<= (time-to-days time) (time-to-days (current-time))))
  8605. (when (= (incf nshift) nshiftmax)
  8606. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8607. (error "Abort")))
  8608. (org-timestamp-change n (cdr (assoc what whata)))
  8609. (org-at-timestamp-p t)
  8610. (setq ts (match-string 1))
  8611. (setq time (save-match-data (org-time-string-to-time ts))))
  8612. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8613. ;; rematch, so that we have everything in place for the real shift
  8614. (org-at-timestamp-p t)
  8615. (setq ts (match-string 1))
  8616. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8617. (org-timestamp-change n (cdr (assoc what whata)))
  8618. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8619. (setq org-log-post-message msg)
  8620. (message "%s" msg))))
  8621. (defun org-show-todo-tree (arg)
  8622. "Make a compact tree which shows all headlines marked with TODO.
  8623. The tree will show the lines where the regexp matches, and all higher
  8624. headlines above the match.
  8625. With a \\[universal-argument] prefix, also show the DONE entries.
  8626. With a numeric prefix N, construct a sparse tree for the Nth element
  8627. of `org-todo-keywords-1'."
  8628. (interactive "P")
  8629. (let ((case-fold-search nil)
  8630. (kwd-re
  8631. (cond ((null arg) org-not-done-regexp)
  8632. ((equal arg '(4))
  8633. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8634. (mapcar 'list org-todo-keywords-1))))
  8635. (concat "\\("
  8636. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8637. "\\)\\>")))
  8638. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8639. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8640. org-todo-keywords-1)))
  8641. (t (error "Invalid prefix argument: %s" arg)))))
  8642. (message "%d TODO entries found"
  8643. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8644. (defun org-deadline (&optional remove time)
  8645. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8646. With argument REMOVE, remove any deadline from the item.
  8647. When TIME is set, it should be an internal time specification, and the
  8648. scheduling will use the corresponding date."
  8649. (interactive "P")
  8650. (if remove
  8651. (progn
  8652. (org-remove-timestamp-with-keyword org-deadline-string)
  8653. (message "Item no longer has a deadline."))
  8654. (if (org-get-repeat)
  8655. (error "Cannot change deadline on task with repeater, please do that by hand")
  8656. (org-add-planning-info 'deadline time 'closed)
  8657. (message "Deadline on %s" org-last-inserted-timestamp))))
  8658. (defun org-schedule (&optional remove time)
  8659. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8660. With argument REMOVE, remove any scheduling date from the item.
  8661. When TIME is set, it should be an internal time specification, and the
  8662. scheduling will use the corresponding date."
  8663. (interactive "P")
  8664. (if remove
  8665. (progn
  8666. (org-remove-timestamp-with-keyword org-scheduled-string)
  8667. (message "Item is no longer scheduled."))
  8668. (if (org-get-repeat)
  8669. (error "Cannot reschedule task with repeater, please do that by hand")
  8670. (org-add-planning-info 'scheduled time 'closed)
  8671. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8672. (defun org-get-scheduled-time (pom &optional inherit)
  8673. "Get the scheduled time as a time tuple, of a format suitable
  8674. for calling org-schedule with, or if there is no scheduling,
  8675. returns nil."
  8676. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8677. (when time
  8678. (apply 'encode-time (org-parse-time-string time)))))
  8679. (defun org-get-deadline-time (pom &optional inherit)
  8680. "Get the deadine as a time tuple, of a format suitable for
  8681. calling org-deadlin with, or if there is no scheduling, returns
  8682. nil."
  8683. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8684. (when time
  8685. (apply 'encode-time (org-parse-time-string time)))))
  8686. (defun org-remove-timestamp-with-keyword (keyword)
  8687. "Remove all time stamps with KEYWORD in the current entry."
  8688. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8689. beg)
  8690. (save-excursion
  8691. (org-back-to-heading t)
  8692. (setq beg (point))
  8693. (org-end-of-subtree t t)
  8694. (while (re-search-backward re beg t)
  8695. (replace-match "")
  8696. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8697. (equal (char-before) ?\ ))
  8698. (backward-delete-char 1)
  8699. (if (string-match "^[ \t]*$" (buffer-substring
  8700. (point-at-bol) (point-at-eol)))
  8701. (delete-region (point-at-bol)
  8702. (min (point-max) (1+ (point-at-eol))))))))))
  8703. (defun org-add-planning-info (what &optional time &rest remove)
  8704. "Insert new timestamp with keyword in the line directly after the headline.
  8705. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8706. If non is given, the user is prompted for a date.
  8707. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8708. be removed."
  8709. (interactive)
  8710. (let (org-time-was-given org-end-time-was-given ts
  8711. end default-time default-input)
  8712. (catch 'exit
  8713. (when (and (not time) (memq what '(scheduled deadline)))
  8714. ;; Try to get a default date/time from existing timestamp
  8715. (save-excursion
  8716. (org-back-to-heading t)
  8717. (setq end (save-excursion (outline-next-heading) (point)))
  8718. (when (re-search-forward (if (eq what 'scheduled)
  8719. org-scheduled-time-regexp
  8720. org-deadline-time-regexp)
  8721. end t)
  8722. (setq ts (match-string 1)
  8723. default-time
  8724. (apply 'encode-time (org-parse-time-string ts))
  8725. default-input (and ts (org-get-compact-tod ts))))))
  8726. (when what
  8727. ;; If necessary, get the time from the user
  8728. (setq time (or time (org-read-date nil 'to-time nil nil
  8729. default-time default-input))))
  8730. (when (and org-insert-labeled-timestamps-at-point
  8731. (member what '(scheduled deadline)))
  8732. (insert
  8733. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8734. (org-insert-time-stamp time org-time-was-given
  8735. nil nil nil (list org-end-time-was-given))
  8736. (setq what nil))
  8737. (save-excursion
  8738. (save-restriction
  8739. (let (col list elt ts buffer-invisibility-spec)
  8740. (org-back-to-heading t)
  8741. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8742. (goto-char (match-end 1))
  8743. (setq col (current-column))
  8744. (goto-char (match-end 0))
  8745. (if (eobp) (insert "\n") (forward-char 1))
  8746. (when (and (not what)
  8747. (not (looking-at
  8748. (concat "[ \t]*"
  8749. org-keyword-time-not-clock-regexp))))
  8750. ;; Nothing to add, nothing to remove...... :-)
  8751. (throw 'exit nil))
  8752. (if (and (not (looking-at outline-regexp))
  8753. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8754. "[^\r\n]*"))
  8755. (not (equal (match-string 1) org-clock-string)))
  8756. (narrow-to-region (match-beginning 0) (match-end 0))
  8757. (insert-before-markers "\n")
  8758. (backward-char 1)
  8759. (narrow-to-region (point) (point))
  8760. (and org-adapt-indentation (org-indent-to-column col)))
  8761. ;; Check if we have to remove something.
  8762. (setq list (cons what remove))
  8763. (while list
  8764. (setq elt (pop list))
  8765. (goto-char (point-min))
  8766. (when (or (and (eq elt 'scheduled)
  8767. (re-search-forward org-scheduled-time-regexp nil t))
  8768. (and (eq elt 'deadline)
  8769. (re-search-forward org-deadline-time-regexp nil t))
  8770. (and (eq elt 'closed)
  8771. (re-search-forward org-closed-time-regexp nil t)))
  8772. (replace-match "")
  8773. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8774. (if (looking-at " +") (replace-match ""))))
  8775. (goto-char (point-max))
  8776. (when what
  8777. (insert
  8778. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8779. (cond ((eq what 'scheduled) org-scheduled-string)
  8780. ((eq what 'deadline) org-deadline-string)
  8781. ((eq what 'closed) org-closed-string))
  8782. " ")
  8783. (setq ts (org-insert-time-stamp
  8784. time
  8785. (or org-time-was-given
  8786. (and (eq what 'closed) org-log-done-with-time))
  8787. (eq what 'closed)
  8788. nil nil (list org-end-time-was-given)))
  8789. (end-of-line 1))
  8790. (goto-char (point-min))
  8791. (widen)
  8792. (if (and (looking-at "[ \t]+\n")
  8793. (equal (char-before) ?\n))
  8794. (delete-region (1- (point)) (point-at-eol)))
  8795. ts))))))
  8796. (defvar org-log-note-marker (make-marker))
  8797. (defvar org-log-note-purpose nil)
  8798. (defvar org-log-note-state nil)
  8799. (defvar org-log-note-previous-state nil)
  8800. (defvar org-log-note-how nil)
  8801. (defvar org-log-note-extra nil)
  8802. (defvar org-log-note-window-configuration nil)
  8803. (defvar org-log-note-return-to (make-marker))
  8804. (defvar org-log-post-message nil
  8805. "Message to be displayed after a log note has been stored.
  8806. The auto-repeater uses this.")
  8807. (defun org-add-note ()
  8808. "Add a note to the current entry.
  8809. This is done in the same way as adding a state change note."
  8810. (interactive)
  8811. (org-add-log-setup 'note nil nil 'findpos nil))
  8812. (defvar org-property-end-re)
  8813. (defun org-add-log-setup (&optional purpose state prev-state
  8814. findpos how &optional extra)
  8815. "Set up the post command hook to take a note.
  8816. If this is about to TODO state change, the new state is expected in STATE.
  8817. When FINDPOS is non-nil, find the correct position for the note in
  8818. the current entry. If not, assume that it can be inserted at point.
  8819. HOW is an indicator what kind of note should be created.
  8820. EXTRA is additional text that will be inserted into the notes buffer."
  8821. (let ((drawer (cond ((stringp org-log-into-drawer)
  8822. org-log-into-drawer)
  8823. (org-log-into-drawer "LOGBOOK")
  8824. (t nil))))
  8825. (save-restriction
  8826. (save-excursion
  8827. (when findpos
  8828. (org-back-to-heading t)
  8829. (narrow-to-region (point) (save-excursion
  8830. (outline-next-heading) (point)))
  8831. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8832. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8833. "[^\r\n]*\\)?"))
  8834. (goto-char (match-end 0))
  8835. (cond
  8836. (drawer
  8837. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8838. nil t)
  8839. (progn
  8840. (goto-char (match-end 0))
  8841. (or org-log-states-order-reversed
  8842. (and (re-search-forward org-property-end-re nil t)
  8843. (goto-char (1- (match-beginning 0))))))
  8844. (insert "\n:" drawer ":\n:END:")
  8845. (beginning-of-line 0)
  8846. (org-indent-line-function)
  8847. (beginning-of-line 2)
  8848. (org-indent-line-function)
  8849. (end-of-line 0)))
  8850. ((and org-log-state-notes-insert-after-drawers
  8851. (save-excursion
  8852. (forward-line) (looking-at org-drawer-regexp)))
  8853. (forward-line)
  8854. (while (looking-at org-drawer-regexp)
  8855. (goto-char (match-end 0))
  8856. (re-search-forward org-property-end-re (point-max) t)
  8857. (forward-line))
  8858. (forward-line -1)))
  8859. (unless org-log-states-order-reversed
  8860. (and (= (char-after) ?\n) (forward-char 1))
  8861. (org-skip-over-state-notes)
  8862. (skip-chars-backward " \t\n\r")))
  8863. (move-marker org-log-note-marker (point))
  8864. (setq org-log-note-purpose purpose
  8865. org-log-note-state state
  8866. org-log-note-previous-state prev-state
  8867. org-log-note-how how
  8868. org-log-note-extra extra)
  8869. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8870. (defun org-skip-over-state-notes ()
  8871. "Skip past the list of State notes in an entry."
  8872. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8873. (while (looking-at "[ \t]*- State")
  8874. (condition-case nil
  8875. (org-next-item)
  8876. (error (org-end-of-item)))))
  8877. (defun org-add-log-note (&optional purpose)
  8878. "Pop up a window for taking a note, and add this note later at point."
  8879. (remove-hook 'post-command-hook 'org-add-log-note)
  8880. (setq org-log-note-window-configuration (current-window-configuration))
  8881. (delete-other-windows)
  8882. (move-marker org-log-note-return-to (point))
  8883. (switch-to-buffer (marker-buffer org-log-note-marker))
  8884. (goto-char org-log-note-marker)
  8885. (org-switch-to-buffer-other-window "*Org Note*")
  8886. (erase-buffer)
  8887. (if (memq org-log-note-how '(time state))
  8888. (let (current-prefix-arg) (org-store-log-note))
  8889. (let ((org-inhibit-startup t)) (org-mode))
  8890. (insert (format "# Insert note for %s.
  8891. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8892. (cond
  8893. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8894. ((eq org-log-note-purpose 'done) "closed todo item")
  8895. ((eq org-log-note-purpose 'state)
  8896. (format "state change from \"%s\" to \"%s\""
  8897. (or org-log-note-previous-state "")
  8898. (or org-log-note-state "")))
  8899. ((eq org-log-note-purpose 'note)
  8900. "this entry")
  8901. (t (error "This should not happen")))))
  8902. (if org-log-note-extra (insert org-log-note-extra))
  8903. (org-set-local 'org-finish-function 'org-store-log-note)))
  8904. (defvar org-note-abort nil) ; dynamically scoped
  8905. (defun org-store-log-note ()
  8906. "Finish taking a log note, and insert it to where it belongs."
  8907. (let ((txt (buffer-string))
  8908. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8909. lines ind)
  8910. (kill-buffer (current-buffer))
  8911. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8912. (setq txt (replace-match "" t t txt)))
  8913. (if (string-match "\\s-+\\'" txt)
  8914. (setq txt (replace-match "" t t txt)))
  8915. (setq lines (org-split-string txt "\n"))
  8916. (when (and note (string-match "\\S-" note))
  8917. (setq note
  8918. (org-replace-escapes
  8919. note
  8920. (list (cons "%u" (user-login-name))
  8921. (cons "%U" user-full-name)
  8922. (cons "%t" (format-time-string
  8923. (org-time-stamp-format 'long 'inactive)
  8924. (current-time)))
  8925. (cons "%s" (if org-log-note-state
  8926. (concat "\"" org-log-note-state "\"")
  8927. ""))
  8928. (cons "%S" (if org-log-note-previous-state
  8929. (concat "\"" org-log-note-previous-state "\"")
  8930. "\"\"")))))
  8931. (if lines (setq note (concat note " \\\\")))
  8932. (push note lines))
  8933. (when (or current-prefix-arg org-note-abort)
  8934. (when org-log-into-drawer
  8935. (org-remove-empty-drawer-at
  8936. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8937. org-log-note-marker))
  8938. (setq lines nil))
  8939. (when lines
  8940. (save-excursion
  8941. (set-buffer (marker-buffer org-log-note-marker))
  8942. (save-excursion
  8943. (goto-char org-log-note-marker)
  8944. (move-marker org-log-note-marker nil)
  8945. (end-of-line 1)
  8946. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8947. (insert "- " (pop lines))
  8948. (org-indent-line-function)
  8949. (beginning-of-line 1)
  8950. (looking-at "[ \t]*")
  8951. (setq ind (concat (match-string 0) " "))
  8952. (end-of-line 1)
  8953. (while lines (insert "\n" ind (pop lines)))
  8954. (message "Note stored")
  8955. (org-back-to-heading t)
  8956. (org-cycle-hide-drawers 'children)))))
  8957. (set-window-configuration org-log-note-window-configuration)
  8958. (with-current-buffer (marker-buffer org-log-note-return-to)
  8959. (goto-char org-log-note-return-to))
  8960. (move-marker org-log-note-return-to nil)
  8961. (and org-log-post-message (message "%s" org-log-post-message)))
  8962. (defun org-remove-empty-drawer-at (drawer pos)
  8963. "Remove an emptyr DARWER drawer at position POS.
  8964. POS may also be a marker."
  8965. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8966. (save-excursion
  8967. (save-restriction
  8968. (widen)
  8969. (goto-char pos)
  8970. (if (org-in-regexp
  8971. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8972. (replace-match ""))))))
  8973. (defun org-sparse-tree (&optional arg)
  8974. "Create a sparse tree, prompt for the details.
  8975. This command can create sparse trees. You first need to select the type
  8976. of match used to create the tree:
  8977. t Show entries with a specific TODO keyword.
  8978. m Show entries selected by a tags/property match.
  8979. p Enter a property name and its value (both with completion on existing
  8980. names/values) and show entries with that property.
  8981. r Show entries matching a regular expression.
  8982. d Show deadlines due within `org-deadline-warning-days'.
  8983. b Show deadlines and scheduled items before a date.
  8984. a Show deadlines and scheduled items after a date."
  8985. (interactive "P")
  8986. (let (ans kwd value)
  8987. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8988. (setq ans (read-char-exclusive))
  8989. (cond
  8990. ((equal ans ?d)
  8991. (call-interactively 'org-check-deadlines))
  8992. ((equal ans ?b)
  8993. (call-interactively 'org-check-before-date))
  8994. ((equal ans ?a)
  8995. (call-interactively 'org-check-after-date))
  8996. ((equal ans ?t)
  8997. (org-show-todo-tree '(4)))
  8998. ((member ans '(?T ?m))
  8999. (call-interactively 'org-match-sparse-tree))
  9000. ((member ans '(?p ?P))
  9001. (setq kwd (org-ido-completing-read "Property: "
  9002. (mapcar 'list (org-buffer-property-keys))))
  9003. (setq value (org-ido-completing-read "Value: "
  9004. (mapcar 'list (org-property-values kwd))))
  9005. (unless (string-match "\\`{.*}\\'" value)
  9006. (setq value (concat "\"" value "\"")))
  9007. (org-match-sparse-tree arg (concat kwd "=" value)))
  9008. ((member ans '(?r ?R ?/))
  9009. (call-interactively 'org-occur))
  9010. (t (error "No such sparse tree command \"%c\"" ans)))))
  9011. (defvar org-occur-highlights nil
  9012. "List of overlays used for occur matches.")
  9013. (make-variable-buffer-local 'org-occur-highlights)
  9014. (defvar org-occur-parameters nil
  9015. "Parameters of the active org-occur calls.
  9016. This is a list, each call to org-occur pushes as cons cell,
  9017. containing the regular expression and the callback, onto the list.
  9018. The list can contain several entries if `org-occur' has been called
  9019. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9020. will only contain one set of parameters. When the highlights are
  9021. removed (for example with `C-c C-c', or with the next edit (depending
  9022. on `org-remove-highlights-with-change'), this variable is emptied
  9023. as well.")
  9024. (make-variable-buffer-local 'org-occur-parameters)
  9025. (defun org-occur (regexp &optional keep-previous callback)
  9026. "Make a compact tree which shows all matches of REGEXP.
  9027. The tree will show the lines where the regexp matches, and all higher
  9028. headlines above the match. It will also show the heading after the match,
  9029. to make sure editing the matching entry is easy.
  9030. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9031. call to `org-occur' will be kept, to allow stacking of calls to this
  9032. command.
  9033. If CALLBACK is non-nil, it is a function which is called to confirm
  9034. that the match should indeed be shown."
  9035. (interactive "sRegexp: \nP")
  9036. (unless keep-previous
  9037. (org-remove-occur-highlights nil nil t))
  9038. (push (cons regexp callback) org-occur-parameters)
  9039. (let ((cnt 0))
  9040. (save-excursion
  9041. (goto-char (point-min))
  9042. (if (or (not keep-previous) ; do not want to keep
  9043. (not org-occur-highlights)) ; no previous matches
  9044. ;; hide everything
  9045. (org-overview))
  9046. (while (re-search-forward regexp nil t)
  9047. (when (or (not callback)
  9048. (save-match-data (funcall callback)))
  9049. (setq cnt (1+ cnt))
  9050. (when org-highlight-sparse-tree-matches
  9051. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9052. (org-show-context 'occur-tree))))
  9053. (when org-remove-highlights-with-change
  9054. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9055. nil 'local))
  9056. (unless org-sparse-tree-open-archived-trees
  9057. (org-hide-archived-subtrees (point-min) (point-max)))
  9058. (run-hooks 'org-occur-hook)
  9059. (if (interactive-p)
  9060. (message "%d match(es) for regexp %s" cnt regexp))
  9061. cnt))
  9062. (defun org-show-context (&optional key)
  9063. "Make sure point and context and visible.
  9064. How much context is shown depends upon the variables
  9065. `org-show-hierarchy-above', `org-show-following-heading'. and
  9066. `org-show-siblings'."
  9067. (let ((heading-p (org-on-heading-p t))
  9068. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9069. (following-p (org-get-alist-option org-show-following-heading key))
  9070. (entry-p (org-get-alist-option org-show-entry-below key))
  9071. (siblings-p (org-get-alist-option org-show-siblings key)))
  9072. (catch 'exit
  9073. ;; Show heading or entry text
  9074. (if (and heading-p (not entry-p))
  9075. (org-flag-heading nil) ; only show the heading
  9076. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9077. (org-show-hidden-entry))) ; show entire entry
  9078. (when following-p
  9079. ;; Show next sibling, or heading below text
  9080. (save-excursion
  9081. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9082. (org-flag-heading nil))))
  9083. (when siblings-p (org-show-siblings))
  9084. (when hierarchy-p
  9085. ;; show all higher headings, possibly with siblings
  9086. (save-excursion
  9087. (while (and (condition-case nil
  9088. (progn (org-up-heading-all 1) t)
  9089. (error nil))
  9090. (not (bobp)))
  9091. (org-flag-heading nil)
  9092. (when siblings-p (org-show-siblings))))))))
  9093. (defun org-reveal (&optional siblings)
  9094. "Show current entry, hierarchy above it, and the following headline.
  9095. This can be used to show a consistent set of context around locations
  9096. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9097. not t for the search context.
  9098. With optional argument SIBLINGS, on each level of the hierarchy all
  9099. siblings are shown. This repairs the tree structure to what it would
  9100. look like when opened with hierarchical calls to `org-cycle'."
  9101. (interactive "P")
  9102. (let ((org-show-hierarchy-above t)
  9103. (org-show-following-heading t)
  9104. (org-show-siblings (if siblings t org-show-siblings)))
  9105. (org-show-context nil)))
  9106. (defun org-highlight-new-match (beg end)
  9107. "Highlight from BEG to END and mark the highlight is an occur headline."
  9108. (let ((ov (org-make-overlay beg end)))
  9109. (org-overlay-put ov 'face 'secondary-selection)
  9110. (push ov org-occur-highlights)))
  9111. (defun org-remove-occur-highlights (&optional beg end noremove)
  9112. "Remove the occur highlights from the buffer.
  9113. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9114. from the `before-change-functions' in the current buffer."
  9115. (interactive)
  9116. (unless org-inhibit-highlight-removal
  9117. (mapc 'org-delete-overlay org-occur-highlights)
  9118. (setq org-occur-highlights nil)
  9119. (setq org-occur-parameters nil)
  9120. (unless noremove
  9121. (remove-hook 'before-change-functions
  9122. 'org-remove-occur-highlights 'local))))
  9123. ;;;; Priorities
  9124. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9125. "Regular expression matching the priority indicator.")
  9126. (defvar org-remove-priority-next-time nil)
  9127. (defun org-priority-up ()
  9128. "Increase the priority of the current item."
  9129. (interactive)
  9130. (org-priority 'up))
  9131. (defun org-priority-down ()
  9132. "Decrease the priority of the current item."
  9133. (interactive)
  9134. (org-priority 'down))
  9135. (defun org-priority (&optional action)
  9136. "Change the priority of an item by ARG.
  9137. ACTION can be `set', `up', `down', or a character."
  9138. (interactive)
  9139. (setq action (or action 'set))
  9140. (let (current new news have remove)
  9141. (save-excursion
  9142. (org-back-to-heading t)
  9143. (if (looking-at org-priority-regexp)
  9144. (setq current (string-to-char (match-string 2))
  9145. have t)
  9146. (setq current org-default-priority))
  9147. (cond
  9148. ((or (eq action 'set)
  9149. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9150. (if (not (eq action 'set))
  9151. (setq new action)
  9152. (message "Priority %c-%c, SPC to remove: "
  9153. org-highest-priority org-lowest-priority)
  9154. (setq new (read-char-exclusive)))
  9155. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9156. (= (upcase org-lowest-priority) org-lowest-priority))
  9157. (setq new (upcase new)))
  9158. (cond ((equal new ?\ ) (setq remove t))
  9159. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9160. (error "Priority must be between `%c' and `%c'"
  9161. org-highest-priority org-lowest-priority))))
  9162. ((eq action 'up)
  9163. (if (and (not have) (eq last-command this-command))
  9164. (setq new org-lowest-priority)
  9165. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9166. org-default-priority (1- current)))))
  9167. ((eq action 'down)
  9168. (if (and (not have) (eq last-command this-command))
  9169. (setq new org-highest-priority)
  9170. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9171. org-default-priority (1+ current)))))
  9172. (t (error "Invalid action")))
  9173. (if (or (< (upcase new) org-highest-priority)
  9174. (> (upcase new) org-lowest-priority))
  9175. (setq remove t))
  9176. (setq news (format "%c" new))
  9177. (if have
  9178. (if remove
  9179. (replace-match "" t t nil 1)
  9180. (replace-match news t t nil 2))
  9181. (if remove
  9182. (error "No priority cookie found in line")
  9183. (let ((case-fold-search nil))
  9184. (looking-at org-todo-line-regexp))
  9185. (if (match-end 2)
  9186. (progn
  9187. (goto-char (match-end 2))
  9188. (insert " [#" news "]"))
  9189. (goto-char (match-beginning 3))
  9190. (insert "[#" news "] ")))))
  9191. (org-preserve-lc (org-set-tags nil 'align))
  9192. (if remove
  9193. (message "Priority removed")
  9194. (message "Priority of current item set to %s" news))))
  9195. (defun org-get-priority (s)
  9196. "Find priority cookie and return priority."
  9197. (save-match-data
  9198. (if (not (string-match org-priority-regexp s))
  9199. (* 1000 (- org-lowest-priority org-default-priority))
  9200. (* 1000 (- org-lowest-priority
  9201. (string-to-char (match-string 2 s)))))))
  9202. ;;;; Tags
  9203. (defvar org-agenda-archives-mode)
  9204. (defvar org-map-continue-from nil
  9205. "Position from where mapping should continue.
  9206. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9207. (defvar org-scanner-tags nil
  9208. "The current tag list while the tags scanner is running.")
  9209. (defvar org-trust-scanner-tags nil
  9210. "Should `org-get-tags-at' use the tags fro the scanner.
  9211. This is for internal dynamical scoping only.
  9212. When this is non-nil, the function `org-get-tags-at' will return the value
  9213. of `org-scanner-tags' instead of building the list by itself. This
  9214. can lead to large speed-ups when the tags scanner is used in a file with
  9215. many entries, and when the list of tags is retrieved, for example to
  9216. obtain a list of properties. Building the tags list for each entry in such
  9217. a file becomes an N^2 operation - but with this variable set, it scales
  9218. as N.")
  9219. (defun org-scan-tags (action matcher &optional todo-only)
  9220. "Scan headline tags with inheritance and produce output ACTION.
  9221. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9222. or `agenda' to produce an entry list for an agenda view. It can also be
  9223. a Lisp form or a function that should be called at each matched headline, in
  9224. this case the return value is a list of all return values from these calls.
  9225. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9226. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9227. only lines with a TODO keyword are included in the output."
  9228. (require 'org-agenda)
  9229. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9230. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9231. (org-re
  9232. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9233. (props (list 'face 'default
  9234. 'done-face 'org-agenda-done
  9235. 'undone-face 'default
  9236. 'mouse-face 'highlight
  9237. 'org-not-done-regexp org-not-done-regexp
  9238. 'org-todo-regexp org-todo-regexp
  9239. 'keymap org-agenda-keymap
  9240. 'help-echo
  9241. (format "mouse-2 or RET jump to org file %s"
  9242. (abbreviate-file-name
  9243. (or (buffer-file-name (buffer-base-buffer))
  9244. (buffer-name (buffer-base-buffer)))))))
  9245. (case-fold-search nil)
  9246. (org-map-continue-from nil)
  9247. lspos tags tags-list
  9248. (tags-alist (list (cons 0 org-file-tags)))
  9249. (llast 0) rtn rtn1 level category i txt
  9250. todo marker entry priority)
  9251. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9252. (setq action (list 'lambda nil action)))
  9253. (save-excursion
  9254. (goto-char (point-min))
  9255. (when (eq action 'sparse-tree)
  9256. (org-overview)
  9257. (org-remove-occur-highlights))
  9258. (while (re-search-forward re nil t)
  9259. (catch :skip
  9260. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9261. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9262. (goto-char (setq lspos (match-beginning 0)))
  9263. (setq level (org-reduced-level (funcall outline-level))
  9264. category (org-get-category))
  9265. (setq i llast llast level)
  9266. ;; remove tag lists from same and sublevels
  9267. (while (>= i level)
  9268. (when (setq entry (assoc i tags-alist))
  9269. (setq tags-alist (delete entry tags-alist)))
  9270. (setq i (1- i)))
  9271. ;; add the next tags
  9272. (when tags
  9273. (setq tags (org-split-string tags ":")
  9274. tags-alist
  9275. (cons (cons level tags) tags-alist)))
  9276. ;; compile tags for current headline
  9277. (setq tags-list
  9278. (if org-use-tag-inheritance
  9279. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9280. tags)
  9281. org-scanner-tags tags-list)
  9282. (when org-use-tag-inheritance
  9283. (setcdr (car tags-alist)
  9284. (mapcar (lambda (x)
  9285. (setq x (copy-sequence x))
  9286. (org-add-prop-inherited x))
  9287. (cdar tags-alist))))
  9288. (when (and tags org-use-tag-inheritance
  9289. (or (not (eq t org-use-tag-inheritance))
  9290. org-tags-exclude-from-inheritance))
  9291. ;; selective inheritance, remove uninherited ones
  9292. (setcdr (car tags-alist)
  9293. (org-remove-uniherited-tags (cdar tags-alist))))
  9294. (when (and (or (not todo-only)
  9295. (and (member todo org-not-done-keywords)
  9296. (or (not org-agenda-tags-todo-honor-ignore-options)
  9297. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9298. (let ((case-fold-search t)) (eval matcher))
  9299. (or
  9300. (not (member org-archive-tag tags-list))
  9301. ;; we have an archive tag, should we use this anyway?
  9302. (or (not org-agenda-skip-archived-trees)
  9303. (and (eq action 'agenda) org-agenda-archives-mode))))
  9304. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9305. ;; select this headline
  9306. (cond
  9307. ((eq action 'sparse-tree)
  9308. (and org-highlight-sparse-tree-matches
  9309. (org-get-heading) (match-end 0)
  9310. (org-highlight-new-match
  9311. (match-beginning 0) (match-beginning 1)))
  9312. (org-show-context 'tags-tree))
  9313. ((eq action 'agenda)
  9314. (setq txt (org-format-agenda-item
  9315. ""
  9316. (concat
  9317. (if (eq org-tags-match-list-sublevels 'indented)
  9318. (make-string (1- level) ?.) "")
  9319. (org-get-heading))
  9320. category
  9321. tags-list
  9322. )
  9323. priority (org-get-priority txt))
  9324. (goto-char lspos)
  9325. (setq marker (org-agenda-new-marker))
  9326. (org-add-props txt props
  9327. 'org-marker marker 'org-hd-marker marker 'org-category category
  9328. 'todo-state todo
  9329. 'priority priority 'type "tagsmatch")
  9330. (push txt rtn))
  9331. ((functionp action)
  9332. (setq org-map-continue-from nil)
  9333. (save-excursion
  9334. (setq rtn1 (funcall action))
  9335. (push rtn1 rtn)))
  9336. (t (error "Invalid action")))
  9337. ;; if we are to skip sublevels, jump to end of subtree
  9338. (unless org-tags-match-list-sublevels
  9339. (org-end-of-subtree t)
  9340. (backward-char 1))))
  9341. ;; Get the correct position from where to continue
  9342. (if org-map-continue-from
  9343. (goto-char org-map-continue-from)
  9344. (and (= (point) lspos) (end-of-line 1)))))
  9345. (when (and (eq action 'sparse-tree)
  9346. (not org-sparse-tree-open-archived-trees))
  9347. (org-hide-archived-subtrees (point-min) (point-max)))
  9348. (nreverse rtn)))
  9349. (defun org-remove-uniherited-tags (tags)
  9350. "Remove all tags that are not inherited from the list TAGS."
  9351. (cond
  9352. ((eq org-use-tag-inheritance t)
  9353. (if org-tags-exclude-from-inheritance
  9354. (org-delete-all org-tags-exclude-from-inheritance tags)
  9355. tags))
  9356. ((not org-use-tag-inheritance) nil)
  9357. ((stringp org-use-tag-inheritance)
  9358. (delq nil (mapcar
  9359. (lambda (x)
  9360. (if (and (string-match org-use-tag-inheritance x)
  9361. (not (member x org-tags-exclude-from-inheritance)))
  9362. x nil))
  9363. tags)))
  9364. ((listp org-use-tag-inheritance)
  9365. (delq nil (mapcar
  9366. (lambda (x)
  9367. (if (member x org-use-tag-inheritance) x nil))
  9368. tags)))))
  9369. (defvar todo-only) ;; dynamically scoped
  9370. (defun org-match-sparse-tree (&optional todo-only match)
  9371. "Create a sparse tree according to tags string MATCH.
  9372. MATCH can contain positive and negative selection of tags, like
  9373. \"+WORK+URGENT-WITHBOSS\".
  9374. If optional argument TODO-ONLY is non-nil, only select lines that are
  9375. also TODO lines."
  9376. (interactive "P")
  9377. (org-prepare-agenda-buffers (list (current-buffer)))
  9378. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9379. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9380. (defvar org-cached-props nil)
  9381. (defun org-cached-entry-get (pom property)
  9382. (if (or (eq t org-use-property-inheritance)
  9383. (and (stringp org-use-property-inheritance)
  9384. (string-match org-use-property-inheritance property))
  9385. (and (listp org-use-property-inheritance)
  9386. (member property org-use-property-inheritance)))
  9387. ;; Caching is not possible, check it directly
  9388. (org-entry-get pom property 'inherit)
  9389. ;; Get all properties, so that we can do complicated checks easily
  9390. (cdr (assoc property (or org-cached-props
  9391. (setq org-cached-props
  9392. (org-entry-properties pom)))))))
  9393. (defun org-global-tags-completion-table (&optional files)
  9394. "Return the list of all tags in all agenda buffer/files."
  9395. (save-excursion
  9396. (org-uniquify
  9397. (delq nil
  9398. (apply 'append
  9399. (mapcar
  9400. (lambda (file)
  9401. (set-buffer (find-file-noselect file))
  9402. (append (org-get-buffer-tags)
  9403. (mapcar (lambda (x) (if (stringp (car-safe x))
  9404. (list (car-safe x)) nil))
  9405. org-tag-alist)))
  9406. (if (and files (car files))
  9407. files
  9408. (org-agenda-files))))))))
  9409. (defun org-make-tags-matcher (match)
  9410. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9411. ;; todo-only is scoped dynamically into this function, and the function
  9412. ;; may change it if the matcher asks for it.
  9413. (unless match
  9414. ;; Get a new match request, with completion
  9415. (let ((org-last-tags-completion-table
  9416. (org-global-tags-completion-table)))
  9417. (setq match (org-completing-read-no-ido
  9418. "Match: " 'org-tags-completion-function nil nil nil
  9419. 'org-tags-history))))
  9420. ;; Parse the string and create a lisp form
  9421. (let ((match0 match)
  9422. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9423. minus tag mm
  9424. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9425. orterms term orlist re-p str-p level-p level-op time-p
  9426. prop-p pn pv po cat-p gv rest)
  9427. (if (string-match "/+" match)
  9428. ;; match contains also a todo-matching request
  9429. (progn
  9430. (setq tagsmatch (substring match 0 (match-beginning 0))
  9431. todomatch (substring match (match-end 0)))
  9432. (if (string-match "^!" todomatch)
  9433. (setq todo-only t todomatch (substring todomatch 1)))
  9434. (if (string-match "^\\s-*$" todomatch)
  9435. (setq todomatch nil)))
  9436. ;; only matching tags
  9437. (setq tagsmatch match todomatch nil))
  9438. ;; Make the tags matcher
  9439. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9440. (setq tagsmatcher t)
  9441. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9442. (while (setq term (pop orterms))
  9443. (while (and (equal (substring term -1) "\\") orterms)
  9444. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9445. (while (string-match re term)
  9446. (setq rest (substring term (match-end 0))
  9447. minus (and (match-end 1)
  9448. (equal (match-string 1 term) "-"))
  9449. tag (match-string 2 term)
  9450. re-p (equal (string-to-char tag) ?{)
  9451. level-p (match-end 4)
  9452. prop-p (match-end 5)
  9453. mm (cond
  9454. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9455. (level-p
  9456. (setq level-op (org-op-to-function (match-string 3 term)))
  9457. `(,level-op level ,(string-to-number
  9458. (match-string 4 term))))
  9459. (prop-p
  9460. (setq pn (match-string 5 term)
  9461. po (match-string 6 term)
  9462. pv (match-string 7 term)
  9463. cat-p (equal pn "CATEGORY")
  9464. re-p (equal (string-to-char pv) ?{)
  9465. str-p (equal (string-to-char pv) ?\")
  9466. time-p (save-match-data
  9467. (string-match "^\"[[<].*[]>]\"$" pv))
  9468. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9469. (if time-p (setq pv (org-matcher-time pv)))
  9470. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9471. (cond
  9472. ((equal pn "CATEGORY")
  9473. (setq gv '(get-text-property (point) 'org-category)))
  9474. ((equal pn "TODO")
  9475. (setq gv 'todo))
  9476. (t
  9477. (setq gv `(org-cached-entry-get nil ,pn))))
  9478. (if re-p
  9479. (if (eq po 'org<>)
  9480. `(not (string-match ,pv (or ,gv "")))
  9481. `(string-match ,pv (or ,gv "")))
  9482. (if str-p
  9483. `(,po (or ,gv "") ,pv)
  9484. `(,po (string-to-number (or ,gv ""))
  9485. ,(string-to-number pv) ))))
  9486. (t `(member ,tag tags-list)))
  9487. mm (if minus (list 'not mm) mm)
  9488. term rest)
  9489. (push mm tagsmatcher))
  9490. (push (if (> (length tagsmatcher) 1)
  9491. (cons 'and tagsmatcher)
  9492. (car tagsmatcher))
  9493. orlist)
  9494. (setq tagsmatcher nil))
  9495. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9496. (setq tagsmatcher
  9497. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9498. ;; Make the todo matcher
  9499. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9500. (setq todomatcher t)
  9501. (setq orterms (org-split-string todomatch "|") orlist nil)
  9502. (while (setq term (pop orterms))
  9503. (while (string-match re term)
  9504. (setq minus (and (match-end 1)
  9505. (equal (match-string 1 term) "-"))
  9506. kwd (match-string 2 term)
  9507. re-p (equal (string-to-char kwd) ?{)
  9508. term (substring term (match-end 0))
  9509. mm (if re-p
  9510. `(string-match ,(substring kwd 1 -1) todo)
  9511. (list 'equal 'todo kwd))
  9512. mm (if minus (list 'not mm) mm))
  9513. (push mm todomatcher))
  9514. (push (if (> (length todomatcher) 1)
  9515. (cons 'and todomatcher)
  9516. (car todomatcher))
  9517. orlist)
  9518. (setq todomatcher nil))
  9519. (setq todomatcher (if (> (length orlist) 1)
  9520. (cons 'or orlist) (car orlist))))
  9521. ;; Return the string and lisp forms of the matcher
  9522. (setq matcher (if todomatcher
  9523. (list 'and tagsmatcher todomatcher)
  9524. tagsmatcher))
  9525. (cons match0 matcher)))
  9526. (defun org-op-to-function (op &optional stringp)
  9527. "Turn an operator into the appropriate function."
  9528. (setq op
  9529. (cond
  9530. ((equal op "<" ) '(< string< org-time<))
  9531. ((equal op ">" ) '(> org-string> org-time>))
  9532. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9533. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9534. ((member op '("=" "==")) '(= string= org-time=))
  9535. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9536. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9537. (defun org<> (a b) (not (= a b)))
  9538. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9539. (defun org-string>= (a b) (not (string< a b)))
  9540. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9541. (defun org-string<> (a b) (not (string= a b)))
  9542. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9543. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9544. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9545. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9546. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9547. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9548. (defun org-2ft (s)
  9549. "Convert S to a floating point time.
  9550. If S is already a number, just return it. If it is a string, parse
  9551. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9552. (cond
  9553. ((numberp s) s)
  9554. ((stringp s)
  9555. (condition-case nil
  9556. (float-time (apply 'encode-time (org-parse-time-string s)))
  9557. (error 0.)))
  9558. (t 0.)))
  9559. (defun org-time-today ()
  9560. "Time in seconds today at 0:00.
  9561. Returns the float number of seconds since the beginning of the
  9562. epoch to the beginning of today (00:00)."
  9563. (float-time (apply 'encode-time
  9564. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9565. (defun org-matcher-time (s)
  9566. "Interpret a time comparison value."
  9567. (save-match-data
  9568. (cond
  9569. ((string= s "<now>") (float-time))
  9570. ((string= s "<today>") (org-time-today))
  9571. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9572. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9573. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9574. (+ (org-time-today)
  9575. (* (string-to-number (match-string 1 s))
  9576. (cdr (assoc (match-string 2 s)
  9577. '(("d" . 86400.0) ("w" . 604800.0)
  9578. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9579. (t (org-2ft s)))))
  9580. (defun org-match-any-p (re list)
  9581. "Does re match any element of list?"
  9582. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9583. (delq nil list))
  9584. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9585. (defvar org-tags-overlay (org-make-overlay 1 1))
  9586. (org-detach-overlay org-tags-overlay)
  9587. (defun org-get-local-tags-at (&optional pos)
  9588. "Get a list of tags defined in the current headline."
  9589. (org-get-tags-at pos 'local))
  9590. (defun org-get-local-tags ()
  9591. "Get a list of tags defined in the current headline."
  9592. (org-get-tags-at nil 'local))
  9593. (defun org-get-tags-at (&optional pos local)
  9594. "Get a list of all headline tags applicable at POS.
  9595. POS defaults to point. If tags are inherited, the list contains
  9596. the targets in the same sequence as the headlines appear, i.e.
  9597. the tags of the current headline come last.
  9598. When LOCAL is non-nil, only return tags from the current headline,
  9599. ignore inherited ones."
  9600. (interactive)
  9601. (if (and org-trust-scanner-tags
  9602. (or (not pos) (equal pos (point)))
  9603. (not local))
  9604. org-scanner-tags
  9605. (let (tags ltags lastpos parent)
  9606. (save-excursion
  9607. (save-restriction
  9608. (widen)
  9609. (goto-char (or pos (point)))
  9610. (save-match-data
  9611. (catch 'done
  9612. (condition-case nil
  9613. (progn
  9614. (org-back-to-heading t)
  9615. (while (not (equal lastpos (point)))
  9616. (setq lastpos (point))
  9617. (when (looking-at
  9618. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9619. (setq ltags (org-split-string
  9620. (org-match-string-no-properties 1) ":"))
  9621. (when parent
  9622. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9623. (setq tags (append
  9624. (if parent
  9625. (org-remove-uniherited-tags ltags)
  9626. ltags)
  9627. tags)))
  9628. (or org-use-tag-inheritance (throw 'done t))
  9629. (if local (throw 'done t))
  9630. (org-up-heading-all 1)
  9631. (setq parent t)))
  9632. (error nil)))))
  9633. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9634. (defun org-add-prop-inherited (s)
  9635. (add-text-properties 0 (length s) '(inherited t) s)
  9636. s)
  9637. (defun org-toggle-tag (tag &optional onoff)
  9638. "Toggle the tag TAG for the current line.
  9639. If ONOFF is `on' or `off', don't toggle but set to this state."
  9640. (let (res current)
  9641. (save-excursion
  9642. (org-back-to-heading t)
  9643. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9644. (point-at-eol) t)
  9645. (progn
  9646. (setq current (match-string 1))
  9647. (replace-match ""))
  9648. (setq current ""))
  9649. (setq current (nreverse (org-split-string current ":")))
  9650. (cond
  9651. ((eq onoff 'on)
  9652. (setq res t)
  9653. (or (member tag current) (push tag current)))
  9654. ((eq onoff 'off)
  9655. (or (not (member tag current)) (setq current (delete tag current))))
  9656. (t (if (member tag current)
  9657. (setq current (delete tag current))
  9658. (setq res t)
  9659. (push tag current))))
  9660. (end-of-line 1)
  9661. (if current
  9662. (progn
  9663. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9664. (org-set-tags nil t))
  9665. (delete-horizontal-space))
  9666. (run-hooks 'org-after-tags-change-hook))
  9667. res))
  9668. (defun org-align-tags-here (to-col)
  9669. ;; Assumes that this is a headline
  9670. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9671. (beginning-of-line 1)
  9672. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9673. (< pos (match-beginning 2)))
  9674. (progn
  9675. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9676. (goto-char (match-beginning 1))
  9677. (insert " ")
  9678. (delete-region (point) (1+ (match-beginning 2)))
  9679. (setq ncol (max (1+ (current-column))
  9680. (1+ col)
  9681. (if (> to-col 0)
  9682. to-col
  9683. (- (abs to-col) tags-l))))
  9684. (setq p (point))
  9685. (insert (make-string (- ncol (current-column)) ?\ ))
  9686. (setq ncol (current-column))
  9687. (when indent-tabs-mode (tabify p (point-at-eol)))
  9688. (org-move-to-column (min ncol col) t))
  9689. (goto-char pos))))
  9690. (defun org-set-tags-command (&optional arg just-align)
  9691. "Call the set-tags command for the current entry."
  9692. (interactive "P")
  9693. (if (org-on-heading-p)
  9694. (org-set-tags arg just-align)
  9695. (save-excursion
  9696. (org-back-to-heading t)
  9697. (org-set-tags arg just-align))))
  9698. (defun org-set-tags (&optional arg just-align)
  9699. "Set the tags for the current headline.
  9700. With prefix ARG, realign all tags in headings in the current buffer."
  9701. (interactive "P")
  9702. (let* ((re (concat "^" outline-regexp))
  9703. (current (org-get-tags-string))
  9704. (col (current-column))
  9705. (org-setting-tags t)
  9706. table current-tags inherited-tags ; computed below when needed
  9707. tags p0 c0 c1 rpl)
  9708. (if arg
  9709. (save-excursion
  9710. (goto-char (point-min))
  9711. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9712. (while (re-search-forward re nil t)
  9713. (org-set-tags nil t)
  9714. (end-of-line 1)))
  9715. (message "All tags realigned to column %d" org-tags-column))
  9716. (if just-align
  9717. (setq tags current)
  9718. ;; Get a new set of tags from the user
  9719. (save-excursion
  9720. (setq table (append org-tag-persistent-alist
  9721. (or org-tag-alist (org-get-buffer-tags)))
  9722. org-last-tags-completion-table table
  9723. current-tags (org-split-string current ":")
  9724. inherited-tags (nreverse
  9725. (nthcdr (length current-tags)
  9726. (nreverse (org-get-tags-at))))
  9727. tags
  9728. (if (or (eq t org-use-fast-tag-selection)
  9729. (and org-use-fast-tag-selection
  9730. (delq nil (mapcar 'cdr table))))
  9731. (org-fast-tag-selection
  9732. current-tags inherited-tags table
  9733. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9734. (let ((org-add-colon-after-tag-completion t))
  9735. (org-trim
  9736. (org-without-partial-completion
  9737. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9738. nil nil current 'org-tags-history)))))))
  9739. (while (string-match "[-+&]+" tags)
  9740. ;; No boolean logic, just a list
  9741. (setq tags (replace-match ":" t t tags))))
  9742. (if (string-match "\\`[\t ]*\\'" tags)
  9743. (setq tags "")
  9744. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9745. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9746. ;; Insert new tags at the correct column
  9747. (beginning-of-line 1)
  9748. (cond
  9749. ((and (equal current "") (equal tags "")))
  9750. ((re-search-forward
  9751. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9752. (point-at-eol) t)
  9753. (if (equal tags "")
  9754. (setq rpl "")
  9755. (goto-char (match-beginning 0))
  9756. (setq c0 (current-column) p0 (point)
  9757. c1 (max (1+ c0) (if (> org-tags-column 0)
  9758. org-tags-column
  9759. (- (- org-tags-column) (length tags))))
  9760. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9761. (replace-match rpl t t)
  9762. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9763. tags)
  9764. (t (error "Tags alignment failed")))
  9765. (org-move-to-column col)
  9766. (unless just-align
  9767. (run-hooks 'org-after-tags-change-hook)))))
  9768. (defun org-change-tag-in-region (beg end tag off)
  9769. "Add or remove TAG for each entry in the region.
  9770. This works in the agenda, and also in an org-mode buffer."
  9771. (interactive
  9772. (list (region-beginning) (region-end)
  9773. (let ((org-last-tags-completion-table
  9774. (if (org-mode-p)
  9775. (org-get-buffer-tags)
  9776. (org-global-tags-completion-table))))
  9777. (org-ido-completing-read
  9778. "Tag: " 'org-tags-completion-function nil nil nil
  9779. 'org-tags-history))
  9780. (progn
  9781. (message "[s]et or [r]emove? ")
  9782. (equal (read-char-exclusive) ?r))))
  9783. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9784. (let ((agendap (equal major-mode 'org-agenda-mode))
  9785. l1 l2 m buf pos newhead (cnt 0))
  9786. (goto-char end)
  9787. (setq l2 (1- (org-current-line)))
  9788. (goto-char beg)
  9789. (setq l1 (org-current-line))
  9790. (loop for l from l1 to l2 do
  9791. (goto-line l)
  9792. (setq m (get-text-property (point) 'org-hd-marker))
  9793. (when (or (and (org-mode-p) (org-on-heading-p))
  9794. (and agendap m))
  9795. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9796. pos (if agendap m (point)))
  9797. (with-current-buffer buf
  9798. (save-excursion
  9799. (save-restriction
  9800. (goto-char pos)
  9801. (setq cnt (1+ cnt))
  9802. (org-toggle-tag tag (if off 'off 'on))
  9803. (setq newhead (org-get-heading)))))
  9804. (and agendap (org-agenda-change-all-lines newhead m))))
  9805. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9806. (defun org-tags-completion-function (string predicate &optional flag)
  9807. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9808. (confirm (lambda (x) (stringp (car x)))))
  9809. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9810. (setq s1 (match-string 1 string)
  9811. s2 (match-string 2 string))
  9812. (setq s1 "" s2 string))
  9813. (cond
  9814. ((eq flag nil)
  9815. ;; try completion
  9816. (setq rtn (try-completion s2 ctable confirm))
  9817. (if (stringp rtn)
  9818. (setq rtn
  9819. (concat s1 s2 (substring rtn (length s2))
  9820. (if (and org-add-colon-after-tag-completion
  9821. (assoc rtn ctable))
  9822. ":" ""))))
  9823. rtn)
  9824. ((eq flag t)
  9825. ;; all-completions
  9826. (all-completions s2 ctable confirm)
  9827. )
  9828. ((eq flag 'lambda)
  9829. ;; exact match?
  9830. (assoc s2 ctable)))
  9831. ))
  9832. (defun org-fast-tag-insert (kwd tags face &optional end)
  9833. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9834. (insert (format "%-12s" (concat kwd ":"))
  9835. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9836. (or end "")))
  9837. (defun org-fast-tag-show-exit (flag)
  9838. (save-excursion
  9839. (goto-line 3)
  9840. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9841. (replace-match ""))
  9842. (when flag
  9843. (end-of-line 1)
  9844. (org-move-to-column (- (window-width) 19) t)
  9845. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9846. (defun org-set-current-tags-overlay (current prefix)
  9847. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9848. (if (featurep 'xemacs)
  9849. (org-overlay-display org-tags-overlay (concat prefix s)
  9850. 'secondary-selection)
  9851. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9852. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9853. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9854. "Fast tag selection with single keys.
  9855. CURRENT is the current list of tags in the headline, INHERITED is the
  9856. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9857. possibly with grouping information. TODO-TABLE is a similar table with
  9858. TODO keywords, should these have keys assigned to them.
  9859. If the keys are nil, a-z are automatically assigned.
  9860. Returns the new tags string, or nil to not change the current settings."
  9861. (let* ((fulltable (append table todo-table))
  9862. (maxlen (apply 'max (mapcar
  9863. (lambda (x)
  9864. (if (stringp (car x)) (string-width (car x)) 0))
  9865. fulltable)))
  9866. (buf (current-buffer))
  9867. (expert (eq org-fast-tag-selection-single-key 'expert))
  9868. (buffer-tags nil)
  9869. (fwidth (+ maxlen 3 1 3))
  9870. (ncol (/ (- (window-width) 4) fwidth))
  9871. (i-face 'org-done)
  9872. (c-face 'org-todo)
  9873. tg cnt e c char c1 c2 ntable tbl rtn
  9874. ov-start ov-end ov-prefix
  9875. (exit-after-next org-fast-tag-selection-single-key)
  9876. (done-keywords org-done-keywords)
  9877. groups ingroup)
  9878. (save-excursion
  9879. (beginning-of-line 1)
  9880. (if (looking-at
  9881. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9882. (setq ov-start (match-beginning 1)
  9883. ov-end (match-end 1)
  9884. ov-prefix "")
  9885. (setq ov-start (1- (point-at-eol))
  9886. ov-end (1+ ov-start))
  9887. (skip-chars-forward "^\n\r")
  9888. (setq ov-prefix
  9889. (concat
  9890. (buffer-substring (1- (point)) (point))
  9891. (if (> (current-column) org-tags-column)
  9892. " "
  9893. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9894. (org-move-overlay org-tags-overlay ov-start ov-end)
  9895. (save-window-excursion
  9896. (if expert
  9897. (set-buffer (get-buffer-create " *Org tags*"))
  9898. (delete-other-windows)
  9899. (split-window-vertically)
  9900. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9901. (erase-buffer)
  9902. (org-set-local 'org-done-keywords done-keywords)
  9903. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9904. (org-fast-tag-insert "Current" current c-face "\n\n")
  9905. (org-fast-tag-show-exit exit-after-next)
  9906. (org-set-current-tags-overlay current ov-prefix)
  9907. (setq tbl fulltable char ?a cnt 0)
  9908. (while (setq e (pop tbl))
  9909. (cond
  9910. ((equal e '(:startgroup))
  9911. (push '() groups) (setq ingroup t)
  9912. (when (not (= cnt 0))
  9913. (setq cnt 0)
  9914. (insert "\n"))
  9915. (insert "{ "))
  9916. ((equal e '(:endgroup))
  9917. (setq ingroup nil cnt 0)
  9918. (insert "}\n"))
  9919. ((equal e '(:newline))
  9920. (when (not (= cnt 0))
  9921. (setq cnt 0)
  9922. (insert "\n")
  9923. (setq e (car tbl))
  9924. (while (equal (car tbl) '(:newline))
  9925. (insert "\n")
  9926. (setq tbl (cdr tbl)))))
  9927. (t
  9928. (setq tg (car e) c2 nil)
  9929. (if (cdr e)
  9930. (setq c (cdr e))
  9931. ;; automatically assign a character.
  9932. (setq c1 (string-to-char
  9933. (downcase (substring
  9934. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9935. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9936. (while (or (rassoc char ntable) (rassoc char table))
  9937. (setq char (1+ char)))
  9938. (setq c2 c1))
  9939. (setq c (or c2 char)))
  9940. (if ingroup (push tg (car groups)))
  9941. (setq tg (org-add-props tg nil 'face
  9942. (cond
  9943. ((not (assoc tg table))
  9944. (org-get-todo-face tg))
  9945. ((member tg current) c-face)
  9946. ((member tg inherited) i-face)
  9947. (t nil))))
  9948. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9949. (insert "[" c "] " tg (make-string
  9950. (- fwidth 4 (length tg)) ?\ ))
  9951. (push (cons tg c) ntable)
  9952. (when (= (setq cnt (1+ cnt)) ncol)
  9953. (insert "\n")
  9954. (if ingroup (insert " "))
  9955. (setq cnt 0)))))
  9956. (setq ntable (nreverse ntable))
  9957. (insert "\n")
  9958. (goto-char (point-min))
  9959. (if (not expert) (org-fit-window-to-buffer))
  9960. (setq rtn
  9961. (catch 'exit
  9962. (while t
  9963. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9964. (if groups " [!] no groups" " [!]groups")
  9965. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9966. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9967. (cond
  9968. ((= c ?\r) (throw 'exit t))
  9969. ((= c ?!)
  9970. (setq groups (not groups))
  9971. (goto-char (point-min))
  9972. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9973. ((= c ?\C-c)
  9974. (if (not expert)
  9975. (org-fast-tag-show-exit
  9976. (setq exit-after-next (not exit-after-next)))
  9977. (setq expert nil)
  9978. (delete-other-windows)
  9979. (split-window-vertically)
  9980. (org-switch-to-buffer-other-window " *Org tags*")
  9981. (org-fit-window-to-buffer)))
  9982. ((or (= c ?\C-g)
  9983. (and (= c ?q) (not (rassoc c ntable))))
  9984. (org-detach-overlay org-tags-overlay)
  9985. (setq quit-flag t))
  9986. ((= c ?\ )
  9987. (setq current nil)
  9988. (if exit-after-next (setq exit-after-next 'now)))
  9989. ((= c ?\t)
  9990. (condition-case nil
  9991. (setq tg (org-ido-completing-read
  9992. "Tag: "
  9993. (or buffer-tags
  9994. (with-current-buffer buf
  9995. (org-get-buffer-tags)))))
  9996. (quit (setq tg "")))
  9997. (when (string-match "\\S-" tg)
  9998. (add-to-list 'buffer-tags (list tg))
  9999. (if (member tg current)
  10000. (setq current (delete tg current))
  10001. (push tg current)))
  10002. (if exit-after-next (setq exit-after-next 'now)))
  10003. ((setq e (rassoc c todo-table) tg (car e))
  10004. (with-current-buffer buf
  10005. (save-excursion (org-todo tg)))
  10006. (if exit-after-next (setq exit-after-next 'now)))
  10007. ((setq e (rassoc c ntable) tg (car e))
  10008. (if (member tg current)
  10009. (setq current (delete tg current))
  10010. (loop for g in groups do
  10011. (if (member tg g)
  10012. (mapc (lambda (x)
  10013. (setq current (delete x current)))
  10014. g)))
  10015. (push tg current))
  10016. (if exit-after-next (setq exit-after-next 'now))))
  10017. ;; Create a sorted list
  10018. (setq current
  10019. (sort current
  10020. (lambda (a b)
  10021. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10022. (if (eq exit-after-next 'now) (throw 'exit t))
  10023. (goto-char (point-min))
  10024. (beginning-of-line 2)
  10025. (delete-region (point) (point-at-eol))
  10026. (org-fast-tag-insert "Current" current c-face)
  10027. (org-set-current-tags-overlay current ov-prefix)
  10028. (while (re-search-forward
  10029. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10030. (setq tg (match-string 1))
  10031. (add-text-properties
  10032. (match-beginning 1) (match-end 1)
  10033. (list 'face
  10034. (cond
  10035. ((member tg current) c-face)
  10036. ((member tg inherited) i-face)
  10037. (t (get-text-property (match-beginning 1) 'face))))))
  10038. (goto-char (point-min)))))
  10039. (org-detach-overlay org-tags-overlay)
  10040. (if rtn
  10041. (mapconcat 'identity current ":")
  10042. nil))))
  10043. (defun org-get-tags-string ()
  10044. "Get the TAGS string in the current headline."
  10045. (unless (org-on-heading-p t)
  10046. (error "Not on a heading"))
  10047. (save-excursion
  10048. (beginning-of-line 1)
  10049. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10050. (org-match-string-no-properties 1)
  10051. "")))
  10052. (defun org-get-tags ()
  10053. "Get the list of tags specified in the current headline."
  10054. (org-split-string (org-get-tags-string) ":"))
  10055. (defun org-get-buffer-tags ()
  10056. "Get a table of all tags used in the buffer, for completion."
  10057. (let (tags)
  10058. (save-excursion
  10059. (goto-char (point-min))
  10060. (while (re-search-forward
  10061. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10062. (when (equal (char-after (point-at-bol 0)) ?*)
  10063. (mapc (lambda (x) (add-to-list 'tags x))
  10064. (org-split-string (org-match-string-no-properties 1) ":")))))
  10065. (mapcar 'list tags)))
  10066. ;;;; The mapping API
  10067. ;;;###autoload
  10068. (defun org-map-entries (func &optional match scope &rest skip)
  10069. "Call FUNC at each headline selected by MATCH in SCOPE.
  10070. FUNC is a function or a lisp form. The function will be called without
  10071. arguments, with the cursor positioned at the beginning of the headline.
  10072. The return values of all calls to the function will be collected and
  10073. returned as a list.
  10074. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10075. does not need to preserve point. After evaluaton, the cursor will be
  10076. moved to the end of the line (presumably of the headline of the
  10077. processed entry) and search continues from there. Under some
  10078. circumstances, this may not produce the wanted results. For example,
  10079. if you have removed (e.g. archived) the current (sub)tree it could
  10080. mean that the next entry will be skipped entirely. In such cases, you
  10081. can specify the position from where search should continue by making
  10082. FUNC set the variable `org-map-continue-from' to the desired buffer
  10083. position.
  10084. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10085. Only headlines that are matched by this query will be considered during
  10086. the iteration. When MATCH is nil or t, all headlines will be
  10087. visited by the iteration.
  10088. SCOPE determines the scope of this command. It can be any of:
  10089. nil The current buffer, respecting the restriction if any
  10090. tree The subtree started with the entry at point
  10091. file The current buffer, without restriction
  10092. file-with-archives
  10093. The current buffer, and any archives associated with it
  10094. agenda All agenda files
  10095. agenda-with-archives
  10096. All agenda files with any archive files associated with them
  10097. \(file1 file2 ...)
  10098. If this is a list, all files in the list will be scanned
  10099. The remaining args are treated as settings for the skipping facilities of
  10100. the scanner. The following items can be given here:
  10101. archive skip trees with the archive tag.
  10102. comment skip trees with the COMMENT keyword
  10103. function or Emacs Lisp form:
  10104. will be used as value for `org-agenda-skip-function', so whenever
  10105. the the function returns t, FUNC will not be called for that
  10106. entry and search will continue from the point where the
  10107. function leaves it.
  10108. If your function needs to retrieve the tags including inherited tags
  10109. at the *current* entry, you can use the value of the variable
  10110. `org-scanner-tags' which will be much faster than getting the value
  10111. with `org-get-tags-at'. If your function gets properties with
  10112. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10113. to t around the call to `org-entry-properties' to get the same speedup.
  10114. Note that if your function moves around to retrieve tags and properties at
  10115. a *different* entry, you cannot use these techniques."
  10116. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10117. (org-agenda-skip-archived-trees (memq 'archive skip))
  10118. (org-agenda-skip-comment-trees (memq 'comment skip))
  10119. (org-agenda-skip-function
  10120. (car (org-delete-all '(comment archive) skip)))
  10121. (org-tags-match-list-sublevels t)
  10122. matcher file res
  10123. org-todo-keywords-for-agenda
  10124. org-done-keywords-for-agenda
  10125. org-todo-keyword-alist-for-agenda
  10126. org-tag-alist-for-agenda)
  10127. (cond
  10128. ((eq match t) (setq matcher t))
  10129. ((eq match nil) (setq matcher t))
  10130. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10131. (save-excursion
  10132. (save-restriction
  10133. (when (eq scope 'tree)
  10134. (org-back-to-heading t)
  10135. (org-narrow-to-subtree)
  10136. (setq scope nil))
  10137. (if (not scope)
  10138. (progn
  10139. (org-prepare-agenda-buffers
  10140. (list (buffer-file-name (current-buffer))))
  10141. (setq res (org-scan-tags func matcher)))
  10142. ;; Get the right scope
  10143. (cond
  10144. ((and scope (listp scope) (symbolp (car scope)))
  10145. (setq scope (eval scope)))
  10146. ((eq scope 'agenda)
  10147. (setq scope (org-agenda-files t)))
  10148. ((eq scope 'agenda-with-archives)
  10149. (setq scope (org-agenda-files t))
  10150. (setq scope (org-add-archive-files scope)))
  10151. ((eq scope 'file)
  10152. (setq scope (list (buffer-file-name))))
  10153. ((eq scope 'file-with-archives)
  10154. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10155. (org-prepare-agenda-buffers scope)
  10156. (while (setq file (pop scope))
  10157. (with-current-buffer (org-find-base-buffer-visiting file)
  10158. (save-excursion
  10159. (save-restriction
  10160. (widen)
  10161. (goto-char (point-min))
  10162. (setq res (append res (org-scan-tags func matcher))))))))))
  10163. res))
  10164. ;;;; Properties
  10165. ;;; Setting and retrieving properties
  10166. (defconst org-special-properties
  10167. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10168. "TIMESTAMP" "TIMESTAMP_IA")
  10169. "The special properties valid in Org-mode.
  10170. These are properties that are not defined in the property drawer,
  10171. but in some other way.")
  10172. (defconst org-default-properties
  10173. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10174. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10175. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10176. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10177. "ORDERED")
  10178. "Some properties that are used by Org-mode for various purposes.
  10179. Being in this list makes sure that they are offered for completion.")
  10180. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10181. "Regular expression matching the first line of a property drawer.")
  10182. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10183. "Regular expression matching the first line of a property drawer.")
  10184. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10185. "Regular expression matching the first line of a property drawer.")
  10186. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10187. "Regular expression matching the first line of a property drawer.")
  10188. (defconst org-property-drawer-re
  10189. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10190. org-property-end-re "\\)\n?")
  10191. "Matches an entire property drawer.")
  10192. (defconst org-clock-drawer-re
  10193. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10194. org-property-end-re "\\)\n?")
  10195. "Matches an entire clock drawer.")
  10196. (defun org-property-action ()
  10197. "Do an action on properties."
  10198. (interactive)
  10199. (let (c)
  10200. (org-at-property-p)
  10201. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10202. (setq c (read-char-exclusive))
  10203. (cond
  10204. ((equal c ?s)
  10205. (call-interactively 'org-set-property))
  10206. ((equal c ?d)
  10207. (call-interactively 'org-delete-property))
  10208. ((equal c ?D)
  10209. (call-interactively 'org-delete-property-globally))
  10210. ((equal c ?c)
  10211. (call-interactively 'org-compute-property-at-point))
  10212. (t (error "No such property action %c" c)))))
  10213. (defun org-at-property-p ()
  10214. "Is the cursor in a property line?"
  10215. ;; FIXME: Does not check if we are actually in the drawer.
  10216. ;; FIXME: also returns true on any drawers.....
  10217. ;; This is used by C-c C-c for property action.
  10218. (save-excursion
  10219. (beginning-of-line 1)
  10220. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10221. (defun org-get-property-block (&optional beg end force)
  10222. "Return the (beg . end) range of the body of the property drawer.
  10223. BEG and END can be beginning and end of subtree, if not given
  10224. they will be found.
  10225. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10226. (catch 'exit
  10227. (save-excursion
  10228. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10229. (end (or end (progn (outline-next-heading) (point)))))
  10230. (goto-char beg)
  10231. (if (re-search-forward org-property-start-re end t)
  10232. (setq beg (1+ (match-end 0)))
  10233. (if force
  10234. (save-excursion
  10235. (org-insert-property-drawer)
  10236. (setq end (progn (outline-next-heading) (point))))
  10237. (throw 'exit nil))
  10238. (goto-char beg)
  10239. (if (re-search-forward org-property-start-re end t)
  10240. (setq beg (1+ (match-end 0)))))
  10241. (if (re-search-forward org-property-end-re end t)
  10242. (setq end (match-beginning 0))
  10243. (or force (throw 'exit nil))
  10244. (goto-char beg)
  10245. (setq end beg)
  10246. (org-indent-line-function)
  10247. (insert ":END:\n"))
  10248. (cons beg end)))))
  10249. (defun org-entry-properties (&optional pom which)
  10250. "Get all properties of the entry at point-or-marker POM.
  10251. This includes the TODO keyword, the tags, time strings for deadline,
  10252. scheduled, and clocking, and any additional properties defined in the
  10253. entry. The return value is an alist, keys may occur multiple times
  10254. if the property key was used several times.
  10255. POM may also be nil, in which case the current entry is used.
  10256. If WHICH is nil or `all', get all properties. If WHICH is
  10257. `special' or `standard', only get that subclass."
  10258. (setq which (or which 'all))
  10259. (org-with-point-at pom
  10260. (let ((clockstr (substring org-clock-string 0 -1))
  10261. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10262. beg end range props sum-props key value string clocksum)
  10263. (save-excursion
  10264. (when (condition-case nil
  10265. (and (org-mode-p) (org-back-to-heading t))
  10266. (error nil))
  10267. (setq beg (point))
  10268. (setq sum-props (get-text-property (point) 'org-summaries))
  10269. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10270. (outline-next-heading)
  10271. (setq end (point))
  10272. (when (memq which '(all special))
  10273. ;; Get the special properties, like TODO and tags
  10274. (goto-char beg)
  10275. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10276. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10277. (when (looking-at org-priority-regexp)
  10278. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10279. (when (and (setq value (org-get-tags-string))
  10280. (string-match "\\S-" value))
  10281. (push (cons "TAGS" value) props))
  10282. (when (setq value (org-get-tags-at))
  10283. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10284. props))
  10285. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10286. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10287. string (if (equal key clockstr)
  10288. (org-no-properties
  10289. (org-trim
  10290. (buffer-substring
  10291. (match-beginning 3) (goto-char (point-at-eol)))))
  10292. (substring (org-match-string-no-properties 3) 1 -1)))
  10293. (unless key
  10294. (if (= (char-after (match-beginning 3)) ?\[)
  10295. (setq key "TIMESTAMP_IA")
  10296. (setq key "TIMESTAMP")))
  10297. (when (or (equal key clockstr) (not (assoc key props)))
  10298. (push (cons key string) props)))
  10299. )
  10300. (when (memq which '(all standard))
  10301. ;; Get the standard properties, like :PROP: ...
  10302. (setq range (org-get-property-block beg end))
  10303. (when range
  10304. (goto-char (car range))
  10305. (while (re-search-forward
  10306. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10307. (cdr range) t)
  10308. (setq key (org-match-string-no-properties 1)
  10309. value (org-trim (or (org-match-string-no-properties 2) "")))
  10310. (unless (member key excluded)
  10311. (push (cons key (or value "")) props)))))
  10312. (if clocksum
  10313. (push (cons "CLOCKSUM"
  10314. (org-columns-number-to-string (/ (float clocksum) 60.)
  10315. 'add_times))
  10316. props))
  10317. (unless (assoc "CATEGORY" props)
  10318. (setq value (or (org-get-category)
  10319. (progn (org-refresh-category-properties)
  10320. (org-get-category))))
  10321. (push (cons "CATEGORY" value) props))
  10322. (append sum-props (nreverse props)))))))
  10323. (defun org-entry-get (pom property &optional inherit)
  10324. "Get value of PROPERTY for entry at point-or-marker POM.
  10325. If INHERIT is non-nil and the entry does not have the property,
  10326. then also check higher levels of the hierarchy.
  10327. If INHERIT is the symbol `selective', use inheritance only if the setting
  10328. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10329. If the property is present but empty, the return value is the empty string.
  10330. If the property is not present at all, nil is returned."
  10331. (org-with-point-at pom
  10332. (if (and inherit (if (eq inherit 'selective)
  10333. (org-property-inherit-p property)
  10334. t))
  10335. (org-entry-get-with-inheritance property)
  10336. (if (member property org-special-properties)
  10337. ;; We need a special property. Use brute force, get all properties.
  10338. (cdr (assoc property (org-entry-properties nil 'special)))
  10339. (let ((range (org-get-property-block)))
  10340. (if (and range
  10341. (goto-char (car range))
  10342. (re-search-forward
  10343. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10344. (cdr range) t))
  10345. ;; Found the property, return it.
  10346. (if (match-end 1)
  10347. (org-match-string-no-properties 1)
  10348. "")))))))
  10349. (defun org-property-or-variable-value (var &optional inherit)
  10350. "Check if there is a property fixing the value of VAR.
  10351. If yes, return this value. If not, return the current value of the variable."
  10352. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10353. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10354. (read prop)
  10355. (symbol-value var))))
  10356. (defun org-entry-delete (pom property)
  10357. "Delete the property PROPERTY from entry at point-or-marker POM."
  10358. (org-with-point-at pom
  10359. (if (member property org-special-properties)
  10360. nil ; cannot delete these properties.
  10361. (let ((range (org-get-property-block)))
  10362. (if (and range
  10363. (goto-char (car range))
  10364. (re-search-forward
  10365. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10366. (cdr range) t))
  10367. (progn
  10368. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10369. t)
  10370. nil)))))
  10371. ;; Multi-values properties are properties that contain multiple values
  10372. ;; These values are assumed to be single words, separated by whitespace.
  10373. (defun org-entry-add-to-multivalued-property (pom property value)
  10374. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10375. (let* ((old (org-entry-get pom property))
  10376. (values (and old (org-split-string old "[ \t]"))))
  10377. (setq value (org-entry-protect-space value))
  10378. (unless (member value values)
  10379. (setq values (cons value values))
  10380. (org-entry-put pom property
  10381. (mapconcat 'identity values " ")))))
  10382. (defun org-entry-remove-from-multivalued-property (pom property value)
  10383. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10384. (let* ((old (org-entry-get pom property))
  10385. (values (and old (org-split-string old "[ \t]"))))
  10386. (setq value (org-entry-protect-space value))
  10387. (when (member value values)
  10388. (setq values (delete value values))
  10389. (org-entry-put pom property
  10390. (mapconcat 'identity values " ")))))
  10391. (defun org-entry-member-in-multivalued-property (pom property value)
  10392. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10393. (let* ((old (org-entry-get pom property))
  10394. (values (and old (org-split-string old "[ \t]"))))
  10395. (setq value (org-entry-protect-space value))
  10396. (member value values)))
  10397. (defun org-entry-get-multivalued-property (pom property)
  10398. "Return a list of values in a multivalued property."
  10399. (let* ((value (org-entry-get pom property))
  10400. (values (and value (org-split-string value "[ \t]"))))
  10401. (mapcar 'org-entry-restore-space values)))
  10402. (defun org-entry-put-multivalued-property (pom property &rest values)
  10403. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10404. VALUES should be a list of strings. Spaces will be protected."
  10405. (org-entry-put pom property
  10406. (mapconcat 'org-entry-protect-space values " "))
  10407. (let* ((value (org-entry-get pom property))
  10408. (values (and value (org-split-string value "[ \t]"))))
  10409. (mapcar 'org-entry-restore-space values)))
  10410. (defun org-entry-protect-space (s)
  10411. "Protect spaces and newline in string S."
  10412. (while (string-match " " s)
  10413. (setq s (replace-match "%20" t t s)))
  10414. (while (string-match "\n" s)
  10415. (setq s (replace-match "%0A" t t s)))
  10416. s)
  10417. (defun org-entry-restore-space (s)
  10418. "Restore spaces and newline in string S."
  10419. (while (string-match "%20" s)
  10420. (setq s (replace-match " " t t s)))
  10421. (while (string-match "%0A" s)
  10422. (setq s (replace-match "\n" t t s)))
  10423. s)
  10424. (defvar org-entry-property-inherited-from (make-marker)
  10425. "Marker pointing to the entry from where a property was inherited.
  10426. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10427. location of the entry where the inheritance search matched. If there was
  10428. no match, the marker will point nowhere.
  10429. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10430. is set.")
  10431. (defun org-entry-get-with-inheritance (property)
  10432. "Get entry property, and search higher levels if not present."
  10433. (move-marker org-entry-property-inherited-from nil)
  10434. (let (tmp)
  10435. (save-excursion
  10436. (save-restriction
  10437. (widen)
  10438. (catch 'ex
  10439. (while t
  10440. (when (setq tmp (org-entry-get nil property))
  10441. (org-back-to-heading t)
  10442. (move-marker org-entry-property-inherited-from (point))
  10443. (throw 'ex tmp))
  10444. (or (org-up-heading-safe) (throw 'ex nil)))))
  10445. (or tmp
  10446. (cdr (assoc property org-file-properties))
  10447. (cdr (assoc property org-global-properties))
  10448. (cdr (assoc property org-global-properties-fixed))))))
  10449. (defun org-entry-put (pom property value)
  10450. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10451. (org-with-point-at pom
  10452. (org-back-to-heading t)
  10453. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10454. range)
  10455. (cond
  10456. ((equal property "TODO")
  10457. (when (and (stringp value) (string-match "\\S-" value)
  10458. (not (member value org-todo-keywords-1)))
  10459. (error "\"%s\" is not a valid TODO state" value))
  10460. (if (or (not value)
  10461. (not (string-match "\\S-" value)))
  10462. (setq value 'none))
  10463. (org-todo value)
  10464. (org-set-tags nil 'align))
  10465. ((equal property "PRIORITY")
  10466. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10467. (string-to-char value) ?\ ))
  10468. (org-set-tags nil 'align))
  10469. ((equal property "SCHEDULED")
  10470. (if (re-search-forward org-scheduled-time-regexp end t)
  10471. (cond
  10472. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10473. ((eq value 'later) (org-timestamp-change 1 'day))
  10474. (t (call-interactively 'org-schedule)))
  10475. (call-interactively 'org-schedule)))
  10476. ((equal property "DEADLINE")
  10477. (if (re-search-forward org-deadline-time-regexp end t)
  10478. (cond
  10479. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10480. ((eq value 'later) (org-timestamp-change 1 'day))
  10481. (t (call-interactively 'org-deadline)))
  10482. (call-interactively 'org-deadline)))
  10483. ((member property org-special-properties)
  10484. (error "The %s property can not yet be set with `org-entry-put'"
  10485. property))
  10486. (t ; a non-special property
  10487. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10488. (setq range (org-get-property-block beg end 'force))
  10489. (goto-char (car range))
  10490. (if (re-search-forward
  10491. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10492. (progn
  10493. (delete-region (match-beginning 1) (match-end 1))
  10494. (goto-char (match-beginning 1)))
  10495. (goto-char (cdr range))
  10496. (insert "\n")
  10497. (backward-char 1)
  10498. (org-indent-line-function)
  10499. (insert ":" property ":"))
  10500. (and value (insert " " value))
  10501. (org-indent-line-function)))))))
  10502. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10503. "Get all property keys in the current buffer.
  10504. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10505. like tags and TODO state.
  10506. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10507. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10508. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10509. formats in the current buffer."
  10510. (let (rtn range cfmt s p)
  10511. (save-excursion
  10512. (save-restriction
  10513. (widen)
  10514. (goto-char (point-min))
  10515. (while (re-search-forward org-property-start-re nil t)
  10516. (setq range (org-get-property-block))
  10517. (goto-char (car range))
  10518. (while (re-search-forward
  10519. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10520. (cdr range) t)
  10521. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10522. (outline-next-heading))))
  10523. (when include-specials
  10524. (setq rtn (append org-special-properties rtn)))
  10525. (when include-defaults
  10526. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10527. (when include-columns
  10528. (save-excursion
  10529. (save-restriction
  10530. (widen)
  10531. (goto-char (point-min))
  10532. (while (re-search-forward
  10533. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10534. nil t)
  10535. (setq cfmt (match-string 2) s 0)
  10536. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10537. cfmt s)
  10538. (setq s (match-end 0)
  10539. p (match-string 1 cfmt))
  10540. (unless (or (equal p "ITEM")
  10541. (member p org-special-properties))
  10542. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10543. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10544. (defun org-property-values (key)
  10545. "Return a list of all values of property KEY."
  10546. (save-excursion
  10547. (save-restriction
  10548. (widen)
  10549. (goto-char (point-min))
  10550. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10551. values)
  10552. (while (re-search-forward re nil t)
  10553. (add-to-list 'values (org-trim (match-string 1))))
  10554. (delete "" values)))))
  10555. (defun org-insert-property-drawer ()
  10556. "Insert a property drawer into the current entry."
  10557. (interactive)
  10558. (org-back-to-heading t)
  10559. (looking-at outline-regexp)
  10560. (let ((indent (- (match-end 0)(match-beginning 0)))
  10561. (beg (point))
  10562. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10563. end hiddenp)
  10564. (outline-next-heading)
  10565. (setq end (point))
  10566. (goto-char beg)
  10567. (while (re-search-forward re end t))
  10568. (setq hiddenp (org-invisible-p))
  10569. (end-of-line 1)
  10570. (and (equal (char-after) ?\n) (forward-char 1))
  10571. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10572. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10573. ;; just skip this line
  10574. (beginning-of-line 2)
  10575. ;; Drawer start, find the end
  10576. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10577. (beginning-of-line 1)))
  10578. (org-skip-over-state-notes)
  10579. (skip-chars-backward " \t\n\r")
  10580. (if (eq (char-before) ?*) (forward-char 1))
  10581. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10582. (beginning-of-line 0)
  10583. (org-indent-to-column indent)
  10584. (beginning-of-line 2)
  10585. (org-indent-to-column indent)
  10586. (beginning-of-line 0)
  10587. (if hiddenp
  10588. (save-excursion
  10589. (org-back-to-heading t)
  10590. (hide-entry))
  10591. (org-flag-drawer t))))
  10592. (defun org-set-property (property value)
  10593. "In the current entry, set PROPERTY to VALUE.
  10594. When called interactively, this will prompt for a property name, offering
  10595. completion on existing and default properties. And then it will prompt
  10596. for a value, offering completion either on allowed values (via an inherited
  10597. xxx_ALL property) or on existing values in other instances of this property
  10598. in the current file."
  10599. (interactive
  10600. (let* ((completion-ignore-case t)
  10601. (keys (org-buffer-property-keys nil t t))
  10602. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10603. (prop (if (member prop0 keys)
  10604. prop0
  10605. (or (cdr (assoc (downcase prop0)
  10606. (mapcar (lambda (x) (cons (downcase x) x))
  10607. keys)))
  10608. prop0)))
  10609. (cur (org-entry-get nil prop))
  10610. (allowed (org-property-get-allowed-values nil prop 'table))
  10611. (existing (mapcar 'list (org-property-values prop)))
  10612. (val (if allowed
  10613. (org-completing-read "Value: " allowed nil 'req-match)
  10614. (let (org-completion-use-ido)
  10615. (org-completing-read
  10616. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10617. (concat "[" cur "]") "")
  10618. ": ")
  10619. existing nil nil "" nil cur)))))
  10620. (list prop (if (equal val "") cur val))))
  10621. (unless (equal (org-entry-get nil property) value)
  10622. (org-entry-put nil property value)))
  10623. (defun org-delete-property (property)
  10624. "In the current entry, delete PROPERTY."
  10625. (interactive
  10626. (let* ((completion-ignore-case t)
  10627. (prop (org-ido-completing-read
  10628. "Property: " (org-entry-properties nil 'standard))))
  10629. (list prop)))
  10630. (message "Property %s %s" property
  10631. (if (org-entry-delete nil property)
  10632. "deleted"
  10633. "was not present in the entry")))
  10634. (defun org-delete-property-globally (property)
  10635. "Remove PROPERTY globally, from all entries."
  10636. (interactive
  10637. (let* ((completion-ignore-case t)
  10638. (prop (org-ido-completing-read
  10639. "Globally remove property: "
  10640. (mapcar 'list (org-buffer-property-keys)))))
  10641. (list prop)))
  10642. (save-excursion
  10643. (save-restriction
  10644. (widen)
  10645. (goto-char (point-min))
  10646. (let ((cnt 0))
  10647. (while (re-search-forward
  10648. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10649. nil t)
  10650. (setq cnt (1+ cnt))
  10651. (replace-match ""))
  10652. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10653. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10654. (defun org-compute-property-at-point ()
  10655. "Compute the property at point.
  10656. This looks for an enclosing column format, extracts the operator and
  10657. then applies it to the property in the column format's scope."
  10658. (interactive)
  10659. (unless (org-at-property-p)
  10660. (error "Not at a property"))
  10661. (let ((prop (org-match-string-no-properties 2)))
  10662. (org-columns-get-format-and-top-level)
  10663. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10664. (error "No operator defined for property %s" prop))
  10665. (org-columns-compute prop)))
  10666. (defun org-property-get-allowed-values (pom property &optional table)
  10667. "Get allowed values for the property PROPERTY.
  10668. When TABLE is non-nil, return an alist that can directly be used for
  10669. completion."
  10670. (let (vals)
  10671. (cond
  10672. ((equal property "TODO")
  10673. (setq vals (org-with-point-at pom
  10674. (append org-todo-keywords-1 '("")))))
  10675. ((equal property "PRIORITY")
  10676. (let ((n org-lowest-priority))
  10677. (while (>= n org-highest-priority)
  10678. (push (char-to-string n) vals)
  10679. (setq n (1- n)))))
  10680. ((member property org-special-properties))
  10681. (t
  10682. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10683. (when (and vals (string-match "\\S-" vals))
  10684. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10685. (setq vals (mapcar (lambda (x)
  10686. (cond ((stringp x) x)
  10687. ((numberp x) (number-to-string x))
  10688. ((symbolp x) (symbol-name x))
  10689. (t "???")))
  10690. vals)))))
  10691. (if table (mapcar 'list vals) vals)))
  10692. (defun org-property-previous-allowed-value (&optional previous)
  10693. "Switch to the next allowed value for this property."
  10694. (interactive)
  10695. (org-property-next-allowed-value t))
  10696. (defun org-property-next-allowed-value (&optional previous)
  10697. "Switch to the next allowed value for this property."
  10698. (interactive)
  10699. (unless (org-at-property-p)
  10700. (error "Not at a property"))
  10701. (let* ((key (match-string 2))
  10702. (value (match-string 3))
  10703. (allowed (or (org-property-get-allowed-values (point) key)
  10704. (and (member value '("[ ]" "[-]" "[X]"))
  10705. '("[ ]" "[X]"))))
  10706. nval)
  10707. (unless allowed
  10708. (error "Allowed values for this property have not been defined"))
  10709. (if previous (setq allowed (reverse allowed)))
  10710. (if (member value allowed)
  10711. (setq nval (car (cdr (member value allowed)))))
  10712. (setq nval (or nval (car allowed)))
  10713. (if (equal nval value)
  10714. (error "Only one allowed value for this property"))
  10715. (org-at-property-p)
  10716. (replace-match (concat " :" key ": " nval) t t)
  10717. (org-indent-line-function)
  10718. (beginning-of-line 1)
  10719. (skip-chars-forward " \t")))
  10720. (defun org-find-entry-with-id (ident)
  10721. "Locate the entry that contains the ID property with exact value IDENT.
  10722. IDENT can be a string, a symbol or a number, this function will search for
  10723. the string representation of it.
  10724. Return the position where this entry starts, or nil if there is no such entry."
  10725. (interactive "sID: ")
  10726. (let ((id (cond
  10727. ((stringp ident) ident)
  10728. ((symbol-name ident) (symbol-name ident))
  10729. ((numberp ident) (number-to-string ident))
  10730. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10731. (case-fold-search nil))
  10732. (save-excursion
  10733. (save-restriction
  10734. (widen)
  10735. (goto-char (point-min))
  10736. (when (re-search-forward
  10737. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10738. nil t)
  10739. (org-back-to-heading)
  10740. (point))))))
  10741. ;;;; Timestamps
  10742. (defvar org-last-changed-timestamp nil)
  10743. (defvar org-last-inserted-timestamp nil
  10744. "The last time stamp inserted with `org-insert-time-stamp'.")
  10745. (defvar org-time-was-given) ; dynamically scoped parameter
  10746. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10747. (defvar org-ts-what) ; dynamically scoped parameter
  10748. (defun org-time-stamp (arg &optional inactive)
  10749. "Prompt for a date/time and insert a time stamp.
  10750. If the user specifies a time like HH:MM, or if this command is called
  10751. with a prefix argument, the time stamp will contain date and time.
  10752. Otherwise, only the date will be included. All parts of a date not
  10753. specified by the user will be filled in from the current date/time.
  10754. So if you press just return without typing anything, the time stamp
  10755. will represent the current date/time. If there is already a timestamp
  10756. at the cursor, it will be modified."
  10757. (interactive "P")
  10758. (let* ((ts nil)
  10759. (default-time
  10760. ;; Default time is either today, or, when entering a range,
  10761. ;; the range start.
  10762. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10763. (save-excursion
  10764. (re-search-backward
  10765. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10766. (- (point) 20) t)))
  10767. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10768. (current-time)))
  10769. (default-input (and ts (org-get-compact-tod ts)))
  10770. org-time-was-given org-end-time-was-given time)
  10771. (cond
  10772. ((and (org-at-timestamp-p t)
  10773. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10774. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10775. (insert "--")
  10776. (setq time (let ((this-command this-command))
  10777. (org-read-date arg 'totime nil nil
  10778. default-time default-input)))
  10779. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10780. ((org-at-timestamp-p t)
  10781. (setq time (let ((this-command this-command))
  10782. (org-read-date arg 'totime nil nil default-time default-input)))
  10783. (when (org-at-timestamp-p t) ; just to get the match data
  10784. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10785. (replace-match "")
  10786. (setq org-last-changed-timestamp
  10787. (org-insert-time-stamp
  10788. time (or org-time-was-given arg)
  10789. inactive nil nil (list org-end-time-was-given))))
  10790. (message "Timestamp updated"))
  10791. (t
  10792. (setq time (let ((this-command this-command))
  10793. (org-read-date arg 'totime nil nil default-time default-input)))
  10794. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10795. nil nil (list org-end-time-was-given))))))
  10796. ;; FIXME: can we use this for something else, like computing time differences?
  10797. (defun org-get-compact-tod (s)
  10798. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10799. (let* ((t1 (match-string 1 s))
  10800. (h1 (string-to-number (match-string 2 s)))
  10801. (m1 (string-to-number (match-string 3 s)))
  10802. (t2 (and (match-end 4) (match-string 5 s)))
  10803. (h2 (and t2 (string-to-number (match-string 6 s))))
  10804. (m2 (and t2 (string-to-number (match-string 7 s))))
  10805. dh dm)
  10806. (if (not t2)
  10807. t1
  10808. (setq dh (- h2 h1) dm (- m2 m1))
  10809. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10810. (concat t1 "+" (number-to-string dh)
  10811. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10812. (defun org-time-stamp-inactive (&optional arg)
  10813. "Insert an inactive time stamp.
  10814. An inactive time stamp is enclosed in square brackets instead of angle
  10815. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10816. does not link to the calendar and cannot be changed with the S-cursor keys.
  10817. So these are more for recording a certain time/date."
  10818. (interactive "P")
  10819. (org-time-stamp arg 'inactive))
  10820. (defvar org-date-ovl (org-make-overlay 1 1))
  10821. (org-overlay-put org-date-ovl 'face 'org-warning)
  10822. (org-detach-overlay org-date-ovl)
  10823. (defvar org-ans1) ; dynamically scoped parameter
  10824. (defvar org-ans2) ; dynamically scoped parameter
  10825. (defvar org-plain-time-of-day-regexp) ; defined below
  10826. (defvar org-overriding-default-time nil) ; dynamically scoped
  10827. (defvar org-read-date-overlay nil)
  10828. (defvar org-dcst nil) ; dynamically scoped
  10829. (defvar org-read-date-history nil)
  10830. (defun org-read-date (&optional with-time to-time from-string prompt
  10831. default-time default-input)
  10832. "Read a date, possibly a time, and make things smooth for the user.
  10833. The prompt will suggest to enter an ISO date, but you can also enter anything
  10834. which will at least partially be understood by `parse-time-string'.
  10835. Unrecognized parts of the date will default to the current day, month, year,
  10836. hour and minute. If this command is called to replace a timestamp at point,
  10837. of to enter the second timestamp of a range, the default time is taken from the
  10838. existing stamp. For example,
  10839. 3-2-5 --> 2003-02-05
  10840. feb 15 --> currentyear-02-15
  10841. sep 12 9 --> 2009-09-12
  10842. 12:45 --> today 12:45
  10843. 22 sept 0:34 --> currentyear-09-22 0:34
  10844. 12 --> currentyear-currentmonth-12
  10845. Fri --> nearest Friday (today or later)
  10846. etc.
  10847. Furthermore you can specify a relative date by giving, as the *first* thing
  10848. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10849. change in days weeks, months, years.
  10850. With a single plus or minus, the date is relative to today. With a double
  10851. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10852. +4d --> four days from today
  10853. +4 --> same as above
  10854. +2w --> two weeks from today
  10855. ++5 --> five days from default date
  10856. The function understands only English month and weekday abbreviations,
  10857. but this can be configured with the variables `parse-time-months' and
  10858. `parse-time-weekdays'.
  10859. While prompting, a calendar is popped up - you can also select the
  10860. date with the mouse (button 1). The calendar shows a period of three
  10861. months. To scroll it to other months, use the keys `>' and `<'.
  10862. If you don't like the calendar, turn it off with
  10863. \(setq org-read-date-popup-calendar nil)
  10864. With optional argument TO-TIME, the date will immediately be converted
  10865. to an internal time.
  10866. With an optional argument WITH-TIME, the prompt will suggest to also
  10867. insert a time. Note that when WITH-TIME is not set, you can still
  10868. enter a time, and this function will inform the calling routine about
  10869. this change. The calling routine may then choose to change the format
  10870. used to insert the time stamp into the buffer to include the time.
  10871. With optional argument FROM-STRING, read from this string instead from
  10872. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10873. the time/date that is used for everything that is not specified by the
  10874. user."
  10875. (require 'parse-time)
  10876. (let* ((org-time-stamp-rounding-minutes
  10877. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10878. (org-dcst org-display-custom-times)
  10879. (ct (org-current-time))
  10880. (def (or org-overriding-default-time default-time ct))
  10881. (defdecode (decode-time def))
  10882. (dummy (progn
  10883. (when (< (nth 2 defdecode) org-extend-today-until)
  10884. (setcar (nthcdr 2 defdecode) -1)
  10885. (setcar (nthcdr 1 defdecode) 59)
  10886. (setq def (apply 'encode-time defdecode)
  10887. defdecode (decode-time def)))))
  10888. (calendar-move-hook nil)
  10889. (calendar-view-diary-initially-flag nil)
  10890. (view-diary-entries-initially nil)
  10891. (calendar-view-holidays-initially-flag nil)
  10892. (view-calendar-holidays-initially nil)
  10893. (timestr (format-time-string
  10894. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10895. (prompt (concat (if prompt (concat prompt " ") "")
  10896. (format "Date+time [%s]: " timestr)))
  10897. ans (org-ans0 "") org-ans1 org-ans2 final)
  10898. (cond
  10899. (from-string (setq ans from-string))
  10900. (org-read-date-popup-calendar
  10901. (save-excursion
  10902. (save-window-excursion
  10903. (calendar)
  10904. (calendar-forward-day (- (time-to-days def)
  10905. (calendar-absolute-from-gregorian
  10906. (calendar-current-date))))
  10907. (org-eval-in-calendar nil t)
  10908. (let* ((old-map (current-local-map))
  10909. (map (copy-keymap calendar-mode-map))
  10910. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10911. (org-defkey map (kbd "RET") 'org-calendar-select)
  10912. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10913. 'org-calendar-select-mouse)
  10914. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10915. 'org-calendar-select-mouse)
  10916. (org-defkey minibuffer-local-map [(meta shift left)]
  10917. (lambda () (interactive)
  10918. (org-eval-in-calendar '(calendar-backward-month 1))))
  10919. (org-defkey minibuffer-local-map [(meta shift right)]
  10920. (lambda () (interactive)
  10921. (org-eval-in-calendar '(calendar-forward-month 1))))
  10922. (org-defkey minibuffer-local-map [(meta shift up)]
  10923. (lambda () (interactive)
  10924. (org-eval-in-calendar '(calendar-backward-year 1))))
  10925. (org-defkey minibuffer-local-map [(meta shift down)]
  10926. (lambda () (interactive)
  10927. (org-eval-in-calendar '(calendar-forward-year 1))))
  10928. (org-defkey minibuffer-local-map [(shift up)]
  10929. (lambda () (interactive)
  10930. (org-eval-in-calendar '(calendar-backward-week 1))))
  10931. (org-defkey minibuffer-local-map [(shift down)]
  10932. (lambda () (interactive)
  10933. (org-eval-in-calendar '(calendar-forward-week 1))))
  10934. (org-defkey minibuffer-local-map [(shift left)]
  10935. (lambda () (interactive)
  10936. (org-eval-in-calendar '(calendar-backward-day 1))))
  10937. (org-defkey minibuffer-local-map [(shift right)]
  10938. (lambda () (interactive)
  10939. (org-eval-in-calendar '(calendar-forward-day 1))))
  10940. (org-defkey minibuffer-local-map ">"
  10941. (lambda () (interactive)
  10942. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10943. (org-defkey minibuffer-local-map "<"
  10944. (lambda () (interactive)
  10945. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10946. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10947. (unwind-protect
  10948. (progn
  10949. (use-local-map map)
  10950. (add-hook 'post-command-hook 'org-read-date-display)
  10951. (setq org-ans0 (read-string prompt default-input
  10952. 'org-read-date-history nil))
  10953. ;; org-ans0: from prompt
  10954. ;; org-ans1: from mouse click
  10955. ;; org-ans2: from calendar motion
  10956. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10957. (remove-hook 'post-command-hook 'org-read-date-display)
  10958. (use-local-map old-map)
  10959. (when org-read-date-overlay
  10960. (org-delete-overlay org-read-date-overlay)
  10961. (setq org-read-date-overlay nil)))))))
  10962. (t ; Naked prompt only
  10963. (unwind-protect
  10964. (setq ans (read-string prompt default-input
  10965. 'org-read-date-history timestr))
  10966. (when org-read-date-overlay
  10967. (org-delete-overlay org-read-date-overlay)
  10968. (setq org-read-date-overlay nil)))))
  10969. (setq final (org-read-date-analyze ans def defdecode))
  10970. (if to-time
  10971. (apply 'encode-time final)
  10972. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10973. (format "%04d-%02d-%02d %02d:%02d"
  10974. (nth 5 final) (nth 4 final) (nth 3 final)
  10975. (nth 2 final) (nth 1 final))
  10976. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10977. (defvar def)
  10978. (defvar defdecode)
  10979. (defvar with-time)
  10980. (defun org-read-date-display ()
  10981. "Display the current date prompt interpretation in the minibuffer."
  10982. (when org-read-date-display-live
  10983. (when org-read-date-overlay
  10984. (org-delete-overlay org-read-date-overlay))
  10985. (let ((p (point)))
  10986. (end-of-line 1)
  10987. (while (not (equal (buffer-substring
  10988. (max (point-min) (- (point) 4)) (point))
  10989. " "))
  10990. (insert " "))
  10991. (goto-char p))
  10992. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10993. " " (or org-ans1 org-ans2)))
  10994. (org-end-time-was-given nil)
  10995. (f (org-read-date-analyze ans def defdecode))
  10996. (fmts (if org-dcst
  10997. org-time-stamp-custom-formats
  10998. org-time-stamp-formats))
  10999. (fmt (if (or with-time
  11000. (and (boundp 'org-time-was-given) org-time-was-given))
  11001. (cdr fmts)
  11002. (car fmts)))
  11003. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11004. (when (and org-end-time-was-given
  11005. (string-match org-plain-time-of-day-regexp txt))
  11006. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11007. org-end-time-was-given
  11008. (substring txt (match-end 0)))))
  11009. (setq org-read-date-overlay
  11010. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11011. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11012. (defun org-read-date-analyze (ans def defdecode)
  11013. "Analyse the combined answer of the date prompt."
  11014. ;; FIXME: cleanup and comment
  11015. (let (delta deltan deltaw deltadef year month day
  11016. hour minute second wday pm h2 m2 tl wday1
  11017. iso-year iso-weekday iso-week iso-year iso-date)
  11018. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11019. (setq ans "+0"))
  11020. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11021. (setq ans (replace-match "" t t ans)
  11022. deltan (car delta)
  11023. deltaw (nth 1 delta)
  11024. deltadef (nth 2 delta)))
  11025. ;; Check if there is an iso week date in there
  11026. ;; If yes, sore the info and postpone interpreting it until the rest
  11027. ;; of the parsing is done
  11028. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11029. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11030. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11031. iso-week (string-to-number (match-string 2 ans)))
  11032. (setq ans (replace-match "" t t ans)))
  11033. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11034. (when (string-match
  11035. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11036. (setq year (if (match-end 2)
  11037. (string-to-number (match-string 2 ans))
  11038. (string-to-number (format-time-string "%Y")))
  11039. month (string-to-number (match-string 3 ans))
  11040. day (string-to-number (match-string 4 ans)))
  11041. (if (< year 100) (setq year (+ 2000 year)))
  11042. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11043. t nil ans)))
  11044. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11045. ;; If there is a time with am/pm, and *no* time without it, we convert
  11046. ;; so that matching will be successful.
  11047. (loop for i from 1 to 2 do ; twice, for end time as well
  11048. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11049. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11050. (setq hour (string-to-number (match-string 1 ans))
  11051. minute (if (match-end 3)
  11052. (string-to-number (match-string 3 ans))
  11053. 0)
  11054. pm (equal ?p
  11055. (string-to-char (downcase (match-string 4 ans)))))
  11056. (if (and (= hour 12) (not pm))
  11057. (setq hour 0)
  11058. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11059. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11060. t t ans))))
  11061. ;; Check if a time range is given as a duration
  11062. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11063. (setq hour (string-to-number (match-string 1 ans))
  11064. h2 (+ hour (string-to-number (match-string 3 ans)))
  11065. minute (string-to-number (match-string 2 ans))
  11066. m2 (+ minute (if (match-end 5) (string-to-number
  11067. (match-string 5 ans))0)))
  11068. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11069. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11070. t t ans)))
  11071. ;; Check if there is a time range
  11072. (when (boundp 'org-end-time-was-given)
  11073. (setq org-time-was-given nil)
  11074. (when (and (string-match org-plain-time-of-day-regexp ans)
  11075. (match-end 8))
  11076. (setq org-end-time-was-given (match-string 8 ans))
  11077. (setq ans (concat (substring ans 0 (match-beginning 7))
  11078. (substring ans (match-end 7))))))
  11079. (setq tl (parse-time-string ans)
  11080. day (or (nth 3 tl) (nth 3 defdecode))
  11081. month (or (nth 4 tl)
  11082. (if (and org-read-date-prefer-future
  11083. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11084. (1+ (nth 4 defdecode))
  11085. (nth 4 defdecode)))
  11086. year (or (nth 5 tl)
  11087. (if (and org-read-date-prefer-future
  11088. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11089. (1+ (nth 5 defdecode))
  11090. (nth 5 defdecode)))
  11091. hour (or (nth 2 tl) (nth 2 defdecode))
  11092. minute (or (nth 1 tl) (nth 1 defdecode))
  11093. second (or (nth 0 tl) 0)
  11094. wday (nth 6 tl))
  11095. ;; Special date definitions below
  11096. (cond
  11097. (iso-week
  11098. ;; There was an iso week
  11099. (setq year (or iso-year year)
  11100. day (or iso-weekday wday 1)
  11101. wday nil ; to make sure that the trigger below does not match
  11102. iso-date (calendar-gregorian-from-absolute
  11103. (calendar-absolute-from-iso
  11104. (list iso-week day year))))
  11105. ; FIXME: Should we also push ISO weeks into the future?
  11106. ; (when (and org-read-date-prefer-future
  11107. ; (not iso-year)
  11108. ; (< (calendar-absolute-from-gregorian iso-date)
  11109. ; (time-to-days (current-time))))
  11110. ; (setq year (1+ year)
  11111. ; iso-date (calendar-gregorian-from-absolute
  11112. ; (calendar-absolute-from-iso
  11113. ; (list iso-week day year)))))
  11114. (setq month (car iso-date)
  11115. year (nth 2 iso-date)
  11116. day (nth 1 iso-date)))
  11117. (deltan
  11118. (unless deltadef
  11119. (let ((now (decode-time (current-time))))
  11120. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11121. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11122. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11123. ((equal deltaw "m") (setq month (+ month deltan)))
  11124. ((equal deltaw "y") (setq year (+ year deltan)))))
  11125. ((and wday (not (nth 3 tl)))
  11126. ;; Weekday was given, but no day, so pick that day in the week
  11127. ;; on or after the derived date.
  11128. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11129. (unless (equal wday wday1)
  11130. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11131. (if (and (boundp 'org-time-was-given)
  11132. (nth 2 tl))
  11133. (setq org-time-was-given t))
  11134. (if (< year 100) (setq year (+ 2000 year)))
  11135. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11136. (list second minute hour day month year)))
  11137. (defvar parse-time-weekdays)
  11138. (defun org-read-date-get-relative (s today default)
  11139. "Check string S for special relative date string.
  11140. TODAY and DEFAULT are internal times, for today and for a default.
  11141. Return shift list (N what def-flag)
  11142. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11143. N is the number of WHATs to shift.
  11144. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11145. the DEFAULT date rather than TODAY."
  11146. (when (and
  11147. (string-match
  11148. (concat
  11149. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11150. "\\([0-9]+\\)?"
  11151. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11152. "\\([ \t]\\|$\\)") s)
  11153. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11154. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11155. (string-to-char (substring (match-string 1 s) -1))
  11156. ?+))
  11157. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11158. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11159. (what (if (match-end 3) (match-string 3 s) "d"))
  11160. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11161. (date (if rel default today))
  11162. (wday (nth 6 (decode-time date)))
  11163. delta)
  11164. (if wday1
  11165. (progn
  11166. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11167. (if (= dir ?-) (setq delta (- delta 7)))
  11168. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11169. (list delta "d" rel))
  11170. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11171. (defun org-eval-in-calendar (form &optional keepdate)
  11172. "Eval FORM in the calendar window and return to current window.
  11173. Also, store the cursor date in variable org-ans2."
  11174. (let ((sw (selected-window)))
  11175. (select-window (get-buffer-window "*Calendar*"))
  11176. (eval form)
  11177. (when (and (not keepdate) (calendar-cursor-to-date))
  11178. (let* ((date (calendar-cursor-to-date))
  11179. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11180. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11181. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11182. (select-window sw)))
  11183. (defun org-calendar-select ()
  11184. "Return to `org-read-date' with the date currently selected.
  11185. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11186. (interactive)
  11187. (when (calendar-cursor-to-date)
  11188. (let* ((date (calendar-cursor-to-date))
  11189. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11190. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11191. (if (active-minibuffer-window) (exit-minibuffer))))
  11192. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11193. "Insert a date stamp for the date given by the internal TIME.
  11194. WITH-HM means, use the stamp format that includes the time of the day.
  11195. INACTIVE means use square brackets instead of angular ones, so that the
  11196. stamp will not contribute to the agenda.
  11197. PRE and POST are optional strings to be inserted before and after the
  11198. stamp.
  11199. The command returns the inserted time stamp."
  11200. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11201. stamp)
  11202. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11203. (insert-before-markers (or pre ""))
  11204. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11205. (when (listp extra)
  11206. (setq extra (car extra))
  11207. (if (and (stringp extra)
  11208. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11209. (setq extra (format "-%02d:%02d"
  11210. (string-to-number (match-string 1 extra))
  11211. (string-to-number (match-string 2 extra))))
  11212. (setq extra nil)))
  11213. (when extra
  11214. (backward-char 1)
  11215. (insert-before-markers extra)
  11216. (forward-char 1))
  11217. (insert-before-markers (or post ""))
  11218. (setq org-last-inserted-timestamp stamp)))
  11219. (defun org-toggle-time-stamp-overlays ()
  11220. "Toggle the use of custom time stamp formats."
  11221. (interactive)
  11222. (setq org-display-custom-times (not org-display-custom-times))
  11223. (unless org-display-custom-times
  11224. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11225. (while (setq p (next-single-property-change p 'display))
  11226. (if (and (get-text-property p 'display)
  11227. (eq (get-text-property p 'face) 'org-date))
  11228. (remove-text-properties
  11229. p (setq p (next-single-property-change p 'display))
  11230. '(display t))))
  11231. (set-buffer-modified-p bmp)))
  11232. (if (featurep 'xemacs)
  11233. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11234. (org-restart-font-lock)
  11235. (setq org-table-may-need-update t)
  11236. (if org-display-custom-times
  11237. (message "Time stamps are overlayed with custom format")
  11238. (message "Time stamp overlays removed")))
  11239. (defun org-display-custom-time (beg end)
  11240. "Overlay modified time stamp format over timestamp between BEG and END."
  11241. (let* ((ts (buffer-substring beg end))
  11242. t1 w1 with-hm tf time str w2 (off 0))
  11243. (save-match-data
  11244. (setq t1 (org-parse-time-string ts t))
  11245. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11246. (setq off (- (match-end 0) (match-beginning 0)))))
  11247. (setq end (- end off))
  11248. (setq w1 (- end beg)
  11249. with-hm (and (nth 1 t1) (nth 2 t1))
  11250. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11251. time (org-fix-decoded-time t1)
  11252. str (org-add-props
  11253. (format-time-string
  11254. (substring tf 1 -1) (apply 'encode-time time))
  11255. nil 'mouse-face 'highlight)
  11256. w2 (length str))
  11257. (if (not (= w2 w1))
  11258. (add-text-properties (1+ beg) (+ 2 beg)
  11259. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11260. (if (featurep 'xemacs)
  11261. (progn
  11262. (put-text-property beg end 'invisible t)
  11263. (put-text-property beg end 'end-glyph (make-glyph str)))
  11264. (put-text-property beg end 'display str))))
  11265. (defun org-translate-time (string)
  11266. "Translate all timestamps in STRING to custom format.
  11267. But do this only if the variable `org-display-custom-times' is set."
  11268. (when org-display-custom-times
  11269. (save-match-data
  11270. (let* ((start 0)
  11271. (re org-ts-regexp-both)
  11272. t1 with-hm inactive tf time str beg end)
  11273. (while (setq start (string-match re string start))
  11274. (setq beg (match-beginning 0)
  11275. end (match-end 0)
  11276. t1 (save-match-data
  11277. (org-parse-time-string (substring string beg end) t))
  11278. with-hm (and (nth 1 t1) (nth 2 t1))
  11279. inactive (equal (substring string beg (1+ beg)) "[")
  11280. tf (funcall (if with-hm 'cdr 'car)
  11281. org-time-stamp-custom-formats)
  11282. time (org-fix-decoded-time t1)
  11283. str (format-time-string
  11284. (concat
  11285. (if inactive "[" "<") (substring tf 1 -1)
  11286. (if inactive "]" ">"))
  11287. (apply 'encode-time time))
  11288. string (replace-match str t t string)
  11289. start (+ start (length str)))))))
  11290. string)
  11291. (defun org-fix-decoded-time (time)
  11292. "Set 0 instead of nil for the first 6 elements of time.
  11293. Don't touch the rest."
  11294. (let ((n 0))
  11295. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11296. (defun org-days-to-time (timestamp-string)
  11297. "Difference between TIMESTAMP-STRING and now in days."
  11298. (- (time-to-days (org-time-string-to-time timestamp-string))
  11299. (time-to-days (current-time))))
  11300. (defun org-deadline-close (timestamp-string &optional ndays)
  11301. "Is the time in TIMESTAMP-STRING close to the current date?"
  11302. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11303. (and (< (org-days-to-time timestamp-string) ndays)
  11304. (not (org-entry-is-done-p))))
  11305. (defun org-get-wdays (ts)
  11306. "Get the deadline lead time appropriate for timestring TS."
  11307. (cond
  11308. ((<= org-deadline-warning-days 0)
  11309. ;; 0 or negative, enforce this value no matter what
  11310. (- org-deadline-warning-days))
  11311. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11312. ;; lead time is specified.
  11313. (floor (* (string-to-number (match-string 1 ts))
  11314. (cdr (assoc (match-string 2 ts)
  11315. '(("d" . 1) ("w" . 7)
  11316. ("m" . 30.4) ("y" . 365.25)))))))
  11317. ;; go for the default.
  11318. (t org-deadline-warning-days)))
  11319. (defun org-calendar-select-mouse (ev)
  11320. "Return to `org-read-date' with the date currently selected.
  11321. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11322. (interactive "e")
  11323. (mouse-set-point ev)
  11324. (when (calendar-cursor-to-date)
  11325. (let* ((date (calendar-cursor-to-date))
  11326. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11327. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11328. (if (active-minibuffer-window) (exit-minibuffer))))
  11329. (defun org-check-deadlines (ndays)
  11330. "Check if there are any deadlines due or past due.
  11331. A deadline is considered due if it happens within `org-deadline-warning-days'
  11332. days from today's date. If the deadline appears in an entry marked DONE,
  11333. it is not shown. The prefix arg NDAYS can be used to test that many
  11334. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11335. (interactive "P")
  11336. (let* ((org-warn-days
  11337. (cond
  11338. ((equal ndays '(4)) 100000)
  11339. (ndays (prefix-numeric-value ndays))
  11340. (t (abs org-deadline-warning-days))))
  11341. (case-fold-search nil)
  11342. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11343. (callback
  11344. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11345. (message "%d deadlines past-due or due within %d days"
  11346. (org-occur regexp nil callback)
  11347. org-warn-days)))
  11348. (defun org-check-before-date (date)
  11349. "Check if there are deadlines or scheduled entries before DATE."
  11350. (interactive (list (org-read-date)))
  11351. (let ((case-fold-search nil)
  11352. (regexp (concat "\\<\\(" org-deadline-string
  11353. "\\|" org-scheduled-string
  11354. "\\) *<\\([^>]+\\)>"))
  11355. (callback
  11356. (lambda () (time-less-p
  11357. (org-time-string-to-time (match-string 2))
  11358. (org-time-string-to-time date)))))
  11359. (message "%d entries before %s"
  11360. (org-occur regexp nil callback) date)))
  11361. (defun org-check-after-date (date)
  11362. "Check if there are deadlines or scheduled entries after DATE."
  11363. (interactive (list (org-read-date)))
  11364. (let ((case-fold-search nil)
  11365. (regexp (concat "\\<\\(" org-deadline-string
  11366. "\\|" org-scheduled-string
  11367. "\\) *<\\([^>]+\\)>"))
  11368. (callback
  11369. (lambda () (not
  11370. (time-less-p
  11371. (org-time-string-to-time (match-string 2))
  11372. (org-time-string-to-time date))))))
  11373. (message "%d entries after %s"
  11374. (org-occur regexp nil callback) date)))
  11375. (defun org-evaluate-time-range (&optional to-buffer)
  11376. "Evaluate a time range by computing the difference between start and end.
  11377. Normally the result is just printed in the echo area, but with prefix arg
  11378. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11379. If the time range is actually in a table, the result is inserted into the
  11380. next column.
  11381. For time difference computation, a year is assumed to be exactly 365
  11382. days in order to avoid rounding problems."
  11383. (interactive "P")
  11384. (or
  11385. (org-clock-update-time-maybe)
  11386. (save-excursion
  11387. (unless (org-at-date-range-p t)
  11388. (goto-char (point-at-bol))
  11389. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11390. (if (not (org-at-date-range-p t))
  11391. (error "Not at a time-stamp range, and none found in current line")))
  11392. (let* ((ts1 (match-string 1))
  11393. (ts2 (match-string 2))
  11394. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11395. (match-end (match-end 0))
  11396. (time1 (org-time-string-to-time ts1))
  11397. (time2 (org-time-string-to-time ts2))
  11398. (t1 (time-to-seconds time1))
  11399. (t2 (time-to-seconds time2))
  11400. (diff (abs (- t2 t1)))
  11401. (negative (< (- t2 t1) 0))
  11402. ;; (ys (floor (* 365 24 60 60)))
  11403. (ds (* 24 60 60))
  11404. (hs (* 60 60))
  11405. (fy "%dy %dd %02d:%02d")
  11406. (fy1 "%dy %dd")
  11407. (fd "%dd %02d:%02d")
  11408. (fd1 "%dd")
  11409. (fh "%02d:%02d")
  11410. y d h m align)
  11411. (if havetime
  11412. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11413. y 0
  11414. d (floor (/ diff ds)) diff (mod diff ds)
  11415. h (floor (/ diff hs)) diff (mod diff hs)
  11416. m (floor (/ diff 60)))
  11417. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11418. y 0
  11419. d (floor (+ (/ diff ds) 0.5))
  11420. h 0 m 0))
  11421. (if (not to-buffer)
  11422. (message "%s" (org-make-tdiff-string y d h m))
  11423. (if (org-at-table-p)
  11424. (progn
  11425. (goto-char match-end)
  11426. (setq align t)
  11427. (and (looking-at " *|") (goto-char (match-end 0))))
  11428. (goto-char match-end))
  11429. (if (looking-at
  11430. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11431. (replace-match ""))
  11432. (if negative (insert " -"))
  11433. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11434. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11435. (insert " " (format fh h m))))
  11436. (if align (org-table-align))
  11437. (message "Time difference inserted")))))
  11438. (defun org-make-tdiff-string (y d h m)
  11439. (let ((fmt "")
  11440. (l nil))
  11441. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11442. l (push y l)))
  11443. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11444. l (push d l)))
  11445. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11446. l (push h l)))
  11447. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11448. l (push m l)))
  11449. (apply 'format fmt (nreverse l))))
  11450. (defun org-time-string-to-time (s)
  11451. (apply 'encode-time (org-parse-time-string s)))
  11452. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11453. "Convert a time stamp to an absolute day number.
  11454. If there is a specifyer for a cyclic time stamp, get the closest date to
  11455. DAYNR.
  11456. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11457. (cond
  11458. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11459. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11460. daynr
  11461. (+ daynr 1000)))
  11462. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11463. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11464. (time-to-days (current-time))) (match-string 0 s)
  11465. prefer show-all))
  11466. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11467. (defun org-days-to-iso-week (days)
  11468. "Return the iso week number."
  11469. (require 'cal-iso)
  11470. (car (calendar-iso-from-absolute days)))
  11471. (defun org-small-year-to-year (year)
  11472. "Convert 2-digit years into 4-digit years.
  11473. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11474. The year 2000 cannot be abbreviated. Any year larger than 99
  11475. is returned unchanged."
  11476. (if (< year 38)
  11477. (setq year (+ 2000 year))
  11478. (if (< year 100)
  11479. (setq year (+ 1900 year))))
  11480. year)
  11481. (defun org-time-from-absolute (d)
  11482. "Return the time corresponding to date D.
  11483. D may be an absolute day number, or a calendar-type list (month day year)."
  11484. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11485. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11486. (defun org-calendar-holiday ()
  11487. "List of holidays, for Diary display in Org-mode."
  11488. (require 'holidays)
  11489. (let ((hl (funcall
  11490. (if (fboundp 'calendar-check-holidays)
  11491. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11492. (if hl (mapconcat 'identity hl "; "))))
  11493. (defun org-diary-sexp-entry (sexp entry date)
  11494. "Process a SEXP diary ENTRY for DATE."
  11495. (require 'diary-lib)
  11496. (let ((result (if calendar-debug-sexp
  11497. (let ((stack-trace-on-error t))
  11498. (eval (car (read-from-string sexp))))
  11499. (condition-case nil
  11500. (eval (car (read-from-string sexp)))
  11501. (error
  11502. (beep)
  11503. (message "Bad sexp at line %d in %s: %s"
  11504. (org-current-line)
  11505. (buffer-file-name) sexp)
  11506. (sleep-for 2))))))
  11507. (cond ((stringp result) result)
  11508. ((and (consp result)
  11509. (stringp (cdr result))) (cdr result))
  11510. (result entry)
  11511. (t nil))))
  11512. (defun org-diary-to-ical-string (frombuf)
  11513. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11514. This uses the icalendar.el library."
  11515. (let* ((tmpdir (if (featurep 'xemacs)
  11516. (temp-directory)
  11517. temporary-file-directory))
  11518. (tmpfile (make-temp-name
  11519. (expand-file-name "orgics" tmpdir)))
  11520. buf rtn b e)
  11521. (save-excursion
  11522. (set-buffer frombuf)
  11523. (icalendar-export-region (point-min) (point-max) tmpfile)
  11524. (setq buf (find-buffer-visiting tmpfile))
  11525. (set-buffer buf)
  11526. (goto-char (point-min))
  11527. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11528. (setq b (match-beginning 0)))
  11529. (goto-char (point-max))
  11530. (if (re-search-backward "^END:VEVENT" nil t)
  11531. (setq e (match-end 0)))
  11532. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11533. (kill-buffer buf)
  11534. (delete-file tmpfile)
  11535. rtn))
  11536. (defun org-closest-date (start current change prefer show-all)
  11537. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11538. When PREFER is `past' return a date that is either CURRENT or past.
  11539. When PREFER is `future', return a date that is either CURRENT or future.
  11540. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11541. ;; Make the proper lists from the dates
  11542. (catch 'exit
  11543. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11544. dn dw sday cday n1 n2 n0
  11545. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11546. (setq start (org-date-to-gregorian start)
  11547. current (org-date-to-gregorian
  11548. (if show-all
  11549. current
  11550. (time-to-days (current-time))))
  11551. sday (calendar-absolute-from-gregorian start)
  11552. cday (calendar-absolute-from-gregorian current))
  11553. (if (<= cday sday) (throw 'exit sday))
  11554. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11555. (setq dn (string-to-number (match-string 1 change))
  11556. dw (cdr (assoc (match-string 2 change) a1)))
  11557. (error "Invalid change specifyer: %s" change))
  11558. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11559. (cond
  11560. ((eq dw 'day)
  11561. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11562. n2 (+ n1 dn)))
  11563. ((eq dw 'year)
  11564. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11565. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11566. (setq date1 (list m d y1)
  11567. n1 (calendar-absolute-from-gregorian date1)
  11568. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11569. n2 (calendar-absolute-from-gregorian date2)))
  11570. ((eq dw 'month)
  11571. ;; approx number of month between the two dates
  11572. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11573. ;; How often does dn fit in there?
  11574. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11575. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11576. m (+ m nm)
  11577. ny (floor (/ m 12))
  11578. y (+ y ny)
  11579. m (- m (* ny 12)))
  11580. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11581. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11582. (setq m2 (+ m dn) y2 y)
  11583. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11584. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11585. (while (<= n2 cday)
  11586. (setq n1 n2 m m2 y y2)
  11587. (setq m2 (+ m dn) y2 y)
  11588. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11589. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11590. ;; Make sure n1 is the earlier date
  11591. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11592. (if show-all
  11593. (cond
  11594. ((eq prefer 'past) n1)
  11595. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11596. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11597. (cond
  11598. ((eq prefer 'past) n1)
  11599. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11600. (t (if (= cday n1) n1 n2)))))))
  11601. (defun org-date-to-gregorian (date)
  11602. "Turn any specification of DATE into a gregorian date for the calendar."
  11603. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11604. ((and (listp date) (= (length date) 3)) date)
  11605. ((stringp date)
  11606. (setq date (org-parse-time-string date))
  11607. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11608. ((listp date)
  11609. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11610. (defun org-parse-time-string (s &optional nodefault)
  11611. "Parse the standard Org-mode time string.
  11612. This should be a lot faster than the normal `parse-time-string'.
  11613. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11614. hour and minute fields will be nil if not given."
  11615. (if (string-match org-ts-regexp0 s)
  11616. (list 0
  11617. (if (or (match-beginning 8) (not nodefault))
  11618. (string-to-number (or (match-string 8 s) "0")))
  11619. (if (or (match-beginning 7) (not nodefault))
  11620. (string-to-number (or (match-string 7 s) "0")))
  11621. (string-to-number (match-string 4 s))
  11622. (string-to-number (match-string 3 s))
  11623. (string-to-number (match-string 2 s))
  11624. nil nil nil)
  11625. (make-list 9 0)))
  11626. (defun org-timestamp-up (&optional arg)
  11627. "Increase the date item at the cursor by one.
  11628. If the cursor is on the year, change the year. If it is on the month or
  11629. the day, change that.
  11630. With prefix ARG, change by that many units."
  11631. (interactive "p")
  11632. (org-timestamp-change (prefix-numeric-value arg)))
  11633. (defun org-timestamp-down (&optional arg)
  11634. "Decrease the date item at the cursor by one.
  11635. If the cursor is on the year, change the year. If it is on the month or
  11636. the day, change that.
  11637. With prefix ARG, change by that many units."
  11638. (interactive "p")
  11639. (org-timestamp-change (- (prefix-numeric-value arg))))
  11640. (defun org-timestamp-up-day (&optional arg)
  11641. "Increase the date in the time stamp by one day.
  11642. With prefix ARG, change that many days."
  11643. (interactive "p")
  11644. (if (and (not (org-at-timestamp-p t))
  11645. (org-on-heading-p))
  11646. (org-todo 'up)
  11647. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11648. (defun org-timestamp-down-day (&optional arg)
  11649. "Decrease the date in the time stamp by one day.
  11650. With prefix ARG, change that many days."
  11651. (interactive "p")
  11652. (if (and (not (org-at-timestamp-p t))
  11653. (org-on-heading-p))
  11654. (org-todo 'down)
  11655. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11656. (defun org-at-timestamp-p (&optional inactive-ok)
  11657. "Determine if the cursor is in or at a timestamp."
  11658. (interactive)
  11659. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11660. (pos (point))
  11661. (ans (or (looking-at tsr)
  11662. (save-excursion
  11663. (skip-chars-backward "^[<\n\r\t")
  11664. (if (> (point) (point-min)) (backward-char 1))
  11665. (and (looking-at tsr)
  11666. (> (- (match-end 0) pos) -1))))))
  11667. (and ans
  11668. (boundp 'org-ts-what)
  11669. (setq org-ts-what
  11670. (cond
  11671. ((= pos (match-beginning 0)) 'bracket)
  11672. ((= pos (1- (match-end 0))) 'bracket)
  11673. ((org-pos-in-match-range pos 2) 'year)
  11674. ((org-pos-in-match-range pos 3) 'month)
  11675. ((org-pos-in-match-range pos 7) 'hour)
  11676. ((org-pos-in-match-range pos 8) 'minute)
  11677. ((or (org-pos-in-match-range pos 4)
  11678. (org-pos-in-match-range pos 5)) 'day)
  11679. ((and (> pos (or (match-end 8) (match-end 5)))
  11680. (< pos (match-end 0)))
  11681. (- pos (or (match-end 8) (match-end 5))))
  11682. (t 'day))))
  11683. ans))
  11684. (defun org-toggle-timestamp-type ()
  11685. "Toggle the type (<active> or [inactive]) of a time stamp."
  11686. (interactive)
  11687. (when (org-at-timestamp-p t)
  11688. (let ((beg (match-beginning 0)) (end (match-end 0))
  11689. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11690. (save-excursion
  11691. (goto-char beg)
  11692. (while (re-search-forward "[][<>]" end t)
  11693. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11694. t t)))
  11695. (message "Timestamp is now %sactive"
  11696. (if (equal (char-after beg) ?<) "" "in")))))
  11697. (defun org-timestamp-change (n &optional what)
  11698. "Change the date in the time stamp at point.
  11699. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11700. `year', `minute', `second'. If WHAT is not given, the cursor position
  11701. in the timestamp determines what will be changed."
  11702. (let ((pos (point))
  11703. with-hm inactive
  11704. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11705. org-ts-what
  11706. extra rem
  11707. ts time time0)
  11708. (if (not (org-at-timestamp-p t))
  11709. (error "Not at a timestamp"))
  11710. (if (and (not what) (eq org-ts-what 'bracket))
  11711. (org-toggle-timestamp-type)
  11712. (if (and (not what) (not (eq org-ts-what 'day))
  11713. org-display-custom-times
  11714. (get-text-property (point) 'display)
  11715. (not (get-text-property (1- (point)) 'display)))
  11716. (setq org-ts-what 'day))
  11717. (setq org-ts-what (or what org-ts-what)
  11718. inactive (= (char-after (match-beginning 0)) ?\[)
  11719. ts (match-string 0))
  11720. (replace-match "")
  11721. (if (string-match
  11722. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11723. ts)
  11724. (setq extra (match-string 1 ts)))
  11725. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11726. (setq with-hm t))
  11727. (setq time0 (org-parse-time-string ts))
  11728. (when (and (eq org-ts-what 'minute)
  11729. (eq current-prefix-arg nil))
  11730. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11731. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11732. (setcar (cdr time0) (+ (nth 1 time0)
  11733. (if (> n 0) (- rem) (- dm rem))))))
  11734. (setq time
  11735. (encode-time (or (car time0) 0)
  11736. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11737. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11738. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11739. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11740. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11741. (nthcdr 6 time0)))
  11742. (when (and (member org-ts-what '(hour minute))
  11743. extra
  11744. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11745. (setq extra (org-modify-ts-extra
  11746. extra
  11747. (if (eq org-ts-what 'hour) 2 5)
  11748. n dm)))
  11749. (when (integerp org-ts-what)
  11750. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11751. (if (eq what 'calendar)
  11752. (let ((cal-date (org-get-date-from-calendar)))
  11753. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11754. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11755. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11756. (setcar time0 (or (car time0) 0))
  11757. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11758. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11759. (setq time (apply 'encode-time time0))))
  11760. (setq org-last-changed-timestamp
  11761. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11762. (org-clock-update-time-maybe)
  11763. (goto-char pos)
  11764. ;; Try to recenter the calendar window, if any
  11765. (if (and org-calendar-follow-timestamp-change
  11766. (get-buffer-window "*Calendar*" t)
  11767. (memq org-ts-what '(day month year)))
  11768. (org-recenter-calendar (time-to-days time))))))
  11769. (defun org-modify-ts-extra (s pos n dm)
  11770. "Change the different parts of the lead-time and repeat fields in timestamp."
  11771. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11772. ng h m new rem)
  11773. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11774. (cond
  11775. ((or (org-pos-in-match-range pos 2)
  11776. (org-pos-in-match-range pos 3))
  11777. (setq m (string-to-number (match-string 3 s))
  11778. h (string-to-number (match-string 2 s)))
  11779. (if (org-pos-in-match-range pos 2)
  11780. (setq h (+ h n))
  11781. (setq n (* dm (org-no-warnings (signum n))))
  11782. (when (not (= 0 (setq rem (% m dm))))
  11783. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11784. (setq m (+ m n)))
  11785. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11786. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11787. (setq h (min 24 (max 0 h)))
  11788. (setq ng 1 new (format "-%02d:%02d" h m)))
  11789. ((org-pos-in-match-range pos 6)
  11790. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11791. ((org-pos-in-match-range pos 5)
  11792. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11793. ((org-pos-in-match-range pos 9)
  11794. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11795. ((org-pos-in-match-range pos 8)
  11796. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11797. (when ng
  11798. (setq s (concat
  11799. (substring s 0 (match-beginning ng))
  11800. new
  11801. (substring s (match-end ng))))))
  11802. s))
  11803. (defun org-recenter-calendar (date)
  11804. "If the calendar is visible, recenter it to DATE."
  11805. (let* ((win (selected-window))
  11806. (cwin (get-buffer-window "*Calendar*" t))
  11807. (calendar-move-hook nil))
  11808. (when cwin
  11809. (select-window cwin)
  11810. (calendar-goto-date (if (listp date) date
  11811. (calendar-gregorian-from-absolute date)))
  11812. (select-window win))))
  11813. (defun org-goto-calendar (&optional arg)
  11814. "Go to the Emacs calendar at the current date.
  11815. If there is a time stamp in the current line, go to that date.
  11816. A prefix ARG can be used to force the current date."
  11817. (interactive "P")
  11818. (let ((tsr org-ts-regexp) diff
  11819. (calendar-move-hook nil)
  11820. (calendar-view-holidays-initially-flag nil)
  11821. (view-calendar-holidays-initially nil)
  11822. (calendar-view-diary-initially-flag nil)
  11823. (view-diary-entries-initially nil))
  11824. (if (or (org-at-timestamp-p)
  11825. (save-excursion
  11826. (beginning-of-line 1)
  11827. (looking-at (concat ".*" tsr))))
  11828. (let ((d1 (time-to-days (current-time)))
  11829. (d2 (time-to-days
  11830. (org-time-string-to-time (match-string 1)))))
  11831. (setq diff (- d2 d1))))
  11832. (calendar)
  11833. (calendar-goto-today)
  11834. (if (and diff (not arg)) (calendar-forward-day diff))))
  11835. (defun org-get-date-from-calendar ()
  11836. "Return a list (month day year) of date at point in calendar."
  11837. (with-current-buffer "*Calendar*"
  11838. (save-match-data
  11839. (calendar-cursor-to-date))))
  11840. (defun org-date-from-calendar ()
  11841. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11842. If there is already a time stamp at the cursor position, update it."
  11843. (interactive)
  11844. (if (org-at-timestamp-p t)
  11845. (org-timestamp-change 0 'calendar)
  11846. (let ((cal-date (org-get-date-from-calendar)))
  11847. (org-insert-time-stamp
  11848. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11849. (defun org-minutes-to-hh:mm-string (m)
  11850. "Compute H:MM from a number of minutes."
  11851. (let ((h (/ m 60)))
  11852. (setq m (- m (* 60 h)))
  11853. (format org-time-clocksum-format h m)))
  11854. (defun org-hh:mm-string-to-minutes (s)
  11855. "Convert a string H:MM to a number of minutes."
  11856. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11857. (+ (* (string-to-number (match-string 1 s)) 60)
  11858. (string-to-number (match-string 2 s)))
  11859. 0))
  11860. ;;;; Files
  11861. (defun org-save-all-org-buffers ()
  11862. "Save all Org-mode buffers without user confirmation."
  11863. (interactive)
  11864. (message "Saving all Org-mode buffers...")
  11865. (save-some-buffers t 'org-mode-p)
  11866. (when (featurep 'org-id) (org-id-locations-save))
  11867. (message "Saving all Org-mode buffers... done"))
  11868. (defun org-revert-all-org-buffers ()
  11869. "Revert all Org-mode buffers.
  11870. Prompt for confirmation when there are unsaved changes.
  11871. Be sure you know what you are doing before letting this function
  11872. overwrite your changes.
  11873. This function is useful in a setup where one tracks org files
  11874. with a version control system, to revert on one machine after pulling
  11875. changes from another. I believe the procedure must be like this:
  11876. 1. M-x org-save-all-org-buffers
  11877. 2. Pull changes from the other machine, resolve conflicts
  11878. 3. M-x org-revert-all-org-buffers"
  11879. (interactive)
  11880. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11881. (error "Abort"))
  11882. (save-excursion
  11883. (save-window-excursion
  11884. (mapc
  11885. (lambda (b)
  11886. (when (and (with-current-buffer b (org-mode-p))
  11887. (with-current-buffer b buffer-file-name))
  11888. (switch-to-buffer b)
  11889. (revert-buffer t 'no-confirm)))
  11890. (buffer-list))
  11891. (when (and (featurep 'org-id) org-id-track-globally)
  11892. (org-id-locations-load)))))
  11893. ;;;; Agenda files
  11894. ;;;###autoload
  11895. (defun org-iswitchb (&optional arg)
  11896. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11897. With a prefix argument, restrict available to files.
  11898. With two prefix arguments, restrict available buffers to agenda files.
  11899. Due to some yet unresolved reason, the global function
  11900. `iswitchb-mode' needs to be active for this function to work."
  11901. (interactive "P")
  11902. (require 'iswitchb)
  11903. (let ((enabled iswitchb-mode) blist)
  11904. (or enabled (iswitchb-mode 1))
  11905. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11906. ((equal arg '(16)) (org-buffer-list 'agenda))
  11907. (t (org-buffer-list))))
  11908. (unwind-protect
  11909. (let ((iswitchb-make-buflist-hook
  11910. (lambda ()
  11911. (setq iswitchb-temp-buflist
  11912. (mapcar 'buffer-name blist)))))
  11913. (switch-to-buffer
  11914. (iswitchb-read-buffer
  11915. "Switch-to: " nil t))
  11916. (or enabled (iswitchb-mode -1))))))
  11917. ;;;###autoload
  11918. (defun org-ido-switchb (&optional arg)
  11919. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11920. With a prefix argument, restrict available to files.
  11921. With two prefix arguments, restrict available buffers to agenda files."
  11922. (interactive "P")
  11923. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11924. ((equal arg '(16)) (org-buffer-list 'agenda))
  11925. (t (org-buffer-list)))))
  11926. (switch-to-buffer
  11927. (org-ido-completing-read "Org buffer: "
  11928. (mapcar 'list (mapcar 'buffer-name blist))
  11929. nil t))))
  11930. (defun org-buffer-list (&optional predicate exclude-tmp)
  11931. "Return a list of Org buffers.
  11932. PREDICATE can be `export', `files' or `agenda'.
  11933. export restrict the list to Export buffers.
  11934. files restrict the list to buffers visiting Org files.
  11935. agenda restrict the list to buffers visiting agenda files.
  11936. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11937. (let* ((bfn nil)
  11938. (agenda-files (and (eq predicate 'agenda)
  11939. (mapcar 'file-truename (org-agenda-files t))))
  11940. (filter
  11941. (cond
  11942. ((eq predicate 'files)
  11943. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11944. ((eq predicate 'export)
  11945. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11946. ((eq predicate 'agenda)
  11947. (lambda (b)
  11948. (with-current-buffer b
  11949. (and (eq major-mode 'org-mode)
  11950. (setq bfn (buffer-file-name b))
  11951. (member (file-truename bfn) agenda-files)))))
  11952. (t (lambda (b) (with-current-buffer b
  11953. (or (eq major-mode 'org-mode)
  11954. (string-match "\*Org .*Export"
  11955. (buffer-name b)))))))))
  11956. (delq nil
  11957. (mapcar
  11958. (lambda(b)
  11959. (if (and (funcall filter b)
  11960. (or (not exclude-tmp)
  11961. (not (string-match "tmp" (buffer-name b)))))
  11962. b
  11963. nil))
  11964. (buffer-list)))))
  11965. (defun org-agenda-files (&optional unrestricted archives)
  11966. "Get the list of agenda files.
  11967. Optional UNRESTRICTED means return the full list even if a restriction
  11968. is currently in place.
  11969. When ARCHIVES is t, include all archive files hat are really being
  11970. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11971. `org-agenda-archives-mode' is t."
  11972. (let ((files
  11973. (cond
  11974. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11975. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11976. ((listp org-agenda-files) org-agenda-files)
  11977. (t (error "Invalid value of `org-agenda-files'")))))
  11978. (setq files (apply 'append
  11979. (mapcar (lambda (f)
  11980. (if (file-directory-p f)
  11981. (directory-files
  11982. f t org-agenda-file-regexp)
  11983. (list f)))
  11984. files)))
  11985. (when org-agenda-skip-unavailable-files
  11986. (setq files (delq nil
  11987. (mapcar (function
  11988. (lambda (file)
  11989. (and (file-readable-p file) file)))
  11990. files))))
  11991. (when (or (eq archives t)
  11992. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11993. (setq files (org-add-archive-files files)))
  11994. files))
  11995. (defun org-edit-agenda-file-list ()
  11996. "Edit the list of agenda files.
  11997. Depending on setup, this either uses customize to edit the variable
  11998. `org-agenda-files', or it visits the file that is holding the list. In the
  11999. latter case, the buffer is set up in a way that saving it automatically kills
  12000. the buffer and restores the previous window configuration."
  12001. (interactive)
  12002. (if (stringp org-agenda-files)
  12003. (let ((cw (current-window-configuration)))
  12004. (find-file org-agenda-files)
  12005. (org-set-local 'org-window-configuration cw)
  12006. (org-add-hook 'after-save-hook
  12007. (lambda ()
  12008. (set-window-configuration
  12009. (prog1 org-window-configuration
  12010. (kill-buffer (current-buffer))))
  12011. (org-install-agenda-files-menu)
  12012. (message "New agenda file list installed"))
  12013. nil 'local)
  12014. (message "%s" (substitute-command-keys
  12015. "Edit list and finish with \\[save-buffer]")))
  12016. (customize-variable 'org-agenda-files)))
  12017. (defun org-store-new-agenda-file-list (list)
  12018. "Set new value for the agenda file list and save it correctly."
  12019. (if (stringp org-agenda-files)
  12020. (let ((f org-agenda-files) b)
  12021. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12022. (with-temp-file f
  12023. (insert (mapconcat 'identity list "\n") "\n")))
  12024. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12025. (setq org-agenda-files list)
  12026. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12027. (defun org-read-agenda-file-list ()
  12028. "Read the list of agenda files from a file."
  12029. (when (file-directory-p org-agenda-files)
  12030. (error "`org-agenda-files' cannot be a single directory"))
  12031. (when (stringp org-agenda-files)
  12032. (with-temp-buffer
  12033. (insert-file-contents org-agenda-files)
  12034. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12035. ;;;###autoload
  12036. (defun org-cycle-agenda-files ()
  12037. "Cycle through the files in `org-agenda-files'.
  12038. If the current buffer visits an agenda file, find the next one in the list.
  12039. If the current buffer does not, find the first agenda file."
  12040. (interactive)
  12041. (let* ((fs (org-agenda-files t))
  12042. (files (append fs (list (car fs))))
  12043. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12044. file)
  12045. (unless files (error "No agenda files"))
  12046. (catch 'exit
  12047. (while (setq file (pop files))
  12048. (if (equal (file-truename file) tcf)
  12049. (when (car files)
  12050. (find-file (car files))
  12051. (throw 'exit t))))
  12052. (find-file (car fs)))
  12053. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12054. (defun org-agenda-file-to-front (&optional to-end)
  12055. "Move/add the current file to the top of the agenda file list.
  12056. If the file is not present in the list, it is added to the front. If it is
  12057. present, it is moved there. With optional argument TO-END, add/move to the
  12058. end of the list."
  12059. (interactive "P")
  12060. (let ((org-agenda-skip-unavailable-files nil)
  12061. (file-alist (mapcar (lambda (x)
  12062. (cons (file-truename x) x))
  12063. (org-agenda-files t)))
  12064. (ctf (file-truename buffer-file-name))
  12065. x had)
  12066. (setq x (assoc ctf file-alist) had x)
  12067. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12068. (if to-end
  12069. (setq file-alist (append (delq x file-alist) (list x)))
  12070. (setq file-alist (cons x (delq x file-alist))))
  12071. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12072. (org-install-agenda-files-menu)
  12073. (message "File %s to %s of agenda file list"
  12074. (if had "moved" "added") (if to-end "end" "front"))))
  12075. (defun org-remove-file (&optional file)
  12076. "Remove current file from the list of files in variable `org-agenda-files'.
  12077. These are the files which are being checked for agenda entries.
  12078. Optional argument FILE means, use this file instead of the current."
  12079. (interactive)
  12080. (let* ((org-agenda-skip-unavailable-files nil)
  12081. (file (or file buffer-file-name))
  12082. (true-file (file-truename file))
  12083. (afile (abbreviate-file-name file))
  12084. (files (delq nil (mapcar
  12085. (lambda (x)
  12086. (if (equal true-file
  12087. (file-truename x))
  12088. nil x))
  12089. (org-agenda-files t)))))
  12090. (if (not (= (length files) (length (org-agenda-files t))))
  12091. (progn
  12092. (org-store-new-agenda-file-list files)
  12093. (org-install-agenda-files-menu)
  12094. (message "Removed file: %s" afile))
  12095. (message "File was not in list: %s (not removed)" afile))))
  12096. (defun org-file-menu-entry (file)
  12097. (vector file (list 'find-file file) t))
  12098. (defun org-check-agenda-file (file)
  12099. "Make sure FILE exists. If not, ask user what to do."
  12100. (when (not (file-exists-p file))
  12101. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12102. (abbreviate-file-name file))
  12103. (let ((r (downcase (read-char-exclusive))))
  12104. (cond
  12105. ((equal r ?r)
  12106. (org-remove-file file)
  12107. (throw 'nextfile t))
  12108. (t (error "Abort"))))))
  12109. (defun org-get-agenda-file-buffer (file)
  12110. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12111. it to the list of buffers which might be released later."
  12112. (let ((buf (org-find-base-buffer-visiting file)))
  12113. (if buf
  12114. buf ; just return it
  12115. ;; Make a new buffer and remember it
  12116. (setq buf (find-file-noselect file))
  12117. (if buf (push buf org-agenda-new-buffers))
  12118. buf)))
  12119. (defun org-release-buffers (blist)
  12120. "Release all buffers in list, asking the user for confirmation when needed.
  12121. When a buffer is unmodified, it is just killed. When modified, it is saved
  12122. \(if the user agrees) and then killed."
  12123. (let (buf file)
  12124. (while (setq buf (pop blist))
  12125. (setq file (buffer-file-name buf))
  12126. (when (and (buffer-modified-p buf)
  12127. file
  12128. (y-or-n-p (format "Save file %s? " file)))
  12129. (with-current-buffer buf (save-buffer)))
  12130. (kill-buffer buf))))
  12131. (defun org-prepare-agenda-buffers (files)
  12132. "Create buffers for all agenda files, protect archived trees and comments."
  12133. (interactive)
  12134. (let ((pa '(:org-archived t))
  12135. (pc '(:org-comment t))
  12136. (pall '(:org-archived t :org-comment t))
  12137. (inhibit-read-only t)
  12138. (rea (concat ":" org-archive-tag ":"))
  12139. bmp file re)
  12140. (save-excursion
  12141. (save-restriction
  12142. (while (setq file (pop files))
  12143. (if (bufferp file)
  12144. (set-buffer file)
  12145. (org-check-agenda-file file)
  12146. (set-buffer (org-get-agenda-file-buffer file)))
  12147. (widen)
  12148. (setq bmp (buffer-modified-p))
  12149. (org-refresh-category-properties)
  12150. (setq org-todo-keywords-for-agenda
  12151. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12152. (setq org-done-keywords-for-agenda
  12153. (append org-done-keywords-for-agenda org-done-keywords))
  12154. (setq org-todo-keyword-alist-for-agenda
  12155. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12156. (setq org-tag-alist-for-agenda
  12157. (append org-tag-alist-for-agenda org-tag-alist))
  12158. (save-excursion
  12159. (remove-text-properties (point-min) (point-max) pall)
  12160. (when org-agenda-skip-archived-trees
  12161. (goto-char (point-min))
  12162. (while (re-search-forward rea nil t)
  12163. (if (org-on-heading-p t)
  12164. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12165. (goto-char (point-min))
  12166. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12167. (while (re-search-forward re nil t)
  12168. (add-text-properties
  12169. (match-beginning 0) (org-end-of-subtree t) pc)))
  12170. (set-buffer-modified-p bmp))))
  12171. (setq org-todo-keyword-alist-for-agenda
  12172. (org-uniquify org-todo-keyword-alist-for-agenda)
  12173. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12174. ;;;; Embedded LaTeX
  12175. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12176. "Keymap for the minor `org-cdlatex-mode'.")
  12177. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12178. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12179. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12180. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12181. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12182. (defvar org-cdlatex-texmathp-advice-is-done nil
  12183. "Flag remembering if we have applied the advice to texmathp already.")
  12184. (define-minor-mode org-cdlatex-mode
  12185. "Toggle the minor `org-cdlatex-mode'.
  12186. This mode supports entering LaTeX environment and math in LaTeX fragments
  12187. in Org-mode.
  12188. \\{org-cdlatex-mode-map}"
  12189. nil " OCDL" nil
  12190. (when org-cdlatex-mode (require 'cdlatex))
  12191. (unless org-cdlatex-texmathp-advice-is-done
  12192. (setq org-cdlatex-texmathp-advice-is-done t)
  12193. (defadvice texmathp (around org-math-always-on activate)
  12194. "Always return t in org-mode buffers.
  12195. This is because we want to insert math symbols without dollars even outside
  12196. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12197. an embedded LaTeX fragment, let texmathp do its job.
  12198. \\[org-cdlatex-mode-map]"
  12199. (interactive)
  12200. (let (p)
  12201. (cond
  12202. ((not (org-mode-p)) ad-do-it)
  12203. ((eq this-command 'cdlatex-math-symbol)
  12204. (setq ad-return-value t
  12205. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12206. (t
  12207. (let ((p (org-inside-LaTeX-fragment-p)))
  12208. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12209. (setq ad-return-value t
  12210. texmathp-why '("Org-mode embedded math" . 0))
  12211. (if p ad-do-it)))))))))
  12212. (defun turn-on-org-cdlatex ()
  12213. "Unconditionally turn on `org-cdlatex-mode'."
  12214. (org-cdlatex-mode 1))
  12215. (defun org-inside-LaTeX-fragment-p ()
  12216. "Test if point is inside a LaTeX fragment.
  12217. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12218. sequence appearing also before point.
  12219. Even though the matchers for math are configurable, this function assumes
  12220. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12221. delimiters are skipped when they have been removed by customization.
  12222. The return value is nil, or a cons cell with the delimiter and
  12223. and the position of this delimiter.
  12224. This function does a reasonably good job, but can locally be fooled by
  12225. for example currency specifications. For example it will assume being in
  12226. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12227. fragments that are properly closed, but during editing, we have to live
  12228. with the uncertainty caused by missing closing delimiters. This function
  12229. looks only before point, not after."
  12230. (catch 'exit
  12231. (let ((pos (point))
  12232. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12233. (lim (progn
  12234. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12235. (point)))
  12236. dd-on str (start 0) m re)
  12237. (goto-char pos)
  12238. (when dodollar
  12239. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12240. re (nth 1 (assoc "$" org-latex-regexps)))
  12241. (while (string-match re str start)
  12242. (cond
  12243. ((= (match-end 0) (length str))
  12244. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12245. ((= (match-end 0) (- (length str) 5))
  12246. (throw 'exit nil))
  12247. (t (setq start (match-end 0))))))
  12248. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12249. (goto-char pos)
  12250. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12251. (and (match-beginning 2) (throw 'exit nil))
  12252. ;; count $$
  12253. (while (re-search-backward "\\$\\$" lim t)
  12254. (setq dd-on (not dd-on)))
  12255. (goto-char pos)
  12256. (if dd-on (cons "$$" m))))))
  12257. (defun org-try-cdlatex-tab ()
  12258. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12259. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12260. - inside a LaTeX fragment, or
  12261. - after the first word in a line, where an abbreviation expansion could
  12262. insert a LaTeX environment."
  12263. (when org-cdlatex-mode
  12264. (cond
  12265. ((save-excursion
  12266. (skip-chars-backward "a-zA-Z0-9*")
  12267. (skip-chars-backward " \t")
  12268. (bolp))
  12269. (cdlatex-tab) t)
  12270. ((org-inside-LaTeX-fragment-p)
  12271. (cdlatex-tab) t)
  12272. (t nil))))
  12273. (defun org-cdlatex-underscore-caret (&optional arg)
  12274. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12275. Revert to the normal definition outside of these fragments."
  12276. (interactive "P")
  12277. (if (org-inside-LaTeX-fragment-p)
  12278. (call-interactively 'cdlatex-sub-superscript)
  12279. (let (org-cdlatex-mode)
  12280. (call-interactively (key-binding (vector last-input-event))))))
  12281. (defun org-cdlatex-math-modify (&optional arg)
  12282. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12283. Revert to the normal definition outside of these fragments."
  12284. (interactive "P")
  12285. (if (org-inside-LaTeX-fragment-p)
  12286. (call-interactively 'cdlatex-math-modify)
  12287. (let (org-cdlatex-mode)
  12288. (call-interactively (key-binding (vector last-input-event))))))
  12289. (defvar org-latex-fragment-image-overlays nil
  12290. "List of overlays carrying the images of latex fragments.")
  12291. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12292. (defun org-remove-latex-fragment-image-overlays ()
  12293. "Remove all overlays with LaTeX fragment images in current buffer."
  12294. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12295. (setq org-latex-fragment-image-overlays nil))
  12296. (defun org-preview-latex-fragment (&optional subtree)
  12297. "Preview the LaTeX fragment at point, or all locally or globally.
  12298. If the cursor is in a LaTeX fragment, create the image and overlay
  12299. it over the source code. If there is no fragment at point, display
  12300. all fragments in the current text, from one headline to the next. With
  12301. prefix SUBTREE, display all fragments in the current subtree. With a
  12302. double prefix `C-u C-u', or when the cursor is before the first headline,
  12303. display all fragments in the buffer.
  12304. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12305. (interactive "P")
  12306. (org-remove-latex-fragment-image-overlays)
  12307. (save-excursion
  12308. (save-restriction
  12309. (let (beg end at msg)
  12310. (cond
  12311. ((or (equal subtree '(16))
  12312. (not (save-excursion
  12313. (re-search-backward (concat "^" outline-regexp) nil t))))
  12314. (setq beg (point-min) end (point-max)
  12315. msg "Creating images for buffer...%s"))
  12316. ((equal subtree '(4))
  12317. (org-back-to-heading)
  12318. (setq beg (point) end (org-end-of-subtree t)
  12319. msg "Creating images for subtree...%s"))
  12320. (t
  12321. (if (setq at (org-inside-LaTeX-fragment-p))
  12322. (goto-char (max (point-min) (- (cdr at) 2)))
  12323. (org-back-to-heading))
  12324. (setq beg (point) end (progn (outline-next-heading) (point))
  12325. msg (if at "Creating image...%s"
  12326. "Creating images for entry...%s"))))
  12327. (message msg "")
  12328. (narrow-to-region beg end)
  12329. (goto-char beg)
  12330. (org-format-latex
  12331. (concat "ltxpng/" (file-name-sans-extension
  12332. (file-name-nondirectory
  12333. buffer-file-name)))
  12334. default-directory 'overlays msg at 'forbuffer)
  12335. (message msg "done. Use `C-c C-c' to remove images.")))))
  12336. (defvar org-latex-regexps
  12337. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12338. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12339. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12340. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12341. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12342. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12343. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12344. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12345. "Regular expressions for matching embedded LaTeX.")
  12346. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12347. "Replace LaTeX fragments with links to an image, and produce images."
  12348. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12349. (let* ((prefixnodir (file-name-nondirectory prefix))
  12350. (absprefix (expand-file-name prefix dir))
  12351. (todir (file-name-directory absprefix))
  12352. (opt org-format-latex-options)
  12353. (matchers (plist-get opt :matchers))
  12354. (re-list org-latex-regexps)
  12355. (cnt 0) txt link beg end re e checkdir
  12356. m n block linkfile movefile ov)
  12357. ;; Check if there are old images files with this prefix, and remove them
  12358. (when (file-directory-p todir)
  12359. (mapc 'delete-file
  12360. (directory-files
  12361. todir 'full
  12362. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12363. ;; Check the different regular expressions
  12364. (while (setq e (pop re-list))
  12365. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12366. block (if (nth 3 e) "\n\n" ""))
  12367. (when (member m matchers)
  12368. (goto-char (point-min))
  12369. (while (re-search-forward re nil t)
  12370. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12371. (not (get-text-property (match-beginning n)
  12372. 'org-protected)))
  12373. (setq txt (match-string n)
  12374. beg (match-beginning n) end (match-end n)
  12375. cnt (1+ cnt)
  12376. linkfile (format "%s_%04d.png" prefix cnt)
  12377. movefile (format "%s_%04d.png" absprefix cnt)
  12378. link (concat block "[[file:" linkfile "]]" block))
  12379. (if msg (message msg cnt))
  12380. (goto-char beg)
  12381. (unless checkdir ; make sure the directory exists
  12382. (setq checkdir t)
  12383. (or (file-directory-p todir) (make-directory todir)))
  12384. (org-create-formula-image
  12385. txt movefile opt forbuffer)
  12386. (if overlays
  12387. (progn
  12388. (setq ov (org-make-overlay beg end))
  12389. (if (featurep 'xemacs)
  12390. (progn
  12391. (org-overlay-put ov 'invisible t)
  12392. (org-overlay-put
  12393. ov 'end-glyph
  12394. (make-glyph (vector 'png :file movefile))))
  12395. (org-overlay-put
  12396. ov 'display
  12397. (list 'image :type 'png :file movefile :ascent 'center)))
  12398. (push ov org-latex-fragment-image-overlays)
  12399. (goto-char end))
  12400. (delete-region beg end)
  12401. (insert link))))))))
  12402. ;; This function borrows from Ganesh Swami's latex2png.el
  12403. (defun org-create-formula-image (string tofile options buffer)
  12404. (let* ((tmpdir (if (featurep 'xemacs)
  12405. (temp-directory)
  12406. temporary-file-directory))
  12407. (texfilebase (make-temp-name
  12408. (expand-file-name "orgtex" tmpdir)))
  12409. (texfile (concat texfilebase ".tex"))
  12410. (dvifile (concat texfilebase ".dvi"))
  12411. (pngfile (concat texfilebase ".png"))
  12412. (fnh (if (featurep 'xemacs)
  12413. (font-height (get-face-font 'default))
  12414. (face-attribute 'default :height nil)))
  12415. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12416. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12417. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12418. "Black"))
  12419. (bg (or (plist-get options (if buffer :background :html-background))
  12420. "Transparent")))
  12421. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12422. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12423. (with-temp-file texfile
  12424. (insert org-format-latex-header
  12425. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12426. (let ((dir default-directory))
  12427. (condition-case nil
  12428. (progn
  12429. (cd tmpdir)
  12430. (call-process "latex" nil nil nil texfile))
  12431. (error nil))
  12432. (cd dir))
  12433. (if (not (file-exists-p dvifile))
  12434. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12435. (condition-case nil
  12436. (call-process "dvipng" nil nil nil
  12437. "-E" "-fg" fg "-bg" bg
  12438. "-D" dpi
  12439. ;;"-x" scale "-y" scale
  12440. "-T" "tight"
  12441. "-o" pngfile
  12442. dvifile)
  12443. (error nil))
  12444. (if (not (file-exists-p pngfile))
  12445. (progn (message "Failed to create png file from %s" texfile) nil)
  12446. ;; Use the requested file name and clean up
  12447. (copy-file pngfile tofile 'replace)
  12448. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12449. (delete-file (concat texfilebase e)))
  12450. pngfile))))
  12451. (defun org-dvipng-color (attr)
  12452. "Return an rgb color specification for dvipng."
  12453. (apply 'format "rgb %s %s %s"
  12454. (mapcar 'org-normalize-color
  12455. (color-values (face-attribute 'default attr nil)))))
  12456. (defun org-normalize-color (value)
  12457. "Return string to be used as color value for an RGB component."
  12458. (format "%g" (/ value 65535.0)))
  12459. ;;;; Key bindings
  12460. ;; Make `C-c C-x' a prefix key
  12461. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12462. ;; TAB key with modifiers
  12463. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12464. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12465. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12466. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12467. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12468. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12469. ;; The following line is necessary under Suse GNU/Linux
  12470. (unless (featurep 'xemacs)
  12471. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12472. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12473. (define-key org-mode-map [backtab] 'org-shifttab)
  12474. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12475. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12476. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12477. ;; Cursor keys with modifiers
  12478. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12479. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12480. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12481. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12482. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12483. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12484. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12485. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12486. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12487. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12488. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12489. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12490. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12491. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12492. ;;; Extra keys for tty access.
  12493. ;; We only set them when really needed because otherwise the
  12494. ;; menus don't show the simple keys
  12495. (when (or org-use-extra-keys
  12496. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12497. (not window-system))
  12498. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12499. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12500. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12501. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12502. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12503. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12504. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12505. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12506. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12507. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12508. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12509. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12510. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12511. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12512. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12513. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12514. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12515. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12516. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12517. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12518. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12519. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12520. ;; All the other keys
  12521. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12522. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12523. (if (boundp 'narrow-map)
  12524. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12525. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12526. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12527. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12528. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12529. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12530. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12531. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12532. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12533. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12534. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12535. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12536. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12537. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12538. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12539. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12540. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12541. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12542. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12543. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12544. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12545. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12546. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12547. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12548. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12549. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12550. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12551. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12552. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12553. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12554. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12555. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12556. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12557. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12558. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12559. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12560. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12561. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12562. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12563. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12564. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12565. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12566. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12567. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12568. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12569. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12570. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12571. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12572. (org-defkey org-mode-map "\C-m" 'org-return)
  12573. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12574. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12575. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12576. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12577. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12578. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12579. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12580. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12581. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12582. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12583. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12584. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12585. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12586. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12587. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12588. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12589. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12590. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12591. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12592. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12593. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12594. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12595. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12596. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12597. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12598. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12599. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12600. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12601. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12602. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12603. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12604. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12605. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12606. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12607. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12608. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12609. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12610. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12611. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12612. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12613. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12614. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12615. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12616. (when (featurep 'xemacs)
  12617. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12618. (defvar org-self-insert-command-undo-counter 0)
  12619. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12620. (defun org-self-insert-command (N)
  12621. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12622. If the cursor is in a table looking at whitespace, the whitespace is
  12623. overwritten, and the table is not marked as requiring realignment."
  12624. (interactive "p")
  12625. (if (and
  12626. (org-table-p)
  12627. (progn
  12628. ;; check if we blank the field, and if that triggers align
  12629. (and (featurep 'org-table) org-table-auto-blank-field
  12630. (member last-command
  12631. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12632. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12633. ;; got extra space, this field does not determine column width
  12634. (let (org-table-may-need-update) (org-table-blank-field))
  12635. ;; no extra space, this field may determine column width
  12636. (org-table-blank-field)))
  12637. t)
  12638. (eq N 1)
  12639. (looking-at "[^|\n]* |"))
  12640. (let (org-table-may-need-update)
  12641. (goto-char (1- (match-end 0)))
  12642. (delete-backward-char 1)
  12643. (goto-char (match-beginning 0))
  12644. (self-insert-command N))
  12645. (setq org-table-may-need-update t)
  12646. (self-insert-command N)
  12647. (org-fix-tags-on-the-fly)
  12648. (if org-self-insert-cluster-for-undo
  12649. (if (not (eq last-command 'org-self-insert-command))
  12650. (setq org-self-insert-command-undo-counter 1)
  12651. (if (>= org-self-insert-command-undo-counter 20)
  12652. (setq org-self-insert-command-undo-counter 1)
  12653. (and (> org-self-insert-command-undo-counter 0)
  12654. buffer-undo-list
  12655. (not (cadr buffer-undo-list)) ; remove nil entry
  12656. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12657. (setq org-self-insert-command-undo-counter
  12658. (1+ org-self-insert-command-undo-counter)))))))
  12659. (defun org-fix-tags-on-the-fly ()
  12660. (when (and (equal (char-after (point-at-bol)) ?*)
  12661. (org-on-heading-p))
  12662. (org-align-tags-here org-tags-column)))
  12663. (defun org-delete-backward-char (N)
  12664. "Like `delete-backward-char', insert whitespace at field end in tables.
  12665. When deleting backwards, in tables this function will insert whitespace in
  12666. front of the next \"|\" separator, to keep the table aligned. The table will
  12667. still be marked for re-alignment if the field did fill the entire column,
  12668. because, in this case the deletion might narrow the column."
  12669. (interactive "p")
  12670. (if (and (org-table-p)
  12671. (eq N 1)
  12672. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12673. (looking-at ".*?|"))
  12674. (let ((pos (point))
  12675. (noalign (looking-at "[^|\n\r]* |"))
  12676. (c org-table-may-need-update))
  12677. (backward-delete-char N)
  12678. (skip-chars-forward "^|")
  12679. (insert " ")
  12680. (goto-char (1- pos))
  12681. ;; noalign: if there were two spaces at the end, this field
  12682. ;; does not determine the width of the column.
  12683. (if noalign (setq org-table-may-need-update c)))
  12684. (backward-delete-char N)
  12685. (org-fix-tags-on-the-fly)))
  12686. (defun org-delete-char (N)
  12687. "Like `delete-char', but insert whitespace at field end in tables.
  12688. When deleting characters, in tables this function will insert whitespace in
  12689. front of the next \"|\" separator, to keep the table aligned. The table will
  12690. still be marked for re-alignment if the field did fill the entire column,
  12691. because, in this case the deletion might narrow the column."
  12692. (interactive "p")
  12693. (if (and (org-table-p)
  12694. (not (bolp))
  12695. (not (= (char-after) ?|))
  12696. (eq N 1))
  12697. (if (looking-at ".*?|")
  12698. (let ((pos (point))
  12699. (noalign (looking-at "[^|\n\r]* |"))
  12700. (c org-table-may-need-update))
  12701. (replace-match (concat
  12702. (substring (match-string 0) 1 -1)
  12703. " |"))
  12704. (goto-char pos)
  12705. ;; noalign: if there were two spaces at the end, this field
  12706. ;; does not determine the width of the column.
  12707. (if noalign (setq org-table-may-need-update c)))
  12708. (delete-char N))
  12709. (delete-char N)
  12710. (org-fix-tags-on-the-fly)))
  12711. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12712. (put 'org-self-insert-command 'delete-selection t)
  12713. (put 'orgtbl-self-insert-command 'delete-selection t)
  12714. (put 'org-delete-char 'delete-selection 'supersede)
  12715. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12716. (put 'org-yank 'delete-selection 'yank)
  12717. ;; Make `flyspell-mode' delay after some commands
  12718. (put 'org-self-insert-command 'flyspell-delayed t)
  12719. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12720. (put 'org-delete-char 'flyspell-delayed t)
  12721. (put 'org-delete-backward-char 'flyspell-delayed t)
  12722. ;; Make pabbrev-mode expand after org-mode commands
  12723. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12724. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12725. ;; How to do this: Measure non-white length of current string
  12726. ;; If equal to column width, we should realign.
  12727. (defun org-remap (map &rest commands)
  12728. "In MAP, remap the functions given in COMMANDS.
  12729. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12730. (let (new old)
  12731. (while commands
  12732. (setq old (pop commands) new (pop commands))
  12733. (if (fboundp 'command-remapping)
  12734. (org-defkey map (vector 'remap old) new)
  12735. (substitute-key-definition old new map global-map)))))
  12736. (when (eq org-enable-table-editor 'optimized)
  12737. ;; If the user wants maximum table support, we need to hijack
  12738. ;; some standard editing functions
  12739. (org-remap org-mode-map
  12740. 'self-insert-command 'org-self-insert-command
  12741. 'delete-char 'org-delete-char
  12742. 'delete-backward-char 'org-delete-backward-char)
  12743. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12744. (defvar org-ctrl-c-ctrl-c-hook nil
  12745. "Hook for functions attaching themselves to `C-c C-c'.
  12746. This can be used to add additional functionality to the C-c C-c key which
  12747. executes context-dependent commands.
  12748. Each function will be called with no arguments. The function must check
  12749. if the context is appropriate for it to act. If yes, it should do its
  12750. thing and then return a non-nil value. If the context is wrong,
  12751. just do nothing.")
  12752. (defvar org-metaleft-hook nil
  12753. "Hook for functions attaching themselves to `M-left'.
  12754. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12755. (defvar org-metaright-hook nil
  12756. "Hook for functions attaching themselves to `M-right'.
  12757. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12758. (defvar org-metaup-hook nil
  12759. "Hook for functions attaching themselves to `M-up'.
  12760. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12761. (defvar org-metadown-hook nil
  12762. "Hook for functions attaching themselves to `M-down'.
  12763. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12764. (defvar org-shiftmetaleft-hook nil
  12765. "Hook for functions attaching themselves to `M-S-left'.
  12766. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12767. (defvar org-shiftmetaright-hook nil
  12768. "Hook for functions attaching themselves to `M-S-right'.
  12769. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12770. (defvar org-shiftmetaup-hook nil
  12771. "Hook for functions attaching themselves to `M-S-up'.
  12772. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12773. (defvar org-shiftmetadown-hook nil
  12774. "Hook for functions attaching themselves to `M-S-down'.
  12775. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12776. (defvar org-metareturn-hook nil
  12777. "Hook for functions attaching themselves to `M-RET'.
  12778. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12779. (defun org-modifier-cursor-error ()
  12780. "Throw an error, a modified cursor command was applied in wrong context."
  12781. (error "This command is active in special context like tables, headlines or items"))
  12782. (defun org-shiftselect-error ()
  12783. "Throw an error because Shift-Cursor command was applied in wrong context."
  12784. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12785. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12786. (error "This command works only in special context like headlines or timestamps.")))
  12787. (defun org-call-for-shift-select (cmd)
  12788. (let ((this-command-keys-shift-translated t))
  12789. (call-interactively cmd)))
  12790. (defun org-shifttab (&optional arg)
  12791. "Global visibility cycling or move to previous table field.
  12792. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12793. on context.
  12794. See the individual commands for more information."
  12795. (interactive "P")
  12796. (cond
  12797. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12798. ((integerp arg)
  12799. (message "Content view to level: %d" arg)
  12800. (org-content (prefix-numeric-value arg))
  12801. (setq org-cycle-global-status 'overview))
  12802. (t (call-interactively 'org-global-cycle))))
  12803. (defun org-shiftmetaleft ()
  12804. "Promote subtree or delete table column.
  12805. Calls `org-promote-subtree', `org-outdent-item',
  12806. or `org-table-delete-column', depending on context.
  12807. See the individual commands for more information."
  12808. (interactive)
  12809. (cond
  12810. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12811. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12812. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12813. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12814. (t (org-modifier-cursor-error))))
  12815. (defun org-shiftmetaright ()
  12816. "Demote subtree or insert table column.
  12817. Calls `org-demote-subtree', `org-indent-item',
  12818. or `org-table-insert-column', depending on context.
  12819. See the individual commands for more information."
  12820. (interactive)
  12821. (cond
  12822. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12823. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12824. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12825. ((org-at-item-p) (call-interactively 'org-indent-item))
  12826. (t (org-modifier-cursor-error))))
  12827. (defun org-shiftmetaup (&optional arg)
  12828. "Move subtree up or kill table row.
  12829. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12830. `org-move-item-up' depending on context. See the individual commands
  12831. for more information."
  12832. (interactive "P")
  12833. (cond
  12834. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12835. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12836. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12837. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12838. (t (org-modifier-cursor-error))))
  12839. (defun org-shiftmetadown (&optional arg)
  12840. "Move subtree down or insert table row.
  12841. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12842. `org-move-item-down', depending on context. See the individual
  12843. commands for more information."
  12844. (interactive "P")
  12845. (cond
  12846. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12847. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12848. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12849. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12850. (t (org-modifier-cursor-error))))
  12851. (defun org-metaleft (&optional arg)
  12852. "Promote heading or move table column to left.
  12853. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12854. With no specific context, calls the Emacs default `backward-word'.
  12855. See the individual commands for more information."
  12856. (interactive "P")
  12857. (cond
  12858. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12859. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12860. ((or (org-on-heading-p)
  12861. (and (org-region-active-p)
  12862. (save-excursion
  12863. (goto-char (region-beginning))
  12864. (org-on-heading-p))))
  12865. (call-interactively 'org-do-promote))
  12866. ((or (org-at-item-p)
  12867. (and (org-region-active-p)
  12868. (save-excursion
  12869. (goto-char (region-beginning))
  12870. (org-at-item-p))))
  12871. (call-interactively 'org-outdent-item))
  12872. (t (call-interactively 'backward-word))))
  12873. (defun org-metaright (&optional arg)
  12874. "Demote subtree or move table column to right.
  12875. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12876. With no specific context, calls the Emacs default `forward-word'.
  12877. See the individual commands for more information."
  12878. (interactive "P")
  12879. (cond
  12880. ((run-hook-with-args-until-success 'org-metaright-hook))
  12881. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12882. ((or (org-on-heading-p)
  12883. (and (org-region-active-p)
  12884. (save-excursion
  12885. (goto-char (region-beginning))
  12886. (org-on-heading-p))))
  12887. (call-interactively 'org-do-demote))
  12888. ((or (org-at-item-p)
  12889. (and (org-region-active-p)
  12890. (save-excursion
  12891. (goto-char (region-beginning))
  12892. (org-at-item-p))))
  12893. (call-interactively 'org-indent-item))
  12894. (t (call-interactively 'forward-word))))
  12895. (defun org-metaup (&optional arg)
  12896. "Move subtree up or move table row up.
  12897. Calls `org-move-subtree-up' or `org-table-move-row' or
  12898. `org-move-item-up', depending on context. See the individual commands
  12899. for more information."
  12900. (interactive "P")
  12901. (cond
  12902. ((run-hook-with-args-until-success 'org-metaup-hook))
  12903. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12904. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12905. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12906. (t (transpose-lines 1) (beginning-of-line -1))))
  12907. (defun org-metadown (&optional arg)
  12908. "Move subtree down or move table row down.
  12909. Calls `org-move-subtree-down' or `org-table-move-row' or
  12910. `org-move-item-down', depending on context. See the individual
  12911. commands for more information."
  12912. (interactive "P")
  12913. (cond
  12914. ((run-hook-with-args-until-success 'org-metadown-hook))
  12915. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12916. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12917. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12918. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12919. (defun org-shiftup (&optional arg)
  12920. "Increase item in timestamp or increase priority of current headline.
  12921. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12922. depending on context. See the individual commands for more information."
  12923. (interactive "P")
  12924. (cond
  12925. ((and org-support-shift-select (org-region-active-p))
  12926. (org-call-for-shift-select 'previous-line))
  12927. ((org-at-timestamp-p t)
  12928. (call-interactively (if org-edit-timestamp-down-means-later
  12929. 'org-timestamp-down 'org-timestamp-up)))
  12930. ((and (not (eq org-support-shift-select 'always))
  12931. (org-on-heading-p))
  12932. (call-interactively 'org-priority-up))
  12933. ((and (not org-support-shift-select) (org-at-item-p))
  12934. (call-interactively 'org-previous-item))
  12935. ((org-clocktable-try-shift 'up arg))
  12936. (org-support-shift-select
  12937. (org-call-for-shift-select 'previous-line))
  12938. (t (org-shiftselect-error))))
  12939. (defun org-shiftdown (&optional arg)
  12940. "Decrease item in timestamp or decrease priority of current headline.
  12941. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12942. depending on context. See the individual commands for more information."
  12943. (interactive "P")
  12944. (cond
  12945. ((and org-support-shift-select (org-region-active-p))
  12946. (org-call-for-shift-select 'next-line))
  12947. ((org-at-timestamp-p t)
  12948. (call-interactively (if org-edit-timestamp-down-means-later
  12949. 'org-timestamp-up 'org-timestamp-down)))
  12950. ((and (not (eq org-support-shift-select 'always))
  12951. (org-on-heading-p))
  12952. (call-interactively 'org-priority-down))
  12953. ((and (not org-support-shift-select) (org-at-item-p))
  12954. (call-interactively 'org-next-item))
  12955. ((org-clocktable-try-shift 'down arg))
  12956. (org-support-shift-select
  12957. (org-call-for-shift-select 'next-line))
  12958. (t (org-shiftselect-error))))
  12959. (defun org-shiftright (&optional arg)
  12960. "Cycle the thing at point or in the current line, depending on context.
  12961. Depending on context, this does one of the following:
  12962. - switch a timestamp at point one day into the future
  12963. - on a headline, switch to the next TODO keyword.
  12964. - on an item, switch entire list to the next bullet type
  12965. - on a property line, switch to the next allowed value
  12966. - on a clocktable definition line, move time block into the future"
  12967. (interactive "P")
  12968. (cond
  12969. ((and org-support-shift-select (org-region-active-p))
  12970. (org-call-for-shift-select 'forward-char))
  12971. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12972. ((and (not (eq org-support-shift-select 'always))
  12973. (org-on-heading-p))
  12974. (org-call-with-arg 'org-todo 'right))
  12975. ((or (and org-support-shift-select
  12976. (not (eq org-support-shift-select 'always))
  12977. (org-at-item-bullet-p))
  12978. (and (not org-support-shift-select) (org-at-item-p)))
  12979. (org-call-with-arg 'org-cycle-list-bullet nil))
  12980. ((and (not (eq org-support-shift-select 'always))
  12981. (org-at-property-p))
  12982. (call-interactively 'org-property-next-allowed-value))
  12983. ((org-clocktable-try-shift 'right arg))
  12984. (org-support-shift-select
  12985. (org-call-for-shift-select 'forward-char))
  12986. (t (org-shiftselect-error))))
  12987. (defun org-shiftleft (&optional arg)
  12988. "Cycle the thing at point or in the current line, depending on context.
  12989. Depending on context, this does one of the following:
  12990. - switch a timestamp at point one day into the past
  12991. - on a headline, switch to the previous TODO keyword.
  12992. - on an item, switch entire list to the previous bullet type
  12993. - on a property line, switch to the previous allowed value
  12994. - on a clocktable definition line, move time block into the past"
  12995. (interactive "P")
  12996. (cond
  12997. ((and org-support-shift-select (org-region-active-p))
  12998. (org-call-for-shift-select 'backward-char))
  12999. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13000. ((and (not (eq org-support-shift-select 'always))
  13001. (org-on-heading-p))
  13002. (org-call-with-arg 'org-todo 'left))
  13003. ((or (and org-support-shift-select
  13004. (not (eq org-support-shift-select 'always))
  13005. (org-at-item-bullet-p))
  13006. (and (not org-support-shift-select) (org-at-item-p)))
  13007. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13008. ((and (not (eq org-support-shift-select 'always))
  13009. (org-at-property-p))
  13010. (call-interactively 'org-property-previous-allowed-value))
  13011. ((org-clocktable-try-shift 'left arg))
  13012. (org-support-shift-select
  13013. (org-call-for-shift-select 'backward-char))
  13014. (t (org-shiftselect-error))))
  13015. (defun org-shiftcontrolright ()
  13016. "Switch to next TODO set."
  13017. (interactive)
  13018. (cond
  13019. ((and org-support-shift-select (org-region-active-p))
  13020. (org-call-for-shift-select 'forward-word))
  13021. ((and (not (eq org-support-shift-select 'always))
  13022. (org-on-heading-p))
  13023. (org-call-with-arg 'org-todo 'nextset))
  13024. (org-support-shift-select
  13025. (org-call-for-shift-select 'forward-word))
  13026. (t (org-shiftselect-error))))
  13027. (defun org-shiftcontrolleft ()
  13028. "Switch to previous TODO set."
  13029. (interactive)
  13030. (cond
  13031. ((and org-support-shift-select (org-region-active-p))
  13032. (org-call-for-shift-select 'backward-word))
  13033. ((and (not (eq org-support-shift-select 'always))
  13034. (org-on-heading-p))
  13035. (org-call-with-arg 'org-todo 'previousset))
  13036. (org-support-shift-select
  13037. (org-call-for-shift-select 'backward-word))
  13038. (t (org-shiftselect-error))))
  13039. (defun org-ctrl-c-ret ()
  13040. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13041. (interactive)
  13042. (cond
  13043. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13044. (t (call-interactively 'org-insert-heading))))
  13045. (defun org-copy-special ()
  13046. "Copy region in table or copy current subtree.
  13047. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13048. See the individual commands for more information."
  13049. (interactive)
  13050. (call-interactively
  13051. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13052. (defun org-cut-special ()
  13053. "Cut region in table or cut current subtree.
  13054. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13055. See the individual commands for more information."
  13056. (interactive)
  13057. (call-interactively
  13058. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13059. (defun org-paste-special (arg)
  13060. "Paste rectangular region into table, or past subtree relative to level.
  13061. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13062. See the individual commands for more information."
  13063. (interactive "P")
  13064. (if (org-at-table-p)
  13065. (org-table-paste-rectangle)
  13066. (org-paste-subtree arg)))
  13067. (defun org-edit-special ()
  13068. "Call a special editor for the stuff at point.
  13069. When at a table, call the formula editor with `org-table-edit-formulas'.
  13070. When at the first line of an src example, call `org-edit-src-code'.
  13071. When in an #+include line, visit the include file. Otherwise call
  13072. `ffap' to visit the file at point."
  13073. (interactive)
  13074. (cond
  13075. ((org-at-table-p)
  13076. (call-interactively 'org-table-edit-formulas))
  13077. ((save-excursion
  13078. (beginning-of-line 1)
  13079. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13080. (find-file (org-trim (match-string 1))))
  13081. ((org-edit-src-code))
  13082. ((org-edit-fixed-width-region))
  13083. (t (call-interactively 'ffap))))
  13084. (defun org-ctrl-c-ctrl-c (&optional arg)
  13085. "Set tags in headline, or update according to changed information at point.
  13086. This command does many different things, depending on context:
  13087. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13088. this is what we do.
  13089. - If the cursor is in a headline, prompt for tags and insert them
  13090. into the current line, aligned to `org-tags-column'. When called
  13091. with prefix arg, realign all tags in the current buffer.
  13092. - If the cursor is in one of the special #+KEYWORD lines, this
  13093. triggers scanning the buffer for these lines and updating the
  13094. information.
  13095. - If the cursor is inside a table, realign the table. This command
  13096. works even if the automatic table editor has been turned off.
  13097. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13098. the entire table.
  13099. - If the cursor is at a footnote reference or definition, jump to
  13100. the corresponding definition or references, respectively.
  13101. - If the cursor is a the beginning of a dynamic block, update it.
  13102. - If the cursor is inside a table created by the table.el package,
  13103. activate that table.
  13104. - If the current buffer is a remember buffer, close note and file
  13105. it. A prefix argument of 1 files to the default location
  13106. without further interaction. A prefix argument of 2 files to
  13107. the currently clocking task.
  13108. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13109. links in this buffer.
  13110. - If the cursor is on a numbered item in a plain list, renumber the
  13111. ordered list.
  13112. - If the cursor is on a checkbox, toggle it."
  13113. (interactive "P")
  13114. (let ((org-enable-table-editor t))
  13115. (cond
  13116. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13117. org-occur-highlights
  13118. org-latex-fragment-image-overlays)
  13119. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13120. (org-remove-occur-highlights)
  13121. (org-remove-latex-fragment-image-overlays)
  13122. (message "Temporary highlights/overlays removed from current buffer"))
  13123. ((and (local-variable-p 'org-finish-function (current-buffer))
  13124. (fboundp org-finish-function))
  13125. (funcall org-finish-function))
  13126. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13127. ((org-at-property-p)
  13128. (call-interactively 'org-property-action))
  13129. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13130. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13131. ((org-at-table.el-p)
  13132. (require 'table)
  13133. (beginning-of-line 1)
  13134. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13135. (call-interactively 'table-recognize-table))
  13136. ((org-at-table-p)
  13137. (org-table-maybe-eval-formula)
  13138. (if arg
  13139. (call-interactively 'org-table-recalculate)
  13140. (org-table-maybe-recalculate-line))
  13141. (call-interactively 'org-table-align))
  13142. ((or (org-footnote-at-reference-p)
  13143. (org-footnote-at-definition-p))
  13144. (call-interactively 'org-footnote-action))
  13145. ((org-at-item-checkbox-p)
  13146. (call-interactively 'org-toggle-checkbox))
  13147. ((org-at-item-p)
  13148. (if arg
  13149. (call-interactively 'org-toggle-checkbox)
  13150. (call-interactively 'org-maybe-renumber-ordered-list)))
  13151. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13152. ;; Dynamic block
  13153. (beginning-of-line 1)
  13154. (save-excursion (org-update-dblock)))
  13155. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13156. (cond
  13157. ((equal (match-string 1) "TBLFM")
  13158. ;; Recalculate the table before this line
  13159. (save-excursion
  13160. (beginning-of-line 1)
  13161. (skip-chars-backward " \r\n\t")
  13162. (if (org-at-table-p)
  13163. (org-call-with-arg 'org-table-recalculate t))))
  13164. (t
  13165. ; (org-set-regexps-and-options)
  13166. ; (org-restart-font-lock)
  13167. (let ((org-inhibit-startup t)) (org-mode-restart))
  13168. (message "Local setup has been refreshed"))))
  13169. (t (error "C-c C-c can do nothing useful at this location.")))))
  13170. (defun org-mode-restart ()
  13171. "Restart Org-mode, to scan again for special lines.
  13172. Also updates the keyword regular expressions."
  13173. (interactive)
  13174. (org-mode)
  13175. (message "Org-mode restarted"))
  13176. (defun org-kill-note-or-show-branches ()
  13177. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13178. (interactive)
  13179. (if (not org-finish-function)
  13180. (call-interactively 'show-branches)
  13181. (let ((org-note-abort t))
  13182. (funcall org-finish-function))))
  13183. (defun org-return (&optional indent)
  13184. "Goto next table row or insert a newline.
  13185. Calls `org-table-next-row' or `newline', depending on context.
  13186. See the individual commands for more information."
  13187. (interactive)
  13188. (cond
  13189. ((bobp) (if indent (newline-and-indent) (newline)))
  13190. ((org-at-table-p)
  13191. (org-table-justify-field-maybe)
  13192. (call-interactively 'org-table-next-row))
  13193. ((and org-return-follows-link
  13194. (eq (get-text-property (point) 'face) 'org-link))
  13195. (call-interactively 'org-open-at-point))
  13196. ((and (org-at-heading-p)
  13197. (looking-at
  13198. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13199. (org-show-entry)
  13200. (end-of-line 1)
  13201. (newline))
  13202. (t (if indent (newline-and-indent) (newline)))))
  13203. (defun org-return-indent ()
  13204. "Goto next table row or insert a newline and indent.
  13205. Calls `org-table-next-row' or `newline-and-indent', depending on
  13206. context. See the individual commands for more information."
  13207. (interactive)
  13208. (org-return t))
  13209. (defun org-ctrl-c-star ()
  13210. "Compute table, or change heading status of lines.
  13211. Calls `org-table-recalculate' or `org-toggle-heading',
  13212. depending on context."
  13213. (interactive)
  13214. (cond
  13215. ((org-at-table-p)
  13216. (call-interactively 'org-table-recalculate))
  13217. (t
  13218. ;; Convert all lines in region to list items
  13219. (call-interactively 'org-toggle-heading))))
  13220. (defun org-ctrl-c-minus ()
  13221. "Insert separator line in table or modify bullet status of line.
  13222. Also turns a plain line or a region of lines into list items.
  13223. Calls `org-table-insert-hline', `org-toggle-item', or
  13224. `org-cycle-list-bullet', depending on context."
  13225. (interactive)
  13226. (cond
  13227. ((org-at-table-p)
  13228. (call-interactively 'org-table-insert-hline))
  13229. ((org-region-active-p)
  13230. (call-interactively 'org-toggle-item))
  13231. ((org-in-item-p)
  13232. (call-interactively 'org-cycle-list-bullet))
  13233. (t
  13234. (call-interactively 'org-toggle-item))))
  13235. (defun org-toggle-item ()
  13236. "Convert headings or normal lines to items, items to normal lines.
  13237. If there is no active region, only the current line is considered.
  13238. If the first line in the region is a headline, convert all headlines to items.
  13239. If the first line in the region is an item, convert all items to normal lines.
  13240. If the first line is normal text, add an item bullet to each line."
  13241. (interactive)
  13242. (let (l2 l beg end)
  13243. (if (org-region-active-p)
  13244. (setq beg (region-beginning) end (region-end))
  13245. (setq beg (point-at-bol)
  13246. end (min (1+ (point-at-eol)) (point-max))))
  13247. (save-excursion
  13248. (goto-char end)
  13249. (setq l2 (org-current-line))
  13250. (goto-char beg)
  13251. (beginning-of-line 1)
  13252. (setq l (1- (org-current-line)))
  13253. (if (org-at-item-p)
  13254. ;; We already have items, de-itemize
  13255. (while (< (setq l (1+ l)) l2)
  13256. (when (org-at-item-p)
  13257. (goto-char (match-beginning 2))
  13258. (delete-region (match-beginning 2) (match-end 2))
  13259. (and (looking-at "[ \t]+") (replace-match "")))
  13260. (beginning-of-line 2))
  13261. (if (org-on-heading-p)
  13262. ;; Headings, convert to items
  13263. (while (< (setq l (1+ l)) l2)
  13264. (if (looking-at org-outline-regexp)
  13265. (replace-match "- " t t))
  13266. (beginning-of-line 2))
  13267. ;; normal lines, turn them into items
  13268. (while (< (setq l (1+ l)) l2)
  13269. (unless (org-at-item-p)
  13270. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13271. (replace-match "\\1- \\2")))
  13272. (beginning-of-line 2)))))))
  13273. (defun org-toggle-heading (&optional nstars)
  13274. "Convert headings to normal text, or items or text to headings.
  13275. If there is no active region, only the current line is considered.
  13276. If the first line is a heading, remove the stars from all headlines
  13277. in the region.
  13278. If the first line is a plain list item, turn all plain list items into
  13279. headings.
  13280. If the first line is a normal line, turn each and every line in the region
  13281. into a heading.
  13282. When converting a line into a heading, the number of stars is chosen
  13283. such that the lines become children of the current entry. However, when
  13284. a prefix argument is given, its value determines the number of stars to add."
  13285. (interactive "P")
  13286. (let (l2 l itemp beg end)
  13287. (if (org-region-active-p)
  13288. (setq beg (region-beginning) end (region-end))
  13289. (setq beg (point-at-bol)
  13290. end (min (1+ (point-at-eol)) (point-max))))
  13291. (save-excursion
  13292. (goto-char end)
  13293. (setq l2 (org-current-line))
  13294. (goto-char beg)
  13295. (beginning-of-line 1)
  13296. (setq l (1- (org-current-line)))
  13297. (if (org-on-heading-p)
  13298. ;; We already have headlines, de-star them
  13299. (while (< (setq l (1+ l)) l2)
  13300. (when (org-on-heading-p t)
  13301. (and (looking-at outline-regexp) (replace-match "")))
  13302. (beginning-of-line 2))
  13303. (setq itemp (org-at-item-p))
  13304. (let* ((stars
  13305. (if nstars
  13306. (make-string (prefix-numeric-value current-prefix-arg)
  13307. ?*)
  13308. (save-excursion
  13309. (re-search-backward org-complex-heading-regexp nil t)
  13310. (or (match-string 1) ""))))
  13311. (add-stars (cond (nstars "")
  13312. ((equal stars "") "*")
  13313. (org-odd-levels-only "**")
  13314. (t "*")))
  13315. (rpl (concat stars add-stars " ")))
  13316. (while (< (setq l (1+ l)) l2)
  13317. (if itemp
  13318. (and (org-at-item-p) (replace-match rpl t t))
  13319. (unless (org-on-heading-p)
  13320. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13321. (replace-match (concat rpl (match-string 2))))))
  13322. (beginning-of-line 2)))))))
  13323. (defun org-meta-return (&optional arg)
  13324. "Insert a new heading or wrap a region in a table.
  13325. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13326. See the individual commands for more information."
  13327. (interactive "P")
  13328. (cond
  13329. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13330. ((org-at-table-p)
  13331. (call-interactively 'org-table-wrap-region))
  13332. (t (call-interactively 'org-insert-heading))))
  13333. ;;; Menu entries
  13334. ;; Define the Org-mode menus
  13335. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13336. '("Tbl"
  13337. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13338. ["Next Field" org-cycle (org-at-table-p)]
  13339. ["Previous Field" org-shifttab (org-at-table-p)]
  13340. ["Next Row" org-return (org-at-table-p)]
  13341. "--"
  13342. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13343. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13344. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13345. "--"
  13346. ("Column"
  13347. ["Move Column Left" org-metaleft (org-at-table-p)]
  13348. ["Move Column Right" org-metaright (org-at-table-p)]
  13349. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13350. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13351. ("Row"
  13352. ["Move Row Up" org-metaup (org-at-table-p)]
  13353. ["Move Row Down" org-metadown (org-at-table-p)]
  13354. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13355. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13356. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13357. "--"
  13358. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13359. ("Rectangle"
  13360. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13361. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13362. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13363. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13364. "--"
  13365. ("Calculate"
  13366. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13367. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13368. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13369. "--"
  13370. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13371. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13372. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13373. "--"
  13374. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13375. "--"
  13376. ["Sum Column/Rectangle" org-table-sum
  13377. (or (org-at-table-p) (org-region-active-p))]
  13378. ["Which Column?" org-table-current-column (org-at-table-p)])
  13379. ["Debug Formulas"
  13380. org-table-toggle-formula-debugger
  13381. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13382. ["Show Col/Row Numbers"
  13383. org-table-toggle-coordinate-overlays
  13384. :style toggle
  13385. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13386. "--"
  13387. ["Create" org-table-create (and (not (org-at-table-p))
  13388. org-enable-table-editor)]
  13389. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13390. ["Import from File" org-table-import (not (org-at-table-p))]
  13391. ["Export to File" org-table-export (org-at-table-p)]
  13392. "--"
  13393. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13394. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13395. '("Org"
  13396. ("Show/Hide"
  13397. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13398. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13399. ["Sparse Tree..." org-sparse-tree t]
  13400. ["Reveal Context" org-reveal t]
  13401. ["Show All" show-all t]
  13402. "--"
  13403. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13404. "--"
  13405. ["New Heading" org-insert-heading t]
  13406. ("Navigate Headings"
  13407. ["Up" outline-up-heading t]
  13408. ["Next" outline-next-visible-heading t]
  13409. ["Previous" outline-previous-visible-heading t]
  13410. ["Next Same Level" outline-forward-same-level t]
  13411. ["Previous Same Level" outline-backward-same-level t]
  13412. "--"
  13413. ["Jump" org-goto t])
  13414. ("Edit Structure"
  13415. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13416. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13417. "--"
  13418. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13419. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13420. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13421. "--"
  13422. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13423. "--"
  13424. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13425. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13426. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13427. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13428. "--"
  13429. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13430. "--"
  13431. ["Convert to odd levels" org-convert-to-odd-levels t]
  13432. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13433. ("Editing"
  13434. ["Emphasis..." org-emphasize t]
  13435. ["Edit Source Example" org-edit-special t]
  13436. "--"
  13437. ["Footnote new/jump" org-footnote-action t]
  13438. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13439. ("Archive"
  13440. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13441. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13442. ; :active t :keys "C-u C-c C-x C-a"]
  13443. ["Sparse trees open ARCHIVE trees"
  13444. (setq org-sparse-tree-open-archived-trees
  13445. (not org-sparse-tree-open-archived-trees))
  13446. :style toggle :selected org-sparse-tree-open-archived-trees]
  13447. ["Cycling opens ARCHIVE trees"
  13448. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13449. :style toggle :selected org-cycle-open-archived-trees]
  13450. "--"
  13451. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13452. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13453. ; ["Check and Move Children" (org-archive-subtree '(4))
  13454. ; :active t :keys "C-u C-c C-x C-s"]
  13455. )
  13456. "--"
  13457. ("Hyperlinks"
  13458. ["Store Link (Global)" org-store-link t]
  13459. ["Insert Link" org-insert-link t]
  13460. ["Follow Link" org-open-at-point t]
  13461. "--"
  13462. ["Next link" org-next-link t]
  13463. ["Previous link" org-previous-link t]
  13464. "--"
  13465. ["Descriptive Links"
  13466. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13467. :style radio
  13468. :selected (member '(org-link) buffer-invisibility-spec)]
  13469. ["Literal Links"
  13470. (progn
  13471. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13472. :style radio
  13473. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13474. "--"
  13475. ("TODO Lists"
  13476. ["TODO/DONE/-" org-todo t]
  13477. ("Select keyword"
  13478. ["Next keyword" org-shiftright (org-on-heading-p)]
  13479. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13480. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13481. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13482. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13483. ["Show TODO Tree" org-show-todo-tree t]
  13484. ["Global TODO list" org-todo-list t]
  13485. "--"
  13486. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13487. :selected org-enforce-todo-dependencies :style toggle :active t]
  13488. "Settings for tree at point"
  13489. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13490. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13491. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13492. ["Do Children parallel" org-toggle-ordered-property :style radio
  13493. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13494. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13495. "--"
  13496. ["Set Priority" org-priority t]
  13497. ["Priority Up" org-shiftup t]
  13498. ["Priority Down" org-shiftdown t]
  13499. "--"
  13500. ["Get news from all feeds" org-feed-update-all t]
  13501. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13502. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13503. ("TAGS and Properties"
  13504. ["Set Tags" org-set-tags-command t]
  13505. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13506. "--"
  13507. ["Set property" org-set-property t]
  13508. ["Column view of properties" org-columns t]
  13509. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13510. ("Dates and Scheduling"
  13511. ["Timestamp" org-time-stamp t]
  13512. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13513. ("Change Date"
  13514. ["1 Day Later" org-shiftright t]
  13515. ["1 Day Earlier" org-shiftleft t]
  13516. ["1 ... Later" org-shiftup t]
  13517. ["1 ... Earlier" org-shiftdown t])
  13518. ["Compute Time Range" org-evaluate-time-range t]
  13519. ["Schedule Item" org-schedule t]
  13520. ["Deadline" org-deadline t]
  13521. "--"
  13522. ["Custom time format" org-toggle-time-stamp-overlays
  13523. :style radio :selected org-display-custom-times]
  13524. "--"
  13525. ["Goto Calendar" org-goto-calendar t]
  13526. ["Date from Calendar" org-date-from-calendar t]
  13527. "--"
  13528. ["Start/Restart Timer" org-timer-start t]
  13529. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13530. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13531. ["Insert Timer String" org-timer t]
  13532. ["Insert Timer Item" org-timer-item t])
  13533. ("Logging work"
  13534. ["Clock in" org-clock-in t]
  13535. ["Clock out" org-clock-out t]
  13536. ["Clock cancel" org-clock-cancel t]
  13537. ["Goto running clock" org-clock-goto t]
  13538. ["Display times" org-clock-display t]
  13539. ["Create clock table" org-clock-report t]
  13540. "--"
  13541. ["Record DONE time"
  13542. (progn (setq org-log-done (not org-log-done))
  13543. (message "Switching to %s will %s record a timestamp"
  13544. (car org-done-keywords)
  13545. (if org-log-done "automatically" "not")))
  13546. :style toggle :selected org-log-done])
  13547. "--"
  13548. ["Agenda Command..." org-agenda t]
  13549. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13550. ("File List for Agenda")
  13551. ("Special views current file"
  13552. ["TODO Tree" org-show-todo-tree t]
  13553. ["Check Deadlines" org-check-deadlines t]
  13554. ["Timeline" org-timeline t]
  13555. ["Tags/Property tree" org-match-sparse-tree t])
  13556. "--"
  13557. ["Export/Publish..." org-export t]
  13558. ("LaTeX"
  13559. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13560. :selected org-cdlatex-mode]
  13561. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13562. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13563. ["Modify math symbol" org-cdlatex-math-modify
  13564. (org-inside-LaTeX-fragment-p)]
  13565. ["Insert citation" org-reftex-citation t]
  13566. "--"
  13567. ["Export LaTeX fragments as images"
  13568. (if (featurep 'org-exp)
  13569. (setq org-export-with-LaTeX-fragments
  13570. (not org-export-with-LaTeX-fragments))
  13571. (require 'org-exp))
  13572. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13573. org-export-with-LaTeX-fragments)])
  13574. "--"
  13575. ("Documentation"
  13576. ["Show Version" org-version t]
  13577. ["Info Documentation" org-info t])
  13578. ("Customize"
  13579. ["Browse Org Group" org-customize t]
  13580. "--"
  13581. ["Expand This Menu" org-create-customize-menu
  13582. (fboundp 'customize-menu-create)])
  13583. "--"
  13584. ("Refresh/Reload"
  13585. ["Refresh setup current buffer" org-mode-restart t]
  13586. ["Reload Org (after update)" org-reload t]
  13587. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13588. ))
  13589. (defun org-info (&optional node)
  13590. "Read documentation for Org-mode in the info system.
  13591. With optional NODE, go directly to that node."
  13592. (interactive)
  13593. (info (format "(org)%s" (or node ""))))
  13594. (defun org-install-agenda-files-menu ()
  13595. (let ((bl (buffer-list)))
  13596. (save-excursion
  13597. (while bl
  13598. (set-buffer (pop bl))
  13599. (if (org-mode-p) (setq bl nil)))
  13600. (when (org-mode-p)
  13601. (easy-menu-change
  13602. '("Org") "File List for Agenda"
  13603. (append
  13604. (list
  13605. ["Edit File List" (org-edit-agenda-file-list) t]
  13606. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13607. ["Remove Current File from List" org-remove-file t]
  13608. ["Cycle through agenda files" org-cycle-agenda-files t]
  13609. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13610. "--")
  13611. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13612. ;;;; Documentation
  13613. ;;;###autoload
  13614. (defun org-require-autoloaded-modules ()
  13615. (interactive)
  13616. (mapc 'require
  13617. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13618. org-docbook org-exp org-html org-icalendar
  13619. org-id org-latex
  13620. org-publish org-remember org-table
  13621. org-timer org-xoxo)))
  13622. ;;;###autoload
  13623. (defun org-reload (&optional uncompiled)
  13624. "Reload all org lisp files.
  13625. With prefix arg UNCOMPILED, load the uncompiled versions."
  13626. (interactive "P")
  13627. (require 'find-func)
  13628. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13629. (dir-org (file-name-directory (org-find-library-name "org")))
  13630. (dir-org-contrib (ignore-errors
  13631. (file-name-directory
  13632. (org-find-library-name "org-contribdir"))))
  13633. (files
  13634. (append (directory-files dir-org t file-re)
  13635. (and dir-org-contrib
  13636. (directory-files dir-org-contrib t file-re))))
  13637. (remove-re (concat (if (featurep 'xemacs)
  13638. "org-colview" "org-colview-xemacs")
  13639. "\\'")))
  13640. (setq files (mapcar 'file-name-sans-extension files))
  13641. (setq files (mapcar
  13642. (lambda (x) (if (string-match remove-re x) nil x))
  13643. files))
  13644. (setq files (delq nil files))
  13645. (mapc
  13646. (lambda (f)
  13647. (when (featurep (intern (file-name-nondirectory f)))
  13648. (if (and (not uncompiled)
  13649. (file-exists-p (concat f ".elc")))
  13650. (load (concat f ".elc") nil nil t)
  13651. (load (concat f ".el") nil nil t))))
  13652. files))
  13653. (org-version))
  13654. ;;;###autoload
  13655. (defun org-customize ()
  13656. "Call the customize function with org as argument."
  13657. (interactive)
  13658. (org-load-modules-maybe)
  13659. (org-require-autoloaded-modules)
  13660. (customize-browse 'org))
  13661. (defun org-create-customize-menu ()
  13662. "Create a full customization menu for Org-mode, insert it into the menu."
  13663. (interactive)
  13664. (org-load-modules-maybe)
  13665. (org-require-autoloaded-modules)
  13666. (if (fboundp 'customize-menu-create)
  13667. (progn
  13668. (easy-menu-change
  13669. '("Org") "Customize"
  13670. `(["Browse Org group" org-customize t]
  13671. "--"
  13672. ,(customize-menu-create 'org)
  13673. ["Set" Custom-set t]
  13674. ["Save" Custom-save t]
  13675. ["Reset to Current" Custom-reset-current t]
  13676. ["Reset to Saved" Custom-reset-saved t]
  13677. ["Reset to Standard Settings" Custom-reset-standard t]))
  13678. (message "\"Org\"-menu now contains full customization menu"))
  13679. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13680. ;;;; Miscellaneous stuff
  13681. ;;; Generally useful functions
  13682. (defun org-find-text-property-in-string (prop s)
  13683. "Return the first non-nil value of property PROP in string S."
  13684. (or (get-text-property 0 prop s)
  13685. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13686. prop s)))
  13687. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13688. "Display the given MESSAGE as a warning."
  13689. (if (fboundp 'display-warning)
  13690. (display-warning 'org message
  13691. (if (featurep 'xemacs)
  13692. 'warning
  13693. :warning))
  13694. (let ((buf (get-buffer-create "*Org warnings*")))
  13695. (with-current-buffer buf
  13696. (goto-char (point-max))
  13697. (insert "Warning (Org): " message)
  13698. (unless (bolp)
  13699. (newline)))
  13700. (display-buffer buf)
  13701. (sit-for 0))))
  13702. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13703. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13704. (if (and marker (marker-buffer marker)
  13705. (buffer-live-p (marker-buffer marker)))
  13706. (progn
  13707. (switch-to-buffer (marker-buffer marker))
  13708. (if (or (> marker (point-max)) (< marker (point-min)))
  13709. (widen))
  13710. (goto-char marker)
  13711. (org-show-context 'org-goto))
  13712. (if bookmark
  13713. (bookmark-jump bookmark)
  13714. (error "Cannot find location"))))
  13715. (defun org-quote-csv-field (s)
  13716. "Quote field for inclusion in CSV material."
  13717. (if (string-match "[\",]" s)
  13718. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13719. s))
  13720. (defun org-plist-delete (plist property)
  13721. "Delete PROPERTY from PLIST.
  13722. This is in contrast to merely setting it to 0."
  13723. (let (p)
  13724. (while plist
  13725. (if (not (eq property (car plist)))
  13726. (setq p (plist-put p (car plist) (nth 1 plist))))
  13727. (setq plist (cddr plist)))
  13728. p))
  13729. (defun org-force-self-insert (N)
  13730. "Needed to enforce self-insert under remapping."
  13731. (interactive "p")
  13732. (self-insert-command N))
  13733. (defun org-string-width (s)
  13734. "Compute width of string, ignoring invisible characters.
  13735. This ignores character with invisibility property `org-link', and also
  13736. characters with property `org-cwidth', because these will become invisible
  13737. upon the next fontification round."
  13738. (let (b l)
  13739. (when (or (eq t buffer-invisibility-spec)
  13740. (assq 'org-link buffer-invisibility-spec))
  13741. (while (setq b (text-property-any 0 (length s)
  13742. 'invisible 'org-link s))
  13743. (setq s (concat (substring s 0 b)
  13744. (substring s (or (next-single-property-change
  13745. b 'invisible s) (length s)))))))
  13746. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13747. (setq s (concat (substring s 0 b)
  13748. (substring s (or (next-single-property-change
  13749. b 'org-cwidth s) (length s))))))
  13750. (setq l (string-width s) b -1)
  13751. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13752. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13753. l))
  13754. (defun org-get-indentation (&optional line)
  13755. "Get the indentation of the current line, interpreting tabs.
  13756. When LINE is given, assume it represents a line and compute its indentation."
  13757. (if line
  13758. (if (string-match "^ *" (org-remove-tabs line))
  13759. (match-end 0))
  13760. (save-excursion
  13761. (beginning-of-line 1)
  13762. (skip-chars-forward " \t")
  13763. (current-column))))
  13764. (defun org-remove-tabs (s &optional width)
  13765. "Replace tabulators in S with spaces.
  13766. Assumes that s is a single line, starting in column 0."
  13767. (setq width (or width tab-width))
  13768. (while (string-match "\t" s)
  13769. (setq s (replace-match
  13770. (make-string
  13771. (- (* width (/ (+ (match-beginning 0) width) width))
  13772. (match-beginning 0)) ?\ )
  13773. t t s)))
  13774. s)
  13775. (defun org-fix-indentation (line ind)
  13776. "Fix indentation in LINE.
  13777. IND is a cons cell with target and minimum indentation.
  13778. If the current indentation in LINE is smaller than the minimum,
  13779. leave it alone. If it is larger than ind, set it to the target."
  13780. (let* ((l (org-remove-tabs line))
  13781. (i (org-get-indentation l))
  13782. (i1 (car ind)) (i2 (cdr ind)))
  13783. (if (>= i i2) (setq l (substring line i2)))
  13784. (if (> i1 0)
  13785. (concat (make-string i1 ?\ ) l)
  13786. l)))
  13787. (defun org-base-buffer (buffer)
  13788. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13789. (if (not buffer)
  13790. buffer
  13791. (or (buffer-base-buffer buffer)
  13792. buffer)))
  13793. (defun org-trim (s)
  13794. "Remove whitespace at beginning and end of string."
  13795. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13796. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13797. s)
  13798. (defun org-wrap (string &optional width lines)
  13799. "Wrap string to either a number of lines, or a width in characters.
  13800. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13801. that costs. If there is a word longer than WIDTH, the text is actually
  13802. wrapped to the length of that word.
  13803. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13804. many lines, whatever width that takes.
  13805. The return value is a list of lines, without newlines at the end."
  13806. (let* ((words (org-split-string string "[ \t\n]+"))
  13807. (maxword (apply 'max (mapcar 'org-string-width words)))
  13808. w ll)
  13809. (cond (width
  13810. (org-do-wrap words (max maxword width)))
  13811. (lines
  13812. (setq w maxword)
  13813. (setq ll (org-do-wrap words maxword))
  13814. (if (<= (length ll) lines)
  13815. ll
  13816. (setq ll words)
  13817. (while (> (length ll) lines)
  13818. (setq w (1+ w))
  13819. (setq ll (org-do-wrap words w)))
  13820. ll))
  13821. (t (error "Cannot wrap this")))))
  13822. (defun org-do-wrap (words width)
  13823. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13824. (let (lines line)
  13825. (while words
  13826. (setq line (pop words))
  13827. (while (and words (< (+ (length line) (length (car words))) width))
  13828. (setq line (concat line " " (pop words))))
  13829. (setq lines (push line lines)))
  13830. (nreverse lines)))
  13831. (defun org-split-string (string &optional separators)
  13832. "Splits STRING into substrings at SEPARATORS.
  13833. No empty strings are returned if there are matches at the beginning
  13834. and end of string."
  13835. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13836. (start 0)
  13837. notfirst
  13838. (list nil))
  13839. (while (and (string-match rexp string
  13840. (if (and notfirst
  13841. (= start (match-beginning 0))
  13842. (< start (length string)))
  13843. (1+ start) start))
  13844. (< (match-beginning 0) (length string)))
  13845. (setq notfirst t)
  13846. (or (eq (match-beginning 0) 0)
  13847. (and (eq (match-beginning 0) (match-end 0))
  13848. (eq (match-beginning 0) start))
  13849. (setq list
  13850. (cons (substring string start (match-beginning 0))
  13851. list)))
  13852. (setq start (match-end 0)))
  13853. (or (eq start (length string))
  13854. (setq list
  13855. (cons (substring string start)
  13856. list)))
  13857. (nreverse list)))
  13858. (defun org-uuidgen-p (s)
  13859. "Is S an ID created by UUIDGEN?"
  13860. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13861. (defun org-context ()
  13862. "Return a list of contexts of the current cursor position.
  13863. If several contexts apply, all are returned.
  13864. Each context entry is a list with a symbol naming the context, and
  13865. two positions indicating start and end of the context. Possible
  13866. contexts are:
  13867. :headline anywhere in a headline
  13868. :headline-stars on the leading stars in a headline
  13869. :todo-keyword on a TODO keyword (including DONE) in a headline
  13870. :tags on the TAGS in a headline
  13871. :priority on the priority cookie in a headline
  13872. :item on the first line of a plain list item
  13873. :item-bullet on the bullet/number of a plain list item
  13874. :checkbox on the checkbox in a plain list item
  13875. :table in an org-mode table
  13876. :table-special on a special filed in a table
  13877. :table-table in a table.el table
  13878. :link on a hyperlink
  13879. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13880. :target on a <<target>>
  13881. :radio-target on a <<<radio-target>>>
  13882. :latex-fragment on a LaTeX fragment
  13883. :latex-preview on a LaTeX fragment with overlayed preview image
  13884. This function expects the position to be visible because it uses font-lock
  13885. faces as a help to recognize the following contexts: :table-special, :link,
  13886. and :keyword."
  13887. (let* ((f (get-text-property (point) 'face))
  13888. (faces (if (listp f) f (list f)))
  13889. (p (point)) clist o)
  13890. ;; First the large context
  13891. (cond
  13892. ((org-on-heading-p t)
  13893. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13894. (when (progn
  13895. (beginning-of-line 1)
  13896. (looking-at org-todo-line-tags-regexp))
  13897. (push (org-point-in-group p 1 :headline-stars) clist)
  13898. (push (org-point-in-group p 2 :todo-keyword) clist)
  13899. (push (org-point-in-group p 4 :tags) clist))
  13900. (goto-char p)
  13901. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13902. (if (looking-at "\\[#[A-Z0-9]\\]")
  13903. (push (org-point-in-group p 0 :priority) clist)))
  13904. ((org-at-item-p)
  13905. (push (org-point-in-group p 2 :item-bullet) clist)
  13906. (push (list :item (point-at-bol)
  13907. (save-excursion (org-end-of-item) (point)))
  13908. clist)
  13909. (and (org-at-item-checkbox-p)
  13910. (push (org-point-in-group p 0 :checkbox) clist)))
  13911. ((org-at-table-p)
  13912. (push (list :table (org-table-begin) (org-table-end)) clist)
  13913. (if (memq 'org-formula faces)
  13914. (push (list :table-special
  13915. (previous-single-property-change p 'face)
  13916. (next-single-property-change p 'face)) clist)))
  13917. ((org-at-table-p 'any)
  13918. (push (list :table-table) clist)))
  13919. (goto-char p)
  13920. ;; Now the small context
  13921. (cond
  13922. ((org-at-timestamp-p)
  13923. (push (org-point-in-group p 0 :timestamp) clist))
  13924. ((memq 'org-link faces)
  13925. (push (list :link
  13926. (previous-single-property-change p 'face)
  13927. (next-single-property-change p 'face)) clist))
  13928. ((memq 'org-special-keyword faces)
  13929. (push (list :keyword
  13930. (previous-single-property-change p 'face)
  13931. (next-single-property-change p 'face)) clist))
  13932. ((org-on-target-p)
  13933. (push (org-point-in-group p 0 :target) clist)
  13934. (goto-char (1- (match-beginning 0)))
  13935. (if (looking-at org-radio-target-regexp)
  13936. (push (org-point-in-group p 0 :radio-target) clist))
  13937. (goto-char p))
  13938. ((setq o (car (delq nil
  13939. (mapcar
  13940. (lambda (x)
  13941. (if (memq x org-latex-fragment-image-overlays) x))
  13942. (org-overlays-at (point))))))
  13943. (push (list :latex-fragment
  13944. (org-overlay-start o) (org-overlay-end o)) clist)
  13945. (push (list :latex-preview
  13946. (org-overlay-start o) (org-overlay-end o)) clist))
  13947. ((org-inside-LaTeX-fragment-p)
  13948. ;; FIXME: positions wrong.
  13949. (push (list :latex-fragment (point) (point)) clist)))
  13950. (setq clist (nreverse (delq nil clist)))
  13951. clist))
  13952. ;; FIXME: Compare with at-regexp-p Do we need both?
  13953. (defun org-in-regexp (re &optional nlines visually)
  13954. "Check if point is inside a match of regexp.
  13955. Normally only the current line is checked, but you can include NLINES extra
  13956. lines both before and after point into the search.
  13957. If VISUALLY is set, require that the cursor is not after the match but
  13958. really on, so that the block visually is on the match."
  13959. (catch 'exit
  13960. (let ((pos (point))
  13961. (eol (point-at-eol (+ 1 (or nlines 0))))
  13962. (inc (if visually 1 0)))
  13963. (save-excursion
  13964. (beginning-of-line (- 1 (or nlines 0)))
  13965. (while (re-search-forward re eol t)
  13966. (if (and (<= (match-beginning 0) pos)
  13967. (>= (+ inc (match-end 0)) pos))
  13968. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13969. (defun org-at-regexp-p (regexp)
  13970. "Is point inside a match of REGEXP in the current line?"
  13971. (catch 'exit
  13972. (save-excursion
  13973. (let ((pos (point)) (end (point-at-eol)))
  13974. (beginning-of-line 1)
  13975. (while (re-search-forward regexp end t)
  13976. (if (and (<= (match-beginning 0) pos)
  13977. (>= (match-end 0) pos))
  13978. (throw 'exit t)))
  13979. nil))))
  13980. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13981. "Call `multi-occur' with buffers for all agenda files."
  13982. (interactive "sOrg-files matching: \np")
  13983. (let* ((files (org-agenda-files))
  13984. (tnames (mapcar 'file-truename files))
  13985. (extra org-agenda-text-search-extra-files)
  13986. f)
  13987. (when (eq (car extra) 'agenda-archives)
  13988. (setq extra (cdr extra))
  13989. (setq files (org-add-archive-files files)))
  13990. (while (setq f (pop extra))
  13991. (unless (member (file-truename f) tnames)
  13992. (add-to-list 'files f 'append)
  13993. (add-to-list 'tnames (file-truename f) 'append)))
  13994. (multi-occur
  13995. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13996. regexp)))
  13997. (if (boundp 'occur-mode-find-occurrence-hook)
  13998. ;; Emacs 23
  13999. (add-hook 'occur-mode-find-occurrence-hook
  14000. (lambda ()
  14001. (when (org-mode-p)
  14002. (org-reveal))))
  14003. ;; Emacs 22
  14004. (defadvice occur-mode-goto-occurrence
  14005. (after org-occur-reveal activate)
  14006. (and (org-mode-p) (org-reveal)))
  14007. (defadvice occur-mode-goto-occurrence-other-window
  14008. (after org-occur-reveal activate)
  14009. (and (org-mode-p) (org-reveal)))
  14010. (defadvice occur-mode-display-occurrence
  14011. (after org-occur-reveal activate)
  14012. (when (org-mode-p)
  14013. (let ((pos (occur-mode-find-occurrence)))
  14014. (with-current-buffer (marker-buffer pos)
  14015. (save-excursion
  14016. (goto-char pos)
  14017. (org-reveal)))))))
  14018. (defun org-uniquify (list)
  14019. "Remove duplicate elements from LIST."
  14020. (let (res)
  14021. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14022. res))
  14023. (defun org-delete-all (elts list)
  14024. "Remove all elements in ELTS from LIST."
  14025. (while elts
  14026. (setq list (delete (pop elts) list)))
  14027. list)
  14028. (defun org-back-over-empty-lines ()
  14029. "Move backwards over whitespace, to the beginning of the first empty line.
  14030. Returns the number of empty lines passed."
  14031. (let ((pos (point)))
  14032. (skip-chars-backward " \t\n\r")
  14033. (beginning-of-line 2)
  14034. (goto-char (min (point) pos))
  14035. (count-lines (point) pos)))
  14036. (defun org-skip-whitespace ()
  14037. (skip-chars-forward " \t\n\r"))
  14038. (defun org-point-in-group (point group &optional context)
  14039. "Check if POINT is in match-group GROUP.
  14040. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14041. match. If the match group does ot exist or point is not inside it,
  14042. return nil."
  14043. (and (match-beginning group)
  14044. (>= point (match-beginning group))
  14045. (<= point (match-end group))
  14046. (if context
  14047. (list context (match-beginning group) (match-end group))
  14048. t)))
  14049. (defun org-switch-to-buffer-other-window (&rest args)
  14050. "Switch to buffer in a second window on the current frame.
  14051. In particular, do not allow pop-up frames."
  14052. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14053. special-display-function)
  14054. (apply 'switch-to-buffer-other-window args)))
  14055. (defun org-combine-plists (&rest plists)
  14056. "Create a single property list from all plists in PLISTS.
  14057. The process starts by copying the first list, and then setting properties
  14058. from the other lists. Settings in the last list are the most significant
  14059. ones and overrule settings in the other lists."
  14060. (let ((rtn (copy-sequence (pop plists)))
  14061. p v ls)
  14062. (while plists
  14063. (setq ls (pop plists))
  14064. (while ls
  14065. (setq p (pop ls) v (pop ls))
  14066. (setq rtn (plist-put rtn p v))))
  14067. rtn))
  14068. (defun org-move-line-down (arg)
  14069. "Move the current line down. With prefix argument, move it past ARG lines."
  14070. (interactive "p")
  14071. (let ((col (current-column))
  14072. beg end pos)
  14073. (beginning-of-line 1) (setq beg (point))
  14074. (beginning-of-line 2) (setq end (point))
  14075. (beginning-of-line (+ 1 arg))
  14076. (setq pos (move-marker (make-marker) (point)))
  14077. (insert (delete-and-extract-region beg end))
  14078. (goto-char pos)
  14079. (org-move-to-column col)))
  14080. (defun org-move-line-up (arg)
  14081. "Move the current line up. With prefix argument, move it past ARG lines."
  14082. (interactive "p")
  14083. (let ((col (current-column))
  14084. beg end pos)
  14085. (beginning-of-line 1) (setq beg (point))
  14086. (beginning-of-line 2) (setq end (point))
  14087. (beginning-of-line (- arg))
  14088. (setq pos (move-marker (make-marker) (point)))
  14089. (insert (delete-and-extract-region beg end))
  14090. (goto-char pos)
  14091. (org-move-to-column col)))
  14092. (defun org-replace-escapes (string table)
  14093. "Replace %-escapes in STRING with values in TABLE.
  14094. TABLE is an association list with keys like \"%a\" and string values.
  14095. The sequences in STRING may contain normal field width and padding information,
  14096. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14097. so values can contain further %-escapes if they are define later in TABLE."
  14098. (let ((case-fold-search nil)
  14099. e re rpl)
  14100. (while (setq e (pop table))
  14101. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14102. (while (string-match re string)
  14103. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14104. (cdr e)))
  14105. (setq string (replace-match rpl t t string))))
  14106. string))
  14107. (defun org-sublist (list start end)
  14108. "Return a section of LIST, from START to END.
  14109. Counting starts at 1."
  14110. (let (rtn (c start))
  14111. (setq list (nthcdr (1- start) list))
  14112. (while (and list (<= c end))
  14113. (push (pop list) rtn)
  14114. (setq c (1+ c)))
  14115. (nreverse rtn)))
  14116. (defun org-find-base-buffer-visiting (file)
  14117. "Like `find-buffer-visiting' but alway return the base buffer and
  14118. not an indirect buffer."
  14119. (let ((buf (find-buffer-visiting file)))
  14120. (if buf
  14121. (or (buffer-base-buffer buf) buf)
  14122. nil)))
  14123. (defun org-image-file-name-regexp (&optional extensions)
  14124. "Return regexp matching the file names of images.
  14125. If EXTENSIONS is given, only match these."
  14126. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14127. (image-file-name-regexp)
  14128. (let ((image-file-name-extensions
  14129. (or extensions
  14130. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14131. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14132. (concat "\\."
  14133. (regexp-opt (nconc (mapcar 'upcase
  14134. image-file-name-extensions)
  14135. image-file-name-extensions)
  14136. t)
  14137. "\\'"))))
  14138. (defun org-file-image-p (file &optional extensions)
  14139. "Return non-nil if FILE is an image."
  14140. (save-match-data
  14141. (string-match (org-image-file-name-regexp extensions) file)))
  14142. (defun org-get-cursor-date ()
  14143. "Return the date at cursor in as a time.
  14144. This works in the calendar and in the agenda, anywhere else it just
  14145. returns the current time."
  14146. (let (date day defd)
  14147. (cond
  14148. ((eq major-mode 'calendar-mode)
  14149. (setq date (calendar-cursor-to-date)
  14150. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14151. ((eq major-mode 'org-agenda-mode)
  14152. (setq day (get-text-property (point) 'day))
  14153. (if day
  14154. (setq date (calendar-gregorian-from-absolute day)
  14155. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14156. (nth 2 date))))))
  14157. (or defd (current-time))))
  14158. (defvar org-agenda-action-marker (make-marker)
  14159. "Marker pointing to the entry for the next agenda action.")
  14160. (defun org-mark-entry-for-agenda-action ()
  14161. "Mark the current entry as target of an agenda action.
  14162. Agenda actions are actions executed from the agenda with the key `k',
  14163. which make use of the date at the cursor."
  14164. (interactive)
  14165. (move-marker org-agenda-action-marker
  14166. (save-excursion (org-back-to-heading t) (point))
  14167. (current-buffer))
  14168. (message
  14169. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14170. ;;; Paragraph filling stuff.
  14171. ;; We want this to be just right, so use the full arsenal.
  14172. (defun org-indent-line-function ()
  14173. "Indent line like previous, but further if previous was headline or item."
  14174. (interactive)
  14175. (let* ((pos (point))
  14176. (itemp (org-at-item-p))
  14177. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14178. column bpos bcol tpos tcol bullet btype bullet-type)
  14179. ;; Find the previous relevant line
  14180. (beginning-of-line 1)
  14181. (cond
  14182. ((looking-at "#") (setq column 0))
  14183. ((looking-at "\\*+ ") (setq column 0))
  14184. ((and (looking-at "[ \t]*:END:")
  14185. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14186. (save-excursion
  14187. (goto-char (1- (match-beginning 1)))
  14188. (setq column (current-column))))
  14189. (t
  14190. (beginning-of-line 0)
  14191. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14192. (not (looking-at "[ \t]*:END:"))
  14193. (not (looking-at org-drawer-regexp)))
  14194. (beginning-of-line 0))
  14195. (cond
  14196. ((looking-at "\\*+[ \t]+")
  14197. (if (not org-adapt-indentation)
  14198. (setq column 0)
  14199. (goto-char (match-end 0))
  14200. (setq column (current-column))))
  14201. ((looking-at org-drawer-regexp)
  14202. (goto-char (1- (match-beginning 1)))
  14203. (setq column (current-column)))
  14204. ((looking-at "\\([ \t]*\\):END:")
  14205. (goto-char (match-end 1))
  14206. (setq column (current-column)))
  14207. ((org-in-item-p)
  14208. (org-beginning-of-item)
  14209. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14210. (setq bpos (match-beginning 1) tpos (match-end 0)
  14211. bcol (progn (goto-char bpos) (current-column))
  14212. tcol (progn (goto-char tpos) (current-column))
  14213. bullet (match-string 1)
  14214. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14215. (if (> tcol (+ bcol org-description-max-indent))
  14216. (setq tcol (+ bcol 5)))
  14217. (if (not itemp)
  14218. (setq column tcol)
  14219. (goto-char pos)
  14220. (beginning-of-line 1)
  14221. (if (looking-at "\\S-")
  14222. (progn
  14223. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14224. (setq bullet (match-string 1)
  14225. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14226. (setq column (if (equal btype bullet-type) bcol tcol)))
  14227. (setq column (org-get-indentation)))))
  14228. (t (setq column (org-get-indentation))))))
  14229. (goto-char pos)
  14230. (if (<= (current-column) (current-indentation))
  14231. (org-indent-line-to column)
  14232. (save-excursion (org-indent-line-to column)))
  14233. (setq column (current-column))
  14234. (beginning-of-line 1)
  14235. (if (looking-at
  14236. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14237. (replace-match (concat "\\1" (format org-property-format
  14238. (match-string 2) (match-string 3)))
  14239. t nil))
  14240. (org-move-to-column column)))
  14241. (defun org-set-autofill-regexps ()
  14242. (interactive)
  14243. ;; In the paragraph separator we include headlines, because filling
  14244. ;; text in a line directly attached to a headline would otherwise
  14245. ;; fill the headline as well.
  14246. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14247. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14248. ;; The paragraph starter includes hand-formatted lists.
  14249. (org-set-local 'paragraph-start
  14250. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14251. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14252. ;; But only if the user has not turned off tables or fixed-width regions
  14253. (org-set-local
  14254. 'auto-fill-inhibit-regexp
  14255. (concat "\\*+ \\|#\\+"
  14256. "\\|[ \t]*" org-keyword-time-regexp
  14257. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14258. (concat
  14259. "\\|[ \t]*["
  14260. (if org-enable-table-editor "|" "")
  14261. (if org-enable-fixed-width-editor ":" "")
  14262. "]"))))
  14263. ;; We use our own fill-paragraph function, to make sure that tables
  14264. ;; and fixed-width regions are not wrapped. That function will pass
  14265. ;; through to `fill-paragraph' when appropriate.
  14266. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14267. ; Adaptive filling: To get full control, first make sure that
  14268. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14269. (org-set-local 'adaptive-fill-regexp "\000")
  14270. (org-set-local 'adaptive-fill-function
  14271. 'org-adaptive-fill-function)
  14272. (org-set-local
  14273. 'align-mode-rules-list
  14274. '((org-in-buffer-settings
  14275. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14276. (modes . '(org-mode))))))
  14277. (defun org-fill-paragraph (&optional justify)
  14278. "Re-align a table, pass through to fill-paragraph if no table."
  14279. (let ((table-p (org-at-table-p))
  14280. (table.el-p (org-at-table.el-p)))
  14281. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14282. (save-excursion (goto-char (point-at-bol))
  14283. (looking-at outline-regexp)))
  14284. t) ; skip headlines
  14285. (table.el-p t) ; skip table.el tables
  14286. (table-p (org-table-align) t) ; align org-mode tables
  14287. (t nil)))) ; call paragraph-fill
  14288. ;; For reference, this is the default value of adaptive-fill-regexp
  14289. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14290. (defun org-adaptive-fill-function ()
  14291. "Return a fill prefix for org-mode files.
  14292. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14293. work correctly."
  14294. (cond ((looking-at "#[ \t]+")
  14295. (match-string 0))
  14296. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14297. (save-excursion
  14298. (if (> (match-end 1) (+ (match-beginning 1)
  14299. org-description-max-indent))
  14300. (goto-char (+ (match-beginning 1) 5))
  14301. (goto-char (match-end 0)))
  14302. (make-string (current-column) ?\ )))
  14303. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14304. (save-excursion
  14305. (goto-char (match-end 0))
  14306. (make-string (current-column) ?\ )))
  14307. (t nil)))
  14308. ;;; Other stuff.
  14309. (defun org-toggle-fixed-width-section (arg)
  14310. "Toggle the fixed-width export.
  14311. If there is no active region, the QUOTE keyword at the current headline is
  14312. inserted or removed. When present, it causes the text between this headline
  14313. and the next to be exported as fixed-width text, and unmodified.
  14314. If there is an active region, this command adds or removes a colon as the
  14315. first character of this line. If the first character of a line is a colon,
  14316. this line is also exported in fixed-width font."
  14317. (interactive "P")
  14318. (let* ((cc 0)
  14319. (regionp (org-region-active-p))
  14320. (beg (if regionp (region-beginning) (point)))
  14321. (end (if regionp (region-end)))
  14322. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14323. (case-fold-search nil)
  14324. (re "[ \t]*\\(: \\)")
  14325. off)
  14326. (if regionp
  14327. (save-excursion
  14328. (goto-char beg)
  14329. (setq cc (current-column))
  14330. (beginning-of-line 1)
  14331. (setq off (looking-at re))
  14332. (while (> nlines 0)
  14333. (setq nlines (1- nlines))
  14334. (beginning-of-line 1)
  14335. (cond
  14336. (arg
  14337. (org-move-to-column cc t)
  14338. (insert ": \n")
  14339. (forward-line -1))
  14340. ((and off (looking-at re))
  14341. (replace-match "" t t nil 1))
  14342. ((not off) (org-move-to-column cc t) (insert ": ")))
  14343. (forward-line 1)))
  14344. (save-excursion
  14345. (org-back-to-heading)
  14346. (if (looking-at (concat outline-regexp
  14347. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14348. (replace-match "" t t nil 1)
  14349. (if (looking-at outline-regexp)
  14350. (progn
  14351. (goto-char (match-end 0))
  14352. (insert org-quote-string " "))))))))
  14353. (defun org-reftex-citation ()
  14354. "Use reftex-citation to insert a citation into the buffer.
  14355. This looks for a line like
  14356. #+BIBLIOGRAPHY: foo plain option:-d
  14357. and derives from it that foo.bib is the bbliography file relevant
  14358. for this document. It then installs the necessary environment for RefTeX
  14359. to work in this buffer and calls `reftex-citation' to insert a citation
  14360. into the buffer.
  14361. Export of such citations to both LaTeX and HTML is handled by the contributed
  14362. package org-exp-bibtex by Taru Karttunen."
  14363. (interactive)
  14364. (let ((reftex-docstruct-symbol 'rds)
  14365. (reftex-cite-format "\\cite{%l}")
  14366. rds bib)
  14367. (save-excursion
  14368. (save-restriction
  14369. (widen)
  14370. (let ((case-fold-search t)
  14371. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14372. (if (not (save-excursion
  14373. (or (re-search-forward re nil t)
  14374. (re-search-backward re nil t))))
  14375. (error "No bibliography defined in file")
  14376. (setq bib (concat (match-string 1) ".bib")
  14377. rds (list (list 'bib bib)))))))
  14378. (call-interactively 'reftex-citation)))
  14379. ;;;; Functions extending outline functionality
  14380. (defun org-beginning-of-line (&optional arg)
  14381. "Go to the beginning of the current line. If that is invisible, continue
  14382. to a visible line beginning. This makes the function of C-a more intuitive.
  14383. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14384. first attempt, and only move to after the tags when the cursor is already
  14385. beyond the end of the headline."
  14386. (interactive "P")
  14387. (let ((pos (point))
  14388. (special (if (consp org-special-ctrl-a/e)
  14389. (car org-special-ctrl-a/e)
  14390. org-special-ctrl-a/e))
  14391. refpos)
  14392. (beginning-of-line 1)
  14393. (if (and arg (fboundp 'move-beginning-of-line))
  14394. (call-interactively 'move-beginning-of-line)
  14395. (if (bobp)
  14396. nil
  14397. (backward-char 1)
  14398. (if (org-invisible-p)
  14399. (while (and (not (bobp)) (org-invisible-p))
  14400. (backward-char 1)
  14401. (beginning-of-line 1))
  14402. (forward-char 1))))
  14403. (when special
  14404. (cond
  14405. ((and (looking-at org-complex-heading-regexp)
  14406. (= (char-after (match-end 1)) ?\ ))
  14407. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14408. (point-at-eol)))
  14409. (goto-char
  14410. (if (eq special t)
  14411. (cond ((> pos refpos) refpos)
  14412. ((= pos (point)) refpos)
  14413. (t (point)))
  14414. (cond ((> pos (point)) (point))
  14415. ((not (eq last-command this-command)) (point))
  14416. (t refpos)))))
  14417. ((org-at-item-p)
  14418. (goto-char
  14419. (if (eq special t)
  14420. (cond ((> pos (match-end 4)) (match-end 4))
  14421. ((= pos (point)) (match-end 4))
  14422. (t (point)))
  14423. (cond ((> pos (point)) (point))
  14424. ((not (eq last-command this-command)) (point))
  14425. (t (match-end 4))))))))
  14426. (org-no-warnings
  14427. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14428. (defun org-end-of-line (&optional arg)
  14429. "Go to the end of the line.
  14430. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14431. first attempt, and only move to after the tags when the cursor is already
  14432. beyond the end of the headline."
  14433. (interactive "P")
  14434. (let ((special (if (consp org-special-ctrl-a/e)
  14435. (cdr org-special-ctrl-a/e)
  14436. org-special-ctrl-a/e)))
  14437. (if (or (not special)
  14438. (not (org-on-heading-p))
  14439. arg)
  14440. (call-interactively (if (fboundp 'move-end-of-line)
  14441. 'move-end-of-line
  14442. 'end-of-line))
  14443. (let ((pos (point)))
  14444. (beginning-of-line 1)
  14445. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14446. (if (eq special t)
  14447. (if (or (< pos (match-beginning 1))
  14448. (= pos (match-end 0)))
  14449. (goto-char (match-beginning 1))
  14450. (goto-char (match-end 0)))
  14451. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14452. (goto-char (match-end 0))
  14453. (goto-char (match-beginning 1))))
  14454. (call-interactively (if (fboundp 'move-end-of-line)
  14455. 'move-end-of-line
  14456. 'end-of-line)))))
  14457. (org-no-warnings
  14458. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14459. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14460. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14461. (defun org-backward-sentence (&optional arg)
  14462. "Go to beginning of sentence, or beginning of table field.
  14463. This will call `backward-sentence' or `org-table-beginning-of-field',
  14464. depending on context."
  14465. (interactive "P")
  14466. (cond
  14467. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14468. (t (call-interactively 'backward-sentence))))
  14469. (defun org-forward-sentence (&optional arg)
  14470. "Go to end of sentence, or end of table field.
  14471. This will call `forward-sentence' or `org-table-end-of-field',
  14472. depending on context."
  14473. (interactive "P")
  14474. (cond
  14475. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14476. (t (call-interactively 'forward-sentence))))
  14477. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14478. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14479. (defun org-kill-line (&optional arg)
  14480. "Kill line, to tags or end of line."
  14481. (interactive "P")
  14482. (cond
  14483. ((or (not org-special-ctrl-k)
  14484. (bolp)
  14485. (not (org-on-heading-p)))
  14486. (call-interactively 'kill-line))
  14487. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14488. (kill-region (point) (match-beginning 1))
  14489. (org-set-tags nil t))
  14490. (t (kill-region (point) (point-at-eol)))))
  14491. (define-key org-mode-map "\C-k" 'org-kill-line)
  14492. (defun org-yank (&optional arg)
  14493. "Yank. If the kill is a subtree, treat it specially.
  14494. This command will look at the current kill and check if is a single
  14495. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14496. cursor is at the beginning of a line or after the stars of a currently
  14497. empty headline, then the yank is handled specially. How exactly depends
  14498. on the value of the following variables, both set by default.
  14499. org-yank-folded-subtrees
  14500. When set, the subtree(s) will be folded after insertion, but only
  14501. if doing so would now swallow text after the yanked text.
  14502. org-yank-adjusted-subtrees
  14503. When set, the subtree will be promoted or demoted in order to
  14504. fit into the local outline tree structure, which means that the level
  14505. will be adjusted so that it becomes the smaller one of the two
  14506. *visible* surrounding headings.
  14507. Any prefix to this command will cause `yank' to be called directly with
  14508. no special treatment. In particular, a simple `C-u' prefix will just
  14509. plainly yank the text as it is.
  14510. \[1] Basically, the test checks if the first non-white line is a heading
  14511. and if there are no other headings with fewer stars."
  14512. (interactive "P")
  14513. (setq this-command 'yank)
  14514. (if arg
  14515. (call-interactively 'yank)
  14516. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14517. (and (org-kill-is-subtree-p)
  14518. (or (bolp)
  14519. (and (looking-at "[ \t]*$")
  14520. (string-match
  14521. "\\`\\*+\\'"
  14522. (buffer-substring (point-at-bol) (point)))))))
  14523. swallowp)
  14524. (cond
  14525. ((and subtreep org-yank-folded-subtrees)
  14526. (let ((beg (point))
  14527. end)
  14528. (if (and subtreep org-yank-adjusted-subtrees)
  14529. (org-paste-subtree nil nil 'for-yank)
  14530. (call-interactively 'yank))
  14531. (setq end (point))
  14532. (goto-char beg)
  14533. (when (and (bolp) subtreep
  14534. (not (setq swallowp
  14535. (org-yank-folding-would-swallow-text beg end))))
  14536. (or (looking-at outline-regexp)
  14537. (re-search-forward (concat "^" outline-regexp) end t))
  14538. (while (and (< (point) end) (looking-at outline-regexp))
  14539. (hide-subtree)
  14540. (org-cycle-show-empty-lines 'folded)
  14541. (condition-case nil
  14542. (outline-forward-same-level 1)
  14543. (error (goto-char end)))))
  14544. (when swallowp
  14545. (message
  14546. "Yanked text not folded because that would swallow text"))
  14547. (goto-char end)
  14548. (skip-chars-forward " \t\n\r")
  14549. (beginning-of-line 1)
  14550. (push-mark beg 'nomsg)))
  14551. ((and subtreep org-yank-adjusted-subtrees)
  14552. (let ((beg (point-at-bol)))
  14553. (org-paste-subtree nil nil 'for-yank)
  14554. (push-mark beg 'nomsg)))
  14555. (t
  14556. (call-interactively 'yank))))))
  14557. (defun org-yank-folding-would-swallow-text (beg end)
  14558. "Would hide-subtree at BEG swallow any text after END?"
  14559. (let (level)
  14560. (save-excursion
  14561. (goto-char beg)
  14562. (when (or (looking-at outline-regexp)
  14563. (re-search-forward (concat "^" outline-regexp) end t))
  14564. (setq level (org-outline-level)))
  14565. (goto-char end)
  14566. (skip-chars-forward " \t\r\n\v\f")
  14567. (if (or (eobp)
  14568. (and (bolp) (looking-at org-outline-regexp)
  14569. (<= (org-outline-level) level)))
  14570. nil ; Nothing would be swallowed
  14571. t)))) ; something would swallow
  14572. (define-key org-mode-map "\C-y" 'org-yank)
  14573. (defun org-invisible-p ()
  14574. "Check if point is at a character currently not visible."
  14575. ;; Early versions of noutline don't have `outline-invisible-p'.
  14576. (if (fboundp 'outline-invisible-p)
  14577. (outline-invisible-p)
  14578. (get-char-property (point) 'invisible)))
  14579. (defun org-invisible-p2 ()
  14580. "Check if point is at a character currently not visible."
  14581. (save-excursion
  14582. (if (and (eolp) (not (bobp))) (backward-char 1))
  14583. ;; Early versions of noutline don't have `outline-invisible-p'.
  14584. (if (fboundp 'outline-invisible-p)
  14585. (outline-invisible-p)
  14586. (get-char-property (point) 'invisible))))
  14587. (defun org-back-to-heading (&optional invisible-ok)
  14588. "Call `outline-back-to-heading', but provide a better error message."
  14589. (condition-case nil
  14590. (outline-back-to-heading invisible-ok)
  14591. (error (error "Before first headline at position %d in buffer %s"
  14592. (point) (current-buffer)))))
  14593. (defun org-before-first-heading-p ()
  14594. "Before first heading?"
  14595. (save-excursion
  14596. (null (re-search-backward "^\\*+ " nil t))))
  14597. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14598. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14599. (defun org-at-heading-or-item-p ()
  14600. (or (org-on-heading-p) (org-at-item-p)))
  14601. (defun org-on-target-p ()
  14602. (or (org-in-regexp org-radio-target-regexp)
  14603. (org-in-regexp org-target-regexp)))
  14604. (defun org-up-heading-all (arg)
  14605. "Move to the heading line of which the present line is a subheading.
  14606. This function considers both visible and invisible heading lines.
  14607. With argument, move up ARG levels."
  14608. (if (fboundp 'outline-up-heading-all)
  14609. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14610. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14611. (defun org-up-heading-safe ()
  14612. "Move to the heading line of which the present line is a subheading.
  14613. This version will not throw an error. It will return the level of the
  14614. headline found, or nil if no higher level is found.
  14615. Also, this function will be a lot faster than `outline-up-heading',
  14616. because it relies on stars being the outline starters. This can really
  14617. make a significant difference in outlines with very many siblings."
  14618. (let (start-level re)
  14619. (org-back-to-heading t)
  14620. (setq start-level (funcall outline-level))
  14621. (if (equal start-level 1)
  14622. nil
  14623. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14624. (if (re-search-backward re nil t)
  14625. (funcall outline-level)))))
  14626. (defun org-first-sibling-p ()
  14627. "Is this heading the first child of its parents?"
  14628. (interactive)
  14629. (let ((re (concat "^" outline-regexp))
  14630. level l)
  14631. (unless (org-at-heading-p t)
  14632. (error "Not at a heading"))
  14633. (setq level (funcall outline-level))
  14634. (save-excursion
  14635. (if (not (re-search-backward re nil t))
  14636. t
  14637. (setq l (funcall outline-level))
  14638. (< l level)))))
  14639. (defun org-goto-sibling (&optional previous)
  14640. "Goto the next sibling, even if it is invisible.
  14641. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14642. when a sibling was found. When none is found, return nil and don't
  14643. move point."
  14644. (let ((fun (if previous 're-search-backward 're-search-forward))
  14645. (pos (point))
  14646. (re (concat "^" outline-regexp))
  14647. level l)
  14648. (when (condition-case nil (org-back-to-heading t) (error nil))
  14649. (setq level (funcall outline-level))
  14650. (catch 'exit
  14651. (or previous (forward-char 1))
  14652. (while (funcall fun re nil t)
  14653. (setq l (funcall outline-level))
  14654. (when (< l level) (goto-char pos) (throw 'exit nil))
  14655. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14656. (goto-char pos)
  14657. nil))))
  14658. (defun org-show-siblings ()
  14659. "Show all siblings of the current headline."
  14660. (save-excursion
  14661. (while (org-goto-sibling) (org-flag-heading nil)))
  14662. (save-excursion
  14663. (while (org-goto-sibling 'previous)
  14664. (org-flag-heading nil))))
  14665. (defun org-show-hidden-entry ()
  14666. "Show an entry where even the heading is hidden."
  14667. (save-excursion
  14668. (org-show-entry)))
  14669. (defun org-flag-heading (flag &optional entry)
  14670. "Flag the current heading. FLAG non-nil means make invisible.
  14671. When ENTRY is non-nil, show the entire entry."
  14672. (save-excursion
  14673. (org-back-to-heading t)
  14674. ;; Check if we should show the entire entry
  14675. (if entry
  14676. (progn
  14677. (org-show-entry)
  14678. (save-excursion
  14679. (and (outline-next-heading)
  14680. (org-flag-heading nil))))
  14681. (outline-flag-region (max (point-min) (1- (point)))
  14682. (save-excursion (outline-end-of-heading) (point))
  14683. flag))))
  14684. (defun org-forward-same-level (arg)
  14685. "Move forward to the ARG'th subheading at same level as this one.
  14686. Stop at the first and last subheadings of a superior heading.
  14687. This is like outline-forward-same-level, but invisible headings are ok."
  14688. (interactive "p")
  14689. (org-back-to-heading t)
  14690. (while (> arg 0)
  14691. (let ((point-to-move-to (save-excursion
  14692. (org-get-next-sibling))))
  14693. (if point-to-move-to
  14694. (progn
  14695. (goto-char point-to-move-to)
  14696. (setq arg (1- arg)))
  14697. (progn
  14698. (setq arg 0)
  14699. (error "No following same-level heading"))))))
  14700. (defun org-get-next-sibling ()
  14701. "Move to next heading of the same level, and return point.
  14702. If there is no such heading, return nil.
  14703. This is like outline-next-sibling, but invisible headings are ok."
  14704. (let ((level (funcall outline-level)))
  14705. (outline-next-heading)
  14706. (while (and (not (eobp)) (> (funcall outline-level) level))
  14707. (outline-next-heading))
  14708. (if (or (eobp) (< (funcall outline-level) level))
  14709. nil
  14710. (point))))
  14711. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14712. ;; This is an exact copy of the original function, but it uses
  14713. ;; `org-back-to-heading', to make it work also in invisible
  14714. ;; trees. And is uses an invisible-OK argument.
  14715. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14716. (org-back-to-heading invisible-OK)
  14717. (let ((first t)
  14718. (level (funcall outline-level)))
  14719. (while (and (not (eobp))
  14720. (or first (> (funcall outline-level) level)))
  14721. (setq first nil)
  14722. (outline-next-heading))
  14723. (unless to-heading
  14724. (if (memq (preceding-char) '(?\n ?\^M))
  14725. (progn
  14726. ;; Go to end of line before heading
  14727. (forward-char -1)
  14728. (if (memq (preceding-char) '(?\n ?\^M))
  14729. ;; leave blank line before heading
  14730. (forward-char -1))))))
  14731. (point))
  14732. (defun org-show-subtree ()
  14733. "Show everything after this heading at deeper levels."
  14734. (outline-flag-region
  14735. (point)
  14736. (save-excursion
  14737. (outline-end-of-subtree) (outline-next-heading) (point))
  14738. nil))
  14739. (defun org-show-entry ()
  14740. "Show the body directly following this heading.
  14741. Show the heading too, if it is currently invisible."
  14742. (interactive)
  14743. (save-excursion
  14744. (condition-case nil
  14745. (progn
  14746. (org-back-to-heading t)
  14747. (outline-flag-region
  14748. (max (point-min) (1- (point)))
  14749. (save-excursion
  14750. (if (re-search-forward
  14751. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14752. (match-beginning 1)
  14753. (point-max)))
  14754. nil))
  14755. (error nil))))
  14756. (defun org-make-options-regexp (kwds &optional extra)
  14757. "Make a regular expression for keyword lines."
  14758. (concat
  14759. "^"
  14760. "#?[ \t]*\\+\\("
  14761. (mapconcat 'regexp-quote kwds "\\|")
  14762. (if extra (concat "\\|" extra))
  14763. "\\):[ \t]*"
  14764. "\\(.+\\)"))
  14765. ;; Make isearch reveal the necessary context
  14766. (defun org-isearch-end ()
  14767. "Reveal context after isearch exits."
  14768. (when isearch-success ; only if search was successful
  14769. (if (featurep 'xemacs)
  14770. ;; Under XEmacs, the hook is run in the correct place,
  14771. ;; we directly show the context.
  14772. (org-show-context 'isearch)
  14773. ;; In Emacs the hook runs *before* restoring the overlays.
  14774. ;; So we have to use a one-time post-command-hook to do this.
  14775. ;; (Emacs 22 has a special variable, see function `org-mode')
  14776. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14777. isearch-mode-end-hook-quit)
  14778. ;; Only when the isearch was not quitted.
  14779. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14780. 'append 'local)))))
  14781. (defun org-isearch-post-command ()
  14782. "Remove self from hook, and show context."
  14783. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14784. (org-show-context 'isearch))
  14785. ;;;; Integration with and fixes for other packages
  14786. ;;; Imenu support
  14787. (defvar org-imenu-markers nil
  14788. "All markers currently used by Imenu.")
  14789. (make-variable-buffer-local 'org-imenu-markers)
  14790. (defun org-imenu-new-marker (&optional pos)
  14791. "Return a new marker for use by Imenu, and remember the marker."
  14792. (let ((m (make-marker)))
  14793. (move-marker m (or pos (point)))
  14794. (push m org-imenu-markers)
  14795. m))
  14796. (defun org-imenu-get-tree ()
  14797. "Produce the index for Imenu."
  14798. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14799. (setq org-imenu-markers nil)
  14800. (let* ((n org-imenu-depth)
  14801. (re (concat "^" outline-regexp))
  14802. (subs (make-vector (1+ n) nil))
  14803. (last-level 0)
  14804. m level head)
  14805. (save-excursion
  14806. (save-restriction
  14807. (widen)
  14808. (goto-char (point-max))
  14809. (while (re-search-backward re nil t)
  14810. (setq level (org-reduced-level (funcall outline-level)))
  14811. (when (<= level n)
  14812. (looking-at org-complex-heading-regexp)
  14813. (setq head (org-link-display-format
  14814. (org-match-string-no-properties 4))
  14815. m (org-imenu-new-marker))
  14816. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14817. (if (>= level last-level)
  14818. (push (cons head m) (aref subs level))
  14819. (push (cons head (aref subs (1+ level))) (aref subs level))
  14820. (loop for i from (1+ level) to n do (aset subs i nil)))
  14821. (setq last-level level)))))
  14822. (aref subs 1)))
  14823. (eval-after-load "imenu"
  14824. '(progn
  14825. (add-hook 'imenu-after-jump-hook
  14826. (lambda ()
  14827. (if (eq major-mode 'org-mode)
  14828. (org-show-context 'org-goto))))))
  14829. (defun org-link-display-format (link)
  14830. "Replace a link with either the description, or the link target
  14831. if no description is present"
  14832. (save-match-data
  14833. (if (string-match org-bracket-link-analytic-regexp link)
  14834. (replace-match (or (match-string 5 link)
  14835. (concat (match-string 1 link)
  14836. (match-string 3 link)))
  14837. nil nil link)
  14838. link)))
  14839. ;; Speedbar support
  14840. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14841. "Overlay marking the agenda restriction line in speedbar.")
  14842. (org-overlay-put org-speedbar-restriction-lock-overlay
  14843. 'face 'org-agenda-restriction-lock)
  14844. (org-overlay-put org-speedbar-restriction-lock-overlay
  14845. 'help-echo "Agendas are currently limited to this item.")
  14846. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14847. (defun org-speedbar-set-agenda-restriction ()
  14848. "Restrict future agenda commands to the location at point in speedbar.
  14849. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14850. (interactive)
  14851. (require 'org-agenda)
  14852. (let (p m tp np dir txt)
  14853. (cond
  14854. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14855. 'org-imenu t))
  14856. (setq m (get-text-property p 'org-imenu-marker))
  14857. (save-excursion
  14858. (save-restriction
  14859. (set-buffer (marker-buffer m))
  14860. (goto-char m)
  14861. (org-agenda-set-restriction-lock 'subtree))))
  14862. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14863. 'speedbar-function 'speedbar-find-file))
  14864. (setq tp (previous-single-property-change
  14865. (1+ p) 'speedbar-function)
  14866. np (next-single-property-change
  14867. tp 'speedbar-function)
  14868. dir (speedbar-line-directory)
  14869. txt (buffer-substring-no-properties (or tp (point-min))
  14870. (or np (point-max))))
  14871. (save-excursion
  14872. (save-restriction
  14873. (set-buffer (find-file-noselect
  14874. (let ((default-directory dir))
  14875. (expand-file-name txt))))
  14876. (unless (org-mode-p)
  14877. (error "Cannot restrict to non-Org-mode file"))
  14878. (org-agenda-set-restriction-lock 'file))))
  14879. (t (error "Don't know how to restrict Org-mode's agenda")))
  14880. (org-move-overlay org-speedbar-restriction-lock-overlay
  14881. (point-at-bol) (point-at-eol))
  14882. (setq current-prefix-arg nil)
  14883. (org-agenda-maybe-redo)))
  14884. (eval-after-load "speedbar"
  14885. '(progn
  14886. (speedbar-add-supported-extension ".org")
  14887. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14888. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14889. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14890. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14891. (add-hook 'speedbar-visiting-tag-hook
  14892. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14893. ;;; Fixes and Hacks for problems with other packages
  14894. ;; Make flyspell not check words in links, to not mess up our keymap
  14895. (defun org-mode-flyspell-verify ()
  14896. "Don't let flyspell put overlays at active buttons."
  14897. (not (get-text-property (point) 'keymap)))
  14898. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14899. (eval-after-load "bookmark"
  14900. '(if (boundp 'bookmark-after-jump-hook)
  14901. ;; We can use the hook
  14902. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14903. ;; Hook not available, use advice
  14904. (defadvice bookmark-jump (after org-make-visible activate)
  14905. "Make the position visible."
  14906. (org-bookmark-jump-unhide))))
  14907. ;; Make sure saveplace show the location if it was hidden
  14908. (eval-after-load "saveplace"
  14909. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14910. "Make the position visible."
  14911. (org-bookmark-jump-unhide)))
  14912. (defun org-bookmark-jump-unhide ()
  14913. "Unhide the current position, to show the bookmark location."
  14914. (and (org-mode-p)
  14915. (or (org-invisible-p)
  14916. (save-excursion (goto-char (max (point-min) (1- (point))))
  14917. (org-invisible-p)))
  14918. (org-show-context 'bookmark-jump)))
  14919. ;; Make session.el ignore our circular variable
  14920. (eval-after-load "session"
  14921. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14922. ;;;; Experimental code
  14923. (defun org-closed-in-range ()
  14924. "Sparse tree of items closed in a certain time range.
  14925. Still experimental, may disappear in the future."
  14926. (interactive)
  14927. ;; Get the time interval from the user.
  14928. (let* ((time1 (time-to-seconds
  14929. (org-read-date nil 'to-time nil "Starting date: ")))
  14930. (time2 (time-to-seconds
  14931. (org-read-date nil 'to-time nil "End date:")))
  14932. ;; callback function
  14933. (callback (lambda ()
  14934. (let ((time
  14935. (time-to-seconds
  14936. (apply 'encode-time
  14937. (org-parse-time-string
  14938. (match-string 1))))))
  14939. ;; check if time in interval
  14940. (and (>= time time1) (<= time time2))))))
  14941. ;; make tree, check each match with the callback
  14942. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14943. ;;;; Finish up
  14944. (provide 'org)
  14945. (run-hooks 'org-load-hook)
  14946. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14947. ;;; org.el ends here