org.el 664 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824
  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.33trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.33trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((origin default-directory)
  96. (version org-version)
  97. (git-version)
  98. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  99. (when (and (file-exists-p (expand-file-name ".git" dir))
  100. (executable-find "git"))
  101. (unwind-protect
  102. (progn
  103. (cd dir)
  104. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  105. (with-current-buffer "*Shell Command Output*"
  106. (goto-char (point-min))
  107. (setq git-version (buffer-substring (point) (point-at-eol))))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (when (string-match "\\S-"
  110. (shell-command-to-string
  111. "git diff-index --name-only HEAD --"))
  112. (setq git-version (concat git-version ".dirty")))
  113. (setq version (concat version " (" git-version ")"))))
  114. (cd origin)))
  115. (setq version (format "Org-mode version %s" version))
  116. (if here (insert version))
  117. (message version)))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (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)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  171. (const :tag " id: Global IDs for identifying entries" org-id)
  172. (const :tag " info: Links to Info nodes" org-info)
  173. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  174. (const :tag " habit: Track your consistency with habits" org-habit)
  175. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  176. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  177. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  178. (const :tag " mew Links to Mew folders/messages" org-mew)
  179. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  180. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  181. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  182. (const :tag " vm: Links to VM folders/messages" org-vm)
  183. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  184. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  185. (const :tag " mouse: Additional mouse support" org-mouse)
  186. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  187. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  188. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  189. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  190. (const :tag "C collector: Collect properties into tables" org-collector)
  191. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  192. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  193. (const :tag "C eval: Include command output as text" org-eval)
  194. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  195. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  196. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  197. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  198. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  199. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  200. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  201. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  202. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  203. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  204. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  205. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  206. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  207. (const :tag "C R: Computation using the R language" org-R)
  208. (const :tag "C registry: A registry for Org-mode links" org-registry)
  209. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  210. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  211. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  212. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  213. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  214. (const :tag "C track: Keep up with Org-mode development" org-track)
  215. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  216. (defcustom org-support-shift-select nil
  217. "Non-nil means, make shift-cursor commands select text when possible.
  218. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  219. selecting a region, or enlarge thusly regions started in this way.
  220. In Org-mode, in special contexts, these same keys are used for other
  221. purposes, important enough to compete with shift selection. Org tries
  222. to balance these needs by supporting `shift-select-mode' outside these
  223. special contexts, under control of this variable.
  224. The default of this variable is nil, to avoid confusing behavior. Shifted
  225. cursor keys will then execute Org commands in the following contexts:
  226. - on a headline, changing TODO state (left/right) and priority (up/down)
  227. - on a time stamp, changing the time
  228. - in a plain list item, changing the bullet type
  229. - in a property definition line, switching between allowed values
  230. - in the BEGIN line of a clock table (changing the time block).
  231. Outside these contexts, the commands will throw an error.
  232. When this variable is t and the cursor is not in a special context,
  233. Org-mode will support shift-selection for making and enlarging regions.
  234. To make this more effective, the bullet cycling will no longer happen
  235. anywhere in an item line, but only if the cursor is exactly on the bullet.
  236. If you set this variable to the symbol `always', then the keys
  237. will not be special in headlines, property lines, and item lines, to make
  238. shift selection work there as well. If this is what you want, you can
  239. use the following alternative commands: `C-c C-t' and `C-c ,' to
  240. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  241. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  242. edited by hand or in column view.
  243. However, when the cursor is on a timestamp, shift-cursor commands
  244. will still edit the time stamp - this is just too good to give up.
  245. XEmacs user should have this variable set to nil, because shift-select-mode
  246. is Emacs 23 only."
  247. :group 'org
  248. :type '(choice
  249. (const :tag "Never" nil)
  250. (const :tag "When outside special context" t)
  251. (const :tag "Everywhere except timestamps" always)))
  252. (defgroup org-startup nil
  253. "Options concerning startup of Org-mode."
  254. :tag "Org Startup"
  255. :group 'org)
  256. (defcustom org-startup-folded t
  257. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  258. This can also be configured on a per-file basis by adding one of
  259. the following lines anywhere in the buffer:
  260. #+STARTUP: fold (or `overview', this is equivalent)
  261. #+STARTUP: nofold (or `showall', this is equivalent)
  262. #+STARTUP: content
  263. #+STARTUP: showeverything"
  264. :group 'org-startup
  265. :type '(choice
  266. (const :tag "nofold: show all" nil)
  267. (const :tag "fold: overview" t)
  268. (const :tag "content: all headlines" content)
  269. (const :tag "show everything, even drawers" showeverything)))
  270. (defcustom org-startup-truncated t
  271. "Non-nil means, entering Org-mode will set `truncate-lines'.
  272. This is useful since some lines containing links can be very long and
  273. uninteresting. Also tables look terrible when wrapped."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-startup-indented nil
  277. "Non-nil means, turn on `org-indent-mode' on startup.
  278. This can also be configured on a per-file basis by adding one of
  279. the following lines anywhere in the buffer:
  280. #+STARTUP: indent
  281. #+STARTUP: noindent"
  282. :group 'org-structure
  283. :type '(choice
  284. (const :tag "Not" nil)
  285. (const :tag "Globally (slow on startup in large files)" t)))
  286. (defcustom org-startup-align-all-tables nil
  287. "Non-nil means, align all tables when visiting a file.
  288. This is useful when the column width in tables is forced with <N> cookies
  289. in table fields. Such tables will look correct only after the first re-align.
  290. This can also be configured on a per-file basis by adding one of
  291. the following lines anywhere in the buffer:
  292. #+STARTUP: align
  293. #+STARTUP: noalign"
  294. :group 'org-startup
  295. :type 'boolean)
  296. (defcustom org-insert-mode-line-in-empty-file nil
  297. "Non-nil means insert the first line setting Org-mode in empty files.
  298. When the function `org-mode' is called interactively in an empty file, this
  299. normally means that the file name does not automatically trigger Org-mode.
  300. To ensure that the file will always be in Org-mode in the future, a
  301. line enforcing Org-mode will be inserted into the buffer, if this option
  302. has been set."
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-replace-disputed-keys nil
  306. "Non-nil means use alternative key bindings for some keys.
  307. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  308. These keys are also used by other packages like shift-selection-mode'
  309. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  310. If you want to use Org-mode together with one of these other modes,
  311. or more generally if you would like to move some Org-mode commands to
  312. other keys, set this variable and configure the keys with the variable
  313. `org-disputed-keys'.
  314. This option is only relevant at load-time of Org-mode, and must be set
  315. *before* org.el is loaded. Changing it requires a restart of Emacs to
  316. become effective."
  317. :group 'org-startup
  318. :type 'boolean)
  319. (defcustom org-use-extra-keys nil
  320. "Non-nil means use extra key sequence definitions for certain
  321. commands. This happens automatically if you run XEmacs or if
  322. window-system is nil. This variable lets you do the same
  323. manually. You must set it before loading org.
  324. Example: on Carbon Emacs 22 running graphically, with an external
  325. keyboard on a Powerbook, the default way of setting M-left might
  326. not work for either Alt or ESC. Setting this variable will make
  327. it work for ESC."
  328. :group 'org-startup
  329. :type 'boolean)
  330. (if (fboundp 'defvaralias)
  331. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  332. (defcustom org-disputed-keys
  333. '(([(shift up)] . [(meta p)])
  334. ([(shift down)] . [(meta n)])
  335. ([(shift left)] . [(meta -)])
  336. ([(shift right)] . [(meta +)])
  337. ([(control shift right)] . [(meta shift +)])
  338. ([(control shift left)] . [(meta shift -)]))
  339. "Keys for which Org-mode and other modes compete.
  340. This is an alist, cars are the default keys, second element specifies
  341. the alternative to use when `org-replace-disputed-keys' is t.
  342. Keys can be specified in any syntax supported by `define-key'.
  343. The value of this option takes effect only at Org-mode's startup,
  344. therefore you'll have to restart Emacs to apply it after changing."
  345. :group 'org-startup
  346. :type 'alist)
  347. (defun org-key (key)
  348. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  349. Or return the original if not disputed."
  350. (if org-replace-disputed-keys
  351. (let* ((nkey (key-description key))
  352. (x (org-find-if (lambda (x)
  353. (equal (key-description (car x)) nkey))
  354. org-disputed-keys)))
  355. (if x (cdr x) key))
  356. key))
  357. (defun org-find-if (predicate seq)
  358. (catch 'exit
  359. (while seq
  360. (if (funcall predicate (car seq))
  361. (throw 'exit (car seq))
  362. (pop seq)))))
  363. (defun org-defkey (keymap key def)
  364. "Define a key, possibly translated, as returned by `org-key'."
  365. (define-key keymap (org-key key) def))
  366. (defcustom org-ellipsis nil
  367. "The ellipsis to use in the Org-mode outline.
  368. When nil, just use the standard three dots. When a string, use that instead,
  369. When a face, use the standard 3 dots, but with the specified face.
  370. The change affects only Org-mode (which will then use its own display table).
  371. Changing this requires executing `M-x org-mode' in a buffer to become
  372. effective."
  373. :group 'org-startup
  374. :type '(choice (const :tag "Default" nil)
  375. (face :tag "Face" :value org-warning)
  376. (string :tag "String" :value "...#")))
  377. (defvar org-display-table nil
  378. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  379. (defgroup org-keywords nil
  380. "Keywords in Org-mode."
  381. :tag "Org Keywords"
  382. :group 'org)
  383. (defcustom org-deadline-string "DEADLINE:"
  384. "String to mark deadline entries.
  385. A deadline is this string, followed by a time stamp. Should be a word,
  386. terminated by a colon. You can insert a schedule keyword and
  387. a timestamp with \\[org-deadline].
  388. Changes become only effective after restarting Emacs."
  389. :group 'org-keywords
  390. :type 'string)
  391. (defcustom org-scheduled-string "SCHEDULED:"
  392. "String to mark scheduled TODO entries.
  393. A schedule is this string, followed by a time stamp. Should be a word,
  394. terminated by a colon. You can insert a schedule keyword and
  395. a timestamp with \\[org-schedule].
  396. Changes become only effective after restarting Emacs."
  397. :group 'org-keywords
  398. :type 'string)
  399. (defcustom org-closed-string "CLOSED:"
  400. "String used as the prefix for timestamps logging closing a TODO entry."
  401. :group 'org-keywords
  402. :type 'string)
  403. (defcustom org-clock-string "CLOCK:"
  404. "String used as prefix for timestamps clocking work hours on an item."
  405. :group 'org-keywords
  406. :type 'string)
  407. (defcustom org-comment-string "COMMENT"
  408. "Entries starting with this keyword will never be exported.
  409. An entry can be toggled between COMMENT and normal with
  410. \\[org-toggle-comment].
  411. Changes become only effective after restarting Emacs."
  412. :group 'org-keywords
  413. :type 'string)
  414. (defcustom org-quote-string "QUOTE"
  415. "Entries starting with this keyword will be exported in fixed-width font.
  416. Quoting applies only to the text in the entry following the headline, and does
  417. not extend beyond the next headline, even if that is lower level.
  418. An entry can be toggled between QUOTE and normal with
  419. \\[org-toggle-fixed-width-section]."
  420. :group 'org-keywords
  421. :type 'string)
  422. (defconst org-repeat-re
  423. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  424. "Regular expression for specifying repeated events.
  425. After a match, group 1 contains the repeat expression.")
  426. (defgroup org-structure nil
  427. "Options concerning the general structure of Org-mode files."
  428. :tag "Org Structure"
  429. :group 'org)
  430. (defgroup org-reveal-location nil
  431. "Options about how to make context of a location visible."
  432. :tag "Org Reveal Location"
  433. :group 'org-structure)
  434. (defconst org-context-choice
  435. '(choice
  436. (const :tag "Always" t)
  437. (const :tag "Never" nil)
  438. (repeat :greedy t :tag "Individual contexts"
  439. (cons
  440. (choice :tag "Context"
  441. (const agenda)
  442. (const org-goto)
  443. (const occur-tree)
  444. (const tags-tree)
  445. (const link-search)
  446. (const mark-goto)
  447. (const bookmark-jump)
  448. (const isearch)
  449. (const default))
  450. (boolean))))
  451. "Contexts for the reveal options.")
  452. (defcustom org-show-hierarchy-above '((default . t))
  453. "Non-nil means, show full hierarchy when revealing a location.
  454. Org-mode often shows locations in an org-mode file which might have
  455. been invisible before. When this is set, the hierarchy of headings
  456. above the exposed location is shown.
  457. Turning this off for example for sparse trees makes them very compact.
  458. Instead of t, this can also be an alist specifying this option for different
  459. contexts. Valid contexts are
  460. agenda when exposing an entry from the agenda
  461. org-goto when using the command `org-goto' on key C-c C-j
  462. occur-tree when using the command `org-occur' on key C-c /
  463. tags-tree when constructing a sparse tree based on tags matches
  464. link-search when exposing search matches associated with a link
  465. mark-goto when exposing the jump goal of a mark
  466. bookmark-jump when exposing a bookmark location
  467. isearch when exiting from an incremental search
  468. default default for all contexts not set explicitly"
  469. :group 'org-reveal-location
  470. :type org-context-choice)
  471. (defcustom org-show-following-heading '((default . nil))
  472. "Non-nil means, show following heading when revealing a location.
  473. Org-mode often shows locations in an org-mode file which might have
  474. been invisible before. When this is set, the heading following the
  475. match is shown.
  476. Turning this off for example for sparse trees makes them very compact,
  477. but makes it harder to edit the location of the match. In such a case,
  478. use the command \\[org-reveal] to show more context.
  479. Instead of t, this can also be an alist specifying this option for different
  480. contexts. See `org-show-hierarchy-above' for valid contexts."
  481. :group 'org-reveal-location
  482. :type org-context-choice)
  483. (defcustom org-show-siblings '((default . nil) (isearch t))
  484. "Non-nil means, show all sibling heading when revealing a location.
  485. Org-mode often shows locations in an org-mode file which might have
  486. been invisible before. When this is set, the sibling of the current entry
  487. heading are all made visible. If `org-show-hierarchy-above' is t,
  488. the same happens on each level of the hierarchy above the current entry.
  489. By default this is on for the isearch context, off for all other contexts.
  490. Turning this off for example for sparse trees makes them very compact,
  491. but makes it harder to edit the location of the match. In such a case,
  492. use the command \\[org-reveal] to show more context.
  493. Instead of t, this can also be an alist specifying this option for different
  494. contexts. See `org-show-hierarchy-above' for valid contexts."
  495. :group 'org-reveal-location
  496. :type org-context-choice)
  497. (defcustom org-show-entry-below '((default . nil))
  498. "Non-nil means, show the entry below a headline when revealing a location.
  499. Org-mode often shows locations in an org-mode file which might have
  500. been invisible before. When this is set, the text below the headline that is
  501. exposed is also shown.
  502. By default this is off for all contexts.
  503. Instead of t, this can also be an alist specifying this option for different
  504. contexts. See `org-show-hierarchy-above' for valid contexts."
  505. :group 'org-reveal-location
  506. :type org-context-choice)
  507. (defcustom org-indirect-buffer-display 'other-window
  508. "How should indirect tree buffers be displayed?
  509. This applies to indirect buffers created with the commands
  510. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  511. Valid values are:
  512. current-window Display in the current window
  513. other-window Just display in another window.
  514. dedicated-frame Create one new frame, and re-use it each time.
  515. new-frame Make a new frame each time. Note that in this case
  516. previously-made indirect buffers are kept, and you need to
  517. kill these buffers yourself."
  518. :group 'org-structure
  519. :group 'org-agenda-windows
  520. :type '(choice
  521. (const :tag "In current window" current-window)
  522. (const :tag "In current frame, other window" other-window)
  523. (const :tag "Each time a new frame" new-frame)
  524. (const :tag "One dedicated frame" dedicated-frame)))
  525. (defcustom org-use-speed-commands nil
  526. "Non-nil means, activate single letter commands at beginning of a headline.
  527. This may also be a function to test for appropriate locations where speed
  528. commands should be active."
  529. :group 'org-structure
  530. :type '(choice
  531. (const :tag "Never" nil)
  532. (const :tag "At beginning of headline stars" t)
  533. (function)))
  534. (defcustom org-speed-commands-user nil
  535. "Alist of additional speed commands.
  536. This list will be checked before `org-speed-commands-default'
  537. when the variable `org-use-speed-commands' is non-nil
  538. and when the cursor is at the beginning of a headline.
  539. The car if each entry is a string with a single letter, which must
  540. be assigned to `self-insert-command' in the global map.
  541. The cdr is either a command to be called interactively, a function
  542. to be called, or a form to be evaluated.
  543. An entry that is just a list with a single string will be interpreted
  544. as a descriptive headline that will be added when listing the speed
  545. copmmands in the Help buffer using the `?' speed command."
  546. :group 'org-structure
  547. :type '(repeat :value ("k" . ignore)
  548. (choice :value ("k" . ignore)
  549. (list :tag "Descriptive Headline" (string :tag "Headline"))
  550. (cons :tag "Letter and Command"
  551. (string :tag "Command letter")
  552. (choice
  553. (function)
  554. (sexp))))))
  555. (defgroup org-cycle nil
  556. "Options concerning visibility cycling in Org-mode."
  557. :tag "Org Cycle"
  558. :group 'org-structure)
  559. (defcustom org-cycle-skip-children-state-if-no-children t
  560. "Non-nil means, skip CHILDREN state in entries that don't have any."
  561. :group 'org-cycle
  562. :type 'boolean)
  563. (defcustom org-cycle-max-level nil
  564. "Maximum level which should still be subject to visibility cycling.
  565. Levels higher than this will, for cycling, be treated as text, not a headline.
  566. When `org-odd-levels-only' is set, a value of N in this variable actually
  567. means 2N-1 stars as the limiting headline.
  568. When nil, cycle all levels.
  569. Note that the limiting level of cycling is also influenced by
  570. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  571. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  572. than its value."
  573. :group 'org-cycle
  574. :type '(choice
  575. (const :tag "No limit" nil)
  576. (integer :tag "Maximum level")))
  577. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  578. "Names of drawers. Drawers are not opened by cycling on the headline above.
  579. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  580. this:
  581. :DRAWERNAME:
  582. .....
  583. :END:
  584. The drawer \"PROPERTIES\" is special for capturing properties through
  585. the property API.
  586. Drawers can be defined on the per-file basis with a line like:
  587. #+DRAWERS: HIDDEN STATE PROPERTIES"
  588. :group 'org-structure
  589. :group 'org-cycle
  590. :type '(repeat (string :tag "Drawer Name")))
  591. (defcustom org-hide-block-startup nil
  592. "Non-nil means, , entering Org-mode will fold all blocks.
  593. This can also be set in on a per-file basis with
  594. #+STARTUP: hideblocks
  595. #+STARTUP: showblocks"
  596. :group 'org-startup
  597. :group 'org-cycle
  598. :type 'boolean)
  599. (defcustom org-cycle-global-at-bob nil
  600. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  601. This makes it possible to do global cycling without having to use S-TAB or
  602. C-u TAB. For this special case to work, the first line of the buffer
  603. must not be a headline - it may be empty or some other text. When used in
  604. this way, `org-cycle-hook' is disables temporarily, to make sure the
  605. cursor stays at the beginning of the buffer.
  606. When this option is nil, don't do anything special at the beginning
  607. of the buffer."
  608. :group 'org-cycle
  609. :type 'boolean)
  610. (defcustom org-cycle-level-after-item/entry-creation t
  611. "Non-nil means, cycle entry level or item indentation in new empty entries.
  612. When the cursor is at the end of an empty headline, i.e with only stars
  613. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  614. and then all possible anchestor states, before returning to the original state.
  615. This makes data entry extremely fast: M-RET to create a new headline,
  616. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  617. When the cursor is at the end of an empty plain list item, one TAB will
  618. make it a subitem, two or more tabs will back up to make this an item
  619. higher up in the item hierarchy."
  620. :group 'org-cycle
  621. :type 'boolean)
  622. (defcustom org-cycle-emulate-tab t
  623. "Where should `org-cycle' emulate TAB.
  624. nil Never
  625. white Only in completely white lines
  626. whitestart Only at the beginning of lines, before the first non-white char
  627. t Everywhere except in headlines
  628. exc-hl-bol Everywhere except at the start of a headline
  629. If TAB is used in a place where it does not emulate TAB, the current subtree
  630. visibility is cycled."
  631. :group 'org-cycle
  632. :type '(choice (const :tag "Never" nil)
  633. (const :tag "Only in completely white lines" white)
  634. (const :tag "Before first char in a line" whitestart)
  635. (const :tag "Everywhere except in headlines" t)
  636. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  637. ))
  638. (defcustom org-cycle-separator-lines 2
  639. "Number of empty lines needed to keep an empty line between collapsed trees.
  640. If you leave an empty line between the end of a subtree and the following
  641. headline, this empty line is hidden when the subtree is folded.
  642. Org-mode will leave (exactly) one empty line visible if the number of
  643. empty lines is equal or larger to the number given in this variable.
  644. So the default 2 means, at least 2 empty lines after the end of a subtree
  645. are needed to produce free space between a collapsed subtree and the
  646. following headline.
  647. If the number is negative, and the number of empty lines is at least -N,
  648. all empty lines are shown.
  649. Special case: when 0, never leave empty lines in collapsed view."
  650. :group 'org-cycle
  651. :type 'integer)
  652. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  653. (defcustom org-pre-cycle-hook nil
  654. "Hook that is run before visibility cycling is happening.
  655. The function(s) in this hook must accept a single argument which indicates
  656. the new state that will be set right after running this hook. The
  657. argument is a symbol. Before a global state change, it can have the values
  658. `overview', `content', or `all'. Before a local state change, it can have
  659. the values `folded', `children', or `subtree'."
  660. :group 'org-cycle
  661. :type 'hook)
  662. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  663. org-cycle-hide-drawers
  664. org-cycle-show-empty-lines
  665. org-optimize-window-after-visibility-change)
  666. "Hook that is run after `org-cycle' has changed the buffer visibility.
  667. The function(s) in this hook must accept a single argument which indicates
  668. the new state that was set by the most recent `org-cycle' command. The
  669. argument is a symbol. After a global state change, it can have the values
  670. `overview', `content', or `all'. After a local state change, it can have
  671. the values `folded', `children', or `subtree'."
  672. :group 'org-cycle
  673. :type 'hook)
  674. (defgroup org-edit-structure nil
  675. "Options concerning structure editing in Org-mode."
  676. :tag "Org Edit Structure"
  677. :group 'org-structure)
  678. (defcustom org-odd-levels-only nil
  679. "Non-nil means, skip even levels and only use odd levels for the outline.
  680. This has the effect that two stars are being added/taken away in
  681. promotion/demotion commands. It also influences how levels are
  682. handled by the exporters.
  683. Changing it requires restart of `font-lock-mode' to become effective
  684. for fontification also in regions already fontified.
  685. You may also set this on a per-file basis by adding one of the following
  686. lines to the buffer:
  687. #+STARTUP: odd
  688. #+STARTUP: oddeven"
  689. :group 'org-edit-structure
  690. :group 'org-font-lock
  691. :type 'boolean)
  692. (defcustom org-adapt-indentation t
  693. "Non-nil means, adapt indentation to outline node level.
  694. When this variable is set, Org assumes that you write outlines by
  695. indenting text in each node to align with the headline (after the stars).
  696. The following issues are influenced by this variable:
  697. - When this is set and the *entire* text in an entry is indented, the
  698. indentation is increased by one space in a demotion command, and
  699. decreased by one in a promotion command. If any line in the entry
  700. body starts with text at column 0, indentation is not changed at all.
  701. - Property drawers and planning information is inserted indented when
  702. this variable s set. When nil, they will not be indented.
  703. - TAB indents a line relative to context. The lines below a headline
  704. will be indented when this variable is set.
  705. Note that this is all about true indentation, by adding and removing
  706. space characters. See also `org-indent.el' which does level-dependent
  707. indentation in a virtual way, i.e. at display time in Emacs."
  708. :group 'org-edit-structure
  709. :type 'boolean)
  710. (defcustom org-special-ctrl-a/e nil
  711. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  712. When t, `C-a' will bring back the cursor to the beginning of the
  713. headline text, i.e. after the stars and after a possible TODO keyword.
  714. In an item, this will be the position after the bullet.
  715. When the cursor is already at that position, another `C-a' will bring
  716. it to the beginning of the line.
  717. `C-e' will jump to the end of the headline, ignoring the presence of tags
  718. in the headline. A second `C-e' will then jump to the true end of the
  719. line, after any tags. This also means that, when this variable is
  720. non-nil, `C-e' also will never jump beyond the end of the heading of a
  721. folded section, i.e. not after the ellipses.
  722. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  723. going to the true line boundary first. Only a directly following, identical
  724. keypress will bring the cursor to the special positions.
  725. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  726. set separately."
  727. :group 'org-edit-structure
  728. :type '(choice
  729. (const :tag "off" nil)
  730. (const :tag "on: after stars/bullet and before tags first" t)
  731. (const :tag "reversed: true line boundary first" reversed)
  732. (cons :tag "Set C-a and C-e separately"
  733. (choice :tag "Special C-a"
  734. (const :tag "off" nil)
  735. (const :tag "on: after stars/bullet first" t)
  736. (const :tag "reversed: before stars/bullet first" reversed))
  737. (choice :tag "Special C-e"
  738. (const :tag "off" nil)
  739. (const :tag "on: before tags first" t)
  740. (const :tag "reversed: after tags first" reversed)))))
  741. (if (fboundp 'defvaralias)
  742. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  743. (defcustom org-special-ctrl-k nil
  744. "Non-nil means `C-k' will behave specially in headlines.
  745. When nil, `C-k' will call the default `kill-line' command.
  746. When t, the following will happen while the cursor is in the headline:
  747. - When the cursor is at the beginning of a headline, kill the entire
  748. line and possible the folded subtree below the line.
  749. - When in the middle of the headline text, kill the headline up to the tags.
  750. - When after the headline text, kill the tags."
  751. :group 'org-edit-structure
  752. :type 'boolean)
  753. (defcustom org-yank-folded-subtrees t
  754. "Non-nil means, when yanking subtrees, fold them.
  755. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  756. it starts with a heading and all other headings in it are either children
  757. or siblings, then fold all the subtrees. However, do this only if no
  758. text after the yank would be swallowed into a folded tree by this action."
  759. :group 'org-edit-structure
  760. :type 'boolean)
  761. (defcustom org-yank-adjusted-subtrees nil
  762. "Non-nil means, when yanking subtrees, adjust the level.
  763. With this setting, `org-paste-subtree' is used to insert the subtree, see
  764. this function for details."
  765. :group 'org-edit-structure
  766. :type 'boolean)
  767. (defcustom org-M-RET-may-split-line '((default . t))
  768. "Non-nil means, M-RET will split the line at the cursor position.
  769. When nil, it will go to the end of the line before making a
  770. new line.
  771. You may also set this option in a different way for different
  772. contexts. Valid contexts are:
  773. headline when creating a new headline
  774. item when creating a new item
  775. table in a table field
  776. default the value to be used for all contexts not explicitly
  777. customized"
  778. :group 'org-structure
  779. :group 'org-table
  780. :type '(choice
  781. (const :tag "Always" t)
  782. (const :tag "Never" nil)
  783. (repeat :greedy t :tag "Individual contexts"
  784. (cons
  785. (choice :tag "Context"
  786. (const headline)
  787. (const item)
  788. (const table)
  789. (const default))
  790. (boolean)))))
  791. (defcustom org-insert-heading-respect-content nil
  792. "Non-nil means, insert new headings after the current subtree.
  793. When nil, the new heading is created directly after the current line.
  794. The commands \\[org-insert-heading-respect-content] and
  795. \\[org-insert-todo-heading-respect-content] turn this variable on
  796. for the duration of the command."
  797. :group 'org-structure
  798. :type 'boolean)
  799. (defcustom org-blank-before-new-entry '((heading . auto)
  800. (plain-list-item . auto))
  801. "Should `org-insert-heading' leave a blank line before new heading/item?
  802. The value is an alist, with `heading' and `plain-list-item' as car,
  803. and a boolean flag as cdr. For plain lists, if the variable
  804. `org-empty-line-terminates-plain-lists' is set, the setting here
  805. is ignored and no empty line is inserted, to keep the list in tact."
  806. :group 'org-edit-structure
  807. :type '(list
  808. (cons (const heading)
  809. (choice (const :tag "Never" nil)
  810. (const :tag "Always" t)
  811. (const :tag "Auto" auto)))
  812. (cons (const plain-list-item)
  813. (choice (const :tag "Never" nil)
  814. (const :tag "Always" t)
  815. (const :tag "Auto" auto)))))
  816. (defcustom org-insert-heading-hook nil
  817. "Hook being run after inserting a new heading."
  818. :group 'org-edit-structure
  819. :type 'hook)
  820. (defcustom org-enable-fixed-width-editor t
  821. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  822. This currently only means, they are never auto-wrapped.
  823. When nil, such lines will be treated like ordinary lines.
  824. See also the QUOTE keyword."
  825. :group 'org-edit-structure
  826. :type 'boolean)
  827. (defcustom org-goto-auto-isearch t
  828. "Non-nil means, typing characters in org-goto starts incremental search."
  829. :group 'org-edit-structure
  830. :type 'boolean)
  831. (defgroup org-sparse-trees nil
  832. "Options concerning sparse trees in Org-mode."
  833. :tag "Org Sparse Trees"
  834. :group 'org-structure)
  835. (defcustom org-highlight-sparse-tree-matches t
  836. "Non-nil means, highlight all matches that define a sparse tree.
  837. The highlights will automatically disappear the next time the buffer is
  838. changed by an edit command."
  839. :group 'org-sparse-trees
  840. :type 'boolean)
  841. (defcustom org-remove-highlights-with-change t
  842. "Non-nil means, any change to the buffer will remove temporary highlights.
  843. Such highlights are created by `org-occur' and `org-clock-display'.
  844. When nil, `C-c C-c needs to be used to get rid of the highlights.
  845. The highlights created by `org-preview-latex-fragment' always need
  846. `C-c C-c' to be removed."
  847. :group 'org-sparse-trees
  848. :group 'org-time
  849. :type 'boolean)
  850. (defcustom org-occur-hook '(org-first-headline-recenter)
  851. "Hook that is run after `org-occur' has constructed a sparse tree.
  852. This can be used to recenter the window to show as much of the structure
  853. as possible."
  854. :group 'org-sparse-trees
  855. :type 'hook)
  856. (defgroup org-imenu-and-speedbar nil
  857. "Options concerning imenu and speedbar in Org-mode."
  858. :tag "Org Imenu and Speedbar"
  859. :group 'org-structure)
  860. (defcustom org-imenu-depth 2
  861. "The maximum level for Imenu access to Org-mode headlines.
  862. This also applied for speedbar access."
  863. :group 'org-imenu-and-speedbar
  864. :type 'integer)
  865. (defgroup org-table nil
  866. "Options concerning tables in Org-mode."
  867. :tag "Org Table"
  868. :group 'org)
  869. (defcustom org-enable-table-editor 'optimized
  870. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  871. When nil, such lines will be treated like ordinary lines.
  872. When equal to the symbol `optimized', the table editor will be optimized to
  873. do the following:
  874. - Automatic overwrite mode in front of whitespace in table fields.
  875. This makes the structure of the table stay in tact as long as the edited
  876. field does not exceed the column width.
  877. - Minimize the number of realigns. Normally, the table is aligned each time
  878. TAB or RET are pressed to move to another field. With optimization this
  879. happens only if changes to a field might have changed the column width.
  880. Optimization requires replacing the functions `self-insert-command',
  881. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  882. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  883. very good at guessing when a re-align will be necessary, but you can always
  884. force one with \\[org-ctrl-c-ctrl-c].
  885. If you would like to use the optimized version in Org-mode, but the
  886. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  887. This variable can be used to turn on and off the table editor during a session,
  888. but in order to toggle optimization, a restart is required.
  889. See also the variable `org-table-auto-blank-field'."
  890. :group 'org-table
  891. :type '(choice
  892. (const :tag "off" nil)
  893. (const :tag "on" t)
  894. (const :tag "on, optimized" optimized)))
  895. (defcustom org-self-insert-cluster-for-undo t
  896. "Non-nil means cluster self-insert commands for undo when possible.
  897. If this is set, then, like in the Emacs command loop, 20 consecutive
  898. characters will be undone together.
  899. This is configurable, because there is some impact on typing performance."
  900. :group 'org-table
  901. :type 'boolean)
  902. (defcustom org-table-tab-recognizes-table.el t
  903. "Non-nil means, TAB will automatically notice a table.el table.
  904. When it sees such a table, it moves point into it and - if necessary -
  905. calls `table-recognize-table'."
  906. :group 'org-table-editing
  907. :type 'boolean)
  908. (defgroup org-link nil
  909. "Options concerning links in Org-mode."
  910. :tag "Org Link"
  911. :group 'org)
  912. (defvar org-link-abbrev-alist-local nil
  913. "Buffer-local version of `org-link-abbrev-alist', which see.
  914. The value of this is taken from the #+LINK lines.")
  915. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  916. (defcustom org-link-abbrev-alist nil
  917. "Alist of link abbreviations.
  918. The car of each element is a string, to be replaced at the start of a link.
  919. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  920. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  921. [[linkkey:tag][description]]
  922. The 'linkkey' must be a word word, starting with a letter, followed
  923. by letters, numbers, '-' or '_'.
  924. If REPLACE is a string, the tag will simply be appended to create the link.
  925. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  926. the placeholder \"%h\" will cause a url-encoded version of the tag to
  927. be inserted at that point (see the function `url-hexify-string').
  928. REPLACE may also be a function that will be called with the tag as the
  929. only argument to create the link, which should be returned as a string.
  930. See the manual for examples."
  931. :group 'org-link
  932. :type '(repeat
  933. (cons
  934. (string :tag "Protocol")
  935. (choice
  936. (string :tag "Format")
  937. (function)))))
  938. (defcustom org-descriptive-links t
  939. "Non-nil means, hide link part and only show description of bracket links.
  940. Bracket links are like [[link][description]]. This variable sets the initial
  941. state in new org-mode buffers. The setting can then be toggled on a
  942. per-buffer basis from the Org->Hyperlinks menu."
  943. :group 'org-link
  944. :type 'boolean)
  945. (defcustom org-link-file-path-type 'adaptive
  946. "How the path name in file links should be stored.
  947. Valid values are:
  948. relative Relative to the current directory, i.e. the directory of the file
  949. into which the link is being inserted.
  950. absolute Absolute path, if possible with ~ for home directory.
  951. noabbrev Absolute path, no abbreviation of home directory.
  952. adaptive Use relative path for files in the current directory and sub-
  953. directories of it. For other files, use an absolute path."
  954. :group 'org-link
  955. :type '(choice
  956. (const relative)
  957. (const absolute)
  958. (const noabbrev)
  959. (const adaptive)))
  960. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  961. "Types of links that should be activated in Org-mode files.
  962. This is a list of symbols, each leading to the activation of a certain link
  963. type. In principle, it does not hurt to turn on most link types - there may
  964. be a small gain when turning off unused link types. The types are:
  965. bracket The recommended [[link][description]] or [[link]] links with hiding.
  966. angular Links in angular brackets that may contain whitespace like
  967. <bbdb:Carsten Dominik>.
  968. plain Plain links in normal text, no whitespace, like http://google.com.
  969. radio Text that is matched by a radio target, see manual for details.
  970. tag Tag settings in a headline (link to tag search).
  971. date Time stamps (link to calendar).
  972. footnote Footnote labels.
  973. Changing this variable requires a restart of Emacs to become effective."
  974. :group 'org-link
  975. :type '(set :greedy t
  976. (const :tag "Double bracket links (new style)" bracket)
  977. (const :tag "Angular bracket links (old style)" angular)
  978. (const :tag "Plain text links" plain)
  979. (const :tag "Radio target matches" radio)
  980. (const :tag "Tags" tag)
  981. (const :tag "Timestamps" date)
  982. (const :tag "Footnotes" footnote)))
  983. (defcustom org-make-link-description-function nil
  984. "Function to use to generate link descriptions from links. If
  985. nil the link location will be used. This function must take two
  986. parameters; the first is the link and the second the description
  987. org-insert-link has generated, and should return the description
  988. to use."
  989. :group 'org-link
  990. :type 'function)
  991. (defgroup org-link-store nil
  992. "Options concerning storing links in Org-mode."
  993. :tag "Org Store Link"
  994. :group 'org-link)
  995. (defcustom org-email-link-description-format "Email %c: %.30s"
  996. "Format of the description part of a link to an email or usenet message.
  997. The following %-escapes will be replaced by corresponding information:
  998. %F full \"From\" field
  999. %f name, taken from \"From\" field, address if no name
  1000. %T full \"To\" field
  1001. %t first name in \"To\" field, address if no name
  1002. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1003. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1004. %s subject
  1005. %m message-id.
  1006. You may use normal field width specification between the % and the letter.
  1007. This is for example useful to limit the length of the subject.
  1008. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1009. :group 'org-link-store
  1010. :type 'string)
  1011. (defcustom org-from-is-user-regexp
  1012. (let (r1 r2)
  1013. (when (and user-mail-address (not (string= user-mail-address "")))
  1014. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1015. (when (and user-full-name (not (string= user-full-name "")))
  1016. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1017. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1018. "Regexp matched against the \"From:\" header of an email or usenet message.
  1019. It should match if the message is from the user him/herself."
  1020. :group 'org-link-store
  1021. :type 'regexp)
  1022. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1023. "Non-nil means, storing a link to an Org file will use entry IDs.
  1024. Note that before this variable is even considered, org-id must be loaded,
  1025. so please customize `org-modules' and turn it on.
  1026. The variable can have the following values:
  1027. t Create an ID if needed to make a link to the current entry.
  1028. create-if-interactive
  1029. If `org-store-link' is called directly (interactively, as a user
  1030. command), do create an ID to support the link. But when doing the
  1031. job for remember, only use the ID if it already exists. The
  1032. purpose of this setting is to avoid proliferation of unwanted
  1033. IDs, just because you happen to be in an Org file when you
  1034. call `org-remember' that automatically and preemptively
  1035. creates a link. If you do want to get an ID link in a remember
  1036. template to an entry not having an ID, create it first by
  1037. explicitly creating a link to it, using `C-c C-l' first.
  1038. create-if-interactive-and-no-custom-id
  1039. Like create-if-interactive, but do not create an ID if there is
  1040. a CUSTOM_ID property defined in the entry. This is the default.
  1041. use-existing
  1042. Use existing ID, do not create one.
  1043. nil Never use an ID to make a link, instead link using a text search for
  1044. the headline text."
  1045. :group 'org-link-store
  1046. :type '(choice
  1047. (const :tag "Create ID to make link" t)
  1048. (const :tag "Create if storing link interactively"
  1049. create-if-interactive)
  1050. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1051. create-if-interactive-and-no-custom-id)
  1052. (const :tag "Only use existing" use-existing)
  1053. (const :tag "Do not use ID to create link" nil)))
  1054. (defcustom org-context-in-file-links t
  1055. "Non-nil means, file links from `org-store-link' contain context.
  1056. A search string will be added to the file name with :: as separator and
  1057. used to find the context when the link is activated by the command
  1058. `org-open-at-point'.
  1059. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1060. negates this setting for the duration of the command."
  1061. :group 'org-link-store
  1062. :type 'boolean)
  1063. (defcustom org-keep-stored-link-after-insertion nil
  1064. "Non-nil means, keep link in list for entire session.
  1065. The command `org-store-link' adds a link pointing to the current
  1066. location to an internal list. These links accumulate during a session.
  1067. The command `org-insert-link' can be used to insert links into any
  1068. Org-mode file (offering completion for all stored links). When this
  1069. option is nil, every link which has been inserted once using \\[org-insert-link]
  1070. will be removed from the list, to make completing the unused links
  1071. more efficient."
  1072. :group 'org-link-store
  1073. :type 'boolean)
  1074. (defgroup org-link-follow nil
  1075. "Options concerning following links in Org-mode."
  1076. :tag "Org Follow Link"
  1077. :group 'org-link)
  1078. (defcustom org-link-translation-function nil
  1079. "Function to translate links with different syntax to Org syntax.
  1080. This can be used to translate links created for example by the Planner
  1081. or emacs-wiki packages to Org syntax.
  1082. The function must accept two parameters, a TYPE containing the link
  1083. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1084. which is everything after the link protocol. It should return a cons
  1085. with possibly modified values of type and path.
  1086. Org contains a function for this, so if you set this variable to
  1087. `org-translate-link-from-planner', you should be able follow many
  1088. links created by planner."
  1089. :group 'org-link-follow
  1090. :type 'function)
  1091. (defcustom org-follow-link-hook nil
  1092. "Hook that is run after a link has been followed."
  1093. :group 'org-link-follow
  1094. :type 'hook)
  1095. (defcustom org-tab-follows-link nil
  1096. "Non-nil means, on links TAB will follow the link.
  1097. Needs to be set before org.el is loaded.
  1098. This really should not be used, it does not make sense, and the
  1099. implementation is bad."
  1100. :group 'org-link-follow
  1101. :type 'boolean)
  1102. (defcustom org-return-follows-link nil
  1103. "Non-nil means, on links RET will follow the link.
  1104. Needs to be set before org.el is loaded."
  1105. :group 'org-link-follow
  1106. :type 'boolean)
  1107. (defcustom org-mouse-1-follows-link
  1108. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1109. "Non-nil means, mouse-1 on a link will follow the link.
  1110. A longer mouse click will still set point. Does not work on XEmacs.
  1111. Needs to be set before org.el is loaded."
  1112. :group 'org-link-follow
  1113. :type 'boolean)
  1114. (defcustom org-mark-ring-length 4
  1115. "Number of different positions to be recorded in the ring
  1116. Changing this requires a restart of Emacs to work correctly."
  1117. :group 'org-link-follow
  1118. :type 'integer)
  1119. (defcustom org-link-frame-setup
  1120. '((vm . vm-visit-folder-other-frame)
  1121. (gnus . gnus-other-frame)
  1122. (file . find-file-other-window))
  1123. "Setup the frame configuration for following links.
  1124. When following a link with Emacs, it may often be useful to display
  1125. this link in another window or frame. This variable can be used to
  1126. set this up for the different types of links.
  1127. For VM, use any of
  1128. `vm-visit-folder'
  1129. `vm-visit-folder-other-frame'
  1130. For Gnus, use any of
  1131. `gnus'
  1132. `gnus-other-frame'
  1133. `org-gnus-no-new-news'
  1134. For FILE, use any of
  1135. `find-file'
  1136. `find-file-other-window'
  1137. `find-file-other-frame'
  1138. For the calendar, use the variable `calendar-setup'.
  1139. For BBDB, it is currently only possible to display the matches in
  1140. another window."
  1141. :group 'org-link-follow
  1142. :type '(list
  1143. (cons (const vm)
  1144. (choice
  1145. (const vm-visit-folder)
  1146. (const vm-visit-folder-other-window)
  1147. (const vm-visit-folder-other-frame)))
  1148. (cons (const gnus)
  1149. (choice
  1150. (const gnus)
  1151. (const gnus-other-frame)
  1152. (const org-gnus-no-new-news)))
  1153. (cons (const file)
  1154. (choice
  1155. (const find-file)
  1156. (const find-file-other-window)
  1157. (const find-file-other-frame)))))
  1158. (defcustom org-display-internal-link-with-indirect-buffer nil
  1159. "Non-nil means, use indirect buffer to display infile links.
  1160. Activating internal links (from one location in a file to another location
  1161. in the same file) normally just jumps to the location. When the link is
  1162. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1163. another window. When this option is set, the other window actually displays
  1164. an indirect buffer clone of the current buffer, to avoid any visibility
  1165. changes to the current buffer."
  1166. :group 'org-link-follow
  1167. :type 'boolean)
  1168. (defcustom org-open-non-existing-files nil
  1169. "Non-nil means, `org-open-file' will open non-existing files.
  1170. When nil, an error will be generated.
  1171. This variable applies only to external applications because they
  1172. might choke on non-existing files. If the link is to a file that
  1173. will be opened in Emacs, the variable is ignored."
  1174. :group 'org-link-follow
  1175. :type 'boolean)
  1176. (defcustom org-open-directory-means-index-dot-org nil
  1177. "Non-nil means, a link to a directory really means to index.org.
  1178. When nil, following a directory link will run dired or open a finder/explorer
  1179. window on that directory."
  1180. :group 'org-link-follow
  1181. :type 'boolean)
  1182. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1183. "Function and arguments to call for following mailto links.
  1184. This is a list with the first element being a lisp function, and the
  1185. remaining elements being arguments to the function. In string arguments,
  1186. %a will be replaced by the address, and %s will be replaced by the subject
  1187. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1188. :group 'org-link-follow
  1189. :type '(choice
  1190. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1191. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1192. (const :tag "message-mail" (message-mail "%a" "%s"))
  1193. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1194. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1195. "Non-nil means, ask for confirmation before executing shell links.
  1196. Shell links can be dangerous: just think about a link
  1197. [[shell:rm -rf ~/*][Google Search]]
  1198. This link would show up in your Org-mode document as \"Google Search\",
  1199. but really it would remove your entire home directory.
  1200. Therefore we advise against setting this variable to nil.
  1201. Just change it to `y-or-n-p' if you want to confirm with a
  1202. single keystroke rather than having to type \"yes\"."
  1203. :group 'org-link-follow
  1204. :type '(choice
  1205. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1206. (const :tag "with y-or-n (faster)" y-or-n-p)
  1207. (const :tag "no confirmation (dangerous)" nil)))
  1208. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1209. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1210. Elisp links can be dangerous: just think about a link
  1211. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1212. This link would show up in your Org-mode document as \"Google Search\",
  1213. but really it would remove your entire home directory.
  1214. Therefore we advise against setting this variable to nil.
  1215. Just change it to `y-or-n-p' if you want to confirm with a
  1216. single keystroke rather than having to type \"yes\"."
  1217. :group 'org-link-follow
  1218. :type '(choice
  1219. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1220. (const :tag "with y-or-n (faster)" y-or-n-p)
  1221. (const :tag "no confirmation (dangerous)" nil)))
  1222. (defconst org-file-apps-defaults-gnu
  1223. '((remote . emacs)
  1224. (system . mailcap)
  1225. (t . mailcap))
  1226. "Default file applications on a UNIX or GNU/Linux system.
  1227. See `org-file-apps'.")
  1228. (defconst org-file-apps-defaults-macosx
  1229. '((remote . emacs)
  1230. (t . "open %s")
  1231. (system . "open %s")
  1232. ("ps.gz" . "gv %s")
  1233. ("eps.gz" . "gv %s")
  1234. ("dvi" . "xdvi %s")
  1235. ("fig" . "xfig %s"))
  1236. "Default file applications on a MacOS X system.
  1237. The system \"open\" is known as a default, but we use X11 applications
  1238. for some files for which the OS does not have a good default.
  1239. See `org-file-apps'.")
  1240. (defconst org-file-apps-defaults-windowsnt
  1241. (list
  1242. '(remote . emacs)
  1243. (cons t
  1244. (list (if (featurep 'xemacs)
  1245. 'mswindows-shell-execute
  1246. 'w32-shell-execute)
  1247. "open" 'file))
  1248. (cons 'system
  1249. (list (if (featurep 'xemacs)
  1250. 'mswindows-shell-execute
  1251. 'w32-shell-execute)
  1252. "open" 'file)))
  1253. "Default file applications on a Windows NT system.
  1254. The system \"open\" is used for most files.
  1255. See `org-file-apps'.")
  1256. (defcustom org-file-apps
  1257. '(
  1258. (auto-mode . emacs)
  1259. ("\\.mm\\'" . default)
  1260. ("\\.x?html?\\'" . default)
  1261. ("\\.pdf\\'" . default)
  1262. )
  1263. "External applications for opening `file:path' items in a document.
  1264. Org-mode uses system defaults for different file types, but
  1265. you can use this variable to set the application for a given file
  1266. extension. The entries in this list are cons cells where the car identifies
  1267. files and the cdr the corresponding command. Possible values for the
  1268. file identifier are
  1269. \"regex\" Regular expression matched against the file name. For backward
  1270. compatibility, this can also be a string with only alphanumeric
  1271. characters, which is then interpreted as an extension.
  1272. `directory' Matches a directory
  1273. `remote' Matches a remote file, accessible through tramp or efs.
  1274. Remote files most likely should be visited through Emacs
  1275. because external applications cannot handle such paths.
  1276. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1277. so all files Emacs knows how to handle. Using this with
  1278. command `emacs' will open most files in Emacs. Beware that this
  1279. will also open html files inside Emacs, unless you add
  1280. (\"html\" . default) to the list as well.
  1281. t Default for files not matched by any of the other options.
  1282. `system' The system command to open files, like `open' on Windows
  1283. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1284. that will be selected if you call `C-c C-o' with a double
  1285. `C-u C-u' prefix.
  1286. Possible values for the command are:
  1287. `emacs' The file will be visited by the current Emacs process.
  1288. `default' Use the default application for this file type, which is the
  1289. association for t in the list, most likely in the system-specific
  1290. part.
  1291. This can be used to overrule an unwanted setting in the
  1292. system-specific variable.
  1293. `system' Use the system command for opening files, like \"open\".
  1294. This command is specified by the entry whose car is `system'.
  1295. Most likely, the system-specific version of this variable
  1296. does define this command, but you can overrule/replace it
  1297. here.
  1298. string A command to be executed by a shell; %s will be replaced
  1299. by the path to the file.
  1300. sexp A Lisp form which will be evaluated. The file path will
  1301. be available in the Lisp variable `file'.
  1302. For more examples, see the system specific constants
  1303. `org-file-apps-defaults-macosx'
  1304. `org-file-apps-defaults-windowsnt'
  1305. `org-file-apps-defaults-gnu'."
  1306. :group 'org-link-follow
  1307. :type '(repeat
  1308. (cons (choice :value ""
  1309. (string :tag "Extension")
  1310. (const :tag "System command to open files" system)
  1311. (const :tag "Default for unrecognized files" t)
  1312. (const :tag "Remote file" remote)
  1313. (const :tag "Links to a directory" directory)
  1314. (const :tag "Any files that have Emacs modes"
  1315. auto-mode))
  1316. (choice :value ""
  1317. (const :tag "Visit with Emacs" emacs)
  1318. (const :tag "Use default" default)
  1319. (const :tag "Use the system command" system)
  1320. (string :tag "Command")
  1321. (sexp :tag "Lisp form")))))
  1322. (defgroup org-refile nil
  1323. "Options concerning refiling entries in Org-mode."
  1324. :tag "Org Refile"
  1325. :group 'org)
  1326. (defcustom org-directory "~/org"
  1327. "Directory with org files.
  1328. This is just a default location to look for Org files. There is no need
  1329. at all to put your files into this directory. It is only used in the
  1330. following situations:
  1331. 1. When a remember template specifies a target file that is not an
  1332. absolute path. The path will then be interpreted relative to
  1333. `org-directory'
  1334. 2. When a remember note is filed away in an interactive way (when exiting the
  1335. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1336. with `org-directory' as the default path."
  1337. :group 'org-refile
  1338. :group 'org-remember
  1339. :type 'directory)
  1340. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1341. "Default target for storing notes.
  1342. Used by the hooks for remember.el. This can be a string, or nil to mean
  1343. the value of `remember-data-file'.
  1344. You can set this on a per-template basis with the variable
  1345. `org-remember-templates'."
  1346. :group 'org-refile
  1347. :group 'org-remember
  1348. :type '(choice
  1349. (const :tag "Default from remember-data-file" nil)
  1350. file))
  1351. (defcustom org-goto-interface 'outline
  1352. "The default interface to be used for `org-goto'.
  1353. Allowed values are:
  1354. outline The interface shows an outline of the relevant file
  1355. and the correct heading is found by moving through
  1356. the outline or by searching with incremental search.
  1357. outline-path-completion Headlines in the current buffer are offered via
  1358. completion. This is the interface also used by
  1359. the refile command."
  1360. :group 'org-refile
  1361. :type '(choice
  1362. (const :tag "Outline" outline)
  1363. (const :tag "Outline-path-completion" outline-path-completion)))
  1364. (defcustom org-goto-max-level 5
  1365. "Maximum level to be considered when running org-goto with refile interface."
  1366. :group 'org-refile
  1367. :type 'integer)
  1368. (defcustom org-reverse-note-order nil
  1369. "Non-nil means, store new notes at the beginning of a file or entry.
  1370. When nil, new notes will be filed to the end of a file or entry.
  1371. This can also be a list with cons cells of regular expressions that
  1372. are matched against file names, and values."
  1373. :group 'org-remember
  1374. :group 'org-refile
  1375. :type '(choice
  1376. (const :tag "Reverse always" t)
  1377. (const :tag "Reverse never" nil)
  1378. (repeat :tag "By file name regexp"
  1379. (cons regexp boolean))))
  1380. (defcustom org-refile-targets nil
  1381. "Targets for refiling entries with \\[org-refile].
  1382. This is list of cons cells. Each cell contains:
  1383. - a specification of the files to be considered, either a list of files,
  1384. or a symbol whose function or variable value will be used to retrieve
  1385. a file name or a list of file names. If you use `org-agenda-files' for
  1386. that, all agenda files will be scanned for targets. Nil means, consider
  1387. headings in the current buffer.
  1388. - A specification of how to find candidate refile targets. This may be
  1389. any of:
  1390. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1391. This tag has to be present in all target headlines, inheritance will
  1392. not be considered.
  1393. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1394. todo keyword.
  1395. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1396. headlines that are refiling targets.
  1397. - a cons cell (:level . N). Any headline of level N is considered a target.
  1398. Note that, when `org-odd-levels-only' is set, level corresponds to
  1399. order in hierarchy, not to the number of stars.
  1400. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1401. Note that, when `org-odd-levels-only' is set, level corresponds to
  1402. order in hierarchy, not to the number of stars.
  1403. You can set the variable `org-refile-target-verify-function' to a function
  1404. to verify each headline found by the simple critery above.
  1405. When this variable is nil, all top-level headlines in the current buffer
  1406. are used, equivalent to the value `((nil . (:level . 1))'."
  1407. :group 'org-refile
  1408. :type '(repeat
  1409. (cons
  1410. (choice :value org-agenda-files
  1411. (const :tag "All agenda files" org-agenda-files)
  1412. (const :tag "Current buffer" nil)
  1413. (function) (variable) (file))
  1414. (choice :tag "Identify target headline by"
  1415. (cons :tag "Specific tag" (const :value :tag) (string))
  1416. (cons :tag "TODO keyword" (const :value :todo) (string))
  1417. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1418. (cons :tag "Level number" (const :value :level) (integer))
  1419. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1420. (defcustom org-refile-target-verify-function nil
  1421. "Function to verify if the headline at point should be a refile target.
  1422. The function will be called without arguments, with point at the
  1423. beginning of the headline. It should return t and leave point
  1424. where it is if the headline is a valid target for refiling.
  1425. If the target should not be selected, the function must return nil.
  1426. In addition to this, it may move point to a place from where the search
  1427. should be continued. For example, the function may decide that the entire
  1428. subtree of the current entry should be excluded and move point to the end
  1429. of the subtree."
  1430. :group 'org-refile
  1431. :type 'function)
  1432. (defcustom org-refile-use-outline-path nil
  1433. "Non-nil means, provide refile targets as paths.
  1434. So a level 3 headline will be available as level1/level2/level3.
  1435. When the value is `file', also include the file name (without directory)
  1436. into the path. In this case, you can also stop the completion after
  1437. the file name, to get entries inserted as top level in the file.
  1438. When `full-file-path', include the full file path."
  1439. :group 'org-refile
  1440. :type '(choice
  1441. (const :tag "Not" nil)
  1442. (const :tag "Yes" t)
  1443. (const :tag "Start with file name" file)
  1444. (const :tag "Start with full file path" full-file-path)))
  1445. (defcustom org-outline-path-complete-in-steps t
  1446. "Non-nil means, complete the outline path in hierarchical steps.
  1447. When Org-mode uses the refile interface to select an outline path
  1448. \(see variable `org-refile-use-outline-path'), the completion of
  1449. the path can be done is a single go, or if can be done in steps down
  1450. the headline hierarchy. Going in steps is probably the best if you
  1451. do not use a special completion package like `ido' or `icicles'.
  1452. However, when using these packages, going in one step can be very
  1453. fast, while still showing the whole path to the entry."
  1454. :group 'org-refile
  1455. :type 'boolean)
  1456. (defcustom org-refile-allow-creating-parent-nodes nil
  1457. "Non-nil means, allow to create new nodes as refile targets.
  1458. New nodes are then created by adding \"/new node name\" to the completion
  1459. of an existing node. When the value of this variable is `confirm',
  1460. new node creation must be confirmed by the user (recommended)
  1461. When nil, the completion must match an existing entry.
  1462. Note that, if the new heading is not seen by the criteria
  1463. listed in `org-refile-targets', multiple instances of the same
  1464. heading would be created by trying again to file under the new
  1465. heading."
  1466. :group 'org-refile
  1467. :type '(choice
  1468. (const :tag "Never" nil)
  1469. (const :tag "Always" t)
  1470. (const :tag "Prompt for confirmation" confirm)))
  1471. (defgroup org-todo nil
  1472. "Options concerning TODO items in Org-mode."
  1473. :tag "Org TODO"
  1474. :group 'org)
  1475. (defgroup org-progress nil
  1476. "Options concerning Progress logging in Org-mode."
  1477. :tag "Org Progress"
  1478. :group 'org-time)
  1479. (defvar org-todo-interpretation-widgets
  1480. '(
  1481. (:tag "Sequence (cycling hits every state)" sequence)
  1482. (:tag "Type (cycling directly to DONE)" type))
  1483. "The available interpretation symbols for customizing
  1484. `org-todo-keywords'.
  1485. Interested libraries should add to this list.")
  1486. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1487. "List of TODO entry keyword sequences and their interpretation.
  1488. \\<org-mode-map>This is a list of sequences.
  1489. Each sequence starts with a symbol, either `sequence' or `type',
  1490. indicating if the keywords should be interpreted as a sequence of
  1491. action steps, or as different types of TODO items. The first
  1492. keywords are states requiring action - these states will select a headline
  1493. for inclusion into the global TODO list Org-mode produces. If one of
  1494. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1495. signify that no further action is necessary. If \"|\" is not found,
  1496. the last keyword is treated as the only DONE state of the sequence.
  1497. The command \\[org-todo] cycles an entry through these states, and one
  1498. additional state where no keyword is present. For details about this
  1499. cycling, see the manual.
  1500. TODO keywords and interpretation can also be set on a per-file basis with
  1501. the special #+SEQ_TODO and #+TYP_TODO lines.
  1502. Each keyword can optionally specify a character for fast state selection
  1503. \(in combination with the variable `org-use-fast-todo-selection')
  1504. and specifiers for state change logging, using the same syntax
  1505. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1506. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1507. indicates to record a time stamp each time this state is selected.
  1508. Each keyword may also specify if a timestamp or a note should be
  1509. recorded when entering or leaving the state, by adding additional
  1510. characters in the parenthesis after the keyword. This looks like this:
  1511. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1512. record only the time of the state change. With X and Y being either
  1513. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1514. Y when leaving the state if and only if the *target* state does not
  1515. define X. You may omit any of the fast-selection key or X or /Y,
  1516. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1517. For backward compatibility, this variable may also be just a list
  1518. of keywords - in this case the interpretation (sequence or type) will be
  1519. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1520. :group 'org-todo
  1521. :group 'org-keywords
  1522. :type '(choice
  1523. (repeat :tag "Old syntax, just keywords"
  1524. (string :tag "Keyword"))
  1525. (repeat :tag "New syntax"
  1526. (cons
  1527. (choice
  1528. :tag "Interpretation"
  1529. ;;Quick and dirty way to see
  1530. ;;`org-todo-interpretations'. This takes the
  1531. ;;place of item arguments
  1532. :convert-widget
  1533. (lambda (widget)
  1534. (widget-put widget
  1535. :args (mapcar
  1536. #'(lambda (x)
  1537. (widget-convert
  1538. (cons 'const x)))
  1539. org-todo-interpretation-widgets))
  1540. widget))
  1541. (repeat
  1542. (string :tag "Keyword"))))))
  1543. (defvar org-todo-keywords-1 nil
  1544. "All TODO and DONE keywords active in a buffer.")
  1545. (make-variable-buffer-local 'org-todo-keywords-1)
  1546. (defvar org-todo-keywords-for-agenda nil)
  1547. (defvar org-done-keywords-for-agenda nil)
  1548. (defvar org-drawers-for-agenda nil)
  1549. (defvar org-todo-keyword-alist-for-agenda nil)
  1550. (defvar org-tag-alist-for-agenda nil)
  1551. (defvar org-agenda-contributing-files nil)
  1552. (defvar org-not-done-keywords nil)
  1553. (make-variable-buffer-local 'org-not-done-keywords)
  1554. (defvar org-done-keywords nil)
  1555. (make-variable-buffer-local 'org-done-keywords)
  1556. (defvar org-todo-heads nil)
  1557. (make-variable-buffer-local 'org-todo-heads)
  1558. (defvar org-todo-sets nil)
  1559. (make-variable-buffer-local 'org-todo-sets)
  1560. (defvar org-todo-log-states nil)
  1561. (make-variable-buffer-local 'org-todo-log-states)
  1562. (defvar org-todo-kwd-alist nil)
  1563. (make-variable-buffer-local 'org-todo-kwd-alist)
  1564. (defvar org-todo-key-alist nil)
  1565. (make-variable-buffer-local 'org-todo-key-alist)
  1566. (defvar org-todo-key-trigger nil)
  1567. (make-variable-buffer-local 'org-todo-key-trigger)
  1568. (defcustom org-todo-interpretation 'sequence
  1569. "Controls how TODO keywords are interpreted.
  1570. This variable is in principle obsolete and is only used for
  1571. backward compatibility, if the interpretation of todo keywords is
  1572. not given already in `org-todo-keywords'. See that variable for
  1573. more information."
  1574. :group 'org-todo
  1575. :group 'org-keywords
  1576. :type '(choice (const sequence)
  1577. (const type)))
  1578. (defcustom org-use-fast-todo-selection t
  1579. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1580. This variable describes if and under what circumstances the cycling
  1581. mechanism for TODO keywords will be replaced by a single-key, direct
  1582. selection scheme.
  1583. When nil, fast selection is never used.
  1584. When the symbol `prefix', it will be used when `org-todo' is called with
  1585. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1586. in an agenda buffer.
  1587. When t, fast selection is used by default. In this case, the prefix
  1588. argument forces cycling instead.
  1589. In all cases, the special interface is only used if access keys have actually
  1590. been assigned by the user, i.e. if keywords in the configuration are followed
  1591. by a letter in parenthesis, like TODO(t)."
  1592. :group 'org-todo
  1593. :type '(choice
  1594. (const :tag "Never" nil)
  1595. (const :tag "By default" t)
  1596. (const :tag "Only with C-u C-c C-t" prefix)))
  1597. (defcustom org-provide-todo-statistics t
  1598. "Non-nil means, update todo statistics after insert and toggle.
  1599. ALL-HEADLINES means update todo statistics by including headlines
  1600. with no TODO keyword as well, counting them as not done.
  1601. A list of TODO keywords means the same, but skip keywords that are
  1602. not in this list.
  1603. When this is set, todo statistics is updated in the parent of the
  1604. current entry each time a todo state is changed."
  1605. :group 'org-todo
  1606. :type '(choice
  1607. (const :tag "Yes, only for TODO entries" t)
  1608. (const :tag "Yes, including all entries" 'all-headlines)
  1609. (repeat :tag "Yes, for TODOs in this list"
  1610. (string :tag "TODO keyword"))
  1611. (other :tag "No TODO statistics" nil)))
  1612. (defcustom org-hierarchical-todo-statistics t
  1613. "Non-nil means, TODO statistics covers just direct children.
  1614. When nil, all entries in the subtree are considered.
  1615. This has only an effect if `org-provide-todo-statistics' is set.
  1616. To set this to nil for only a single subtree, use a COOKIE_DATA
  1617. property and include the word \"recursive\" into the value."
  1618. :group 'org-todo
  1619. :type 'boolean)
  1620. (defcustom org-after-todo-state-change-hook nil
  1621. "Hook which is run after the state of a TODO item was changed.
  1622. The new state (a string with a TODO keyword, or nil) is available in the
  1623. Lisp variable `state'."
  1624. :group 'org-todo
  1625. :type 'hook)
  1626. (defvar org-blocker-hook nil
  1627. "Hook for functions that are allowed to block a state change.
  1628. Each function gets as its single argument a property list, see
  1629. `org-trigger-hook' for more information about this list.
  1630. If any of the functions in this hook returns nil, the state change
  1631. is blocked.")
  1632. (defvar org-trigger-hook nil
  1633. "Hook for functions that are triggered by a state change.
  1634. Each function gets as its single argument a property list with at least
  1635. the following elements:
  1636. (:type type-of-change :position pos-at-entry-start
  1637. :from old-state :to new-state)
  1638. Depending on the type, more properties may be present.
  1639. This mechanism is currently implemented for:
  1640. TODO state changes
  1641. ------------------
  1642. :type todo-state-change
  1643. :from previous state (keyword as a string), or nil, or a symbol
  1644. 'todo' or 'done', to indicate the general type of state.
  1645. :to new state, like in :from")
  1646. (defcustom org-enforce-todo-dependencies nil
  1647. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1648. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1649. be blocked if any prior sibling is not yet done.
  1650. Finally, if the parent is blocked because of ordered siblings of its own,
  1651. the child will also be blocked.
  1652. This variable needs to be set before org.el is loaded, and you need to
  1653. restart Emacs after a change to make the change effective. The only way
  1654. to change is while Emacs is running is through the customize interface."
  1655. :set (lambda (var val)
  1656. (set var val)
  1657. (if val
  1658. (add-hook 'org-blocker-hook
  1659. 'org-block-todo-from-children-or-siblings-or-parent)
  1660. (remove-hook 'org-blocker-hook
  1661. 'org-block-todo-from-children-or-siblings-or-parent)))
  1662. :group 'org-todo
  1663. :type 'boolean)
  1664. (defcustom org-enforce-todo-checkbox-dependencies nil
  1665. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1666. When this is nil, checkboxes have no influence on switching TODO states.
  1667. When non-nil, you first need to check off all check boxes before the TODO
  1668. entry can be switched to DONE.
  1669. This variable needs to be set before org.el is loaded, and you need to
  1670. restart Emacs after a change to make the change effective. The only way
  1671. to change is while Emacs is running is through the customize interface."
  1672. :set (lambda (var val)
  1673. (set var val)
  1674. (if val
  1675. (add-hook 'org-blocker-hook
  1676. 'org-block-todo-from-checkboxes)
  1677. (remove-hook 'org-blocker-hook
  1678. 'org-block-todo-from-checkboxes)))
  1679. :group 'org-todo
  1680. :type 'boolean)
  1681. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1682. "Non-nil means, inserting a TODO heading is treated as state change.
  1683. So when the command \\[org-insert-todo-heading] is used, state change
  1684. logging will apply if appropriate. When nil, the new TODO item will
  1685. be inserted directly, and no logging will take place."
  1686. :group 'org-todo
  1687. :type 'boolean)
  1688. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1689. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1690. This is the default behavior. However, setting this to nil allows a
  1691. convenient way to select a TODO state and bypass any logging associated
  1692. with that."
  1693. :group 'org-todo
  1694. :type 'boolean)
  1695. (defcustom org-todo-state-tags-triggers nil
  1696. "Tag changes that should be triggered by TODO state changes.
  1697. This is a list. Each entry is
  1698. (state-change (tag . flag) .......)
  1699. State-change can be a string with a state, and empty string to indicate the
  1700. state that has no TODO keyword, or it can be one of the symbols `todo'
  1701. or `done', meaning any not-done or done state, respectively."
  1702. :group 'org-todo
  1703. :group 'org-tags
  1704. :type '(repeat
  1705. (cons (choice :tag "When changing to"
  1706. (const :tag "Not-done state" todo)
  1707. (const :tag "Done state" done)
  1708. (string :tag "State"))
  1709. (repeat
  1710. (cons :tag "Tag action"
  1711. (string :tag "Tag")
  1712. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1713. (defcustom org-log-done nil
  1714. "Information to record when a task moves to the DONE state.
  1715. Possible values are:
  1716. nil Don't add anything, just change the keyword
  1717. time Add a time stamp to the task
  1718. note Prompt for a note and add it with template `org-log-note-headings'
  1719. This option can also be set with on a per-file-basis with
  1720. #+STARTUP: nologdone
  1721. #+STARTUP: logdone
  1722. #+STARTUP: lognotedone
  1723. You can have local logging settings for a subtree by setting the LOGGING
  1724. property to one or more of these keywords."
  1725. :group 'org-todo
  1726. :group 'org-progress
  1727. :type '(choice
  1728. (const :tag "No logging" nil)
  1729. (const :tag "Record CLOSED timestamp" time)
  1730. (const :tag "Record CLOSED timestamp with note." note)))
  1731. ;; Normalize old uses of org-log-done.
  1732. (cond
  1733. ((eq org-log-done t) (setq org-log-done 'time))
  1734. ((and (listp org-log-done) (memq 'done org-log-done))
  1735. (setq org-log-done 'note)))
  1736. (defcustom org-log-reschedule nil
  1737. "Information to record when the scheduling date of a tasks is modified.
  1738. Possible values are:
  1739. nil Don't add anything, just change the date
  1740. time Add a time stamp to the task
  1741. note Prompt for a note and add it with template `org-log-note-headings'
  1742. This option can also be set with on a per-file-basis with
  1743. #+STARTUP: nologreschedule
  1744. #+STARTUP: logreschedule
  1745. #+STARTUP: lognotereschedule"
  1746. :group 'org-todo
  1747. :group 'org-progress
  1748. :type '(choice
  1749. (const :tag "No logging" nil)
  1750. (const :tag "Record timestamp" time)
  1751. (const :tag "Record timestamp with note." note)))
  1752. (defcustom org-log-redeadline nil
  1753. "Information to record when the deadline date of a tasks is modified.
  1754. Possible values are:
  1755. nil Don't add anything, just change the date
  1756. time Add a time stamp to the task
  1757. note Prompt for a note and add it with template `org-log-note-headings'
  1758. This option can also be set with on a per-file-basis with
  1759. #+STARTUP: nologredeadline
  1760. #+STARTUP: logredeadline
  1761. #+STARTUP: lognoteredeadline
  1762. You can have local logging settings for a subtree by setting the LOGGING
  1763. property to one or more of these keywords."
  1764. :group 'org-todo
  1765. :group 'org-progress
  1766. :type '(choice
  1767. (const :tag "No logging" nil)
  1768. (const :tag "Record timestamp" time)
  1769. (const :tag "Record timestamp with note." note)))
  1770. (defcustom org-log-note-clock-out nil
  1771. "Non-nil means, record a note when clocking out of an item.
  1772. This can also be configured on a per-file basis by adding one of
  1773. the following lines anywhere in the buffer:
  1774. #+STARTUP: lognoteclock-out
  1775. #+STARTUP: nolognoteclock-out"
  1776. :group 'org-todo
  1777. :group 'org-progress
  1778. :type 'boolean)
  1779. (defcustom org-log-done-with-time t
  1780. "Non-nil means, the CLOSED time stamp will contain date and time.
  1781. When nil, only the date will be recorded."
  1782. :group 'org-progress
  1783. :type 'boolean)
  1784. (defcustom org-log-note-headings
  1785. '((done . "CLOSING NOTE %t")
  1786. (state . "State %-12s from %-12S %t")
  1787. (note . "Note taken on %t")
  1788. (reschedule . "Rescheduled from %S on %t")
  1789. (redeadline . "New deadline from %S on %t")
  1790. (clock-out . ""))
  1791. "Headings for notes added to entries.
  1792. The value is an alist, with the car being a symbol indicating the note
  1793. context, and the cdr is the heading to be used. The heading may also be the
  1794. empty string.
  1795. %t in the heading will be replaced by a time stamp.
  1796. %s will be replaced by the new TODO state, in double quotes.
  1797. %S will be replaced by the old TODO state, in double quotes.
  1798. %u will be replaced by the user name.
  1799. %U will be replaced by the full user name."
  1800. :group 'org-todo
  1801. :group 'org-progress
  1802. :type '(list :greedy t
  1803. (cons (const :tag "Heading when closing an item" done) string)
  1804. (cons (const :tag
  1805. "Heading when changing todo state (todo sequence only)"
  1806. state) string)
  1807. (cons (const :tag "Heading when just taking a note" note) string)
  1808. (cons (const :tag "Heading when clocking out" clock-out) string)
  1809. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1810. (cons (const :tag "Heading when changing deadline" redeadline) string)))
  1811. (unless (assq 'note org-log-note-headings)
  1812. (push '(note . "%t") org-log-note-headings))
  1813. (defcustom org-log-into-drawer nil
  1814. "Non-nil means, insert state change notes and time stamps into a drawer.
  1815. When nil, state changes notes will be inserted after the headline and
  1816. any scheduling and clock lines, but not inside a drawer.
  1817. The value of this variable should be the name of the drawer to use.
  1818. LOGBOOK is proposed at the default drawer for this purpose, you can
  1819. also set this to a string to define the drawer of your choice.
  1820. A value of t is also allowed, representing \"LOGBOOK\".
  1821. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1822. will be ignored.
  1823. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1824. a subtree."
  1825. :group 'org-todo
  1826. :group 'org-progress
  1827. :type '(choice
  1828. (const :tag "Not into a drawer" nil)
  1829. (const :tag "LOGBOOK" t)
  1830. (string :tag "Other")))
  1831. (if (fboundp 'defvaralias)
  1832. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1833. (defun org-log-into-drawer ()
  1834. "Return the value of `org-log-into-drawer', but let properties overrule.
  1835. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1836. used instead of the default value."
  1837. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1838. (cond
  1839. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1840. ((equal p "t") "LOGBOOK")
  1841. (t p))))
  1842. (defcustom org-log-state-notes-insert-after-drawers nil
  1843. "Non-nil means, insert state change notes after any drawers in entry.
  1844. Only the drawers that *immediately* follow the headline and the
  1845. deadline/scheduled line are skipped.
  1846. When nil, insert notes right after the heading and perhaps the line
  1847. with deadline/scheduling if present.
  1848. This variable will have no effect if `org-log-into-drawer' is
  1849. set."
  1850. :group 'org-todo
  1851. :group 'org-progress
  1852. :type 'boolean)
  1853. (defcustom org-log-states-order-reversed t
  1854. "Non-nil means, the latest state change note will be directly after heading.
  1855. When nil, the notes will be orderer according to time."
  1856. :group 'org-todo
  1857. :group 'org-progress
  1858. :type 'boolean)
  1859. (defcustom org-log-repeat 'time
  1860. "Non-nil means, record moving through the DONE state when triggering repeat.
  1861. An auto-repeating task is immediately switched back to TODO when
  1862. marked DONE. If you are not logging state changes (by adding \"@\"
  1863. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1864. record a closing note, there will be no record of the task moving
  1865. through DONE. This variable forces taking a note anyway.
  1866. nil Don't force a record
  1867. time Record a time stamp
  1868. note Record a note
  1869. This option can also be set with on a per-file-basis with
  1870. #+STARTUP: logrepeat
  1871. #+STARTUP: lognoterepeat
  1872. #+STARTUP: nologrepeat
  1873. You can have local logging settings for a subtree by setting the LOGGING
  1874. property to one or more of these keywords."
  1875. :group 'org-todo
  1876. :group 'org-progress
  1877. :type '(choice
  1878. (const :tag "Don't force a record" nil)
  1879. (const :tag "Force recording the DONE state" time)
  1880. (const :tag "Force recording a note with the DONE state" note)))
  1881. (defgroup org-priorities nil
  1882. "Priorities in Org-mode."
  1883. :tag "Org Priorities"
  1884. :group 'org-todo)
  1885. (defcustom org-enable-priority-commands t
  1886. "Non-nil means, priority commands are active.
  1887. When nil, these commands will be disabled, so that you never accidentally
  1888. set a priority."
  1889. :group 'org-priorities
  1890. :type 'boolean)
  1891. (defcustom org-highest-priority ?A
  1892. "The highest priority of TODO items. A character like ?A, ?B etc.
  1893. Must have a smaller ASCII number than `org-lowest-priority'."
  1894. :group 'org-priorities
  1895. :type 'character)
  1896. (defcustom org-lowest-priority ?C
  1897. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1898. Must have a larger ASCII number than `org-highest-priority'."
  1899. :group 'org-priorities
  1900. :type 'character)
  1901. (defcustom org-default-priority ?B
  1902. "The default priority of TODO items.
  1903. This is the priority an item get if no explicit priority is given."
  1904. :group 'org-priorities
  1905. :type 'character)
  1906. (defcustom org-priority-start-cycle-with-default t
  1907. "Non-nil means, start with default priority when starting to cycle.
  1908. When this is nil, the first step in the cycle will be (depending on the
  1909. command used) one higher or lower that the default priority."
  1910. :group 'org-priorities
  1911. :type 'boolean)
  1912. (defgroup org-time nil
  1913. "Options concerning time stamps and deadlines in Org-mode."
  1914. :tag "Org Time"
  1915. :group 'org)
  1916. (defcustom org-insert-labeled-timestamps-at-point nil
  1917. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1918. When nil, these labeled time stamps are forces into the second line of an
  1919. entry, just after the headline. When scheduling from the global TODO list,
  1920. the time stamp will always be forced into the second line."
  1921. :group 'org-time
  1922. :type 'boolean)
  1923. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1924. "Formats for `format-time-string' which are used for time stamps.
  1925. It is not recommended to change this constant.")
  1926. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1927. "Number of minutes to round time stamps to.
  1928. These are two values, the first applies when first creating a time stamp.
  1929. The second applies when changing it with the commands `S-up' and `S-down'.
  1930. When changing the time stamp, this means that it will change in steps
  1931. of N minutes, as given by the second value.
  1932. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1933. numbers should be factors of 60, so for example 5, 10, 15.
  1934. When this is larger than 1, you can still force an exact time-stamp by using
  1935. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1936. and by using a prefix arg to `S-up/down' to specify the exact number
  1937. of minutes to shift."
  1938. :group 'org-time
  1939. :get '(lambda (var) ; Make sure all entries have 5 elements
  1940. (if (integerp (default-value var))
  1941. (list (default-value var) 5)
  1942. (default-value var)))
  1943. :type '(list
  1944. (integer :tag "when inserting times")
  1945. (integer :tag "when modifying times")))
  1946. ;; Normalize old customizations of this variable.
  1947. (when (integerp org-time-stamp-rounding-minutes)
  1948. (setq org-time-stamp-rounding-minutes
  1949. (list org-time-stamp-rounding-minutes
  1950. org-time-stamp-rounding-minutes)))
  1951. (defcustom org-display-custom-times nil
  1952. "Non-nil means, overlay custom formats over all time stamps.
  1953. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1954. To turn this on on a per-file basis, insert anywhere in the file:
  1955. #+STARTUP: customtime"
  1956. :group 'org-time
  1957. :set 'set-default
  1958. :type 'sexp)
  1959. (make-variable-buffer-local 'org-display-custom-times)
  1960. (defcustom org-time-stamp-custom-formats
  1961. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1962. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1963. These are overlayed over the default ISO format if the variable
  1964. `org-display-custom-times' is set. Time like %H:%M should be at the
  1965. end of the second format. The custom formats are also honored by export
  1966. commands, if custom time display is turned on at the time of export."
  1967. :group 'org-time
  1968. :type 'sexp)
  1969. (defun org-time-stamp-format (&optional long inactive)
  1970. "Get the right format for a time string."
  1971. (let ((f (if long (cdr org-time-stamp-formats)
  1972. (car org-time-stamp-formats))))
  1973. (if inactive
  1974. (concat "[" (substring f 1 -1) "]")
  1975. f)))
  1976. (defcustom org-time-clocksum-format "%d:%02d"
  1977. "The format string used when creating CLOCKSUM lines, or when
  1978. org-mode generates a time duration."
  1979. :group 'org-time
  1980. :type 'string)
  1981. (defcustom org-time-clocksum-use-fractional nil
  1982. "If non-nil, \\[org-clock-display] uses fractional times.
  1983. org-mode generates a time duration."
  1984. :group 'org-time
  1985. :type 'boolean)
  1986. (defcustom org-time-clocksum-fractional-format "%.2f"
  1987. "The format string used when creating CLOCKSUM lines, or when
  1988. org-mode generates a time duration."
  1989. :group 'org-time
  1990. :type 'string)
  1991. (defcustom org-deadline-warning-days 14
  1992. "No. of days before expiration during which a deadline becomes active.
  1993. This variable governs the display in sparse trees and in the agenda.
  1994. When 0 or negative, it means use this number (the absolute value of it)
  1995. even if a deadline has a different individual lead time specified.
  1996. Custom commands can set this variable in the options section."
  1997. :group 'org-time
  1998. :group 'org-agenda-daily/weekly
  1999. :type 'integer)
  2000. (defcustom org-read-date-prefer-future t
  2001. "Non-nil means, assume future for incomplete date input from user.
  2002. This affects the following situations:
  2003. 1. The user gives a month but not a year.
  2004. For example, if it is april and you enter \"feb 2\", this will be read
  2005. as feb 2, *next* year. \"May 5\", however, will be this year.
  2006. 2. The user gives a day, but no month.
  2007. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2008. read this as the third of *next* month. However, if you enter \"17\",
  2009. it will be considered as *this* month.
  2010. If you set this variable to the symbol `time', then also the following
  2011. will work:
  2012. 3. If the user gives a time, but no day. If the time is before now,
  2013. to will be interpreted as tomorrow.
  2014. Currently none of this works for ISO week specifications.
  2015. When this option is nil, the current day, month and year will always be
  2016. used as defaults."
  2017. :group 'org-time
  2018. :type '(choice
  2019. (const :tag "Never" nil)
  2020. (const :tag "Check month and day" t)
  2021. (const :tag "Check month, day, and time" time)))
  2022. (defcustom org-read-date-display-live t
  2023. "Non-nil means, display current interpretation of date prompt live.
  2024. This display will be in an overlay, in the minibuffer."
  2025. :group 'org-time
  2026. :type 'boolean)
  2027. (defcustom org-read-date-popup-calendar t
  2028. "Non-nil means, pop up a calendar when prompting for a date.
  2029. In the calendar, the date can be selected with mouse-1. However, the
  2030. minibuffer will also be active, and you can simply enter the date as well.
  2031. When nil, only the minibuffer will be available."
  2032. :group 'org-time
  2033. :type 'boolean)
  2034. (if (fboundp 'defvaralias)
  2035. (defvaralias 'org-popup-calendar-for-date-prompt
  2036. 'org-read-date-popup-calendar))
  2037. (defcustom org-read-date-minibuffer-setup-hook nil
  2038. "Hook to be used to set up keys for the date/time interface.
  2039. Add key definitions to `minibuffer-local-map', which will be a temporary
  2040. copy."
  2041. :group 'org-time
  2042. :type 'hook)
  2043. (defcustom org-extend-today-until 0
  2044. "The hour when your day really ends. Must be an integer.
  2045. This has influence for the following applications:
  2046. - When switching the agenda to \"today\". It it is still earlier than
  2047. the time given here, the day recognized as TODAY is actually yesterday.
  2048. - When a date is read from the user and it is still before the time given
  2049. here, the current date and time will be assumed to be yesterday, 23:59.
  2050. Also, timestamps inserted in remember templates follow this rule.
  2051. IMPORTANT: This is a feature whose implementation is and likely will
  2052. remain incomplete. Really, it is only here because past midnight seems to
  2053. be the favorite working time of John Wiegley :-)"
  2054. :group 'org-time
  2055. :type 'integer)
  2056. (defcustom org-edit-timestamp-down-means-later nil
  2057. "Non-nil means, S-down will increase the time in a time stamp.
  2058. When nil, S-up will increase."
  2059. :group 'org-time
  2060. :type 'boolean)
  2061. (defcustom org-calendar-follow-timestamp-change t
  2062. "Non-nil means, make the calendar window follow timestamp changes.
  2063. When a timestamp is modified and the calendar window is visible, it will be
  2064. moved to the new date."
  2065. :group 'org-time
  2066. :type 'boolean)
  2067. (defgroup org-tags nil
  2068. "Options concerning tags in Org-mode."
  2069. :tag "Org Tags"
  2070. :group 'org)
  2071. (defcustom org-tag-alist nil
  2072. "List of tags allowed in Org-mode files.
  2073. When this list is nil, Org-mode will base TAG input on what is already in the
  2074. buffer.
  2075. The value of this variable is an alist, the car of each entry must be a
  2076. keyword as a string, the cdr may be a character that is used to select
  2077. that tag through the fast-tag-selection interface.
  2078. See the manual for details."
  2079. :group 'org-tags
  2080. :type '(repeat
  2081. (choice
  2082. (cons (string :tag "Tag name")
  2083. (character :tag "Access char"))
  2084. (list :tag "Start radio group"
  2085. (const :startgroup)
  2086. (option (string :tag "Group description")))
  2087. (list :tag "End radio group"
  2088. (const :endgroup)
  2089. (option (string :tag "Group description")))
  2090. (const :tag "New line" (:newline)))))
  2091. (defcustom org-tag-persistent-alist nil
  2092. "List of tags that will always appear in all Org-mode files.
  2093. This is in addition to any in buffer settings or customizations
  2094. of `org-tag-alist'.
  2095. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2096. The value of this variable is an alist, the car of each entry must be a
  2097. keyword as a string, the cdr may be a character that is used to select
  2098. that tag through the fast-tag-selection interface.
  2099. See the manual for details.
  2100. To disable these tags on a per-file basis, insert anywhere in the file:
  2101. #+STARTUP: noptag"
  2102. :group 'org-tags
  2103. :type '(repeat
  2104. (choice
  2105. (cons (string :tag "Tag name")
  2106. (character :tag "Access char"))
  2107. (const :tag "Start radio group" (:startgroup))
  2108. (const :tag "End radio group" (:endgroup))
  2109. (const :tag "New line" (:newline)))))
  2110. (defvar org-file-tags nil
  2111. "List of tags that can be inherited by all entries in the file.
  2112. The tags will be inherited if the variable `org-use-tag-inheritance'
  2113. says they should be.
  2114. This variable is populated from #+FILETAGS lines.")
  2115. (defcustom org-use-fast-tag-selection 'auto
  2116. "Non-nil means, use fast tag selection scheme.
  2117. This is a special interface to select and deselect tags with single keys.
  2118. When nil, fast selection is never used.
  2119. When the symbol `auto', fast selection is used if and only if selection
  2120. characters for tags have been configured, either through the variable
  2121. `org-tag-alist' or through a #+TAGS line in the buffer.
  2122. When t, fast selection is always used and selection keys are assigned
  2123. automatically if necessary."
  2124. :group 'org-tags
  2125. :type '(choice
  2126. (const :tag "Always" t)
  2127. (const :tag "Never" nil)
  2128. (const :tag "When selection characters are configured" 'auto)))
  2129. (defcustom org-fast-tag-selection-single-key nil
  2130. "Non-nil means, fast tag selection exits after first change.
  2131. When nil, you have to press RET to exit it.
  2132. During fast tag selection, you can toggle this flag with `C-c'.
  2133. This variable can also have the value `expert'. In this case, the window
  2134. displaying the tags menu is not even shown, until you press C-c again."
  2135. :group 'org-tags
  2136. :type '(choice
  2137. (const :tag "No" nil)
  2138. (const :tag "Yes" t)
  2139. (const :tag "Expert" expert)))
  2140. (defvar org-fast-tag-selection-include-todo nil
  2141. "Non-nil means, fast tags selection interface will also offer TODO states.
  2142. This is an undocumented feature, you should not rely on it.")
  2143. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2144. "The column to which tags should be indented in a headline.
  2145. If this number is positive, it specifies the column. If it is negative,
  2146. it means that the tags should be flushright to that column. For example,
  2147. -80 works well for a normal 80 character screen."
  2148. :group 'org-tags
  2149. :type 'integer)
  2150. (defcustom org-auto-align-tags t
  2151. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2152. These operations change the length of a headline and therefore shift
  2153. the tags around. With this options turned on, after each such operation
  2154. the tags are again aligned to `org-tags-column'."
  2155. :group 'org-tags
  2156. :type 'boolean)
  2157. (defcustom org-use-tag-inheritance t
  2158. "Non-nil means, tags in levels apply also for sublevels.
  2159. When nil, only the tags directly given in a specific line apply there.
  2160. This may also be a list of tags that should be inherited, or a regexp that
  2161. matches tags that should be inherited. Additional control is possible
  2162. with the variable `org-tags-exclude-from-inheritance' which gives an
  2163. explicit list of tags to be excluded from inheritance., even if the value of
  2164. `org-use-tag-inheritance' would select it for inheritance.
  2165. If this option is t, a match early-on in a tree can lead to a large
  2166. number of matches in the subtree when constructing the agenda or creating
  2167. a sparse tree. If you only want to see the first match in a tree during
  2168. a search, check out the variable `org-tags-match-list-sublevels'."
  2169. :group 'org-tags
  2170. :type '(choice
  2171. (const :tag "Not" nil)
  2172. (const :tag "Always" t)
  2173. (repeat :tag "Specific tags" (string :tag "Tag"))
  2174. (regexp :tag "Tags matched by regexp")))
  2175. (defcustom org-tags-exclude-from-inheritance nil
  2176. "List of tags that should never be inherited.
  2177. This is a way to exclude a few tags from inheritance. For way to do
  2178. the opposite, to actively allow inheritance for selected tags,
  2179. see the variable `org-use-tag-inheritance'."
  2180. :group 'org-tags
  2181. :type '(repeat (string :tag "Tag")))
  2182. (defun org-tag-inherit-p (tag)
  2183. "Check if TAG is one that should be inherited."
  2184. (cond
  2185. ((member tag org-tags-exclude-from-inheritance) nil)
  2186. ((eq org-use-tag-inheritance t) t)
  2187. ((not org-use-tag-inheritance) nil)
  2188. ((stringp org-use-tag-inheritance)
  2189. (string-match org-use-tag-inheritance tag))
  2190. ((listp org-use-tag-inheritance)
  2191. (member tag org-use-tag-inheritance))
  2192. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2193. (defcustom org-tags-match-list-sublevels t
  2194. "Non-nil means list also sublevels of headlines matching a search.
  2195. This variable applies to tags/property searches, and also to stuck
  2196. projects because this search is based on a tags match as well.
  2197. When set to the symbol `indented', sublevels are indented with
  2198. leading dots.
  2199. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2200. the sublevels of a headline matching a tag search often also match
  2201. the same search. Listing all of them can create very long lists.
  2202. Setting this variable to nil causes subtrees of a match to be skipped.
  2203. This variable is semi-obsolete and probably should always be true. It
  2204. is better to limit inheritance to certain tags using the variables
  2205. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2206. :group 'org-tags
  2207. :type '(choice
  2208. (const :tag "No, don't list them" nil)
  2209. (const :tag "Yes, do list them" t)
  2210. (const :tag "List them, indented with leading dots" indented)))
  2211. (defcustom org-tags-sort-function nil
  2212. "When set, tags are sorted using this function as a comparator"
  2213. :group 'org-tags
  2214. :type '(choice
  2215. (const :tag "No sorting" nil)
  2216. (const :tag "Alphabetical" string<)
  2217. (const :tag "Reverse alphabetical" string>)
  2218. (function :tag "Custom function" nil)))
  2219. (defvar org-tags-history nil
  2220. "History of minibuffer reads for tags.")
  2221. (defvar org-last-tags-completion-table nil
  2222. "The last used completion table for tags.")
  2223. (defvar org-after-tags-change-hook nil
  2224. "Hook that is run after the tags in a line have changed.")
  2225. (defgroup org-properties nil
  2226. "Options concerning properties in Org-mode."
  2227. :tag "Org Properties"
  2228. :group 'org)
  2229. (defcustom org-property-format "%-10s %s"
  2230. "How property key/value pairs should be formatted by `indent-line'.
  2231. When `indent-line' hits a property definition, it will format the line
  2232. according to this format, mainly to make sure that the values are
  2233. lined-up with respect to each other."
  2234. :group 'org-properties
  2235. :type 'string)
  2236. (defcustom org-use-property-inheritance nil
  2237. "Non-nil means, properties apply also for sublevels.
  2238. This setting is chiefly used during property searches. Turning it on can
  2239. cause significant overhead when doing a search, which is why it is not
  2240. on by default.
  2241. When nil, only the properties directly given in the current entry count.
  2242. When t, every property is inherited. The value may also be a list of
  2243. properties that should have inheritance, or a regular expression matching
  2244. properties that should be inherited.
  2245. However, note that some special properties use inheritance under special
  2246. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2247. and the properties ending in \"_ALL\" when they are used as descriptor
  2248. for valid values of a property.
  2249. Note for programmers:
  2250. When querying an entry with `org-entry-get', you can control if inheritance
  2251. should be used. By default, `org-entry-get' looks only at the local
  2252. properties. You can request inheritance by setting the inherit argument
  2253. to t (to force inheritance) or to `selective' (to respect the setting
  2254. in this variable)."
  2255. :group 'org-properties
  2256. :type '(choice
  2257. (const :tag "Not" nil)
  2258. (const :tag "Always" t)
  2259. (repeat :tag "Specific properties" (string :tag "Property"))
  2260. (regexp :tag "Properties matched by regexp")))
  2261. (defun org-property-inherit-p (property)
  2262. "Check if PROPERTY is one that should be inherited."
  2263. (cond
  2264. ((eq org-use-property-inheritance t) t)
  2265. ((not org-use-property-inheritance) nil)
  2266. ((stringp org-use-property-inheritance)
  2267. (string-match org-use-property-inheritance property))
  2268. ((listp org-use-property-inheritance)
  2269. (member property org-use-property-inheritance))
  2270. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2271. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2272. "The default column format, if no other format has been defined.
  2273. This variable can be set on the per-file basis by inserting a line
  2274. #+COLUMNS: %25ITEM ....."
  2275. :group 'org-properties
  2276. :type 'string)
  2277. (defcustom org-columns-ellipses ".."
  2278. "The ellipses to be used when a field in column view is truncated.
  2279. When this is the empty string, as many characters as possible are shown,
  2280. but then there will be no visual indication that the field has been truncated.
  2281. When this is a string of length N, the last N characters of a truncated
  2282. field are replaced by this string. If the column is narrower than the
  2283. ellipses string, only part of the ellipses string will be shown."
  2284. :group 'org-properties
  2285. :type 'string)
  2286. (defcustom org-columns-modify-value-for-display-function nil
  2287. "Function that modifies values for display in column view.
  2288. For example, it can be used to cut out a certain part from a time stamp.
  2289. The function must take 2 arguments:
  2290. column-title The title of the column (*not* the property name)
  2291. value The value that should be modified.
  2292. The function should return the value that should be displayed,
  2293. or nil if the normal value should be used."
  2294. :group 'org-properties
  2295. :type 'function)
  2296. (defcustom org-effort-property "Effort"
  2297. "The property that is being used to keep track of effort estimates.
  2298. Effort estimates given in this property need to have the format H:MM."
  2299. :group 'org-properties
  2300. :group 'org-progress
  2301. :type '(string :tag "Property"))
  2302. (defconst org-global-properties-fixed
  2303. '(("VISIBILITY_ALL" . "folded children content all")
  2304. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2305. "List of property/value pairs that can be inherited by any entry.
  2306. These are fixed values, for the preset properties. The user variable
  2307. that can be used to add to this list is `org-global-properties'.
  2308. The entries in this list are cons cells where the car is a property
  2309. name and cdr is a string with the value. If the value represents
  2310. multiple items like an \"_ALL\" property, separate the items by
  2311. spaces.")
  2312. (defcustom org-global-properties nil
  2313. "List of property/value pairs that can be inherited by any entry.
  2314. This list will be combined with the constant `org-global-properties-fixed'.
  2315. The entries in this list are cons cells where the car is a property
  2316. name and cdr is a string with the value.
  2317. You can set buffer-local values for the same purpose in the variable
  2318. `org-file-properties' this by adding lines like
  2319. #+PROPERTY: NAME VALUE"
  2320. :group 'org-properties
  2321. :type '(repeat
  2322. (cons (string :tag "Property")
  2323. (string :tag "Value"))))
  2324. (defvar org-file-properties nil
  2325. "List of property/value pairs that can be inherited by any entry.
  2326. Valid for the current buffer.
  2327. This variable is populated from #+PROPERTY lines.")
  2328. (make-variable-buffer-local 'org-file-properties)
  2329. (defgroup org-agenda nil
  2330. "Options concerning agenda views in Org-mode."
  2331. :tag "Org Agenda"
  2332. :group 'org)
  2333. (defvar org-category nil
  2334. "Variable used by org files to set a category for agenda display.
  2335. Such files should use a file variable to set it, for example
  2336. # -*- mode: org; org-category: \"ELisp\"
  2337. or contain a special line
  2338. #+CATEGORY: ELisp
  2339. If the file does not specify a category, then file's base name
  2340. is used instead.")
  2341. (make-variable-buffer-local 'org-category)
  2342. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2343. (defcustom org-agenda-files nil
  2344. "The files to be used for agenda display.
  2345. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2346. \\[org-remove-file]. You can also use customize to edit the list.
  2347. If an entry is a directory, all files in that directory that are matched by
  2348. `org-agenda-file-regexp' will be part of the file list.
  2349. If the value of the variable is not a list but a single file name, then
  2350. the list of agenda files is actually stored and maintained in that file, one
  2351. agenda file per line."
  2352. :group 'org-agenda
  2353. :type '(choice
  2354. (repeat :tag "List of files and directories" file)
  2355. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2356. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2357. "Regular expression to match files for `org-agenda-files'.
  2358. If any element in the list in that variable contains a directory instead
  2359. of a normal file, all files in that directory that are matched by this
  2360. regular expression will be included."
  2361. :group 'org-agenda
  2362. :type 'regexp)
  2363. (defcustom org-agenda-text-search-extra-files nil
  2364. "List of extra files to be searched by text search commands.
  2365. These files will be search in addition to the agenda files by the
  2366. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2367. Note that these files will only be searched for text search commands,
  2368. not for the other agenda views like todo lists, tag searches or the weekly
  2369. agenda. This variable is intended to list notes and possibly archive files
  2370. that should also be searched by these two commands.
  2371. In fact, if the first element in the list is the symbol `agenda-archives',
  2372. than all archive files of all agenda files will be added to the search
  2373. scope."
  2374. :group 'org-agenda
  2375. :type '(set :greedy t
  2376. (const :tag "Agenda Archives" agenda-archives)
  2377. (repeat :inline t (file))))
  2378. (if (fboundp 'defvaralias)
  2379. (defvaralias 'org-agenda-multi-occur-extra-files
  2380. 'org-agenda-text-search-extra-files))
  2381. (defcustom org-agenda-skip-unavailable-files nil
  2382. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2383. A nil value means to remove them, after a query, from the list."
  2384. :group 'org-agenda
  2385. :type 'boolean)
  2386. (defcustom org-calendar-to-agenda-key [?c]
  2387. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2388. The command `org-calendar-goto-agenda' will be bound to this key. The
  2389. default is the character `c' because then `c' can be used to switch back and
  2390. forth between agenda and calendar."
  2391. :group 'org-agenda
  2392. :type 'sexp)
  2393. (defcustom org-calendar-agenda-action-key [?k]
  2394. "The key to be installed in `calendar-mode-map' for agenda-action.
  2395. The command `org-agenda-action' will be bound to this key. The
  2396. default is the character `k' because we use the same key in the agenda."
  2397. :group 'org-agenda
  2398. :type 'sexp)
  2399. (defcustom org-calendar-insert-diary-entry-key [?i]
  2400. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2401. This option is irrelevant until `org-agenda-diary-file' has been configured
  2402. to point to an Org-mode file. When that is the case, the command
  2403. `org-agenda-diary-entry' will be bound to the key given here, by default
  2404. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2405. if you want to continue doing this, you need to change this to a different
  2406. key."
  2407. :group 'org-agenda
  2408. :type 'sexp)
  2409. (defcustom org-agenda-diary-file 'diary-file
  2410. "File to which to add new entries with the `i' key in agenda and calendar.
  2411. When this is the symbol `diary-file', the functionality in the Emacs
  2412. calendar will be used to add entries to the `diary-file'. But when this
  2413. points to a file, `org-agenda-diary-entry' will be used instead."
  2414. :group 'org-agenda
  2415. :type '(choice
  2416. (const :tag "The standard Emacs diary file" diary-file)
  2417. (file :tag "Special Org file diary entries")))
  2418. (eval-after-load "calendar"
  2419. '(progn
  2420. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2421. 'org-calendar-goto-agenda)
  2422. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2423. 'org-agenda-action)
  2424. (add-hook 'calendar-mode-hook
  2425. (lambda ()
  2426. (unless (eq org-agenda-diary-file 'diary-file)
  2427. (define-key calendar-mode-map
  2428. org-calendar-insert-diary-entry-key
  2429. 'org-agenda-diary-entry))))))
  2430. (defgroup org-latex nil
  2431. "Options for embedding LaTeX code into Org-mode."
  2432. :tag "Org LaTeX"
  2433. :group 'org)
  2434. (defcustom org-format-latex-options
  2435. '(:foreground default :background default :scale 1.0
  2436. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2437. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2438. "Options for creating images from LaTeX fragments.
  2439. This is a property list with the following properties:
  2440. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2441. `default' means use the foreground of the default face.
  2442. :background the background color, or \"Transparent\".
  2443. `default' means use the background of the default face.
  2444. :scale a scaling factor for the size of the images.
  2445. :html-foreground, :html-background, :html-scale
  2446. the same numbers for HTML export.
  2447. :matchers a list indicating which matchers should be used to
  2448. find LaTeX fragments. Valid members of this list are:
  2449. \"begin\" find environments
  2450. \"$1\" find single characters surrounded by $.$
  2451. \"$\" find math expressions surrounded by $...$
  2452. \"$$\" find math expressions surrounded by $$....$$
  2453. \"\\(\" find math expressions surrounded by \\(...\\)
  2454. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2455. :group 'org-latex
  2456. :type 'plist)
  2457. (defcustom org-format-latex-header "\\documentclass{article}
  2458. \\usepackage{amssymb}
  2459. \\usepackage[usenames]{color}
  2460. \\usepackage{amsmath}
  2461. \\usepackage{latexsym}
  2462. \\usepackage[mathscr]{eucal}
  2463. \\pagestyle{empty} % do not remove
  2464. % The settings below are copied from fullpage.sty
  2465. \\setlength{\\textwidth}{\\paperwidth}
  2466. \\addtolength{\\textwidth}{-3cm}
  2467. \\setlength{\\oddsidemargin}{1.5cm}
  2468. \\addtolength{\\oddsidemargin}{-2.54cm}
  2469. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2470. \\setlength{\\textheight}{\\paperheight}
  2471. \\addtolength{\\textheight}{-\\headheight}
  2472. \\addtolength{\\textheight}{-\\headsep}
  2473. \\addtolength{\\textheight}{-\\footskip}
  2474. \\addtolength{\\textheight}{-3cm}
  2475. \\setlength{\\topmargin}{1.5cm}
  2476. \\addtolength{\\topmargin}{-2.54cm}"
  2477. "The document header used for processing LaTeX fragments.
  2478. It is imperative that this header make sure that no page number
  2479. appears on the page."
  2480. :group 'org-latex
  2481. :type 'string)
  2482. (defgroup org-font-lock nil
  2483. "Font-lock settings for highlighting in Org-mode."
  2484. :tag "Org Font Lock"
  2485. :group 'org)
  2486. (defcustom org-level-color-stars-only nil
  2487. "Non-nil means fontify only the stars in each headline.
  2488. When nil, the entire headline is fontified.
  2489. Changing it requires restart of `font-lock-mode' to become effective
  2490. also in regions already fontified."
  2491. :group 'org-font-lock
  2492. :type 'boolean)
  2493. (defcustom org-hide-leading-stars nil
  2494. "Non-nil means, hide the first N-1 stars in a headline.
  2495. This works by using the face `org-hide' for these stars. This
  2496. face is white for a light background, and black for a dark
  2497. background. You may have to customize the face `org-hide' to
  2498. make this work.
  2499. Changing it requires restart of `font-lock-mode' to become effective
  2500. also in regions already fontified.
  2501. You may also set this on a per-file basis by adding one of the following
  2502. lines to the buffer:
  2503. #+STARTUP: hidestars
  2504. #+STARTUP: showstars"
  2505. :group 'org-font-lock
  2506. :type 'boolean)
  2507. (defcustom org-fontify-done-headline nil
  2508. "Non-nil means, change the face of a headline if it is marked DONE.
  2509. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2510. When this is non-nil, the headline after the keyword is set to the
  2511. `org-headline-done' as an additional indication."
  2512. :group 'org-font-lock
  2513. :type 'boolean)
  2514. (defcustom org-fontify-emphasized-text t
  2515. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2516. Changing this variable requires a restart of Emacs to take effect."
  2517. :group 'org-font-lock
  2518. :type 'boolean)
  2519. (defcustom org-fontify-whole-heading-line nil
  2520. "Non-nil means fontify the whole line for headings.
  2521. This is useful when setting a background color for the
  2522. org-level-* faces."
  2523. :group 'org-font-lock
  2524. :type 'boolean)
  2525. (defcustom org-highlight-latex-fragments-and-specials nil
  2526. "Non-nil means, fontify what is treated specially by the exporters."
  2527. :group 'org-font-lock
  2528. :type 'boolean)
  2529. (defcustom org-hide-emphasis-markers nil
  2530. "Non-nil mean font-lock should hide the emphasis marker characters."
  2531. :group 'org-font-lock
  2532. :type 'boolean)
  2533. (defvar org-emph-re nil
  2534. "Regular expression for matching emphasis.")
  2535. (defvar org-verbatim-re nil
  2536. "Regular expression for matching verbatim text.")
  2537. (defvar org-emphasis-regexp-components) ; defined just below
  2538. (defvar org-emphasis-alist) ; defined just below
  2539. (defun org-set-emph-re (var val)
  2540. "Set variable and compute the emphasis regular expression."
  2541. (set var val)
  2542. (when (and (boundp 'org-emphasis-alist)
  2543. (boundp 'org-emphasis-regexp-components)
  2544. org-emphasis-alist org-emphasis-regexp-components)
  2545. (let* ((e org-emphasis-regexp-components)
  2546. (pre (car e))
  2547. (post (nth 1 e))
  2548. (border (nth 2 e))
  2549. (body (nth 3 e))
  2550. (nl (nth 4 e))
  2551. (body1 (concat body "*?"))
  2552. (markers (mapconcat 'car org-emphasis-alist ""))
  2553. (vmarkers (mapconcat
  2554. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2555. org-emphasis-alist "")))
  2556. ;; make sure special characters appear at the right position in the class
  2557. (if (string-match "\\^" markers)
  2558. (setq markers (concat (replace-match "" t t markers) "^")))
  2559. (if (string-match "-" markers)
  2560. (setq markers (concat (replace-match "" t t markers) "-")))
  2561. (if (string-match "\\^" vmarkers)
  2562. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2563. (if (string-match "-" vmarkers)
  2564. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2565. (if (> nl 0)
  2566. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2567. (int-to-string nl) "\\}")))
  2568. ;; Make the regexp
  2569. (setq org-emph-re
  2570. (concat "\\([" pre "]\\|^\\)"
  2571. "\\("
  2572. "\\([" markers "]\\)"
  2573. "\\("
  2574. "[^" border "]\\|"
  2575. "[^" border "]"
  2576. body1
  2577. "[^" border "]"
  2578. "\\)"
  2579. "\\3\\)"
  2580. "\\([" post "]\\|$\\)"))
  2581. (setq org-verbatim-re
  2582. (concat "\\([" pre "]\\|^\\)"
  2583. "\\("
  2584. "\\([" vmarkers "]\\)"
  2585. "\\("
  2586. "[^" border "]\\|"
  2587. "[^" border "]"
  2588. body1
  2589. "[^" border "]"
  2590. "\\)"
  2591. "\\3\\)"
  2592. "\\([" post "]\\|$\\)")))))
  2593. (defcustom org-emphasis-regexp-components
  2594. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2595. "Components used to build the regular expression for emphasis.
  2596. This is a list with 6 entries. Terminology: In an emphasis string
  2597. like \" *strong word* \", we call the initial space PREMATCH, the final
  2598. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2599. and \"trong wor\" is the body. The different components in this variable
  2600. specify what is allowed/forbidden in each part:
  2601. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2602. post Chars allowed as postmatch. End of line will be allowed too.
  2603. border The chars *forbidden* as border characters.
  2604. body-regexp A regexp like \".\" to match a body character. Don't use
  2605. non-shy groups here, and don't allow newline here.
  2606. newline The maximum number of newlines allowed in an emphasis exp.
  2607. Use customize to modify this, or restart Emacs after changing it."
  2608. :group 'org-font-lock
  2609. :set 'org-set-emph-re
  2610. :type '(list
  2611. (sexp :tag "Allowed chars in pre ")
  2612. (sexp :tag "Allowed chars in post ")
  2613. (sexp :tag "Forbidden chars in border ")
  2614. (sexp :tag "Regexp for body ")
  2615. (integer :tag "number of newlines allowed")
  2616. (option (boolean :tag "Please ignore this button"))))
  2617. (defcustom org-emphasis-alist
  2618. `(("*" bold "<b>" "</b>")
  2619. ("/" italic "<i>" "</i>")
  2620. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2621. ("=" org-code "<code>" "</code>" verbatim)
  2622. ("~" org-verbatim "<code>" "</code>" verbatim)
  2623. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2624. "<del>" "</del>")
  2625. )
  2626. "Special syntax for emphasized text.
  2627. Text starting and ending with a special character will be emphasized, for
  2628. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2629. characters, the face to be used by font-lock for highlighting in Org-mode
  2630. Emacs buffers, and the HTML tags to be used for this.
  2631. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2632. Use customize to modify this, or restart Emacs after changing it."
  2633. :group 'org-font-lock
  2634. :set 'org-set-emph-re
  2635. :type '(repeat
  2636. (list
  2637. (string :tag "Marker character")
  2638. (choice
  2639. (face :tag "Font-lock-face")
  2640. (plist :tag "Face property list"))
  2641. (string :tag "HTML start tag")
  2642. (string :tag "HTML end tag")
  2643. (option (const verbatim)))))
  2644. (defvar org-protecting-blocks
  2645. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2646. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2647. This is needed for font-lock setup.")
  2648. ;;; Miscellaneous options
  2649. (defgroup org-completion nil
  2650. "Completion in Org-mode."
  2651. :tag "Org Completion"
  2652. :group 'org)
  2653. (defcustom org-completion-use-ido nil
  2654. "Non-nil means, use ido completion wherever possible.
  2655. Note that `ido-mode' must be active for this variable to be relevant.
  2656. If you decide to turn this variable on, you might well want to turn off
  2657. `org-outline-path-complete-in-steps'.
  2658. See also `org-completion-use-iswitchb'."
  2659. :group 'org-completion
  2660. :type 'boolean)
  2661. (defcustom org-completion-use-iswitchb nil
  2662. "Non-nil means, use iswitchb completion wherever possible.
  2663. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2664. If you decide to turn this variable on, you might well want to turn off
  2665. `org-outline-path-complete-in-steps'.
  2666. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2667. :group 'org-completion
  2668. :type 'boolean)
  2669. (defcustom org-completion-fallback-command 'hippie-expand
  2670. "The expansion command called by \\[org-complete] in normal context.
  2671. Normal means, no org-mode-specific context."
  2672. :group 'org-completion
  2673. :type 'function)
  2674. ;;; Functions and variables from their packages
  2675. ;; Declared here to avoid compiler warnings
  2676. ;; XEmacs only
  2677. (defvar outline-mode-menu-heading)
  2678. (defvar outline-mode-menu-show)
  2679. (defvar outline-mode-menu-hide)
  2680. (defvar zmacs-regions) ; XEmacs regions
  2681. ;; Emacs only
  2682. (defvar mark-active)
  2683. ;; Various packages
  2684. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2685. (declare-function calendar-forward-day "cal-move" (arg))
  2686. (declare-function calendar-goto-date "cal-move" (date))
  2687. (declare-function calendar-goto-today "cal-move" ())
  2688. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2689. (defvar calc-embedded-close-formula)
  2690. (defvar calc-embedded-open-formula)
  2691. (declare-function cdlatex-tab "ext:cdlatex" ())
  2692. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2693. (defvar font-lock-unfontify-region-function)
  2694. (declare-function iswitchb-read-buffer "iswitchb"
  2695. (prompt &optional default require-match start matches-set))
  2696. (defvar iswitchb-temp-buflist)
  2697. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2698. (defvar org-agenda-tags-todo-honor-ignore-options)
  2699. (declare-function org-agenda-skip "org-agenda" ())
  2700. (declare-function
  2701. org-format-agenda-item "org-agenda"
  2702. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2703. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2704. (declare-function org-agenda-change-all-lines "org-agenda"
  2705. (newhead hdmarker &optional fixface just-this))
  2706. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2707. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2708. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2709. (beg end))
  2710. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2711. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2712. "org-agenda" (&optional end))
  2713. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2714. (declare-function org-indent-mode "org-indent" (&optional arg))
  2715. (declare-function parse-time-string "parse-time" (string))
  2716. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2717. (defvar remember-data-file)
  2718. (defvar texmathp-why)
  2719. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2720. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2721. (defvar w3m-current-url)
  2722. (defvar w3m-current-title)
  2723. (defvar org-latex-regexps)
  2724. ;;; Autoload and prepare some org modules
  2725. ;; Some table stuff that needs to be defined here, because it is used
  2726. ;; by the functions setting up org-mode or checking for table context.
  2727. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2728. "Detects an org-type or table-type table.")
  2729. (defconst org-table-line-regexp "^[ \t]*|"
  2730. "Detects an org-type table line.")
  2731. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2732. "Detects an org-type table line.")
  2733. (defconst org-table-hline-regexp "^[ \t]*|-"
  2734. "Detects an org-type table hline.")
  2735. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2736. "Detects a table-type table hline.")
  2737. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2738. "Searching from within a table (any type) this finds the first line
  2739. outside the table.")
  2740. ;; Autoload the functions in org-table.el that are needed by functions here.
  2741. (eval-and-compile
  2742. (org-autoload "org-table"
  2743. '(org-table-align org-table-begin org-table-blank-field
  2744. org-table-convert org-table-convert-region org-table-copy-down
  2745. org-table-copy-region org-table-create
  2746. org-table-create-or-convert-from-region
  2747. org-table-create-with-table.el org-table-current-dline
  2748. org-table-cut-region org-table-delete-column org-table-edit-field
  2749. org-table-edit-formulas org-table-end org-table-eval-formula
  2750. org-table-export org-table-field-info
  2751. org-table-get-stored-formulas org-table-goto-column
  2752. org-table-hline-and-move org-table-import org-table-insert-column
  2753. org-table-insert-hline org-table-insert-row org-table-iterate
  2754. org-table-justify-field-maybe org-table-kill-row
  2755. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2756. org-table-move-column org-table-move-column-left
  2757. org-table-move-column-right org-table-move-row
  2758. org-table-move-row-down org-table-move-row-up
  2759. org-table-next-field org-table-next-row org-table-paste-rectangle
  2760. org-table-previous-field org-table-recalculate
  2761. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2762. org-table-toggle-coordinate-overlays
  2763. org-table-toggle-formula-debugger org-table-wrap-region
  2764. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2765. (defun org-at-table-p (&optional table-type)
  2766. "Return t if the cursor is inside an org-type table.
  2767. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2768. (if org-enable-table-editor
  2769. (save-excursion
  2770. (beginning-of-line 1)
  2771. (looking-at (if table-type org-table-any-line-regexp
  2772. org-table-line-regexp)))
  2773. nil))
  2774. (defsubst org-table-p () (org-at-table-p))
  2775. (defun org-at-table.el-p ()
  2776. "Return t if and only if we are at a table.el table."
  2777. (and (org-at-table-p 'any)
  2778. (save-excursion
  2779. (goto-char (org-table-begin 'any))
  2780. (looking-at org-table1-hline-regexp))))
  2781. (defun org-table-recognize-table.el ()
  2782. "If there is a table.el table nearby, recognize it and move into it."
  2783. (if org-table-tab-recognizes-table.el
  2784. (if (org-at-table.el-p)
  2785. (progn
  2786. (beginning-of-line 1)
  2787. (if (looking-at org-table-dataline-regexp)
  2788. nil
  2789. (if (looking-at org-table1-hline-regexp)
  2790. (progn
  2791. (beginning-of-line 2)
  2792. (if (looking-at org-table-any-border-regexp)
  2793. (beginning-of-line -1)))))
  2794. (if (re-search-forward "|" (org-table-end t) t)
  2795. (progn
  2796. (require 'table)
  2797. (if (table--at-cell-p (point))
  2798. t
  2799. (message "recognizing table.el table...")
  2800. (table-recognize-table)
  2801. (message "recognizing table.el table...done")))
  2802. (error "This should not happen..."))
  2803. t)
  2804. nil)
  2805. nil))
  2806. (defun org-at-table-hline-p ()
  2807. "Return t if the cursor is inside a hline in a table."
  2808. (if org-enable-table-editor
  2809. (save-excursion
  2810. (beginning-of-line 1)
  2811. (looking-at org-table-hline-regexp))
  2812. nil))
  2813. (defvar org-table-clean-did-remove-column nil)
  2814. (defun org-table-map-tables (function)
  2815. "Apply FUNCTION to the start of all tables in the buffer."
  2816. (save-excursion
  2817. (save-restriction
  2818. (widen)
  2819. (goto-char (point-min))
  2820. (while (re-search-forward org-table-any-line-regexp nil t)
  2821. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2822. (beginning-of-line 1)
  2823. (when (looking-at org-table-line-regexp)
  2824. (save-excursion (funcall function))
  2825. (or (looking-at org-table-line-regexp)
  2826. (forward-char 1)))
  2827. (re-search-forward org-table-any-border-regexp nil 1))))
  2828. (message "Mapping tables: done"))
  2829. ;; Declare and autoload functions from org-exp.el & Co
  2830. (declare-function org-default-export-plist "org-exp")
  2831. (declare-function org-infile-export-plist "org-exp")
  2832. (declare-function org-get-current-options "org-exp")
  2833. (eval-and-compile
  2834. (org-autoload "org-exp"
  2835. '(org-export org-export-visible
  2836. org-insert-export-options-template
  2837. org-table-clean-before-export))
  2838. (org-autoload "org-ascii"
  2839. '(org-export-as-ascii org-export-ascii-preprocess
  2840. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2841. org-export-region-as-ascii))
  2842. (org-autoload "org-html"
  2843. '(org-export-as-html-and-open
  2844. org-export-as-html-batch org-export-as-html-to-buffer
  2845. org-replace-region-by-html org-export-region-as-html
  2846. org-export-as-html))
  2847. (org-autoload "org-icalendar"
  2848. '(org-export-icalendar-this-file
  2849. org-export-icalendar-all-agenda-files
  2850. org-export-icalendar-combine-agenda-files))
  2851. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2852. ;; Declare and autoload functions from org-agenda.el
  2853. (eval-and-compile
  2854. (org-autoload "org-agenda"
  2855. '(org-agenda org-agenda-list org-search-view
  2856. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2857. org-diary org-agenda-to-appt
  2858. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2859. ;; Autoload org-remember
  2860. (eval-and-compile
  2861. (org-autoload "org-remember"
  2862. '(org-remember-insinuate org-remember-annotation
  2863. org-remember-apply-template org-remember org-remember-handler)))
  2864. ;; Autoload org-clock.el
  2865. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2866. (beg end))
  2867. (declare-function org-clock-update-mode-line "org-clock" ())
  2868. (declare-function org-resolve-clocks "org-clock"
  2869. (&optional also-non-dangling-p prompt last-valid))
  2870. (defvar org-clock-start-time)
  2871. (defvar org-clock-marker (make-marker)
  2872. "Marker recording the last clock-in.")
  2873. (defvar org-clock-hd-marker (make-marker)
  2874. "Marker recording the last clock-in, but the headline position.")
  2875. (defvar org-clock-heading ""
  2876. "The heading of the current clock entry.")
  2877. (defun org-clock-is-active ()
  2878. "Return non-nil if clock is currently running.
  2879. The return value is actually the clock marker."
  2880. (marker-buffer org-clock-marker))
  2881. (eval-and-compile
  2882. (org-autoload
  2883. "org-clock"
  2884. '(org-clock-in org-clock-out org-clock-cancel
  2885. org-clock-goto org-clock-sum org-clock-display
  2886. org-clock-remove-overlays org-clock-report
  2887. org-clocktable-shift org-dblock-write:clocktable
  2888. org-get-clocktable org-resolve-clocks)))
  2889. (defun org-clock-update-time-maybe ()
  2890. "If this is a CLOCK line, update it and return t.
  2891. Otherwise, return nil."
  2892. (interactive)
  2893. (save-excursion
  2894. (beginning-of-line 1)
  2895. (skip-chars-forward " \t")
  2896. (when (looking-at org-clock-string)
  2897. (let ((re (concat "[ \t]*" org-clock-string
  2898. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2899. "\\([ \t]*=>.*\\)?\\)?"))
  2900. ts te h m s neg)
  2901. (cond
  2902. ((not (looking-at re))
  2903. nil)
  2904. ((not (match-end 2))
  2905. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2906. (> org-clock-marker (point))
  2907. (<= org-clock-marker (point-at-eol)))
  2908. ;; The clock is running here
  2909. (setq org-clock-start-time
  2910. (apply 'encode-time
  2911. (org-parse-time-string (match-string 1))))
  2912. (org-clock-update-mode-line)))
  2913. (t
  2914. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2915. (end-of-line 1)
  2916. (setq ts (match-string 1)
  2917. te (match-string 3))
  2918. (setq s (- (org-float-time
  2919. (apply 'encode-time (org-parse-time-string te)))
  2920. (org-float-time
  2921. (apply 'encode-time (org-parse-time-string ts))))
  2922. neg (< s 0)
  2923. s (abs s)
  2924. h (floor (/ s 3600))
  2925. s (- s (* 3600 h))
  2926. m (floor (/ s 60))
  2927. s (- s (* 60 s)))
  2928. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2929. t))))))
  2930. (defun org-check-running-clock ()
  2931. "Check if the current buffer contains the running clock.
  2932. If yes, offer to stop it and to save the buffer with the changes."
  2933. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2934. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2935. (buffer-name))))
  2936. (org-clock-out)
  2937. (when (y-or-n-p "Save changed buffer?")
  2938. (save-buffer))))
  2939. (defun org-clocktable-try-shift (dir n)
  2940. "Check if this line starts a clock table, if yes, shift the time block."
  2941. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2942. (org-clocktable-shift dir n)))
  2943. ;; Autoload org-timer.el
  2944. (eval-and-compile
  2945. (org-autoload
  2946. "org-timer"
  2947. '(org-timer-start org-timer org-timer-item
  2948. org-timer-change-times-in-region
  2949. org-timer-set-timer
  2950. org-timer-reset-timers
  2951. org-timer-show-remaining-time)))
  2952. ;; Autoload org-feed.el
  2953. (eval-and-compile
  2954. (org-autoload
  2955. "org-feed"
  2956. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2957. ;; Autoload org-indent.el
  2958. ;; Define the variable already here, to make sure we have it.
  2959. (defvar org-indent-mode nil
  2960. "Non-nil if Org-Indent mode is enabled.
  2961. Use the command `org-indent-mode' to change this variable.")
  2962. (eval-and-compile
  2963. (org-autoload
  2964. "org-indent"
  2965. '(org-indent-mode)))
  2966. ;; Autoload org-mobile.el
  2967. (eval-and-compile
  2968. (org-autoload
  2969. "org-mobile"
  2970. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  2971. ;; Autoload archiving code
  2972. ;; The stuff that is needed for cycling and tags has to be defined here.
  2973. (defgroup org-archive nil
  2974. "Options concerning archiving in Org-mode."
  2975. :tag "Org Archive"
  2976. :group 'org-structure)
  2977. (defcustom org-archive-location "%s_archive::"
  2978. "The location where subtrees should be archived.
  2979. The value of this variable is a string, consisting of two parts,
  2980. separated by a double-colon. The first part is a filename and
  2981. the second part is a headline.
  2982. When the filename is omitted, archiving happens in the same file.
  2983. %s in the filename will be replaced by the current file
  2984. name (without the directory part). Archiving to a different file
  2985. is useful to keep archived entries from contributing to the
  2986. Org-mode Agenda.
  2987. The archived entries will be filed as subtrees of the specified
  2988. headline. When the headline is omitted, the subtrees are simply
  2989. filed away at the end of the file, as top-level entries. Also in
  2990. the heading you can use %s to represent the file name, this can be
  2991. useful when using the same archive for a number of different files.
  2992. Here are a few examples:
  2993. \"%s_archive::\"
  2994. If the current file is Projects.org, archive in file
  2995. Projects.org_archive, as top-level trees. This is the default.
  2996. \"::* Archived Tasks\"
  2997. Archive in the current file, under the top-level headline
  2998. \"* Archived Tasks\".
  2999. \"~/org/archive.org::\"
  3000. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3001. \"~/org/archive.org::From %s\"
  3002. Archive in file ~/org/archive.org (absolute path), under headlines
  3003. \"From FILENAME\" where file name is the current file name.
  3004. \"basement::** Finished Tasks\"
  3005. Archive in file ./basement (relative path), as level 3 trees
  3006. below the level 2 heading \"** Finished Tasks\".
  3007. You may set this option on a per-file basis by adding to the buffer a
  3008. line like
  3009. #+ARCHIVE: basement::** Finished Tasks
  3010. You may also define it locally for a subtree by setting an ARCHIVE property
  3011. in the entry. If such a property is found in an entry, or anywhere up
  3012. the hierarchy, it will be used."
  3013. :group 'org-archive
  3014. :type 'string)
  3015. (defcustom org-archive-tag "ARCHIVE"
  3016. "The tag that marks a subtree as archived.
  3017. An archived subtree does not open during visibility cycling, and does
  3018. not contribute to the agenda listings.
  3019. After changing this, font-lock must be restarted in the relevant buffers to
  3020. get the proper fontification."
  3021. :group 'org-archive
  3022. :group 'org-keywords
  3023. :type 'string)
  3024. (defcustom org-agenda-skip-archived-trees t
  3025. "Non-nil means, the agenda will skip any items located in archived trees.
  3026. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3027. variable is no longer recommended, you should leave it at the value t.
  3028. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3029. :group 'org-archive
  3030. :group 'org-agenda-skip
  3031. :type 'boolean)
  3032. (defcustom org-columns-skip-archived-trees t
  3033. "Non-nil means, ignore archived trees when creating column view."
  3034. :group 'org-archive
  3035. :group 'org-properties
  3036. :type 'boolean)
  3037. (defcustom org-cycle-open-archived-trees nil
  3038. "Non-nil means, `org-cycle' will open archived trees.
  3039. An archived tree is a tree marked with the tag ARCHIVE.
  3040. When nil, archived trees will stay folded. You can still open them with
  3041. normal outline commands like `show-all', but not with the cycling commands."
  3042. :group 'org-archive
  3043. :group 'org-cycle
  3044. :type 'boolean)
  3045. (defcustom org-sparse-tree-open-archived-trees nil
  3046. "Non-nil means sparse tree construction shows matches in archived trees.
  3047. When nil, matches in these trees are highlighted, but the trees are kept in
  3048. collapsed state."
  3049. :group 'org-archive
  3050. :group 'org-sparse-trees
  3051. :type 'boolean)
  3052. (defun org-cycle-hide-archived-subtrees (state)
  3053. "Re-hide all archived subtrees after a visibility state change."
  3054. (when (and (not org-cycle-open-archived-trees)
  3055. (not (memq state '(overview folded))))
  3056. (save-excursion
  3057. (let* ((globalp (memq state '(contents all)))
  3058. (beg (if globalp (point-min) (point)))
  3059. (end (if globalp (point-max) (org-end-of-subtree t))))
  3060. (org-hide-archived-subtrees beg end)
  3061. (goto-char beg)
  3062. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3063. (message "%s" (substitute-command-keys
  3064. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3065. (defun org-force-cycle-archived ()
  3066. "Cycle subtree even if it is archived."
  3067. (interactive)
  3068. (setq this-command 'org-cycle)
  3069. (let ((org-cycle-open-archived-trees t))
  3070. (call-interactively 'org-cycle)))
  3071. (defun org-hide-archived-subtrees (beg end)
  3072. "Re-hide all archived subtrees after a visibility state change."
  3073. (save-excursion
  3074. (let* ((re (concat ":" org-archive-tag ":")))
  3075. (goto-char beg)
  3076. (while (re-search-forward re end t)
  3077. (and (org-on-heading-p) (org-flag-subtree t))
  3078. (org-end-of-subtree t)))))
  3079. (defun org-flag-subtree (flag)
  3080. (save-excursion
  3081. (org-back-to-heading t)
  3082. (outline-end-of-heading)
  3083. (outline-flag-region (point)
  3084. (progn (org-end-of-subtree t) (point))
  3085. flag)))
  3086. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3087. (eval-and-compile
  3088. (org-autoload "org-archive"
  3089. '(org-add-archive-files org-archive-subtree
  3090. org-archive-to-archive-sibling org-toggle-archive-tag)))
  3091. ;; Autoload Column View Code
  3092. (declare-function org-columns-number-to-string "org-colview")
  3093. (declare-function org-columns-get-format-and-top-level "org-colview")
  3094. (declare-function org-columns-compute "org-colview")
  3095. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3096. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3097. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3098. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3099. ;; Autoload ID code
  3100. (declare-function org-id-store-link "org-id")
  3101. (declare-function org-id-locations-load "org-id")
  3102. (declare-function org-id-locations-save "org-id")
  3103. (defvar org-id-track-globally)
  3104. (org-autoload "org-id"
  3105. '(org-id-get-create org-id-new org-id-copy org-id-get
  3106. org-id-get-with-outline-path-completion
  3107. org-id-get-with-outline-drilling
  3108. org-id-goto org-id-find org-id-store-link))
  3109. ;; Autoload Plotting Code
  3110. (org-autoload "org-plot"
  3111. '(org-plot/gnuplot))
  3112. ;;; Variables for pre-computed regular expressions, all buffer local
  3113. (defvar org-drawer-regexp nil
  3114. "Matches first line of a hidden block.")
  3115. (make-variable-buffer-local 'org-drawer-regexp)
  3116. (defvar org-todo-regexp nil
  3117. "Matches any of the TODO state keywords.")
  3118. (make-variable-buffer-local 'org-todo-regexp)
  3119. (defvar org-not-done-regexp nil
  3120. "Matches any of the TODO state keywords except the last one.")
  3121. (make-variable-buffer-local 'org-not-done-regexp)
  3122. (defvar org-not-done-heading-regexp nil
  3123. "Matches a TODO headline that is not done.")
  3124. (make-variable-buffer-local 'org-not-done-regexp)
  3125. (defvar org-todo-line-regexp nil
  3126. "Matches a headline and puts TODO state into group 2 if present.")
  3127. (make-variable-buffer-local 'org-todo-line-regexp)
  3128. (defvar org-complex-heading-regexp nil
  3129. "Matches a headline and puts everything into groups:
  3130. group 1: the stars
  3131. group 2: The todo keyword, maybe
  3132. group 3: Priority cookie
  3133. group 4: True headline
  3134. group 5: Tags")
  3135. (make-variable-buffer-local 'org-complex-heading-regexp)
  3136. (defvar org-complex-heading-regexp-format nil)
  3137. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3138. (defvar org-todo-line-tags-regexp nil
  3139. "Matches a headline and puts TODO state into group 2 if present.
  3140. Also put tags into group 4 if tags are present.")
  3141. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3142. (defvar org-nl-done-regexp nil
  3143. "Matches newline followed by a headline with the DONE keyword.")
  3144. (make-variable-buffer-local 'org-nl-done-regexp)
  3145. (defvar org-looking-at-done-regexp nil
  3146. "Matches the DONE keyword a point.")
  3147. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3148. (defvar org-ds-keyword-length 12
  3149. "Maximum length of the Deadline and SCHEDULED keywords.")
  3150. (make-variable-buffer-local 'org-ds-keyword-length)
  3151. (defvar org-deadline-regexp nil
  3152. "Matches the DEADLINE keyword.")
  3153. (make-variable-buffer-local 'org-deadline-regexp)
  3154. (defvar org-deadline-time-regexp nil
  3155. "Matches the DEADLINE keyword together with a time stamp.")
  3156. (make-variable-buffer-local 'org-deadline-time-regexp)
  3157. (defvar org-deadline-line-regexp nil
  3158. "Matches the DEADLINE keyword and the rest of the line.")
  3159. (make-variable-buffer-local 'org-deadline-line-regexp)
  3160. (defvar org-scheduled-regexp nil
  3161. "Matches the SCHEDULED keyword.")
  3162. (make-variable-buffer-local 'org-scheduled-regexp)
  3163. (defvar org-scheduled-time-regexp nil
  3164. "Matches the SCHEDULED keyword together with a time stamp.")
  3165. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3166. (defvar org-closed-time-regexp nil
  3167. "Matches the CLOSED keyword together with a time stamp.")
  3168. (make-variable-buffer-local 'org-closed-time-regexp)
  3169. (defvar org-keyword-time-regexp nil
  3170. "Matches any of the 4 keywords, together with the time stamp.")
  3171. (make-variable-buffer-local 'org-keyword-time-regexp)
  3172. (defvar org-keyword-time-not-clock-regexp nil
  3173. "Matches any of the 3 keywords, together with the time stamp.")
  3174. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3175. (defvar org-maybe-keyword-time-regexp nil
  3176. "Matches a timestamp, possibly preceeded by a keyword.")
  3177. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3178. (defvar org-planning-or-clock-line-re nil
  3179. "Matches a line with planning or clock info.")
  3180. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3181. (defconst org-plain-time-of-day-regexp
  3182. (concat
  3183. "\\(\\<[012]?[0-9]"
  3184. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3185. "\\(--?"
  3186. "\\(\\<[012]?[0-9]"
  3187. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3188. "\\)?")
  3189. "Regular expression to match a plain time or time range.
  3190. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3191. groups carry important information:
  3192. 0 the full match
  3193. 1 the first time, range or not
  3194. 8 the second time, if it is a range.")
  3195. (defconst org-plain-time-extension-regexp
  3196. (concat
  3197. "\\(\\<[012]?[0-9]"
  3198. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3199. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3200. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3201. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3202. groups carry important information:
  3203. 0 the full match
  3204. 7 hours of duration
  3205. 9 minutes of duration")
  3206. (defconst org-stamp-time-of-day-regexp
  3207. (concat
  3208. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3209. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3210. "\\(--?"
  3211. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3212. "Regular expression to match a timestamp time or time range.
  3213. After a match, the following groups carry important information:
  3214. 0 the full match
  3215. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3216. 2 the first time, range or not
  3217. 4 the second time, if it is a range.")
  3218. (defconst org-startup-options
  3219. '(("fold" org-startup-folded t)
  3220. ("overview" org-startup-folded t)
  3221. ("nofold" org-startup-folded nil)
  3222. ("showall" org-startup-folded nil)
  3223. ("showeverything" org-startup-folded showeverything)
  3224. ("content" org-startup-folded content)
  3225. ("indent" org-startup-indented t)
  3226. ("noindent" org-startup-indented nil)
  3227. ("hidestars" org-hide-leading-stars t)
  3228. ("showstars" org-hide-leading-stars nil)
  3229. ("odd" org-odd-levels-only t)
  3230. ("oddeven" org-odd-levels-only nil)
  3231. ("align" org-startup-align-all-tables t)
  3232. ("noalign" org-startup-align-all-tables nil)
  3233. ("customtime" org-display-custom-times t)
  3234. ("logdone" org-log-done time)
  3235. ("lognotedone" org-log-done note)
  3236. ("nologdone" org-log-done nil)
  3237. ("lognoteclock-out" org-log-note-clock-out t)
  3238. ("nolognoteclock-out" org-log-note-clock-out nil)
  3239. ("logrepeat" org-log-repeat state)
  3240. ("lognoterepeat" org-log-repeat note)
  3241. ("nologrepeat" org-log-repeat nil)
  3242. ("logreschedule" org-log-reschedule time)
  3243. ("lognotereschedule" org-log-reschedule note)
  3244. ("nologreschedule" org-log-reschedule nil)
  3245. ("logredeadline" org-log-redeadline time)
  3246. ("lognoteredeadline" org-log-redeadline note)
  3247. ("nologredeadline" org-log-redeadline nil)
  3248. ("fninline" org-footnote-define-inline t)
  3249. ("nofninline" org-footnote-define-inline nil)
  3250. ("fnlocal" org-footnote-section nil)
  3251. ("fnauto" org-footnote-auto-label t)
  3252. ("fnprompt" org-footnote-auto-label nil)
  3253. ("fnconfirm" org-footnote-auto-label confirm)
  3254. ("fnplain" org-footnote-auto-label plain)
  3255. ("fnadjust" org-footnote-auto-adjust t)
  3256. ("nofnadjust" org-footnote-auto-adjust nil)
  3257. ("constcgs" constants-unit-system cgs)
  3258. ("constSI" constants-unit-system SI)
  3259. ("noptag" org-tag-persistent-alist nil)
  3260. ("hideblocks" org-hide-block-startup t)
  3261. ("nohideblocks" org-hide-block-startup nil))
  3262. "Variable associated with STARTUP options for org-mode.
  3263. Each element is a list of three items: The startup options as written
  3264. in the #+STARTUP line, the corresponding variable, and the value to
  3265. set this variable to if the option is found. An optional forth element PUSH
  3266. means to push this value onto the list in the variable.")
  3267. (defun org-set-regexps-and-options ()
  3268. "Precompute regular expressions for current buffer."
  3269. (when (org-mode-p)
  3270. (org-set-local 'org-todo-kwd-alist nil)
  3271. (org-set-local 'org-todo-key-alist nil)
  3272. (org-set-local 'org-todo-key-trigger nil)
  3273. (org-set-local 'org-todo-keywords-1 nil)
  3274. (org-set-local 'org-done-keywords nil)
  3275. (org-set-local 'org-todo-heads nil)
  3276. (org-set-local 'org-todo-sets nil)
  3277. (org-set-local 'org-todo-log-states nil)
  3278. (org-set-local 'org-file-properties nil)
  3279. (org-set-local 'org-file-tags nil)
  3280. (let ((re (org-make-options-regexp
  3281. '("CATEGORY" "TODO" "COLUMNS"
  3282. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3283. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3284. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3285. (splitre "[ \t]+")
  3286. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3287. tail sep kws1 prio props ftags drawers
  3288. ext-setup-or-nil setup-contents (start 0))
  3289. (save-excursion
  3290. (save-restriction
  3291. (widen)
  3292. (goto-char (point-min))
  3293. (while (or (and ext-setup-or-nil
  3294. (string-match re ext-setup-or-nil start)
  3295. (setq start (match-end 0)))
  3296. (and (setq ext-setup-or-nil nil start 0)
  3297. (re-search-forward re nil t)))
  3298. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3299. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3300. (cond
  3301. ((equal key "CATEGORY")
  3302. (if (string-match "[ \t]+$" value)
  3303. (setq value (replace-match "" t t value)))
  3304. (setq cat value))
  3305. ((member key '("SEQ_TODO" "TODO"))
  3306. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3307. ((equal key "TYP_TODO")
  3308. (push (cons 'type (org-split-string value splitre)) kwds))
  3309. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3310. ;; general TODO-like setup
  3311. (push (cons (intern (downcase (match-string 1 key)))
  3312. (org-split-string value splitre)) kwds))
  3313. ((equal key "TAGS")
  3314. (setq tags (append tags (if tags '("\\n") nil)
  3315. (org-split-string value splitre))))
  3316. ((equal key "COLUMNS")
  3317. (org-set-local 'org-columns-default-format value))
  3318. ((equal key "LINK")
  3319. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3320. (push (cons (match-string 1 value)
  3321. (org-trim (match-string 2 value)))
  3322. links)))
  3323. ((equal key "PRIORITIES")
  3324. (setq prio (org-split-string value " +")))
  3325. ((equal key "PROPERTY")
  3326. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3327. (push (cons (match-string 1 value) (match-string 2 value))
  3328. props)))
  3329. ((equal key "FILETAGS")
  3330. (when (string-match "\\S-" value)
  3331. (setq ftags
  3332. (append
  3333. ftags
  3334. (apply 'append
  3335. (mapcar (lambda (x) (org-split-string x ":"))
  3336. (org-split-string value)))))))
  3337. ((equal key "DRAWERS")
  3338. (setq drawers (org-split-string value splitre)))
  3339. ((equal key "CONSTANTS")
  3340. (setq const (append const (org-split-string value splitre))))
  3341. ((equal key "STARTUP")
  3342. (let ((opts (org-split-string value splitre))
  3343. l var val)
  3344. (while (setq l (pop opts))
  3345. (when (setq l (assoc l org-startup-options))
  3346. (setq var (nth 1 l) val (nth 2 l))
  3347. (if (not (nth 3 l))
  3348. (set (make-local-variable var) val)
  3349. (if (not (listp (symbol-value var)))
  3350. (set (make-local-variable var) nil))
  3351. (set (make-local-variable var) (symbol-value var))
  3352. (add-to-list var val))))))
  3353. ((equal key "ARCHIVE")
  3354. (string-match " *$" value)
  3355. (setq arch (replace-match "" t t value))
  3356. (remove-text-properties 0 (length arch)
  3357. '(face t fontified t) arch))
  3358. ((equal key "SETUPFILE")
  3359. (setq setup-contents (org-file-contents
  3360. (expand-file-name
  3361. (org-remove-double-quotes value))
  3362. 'noerror))
  3363. (if (not ext-setup-or-nil)
  3364. (setq ext-setup-or-nil setup-contents start 0)
  3365. (setq ext-setup-or-nil
  3366. (concat (substring ext-setup-or-nil 0 start)
  3367. "\n" setup-contents "\n"
  3368. (substring ext-setup-or-nil start)))))
  3369. ))))
  3370. (when cat
  3371. (org-set-local 'org-category (intern cat))
  3372. (push (cons "CATEGORY" cat) props))
  3373. (when prio
  3374. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3375. (setq prio (mapcar 'string-to-char prio))
  3376. (org-set-local 'org-highest-priority (nth 0 prio))
  3377. (org-set-local 'org-lowest-priority (nth 1 prio))
  3378. (org-set-local 'org-default-priority (nth 2 prio)))
  3379. (and props (org-set-local 'org-file-properties (nreverse props)))
  3380. (and ftags (org-set-local 'org-file-tags
  3381. (mapcar 'org-add-prop-inherited ftags)))
  3382. (and drawers (org-set-local 'org-drawers drawers))
  3383. (and arch (org-set-local 'org-archive-location arch))
  3384. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3385. ;; Process the TODO keywords
  3386. (unless kwds
  3387. ;; Use the global values as if they had been given locally.
  3388. (setq kwds (default-value 'org-todo-keywords))
  3389. (if (stringp (car kwds))
  3390. (setq kwds (list (cons org-todo-interpretation
  3391. (default-value 'org-todo-keywords)))))
  3392. (setq kwds (reverse kwds)))
  3393. (setq kwds (nreverse kwds))
  3394. (let (inter kws kw)
  3395. (while (setq kws (pop kwds))
  3396. (let ((kws (or
  3397. (run-hook-with-args-until-success
  3398. 'org-todo-setup-filter-hook kws)
  3399. kws)))
  3400. (setq inter (pop kws) sep (member "|" kws)
  3401. kws0 (delete "|" (copy-sequence kws))
  3402. kwsa nil
  3403. kws1 (mapcar
  3404. (lambda (x)
  3405. ;; 1 2
  3406. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3407. (progn
  3408. (setq kw (match-string 1 x)
  3409. key (and (match-end 2) (match-string 2 x))
  3410. log (org-extract-log-state-settings x))
  3411. (push (cons kw (and key (string-to-char key))) kwsa)
  3412. (and log (push log org-todo-log-states))
  3413. kw)
  3414. (error "Invalid TODO keyword %s" x)))
  3415. kws0)
  3416. kwsa (if kwsa (append '((:startgroup))
  3417. (nreverse kwsa)
  3418. '((:endgroup))))
  3419. hw (car kws1)
  3420. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3421. tail (list inter hw (car dws) (org-last dws))))
  3422. (add-to-list 'org-todo-heads hw 'append)
  3423. (push kws1 org-todo-sets)
  3424. (setq org-done-keywords (append org-done-keywords dws nil))
  3425. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3426. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3427. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3428. (setq org-todo-sets (nreverse org-todo-sets)
  3429. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3430. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3431. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3432. ;; Process the constants
  3433. (when const
  3434. (let (e cst)
  3435. (while (setq e (pop const))
  3436. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3437. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3438. (setq org-table-formula-constants-local cst)))
  3439. ;; Process the tags.
  3440. (when tags
  3441. (let (e tgs)
  3442. (while (setq e (pop tags))
  3443. (cond
  3444. ((equal e "{") (push '(:startgroup) tgs))
  3445. ((equal e "}") (push '(:endgroup) tgs))
  3446. ((equal e "\\n") (push '(:newline) tgs))
  3447. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3448. (push (cons (match-string 1 e)
  3449. (string-to-char (match-string 2 e)))
  3450. tgs))
  3451. (t (push (list e) tgs))))
  3452. (org-set-local 'org-tag-alist nil)
  3453. (while (setq e (pop tgs))
  3454. (or (and (stringp (car e))
  3455. (assoc (car e) org-tag-alist))
  3456. (push e org-tag-alist)))))
  3457. ;; Compute the regular expressions and other local variables
  3458. (if (not org-done-keywords)
  3459. (setq org-done-keywords (and org-todo-keywords-1
  3460. (list (org-last org-todo-keywords-1)))))
  3461. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3462. (length org-scheduled-string)
  3463. (length org-clock-string)
  3464. (length org-closed-string)))
  3465. org-drawer-regexp
  3466. (concat "^[ \t]*:\\("
  3467. (mapconcat 'regexp-quote org-drawers "\\|")
  3468. "\\):[ \t]*$")
  3469. org-not-done-keywords
  3470. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3471. org-todo-regexp
  3472. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3473. "\\|") "\\)\\>")
  3474. org-not-done-regexp
  3475. (concat "\\<\\("
  3476. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3477. "\\)\\>")
  3478. org-not-done-heading-regexp
  3479. (concat "^\\(\\*+\\)[ \t]+\\("
  3480. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3481. "\\)\\>")
  3482. org-todo-line-regexp
  3483. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3484. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3485. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3486. org-complex-heading-regexp
  3487. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3488. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3489. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3490. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3491. org-complex-heading-regexp-format
  3492. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3493. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3494. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3495. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3496. org-nl-done-regexp
  3497. (concat "\n\\*+[ \t]+"
  3498. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3499. "\\)" "\\>")
  3500. org-todo-line-tags-regexp
  3501. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3502. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3503. (org-re
  3504. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3505. org-looking-at-done-regexp
  3506. (concat "^" "\\(?:"
  3507. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3508. "\\>")
  3509. org-deadline-regexp (concat "\\<" org-deadline-string)
  3510. org-deadline-time-regexp
  3511. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3512. org-deadline-line-regexp
  3513. (concat "\\<\\(" org-deadline-string "\\).*")
  3514. org-scheduled-regexp
  3515. (concat "\\<" org-scheduled-string)
  3516. org-scheduled-time-regexp
  3517. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3518. org-closed-time-regexp
  3519. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3520. org-keyword-time-regexp
  3521. (concat "\\<\\(" org-scheduled-string
  3522. "\\|" org-deadline-string
  3523. "\\|" org-closed-string
  3524. "\\|" org-clock-string "\\)"
  3525. " *[[<]\\([^]>]+\\)[]>]")
  3526. org-keyword-time-not-clock-regexp
  3527. (concat "\\<\\(" org-scheduled-string
  3528. "\\|" org-deadline-string
  3529. "\\|" org-closed-string
  3530. "\\)"
  3531. " *[[<]\\([^]>]+\\)[]>]")
  3532. org-maybe-keyword-time-regexp
  3533. (concat "\\(\\<\\(" org-scheduled-string
  3534. "\\|" org-deadline-string
  3535. "\\|" org-closed-string
  3536. "\\|" org-clock-string "\\)\\)?"
  3537. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3538. org-planning-or-clock-line-re
  3539. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3540. "\\|" org-deadline-string
  3541. "\\|" org-closed-string "\\|" org-clock-string
  3542. "\\)\\>\\)")
  3543. )
  3544. (org-compute-latex-and-specials-regexp)
  3545. (org-set-font-lock-defaults))))
  3546. (defun org-file-contents (file &optional noerror)
  3547. "Return the contents of FILE, as a string."
  3548. (if (or (not file)
  3549. (not (file-readable-p file)))
  3550. (if noerror
  3551. (progn
  3552. (message "Cannot read file %s" file)
  3553. (ding) (sit-for 2)
  3554. "")
  3555. (error "Cannot read file %s" file))
  3556. (with-temp-buffer
  3557. (insert-file-contents file)
  3558. (buffer-string))))
  3559. (defun org-extract-log-state-settings (x)
  3560. "Extract the log state setting from a TODO keyword string.
  3561. This will extract info from a string like \"WAIT(w@/!)\"."
  3562. (let (kw key log1 log2)
  3563. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3564. (setq kw (match-string 1 x)
  3565. key (and (match-end 2) (match-string 2 x))
  3566. log1 (and (match-end 3) (match-string 3 x))
  3567. log2 (and (match-end 4) (match-string 4 x)))
  3568. (and (or log1 log2)
  3569. (list kw
  3570. (and log1 (if (equal log1 "!") 'time 'note))
  3571. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3572. (defun org-remove-keyword-keys (list)
  3573. "Remove a pair of parenthesis at the end of each string in LIST."
  3574. (mapcar (lambda (x)
  3575. (if (string-match "(.*)$" x)
  3576. (substring x 0 (match-beginning 0))
  3577. x))
  3578. list))
  3579. ;; FIXME: this could be done much better, using second characters etc.
  3580. (defun org-assign-fast-keys (alist)
  3581. "Assign fast keys to a keyword-key alist.
  3582. Respect keys that are already there."
  3583. (let (new e k c c1 c2 (char ?a))
  3584. (while (setq e (pop alist))
  3585. (cond
  3586. ((equal e '(:startgroup)) (push e new))
  3587. ((equal e '(:endgroup)) (push e new))
  3588. ((equal e '(:newline)) (push e new))
  3589. (t
  3590. (setq k (car e) c2 nil)
  3591. (if (cdr e)
  3592. (setq c (cdr e))
  3593. ;; automatically assign a character.
  3594. (setq c1 (string-to-char
  3595. (downcase (substring
  3596. k (if (= (string-to-char k) ?@) 1 0)))))
  3597. (if (or (rassoc c1 new) (rassoc c1 alist))
  3598. (while (or (rassoc char new) (rassoc char alist))
  3599. (setq char (1+ char)))
  3600. (setq c2 c1))
  3601. (setq c (or c2 char)))
  3602. (push (cons k c) new))))
  3603. (nreverse new)))
  3604. ;;; Some variables used in various places
  3605. (defvar org-window-configuration nil
  3606. "Used in various places to store a window configuration.")
  3607. (defvar org-selected-window nil
  3608. "Used in various places to store a window configuration.")
  3609. (defvar org-finish-function nil
  3610. "Function to be called when `C-c C-c' is used.
  3611. This is for getting out of special buffers like remember.")
  3612. ;; FIXME: Occasionally check by commenting these, to make sure
  3613. ;; no other functions uses these, forgetting to let-bind them.
  3614. (defvar entry)
  3615. (defvar last-state)
  3616. (defvar date)
  3617. ;; Defined somewhere in this file, but used before definition.
  3618. (defvar org-html-entities)
  3619. (defvar org-struct-menu)
  3620. (defvar org-org-menu)
  3621. (defvar org-tbl-menu)
  3622. ;;;; Define the Org-mode
  3623. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3624. (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."))
  3625. ;; We use a before-change function to check if a table might need
  3626. ;; an update.
  3627. (defvar org-table-may-need-update t
  3628. "Indicates that a table might need an update.
  3629. This variable is set by `org-before-change-function'.
  3630. `org-table-align' sets it back to nil.")
  3631. (defun org-before-change-function (beg end)
  3632. "Every change indicates that a table might need an update."
  3633. (setq org-table-may-need-update t))
  3634. (defvar org-mode-map)
  3635. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3636. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3637. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3638. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3639. (defvar org-table-buffer-is-an nil)
  3640. (defconst org-outline-regexp "\\*+ ")
  3641. ;;;###autoload
  3642. (define-derived-mode org-mode outline-mode "Org"
  3643. "Outline-based notes management and organizer, alias
  3644. \"Carsten's outline-mode for keeping track of everything.\"
  3645. Org-mode develops organizational tasks around a NOTES file which
  3646. contains information about projects as plain text. Org-mode is
  3647. implemented on top of outline-mode, which is ideal to keep the content
  3648. of large files well structured. It supports ToDo items, deadlines and
  3649. time stamps, which magically appear in the diary listing of the Emacs
  3650. calendar. Tables are easily created with a built-in table editor.
  3651. Plain text URL-like links connect to websites, emails (VM), Usenet
  3652. messages (Gnus), BBDB entries, and any files related to the project.
  3653. For printing and sharing of notes, an Org-mode file (or a part of it)
  3654. can be exported as a structured ASCII or HTML file.
  3655. The following commands are available:
  3656. \\{org-mode-map}"
  3657. ;; Get rid of Outline menus, they are not needed
  3658. ;; Need to do this here because define-derived-mode sets up
  3659. ;; the keymap so late. Still, it is a waste to call this each time
  3660. ;; we switch another buffer into org-mode.
  3661. (if (featurep 'xemacs)
  3662. (when (boundp 'outline-mode-menu-heading)
  3663. ;; Assume this is Greg's port, it used easymenu
  3664. (easy-menu-remove outline-mode-menu-heading)
  3665. (easy-menu-remove outline-mode-menu-show)
  3666. (easy-menu-remove outline-mode-menu-hide))
  3667. (define-key org-mode-map [menu-bar headings] 'undefined)
  3668. (define-key org-mode-map [menu-bar hide] 'undefined)
  3669. (define-key org-mode-map [menu-bar show] 'undefined))
  3670. (org-load-modules-maybe)
  3671. (easy-menu-add org-org-menu)
  3672. (easy-menu-add org-tbl-menu)
  3673. (org-install-agenda-files-menu)
  3674. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3675. (org-add-to-invisibility-spec '(org-cwidth))
  3676. (org-add-to-invisibility-spec '(org-hide-block . t))
  3677. (when (featurep 'xemacs)
  3678. (org-set-local 'line-move-ignore-invisible t))
  3679. (org-set-local 'outline-regexp org-outline-regexp)
  3680. (org-set-local 'outline-level 'org-outline-level)
  3681. (when (and org-ellipsis
  3682. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3683. (fboundp 'make-glyph-code))
  3684. (unless org-display-table
  3685. (setq org-display-table (make-display-table)))
  3686. (set-display-table-slot
  3687. org-display-table 4
  3688. (vconcat (mapcar
  3689. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3690. org-ellipsis)))
  3691. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3692. (setq buffer-display-table org-display-table))
  3693. (org-set-regexps-and-options)
  3694. (when (and org-tag-faces (not org-tags-special-faces-re))
  3695. ;; tag faces set outside customize.... force initialization.
  3696. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3697. ;; Calc embedded
  3698. (org-set-local 'calc-embedded-open-mode "# ")
  3699. (modify-syntax-entry ?# "<")
  3700. (modify-syntax-entry ?@ "w")
  3701. (if org-startup-truncated (setq truncate-lines t))
  3702. (org-set-local 'font-lock-unfontify-region-function
  3703. 'org-unfontify-region)
  3704. ;; Activate before-change-function
  3705. (org-set-local 'org-table-may-need-update t)
  3706. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3707. 'local)
  3708. ;; Check for running clock before killing a buffer
  3709. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3710. ;; Paragraphs and auto-filling
  3711. (org-set-autofill-regexps)
  3712. (setq indent-line-function 'org-indent-line-function)
  3713. (org-update-radio-target-regexp)
  3714. ;; Make sure dependence stuff works reliably, even for users who set it
  3715. ;; too late :-(
  3716. (if org-enforce-todo-dependencies
  3717. (add-hook 'org-blocker-hook
  3718. 'org-block-todo-from-children-or-siblings-or-parent)
  3719. (remove-hook 'org-blocker-hook
  3720. 'org-block-todo-from-children-or-siblings-or-parent))
  3721. (if org-enforce-todo-checkbox-dependencies
  3722. (add-hook 'org-blocker-hook
  3723. 'org-block-todo-from-checkboxes)
  3724. (remove-hook 'org-blocker-hook
  3725. 'org-block-todo-from-checkboxes))
  3726. ;; Comment characters
  3727. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3728. (org-set-local 'comment-padding " ")
  3729. ;; Align options lines
  3730. (org-set-local
  3731. 'align-mode-rules-list
  3732. '((org-in-buffer-settings
  3733. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3734. (modes . '(org-mode)))))
  3735. ;; Imenu
  3736. (org-set-local 'imenu-create-index-function
  3737. 'org-imenu-get-tree)
  3738. ;; Make isearch reveal context
  3739. (if (or (featurep 'xemacs)
  3740. (not (boundp 'outline-isearch-open-invisible-function)))
  3741. ;; Emacs 21 and XEmacs make use of the hook
  3742. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3743. ;; Emacs 22 deals with this through a special variable
  3744. (org-set-local 'outline-isearch-open-invisible-function
  3745. (lambda (&rest ignore) (org-show-context 'isearch))))
  3746. ;; If empty file that did not turn on org-mode automatically, make it to.
  3747. (if (and org-insert-mode-line-in-empty-file
  3748. (interactive-p)
  3749. (= (point-min) (point-max)))
  3750. (insert "# -*- mode: org -*-\n\n"))
  3751. (unless org-inhibit-startup
  3752. (when org-startup-align-all-tables
  3753. (let ((bmp (buffer-modified-p)))
  3754. (org-table-map-tables 'org-table-align)
  3755. (set-buffer-modified-p bmp)))
  3756. (when org-startup-indented
  3757. (require 'org-indent)
  3758. (org-indent-mode 1))
  3759. (org-set-startup-visibility)))
  3760. (when (fboundp 'abbrev-table-put)
  3761. (abbrev-table-put org-mode-abbrev-table
  3762. :parents (list text-mode-abbrev-table)))
  3763. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3764. (defun org-current-time ()
  3765. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3766. (if (> (car org-time-stamp-rounding-minutes) 1)
  3767. (let ((r (car org-time-stamp-rounding-minutes))
  3768. (time (decode-time)))
  3769. (apply 'encode-time
  3770. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3771. (nthcdr 2 time))))
  3772. (current-time)))
  3773. ;;;; Font-Lock stuff, including the activators
  3774. (defvar org-mouse-map (make-sparse-keymap))
  3775. (org-defkey org-mouse-map
  3776. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3777. (org-defkey org-mouse-map
  3778. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3779. (when org-mouse-1-follows-link
  3780. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3781. (when org-tab-follows-link
  3782. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3783. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3784. (require 'font-lock)
  3785. (defconst org-non-link-chars "]\t\n\r<>")
  3786. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3787. "shell" "elisp"))
  3788. (defvar org-link-types-re nil
  3789. "Matches a link that has a url-like prefix like \"http:\"")
  3790. (defvar org-link-re-with-space nil
  3791. "Matches a link with spaces, optional angular brackets around it.")
  3792. (defvar org-link-re-with-space2 nil
  3793. "Matches a link with spaces, optional angular brackets around it.")
  3794. (defvar org-link-re-with-space3 nil
  3795. "Matches a link with spaces, only for internal part in bracket links.")
  3796. (defvar org-angle-link-re nil
  3797. "Matches link with angular brackets, spaces are allowed.")
  3798. (defvar org-plain-link-re nil
  3799. "Matches plain link, without spaces.")
  3800. (defvar org-bracket-link-regexp nil
  3801. "Matches a link in double brackets.")
  3802. (defvar org-bracket-link-analytic-regexp nil
  3803. "Regular expression used to analyze links.
  3804. Here is what the match groups contain after a match:
  3805. 1: http:
  3806. 2: http
  3807. 3: path
  3808. 4: [desc]
  3809. 5: desc")
  3810. (defvar org-bracket-link-analytic-regexp++ nil
  3811. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3812. (defvar org-any-link-re nil
  3813. "Regular expression matching any link.")
  3814. (defun org-make-link-regexps ()
  3815. "Update the link regular expressions.
  3816. This should be called after the variable `org-link-types' has changed."
  3817. (setq org-link-types-re
  3818. (concat
  3819. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3820. org-link-re-with-space
  3821. (concat
  3822. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3823. "\\([^" org-non-link-chars " ]"
  3824. "[^" org-non-link-chars "]*"
  3825. "[^" org-non-link-chars " ]\\)>?")
  3826. org-link-re-with-space2
  3827. (concat
  3828. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3829. "\\([^" org-non-link-chars " ]"
  3830. "[^\t\n\r]*"
  3831. "[^" org-non-link-chars " ]\\)>?")
  3832. org-link-re-with-space3
  3833. (concat
  3834. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3835. "\\([^" org-non-link-chars " ]"
  3836. "[^\t\n\r]*\\)")
  3837. org-angle-link-re
  3838. (concat
  3839. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3840. "\\([^" org-non-link-chars " ]"
  3841. "[^" org-non-link-chars "]*"
  3842. "\\)>")
  3843. org-plain-link-re
  3844. (concat
  3845. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3846. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3847. org-bracket-link-regexp
  3848. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3849. org-bracket-link-analytic-regexp
  3850. (concat
  3851. "\\[\\["
  3852. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3853. "\\([^]]+\\)"
  3854. "\\]"
  3855. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3856. "\\]")
  3857. org-bracket-link-analytic-regexp++
  3858. (concat
  3859. "\\[\\["
  3860. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3861. "\\([^]]+\\)"
  3862. "\\]"
  3863. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3864. "\\]")
  3865. org-any-link-re
  3866. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3867. org-angle-link-re "\\)\\|\\("
  3868. org-plain-link-re "\\)")))
  3869. (org-make-link-regexps)
  3870. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3871. "Regular expression for fast time stamp matching.")
  3872. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3873. "Regular expression for fast time stamp matching.")
  3874. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3875. "Regular expression matching time strings for analysis.
  3876. This one does not require the space after the date, so it can be used
  3877. on a string that terminates immediately after the date.")
  3878. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3879. "Regular expression matching time strings for analysis.")
  3880. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3881. "Regular expression matching time stamps, with groups.")
  3882. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3883. "Regular expression matching time stamps (also [..]), with groups.")
  3884. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3885. "Regular expression matching a time stamp range.")
  3886. (defconst org-tr-regexp-both
  3887. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3888. "Regular expression matching a time stamp range.")
  3889. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3890. org-ts-regexp "\\)?")
  3891. "Regular expression matching a time stamp or time stamp range.")
  3892. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3893. org-ts-regexp-both "\\)?")
  3894. "Regular expression matching a time stamp or time stamp range.
  3895. The time stamps may be either active or inactive.")
  3896. (defvar org-emph-face nil)
  3897. (defun org-do-emphasis-faces (limit)
  3898. "Run through the buffer and add overlays to links."
  3899. (let (rtn a)
  3900. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3901. (if (not (= (char-after (match-beginning 3))
  3902. (char-after (match-beginning 4))))
  3903. (progn
  3904. (setq rtn t)
  3905. (setq a (assoc (match-string 3) org-emphasis-alist))
  3906. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3907. 'face
  3908. (nth 1 a))
  3909. (and (nth 4 a)
  3910. (org-remove-flyspell-overlays-in
  3911. (match-beginning 0) (match-end 0)))
  3912. (add-text-properties (match-beginning 2) (match-end 2)
  3913. '(font-lock-multiline t))
  3914. (when org-hide-emphasis-markers
  3915. (add-text-properties (match-end 4) (match-beginning 5)
  3916. '(invisible org-link))
  3917. (add-text-properties (match-beginning 3) (match-end 3)
  3918. '(invisible org-link)))))
  3919. (backward-char 1))
  3920. rtn))
  3921. (defun org-emphasize (&optional char)
  3922. "Insert or change an emphasis, i.e. a font like bold or italic.
  3923. If there is an active region, change that region to a new emphasis.
  3924. If there is no region, just insert the marker characters and position
  3925. the cursor between them.
  3926. CHAR should be either the marker character, or the first character of the
  3927. HTML tag associated with that emphasis. If CHAR is a space, the means
  3928. to remove the emphasis of the selected region.
  3929. If char is not given (for example in an interactive call) it
  3930. will be prompted for."
  3931. (interactive)
  3932. (let ((eal org-emphasis-alist) e det
  3933. (erc org-emphasis-regexp-components)
  3934. (prompt "")
  3935. (string "") beg end move tag c s)
  3936. (if (org-region-active-p)
  3937. (setq beg (region-beginning) end (region-end)
  3938. string (buffer-substring beg end))
  3939. (setq move t))
  3940. (while (setq e (pop eal))
  3941. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3942. c (aref tag 0))
  3943. (push (cons c (string-to-char (car e))) det)
  3944. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3945. (substring tag 1)))))
  3946. (setq det (nreverse det))
  3947. (unless char
  3948. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3949. (setq char (read-char-exclusive)))
  3950. (setq char (or (cdr (assoc char det)) char))
  3951. (if (equal char ?\ )
  3952. (setq s "" move nil)
  3953. (unless (assoc (char-to-string char) org-emphasis-alist)
  3954. (error "No such emphasis marker: \"%c\"" char))
  3955. (setq s (char-to-string char)))
  3956. (while (and (> (length string) 1)
  3957. (equal (substring string 0 1) (substring string -1))
  3958. (assoc (substring string 0 1) org-emphasis-alist))
  3959. (setq string (substring string 1 -1)))
  3960. (setq string (concat s string s))
  3961. (if beg (delete-region beg end))
  3962. (unless (or (bolp)
  3963. (string-match (concat "[" (nth 0 erc) "\n]")
  3964. (char-to-string (char-before (point)))))
  3965. (insert " "))
  3966. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3967. (char-to-string (char-after (point))))
  3968. (insert " ") (backward-char 1))
  3969. (insert string)
  3970. (and move (backward-char 1))))
  3971. (defconst org-nonsticky-props
  3972. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3973. (defsubst org-rear-nonsticky-at (pos)
  3974. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3975. (defun org-activate-plain-links (limit)
  3976. "Run through the buffer and add overlays to links."
  3977. (catch 'exit
  3978. (let (f)
  3979. (if (re-search-forward org-plain-link-re limit t)
  3980. (progn
  3981. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3982. (setq f (get-text-property (match-beginning 0) 'face))
  3983. (if (or (eq f 'org-tag)
  3984. (and (listp f) (memq 'org-tag f)))
  3985. nil
  3986. (add-text-properties (match-beginning 0) (match-end 0)
  3987. (list 'mouse-face 'highlight
  3988. 'keymap org-mouse-map))
  3989. (org-rear-nonsticky-at (match-end 0)))
  3990. t)))))
  3991. (defun org-activate-code (limit)
  3992. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3993. (progn
  3994. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3995. (remove-text-properties (match-beginning 0) (match-end 0)
  3996. '(display t invisible t intangible t))
  3997. t)))
  3998. (defun org-fontify-meta-lines-and-blocks (limit)
  3999. "Fontify #+ lines and blocks, in the correct ways."
  4000. (let ((case-fold-search t))
  4001. (if (re-search-forward
  4002. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4003. limit t)
  4004. (let ((beg (match-beginning 0))
  4005. (beg1 (line-beginning-position 2))
  4006. (dc1 (downcase (match-string 2)))
  4007. (dc3 (downcase (match-string 3)))
  4008. end end1 quoting)
  4009. (cond
  4010. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4011. ;; a single line of backend-specific content
  4012. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4013. (remove-text-properties (match-beginning 0) (match-end 0)
  4014. '(display t invisible t intangible t))
  4015. (add-text-properties (match-beginning 1) (match-end 3)
  4016. '(font-lock-fontified t face org-meta-line))
  4017. (add-text-properties (match-beginning 6) (match-end 6)
  4018. '(font-lock-fontified t face org-block))
  4019. t)
  4020. ((and (match-end 4) (equal dc3 "begin"))
  4021. ;; Truely a block
  4022. (setq quoting (member (downcase (match-string 5))
  4023. org-protecting-blocks))
  4024. (when (re-search-forward
  4025. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4026. nil t) ;; on purpose, we look further than LIMIT
  4027. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4028. (when quoting
  4029. (remove-text-properties beg end
  4030. '(display t invisible t intangible t)))
  4031. (add-text-properties
  4032. beg end
  4033. '(font-lock-fontified t font-lock-multiline t))
  4034. (add-text-properties beg beg1 '(face org-meta-line))
  4035. (add-text-properties end1 end '(face org-meta-line))
  4036. (when quoting
  4037. (add-text-properties beg1 end1 '(face org-block)))
  4038. t))
  4039. ((not (member (char-after beg) '(?\ ?\t)))
  4040. ;; just any other in-buffer setting, but not indented
  4041. (add-text-properties
  4042. beg (match-end 0)
  4043. '(font-lock-fontified t face org-meta-line))
  4044. t)
  4045. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4046. "orgtbl:" "tblfm:" "tblname:"))
  4047. (and (match-end 4) (equal dc3 "attr")))
  4048. (add-text-properties
  4049. beg (match-end 0)
  4050. '(font-lock-fontified t face org-meta-line))
  4051. t)
  4052. ((member dc3 '(" " ""))
  4053. (add-text-properties
  4054. beg (match-end 0)
  4055. '(font-lock-fontified t face font-lock-comment-face)))
  4056. (t nil))))))
  4057. (defun org-activate-angle-links (limit)
  4058. "Run through the buffer and add overlays to links."
  4059. (if (re-search-forward org-angle-link-re limit t)
  4060. (progn
  4061. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4062. (add-text-properties (match-beginning 0) (match-end 0)
  4063. (list 'mouse-face 'highlight
  4064. 'keymap org-mouse-map))
  4065. (org-rear-nonsticky-at (match-end 0))
  4066. t)))
  4067. (defun org-activate-footnote-links (limit)
  4068. "Run through the buffer and add overlays to links."
  4069. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4070. limit t)
  4071. (progn
  4072. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4073. (add-text-properties (match-beginning 2) (match-end 2)
  4074. (list 'mouse-face 'highlight
  4075. 'keymap org-mouse-map
  4076. 'help-echo
  4077. (if (= (point-at-bol) (match-beginning 2))
  4078. "Footnote definition"
  4079. "Footnote reference")
  4080. ))
  4081. (org-rear-nonsticky-at (match-end 2))
  4082. t)))
  4083. (defun org-activate-bracket-links (limit)
  4084. "Run through the buffer and add overlays to bracketed links."
  4085. (if (re-search-forward org-bracket-link-regexp limit t)
  4086. (let* ((help (concat "LINK: "
  4087. (org-match-string-no-properties 1)))
  4088. ;; FIXME: above we should remove the escapes.
  4089. ;; but that requires another match, protecting match data,
  4090. ;; a lot of overhead for font-lock.
  4091. (ip (org-maybe-intangible
  4092. (list 'invisible 'org-link
  4093. 'keymap org-mouse-map 'mouse-face 'highlight
  4094. 'font-lock-multiline t 'help-echo help)))
  4095. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4096. 'font-lock-multiline t 'help-echo help)))
  4097. ;; We need to remove the invisible property here. Table narrowing
  4098. ;; may have made some of this invisible.
  4099. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4100. (remove-text-properties (match-beginning 0) (match-end 0)
  4101. '(invisible nil))
  4102. (if (match-end 3)
  4103. (progn
  4104. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4105. (org-rear-nonsticky-at (match-beginning 3))
  4106. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4107. (org-rear-nonsticky-at (match-end 3))
  4108. (add-text-properties (match-end 3) (match-end 0) ip)
  4109. (org-rear-nonsticky-at (match-end 0)))
  4110. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4111. (org-rear-nonsticky-at (match-beginning 1))
  4112. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4113. (org-rear-nonsticky-at (match-end 1))
  4114. (add-text-properties (match-end 1) (match-end 0) ip)
  4115. (org-rear-nonsticky-at (match-end 0)))
  4116. t)))
  4117. (defun org-activate-dates (limit)
  4118. "Run through the buffer and add overlays to dates."
  4119. (if (re-search-forward org-tsr-regexp-both limit t)
  4120. (progn
  4121. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4122. (add-text-properties (match-beginning 0) (match-end 0)
  4123. (list 'mouse-face 'highlight
  4124. 'keymap org-mouse-map))
  4125. (org-rear-nonsticky-at (match-end 0))
  4126. (when org-display-custom-times
  4127. (if (match-end 3)
  4128. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4129. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4130. t)))
  4131. (defvar org-target-link-regexp nil
  4132. "Regular expression matching radio targets in plain text.")
  4133. (make-variable-buffer-local 'org-target-link-regexp)
  4134. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4135. "Regular expression matching a link target.")
  4136. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4137. "Regular expression matching a radio target.")
  4138. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4139. "Regular expression matching any target.")
  4140. (defun org-activate-target-links (limit)
  4141. "Run through the buffer and add overlays to target matches."
  4142. (when org-target-link-regexp
  4143. (let ((case-fold-search t))
  4144. (if (re-search-forward org-target-link-regexp limit t)
  4145. (progn
  4146. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4147. (add-text-properties (match-beginning 0) (match-end 0)
  4148. (list 'mouse-face 'highlight
  4149. 'keymap org-mouse-map
  4150. 'help-echo "Radio target link"
  4151. 'org-linked-text t))
  4152. (org-rear-nonsticky-at (match-end 0))
  4153. t)))))
  4154. (defun org-update-radio-target-regexp ()
  4155. "Find all radio targets in this file and update the regular expression."
  4156. (interactive)
  4157. (when (memq 'radio org-activate-links)
  4158. (setq org-target-link-regexp
  4159. (org-make-target-link-regexp (org-all-targets 'radio)))
  4160. (org-restart-font-lock)))
  4161. (defun org-hide-wide-columns (limit)
  4162. (let (s e)
  4163. (setq s (text-property-any (point) (or limit (point-max))
  4164. 'org-cwidth t))
  4165. (when s
  4166. (setq e (next-single-property-change s 'org-cwidth))
  4167. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4168. (goto-char e)
  4169. t)))
  4170. (defvar org-latex-and-specials-regexp nil
  4171. "Regular expression for highlighting export special stuff.")
  4172. (defvar org-match-substring-regexp)
  4173. (defvar org-match-substring-with-braces-regexp)
  4174. ;; This should be with the exporter code, but we also use if for font-locking
  4175. (defconst org-export-html-special-string-regexps
  4176. '(("\\\\-" . "&shy;")
  4177. ("---\\([^-]\\)" . "&mdash;\\1")
  4178. ("--\\([^-]\\)" . "&ndash;\\1")
  4179. ("\\.\\.\\." . "&hellip;"))
  4180. "Regular expressions for special string conversion.")
  4181. (defun org-compute-latex-and-specials-regexp ()
  4182. "Compute regular expression for stuff treated specially by exporters."
  4183. (if (not org-highlight-latex-fragments-and-specials)
  4184. (org-set-local 'org-latex-and-specials-regexp nil)
  4185. (require 'org-exp)
  4186. (let*
  4187. ((matchers (plist-get org-format-latex-options :matchers))
  4188. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4189. org-latex-regexps)))
  4190. (options (org-combine-plists (org-default-export-plist)
  4191. (org-infile-export-plist)))
  4192. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4193. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4194. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4195. (org-export-html-expand (plist-get options :expand-quoted-html))
  4196. (org-export-with-special-strings (plist-get options :special-strings))
  4197. (re-sub
  4198. (cond
  4199. ((equal org-export-with-sub-superscripts '{})
  4200. (list org-match-substring-with-braces-regexp))
  4201. (org-export-with-sub-superscripts
  4202. (list org-match-substring-regexp))
  4203. (t nil)))
  4204. (re-latex
  4205. (if org-export-with-LaTeX-fragments
  4206. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4207. (re-macros
  4208. (if org-export-with-TeX-macros
  4209. (list (concat "\\\\"
  4210. (regexp-opt
  4211. (append (mapcar 'car org-html-entities)
  4212. (if (boundp 'org-latex-entities)
  4213. (mapcar (lambda (x)
  4214. (or (car-safe x) x))
  4215. org-latex-entities)
  4216. nil))
  4217. 'words))) ; FIXME
  4218. ))
  4219. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4220. (re-special (if org-export-with-special-strings
  4221. (mapcar (lambda (x) (car x))
  4222. org-export-html-special-string-regexps)))
  4223. (re-rest
  4224. (delq nil
  4225. (list
  4226. (if org-export-html-expand "@<[^>\n]+>")
  4227. ))))
  4228. (org-set-local
  4229. 'org-latex-and-specials-regexp
  4230. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4231. re-rest) "\\|")))))
  4232. (defun org-do-latex-and-special-faces (limit)
  4233. "Run through the buffer and add overlays to links."
  4234. (when org-latex-and-specials-regexp
  4235. (let (rtn d)
  4236. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4237. limit t))
  4238. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4239. 'face))
  4240. '(org-code org-verbatim underline)))
  4241. (progn
  4242. (setq rtn t
  4243. d (cond ((member (char-after (1+ (match-beginning 0)))
  4244. '(?_ ?^)) 1)
  4245. (t 0)))
  4246. (font-lock-prepend-text-property
  4247. (+ d (match-beginning 0)) (match-end 0)
  4248. 'face 'org-latex-and-export-specials)
  4249. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4250. '(font-lock-multiline t)))))
  4251. rtn)))
  4252. (defun org-restart-font-lock ()
  4253. "Restart font-lock-mode, to force refontification."
  4254. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4255. (font-lock-mode -1)
  4256. (font-lock-mode 1)))
  4257. (defun org-all-targets (&optional radio)
  4258. "Return a list of all targets in this file.
  4259. With optional argument RADIO, only find radio targets."
  4260. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4261. rtn)
  4262. (save-excursion
  4263. (goto-char (point-min))
  4264. (while (re-search-forward re nil t)
  4265. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4266. rtn)))
  4267. (defun org-make-target-link-regexp (targets)
  4268. "Make regular expression matching all strings in TARGETS.
  4269. The regular expression finds the targets also if there is a line break
  4270. between words."
  4271. (and targets
  4272. (concat
  4273. "\\<\\("
  4274. (mapconcat
  4275. (lambda (x)
  4276. (while (string-match " +" x)
  4277. (setq x (replace-match "\\s-+" t t x)))
  4278. x)
  4279. targets
  4280. "\\|")
  4281. "\\)\\>")))
  4282. (defun org-activate-tags (limit)
  4283. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4284. (progn
  4285. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4286. (add-text-properties (match-beginning 1) (match-end 1)
  4287. (list 'mouse-face 'highlight
  4288. 'keymap org-mouse-map))
  4289. (org-rear-nonsticky-at (match-end 1))
  4290. t)))
  4291. (defun org-outline-level ()
  4292. "Compute the outline level of the heading at point.
  4293. This function assumes that the cursor is at the beginning of a line matched
  4294. by outline-regexp. Otherwise it returns garbage.
  4295. If this is called at a normal headline, the level is the number of stars.
  4296. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4297. For plain list items, if they are matched by `outline-regexp', this returns
  4298. 1000 plus the line indentation."
  4299. (save-excursion
  4300. (looking-at outline-regexp)
  4301. (if (match-beginning 1)
  4302. (+ (org-get-string-indentation (match-string 1)) 1000)
  4303. (1- (- (match-end 0) (match-beginning 0))))))
  4304. (defvar org-font-lock-keywords nil)
  4305. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4306. "Regular expression matching a property line.")
  4307. (defvar org-font-lock-hook nil
  4308. "Functions to be called for special font lock stuff.")
  4309. (defun org-font-lock-hook (limit)
  4310. (run-hook-with-args 'org-font-lock-hook limit))
  4311. (defun org-set-font-lock-defaults ()
  4312. (let* ((em org-fontify-emphasized-text)
  4313. (lk org-activate-links)
  4314. (org-font-lock-extra-keywords
  4315. (list
  4316. ;; Call the hook
  4317. '(org-font-lock-hook)
  4318. ;; Headlines
  4319. `(,(if org-fontify-whole-heading-line
  4320. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4321. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4322. (1 (org-get-level-face 1))
  4323. (2 (org-get-level-face 2))
  4324. (3 (org-get-level-face 3)))
  4325. ;; Table lines
  4326. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4327. (1 'org-table t))
  4328. ;; Table internals
  4329. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4330. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4331. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4332. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4333. ;; Drawers
  4334. (list org-drawer-regexp '(0 'org-special-keyword t))
  4335. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4336. ;; Properties
  4337. (list org-property-re
  4338. '(1 'org-special-keyword t)
  4339. '(3 'org-property-value t))
  4340. ;; Links
  4341. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4342. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4343. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4344. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4345. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4346. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4347. (if (memq 'footnote lk) '(org-activate-footnote-links
  4348. (2 'org-footnote t)))
  4349. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4350. '(org-hide-wide-columns (0 nil append))
  4351. ;; TODO lines
  4352. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4353. '(1 (org-get-todo-face 1) t))
  4354. ;; DONE
  4355. (if org-fontify-done-headline
  4356. (list (concat "^[*]+ +\\<\\("
  4357. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4358. "\\)\\(.*\\)")
  4359. '(2 'org-headline-done t))
  4360. nil)
  4361. ;; Priorities
  4362. '(org-font-lock-add-priority-faces)
  4363. ;; Tags
  4364. '(org-font-lock-add-tag-faces)
  4365. ;; Special keywords
  4366. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4367. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4368. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4369. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4370. ;; Emphasis
  4371. (if em
  4372. (if (featurep 'xemacs)
  4373. '(org-do-emphasis-faces (0 nil append))
  4374. '(org-do-emphasis-faces)))
  4375. ;; Checkboxes
  4376. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4377. 2 'org-checkbox prepend)
  4378. (if org-provide-checkbox-statistics
  4379. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4380. (0 (org-get-checkbox-statistics-face) t)))
  4381. ;; Description list items
  4382. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4383. 2 'bold prepend)
  4384. ;; ARCHIVEd headings
  4385. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4386. '(1 'org-archived prepend))
  4387. ;; Specials
  4388. '(org-do-latex-and-special-faces)
  4389. ;; Code
  4390. '(org-activate-code (1 'org-code t))
  4391. ;; COMMENT
  4392. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4393. "\\|" org-quote-string "\\)\\>")
  4394. '(1 'org-special-keyword t))
  4395. '("^#.*" (0 'font-lock-comment-face t))
  4396. ;; Blocks and meta lines
  4397. '(org-fontify-meta-lines-and-blocks)
  4398. )))
  4399. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4400. ;; Now set the full font-lock-keywords
  4401. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4402. (org-set-local 'font-lock-defaults
  4403. '(org-font-lock-keywords t nil nil backward-paragraph))
  4404. (kill-local-variable 'font-lock-keywords) nil))
  4405. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4406. "Fontify string S like in Org-mode"
  4407. (with-temp-buffer
  4408. (insert s)
  4409. (let ((org-odd-levels-only odd-levels))
  4410. (org-mode)
  4411. (font-lock-fontify-buffer)
  4412. (buffer-string))))
  4413. (defvar org-m nil)
  4414. (defvar org-l nil)
  4415. (defvar org-f nil)
  4416. (defun org-get-level-face (n)
  4417. "Get the right face for match N in font-lock matching of headlines."
  4418. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4419. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4420. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4421. (cond
  4422. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4423. ((eq n 2) org-f)
  4424. (t (if org-level-color-stars-only nil org-f))))
  4425. (defun org-get-todo-face (kwd)
  4426. "Get the right face for a TODO keyword KWD.
  4427. If KWD is a number, get the corresponding match group."
  4428. (if (numberp kwd) (setq kwd (match-string kwd)))
  4429. (or (cdr (assoc kwd org-todo-keyword-faces))
  4430. (and (member kwd org-done-keywords) 'org-done)
  4431. 'org-todo))
  4432. (defun org-font-lock-add-tag-faces (limit)
  4433. "Add the special tag faces."
  4434. (when (and org-tag-faces org-tags-special-faces-re)
  4435. (while (re-search-forward org-tags-special-faces-re limit t)
  4436. (add-text-properties (match-beginning 1) (match-end 1)
  4437. (list 'face (org-get-tag-face 1)
  4438. 'font-lock-fontified t))
  4439. (backward-char 1))))
  4440. (defun org-font-lock-add-priority-faces (limit)
  4441. "Add the special priority faces."
  4442. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4443. (add-text-properties
  4444. (match-beginning 0) (match-end 0)
  4445. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4446. org-priority-faces))
  4447. 'org-special-keyword)
  4448. 'font-lock-fontified t))))
  4449. (defun org-get-tag-face (kwd)
  4450. "Get the right face for a TODO keyword KWD.
  4451. If KWD is a number, get the corresponding match group."
  4452. (if (numberp kwd) (setq kwd (match-string kwd)))
  4453. (or (cdr (assoc kwd org-tag-faces))
  4454. 'org-tag))
  4455. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4456. "Remove fontification and activation overlays from links."
  4457. (font-lock-default-unfontify-region beg end)
  4458. (let* ((buffer-undo-list t)
  4459. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4460. (inhibit-modification-hooks t)
  4461. deactivate-mark buffer-file-name buffer-file-truename)
  4462. (remove-text-properties
  4463. beg end
  4464. (if org-indent-mode
  4465. ;; also remove line-prefix and wrap-prefix properties
  4466. '(mouse-face t keymap t org-linked-text t
  4467. invisible t intangible t
  4468. line-prefix t wrap-prefix t
  4469. org-no-flyspell t)
  4470. '(mouse-face t keymap t org-linked-text t
  4471. invisible t intangible t
  4472. org-no-flyspell t)))))
  4473. ;;;; Visibility cycling, including org-goto and indirect buffer
  4474. ;;; Cycling
  4475. (defvar org-cycle-global-status nil)
  4476. (make-variable-buffer-local 'org-cycle-global-status)
  4477. (defvar org-cycle-subtree-status nil)
  4478. (make-variable-buffer-local 'org-cycle-subtree-status)
  4479. ;;;###autoload
  4480. (defvar org-inlinetask-min-level)
  4481. (defun org-cycle (&optional arg)
  4482. "TAB-action and visibility cycling for Org-mode.
  4483. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4484. is outline visibility cycling, but it also invokes other actions
  4485. in special contexts.
  4486. - When this function is called with a prefix argument, rotate the entire
  4487. buffer through 3 states (global cycling)
  4488. 1. OVERVIEW: Show only top-level headlines.
  4489. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4490. 3. SHOW ALL: Show everything.
  4491. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4492. determined by the variable `org-startup-folded', and by any VISIBILITY
  4493. properties in the buffer.
  4494. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4495. including any drawers.
  4496. - When inside a table, re-align the table and move to the next field.
  4497. - When point is at the beginning of a headline, rotate the subtree started
  4498. by this line through 3 different states (local cycling)
  4499. 1. FOLDED: Only the main headline is shown.
  4500. 2. CHILDREN: The main headline and the direct children are shown.
  4501. From this state, you can move to one of the children
  4502. and zoom in further.
  4503. 3. SUBTREE: Show the entire subtree, including body text.
  4504. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4505. - When there is a numeric prefix, go up to a heading with level ARG, do
  4506. a `show-subtree' and return to the previous cursor position. If ARG
  4507. is negative, go up that many levels.
  4508. - When point is not at the beginning of a headline, execute the global
  4509. binding for TAB, which is re-indenting the line. See the option
  4510. `org-cycle-emulate-tab' for details.
  4511. - Special case: if point is at the beginning of the buffer and there is
  4512. no headline in line 1, this function will act as if called with prefix arg.
  4513. But only if also the variable `org-cycle-global-at-bob' is t."
  4514. (interactive "P")
  4515. (org-load-modules-maybe)
  4516. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4517. (and org-cycle-level-after-item/entry-creation
  4518. (or (org-cycle-level)
  4519. (org-cycle-item-indentation))))
  4520. (let* ((limit-level
  4521. (or org-cycle-max-level
  4522. (and (boundp 'org-inlinetask-min-level)
  4523. org-inlinetask-min-level
  4524. (1- org-inlinetask-min-level))))
  4525. (nstars (and limit-level
  4526. (if org-odd-levels-only
  4527. (and limit-level (1- (* limit-level 2)))
  4528. limit-level)))
  4529. (outline-regexp
  4530. (cond
  4531. ((not (org-mode-p)) outline-regexp)
  4532. ((or (eq org-cycle-include-plain-lists 'integrate)
  4533. (and org-cycle-include-plain-lists (org-at-item-p)))
  4534. (concat "\\(?:\\*"
  4535. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4536. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4537. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4538. (bob-special (and org-cycle-global-at-bob (bobp)
  4539. (not (looking-at outline-regexp))))
  4540. (org-cycle-hook
  4541. (if bob-special
  4542. (delq 'org-optimize-window-after-visibility-change
  4543. (copy-sequence org-cycle-hook))
  4544. org-cycle-hook))
  4545. (pos (point)))
  4546. (if (or bob-special (equal arg '(4)))
  4547. ;; special case: use global cycling
  4548. (setq arg t))
  4549. (cond
  4550. ((equal arg '(16))
  4551. (org-set-startup-visibility)
  4552. (message "Startup visibility, plus VISIBILITY properties"))
  4553. ((equal arg '(64))
  4554. (show-all)
  4555. (message "Entire buffer visible, including drawers"))
  4556. ((org-at-table-p 'any)
  4557. ;; Enter the table or move to the next field in the table
  4558. (or (org-table-recognize-table.el)
  4559. (progn
  4560. (if arg (org-table-edit-field t)
  4561. (org-table-justify-field-maybe)
  4562. (call-interactively 'org-table-next-field)))))
  4563. ((run-hook-with-args-until-success
  4564. 'org-tab-after-check-for-table-hook))
  4565. ((eq arg t) ;; Global cycling
  4566. (org-cycle-internal-global))
  4567. ((and org-drawers org-drawer-regexp
  4568. (save-excursion
  4569. (beginning-of-line 1)
  4570. (looking-at org-drawer-regexp)))
  4571. ;; Toggle block visibility
  4572. (org-flag-drawer
  4573. (not (get-char-property (match-end 0) 'invisible))))
  4574. ((integerp arg)
  4575. ;; Show-subtree, ARG levels up from here.
  4576. (save-excursion
  4577. (org-back-to-heading)
  4578. (outline-up-heading (if (< arg 0) (- arg)
  4579. (- (funcall outline-level) arg)))
  4580. (org-show-subtree)))
  4581. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4582. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4583. (org-cycle-internal-local))
  4584. ;; TAB emulation and template completion
  4585. (buffer-read-only (org-back-to-heading))
  4586. ((run-hook-with-args-until-success
  4587. 'org-tab-after-check-for-cycling-hook))
  4588. ((org-try-structure-completion))
  4589. ((org-try-cdlatex-tab))
  4590. ((run-hook-with-args-until-success
  4591. 'org-tab-before-tab-emulation-hook))
  4592. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4593. (or (not (bolp))
  4594. (not (looking-at outline-regexp))))
  4595. (call-interactively (global-key-binding "\t")))
  4596. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4597. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4598. (or (and (eq org-cycle-emulate-tab 'white)
  4599. (= (match-end 0) (point-at-eol)))
  4600. (and (eq org-cycle-emulate-tab 'whitestart)
  4601. (>= (match-end 0) pos))))
  4602. t
  4603. (eq org-cycle-emulate-tab t))
  4604. (call-interactively (global-key-binding "\t")))
  4605. (t (save-excursion
  4606. (org-back-to-heading)
  4607. (org-cycle)))))))
  4608. (defun org-cycle-internal-global ()
  4609. "Do the global cycling action."
  4610. (cond
  4611. ((and (eq last-command this-command)
  4612. (eq org-cycle-global-status 'overview))
  4613. ;; We just created the overview - now do table of contents
  4614. ;; This can be slow in very large buffers, so indicate action
  4615. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4616. (message "CONTENTS...")
  4617. (org-content)
  4618. (message "CONTENTS...done")
  4619. (setq org-cycle-global-status 'contents)
  4620. (run-hook-with-args 'org-cycle-hook 'contents))
  4621. ((and (eq last-command this-command)
  4622. (eq org-cycle-global-status 'contents))
  4623. ;; We just showed the table of contents - now show everything
  4624. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4625. (show-all)
  4626. (message "SHOW ALL")
  4627. (setq org-cycle-global-status 'all)
  4628. (run-hook-with-args 'org-cycle-hook 'all))
  4629. (t
  4630. ;; Default action: go to overview
  4631. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4632. (org-overview)
  4633. (message "OVERVIEW")
  4634. (setq org-cycle-global-status 'overview)
  4635. (run-hook-with-args 'org-cycle-hook 'overview))))
  4636. (defun org-cycle-internal-local ()
  4637. "Do the local cycling action."
  4638. (org-back-to-heading)
  4639. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4640. ;; First, some boundaries
  4641. (save-excursion
  4642. (org-back-to-heading)
  4643. (setq level (funcall outline-level))
  4644. (save-excursion
  4645. (beginning-of-line 2)
  4646. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4647. ; XEmacs does not have `next-single-char-property-change'
  4648. ; I'm not sure about Emacs 21.
  4649. (while (and (not (eobp)) ;; this is like `next-line'
  4650. (get-char-property (1- (point)) 'invisible))
  4651. (beginning-of-line 2))
  4652. (while (and (not (eobp)) ;; this is like `next-line'
  4653. (get-char-property (1- (point)) 'invisible))
  4654. (goto-char (next-single-char-property-change (point) 'invisible))
  4655. ;;;??? (or (bolp) (beginning-of-line 2))))
  4656. (and (eolp) (beginning-of-line 2))))
  4657. (setq eol (point)))
  4658. (outline-end-of-heading) (setq eoh (point))
  4659. (save-excursion
  4660. (outline-next-heading)
  4661. (setq has-children (and (org-at-heading-p t)
  4662. (> (funcall outline-level) level))))
  4663. (org-end-of-subtree t)
  4664. (unless (eobp)
  4665. (skip-chars-forward " \t\n")
  4666. (beginning-of-line 1) ; in case this is an item
  4667. )
  4668. (setq eos (if (eobp) (point) (1- (point)))))
  4669. ;; Find out what to do next and set `this-command'
  4670. (cond
  4671. ((= eos eoh)
  4672. ;; Nothing is hidden behind this heading
  4673. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4674. (message "EMPTY ENTRY")
  4675. (setq org-cycle-subtree-status nil)
  4676. (save-excursion
  4677. (goto-char eos)
  4678. (outline-next-heading)
  4679. (if (org-invisible-p) (org-flag-heading nil))))
  4680. ((and (or (>= eol eos)
  4681. (not (string-match "\\S-" (buffer-substring eol eos))))
  4682. (or has-children
  4683. (not (setq children-skipped
  4684. org-cycle-skip-children-state-if-no-children))))
  4685. ;; Entire subtree is hidden in one line: children view
  4686. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4687. (org-show-entry)
  4688. (show-children)
  4689. (message "CHILDREN")
  4690. (save-excursion
  4691. (goto-char eos)
  4692. (outline-next-heading)
  4693. (if (org-invisible-p) (org-flag-heading nil)))
  4694. (setq org-cycle-subtree-status 'children)
  4695. (run-hook-with-args 'org-cycle-hook 'children))
  4696. ((or children-skipped
  4697. (and (eq last-command this-command)
  4698. (eq org-cycle-subtree-status 'children)))
  4699. ;; We just showed the children, or no children are there,
  4700. ;; now show everything.
  4701. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4702. (org-show-subtree)
  4703. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4704. (setq org-cycle-subtree-status 'subtree)
  4705. (run-hook-with-args 'org-cycle-hook 'subtree))
  4706. (t
  4707. ;; Default action: hide the subtree.
  4708. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4709. (hide-subtree)
  4710. (message "FOLDED")
  4711. (setq org-cycle-subtree-status 'folded)
  4712. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4713. ;;;###autoload
  4714. (defun org-global-cycle (&optional arg)
  4715. "Cycle the global visibility. For details see `org-cycle'.
  4716. With C-u prefix arg, switch to startup visibility.
  4717. With a numeric prefix, show all headlines up to that level."
  4718. (interactive "P")
  4719. (let ((org-cycle-include-plain-lists
  4720. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4721. (cond
  4722. ((integerp arg)
  4723. (show-all)
  4724. (hide-sublevels arg)
  4725. (setq org-cycle-global-status 'contents))
  4726. ((equal arg '(4))
  4727. (org-set-startup-visibility)
  4728. (message "Startup visibility, plus VISIBILITY properties."))
  4729. (t
  4730. (org-cycle '(4))))))
  4731. (defun org-set-startup-visibility ()
  4732. "Set the visibility required by startup options and properties."
  4733. (cond
  4734. ((eq org-startup-folded t)
  4735. (org-cycle '(4)))
  4736. ((eq org-startup-folded 'content)
  4737. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4738. (org-cycle '(4)) (org-cycle '(4)))))
  4739. (unless (eq org-startup-folded 'showeverything)
  4740. (if org-hide-block-startup (org-hide-block-all))
  4741. (org-set-visibility-according-to-property 'no-cleanup)
  4742. (org-cycle-hide-archived-subtrees 'all)
  4743. (org-cycle-hide-drawers 'all)
  4744. (org-cycle-show-empty-lines 'all)))
  4745. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4746. "Switch subtree visibilities according to :VISIBILITY: property."
  4747. (interactive)
  4748. (let (org-show-entry-below state)
  4749. (save-excursion
  4750. (goto-char (point-min))
  4751. (while (re-search-forward
  4752. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4753. nil t)
  4754. (setq state (match-string 1))
  4755. (save-excursion
  4756. (org-back-to-heading t)
  4757. (hide-subtree)
  4758. (org-reveal)
  4759. (cond
  4760. ((equal state '("fold" "folded"))
  4761. (hide-subtree))
  4762. ((equal state "children")
  4763. (org-show-hidden-entry)
  4764. (show-children))
  4765. ((equal state "content")
  4766. (save-excursion
  4767. (save-restriction
  4768. (org-narrow-to-subtree)
  4769. (org-content))))
  4770. ((member state '("all" "showall"))
  4771. (show-subtree)))))
  4772. (unless no-cleanup
  4773. (org-cycle-hide-archived-subtrees 'all)
  4774. (org-cycle-hide-drawers 'all)
  4775. (org-cycle-show-empty-lines 'all)))))
  4776. (defun org-overview ()
  4777. "Switch to overview mode, showing only top-level headlines.
  4778. Really, this shows all headlines with level equal or greater than the level
  4779. of the first headline in the buffer. This is important, because if the
  4780. first headline is not level one, then (hide-sublevels 1) gives confusing
  4781. results."
  4782. (interactive)
  4783. (let ((level (save-excursion
  4784. (goto-char (point-min))
  4785. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4786. (progn
  4787. (goto-char (match-beginning 0))
  4788. (funcall outline-level))))))
  4789. (and level (hide-sublevels level))))
  4790. (defun org-content (&optional arg)
  4791. "Show all headlines in the buffer, like a table of contents.
  4792. With numerical argument N, show content up to level N."
  4793. (interactive "P")
  4794. (save-excursion
  4795. ;; Visit all headings and show their offspring
  4796. (and (integerp arg) (org-overview))
  4797. (goto-char (point-max))
  4798. (catch 'exit
  4799. (while (and (progn (condition-case nil
  4800. (outline-previous-visible-heading 1)
  4801. (error (goto-char (point-min))))
  4802. t)
  4803. (looking-at outline-regexp))
  4804. (if (integerp arg)
  4805. (show-children (1- arg))
  4806. (show-branches))
  4807. (if (bobp) (throw 'exit nil))))))
  4808. (defun org-optimize-window-after-visibility-change (state)
  4809. "Adjust the window after a change in outline visibility.
  4810. This function is the default value of the hook `org-cycle-hook'."
  4811. (when (get-buffer-window (current-buffer))
  4812. (cond
  4813. ((eq state 'content) nil)
  4814. ((eq state 'all) nil)
  4815. ((eq state 'folded) nil)
  4816. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4817. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4818. ;; FIXME: no longer in use
  4819. (defun org-compact-display-after-subtree-move ()
  4820. "Show a compacter version of the tree of the entry's parent."
  4821. (save-excursion
  4822. (if (org-up-heading-safe)
  4823. (progn
  4824. (hide-subtree)
  4825. (show-entry)
  4826. (show-children)
  4827. (org-cycle-show-empty-lines 'children)
  4828. (org-cycle-hide-drawers 'children))
  4829. (org-overview))))
  4830. (defun org-remove-empty-overlays-at (pos)
  4831. "Remove outline overlays that do not contain non-white stuff."
  4832. (mapc
  4833. (lambda (o)
  4834. (and (eq 'outline (org-overlay-get o 'invisible))
  4835. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4836. (org-overlay-end o))))
  4837. (org-delete-overlay o)))
  4838. (org-overlays-at pos)))
  4839. (defun org-clean-visibility-after-subtree-move ()
  4840. "Fix visibility issues after moving a subtree."
  4841. ;; First, find a reasonable region to look at:
  4842. ;; Start two siblings above, end three below
  4843. (let* ((beg (save-excursion
  4844. (and (org-get-last-sibling)
  4845. (org-get-last-sibling))
  4846. (point)))
  4847. (end (save-excursion
  4848. (and (org-get-next-sibling)
  4849. (org-get-next-sibling)
  4850. (org-get-next-sibling))
  4851. (if (org-at-heading-p)
  4852. (point-at-eol)
  4853. (point))))
  4854. (level (looking-at "\\*+"))
  4855. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4856. (save-excursion
  4857. (save-restriction
  4858. (narrow-to-region beg end)
  4859. (when re
  4860. ;; Properly fold already folded siblings
  4861. (goto-char (point-min))
  4862. (while (re-search-forward re nil t)
  4863. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4864. (hide-entry))))
  4865. (org-cycle-show-empty-lines 'overview)
  4866. (org-cycle-hide-drawers 'overview)))))
  4867. (defun org-cycle-show-empty-lines (state)
  4868. "Show empty lines above all visible headlines.
  4869. The region to be covered depends on STATE when called through
  4870. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4871. entire buffer covered. Note that an empty line is only shown if there
  4872. are at least `org-cycle-separator-lines' empty lines before the headline."
  4873. (when (not (= org-cycle-separator-lines 0))
  4874. (save-excursion
  4875. (let* ((n (abs org-cycle-separator-lines))
  4876. (re (cond
  4877. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4878. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4879. (t (let ((ns (number-to-string (- n 2))))
  4880. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4881. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4882. beg end b e)
  4883. (cond
  4884. ((memq state '(overview contents t))
  4885. (setq beg (point-min) end (point-max)))
  4886. ((memq state '(children folded))
  4887. (setq beg (point) end (progn (org-end-of-subtree t t)
  4888. (beginning-of-line 2)
  4889. (point)))))
  4890. (when beg
  4891. (goto-char beg)
  4892. (while (re-search-forward re end t)
  4893. (unless (get-char-property (match-end 1) 'invisible)
  4894. (setq e (match-end 1))
  4895. (if (< org-cycle-separator-lines 0)
  4896. (setq b (save-excursion
  4897. (goto-char (match-beginning 0))
  4898. (org-back-over-empty-lines)
  4899. (if (save-excursion
  4900. (goto-char (max (point-min) (1- (point))))
  4901. (org-on-heading-p))
  4902. (1- (point))
  4903. (point))))
  4904. (setq b (match-beginning 1)))
  4905. (outline-flag-region b e nil)))))))
  4906. ;; Never hide empty lines at the end of the file.
  4907. (save-excursion
  4908. (goto-char (point-max))
  4909. (outline-previous-heading)
  4910. (outline-end-of-heading)
  4911. (if (and (looking-at "[ \t\n]+")
  4912. (= (match-end 0) (point-max)))
  4913. (outline-flag-region (point) (match-end 0) nil))))
  4914. (defun org-show-empty-lines-in-parent ()
  4915. "Move to the parent and re-show empty lines before visible headlines."
  4916. (save-excursion
  4917. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4918. (org-cycle-show-empty-lines context))))
  4919. (defun org-files-list ()
  4920. "Return `org-agenda-files' list, plus all open org-mode files.
  4921. This is useful for operations that need to scan all of a user's
  4922. open and agenda-wise Org files."
  4923. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4924. (dolist (buf (buffer-list))
  4925. (with-current-buffer buf
  4926. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4927. (let ((file (expand-file-name (buffer-file-name))))
  4928. (unless (member file files)
  4929. (push file files))))))
  4930. files))
  4931. (defsubst org-entry-beginning-position ()
  4932. "Return the beginning position of the current entry."
  4933. (save-excursion (outline-back-to-heading t) (point)))
  4934. (defsubst org-entry-end-position ()
  4935. "Return the end position of the current entry."
  4936. (save-excursion (outline-next-heading) (point)))
  4937. (defun org-cycle-hide-drawers (state)
  4938. "Re-hide all drawers after a visibility state change."
  4939. (when (and (org-mode-p)
  4940. (not (memq state '(overview folded contents))))
  4941. (save-excursion
  4942. (let* ((globalp (memq state '(contents all)))
  4943. (beg (if globalp (point-min) (point)))
  4944. (end (if globalp (point-max)
  4945. (if (eq state 'children)
  4946. (save-excursion (outline-next-heading) (point))
  4947. (org-end-of-subtree t)))))
  4948. (goto-char beg)
  4949. (while (re-search-forward org-drawer-regexp end t)
  4950. (org-flag-drawer t))))))
  4951. (defun org-flag-drawer (flag)
  4952. (save-excursion
  4953. (beginning-of-line 1)
  4954. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4955. (let ((b (match-end 0))
  4956. (outline-regexp org-outline-regexp))
  4957. (if (re-search-forward
  4958. "^[ \t]*:END:"
  4959. (save-excursion (outline-next-heading) (point)) t)
  4960. (outline-flag-region b (point-at-eol) flag)
  4961. (error ":END: line missing at position %s" b))))))
  4962. (defun org-subtree-end-visible-p ()
  4963. "Is the end of the current subtree visible?"
  4964. (pos-visible-in-window-p
  4965. (save-excursion (org-end-of-subtree t) (point))))
  4966. (defun org-first-headline-recenter (&optional N)
  4967. "Move cursor to the first headline and recenter the headline.
  4968. Optional argument N means, put the headline into the Nth line of the window."
  4969. (goto-char (point-min))
  4970. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4971. (beginning-of-line)
  4972. (recenter (prefix-numeric-value N))))
  4973. ;;; Folding of blocks
  4974. (defconst org-block-regexp
  4975. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4976. "Regular expression for hiding blocks.")
  4977. (defvar org-hide-block-overlays nil
  4978. "Overlays hiding blocks.")
  4979. (make-variable-buffer-local 'org-hide-block-overlays)
  4980. (defun org-block-map (function &optional start end)
  4981. "Call func at the head of all source blocks in the current
  4982. buffer. Optional arguments START and END can be used to limit
  4983. the range."
  4984. (let ((start (or start (point-min)))
  4985. (end (or end (point-max))))
  4986. (save-excursion
  4987. (goto-char start)
  4988. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4989. (save-excursion
  4990. (save-match-data
  4991. (goto-char (match-beginning 0))
  4992. (funcall function)))))))
  4993. (defun org-hide-block-toggle-all ()
  4994. "Toggle the visibility of all blocks in the current buffer."
  4995. (org-block-map #'org-hide-block-toggle))
  4996. (defun org-hide-block-all ()
  4997. "Fold all blocks in the current buffer."
  4998. (interactive)
  4999. (org-show-block-all)
  5000. (org-block-map #'org-hide-block-toggle-maybe))
  5001. (defun org-show-block-all ()
  5002. "Unfold all blocks in the current buffer."
  5003. (mapc 'org-delete-overlay org-hide-block-overlays)
  5004. (setq org-hide-block-overlays nil))
  5005. (defun org-hide-block-toggle-maybe ()
  5006. "Toggle visibility of block at point."
  5007. (interactive)
  5008. (let ((case-fold-search t))
  5009. (if (save-excursion
  5010. (beginning-of-line 1)
  5011. (looking-at org-block-regexp))
  5012. (progn (org-hide-block-toggle)
  5013. t) ;; to signal that we took action
  5014. nil))) ;; to signal that we did not
  5015. (defun org-hide-block-toggle (&optional force)
  5016. "Toggle the visibility of the current block."
  5017. (interactive)
  5018. (save-excursion
  5019. (beginning-of-line)
  5020. (if (re-search-forward org-block-regexp nil t)
  5021. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5022. (end (match-end 0)) ;; end of entire body
  5023. ov)
  5024. (if (memq t (mapcar (lambda (overlay)
  5025. (eq (org-overlay-get overlay 'invisible)
  5026. 'org-hide-block))
  5027. (org-overlays-at start)))
  5028. (if (or (not force) (eq force 'off))
  5029. (mapc (lambda (ov)
  5030. (when (member ov org-hide-block-overlays)
  5031. (setq org-hide-block-overlays
  5032. (delq ov org-hide-block-overlays)))
  5033. (when (eq (org-overlay-get ov 'invisible)
  5034. 'org-hide-block)
  5035. (org-delete-overlay ov)))
  5036. (org-overlays-at start)))
  5037. (setq ov (org-make-overlay start end))
  5038. (org-overlay-put ov 'invisible 'org-hide-block)
  5039. ;; make the block accessible to isearch
  5040. (org-overlay-put
  5041. ov 'isearch-open-invisible
  5042. (lambda (ov)
  5043. (when (member ov org-hide-block-overlays)
  5044. (setq org-hide-block-overlays
  5045. (delq ov org-hide-block-overlays)))
  5046. (when (eq (org-overlay-get ov 'invisible)
  5047. 'org-hide-block)
  5048. (org-delete-overlay ov))))
  5049. (push ov org-hide-block-overlays)))
  5050. (error "Not looking at a source block"))))
  5051. ;; org-tab-after-check-for-cycling-hook
  5052. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5053. ;; Remove overlays when changing major mode
  5054. (add-hook 'org-mode-hook
  5055. (lambda () (org-add-hook 'change-major-mode-hook
  5056. 'org-show-block-all 'append 'local)))
  5057. ;;; Org-goto
  5058. (defvar org-goto-window-configuration nil)
  5059. (defvar org-goto-marker nil)
  5060. (defvar org-goto-map
  5061. (let ((map (make-sparse-keymap)))
  5062. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5063. (while (setq cmd (pop cmds))
  5064. (substitute-key-definition cmd cmd map global-map)))
  5065. (suppress-keymap map)
  5066. (org-defkey map "\C-m" 'org-goto-ret)
  5067. (org-defkey map [(return)] 'org-goto-ret)
  5068. (org-defkey map [(left)] 'org-goto-left)
  5069. (org-defkey map [(right)] 'org-goto-right)
  5070. (org-defkey map [(control ?g)] 'org-goto-quit)
  5071. (org-defkey map "\C-i" 'org-cycle)
  5072. (org-defkey map [(tab)] 'org-cycle)
  5073. (org-defkey map [(down)] 'outline-next-visible-heading)
  5074. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5075. (if org-goto-auto-isearch
  5076. (if (fboundp 'define-key-after)
  5077. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5078. nil)
  5079. (org-defkey map "q" 'org-goto-quit)
  5080. (org-defkey map "n" 'outline-next-visible-heading)
  5081. (org-defkey map "p" 'outline-previous-visible-heading)
  5082. (org-defkey map "f" 'outline-forward-same-level)
  5083. (org-defkey map "b" 'outline-backward-same-level)
  5084. (org-defkey map "u" 'outline-up-heading))
  5085. (org-defkey map "/" 'org-occur)
  5086. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5087. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5088. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5089. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5090. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5091. map))
  5092. (defconst org-goto-help
  5093. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5094. RET=jump to location [Q]uit and return to previous location
  5095. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5096. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5097. ;; FIXME: Docstring does not mention both interfaces
  5098. (defun org-goto (&optional alternative-interface)
  5099. "Look up a different location in the current file, keeping current visibility.
  5100. When you want look-up or go to a different location in a document, the
  5101. fastest way is often to fold the entire buffer and then dive into the tree.
  5102. This method has the disadvantage, that the previous location will be folded,
  5103. which may not be what you want.
  5104. This command works around this by showing a copy of the current buffer
  5105. in an indirect buffer, in overview mode. You can dive into the tree in
  5106. that copy, use org-occur and incremental search to find a location.
  5107. When pressing RET or `Q', the command returns to the original buffer in
  5108. which the visibility is still unchanged. After RET is will also jump to
  5109. the location selected in the indirect buffer and expose the
  5110. the headline hierarchy above."
  5111. (interactive "P")
  5112. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5113. (org-refile-use-outline-path t)
  5114. (org-refile-target-verify-function nil)
  5115. (interface
  5116. (if (not alternative-interface)
  5117. org-goto-interface
  5118. (if (eq org-goto-interface 'outline)
  5119. 'outline-path-completion
  5120. 'outline)))
  5121. (org-goto-start-pos (point))
  5122. (selected-point
  5123. (if (eq interface 'outline)
  5124. (car (org-get-location (current-buffer) org-goto-help))
  5125. (nth 3 (org-refile-get-location "Goto: ")))))
  5126. (if selected-point
  5127. (progn
  5128. (org-mark-ring-push org-goto-start-pos)
  5129. (goto-char selected-point)
  5130. (if (or (org-invisible-p) (org-invisible-p2))
  5131. (org-show-context 'org-goto)))
  5132. (message "Quit"))))
  5133. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5134. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5135. (defvar org-goto-local-auto-isearch-map) ; defined below
  5136. (defun org-get-location (buf help)
  5137. "Let the user select a location in the Org-mode buffer BUF.
  5138. This function uses a recursive edit. It returns the selected position
  5139. or nil."
  5140. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5141. (isearch-hide-immediately nil)
  5142. (isearch-search-fun-function
  5143. (lambda () 'org-goto-local-search-headings))
  5144. (org-goto-selected-point org-goto-exit-command))
  5145. (save-excursion
  5146. (save-window-excursion
  5147. (delete-other-windows)
  5148. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5149. (switch-to-buffer
  5150. (condition-case nil
  5151. (make-indirect-buffer (current-buffer) "*org-goto*")
  5152. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5153. (with-output-to-temp-buffer "*Help*"
  5154. (princ help))
  5155. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5156. (setq buffer-read-only nil)
  5157. (let ((org-startup-truncated t)
  5158. (org-startup-folded nil)
  5159. (org-startup-align-all-tables nil))
  5160. (org-mode)
  5161. (org-overview))
  5162. (setq buffer-read-only t)
  5163. (if (and (boundp 'org-goto-start-pos)
  5164. (integer-or-marker-p org-goto-start-pos))
  5165. (let ((org-show-hierarchy-above t)
  5166. (org-show-siblings t)
  5167. (org-show-following-heading t))
  5168. (goto-char org-goto-start-pos)
  5169. (and (org-invisible-p) (org-show-context)))
  5170. (goto-char (point-min)))
  5171. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5172. (message "Select location and press RET")
  5173. (use-local-map org-goto-map)
  5174. (recursive-edit)
  5175. ))
  5176. (kill-buffer "*org-goto*")
  5177. (cons org-goto-selected-point org-goto-exit-command)))
  5178. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5179. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5180. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5181. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5182. (defun org-goto-local-search-headings (string bound noerror)
  5183. "Search and make sure that any matches are in headlines."
  5184. (catch 'return
  5185. (while (if isearch-forward
  5186. (search-forward string bound noerror)
  5187. (search-backward string bound noerror))
  5188. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5189. (and (member :headline context)
  5190. (not (member :tags context))))
  5191. (throw 'return (point))))))
  5192. (defun org-goto-local-auto-isearch ()
  5193. "Start isearch."
  5194. (interactive)
  5195. (goto-char (point-min))
  5196. (let ((keys (this-command-keys)))
  5197. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5198. (isearch-mode t)
  5199. (isearch-process-search-char (string-to-char keys)))))
  5200. (defun org-goto-ret (&optional arg)
  5201. "Finish `org-goto' by going to the new location."
  5202. (interactive "P")
  5203. (setq org-goto-selected-point (point)
  5204. org-goto-exit-command 'return)
  5205. (throw 'exit nil))
  5206. (defun org-goto-left ()
  5207. "Finish `org-goto' by going to the new location."
  5208. (interactive)
  5209. (if (org-on-heading-p)
  5210. (progn
  5211. (beginning-of-line 1)
  5212. (setq org-goto-selected-point (point)
  5213. org-goto-exit-command 'left)
  5214. (throw 'exit nil))
  5215. (error "Not on a heading")))
  5216. (defun org-goto-right ()
  5217. "Finish `org-goto' by going to the new location."
  5218. (interactive)
  5219. (if (org-on-heading-p)
  5220. (progn
  5221. (setq org-goto-selected-point (point)
  5222. org-goto-exit-command 'right)
  5223. (throw 'exit nil))
  5224. (error "Not on a heading")))
  5225. (defun org-goto-quit ()
  5226. "Finish `org-goto' without cursor motion."
  5227. (interactive)
  5228. (setq org-goto-selected-point nil)
  5229. (setq org-goto-exit-command 'quit)
  5230. (throw 'exit nil))
  5231. ;;; Indirect buffer display of subtrees
  5232. (defvar org-indirect-dedicated-frame nil
  5233. "This is the frame being used for indirect tree display.")
  5234. (defvar org-last-indirect-buffer nil)
  5235. (defun org-tree-to-indirect-buffer (&optional arg)
  5236. "Create indirect buffer and narrow it to current subtree.
  5237. With numerical prefix ARG, go up to this level and then take that tree.
  5238. If ARG is negative, go up that many levels.
  5239. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5240. indirect buffer previously made with this command, to avoid proliferation of
  5241. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5242. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5243. is kept so that you can work with several indirect buffers at the same time.
  5244. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5245. requests that a new frame be made for the new buffer, so that the dedicated
  5246. frame is not changed."
  5247. (interactive "P")
  5248. (let ((cbuf (current-buffer))
  5249. (cwin (selected-window))
  5250. (pos (point))
  5251. beg end level heading ibuf)
  5252. (save-excursion
  5253. (org-back-to-heading t)
  5254. (when (numberp arg)
  5255. (setq level (org-outline-level))
  5256. (if (< arg 0) (setq arg (+ level arg)))
  5257. (while (> (setq level (org-outline-level)) arg)
  5258. (outline-up-heading 1 t)))
  5259. (setq beg (point)
  5260. heading (org-get-heading))
  5261. (org-end-of-subtree t t) (setq end (point)))
  5262. (if (and (buffer-live-p org-last-indirect-buffer)
  5263. (not (eq org-indirect-buffer-display 'new-frame))
  5264. (not arg))
  5265. (kill-buffer org-last-indirect-buffer))
  5266. (setq ibuf (org-get-indirect-buffer cbuf)
  5267. org-last-indirect-buffer ibuf)
  5268. (cond
  5269. ((or (eq org-indirect-buffer-display 'new-frame)
  5270. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5271. (select-frame (make-frame))
  5272. (delete-other-windows)
  5273. (switch-to-buffer ibuf)
  5274. (org-set-frame-title heading))
  5275. ((eq org-indirect-buffer-display 'dedicated-frame)
  5276. (raise-frame
  5277. (select-frame (or (and org-indirect-dedicated-frame
  5278. (frame-live-p org-indirect-dedicated-frame)
  5279. org-indirect-dedicated-frame)
  5280. (setq org-indirect-dedicated-frame (make-frame)))))
  5281. (delete-other-windows)
  5282. (switch-to-buffer ibuf)
  5283. (org-set-frame-title (concat "Indirect: " heading)))
  5284. ((eq org-indirect-buffer-display 'current-window)
  5285. (switch-to-buffer ibuf))
  5286. ((eq org-indirect-buffer-display 'other-window)
  5287. (pop-to-buffer ibuf))
  5288. (t (error "Invalid value")))
  5289. (if (featurep 'xemacs)
  5290. (save-excursion (org-mode) (turn-on-font-lock)))
  5291. (narrow-to-region beg end)
  5292. (show-all)
  5293. (goto-char pos)
  5294. (and (window-live-p cwin) (select-window cwin))))
  5295. (defun org-get-indirect-buffer (&optional buffer)
  5296. (setq buffer (or buffer (current-buffer)))
  5297. (let ((n 1) (base (buffer-name buffer)) bname)
  5298. (while (buffer-live-p
  5299. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5300. (setq n (1+ n)))
  5301. (condition-case nil
  5302. (make-indirect-buffer buffer bname 'clone)
  5303. (error (make-indirect-buffer buffer bname)))))
  5304. (defun org-set-frame-title (title)
  5305. "Set the title of the current frame to the string TITLE."
  5306. ;; FIXME: how to name a single frame in XEmacs???
  5307. (unless (featurep 'xemacs)
  5308. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5309. ;;;; Structure editing
  5310. ;;; Inserting headlines
  5311. (defun org-previous-line-empty-p ()
  5312. (save-excursion
  5313. (and (not (bobp))
  5314. (or (beginning-of-line 0) t)
  5315. (save-match-data
  5316. (looking-at "[ \t]*$")))))
  5317. (defun org-insert-heading (&optional force-heading)
  5318. "Insert a new heading or item with same depth at point.
  5319. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5320. If point is at the beginning of a headline, insert a sibling before the
  5321. current headline. If point is not at the beginning, do not split the line,
  5322. but create the new headline after the current line."
  5323. (interactive "P")
  5324. (if (= (buffer-size) 0)
  5325. (insert "\n* ")
  5326. (when (or force-heading (not (org-insert-item)))
  5327. (let* ((empty-line-p nil)
  5328. (head (save-excursion
  5329. (condition-case nil
  5330. (progn
  5331. (org-back-to-heading)
  5332. (setq empty-line-p (org-previous-line-empty-p))
  5333. (match-string 0))
  5334. (error "*"))))
  5335. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5336. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5337. pos hide-previous previous-pos)
  5338. (cond
  5339. ((and (org-on-heading-p) (bolp)
  5340. (or (bobp)
  5341. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5342. ;; insert before the current line
  5343. (open-line (if blank 2 1)))
  5344. ((and (bolp)
  5345. (not org-insert-heading-respect-content)
  5346. (or (bobp)
  5347. (save-excursion
  5348. (backward-char 1) (not (org-invisible-p)))))
  5349. ;; insert right here
  5350. nil)
  5351. (t
  5352. ;; somewhere in the line
  5353. (save-excursion
  5354. (setq previous-pos (point-at-bol))
  5355. (end-of-line)
  5356. (setq hide-previous (org-invisible-p)))
  5357. (and org-insert-heading-respect-content (org-show-subtree))
  5358. (let ((split
  5359. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5360. (save-excursion
  5361. (let ((p (point)))
  5362. (goto-char (point-at-bol))
  5363. (and (looking-at org-complex-heading-regexp)
  5364. (> p (match-beginning 4)))))))
  5365. tags pos)
  5366. (cond
  5367. (org-insert-heading-respect-content
  5368. (org-end-of-subtree nil t)
  5369. (or (bolp) (newline))
  5370. (or (org-previous-line-empty-p)
  5371. (and blank (newline)))
  5372. (open-line 1))
  5373. ((org-on-heading-p)
  5374. (when hide-previous
  5375. (show-children)
  5376. (org-show-entry))
  5377. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5378. (setq tags (and (match-end 2) (match-string 2)))
  5379. (and (match-end 1)
  5380. (delete-region (match-beginning 1) (match-end 1)))
  5381. (setq pos (point-at-bol))
  5382. (or split (end-of-line 1))
  5383. (delete-horizontal-space)
  5384. (newline (if blank 2 1))
  5385. (when tags
  5386. (save-excursion
  5387. (goto-char pos)
  5388. (end-of-line 1)
  5389. (insert " " tags)
  5390. (org-set-tags nil 'align))))
  5391. (t
  5392. (or split (end-of-line 1))
  5393. (newline (if blank 2 1)))))))
  5394. (insert head) (just-one-space)
  5395. (setq pos (point))
  5396. (end-of-line 1)
  5397. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5398. (when (and org-insert-heading-respect-content hide-previous)
  5399. (save-excursion
  5400. (goto-char previous-pos)
  5401. (hide-subtree)))
  5402. (run-hooks 'org-insert-heading-hook)))))
  5403. (defun org-get-heading (&optional no-tags)
  5404. "Return the heading of the current entry, without the stars."
  5405. (save-excursion
  5406. (org-back-to-heading t)
  5407. (if (looking-at
  5408. (if no-tags
  5409. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5410. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5411. (match-string 1) "")))
  5412. (defun org-heading-components ()
  5413. "Return the components of the current heading.
  5414. This is a list with the following elements:
  5415. - the level as an integer
  5416. - the reduced level, different if `org-odd-levels-only' is set.
  5417. - the TODO keyword, or nil
  5418. - the priority character, like ?A, or nil if no priority is given
  5419. - the headline text itself, or the tags string if no headline text
  5420. - the tags string, or nil."
  5421. (save-excursion
  5422. (org-back-to-heading t)
  5423. (if (looking-at org-complex-heading-regexp)
  5424. (list (length (match-string 1))
  5425. (org-reduced-level (length (match-string 1)))
  5426. (org-match-string-no-properties 2)
  5427. (and (match-end 3) (aref (match-string 3) 2))
  5428. (org-match-string-no-properties 4)
  5429. (org-match-string-no-properties 5)))))
  5430. (defun org-get-entry ()
  5431. "Get the entry text, after heading, entire subtree."
  5432. (save-excursion
  5433. (org-back-to-heading t)
  5434. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5435. (defun org-insert-heading-after-current ()
  5436. "Insert a new heading with same level as current, after current subtree."
  5437. (interactive)
  5438. (org-back-to-heading)
  5439. (org-insert-heading)
  5440. (org-move-subtree-down)
  5441. (end-of-line 1))
  5442. (defun org-insert-heading-respect-content ()
  5443. (interactive)
  5444. (let ((org-insert-heading-respect-content t))
  5445. (org-insert-heading t)))
  5446. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5447. (interactive "P")
  5448. (let ((org-insert-heading-respect-content t))
  5449. (org-insert-todo-heading force-state t)))
  5450. (defun org-insert-todo-heading (arg &optional force-heading)
  5451. "Insert a new heading with the same level and TODO state as current heading.
  5452. If the heading has no TODO state, or if the state is DONE, use the first
  5453. state (TODO by default). Also with prefix arg, force first state."
  5454. (interactive "P")
  5455. (when (or force-heading (not (org-insert-item 'checkbox)))
  5456. (org-insert-heading force-heading)
  5457. (save-excursion
  5458. (org-back-to-heading)
  5459. (outline-previous-heading)
  5460. (looking-at org-todo-line-regexp))
  5461. (let*
  5462. ((new-mark-x
  5463. (if (or arg
  5464. (not (match-beginning 2))
  5465. (member (match-string 2) org-done-keywords))
  5466. (car org-todo-keywords-1)
  5467. (match-string 2)))
  5468. (new-mark
  5469. (or
  5470. (run-hook-with-args-until-success
  5471. 'org-todo-get-default-hook new-mark-x nil)
  5472. new-mark-x)))
  5473. (beginning-of-line 1)
  5474. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5475. (if org-treat-insert-todo-heading-as-state-change
  5476. (org-todo new-mark)
  5477. (insert new-mark " "))))
  5478. (when org-provide-todo-statistics
  5479. (org-update-parent-todo-statistics))))
  5480. (defun org-insert-subheading (arg)
  5481. "Insert a new subheading and demote it.
  5482. Works for outline headings and for plain lists alike."
  5483. (interactive "P")
  5484. (org-insert-heading arg)
  5485. (cond
  5486. ((org-on-heading-p) (org-do-demote))
  5487. ((org-at-item-p) (org-indent-item 1))))
  5488. (defun org-insert-todo-subheading (arg)
  5489. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5490. Works for outline headings and for plain lists alike."
  5491. (interactive "P")
  5492. (org-insert-todo-heading arg)
  5493. (cond
  5494. ((org-on-heading-p) (org-do-demote))
  5495. ((org-at-item-p) (org-indent-item 1))))
  5496. ;;; Promotion and Demotion
  5497. (defvar org-after-demote-entry-hook nil
  5498. "Hook run after an entry has been demoted.
  5499. The cursor will be at the beginning of the entry.
  5500. When a subtree is being demoted, the hook will be called for each node.")
  5501. (defvar org-after-promote-entry-hook nil
  5502. "Hook run after an entry has been promoted.
  5503. The cursor will be at the beginning of the entry.
  5504. When a subtree is being promoted, the hook will be called for each node.")
  5505. (defun org-promote-subtree ()
  5506. "Promote the entire subtree.
  5507. See also `org-promote'."
  5508. (interactive)
  5509. (save-excursion
  5510. (org-map-tree 'org-promote))
  5511. (org-fix-position-after-promote))
  5512. (defun org-demote-subtree ()
  5513. "Demote the entire subtree. See `org-demote'.
  5514. See also `org-promote'."
  5515. (interactive)
  5516. (save-excursion
  5517. (org-map-tree 'org-demote))
  5518. (org-fix-position-after-promote))
  5519. (defun org-do-promote ()
  5520. "Promote the current heading higher up the tree.
  5521. If the region is active in `transient-mark-mode', promote all headings
  5522. in the region."
  5523. (interactive)
  5524. (save-excursion
  5525. (if (org-region-active-p)
  5526. (org-map-region 'org-promote (region-beginning) (region-end))
  5527. (org-promote)))
  5528. (org-fix-position-after-promote))
  5529. (defun org-do-demote ()
  5530. "Demote the current heading lower down the tree.
  5531. If the region is active in `transient-mark-mode', demote all headings
  5532. in the region."
  5533. (interactive)
  5534. (save-excursion
  5535. (if (org-region-active-p)
  5536. (org-map-region 'org-demote (region-beginning) (region-end))
  5537. (org-demote)))
  5538. (org-fix-position-after-promote))
  5539. (defun org-fix-position-after-promote ()
  5540. "Make sure that after pro/demotion cursor position is right."
  5541. (let ((pos (point)))
  5542. (when (save-excursion
  5543. (beginning-of-line 1)
  5544. (looking-at org-todo-line-regexp)
  5545. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5546. (cond ((eobp) (insert " "))
  5547. ((eolp) (insert " "))
  5548. ((equal (char-after) ?\ ) (forward-char 1))))))
  5549. (defun org-current-level ()
  5550. "Return the level of the current entry, or nil if before the first headline.
  5551. The level is the number of stars at the beginning of the headline."
  5552. (save-excursion
  5553. (condition-case nil
  5554. (progn
  5555. (org-back-to-heading t)
  5556. (funcall outline-level))
  5557. (error nil))))
  5558. (defun org-reduced-level (l)
  5559. "Compute the effective level of a heading.
  5560. This takes into account the setting of `org-odd-levels-only'."
  5561. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5562. (defun org-get-valid-level (level &optional change)
  5563. "Rectify a level change under the influence of `org-odd-levels-only'
  5564. LEVEL is a current level, CHANGE is by how much the level should be
  5565. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5566. even level numbers will become the next higher odd number."
  5567. (if org-odd-levels-only
  5568. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5569. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5570. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5571. (max 1 (+ level (or change 0)))))
  5572. (if (boundp 'define-obsolete-function-alias)
  5573. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5574. (define-obsolete-function-alias 'org-get-legal-level
  5575. 'org-get-valid-level)
  5576. (define-obsolete-function-alias 'org-get-legal-level
  5577. 'org-get-valid-level "23.1")))
  5578. (defun org-promote ()
  5579. "Promote the current heading higher up the tree.
  5580. If the region is active in `transient-mark-mode', promote all headings
  5581. in the region."
  5582. (org-back-to-heading t)
  5583. (let* ((level (save-match-data (funcall outline-level)))
  5584. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5585. (diff (abs (- level (length up-head) -1))))
  5586. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5587. (replace-match up-head nil t)
  5588. ;; Fixup tag positioning
  5589. (and org-auto-align-tags (org-set-tags nil t))
  5590. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5591. (run-hooks 'org-after-promote-entry-hook)))
  5592. (defun org-demote ()
  5593. "Demote the current heading lower down the tree.
  5594. If the region is active in `transient-mark-mode', demote all headings
  5595. in the region."
  5596. (org-back-to-heading t)
  5597. (let* ((level (save-match-data (funcall outline-level)))
  5598. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5599. (diff (abs (- level (length down-head) -1))))
  5600. (replace-match down-head nil t)
  5601. ;; Fixup tag positioning
  5602. (and org-auto-align-tags (org-set-tags nil t))
  5603. (if org-adapt-indentation (org-fixup-indentation diff))
  5604. (run-hooks 'org-after-demote-entry-hook)))
  5605. (defvar org-tab-ind-state nil)
  5606. (defun org-cycle-level ()
  5607. (let ((org-adapt-indentation nil))
  5608. (when (and (looking-at "[ \t]*$")
  5609. (looking-back
  5610. (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))
  5611. (setq this-command 'org-cycle-level)
  5612. (if (eq last-command 'org-cycle-level)
  5613. (condition-case nil
  5614. (progn (org-do-promote)
  5615. (if (equal org-tab-ind-state (org-current-level))
  5616. (org-do-promote)))
  5617. (error
  5618. (progn
  5619. (save-excursion
  5620. (beginning-of-line 1)
  5621. (and (looking-at "\\*+")
  5622. (replace-match
  5623. (make-string org-tab-ind-state ?*))))
  5624. (setq this-command 'org-cycle))))
  5625. (setq org-tab-ind-state (- (match-end 1) (match-beginning 1)))
  5626. (org-do-demote))
  5627. t)))
  5628. (defun org-map-tree (fun)
  5629. "Call FUN for every heading underneath the current one."
  5630. (org-back-to-heading)
  5631. (let ((level (funcall outline-level)))
  5632. (save-excursion
  5633. (funcall fun)
  5634. (while (and (progn
  5635. (outline-next-heading)
  5636. (> (funcall outline-level) level))
  5637. (not (eobp)))
  5638. (funcall fun)))))
  5639. (defun org-map-region (fun beg end)
  5640. "Call FUN for every heading between BEG and END."
  5641. (let ((org-ignore-region t))
  5642. (save-excursion
  5643. (setq end (copy-marker end))
  5644. (goto-char beg)
  5645. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5646. (< (point) end))
  5647. (funcall fun))
  5648. (while (and (progn
  5649. (outline-next-heading)
  5650. (< (point) end))
  5651. (not (eobp)))
  5652. (funcall fun)))))
  5653. (defun org-fixup-indentation (diff)
  5654. "Change the indentation in the current entry by DIFF
  5655. However, if any line in the current entry has no indentation, or if it
  5656. would end up with no indentation after the change, nothing at all is done."
  5657. (save-excursion
  5658. (let ((end (save-excursion (outline-next-heading)
  5659. (point-marker)))
  5660. (prohibit (if (> diff 0)
  5661. "^\\S-"
  5662. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5663. col)
  5664. (unless (save-excursion (end-of-line 1)
  5665. (re-search-forward prohibit end t))
  5666. (while (and (< (point) end)
  5667. (re-search-forward "^[ \t]+" end t))
  5668. (goto-char (match-end 0))
  5669. (setq col (current-column))
  5670. (if (< diff 0) (replace-match ""))
  5671. (org-indent-to-column (+ diff col))))
  5672. (move-marker end nil))))
  5673. (defun org-convert-to-odd-levels ()
  5674. "Convert an org-mode file with all levels allowed to one with odd levels.
  5675. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5676. level 5 etc."
  5677. (interactive)
  5678. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5679. (let ((outline-regexp org-outline-regexp)
  5680. (outline-level 'org-outline-level)
  5681. (org-odd-levels-only nil) n)
  5682. (save-excursion
  5683. (goto-char (point-min))
  5684. (while (re-search-forward "^\\*\\*+ " nil t)
  5685. (setq n (- (length (match-string 0)) 2))
  5686. (while (>= (setq n (1- n)) 0)
  5687. (org-demote))
  5688. (end-of-line 1))))))
  5689. (defun org-convert-to-oddeven-levels ()
  5690. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5691. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5692. section with an even level, conversion would destroy the structure of the file. An error
  5693. is signaled in this case."
  5694. (interactive)
  5695. (goto-char (point-min))
  5696. ;; First check if there are no even levels
  5697. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5698. (org-show-context t)
  5699. (error "Not all levels are odd in this file. Conversion not possible"))
  5700. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5701. (let ((outline-regexp org-outline-regexp)
  5702. (outline-level 'org-outline-level)
  5703. (org-odd-levels-only nil) n)
  5704. (save-excursion
  5705. (goto-char (point-min))
  5706. (while (re-search-forward "^\\*\\*+ " nil t)
  5707. (setq n (/ (1- (length (match-string 0))) 2))
  5708. (while (>= (setq n (1- n)) 0)
  5709. (org-promote))
  5710. (end-of-line 1))))))
  5711. (defun org-tr-level (n)
  5712. "Make N odd if required."
  5713. (if org-odd-levels-only (1+ (/ n 2)) n))
  5714. ;;; Vertical tree motion, cutting and pasting of subtrees
  5715. (defun org-move-subtree-up (&optional arg)
  5716. "Move the current subtree up past ARG headlines of the same level."
  5717. (interactive "p")
  5718. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5719. (defun org-move-subtree-down (&optional arg)
  5720. "Move the current subtree down past ARG headlines of the same level."
  5721. (interactive "p")
  5722. (setq arg (prefix-numeric-value arg))
  5723. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5724. 'org-get-last-sibling))
  5725. (ins-point (make-marker))
  5726. (cnt (abs arg))
  5727. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5728. ;; Select the tree
  5729. (org-back-to-heading)
  5730. (setq beg0 (point))
  5731. (save-excursion
  5732. (setq ne-beg (org-back-over-empty-lines))
  5733. (setq beg (point)))
  5734. (save-match-data
  5735. (save-excursion (outline-end-of-heading)
  5736. (setq folded (org-invisible-p)))
  5737. (outline-end-of-subtree))
  5738. (outline-next-heading)
  5739. (setq ne-end (org-back-over-empty-lines))
  5740. (setq end (point))
  5741. (goto-char beg0)
  5742. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5743. ;; include less whitespace
  5744. (save-excursion
  5745. (goto-char beg)
  5746. (forward-line (- ne-beg ne-end))
  5747. (setq beg (point))))
  5748. ;; Find insertion point, with error handling
  5749. (while (> cnt 0)
  5750. (or (and (funcall movfunc) (looking-at outline-regexp))
  5751. (progn (goto-char beg0)
  5752. (error "Cannot move past superior level or buffer limit")))
  5753. (setq cnt (1- cnt)))
  5754. (if (> arg 0)
  5755. ;; Moving forward - still need to move over subtree
  5756. (progn (org-end-of-subtree t t)
  5757. (save-excursion
  5758. (org-back-over-empty-lines)
  5759. (or (bolp) (newline)))))
  5760. (setq ne-ins (org-back-over-empty-lines))
  5761. (move-marker ins-point (point))
  5762. (setq txt (buffer-substring beg end))
  5763. (org-save-markers-in-region beg end)
  5764. (delete-region beg end)
  5765. (org-remove-empty-overlays-at beg)
  5766. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5767. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5768. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5769. (let ((bbb (point)))
  5770. (insert-before-markers txt)
  5771. (org-reinstall-markers-in-region bbb)
  5772. (move-marker ins-point bbb))
  5773. (or (bolp) (insert "\n"))
  5774. (setq ins-end (point))
  5775. (goto-char ins-point)
  5776. (org-skip-whitespace)
  5777. (when (and (< arg 0)
  5778. (org-first-sibling-p)
  5779. (> ne-ins ne-beg))
  5780. ;; Move whitespace back to beginning
  5781. (save-excursion
  5782. (goto-char ins-end)
  5783. (let ((kill-whole-line t))
  5784. (kill-line (- ne-ins ne-beg)) (point)))
  5785. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5786. (move-marker ins-point nil)
  5787. (if folded
  5788. (hide-subtree)
  5789. (org-show-entry)
  5790. (show-children)
  5791. (org-cycle-hide-drawers 'children))
  5792. (org-clean-visibility-after-subtree-move)))
  5793. (defvar org-subtree-clip ""
  5794. "Clipboard for cut and paste of subtrees.
  5795. This is actually only a copy of the kill, because we use the normal kill
  5796. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5797. (defvar org-subtree-clip-folded nil
  5798. "Was the last copied subtree folded?
  5799. This is used to fold the tree back after pasting.")
  5800. (defun org-cut-subtree (&optional n)
  5801. "Cut the current subtree into the clipboard.
  5802. With prefix arg N, cut this many sequential subtrees.
  5803. This is a short-hand for marking the subtree and then cutting it."
  5804. (interactive "p")
  5805. (org-copy-subtree n 'cut))
  5806. (defun org-copy-subtree (&optional n cut force-store-markers)
  5807. "Cut the current subtree into the clipboard.
  5808. With prefix arg N, cut this many sequential subtrees.
  5809. This is a short-hand for marking the subtree and then copying it.
  5810. If CUT is non-nil, actually cut the subtree.
  5811. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5812. of some markers in the region, even if CUT is non-nil. This is
  5813. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5814. (interactive "p")
  5815. (let (beg end folded (beg0 (point)))
  5816. (if (interactive-p)
  5817. (org-back-to-heading nil) ; take what looks like a subtree
  5818. (org-back-to-heading t)) ; take what is really there
  5819. (org-back-over-empty-lines)
  5820. (setq beg (point))
  5821. (skip-chars-forward " \t\r\n")
  5822. (save-match-data
  5823. (save-excursion (outline-end-of-heading)
  5824. (setq folded (org-invisible-p)))
  5825. (condition-case nil
  5826. (org-forward-same-level (1- n) t)
  5827. (error nil))
  5828. (org-end-of-subtree t t))
  5829. (org-back-over-empty-lines)
  5830. (setq end (point))
  5831. (goto-char beg0)
  5832. (when (> end beg)
  5833. (setq org-subtree-clip-folded folded)
  5834. (when (or cut force-store-markers)
  5835. (org-save-markers-in-region beg end))
  5836. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5837. (setq org-subtree-clip (current-kill 0))
  5838. (message "%s: Subtree(s) with %d characters"
  5839. (if cut "Cut" "Copied")
  5840. (length org-subtree-clip)))))
  5841. (defun org-paste-subtree (&optional level tree for-yank)
  5842. "Paste the clipboard as a subtree, with modification of headline level.
  5843. The entire subtree is promoted or demoted in order to match a new headline
  5844. level.
  5845. If the cursor is at the beginning of a headline, the same level as
  5846. that headline is used to paste the tree
  5847. If not, the new level is derived from the *visible* headings
  5848. before and after the insertion point, and taken to be the inferior headline
  5849. level of the two. So if the previous visible heading is level 3 and the
  5850. next is level 4 (or vice versa), level 4 will be used for insertion.
  5851. This makes sure that the subtree remains an independent subtree and does
  5852. not swallow low level entries.
  5853. You can also force a different level, either by using a numeric prefix
  5854. argument, or by inserting the heading marker by hand. For example, if the
  5855. cursor is after \"*****\", then the tree will be shifted to level 5.
  5856. If optional TREE is given, use this text instead of the kill ring.
  5857. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5858. move back over whitespace before inserting, and move point to the end of
  5859. the inserted text when done."
  5860. (interactive "P")
  5861. (setq tree (or tree (and kill-ring (current-kill 0))))
  5862. (unless (org-kill-is-subtree-p tree)
  5863. (error "%s"
  5864. (substitute-command-keys
  5865. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5866. (let* ((visp (not (org-invisible-p)))
  5867. (txt tree)
  5868. (^re (concat "^\\(" outline-regexp "\\)"))
  5869. (re (concat "\\(" outline-regexp "\\)"))
  5870. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5871. (old-level (if (string-match ^re txt)
  5872. (- (match-end 0) (match-beginning 0) 1)
  5873. -1))
  5874. (force-level (cond (level (prefix-numeric-value level))
  5875. ((and (looking-at "[ \t]*$")
  5876. (string-match
  5877. ^re_ (buffer-substring
  5878. (point-at-bol) (point))))
  5879. (- (match-end 1) (match-beginning 1)))
  5880. ((and (bolp)
  5881. (looking-at org-outline-regexp))
  5882. (- (match-end 0) (point) 1))
  5883. (t nil)))
  5884. (previous-level (save-excursion
  5885. (condition-case nil
  5886. (progn
  5887. (outline-previous-visible-heading 1)
  5888. (if (looking-at re)
  5889. (- (match-end 0) (match-beginning 0) 1)
  5890. 1))
  5891. (error 1))))
  5892. (next-level (save-excursion
  5893. (condition-case nil
  5894. (progn
  5895. (or (looking-at outline-regexp)
  5896. (outline-next-visible-heading 1))
  5897. (if (looking-at re)
  5898. (- (match-end 0) (match-beginning 0) 1)
  5899. 1))
  5900. (error 1))))
  5901. (new-level (or force-level (max previous-level next-level)))
  5902. (shift (if (or (= old-level -1)
  5903. (= new-level -1)
  5904. (= old-level new-level))
  5905. 0
  5906. (- new-level old-level)))
  5907. (delta (if (> shift 0) -1 1))
  5908. (func (if (> shift 0) 'org-demote 'org-promote))
  5909. (org-odd-levels-only nil)
  5910. beg end newend)
  5911. ;; Remove the forced level indicator
  5912. (if force-level
  5913. (delete-region (point-at-bol) (point)))
  5914. ;; Paste
  5915. (beginning-of-line 1)
  5916. (unless for-yank (org-back-over-empty-lines))
  5917. (setq beg (point))
  5918. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5919. (insert-before-markers txt)
  5920. (unless (string-match "\n\\'" txt) (insert "\n"))
  5921. (setq newend (point))
  5922. (org-reinstall-markers-in-region beg)
  5923. (setq end (point))
  5924. (goto-char beg)
  5925. (skip-chars-forward " \t\n\r")
  5926. (setq beg (point))
  5927. (if (and (org-invisible-p) visp)
  5928. (save-excursion (outline-show-heading)))
  5929. ;; Shift if necessary
  5930. (unless (= shift 0)
  5931. (save-restriction
  5932. (narrow-to-region beg end)
  5933. (while (not (= shift 0))
  5934. (org-map-region func (point-min) (point-max))
  5935. (setq shift (+ delta shift)))
  5936. (goto-char (point-min))
  5937. (setq newend (point-max))))
  5938. (when (or (interactive-p) for-yank)
  5939. (message "Clipboard pasted as level %d subtree" new-level))
  5940. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5941. kill-ring
  5942. (eq org-subtree-clip (current-kill 0))
  5943. org-subtree-clip-folded)
  5944. ;; The tree was folded before it was killed/copied
  5945. (hide-subtree))
  5946. (and for-yank (goto-char newend))))
  5947. (defun org-kill-is-subtree-p (&optional txt)
  5948. "Check if the current kill is an outline subtree, or a set of trees.
  5949. Returns nil if kill does not start with a headline, or if the first
  5950. headline level is not the largest headline level in the tree.
  5951. So this will actually accept several entries of equal levels as well,
  5952. which is OK for `org-paste-subtree'.
  5953. If optional TXT is given, check this string instead of the current kill."
  5954. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5955. (start-level (and kill
  5956. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5957. org-outline-regexp "\\)")
  5958. kill)
  5959. (- (match-end 2) (match-beginning 2) 1)))
  5960. (re (concat "^" org-outline-regexp))
  5961. (start (1+ (or (match-beginning 2) -1))))
  5962. (if (not start-level)
  5963. (progn
  5964. nil) ;; does not even start with a heading
  5965. (catch 'exit
  5966. (while (setq start (string-match re kill (1+ start)))
  5967. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5968. (throw 'exit nil)))
  5969. t))))
  5970. (defvar org-markers-to-move nil
  5971. "Markers that should be moved with a cut-and-paste operation.
  5972. Those markers are stored together with their positions relative to
  5973. the start of the region.")
  5974. (defun org-save-markers-in-region (beg end)
  5975. "Check markers in region.
  5976. If these markers are between BEG and END, record their position relative
  5977. to BEG, so that after moving the block of text, we can put the markers back
  5978. into place.
  5979. This function gets called just before an entry or tree gets cut from the
  5980. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5981. called immediately, to move the markers with the entries."
  5982. (setq org-markers-to-move nil)
  5983. (when (featurep 'org-clock)
  5984. (org-clock-save-markers-for-cut-and-paste beg end))
  5985. (when (featurep 'org-agenda)
  5986. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5987. (defun org-check-and-save-marker (marker beg end)
  5988. "Check if MARKER is between BEG and END.
  5989. If yes, remember the marker and the distance to BEG."
  5990. (when (and (marker-buffer marker)
  5991. (equal (marker-buffer marker) (current-buffer)))
  5992. (if (and (>= marker beg) (< marker end))
  5993. (push (cons marker (- marker beg)) org-markers-to-move))))
  5994. (defun org-reinstall-markers-in-region (beg)
  5995. "Move all remembered markers to their position relative to BEG."
  5996. (mapc (lambda (x)
  5997. (move-marker (car x) (+ beg (cdr x))))
  5998. org-markers-to-move)
  5999. (setq org-markers-to-move nil))
  6000. (defun org-narrow-to-subtree ()
  6001. "Narrow buffer to the current subtree."
  6002. (interactive)
  6003. (save-excursion
  6004. (save-match-data
  6005. (narrow-to-region
  6006. (progn (org-back-to-heading t) (point))
  6007. (progn (org-end-of-subtree t t) (point))))))
  6008. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6009. "Clone the task (subtree) at point N times.
  6010. The clones will be inserted as siblings.
  6011. In interactive use, the user will be prompted for the number of clones
  6012. to be produced, and for a time SHIFT, which may be a repeater as used
  6013. in time stamps, for example `+3d'.
  6014. When a valid repeater is given and the entry contains any time stamps,
  6015. the clones will become a sequence in time, with time stamps in the
  6016. subtree shifted for each clone produced. If SHIFT is nil or the
  6017. empty string, time stamps will be left alone.
  6018. If the original subtree did contain time stamps with a repeater,
  6019. the following will happen:
  6020. - the repeater will be removed in each clone
  6021. - an additional clone will be produced, with the current, unshifted
  6022. date(s) in the entry.
  6023. - the original entry will be placed *after* all the clones, with
  6024. repeater intact.
  6025. - the start days in the repeater in the original entry will be shifted
  6026. to past the last clone.
  6027. I this way you can spell out a number of instances of a repeating task,
  6028. and still retain the repeater to cover future instances of the task."
  6029. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6030. (let (beg end template task
  6031. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6032. (if (not (and (integerp n) (> n 0)))
  6033. (error "Invalid number of replications %s" n))
  6034. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6035. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6036. shift)))
  6037. (error "Invalid shift specification %s" shift))
  6038. (when doshift
  6039. (setq shift-n (string-to-number (match-string 1 shift))
  6040. shift-what (cdr (assoc (match-string 2 shift)
  6041. '(("d" . day) ("w" . week)
  6042. ("m" . month) ("y" . year))))))
  6043. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6044. (setq nmin 1 nmax n)
  6045. (org-back-to-heading t)
  6046. (setq beg (point))
  6047. (org-end-of-subtree t t)
  6048. (or (bolp) (insert "\n"))
  6049. (setq end (point))
  6050. (setq template (buffer-substring beg end))
  6051. (when (and doshift
  6052. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6053. (delete-region beg end)
  6054. (setq end beg)
  6055. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6056. (goto-char end)
  6057. (loop for n from nmin to nmax do
  6058. (if (not doshift)
  6059. (setq task template)
  6060. (with-temp-buffer
  6061. (insert template)
  6062. (org-mode)
  6063. (goto-char (point-min))
  6064. (while (re-search-forward org-ts-regexp-both nil t)
  6065. (org-timestamp-change (* n shift-n) shift-what))
  6066. (unless (= n n-no-remove)
  6067. (goto-char (point-min))
  6068. (while (re-search-forward org-ts-regexp nil t)
  6069. (save-excursion
  6070. (goto-char (match-beginning 0))
  6071. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6072. (delete-region (match-beginning 1) (match-end 1))))))
  6073. (setq task (buffer-string))))
  6074. (insert task))
  6075. (goto-char beg)))
  6076. ;;; Outline Sorting
  6077. (defun org-sort (with-case)
  6078. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6079. Optional argument WITH-CASE means sort case-sensitively.
  6080. With a double prefix argument, also remove duplicate entries."
  6081. (interactive "P")
  6082. (if (org-at-table-p)
  6083. (org-call-with-arg 'org-table-sort-lines with-case)
  6084. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6085. (defun org-sort-remove-invisible (s)
  6086. (remove-text-properties 0 (length s) org-rm-props s)
  6087. (while (string-match org-bracket-link-regexp s)
  6088. (setq s (replace-match (if (match-end 2)
  6089. (match-string 3 s)
  6090. (match-string 1 s)) t t s)))
  6091. s)
  6092. (defvar org-priority-regexp) ; defined later in the file
  6093. (defvar org-after-sorting-entries-or-items-hook nil
  6094. "Hook that is run after a bunch of entries or items have been sorted.
  6095. When children are sorted, the cursor is in the parent line when this
  6096. hook gets called. When a region or a plain list is sorted, the cursor
  6097. will be in the first entry of the sorted region/list.")
  6098. (defun org-sort-entries-or-items
  6099. (&optional with-case sorting-type getkey-func compare-func property)
  6100. "Sort entries on a certain level of an outline tree, or plain list items.
  6101. If there is an active region, the entries in the region are sorted.
  6102. Else, if the cursor is before the first entry, sort the top-level items.
  6103. Else, the children of the entry at point are sorted.
  6104. If the cursor is at the first item in a plain list, the list items will be
  6105. sorted.
  6106. Sorting can be alphabetically, numerically, by date/time as given by
  6107. a time stamp, by a property or by priority.
  6108. The command prompts for the sorting type unless it has been given to the
  6109. function through the SORTING-TYPE argument, which needs to a character,
  6110. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6111. precise meaning of each character:
  6112. n Numerically, by converting the beginning of the entry/item to a number.
  6113. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6114. t By date/time, either the first active time stamp in the entry, or, if
  6115. none exist, by the first inactive one.
  6116. In items, only the first line will be checked.
  6117. s By the scheduled date/time.
  6118. d By deadline date/time.
  6119. c By creation time, which is assumed to be the first inactive time stamp
  6120. at the beginning of a line.
  6121. p By priority according to the cookie.
  6122. r By the value of a property.
  6123. Capital letters will reverse the sort order.
  6124. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6125. called with point at the beginning of the record. It must return either
  6126. a string or a number that should serve as the sorting key for that record.
  6127. Comparing entries ignores case by default. However, with an optional argument
  6128. WITH-CASE, the sorting considers case as well."
  6129. (interactive "P")
  6130. (let ((case-func (if with-case 'identity 'downcase))
  6131. start beg end stars re re2
  6132. txt what tmp plain-list-p)
  6133. ;; Find beginning and end of region to sort
  6134. (cond
  6135. ((org-region-active-p)
  6136. ;; we will sort the region
  6137. (setq end (region-end)
  6138. what "region")
  6139. (goto-char (region-beginning))
  6140. (if (not (org-on-heading-p)) (outline-next-heading))
  6141. (setq start (point)))
  6142. ((org-at-item-p)
  6143. ;; we will sort this plain list
  6144. (org-beginning-of-item-list) (setq start (point))
  6145. (org-end-of-item-list) (setq end (point))
  6146. (goto-char start)
  6147. (setq plain-list-p t
  6148. what "plain list"))
  6149. ((or (org-on-heading-p)
  6150. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6151. ;; we will sort the children of the current headline
  6152. (org-back-to-heading)
  6153. (setq start (point)
  6154. end (progn (org-end-of-subtree t t)
  6155. (org-back-over-empty-lines)
  6156. (point))
  6157. what "children")
  6158. (goto-char start)
  6159. (show-subtree)
  6160. (outline-next-heading))
  6161. (t
  6162. ;; we will sort the top-level entries in this file
  6163. (goto-char (point-min))
  6164. (or (org-on-heading-p) (outline-next-heading))
  6165. (setq start (point) end (point-max) what "top-level")
  6166. (goto-char start)
  6167. (show-all)))
  6168. (setq beg (point))
  6169. (if (>= beg end) (error "Nothing to sort"))
  6170. (unless plain-list-p
  6171. (looking-at "\\(\\*+\\)")
  6172. (setq stars (match-string 1)
  6173. re (concat "^" (regexp-quote stars) " +")
  6174. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6175. txt (buffer-substring beg end))
  6176. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6177. (if (and (not (equal stars "*")) (string-match re2 txt))
  6178. (error "Region to sort contains a level above the first entry")))
  6179. (unless sorting-type
  6180. (message
  6181. (if plain-list-p
  6182. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6183. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6184. [t]ime [s]cheduled [d]eadline [c]reated
  6185. A/N/T/S/D/C/P/O/F means reversed:")
  6186. what)
  6187. (setq sorting-type (read-char-exclusive))
  6188. (and (= (downcase sorting-type) ?f)
  6189. (setq getkey-func
  6190. (org-icompleting-read "Sort using function: "
  6191. obarray 'fboundp t nil nil))
  6192. (setq getkey-func (intern getkey-func)))
  6193. (and (= (downcase sorting-type) ?r)
  6194. (setq property
  6195. (org-icompleting-read "Property: "
  6196. (mapcar 'list (org-buffer-property-keys t))
  6197. nil t))))
  6198. (message "Sorting entries...")
  6199. (save-restriction
  6200. (narrow-to-region start end)
  6201. (let ((dcst (downcase sorting-type))
  6202. (case-fold-search nil)
  6203. (now (current-time)))
  6204. (sort-subr
  6205. (/= dcst sorting-type)
  6206. ;; This function moves to the beginning character of the "record" to
  6207. ;; be sorted.
  6208. (if plain-list-p
  6209. (lambda nil
  6210. (if (org-at-item-p) t (goto-char (point-max))))
  6211. (lambda nil
  6212. (if (re-search-forward re nil t)
  6213. (goto-char (match-beginning 0))
  6214. (goto-char (point-max)))))
  6215. ;; This function moves to the last character of the "record" being
  6216. ;; sorted.
  6217. (if plain-list-p
  6218. 'org-end-of-item
  6219. (lambda nil
  6220. (save-match-data
  6221. (condition-case nil
  6222. (outline-forward-same-level 1)
  6223. (error
  6224. (goto-char (point-max)))))))
  6225. ;; This function returns the value that gets sorted against.
  6226. (if plain-list-p
  6227. (lambda nil
  6228. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6229. (cond
  6230. ((= dcst ?n)
  6231. (string-to-number (buffer-substring (match-end 0)
  6232. (point-at-eol))))
  6233. ((= dcst ?a)
  6234. (buffer-substring (match-end 0) (point-at-eol)))
  6235. ((= dcst ?t)
  6236. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6237. (re-search-forward org-ts-regexp-both
  6238. (point-at-eol) t))
  6239. (org-time-string-to-seconds (match-string 0))
  6240. (org-float-time now)))
  6241. ((= dcst ?f)
  6242. (if getkey-func
  6243. (progn
  6244. (setq tmp (funcall getkey-func))
  6245. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6246. tmp)
  6247. (error "Invalid key function `%s'" getkey-func)))
  6248. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6249. (lambda nil
  6250. (cond
  6251. ((= dcst ?n)
  6252. (if (looking-at org-complex-heading-regexp)
  6253. (string-to-number (match-string 4))
  6254. nil))
  6255. ((= dcst ?a)
  6256. (if (looking-at org-complex-heading-regexp)
  6257. (funcall case-func (match-string 4))
  6258. nil))
  6259. ((= dcst ?t)
  6260. (let ((end (save-excursion (outline-next-heading) (point))))
  6261. (if (or (re-search-forward org-ts-regexp end t)
  6262. (re-search-forward org-ts-regexp-both end t))
  6263. (org-time-string-to-seconds (match-string 0))
  6264. (org-float-time now))))
  6265. ((= dcst ?c)
  6266. (let ((end (save-excursion (outline-next-heading) (point))))
  6267. (if (re-search-forward
  6268. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6269. end t)
  6270. (org-time-string-to-seconds (match-string 0))
  6271. (org-float-time now))))
  6272. ((= dcst ?s)
  6273. (let ((end (save-excursion (outline-next-heading) (point))))
  6274. (if (re-search-forward org-scheduled-time-regexp end t)
  6275. (org-time-string-to-seconds (match-string 1))
  6276. (org-float-time now))))
  6277. ((= dcst ?d)
  6278. (let ((end (save-excursion (outline-next-heading) (point))))
  6279. (if (re-search-forward org-deadline-time-regexp end t)
  6280. (org-time-string-to-seconds (match-string 1))
  6281. (org-float-time now))))
  6282. ((= dcst ?p)
  6283. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6284. (string-to-char (match-string 2))
  6285. org-default-priority))
  6286. ((= dcst ?r)
  6287. (or (org-entry-get nil property) ""))
  6288. ((= dcst ?o)
  6289. (if (looking-at org-complex-heading-regexp)
  6290. (- 9999 (length (member (match-string 2)
  6291. org-todo-keywords-1)))))
  6292. ((= dcst ?f)
  6293. (if getkey-func
  6294. (progn
  6295. (setq tmp (funcall getkey-func))
  6296. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6297. tmp)
  6298. (error "Invalid key function `%s'" getkey-func)))
  6299. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6300. nil
  6301. (cond
  6302. ((= dcst ?a) 'string<)
  6303. ((= dcst ?f) compare-func)
  6304. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6305. (t nil)))))
  6306. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6307. (message "Sorting entries...done")))
  6308. (defun org-do-sort (table what &optional with-case sorting-type)
  6309. "Sort TABLE of WHAT according to SORTING-TYPE.
  6310. The user will be prompted for the SORTING-TYPE if the call to this
  6311. function does not specify it. WHAT is only for the prompt, to indicate
  6312. what is being sorted. The sorting key will be extracted from
  6313. the car of the elements of the table.
  6314. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6315. (unless sorting-type
  6316. (message
  6317. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6318. what)
  6319. (setq sorting-type (read-char-exclusive)))
  6320. (let ((dcst (downcase sorting-type))
  6321. extractfun comparefun)
  6322. ;; Define the appropriate functions
  6323. (cond
  6324. ((= dcst ?n)
  6325. (setq extractfun 'string-to-number
  6326. comparefun (if (= dcst sorting-type) '< '>)))
  6327. ((= dcst ?a)
  6328. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6329. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6330. comparefun (if (= dcst sorting-type)
  6331. 'string<
  6332. (lambda (a b) (and (not (string< a b))
  6333. (not (string= a b)))))))
  6334. ((= dcst ?t)
  6335. (setq extractfun
  6336. (lambda (x)
  6337. (if (or (string-match org-ts-regexp x)
  6338. (string-match org-ts-regexp-both x))
  6339. (org-float-time
  6340. (org-time-string-to-time (match-string 0 x)))
  6341. 0))
  6342. comparefun (if (= dcst sorting-type) '< '>)))
  6343. (t (error "Invalid sorting type `%c'" sorting-type)))
  6344. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6345. table)
  6346. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6347. ;;; The orgstruct minor mode
  6348. ;; Define a minor mode which can be used in other modes in order to
  6349. ;; integrate the org-mode structure editing commands.
  6350. ;; This is really a hack, because the org-mode structure commands use
  6351. ;; keys which normally belong to the major mode. Here is how it
  6352. ;; works: The minor mode defines all the keys necessary to operate the
  6353. ;; structure commands, but wraps the commands into a function which
  6354. ;; tests if the cursor is currently at a headline or a plain list
  6355. ;; item. If that is the case, the structure command is used,
  6356. ;; temporarily setting many Org-mode variables like regular
  6357. ;; expressions for filling etc. However, when any of those keys is
  6358. ;; used at a different location, function uses `key-binding' to look
  6359. ;; up if the key has an associated command in another currently active
  6360. ;; keymap (minor modes, major mode, global), and executes that
  6361. ;; command. There might be problems if any of the keys is otherwise
  6362. ;; used as a prefix key.
  6363. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6364. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6365. ;; addresses this by checking explicitly for both bindings.
  6366. (defvar orgstruct-mode-map (make-sparse-keymap)
  6367. "Keymap for the minor `orgstruct-mode'.")
  6368. (defvar org-local-vars nil
  6369. "List of local variables, for use by `orgstruct-mode'")
  6370. ;;;###autoload
  6371. (define-minor-mode orgstruct-mode
  6372. "Toggle the minor more `orgstruct-mode'.
  6373. This mode is for using Org-mode structure commands in other modes.
  6374. The following key behave as if Org-mode was active, if the cursor
  6375. is on a headline, or on a plain list item (both in the definition
  6376. of Org-mode).
  6377. M-up Move entry/item up
  6378. M-down Move entry/item down
  6379. M-left Promote
  6380. M-right Demote
  6381. M-S-up Move entry/item up
  6382. M-S-down Move entry/item down
  6383. M-S-left Promote subtree
  6384. M-S-right Demote subtree
  6385. M-q Fill paragraph and items like in Org-mode
  6386. C-c ^ Sort entries
  6387. C-c - Cycle list bullet
  6388. TAB Cycle item visibility
  6389. M-RET Insert new heading/item
  6390. S-M-RET Insert new TODO heading / Checkbox item
  6391. C-c C-c Set tags / toggle checkbox"
  6392. nil " OrgStruct" nil
  6393. (org-load-modules-maybe)
  6394. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6395. ;;;###autoload
  6396. (defun turn-on-orgstruct ()
  6397. "Unconditionally turn on `orgstruct-mode'."
  6398. (orgstruct-mode 1))
  6399. (defun orgstruct++-mode (&optional arg)
  6400. "Toggle `orgstruct-mode', the enhanced version of it.
  6401. In addition to setting orgstruct-mode, this also exports all indentation
  6402. and autofilling variables from org-mode into the buffer. It will also
  6403. recognize item context in multiline items.
  6404. Note that turning off orgstruct-mode will *not* remove the
  6405. indentation/paragraph settings. This can only be done by refreshing the
  6406. major mode, for example with \\[normal-mode]."
  6407. (interactive "P")
  6408. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6409. (if (< arg 1)
  6410. (orgstruct-mode -1)
  6411. (orgstruct-mode 1)
  6412. (let (var val)
  6413. (mapc
  6414. (lambda (x)
  6415. (when (string-match
  6416. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6417. (symbol-name (car x)))
  6418. (setq var (car x) val (nth 1 x))
  6419. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6420. org-local-vars)
  6421. (org-set-local 'orgstruct-is-++ t))))
  6422. (defvar orgstruct-is-++ nil
  6423. "Is orgstruct-mode in ++ version in the current-buffer?")
  6424. (make-variable-buffer-local 'orgstruct-is-++)
  6425. ;;;###autoload
  6426. (defun turn-on-orgstruct++ ()
  6427. "Unconditionally turn on `orgstruct++-mode'."
  6428. (orgstruct++-mode 1))
  6429. (defun orgstruct-error ()
  6430. "Error when there is no default binding for a structure key."
  6431. (interactive)
  6432. (error "This key has no function outside structure elements"))
  6433. (defun orgstruct-setup ()
  6434. "Setup orgstruct keymaps."
  6435. (let ((nfunc 0)
  6436. (bindings
  6437. (list
  6438. '([(meta up)] org-metaup)
  6439. '([(meta down)] org-metadown)
  6440. '([(meta left)] org-metaleft)
  6441. '([(meta right)] org-metaright)
  6442. '([(meta shift up)] org-shiftmetaup)
  6443. '([(meta shift down)] org-shiftmetadown)
  6444. '([(meta shift left)] org-shiftmetaleft)
  6445. '([(meta shift right)] org-shiftmetaright)
  6446. '([?\e (up)] org-metaup)
  6447. '([?\e (down)] org-metadown)
  6448. '([?\e (left)] org-metaleft)
  6449. '([?\e (right)] org-metaright)
  6450. '([?\e (shift up)] org-shiftmetaup)
  6451. '([?\e (shift down)] org-shiftmetadown)
  6452. '([?\e (shift left)] org-shiftmetaleft)
  6453. '([?\e (shift right)] org-shiftmetaright)
  6454. '([(shift up)] org-shiftup)
  6455. '([(shift down)] org-shiftdown)
  6456. '([(shift left)] org-shiftleft)
  6457. '([(shift right)] org-shiftright)
  6458. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6459. '("\M-q" fill-paragraph)
  6460. '("\C-c^" org-sort)
  6461. '("\C-c-" org-cycle-list-bullet)))
  6462. elt key fun cmd)
  6463. (while (setq elt (pop bindings))
  6464. (setq nfunc (1+ nfunc))
  6465. (setq key (org-key (car elt))
  6466. fun (nth 1 elt)
  6467. cmd (orgstruct-make-binding fun nfunc key))
  6468. (org-defkey orgstruct-mode-map key cmd))
  6469. ;; Special treatment needed for TAB and RET
  6470. (org-defkey orgstruct-mode-map [(tab)]
  6471. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6472. (org-defkey orgstruct-mode-map "\C-i"
  6473. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6474. (org-defkey orgstruct-mode-map "\M-\C-m"
  6475. (orgstruct-make-binding 'org-insert-heading 105
  6476. "\M-\C-m" [(meta return)]))
  6477. (org-defkey orgstruct-mode-map [(meta return)]
  6478. (orgstruct-make-binding 'org-insert-heading 106
  6479. [(meta return)] "\M-\C-m"))
  6480. (org-defkey orgstruct-mode-map [(shift meta return)]
  6481. (orgstruct-make-binding 'org-insert-todo-heading 107
  6482. [(meta return)] "\M-\C-m"))
  6483. (org-defkey orgstruct-mode-map "\e\C-m"
  6484. (orgstruct-make-binding 'org-insert-heading 108
  6485. "\e\C-m" [?\e (return)]))
  6486. (org-defkey orgstruct-mode-map [?\e (return)]
  6487. (orgstruct-make-binding 'org-insert-heading 109
  6488. [?\e (return)] "\e\C-m"))
  6489. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6490. (orgstruct-make-binding 'org-insert-todo-heading 110
  6491. [?\e (return)] "\e\C-m"))
  6492. (unless org-local-vars
  6493. (setq org-local-vars (org-get-local-variables)))
  6494. t))
  6495. (defun orgstruct-make-binding (fun n &rest keys)
  6496. "Create a function for binding in the structure minor mode.
  6497. FUN is the command to call inside a table. N is used to create a unique
  6498. command name. KEYS are keys that should be checked in for a command
  6499. to execute outside of tables."
  6500. (eval
  6501. (list 'defun
  6502. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6503. '(arg)
  6504. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6505. "Outside of structure, run the binding of `"
  6506. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6507. "'.")
  6508. '(interactive "p")
  6509. (list 'if
  6510. `(org-context-p 'headline 'item
  6511. (and orgstruct-is-++
  6512. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6513. 'item-body))
  6514. (list 'org-run-like-in-org-mode (list 'quote fun))
  6515. (list 'let '(orgstruct-mode)
  6516. (list 'call-interactively
  6517. (append '(or)
  6518. (mapcar (lambda (k)
  6519. (list 'key-binding k))
  6520. keys)
  6521. '('orgstruct-error))))))))
  6522. (defun org-context-p (&rest contexts)
  6523. "Check if local context is any of CONTEXTS.
  6524. Possible values in the list of contexts are `table', `headline', and `item'."
  6525. (let ((pos (point)))
  6526. (goto-char (point-at-bol))
  6527. (prog1 (or (and (memq 'table contexts)
  6528. (looking-at "[ \t]*|"))
  6529. (and (memq 'headline contexts)
  6530. ;;????????? (looking-at "\\*+"))
  6531. (looking-at outline-regexp))
  6532. (and (memq 'item contexts)
  6533. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6534. (and (memq 'item-body contexts)
  6535. (org-in-item-p)))
  6536. (goto-char pos))))
  6537. (defun org-get-local-variables ()
  6538. "Return a list of all local variables in an org-mode buffer."
  6539. (let (varlist)
  6540. (with-current-buffer (get-buffer-create "*Org tmp*")
  6541. (erase-buffer)
  6542. (org-mode)
  6543. (setq varlist (buffer-local-variables)))
  6544. (kill-buffer "*Org tmp*")
  6545. (delq nil
  6546. (mapcar
  6547. (lambda (x)
  6548. (setq x
  6549. (if (symbolp x)
  6550. (list x)
  6551. (list (car x) (list 'quote (cdr x)))))
  6552. (if (string-match
  6553. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6554. (symbol-name (car x)))
  6555. x nil))
  6556. varlist))))
  6557. ;;;###autoload
  6558. (defun org-run-like-in-org-mode (cmd)
  6559. "Run a command, pretending that the current buffer is in Org-mode.
  6560. This will temporarily bind local variables that are typically bound in
  6561. Org-mode to the values they have in Org-mode, and then interactively
  6562. call CMD."
  6563. (org-load-modules-maybe)
  6564. (unless org-local-vars
  6565. (setq org-local-vars (org-get-local-variables)))
  6566. (eval (list 'let org-local-vars
  6567. (list 'call-interactively (list 'quote cmd)))))
  6568. ;;;; Archiving
  6569. (defun org-get-category (&optional pos)
  6570. "Get the category applying to position POS."
  6571. (get-text-property (or pos (point)) 'org-category))
  6572. (defun org-refresh-category-properties ()
  6573. "Refresh category text properties in the buffer."
  6574. (let ((def-cat (cond
  6575. ((null org-category)
  6576. (if buffer-file-name
  6577. (file-name-sans-extension
  6578. (file-name-nondirectory buffer-file-name))
  6579. "???"))
  6580. ((symbolp org-category) (symbol-name org-category))
  6581. (t org-category)))
  6582. beg end cat pos optionp)
  6583. (org-unmodified
  6584. (save-excursion
  6585. (save-restriction
  6586. (widen)
  6587. (goto-char (point-min))
  6588. (put-text-property (point) (point-max) 'org-category def-cat)
  6589. (while (re-search-forward
  6590. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6591. (setq pos (match-end 0)
  6592. optionp (equal (char-after (match-beginning 0)) ?#)
  6593. cat (org-trim (match-string 2)))
  6594. (if optionp
  6595. (setq beg (point-at-bol) end (point-max))
  6596. (org-back-to-heading t)
  6597. (setq beg (point) end (org-end-of-subtree t t)))
  6598. (put-text-property beg end 'org-category cat)
  6599. (goto-char pos)))))))
  6600. ;;;; Link Stuff
  6601. ;;; Link abbreviations
  6602. (defun org-link-expand-abbrev (link)
  6603. "Apply replacements as defined in `org-link-abbrev-alist."
  6604. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6605. (let* ((key (match-string 1 link))
  6606. (as (or (assoc key org-link-abbrev-alist-local)
  6607. (assoc key org-link-abbrev-alist)))
  6608. (tag (and (match-end 2) (match-string 3 link)))
  6609. rpl)
  6610. (if (not as)
  6611. link
  6612. (setq rpl (cdr as))
  6613. (cond
  6614. ((symbolp rpl) (funcall rpl tag))
  6615. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6616. ((string-match "%h" rpl)
  6617. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6618. (t (concat rpl tag)))))
  6619. link))
  6620. ;;; Storing and inserting links
  6621. (defvar org-insert-link-history nil
  6622. "Minibuffer history for links inserted with `org-insert-link'.")
  6623. (defvar org-stored-links nil
  6624. "Contains the links stored with `org-store-link'.")
  6625. (defvar org-store-link-plist nil
  6626. "Plist with info about the most recently link created with `org-store-link'.")
  6627. (defvar org-link-protocols nil
  6628. "Link protocols added to Org-mode using `org-add-link-type'.")
  6629. (defvar org-store-link-functions nil
  6630. "List of functions that are called to create and store a link.
  6631. Each function will be called in turn until one returns a non-nil
  6632. value. Each function should check if it is responsible for creating
  6633. this link (for example by looking at the major mode).
  6634. If not, it must exit and return nil.
  6635. If yes, it should return a non-nil value after a calling
  6636. `org-store-link-props' with a list of properties and values.
  6637. Special properties are:
  6638. :type The link prefix. like \"http\". This must be given.
  6639. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6640. This is obligatory as well.
  6641. :description Optional default description for the second pair
  6642. of brackets in an Org-mode link. The user can still change
  6643. this when inserting this link into an Org-mode buffer.
  6644. In addition to these, any additional properties can be specified
  6645. and then used in remember templates.")
  6646. (defun org-add-link-type (type &optional follow export)
  6647. "Add TYPE to the list of `org-link-types'.
  6648. Re-compute all regular expressions depending on `org-link-types'
  6649. FOLLOW and EXPORT are two functions.
  6650. FOLLOW should take the link path as the single argument and do whatever
  6651. is necessary to follow the link, for example find a file or display
  6652. a mail message.
  6653. EXPORT should format the link path for export to one of the export formats.
  6654. It should be a function accepting three arguments:
  6655. path the path of the link, the text after the prefix (like \"http:\")
  6656. desc the description of the link, if any, nil if there was no description
  6657. format the export format, a symbol like `html' or `latex'.
  6658. The function may use the FORMAT information to return different values
  6659. depending on the format. The return value will be put literally into
  6660. the exported file.
  6661. Org-mode has a built-in default for exporting links. If you are happy with
  6662. this default, there is no need to define an export function for the link
  6663. type. For a simple example of an export function, see `org-bbdb.el'."
  6664. (add-to-list 'org-link-types type t)
  6665. (org-make-link-regexps)
  6666. (if (assoc type org-link-protocols)
  6667. (setcdr (assoc type org-link-protocols) (list follow export))
  6668. (push (list type follow export) org-link-protocols)))
  6669. (defvar org-agenda-buffer-name)
  6670. ;;;###autoload
  6671. (defun org-store-link (arg)
  6672. "\\<org-mode-map>Store an org-link to the current location.
  6673. This link is added to `org-stored-links' and can later be inserted
  6674. into an org-buffer with \\[org-insert-link].
  6675. For some link types, a prefix arg is interpreted:
  6676. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6677. For file links, arg negates `org-context-in-file-links'."
  6678. (interactive "P")
  6679. (org-load-modules-maybe)
  6680. (setq org-store-link-plist nil) ; reset
  6681. (let ((outline-regexp (org-get-limited-outline-regexp))
  6682. link cpltxt desc description search txt custom-id)
  6683. (cond
  6684. ((run-hook-with-args-until-success 'org-store-link-functions)
  6685. (setq link (plist-get org-store-link-plist :link)
  6686. desc (or (plist-get org-store-link-plist :description) link)))
  6687. ((equal (buffer-name) "*Org Edit Src Example*")
  6688. (let (label gc)
  6689. (while (or (not label)
  6690. (save-excursion
  6691. (save-restriction
  6692. (widen)
  6693. (goto-char (point-min))
  6694. (re-search-forward
  6695. (regexp-quote (format org-coderef-label-format label))
  6696. nil t))))
  6697. (when label (message "Label exists already") (sit-for 2))
  6698. (setq label (read-string "Code line label: " label)))
  6699. (end-of-line 1)
  6700. (setq link (format org-coderef-label-format label))
  6701. (setq gc (- 79 (length link)))
  6702. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6703. (insert link)
  6704. (setq link (concat "(" label ")") desc nil)))
  6705. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6706. ;; We are in the agenda, link to referenced location
  6707. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6708. (get-text-property (point) 'org-marker))))
  6709. (when m
  6710. (org-with-point-at m
  6711. (call-interactively 'org-store-link)))))
  6712. ((eq major-mode 'calendar-mode)
  6713. (let ((cd (calendar-cursor-to-date)))
  6714. (setq link
  6715. (format-time-string
  6716. (car org-time-stamp-formats)
  6717. (apply 'encode-time
  6718. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6719. nil nil nil))))
  6720. (org-store-link-props :type "calendar" :date cd)))
  6721. ((eq major-mode 'w3-mode)
  6722. (setq cpltxt (if (and (buffer-name)
  6723. (not (string-match "Untitled" (buffer-name))))
  6724. (buffer-name)
  6725. (url-view-url t))
  6726. link (org-make-link (url-view-url t)))
  6727. (org-store-link-props :type "w3" :url (url-view-url t)))
  6728. ((eq major-mode 'w3m-mode)
  6729. (setq cpltxt (or w3m-current-title w3m-current-url)
  6730. link (org-make-link w3m-current-url))
  6731. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6732. ((setq search (run-hook-with-args-until-success
  6733. 'org-create-file-search-functions))
  6734. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6735. "::" search))
  6736. (setq cpltxt (or description link)))
  6737. ((eq major-mode 'image-mode)
  6738. (setq cpltxt (concat "file:"
  6739. (abbreviate-file-name buffer-file-name))
  6740. link (org-make-link cpltxt))
  6741. (org-store-link-props :type "image" :file buffer-file-name))
  6742. ((eq major-mode 'dired-mode)
  6743. ;; link to the file in the current line
  6744. (setq cpltxt (concat "file:"
  6745. (abbreviate-file-name
  6746. (expand-file-name
  6747. (dired-get-filename nil t))))
  6748. link (org-make-link cpltxt)))
  6749. ((and buffer-file-name (org-mode-p))
  6750. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6751. (cond
  6752. ((org-in-regexp "<<\\(.*?\\)>>")
  6753. (setq cpltxt
  6754. (concat "file:"
  6755. (abbreviate-file-name buffer-file-name)
  6756. "::" (match-string 1))
  6757. link (org-make-link cpltxt)))
  6758. ((and (featurep 'org-id)
  6759. (or (eq org-link-to-org-use-id t)
  6760. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6761. (interactive-p))
  6762. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6763. (interactive-p)
  6764. (not custom-id))
  6765. (and org-link-to-org-use-id
  6766. (condition-case nil
  6767. (org-entry-get nil "ID")
  6768. (error nil)))))
  6769. ;; We can make a link using the ID.
  6770. (setq link (condition-case nil
  6771. (prog1 (org-id-store-link)
  6772. (setq desc (plist-get org-store-link-plist
  6773. :description)))
  6774. (error
  6775. ;; probably before first headline, link to file only
  6776. (concat "file:"
  6777. (abbreviate-file-name buffer-file-name))))))
  6778. (t
  6779. ;; Just link to current headline
  6780. (setq cpltxt (concat "file:"
  6781. (abbreviate-file-name buffer-file-name)))
  6782. ;; Add a context search string
  6783. (when (org-xor org-context-in-file-links arg)
  6784. (setq txt (cond
  6785. ((org-on-heading-p) nil)
  6786. ((org-region-active-p)
  6787. (buffer-substring (region-beginning) (region-end)))
  6788. (t nil)))
  6789. (when (or (null txt) (string-match "\\S-" txt))
  6790. (setq cpltxt
  6791. (concat cpltxt "::"
  6792. (condition-case nil
  6793. (org-make-org-heading-search-string txt)
  6794. (error "")))
  6795. desc (or (nth 4 (ignore-errors
  6796. (org-heading-components))) "NONE"))))
  6797. (if (string-match "::\\'" cpltxt)
  6798. (setq cpltxt (substring cpltxt 0 -2)))
  6799. (setq link (org-make-link cpltxt)))))
  6800. ((buffer-file-name (buffer-base-buffer))
  6801. ;; Just link to this file here.
  6802. (setq cpltxt (concat "file:"
  6803. (abbreviate-file-name
  6804. (buffer-file-name (buffer-base-buffer)))))
  6805. ;; Add a context string
  6806. (when (org-xor org-context-in-file-links arg)
  6807. (setq txt (if (org-region-active-p)
  6808. (buffer-substring (region-beginning) (region-end))
  6809. (buffer-substring (point-at-bol) (point-at-eol))))
  6810. ;; Only use search option if there is some text.
  6811. (when (string-match "\\S-" txt)
  6812. (setq cpltxt
  6813. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6814. desc "NONE")))
  6815. (setq link (org-make-link cpltxt)))
  6816. ((interactive-p)
  6817. (error "Cannot link to a buffer which is not visiting a file"))
  6818. (t (setq link nil)))
  6819. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6820. (setq link (or link cpltxt)
  6821. desc (or desc cpltxt))
  6822. (if (equal desc "NONE") (setq desc nil))
  6823. (if (and (or (interactive-p) executing-kbd-macro) link)
  6824. (progn
  6825. (setq org-stored-links
  6826. (cons (list link desc) org-stored-links))
  6827. (message "Stored: %s" (or desc link))
  6828. (when custom-id
  6829. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6830. "::#" custom-id))
  6831. (setq org-stored-links
  6832. (cons (list link desc) org-stored-links))))
  6833. (and link (org-make-link-string link desc)))))
  6834. (defun org-store-link-props (&rest plist)
  6835. "Store link properties, extract names and addresses."
  6836. (let (x adr)
  6837. (when (setq x (plist-get plist :from))
  6838. (setq adr (mail-extract-address-components x))
  6839. (setq plist (plist-put plist :fromname (car adr)))
  6840. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6841. (when (setq x (plist-get plist :to))
  6842. (setq adr (mail-extract-address-components x))
  6843. (setq plist (plist-put plist :toname (car adr)))
  6844. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6845. (let ((from (plist-get plist :from))
  6846. (to (plist-get plist :to)))
  6847. (when (and from to org-from-is-user-regexp)
  6848. (setq plist
  6849. (plist-put plist :fromto
  6850. (if (string-match org-from-is-user-regexp from)
  6851. (concat "to %t")
  6852. (concat "from %f"))))))
  6853. (setq org-store-link-plist plist))
  6854. (defun org-add-link-props (&rest plist)
  6855. "Add these properties to the link property list."
  6856. (let (key value)
  6857. (while plist
  6858. (setq key (pop plist) value (pop plist))
  6859. (setq org-store-link-plist
  6860. (plist-put org-store-link-plist key value)))))
  6861. (defun org-email-link-description (&optional fmt)
  6862. "Return the description part of an email link.
  6863. This takes information from `org-store-link-plist' and formats it
  6864. according to FMT (default from `org-email-link-description-format')."
  6865. (setq fmt (or fmt org-email-link-description-format))
  6866. (let* ((p org-store-link-plist)
  6867. (to (plist-get p :toaddress))
  6868. (from (plist-get p :fromaddress))
  6869. (table
  6870. (list
  6871. (cons "%c" (plist-get p :fromto))
  6872. (cons "%F" (plist-get p :from))
  6873. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6874. (cons "%T" (plist-get p :to))
  6875. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6876. (cons "%s" (plist-get p :subject))
  6877. (cons "%m" (plist-get p :message-id)))))
  6878. (when (string-match "%c" fmt)
  6879. ;; Check if the user wrote this message
  6880. (if (and org-from-is-user-regexp from to
  6881. (save-match-data (string-match org-from-is-user-regexp from)))
  6882. (setq fmt (replace-match "to %t" t t fmt))
  6883. (setq fmt (replace-match "from %f" t t fmt))))
  6884. (org-replace-escapes fmt table)))
  6885. (defun org-make-org-heading-search-string (&optional string heading)
  6886. "Make search string for STRING or current headline."
  6887. (interactive)
  6888. (let ((s (or string (org-get-heading))))
  6889. (unless (and string (not heading))
  6890. ;; We are using a headline, clean up garbage in there.
  6891. (if (string-match org-todo-regexp s)
  6892. (setq s (replace-match "" t t s)))
  6893. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6894. (setq s (replace-match "" t t s)))
  6895. (setq s (org-trim s))
  6896. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6897. org-comment-string "\\)") s)
  6898. (setq s (replace-match "" t t s)))
  6899. (while (string-match org-ts-regexp s)
  6900. (setq s (replace-match "" t t s))))
  6901. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6902. (setq s (replace-match " " t t s)))
  6903. (or string (setq s (concat "*" s))) ; Add * for headlines
  6904. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6905. (defun org-make-link (&rest strings)
  6906. "Concatenate STRINGS."
  6907. (apply 'concat strings))
  6908. (defun org-make-link-string (link &optional description)
  6909. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6910. (unless (string-match "\\S-" link)
  6911. (error "Empty link"))
  6912. (when (stringp description)
  6913. ;; Remove brackets from the description, they are fatal.
  6914. (while (string-match "\\[" description)
  6915. (setq description (replace-match "{" t t description)))
  6916. (while (string-match "\\]" description)
  6917. (setq description (replace-match "}" t t description))))
  6918. (when (equal (org-link-escape link) description)
  6919. ;; No description needed, it is identical
  6920. (setq description nil))
  6921. (when (and (not description)
  6922. (not (equal link (org-link-escape link))))
  6923. (setq description (org-extract-attributes link)))
  6924. (concat "[[" (org-link-escape link) "]"
  6925. (if description (concat "[" description "]") "")
  6926. "]"))
  6927. (defconst org-link-escape-chars
  6928. '((?\ . "%20")
  6929. (?\[ . "%5B")
  6930. (?\] . "%5D")
  6931. (?\340 . "%E0") ; `a
  6932. (?\342 . "%E2") ; ^a
  6933. (?\347 . "%E7") ; ,c
  6934. (?\350 . "%E8") ; `e
  6935. (?\351 . "%E9") ; 'e
  6936. (?\352 . "%EA") ; ^e
  6937. (?\356 . "%EE") ; ^i
  6938. (?\364 . "%F4") ; ^o
  6939. (?\371 . "%F9") ; `u
  6940. (?\373 . "%FB") ; ^u
  6941. (?\; . "%3B")
  6942. (?? . "%3F")
  6943. (?= . "%3D")
  6944. (?+ . "%2B")
  6945. )
  6946. "Association list of escapes for some characters problematic in links.
  6947. This is the list that is used for internal purposes.")
  6948. (defvar org-url-encoding-use-url-hexify nil)
  6949. (defconst org-link-escape-chars-browser
  6950. '((?\ . "%20")) ; 32 for the SPC char
  6951. "Association list of escapes for some characters problematic in links.
  6952. This is the list that is used before handing over to the browser.")
  6953. (defun org-link-escape (text &optional table)
  6954. "Escape characters in TEXT that are problematic for links."
  6955. (if org-url-encoding-use-url-hexify
  6956. (url-hexify-string text)
  6957. (setq table (or table org-link-escape-chars))
  6958. (when text
  6959. (let ((re (mapconcat (lambda (x) (regexp-quote
  6960. (char-to-string (car x))))
  6961. table "\\|")))
  6962. (while (string-match re text)
  6963. (setq text
  6964. (replace-match
  6965. (cdr (assoc (string-to-char (match-string 0 text))
  6966. table))
  6967. t t text)))
  6968. text))))
  6969. (defun org-link-unescape (text &optional table)
  6970. "Reverse the action of `org-link-escape'."
  6971. (if org-url-encoding-use-url-hexify
  6972. (url-unhex-string text)
  6973. (setq table (or table org-link-escape-chars))
  6974. (when text
  6975. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6976. table "\\|")))
  6977. (while (string-match re text)
  6978. (setq text
  6979. (replace-match
  6980. (char-to-string (car (rassoc (match-string 0 text) table)))
  6981. t t text)))
  6982. text))))
  6983. (defun org-xor (a b)
  6984. "Exclusive or."
  6985. (if a (not b) b))
  6986. (defun org-fixup-message-id-for-http (s)
  6987. "Replace special characters in a message id, so it can be used in an http query."
  6988. (while (string-match "<" s)
  6989. (setq s (replace-match "%3C" t t s)))
  6990. (while (string-match ">" s)
  6991. (setq s (replace-match "%3E" t t s)))
  6992. (while (string-match "@" s)
  6993. (setq s (replace-match "%40" t t s)))
  6994. s)
  6995. ;;;###autoload
  6996. (defun org-insert-link-global ()
  6997. "Insert a link like Org-mode does.
  6998. This command can be called in any mode to insert a link in Org-mode syntax."
  6999. (interactive)
  7000. (org-load-modules-maybe)
  7001. (org-run-like-in-org-mode 'org-insert-link))
  7002. (defun org-insert-link (&optional complete-file link-location)
  7003. "Insert a link. At the prompt, enter the link.
  7004. Completion can be used to insert any of the link protocol prefixes like
  7005. http or ftp in use.
  7006. The history can be used to select a link previously stored with
  7007. `org-store-link'. When the empty string is entered (i.e. if you just
  7008. press RET at the prompt), the link defaults to the most recently
  7009. stored link. As SPC triggers completion in the minibuffer, you need to
  7010. use M-SPC or C-q SPC to force the insertion of a space character.
  7011. You will also be prompted for a description, and if one is given, it will
  7012. be displayed in the buffer instead of the link.
  7013. If there is already a link at point, this command will allow you to edit link
  7014. and description parts.
  7015. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7016. be selected using completion. The path to the file will be relative to the
  7017. current directory if the file is in the current directory or a subdirectory.
  7018. Otherwise, the link will be the absolute path as completed in the minibuffer
  7019. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7020. option `org-link-file-path-type'.
  7021. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7022. the current directory or below.
  7023. With three \\[universal-argument] prefixes, negate the meaning of
  7024. `org-keep-stored-link-after-insertion'.
  7025. If `org-make-link-description-function' is non-nil, this function will be
  7026. called with the link target, and the result will be the default
  7027. link description.
  7028. If the LINK-LOCATION parameter is non-nil, this value will be
  7029. used as the link location instead of reading one interactively."
  7030. (interactive "P")
  7031. (let* ((wcf (current-window-configuration))
  7032. (region (if (org-region-active-p)
  7033. (buffer-substring (region-beginning) (region-end))))
  7034. (remove (and region (list (region-beginning) (region-end))))
  7035. (desc region)
  7036. tmphist ; byte-compile incorrectly complains about this
  7037. (link link-location)
  7038. entry file all-prefixes)
  7039. (cond
  7040. (link-location) ; specified by arg, just use it.
  7041. ((org-in-regexp org-bracket-link-regexp 1)
  7042. ;; We do have a link at point, and we are going to edit it.
  7043. (setq remove (list (match-beginning 0) (match-end 0)))
  7044. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7045. (setq link (read-string "Link: "
  7046. (org-link-unescape
  7047. (org-match-string-no-properties 1)))))
  7048. ((or (org-in-regexp org-angle-link-re)
  7049. (org-in-regexp org-plain-link-re))
  7050. ;; Convert to bracket link
  7051. (setq remove (list (match-beginning 0) (match-end 0))
  7052. link (read-string "Link: "
  7053. (org-remove-angle-brackets (match-string 0)))))
  7054. ((member complete-file '((4) (16)))
  7055. ;; Completing read for file names.
  7056. (setq link (org-file-complete-link complete-file)))
  7057. (t
  7058. ;; Read link, with completion for stored links.
  7059. (with-output-to-temp-buffer "*Org Links*"
  7060. (princ "Insert a link.
  7061. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7062. (when org-stored-links
  7063. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7064. (princ (mapconcat
  7065. (lambda (x)
  7066. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7067. (reverse org-stored-links) "\n"))))
  7068. (let ((cw (selected-window)))
  7069. (select-window (get-buffer-window "*Org Links*"))
  7070. (setq truncate-lines t)
  7071. (unless (pos-visible-in-window-p (point-max))
  7072. (org-fit-window-to-buffer))
  7073. (and (window-live-p cw) (select-window cw)))
  7074. ;; Fake a link history, containing the stored links.
  7075. (setq tmphist (append (mapcar 'car org-stored-links)
  7076. org-insert-link-history))
  7077. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7078. (mapcar 'car org-link-abbrev-alist)
  7079. org-link-types))
  7080. (unwind-protect
  7081. (progn
  7082. (setq link
  7083. (let ((org-completion-use-ido nil)
  7084. (org-completion-use-iswitchb nil))
  7085. (org-completing-read
  7086. "Link: "
  7087. (append
  7088. (mapcar (lambda (x) (list (concat x ":")))
  7089. all-prefixes)
  7090. (mapcar 'car org-stored-links))
  7091. nil nil nil
  7092. 'tmphist
  7093. (car (car org-stored-links)))))
  7094. (if (or (member link all-prefixes)
  7095. (and (equal ":" (substring link -1))
  7096. (member (substring link 0 -1) all-prefixes)
  7097. (setq link (substring link 0 -1))))
  7098. (setq link (org-link-try-special-completion link))))
  7099. (set-window-configuration wcf)
  7100. (kill-buffer "*Org Links*"))
  7101. (setq entry (assoc link org-stored-links))
  7102. (or entry (push link org-insert-link-history))
  7103. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7104. (not org-keep-stored-link-after-insertion))
  7105. (setq org-stored-links (delq (assoc link org-stored-links)
  7106. org-stored-links)))
  7107. (setq desc (or desc (nth 1 entry)))))
  7108. (if (string-match org-plain-link-re link)
  7109. ;; URL-like link, normalize the use of angular brackets.
  7110. (setq link (org-make-link (org-remove-angle-brackets link))))
  7111. ;; Check if we are linking to the current file with a search option
  7112. ;; If yes, simplify the link by using only the search option.
  7113. (when (and buffer-file-name
  7114. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7115. (let* ((path (match-string 1 link))
  7116. (case-fold-search nil)
  7117. (search (match-string 2 link)))
  7118. (save-match-data
  7119. (if (equal (file-truename buffer-file-name) (file-truename path))
  7120. ;; We are linking to this same file, with a search option
  7121. (setq link search)))))
  7122. ;; Check if we can/should use a relative path. If yes, simplify the link
  7123. (when (string-match "^file:\\(.*\\)" link)
  7124. (let* ((path (match-string 1 link))
  7125. (origpath path)
  7126. (case-fold-search nil))
  7127. (cond
  7128. ((or (eq org-link-file-path-type 'absolute)
  7129. (equal complete-file '(16)))
  7130. (setq path (abbreviate-file-name (expand-file-name path))))
  7131. ((eq org-link-file-path-type 'noabbrev)
  7132. (setq path (expand-file-name path)))
  7133. ((eq org-link-file-path-type 'relative)
  7134. (setq path (file-relative-name path)))
  7135. (t
  7136. (save-match-data
  7137. (if (string-match (concat "^" (regexp-quote
  7138. (file-name-as-directory
  7139. (expand-file-name "."))))
  7140. (expand-file-name path))
  7141. ;; We are linking a file with relative path name.
  7142. (setq path (substring (expand-file-name path)
  7143. (match-end 0)))
  7144. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7145. (setq link (concat "file:" path))
  7146. (if (equal desc origpath)
  7147. (setq desc path))))
  7148. (if org-make-link-description-function
  7149. (setq desc (funcall org-make-link-description-function link desc)))
  7150. (setq desc (read-string "Description: " desc))
  7151. (unless (string-match "\\S-" desc) (setq desc nil))
  7152. (if remove (apply 'delete-region remove))
  7153. (insert (org-make-link-string link desc))))
  7154. (defun org-link-try-special-completion (type)
  7155. "If there is completion support for link type TYPE, offer it."
  7156. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7157. (if (functionp fun)
  7158. (funcall fun)
  7159. (read-string "Link (no completion support): " (concat type ":")))))
  7160. (defun org-file-complete-link (&optional arg)
  7161. "Create a file link using completion."
  7162. (let (file link)
  7163. (setq file (read-file-name "File: "))
  7164. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7165. (pwd1 (file-name-as-directory (abbreviate-file-name
  7166. (expand-file-name ".")))))
  7167. (cond
  7168. ((equal arg '(16))
  7169. (setq link (org-make-link
  7170. "file:"
  7171. (abbreviate-file-name (expand-file-name file)))))
  7172. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7173. (setq link (org-make-link "file:" (match-string 1 file))))
  7174. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7175. (expand-file-name file))
  7176. (setq link (org-make-link
  7177. "file:" (match-string 1 (expand-file-name file)))))
  7178. (t (setq link (org-make-link "file:" file)))))
  7179. link))
  7180. (defun org-completing-read (&rest args)
  7181. "Completing-read with SPACE being a normal character."
  7182. (let ((minibuffer-local-completion-map
  7183. (copy-keymap minibuffer-local-completion-map)))
  7184. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7185. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7186. (apply 'org-icompleting-read args)))
  7187. (defun org-completing-read-no-i (&rest args)
  7188. (let (org-completion-use-ido org-completion-use-iswitchb)
  7189. (apply 'org-completing-read args)))
  7190. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7191. "Use iswitch as a completing-read replacement to choose from choices.
  7192. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7193. from."
  7194. (let* ((iswitchb-use-virtual-buffers nil)
  7195. (iswitchb-make-buflist-hook
  7196. (lambda ()
  7197. (setq iswitchb-temp-buflist choices))))
  7198. (iswitchb-read-buffer prompt)))
  7199. (defun org-icompleting-read (&rest args)
  7200. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7201. (org-without-partial-completion
  7202. (if (and org-completion-use-ido
  7203. (fboundp 'ido-completing-read)
  7204. (boundp 'ido-mode) ido-mode
  7205. (listp (second args)))
  7206. (let ((ido-enter-matching-directory nil))
  7207. (apply 'ido-completing-read (concat (car args))
  7208. (if (consp (car (nth 1 args)))
  7209. (mapcar (lambda (x) (car x)) (nth 1 args))
  7210. (nth 1 args))
  7211. (cddr args)))
  7212. (if (and org-completion-use-iswitchb
  7213. (boundp 'iswitchb-mode) iswitchb-mode
  7214. (listp (second args)))
  7215. (apply 'org-iswitchb-completing-read (concat (car args))
  7216. (if (consp (car (nth 1 args)))
  7217. (mapcar (lambda (x) (car x)) (nth 1 args))
  7218. (nth 1 args))
  7219. (cddr args))
  7220. (apply 'completing-read args)))))
  7221. (defun org-extract-attributes (s)
  7222. "Extract the attributes cookie from a string and set as text property."
  7223. (let (a attr (start 0) key value)
  7224. (save-match-data
  7225. (when (string-match "{{\\([^}]+\\)}}$" s)
  7226. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7227. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7228. (setq key (match-string 1 a) value (match-string 2 a)
  7229. start (match-end 0)
  7230. attr (plist-put attr (intern key) value))))
  7231. (org-add-props s nil 'org-attr attr))
  7232. s))
  7233. (defun org-extract-attributes-from-string (tag)
  7234. (let (key value attr)
  7235. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7236. (setq key (match-string 1 tag) value (match-string 2 tag)
  7237. tag (replace-match "" t t tag)
  7238. attr (plist-put attr (intern key) value)))
  7239. (cons tag attr)))
  7240. (defun org-attributes-to-string (plist)
  7241. "Format a property list into an HTML attribute list."
  7242. (let ((s "") key value)
  7243. (while plist
  7244. (setq key (pop plist) value (pop plist))
  7245. (and value
  7246. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7247. s))
  7248. ;;; Opening/following a link
  7249. (defvar org-link-search-failed nil)
  7250. (defun org-next-link ()
  7251. "Move forward to the next link.
  7252. If the link is in hidden text, expose it."
  7253. (interactive)
  7254. (when (and org-link-search-failed (eq this-command last-command))
  7255. (goto-char (point-min))
  7256. (message "Link search wrapped back to beginning of buffer"))
  7257. (setq org-link-search-failed nil)
  7258. (let* ((pos (point))
  7259. (ct (org-context))
  7260. (a (assoc :link ct)))
  7261. (if a (goto-char (nth 2 a)))
  7262. (if (re-search-forward org-any-link-re nil t)
  7263. (progn
  7264. (goto-char (match-beginning 0))
  7265. (if (org-invisible-p) (org-show-context)))
  7266. (goto-char pos)
  7267. (setq org-link-search-failed t)
  7268. (error "No further link found"))))
  7269. (defun org-previous-link ()
  7270. "Move backward to the previous link.
  7271. If the link is in hidden text, expose it."
  7272. (interactive)
  7273. (when (and org-link-search-failed (eq this-command last-command))
  7274. (goto-char (point-max))
  7275. (message "Link search wrapped back to end of buffer"))
  7276. (setq org-link-search-failed nil)
  7277. (let* ((pos (point))
  7278. (ct (org-context))
  7279. (a (assoc :link ct)))
  7280. (if a (goto-char (nth 1 a)))
  7281. (if (re-search-backward org-any-link-re nil t)
  7282. (progn
  7283. (goto-char (match-beginning 0))
  7284. (if (org-invisible-p) (org-show-context)))
  7285. (goto-char pos)
  7286. (setq org-link-search-failed t)
  7287. (error "No further link found"))))
  7288. (defun org-translate-link (s)
  7289. "Translate a link string if a translation function has been defined."
  7290. (if (and org-link-translation-function
  7291. (fboundp org-link-translation-function)
  7292. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7293. (progn
  7294. (setq s (funcall org-link-translation-function
  7295. (match-string 1) (match-string 2)))
  7296. (concat (car s) ":" (cdr s)))
  7297. s))
  7298. (defun org-translate-link-from-planner (type path)
  7299. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7300. This is still an experimental function, your mileage may vary."
  7301. (cond
  7302. ((member type '("http" "https" "news" "ftp"))
  7303. ;; standard Internet links are the same.
  7304. nil)
  7305. ((and (equal type "irc") (string-match "^//" path))
  7306. ;; Planner has two / at the beginning of an irc link, we have 1.
  7307. ;; We should have zero, actually....
  7308. (setq path (substring path 1)))
  7309. ((and (equal type "lisp") (string-match "^/" path))
  7310. ;; Planner has a slash, we do not.
  7311. (setq type "elisp" path (substring path 1)))
  7312. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7313. ;; A typical message link. Planner has the id after the final slash,
  7314. ;; we separate it with a hash mark
  7315. (setq path (concat (match-string 1 path) "#"
  7316. (org-remove-angle-brackets (match-string 2 path)))))
  7317. )
  7318. (cons type path))
  7319. (defun org-find-file-at-mouse (ev)
  7320. "Open file link or URL at mouse."
  7321. (interactive "e")
  7322. (mouse-set-point ev)
  7323. (org-open-at-point 'in-emacs))
  7324. (defun org-open-at-mouse (ev)
  7325. "Open file link or URL at mouse."
  7326. (interactive "e")
  7327. (mouse-set-point ev)
  7328. (if (eq major-mode 'org-agenda-mode)
  7329. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7330. (org-open-at-point))
  7331. (defvar org-window-config-before-follow-link nil
  7332. "The window configuration before following a link.
  7333. This is saved in case the need arises to restore it.")
  7334. (defvar org-open-link-marker (make-marker)
  7335. "Marker pointing to the location where `org-open-at-point; was called.")
  7336. ;;;###autoload
  7337. (defun org-open-at-point-global ()
  7338. "Follow a link like Org-mode does.
  7339. This command can be called in any mode to follow a link that has
  7340. Org-mode syntax."
  7341. (interactive)
  7342. (org-run-like-in-org-mode 'org-open-at-point))
  7343. ;;;###autoload
  7344. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7345. "Open a link in the string S, as if it was in Org-mode."
  7346. (interactive "sLink: \nP")
  7347. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7348. (with-temp-buffer
  7349. (let ((org-inhibit-startup t))
  7350. (org-mode)
  7351. (insert s)
  7352. (goto-char (point-min))
  7353. (org-open-at-point arg reference-buffer)))))
  7354. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7355. "Open link at or after point.
  7356. If there is no link at point, this function will search forward up to
  7357. the end of the current line.
  7358. Normally, files will be opened by an appropriate application. If the
  7359. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7360. With a double prefix argument, try to open outside of Emacs, in the
  7361. application the system uses for this file type."
  7362. (interactive "P")
  7363. (org-load-modules-maybe)
  7364. (move-marker org-open-link-marker (point))
  7365. (setq org-window-config-before-follow-link (current-window-configuration))
  7366. (org-remove-occur-highlights nil nil t)
  7367. (cond
  7368. ((and (org-on-heading-p)
  7369. (not (org-in-regexp
  7370. (concat org-plain-link-re "\\|"
  7371. org-bracket-link-regexp "\\|"
  7372. org-angle-link-re "\\|"
  7373. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7374. (or (org-offer-links-in-entry in-emacs)
  7375. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7376. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7377. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7378. (org-footnote-action))
  7379. (t
  7380. (let (type path link line search (pos (point)))
  7381. (catch 'match
  7382. (save-excursion
  7383. (skip-chars-forward "^]\n\r")
  7384. (when (org-in-regexp org-bracket-link-regexp)
  7385. (setq link (org-extract-attributes
  7386. (org-link-unescape (org-match-string-no-properties 1))))
  7387. (while (string-match " *\n *" link)
  7388. (setq link (replace-match " " t t link)))
  7389. (setq link (org-link-expand-abbrev link))
  7390. (cond
  7391. ((or (file-name-absolute-p link)
  7392. (string-match "^\\.\\.?/" link))
  7393. (setq type "file" path link))
  7394. ((string-match org-link-re-with-space3 link)
  7395. (setq type (match-string 1 link) path (match-string 2 link)))
  7396. (t (setq type "thisfile" path link)))
  7397. (throw 'match t)))
  7398. (when (get-text-property (point) 'org-linked-text)
  7399. (setq type "thisfile"
  7400. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7401. (1+ (point)) (point))
  7402. path (buffer-substring
  7403. (previous-single-property-change pos 'org-linked-text)
  7404. (next-single-property-change pos 'org-linked-text)))
  7405. (throw 'match t))
  7406. (save-excursion
  7407. (when (or (org-in-regexp org-angle-link-re)
  7408. (org-in-regexp org-plain-link-re))
  7409. (setq type (match-string 1) path (match-string 2))
  7410. (throw 'match t)))
  7411. (save-excursion
  7412. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7413. (setq type "tags"
  7414. path (match-string 1))
  7415. (while (string-match ":" path)
  7416. (setq path (replace-match "+" t t path)))
  7417. (throw 'match t)))
  7418. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7419. (setq type "tree-match"
  7420. path (match-string 1))
  7421. (throw 'match t)))
  7422. (unless path
  7423. (error "No link found"))
  7424. ;; switch back to reference buffer
  7425. ;; needed when if called in a temporary buffer through
  7426. ;; org-open-link-from-string
  7427. (with-current-buffer (or reference-buffer (current-buffer))
  7428. ;; Remove any trailing spaces in path
  7429. (if (string-match " +\\'" path)
  7430. (setq path (replace-match "" t t path)))
  7431. (if (and org-link-translation-function
  7432. (fboundp org-link-translation-function))
  7433. ;; Check if we need to translate the link
  7434. (let ((tmp (funcall org-link-translation-function type path)))
  7435. (setq type (car tmp) path (cdr tmp))))
  7436. (cond
  7437. ((assoc type org-link-protocols)
  7438. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7439. ((equal type "mailto")
  7440. (let ((cmd (car org-link-mailto-program))
  7441. (args (cdr org-link-mailto-program)) args1
  7442. (address path) (subject "") a)
  7443. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7444. (setq address (match-string 1 path)
  7445. subject (org-link-escape (match-string 2 path))))
  7446. (while args
  7447. (cond
  7448. ((not (stringp (car args))) (push (pop args) args1))
  7449. (t (setq a (pop args))
  7450. (if (string-match "%a" a)
  7451. (setq a (replace-match address t t a)))
  7452. (if (string-match "%s" a)
  7453. (setq a (replace-match subject t t a)))
  7454. (push a args1))))
  7455. (apply cmd (nreverse args1))))
  7456. ((member type '("http" "https" "ftp" "news"))
  7457. (browse-url (concat type ":" (org-link-escape
  7458. path org-link-escape-chars-browser))))
  7459. ((member type '("message"))
  7460. (browse-url (concat type ":" path)))
  7461. ((string= type "tags")
  7462. (org-tags-view in-emacs path))
  7463. ((string= type "thisfile")
  7464. (if in-emacs
  7465. (switch-to-buffer-other-window
  7466. (org-get-buffer-for-internal-link (current-buffer)))
  7467. (org-mark-ring-push))
  7468. (let ((cmd `(org-link-search
  7469. ,path
  7470. ,(cond ((equal in-emacs '(4)) 'occur)
  7471. ((equal in-emacs '(16)) 'org-occur)
  7472. (t nil))
  7473. ,pos)))
  7474. (condition-case nil (eval cmd)
  7475. (error (progn (widen) (eval cmd))))))
  7476. ((string= type "tree-match")
  7477. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7478. ((string= type "file")
  7479. (if (string-match "::\\([0-9]+\\)\\'" path)
  7480. (setq line (string-to-number (match-string 1 path))
  7481. path (substring path 0 (match-beginning 0)))
  7482. (if (string-match "::\\(.+\\)\\'" path)
  7483. (setq search (match-string 1 path)
  7484. path (substring path 0 (match-beginning 0)))))
  7485. (if (string-match "[*?{]" (file-name-nondirectory path))
  7486. (dired path)
  7487. (org-open-file path in-emacs line search)))
  7488. ((string= type "news")
  7489. (require 'org-gnus)
  7490. (org-gnus-follow-link path))
  7491. ((string= type "shell")
  7492. (let ((cmd path))
  7493. (if (or (not org-confirm-shell-link-function)
  7494. (funcall org-confirm-shell-link-function
  7495. (format "Execute \"%s\" in shell? "
  7496. (org-add-props cmd nil
  7497. 'face 'org-warning))))
  7498. (progn
  7499. (message "Executing %s" cmd)
  7500. (shell-command cmd))
  7501. (error "Abort"))))
  7502. ((string= type "elisp")
  7503. (let ((cmd path))
  7504. (if (or (not org-confirm-elisp-link-function)
  7505. (funcall org-confirm-elisp-link-function
  7506. (format "Execute \"%s\" as elisp? "
  7507. (org-add-props cmd nil
  7508. 'face 'org-warning))))
  7509. (message "%s => %s" cmd
  7510. (if (equal (string-to-char cmd) ?\()
  7511. (eval (read cmd))
  7512. (call-interactively (read cmd))))
  7513. (error "Abort"))))
  7514. (t
  7515. (browse-url-at-point)))))))
  7516. (move-marker org-open-link-marker nil)
  7517. (run-hook-with-args 'org-follow-link-hook))
  7518. (defun org-offer-links-in-entry (&optional nth zero)
  7519. "Offer links in the current entry and follow the selected link.
  7520. If there is only one link, follow it immediately as well.
  7521. If NTH is an integer, immediately pick the NTH link found.
  7522. If ZERO is a string, check also this string for a link, and if
  7523. there is one, offer it as link number zero."
  7524. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7525. "\\(" org-angle-link-re "\\)\\|"
  7526. "\\(" org-plain-link-re "\\)"))
  7527. (cnt ?0)
  7528. (in-emacs (if (integerp nth) nil nth))
  7529. have-zero end links link c)
  7530. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7531. (push (match-string 0 zero) links)
  7532. (setq cnt (1- cnt) have-zero t))
  7533. (save-excursion
  7534. (org-back-to-heading t)
  7535. (setq end (save-excursion (outline-next-heading) (point)))
  7536. (while (re-search-forward re end t)
  7537. (push (match-string 0) links))
  7538. (setq links (org-uniquify (reverse links))))
  7539. (cond
  7540. ((null links)
  7541. (message "No links"))
  7542. ((equal (length links) 1)
  7543. (setq link (car links)))
  7544. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7545. (setq link (nth (if have-zero nth (1- nth)) links)))
  7546. (t ; we have to select a link
  7547. (save-excursion
  7548. (save-window-excursion
  7549. (delete-other-windows)
  7550. (with-output-to-temp-buffer "*Select Link*"
  7551. (mapc (lambda (l)
  7552. (if (not (string-match org-bracket-link-regexp l))
  7553. (princ (format "[%c] %s\n" (incf cnt)
  7554. (org-remove-angle-brackets l)))
  7555. (if (match-end 3)
  7556. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7557. (match-string 3 l) (match-string 1 l)))
  7558. (princ (format "[%c] %s\n" (incf cnt)
  7559. (match-string 1 l))))))
  7560. links))
  7561. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7562. (message "Select link to open:")
  7563. (setq c (read-char-exclusive))
  7564. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7565. (when (equal c ?q) (error "Abort"))
  7566. (setq nth (- c ?0))
  7567. (if have-zero (setq nth (1+ nth)))
  7568. (unless (and (integerp nth) (>= (length links) nth))
  7569. (error "Invalid link selection"))
  7570. (setq link (nth (1- nth) links))))
  7571. (if link
  7572. (progn (org-open-link-from-string link in-emacs (current-buffer)) t)
  7573. nil)))
  7574. ;;;; Time estimates
  7575. (defun org-get-effort (&optional pom)
  7576. "Get the effort estimate for the current entry."
  7577. (org-entry-get pom org-effort-property))
  7578. ;;; File search
  7579. (defvar org-create-file-search-functions nil
  7580. "List of functions to construct the right search string for a file link.
  7581. These functions are called in turn with point at the location to
  7582. which the link should point.
  7583. A function in the hook should first test if it would like to
  7584. handle this file type, for example by checking the major-mode or
  7585. the file extension. If it decides not to handle this file, it
  7586. should just return nil to give other functions a chance. If it
  7587. does handle the file, it must return the search string to be used
  7588. when following the link. The search string will be part of the
  7589. file link, given after a double colon, and `org-open-at-point'
  7590. will automatically search for it. If special measures must be
  7591. taken to make the search successful, another function should be
  7592. added to the companion hook `org-execute-file-search-functions',
  7593. which see.
  7594. A function in this hook may also use `setq' to set the variable
  7595. `description' to provide a suggestion for the descriptive text to
  7596. be used for this link when it gets inserted into an Org-mode
  7597. buffer with \\[org-insert-link].")
  7598. (defvar org-execute-file-search-functions nil
  7599. "List of functions to execute a file search triggered by a link.
  7600. Functions added to this hook must accept a single argument, the
  7601. search string that was part of the file link, the part after the
  7602. double colon. The function must first check if it would like to
  7603. handle this search, for example by checking the major-mode or the
  7604. file extension. If it decides not to handle this search, it
  7605. should just return nil to give other functions a chance. If it
  7606. does handle the search, it must return a non-nil value to keep
  7607. other functions from trying.
  7608. Each function can access the current prefix argument through the
  7609. variable `current-prefix-argument'. Note that a single prefix is
  7610. used to force opening a link in Emacs, so it may be good to only
  7611. use a numeric or double prefix to guide the search function.
  7612. In case this is needed, a function in this hook can also restore
  7613. the window configuration before `org-open-at-point' was called using:
  7614. (set-window-configuration org-window-config-before-follow-link)")
  7615. (defun org-link-search (s &optional type avoid-pos)
  7616. "Search for a link search option.
  7617. If S is surrounded by forward slashes, it is interpreted as a
  7618. regular expression. In org-mode files, this will create an `org-occur'
  7619. sparse tree. In ordinary files, `occur' will be used to list matches.
  7620. If the current buffer is in `dired-mode', grep will be used to search
  7621. in all files. If AVOID-POS is given, ignore matches near that position."
  7622. (let ((case-fold-search t)
  7623. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7624. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7625. (append '(("") (" ") ("\t") ("\n"))
  7626. org-emphasis-alist)
  7627. "\\|") "\\)"))
  7628. (pos (point))
  7629. (pre nil) (post nil)
  7630. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7631. (cond
  7632. ;; First check if there are any special
  7633. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7634. ;; Now try the builtin stuff
  7635. ((and (equal (string-to-char s0) ?#)
  7636. (> (length s0) 1)
  7637. (save-excursion
  7638. (goto-char (point-min))
  7639. (and
  7640. (re-search-forward
  7641. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7642. (setq type 'dedicated
  7643. pos (match-beginning 0))))
  7644. ;; There is an exact target for this
  7645. (goto-char pos)
  7646. (org-back-to-heading t)))
  7647. ((save-excursion
  7648. (goto-char (point-min))
  7649. (and
  7650. (re-search-forward
  7651. (concat "<<" (regexp-quote s0) ">>") nil t)
  7652. (setq type 'dedicated
  7653. pos (match-beginning 0))))
  7654. ;; There is an exact target for this
  7655. (goto-char pos))
  7656. ((and (string-match "^(\\(.*\\))$" s0)
  7657. (save-excursion
  7658. (goto-char (point-min))
  7659. (and
  7660. (re-search-forward
  7661. (concat "[^[]" (regexp-quote
  7662. (format org-coderef-label-format
  7663. (match-string 1 s0))))
  7664. nil t)
  7665. (setq type 'dedicated
  7666. pos (1+ (match-beginning 0))))))
  7667. ;; There is a coderef target for this
  7668. (goto-char pos))
  7669. ((string-match "^/\\(.*\\)/$" s)
  7670. ;; A regular expression
  7671. (cond
  7672. ((org-mode-p)
  7673. (org-occur (match-string 1 s)))
  7674. ;;((eq major-mode 'dired-mode)
  7675. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7676. (t (org-do-occur (match-string 1 s)))))
  7677. (t
  7678. ;; A normal search strings
  7679. (when (equal (string-to-char s) ?*)
  7680. ;; Anchor on headlines, post may include tags.
  7681. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7682. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7683. s (substring s 1)))
  7684. (remove-text-properties
  7685. 0 (length s)
  7686. '(face nil mouse-face nil keymap nil fontified nil) s)
  7687. ;; Make a series of regular expressions to find a match
  7688. (setq words (org-split-string s "[ \n\r\t]+")
  7689. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7690. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7691. "\\)" markers)
  7692. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7693. re2a (concat "[ \t\r\n]" re2a_)
  7694. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7695. re4 (concat "[^a-zA-Z_]" re4_)
  7696. re1 (concat pre re2 post)
  7697. re3 (concat pre (if pre re4_ re4) post)
  7698. re5 (concat pre ".*" re4)
  7699. re2 (concat pre re2)
  7700. re2a (concat pre (if pre re2a_ re2a))
  7701. re4 (concat pre (if pre re4_ re4))
  7702. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7703. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7704. re5 "\\)"
  7705. ))
  7706. (cond
  7707. ((eq type 'org-occur) (org-occur reall))
  7708. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7709. (t (goto-char (point-min))
  7710. (setq type 'fuzzy)
  7711. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7712. (org-search-not-self 1 re1 nil t)
  7713. (org-search-not-self 1 re2 nil t)
  7714. (org-search-not-self 1 re2a nil t)
  7715. (org-search-not-self 1 re3 nil t)
  7716. (org-search-not-self 1 re4 nil t)
  7717. (org-search-not-self 1 re5 nil t)
  7718. )
  7719. (goto-char (match-beginning 1))
  7720. (goto-char pos)
  7721. (error "No match")))))
  7722. (t
  7723. ;; Normal string-search
  7724. (goto-char (point-min))
  7725. (if (search-forward s nil t)
  7726. (goto-char (match-beginning 0))
  7727. (error "No match"))))
  7728. (and (org-mode-p) (org-show-context 'link-search))
  7729. type))
  7730. (defun org-search-not-self (group &rest args)
  7731. "Execute `re-search-forward', but only accept matches that do not
  7732. enclose the position of `org-open-link-marker'."
  7733. (let ((m org-open-link-marker))
  7734. (catch 'exit
  7735. (while (apply 're-search-forward args)
  7736. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7737. (goto-char (match-end group))
  7738. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7739. (> (match-beginning 0) (marker-position m))
  7740. (< (match-end 0) (marker-position m)))
  7741. (save-match-data
  7742. (or (not (org-in-regexp
  7743. org-bracket-link-analytic-regexp 1))
  7744. (not (match-end 4)) ; no description
  7745. (and (<= (match-beginning 4) (point))
  7746. (>= (match-end 4) (point))))))
  7747. (throw 'exit (point))))))))
  7748. (defun org-get-buffer-for-internal-link (buffer)
  7749. "Return a buffer to be used for displaying the link target of internal links."
  7750. (cond
  7751. ((not org-display-internal-link-with-indirect-buffer)
  7752. buffer)
  7753. ((string-match "(Clone)$" (buffer-name buffer))
  7754. (message "Buffer is already a clone, not making another one")
  7755. ;; we also do not modify visibility in this case
  7756. buffer)
  7757. (t ; make a new indirect buffer for displaying the link
  7758. (let* ((bn (buffer-name buffer))
  7759. (ibn (concat bn "(Clone)"))
  7760. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7761. (with-current-buffer ib (org-overview))
  7762. ib))))
  7763. (defun org-do-occur (regexp &optional cleanup)
  7764. "Call the Emacs command `occur'.
  7765. If CLEANUP is non-nil, remove the printout of the regular expression
  7766. in the *Occur* buffer. This is useful if the regex is long and not useful
  7767. to read."
  7768. (occur regexp)
  7769. (when cleanup
  7770. (let ((cwin (selected-window)) win beg end)
  7771. (when (setq win (get-buffer-window "*Occur*"))
  7772. (select-window win))
  7773. (goto-char (point-min))
  7774. (when (re-search-forward "match[a-z]+" nil t)
  7775. (setq beg (match-end 0))
  7776. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7777. (setq end (1- (match-beginning 0)))))
  7778. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7779. (goto-char (point-min))
  7780. (select-window cwin))))
  7781. ;;; The mark ring for links jumps
  7782. (defvar org-mark-ring nil
  7783. "Mark ring for positions before jumps in Org-mode.")
  7784. (defvar org-mark-ring-last-goto nil
  7785. "Last position in the mark ring used to go back.")
  7786. ;; Fill and close the ring
  7787. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7788. (loop for i from 1 to org-mark-ring-length do
  7789. (push (make-marker) org-mark-ring))
  7790. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7791. org-mark-ring)
  7792. (defun org-mark-ring-push (&optional pos buffer)
  7793. "Put the current position or POS into the mark ring and rotate it."
  7794. (interactive)
  7795. (setq pos (or pos (point)))
  7796. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7797. (move-marker (car org-mark-ring)
  7798. (or pos (point))
  7799. (or buffer (current-buffer)))
  7800. (message "%s"
  7801. (substitute-command-keys
  7802. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7803. (defun org-mark-ring-goto (&optional n)
  7804. "Jump to the previous position in the mark ring.
  7805. With prefix arg N, jump back that many stored positions. When
  7806. called several times in succession, walk through the entire ring.
  7807. Org-mode commands jumping to a different position in the current file,
  7808. or to another Org-mode file, automatically push the old position
  7809. onto the ring."
  7810. (interactive "p")
  7811. (let (p m)
  7812. (if (eq last-command this-command)
  7813. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7814. (setq p org-mark-ring))
  7815. (setq org-mark-ring-last-goto p)
  7816. (setq m (car p))
  7817. (switch-to-buffer (marker-buffer m))
  7818. (goto-char m)
  7819. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7820. (defun org-remove-angle-brackets (s)
  7821. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7822. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7823. s)
  7824. (defun org-add-angle-brackets (s)
  7825. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7826. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7827. s)
  7828. (defun org-remove-double-quotes (s)
  7829. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7830. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7831. s)
  7832. ;;; Following specific links
  7833. (defun org-follow-timestamp-link ()
  7834. (cond
  7835. ((org-at-date-range-p t)
  7836. (let ((org-agenda-start-on-weekday)
  7837. (t1 (match-string 1))
  7838. (t2 (match-string 2)))
  7839. (setq t1 (time-to-days (org-time-string-to-time t1))
  7840. t2 (time-to-days (org-time-string-to-time t2)))
  7841. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7842. ((org-at-timestamp-p t)
  7843. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7844. (substring (match-string 1) 0 10)))
  7845. 1))
  7846. (t (error "This should not happen"))))
  7847. ;;; Following file links
  7848. (defvar org-wait nil)
  7849. (defun org-open-file (path &optional in-emacs line search)
  7850. "Open the file at PATH.
  7851. First, this expands any special file name abbreviations. Then the
  7852. configuration variable `org-file-apps' is checked if it contains an
  7853. entry for this file type, and if yes, the corresponding command is launched.
  7854. If no application is found, Emacs simply visits the file.
  7855. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7856. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7857. and o use an external application to visit the file.
  7858. Optional LINE specifies a line to go to, optional SEARCH a string to
  7859. search for. If LINE or SEARCH is given, the file will always be
  7860. opened in Emacs.
  7861. If the file does not exist, an error is thrown."
  7862. (setq in-emacs (or in-emacs line search))
  7863. (let* ((file (if (equal path "")
  7864. buffer-file-name
  7865. (substitute-in-file-name (expand-file-name path))))
  7866. (apps (append org-file-apps (org-default-apps)))
  7867. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7868. (dirp (if remp nil (file-directory-p file)))
  7869. (file (if (and dirp org-open-directory-means-index-dot-org)
  7870. (concat (file-name-as-directory file) "index.org")
  7871. file))
  7872. (a-m-a-p (assq 'auto-mode apps))
  7873. (dfile (downcase file))
  7874. (old-buffer (current-buffer))
  7875. (old-pos (point))
  7876. (old-mode major-mode)
  7877. ext cmd)
  7878. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7879. (setq ext (match-string 1 dfile))
  7880. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7881. (setq ext (match-string 1 dfile))))
  7882. (cond
  7883. ((equal in-emacs '(16))
  7884. (setq cmd (cdr (assoc 'system apps))))
  7885. (in-emacs (setq cmd 'emacs))
  7886. (t
  7887. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7888. (and dirp (cdr (assoc 'directory apps)))
  7889. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7890. 'string-match)
  7891. (cdr (assoc ext apps))
  7892. (cdr (assoc t apps))))))
  7893. (when (eq cmd 'system)
  7894. (setq cmd (cdr (assoc 'system apps))))
  7895. (when (eq cmd 'default)
  7896. (setq cmd (cdr (assoc t apps))))
  7897. (when (eq cmd 'mailcap)
  7898. (require 'mailcap)
  7899. (mailcap-parse-mailcaps)
  7900. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7901. (command (mailcap-mime-info mime-type)))
  7902. (if (stringp command)
  7903. (setq cmd command)
  7904. (setq cmd 'emacs))))
  7905. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7906. (not (file-exists-p file))
  7907. (not org-open-non-existing-files))
  7908. (error "No such file: %s" file))
  7909. (cond
  7910. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7911. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7912. (while (string-match "['\"]%s['\"]" cmd)
  7913. (setq cmd (replace-match "%s" t t cmd)))
  7914. (while (string-match "%s" cmd)
  7915. (setq cmd (replace-match
  7916. (save-match-data
  7917. (shell-quote-argument
  7918. (convert-standard-filename file)))
  7919. t t cmd)))
  7920. (save-window-excursion
  7921. (start-process-shell-command cmd nil cmd)
  7922. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7923. ))
  7924. ((or (stringp cmd)
  7925. (eq cmd 'emacs))
  7926. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7927. (widen)
  7928. (if line (org-goto-line line)
  7929. (if search (org-link-search search))))
  7930. ((consp cmd)
  7931. (let ((file (convert-standard-filename file)))
  7932. (eval cmd)))
  7933. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7934. (and (org-mode-p) (eq old-mode 'org-mode)
  7935. (or (not (equal old-buffer (current-buffer)))
  7936. (not (equal old-pos (point))))
  7937. (org-mark-ring-push old-pos old-buffer))))
  7938. (defun org-default-apps ()
  7939. "Return the default applications for this operating system."
  7940. (cond
  7941. ((eq system-type 'darwin)
  7942. org-file-apps-defaults-macosx)
  7943. ((eq system-type 'windows-nt)
  7944. org-file-apps-defaults-windowsnt)
  7945. (t org-file-apps-defaults-gnu)))
  7946. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7947. "Convert extensions to regular expressions in the cars of LIST.
  7948. Also, weed out any non-string entries, because the return value is used
  7949. only for regexp matching.
  7950. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7951. point to the symbol `emacs', indicating that the file should
  7952. be opened in Emacs."
  7953. (append
  7954. (delq nil
  7955. (mapcar (lambda (x)
  7956. (if (not (stringp (car x)))
  7957. nil
  7958. (if (string-match "\\W" (car x))
  7959. x
  7960. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7961. list))
  7962. (if add-auto-mode
  7963. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7964. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7965. (defun org-file-remote-p (file)
  7966. "Test whether FILE specifies a location on a remote system.
  7967. Return non-nil if the location is indeed remote.
  7968. For example, the filename \"/user@host:/foo\" specifies a location
  7969. on the system \"/user@host:\"."
  7970. (cond ((fboundp 'file-remote-p)
  7971. (file-remote-p file))
  7972. ((fboundp 'tramp-handle-file-remote-p)
  7973. (tramp-handle-file-remote-p file))
  7974. ((and (boundp 'ange-ftp-name-format)
  7975. (string-match (car ange-ftp-name-format) file))
  7976. t)
  7977. (t nil)))
  7978. ;;;; Refiling
  7979. (defun org-get-org-file ()
  7980. "Read a filename, with default directory `org-directory'."
  7981. (let ((default (or org-default-notes-file remember-data-file)))
  7982. (read-file-name (format "File name [%s]: " default)
  7983. (file-name-as-directory org-directory)
  7984. default)))
  7985. (defun org-notes-order-reversed-p ()
  7986. "Check if the current file should receive notes in reversed order."
  7987. (cond
  7988. ((not org-reverse-note-order) nil)
  7989. ((eq t org-reverse-note-order) t)
  7990. ((not (listp org-reverse-note-order)) nil)
  7991. (t (catch 'exit
  7992. (let ((all org-reverse-note-order)
  7993. entry)
  7994. (while (setq entry (pop all))
  7995. (if (string-match (car entry) buffer-file-name)
  7996. (throw 'exit (cdr entry))))
  7997. nil)))))
  7998. (defvar org-refile-target-table nil
  7999. "The list of refile targets, created by `org-refile'.")
  8000. (defvar org-agenda-new-buffers nil
  8001. "Buffers created to visit agenda files.")
  8002. (defun org-get-refile-targets (&optional default-buffer)
  8003. "Produce a table with refile targets."
  8004. (let ((case-fold-search nil)
  8005. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8006. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8007. targets txt re files f desc descre fast-path-p level pos0)
  8008. (message "Getting targets...")
  8009. (with-current-buffer (or default-buffer (current-buffer))
  8010. (while (setq entry (pop entries))
  8011. (setq files (car entry) desc (cdr entry))
  8012. (setq fast-path-p nil)
  8013. (cond
  8014. ((null files) (setq files (list (current-buffer))))
  8015. ((eq files 'org-agenda-files)
  8016. (setq files (org-agenda-files 'unrestricted)))
  8017. ((and (symbolp files) (fboundp files))
  8018. (setq files (funcall files)))
  8019. ((and (symbolp files) (boundp files))
  8020. (setq files (symbol-value files))))
  8021. (if (stringp files) (setq files (list files)))
  8022. (cond
  8023. ((eq (car desc) :tag)
  8024. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8025. ((eq (car desc) :todo)
  8026. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8027. ((eq (car desc) :regexp)
  8028. (setq descre (cdr desc)))
  8029. ((eq (car desc) :level)
  8030. (setq descre (concat "^\\*\\{" (number-to-string
  8031. (if org-odd-levels-only
  8032. (1- (* 2 (cdr desc)))
  8033. (cdr desc)))
  8034. "\\}[ \t]")))
  8035. ((eq (car desc) :maxlevel)
  8036. (setq fast-path-p t)
  8037. (setq descre (concat "^\\*\\{1," (number-to-string
  8038. (if org-odd-levels-only
  8039. (1- (* 2 (cdr desc)))
  8040. (cdr desc)))
  8041. "\\}[ \t]")))
  8042. (t (error "Bad refiling target description %s" desc)))
  8043. (while (setq f (pop files))
  8044. (with-current-buffer
  8045. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8046. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8047. (setq f (expand-file-name f))
  8048. (if (eq org-refile-use-outline-path 'file)
  8049. (push (list (file-name-nondirectory f) f nil nil) targets))
  8050. (save-excursion
  8051. (save-restriction
  8052. (widen)
  8053. (goto-char (point-min))
  8054. (while (re-search-forward descre nil t)
  8055. (goto-char (setq pos0 (point-at-bol)))
  8056. (catch 'next
  8057. (when org-refile-target-verify-function
  8058. (save-match-data
  8059. (or (funcall org-refile-target-verify-function)
  8060. (throw 'next t))))
  8061. (when (looking-at org-complex-heading-regexp)
  8062. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8063. txt (org-link-display-format (match-string 4))
  8064. re (concat "^" (regexp-quote
  8065. (buffer-substring (match-beginning 1)
  8066. (match-end 4)))))
  8067. (if (match-end 5) (setq re (concat re "[ \t]+"
  8068. (regexp-quote
  8069. (match-string 5)))))
  8070. (setq re (concat re "[ \t]*$"))
  8071. (when org-refile-use-outline-path
  8072. (setq txt (mapconcat 'org-protect-slash
  8073. (append
  8074. (if (eq org-refile-use-outline-path 'file)
  8075. (list (file-name-nondirectory
  8076. (buffer-file-name (buffer-base-buffer))))
  8077. (if (eq org-refile-use-outline-path 'full-file-path)
  8078. (list (buffer-file-name (buffer-base-buffer)))))
  8079. (org-get-outline-path fast-path-p level txt)
  8080. (list txt))
  8081. "/")))
  8082. (push (list txt f re (point)) targets)))
  8083. (when (= (point) pos0)
  8084. ;; verification function has not moved point
  8085. (goto-char (point-at-eol))))))))))
  8086. (message "Getting targets...done")
  8087. (nreverse targets)))
  8088. (defun org-protect-slash (s)
  8089. (while (string-match "/" s)
  8090. (setq s (replace-match "\\" t t s)))
  8091. s)
  8092. (defvar org-olpa (make-vector 20 nil))
  8093. (defun org-get-outline-path (&optional fastp level heading)
  8094. "Return the outline path to the current entry, as a list.
  8095. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8096. routine which makes outline path derivations for an entire file,
  8097. avoiding backtracing."
  8098. (if fastp
  8099. (progn
  8100. (if (> level 19)
  8101. (error "Outline path failure, more than 19 levels."))
  8102. (loop for i from level upto 19 do
  8103. (aset org-olpa i nil))
  8104. (prog1
  8105. (delq nil (append org-olpa nil))
  8106. (aset org-olpa level heading)))
  8107. (let (rtn)
  8108. (save-excursion
  8109. (while (org-up-heading-safe)
  8110. (when (looking-at org-complex-heading-regexp)
  8111. (push (org-match-string-no-properties 4) rtn)))
  8112. rtn))))
  8113. (defun org-format-outline-path (path &optional width prefix)
  8114. "Format the outlie path PATH for display.
  8115. Width is the maximum number of characters that is available.
  8116. Prefix is a prefix to be included in the returned string,
  8117. such as the file name."
  8118. (setq width (or width 79))
  8119. (if prefix (setq width (- width (length prefix))))
  8120. (if (not path)
  8121. (or prefix "")
  8122. (let* ((nsteps (length path))
  8123. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8124. (maxwidth (if (<= total-width width)
  8125. 10000 ;; everything fits
  8126. ;; we need to shorten the level headings
  8127. (/ (- width nsteps) nsteps)))
  8128. (org-odd-levels-only nil)
  8129. (n 0)
  8130. (total (1+ (length prefix))))
  8131. (setq maxwidth (max maxwidth 10))
  8132. (concat prefix
  8133. (mapconcat
  8134. (lambda (h)
  8135. (setq n (1+ n))
  8136. (if (and (= n nsteps) (< maxwidth 10000))
  8137. (setq maxwidth (- total-width total)))
  8138. (if (< (length h) maxwidth)
  8139. (progn (setq total (+ total (length h) 1)) h)
  8140. (setq h (substring h 0 (- maxwidth 2))
  8141. total (+ total maxwidth 1))
  8142. (if (string-match "[ \t]+\\'" h)
  8143. (setq h (substring h 0 (match-beginning 0))))
  8144. (setq h (concat h "..")))
  8145. (org-add-props h nil 'face
  8146. (nth (% (1- n) org-n-level-faces)
  8147. org-level-faces))
  8148. h)
  8149. path "/")))))
  8150. (defun org-display-outline-path (&optional file current)
  8151. "Display the current outline path in the echo area."
  8152. (interactive "P")
  8153. (let ((bfn (buffer-file-name (buffer-base-buffer)))
  8154. (path (and (org-mode-p) (org-get-outline-path))))
  8155. (if current (setq path (append path
  8156. (save-excursion
  8157. (org-back-to-heading t)
  8158. (if (looking-at org-complex-heading-regexp)
  8159. (list (match-string 4)))))))
  8160. (message (org-format-outline-path
  8161. path
  8162. (1- (frame-width))
  8163. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8164. (defvar org-refile-history nil
  8165. "History for refiling operations.")
  8166. (defvar org-after-refile-insert-hook nil
  8167. "Hook run after `org-refile' has inserted its stuff at the new location.
  8168. Note that this is still *before* the stuff will be removed from
  8169. the *old* location.")
  8170. (defun org-refile (&optional goto default-buffer rfloc)
  8171. "Move the entry at point to another heading.
  8172. The list of target headings is compiled using the information in
  8173. `org-refile-targets', which see. This list is created before each use
  8174. and will therefore always be up-to-date.
  8175. At the target location, the entry is filed as a subitem of the target heading.
  8176. Depending on `org-reverse-note-order', the new subitem will either be the
  8177. first or the last subitem.
  8178. If there is an active region, all entries in that region will be moved.
  8179. However, the region must fulfil the requirement that the first heading
  8180. is the first one sets the top-level of the moved text - at most siblings
  8181. below it are allowed.
  8182. With prefix arg GOTO, the command will only visit the target location,
  8183. not actually move anything.
  8184. With a double prefix `C-u C-u', go to the location where the last refiling
  8185. operation has put the subtree.
  8186. With a prefix argument of `2', refile to the running clock.
  8187. RFLOC can be a refile location obtained in a different way.
  8188. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8189. (interactive "P")
  8190. (let* ((cbuf (current-buffer))
  8191. (regionp (org-region-active-p))
  8192. (region-start (and regionp (region-beginning)))
  8193. (region-end (and regionp (region-end)))
  8194. (region-length (and regionp (- region-end region-start)))
  8195. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8196. pos it nbuf file re level reversed)
  8197. (setq last-command nil)
  8198. (when regionp
  8199. (goto-char region-start)
  8200. (or (bolp) (goto-char (point-at-bol)))
  8201. (setq region-start (point))
  8202. (unless (org-kill-is-subtree-p
  8203. (buffer-substring region-start region-end))
  8204. (error "The region is not a (sequence of) subtree(s)")))
  8205. (if (equal goto '(16))
  8206. (org-refile-goto-last-stored)
  8207. (when (or
  8208. (and (equal goto 2)
  8209. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8210. (prog1
  8211. (setq it (list (or org-clock-heading "running clock")
  8212. (buffer-file-name
  8213. (marker-buffer org-clock-hd-marker))
  8214. ""
  8215. (marker-position org-clock-hd-marker)))
  8216. (setq goto nil)))
  8217. (setq it (or rfloc
  8218. (save-excursion
  8219. (org-refile-get-location
  8220. (if goto "Goto: " "Refile to: ") default-buffer
  8221. org-refile-allow-creating-parent-nodes)))))
  8222. (setq file (nth 1 it)
  8223. re (nth 2 it)
  8224. pos (nth 3 it))
  8225. (if (and (not goto)
  8226. pos
  8227. (equal (buffer-file-name) file)
  8228. (if regionp
  8229. (and (>= pos region-start)
  8230. (<= pos region-end))
  8231. (and (>= pos (point))
  8232. (< pos (save-excursion
  8233. (org-end-of-subtree t t))))))
  8234. (error "Cannot refile to position inside the tree or region"))
  8235. (setq nbuf (or (find-buffer-visiting file)
  8236. (find-file-noselect file)))
  8237. (if goto
  8238. (progn
  8239. (switch-to-buffer nbuf)
  8240. (goto-char pos)
  8241. (org-show-context 'org-goto))
  8242. (if regionp
  8243. (progn
  8244. (org-kill-new (buffer-substring region-start region-end))
  8245. (org-save-markers-in-region region-start region-end))
  8246. (org-copy-subtree 1 nil t))
  8247. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8248. (find-file-noselect file)))
  8249. (setq reversed (org-notes-order-reversed-p))
  8250. (save-excursion
  8251. (save-restriction
  8252. (widen)
  8253. (if pos
  8254. (progn
  8255. (goto-char pos)
  8256. (looking-at outline-regexp)
  8257. (setq level (org-get-valid-level (funcall outline-level) 1))
  8258. (goto-char
  8259. (if reversed
  8260. (or (outline-next-heading) (point-max))
  8261. (or (save-excursion (org-get-next-sibling))
  8262. (org-end-of-subtree t t)
  8263. (point-max)))))
  8264. (setq level 1)
  8265. (if (not reversed)
  8266. (goto-char (point-max))
  8267. (goto-char (point-min))
  8268. (or (outline-next-heading) (goto-char (point-max)))))
  8269. (if (not (bolp)) (newline))
  8270. (bookmark-set "org-refile-last-stored")
  8271. (org-paste-subtree level)
  8272. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8273. (run-hooks 'org-after-refile-insert-hook))))
  8274. (if regionp
  8275. (delete-region (point) (+ (point) region-length))
  8276. (org-cut-subtree))
  8277. (when (featurep 'org-inlinetask)
  8278. (org-inlinetask-remove-END-maybe))
  8279. (setq org-markers-to-move nil)
  8280. (message "Refiled to \"%s\"" (car it))))))
  8281. (org-reveal))
  8282. (defun org-refile-goto-last-stored ()
  8283. "Go to the location where the last refile was stored."
  8284. (interactive)
  8285. (bookmark-jump "org-refile-last-stored")
  8286. (message "This is the location of the last refile"))
  8287. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8288. "Prompt the user for a refile location, using PROMPT."
  8289. (let ((org-refile-targets org-refile-targets)
  8290. (org-refile-use-outline-path org-refile-use-outline-path))
  8291. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8292. (unless org-refile-target-table
  8293. (error "No refile targets"))
  8294. (let* ((cbuf (current-buffer))
  8295. (partial-completion-mode nil)
  8296. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8297. (cfunc (if (and org-refile-use-outline-path
  8298. org-outline-path-complete-in-steps)
  8299. 'org-olpath-completing-read
  8300. 'org-icompleting-read))
  8301. (extra (if org-refile-use-outline-path "/" ""))
  8302. (filename (and cfn (expand-file-name cfn)))
  8303. (tbl (mapcar
  8304. (lambda (x)
  8305. (if (and (not (member org-refile-use-outline-path
  8306. '(file full-file-path)))
  8307. (not (equal filename (nth 1 x))))
  8308. (cons (concat (car x) extra " ("
  8309. (file-name-nondirectory (nth 1 x)) ")")
  8310. (cdr x))
  8311. (cons (concat (car x) extra) (cdr x))))
  8312. org-refile-target-table))
  8313. (completion-ignore-case t)
  8314. pa answ parent-target child parent old-hist)
  8315. (setq old-hist org-refile-history)
  8316. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8317. nil 'org-refile-history))
  8318. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8319. (if pa
  8320. (progn
  8321. (when (or (not org-refile-history)
  8322. (not (eq old-hist org-refile-history))
  8323. (not (equal (car pa) (car org-refile-history))))
  8324. (setq org-refile-history
  8325. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8326. org-refile-history
  8327. (cdr org-refile-history))))
  8328. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8329. (pop org-refile-history)))
  8330. pa)
  8331. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8332. (setq parent (match-string 1 answ)
  8333. child (match-string 2 answ))
  8334. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8335. (when (and parent-target
  8336. (or (eq new-nodes t)
  8337. (and (eq new-nodes 'confirm)
  8338. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8339. (org-refile-new-child parent-target child))))))
  8340. (defun org-refile-new-child (parent-target child)
  8341. "Use refile target PARENT-TARGET to add new CHILD below it."
  8342. (unless parent-target
  8343. (error "Cannot find parent for new node"))
  8344. (let ((file (nth 1 parent-target))
  8345. (pos (nth 3 parent-target))
  8346. level)
  8347. (with-current-buffer (or (find-buffer-visiting file)
  8348. (find-file-noselect file))
  8349. (save-excursion
  8350. (save-restriction
  8351. (widen)
  8352. (if pos
  8353. (goto-char pos)
  8354. (goto-char (point-max))
  8355. (if (not (bolp)) (newline)))
  8356. (when (looking-at outline-regexp)
  8357. (setq level (funcall outline-level))
  8358. (org-end-of-subtree t t))
  8359. (org-back-over-empty-lines)
  8360. (insert "\n" (make-string
  8361. (if pos (org-get-valid-level level 1) 1) ?*)
  8362. " " child "\n")
  8363. (beginning-of-line 0)
  8364. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8365. (defun org-olpath-completing-read (prompt collection &rest args)
  8366. "Read an outline path like a file name."
  8367. (let ((thetable collection)
  8368. (org-completion-use-ido nil) ; does not work with ido.
  8369. (org-completion-use-iswitchb nil)) ; or iswitchb
  8370. (apply
  8371. 'org-icompleting-read prompt
  8372. (lambda (string predicate &optional flag)
  8373. (let (rtn r f (l (length string)))
  8374. (cond
  8375. ((eq flag nil)
  8376. ;; try completion
  8377. (try-completion string thetable))
  8378. ((eq flag t)
  8379. ;; all-completions
  8380. (setq rtn (all-completions string thetable predicate))
  8381. (mapcar
  8382. (lambda (x)
  8383. (setq r (substring x l))
  8384. (if (string-match " ([^)]*)$" x)
  8385. (setq f (match-string 0 x))
  8386. (setq f ""))
  8387. (if (string-match "/" r)
  8388. (concat string (substring r 0 (match-end 0)) f)
  8389. x))
  8390. rtn))
  8391. ((eq flag 'lambda)
  8392. ;; exact match?
  8393. (assoc string thetable)))
  8394. ))
  8395. args)))
  8396. ;;;; Dynamic blocks
  8397. (defun org-find-dblock (name)
  8398. "Find the first dynamic block with name NAME in the buffer.
  8399. If not found, stay at current position and return nil."
  8400. (let (pos)
  8401. (save-excursion
  8402. (goto-char (point-min))
  8403. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8404. nil t)
  8405. (match-beginning 0))))
  8406. (if pos (goto-char pos))
  8407. pos))
  8408. (defconst org-dblock-start-re
  8409. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8410. "Matches the start line of a dynamic block, with parameters.")
  8411. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8412. "Matches the end of a dynamic block.")
  8413. (defun org-create-dblock (plist)
  8414. "Create a dynamic block section, with parameters taken from PLIST.
  8415. PLIST must contain a :name entry which is used as name of the block."
  8416. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8417. (end-of-line 1)
  8418. (newline))
  8419. (let ((col (current-column))
  8420. (name (plist-get plist :name)))
  8421. (insert "#+BEGIN: " name)
  8422. (while plist
  8423. (if (eq (car plist) :name)
  8424. (setq plist (cddr plist))
  8425. (insert " " (prin1-to-string (pop plist)))))
  8426. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8427. (beginning-of-line -2)))
  8428. (defun org-prepare-dblock ()
  8429. "Prepare dynamic block for refresh.
  8430. This empties the block, puts the cursor at the insert position and returns
  8431. the property list including an extra property :name with the block name."
  8432. (unless (looking-at org-dblock-start-re)
  8433. (error "Not at a dynamic block"))
  8434. (let* ((begdel (1+ (match-end 0)))
  8435. (name (org-no-properties (match-string 1)))
  8436. (params (append (list :name name)
  8437. (read (concat "(" (match-string 3) ")")))))
  8438. (save-excursion
  8439. (beginning-of-line 1)
  8440. (skip-chars-forward " \t")
  8441. (setq params (plist-put params :indentation-column (current-column))))
  8442. (unless (re-search-forward org-dblock-end-re nil t)
  8443. (error "Dynamic block not terminated"))
  8444. (setq params
  8445. (append params
  8446. (list :content (buffer-substring
  8447. begdel (match-beginning 0)))))
  8448. (delete-region begdel (match-beginning 0))
  8449. (goto-char begdel)
  8450. (open-line 1)
  8451. params))
  8452. (defun org-map-dblocks (&optional command)
  8453. "Apply COMMAND to all dynamic blocks in the current buffer.
  8454. If COMMAND is not given, use `org-update-dblock'."
  8455. (let ((cmd (or command 'org-update-dblock))
  8456. pos)
  8457. (save-excursion
  8458. (goto-char (point-min))
  8459. (while (re-search-forward org-dblock-start-re nil t)
  8460. (goto-char (setq pos (match-beginning 0)))
  8461. (condition-case nil
  8462. (funcall cmd)
  8463. (error (message "Error during update of dynamic block")))
  8464. (goto-char pos)
  8465. (unless (re-search-forward org-dblock-end-re nil t)
  8466. (error "Dynamic block not terminated"))))))
  8467. (defun org-dblock-update (&optional arg)
  8468. "User command for updating dynamic blocks.
  8469. Update the dynamic block at point. With prefix ARG, update all dynamic
  8470. blocks in the buffer."
  8471. (interactive "P")
  8472. (if arg
  8473. (org-update-all-dblocks)
  8474. (or (looking-at org-dblock-start-re)
  8475. (org-beginning-of-dblock))
  8476. (org-update-dblock)))
  8477. (defun org-update-dblock ()
  8478. "Update the dynamic block at point
  8479. This means to empty the block, parse for parameters and then call
  8480. the correct writing function."
  8481. (save-window-excursion
  8482. (let* ((pos (point))
  8483. (line (org-current-line))
  8484. (params (org-prepare-dblock))
  8485. (name (plist-get params :name))
  8486. (indent (plist-get params :indentation-column))
  8487. (cmd (intern (concat "org-dblock-write:" name))))
  8488. (message "Updating dynamic block `%s' at line %d..." name line)
  8489. (funcall cmd params)
  8490. (message "Updating dynamic block `%s' at line %d...done" name line)
  8491. (goto-char pos)
  8492. (when (and indent (> indent 0))
  8493. (setq indent (make-string indent ?\ ))
  8494. (save-excursion
  8495. (org-beginning-of-dblock)
  8496. (forward-line 1)
  8497. (while (not (looking-at org-dblock-end-re))
  8498. (insert indent)
  8499. (beginning-of-line 2))
  8500. (when (looking-at org-dblock-end-re)
  8501. (and (looking-at "[ \t]+")
  8502. (replace-match ""))
  8503. (insert indent)))))))
  8504. (defun org-beginning-of-dblock ()
  8505. "Find the beginning of the dynamic block at point.
  8506. Error if there is no such block at point."
  8507. (let ((pos (point))
  8508. beg)
  8509. (end-of-line 1)
  8510. (if (and (re-search-backward org-dblock-start-re nil t)
  8511. (setq beg (match-beginning 0))
  8512. (re-search-forward org-dblock-end-re nil t)
  8513. (> (match-end 0) pos))
  8514. (goto-char beg)
  8515. (goto-char pos)
  8516. (error "Not in a dynamic block"))))
  8517. (defun org-update-all-dblocks ()
  8518. "Update all dynamic blocks in the buffer.
  8519. This function can be used in a hook."
  8520. (when (org-mode-p)
  8521. (org-map-dblocks 'org-update-dblock)))
  8522. ;;;; Completion
  8523. (defconst org-additional-option-like-keywords
  8524. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8525. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8526. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
  8527. "BEGIN:" "END:"
  8528. "ORGTBL" "TBLFM:" "TBLNAME:"
  8529. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8530. "BEGIN_QUOTE" "END_QUOTE"
  8531. "BEGIN_VERSE" "END_VERSE"
  8532. "BEGIN_CENTER" "END_CENTER"
  8533. "BEGIN_SRC" "END_SRC"
  8534. "CATEGORY" "COLUMNS"
  8535. "CAPTION" "LABEL"
  8536. "SETUPFILE"
  8537. "BIND"
  8538. "MACRO"))
  8539. (defcustom org-structure-template-alist
  8540. '(
  8541. ("s" "#+begin_src ?\n\n#+end_src"
  8542. "<src lang=\"?\">\n\n</src>")
  8543. ("e" "#+begin_example\n?\n#+end_example"
  8544. "<example>\n?\n</example>")
  8545. ("q" "#+begin_quote\n?\n#+end_quote"
  8546. "<quote>\n?\n</quote>")
  8547. ("v" "#+begin_verse\n?\n#+end_verse"
  8548. "<verse>\n?\n/verse>")
  8549. ("c" "#+begin_center\n?\n#+end_center"
  8550. "<center>\n?\n/center>")
  8551. ("l" "#+begin_latex\n?\n#+end_latex"
  8552. "<literal style=\"latex\">\n?\n</literal>")
  8553. ("L" "#+latex: "
  8554. "<literal style=\"latex\">?</literal>")
  8555. ("h" "#+begin_html\n?\n#+end_html"
  8556. "<literal style=\"html\">\n?\n</literal>")
  8557. ("H" "#+html: "
  8558. "<literal style=\"html\">?</literal>")
  8559. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8560. ("A" "#+ascii: ")
  8561. ("i" "#+include %file ?"
  8562. "<include file=%file markup=\"?\">")
  8563. )
  8564. "Structure completion elements.
  8565. This is a list of abbreviation keys and values. The value gets inserted
  8566. it you type @samp{.} followed by the key and then the completion key,
  8567. usually `M-TAB'. %file will be replaced by a file name after prompting
  8568. for the file using completion.
  8569. There are two templates for each key, the first uses the original Org syntax,
  8570. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8571. the default when the /org-mtags.el/ module has been loaded. See also the
  8572. variable `org-mtags-prefer-muse-templates'.
  8573. This is an experimental feature, it is undecided if it is going to stay in."
  8574. :group 'org-completion
  8575. :type '(repeat
  8576. (string :tag "Key")
  8577. (string :tag "Template")
  8578. (string :tag "Muse Template")))
  8579. (defun org-try-structure-completion ()
  8580. "Try to complete a structure template before point.
  8581. This looks for strings like \"<e\" on an otherwise empty line and
  8582. expands them."
  8583. (let ((l (buffer-substring (point-at-bol) (point)))
  8584. a)
  8585. (when (and (looking-at "[ \t]*$")
  8586. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8587. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8588. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8589. (match-beginning 1)) a)
  8590. t)))
  8591. (defun org-complete-expand-structure-template (start cell)
  8592. "Expand a structure template."
  8593. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8594. (rpl (nth (if musep 2 1) cell))
  8595. (ind ""))
  8596. (delete-region start (point))
  8597. (when (string-match "\\`#\\+" rpl)
  8598. (cond
  8599. ((bolp))
  8600. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8601. (setq ind (buffer-substring (point-at-bol) (point))))
  8602. (t (newline))))
  8603. (setq start (point))
  8604. (if (string-match "%file" rpl)
  8605. (setq rpl (replace-match
  8606. (concat
  8607. "\""
  8608. (save-match-data
  8609. (abbreviate-file-name (read-file-name "Include file: ")))
  8610. "\"")
  8611. t t rpl)))
  8612. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8613. (concat "\n" ind)))
  8614. (insert rpl)
  8615. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8616. (defun org-complete (&optional arg)
  8617. "Perform completion on word at point.
  8618. At the beginning of a headline, this completes TODO keywords as given in
  8619. `org-todo-keywords'.
  8620. If the current word is preceded by a backslash, completes the TeX symbols
  8621. that are supported for HTML support.
  8622. If the current word is preceded by \"#+\", completes special words for
  8623. setting file options.
  8624. In the line after \"#+STARTUP:, complete valid keywords.\"
  8625. At all other locations, this simply calls the value of
  8626. `org-completion-fallback-command'."
  8627. (interactive "P")
  8628. (org-without-partial-completion
  8629. (catch 'exit
  8630. (let* ((a nil)
  8631. (end (point))
  8632. (beg1 (save-excursion
  8633. (skip-chars-backward (org-re "[:alnum:]_@"))
  8634. (point)))
  8635. (beg (save-excursion
  8636. (skip-chars-backward "a-zA-Z0-9_:$")
  8637. (point)))
  8638. (confirm (lambda (x) (stringp (car x))))
  8639. (searchhead (equal (char-before beg) ?*))
  8640. (struct
  8641. (when (and (member (char-before beg1) '(?. ?<))
  8642. (setq a (assoc (buffer-substring beg1 (point))
  8643. org-structure-template-alist)))
  8644. (org-complete-expand-structure-template (1- beg1) a)
  8645. (throw 'exit t)))
  8646. (tag (and (equal (char-before beg1) ?:)
  8647. (equal (char-after (point-at-bol)) ?*)))
  8648. (prop (and (equal (char-before beg1) ?:)
  8649. (not (equal (char-after (point-at-bol)) ?*))))
  8650. (texp (equal (char-before beg) ?\\))
  8651. (link (equal (char-before beg) ?\[))
  8652. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8653. beg)
  8654. "#+"))
  8655. (startup (string-match "^#\\+STARTUP:.*"
  8656. (buffer-substring (point-at-bol) (point))))
  8657. (completion-ignore-case opt)
  8658. (type nil)
  8659. (tbl nil)
  8660. (table (cond
  8661. (opt
  8662. (setq type :opt)
  8663. (require 'org-exp)
  8664. (append
  8665. (delq nil
  8666. (mapcar
  8667. (lambda (x)
  8668. (if (string-match
  8669. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8670. (cons (match-string 2 x)
  8671. (match-string 1 x))))
  8672. (org-split-string (org-get-current-options) "\n")))
  8673. (mapcar 'list org-additional-option-like-keywords)))
  8674. (startup
  8675. (setq type :startup)
  8676. org-startup-options)
  8677. (link (append org-link-abbrev-alist-local
  8678. org-link-abbrev-alist))
  8679. (texp
  8680. (setq type :tex)
  8681. org-html-entities)
  8682. ((string-match "\\`\\*+[ \t]+\\'"
  8683. (buffer-substring (point-at-bol) beg))
  8684. (setq type :todo)
  8685. (mapcar 'list org-todo-keywords-1))
  8686. (searchhead
  8687. (setq type :searchhead)
  8688. (save-excursion
  8689. (goto-char (point-min))
  8690. (while (re-search-forward org-todo-line-regexp nil t)
  8691. (push (list
  8692. (org-make-org-heading-search-string
  8693. (match-string 3) t))
  8694. tbl)))
  8695. tbl)
  8696. (tag (setq type :tag beg beg1)
  8697. (or org-tag-alist (org-get-buffer-tags)))
  8698. (prop (setq type :prop beg beg1)
  8699. (mapcar 'list (org-buffer-property-keys nil t t)))
  8700. (t (progn
  8701. (call-interactively org-completion-fallback-command)
  8702. (throw 'exit nil)))))
  8703. (pattern (buffer-substring-no-properties beg end))
  8704. (completion (try-completion pattern table confirm)))
  8705. (cond ((eq completion t)
  8706. (if (not (assoc (upcase pattern) table))
  8707. (message "Already complete")
  8708. (if (and (equal type :opt)
  8709. (not (member (car (assoc (upcase pattern) table))
  8710. org-additional-option-like-keywords)))
  8711. (insert (substring (cdr (assoc (upcase pattern) table))
  8712. (length pattern)))
  8713. (if (memq type '(:tag :prop)) (insert ":")))))
  8714. ((null completion)
  8715. (message "Can't find completion for \"%s\"" pattern)
  8716. (ding))
  8717. ((not (string= pattern completion))
  8718. (delete-region beg end)
  8719. (if (string-match " +$" completion)
  8720. (setq completion (replace-match "" t t completion)))
  8721. (insert completion)
  8722. (if (get-buffer-window "*Completions*")
  8723. (delete-window (get-buffer-window "*Completions*")))
  8724. (if (assoc completion table)
  8725. (if (eq type :todo) (insert " ")
  8726. (if (memq type '(:tag :prop)) (insert ":"))))
  8727. (if (and (equal type :opt) (assoc completion table))
  8728. (message "%s" (substitute-command-keys
  8729. "Press \\[org-complete] again to insert example settings"))))
  8730. (t
  8731. (message "Making completion list...")
  8732. (let ((list (sort (all-completions pattern table confirm)
  8733. 'string<)))
  8734. (with-output-to-temp-buffer "*Completions*"
  8735. (condition-case nil
  8736. ;; Protection needed for XEmacs and emacs 21
  8737. (display-completion-list list pattern)
  8738. (error (display-completion-list list)))))
  8739. (message "Making completion list...%s" "done")))))))
  8740. ;;;; TODO, DEADLINE, Comments
  8741. (defun org-toggle-comment ()
  8742. "Change the COMMENT state of an entry."
  8743. (interactive)
  8744. (save-excursion
  8745. (org-back-to-heading)
  8746. (let (case-fold-search)
  8747. (if (looking-at (concat outline-regexp
  8748. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8749. (replace-match "" t t nil 1)
  8750. (if (looking-at outline-regexp)
  8751. (progn
  8752. (goto-char (match-end 0))
  8753. (insert org-comment-string " ")))))))
  8754. (defvar org-last-todo-state-is-todo nil
  8755. "This is non-nil when the last TODO state change led to a TODO state.
  8756. If the last change removed the TODO tag or switched to DONE, then
  8757. this is nil.")
  8758. (defvar org-setting-tags nil) ; dynamically skipped
  8759. (defun org-parse-local-options (string var)
  8760. "Parse STRING for startup setting relevant for variable VAR."
  8761. (let ((rtn (symbol-value var))
  8762. e opts)
  8763. (save-match-data
  8764. (if (or (not string) (not (string-match "\\S-" string)))
  8765. rtn
  8766. (setq opts (delq nil (mapcar (lambda (x)
  8767. (setq e (assoc x org-startup-options))
  8768. (if (eq (nth 1 e) var) e nil))
  8769. (org-split-string string "[ \t]+"))))
  8770. (if (not opts)
  8771. rtn
  8772. (setq rtn nil)
  8773. (while (setq e (pop opts))
  8774. (if (not (nth 3 e))
  8775. (setq rtn (nth 2 e))
  8776. (if (not (listp rtn)) (setq rtn nil))
  8777. (push (nth 2 e) rtn)))
  8778. rtn)))))
  8779. (defvar org-todo-setup-filter-hook nil
  8780. "Hook for functions that pre-filter todo specs.
  8781. Each function takes a todo spec and returns either `nil' or the spec
  8782. transformed into canonical form." )
  8783. (defvar org-todo-get-default-hook nil
  8784. "Hook for functions that get a default item for todo.
  8785. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8786. `nil' or a string to be used for the todo mark." )
  8787. (defvar org-agenda-headline-snapshot-before-repeat)
  8788. (defun org-todo (&optional arg)
  8789. "Change the TODO state of an item.
  8790. The state of an item is given by a keyword at the start of the heading,
  8791. like
  8792. *** TODO Write paper
  8793. *** DONE Call mom
  8794. The different keywords are specified in the variable `org-todo-keywords'.
  8795. By default the available states are \"TODO\" and \"DONE\".
  8796. So for this example: when the item starts with TODO, it is changed to DONE.
  8797. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8798. DONE are present, add TODO at the beginning of the heading.
  8799. With C-u prefix arg, use completion to determine the new state.
  8800. With numeric prefix arg, switch to that state.
  8801. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8802. With a triple C-u prefix, circumvent any state blocking.
  8803. For calling through lisp, arg is also interpreted in the following way:
  8804. 'none -> empty state
  8805. \"\"(empty string) -> switch to empty state
  8806. 'done -> switch to DONE
  8807. 'nextset -> switch to the next set of keywords
  8808. 'previousset -> switch to the previous set of keywords
  8809. \"WAITING\" -> switch to the specified keyword, but only if it
  8810. really is a member of `org-todo-keywords'."
  8811. (interactive "P")
  8812. (if (equal arg '(16)) (setq arg 'nextset))
  8813. (let ((org-blocker-hook org-blocker-hook)
  8814. (case-fold-search nil))
  8815. (when (equal arg '(64))
  8816. (setq arg nil org-blocker-hook nil))
  8817. (when (and org-blocker-hook
  8818. (or org-inhibit-blocking
  8819. (org-entry-get nil "NOBLOCKING")))
  8820. (setq org-blocker-hook nil))
  8821. (save-excursion
  8822. (catch 'exit
  8823. (org-back-to-heading t)
  8824. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8825. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8826. (looking-at " *"))
  8827. (let* ((match-data (match-data))
  8828. (startpos (point-at-bol))
  8829. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8830. (org-log-done org-log-done)
  8831. (org-log-repeat org-log-repeat)
  8832. (org-todo-log-states org-todo-log-states)
  8833. (this (match-string 1))
  8834. (hl-pos (match-beginning 0))
  8835. (head (org-get-todo-sequence-head this))
  8836. (ass (assoc head org-todo-kwd-alist))
  8837. (interpret (nth 1 ass))
  8838. (done-word (nth 3 ass))
  8839. (final-done-word (nth 4 ass))
  8840. (last-state (or this ""))
  8841. (completion-ignore-case t)
  8842. (member (member this org-todo-keywords-1))
  8843. (tail (cdr member))
  8844. (state (cond
  8845. ((and org-todo-key-trigger
  8846. (or (and (equal arg '(4))
  8847. (eq org-use-fast-todo-selection 'prefix))
  8848. (and (not arg) org-use-fast-todo-selection
  8849. (not (eq org-use-fast-todo-selection
  8850. 'prefix)))))
  8851. ;; Use fast selection
  8852. (org-fast-todo-selection))
  8853. ((and (equal arg '(4))
  8854. (or (not org-use-fast-todo-selection)
  8855. (not org-todo-key-trigger)))
  8856. ;; Read a state with completion
  8857. (org-icompleting-read
  8858. "State: " (mapcar (lambda(x) (list x))
  8859. org-todo-keywords-1)
  8860. nil t))
  8861. ((eq arg 'right)
  8862. (if this
  8863. (if tail (car tail) nil)
  8864. (car org-todo-keywords-1)))
  8865. ((eq arg 'left)
  8866. (if (equal member org-todo-keywords-1)
  8867. nil
  8868. (if this
  8869. (nth (- (length org-todo-keywords-1)
  8870. (length tail) 2)
  8871. org-todo-keywords-1)
  8872. (org-last org-todo-keywords-1))))
  8873. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8874. (setq arg nil))) ; hack to fall back to cycling
  8875. (arg
  8876. ;; user or caller requests a specific state
  8877. (cond
  8878. ((equal arg "") nil)
  8879. ((eq arg 'none) nil)
  8880. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8881. ((eq arg 'nextset)
  8882. (or (car (cdr (member head org-todo-heads)))
  8883. (car org-todo-heads)))
  8884. ((eq arg 'previousset)
  8885. (let ((org-todo-heads (reverse org-todo-heads)))
  8886. (or (car (cdr (member head org-todo-heads)))
  8887. (car org-todo-heads))))
  8888. ((car (member arg org-todo-keywords-1)))
  8889. ((stringp arg)
  8890. (error "State `%s' not valid in this file" arg))
  8891. ((nth (1- (prefix-numeric-value arg))
  8892. org-todo-keywords-1))))
  8893. ((null member) (or head (car org-todo-keywords-1)))
  8894. ((equal this final-done-word) nil) ;; -> make empty
  8895. ((null tail) nil) ;; -> first entry
  8896. ((memq interpret '(type priority))
  8897. (if (eq this-command last-command)
  8898. (car tail)
  8899. (if (> (length tail) 0)
  8900. (or done-word (car org-done-keywords))
  8901. nil)))
  8902. (t
  8903. (car tail))))
  8904. (state (or
  8905. (run-hook-with-args-until-success
  8906. 'org-todo-get-default-hook state last-state)
  8907. state))
  8908. (next (if state (concat " " state " ") " "))
  8909. (change-plist (list :type 'todo-state-change :from this :to state
  8910. :position startpos))
  8911. dolog now-done-p)
  8912. (when org-blocker-hook
  8913. (setq org-last-todo-state-is-todo
  8914. (not (member this org-done-keywords)))
  8915. (unless (save-excursion
  8916. (save-match-data
  8917. (run-hook-with-args-until-failure
  8918. 'org-blocker-hook change-plist)))
  8919. (if (interactive-p)
  8920. (error "TODO state change from %s to %s blocked" this state)
  8921. ;; fail silently
  8922. (message "TODO state change from %s to %s blocked" this state)
  8923. (throw 'exit nil))))
  8924. (store-match-data match-data)
  8925. (replace-match next t t)
  8926. (unless (pos-visible-in-window-p hl-pos)
  8927. (message "TODO state changed to %s" (org-trim next)))
  8928. (unless head
  8929. (setq head (org-get-todo-sequence-head state)
  8930. ass (assoc head org-todo-kwd-alist)
  8931. interpret (nth 1 ass)
  8932. done-word (nth 3 ass)
  8933. final-done-word (nth 4 ass)))
  8934. (when (memq arg '(nextset previousset))
  8935. (message "Keyword-Set %d/%d: %s"
  8936. (- (length org-todo-sets) -1
  8937. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8938. (length org-todo-sets)
  8939. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8940. (setq org-last-todo-state-is-todo
  8941. (not (member state org-done-keywords)))
  8942. (setq now-done-p (and (member state org-done-keywords)
  8943. (not (member this org-done-keywords))))
  8944. (and logging (org-local-logging logging))
  8945. (when (and (or org-todo-log-states org-log-done)
  8946. (not (eq org-inhibit-logging t))
  8947. (not (memq arg '(nextset previousset))))
  8948. ;; we need to look at recording a time and note
  8949. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8950. (nth 2 (assoc this org-todo-log-states))))
  8951. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8952. (setq dolog 'time))
  8953. (when (and state
  8954. (member state org-not-done-keywords)
  8955. (not (member this org-not-done-keywords)))
  8956. ;; This is now a todo state and was not one before
  8957. ;; If there was a CLOSED time stamp, get rid of it.
  8958. (org-add-planning-info nil nil 'closed))
  8959. (when (and now-done-p org-log-done)
  8960. ;; It is now done, and it was not done before
  8961. (org-add-planning-info 'closed (org-current-time))
  8962. (if (and (not dolog) (eq 'note org-log-done))
  8963. (org-add-log-setup 'done state this 'findpos 'note)))
  8964. (when (and state dolog)
  8965. ;; This is a non-nil state, and we need to log it
  8966. (org-add-log-setup 'state state this 'findpos dolog)))
  8967. ;; Fixup tag positioning
  8968. (org-todo-trigger-tag-changes state)
  8969. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8970. (when org-provide-todo-statistics
  8971. (org-update-parent-todo-statistics))
  8972. (run-hooks 'org-after-todo-state-change-hook)
  8973. (if (and arg (not (member state org-done-keywords)))
  8974. (setq head (org-get-todo-sequence-head state)))
  8975. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8976. ;; Do we need to trigger a repeat?
  8977. (when now-done-p
  8978. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8979. ;; This is for the agenda, take a snapshot of the headline.
  8980. (save-match-data
  8981. (setq org-agenda-headline-snapshot-before-repeat
  8982. (org-get-heading))))
  8983. (org-auto-repeat-maybe state))
  8984. ;; Fixup cursor location if close to the keyword
  8985. (if (and (outline-on-heading-p)
  8986. (not (bolp))
  8987. (save-excursion (beginning-of-line 1)
  8988. (looking-at org-todo-line-regexp))
  8989. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8990. (progn
  8991. (goto-char (or (match-end 2) (match-end 1)))
  8992. (and (looking-at " ") (just-one-space))))
  8993. (when org-trigger-hook
  8994. (save-excursion
  8995. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8996. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8997. "Block turning an entry into a TODO, using the hierarchy.
  8998. This checks whether the current task should be blocked from state
  8999. changes. Such blocking occurs when:
  9000. 1. The task has children which are not all in a completed state.
  9001. 2. A task has a parent with the property :ORDERED:, and there
  9002. are siblings prior to the current task with incomplete
  9003. status.
  9004. 3. The parent of the task is blocked because it has siblings that should
  9005. be done first, or is child of a block grandparent TODO entry."
  9006. (catch 'dont-block
  9007. ;; If this is not a todo state change, or if this entry is already DONE,
  9008. ;; do not block
  9009. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9010. (member (plist-get change-plist :from)
  9011. (cons 'done org-done-keywords))
  9012. (member (plist-get change-plist :to)
  9013. (cons 'todo org-not-done-keywords))
  9014. (not (plist-get change-plist :to)))
  9015. (throw 'dont-block t))
  9016. ;; If this task has children, and any are undone, it's blocked
  9017. (save-excursion
  9018. (org-back-to-heading t)
  9019. (let ((this-level (funcall outline-level)))
  9020. (outline-next-heading)
  9021. (let ((child-level (funcall outline-level)))
  9022. (while (and (not (eobp))
  9023. (> child-level this-level))
  9024. ;; this todo has children, check whether they are all
  9025. ;; completed
  9026. (if (and (not (org-entry-is-done-p))
  9027. (org-entry-is-todo-p))
  9028. (throw 'dont-block nil))
  9029. (outline-next-heading)
  9030. (setq child-level (funcall outline-level))))))
  9031. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9032. ;; any previous siblings are undone, it's blocked
  9033. (save-excursion
  9034. (org-back-to-heading t)
  9035. (let* ((pos (point))
  9036. (parent-pos (and (org-up-heading-safe) (point))))
  9037. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9038. (when (and (org-entry-get (point) "ORDERED")
  9039. (forward-line 1)
  9040. (re-search-forward org-not-done-heading-regexp pos t))
  9041. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9042. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9043. (while t
  9044. (goto-char parent-pos)
  9045. (if (not (looking-at org-not-done-heading-regexp))
  9046. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9047. (setq pos (point))
  9048. (setq parent-pos (and (org-up-heading-safe) (point)))
  9049. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9050. (when (and (org-entry-get (point) "ORDERED")
  9051. (forward-line 1)
  9052. (re-search-forward org-not-done-heading-regexp pos t))
  9053. (throw 'dont-block nil))))))) ; block, older sibling not done.
  9054. (defcustom org-track-ordered-property-with-tag nil
  9055. "Should the ORDERED property also be shown as a tag?
  9056. The ORDERED property decides if an entry should require subtasks to be
  9057. completed in sequence. Since a property is not very visible, setting
  9058. this option means that toggling the ORDERED property with the command
  9059. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9060. not relevant for the behavior, but it makes things more visible.
  9061. Note that toggling the tag with tags commands will not change the property
  9062. and therefore not influence behavior!
  9063. This can be t, meaning the tag ORDERED should be used, It can also be a
  9064. string to select a different tag for this task."
  9065. :group 'org-todo
  9066. :type '(choice
  9067. (const :tag "No tracking" nil)
  9068. (const :tag "Track with ORDERED tag" t)
  9069. (string :tag "Use other tag")))
  9070. (defun org-toggle-ordered-property ()
  9071. "Toggle the ORDERED property of the current entry.
  9072. For better visibility, you can track the value of this property with a tag.
  9073. See variable `org-track-ordered-property-with-tag'."
  9074. (interactive)
  9075. (let* ((t1 org-track-ordered-property-with-tag)
  9076. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9077. (save-excursion
  9078. (org-back-to-heading)
  9079. (if (org-entry-get nil "ORDERED")
  9080. (progn
  9081. (org-delete-property "ORDERED")
  9082. (and tag (org-toggle-tag tag 'off))
  9083. (message "Subtasks can be completed in arbitrary order"))
  9084. (org-entry-put nil "ORDERED" "t")
  9085. (and tag (org-toggle-tag tag 'on))
  9086. (message "Subtasks must be completed in sequence")))))
  9087. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9088. (defun org-block-todo-from-checkboxes (change-plist)
  9089. "Block turning an entry into a TODO, using checkboxes.
  9090. This checks whether the current task should be blocked from state
  9091. changes because there are unchecked boxes in this entry."
  9092. (catch 'dont-block
  9093. ;; If this is not a todo state change, or if this entry is already DONE,
  9094. ;; do not block
  9095. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9096. (member (plist-get change-plist :from)
  9097. (cons 'done org-done-keywords))
  9098. (member (plist-get change-plist :to)
  9099. (cons 'todo org-not-done-keywords))
  9100. (not (plist-get change-plist :to)))
  9101. (throw 'dont-block t))
  9102. ;; If this task has checkboxes that are not checked, it's blocked
  9103. (save-excursion
  9104. (org-back-to-heading t)
  9105. (let ((beg (point)) end)
  9106. (outline-next-heading)
  9107. (setq end (point))
  9108. (goto-char beg)
  9109. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9110. end t)
  9111. (progn
  9112. (if (boundp 'org-blocked-by-checkboxes)
  9113. (setq org-blocked-by-checkboxes t))
  9114. (throw 'dont-block nil)))))
  9115. t)) ; do not block
  9116. (defun org-update-statistics-cookies (all)
  9117. "Update the statistics cookie, either from TODO or from checkboxes.
  9118. This should be called with the cursor in a line with a statistics cookie."
  9119. (interactive "P")
  9120. (if all
  9121. (progn
  9122. (org-update-checkbox-count 'all)
  9123. (org-map-entries 'org-update-parent-todo-statistics))
  9124. (if (not (org-on-heading-p))
  9125. (org-update-checkbox-count)
  9126. (let ((pos (move-marker (make-marker) (point)))
  9127. end l1 l2)
  9128. (ignore-errors (org-back-to-heading t))
  9129. (if (not (org-on-heading-p))
  9130. (org-update-checkbox-count)
  9131. (setq l1 (org-outline-level))
  9132. (setq end (save-excursion
  9133. (outline-next-heading)
  9134. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9135. (point)))
  9136. (if (and (save-excursion (re-search-forward
  9137. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  9138. (not (save-excursion (re-search-forward
  9139. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9140. (org-update-checkbox-count)
  9141. (if (and l2 (> l2 l1))
  9142. (progn
  9143. (goto-char end)
  9144. (org-update-parent-todo-statistics))
  9145. (error "No data for statistics cookie"))))
  9146. (goto-char pos)
  9147. (move-marker pos nil)))))
  9148. (defvar org-entry-property-inherited-from) ;; defined below
  9149. (defun org-update-parent-todo-statistics ()
  9150. "Update any statistics cookie in the parent of the current headline.
  9151. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9152. the entire subtree and this will travel up the hierarchy and update
  9153. statistics everywhere."
  9154. (interactive)
  9155. (let* ((lim 0) prop
  9156. (recursive (or (not org-hierarchical-todo-statistics)
  9157. (string-match
  9158. "\\<recursive\\>"
  9159. (or (setq prop (org-entry-get
  9160. nil "COOKIE_DATA" 'inherit)) ""))))
  9161. (lim (or (and prop (marker-position
  9162. org-entry-property-inherited-from))
  9163. lim))
  9164. (first t)
  9165. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9166. level ltoggle l1 new ndel
  9167. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9168. (catch 'exit
  9169. (save-excursion
  9170. (beginning-of-line 1)
  9171. (if (org-at-heading-p)
  9172. (setq ltoggle (funcall outline-level))
  9173. (error "This should not happen"))
  9174. (while (and (setq level (org-up-heading-safe))
  9175. (or recursive first)
  9176. (>= (point) lim))
  9177. (setq first nil cookie-present nil)
  9178. (unless (and level
  9179. (not (string-match
  9180. "\\<checkbox\\>"
  9181. (downcase
  9182. (or (org-entry-get
  9183. nil "COOKIE_DATA")
  9184. "")))))
  9185. (throw 'exit nil))
  9186. (while (re-search-forward box-re (point-at-eol) t)
  9187. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9188. (setq is-percent (match-end 2))
  9189. (save-match-data
  9190. (unless (outline-next-heading) (throw 'exit nil))
  9191. (while (and (looking-at org-complex-heading-regexp)
  9192. (> (setq l1 (length (match-string 1))) level))
  9193. (setq kwd (and (or recursive (= l1 ltoggle))
  9194. (match-string 2)))
  9195. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9196. (and (listp org-provide-todo-statistics)
  9197. (or (member kwd org-provide-todo-statistics)
  9198. (member kwd org-done-keywords))))
  9199. (setq cnt-all (1+ cnt-all))
  9200. (if (eq org-provide-todo-statistics t)
  9201. (and kwd (setq cnt-all (1+ cnt-all)))))
  9202. (and (member kwd org-done-keywords)
  9203. (setq cnt-done (1+ cnt-done)))
  9204. (outline-next-heading)))
  9205. (setq new
  9206. (if is-percent
  9207. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9208. (format "[%d/%d]" cnt-done cnt-all))
  9209. ndel (- (match-end 0) (match-beginning 0)))
  9210. (goto-char (match-beginning 0))
  9211. (insert new)
  9212. (delete-region (point) (+ (point) ndel)))
  9213. (when cookie-present
  9214. (run-hook-with-args 'org-after-todo-statistics-hook
  9215. cnt-done (- cnt-all cnt-done))))))
  9216. (run-hooks 'org-todo-statistics-hook)))
  9217. (defvar org-after-todo-statistics-hook nil
  9218. "Hook that is called after a TODO statistics cookie has been updated.
  9219. Each function is called with two arguments: the number of not-done entries
  9220. and the number of done entries.
  9221. For example, the following function, when added to this hook, will switch
  9222. an entry to DONE when all children are done, and back to TODO when new
  9223. entries are set to a TODO status. Note that this hook is only called
  9224. when there is a statistics cookie in the headline!
  9225. (defun org-summary-todo (n-done n-not-done)
  9226. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9227. (let (org-log-done org-log-states) ; turn off logging
  9228. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9229. ")
  9230. (defvar org-todo-statistics-hook nil
  9231. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9232. This hook runs even if there is no statistics cookie present, in which case
  9233. `org-after-todo-statistics-hook' would not run.")
  9234. (defun org-todo-trigger-tag-changes (state)
  9235. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9236. (let ((l org-todo-state-tags-triggers)
  9237. changes)
  9238. (when (or (not state) (equal state ""))
  9239. (setq changes (append changes (cdr (assoc "" l)))))
  9240. (when (and (stringp state) (> (length state) 0))
  9241. (setq changes (append changes (cdr (assoc state l)))))
  9242. (when (member state org-not-done-keywords)
  9243. (setq changes (append changes (cdr (assoc 'todo l)))))
  9244. (when (member state org-done-keywords)
  9245. (setq changes (append changes (cdr (assoc 'done l)))))
  9246. (dolist (c changes)
  9247. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9248. (defun org-local-logging (value)
  9249. "Get logging settings from a property VALUE."
  9250. (let* (words w a)
  9251. ;; directly set the variables, they are already local.
  9252. (setq org-log-done nil
  9253. org-log-repeat nil
  9254. org-todo-log-states nil)
  9255. (setq words (org-split-string value))
  9256. (while (setq w (pop words))
  9257. (cond
  9258. ((setq a (assoc w org-startup-options))
  9259. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9260. (set (nth 1 a) (nth 2 a))))
  9261. ((setq a (org-extract-log-state-settings w))
  9262. (and (member (car a) org-todo-keywords-1)
  9263. (push a org-todo-log-states)))))))
  9264. (defun org-get-todo-sequence-head (kwd)
  9265. "Return the head of the TODO sequence to which KWD belongs.
  9266. If KWD is not set, check if there is a text property remembering the
  9267. right sequence."
  9268. (let (p)
  9269. (cond
  9270. ((not kwd)
  9271. (or (get-text-property (point-at-bol) 'org-todo-head)
  9272. (progn
  9273. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9274. nil (point-at-eol)))
  9275. (get-text-property p 'org-todo-head))))
  9276. ((not (member kwd org-todo-keywords-1))
  9277. (car org-todo-keywords-1))
  9278. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9279. (defun org-fast-todo-selection ()
  9280. "Fast TODO keyword selection with single keys.
  9281. Returns the new TODO keyword, or nil if no state change should occur."
  9282. (let* ((fulltable org-todo-key-alist)
  9283. (done-keywords org-done-keywords) ;; needed for the faces.
  9284. (maxlen (apply 'max (mapcar
  9285. (lambda (x)
  9286. (if (stringp (car x)) (string-width (car x)) 0))
  9287. fulltable)))
  9288. (expert nil)
  9289. (fwidth (+ maxlen 3 1 3))
  9290. (ncol (/ (- (window-width) 4) fwidth))
  9291. tg cnt e c tbl
  9292. groups ingroup)
  9293. (save-excursion
  9294. (save-window-excursion
  9295. (if expert
  9296. (set-buffer (get-buffer-create " *Org todo*"))
  9297. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9298. (erase-buffer)
  9299. (org-set-local 'org-done-keywords done-keywords)
  9300. (setq tbl fulltable cnt 0)
  9301. (while (setq e (pop tbl))
  9302. (cond
  9303. ((equal e '(:startgroup))
  9304. (push '() groups) (setq ingroup t)
  9305. (when (not (= cnt 0))
  9306. (setq cnt 0)
  9307. (insert "\n"))
  9308. (insert "{ "))
  9309. ((equal e '(:endgroup))
  9310. (setq ingroup nil cnt 0)
  9311. (insert "}\n"))
  9312. ((equal e '(:newline))
  9313. (when (not (= cnt 0))
  9314. (setq cnt 0)
  9315. (insert "\n")
  9316. (setq e (car tbl))
  9317. (while (equal (car tbl) '(:newline))
  9318. (insert "\n")
  9319. (setq tbl (cdr tbl)))))
  9320. (t
  9321. (setq tg (car e) c (cdr e))
  9322. (if ingroup (push tg (car groups)))
  9323. (setq tg (org-add-props tg nil 'face
  9324. (org-get-todo-face tg)))
  9325. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9326. (insert "[" c "] " tg (make-string
  9327. (- fwidth 4 (length tg)) ?\ ))
  9328. (when (= (setq cnt (1+ cnt)) ncol)
  9329. (insert "\n")
  9330. (if ingroup (insert " "))
  9331. (setq cnt 0)))))
  9332. (insert "\n")
  9333. (goto-char (point-min))
  9334. (if (not expert) (org-fit-window-to-buffer))
  9335. (message "[a-z..]:Set [SPC]:clear")
  9336. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9337. (cond
  9338. ((or (= c ?\C-g)
  9339. (and (= c ?q) (not (rassoc c fulltable))))
  9340. (setq quit-flag t))
  9341. ((= c ?\ ) nil)
  9342. ((setq e (rassoc c fulltable) tg (car e))
  9343. tg)
  9344. (t (setq quit-flag t)))))))
  9345. (defun org-entry-is-todo-p ()
  9346. (member (org-get-todo-state) org-not-done-keywords))
  9347. (defun org-entry-is-done-p ()
  9348. (member (org-get-todo-state) org-done-keywords))
  9349. (defun org-get-todo-state ()
  9350. (save-excursion
  9351. (org-back-to-heading t)
  9352. (and (looking-at org-todo-line-regexp)
  9353. (match-end 2)
  9354. (match-string 2))))
  9355. (defun org-at-date-range-p (&optional inactive-ok)
  9356. "Is the cursor inside a date range?"
  9357. (interactive)
  9358. (save-excursion
  9359. (catch 'exit
  9360. (let ((pos (point)))
  9361. (skip-chars-backward "^[<\r\n")
  9362. (skip-chars-backward "<[")
  9363. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9364. (>= (match-end 0) pos)
  9365. (throw 'exit t))
  9366. (skip-chars-backward "^<[\r\n")
  9367. (skip-chars-backward "<[")
  9368. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9369. (>= (match-end 0) pos)
  9370. (throw 'exit t)))
  9371. nil)))
  9372. (defun org-get-repeat (&optional tagline)
  9373. "Check if there is a deadline/schedule with repeater in this entry."
  9374. (save-match-data
  9375. (save-excursion
  9376. (org-back-to-heading t)
  9377. (and (re-search-forward (if tagline
  9378. (concat tagline "\\s-*" org-repeat-re)
  9379. org-repeat-re)
  9380. (org-entry-end-position) t)
  9381. (match-string-no-properties 1)))))
  9382. (defvar org-last-changed-timestamp)
  9383. (defvar org-last-inserted-timestamp)
  9384. (defvar org-log-post-message)
  9385. (defvar org-log-note-purpose)
  9386. (defvar org-log-note-how)
  9387. (defvar org-log-note-extra)
  9388. (defun org-auto-repeat-maybe (done-word)
  9389. "Check if the current headline contains a repeated deadline/schedule.
  9390. If yes, set TODO state back to what it was and change the base date
  9391. of repeating deadline/scheduled time stamps to new date.
  9392. This function is run automatically after each state change to a DONE state."
  9393. ;; last-state is dynamically scoped into this function
  9394. (let* ((repeat (org-get-repeat))
  9395. (aa (assoc last-state org-todo-kwd-alist))
  9396. (interpret (nth 1 aa))
  9397. (head (nth 2 aa))
  9398. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9399. (msg "Entry repeats: ")
  9400. (org-log-done nil)
  9401. (org-todo-log-states nil)
  9402. (nshiftmax 10) (nshift 0)
  9403. re type n what ts time)
  9404. (when repeat
  9405. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9406. (org-todo (if (eq interpret 'type) last-state head))
  9407. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9408. (org-time-stamp-format t t)))
  9409. (when org-log-repeat
  9410. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9411. (memq 'org-add-log-note post-command-hook))
  9412. ;; OK, we are already setup for some record
  9413. (if (eq org-log-repeat 'note)
  9414. ;; make sure we take a note, not only a time stamp
  9415. (setq org-log-note-how 'note))
  9416. ;; Set up for taking a record
  9417. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9418. last-state
  9419. 'findpos org-log-repeat)))
  9420. (org-back-to-heading t)
  9421. (org-add-planning-info nil nil 'closed)
  9422. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9423. org-deadline-time-regexp "\\)\\|\\("
  9424. org-ts-regexp "\\)"))
  9425. (while (re-search-forward
  9426. re (save-excursion (outline-next-heading) (point)) t)
  9427. (setq type (if (match-end 1) org-scheduled-string
  9428. (if (match-end 3) org-deadline-string "Plain:"))
  9429. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9430. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9431. (setq n (string-to-number (match-string 2 ts))
  9432. what (match-string 3 ts))
  9433. (if (equal what "w") (setq n (* n 7) what "d"))
  9434. ;; Preparation, see if we need to modify the start date for the change
  9435. (when (match-end 1)
  9436. (setq time (save-match-data (org-time-string-to-time ts)))
  9437. (cond
  9438. ((equal (match-string 1 ts) ".")
  9439. ;; Shift starting date to today
  9440. (org-timestamp-change
  9441. (- (time-to-days (current-time)) (time-to-days time))
  9442. 'day))
  9443. ((equal (match-string 1 ts) "+")
  9444. (while (or (= nshift 0)
  9445. (<= (time-to-days time) (time-to-days (current-time))))
  9446. (when (= (incf nshift) nshiftmax)
  9447. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9448. (error "Abort")))
  9449. (org-timestamp-change n (cdr (assoc what whata)))
  9450. (org-at-timestamp-p t)
  9451. (setq ts (match-string 1))
  9452. (setq time (save-match-data (org-time-string-to-time ts))))
  9453. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9454. ;; rematch, so that we have everything in place for the real shift
  9455. (org-at-timestamp-p t)
  9456. (setq ts (match-string 1))
  9457. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9458. (org-timestamp-change n (cdr (assoc what whata)))
  9459. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9460. (setq org-log-post-message msg)
  9461. (message "%s" msg))))
  9462. (defun org-show-todo-tree (arg)
  9463. "Make a compact tree which shows all headlines marked with TODO.
  9464. The tree will show the lines where the regexp matches, and all higher
  9465. headlines above the match.
  9466. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9467. With a numeric prefix N, construct a sparse tree for the Nth element
  9468. of `org-todo-keywords-1'."
  9469. (interactive "P")
  9470. (let ((case-fold-search nil)
  9471. (kwd-re
  9472. (cond ((null arg) org-not-done-regexp)
  9473. ((equal arg '(4))
  9474. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9475. (mapcar 'list org-todo-keywords-1))))
  9476. (concat "\\("
  9477. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9478. "\\)\\>")))
  9479. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9480. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9481. org-todo-keywords-1)))
  9482. (t (error "Invalid prefix argument: %s" arg)))))
  9483. (message "%d TODO entries found"
  9484. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9485. (defun org-deadline (&optional remove time)
  9486. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9487. With argument REMOVE, remove any deadline from the item.
  9488. When TIME is set, it should be an internal time specification, and the
  9489. scheduling will use the corresponding date."
  9490. (interactive "P")
  9491. (let ((old-date (org-entry-get nil "DEADLINE")))
  9492. (if remove
  9493. (progn
  9494. (org-remove-timestamp-with-keyword org-deadline-string)
  9495. (message "Item no longer has a deadline."))
  9496. (if (org-get-repeat)
  9497. (error "Cannot change deadline on task with repeater, please do that by hand")
  9498. (org-add-planning-info 'deadline time 'closed)
  9499. (when (and old-date org-log-redeadline
  9500. (not (equal old-date
  9501. (substring org-last-inserted-timestamp 1 -1))))
  9502. (org-add-log-setup 'redeadline nil old-date 'findpos
  9503. org-log-redeadline))
  9504. (message "Deadline on %s" org-last-inserted-timestamp)))))
  9505. (defun org-schedule (&optional remove time)
  9506. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9507. With argument REMOVE, remove any scheduling date from the item.
  9508. When TIME is set, it should be an internal time specification, and the
  9509. scheduling will use the corresponding date."
  9510. (interactive "P")
  9511. (let ((old-date (org-entry-get nil "SCHEDULED")))
  9512. (if remove
  9513. (progn
  9514. (org-remove-timestamp-with-keyword org-scheduled-string)
  9515. (message "Item is no longer scheduled."))
  9516. (if (org-get-repeat)
  9517. (error "Cannot reschedule task with repeater, please do that by hand")
  9518. (org-add-planning-info 'scheduled time 'closed)
  9519. (when (and old-date org-log-reschedule
  9520. (not (equal old-date
  9521. (substring org-last-inserted-timestamp 1 -1))))
  9522. (org-add-log-setup 'reschedule nil old-date 'findpos
  9523. org-log-reschedule))
  9524. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  9525. (defun org-get-scheduled-time (pom &optional inherit)
  9526. "Get the scheduled time as a time tuple, of a format suitable
  9527. for calling org-schedule with, or if there is no scheduling,
  9528. returns nil."
  9529. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9530. (when time
  9531. (apply 'encode-time (org-parse-time-string time)))))
  9532. (defun org-get-deadline-time (pom &optional inherit)
  9533. "Get the deadine as a time tuple, of a format suitable for
  9534. calling org-deadline with, or if there is no scheduling, returns
  9535. nil."
  9536. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9537. (when time
  9538. (apply 'encode-time (org-parse-time-string time)))))
  9539. (defun org-remove-timestamp-with-keyword (keyword)
  9540. "Remove all time stamps with KEYWORD in the current entry."
  9541. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9542. beg)
  9543. (save-excursion
  9544. (org-back-to-heading t)
  9545. (setq beg (point))
  9546. (outline-next-heading)
  9547. (while (re-search-backward re beg t)
  9548. (replace-match "")
  9549. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9550. (equal (char-before) ?\ ))
  9551. (backward-delete-char 1)
  9552. (if (string-match "^[ \t]*$" (buffer-substring
  9553. (point-at-bol) (point-at-eol)))
  9554. (delete-region (point-at-bol)
  9555. (min (point-max) (1+ (point-at-eol))))))))))
  9556. (defun org-add-planning-info (what &optional time &rest remove)
  9557. "Insert new timestamp with keyword in the line directly after the headline.
  9558. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9559. If non is given, the user is prompted for a date.
  9560. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9561. be removed."
  9562. (interactive)
  9563. (let (org-time-was-given org-end-time-was-given ts
  9564. end default-time default-input)
  9565. (catch 'exit
  9566. (when (and (not time) (memq what '(scheduled deadline)))
  9567. ;; Try to get a default date/time from existing timestamp
  9568. (save-excursion
  9569. (org-back-to-heading t)
  9570. (setq end (save-excursion (outline-next-heading) (point)))
  9571. (when (re-search-forward (if (eq what 'scheduled)
  9572. org-scheduled-time-regexp
  9573. org-deadline-time-regexp)
  9574. end t)
  9575. (setq ts (match-string 1)
  9576. default-time
  9577. (apply 'encode-time (org-parse-time-string ts))
  9578. default-input (and ts (org-get-compact-tod ts))))))
  9579. (when what
  9580. ;; If necessary, get the time from the user
  9581. (setq time (or time (org-read-date nil 'to-time nil nil
  9582. default-time default-input))))
  9583. (when (and org-insert-labeled-timestamps-at-point
  9584. (member what '(scheduled deadline)))
  9585. (insert
  9586. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9587. (org-insert-time-stamp time org-time-was-given
  9588. nil nil nil (list org-end-time-was-given))
  9589. (setq what nil))
  9590. (save-excursion
  9591. (save-restriction
  9592. (let (col list elt ts buffer-invisibility-spec)
  9593. (org-back-to-heading t)
  9594. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9595. (goto-char (match-end 1))
  9596. (setq col (current-column))
  9597. (goto-char (match-end 0))
  9598. (if (eobp) (insert "\n") (forward-char 1))
  9599. (when (and (not what)
  9600. (not (looking-at
  9601. (concat "[ \t]*"
  9602. org-keyword-time-not-clock-regexp))))
  9603. ;; Nothing to add, nothing to remove...... :-)
  9604. (throw 'exit nil))
  9605. (if (and (not (looking-at outline-regexp))
  9606. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9607. "[^\r\n]*"))
  9608. (not (equal (match-string 1) org-clock-string)))
  9609. (narrow-to-region (match-beginning 0) (match-end 0))
  9610. (insert-before-markers "\n")
  9611. (backward-char 1)
  9612. (narrow-to-region (point) (point))
  9613. (and org-adapt-indentation (org-indent-to-column col)))
  9614. ;; Check if we have to remove something.
  9615. (setq list (cons what remove))
  9616. (while list
  9617. (setq elt (pop list))
  9618. (goto-char (point-min))
  9619. (when (or (and (eq elt 'scheduled)
  9620. (re-search-forward org-scheduled-time-regexp nil t))
  9621. (and (eq elt 'deadline)
  9622. (re-search-forward org-deadline-time-regexp nil t))
  9623. (and (eq elt 'closed)
  9624. (re-search-forward org-closed-time-regexp nil t)))
  9625. (replace-match "")
  9626. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9627. (skip-chars-backward " ")
  9628. (if (looking-at " +") (replace-match ""))))
  9629. (goto-char (point-max))
  9630. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9631. (when what
  9632. (insert
  9633. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9634. (cond ((eq what 'scheduled) org-scheduled-string)
  9635. ((eq what 'deadline) org-deadline-string)
  9636. ((eq what 'closed) org-closed-string))
  9637. " ")
  9638. (setq ts (org-insert-time-stamp
  9639. time
  9640. (or org-time-was-given
  9641. (and (eq what 'closed) org-log-done-with-time))
  9642. (eq what 'closed)
  9643. nil nil (list org-end-time-was-given)))
  9644. (end-of-line 1))
  9645. (goto-char (point-min))
  9646. (widen)
  9647. (if (and (looking-at "[ \t]+\n")
  9648. (equal (char-before) ?\n))
  9649. (delete-region (1- (point)) (point-at-eol)))
  9650. ts))))))
  9651. (defvar org-log-note-marker (make-marker))
  9652. (defvar org-log-note-purpose nil)
  9653. (defvar org-log-note-state nil)
  9654. (defvar org-log-note-previous-state nil)
  9655. (defvar org-log-note-how nil)
  9656. (defvar org-log-note-extra nil)
  9657. (defvar org-log-note-window-configuration nil)
  9658. (defvar org-log-note-return-to (make-marker))
  9659. (defvar org-log-post-message nil
  9660. "Message to be displayed after a log note has been stored.
  9661. The auto-repeater uses this.")
  9662. (defun org-add-note ()
  9663. "Add a note to the current entry.
  9664. This is done in the same way as adding a state change note."
  9665. (interactive)
  9666. (org-add-log-setup 'note nil nil 'findpos nil))
  9667. (defvar org-property-end-re)
  9668. (defun org-add-log-setup (&optional purpose state prev-state
  9669. findpos how &optional extra)
  9670. "Set up the post command hook to take a note.
  9671. If this is about to TODO state change, the new state is expected in STATE.
  9672. When FINDPOS is non-nil, find the correct position for the note in
  9673. the current entry. If not, assume that it can be inserted at point.
  9674. HOW is an indicator what kind of note should be created.
  9675. EXTRA is additional text that will be inserted into the notes buffer."
  9676. (let* ((org-log-into-drawer (org-log-into-drawer))
  9677. (drawer (cond ((stringp org-log-into-drawer)
  9678. org-log-into-drawer)
  9679. (org-log-into-drawer "LOGBOOK")
  9680. (t nil))))
  9681. (save-restriction
  9682. (save-excursion
  9683. (when findpos
  9684. (org-back-to-heading t)
  9685. (narrow-to-region (point) (save-excursion
  9686. (outline-next-heading) (point)))
  9687. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9688. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9689. "[^\r\n]*\\)?"))
  9690. (goto-char (match-end 0))
  9691. (cond
  9692. (drawer
  9693. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9694. nil t)
  9695. (progn
  9696. (goto-char (match-end 0))
  9697. (or org-log-states-order-reversed
  9698. (and (re-search-forward org-property-end-re nil t)
  9699. (goto-char (1- (match-beginning 0))))))
  9700. (insert "\n:" drawer ":\n:END:")
  9701. (beginning-of-line 0)
  9702. (org-indent-line-function)
  9703. (beginning-of-line 2)
  9704. (org-indent-line-function)
  9705. (end-of-line 0)))
  9706. ((and org-log-state-notes-insert-after-drawers
  9707. (save-excursion
  9708. (forward-line) (looking-at org-drawer-regexp)))
  9709. (forward-line)
  9710. (while (looking-at org-drawer-regexp)
  9711. (goto-char (match-end 0))
  9712. (re-search-forward org-property-end-re (point-max) t)
  9713. (forward-line))
  9714. (forward-line -1)))
  9715. (unless org-log-states-order-reversed
  9716. (and (= (char-after) ?\n) (forward-char 1))
  9717. (org-skip-over-state-notes)
  9718. (skip-chars-backward " \t\n\r")))
  9719. (move-marker org-log-note-marker (point))
  9720. (setq org-log-note-purpose purpose
  9721. org-log-note-state state
  9722. org-log-note-previous-state prev-state
  9723. org-log-note-how how
  9724. org-log-note-extra extra)
  9725. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9726. (defun org-skip-over-state-notes ()
  9727. "Skip past the list of State notes in an entry."
  9728. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9729. (while (looking-at "[ \t]*- State")
  9730. (condition-case nil
  9731. (org-next-item)
  9732. (error (org-end-of-item)))))
  9733. (defun org-add-log-note (&optional purpose)
  9734. "Pop up a window for taking a note, and add this note later at point."
  9735. (remove-hook 'post-command-hook 'org-add-log-note)
  9736. (setq org-log-note-window-configuration (current-window-configuration))
  9737. (delete-other-windows)
  9738. (move-marker org-log-note-return-to (point))
  9739. (switch-to-buffer (marker-buffer org-log-note-marker))
  9740. (goto-char org-log-note-marker)
  9741. (org-switch-to-buffer-other-window "*Org Note*")
  9742. (erase-buffer)
  9743. (if (memq org-log-note-how '(time state))
  9744. (let (current-prefix-arg) (org-store-log-note))
  9745. (let ((org-inhibit-startup t)) (org-mode))
  9746. (insert (format "# Insert note for %s.
  9747. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9748. (cond
  9749. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9750. ((eq org-log-note-purpose 'done) "closed todo item")
  9751. ((eq org-log-note-purpose 'state)
  9752. (format "state change from \"%s\" to \"%s\""
  9753. (or org-log-note-previous-state "")
  9754. (or org-log-note-state "")))
  9755. ((eq org-log-note-purpose 'reschedule)
  9756. "rescheduling")
  9757. ((eq org-log-note-purpose 'redeadline)
  9758. "changing deadline")
  9759. ((eq org-log-note-purpose 'note)
  9760. "this entry")
  9761. (t (error "This should not happen")))))
  9762. (if org-log-note-extra (insert org-log-note-extra))
  9763. (org-set-local 'org-finish-function 'org-store-log-note)))
  9764. (defvar org-note-abort nil) ; dynamically scoped
  9765. (defun org-store-log-note ()
  9766. "Finish taking a log note, and insert it to where it belongs."
  9767. (let ((txt (buffer-string))
  9768. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9769. lines ind)
  9770. (kill-buffer (current-buffer))
  9771. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9772. (setq txt (replace-match "" t t txt)))
  9773. (if (string-match "\\s-+\\'" txt)
  9774. (setq txt (replace-match "" t t txt)))
  9775. (setq lines (org-split-string txt "\n"))
  9776. (when (and note (string-match "\\S-" note))
  9777. (setq note
  9778. (org-replace-escapes
  9779. note
  9780. (list (cons "%u" (user-login-name))
  9781. (cons "%U" user-full-name)
  9782. (cons "%t" (format-time-string
  9783. (org-time-stamp-format 'long 'inactive)
  9784. (current-time)))
  9785. (cons "%s" (if org-log-note-state
  9786. (concat "\"" org-log-note-state "\"")
  9787. ""))
  9788. (cons "%S" (if org-log-note-previous-state
  9789. (concat "\"" org-log-note-previous-state "\"")
  9790. "\"\"")))))
  9791. (if lines (setq note (concat note " \\\\")))
  9792. (push note lines))
  9793. (when (or current-prefix-arg org-note-abort)
  9794. (when org-log-into-drawer
  9795. (org-remove-empty-drawer-at
  9796. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9797. org-log-note-marker))
  9798. (setq lines nil))
  9799. (when lines
  9800. (with-current-buffer (marker-buffer org-log-note-marker)
  9801. (save-excursion
  9802. (goto-char org-log-note-marker)
  9803. (move-marker org-log-note-marker nil)
  9804. (end-of-line 1)
  9805. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9806. (insert "- " (pop lines))
  9807. (org-indent-line-function)
  9808. (beginning-of-line 1)
  9809. (looking-at "[ \t]*")
  9810. (setq ind (concat (match-string 0) " "))
  9811. (end-of-line 1)
  9812. (while lines (insert "\n" ind (pop lines)))
  9813. (message "Note stored")
  9814. (org-back-to-heading t)
  9815. (org-cycle-hide-drawers 'children)))))
  9816. (set-window-configuration org-log-note-window-configuration)
  9817. (with-current-buffer (marker-buffer org-log-note-return-to)
  9818. (goto-char org-log-note-return-to))
  9819. (move-marker org-log-note-return-to nil)
  9820. (and org-log-post-message (message "%s" org-log-post-message)))
  9821. (defun org-remove-empty-drawer-at (drawer pos)
  9822. "Remove an empty drawer DRAWER at position POS.
  9823. POS may also be a marker."
  9824. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9825. (save-excursion
  9826. (save-restriction
  9827. (widen)
  9828. (goto-char pos)
  9829. (if (org-in-regexp
  9830. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9831. (replace-match ""))))))
  9832. (defun org-sparse-tree (&optional arg)
  9833. "Create a sparse tree, prompt for the details.
  9834. This command can create sparse trees. You first need to select the type
  9835. of match used to create the tree:
  9836. t Show entries with a specific TODO keyword.
  9837. m Show entries selected by a tags/property match.
  9838. p Enter a property name and its value (both with completion on existing
  9839. names/values) and show entries with that property.
  9840. / Show entries matching a regular expression (`r' can be used as well)
  9841. d Show deadlines due within `org-deadline-warning-days'.
  9842. b Show deadlines and scheduled items before a date.
  9843. a Show deadlines and scheduled items after a date."
  9844. (interactive "P")
  9845. (let (ans kwd value)
  9846. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9847. (setq ans (read-char-exclusive))
  9848. (cond
  9849. ((equal ans ?d)
  9850. (call-interactively 'org-check-deadlines))
  9851. ((equal ans ?b)
  9852. (call-interactively 'org-check-before-date))
  9853. ((equal ans ?a)
  9854. (call-interactively 'org-check-after-date))
  9855. ((equal ans ?t)
  9856. (org-show-todo-tree '(4)))
  9857. ((member ans '(?T ?m))
  9858. (call-interactively 'org-match-sparse-tree))
  9859. ((member ans '(?p ?P))
  9860. (setq kwd (org-icompleting-read "Property: "
  9861. (mapcar 'list (org-buffer-property-keys))))
  9862. (setq value (org-icompleting-read "Value: "
  9863. (mapcar 'list (org-property-values kwd))))
  9864. (unless (string-match "\\`{.*}\\'" value)
  9865. (setq value (concat "\"" value "\"")))
  9866. (org-match-sparse-tree arg (concat kwd "=" value)))
  9867. ((member ans '(?r ?R ?/))
  9868. (call-interactively 'org-occur))
  9869. (t (error "No such sparse tree command \"%c\"" ans)))))
  9870. (defvar org-occur-highlights nil
  9871. "List of overlays used for occur matches.")
  9872. (make-variable-buffer-local 'org-occur-highlights)
  9873. (defvar org-occur-parameters nil
  9874. "Parameters of the active org-occur calls.
  9875. This is a list, each call to org-occur pushes as cons cell,
  9876. containing the regular expression and the callback, onto the list.
  9877. The list can contain several entries if `org-occur' has been called
  9878. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9879. will only contain one set of parameters. When the highlights are
  9880. removed (for example with `C-c C-c', or with the next edit (depending
  9881. on `org-remove-highlights-with-change'), this variable is emptied
  9882. as well.")
  9883. (make-variable-buffer-local 'org-occur-parameters)
  9884. (defun org-occur (regexp &optional keep-previous callback)
  9885. "Make a compact tree which shows all matches of REGEXP.
  9886. The tree will show the lines where the regexp matches, and all higher
  9887. headlines above the match. It will also show the heading after the match,
  9888. to make sure editing the matching entry is easy.
  9889. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9890. call to `org-occur' will be kept, to allow stacking of calls to this
  9891. command.
  9892. If CALLBACK is non-nil, it is a function which is called to confirm
  9893. that the match should indeed be shown."
  9894. (interactive "sRegexp: \nP")
  9895. (when (equal regexp "")
  9896. (error "Regexp cannot be empty"))
  9897. (unless keep-previous
  9898. (org-remove-occur-highlights nil nil t))
  9899. (push (cons regexp callback) org-occur-parameters)
  9900. (let ((cnt 0))
  9901. (save-excursion
  9902. (goto-char (point-min))
  9903. (if (or (not keep-previous) ; do not want to keep
  9904. (not org-occur-highlights)) ; no previous matches
  9905. ;; hide everything
  9906. (org-overview))
  9907. (while (re-search-forward regexp nil t)
  9908. (when (or (not callback)
  9909. (save-match-data (funcall callback)))
  9910. (setq cnt (1+ cnt))
  9911. (when org-highlight-sparse-tree-matches
  9912. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9913. (org-show-context 'occur-tree))))
  9914. (when org-remove-highlights-with-change
  9915. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9916. nil 'local))
  9917. (unless org-sparse-tree-open-archived-trees
  9918. (org-hide-archived-subtrees (point-min) (point-max)))
  9919. (run-hooks 'org-occur-hook)
  9920. (if (interactive-p)
  9921. (message "%d match(es) for regexp %s" cnt regexp))
  9922. cnt))
  9923. (defun org-show-context (&optional key)
  9924. "Make sure point and context and visible.
  9925. How much context is shown depends upon the variables
  9926. `org-show-hierarchy-above', `org-show-following-heading'. and
  9927. `org-show-siblings'."
  9928. (let ((heading-p (org-on-heading-p t))
  9929. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9930. (following-p (org-get-alist-option org-show-following-heading key))
  9931. (entry-p (org-get-alist-option org-show-entry-below key))
  9932. (siblings-p (org-get-alist-option org-show-siblings key)))
  9933. (catch 'exit
  9934. ;; Show heading or entry text
  9935. (if (and heading-p (not entry-p))
  9936. (org-flag-heading nil) ; only show the heading
  9937. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9938. (org-show-hidden-entry))) ; show entire entry
  9939. (when following-p
  9940. ;; Show next sibling, or heading below text
  9941. (save-excursion
  9942. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9943. (org-flag-heading nil))))
  9944. (when siblings-p (org-show-siblings))
  9945. (when hierarchy-p
  9946. ;; show all higher headings, possibly with siblings
  9947. (save-excursion
  9948. (while (and (condition-case nil
  9949. (progn (org-up-heading-all 1) t)
  9950. (error nil))
  9951. (not (bobp)))
  9952. (org-flag-heading nil)
  9953. (when siblings-p (org-show-siblings))))))))
  9954. (defun org-reveal (&optional siblings)
  9955. "Show current entry, hierarchy above it, and the following headline.
  9956. This can be used to show a consistent set of context around locations
  9957. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9958. not t for the search context.
  9959. With optional argument SIBLINGS, on each level of the hierarchy all
  9960. siblings are shown. This repairs the tree structure to what it would
  9961. look like when opened with hierarchical calls to `org-cycle'."
  9962. (interactive "P")
  9963. (let ((org-show-hierarchy-above t)
  9964. (org-show-following-heading t)
  9965. (org-show-siblings (if siblings t org-show-siblings)))
  9966. (org-show-context nil)))
  9967. (defun org-highlight-new-match (beg end)
  9968. "Highlight from BEG to END and mark the highlight is an occur headline."
  9969. (let ((ov (org-make-overlay beg end)))
  9970. (org-overlay-put ov 'face 'secondary-selection)
  9971. (push ov org-occur-highlights)))
  9972. (defun org-remove-occur-highlights (&optional beg end noremove)
  9973. "Remove the occur highlights from the buffer.
  9974. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9975. from the `before-change-functions' in the current buffer."
  9976. (interactive)
  9977. (unless org-inhibit-highlight-removal
  9978. (mapc 'org-delete-overlay org-occur-highlights)
  9979. (setq org-occur-highlights nil)
  9980. (setq org-occur-parameters nil)
  9981. (unless noremove
  9982. (remove-hook 'before-change-functions
  9983. 'org-remove-occur-highlights 'local))))
  9984. ;;;; Priorities
  9985. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9986. "Regular expression matching the priority indicator.")
  9987. (defvar org-remove-priority-next-time nil)
  9988. (defun org-priority-up ()
  9989. "Increase the priority of the current item."
  9990. (interactive)
  9991. (org-priority 'up))
  9992. (defun org-priority-down ()
  9993. "Decrease the priority of the current item."
  9994. (interactive)
  9995. (org-priority 'down))
  9996. (defun org-priority (&optional action)
  9997. "Change the priority of an item by ARG.
  9998. ACTION can be `set', `up', `down', or a character."
  9999. (interactive)
  10000. (unless org-enable-priority-commands
  10001. (error "Priority commands are disabled"))
  10002. (setq action (or action 'set))
  10003. (let (current new news have remove)
  10004. (save-excursion
  10005. (org-back-to-heading t)
  10006. (if (looking-at org-priority-regexp)
  10007. (setq current (string-to-char (match-string 2))
  10008. have t)
  10009. (setq current org-default-priority))
  10010. (cond
  10011. ((eq action 'remove)
  10012. (setq remove t new ?\ ))
  10013. ((or (eq action 'set)
  10014. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10015. (if (not (eq action 'set))
  10016. (setq new action)
  10017. (message "Priority %c-%c, SPC to remove: "
  10018. org-highest-priority org-lowest-priority)
  10019. (setq new (read-char-exclusive)))
  10020. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10021. (= (upcase org-lowest-priority) org-lowest-priority))
  10022. (setq new (upcase new)))
  10023. (cond ((equal new ?\ ) (setq remove t))
  10024. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10025. (error "Priority must be between `%c' and `%c'"
  10026. org-highest-priority org-lowest-priority))))
  10027. ((eq action 'up)
  10028. (if (and (not have) (eq last-command this-command))
  10029. (setq new org-lowest-priority)
  10030. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10031. org-default-priority (1- current)))))
  10032. ((eq action 'down)
  10033. (if (and (not have) (eq last-command this-command))
  10034. (setq new org-highest-priority)
  10035. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10036. org-default-priority (1+ current)))))
  10037. (t (error "Invalid action")))
  10038. (if (or (< (upcase new) org-highest-priority)
  10039. (> (upcase new) org-lowest-priority))
  10040. (setq remove t))
  10041. (setq news (format "%c" new))
  10042. (if have
  10043. (if remove
  10044. (replace-match "" t t nil 1)
  10045. (replace-match news t t nil 2))
  10046. (if remove
  10047. (error "No priority cookie found in line")
  10048. (let ((case-fold-search nil))
  10049. (looking-at org-todo-line-regexp))
  10050. (if (match-end 2)
  10051. (progn
  10052. (goto-char (match-end 2))
  10053. (insert " [#" news "]"))
  10054. (goto-char (match-beginning 3))
  10055. (insert "[#" news "] "))))
  10056. (org-preserve-lc (org-set-tags nil 'align)))
  10057. (if remove
  10058. (message "Priority removed")
  10059. (message "Priority of current item set to %s" news))))
  10060. (defun org-get-priority (s)
  10061. "Find priority cookie and return priority."
  10062. (save-match-data
  10063. (if (not (string-match org-priority-regexp s))
  10064. (* 1000 (- org-lowest-priority org-default-priority))
  10065. (* 1000 (- org-lowest-priority
  10066. (string-to-char (match-string 2 s)))))))
  10067. ;;;; Tags
  10068. (defvar org-agenda-archives-mode)
  10069. (defvar org-map-continue-from nil
  10070. "Position from where mapping should continue.
  10071. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10072. (defvar org-scanner-tags nil
  10073. "The current tag list while the tags scanner is running.")
  10074. (defvar org-trust-scanner-tags nil
  10075. "Should `org-get-tags-at' use the tags fro the scanner.
  10076. This is for internal dynamical scoping only.
  10077. When this is non-nil, the function `org-get-tags-at' will return the value
  10078. of `org-scanner-tags' instead of building the list by itself. This
  10079. can lead to large speed-ups when the tags scanner is used in a file with
  10080. many entries, and when the list of tags is retrieved, for example to
  10081. obtain a list of properties. Building the tags list for each entry in such
  10082. a file becomes an N^2 operation - but with this variable set, it scales
  10083. as N.")
  10084. (defun org-scan-tags (action matcher &optional todo-only)
  10085. "Scan headline tags with inheritance and produce output ACTION.
  10086. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10087. or `agenda' to produce an entry list for an agenda view. It can also be
  10088. a Lisp form or a function that should be called at each matched headline, in
  10089. this case the return value is a list of all return values from these calls.
  10090. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10091. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10092. only lines with a TODO keyword are included in the output."
  10093. (require 'org-agenda)
  10094. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10095. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10096. (org-re
  10097. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10098. (props (list 'face 'default
  10099. 'done-face 'org-agenda-done
  10100. 'undone-face 'default
  10101. 'mouse-face 'highlight
  10102. 'org-not-done-regexp org-not-done-regexp
  10103. 'org-todo-regexp org-todo-regexp
  10104. 'help-echo
  10105. (format "mouse-2 or RET jump to org file %s"
  10106. (abbreviate-file-name
  10107. (or (buffer-file-name (buffer-base-buffer))
  10108. (buffer-name (buffer-base-buffer)))))))
  10109. (case-fold-search nil)
  10110. (org-map-continue-from nil)
  10111. lspos tags tags-list
  10112. (tags-alist (list (cons 0 org-file-tags)))
  10113. (llast 0) rtn rtn1 level category i txt
  10114. todo marker entry priority)
  10115. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10116. (setq action (list 'lambda nil action)))
  10117. (save-excursion
  10118. (goto-char (point-min))
  10119. (when (eq action 'sparse-tree)
  10120. (org-overview)
  10121. (org-remove-occur-highlights))
  10122. (while (re-search-forward re nil t)
  10123. (catch :skip
  10124. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10125. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10126. (goto-char (setq lspos (match-beginning 0)))
  10127. (setq level (org-reduced-level (funcall outline-level))
  10128. category (org-get-category))
  10129. (setq i llast llast level)
  10130. ;; remove tag lists from same and sublevels
  10131. (while (>= i level)
  10132. (when (setq entry (assoc i tags-alist))
  10133. (setq tags-alist (delete entry tags-alist)))
  10134. (setq i (1- i)))
  10135. ;; add the next tags
  10136. (when tags
  10137. (setq tags (org-split-string tags ":")
  10138. tags-alist
  10139. (cons (cons level tags) tags-alist)))
  10140. ;; compile tags for current headline
  10141. (setq tags-list
  10142. (if org-use-tag-inheritance
  10143. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10144. tags)
  10145. org-scanner-tags tags-list)
  10146. (when org-use-tag-inheritance
  10147. (setcdr (car tags-alist)
  10148. (mapcar (lambda (x)
  10149. (setq x (copy-sequence x))
  10150. (org-add-prop-inherited x))
  10151. (cdar tags-alist))))
  10152. (when (and tags org-use-tag-inheritance
  10153. (or (not (eq t org-use-tag-inheritance))
  10154. org-tags-exclude-from-inheritance))
  10155. ;; selective inheritance, remove uninherited ones
  10156. (setcdr (car tags-alist)
  10157. (org-remove-uniherited-tags (cdar tags-alist))))
  10158. (when (and (or (not todo-only)
  10159. (and (member todo org-not-done-keywords)
  10160. (or (not org-agenda-tags-todo-honor-ignore-options)
  10161. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10162. (let ((case-fold-search t)) (eval matcher))
  10163. (or
  10164. (not (member org-archive-tag tags-list))
  10165. ;; we have an archive tag, should we use this anyway?
  10166. (or (not org-agenda-skip-archived-trees)
  10167. (and (eq action 'agenda) org-agenda-archives-mode))))
  10168. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10169. ;; select this headline
  10170. (cond
  10171. ((eq action 'sparse-tree)
  10172. (and org-highlight-sparse-tree-matches
  10173. (org-get-heading) (match-end 0)
  10174. (org-highlight-new-match
  10175. (match-beginning 0) (match-beginning 1)))
  10176. (org-show-context 'tags-tree))
  10177. ((eq action 'agenda)
  10178. (setq txt (org-format-agenda-item
  10179. ""
  10180. (concat
  10181. (if (eq org-tags-match-list-sublevels 'indented)
  10182. (make-string (1- level) ?.) "")
  10183. (org-get-heading))
  10184. category
  10185. tags-list
  10186. )
  10187. priority (org-get-priority txt))
  10188. (goto-char lspos)
  10189. (setq marker (org-agenda-new-marker))
  10190. (org-add-props txt props
  10191. 'org-marker marker 'org-hd-marker marker 'org-category category
  10192. 'todo-state todo
  10193. 'priority priority 'type "tagsmatch")
  10194. (push txt rtn))
  10195. ((functionp action)
  10196. (setq org-map-continue-from nil)
  10197. (save-excursion
  10198. (setq rtn1 (funcall action))
  10199. (push rtn1 rtn)))
  10200. (t (error "Invalid action")))
  10201. ;; if we are to skip sublevels, jump to end of subtree
  10202. (unless org-tags-match-list-sublevels
  10203. (org-end-of-subtree t)
  10204. (backward-char 1))))
  10205. ;; Get the correct position from where to continue
  10206. (if org-map-continue-from
  10207. (goto-char org-map-continue-from)
  10208. (and (= (point) lspos) (end-of-line 1)))))
  10209. (when (and (eq action 'sparse-tree)
  10210. (not org-sparse-tree-open-archived-trees))
  10211. (org-hide-archived-subtrees (point-min) (point-max)))
  10212. (nreverse rtn)))
  10213. (defun org-remove-uniherited-tags (tags)
  10214. "Remove all tags that are not inherited from the list TAGS."
  10215. (cond
  10216. ((eq org-use-tag-inheritance t)
  10217. (if org-tags-exclude-from-inheritance
  10218. (org-delete-all org-tags-exclude-from-inheritance tags)
  10219. tags))
  10220. ((not org-use-tag-inheritance) nil)
  10221. ((stringp org-use-tag-inheritance)
  10222. (delq nil (mapcar
  10223. (lambda (x)
  10224. (if (and (string-match org-use-tag-inheritance x)
  10225. (not (member x org-tags-exclude-from-inheritance)))
  10226. x nil))
  10227. tags)))
  10228. ((listp org-use-tag-inheritance)
  10229. (delq nil (mapcar
  10230. (lambda (x)
  10231. (if (member x org-use-tag-inheritance) x nil))
  10232. tags)))))
  10233. (defvar todo-only) ;; dynamically scoped
  10234. (defun org-match-sparse-tree (&optional todo-only match)
  10235. "Create a sparse tree according to tags string MATCH.
  10236. MATCH can contain positive and negative selection of tags, like
  10237. \"+WORK+URGENT-WITHBOSS\".
  10238. If optional argument TODO-ONLY is non-nil, only select lines that are
  10239. also TODO lines."
  10240. (interactive "P")
  10241. (org-prepare-agenda-buffers (list (current-buffer)))
  10242. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10243. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10244. (defvar org-cached-props nil)
  10245. (defun org-cached-entry-get (pom property)
  10246. (if (or (eq t org-use-property-inheritance)
  10247. (and (stringp org-use-property-inheritance)
  10248. (string-match org-use-property-inheritance property))
  10249. (and (listp org-use-property-inheritance)
  10250. (member property org-use-property-inheritance)))
  10251. ;; Caching is not possible, check it directly
  10252. (org-entry-get pom property 'inherit)
  10253. ;; Get all properties, so that we can do complicated checks easily
  10254. (cdr (assoc property (or org-cached-props
  10255. (setq org-cached-props
  10256. (org-entry-properties pom)))))))
  10257. (defun org-global-tags-completion-table (&optional files)
  10258. "Return the list of all tags in all agenda buffer/files."
  10259. (save-excursion
  10260. (org-uniquify
  10261. (delq nil
  10262. (apply 'append
  10263. (mapcar
  10264. (lambda (file)
  10265. (set-buffer (find-file-noselect file))
  10266. (append (org-get-buffer-tags)
  10267. (mapcar (lambda (x) (if (stringp (car-safe x))
  10268. (list (car-safe x)) nil))
  10269. org-tag-alist)))
  10270. (if (and files (car files))
  10271. files
  10272. (org-agenda-files))))))))
  10273. (defun org-make-tags-matcher (match)
  10274. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10275. ;; todo-only is scoped dynamically into this function, and the function
  10276. ;; may change it if the matcher asks for it.
  10277. (unless match
  10278. ;; Get a new match request, with completion
  10279. (let ((org-last-tags-completion-table
  10280. (org-global-tags-completion-table)))
  10281. (setq match (org-completing-read-no-i
  10282. "Match: " 'org-tags-completion-function nil nil nil
  10283. 'org-tags-history))))
  10284. ;; Parse the string and create a lisp form
  10285. (let ((match0 match)
  10286. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10287. minus tag mm
  10288. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10289. orterms term orlist re-p str-p level-p level-op time-p
  10290. prop-p pn pv po cat-p gv rest)
  10291. (if (string-match "/+" match)
  10292. ;; match contains also a todo-matching request
  10293. (progn
  10294. (setq tagsmatch (substring match 0 (match-beginning 0))
  10295. todomatch (substring match (match-end 0)))
  10296. (if (string-match "^!" todomatch)
  10297. (setq todo-only t todomatch (substring todomatch 1)))
  10298. (if (string-match "^\\s-*$" todomatch)
  10299. (setq todomatch nil)))
  10300. ;; only matching tags
  10301. (setq tagsmatch match todomatch nil))
  10302. ;; Make the tags matcher
  10303. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10304. (setq tagsmatcher t)
  10305. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10306. (while (setq term (pop orterms))
  10307. (while (and (equal (substring term -1) "\\") orterms)
  10308. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10309. (while (string-match re term)
  10310. (setq rest (substring term (match-end 0))
  10311. minus (and (match-end 1)
  10312. (equal (match-string 1 term) "-"))
  10313. tag (match-string 2 term)
  10314. re-p (equal (string-to-char tag) ?{)
  10315. level-p (match-end 4)
  10316. prop-p (match-end 5)
  10317. mm (cond
  10318. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10319. (level-p
  10320. (setq level-op (org-op-to-function (match-string 3 term)))
  10321. `(,level-op level ,(string-to-number
  10322. (match-string 4 term))))
  10323. (prop-p
  10324. (setq pn (match-string 5 term)
  10325. po (match-string 6 term)
  10326. pv (match-string 7 term)
  10327. cat-p (equal pn "CATEGORY")
  10328. re-p (equal (string-to-char pv) ?{)
  10329. str-p (equal (string-to-char pv) ?\")
  10330. time-p (save-match-data
  10331. (string-match "^\"[[<].*[]>]\"$" pv))
  10332. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10333. (if time-p (setq pv (org-matcher-time pv)))
  10334. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10335. (cond
  10336. ((equal pn "CATEGORY")
  10337. (setq gv '(get-text-property (point) 'org-category)))
  10338. ((equal pn "TODO")
  10339. (setq gv 'todo))
  10340. (t
  10341. (setq gv `(org-cached-entry-get nil ,pn))))
  10342. (if re-p
  10343. (if (eq po 'org<>)
  10344. `(not (string-match ,pv (or ,gv "")))
  10345. `(string-match ,pv (or ,gv "")))
  10346. (if str-p
  10347. `(,po (or ,gv "") ,pv)
  10348. `(,po (string-to-number (or ,gv ""))
  10349. ,(string-to-number pv) ))))
  10350. (t `(member ,tag tags-list)))
  10351. mm (if minus (list 'not mm) mm)
  10352. term rest)
  10353. (push mm tagsmatcher))
  10354. (push (if (> (length tagsmatcher) 1)
  10355. (cons 'and tagsmatcher)
  10356. (car tagsmatcher))
  10357. orlist)
  10358. (setq tagsmatcher nil))
  10359. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10360. (setq tagsmatcher
  10361. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10362. ;; Make the todo matcher
  10363. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10364. (setq todomatcher t)
  10365. (setq orterms (org-split-string todomatch "|") orlist nil)
  10366. (while (setq term (pop orterms))
  10367. (while (string-match re term)
  10368. (setq minus (and (match-end 1)
  10369. (equal (match-string 1 term) "-"))
  10370. kwd (match-string 2 term)
  10371. re-p (equal (string-to-char kwd) ?{)
  10372. term (substring term (match-end 0))
  10373. mm (if re-p
  10374. `(string-match ,(substring kwd 1 -1) todo)
  10375. (list 'equal 'todo kwd))
  10376. mm (if minus (list 'not mm) mm))
  10377. (push mm todomatcher))
  10378. (push (if (> (length todomatcher) 1)
  10379. (cons 'and todomatcher)
  10380. (car todomatcher))
  10381. orlist)
  10382. (setq todomatcher nil))
  10383. (setq todomatcher (if (> (length orlist) 1)
  10384. (cons 'or orlist) (car orlist))))
  10385. ;; Return the string and lisp forms of the matcher
  10386. (setq matcher (if todomatcher
  10387. (list 'and tagsmatcher todomatcher)
  10388. tagsmatcher))
  10389. (cons match0 matcher)))
  10390. (defun org-op-to-function (op &optional stringp)
  10391. "Turn an operator into the appropriate function."
  10392. (setq op
  10393. (cond
  10394. ((equal op "<" ) '(< string< org-time<))
  10395. ((equal op ">" ) '(> org-string> org-time>))
  10396. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10397. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10398. ((member op '("=" "==")) '(= string= org-time=))
  10399. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10400. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10401. (defun org<> (a b) (not (= a b)))
  10402. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10403. (defun org-string>= (a b) (not (string< a b)))
  10404. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10405. (defun org-string<> (a b) (not (string= a b)))
  10406. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10407. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10408. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10409. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10410. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10411. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10412. (defun org-2ft (s)
  10413. "Convert S to a floating point time.
  10414. If S is already a number, just return it. If it is a string, parse
  10415. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10416. (cond
  10417. ((numberp s) s)
  10418. ((stringp s)
  10419. (condition-case nil
  10420. (float-time (apply 'encode-time (org-parse-time-string s)))
  10421. (error 0.)))
  10422. (t 0.)))
  10423. (defun org-time-today ()
  10424. "Time in seconds today at 0:00.
  10425. Returns the float number of seconds since the beginning of the
  10426. epoch to the beginning of today (00:00)."
  10427. (float-time (apply 'encode-time
  10428. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10429. (defun org-matcher-time (s)
  10430. "Interpret a time comparison value."
  10431. (save-match-data
  10432. (cond
  10433. ((string= s "<now>") (float-time))
  10434. ((string= s "<today>") (org-time-today))
  10435. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10436. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10437. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10438. (+ (org-time-today)
  10439. (* (string-to-number (match-string 1 s))
  10440. (cdr (assoc (match-string 2 s)
  10441. '(("d" . 86400.0) ("w" . 604800.0)
  10442. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10443. (t (org-2ft s)))))
  10444. (defun org-match-any-p (re list)
  10445. "Does re match any element of list?"
  10446. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10447. (delq nil list))
  10448. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10449. (defvar org-tags-overlay (org-make-overlay 1 1))
  10450. (org-detach-overlay org-tags-overlay)
  10451. (defun org-get-local-tags-at (&optional pos)
  10452. "Get a list of tags defined in the current headline."
  10453. (org-get-tags-at pos 'local))
  10454. (defun org-get-local-tags ()
  10455. "Get a list of tags defined in the current headline."
  10456. (org-get-tags-at nil 'local))
  10457. (defun org-get-tags-at (&optional pos local)
  10458. "Get a list of all headline tags applicable at POS.
  10459. POS defaults to point. If tags are inherited, the list contains
  10460. the targets in the same sequence as the headlines appear, i.e.
  10461. the tags of the current headline come last.
  10462. When LOCAL is non-nil, only return tags from the current headline,
  10463. ignore inherited ones."
  10464. (interactive)
  10465. (if (and org-trust-scanner-tags
  10466. (or (not pos) (equal pos (point)))
  10467. (not local))
  10468. org-scanner-tags
  10469. (let (tags ltags lastpos parent)
  10470. (save-excursion
  10471. (save-restriction
  10472. (widen)
  10473. (goto-char (or pos (point)))
  10474. (save-match-data
  10475. (catch 'done
  10476. (condition-case nil
  10477. (progn
  10478. (org-back-to-heading t)
  10479. (while (not (equal lastpos (point)))
  10480. (setq lastpos (point))
  10481. (when (looking-at
  10482. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10483. (setq ltags (org-split-string
  10484. (org-match-string-no-properties 1) ":"))
  10485. (when parent
  10486. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10487. (setq tags (append
  10488. (if parent
  10489. (org-remove-uniherited-tags ltags)
  10490. ltags)
  10491. tags)))
  10492. (or org-use-tag-inheritance (throw 'done t))
  10493. (if local (throw 'done t))
  10494. (or (org-up-heading-safe) (error nil))
  10495. (setq parent t)))
  10496. (error nil)))))
  10497. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10498. (defun org-add-prop-inherited (s)
  10499. (add-text-properties 0 (length s) '(inherited t) s)
  10500. s)
  10501. (defun org-toggle-tag (tag &optional onoff)
  10502. "Toggle the tag TAG for the current line.
  10503. If ONOFF is `on' or `off', don't toggle but set to this state."
  10504. (let (res current)
  10505. (save-excursion
  10506. (org-back-to-heading t)
  10507. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10508. (point-at-eol) t)
  10509. (progn
  10510. (setq current (match-string 1))
  10511. (replace-match ""))
  10512. (setq current ""))
  10513. (setq current (nreverse (org-split-string current ":")))
  10514. (cond
  10515. ((eq onoff 'on)
  10516. (setq res t)
  10517. (or (member tag current) (push tag current)))
  10518. ((eq onoff 'off)
  10519. (or (not (member tag current)) (setq current (delete tag current))))
  10520. (t (if (member tag current)
  10521. (setq current (delete tag current))
  10522. (setq res t)
  10523. (push tag current))))
  10524. (end-of-line 1)
  10525. (if current
  10526. (progn
  10527. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10528. (org-set-tags nil t))
  10529. (delete-horizontal-space))
  10530. (run-hooks 'org-after-tags-change-hook))
  10531. res))
  10532. (defun org-align-tags-here (to-col)
  10533. ;; Assumes that this is a headline
  10534. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10535. (beginning-of-line 1)
  10536. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10537. (< pos (match-beginning 2)))
  10538. (progn
  10539. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10540. (goto-char (match-beginning 1))
  10541. (insert " ")
  10542. (delete-region (point) (1+ (match-beginning 2)))
  10543. (setq ncol (max (1+ (current-column))
  10544. (1+ col)
  10545. (if (> to-col 0)
  10546. to-col
  10547. (- (abs to-col) tags-l))))
  10548. (setq p (point))
  10549. (insert (make-string (- ncol (current-column)) ?\ ))
  10550. (setq ncol (current-column))
  10551. (when indent-tabs-mode (tabify p (point-at-eol)))
  10552. (org-move-to-column (min ncol col) t))
  10553. (goto-char pos))))
  10554. (defun org-set-tags-command (&optional arg just-align)
  10555. "Call the set-tags command for the current entry."
  10556. (interactive "P")
  10557. (if (org-on-heading-p)
  10558. (org-set-tags arg just-align)
  10559. (save-excursion
  10560. (org-back-to-heading t)
  10561. (org-set-tags arg just-align))))
  10562. (defun org-set-tags-to (data)
  10563. "Set the tags of the current entry to DATA, replacing the current tags.
  10564. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10565. If DATA is nil or the empty string, any tags will be removed."
  10566. (interactive "sTags: ")
  10567. (setq data
  10568. (cond
  10569. ((eq data nil) "")
  10570. ((equal data "") "")
  10571. ((stringp data)
  10572. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10573. ":"))
  10574. ((listp data)
  10575. (concat ":" (mapconcat 'identity data ":") ":"))
  10576. (t nil)))
  10577. (when data
  10578. (save-excursion
  10579. (org-back-to-heading t)
  10580. (when (looking-at org-complex-heading-regexp)
  10581. (if (match-end 5)
  10582. (progn
  10583. (goto-char (match-beginning 5))
  10584. (insert data)
  10585. (delete-region (point) (point-at-eol))
  10586. (org-set-tags nil 'align))
  10587. (goto-char (point-at-eol))
  10588. (insert " " data)
  10589. (org-set-tags nil 'align)))
  10590. (beginning-of-line 1)
  10591. (if (looking-at ".*?\\([ \t]+\\)$")
  10592. (delete-region (match-beginning 1) (match-end 1))))))
  10593. (defun org-set-tags (&optional arg just-align)
  10594. "Set the tags for the current headline.
  10595. With prefix ARG, realign all tags in headings in the current buffer."
  10596. (interactive "P")
  10597. (let* ((re (concat "^" outline-regexp))
  10598. (current (org-get-tags-string))
  10599. (col (current-column))
  10600. (org-setting-tags t)
  10601. table current-tags inherited-tags ; computed below when needed
  10602. tags p0 c0 c1 rpl)
  10603. (if arg
  10604. (save-excursion
  10605. (goto-char (point-min))
  10606. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10607. (while (re-search-forward re nil t)
  10608. (org-set-tags nil t)
  10609. (end-of-line 1)))
  10610. (message "All tags realigned to column %d" org-tags-column))
  10611. (if just-align
  10612. (setq tags current)
  10613. ;; Get a new set of tags from the user
  10614. (save-excursion
  10615. (setq table (append org-tag-persistent-alist
  10616. (or org-tag-alist (org-get-buffer-tags)))
  10617. org-last-tags-completion-table table
  10618. current-tags (org-split-string current ":")
  10619. inherited-tags (nreverse
  10620. (nthcdr (length current-tags)
  10621. (nreverse (org-get-tags-at))))
  10622. tags
  10623. (if (or (eq t org-use-fast-tag-selection)
  10624. (and org-use-fast-tag-selection
  10625. (delq nil (mapcar 'cdr table))))
  10626. (org-fast-tag-selection
  10627. current-tags inherited-tags table
  10628. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10629. (let ((org-add-colon-after-tag-completion t))
  10630. (org-trim
  10631. (org-without-partial-completion
  10632. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10633. nil nil current 'org-tags-history)))))))
  10634. (while (string-match "[-+&]+" tags)
  10635. ;; No boolean logic, just a list
  10636. (setq tags (replace-match ":" t t tags))))
  10637. (if org-tags-sort-function
  10638. (setq tags (mapconcat 'identity
  10639. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10640. org-tags-sort-function) ":")))
  10641. (if (string-match "\\`[\t ]*\\'" tags)
  10642. (setq tags "")
  10643. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10644. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10645. ;; Insert new tags at the correct column
  10646. (beginning-of-line 1)
  10647. (cond
  10648. ((and (equal current "") (equal tags "")))
  10649. ((re-search-forward
  10650. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10651. (point-at-eol) t)
  10652. (if (equal tags "")
  10653. (setq rpl "")
  10654. (goto-char (match-beginning 0))
  10655. (setq c0 (current-column) p0 (point)
  10656. c1 (max (1+ c0) (if (> org-tags-column 0)
  10657. org-tags-column
  10658. (- (- org-tags-column) (length tags))))
  10659. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10660. (replace-match rpl t t)
  10661. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10662. tags)
  10663. (t (error "Tags alignment failed")))
  10664. (org-move-to-column col)
  10665. (unless just-align
  10666. (run-hooks 'org-after-tags-change-hook)))))
  10667. (defun org-change-tag-in-region (beg end tag off)
  10668. "Add or remove TAG for each entry in the region.
  10669. This works in the agenda, and also in an org-mode buffer."
  10670. (interactive
  10671. (list (region-beginning) (region-end)
  10672. (let ((org-last-tags-completion-table
  10673. (if (org-mode-p)
  10674. (org-get-buffer-tags)
  10675. (org-global-tags-completion-table))))
  10676. (org-icompleting-read
  10677. "Tag: " 'org-tags-completion-function nil nil nil
  10678. 'org-tags-history))
  10679. (progn
  10680. (message "[s]et or [r]emove? ")
  10681. (equal (read-char-exclusive) ?r))))
  10682. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10683. (let ((agendap (equal major-mode 'org-agenda-mode))
  10684. l1 l2 m buf pos newhead (cnt 0))
  10685. (goto-char end)
  10686. (setq l2 (1- (org-current-line)))
  10687. (goto-char beg)
  10688. (setq l1 (org-current-line))
  10689. (loop for l from l1 to l2 do
  10690. (org-goto-line l)
  10691. (setq m (get-text-property (point) 'org-hd-marker))
  10692. (when (or (and (org-mode-p) (org-on-heading-p))
  10693. (and agendap m))
  10694. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10695. pos (if agendap m (point)))
  10696. (with-current-buffer buf
  10697. (save-excursion
  10698. (save-restriction
  10699. (goto-char pos)
  10700. (setq cnt (1+ cnt))
  10701. (org-toggle-tag tag (if off 'off 'on))
  10702. (setq newhead (org-get-heading)))))
  10703. (and agendap (org-agenda-change-all-lines newhead m))))
  10704. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10705. (defun org-tags-completion-function (string predicate &optional flag)
  10706. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10707. (confirm (lambda (x) (stringp (car x)))))
  10708. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10709. (setq s1 (match-string 1 string)
  10710. s2 (match-string 2 string))
  10711. (setq s1 "" s2 string))
  10712. (cond
  10713. ((eq flag nil)
  10714. ;; try completion
  10715. (setq rtn (try-completion s2 ctable confirm))
  10716. (if (stringp rtn)
  10717. (setq rtn
  10718. (concat s1 s2 (substring rtn (length s2))
  10719. (if (and org-add-colon-after-tag-completion
  10720. (assoc rtn ctable))
  10721. ":" ""))))
  10722. rtn)
  10723. ((eq flag t)
  10724. ;; all-completions
  10725. (all-completions s2 ctable confirm)
  10726. )
  10727. ((eq flag 'lambda)
  10728. ;; exact match?
  10729. (assoc s2 ctable)))
  10730. ))
  10731. (defun org-fast-tag-insert (kwd tags face &optional end)
  10732. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10733. (insert (format "%-12s" (concat kwd ":"))
  10734. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10735. (or end "")))
  10736. (defun org-fast-tag-show-exit (flag)
  10737. (save-excursion
  10738. (org-goto-line 3)
  10739. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10740. (replace-match ""))
  10741. (when flag
  10742. (end-of-line 1)
  10743. (org-move-to-column (- (window-width) 19) t)
  10744. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10745. (defun org-set-current-tags-overlay (current prefix)
  10746. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10747. (if (featurep 'xemacs)
  10748. (org-overlay-display org-tags-overlay (concat prefix s)
  10749. 'secondary-selection)
  10750. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10751. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10752. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10753. "Fast tag selection with single keys.
  10754. CURRENT is the current list of tags in the headline, INHERITED is the
  10755. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10756. possibly with grouping information. TODO-TABLE is a similar table with
  10757. TODO keywords, should these have keys assigned to them.
  10758. If the keys are nil, a-z are automatically assigned.
  10759. Returns the new tags string, or nil to not change the current settings."
  10760. (let* ((fulltable (append table todo-table))
  10761. (maxlen (apply 'max (mapcar
  10762. (lambda (x)
  10763. (if (stringp (car x)) (string-width (car x)) 0))
  10764. fulltable)))
  10765. (buf (current-buffer))
  10766. (expert (eq org-fast-tag-selection-single-key 'expert))
  10767. (buffer-tags nil)
  10768. (fwidth (+ maxlen 3 1 3))
  10769. (ncol (/ (- (window-width) 4) fwidth))
  10770. (i-face 'org-done)
  10771. (c-face 'org-todo)
  10772. tg cnt e c char c1 c2 ntable tbl rtn
  10773. ov-start ov-end ov-prefix
  10774. (exit-after-next org-fast-tag-selection-single-key)
  10775. (done-keywords org-done-keywords)
  10776. groups ingroup)
  10777. (save-excursion
  10778. (beginning-of-line 1)
  10779. (if (looking-at
  10780. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10781. (setq ov-start (match-beginning 1)
  10782. ov-end (match-end 1)
  10783. ov-prefix "")
  10784. (setq ov-start (1- (point-at-eol))
  10785. ov-end (1+ ov-start))
  10786. (skip-chars-forward "^\n\r")
  10787. (setq ov-prefix
  10788. (concat
  10789. (buffer-substring (1- (point)) (point))
  10790. (if (> (current-column) org-tags-column)
  10791. " "
  10792. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10793. (org-move-overlay org-tags-overlay ov-start ov-end)
  10794. (save-window-excursion
  10795. (if expert
  10796. (set-buffer (get-buffer-create " *Org tags*"))
  10797. (delete-other-windows)
  10798. (split-window-vertically)
  10799. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10800. (erase-buffer)
  10801. (org-set-local 'org-done-keywords done-keywords)
  10802. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10803. (org-fast-tag-insert "Current" current c-face "\n\n")
  10804. (org-fast-tag-show-exit exit-after-next)
  10805. (org-set-current-tags-overlay current ov-prefix)
  10806. (setq tbl fulltable char ?a cnt 0)
  10807. (while (setq e (pop tbl))
  10808. (cond
  10809. ((equal (car e) :startgroup)
  10810. (push '() groups) (setq ingroup t)
  10811. (when (not (= cnt 0))
  10812. (setq cnt 0)
  10813. (insert "\n"))
  10814. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  10815. ((equal (car e) :endgroup)
  10816. (setq ingroup nil cnt 0)
  10817. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  10818. ((equal e '(:newline))
  10819. (when (not (= cnt 0))
  10820. (setq cnt 0)
  10821. (insert "\n")
  10822. (setq e (car tbl))
  10823. (while (equal (car tbl) '(:newline))
  10824. (insert "\n")
  10825. (setq tbl (cdr tbl)))))
  10826. (t
  10827. (setq tg (copy-sequence (car e)) c2 nil)
  10828. (if (cdr e)
  10829. (setq c (cdr e))
  10830. ;; automatically assign a character.
  10831. (setq c1 (string-to-char
  10832. (downcase (substring
  10833. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10834. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10835. (while (or (rassoc char ntable) (rassoc char table))
  10836. (setq char (1+ char)))
  10837. (setq c2 c1))
  10838. (setq c (or c2 char)))
  10839. (if ingroup (push tg (car groups)))
  10840. (setq tg (org-add-props tg nil 'face
  10841. (cond
  10842. ((not (assoc tg table))
  10843. (org-get-todo-face tg))
  10844. ((member tg current) c-face)
  10845. ((member tg inherited) i-face)
  10846. (t nil))))
  10847. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10848. (insert "[" c "] " tg (make-string
  10849. (- fwidth 4 (length tg)) ?\ ))
  10850. (push (cons tg c) ntable)
  10851. (when (= (setq cnt (1+ cnt)) ncol)
  10852. (insert "\n")
  10853. (if ingroup (insert " "))
  10854. (setq cnt 0)))))
  10855. (setq ntable (nreverse ntable))
  10856. (insert "\n")
  10857. (goto-char (point-min))
  10858. (if (not expert) (org-fit-window-to-buffer))
  10859. (setq rtn
  10860. (catch 'exit
  10861. (while t
  10862. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  10863. (if (not groups) "no " "")
  10864. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10865. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10866. (cond
  10867. ((= c ?\r) (throw 'exit t))
  10868. ((= c ?!)
  10869. (setq groups (not groups))
  10870. (goto-char (point-min))
  10871. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10872. ((= c ?\C-c)
  10873. (if (not expert)
  10874. (org-fast-tag-show-exit
  10875. (setq exit-after-next (not exit-after-next)))
  10876. (setq expert nil)
  10877. (delete-other-windows)
  10878. (split-window-vertically)
  10879. (org-switch-to-buffer-other-window " *Org tags*")
  10880. (org-fit-window-to-buffer)))
  10881. ((or (= c ?\C-g)
  10882. (and (= c ?q) (not (rassoc c ntable))))
  10883. (org-detach-overlay org-tags-overlay)
  10884. (setq quit-flag t))
  10885. ((= c ?\ )
  10886. (setq current nil)
  10887. (if exit-after-next (setq exit-after-next 'now)))
  10888. ((= c ?\t)
  10889. (condition-case nil
  10890. (setq tg (org-icompleting-read
  10891. "Tag: "
  10892. (or buffer-tags
  10893. (with-current-buffer buf
  10894. (org-get-buffer-tags)))))
  10895. (quit (setq tg "")))
  10896. (when (string-match "\\S-" tg)
  10897. (add-to-list 'buffer-tags (list tg))
  10898. (if (member tg current)
  10899. (setq current (delete tg current))
  10900. (push tg current)))
  10901. (if exit-after-next (setq exit-after-next 'now)))
  10902. ((setq e (rassoc c todo-table) tg (car e))
  10903. (with-current-buffer buf
  10904. (save-excursion (org-todo tg)))
  10905. (if exit-after-next (setq exit-after-next 'now)))
  10906. ((setq e (rassoc c ntable) tg (car e))
  10907. (if (member tg current)
  10908. (setq current (delete tg current))
  10909. (loop for g in groups do
  10910. (if (member tg g)
  10911. (mapc (lambda (x)
  10912. (setq current (delete x current)))
  10913. g)))
  10914. (push tg current))
  10915. (if exit-after-next (setq exit-after-next 'now))))
  10916. ;; Create a sorted list
  10917. (setq current
  10918. (sort current
  10919. (lambda (a b)
  10920. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10921. (if (eq exit-after-next 'now) (throw 'exit t))
  10922. (goto-char (point-min))
  10923. (beginning-of-line 2)
  10924. (delete-region (point) (point-at-eol))
  10925. (org-fast-tag-insert "Current" current c-face)
  10926. (org-set-current-tags-overlay current ov-prefix)
  10927. (while (re-search-forward
  10928. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10929. (setq tg (match-string 1))
  10930. (add-text-properties
  10931. (match-beginning 1) (match-end 1)
  10932. (list 'face
  10933. (cond
  10934. ((member tg current) c-face)
  10935. ((member tg inherited) i-face)
  10936. (t (get-text-property (match-beginning 1) 'face))))))
  10937. (goto-char (point-min)))))
  10938. (org-detach-overlay org-tags-overlay)
  10939. (if rtn
  10940. (mapconcat 'identity current ":")
  10941. nil))))
  10942. (defun org-get-tags-string ()
  10943. "Get the TAGS string in the current headline."
  10944. (unless (org-on-heading-p t)
  10945. (error "Not on a heading"))
  10946. (save-excursion
  10947. (beginning-of-line 1)
  10948. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10949. (org-match-string-no-properties 1)
  10950. "")))
  10951. (defun org-get-tags ()
  10952. "Get the list of tags specified in the current headline."
  10953. (org-split-string (org-get-tags-string) ":"))
  10954. (defun org-get-buffer-tags ()
  10955. "Get a table of all tags used in the buffer, for completion."
  10956. (let (tags)
  10957. (save-excursion
  10958. (goto-char (point-min))
  10959. (while (re-search-forward
  10960. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10961. (when (equal (char-after (point-at-bol 0)) ?*)
  10962. (mapc (lambda (x) (add-to-list 'tags x))
  10963. (org-split-string (org-match-string-no-properties 1) ":")))))
  10964. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  10965. (mapcar 'list tags)))
  10966. ;;;; The mapping API
  10967. ;;;###autoload
  10968. (defun org-map-entries (func &optional match scope &rest skip)
  10969. "Call FUNC at each headline selected by MATCH in SCOPE.
  10970. FUNC is a function or a lisp form. The function will be called without
  10971. arguments, with the cursor positioned at the beginning of the headline.
  10972. The return values of all calls to the function will be collected and
  10973. returned as a list.
  10974. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10975. does not need to preserve point. After evaluation, the cursor will be
  10976. moved to the end of the line (presumably of the headline of the
  10977. processed entry) and search continues from there. Under some
  10978. circumstances, this may not produce the wanted results. For example,
  10979. if you have removed (e.g. archived) the current (sub)tree it could
  10980. mean that the next entry will be skipped entirely. In such cases, you
  10981. can specify the position from where search should continue by making
  10982. FUNC set the variable `org-map-continue-from' to the desired buffer
  10983. position.
  10984. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10985. Only headlines that are matched by this query will be considered during
  10986. the iteration. When MATCH is nil or t, all headlines will be
  10987. visited by the iteration.
  10988. SCOPE determines the scope of this command. It can be any of:
  10989. nil The current buffer, respecting the restriction if any
  10990. tree The subtree started with the entry at point
  10991. file The current buffer, without restriction
  10992. file-with-archives
  10993. The current buffer, and any archives associated with it
  10994. agenda All agenda files
  10995. agenda-with-archives
  10996. All agenda files with any archive files associated with them
  10997. \(file1 file2 ...)
  10998. If this is a list, all files in the list will be scanned
  10999. The remaining args are treated as settings for the skipping facilities of
  11000. the scanner. The following items can be given here:
  11001. archive skip trees with the archive tag.
  11002. comment skip trees with the COMMENT keyword
  11003. function or Emacs Lisp form:
  11004. will be used as value for `org-agenda-skip-function', so whenever
  11005. the function returns t, FUNC will not be called for that
  11006. entry and search will continue from the point where the
  11007. function leaves it.
  11008. If your function needs to retrieve the tags including inherited tags
  11009. at the *current* entry, you can use the value of the variable
  11010. `org-scanner-tags' which will be much faster than getting the value
  11011. with `org-get-tags-at'. If your function gets properties with
  11012. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11013. to t around the call to `org-entry-properties' to get the same speedup.
  11014. Note that if your function moves around to retrieve tags and properties at
  11015. a *different* entry, you cannot use these techniques."
  11016. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11017. (org-agenda-skip-archived-trees (memq 'archive skip))
  11018. (org-agenda-skip-comment-trees (memq 'comment skip))
  11019. (org-agenda-skip-function
  11020. (car (org-delete-all '(comment archive) skip)))
  11021. (org-tags-match-list-sublevels t)
  11022. matcher file res
  11023. org-todo-keywords-for-agenda
  11024. org-done-keywords-for-agenda
  11025. org-todo-keyword-alist-for-agenda
  11026. org-drawers-for-agenda
  11027. org-tag-alist-for-agenda)
  11028. (cond
  11029. ((eq match t) (setq matcher t))
  11030. ((eq match nil) (setq matcher t))
  11031. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11032. (save-excursion
  11033. (save-restriction
  11034. (when (eq scope 'tree)
  11035. (org-back-to-heading t)
  11036. (org-narrow-to-subtree)
  11037. (setq scope nil))
  11038. (if (not scope)
  11039. (progn
  11040. (org-prepare-agenda-buffers
  11041. (list (buffer-file-name (current-buffer))))
  11042. (setq res (org-scan-tags func matcher)))
  11043. ;; Get the right scope
  11044. (cond
  11045. ((and scope (listp scope) (symbolp (car scope)))
  11046. (setq scope (eval scope)))
  11047. ((eq scope 'agenda)
  11048. (setq scope (org-agenda-files t)))
  11049. ((eq scope 'agenda-with-archives)
  11050. (setq scope (org-agenda-files t))
  11051. (setq scope (org-add-archive-files scope)))
  11052. ((eq scope 'file)
  11053. (setq scope (list (buffer-file-name))))
  11054. ((eq scope 'file-with-archives)
  11055. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11056. (org-prepare-agenda-buffers scope)
  11057. (while (setq file (pop scope))
  11058. (with-current-buffer (org-find-base-buffer-visiting file)
  11059. (save-excursion
  11060. (save-restriction
  11061. (widen)
  11062. (goto-char (point-min))
  11063. (setq res (append res (org-scan-tags func matcher))))))))))
  11064. res))
  11065. ;;;; Properties
  11066. ;;; Setting and retrieving properties
  11067. (defconst org-special-properties
  11068. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11069. "TIMESTAMP" "TIMESTAMP_IA")
  11070. "The special properties valid in Org-mode.
  11071. These are properties that are not defined in the property drawer,
  11072. but in some other way.")
  11073. (defconst org-default-properties
  11074. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11075. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11076. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11077. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11078. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11079. "CLOCK_MODELINE_TOTAL" "STYLE")
  11080. "Some properties that are used by Org-mode for various purposes.
  11081. Being in this list makes sure that they are offered for completion.")
  11082. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11083. "Regular expression matching the first line of a property drawer.")
  11084. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11085. "Regular expression matching the first line of a property drawer.")
  11086. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11087. "Regular expression matching the first line of a property drawer.")
  11088. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11089. "Regular expression matching the first line of a property drawer.")
  11090. (defconst org-property-drawer-re
  11091. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11092. org-property-end-re "\\)\n?")
  11093. "Matches an entire property drawer.")
  11094. (defconst org-clock-drawer-re
  11095. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11096. org-property-end-re "\\)\n?")
  11097. "Matches an entire clock drawer.")
  11098. (defun org-property-action ()
  11099. "Do an action on properties."
  11100. (interactive)
  11101. (let (c)
  11102. (org-at-property-p)
  11103. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11104. (setq c (read-char-exclusive))
  11105. (cond
  11106. ((equal c ?s)
  11107. (call-interactively 'org-set-property))
  11108. ((equal c ?d)
  11109. (call-interactively 'org-delete-property))
  11110. ((equal c ?D)
  11111. (call-interactively 'org-delete-property-globally))
  11112. ((equal c ?c)
  11113. (call-interactively 'org-compute-property-at-point))
  11114. (t (error "No such property action %c" c)))))
  11115. (defun org-set-effort (&optional value)
  11116. "Set the effort property of the current entry.
  11117. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11118. allowed value."
  11119. (interactive "P")
  11120. (if (equal value 0) (setq value 10))
  11121. (let* ((completion-ignore-case t)
  11122. (prop org-effort-property)
  11123. (cur (org-entry-get nil prop))
  11124. (allowed (org-property-get-allowed-values nil prop 'table))
  11125. (existing (mapcar 'list (org-property-values prop)))
  11126. rpl
  11127. (val (cond
  11128. ((stringp value) value)
  11129. ((and allowed (integerp value))
  11130. (or (car (nth (1- value) allowed))
  11131. (car (org-last allowed))))
  11132. (allowed
  11133. (message "Select 1-9,0, [RET%s]: %s"
  11134. (if cur (concat "=" cur) "")
  11135. (mapconcat 'car allowed " "))
  11136. (setq rpl (read-char-exclusive))
  11137. (if (equal rpl ?\r)
  11138. cur
  11139. (setq rpl (- rpl ?0))
  11140. (if (equal rpl 0) (setq rpl 10))
  11141. (if (and (> rpl 0) (<= rpl (length allowed)))
  11142. (car (nth (1- rpl) allowed))
  11143. (org-completing-read "Effort: " allowed nil))))
  11144. (t
  11145. (let (org-completion-use-ido org-completion-use-iswitchb)
  11146. (org-completing-read
  11147. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11148. (concat "[" cur "]") "")
  11149. ": ")
  11150. existing nil nil "" nil cur))))))
  11151. (unless (equal (org-entry-get nil prop) val)
  11152. (org-entry-put nil prop val))
  11153. (message "%s is now %s" prop val)))
  11154. (defun org-at-property-p ()
  11155. "Is the cursor in a property line?"
  11156. ;; FIXME: Does not check if we are actually in the drawer.
  11157. ;; FIXME: also returns true on any drawers.....
  11158. ;; This is used by C-c C-c for property action.
  11159. (save-excursion
  11160. (beginning-of-line 1)
  11161. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  11162. (defun org-get-property-block (&optional beg end force)
  11163. "Return the (beg . end) range of the body of the property drawer.
  11164. BEG and END can be beginning and end of subtree, if not given
  11165. they will be found.
  11166. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11167. (catch 'exit
  11168. (save-excursion
  11169. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11170. (end (or end (progn (outline-next-heading) (point)))))
  11171. (goto-char beg)
  11172. (if (re-search-forward org-property-start-re end t)
  11173. (setq beg (1+ (match-end 0)))
  11174. (if force
  11175. (save-excursion
  11176. (org-insert-property-drawer)
  11177. (setq end (progn (outline-next-heading) (point))))
  11178. (throw 'exit nil))
  11179. (goto-char beg)
  11180. (if (re-search-forward org-property-start-re end t)
  11181. (setq beg (1+ (match-end 0)))))
  11182. (if (re-search-forward org-property-end-re end t)
  11183. (setq end (match-beginning 0))
  11184. (or force (throw 'exit nil))
  11185. (goto-char beg)
  11186. (setq end beg)
  11187. (org-indent-line-function)
  11188. (insert ":END:\n"))
  11189. (cons beg end)))))
  11190. (defun org-entry-properties (&optional pom which)
  11191. "Get all properties of the entry at point-or-marker POM.
  11192. This includes the TODO keyword, the tags, time strings for deadline,
  11193. scheduled, and clocking, and any additional properties defined in the
  11194. entry. The return value is an alist, keys may occur multiple times
  11195. if the property key was used several times.
  11196. POM may also be nil, in which case the current entry is used.
  11197. If WHICH is nil or `all', get all properties. If WHICH is
  11198. `special' or `standard', only get that subclass."
  11199. (setq which (or which 'all))
  11200. (org-with-point-at pom
  11201. (let ((clockstr (substring org-clock-string 0 -1))
  11202. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  11203. beg end range props sum-props key value string clocksum)
  11204. (save-excursion
  11205. (when (condition-case nil
  11206. (and (org-mode-p) (org-back-to-heading t))
  11207. (error nil))
  11208. (setq beg (point))
  11209. (setq sum-props (get-text-property (point) 'org-summaries))
  11210. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11211. (outline-next-heading)
  11212. (setq end (point))
  11213. (when (memq which '(all special))
  11214. ;; Get the special properties, like TODO and tags
  11215. (goto-char beg)
  11216. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  11217. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11218. (when (looking-at org-priority-regexp)
  11219. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11220. (when (and (setq value (org-get-tags-string))
  11221. (string-match "\\S-" value))
  11222. (push (cons "TAGS" value) props))
  11223. (when (setq value (org-get-tags-at))
  11224. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  11225. props))
  11226. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11227. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11228. string (if (equal key clockstr)
  11229. (org-no-properties
  11230. (org-trim
  11231. (buffer-substring
  11232. (match-beginning 3) (goto-char (point-at-eol)))))
  11233. (substring (org-match-string-no-properties 3) 1 -1)))
  11234. (unless key
  11235. (if (= (char-after (match-beginning 3)) ?\[)
  11236. (setq key "TIMESTAMP_IA")
  11237. (setq key "TIMESTAMP")))
  11238. (when (or (equal key clockstr) (not (assoc key props)))
  11239. (push (cons key string) props)))
  11240. )
  11241. (when (memq which '(all standard))
  11242. ;; Get the standard properties, like :PROP: ...
  11243. (setq range (org-get-property-block beg end))
  11244. (when range
  11245. (goto-char (car range))
  11246. (while (re-search-forward
  11247. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11248. (cdr range) t)
  11249. (setq key (org-match-string-no-properties 1)
  11250. value (org-trim (or (org-match-string-no-properties 2) "")))
  11251. (unless (member key excluded)
  11252. (push (cons key (or value "")) props)))))
  11253. (if clocksum
  11254. (push (cons "CLOCKSUM"
  11255. (org-columns-number-to-string (/ (float clocksum) 60.)
  11256. 'add_times))
  11257. props))
  11258. (unless (assoc "CATEGORY" props)
  11259. (setq value (or (org-get-category)
  11260. (progn (org-refresh-category-properties)
  11261. (org-get-category))))
  11262. (push (cons "CATEGORY" value) props))
  11263. (append sum-props (nreverse props)))))))
  11264. (defun org-entry-get (pom property &optional inherit)
  11265. "Get value of PROPERTY for entry at point-or-marker POM.
  11266. If INHERIT is non-nil and the entry does not have the property,
  11267. then also check higher levels of the hierarchy.
  11268. If INHERIT is the symbol `selective', use inheritance only if the setting
  11269. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11270. If the property is present but empty, the return value is the empty string.
  11271. If the property is not present at all, nil is returned."
  11272. (org-with-point-at pom
  11273. (if (and inherit (if (eq inherit 'selective)
  11274. (org-property-inherit-p property)
  11275. t))
  11276. (org-entry-get-with-inheritance property)
  11277. (if (member property org-special-properties)
  11278. ;; We need a special property. Use brute force, get all properties.
  11279. (cdr (assoc property (org-entry-properties nil 'special)))
  11280. (let ((range (org-get-property-block)))
  11281. (if (and range
  11282. (goto-char (car range))
  11283. (re-search-forward
  11284. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11285. (cdr range) t))
  11286. ;; Found the property, return it.
  11287. (if (match-end 1)
  11288. (org-match-string-no-properties 1)
  11289. "")))))))
  11290. (defun org-property-or-variable-value (var &optional inherit)
  11291. "Check if there is a property fixing the value of VAR.
  11292. If yes, return this value. If not, return the current value of the variable."
  11293. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11294. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11295. (read prop)
  11296. (symbol-value var))))
  11297. (defun org-entry-delete (pom property)
  11298. "Delete the property PROPERTY from entry at point-or-marker POM."
  11299. (org-with-point-at pom
  11300. (if (member property org-special-properties)
  11301. nil ; cannot delete these properties.
  11302. (let ((range (org-get-property-block)))
  11303. (if (and range
  11304. (goto-char (car range))
  11305. (re-search-forward
  11306. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11307. (cdr range) t))
  11308. (progn
  11309. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11310. t)
  11311. nil)))))
  11312. ;; Multi-values properties are properties that contain multiple values
  11313. ;; These values are assumed to be single words, separated by whitespace.
  11314. (defun org-entry-add-to-multivalued-property (pom property value)
  11315. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11316. (let* ((old (org-entry-get pom property))
  11317. (values (and old (org-split-string old "[ \t]"))))
  11318. (setq value (org-entry-protect-space value))
  11319. (unless (member value values)
  11320. (setq values (cons value values))
  11321. (org-entry-put pom property
  11322. (mapconcat 'identity values " ")))))
  11323. (defun org-entry-remove-from-multivalued-property (pom property value)
  11324. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11325. (let* ((old (org-entry-get pom property))
  11326. (values (and old (org-split-string old "[ \t]"))))
  11327. (setq value (org-entry-protect-space value))
  11328. (when (member value values)
  11329. (setq values (delete value values))
  11330. (org-entry-put pom property
  11331. (mapconcat 'identity values " ")))))
  11332. (defun org-entry-member-in-multivalued-property (pom property value)
  11333. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11334. (let* ((old (org-entry-get pom property))
  11335. (values (and old (org-split-string old "[ \t]"))))
  11336. (setq value (org-entry-protect-space value))
  11337. (member value values)))
  11338. (defun org-entry-get-multivalued-property (pom property)
  11339. "Return a list of values in a multivalued property."
  11340. (let* ((value (org-entry-get pom property))
  11341. (values (and value (org-split-string value "[ \t]"))))
  11342. (mapcar 'org-entry-restore-space values)))
  11343. (defun org-entry-put-multivalued-property (pom property &rest values)
  11344. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11345. VALUES should be a list of strings. Spaces will be protected."
  11346. (org-entry-put pom property
  11347. (mapconcat 'org-entry-protect-space values " "))
  11348. (let* ((value (org-entry-get pom property))
  11349. (values (and value (org-split-string value "[ \t]"))))
  11350. (mapcar 'org-entry-restore-space values)))
  11351. (defun org-entry-protect-space (s)
  11352. "Protect spaces and newline in string S."
  11353. (while (string-match " " s)
  11354. (setq s (replace-match "%20" t t s)))
  11355. (while (string-match "\n" s)
  11356. (setq s (replace-match "%0A" t t s)))
  11357. s)
  11358. (defun org-entry-restore-space (s)
  11359. "Restore spaces and newline in string S."
  11360. (while (string-match "%20" s)
  11361. (setq s (replace-match " " t t s)))
  11362. (while (string-match "%0A" s)
  11363. (setq s (replace-match "\n" t t s)))
  11364. s)
  11365. (defvar org-entry-property-inherited-from (make-marker)
  11366. "Marker pointing to the entry from where a property was inherited.
  11367. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11368. location of the entry where the inheritance search matched. If there was
  11369. no match, the marker will point nowhere.
  11370. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11371. is set.")
  11372. (defun org-entry-get-with-inheritance (property)
  11373. "Get entry property, and search higher levels if not present."
  11374. (move-marker org-entry-property-inherited-from nil)
  11375. (let (tmp)
  11376. (save-excursion
  11377. (save-restriction
  11378. (widen)
  11379. (catch 'ex
  11380. (while t
  11381. (when (setq tmp (org-entry-get nil property))
  11382. (org-back-to-heading t)
  11383. (move-marker org-entry-property-inherited-from (point))
  11384. (throw 'ex tmp))
  11385. (or (org-up-heading-safe) (throw 'ex nil)))))
  11386. (or tmp
  11387. (cdr (assoc property org-file-properties))
  11388. (cdr (assoc property org-global-properties))
  11389. (cdr (assoc property org-global-properties-fixed))))))
  11390. (defun org-entry-put (pom property value)
  11391. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11392. (org-with-point-at pom
  11393. (org-back-to-heading t)
  11394. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11395. range)
  11396. (cond
  11397. ((equal property "TODO")
  11398. (when (and (stringp value) (string-match "\\S-" value)
  11399. (not (member value org-todo-keywords-1)))
  11400. (error "\"%s\" is not a valid TODO state" value))
  11401. (if (or (not value)
  11402. (not (string-match "\\S-" value)))
  11403. (setq value 'none))
  11404. (org-todo value)
  11405. (org-set-tags nil 'align))
  11406. ((equal property "PRIORITY")
  11407. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11408. (string-to-char value) ?\ ))
  11409. (org-set-tags nil 'align))
  11410. ((equal property "SCHEDULED")
  11411. (if (re-search-forward org-scheduled-time-regexp end t)
  11412. (cond
  11413. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11414. ((eq value 'later) (org-timestamp-change 1 'day))
  11415. (t (call-interactively 'org-schedule)))
  11416. (call-interactively 'org-schedule)))
  11417. ((equal property "DEADLINE")
  11418. (if (re-search-forward org-deadline-time-regexp end t)
  11419. (cond
  11420. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11421. ((eq value 'later) (org-timestamp-change 1 'day))
  11422. (t (call-interactively 'org-deadline)))
  11423. (call-interactively 'org-deadline)))
  11424. ((member property org-special-properties)
  11425. (error "The %s property can not yet be set with `org-entry-put'"
  11426. property))
  11427. (t ; a non-special property
  11428. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11429. (setq range (org-get-property-block beg end 'force))
  11430. (goto-char (car range))
  11431. (if (re-search-forward
  11432. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11433. (progn
  11434. (delete-region (match-beginning 1) (match-end 1))
  11435. (goto-char (match-beginning 1)))
  11436. (goto-char (cdr range))
  11437. (insert "\n")
  11438. (backward-char 1)
  11439. (org-indent-line-function)
  11440. (insert ":" property ":"))
  11441. (and value (insert " " value))
  11442. (org-indent-line-function)))))))
  11443. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11444. "Get all property keys in the current buffer.
  11445. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11446. like tags and TODO state.
  11447. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11448. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11449. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11450. formats in the current buffer."
  11451. (let (rtn range cfmt s p)
  11452. (save-excursion
  11453. (save-restriction
  11454. (widen)
  11455. (goto-char (point-min))
  11456. (while (re-search-forward org-property-start-re nil t)
  11457. (setq range (org-get-property-block))
  11458. (goto-char (car range))
  11459. (while (re-search-forward
  11460. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11461. (cdr range) t)
  11462. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11463. (outline-next-heading))))
  11464. (when include-specials
  11465. (setq rtn (append org-special-properties rtn)))
  11466. (when include-defaults
  11467. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11468. (add-to-list 'rtn org-effort-property))
  11469. (when include-columns
  11470. (save-excursion
  11471. (save-restriction
  11472. (widen)
  11473. (goto-char (point-min))
  11474. (while (re-search-forward
  11475. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11476. nil t)
  11477. (setq cfmt (match-string 2) s 0)
  11478. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11479. cfmt s)
  11480. (setq s (match-end 0)
  11481. p (match-string 1 cfmt))
  11482. (unless (or (equal p "ITEM")
  11483. (member p org-special-properties))
  11484. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11485. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11486. (defun org-property-values (key)
  11487. "Return a list of all values of property KEY."
  11488. (save-excursion
  11489. (save-restriction
  11490. (widen)
  11491. (goto-char (point-min))
  11492. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11493. values)
  11494. (while (re-search-forward re nil t)
  11495. (add-to-list 'values (org-trim (match-string 1))))
  11496. (delete "" values)))))
  11497. (defun org-insert-property-drawer ()
  11498. "Insert a property drawer into the current entry."
  11499. (interactive)
  11500. (org-back-to-heading t)
  11501. (looking-at outline-regexp)
  11502. (let ((indent (if org-adapt-indentation
  11503. (- (match-end 0)(match-beginning 0))
  11504. 0))
  11505. (beg (point))
  11506. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11507. end hiddenp)
  11508. (outline-next-heading)
  11509. (setq end (point))
  11510. (goto-char beg)
  11511. (while (re-search-forward re end t))
  11512. (setq hiddenp (org-invisible-p))
  11513. (end-of-line 1)
  11514. (and (equal (char-after) ?\n) (forward-char 1))
  11515. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11516. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11517. ;; just skip this line
  11518. (beginning-of-line 2)
  11519. ;; Drawer start, find the end
  11520. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11521. (beginning-of-line 1)))
  11522. (org-skip-over-state-notes)
  11523. (skip-chars-backward " \t\n\r")
  11524. (if (eq (char-before) ?*) (forward-char 1))
  11525. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11526. (beginning-of-line 0)
  11527. (org-indent-to-column indent)
  11528. (beginning-of-line 2)
  11529. (org-indent-to-column indent)
  11530. (beginning-of-line 0)
  11531. (if hiddenp
  11532. (save-excursion
  11533. (org-back-to-heading t)
  11534. (hide-entry))
  11535. (org-flag-drawer t))))
  11536. (defun org-set-property (property value)
  11537. "In the current entry, set PROPERTY to VALUE.
  11538. When called interactively, this will prompt for a property name, offering
  11539. completion on existing and default properties. And then it will prompt
  11540. for a value, offering completion either on allowed values (via an inherited
  11541. xxx_ALL property) or on existing values in other instances of this property
  11542. in the current file."
  11543. (interactive
  11544. (let* ((completion-ignore-case t)
  11545. (keys (org-buffer-property-keys nil t t))
  11546. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11547. (prop (if (member prop0 keys)
  11548. prop0
  11549. (or (cdr (assoc (downcase prop0)
  11550. (mapcar (lambda (x) (cons (downcase x) x))
  11551. keys)))
  11552. prop0)))
  11553. (cur (org-entry-get nil prop))
  11554. (allowed (org-property-get-allowed-values nil prop 'table))
  11555. (existing (mapcar 'list (org-property-values prop)))
  11556. (val (if allowed
  11557. (org-completing-read "Value: " allowed nil 'req-match)
  11558. (let (org-completion-use-ido org-completion-use-iswitchb)
  11559. (org-completing-read
  11560. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11561. (concat "[" cur "]") "")
  11562. ": ")
  11563. existing nil nil "" nil cur)))))
  11564. (list prop (if (equal val "") cur val))))
  11565. (unless (equal (org-entry-get nil property) value)
  11566. (org-entry-put nil property value)))
  11567. (defun org-delete-property (property)
  11568. "In the current entry, delete PROPERTY."
  11569. (interactive
  11570. (let* ((completion-ignore-case t)
  11571. (prop (org-icompleting-read
  11572. "Property: " (org-entry-properties nil 'standard))))
  11573. (list prop)))
  11574. (message "Property %s %s" property
  11575. (if (org-entry-delete nil property)
  11576. "deleted"
  11577. "was not present in the entry")))
  11578. (defun org-delete-property-globally (property)
  11579. "Remove PROPERTY globally, from all entries."
  11580. (interactive
  11581. (let* ((completion-ignore-case t)
  11582. (prop (org-icompleting-read
  11583. "Globally remove property: "
  11584. (mapcar 'list (org-buffer-property-keys)))))
  11585. (list prop)))
  11586. (save-excursion
  11587. (save-restriction
  11588. (widen)
  11589. (goto-char (point-min))
  11590. (let ((cnt 0))
  11591. (while (re-search-forward
  11592. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11593. nil t)
  11594. (setq cnt (1+ cnt))
  11595. (replace-match ""))
  11596. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11597. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11598. (defun org-compute-property-at-point ()
  11599. "Compute the property at point.
  11600. This looks for an enclosing column format, extracts the operator and
  11601. then applies it to the property in the column format's scope."
  11602. (interactive)
  11603. (unless (org-at-property-p)
  11604. (error "Not at a property"))
  11605. (let ((prop (org-match-string-no-properties 2)))
  11606. (org-columns-get-format-and-top-level)
  11607. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11608. (error "No operator defined for property %s" prop))
  11609. (org-columns-compute prop)))
  11610. (defun org-property-get-allowed-values (pom property &optional table)
  11611. "Get allowed values for the property PROPERTY.
  11612. When TABLE is non-nil, return an alist that can directly be used for
  11613. completion."
  11614. (let (vals)
  11615. (cond
  11616. ((equal property "TODO")
  11617. (setq vals (org-with-point-at pom
  11618. (append org-todo-keywords-1 '("")))))
  11619. ((equal property "PRIORITY")
  11620. (let ((n org-lowest-priority))
  11621. (while (>= n org-highest-priority)
  11622. (push (char-to-string n) vals)
  11623. (setq n (1- n)))))
  11624. ((member property org-special-properties))
  11625. (t
  11626. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11627. (when (and vals (string-match "\\S-" vals))
  11628. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11629. (setq vals (mapcar (lambda (x)
  11630. (cond ((stringp x) x)
  11631. ((numberp x) (number-to-string x))
  11632. ((symbolp x) (symbol-name x))
  11633. (t "???")))
  11634. vals)))))
  11635. (if table (mapcar 'list vals) vals)))
  11636. (defun org-property-previous-allowed-value (&optional previous)
  11637. "Switch to the next allowed value for this property."
  11638. (interactive)
  11639. (org-property-next-allowed-value t))
  11640. (defun org-property-next-allowed-value (&optional previous)
  11641. "Switch to the next allowed value for this property."
  11642. (interactive)
  11643. (unless (org-at-property-p)
  11644. (error "Not at a property"))
  11645. (let* ((key (match-string 2))
  11646. (value (match-string 3))
  11647. (allowed (or (org-property-get-allowed-values (point) key)
  11648. (and (member value '("[ ]" "[-]" "[X]"))
  11649. '("[ ]" "[X]"))))
  11650. nval)
  11651. (unless allowed
  11652. (error "Allowed values for this property have not been defined"))
  11653. (if previous (setq allowed (reverse allowed)))
  11654. (if (member value allowed)
  11655. (setq nval (car (cdr (member value allowed)))))
  11656. (setq nval (or nval (car allowed)))
  11657. (if (equal nval value)
  11658. (error "Only one allowed value for this property"))
  11659. (org-at-property-p)
  11660. (replace-match (concat " :" key ": " nval) t t)
  11661. (org-indent-line-function)
  11662. (beginning-of-line 1)
  11663. (skip-chars-forward " \t")))
  11664. (defun org-find-entry-with-id (ident)
  11665. "Locate the entry that contains the ID property with exact value IDENT.
  11666. IDENT can be a string, a symbol or a number, this function will search for
  11667. the string representation of it.
  11668. Return the position where this entry starts, or nil if there is no such entry."
  11669. (interactive "sID: ")
  11670. (let ((id (cond
  11671. ((stringp ident) ident)
  11672. ((symbol-name ident) (symbol-name ident))
  11673. ((numberp ident) (number-to-string ident))
  11674. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11675. (case-fold-search nil))
  11676. (save-excursion
  11677. (save-restriction
  11678. (widen)
  11679. (goto-char (point-min))
  11680. (when (re-search-forward
  11681. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11682. nil t)
  11683. (org-back-to-heading t)
  11684. (point))))))
  11685. ;;;; Timestamps
  11686. (defvar org-last-changed-timestamp nil)
  11687. (defvar org-last-inserted-timestamp nil
  11688. "The last time stamp inserted with `org-insert-time-stamp'.")
  11689. (defvar org-time-was-given) ; dynamically scoped parameter
  11690. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11691. (defvar org-ts-what) ; dynamically scoped parameter
  11692. (defun org-time-stamp (arg &optional inactive)
  11693. "Prompt for a date/time and insert a time stamp.
  11694. If the user specifies a time like HH:MM, or if this command is called
  11695. with a prefix argument, the time stamp will contain date and time.
  11696. Otherwise, only the date will be included. All parts of a date not
  11697. specified by the user will be filled in from the current date/time.
  11698. So if you press just return without typing anything, the time stamp
  11699. will represent the current date/time. If there is already a timestamp
  11700. at the cursor, it will be modified."
  11701. (interactive "P")
  11702. (let* ((ts nil)
  11703. (default-time
  11704. ;; Default time is either today, or, when entering a range,
  11705. ;; the range start.
  11706. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11707. (save-excursion
  11708. (re-search-backward
  11709. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11710. (- (point) 20) t)))
  11711. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11712. (current-time)))
  11713. (default-input (and ts (org-get-compact-tod ts)))
  11714. org-time-was-given org-end-time-was-given time)
  11715. (cond
  11716. ((and (org-at-timestamp-p t)
  11717. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11718. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11719. (insert "--")
  11720. (setq time (let ((this-command this-command))
  11721. (org-read-date arg 'totime nil nil
  11722. default-time default-input)))
  11723. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11724. ((org-at-timestamp-p t)
  11725. (setq time (let ((this-command this-command))
  11726. (org-read-date arg 'totime nil nil default-time default-input)))
  11727. (when (org-at-timestamp-p t) ; just to get the match data
  11728. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11729. (replace-match "")
  11730. (setq org-last-changed-timestamp
  11731. (org-insert-time-stamp
  11732. time (or org-time-was-given arg)
  11733. inactive nil nil (list org-end-time-was-given))))
  11734. (message "Timestamp updated"))
  11735. (t
  11736. (setq time (let ((this-command this-command))
  11737. (org-read-date arg 'totime nil nil default-time default-input)))
  11738. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11739. nil nil (list org-end-time-was-given))))))
  11740. ;; FIXME: can we use this for something else, like computing time differences?
  11741. (defun org-get-compact-tod (s)
  11742. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11743. (let* ((t1 (match-string 1 s))
  11744. (h1 (string-to-number (match-string 2 s)))
  11745. (m1 (string-to-number (match-string 3 s)))
  11746. (t2 (and (match-end 4) (match-string 5 s)))
  11747. (h2 (and t2 (string-to-number (match-string 6 s))))
  11748. (m2 (and t2 (string-to-number (match-string 7 s))))
  11749. dh dm)
  11750. (if (not t2)
  11751. t1
  11752. (setq dh (- h2 h1) dm (- m2 m1))
  11753. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11754. (concat t1 "+" (number-to-string dh)
  11755. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11756. (defun org-time-stamp-inactive (&optional arg)
  11757. "Insert an inactive time stamp.
  11758. An inactive time stamp is enclosed in square brackets instead of angle
  11759. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11760. does not link to the calendar and cannot be changed with the S-cursor keys.
  11761. So these are more for recording a certain time/date."
  11762. (interactive "P")
  11763. (org-time-stamp arg 'inactive))
  11764. (defvar org-date-ovl (org-make-overlay 1 1))
  11765. (org-overlay-put org-date-ovl 'face 'org-warning)
  11766. (org-detach-overlay org-date-ovl)
  11767. (defvar org-ans1) ; dynamically scoped parameter
  11768. (defvar org-ans2) ; dynamically scoped parameter
  11769. (defvar org-plain-time-of-day-regexp) ; defined below
  11770. (defvar org-overriding-default-time nil) ; dynamically scoped
  11771. (defvar org-read-date-overlay nil)
  11772. (defvar org-dcst nil) ; dynamically scoped
  11773. (defvar org-read-date-history nil)
  11774. (defvar org-read-date-final-answer nil)
  11775. (defun org-read-date (&optional with-time to-time from-string prompt
  11776. default-time default-input)
  11777. "Read a date, possibly a time, and make things smooth for the user.
  11778. The prompt will suggest to enter an ISO date, but you can also enter anything
  11779. which will at least partially be understood by `parse-time-string'.
  11780. Unrecognized parts of the date will default to the current day, month, year,
  11781. hour and minute. If this command is called to replace a timestamp at point,
  11782. of to enter the second timestamp of a range, the default time is taken from the
  11783. existing stamp. For example,
  11784. 3-2-5 --> 2003-02-05
  11785. feb 15 --> currentyear-02-15
  11786. sep 12 9 --> 2009-09-12
  11787. 12:45 --> today 12:45
  11788. 22 sept 0:34 --> currentyear-09-22 0:34
  11789. 12 --> currentyear-currentmonth-12
  11790. Fri --> nearest Friday (today or later)
  11791. etc.
  11792. Furthermore you can specify a relative date by giving, as the *first* thing
  11793. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11794. change in days weeks, months, years.
  11795. With a single plus or minus, the date is relative to today. With a double
  11796. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11797. +4d --> four days from today
  11798. +4 --> same as above
  11799. +2w --> two weeks from today
  11800. ++5 --> five days from default date
  11801. The function understands only English month and weekday abbreviations,
  11802. but this can be configured with the variables `parse-time-months' and
  11803. `parse-time-weekdays'.
  11804. While prompting, a calendar is popped up - you can also select the
  11805. date with the mouse (button 1). The calendar shows a period of three
  11806. months. To scroll it to other months, use the keys `>' and `<'.
  11807. If you don't like the calendar, turn it off with
  11808. \(setq org-read-date-popup-calendar nil)
  11809. With optional argument TO-TIME, the date will immediately be converted
  11810. to an internal time.
  11811. With an optional argument WITH-TIME, the prompt will suggest to also
  11812. insert a time. Note that when WITH-TIME is not set, you can still
  11813. enter a time, and this function will inform the calling routine about
  11814. this change. The calling routine may then choose to change the format
  11815. used to insert the time stamp into the buffer to include the time.
  11816. With optional argument FROM-STRING, read from this string instead from
  11817. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11818. the time/date that is used for everything that is not specified by the
  11819. user."
  11820. (require 'parse-time)
  11821. (let* ((org-time-stamp-rounding-minutes
  11822. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11823. (org-dcst org-display-custom-times)
  11824. (ct (org-current-time))
  11825. (def (or org-overriding-default-time default-time ct))
  11826. (defdecode (decode-time def))
  11827. (dummy (progn
  11828. (when (< (nth 2 defdecode) org-extend-today-until)
  11829. (setcar (nthcdr 2 defdecode) -1)
  11830. (setcar (nthcdr 1 defdecode) 59)
  11831. (setq def (apply 'encode-time defdecode)
  11832. defdecode (decode-time def)))))
  11833. (calendar-frame-setup nil)
  11834. (calendar-move-hook nil)
  11835. (calendar-view-diary-initially-flag nil)
  11836. (view-diary-entries-initially nil)
  11837. (calendar-view-holidays-initially-flag nil)
  11838. (view-calendar-holidays-initially nil)
  11839. (timestr (format-time-string
  11840. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11841. (prompt (concat (if prompt (concat prompt " ") "")
  11842. (format "Date+time [%s]: " timestr)))
  11843. ans (org-ans0 "") org-ans1 org-ans2 final)
  11844. (cond
  11845. (from-string (setq ans from-string))
  11846. (org-read-date-popup-calendar
  11847. (save-excursion
  11848. (save-window-excursion
  11849. (calendar)
  11850. (calendar-forward-day (- (time-to-days def)
  11851. (calendar-absolute-from-gregorian
  11852. (calendar-current-date))))
  11853. (org-eval-in-calendar nil t)
  11854. (let* ((old-map (current-local-map))
  11855. (map (copy-keymap calendar-mode-map))
  11856. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11857. (org-defkey map (kbd "RET") 'org-calendar-select)
  11858. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11859. 'org-calendar-select-mouse)
  11860. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11861. 'org-calendar-select-mouse)
  11862. (org-defkey minibuffer-local-map [(meta shift left)]
  11863. (lambda () (interactive)
  11864. (org-eval-in-calendar '(calendar-backward-month 1))))
  11865. (org-defkey minibuffer-local-map [(meta shift right)]
  11866. (lambda () (interactive)
  11867. (org-eval-in-calendar '(calendar-forward-month 1))))
  11868. (org-defkey minibuffer-local-map [(meta shift up)]
  11869. (lambda () (interactive)
  11870. (org-eval-in-calendar '(calendar-backward-year 1))))
  11871. (org-defkey minibuffer-local-map [(meta shift down)]
  11872. (lambda () (interactive)
  11873. (org-eval-in-calendar '(calendar-forward-year 1))))
  11874. (org-defkey minibuffer-local-map [?\e (shift left)]
  11875. (lambda () (interactive)
  11876. (org-eval-in-calendar '(calendar-backward-month 1))))
  11877. (org-defkey minibuffer-local-map [?\e (shift right)]
  11878. (lambda () (interactive)
  11879. (org-eval-in-calendar '(calendar-forward-month 1))))
  11880. (org-defkey minibuffer-local-map [?\e (shift up)]
  11881. (lambda () (interactive)
  11882. (org-eval-in-calendar '(calendar-backward-year 1))))
  11883. (org-defkey minibuffer-local-map [?\e (shift down)]
  11884. (lambda () (interactive)
  11885. (org-eval-in-calendar '(calendar-forward-year 1))))
  11886. (org-defkey minibuffer-local-map [(shift up)]
  11887. (lambda () (interactive)
  11888. (org-eval-in-calendar '(calendar-backward-week 1))))
  11889. (org-defkey minibuffer-local-map [(shift down)]
  11890. (lambda () (interactive)
  11891. (org-eval-in-calendar '(calendar-forward-week 1))))
  11892. (org-defkey minibuffer-local-map [(shift left)]
  11893. (lambda () (interactive)
  11894. (org-eval-in-calendar '(calendar-backward-day 1))))
  11895. (org-defkey minibuffer-local-map [(shift right)]
  11896. (lambda () (interactive)
  11897. (org-eval-in-calendar '(calendar-forward-day 1))))
  11898. (org-defkey minibuffer-local-map ">"
  11899. (lambda () (interactive)
  11900. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11901. (org-defkey minibuffer-local-map "<"
  11902. (lambda () (interactive)
  11903. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11904. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11905. (unwind-protect
  11906. (progn
  11907. (use-local-map map)
  11908. (add-hook 'post-command-hook 'org-read-date-display)
  11909. (setq org-ans0 (read-string prompt default-input
  11910. 'org-read-date-history nil))
  11911. ;; org-ans0: from prompt
  11912. ;; org-ans1: from mouse click
  11913. ;; org-ans2: from calendar motion
  11914. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11915. (remove-hook 'post-command-hook 'org-read-date-display)
  11916. (use-local-map old-map)
  11917. (when org-read-date-overlay
  11918. (org-delete-overlay org-read-date-overlay)
  11919. (setq org-read-date-overlay nil)))))))
  11920. (t ; Naked prompt only
  11921. (unwind-protect
  11922. (setq ans (read-string prompt default-input
  11923. 'org-read-date-history timestr))
  11924. (when org-read-date-overlay
  11925. (org-delete-overlay org-read-date-overlay)
  11926. (setq org-read-date-overlay nil)))))
  11927. (setq final (org-read-date-analyze ans def defdecode))
  11928. (setq org-read-date-final-answer ans)
  11929. (if to-time
  11930. (apply 'encode-time final)
  11931. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11932. (format "%04d-%02d-%02d %02d:%02d"
  11933. (nth 5 final) (nth 4 final) (nth 3 final)
  11934. (nth 2 final) (nth 1 final))
  11935. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11936. (defvar def)
  11937. (defvar defdecode)
  11938. (defvar with-time)
  11939. (defvar org-read-date-analyze-futurep nil)
  11940. (defun org-read-date-display ()
  11941. "Display the current date prompt interpretation in the minibuffer."
  11942. (when org-read-date-display-live
  11943. (when org-read-date-overlay
  11944. (org-delete-overlay org-read-date-overlay))
  11945. (let ((p (point)))
  11946. (end-of-line 1)
  11947. (while (not (equal (buffer-substring
  11948. (max (point-min) (- (point) 4)) (point))
  11949. " "))
  11950. (insert " "))
  11951. (goto-char p))
  11952. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11953. " " (or org-ans1 org-ans2)))
  11954. (org-end-time-was-given nil)
  11955. (f (org-read-date-analyze ans def defdecode))
  11956. (fmts (if org-dcst
  11957. org-time-stamp-custom-formats
  11958. org-time-stamp-formats))
  11959. (fmt (if (or with-time
  11960. (and (boundp 'org-time-was-given) org-time-was-given))
  11961. (cdr fmts)
  11962. (car fmts)))
  11963. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11964. (when (and org-end-time-was-given
  11965. (string-match org-plain-time-of-day-regexp txt))
  11966. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11967. org-end-time-was-given
  11968. (substring txt (match-end 0)))))
  11969. (when org-read-date-analyze-futurep
  11970. (setq txt (concat txt " (=>F)")))
  11971. (setq org-read-date-overlay
  11972. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11973. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11974. (defun org-read-date-analyze (ans def defdecode)
  11975. "Analyse the combined answer of the date prompt."
  11976. ;; FIXME: cleanup and comment
  11977. (let (delta deltan deltaw deltadef year month day
  11978. hour minute second wday pm h2 m2 tl wday1
  11979. iso-year iso-weekday iso-week iso-year iso-date futurep)
  11980. (setq org-read-date-analyze-futurep nil)
  11981. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11982. (setq ans "+0"))
  11983. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11984. (setq ans (replace-match "" t t ans)
  11985. deltan (car delta)
  11986. deltaw (nth 1 delta)
  11987. deltadef (nth 2 delta)))
  11988. ;; Check if there is an iso week date in there
  11989. ;; If yes, sore the info and postpone interpreting it until the rest
  11990. ;; of the parsing is done
  11991. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11992. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11993. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11994. iso-week (string-to-number (match-string 2 ans)))
  11995. (setq ans (replace-match "" t t ans)))
  11996. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11997. (when (string-match
  11998. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11999. (setq year (if (match-end 2)
  12000. (string-to-number (match-string 2 ans))
  12001. (string-to-number (format-time-string "%Y")))
  12002. month (string-to-number (match-string 3 ans))
  12003. day (string-to-number (match-string 4 ans)))
  12004. (if (< year 100) (setq year (+ 2000 year)))
  12005. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12006. t nil ans)))
  12007. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12008. ;; If there is a time with am/pm, and *no* time without it, we convert
  12009. ;; so that matching will be successful.
  12010. (loop for i from 1 to 2 do ; twice, for end time as well
  12011. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12012. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12013. (setq hour (string-to-number (match-string 1 ans))
  12014. minute (if (match-end 3)
  12015. (string-to-number (match-string 3 ans))
  12016. 0)
  12017. pm (equal ?p
  12018. (string-to-char (downcase (match-string 4 ans)))))
  12019. (if (and (= hour 12) (not pm))
  12020. (setq hour 0)
  12021. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12022. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12023. t t ans))))
  12024. ;; Check if a time range is given as a duration
  12025. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12026. (setq hour (string-to-number (match-string 1 ans))
  12027. h2 (+ hour (string-to-number (match-string 3 ans)))
  12028. minute (string-to-number (match-string 2 ans))
  12029. m2 (+ minute (if (match-end 5) (string-to-number
  12030. (match-string 5 ans))0)))
  12031. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12032. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12033. t t ans)))
  12034. ;; Check if there is a time range
  12035. (when (boundp 'org-end-time-was-given)
  12036. (setq org-time-was-given nil)
  12037. (when (and (string-match org-plain-time-of-day-regexp ans)
  12038. (match-end 8))
  12039. (setq org-end-time-was-given (match-string 8 ans))
  12040. (setq ans (concat (substring ans 0 (match-beginning 7))
  12041. (substring ans (match-end 7))))))
  12042. (setq tl (parse-time-string ans)
  12043. day (or (nth 3 tl) (nth 3 defdecode))
  12044. month (or (nth 4 tl)
  12045. (if (and org-read-date-prefer-future
  12046. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  12047. (prog1 (1+ (nth 4 defdecode)) (setq futurep t))
  12048. (nth 4 defdecode)))
  12049. year (or (nth 5 tl)
  12050. (if (and org-read-date-prefer-future
  12051. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  12052. (prog1 (1+ (nth 5 defdecode)) (setq futurep t))
  12053. (nth 5 defdecode)))
  12054. hour (or (nth 2 tl) (nth 2 defdecode))
  12055. minute (or (nth 1 tl) (nth 1 defdecode))
  12056. second (or (nth 0 tl) 0)
  12057. wday (nth 6 tl))
  12058. (when (and (eq org-read-date-prefer-future 'time)
  12059. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12060. (equal day (nth 3 defdecode))
  12061. (equal month (nth 4 defdecode))
  12062. (equal year (nth 5 defdecode))
  12063. (nth 2 tl)
  12064. (or (< (nth 2 tl) (nth 2 defdecode))
  12065. (and (= (nth 2 tl) (nth 2 defdecode))
  12066. (nth 1 tl)
  12067. (< (nth 1 tl) (nth 1 defdecode)))))
  12068. (setq day (1+ day)
  12069. futurep t))
  12070. ;; Special date definitions below
  12071. (cond
  12072. (iso-week
  12073. ;; There was an iso week
  12074. (setq futurep nil)
  12075. (setq year (or iso-year year)
  12076. day (or iso-weekday wday 1)
  12077. wday nil ; to make sure that the trigger below does not match
  12078. iso-date (calendar-gregorian-from-absolute
  12079. (calendar-absolute-from-iso
  12080. (list iso-week day year))))
  12081. ; FIXME: Should we also push ISO weeks into the future?
  12082. ; (when (and org-read-date-prefer-future
  12083. ; (not iso-year)
  12084. ; (< (calendar-absolute-from-gregorian iso-date)
  12085. ; (time-to-days (current-time))))
  12086. ; (setq year (1+ year)
  12087. ; iso-date (calendar-gregorian-from-absolute
  12088. ; (calendar-absolute-from-iso
  12089. ; (list iso-week day year)))))
  12090. (setq month (car iso-date)
  12091. year (nth 2 iso-date)
  12092. day (nth 1 iso-date)))
  12093. (deltan
  12094. (setq futurep nil)
  12095. (unless deltadef
  12096. (let ((now (decode-time (current-time))))
  12097. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12098. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12099. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12100. ((equal deltaw "m") (setq month (+ month deltan)))
  12101. ((equal deltaw "y") (setq year (+ year deltan)))))
  12102. ((and wday (not (nth 3 tl)))
  12103. (setq futurep nil)
  12104. ;; Weekday was given, but no day, so pick that day in the week
  12105. ;; on or after the derived date.
  12106. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12107. (unless (equal wday wday1)
  12108. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12109. (if (and (boundp 'org-time-was-given)
  12110. (nth 2 tl))
  12111. (setq org-time-was-given t))
  12112. (if (< year 100) (setq year (+ 2000 year)))
  12113. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12114. (setq org-read-date-analyze-futurep futurep)
  12115. (list second minute hour day month year)))
  12116. (defvar parse-time-weekdays)
  12117. (defun org-read-date-get-relative (s today default)
  12118. "Check string S for special relative date string.
  12119. TODAY and DEFAULT are internal times, for today and for a default.
  12120. Return shift list (N what def-flag)
  12121. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12122. N is the number of WHATs to shift.
  12123. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12124. the DEFAULT date rather than TODAY."
  12125. (when (and
  12126. (string-match
  12127. (concat
  12128. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12129. "\\([0-9]+\\)?"
  12130. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12131. "\\([ \t]\\|$\\)") s)
  12132. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12133. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12134. (string-to-char (substring (match-string 1 s) -1))
  12135. ?+))
  12136. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12137. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12138. (what (if (match-end 3) (match-string 3 s) "d"))
  12139. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12140. (date (if rel default today))
  12141. (wday (nth 6 (decode-time date)))
  12142. delta)
  12143. (if wday1
  12144. (progn
  12145. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12146. (if (= dir ?-) (setq delta (- delta 7)))
  12147. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12148. (list delta "d" rel))
  12149. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12150. (defun org-eval-in-calendar (form &optional keepdate)
  12151. "Eval FORM in the calendar window and return to current window.
  12152. Also, store the cursor date in variable org-ans2."
  12153. (let ((sf (selected-frame))
  12154. (sw (selected-window)))
  12155. (select-window (get-buffer-window "*Calendar*" t))
  12156. (eval form)
  12157. (when (and (not keepdate) (calendar-cursor-to-date))
  12158. (let* ((date (calendar-cursor-to-date))
  12159. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12160. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12161. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12162. (select-window sw)
  12163. (org-select-frame-set-input-focus sf)))
  12164. (defun org-calendar-select ()
  12165. "Return to `org-read-date' with the date currently selected.
  12166. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12167. (interactive)
  12168. (when (calendar-cursor-to-date)
  12169. (let* ((date (calendar-cursor-to-date))
  12170. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12171. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12172. (if (active-minibuffer-window) (exit-minibuffer))))
  12173. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12174. "Insert a date stamp for the date given by the internal TIME.
  12175. WITH-HM means, use the stamp format that includes the time of the day.
  12176. INACTIVE means use square brackets instead of angular ones, so that the
  12177. stamp will not contribute to the agenda.
  12178. PRE and POST are optional strings to be inserted before and after the
  12179. stamp.
  12180. The command returns the inserted time stamp."
  12181. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12182. stamp)
  12183. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12184. (insert-before-markers (or pre ""))
  12185. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12186. (when (listp extra)
  12187. (setq extra (car extra))
  12188. (if (and (stringp extra)
  12189. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12190. (setq extra (format "-%02d:%02d"
  12191. (string-to-number (match-string 1 extra))
  12192. (string-to-number (match-string 2 extra))))
  12193. (setq extra nil)))
  12194. (when extra
  12195. (backward-char 1)
  12196. (insert-before-markers extra)
  12197. (forward-char 1))
  12198. (insert-before-markers (or post ""))
  12199. (setq org-last-inserted-timestamp stamp)))
  12200. (defun org-toggle-time-stamp-overlays ()
  12201. "Toggle the use of custom time stamp formats."
  12202. (interactive)
  12203. (setq org-display-custom-times (not org-display-custom-times))
  12204. (unless org-display-custom-times
  12205. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12206. (while (setq p (next-single-property-change p 'display))
  12207. (if (and (get-text-property p 'display)
  12208. (eq (get-text-property p 'face) 'org-date))
  12209. (remove-text-properties
  12210. p (setq p (next-single-property-change p 'display))
  12211. '(display t))))
  12212. (set-buffer-modified-p bmp)))
  12213. (if (featurep 'xemacs)
  12214. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12215. (org-restart-font-lock)
  12216. (setq org-table-may-need-update t)
  12217. (if org-display-custom-times
  12218. (message "Time stamps are overlayed with custom format")
  12219. (message "Time stamp overlays removed")))
  12220. (defun org-display-custom-time (beg end)
  12221. "Overlay modified time stamp format over timestamp between BEG and END."
  12222. (let* ((ts (buffer-substring beg end))
  12223. t1 w1 with-hm tf time str w2 (off 0))
  12224. (save-match-data
  12225. (setq t1 (org-parse-time-string ts t))
  12226. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12227. (setq off (- (match-end 0) (match-beginning 0)))))
  12228. (setq end (- end off))
  12229. (setq w1 (- end beg)
  12230. with-hm (and (nth 1 t1) (nth 2 t1))
  12231. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12232. time (org-fix-decoded-time t1)
  12233. str (org-add-props
  12234. (format-time-string
  12235. (substring tf 1 -1) (apply 'encode-time time))
  12236. nil 'mouse-face 'highlight)
  12237. w2 (length str))
  12238. (if (not (= w2 w1))
  12239. (add-text-properties (1+ beg) (+ 2 beg)
  12240. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12241. (if (featurep 'xemacs)
  12242. (progn
  12243. (put-text-property beg end 'invisible t)
  12244. (put-text-property beg end 'end-glyph (make-glyph str)))
  12245. (put-text-property beg end 'display str))))
  12246. (defun org-translate-time (string)
  12247. "Translate all timestamps in STRING to custom format.
  12248. But do this only if the variable `org-display-custom-times' is set."
  12249. (when org-display-custom-times
  12250. (save-match-data
  12251. (let* ((start 0)
  12252. (re org-ts-regexp-both)
  12253. t1 with-hm inactive tf time str beg end)
  12254. (while (setq start (string-match re string start))
  12255. (setq beg (match-beginning 0)
  12256. end (match-end 0)
  12257. t1 (save-match-data
  12258. (org-parse-time-string (substring string beg end) t))
  12259. with-hm (and (nth 1 t1) (nth 2 t1))
  12260. inactive (equal (substring string beg (1+ beg)) "[")
  12261. tf (funcall (if with-hm 'cdr 'car)
  12262. org-time-stamp-custom-formats)
  12263. time (org-fix-decoded-time t1)
  12264. str (format-time-string
  12265. (concat
  12266. (if inactive "[" "<") (substring tf 1 -1)
  12267. (if inactive "]" ">"))
  12268. (apply 'encode-time time))
  12269. string (replace-match str t t string)
  12270. start (+ start (length str)))))))
  12271. string)
  12272. (defun org-fix-decoded-time (time)
  12273. "Set 0 instead of nil for the first 6 elements of time.
  12274. Don't touch the rest."
  12275. (let ((n 0))
  12276. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12277. (defun org-days-to-time (timestamp-string)
  12278. "Difference between TIMESTAMP-STRING and now in days."
  12279. (- (time-to-days (org-time-string-to-time timestamp-string))
  12280. (time-to-days (current-time))))
  12281. (defun org-deadline-close (timestamp-string &optional ndays)
  12282. "Is the time in TIMESTAMP-STRING close to the current date?"
  12283. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12284. (and (< (org-days-to-time timestamp-string) ndays)
  12285. (not (org-entry-is-done-p))))
  12286. (defun org-get-wdays (ts)
  12287. "Get the deadline lead time appropriate for timestring TS."
  12288. (cond
  12289. ((<= org-deadline-warning-days 0)
  12290. ;; 0 or negative, enforce this value no matter what
  12291. (- org-deadline-warning-days))
  12292. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12293. ;; lead time is specified.
  12294. (floor (* (string-to-number (match-string 1 ts))
  12295. (cdr (assoc (match-string 2 ts)
  12296. '(("d" . 1) ("w" . 7)
  12297. ("m" . 30.4) ("y" . 365.25)))))))
  12298. ;; go for the default.
  12299. (t org-deadline-warning-days)))
  12300. (defun org-calendar-select-mouse (ev)
  12301. "Return to `org-read-date' with the date currently selected.
  12302. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12303. (interactive "e")
  12304. (mouse-set-point ev)
  12305. (when (calendar-cursor-to-date)
  12306. (let* ((date (calendar-cursor-to-date))
  12307. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12308. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12309. (if (active-minibuffer-window) (exit-minibuffer))))
  12310. (defun org-check-deadlines (ndays)
  12311. "Check if there are any deadlines due or past due.
  12312. A deadline is considered due if it happens within `org-deadline-warning-days'
  12313. days from today's date. If the deadline appears in an entry marked DONE,
  12314. it is not shown. The prefix arg NDAYS can be used to test that many
  12315. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12316. (interactive "P")
  12317. (let* ((org-warn-days
  12318. (cond
  12319. ((equal ndays '(4)) 100000)
  12320. (ndays (prefix-numeric-value ndays))
  12321. (t (abs org-deadline-warning-days))))
  12322. (case-fold-search nil)
  12323. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12324. (callback
  12325. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12326. (message "%d deadlines past-due or due within %d days"
  12327. (org-occur regexp nil callback)
  12328. org-warn-days)))
  12329. (defun org-check-before-date (date)
  12330. "Check if there are deadlines or scheduled entries before DATE."
  12331. (interactive (list (org-read-date)))
  12332. (let ((case-fold-search nil)
  12333. (regexp (concat "\\<\\(" org-deadline-string
  12334. "\\|" org-scheduled-string
  12335. "\\) *<\\([^>]+\\)>"))
  12336. (callback
  12337. (lambda () (time-less-p
  12338. (org-time-string-to-time (match-string 2))
  12339. (org-time-string-to-time date)))))
  12340. (message "%d entries before %s"
  12341. (org-occur regexp nil callback) date)))
  12342. (defun org-check-after-date (date)
  12343. "Check if there are deadlines or scheduled entries after DATE."
  12344. (interactive (list (org-read-date)))
  12345. (let ((case-fold-search nil)
  12346. (regexp (concat "\\<\\(" org-deadline-string
  12347. "\\|" org-scheduled-string
  12348. "\\) *<\\([^>]+\\)>"))
  12349. (callback
  12350. (lambda () (not
  12351. (time-less-p
  12352. (org-time-string-to-time (match-string 2))
  12353. (org-time-string-to-time date))))))
  12354. (message "%d entries after %s"
  12355. (org-occur regexp nil callback) date)))
  12356. (defun org-evaluate-time-range (&optional to-buffer)
  12357. "Evaluate a time range by computing the difference between start and end.
  12358. Normally the result is just printed in the echo area, but with prefix arg
  12359. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12360. If the time range is actually in a table, the result is inserted into the
  12361. next column.
  12362. For time difference computation, a year is assumed to be exactly 365
  12363. days in order to avoid rounding problems."
  12364. (interactive "P")
  12365. (or
  12366. (org-clock-update-time-maybe)
  12367. (save-excursion
  12368. (unless (org-at-date-range-p t)
  12369. (goto-char (point-at-bol))
  12370. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12371. (if (not (org-at-date-range-p t))
  12372. (error "Not at a time-stamp range, and none found in current line")))
  12373. (let* ((ts1 (match-string 1))
  12374. (ts2 (match-string 2))
  12375. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12376. (match-end (match-end 0))
  12377. (time1 (org-time-string-to-time ts1))
  12378. (time2 (org-time-string-to-time ts2))
  12379. (t1 (org-float-time time1))
  12380. (t2 (org-float-time time2))
  12381. (diff (abs (- t2 t1)))
  12382. (negative (< (- t2 t1) 0))
  12383. ;; (ys (floor (* 365 24 60 60)))
  12384. (ds (* 24 60 60))
  12385. (hs (* 60 60))
  12386. (fy "%dy %dd %02d:%02d")
  12387. (fy1 "%dy %dd")
  12388. (fd "%dd %02d:%02d")
  12389. (fd1 "%dd")
  12390. (fh "%02d:%02d")
  12391. y d h m align)
  12392. (if havetime
  12393. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12394. y 0
  12395. d (floor (/ diff ds)) diff (mod diff ds)
  12396. h (floor (/ diff hs)) diff (mod diff hs)
  12397. m (floor (/ diff 60)))
  12398. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12399. y 0
  12400. d (floor (+ (/ diff ds) 0.5))
  12401. h 0 m 0))
  12402. (if (not to-buffer)
  12403. (message "%s" (org-make-tdiff-string y d h m))
  12404. (if (org-at-table-p)
  12405. (progn
  12406. (goto-char match-end)
  12407. (setq align t)
  12408. (and (looking-at " *|") (goto-char (match-end 0))))
  12409. (goto-char match-end))
  12410. (if (looking-at
  12411. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12412. (replace-match ""))
  12413. (if negative (insert " -"))
  12414. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12415. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12416. (insert " " (format fh h m))))
  12417. (if align (org-table-align))
  12418. (message "Time difference inserted")))))
  12419. (defun org-make-tdiff-string (y d h m)
  12420. (let ((fmt "")
  12421. (l nil))
  12422. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12423. l (push y l)))
  12424. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12425. l (push d l)))
  12426. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12427. l (push h l)))
  12428. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12429. l (push m l)))
  12430. (apply 'format fmt (nreverse l))))
  12431. (defun org-time-string-to-time (s)
  12432. (apply 'encode-time (org-parse-time-string s)))
  12433. (defun org-time-string-to-seconds (s)
  12434. (org-float-time (org-time-string-to-time s)))
  12435. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12436. "Convert a time stamp to an absolute day number.
  12437. If there is a specifyer for a cyclic time stamp, get the closest date to
  12438. DAYNR.
  12439. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12440. the variable date is bound by the calendar when this is called."
  12441. (cond
  12442. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12443. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12444. daynr
  12445. (+ daynr 1000)))
  12446. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12447. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12448. (time-to-days (current-time))) (match-string 0 s)
  12449. prefer show-all))
  12450. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12451. (defun org-days-to-iso-week (days)
  12452. "Return the iso week number."
  12453. (require 'cal-iso)
  12454. (car (calendar-iso-from-absolute days)))
  12455. (defun org-small-year-to-year (year)
  12456. "Convert 2-digit years into 4-digit years.
  12457. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12458. The year 2000 cannot be abbreviated. Any year larger than 99
  12459. is returned unchanged."
  12460. (if (< year 38)
  12461. (setq year (+ 2000 year))
  12462. (if (< year 100)
  12463. (setq year (+ 1900 year))))
  12464. year)
  12465. (defun org-time-from-absolute (d)
  12466. "Return the time corresponding to date D.
  12467. D may be an absolute day number, or a calendar-type list (month day year)."
  12468. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12469. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12470. (defun org-calendar-holiday ()
  12471. "List of holidays, for Diary display in Org-mode."
  12472. (require 'holidays)
  12473. (let ((hl (funcall
  12474. (if (fboundp 'calendar-check-holidays)
  12475. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12476. (if hl (mapconcat 'identity hl "; "))))
  12477. (defun org-diary-sexp-entry (sexp entry date)
  12478. "Process a SEXP diary ENTRY for DATE."
  12479. (require 'diary-lib)
  12480. (let ((result (if calendar-debug-sexp
  12481. (let ((stack-trace-on-error t))
  12482. (eval (car (read-from-string sexp))))
  12483. (condition-case nil
  12484. (eval (car (read-from-string sexp)))
  12485. (error
  12486. (beep)
  12487. (message "Bad sexp at line %d in %s: %s"
  12488. (org-current-line)
  12489. (buffer-file-name) sexp)
  12490. (sleep-for 2))))))
  12491. (cond ((stringp result) result)
  12492. ((and (consp result)
  12493. (stringp (cdr result))) (cdr result))
  12494. (result entry)
  12495. (t nil))))
  12496. (defun org-diary-to-ical-string (frombuf)
  12497. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12498. This uses the icalendar.el library."
  12499. (let* ((tmpdir (if (featurep 'xemacs)
  12500. (temp-directory)
  12501. temporary-file-directory))
  12502. (tmpfile (make-temp-name
  12503. (expand-file-name "orgics" tmpdir)))
  12504. buf rtn b e)
  12505. (with-current-buffer frombuf
  12506. (icalendar-export-region (point-min) (point-max) tmpfile)
  12507. (setq buf (find-buffer-visiting tmpfile))
  12508. (set-buffer buf)
  12509. (goto-char (point-min))
  12510. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12511. (setq b (match-beginning 0)))
  12512. (goto-char (point-max))
  12513. (if (re-search-backward "^END:VEVENT" nil t)
  12514. (setq e (match-end 0)))
  12515. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12516. (kill-buffer buf)
  12517. (delete-file tmpfile)
  12518. rtn))
  12519. (defun org-closest-date (start current change prefer show-all)
  12520. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12521. When PREFER is `past' return a date that is either CURRENT or past.
  12522. When PREFER is `future', return a date that is either CURRENT or future.
  12523. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12524. ;; Make the proper lists from the dates
  12525. (catch 'exit
  12526. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12527. dn dw sday cday n1 n2 n0
  12528. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12529. (setq start (org-date-to-gregorian start)
  12530. current (org-date-to-gregorian
  12531. (if show-all
  12532. current
  12533. (time-to-days (current-time))))
  12534. sday (calendar-absolute-from-gregorian start)
  12535. cday (calendar-absolute-from-gregorian current))
  12536. (if (<= cday sday) (throw 'exit sday))
  12537. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12538. (setq dn (string-to-number (match-string 1 change))
  12539. dw (cdr (assoc (match-string 2 change) a1)))
  12540. (error "Invalid change specifyer: %s" change))
  12541. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12542. (cond
  12543. ((eq dw 'day)
  12544. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12545. n2 (+ n1 dn)))
  12546. ((eq dw 'year)
  12547. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12548. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12549. (setq date1 (list m d y1)
  12550. n1 (calendar-absolute-from-gregorian date1)
  12551. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12552. n2 (calendar-absolute-from-gregorian date2)))
  12553. ((eq dw 'month)
  12554. ;; approx number of month between the two dates
  12555. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12556. ;; How often does dn fit in there?
  12557. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12558. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12559. m (+ m nm)
  12560. ny (floor (/ m 12))
  12561. y (+ y ny)
  12562. m (- m (* ny 12)))
  12563. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12564. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12565. (setq m2 (+ m dn) y2 y)
  12566. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12567. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12568. (while (<= n2 cday)
  12569. (setq n1 n2 m m2 y y2)
  12570. (setq m2 (+ m dn) y2 y)
  12571. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12572. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12573. ;; Make sure n1 is the earlier date
  12574. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12575. (if show-all
  12576. (cond
  12577. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12578. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12579. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12580. (cond
  12581. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12582. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12583. (t (if (= cday n1) n1 n2)))))))
  12584. (defun org-date-to-gregorian (date)
  12585. "Turn any specification of DATE into a gregorian date for the calendar."
  12586. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12587. ((and (listp date) (= (length date) 3)) date)
  12588. ((stringp date)
  12589. (setq date (org-parse-time-string date))
  12590. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12591. ((listp date)
  12592. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12593. (defun org-parse-time-string (s &optional nodefault)
  12594. "Parse the standard Org-mode time string.
  12595. This should be a lot faster than the normal `parse-time-string'.
  12596. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12597. hour and minute fields will be nil if not given."
  12598. (if (string-match org-ts-regexp0 s)
  12599. (list 0
  12600. (if (or (match-beginning 8) (not nodefault))
  12601. (string-to-number (or (match-string 8 s) "0")))
  12602. (if (or (match-beginning 7) (not nodefault))
  12603. (string-to-number (or (match-string 7 s) "0")))
  12604. (string-to-number (match-string 4 s))
  12605. (string-to-number (match-string 3 s))
  12606. (string-to-number (match-string 2 s))
  12607. nil nil nil)
  12608. (error "Not a standard Org-mode time string: %s" s)))
  12609. (defun org-timestamp-up (&optional arg)
  12610. "Increase the date item at the cursor by one.
  12611. If the cursor is on the year, change the year. If it is on the month or
  12612. the day, change that.
  12613. With prefix ARG, change by that many units."
  12614. (interactive "p")
  12615. (org-timestamp-change (prefix-numeric-value arg)))
  12616. (defun org-timestamp-down (&optional arg)
  12617. "Decrease the date item at the cursor by one.
  12618. If the cursor is on the year, change the year. If it is on the month or
  12619. the day, change that.
  12620. With prefix ARG, change by that many units."
  12621. (interactive "p")
  12622. (org-timestamp-change (- (prefix-numeric-value arg))))
  12623. (defun org-timestamp-up-day (&optional arg)
  12624. "Increase the date in the time stamp by one day.
  12625. With prefix ARG, change that many days."
  12626. (interactive "p")
  12627. (if (and (not (org-at-timestamp-p t))
  12628. (org-on-heading-p))
  12629. (org-todo 'up)
  12630. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12631. (defun org-timestamp-down-day (&optional arg)
  12632. "Decrease the date in the time stamp by one day.
  12633. With prefix ARG, change that many days."
  12634. (interactive "p")
  12635. (if (and (not (org-at-timestamp-p t))
  12636. (org-on-heading-p))
  12637. (org-todo 'down)
  12638. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12639. (defun org-at-timestamp-p (&optional inactive-ok)
  12640. "Determine if the cursor is in or at a timestamp."
  12641. (interactive)
  12642. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12643. (pos (point))
  12644. (ans (or (looking-at tsr)
  12645. (save-excursion
  12646. (skip-chars-backward "^[<\n\r\t")
  12647. (if (> (point) (point-min)) (backward-char 1))
  12648. (and (looking-at tsr)
  12649. (> (- (match-end 0) pos) -1))))))
  12650. (and ans
  12651. (boundp 'org-ts-what)
  12652. (setq org-ts-what
  12653. (cond
  12654. ((= pos (match-beginning 0)) 'bracket)
  12655. ((= pos (1- (match-end 0))) 'bracket)
  12656. ((org-pos-in-match-range pos 2) 'year)
  12657. ((org-pos-in-match-range pos 3) 'month)
  12658. ((org-pos-in-match-range pos 7) 'hour)
  12659. ((org-pos-in-match-range pos 8) 'minute)
  12660. ((or (org-pos-in-match-range pos 4)
  12661. (org-pos-in-match-range pos 5)) 'day)
  12662. ((and (> pos (or (match-end 8) (match-end 5)))
  12663. (< pos (match-end 0)))
  12664. (- pos (or (match-end 8) (match-end 5))))
  12665. (t 'day))))
  12666. ans))
  12667. (defun org-toggle-timestamp-type ()
  12668. "Toggle the type (<active> or [inactive]) of a time stamp."
  12669. (interactive)
  12670. (when (org-at-timestamp-p t)
  12671. (let ((beg (match-beginning 0)) (end (match-end 0))
  12672. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12673. (save-excursion
  12674. (goto-char beg)
  12675. (while (re-search-forward "[][<>]" end t)
  12676. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12677. t t)))
  12678. (message "Timestamp is now %sactive"
  12679. (if (equal (char-after beg) ?<) "" "in")))))
  12680. (defun org-timestamp-change (n &optional what)
  12681. "Change the date in the time stamp at point.
  12682. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12683. `year', `minute', `second'. If WHAT is not given, the cursor position
  12684. in the timestamp determines what will be changed."
  12685. (let ((pos (point))
  12686. with-hm inactive
  12687. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12688. org-ts-what
  12689. extra rem
  12690. ts time time0)
  12691. (if (not (org-at-timestamp-p t))
  12692. (error "Not at a timestamp"))
  12693. (if (and (not what) (eq org-ts-what 'bracket))
  12694. (org-toggle-timestamp-type)
  12695. (if (and (not what) (not (eq org-ts-what 'day))
  12696. org-display-custom-times
  12697. (get-text-property (point) 'display)
  12698. (not (get-text-property (1- (point)) 'display)))
  12699. (setq org-ts-what 'day))
  12700. (setq org-ts-what (or what org-ts-what)
  12701. inactive (= (char-after (match-beginning 0)) ?\[)
  12702. ts (match-string 0))
  12703. (replace-match "")
  12704. (if (string-match
  12705. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12706. ts)
  12707. (setq extra (match-string 1 ts)))
  12708. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12709. (setq with-hm t))
  12710. (setq time0 (org-parse-time-string ts))
  12711. (when (and (eq org-ts-what 'minute)
  12712. (eq current-prefix-arg nil))
  12713. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12714. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12715. (setcar (cdr time0) (+ (nth 1 time0)
  12716. (if (> n 0) (- rem) (- dm rem))))))
  12717. (setq time
  12718. (encode-time (or (car time0) 0)
  12719. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12720. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12721. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12722. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12723. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12724. (nthcdr 6 time0)))
  12725. (when (and (member org-ts-what '(hour minute))
  12726. extra
  12727. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12728. (setq extra (org-modify-ts-extra
  12729. extra
  12730. (if (eq org-ts-what 'hour) 2 5)
  12731. n dm)))
  12732. (when (integerp org-ts-what)
  12733. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12734. (if (eq what 'calendar)
  12735. (let ((cal-date (org-get-date-from-calendar)))
  12736. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12737. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12738. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12739. (setcar time0 (or (car time0) 0))
  12740. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12741. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12742. (setq time (apply 'encode-time time0))))
  12743. (setq org-last-changed-timestamp
  12744. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12745. (org-clock-update-time-maybe)
  12746. (goto-char pos)
  12747. ;; Try to recenter the calendar window, if any
  12748. (if (and org-calendar-follow-timestamp-change
  12749. (get-buffer-window "*Calendar*" t)
  12750. (memq org-ts-what '(day month year)))
  12751. (org-recenter-calendar (time-to-days time))))))
  12752. (defun org-modify-ts-extra (s pos n dm)
  12753. "Change the different parts of the lead-time and repeat fields in timestamp."
  12754. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12755. ng h m new rem)
  12756. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12757. (cond
  12758. ((or (org-pos-in-match-range pos 2)
  12759. (org-pos-in-match-range pos 3))
  12760. (setq m (string-to-number (match-string 3 s))
  12761. h (string-to-number (match-string 2 s)))
  12762. (if (org-pos-in-match-range pos 2)
  12763. (setq h (+ h n))
  12764. (setq n (* dm (org-no-warnings (signum n))))
  12765. (when (not (= 0 (setq rem (% m dm))))
  12766. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12767. (setq m (+ m n)))
  12768. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12769. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12770. (setq h (min 24 (max 0 h)))
  12771. (setq ng 1 new (format "-%02d:%02d" h m)))
  12772. ((org-pos-in-match-range pos 6)
  12773. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12774. ((org-pos-in-match-range pos 5)
  12775. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12776. ((org-pos-in-match-range pos 9)
  12777. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12778. ((org-pos-in-match-range pos 8)
  12779. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12780. (when ng
  12781. (setq s (concat
  12782. (substring s 0 (match-beginning ng))
  12783. new
  12784. (substring s (match-end ng))))))
  12785. s))
  12786. (defun org-recenter-calendar (date)
  12787. "If the calendar is visible, recenter it to DATE."
  12788. (let* ((win (selected-window))
  12789. (cwin (get-buffer-window "*Calendar*" t))
  12790. (calendar-move-hook nil))
  12791. (when cwin
  12792. (select-window cwin)
  12793. (calendar-goto-date (if (listp date) date
  12794. (calendar-gregorian-from-absolute date)))
  12795. (select-window win))))
  12796. (defun org-goto-calendar (&optional arg)
  12797. "Go to the Emacs calendar at the current date.
  12798. If there is a time stamp in the current line, go to that date.
  12799. A prefix ARG can be used to force the current date."
  12800. (interactive "P")
  12801. (let ((tsr org-ts-regexp) diff
  12802. (calendar-move-hook nil)
  12803. (calendar-view-holidays-initially-flag nil)
  12804. (view-calendar-holidays-initially nil)
  12805. (calendar-view-diary-initially-flag nil)
  12806. (view-diary-entries-initially nil))
  12807. (if (or (org-at-timestamp-p)
  12808. (save-excursion
  12809. (beginning-of-line 1)
  12810. (looking-at (concat ".*" tsr))))
  12811. (let ((d1 (time-to-days (current-time)))
  12812. (d2 (time-to-days
  12813. (org-time-string-to-time (match-string 1)))))
  12814. (setq diff (- d2 d1))))
  12815. (calendar)
  12816. (calendar-goto-today)
  12817. (if (and diff (not arg)) (calendar-forward-day diff))))
  12818. (defun org-get-date-from-calendar ()
  12819. "Return a list (month day year) of date at point in calendar."
  12820. (with-current-buffer "*Calendar*"
  12821. (save-match-data
  12822. (calendar-cursor-to-date))))
  12823. (defun org-date-from-calendar ()
  12824. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12825. If there is already a time stamp at the cursor position, update it."
  12826. (interactive)
  12827. (if (org-at-timestamp-p t)
  12828. (org-timestamp-change 0 'calendar)
  12829. (let ((cal-date (org-get-date-from-calendar)))
  12830. (org-insert-time-stamp
  12831. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12832. (defun org-minutes-to-hh:mm-string (m)
  12833. "Compute H:MM from a number of minutes."
  12834. (let ((h (/ m 60)))
  12835. (setq m (- m (* 60 h)))
  12836. (format org-time-clocksum-format h m)))
  12837. (defun org-hh:mm-string-to-minutes (s)
  12838. "Convert a string H:MM to a number of minutes.
  12839. If the string is just a number, interpret it as minutes.
  12840. In fact, the first hh:mm or number in the string will be taken,
  12841. there can be extra stuff in the string.
  12842. If no number is found, the return value is 0."
  12843. (cond
  12844. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12845. (+ (* (string-to-number (match-string 1 s)) 60)
  12846. (string-to-number (match-string 2 s))))
  12847. ((string-match "\\([0-9]+\\)" s)
  12848. (string-to-number (match-string 1 s)))
  12849. (t 0)))
  12850. ;;;; Files
  12851. (defun org-save-all-org-buffers ()
  12852. "Save all Org-mode buffers without user confirmation."
  12853. (interactive)
  12854. (message "Saving all Org-mode buffers...")
  12855. (save-some-buffers t 'org-mode-p)
  12856. (when (featurep 'org-id) (org-id-locations-save))
  12857. (message "Saving all Org-mode buffers... done"))
  12858. (defun org-revert-all-org-buffers ()
  12859. "Revert all Org-mode buffers.
  12860. Prompt for confirmation when there are unsaved changes.
  12861. Be sure you know what you are doing before letting this function
  12862. overwrite your changes.
  12863. This function is useful in a setup where one tracks org files
  12864. with a version control system, to revert on one machine after pulling
  12865. changes from another. I believe the procedure must be like this:
  12866. 1. M-x org-save-all-org-buffers
  12867. 2. Pull changes from the other machine, resolve conflicts
  12868. 3. M-x org-revert-all-org-buffers"
  12869. (interactive)
  12870. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12871. (error "Abort"))
  12872. (save-excursion
  12873. (save-window-excursion
  12874. (mapc
  12875. (lambda (b)
  12876. (when (and (with-current-buffer b (org-mode-p))
  12877. (with-current-buffer b buffer-file-name))
  12878. (switch-to-buffer b)
  12879. (revert-buffer t 'no-confirm)))
  12880. (buffer-list))
  12881. (when (and (featurep 'org-id) org-id-track-globally)
  12882. (org-id-locations-load)))))
  12883. ;;;; Agenda files
  12884. ;;;###autoload
  12885. (defun org-iswitchb (&optional arg)
  12886. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12887. With a prefix argument, restrict available to files.
  12888. With two prefix arguments, restrict available buffers to agenda files."
  12889. (interactive "P")
  12890. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12891. ((equal arg '(16)) (org-buffer-list 'agenda))
  12892. (t (org-buffer-list)))))
  12893. (switch-to-buffer
  12894. (org-icompleting-read "Org buffer: "
  12895. (mapcar 'list (mapcar 'buffer-name blist))
  12896. nil t))))
  12897. ;;;###autoload
  12898. (defalias 'org-ido-switchb 'org-iswitchb)
  12899. (defun org-buffer-list (&optional predicate exclude-tmp)
  12900. "Return a list of Org buffers.
  12901. PREDICATE can be `export', `files' or `agenda'.
  12902. export restrict the list to Export buffers.
  12903. files restrict the list to buffers visiting Org files.
  12904. agenda restrict the list to buffers visiting agenda files.
  12905. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12906. (let* ((bfn nil)
  12907. (agenda-files (and (eq predicate 'agenda)
  12908. (mapcar 'file-truename (org-agenda-files t))))
  12909. (filter
  12910. (cond
  12911. ((eq predicate 'files)
  12912. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12913. ((eq predicate 'export)
  12914. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12915. ((eq predicate 'agenda)
  12916. (lambda (b)
  12917. (with-current-buffer b
  12918. (and (eq major-mode 'org-mode)
  12919. (setq bfn (buffer-file-name b))
  12920. (member (file-truename bfn) agenda-files)))))
  12921. (t (lambda (b) (with-current-buffer b
  12922. (or (eq major-mode 'org-mode)
  12923. (string-match "\*Org .*Export"
  12924. (buffer-name b)))))))))
  12925. (delq nil
  12926. (mapcar
  12927. (lambda(b)
  12928. (if (and (funcall filter b)
  12929. (or (not exclude-tmp)
  12930. (not (string-match "tmp" (buffer-name b)))))
  12931. b
  12932. nil))
  12933. (buffer-list)))))
  12934. (defun org-agenda-files (&optional unrestricted archives)
  12935. "Get the list of agenda files.
  12936. Optional UNRESTRICTED means return the full list even if a restriction
  12937. is currently in place.
  12938. When ARCHIVES is t, include all archive files hat are really being
  12939. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12940. `org-agenda-archives-mode' is t."
  12941. (let ((files
  12942. (cond
  12943. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12944. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12945. ((listp org-agenda-files) org-agenda-files)
  12946. (t (error "Invalid value of `org-agenda-files'")))))
  12947. (setq files (apply 'append
  12948. (mapcar (lambda (f)
  12949. (if (file-directory-p f)
  12950. (directory-files
  12951. f t org-agenda-file-regexp)
  12952. (list f)))
  12953. files)))
  12954. (when org-agenda-skip-unavailable-files
  12955. (setq files (delq nil
  12956. (mapcar (function
  12957. (lambda (file)
  12958. (and (file-readable-p file) file)))
  12959. files))))
  12960. (when (or (eq archives t)
  12961. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12962. (setq files (org-add-archive-files files)))
  12963. files))
  12964. (defun org-edit-agenda-file-list ()
  12965. "Edit the list of agenda files.
  12966. Depending on setup, this either uses customize to edit the variable
  12967. `org-agenda-files', or it visits the file that is holding the list. In the
  12968. latter case, the buffer is set up in a way that saving it automatically kills
  12969. the buffer and restores the previous window configuration."
  12970. (interactive)
  12971. (if (stringp org-agenda-files)
  12972. (let ((cw (current-window-configuration)))
  12973. (find-file org-agenda-files)
  12974. (org-set-local 'org-window-configuration cw)
  12975. (org-add-hook 'after-save-hook
  12976. (lambda ()
  12977. (set-window-configuration
  12978. (prog1 org-window-configuration
  12979. (kill-buffer (current-buffer))))
  12980. (org-install-agenda-files-menu)
  12981. (message "New agenda file list installed"))
  12982. nil 'local)
  12983. (message "%s" (substitute-command-keys
  12984. "Edit list and finish with \\[save-buffer]")))
  12985. (customize-variable 'org-agenda-files)))
  12986. (defun org-store-new-agenda-file-list (list)
  12987. "Set new value for the agenda file list and save it correctly."
  12988. (if (stringp org-agenda-files)
  12989. (let ((f org-agenda-files) b)
  12990. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12991. (with-temp-file f
  12992. (insert (mapconcat 'identity list "\n") "\n")))
  12993. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12994. (org-insert-mode-line-in-empty-file nil))
  12995. (setq org-agenda-files list)
  12996. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12997. (defun org-read-agenda-file-list ()
  12998. "Read the list of agenda files from a file."
  12999. (when (file-directory-p org-agenda-files)
  13000. (error "`org-agenda-files' cannot be a single directory"))
  13001. (when (stringp org-agenda-files)
  13002. (with-temp-buffer
  13003. (insert-file-contents org-agenda-files)
  13004. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  13005. ;;;###autoload
  13006. (defun org-cycle-agenda-files ()
  13007. "Cycle through the files in `org-agenda-files'.
  13008. If the current buffer visits an agenda file, find the next one in the list.
  13009. If the current buffer does not, find the first agenda file."
  13010. (interactive)
  13011. (let* ((fs (org-agenda-files t))
  13012. (files (append fs (list (car fs))))
  13013. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13014. file)
  13015. (unless files (error "No agenda files"))
  13016. (catch 'exit
  13017. (while (setq file (pop files))
  13018. (if (equal (file-truename file) tcf)
  13019. (when (car files)
  13020. (find-file (car files))
  13021. (throw 'exit t))))
  13022. (find-file (car fs)))
  13023. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13024. (defun org-agenda-file-to-front (&optional to-end)
  13025. "Move/add the current file to the top of the agenda file list.
  13026. If the file is not present in the list, it is added to the front. If it is
  13027. present, it is moved there. With optional argument TO-END, add/move to the
  13028. end of the list."
  13029. (interactive "P")
  13030. (let ((org-agenda-skip-unavailable-files nil)
  13031. (file-alist (mapcar (lambda (x)
  13032. (cons (file-truename x) x))
  13033. (org-agenda-files t)))
  13034. (ctf (file-truename buffer-file-name))
  13035. x had)
  13036. (setq x (assoc ctf file-alist) had x)
  13037. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13038. (if to-end
  13039. (setq file-alist (append (delq x file-alist) (list x)))
  13040. (setq file-alist (cons x (delq x file-alist))))
  13041. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13042. (org-install-agenda-files-menu)
  13043. (message "File %s to %s of agenda file list"
  13044. (if had "moved" "added") (if to-end "end" "front"))))
  13045. (defun org-remove-file (&optional file)
  13046. "Remove current file from the list of files in variable `org-agenda-files'.
  13047. These are the files which are being checked for agenda entries.
  13048. Optional argument FILE means, use this file instead of the current."
  13049. (interactive)
  13050. (let* ((org-agenda-skip-unavailable-files nil)
  13051. (file (or file buffer-file-name))
  13052. (true-file (file-truename file))
  13053. (afile (abbreviate-file-name file))
  13054. (files (delq nil (mapcar
  13055. (lambda (x)
  13056. (if (equal true-file
  13057. (file-truename x))
  13058. nil x))
  13059. (org-agenda-files t)))))
  13060. (if (not (= (length files) (length (org-agenda-files t))))
  13061. (progn
  13062. (org-store-new-agenda-file-list files)
  13063. (org-install-agenda-files-menu)
  13064. (message "Removed file: %s" afile))
  13065. (message "File was not in list: %s (not removed)" afile))))
  13066. (defun org-file-menu-entry (file)
  13067. (vector file (list 'find-file file) t))
  13068. (defun org-check-agenda-file (file)
  13069. "Make sure FILE exists. If not, ask user what to do."
  13070. (when (not (file-exists-p file))
  13071. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13072. (abbreviate-file-name file))
  13073. (let ((r (downcase (read-char-exclusive))))
  13074. (cond
  13075. ((equal r ?r)
  13076. (org-remove-file file)
  13077. (throw 'nextfile t))
  13078. (t (error "Abort"))))))
  13079. (defun org-get-agenda-file-buffer (file)
  13080. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13081. it to the list of buffers which might be released later."
  13082. (let ((buf (org-find-base-buffer-visiting file)))
  13083. (if buf
  13084. buf ; just return it
  13085. ;; Make a new buffer and remember it
  13086. (setq buf (find-file-noselect file))
  13087. (if buf (push buf org-agenda-new-buffers))
  13088. buf)))
  13089. (defun org-release-buffers (blist)
  13090. "Release all buffers in list, asking the user for confirmation when needed.
  13091. When a buffer is unmodified, it is just killed. When modified, it is saved
  13092. \(if the user agrees) and then killed."
  13093. (let (buf file)
  13094. (while (setq buf (pop blist))
  13095. (setq file (buffer-file-name buf))
  13096. (when (and (buffer-modified-p buf)
  13097. file
  13098. (y-or-n-p (format "Save file %s? " file)))
  13099. (with-current-buffer buf (save-buffer)))
  13100. (kill-buffer buf))))
  13101. (defun org-prepare-agenda-buffers (files)
  13102. "Create buffers for all agenda files, protect archived trees and comments."
  13103. (interactive)
  13104. (let ((pa '(:org-archived t))
  13105. (pc '(:org-comment t))
  13106. (pall '(:org-archived t :org-comment t))
  13107. (inhibit-read-only t)
  13108. (rea (concat ":" org-archive-tag ":"))
  13109. bmp file re)
  13110. (save-excursion
  13111. (save-restriction
  13112. (while (setq file (pop files))
  13113. (catch 'nextfile
  13114. (if (bufferp file)
  13115. (set-buffer file)
  13116. (org-check-agenda-file file)
  13117. (set-buffer (org-get-agenda-file-buffer file)))
  13118. (widen)
  13119. (setq bmp (buffer-modified-p))
  13120. (org-refresh-category-properties)
  13121. (setq org-todo-keywords-for-agenda
  13122. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13123. (setq org-done-keywords-for-agenda
  13124. (append org-done-keywords-for-agenda org-done-keywords))
  13125. (setq org-todo-keyword-alist-for-agenda
  13126. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13127. (setq org-drawers-for-agenda
  13128. (append org-drawers-for-agenda org-drawers))
  13129. (setq org-tag-alist-for-agenda
  13130. (append org-tag-alist-for-agenda org-tag-alist))
  13131. (save-excursion
  13132. (remove-text-properties (point-min) (point-max) pall)
  13133. (when org-agenda-skip-archived-trees
  13134. (goto-char (point-min))
  13135. (while (re-search-forward rea nil t)
  13136. (if (org-on-heading-p t)
  13137. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13138. (goto-char (point-min))
  13139. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13140. (while (re-search-forward re nil t)
  13141. (add-text-properties
  13142. (match-beginning 0) (org-end-of-subtree t) pc)))
  13143. (set-buffer-modified-p bmp)))))
  13144. (setq org-todo-keyword-alist-for-agenda
  13145. (org-uniquify org-todo-keyword-alist-for-agenda)
  13146. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13147. ;;;; Embedded LaTeX
  13148. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13149. "Keymap for the minor `org-cdlatex-mode'.")
  13150. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13151. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13152. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13153. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13154. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13155. (defvar org-cdlatex-texmathp-advice-is-done nil
  13156. "Flag remembering if we have applied the advice to texmathp already.")
  13157. (define-minor-mode org-cdlatex-mode
  13158. "Toggle the minor `org-cdlatex-mode'.
  13159. This mode supports entering LaTeX environment and math in LaTeX fragments
  13160. in Org-mode.
  13161. \\{org-cdlatex-mode-map}"
  13162. nil " OCDL" nil
  13163. (when org-cdlatex-mode (require 'cdlatex))
  13164. (unless org-cdlatex-texmathp-advice-is-done
  13165. (setq org-cdlatex-texmathp-advice-is-done t)
  13166. (defadvice texmathp (around org-math-always-on activate)
  13167. "Always return t in org-mode buffers.
  13168. This is because we want to insert math symbols without dollars even outside
  13169. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13170. an embedded LaTeX fragment, let texmathp do its job.
  13171. \\[org-cdlatex-mode-map]"
  13172. (interactive)
  13173. (let (p)
  13174. (cond
  13175. ((not (org-mode-p)) ad-do-it)
  13176. ((eq this-command 'cdlatex-math-symbol)
  13177. (setq ad-return-value t
  13178. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13179. (t
  13180. (let ((p (org-inside-LaTeX-fragment-p)))
  13181. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13182. (setq ad-return-value t
  13183. texmathp-why '("Org-mode embedded math" . 0))
  13184. (if p ad-do-it)))))))))
  13185. (defun turn-on-org-cdlatex ()
  13186. "Unconditionally turn on `org-cdlatex-mode'."
  13187. (org-cdlatex-mode 1))
  13188. (defun org-inside-LaTeX-fragment-p ()
  13189. "Test if point is inside a LaTeX fragment.
  13190. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13191. sequence appearing also before point.
  13192. Even though the matchers for math are configurable, this function assumes
  13193. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13194. delimiters are skipped when they have been removed by customization.
  13195. The return value is nil, or a cons cell with the delimiter and
  13196. and the position of this delimiter.
  13197. This function does a reasonably good job, but can locally be fooled by
  13198. for example currency specifications. For example it will assume being in
  13199. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13200. fragments that are properly closed, but during editing, we have to live
  13201. with the uncertainty caused by missing closing delimiters. This function
  13202. looks only before point, not after."
  13203. (catch 'exit
  13204. (let ((pos (point))
  13205. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13206. (lim (progn
  13207. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13208. (point)))
  13209. dd-on str (start 0) m re)
  13210. (goto-char pos)
  13211. (when dodollar
  13212. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13213. re (nth 1 (assoc "$" org-latex-regexps)))
  13214. (while (string-match re str start)
  13215. (cond
  13216. ((= (match-end 0) (length str))
  13217. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13218. ((= (match-end 0) (- (length str) 5))
  13219. (throw 'exit nil))
  13220. (t (setq start (match-end 0))))))
  13221. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13222. (goto-char pos)
  13223. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13224. (and (match-beginning 2) (throw 'exit nil))
  13225. ;; count $$
  13226. (while (re-search-backward "\\$\\$" lim t)
  13227. (setq dd-on (not dd-on)))
  13228. (goto-char pos)
  13229. (if dd-on (cons "$$" m))))))
  13230. (defun org-try-cdlatex-tab ()
  13231. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13232. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13233. - inside a LaTeX fragment, or
  13234. - after the first word in a line, where an abbreviation expansion could
  13235. insert a LaTeX environment."
  13236. (when org-cdlatex-mode
  13237. (cond
  13238. ((save-excursion
  13239. (skip-chars-backward "a-zA-Z0-9*")
  13240. (skip-chars-backward " \t")
  13241. (bolp))
  13242. (cdlatex-tab) t)
  13243. ((org-inside-LaTeX-fragment-p)
  13244. (cdlatex-tab) t)
  13245. (t nil))))
  13246. (defun org-cdlatex-underscore-caret (&optional arg)
  13247. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13248. Revert to the normal definition outside of these fragments."
  13249. (interactive "P")
  13250. (if (org-inside-LaTeX-fragment-p)
  13251. (call-interactively 'cdlatex-sub-superscript)
  13252. (let (org-cdlatex-mode)
  13253. (call-interactively (key-binding (vector last-input-event))))))
  13254. (defun org-cdlatex-math-modify (&optional arg)
  13255. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13256. Revert to the normal definition outside of these fragments."
  13257. (interactive "P")
  13258. (if (org-inside-LaTeX-fragment-p)
  13259. (call-interactively 'cdlatex-math-modify)
  13260. (let (org-cdlatex-mode)
  13261. (call-interactively (key-binding (vector last-input-event))))))
  13262. (defvar org-latex-fragment-image-overlays nil
  13263. "List of overlays carrying the images of latex fragments.")
  13264. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13265. (defun org-remove-latex-fragment-image-overlays ()
  13266. "Remove all overlays with LaTeX fragment images in current buffer."
  13267. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13268. (setq org-latex-fragment-image-overlays nil))
  13269. (defun org-preview-latex-fragment (&optional subtree)
  13270. "Preview the LaTeX fragment at point, or all locally or globally.
  13271. If the cursor is in a LaTeX fragment, create the image and overlay
  13272. it over the source code. If there is no fragment at point, display
  13273. all fragments in the current text, from one headline to the next. With
  13274. prefix SUBTREE, display all fragments in the current subtree. With a
  13275. double prefix `C-u C-u', or when the cursor is before the first headline,
  13276. display all fragments in the buffer.
  13277. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13278. (interactive "P")
  13279. (org-remove-latex-fragment-image-overlays)
  13280. (save-excursion
  13281. (save-restriction
  13282. (let (beg end at msg)
  13283. (cond
  13284. ((or (equal subtree '(16))
  13285. (not (save-excursion
  13286. (re-search-backward (concat "^" outline-regexp) nil t))))
  13287. (setq beg (point-min) end (point-max)
  13288. msg "Creating images for buffer...%s"))
  13289. ((equal subtree '(4))
  13290. (org-back-to-heading)
  13291. (setq beg (point) end (org-end-of-subtree t)
  13292. msg "Creating images for subtree...%s"))
  13293. (t
  13294. (if (setq at (org-inside-LaTeX-fragment-p))
  13295. (goto-char (max (point-min) (- (cdr at) 2)))
  13296. (org-back-to-heading))
  13297. (setq beg (point) end (progn (outline-next-heading) (point))
  13298. msg (if at "Creating image...%s"
  13299. "Creating images for entry...%s"))))
  13300. (message msg "")
  13301. (narrow-to-region beg end)
  13302. (goto-char beg)
  13303. (org-format-latex
  13304. (concat "ltxpng/" (file-name-sans-extension
  13305. (file-name-nondirectory
  13306. buffer-file-name)))
  13307. default-directory 'overlays msg at 'forbuffer)
  13308. (message msg "done. Use `C-c C-c' to remove images.")))))
  13309. (defvar org-latex-regexps
  13310. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13311. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13312. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13313. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13314. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13315. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13316. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13317. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13318. "Regular expressions for matching embedded LaTeX.")
  13319. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13320. "Replace LaTeX fragments with links to an image, and produce images.
  13321. Some of the options can be changed using the variable
  13322. `org-format-latex-options'."
  13323. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13324. (let* ((prefixnodir (file-name-nondirectory prefix))
  13325. (absprefix (expand-file-name prefix dir))
  13326. (todir (file-name-directory absprefix))
  13327. (opt org-format-latex-options)
  13328. (matchers (plist-get opt :matchers))
  13329. (re-list org-latex-regexps)
  13330. (cnt 0) txt link beg end re e checkdir
  13331. executables-checked
  13332. m n block linkfile movefile ov)
  13333. ;; Check if there are old images files with this prefix, and remove them
  13334. (when (file-directory-p todir)
  13335. (mapc 'delete-file
  13336. (directory-files
  13337. todir 'full
  13338. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  13339. ;; Check the different regular expressions
  13340. (while (setq e (pop re-list))
  13341. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13342. block (if (nth 3 e) "\n\n" ""))
  13343. (when (member m matchers)
  13344. (goto-char (point-min))
  13345. (while (re-search-forward re nil t)
  13346. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13347. (not (get-text-property (match-beginning n)
  13348. 'org-protected))
  13349. (or (not overlays)
  13350. (not (eq (get-char-property (match-beginning n)
  13351. 'org-overlay-type)
  13352. 'org-latex-overlay))))
  13353. (setq txt (match-string n)
  13354. beg (match-beginning n) end (match-end n)
  13355. cnt (1+ cnt)
  13356. linkfile (format "%s_%04d.png" prefix cnt)
  13357. movefile (format "%s_%04d.png" absprefix cnt)
  13358. link (concat block "[[file:" linkfile "]]" block))
  13359. (if msg (message msg cnt))
  13360. (goto-char beg)
  13361. (unless checkdir ; make sure the directory exists
  13362. (setq checkdir t)
  13363. (or (file-directory-p todir) (make-directory todir)))
  13364. (unless executables-checked
  13365. (org-check-external-command
  13366. "latex" "needed to convert LaTeX fragments to images")
  13367. (org-check-external-command
  13368. "dvipng" "needed to convert LaTeX fragments to images")
  13369. (setq executables-checked t))
  13370. (org-create-formula-image
  13371. txt movefile opt forbuffer)
  13372. (if overlays
  13373. (progn
  13374. (mapc (lambda (o)
  13375. (if (eq (org-overlay-get o 'org-overlay-type)
  13376. 'org-latex-overlay)
  13377. (org-delete-overlay o)))
  13378. (org-overlays-in beg end))
  13379. (setq ov (org-make-overlay beg end))
  13380. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13381. (if (featurep 'xemacs)
  13382. (progn
  13383. (org-overlay-put ov 'invisible t)
  13384. (org-overlay-put
  13385. ov 'end-glyph
  13386. (make-glyph (vector 'png :file movefile))))
  13387. (org-overlay-put
  13388. ov 'display
  13389. (list 'image :type 'png :file movefile :ascent 'center)))
  13390. (push ov org-latex-fragment-image-overlays)
  13391. (goto-char end))
  13392. (delete-region beg end)
  13393. (insert link))))))))
  13394. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  13395. ;; This function borrows from Ganesh Swami's latex2png.el
  13396. (defun org-create-formula-image (string tofile options buffer)
  13397. "This calls dvipng."
  13398. (require 'org-latex)
  13399. (let* ((tmpdir (if (featurep 'xemacs)
  13400. (temp-directory)
  13401. temporary-file-directory))
  13402. (texfilebase (make-temp-name
  13403. (expand-file-name "orgtex" tmpdir)))
  13404. (texfile (concat texfilebase ".tex"))
  13405. (dvifile (concat texfilebase ".dvi"))
  13406. (pngfile (concat texfilebase ".png"))
  13407. (fnh (if (featurep 'xemacs)
  13408. (font-height (get-face-font 'default))
  13409. (face-attribute 'default :height nil)))
  13410. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13411. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13412. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13413. "Black"))
  13414. (bg (or (plist-get options (if buffer :background :html-background))
  13415. "Transparent")))
  13416. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13417. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13418. (with-temp-file texfile
  13419. (insert org-format-latex-header
  13420. (if org-export-latex-packages-alist
  13421. (concat "\n"
  13422. (mapconcat (lambda(p)
  13423. (if (equal "" (car p))
  13424. (format "\\usepackage{%s}" (cadr p))
  13425. (format "\\usepackage[%s]{%s}"
  13426. (car p) (cadr p))))
  13427. org-export-latex-packages-alist "\n"))
  13428. "")
  13429. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13430. (let ((dir default-directory))
  13431. (condition-case nil
  13432. (progn
  13433. (cd tmpdir)
  13434. (call-process "latex" nil nil nil texfile))
  13435. (error nil))
  13436. (cd dir))
  13437. (if (not (file-exists-p dvifile))
  13438. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13439. (condition-case nil
  13440. (call-process "dvipng" nil nil nil
  13441. "-fg" fg "-bg" bg
  13442. "-D" dpi
  13443. ;;"-x" scale "-y" scale
  13444. "-T" "tight"
  13445. "-o" pngfile
  13446. dvifile)
  13447. (error nil))
  13448. (if (not (file-exists-p pngfile))
  13449. (progn (message "Failed to create png file from %s" texfile) nil)
  13450. ;; Use the requested file name and clean up
  13451. (copy-file pngfile tofile 'replace)
  13452. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13453. (delete-file (concat texfilebase e)))
  13454. pngfile))))
  13455. (defun org-dvipng-color (attr)
  13456. "Return an rgb color specification for dvipng."
  13457. (apply 'format "rgb %s %s %s"
  13458. (mapcar 'org-normalize-color
  13459. (color-values (face-attribute 'default attr nil)))))
  13460. (defun org-normalize-color (value)
  13461. "Return string to be used as color value for an RGB component."
  13462. (format "%g" (/ value 65535.0)))
  13463. ;;;; Key bindings
  13464. ;; Make `C-c C-x' a prefix key
  13465. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13466. ;; TAB key with modifiers
  13467. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13468. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13469. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13470. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13471. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13472. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13473. ;; The following line is necessary under Suse GNU/Linux
  13474. (unless (featurep 'xemacs)
  13475. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13476. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13477. (define-key org-mode-map [backtab] 'org-shifttab)
  13478. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13479. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13480. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13481. ;; Cursor keys with modifiers
  13482. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13483. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13484. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13485. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13486. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13487. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13488. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13489. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13490. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13491. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13492. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13493. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13494. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13495. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13496. ;;; Extra keys for tty access.
  13497. ;; We only set them when really needed because otherwise the
  13498. ;; menus don't show the simple keys
  13499. (when (or org-use-extra-keys
  13500. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13501. (not window-system))
  13502. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13503. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13504. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13505. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13506. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13507. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13508. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13509. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13510. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13511. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13512. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13513. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13514. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13515. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13516. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13517. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13518. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13519. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13520. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13521. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13522. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13523. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13524. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13525. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13526. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13527. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13528. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13529. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13530. ;; All the other keys
  13531. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13532. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13533. (if (boundp 'narrow-map)
  13534. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13535. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13536. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13537. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13538. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13539. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13540. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13541. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13542. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13543. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13544. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13545. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13546. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13547. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13548. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13549. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13550. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13551. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13552. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13553. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13554. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13555. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13556. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13557. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13558. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13559. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13560. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13561. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13562. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13563. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13564. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13565. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13566. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13567. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13568. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13569. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13570. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13571. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13572. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13573. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13574. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13575. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13576. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13577. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13578. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13579. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13580. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13581. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13582. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13583. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13584. (org-defkey org-mode-map "\C-m" 'org-return)
  13585. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13586. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13587. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13588. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13589. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13590. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13591. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13592. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13593. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13594. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13595. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13596. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13597. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13598. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13599. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13600. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13601. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13602. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13603. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13604. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13605. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13606. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13607. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13608. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13609. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13610. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13611. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13612. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13613. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13614. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13615. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13616. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13617. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13618. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13619. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13620. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13621. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13622. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13623. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13624. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13625. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13626. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13627. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13628. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13629. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13630. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13631. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13632. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13633. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13634. (when (featurep 'xemacs)
  13635. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13636. (defconst org-speed-commands-default
  13637. '(
  13638. ("Outline Navigation")
  13639. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  13640. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  13641. ("f" . (org-speed-move-safe 'org-forward-same-level))
  13642. ("b" . (org-speed-move-safe 'org-backward-same-level))
  13643. ("u" . (org-speed-move-safe 'outline-up-heading))
  13644. ("j" . org-goto)
  13645. ("g" . (org-refile t))
  13646. ("Outline Visibility")
  13647. ("c" . org-cycle)
  13648. ("C" . org-shifttab)
  13649. (" " . org-display-outline-path)
  13650. ("Outline Structure Editing")
  13651. ("U" . org-shiftmetaup)
  13652. ("D" . org-shiftmetadown)
  13653. ("r" . org-metaright)
  13654. ("l" . org-metaleft)
  13655. ("R" . org-shiftmetaright)
  13656. ("L" . org-shiftmetaleft)
  13657. ("i" . (progn (forward-char 1) (call-interactively
  13658. 'org-insert-heading-respect-content)))
  13659. ("^" . org-sort)
  13660. ("w" . org-refile)
  13661. ("a" . org-archive-subtree-default-with-confirmation)
  13662. ("." . outline-mark-subtree)
  13663. ("Clock Commands")
  13664. ("I" . org-clock-in)
  13665. ("O" . org-clock-out)
  13666. ("Meta Data Editing")
  13667. ("t" . org-todo)
  13668. ("0" . (org-priority ?\ ))
  13669. ("1" . (org-priority ?A))
  13670. ("2" . (org-priority ?B))
  13671. ("3" . (org-priority ?C))
  13672. (";" . org-set-tags-command)
  13673. ("e" . org-set-effort)
  13674. ("Agenda Views etc")
  13675. ("v" . org-agenda)
  13676. ("/" . org-sparse-tree)
  13677. ("/" . org-sparse-tree)
  13678. ("Misc")
  13679. ("o" . org-open-at-point)
  13680. ("?" . org-speed-command-help)
  13681. )
  13682. "The default speed commands.")
  13683. (defun org-print-speed-command (e)
  13684. (if (> (length (car e)) 1)
  13685. (progn
  13686. (princ "\n")
  13687. (princ (car e))
  13688. (princ "\n")
  13689. (princ (make-string (length (car e)) ?-))
  13690. (princ "\n"))
  13691. (princ (car e))
  13692. (princ " ")
  13693. (if (symbolp (cdr e))
  13694. (princ (symbol-name (cdr e)))
  13695. (prin1 (cdr e)))
  13696. (princ "\n")))
  13697. (defun org-speed-command-help ()
  13698. "Show the available speed commands."
  13699. (interactive)
  13700. (if (not org-use-speed-commands)
  13701. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13702. (with-output-to-temp-buffer "*Help*"
  13703. (princ "User-defined Speed commands\n===========================\n")
  13704. (mapc 'org-print-speed-command org-speed-commands-user)
  13705. (princ "\n")
  13706. (princ "Built-in Speed commands\n=======================\n")
  13707. (mapc 'org-print-speed-command org-speed-commands-default))
  13708. (with-current-buffer "*Help*"
  13709. (setq truncate-lines t))))
  13710. (defun org-speed-move-safe (cmd)
  13711. "Execute CMD, but make sure that the cursor always ends up in a headline.
  13712. If not, return to the original position and throw an error."
  13713. (interactive)
  13714. (let ((pos (point)))
  13715. (call-interactively cmd)
  13716. (unless (and (bolp) (org-on-heading-p))
  13717. (goto-char pos)
  13718. (error "Boundary reached while executing %s" cmd))))
  13719. (defvar org-self-insert-command-undo-counter 0)
  13720. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13721. (defvar org-speed-command nil)
  13722. (defun org-self-insert-command (N)
  13723. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13724. If the cursor is in a table looking at whitespace, the whitespace is
  13725. overwritten, and the table is not marked as requiring realignment."
  13726. (interactive "p")
  13727. (cond
  13728. ((and org-use-speed-commands
  13729. (or (and (bolp) (looking-at outline-regexp))
  13730. (and (functionp org-use-speed-commands)
  13731. (funcall org-use-speed-commands)))
  13732. (setq
  13733. org-speed-command
  13734. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  13735. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  13736. (cond
  13737. ((commandp org-speed-command)
  13738. (setq this-command org-speed-command)
  13739. (call-interactively org-speed-command))
  13740. ((functionp org-speed-command)
  13741. (funcall org-speed-command))
  13742. ((and org-speed-command (listp org-speed-command))
  13743. (eval org-speed-command))
  13744. (t (let (org-use-speed-commands)
  13745. (call-interactively 'org-self-insert-command)))))
  13746. ((and
  13747. (org-table-p)
  13748. (progn
  13749. ;; check if we blank the field, and if that triggers align
  13750. (and (featurep 'org-table) org-table-auto-blank-field
  13751. (member last-command
  13752. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13753. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13754. ;; got extra space, this field does not determine column width
  13755. (let (org-table-may-need-update) (org-table-blank-field))
  13756. ;; no extra space, this field may determine column width
  13757. (org-table-blank-field)))
  13758. t)
  13759. (eq N 1)
  13760. (looking-at "[^|\n]* |"))
  13761. (let (org-table-may-need-update)
  13762. (goto-char (1- (match-end 0)))
  13763. (delete-backward-char 1)
  13764. (goto-char (match-beginning 0))
  13765. (self-insert-command N)))
  13766. (t
  13767. (setq org-table-may-need-update t)
  13768. (self-insert-command N)
  13769. (org-fix-tags-on-the-fly)
  13770. (if org-self-insert-cluster-for-undo
  13771. (if (not (eq last-command 'org-self-insert-command))
  13772. (setq org-self-insert-command-undo-counter 1)
  13773. (if (>= org-self-insert-command-undo-counter 20)
  13774. (setq org-self-insert-command-undo-counter 1)
  13775. (and (> org-self-insert-command-undo-counter 0)
  13776. buffer-undo-list
  13777. (not (cadr buffer-undo-list)) ; remove nil entry
  13778. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13779. (setq org-self-insert-command-undo-counter
  13780. (1+ org-self-insert-command-undo-counter))))))))
  13781. (defun org-fix-tags-on-the-fly ()
  13782. (when (and (equal (char-after (point-at-bol)) ?*)
  13783. (org-on-heading-p))
  13784. (org-align-tags-here org-tags-column)))
  13785. (defun org-delete-backward-char (N)
  13786. "Like `delete-backward-char', insert whitespace at field end in tables.
  13787. When deleting backwards, in tables this function will insert whitespace in
  13788. front of the next \"|\" separator, to keep the table aligned. The table will
  13789. still be marked for re-alignment if the field did fill the entire column,
  13790. because, in this case the deletion might narrow the column."
  13791. (interactive "p")
  13792. (if (and (org-table-p)
  13793. (eq N 1)
  13794. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13795. (looking-at ".*?|"))
  13796. (let ((pos (point))
  13797. (noalign (looking-at "[^|\n\r]* |"))
  13798. (c org-table-may-need-update))
  13799. (backward-delete-char N)
  13800. (skip-chars-forward "^|")
  13801. (insert " ")
  13802. (goto-char (1- pos))
  13803. ;; noalign: if there were two spaces at the end, this field
  13804. ;; does not determine the width of the column.
  13805. (if noalign (setq org-table-may-need-update c)))
  13806. (backward-delete-char N)
  13807. (org-fix-tags-on-the-fly)))
  13808. (defun org-delete-char (N)
  13809. "Like `delete-char', but insert whitespace at field end in tables.
  13810. When deleting characters, in tables this function will insert whitespace in
  13811. front of the next \"|\" separator, to keep the table aligned. The table will
  13812. still be marked for re-alignment if the field did fill the entire column,
  13813. because, in this case the deletion might narrow the column."
  13814. (interactive "p")
  13815. (if (and (org-table-p)
  13816. (not (bolp))
  13817. (not (= (char-after) ?|))
  13818. (eq N 1))
  13819. (if (looking-at ".*?|")
  13820. (let ((pos (point))
  13821. (noalign (looking-at "[^|\n\r]* |"))
  13822. (c org-table-may-need-update))
  13823. (replace-match (concat
  13824. (substring (match-string 0) 1 -1)
  13825. " |"))
  13826. (goto-char pos)
  13827. ;; noalign: if there were two spaces at the end, this field
  13828. ;; does not determine the width of the column.
  13829. (if noalign (setq org-table-may-need-update c)))
  13830. (delete-char N))
  13831. (delete-char N)
  13832. (org-fix-tags-on-the-fly)))
  13833. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13834. (put 'org-self-insert-command 'delete-selection t)
  13835. (put 'orgtbl-self-insert-command 'delete-selection t)
  13836. (put 'org-delete-char 'delete-selection 'supersede)
  13837. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13838. (put 'org-yank 'delete-selection 'yank)
  13839. ;; Make `flyspell-mode' delay after some commands
  13840. (put 'org-self-insert-command 'flyspell-delayed t)
  13841. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13842. (put 'org-delete-char 'flyspell-delayed t)
  13843. (put 'org-delete-backward-char 'flyspell-delayed t)
  13844. ;; Make pabbrev-mode expand after org-mode commands
  13845. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13846. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13847. ;; How to do this: Measure non-white length of current string
  13848. ;; If equal to column width, we should realign.
  13849. (defun org-remap (map &rest commands)
  13850. "In MAP, remap the functions given in COMMANDS.
  13851. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13852. (let (new old)
  13853. (while commands
  13854. (setq old (pop commands) new (pop commands))
  13855. (if (fboundp 'command-remapping)
  13856. (org-defkey map (vector 'remap old) new)
  13857. (substitute-key-definition old new map global-map)))))
  13858. (when (eq org-enable-table-editor 'optimized)
  13859. ;; If the user wants maximum table support, we need to hijack
  13860. ;; some standard editing functions
  13861. (org-remap org-mode-map
  13862. 'self-insert-command 'org-self-insert-command
  13863. 'delete-char 'org-delete-char
  13864. 'delete-backward-char 'org-delete-backward-char)
  13865. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13866. (defvar org-ctrl-c-ctrl-c-hook nil
  13867. "Hook for functions attaching themselves to `C-c C-c'.
  13868. This can be used to add additional functionality to the C-c C-c key which
  13869. executes context-dependent commands.
  13870. Each function will be called with no arguments. The function must check
  13871. if the context is appropriate for it to act. If yes, it should do its
  13872. thing and then return a non-nil value. If the context is wrong,
  13873. just do nothing and return nil.")
  13874. (defvar org-tab-first-hook nil
  13875. "Hook for functions to attach themselves to TAB.
  13876. See `org-ctrl-c-ctrl-c-hook' for more information.
  13877. This hook runs as the first action when TAB is pressed, even before
  13878. `org-cycle' messes around with the `outline-regexp' to cater for
  13879. inline tasks and plain list item folding.
  13880. If any function in this hook returns t, not other actions like table
  13881. field motion visibility cycling will be done.")
  13882. (defvar org-tab-after-check-for-table-hook nil
  13883. "Hook for functions to attach themselves to TAB.
  13884. See `org-ctrl-c-ctrl-c-hook' for more information.
  13885. This hook runs after it has been established that the cursor is not in a
  13886. table, but before checking if the cursor is in a headline or if global cycling
  13887. should be done.
  13888. If any function in this hook returns t, not other actions like visibility
  13889. cycling will be done.")
  13890. (defvar org-tab-after-check-for-cycling-hook nil
  13891. "Hook for functions to attach themselves to TAB.
  13892. See `org-ctrl-c-ctrl-c-hook' for more information.
  13893. This hook runs after it has been established that not table field motion and
  13894. not visibility should be done because of current context. This is probably
  13895. the place where a package like yasnippets can hook in.")
  13896. (defvar org-tab-before-tab-emulation-hook nil
  13897. "Hook for functions to attach themselves to TAB.
  13898. See `org-ctrl-c-ctrl-c-hook' for more information.
  13899. This hook runs after every other options for TAB have been exhausted, but
  13900. before indentation and \t insertion takes place.")
  13901. (defvar org-metaleft-hook nil
  13902. "Hook for functions attaching themselves to `M-left'.
  13903. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13904. (defvar org-metaright-hook nil
  13905. "Hook for functions attaching themselves to `M-right'.
  13906. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13907. (defvar org-metaup-hook nil
  13908. "Hook for functions attaching themselves to `M-up'.
  13909. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13910. (defvar org-metadown-hook nil
  13911. "Hook for functions attaching themselves to `M-down'.
  13912. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13913. (defvar org-shiftmetaleft-hook nil
  13914. "Hook for functions attaching themselves to `M-S-left'.
  13915. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13916. (defvar org-shiftmetaright-hook nil
  13917. "Hook for functions attaching themselves to `M-S-right'.
  13918. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13919. (defvar org-shiftmetaup-hook nil
  13920. "Hook for functions attaching themselves to `M-S-up'.
  13921. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13922. (defvar org-shiftmetadown-hook nil
  13923. "Hook for functions attaching themselves to `M-S-down'.
  13924. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13925. (defvar org-metareturn-hook nil
  13926. "Hook for functions attaching themselves to `M-RET'.
  13927. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13928. (defun org-modifier-cursor-error ()
  13929. "Throw an error, a modified cursor command was applied in wrong context."
  13930. (error "This command is active in special context like tables, headlines or items"))
  13931. (defun org-shiftselect-error ()
  13932. "Throw an error because Shift-Cursor command was applied in wrong context."
  13933. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13934. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  13935. (error "This command works only in special context like headlines or timestamps")))
  13936. (defun org-call-for-shift-select (cmd)
  13937. (let ((this-command-keys-shift-translated t))
  13938. (call-interactively cmd)))
  13939. (defun org-shifttab (&optional arg)
  13940. "Global visibility cycling or move to previous table field.
  13941. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13942. on context.
  13943. See the individual commands for more information."
  13944. (interactive "P")
  13945. (cond
  13946. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13947. ((integerp arg)
  13948. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13949. (message "Content view to level: %d" arg)
  13950. (org-content (prefix-numeric-value arg2))
  13951. (setq org-cycle-global-status 'overview)))
  13952. (t (call-interactively 'org-global-cycle))))
  13953. (defun org-shiftmetaleft ()
  13954. "Promote subtree or delete table column.
  13955. Calls `org-promote-subtree', `org-outdent-item',
  13956. or `org-table-delete-column', depending on context.
  13957. See the individual commands for more information."
  13958. (interactive)
  13959. (cond
  13960. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13961. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13962. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13963. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13964. (t (org-modifier-cursor-error))))
  13965. (defun org-shiftmetaright ()
  13966. "Demote subtree or insert table column.
  13967. Calls `org-demote-subtree', `org-indent-item',
  13968. or `org-table-insert-column', depending on context.
  13969. See the individual commands for more information."
  13970. (interactive)
  13971. (cond
  13972. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13973. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13974. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13975. ((org-at-item-p) (call-interactively 'org-indent-item))
  13976. (t (org-modifier-cursor-error))))
  13977. (defun org-shiftmetaup (&optional arg)
  13978. "Move subtree up or kill table row.
  13979. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13980. `org-move-item-up' depending on context. See the individual commands
  13981. for more information."
  13982. (interactive "P")
  13983. (cond
  13984. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13985. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13986. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13987. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13988. (t (org-modifier-cursor-error))))
  13989. (defun org-shiftmetadown (&optional arg)
  13990. "Move subtree down or insert table row.
  13991. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13992. `org-move-item-down', depending on context. See the individual
  13993. commands for more information."
  13994. (interactive "P")
  13995. (cond
  13996. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13997. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13998. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13999. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14000. (t (org-modifier-cursor-error))))
  14001. (defun org-metaleft (&optional arg)
  14002. "Promote heading or move table column to left.
  14003. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14004. With no specific context, calls the Emacs default `backward-word'.
  14005. See the individual commands for more information."
  14006. (interactive "P")
  14007. (cond
  14008. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14009. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14010. ((or (org-on-heading-p)
  14011. (and (org-region-active-p)
  14012. (save-excursion
  14013. (goto-char (region-beginning))
  14014. (org-on-heading-p))))
  14015. (call-interactively 'org-do-promote))
  14016. ((or (org-at-item-p)
  14017. (and (org-region-active-p)
  14018. (save-excursion
  14019. (goto-char (region-beginning))
  14020. (org-at-item-p))))
  14021. (call-interactively 'org-outdent-item))
  14022. (t (call-interactively 'backward-word))))
  14023. (defun org-metaright (&optional arg)
  14024. "Demote subtree or move table column to right.
  14025. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14026. With no specific context, calls the Emacs default `forward-word'.
  14027. See the individual commands for more information."
  14028. (interactive "P")
  14029. (cond
  14030. ((run-hook-with-args-until-success 'org-metaright-hook))
  14031. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14032. ((or (org-on-heading-p)
  14033. (and (org-region-active-p)
  14034. (save-excursion
  14035. (goto-char (region-beginning))
  14036. (org-on-heading-p))))
  14037. (call-interactively 'org-do-demote))
  14038. ((or (org-at-item-p)
  14039. (and (org-region-active-p)
  14040. (save-excursion
  14041. (goto-char (region-beginning))
  14042. (org-at-item-p))))
  14043. (call-interactively 'org-indent-item))
  14044. (t (call-interactively 'forward-word))))
  14045. (defun org-metaup (&optional arg)
  14046. "Move subtree up or move table row up.
  14047. Calls `org-move-subtree-up' or `org-table-move-row' or
  14048. `org-move-item-up', depending on context. See the individual commands
  14049. for more information."
  14050. (interactive "P")
  14051. (cond
  14052. ((run-hook-with-args-until-success 'org-metaup-hook))
  14053. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14054. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14055. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14056. (t (transpose-lines 1) (beginning-of-line -1))))
  14057. (defun org-metadown (&optional arg)
  14058. "Move subtree down or move table row down.
  14059. Calls `org-move-subtree-down' or `org-table-move-row' or
  14060. `org-move-item-down', depending on context. See the individual
  14061. commands for more information."
  14062. (interactive "P")
  14063. (cond
  14064. ((run-hook-with-args-until-success 'org-metadown-hook))
  14065. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14066. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14067. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14068. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14069. (defun org-shiftup (&optional arg)
  14070. "Increase item in timestamp or increase priority of current headline.
  14071. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14072. depending on context. See the individual commands for more information."
  14073. (interactive "P")
  14074. (cond
  14075. ((and org-support-shift-select (org-region-active-p))
  14076. (org-call-for-shift-select 'previous-line))
  14077. ((org-at-timestamp-p t)
  14078. (call-interactively (if org-edit-timestamp-down-means-later
  14079. 'org-timestamp-down 'org-timestamp-up)))
  14080. ((and (not (eq org-support-shift-select 'always))
  14081. org-enable-priority-commands
  14082. (org-on-heading-p))
  14083. (call-interactively 'org-priority-up))
  14084. ((and (not org-support-shift-select) (org-at-item-p))
  14085. (call-interactively 'org-previous-item))
  14086. ((org-clocktable-try-shift 'up arg))
  14087. (org-support-shift-select
  14088. (org-call-for-shift-select 'previous-line))
  14089. (t (org-shiftselect-error))))
  14090. (defun org-shiftdown (&optional arg)
  14091. "Decrease item in timestamp or decrease priority of current headline.
  14092. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14093. depending on context. See the individual commands for more information."
  14094. (interactive "P")
  14095. (cond
  14096. ((and org-support-shift-select (org-region-active-p))
  14097. (org-call-for-shift-select 'next-line))
  14098. ((org-at-timestamp-p t)
  14099. (call-interactively (if org-edit-timestamp-down-means-later
  14100. 'org-timestamp-up 'org-timestamp-down)))
  14101. ((and (not (eq org-support-shift-select 'always))
  14102. org-enable-priority-commands
  14103. (org-on-heading-p))
  14104. (call-interactively 'org-priority-down))
  14105. ((and (not org-support-shift-select) (org-at-item-p))
  14106. (call-interactively 'org-next-item))
  14107. ((org-clocktable-try-shift 'down arg))
  14108. (org-support-shift-select
  14109. (org-call-for-shift-select 'next-line))
  14110. (t (org-shiftselect-error))))
  14111. (defun org-shiftright (&optional arg)
  14112. "Cycle the thing at point or in the current line, depending on context.
  14113. Depending on context, this does one of the following:
  14114. - switch a timestamp at point one day into the future
  14115. - on a headline, switch to the next TODO keyword.
  14116. - on an item, switch entire list to the next bullet type
  14117. - on a property line, switch to the next allowed value
  14118. - on a clocktable definition line, move time block into the future"
  14119. (interactive "P")
  14120. (cond
  14121. ((and org-support-shift-select (org-region-active-p))
  14122. (org-call-for-shift-select 'forward-char))
  14123. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14124. ((and (not (eq org-support-shift-select 'always))
  14125. (org-on-heading-p))
  14126. (let ((org-inhibit-logging
  14127. (not org-treat-S-cursor-todo-selection-as-state-change))
  14128. (org-inhibit-blocking
  14129. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14130. (org-call-with-arg 'org-todo 'right)))
  14131. ((or (and org-support-shift-select
  14132. (not (eq org-support-shift-select 'always))
  14133. (org-at-item-bullet-p))
  14134. (and (not org-support-shift-select) (org-at-item-p)))
  14135. (org-call-with-arg 'org-cycle-list-bullet nil))
  14136. ((and (not (eq org-support-shift-select 'always))
  14137. (org-at-property-p))
  14138. (call-interactively 'org-property-next-allowed-value))
  14139. ((org-clocktable-try-shift 'right arg))
  14140. (org-support-shift-select
  14141. (org-call-for-shift-select 'forward-char))
  14142. (t (org-shiftselect-error))))
  14143. (defun org-shiftleft (&optional arg)
  14144. "Cycle the thing at point or in the current line, depending on context.
  14145. Depending on context, this does one of the following:
  14146. - switch a timestamp at point one day into the past
  14147. - on a headline, switch to the previous TODO keyword.
  14148. - on an item, switch entire list to the previous bullet type
  14149. - on a property line, switch to the previous allowed value
  14150. - on a clocktable definition line, move time block into the past"
  14151. (interactive "P")
  14152. (cond
  14153. ((and org-support-shift-select (org-region-active-p))
  14154. (org-call-for-shift-select 'backward-char))
  14155. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14156. ((and (not (eq org-support-shift-select 'always))
  14157. (org-on-heading-p))
  14158. (let ((org-inhibit-logging
  14159. (not org-treat-S-cursor-todo-selection-as-state-change))
  14160. (org-inhibit-blocking
  14161. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14162. (org-call-with-arg 'org-todo 'left)))
  14163. ((or (and org-support-shift-select
  14164. (not (eq org-support-shift-select 'always))
  14165. (org-at-item-bullet-p))
  14166. (and (not org-support-shift-select) (org-at-item-p)))
  14167. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14168. ((and (not (eq org-support-shift-select 'always))
  14169. (org-at-property-p))
  14170. (call-interactively 'org-property-previous-allowed-value))
  14171. ((org-clocktable-try-shift 'left arg))
  14172. (org-support-shift-select
  14173. (org-call-for-shift-select 'backward-char))
  14174. (t (org-shiftselect-error))))
  14175. (defun org-shiftcontrolright ()
  14176. "Switch to next TODO set."
  14177. (interactive)
  14178. (cond
  14179. ((and org-support-shift-select (org-region-active-p))
  14180. (org-call-for-shift-select 'forward-word))
  14181. ((and (not (eq org-support-shift-select 'always))
  14182. (org-on-heading-p))
  14183. (org-call-with-arg 'org-todo 'nextset))
  14184. (org-support-shift-select
  14185. (org-call-for-shift-select 'forward-word))
  14186. (t (org-shiftselect-error))))
  14187. (defun org-shiftcontrolleft ()
  14188. "Switch to previous TODO set."
  14189. (interactive)
  14190. (cond
  14191. ((and org-support-shift-select (org-region-active-p))
  14192. (org-call-for-shift-select 'backward-word))
  14193. ((and (not (eq org-support-shift-select 'always))
  14194. (org-on-heading-p))
  14195. (org-call-with-arg 'org-todo 'previousset))
  14196. (org-support-shift-select
  14197. (org-call-for-shift-select 'backward-word))
  14198. (t (org-shiftselect-error))))
  14199. (defun org-ctrl-c-ret ()
  14200. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14201. (interactive)
  14202. (cond
  14203. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14204. (t (call-interactively 'org-insert-heading))))
  14205. (defun org-copy-special ()
  14206. "Copy region in table or copy current subtree.
  14207. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14208. See the individual commands for more information."
  14209. (interactive)
  14210. (call-interactively
  14211. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14212. (defun org-cut-special ()
  14213. "Cut region in table or cut current subtree.
  14214. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14215. See the individual commands for more information."
  14216. (interactive)
  14217. (call-interactively
  14218. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14219. (defun org-paste-special (arg)
  14220. "Paste rectangular region into table, or past subtree relative to level.
  14221. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14222. See the individual commands for more information."
  14223. (interactive "P")
  14224. (if (org-at-table-p)
  14225. (org-table-paste-rectangle)
  14226. (org-paste-subtree arg)))
  14227. (defun org-edit-special ()
  14228. "Call a special editor for the stuff at point.
  14229. When at a table, call the formula editor with `org-table-edit-formulas'.
  14230. When at the first line of an src example, call `org-edit-src-code'.
  14231. When in an #+include line, visit the include file. Otherwise call
  14232. `ffap' to visit the file at point."
  14233. (interactive)
  14234. (cond
  14235. ((org-at-table-p)
  14236. (call-interactively 'org-table-edit-formulas))
  14237. ((save-excursion
  14238. (beginning-of-line 1)
  14239. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14240. (find-file (org-trim (match-string 1))))
  14241. ((org-edit-src-code))
  14242. ((org-edit-fixed-width-region))
  14243. (t (call-interactively 'ffap))))
  14244. (defun org-ctrl-c-ctrl-c (&optional arg)
  14245. "Set tags in headline, or update according to changed information at point.
  14246. This command does many different things, depending on context:
  14247. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14248. this is what we do.
  14249. - If the cursor is on a statistics cookie, update it.
  14250. - If the cursor is in a headline, prompt for tags and insert them
  14251. into the current line, aligned to `org-tags-column'. When called
  14252. with prefix arg, realign all tags in the current buffer.
  14253. - If the cursor is in one of the special #+KEYWORD lines, this
  14254. triggers scanning the buffer for these lines and updating the
  14255. information.
  14256. - If the cursor is inside a table, realign the table. This command
  14257. works even if the automatic table editor has been turned off.
  14258. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14259. the entire table.
  14260. - If the cursor is at a footnote reference or definition, jump to
  14261. the corresponding definition or references, respectively.
  14262. - If the cursor is a the beginning of a dynamic block, update it.
  14263. - If the cursor is inside a table created by the table.el package,
  14264. activate that table.
  14265. - If the current buffer is a remember buffer, close note and file
  14266. it. A prefix argument of 1 files to the default location
  14267. without further interaction. A prefix argument of 2 files to
  14268. the currently clocking task.
  14269. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14270. links in this buffer.
  14271. - If the cursor is on a numbered item in a plain list, renumber the
  14272. ordered list.
  14273. - If the cursor is on a checkbox, toggle it."
  14274. (interactive "P")
  14275. (let ((org-enable-table-editor t))
  14276. (cond
  14277. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14278. org-occur-highlights
  14279. org-latex-fragment-image-overlays)
  14280. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14281. (org-remove-occur-highlights)
  14282. (org-remove-latex-fragment-image-overlays)
  14283. (message "Temporary highlights/overlays removed from current buffer"))
  14284. ((and (local-variable-p 'org-finish-function (current-buffer))
  14285. (fboundp org-finish-function))
  14286. (funcall org-finish-function))
  14287. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14288. ((org-at-property-p)
  14289. (call-interactively 'org-property-action))
  14290. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14291. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14292. (or (org-on-heading-p) (org-at-item-p)))
  14293. (call-interactively 'org-update-statistics-cookies))
  14294. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14295. ((org-at-table.el-p)
  14296. (require 'table)
  14297. (beginning-of-line 1)
  14298. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14299. (call-interactively 'table-recognize-table))
  14300. ((org-at-table-p)
  14301. (org-table-maybe-eval-formula)
  14302. (if arg
  14303. (call-interactively 'org-table-recalculate)
  14304. (org-table-maybe-recalculate-line))
  14305. (call-interactively 'org-table-align))
  14306. ((or (org-footnote-at-reference-p)
  14307. (org-footnote-at-definition-p))
  14308. (call-interactively 'org-footnote-action))
  14309. ((org-at-item-checkbox-p)
  14310. (call-interactively 'org-toggle-checkbox))
  14311. ((org-at-item-p)
  14312. (if arg
  14313. (call-interactively 'org-toggle-checkbox)
  14314. (call-interactively 'org-maybe-renumber-ordered-list)))
  14315. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14316. ;; Dynamic block
  14317. (beginning-of-line 1)
  14318. (save-excursion (org-update-dblock)))
  14319. ((save-excursion
  14320. (beginning-of-line 1)
  14321. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14322. (cond
  14323. ((equal (match-string 1) "TBLFM")
  14324. ;; Recalculate the table before this line
  14325. (save-excursion
  14326. (beginning-of-line 1)
  14327. (skip-chars-backward " \r\n\t")
  14328. (if (org-at-table-p)
  14329. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14330. (t
  14331. ; (org-set-regexps-and-options)
  14332. ; (org-restart-font-lock)
  14333. (let ((org-inhibit-startup t)) (org-mode-restart))
  14334. (message "Local setup has been refreshed"))))
  14335. ((org-clock-update-time-maybe))
  14336. (t (error "C-c C-c can do nothing useful at this location")))))
  14337. (defun org-mode-restart ()
  14338. "Restart Org-mode, to scan again for special lines.
  14339. Also updates the keyword regular expressions."
  14340. (interactive)
  14341. (org-mode)
  14342. (message "Org-mode restarted"))
  14343. (defun org-kill-note-or-show-branches ()
  14344. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14345. (interactive)
  14346. (if (not org-finish-function)
  14347. (call-interactively 'show-branches)
  14348. (let ((org-note-abort t))
  14349. (funcall org-finish-function))))
  14350. (defun org-return (&optional indent)
  14351. "Goto next table row or insert a newline.
  14352. Calls `org-table-next-row' or `newline', depending on context.
  14353. See the individual commands for more information."
  14354. (interactive)
  14355. (cond
  14356. ((bobp) (if indent (newline-and-indent) (newline)))
  14357. ((org-at-table-p)
  14358. (org-table-justify-field-maybe)
  14359. (call-interactively 'org-table-next-row))
  14360. ((and org-return-follows-link
  14361. (eq (get-text-property (point) 'face) 'org-link))
  14362. (call-interactively 'org-open-at-point))
  14363. ((and (org-at-heading-p)
  14364. (looking-at
  14365. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14366. (org-show-entry)
  14367. (end-of-line 1)
  14368. (newline))
  14369. (t (if indent (newline-and-indent) (newline)))))
  14370. (defun org-return-indent ()
  14371. "Goto next table row or insert a newline and indent.
  14372. Calls `org-table-next-row' or `newline-and-indent', depending on
  14373. context. See the individual commands for more information."
  14374. (interactive)
  14375. (org-return t))
  14376. (defun org-ctrl-c-star ()
  14377. "Compute table, or change heading status of lines.
  14378. Calls `org-table-recalculate' or `org-toggle-heading',
  14379. depending on context."
  14380. (interactive)
  14381. (cond
  14382. ((org-at-table-p)
  14383. (call-interactively 'org-table-recalculate))
  14384. (t
  14385. ;; Convert all lines in region to list items
  14386. (call-interactively 'org-toggle-heading))))
  14387. (defun org-ctrl-c-minus ()
  14388. "Insert separator line in table or modify bullet status of line.
  14389. Also turns a plain line or a region of lines into list items.
  14390. Calls `org-table-insert-hline', `org-toggle-item', or
  14391. `org-cycle-list-bullet', depending on context."
  14392. (interactive)
  14393. (cond
  14394. ((org-at-table-p)
  14395. (call-interactively 'org-table-insert-hline))
  14396. ((org-region-active-p)
  14397. (call-interactively 'org-toggle-item))
  14398. ((org-in-item-p)
  14399. (call-interactively 'org-cycle-list-bullet))
  14400. (t
  14401. (call-interactively 'org-toggle-item))))
  14402. (defun org-toggle-item ()
  14403. "Convert headings or normal lines to items, items to normal lines.
  14404. If there is no active region, only the current line is considered.
  14405. If the first line in the region is a headline, convert all headlines to items.
  14406. If the first line in the region is an item, convert all items to normal lines.
  14407. If the first line is normal text, add an item bullet to each line."
  14408. (interactive)
  14409. (let (l2 l beg end)
  14410. (if (org-region-active-p)
  14411. (setq beg (region-beginning) end (region-end))
  14412. (setq beg (point-at-bol)
  14413. end (min (1+ (point-at-eol)) (point-max))))
  14414. (save-excursion
  14415. (goto-char end)
  14416. (setq l2 (org-current-line))
  14417. (goto-char beg)
  14418. (beginning-of-line 1)
  14419. (setq l (1- (org-current-line)))
  14420. (if (org-at-item-p)
  14421. ;; We already have items, de-itemize
  14422. (while (< (setq l (1+ l)) l2)
  14423. (when (org-at-item-p)
  14424. (goto-char (match-beginning 2))
  14425. (delete-region (match-beginning 2) (match-end 2))
  14426. (and (looking-at "[ \t]+") (replace-match "")))
  14427. (beginning-of-line 2))
  14428. (if (org-on-heading-p)
  14429. ;; Headings, convert to items
  14430. (while (< (setq l (1+ l)) l2)
  14431. (if (looking-at org-outline-regexp)
  14432. (replace-match "- " t t))
  14433. (beginning-of-line 2))
  14434. ;; normal lines, turn them into items
  14435. (while (< (setq l (1+ l)) l2)
  14436. (unless (org-at-item-p)
  14437. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14438. (replace-match "\\1- \\2")))
  14439. (beginning-of-line 2)))))))
  14440. (defun org-toggle-heading (&optional nstars)
  14441. "Convert headings to normal text, or items or text to headings.
  14442. If there is no active region, only the current line is considered.
  14443. If the first line is a heading, remove the stars from all headlines
  14444. in the region.
  14445. If the first line is a plain list item, turn all plain list items
  14446. into headings.
  14447. If the first line is a normal line, turn each and every line in the
  14448. region into a heading.
  14449. When converting a line into a heading, the number of stars is chosen
  14450. such that the lines become children of the current entry. However,
  14451. when a prefix argument is given, its value determines the number of
  14452. stars to add."
  14453. (interactive "P")
  14454. (let (l2 l itemp beg end)
  14455. (if (org-region-active-p)
  14456. (setq beg (region-beginning) end (region-end))
  14457. (setq beg (point-at-bol)
  14458. end (min (1+ (point-at-eol)) (point-max))))
  14459. (save-excursion
  14460. (goto-char end)
  14461. (setq l2 (org-current-line))
  14462. (goto-char beg)
  14463. (beginning-of-line 1)
  14464. (setq l (1- (org-current-line)))
  14465. (if (org-on-heading-p)
  14466. ;; We already have headlines, de-star them
  14467. (while (< (setq l (1+ l)) l2)
  14468. (when (org-on-heading-p t)
  14469. (and (looking-at outline-regexp) (replace-match "")))
  14470. (beginning-of-line 2))
  14471. (setq itemp (org-at-item-p))
  14472. (let* ((stars
  14473. (if nstars
  14474. (make-string (prefix-numeric-value current-prefix-arg)
  14475. ?*)
  14476. (save-excursion
  14477. (if (re-search-backward org-complex-heading-regexp nil t)
  14478. (match-string 1) ""))))
  14479. (add-stars (cond (nstars "")
  14480. ((equal stars "") "*")
  14481. (org-odd-levels-only "**")
  14482. (t "*")))
  14483. (rpl (concat stars add-stars " ")))
  14484. (while (< (setq l (1+ l)) l2)
  14485. (if itemp
  14486. (and (org-at-item-p) (replace-match rpl t t))
  14487. (unless (org-on-heading-p)
  14488. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14489. (replace-match (concat rpl (match-string 2))))))
  14490. (beginning-of-line 2)))))))
  14491. (defun org-meta-return (&optional arg)
  14492. "Insert a new heading or wrap a region in a table.
  14493. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14494. See the individual commands for more information."
  14495. (interactive "P")
  14496. (cond
  14497. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14498. ((org-at-table-p)
  14499. (call-interactively 'org-table-wrap-region))
  14500. (t (call-interactively 'org-insert-heading))))
  14501. ;;; Menu entries
  14502. ;; Define the Org-mode menus
  14503. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14504. '("Tbl"
  14505. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14506. ["Next Field" org-cycle (org-at-table-p)]
  14507. ["Previous Field" org-shifttab (org-at-table-p)]
  14508. ["Next Row" org-return (org-at-table-p)]
  14509. "--"
  14510. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14511. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14512. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14513. "--"
  14514. ("Column"
  14515. ["Move Column Left" org-metaleft (org-at-table-p)]
  14516. ["Move Column Right" org-metaright (org-at-table-p)]
  14517. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14518. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14519. ("Row"
  14520. ["Move Row Up" org-metaup (org-at-table-p)]
  14521. ["Move Row Down" org-metadown (org-at-table-p)]
  14522. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14523. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14524. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14525. "--"
  14526. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14527. ("Rectangle"
  14528. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14529. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14530. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14531. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14532. "--"
  14533. ("Calculate"
  14534. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14535. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14536. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14537. "--"
  14538. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14539. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14540. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14541. "--"
  14542. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14543. "--"
  14544. ["Sum Column/Rectangle" org-table-sum
  14545. (or (org-at-table-p) (org-region-active-p))]
  14546. ["Which Column?" org-table-current-column (org-at-table-p)])
  14547. ["Debug Formulas"
  14548. org-table-toggle-formula-debugger
  14549. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14550. ["Show Col/Row Numbers"
  14551. org-table-toggle-coordinate-overlays
  14552. :style toggle
  14553. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14554. "--"
  14555. ["Create" org-table-create (and (not (org-at-table-p))
  14556. org-enable-table-editor)]
  14557. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14558. ["Import from File" org-table-import (not (org-at-table-p))]
  14559. ["Export to File" org-table-export (org-at-table-p)]
  14560. "--"
  14561. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14562. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14563. '("Org"
  14564. ("Show/Hide"
  14565. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14566. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14567. ["Sparse Tree..." org-sparse-tree t]
  14568. ["Reveal Context" org-reveal t]
  14569. ["Show All" show-all t]
  14570. "--"
  14571. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14572. "--"
  14573. ["New Heading" org-insert-heading t]
  14574. ("Navigate Headings"
  14575. ["Up" outline-up-heading t]
  14576. ["Next" outline-next-visible-heading t]
  14577. ["Previous" outline-previous-visible-heading t]
  14578. ["Next Same Level" outline-forward-same-level t]
  14579. ["Previous Same Level" outline-backward-same-level t]
  14580. "--"
  14581. ["Jump" org-goto t])
  14582. ("Edit Structure"
  14583. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14584. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14585. "--"
  14586. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14587. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14588. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14589. "--"
  14590. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14591. "--"
  14592. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14593. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14594. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14595. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14596. "--"
  14597. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14598. "--"
  14599. ["Convert to odd levels" org-convert-to-odd-levels t]
  14600. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14601. ("Editing"
  14602. ["Emphasis..." org-emphasize t]
  14603. ["Edit Source Example" org-edit-special t]
  14604. "--"
  14605. ["Footnote new/jump" org-footnote-action t]
  14606. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14607. ("Archive"
  14608. ["Archive (default method)" org-archive-subtree-default t]
  14609. "--"
  14610. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14611. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14612. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14613. )
  14614. "--"
  14615. ("Hyperlinks"
  14616. ["Store Link (Global)" org-store-link t]
  14617. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14618. ["Insert Link" org-insert-link t]
  14619. ["Follow Link" org-open-at-point t]
  14620. "--"
  14621. ["Next link" org-next-link t]
  14622. ["Previous link" org-previous-link t]
  14623. "--"
  14624. ["Descriptive Links"
  14625. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14626. :style radio
  14627. :selected (member '(org-link) buffer-invisibility-spec)]
  14628. ["Literal Links"
  14629. (progn
  14630. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14631. :style radio
  14632. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14633. "--"
  14634. ("TODO Lists"
  14635. ["TODO/DONE/-" org-todo t]
  14636. ("Select keyword"
  14637. ["Next keyword" org-shiftright (org-on-heading-p)]
  14638. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14639. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14640. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14641. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14642. ["Show TODO Tree" org-show-todo-tree t]
  14643. ["Global TODO list" org-todo-list t]
  14644. "--"
  14645. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14646. :selected org-enforce-todo-dependencies :style toggle :active t]
  14647. "Settings for tree at point"
  14648. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14649. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14650. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14651. ["Do Children parallel" org-toggle-ordered-property :style radio
  14652. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14653. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14654. "--"
  14655. ["Set Priority" org-priority t]
  14656. ["Priority Up" org-shiftup t]
  14657. ["Priority Down" org-shiftdown t]
  14658. "--"
  14659. ["Get news from all feeds" org-feed-update-all t]
  14660. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14661. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14662. ("TAGS and Properties"
  14663. ["Set Tags" org-set-tags-command t]
  14664. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14665. "--"
  14666. ["Set property" org-set-property t]
  14667. ["Column view of properties" org-columns t]
  14668. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14669. ("Dates and Scheduling"
  14670. ["Timestamp" org-time-stamp t]
  14671. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14672. ("Change Date"
  14673. ["1 Day Later" org-shiftright t]
  14674. ["1 Day Earlier" org-shiftleft t]
  14675. ["1 ... Later" org-shiftup t]
  14676. ["1 ... Earlier" org-shiftdown t])
  14677. ["Compute Time Range" org-evaluate-time-range t]
  14678. ["Schedule Item" org-schedule t]
  14679. ["Deadline" org-deadline t]
  14680. "--"
  14681. ["Custom time format" org-toggle-time-stamp-overlays
  14682. :style radio :selected org-display-custom-times]
  14683. "--"
  14684. ["Goto Calendar" org-goto-calendar t]
  14685. ["Date from Calendar" org-date-from-calendar t]
  14686. "--"
  14687. ["Start/Restart Timer" org-timer-start t]
  14688. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14689. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14690. ["Insert Timer String" org-timer t]
  14691. ["Insert Timer Item" org-timer-item t])
  14692. ("Logging work"
  14693. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14694. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14695. ["Clock out" org-clock-out t]
  14696. ["Clock cancel" org-clock-cancel t]
  14697. "--"
  14698. ["Mark as default task" org-clock-mark-default-task t]
  14699. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14700. ["Goto running clock" org-clock-goto t]
  14701. "--"
  14702. ["Display times" org-clock-display t]
  14703. ["Create clock table" org-clock-report t]
  14704. "--"
  14705. ["Record DONE time"
  14706. (progn (setq org-log-done (not org-log-done))
  14707. (message "Switching to %s will %s record a timestamp"
  14708. (car org-done-keywords)
  14709. (if org-log-done "automatically" "not")))
  14710. :style toggle :selected org-log-done])
  14711. "--"
  14712. ["Agenda Command..." org-agenda t]
  14713. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14714. ("File List for Agenda")
  14715. ("Special views current file"
  14716. ["TODO Tree" org-show-todo-tree t]
  14717. ["Check Deadlines" org-check-deadlines t]
  14718. ["Timeline" org-timeline t]
  14719. ["Tags/Property tree" org-match-sparse-tree t])
  14720. "--"
  14721. ["Export/Publish..." org-export t]
  14722. ("LaTeX"
  14723. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14724. :selected org-cdlatex-mode]
  14725. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14726. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14727. ["Modify math symbol" org-cdlatex-math-modify
  14728. (org-inside-LaTeX-fragment-p)]
  14729. ["Insert citation" org-reftex-citation t]
  14730. "--"
  14731. ["Export LaTeX fragments as images"
  14732. (if (featurep 'org-exp)
  14733. (setq org-export-with-LaTeX-fragments
  14734. (not org-export-with-LaTeX-fragments))
  14735. (require 'org-exp))
  14736. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14737. org-export-with-LaTeX-fragments)])
  14738. "--"
  14739. ("MobileOrg"
  14740. ["Push Files and Views" org-mobile-push t]
  14741. ["Get Captured and Flagged" org-mobile-pull t]
  14742. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  14743. "--"
  14744. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  14745. "--"
  14746. ("Documentation"
  14747. ["Show Version" org-version t]
  14748. ["Info Documentation" org-info t])
  14749. ("Customize"
  14750. ["Browse Org Group" org-customize t]
  14751. "--"
  14752. ["Expand This Menu" org-create-customize-menu
  14753. (fboundp 'customize-menu-create)])
  14754. ["Send bug report" org-submit-bug-report t]
  14755. "--"
  14756. ("Refresh/Reload"
  14757. ["Refresh setup current buffer" org-mode-restart t]
  14758. ["Reload Org (after update)" org-reload t]
  14759. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14760. ))
  14761. (defun org-info (&optional node)
  14762. "Read documentation for Org-mode in the info system.
  14763. With optional NODE, go directly to that node."
  14764. (interactive)
  14765. (info (format "(org)%s" (or node ""))))
  14766. ;;;###autoload
  14767. (defun org-submit-bug-report ()
  14768. "Submit a bug report on Org-mode via mail.
  14769. Don't hesitate to report any problems or inaccurate documentation.
  14770. If you don't have setup sending mail from (X)Emacs, please copy the
  14771. output buffer into your mail program, as it gives us important
  14772. information about your Org-mode version and configuration."
  14773. (interactive)
  14774. (require 'reporter)
  14775. (org-load-modules-maybe)
  14776. (org-require-autoloaded-modules)
  14777. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14778. (reporter-submit-bug-report
  14779. "emacs-orgmode@gnu.org"
  14780. (org-version)
  14781. (let (list)
  14782. (save-window-excursion
  14783. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14784. (delete-other-windows)
  14785. (erase-buffer)
  14786. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14787. We would like to add your full Org-mode and Outline configuration to the
  14788. bug report. This greatly simplifies the work of the maintainer and
  14789. other experts on the mailing list.
  14790. HOWEVER, some variables you have customized may contain private
  14791. information. The names of customers, colleagues, or friends, might
  14792. appear in the form of file names, tags, todo states, or search strings.
  14793. If you answer yes to the prompt, you might want to check and remove
  14794. such private information before sending the email.")
  14795. (add-text-properties (point-min) (point-max) '(face org-warning))
  14796. (when (yes-or-no-p "Include your Org-mode configuration ")
  14797. (mapatoms
  14798. (lambda (v)
  14799. (and (boundp v)
  14800. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14801. (or (and (symbol-value v)
  14802. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14803. (and
  14804. (get v 'custom-type) (get v 'standard-value)
  14805. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14806. (push v list)))))
  14807. (kill-buffer (get-buffer "*Warn about privacy*"))
  14808. list))
  14809. nil nil
  14810. "Remember to cover the basics, that is, what you expected to happen and
  14811. what in fact did happen. You don't know how to make a good report? See
  14812. http://orgmode.org/manual/Feedback.html#Feedback
  14813. Your bug report will be posted to the Org-mode mailing list.
  14814. ------------------------------------------------------------------------")
  14815. (save-excursion
  14816. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  14817. (replace-match "\\1Bug: \\3 [\\2]")))))
  14818. (defun org-install-agenda-files-menu ()
  14819. (let ((bl (buffer-list)))
  14820. (save-excursion
  14821. (while bl
  14822. (set-buffer (pop bl))
  14823. (if (org-mode-p) (setq bl nil)))
  14824. (when (org-mode-p)
  14825. (easy-menu-change
  14826. '("Org") "File List for Agenda"
  14827. (append
  14828. (list
  14829. ["Edit File List" (org-edit-agenda-file-list) t]
  14830. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14831. ["Remove Current File from List" org-remove-file t]
  14832. ["Cycle through agenda files" org-cycle-agenda-files t]
  14833. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14834. "--")
  14835. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14836. ;;;; Documentation
  14837. ;;;###autoload
  14838. (defun org-require-autoloaded-modules ()
  14839. (interactive)
  14840. (mapc 'require
  14841. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14842. org-docbook org-exp org-html org-icalendar
  14843. org-id org-latex
  14844. org-publish org-remember org-table
  14845. org-timer org-xoxo)))
  14846. ;;;###autoload
  14847. (defun org-reload (&optional uncompiled)
  14848. "Reload all org lisp files.
  14849. With prefix arg UNCOMPILED, load the uncompiled versions."
  14850. (interactive "P")
  14851. (require 'find-func)
  14852. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14853. (dir-org (file-name-directory (org-find-library-name "org")))
  14854. (dir-org-contrib (ignore-errors
  14855. (file-name-directory
  14856. (org-find-library-name "org-contribdir"))))
  14857. (files
  14858. (append (directory-files dir-org t file-re)
  14859. (and dir-org-contrib
  14860. (directory-files dir-org-contrib t file-re))))
  14861. (remove-re (concat (if (featurep 'xemacs)
  14862. "org-colview" "org-colview-xemacs")
  14863. "\\'")))
  14864. (setq files (mapcar 'file-name-sans-extension files))
  14865. (setq files (mapcar
  14866. (lambda (x) (if (string-match remove-re x) nil x))
  14867. files))
  14868. (setq files (delq nil files))
  14869. (mapc
  14870. (lambda (f)
  14871. (when (featurep (intern (file-name-nondirectory f)))
  14872. (if (and (not uncompiled)
  14873. (file-exists-p (concat f ".elc")))
  14874. (load (concat f ".elc") nil nil t)
  14875. (load (concat f ".el") nil nil t))))
  14876. files))
  14877. (org-version))
  14878. ;;;###autoload
  14879. (defun org-customize ()
  14880. "Call the customize function with org as argument."
  14881. (interactive)
  14882. (org-load-modules-maybe)
  14883. (org-require-autoloaded-modules)
  14884. (customize-browse 'org))
  14885. (defun org-create-customize-menu ()
  14886. "Create a full customization menu for Org-mode, insert it into the menu."
  14887. (interactive)
  14888. (org-load-modules-maybe)
  14889. (org-require-autoloaded-modules)
  14890. (if (fboundp 'customize-menu-create)
  14891. (progn
  14892. (easy-menu-change
  14893. '("Org") "Customize"
  14894. `(["Browse Org group" org-customize t]
  14895. "--"
  14896. ,(customize-menu-create 'org)
  14897. ["Set" Custom-set t]
  14898. ["Save" Custom-save t]
  14899. ["Reset to Current" Custom-reset-current t]
  14900. ["Reset to Saved" Custom-reset-saved t]
  14901. ["Reset to Standard Settings" Custom-reset-standard t]))
  14902. (message "\"Org\"-menu now contains full customization menu"))
  14903. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14904. ;;;; Miscellaneous stuff
  14905. ;;; Generally useful functions
  14906. (defun org-get-at-bol (property)
  14907. "Get text property PROPERTY at beginning of line."
  14908. (get-text-property (point-at-bol) property))
  14909. (defun org-find-text-property-in-string (prop s)
  14910. "Return the first non-nil value of property PROP in string S."
  14911. (or (get-text-property 0 prop s)
  14912. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14913. prop s)))
  14914. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14915. "Display the given MESSAGE as a warning."
  14916. (if (fboundp 'display-warning)
  14917. (display-warning 'org message
  14918. (if (featurep 'xemacs)
  14919. 'warning
  14920. :warning))
  14921. (let ((buf (get-buffer-create "*Org warnings*")))
  14922. (with-current-buffer buf
  14923. (goto-char (point-max))
  14924. (insert "Warning (Org): " message)
  14925. (unless (bolp)
  14926. (newline)))
  14927. (display-buffer buf)
  14928. (sit-for 0))))
  14929. (defun org-in-commented-line ()
  14930. "Is point in a line starting with `#'?"
  14931. (equal (char-after (point-at-bol)) ?#))
  14932. (defun org-in-verbatim-emphasis ()
  14933. (save-match-data
  14934. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  14935. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14936. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14937. (if (and marker (marker-buffer marker)
  14938. (buffer-live-p (marker-buffer marker)))
  14939. (progn
  14940. (switch-to-buffer (marker-buffer marker))
  14941. (if (or (> marker (point-max)) (< marker (point-min)))
  14942. (widen))
  14943. (goto-char marker)
  14944. (org-show-context 'org-goto))
  14945. (if bookmark
  14946. (bookmark-jump bookmark)
  14947. (error "Cannot find location"))))
  14948. (defun org-quote-csv-field (s)
  14949. "Quote field for inclusion in CSV material."
  14950. (if (string-match "[\",]" s)
  14951. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14952. s))
  14953. (defun org-plist-delete (plist property)
  14954. "Delete PROPERTY from PLIST.
  14955. This is in contrast to merely setting it to 0."
  14956. (let (p)
  14957. (while plist
  14958. (if (not (eq property (car plist)))
  14959. (setq p (plist-put p (car plist) (nth 1 plist))))
  14960. (setq plist (cddr plist)))
  14961. p))
  14962. (defun org-force-self-insert (N)
  14963. "Needed to enforce self-insert under remapping."
  14964. (interactive "p")
  14965. (self-insert-command N))
  14966. (defun org-string-width (s)
  14967. "Compute width of string, ignoring invisible characters.
  14968. This ignores character with invisibility property `org-link', and also
  14969. characters with property `org-cwidth', because these will become invisible
  14970. upon the next fontification round."
  14971. (let (b l)
  14972. (when (or (eq t buffer-invisibility-spec)
  14973. (assq 'org-link buffer-invisibility-spec))
  14974. (while (setq b (text-property-any 0 (length s)
  14975. 'invisible 'org-link s))
  14976. (setq s (concat (substring s 0 b)
  14977. (substring s (or (next-single-property-change
  14978. b 'invisible s) (length s)))))))
  14979. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14980. (setq s (concat (substring s 0 b)
  14981. (substring s (or (next-single-property-change
  14982. b 'org-cwidth s) (length s))))))
  14983. (setq l (string-width s) b -1)
  14984. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14985. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14986. l))
  14987. (defun org-get-indentation (&optional line)
  14988. "Get the indentation of the current line, interpreting tabs.
  14989. When LINE is given, assume it represents a line and compute its indentation."
  14990. (if line
  14991. (if (string-match "^ *" (org-remove-tabs line))
  14992. (match-end 0))
  14993. (save-excursion
  14994. (beginning-of-line 1)
  14995. (skip-chars-forward " \t")
  14996. (current-column))))
  14997. (defun org-remove-tabs (s &optional width)
  14998. "Replace tabulators in S with spaces.
  14999. Assumes that s is a single line, starting in column 0."
  15000. (setq width (or width tab-width))
  15001. (while (string-match "\t" s)
  15002. (setq s (replace-match
  15003. (make-string
  15004. (- (* width (/ (+ (match-beginning 0) width) width))
  15005. (match-beginning 0)) ?\ )
  15006. t t s)))
  15007. s)
  15008. (defun org-fix-indentation (line ind)
  15009. "Fix indentation in LINE.
  15010. IND is a cons cell with target and minimum indentation.
  15011. If the current indentation in LINE is smaller than the minimum,
  15012. leave it alone. If it is larger than ind, set it to the target."
  15013. (let* ((l (org-remove-tabs line))
  15014. (i (org-get-indentation l))
  15015. (i1 (car ind)) (i2 (cdr ind)))
  15016. (if (>= i i2) (setq l (substring line i2)))
  15017. (if (> i1 0)
  15018. (concat (make-string i1 ?\ ) l)
  15019. l)))
  15020. (defun org-remove-indentation (code &optional n)
  15021. "Remove the maximum common indentation from the lines in CODE.
  15022. N may optionally be the number of spaces to remove."
  15023. (with-temp-buffer
  15024. (insert code)
  15025. (org-do-remove-indentation n)
  15026. (buffer-string)))
  15027. (defun org-do-remove-indentation (&optional n)
  15028. "Remove the maximum common indentation from the buffer."
  15029. (untabify (point-min) (point-max))
  15030. (let ((min 10000) re)
  15031. (if n
  15032. (setq min n)
  15033. (goto-char (point-min))
  15034. (while (re-search-forward "^ *[^ \n]" nil t)
  15035. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15036. (unless (or (= min 0) (= min 10000))
  15037. (setq re (format "^ \\{%d\\}" min))
  15038. (goto-char (point-min))
  15039. (while (re-search-forward re nil t)
  15040. (replace-match "")
  15041. (end-of-line 1))
  15042. min)))
  15043. (defun org-fill-template (template alist)
  15044. "Find each %key of ALIST in TEMPLATE and replace it."
  15045. (let (entry key value)
  15046. (setq alist (sort (copy-sequence alist)
  15047. (lambda (a b) (< (length (car a)) (length (car b))))))
  15048. (while (setq entry (pop alist))
  15049. (setq template
  15050. (replace-regexp-in-string
  15051. (concat "%" (regexp-quote (car entry)))
  15052. (cdr entry) template t t)))
  15053. template))
  15054. (defun org-base-buffer (buffer)
  15055. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15056. (if (not buffer)
  15057. buffer
  15058. (or (buffer-base-buffer buffer)
  15059. buffer)))
  15060. (defun org-trim (s)
  15061. "Remove whitespace at beginning and end of string."
  15062. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15063. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15064. s)
  15065. (defun org-wrap (string &optional width lines)
  15066. "Wrap string to either a number of lines, or a width in characters.
  15067. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15068. that costs. If there is a word longer than WIDTH, the text is actually
  15069. wrapped to the length of that word.
  15070. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15071. many lines, whatever width that takes.
  15072. The return value is a list of lines, without newlines at the end."
  15073. (let* ((words (org-split-string string "[ \t\n]+"))
  15074. (maxword (apply 'max (mapcar 'org-string-width words)))
  15075. w ll)
  15076. (cond (width
  15077. (org-do-wrap words (max maxword width)))
  15078. (lines
  15079. (setq w maxword)
  15080. (setq ll (org-do-wrap words maxword))
  15081. (if (<= (length ll) lines)
  15082. ll
  15083. (setq ll words)
  15084. (while (> (length ll) lines)
  15085. (setq w (1+ w))
  15086. (setq ll (org-do-wrap words w)))
  15087. ll))
  15088. (t (error "Cannot wrap this")))))
  15089. (defun org-do-wrap (words width)
  15090. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15091. (let (lines line)
  15092. (while words
  15093. (setq line (pop words))
  15094. (while (and words (< (+ (length line) (length (car words))) width))
  15095. (setq line (concat line " " (pop words))))
  15096. (setq lines (push line lines)))
  15097. (nreverse lines)))
  15098. (defun org-split-string (string &optional separators)
  15099. "Splits STRING into substrings at SEPARATORS.
  15100. No empty strings are returned if there are matches at the beginning
  15101. and end of string."
  15102. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15103. (start 0)
  15104. notfirst
  15105. (list nil))
  15106. (while (and (string-match rexp string
  15107. (if (and notfirst
  15108. (= start (match-beginning 0))
  15109. (< start (length string)))
  15110. (1+ start) start))
  15111. (< (match-beginning 0) (length string)))
  15112. (setq notfirst t)
  15113. (or (eq (match-beginning 0) 0)
  15114. (and (eq (match-beginning 0) (match-end 0))
  15115. (eq (match-beginning 0) start))
  15116. (setq list
  15117. (cons (substring string start (match-beginning 0))
  15118. list)))
  15119. (setq start (match-end 0)))
  15120. (or (eq start (length string))
  15121. (setq list
  15122. (cons (substring string start)
  15123. list)))
  15124. (nreverse list)))
  15125. (defun org-quote-vert (s)
  15126. "Replace \"|\" with \"\\vert\"."
  15127. (while (string-match "|" s)
  15128. (setq s (replace-match "\\vert" t t s)))
  15129. s)
  15130. (defun org-uuidgen-p (s)
  15131. "Is S an ID created by UUIDGEN?"
  15132. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15133. (defun org-context ()
  15134. "Return a list of contexts of the current cursor position.
  15135. If several contexts apply, all are returned.
  15136. Each context entry is a list with a symbol naming the context, and
  15137. two positions indicating start and end of the context. Possible
  15138. contexts are:
  15139. :headline anywhere in a headline
  15140. :headline-stars on the leading stars in a headline
  15141. :todo-keyword on a TODO keyword (including DONE) in a headline
  15142. :tags on the TAGS in a headline
  15143. :priority on the priority cookie in a headline
  15144. :item on the first line of a plain list item
  15145. :item-bullet on the bullet/number of a plain list item
  15146. :checkbox on the checkbox in a plain list item
  15147. :table in an org-mode table
  15148. :table-special on a special filed in a table
  15149. :table-table in a table.el table
  15150. :link on a hyperlink
  15151. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15152. :target on a <<target>>
  15153. :radio-target on a <<<radio-target>>>
  15154. :latex-fragment on a LaTeX fragment
  15155. :latex-preview on a LaTeX fragment with overlayed preview image
  15156. This function expects the position to be visible because it uses font-lock
  15157. faces as a help to recognize the following contexts: :table-special, :link,
  15158. and :keyword."
  15159. (let* ((f (get-text-property (point) 'face))
  15160. (faces (if (listp f) f (list f)))
  15161. (p (point)) clist o)
  15162. ;; First the large context
  15163. (cond
  15164. ((org-on-heading-p t)
  15165. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15166. (when (progn
  15167. (beginning-of-line 1)
  15168. (looking-at org-todo-line-tags-regexp))
  15169. (push (org-point-in-group p 1 :headline-stars) clist)
  15170. (push (org-point-in-group p 2 :todo-keyword) clist)
  15171. (push (org-point-in-group p 4 :tags) clist))
  15172. (goto-char p)
  15173. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15174. (if (looking-at "\\[#[A-Z0-9]\\]")
  15175. (push (org-point-in-group p 0 :priority) clist)))
  15176. ((org-at-item-p)
  15177. (push (org-point-in-group p 2 :item-bullet) clist)
  15178. (push (list :item (point-at-bol)
  15179. (save-excursion (org-end-of-item) (point)))
  15180. clist)
  15181. (and (org-at-item-checkbox-p)
  15182. (push (org-point-in-group p 0 :checkbox) clist)))
  15183. ((org-at-table-p)
  15184. (push (list :table (org-table-begin) (org-table-end)) clist)
  15185. (if (memq 'org-formula faces)
  15186. (push (list :table-special
  15187. (previous-single-property-change p 'face)
  15188. (next-single-property-change p 'face)) clist)))
  15189. ((org-at-table-p 'any)
  15190. (push (list :table-table) clist)))
  15191. (goto-char p)
  15192. ;; Now the small context
  15193. (cond
  15194. ((org-at-timestamp-p)
  15195. (push (org-point-in-group p 0 :timestamp) clist))
  15196. ((memq 'org-link faces)
  15197. (push (list :link
  15198. (previous-single-property-change p 'face)
  15199. (next-single-property-change p 'face)) clist))
  15200. ((memq 'org-special-keyword faces)
  15201. (push (list :keyword
  15202. (previous-single-property-change p 'face)
  15203. (next-single-property-change p 'face)) clist))
  15204. ((org-on-target-p)
  15205. (push (org-point-in-group p 0 :target) clist)
  15206. (goto-char (1- (match-beginning 0)))
  15207. (if (looking-at org-radio-target-regexp)
  15208. (push (org-point-in-group p 0 :radio-target) clist))
  15209. (goto-char p))
  15210. ((setq o (car (delq nil
  15211. (mapcar
  15212. (lambda (x)
  15213. (if (memq x org-latex-fragment-image-overlays) x))
  15214. (org-overlays-at (point))))))
  15215. (push (list :latex-fragment
  15216. (org-overlay-start o) (org-overlay-end o)) clist)
  15217. (push (list :latex-preview
  15218. (org-overlay-start o) (org-overlay-end o)) clist))
  15219. ((org-inside-LaTeX-fragment-p)
  15220. ;; FIXME: positions wrong.
  15221. (push (list :latex-fragment (point) (point)) clist)))
  15222. (setq clist (nreverse (delq nil clist)))
  15223. clist))
  15224. ;; FIXME: Compare with at-regexp-p Do we need both?
  15225. (defun org-in-regexp (re &optional nlines visually)
  15226. "Check if point is inside a match of regexp.
  15227. Normally only the current line is checked, but you can include NLINES extra
  15228. lines both before and after point into the search.
  15229. If VISUALLY is set, require that the cursor is not after the match but
  15230. really on, so that the block visually is on the match."
  15231. (catch 'exit
  15232. (let ((pos (point))
  15233. (eol (point-at-eol (+ 1 (or nlines 0))))
  15234. (inc (if visually 1 0)))
  15235. (save-excursion
  15236. (beginning-of-line (- 1 (or nlines 0)))
  15237. (while (re-search-forward re eol t)
  15238. (if (and (<= (match-beginning 0) pos)
  15239. (>= (+ inc (match-end 0)) pos))
  15240. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15241. (defun org-at-regexp-p (regexp)
  15242. "Is point inside a match of REGEXP in the current line?"
  15243. (catch 'exit
  15244. (save-excursion
  15245. (let ((pos (point)) (end (point-at-eol)))
  15246. (beginning-of-line 1)
  15247. (while (re-search-forward regexp end t)
  15248. (if (and (<= (match-beginning 0) pos)
  15249. (>= (match-end 0) pos))
  15250. (throw 'exit t)))
  15251. nil))))
  15252. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15253. "Call `multi-occur' with buffers for all agenda files."
  15254. (interactive "sOrg-files matching: \np")
  15255. (let* ((files (org-agenda-files))
  15256. (tnames (mapcar 'file-truename files))
  15257. (extra org-agenda-text-search-extra-files)
  15258. f)
  15259. (when (eq (car extra) 'agenda-archives)
  15260. (setq extra (cdr extra))
  15261. (setq files (org-add-archive-files files)))
  15262. (while (setq f (pop extra))
  15263. (unless (member (file-truename f) tnames)
  15264. (add-to-list 'files f 'append)
  15265. (add-to-list 'tnames (file-truename f) 'append)))
  15266. (multi-occur
  15267. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  15268. regexp)))
  15269. (if (boundp 'occur-mode-find-occurrence-hook)
  15270. ;; Emacs 23
  15271. (add-hook 'occur-mode-find-occurrence-hook
  15272. (lambda ()
  15273. (when (org-mode-p)
  15274. (org-reveal))))
  15275. ;; Emacs 22
  15276. (defadvice occur-mode-goto-occurrence
  15277. (after org-occur-reveal activate)
  15278. (and (org-mode-p) (org-reveal)))
  15279. (defadvice occur-mode-goto-occurrence-other-window
  15280. (after org-occur-reveal activate)
  15281. (and (org-mode-p) (org-reveal)))
  15282. (defadvice occur-mode-display-occurrence
  15283. (after org-occur-reveal activate)
  15284. (when (org-mode-p)
  15285. (let ((pos (occur-mode-find-occurrence)))
  15286. (with-current-buffer (marker-buffer pos)
  15287. (save-excursion
  15288. (goto-char pos)
  15289. (org-reveal)))))))
  15290. (defun org-occur-link-in-agenda-files ()
  15291. "Create a link and search for it in the agendas.
  15292. The link is not stored in `org-stored-links', it is just created
  15293. for the search purpose."
  15294. (interactive)
  15295. (let ((link (condition-case nil
  15296. (org-store-link nil)
  15297. (error "Unable to create a link to here"))))
  15298. (org-occur-in-agenda-files (regexp-quote link))))
  15299. (defun org-uniquify (list)
  15300. "Remove duplicate elements from LIST."
  15301. (let (res)
  15302. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15303. res))
  15304. (defun org-delete-all (elts list)
  15305. "Remove all elements in ELTS from LIST."
  15306. (while elts
  15307. (setq list (delete (pop elts) list)))
  15308. list)
  15309. (defun org-back-over-empty-lines ()
  15310. "Move backwards over whitespace, to the beginning of the first empty line.
  15311. Returns the number of empty lines passed."
  15312. (let ((pos (point)))
  15313. (skip-chars-backward " \t\n\r")
  15314. (beginning-of-line 2)
  15315. (goto-char (min (point) pos))
  15316. (count-lines (point) pos)))
  15317. (defun org-skip-whitespace ()
  15318. (skip-chars-forward " \t\n\r"))
  15319. (defun org-point-in-group (point group &optional context)
  15320. "Check if POINT is in match-group GROUP.
  15321. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15322. match. If the match group does ot exist or point is not inside it,
  15323. return nil."
  15324. (and (match-beginning group)
  15325. (>= point (match-beginning group))
  15326. (<= point (match-end group))
  15327. (if context
  15328. (list context (match-beginning group) (match-end group))
  15329. t)))
  15330. (defun org-switch-to-buffer-other-window (&rest args)
  15331. "Switch to buffer in a second window on the current frame.
  15332. In particular, do not allow pop-up frames."
  15333. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15334. special-display-function)
  15335. (apply 'switch-to-buffer-other-window args)))
  15336. (defun org-combine-plists (&rest plists)
  15337. "Create a single property list from all plists in PLISTS.
  15338. The process starts by copying the first list, and then setting properties
  15339. from the other lists. Settings in the last list are the most significant
  15340. ones and overrule settings in the other lists."
  15341. (let ((rtn (copy-sequence (pop plists)))
  15342. p v ls)
  15343. (while plists
  15344. (setq ls (pop plists))
  15345. (while ls
  15346. (setq p (pop ls) v (pop ls))
  15347. (setq rtn (plist-put rtn p v))))
  15348. rtn))
  15349. (defun org-move-line-down (arg)
  15350. "Move the current line down. With prefix argument, move it past ARG lines."
  15351. (interactive "p")
  15352. (let ((col (current-column))
  15353. beg end pos)
  15354. (beginning-of-line 1) (setq beg (point))
  15355. (beginning-of-line 2) (setq end (point))
  15356. (beginning-of-line (+ 1 arg))
  15357. (setq pos (move-marker (make-marker) (point)))
  15358. (insert (delete-and-extract-region beg end))
  15359. (goto-char pos)
  15360. (org-move-to-column col)))
  15361. (defun org-move-line-up (arg)
  15362. "Move the current line up. With prefix argument, move it past ARG lines."
  15363. (interactive "p")
  15364. (let ((col (current-column))
  15365. beg end pos)
  15366. (beginning-of-line 1) (setq beg (point))
  15367. (beginning-of-line 2) (setq end (point))
  15368. (beginning-of-line (- arg))
  15369. (setq pos (move-marker (make-marker) (point)))
  15370. (insert (delete-and-extract-region beg end))
  15371. (goto-char pos)
  15372. (org-move-to-column col)))
  15373. (defun org-replace-escapes (string table)
  15374. "Replace %-escapes in STRING with values in TABLE.
  15375. TABLE is an association list with keys like \"%a\" and string values.
  15376. The sequences in STRING may contain normal field width and padding information,
  15377. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15378. so values can contain further %-escapes if they are define later in TABLE."
  15379. (let ((case-fold-search nil)
  15380. e re rpl)
  15381. (while (setq e (pop table))
  15382. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15383. (while (string-match re string)
  15384. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15385. (cdr e)))
  15386. (setq string (replace-match rpl t t string))))
  15387. string))
  15388. (defun org-sublist (list start end)
  15389. "Return a section of LIST, from START to END.
  15390. Counting starts at 1."
  15391. (let (rtn (c start))
  15392. (setq list (nthcdr (1- start) list))
  15393. (while (and list (<= c end))
  15394. (push (pop list) rtn)
  15395. (setq c (1+ c)))
  15396. (nreverse rtn)))
  15397. (defun org-find-base-buffer-visiting (file)
  15398. "Like `find-buffer-visiting' but always return the base buffer and
  15399. not an indirect buffer."
  15400. (let ((buf (or (get-file-buffer file)
  15401. (find-buffer-visiting file))))
  15402. (if buf
  15403. (or (buffer-base-buffer buf) buf)
  15404. nil)))
  15405. (defun org-image-file-name-regexp (&optional extensions)
  15406. "Return regexp matching the file names of images.
  15407. If EXTENSIONS is given, only match these."
  15408. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15409. (image-file-name-regexp)
  15410. (let ((image-file-name-extensions
  15411. (or extensions
  15412. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15413. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15414. (concat "\\."
  15415. (regexp-opt (nconc (mapcar 'upcase
  15416. image-file-name-extensions)
  15417. image-file-name-extensions)
  15418. t)
  15419. "\\'"))))
  15420. (defun org-file-image-p (file &optional extensions)
  15421. "Return non-nil if FILE is an image."
  15422. (save-match-data
  15423. (string-match (org-image-file-name-regexp extensions) file)))
  15424. (defun org-get-cursor-date ()
  15425. "Return the date at cursor in as a time.
  15426. This works in the calendar and in the agenda, anywhere else it just
  15427. returns the current time."
  15428. (let (date day defd)
  15429. (cond
  15430. ((eq major-mode 'calendar-mode)
  15431. (setq date (calendar-cursor-to-date)
  15432. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15433. ((eq major-mode 'org-agenda-mode)
  15434. (setq day (get-text-property (point) 'day))
  15435. (if day
  15436. (setq date (calendar-gregorian-from-absolute day)
  15437. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15438. (nth 2 date))))))
  15439. (or defd (current-time))))
  15440. (defvar org-agenda-action-marker (make-marker)
  15441. "Marker pointing to the entry for the next agenda action.")
  15442. (defun org-mark-entry-for-agenda-action ()
  15443. "Mark the current entry as target of an agenda action.
  15444. Agenda actions are actions executed from the agenda with the key `k',
  15445. which make use of the date at the cursor."
  15446. (interactive)
  15447. (move-marker org-agenda-action-marker
  15448. (save-excursion (org-back-to-heading t) (point))
  15449. (current-buffer))
  15450. (message
  15451. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15452. ;;; Paragraph filling stuff.
  15453. ;; We want this to be just right, so use the full arsenal.
  15454. (defun org-indent-line-function ()
  15455. "Indent line like previous, but further if previous was headline or item."
  15456. (interactive)
  15457. (let* ((pos (point))
  15458. (itemp (org-at-item-p))
  15459. (case-fold-search t)
  15460. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15461. column bpos bcol tpos tcol bullet btype bullet-type)
  15462. ;; Find the previous relevant line
  15463. (beginning-of-line 1)
  15464. (cond
  15465. ((looking-at "#") (setq column 0))
  15466. ((looking-at "\\*+ ") (setq column 0))
  15467. ((and (looking-at "[ \t]*:END:")
  15468. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15469. (save-excursion
  15470. (goto-char (1- (match-beginning 1)))
  15471. (setq column (current-column))))
  15472. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15473. (save-excursion
  15474. (re-search-backward
  15475. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15476. (setq column (org-get-indentation (match-string 0))))
  15477. (t
  15478. (beginning-of-line 0)
  15479. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15480. (not (looking-at "[ \t]*:END:"))
  15481. (not (looking-at org-drawer-regexp)))
  15482. (beginning-of-line 0))
  15483. (cond
  15484. ((looking-at "\\*+[ \t]+")
  15485. (if (not org-adapt-indentation)
  15486. (setq column 0)
  15487. (goto-char (match-end 0))
  15488. (setq column (current-column))))
  15489. ((looking-at org-drawer-regexp)
  15490. (goto-char (1- (match-beginning 1)))
  15491. (setq column (current-column)))
  15492. ((looking-at "\\([ \t]*\\):END:")
  15493. (goto-char (match-end 1))
  15494. (setq column (current-column)))
  15495. ((org-in-item-p)
  15496. (org-beginning-of-item)
  15497. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15498. (setq bpos (match-beginning 1) tpos (match-end 0)
  15499. bcol (progn (goto-char bpos) (current-column))
  15500. tcol (progn (goto-char tpos) (current-column))
  15501. bullet (match-string 1)
  15502. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15503. (if (> tcol (+ bcol org-description-max-indent))
  15504. (setq tcol (+ bcol 5)))
  15505. (if (not itemp)
  15506. (setq column tcol)
  15507. (goto-char pos)
  15508. (beginning-of-line 1)
  15509. (if (looking-at "\\S-")
  15510. (progn
  15511. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15512. (setq bullet (match-string 1)
  15513. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15514. (setq column (if (equal btype bullet-type) bcol tcol)))
  15515. (setq column (org-get-indentation)))))
  15516. (t (setq column (org-get-indentation))))))
  15517. (goto-char pos)
  15518. (if (<= (current-column) (current-indentation))
  15519. (org-indent-line-to column)
  15520. (save-excursion (org-indent-line-to column)))
  15521. (setq column (current-column))
  15522. (beginning-of-line 1)
  15523. (if (looking-at
  15524. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15525. (replace-match (concat (match-string 1)
  15526. (format org-property-format
  15527. (match-string 2) (match-string 3)))
  15528. t t))
  15529. (org-move-to-column column)))
  15530. (defun org-set-autofill-regexps ()
  15531. (interactive)
  15532. ;; In the paragraph separator we include headlines, because filling
  15533. ;; text in a line directly attached to a headline would otherwise
  15534. ;; fill the headline as well.
  15535. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15536. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15537. ;; The paragraph starter includes hand-formatted lists.
  15538. (org-set-local
  15539. 'paragraph-start
  15540. (concat
  15541. "\f" "\\|"
  15542. "[ ]*$" "\\|"
  15543. "\\*+ " "\\|"
  15544. "[ \t]*#" "\\|"
  15545. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15546. "[ \t]*[:|]" "\\|"
  15547. "\\$\\$" "\\|"
  15548. "\\\\\\(begin\\|end\\|[][]\\)"))
  15549. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15550. ;; But only if the user has not turned off tables or fixed-width regions
  15551. (org-set-local
  15552. 'auto-fill-inhibit-regexp
  15553. (concat "\\*+ \\|#\\+"
  15554. "\\|[ \t]*" org-keyword-time-regexp
  15555. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15556. (concat
  15557. "\\|[ \t]*["
  15558. (if org-enable-table-editor "|" "")
  15559. (if org-enable-fixed-width-editor ":" "")
  15560. "]"))))
  15561. ;; We use our own fill-paragraph function, to make sure that tables
  15562. ;; and fixed-width regions are not wrapped. That function will pass
  15563. ;; through to `fill-paragraph' when appropriate.
  15564. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15565. ; Adaptive filling: To get full control, first make sure that
  15566. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15567. (org-set-local 'adaptive-fill-regexp "\000")
  15568. (org-set-local 'adaptive-fill-function
  15569. 'org-adaptive-fill-function)
  15570. (org-set-local
  15571. 'align-mode-rules-list
  15572. '((org-in-buffer-settings
  15573. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15574. (modes . '(org-mode))))))
  15575. (defun org-fill-paragraph (&optional justify)
  15576. "Re-align a table, pass through to fill-paragraph if no table."
  15577. (let ((table-p (org-at-table-p))
  15578. (table.el-p (org-at-table.el-p)))
  15579. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15580. (save-excursion (goto-char (point-at-bol))
  15581. (looking-at outline-regexp)))
  15582. t) ; skip headlines
  15583. (table.el-p t) ; skip table.el tables
  15584. (table-p (org-table-align) t) ; align org-mode tables
  15585. (t nil)))) ; call paragraph-fill
  15586. ;; For reference, this is the default value of adaptive-fill-regexp
  15587. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15588. (defun org-adaptive-fill-function ()
  15589. "Return a fill prefix for org-mode files.
  15590. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15591. work correctly."
  15592. (cond ((looking-at "#[ \t]+")
  15593. (match-string 0))
  15594. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15595. (save-excursion
  15596. (if (> (match-end 1) (+ (match-beginning 1)
  15597. org-description-max-indent))
  15598. (goto-char (+ (match-beginning 1) 5))
  15599. (goto-char (match-end 0)))
  15600. (make-string (current-column) ?\ )))
  15601. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15602. (save-excursion
  15603. (goto-char (match-end 0))
  15604. (make-string (current-column) ?\ )))
  15605. (t nil)))
  15606. ;;; Other stuff.
  15607. (defun org-toggle-fixed-width-section (arg)
  15608. "Toggle the fixed-width export.
  15609. If there is no active region, the QUOTE keyword at the current headline is
  15610. inserted or removed. When present, it causes the text between this headline
  15611. and the next to be exported as fixed-width text, and unmodified.
  15612. If there is an active region, this command adds or removes a colon as the
  15613. first character of this line. If the first character of a line is a colon,
  15614. this line is also exported in fixed-width font."
  15615. (interactive "P")
  15616. (let* ((cc 0)
  15617. (regionp (org-region-active-p))
  15618. (beg (if regionp (region-beginning) (point)))
  15619. (end (if regionp (region-end)))
  15620. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15621. (case-fold-search nil)
  15622. (re "[ \t]*\\(: \\)")
  15623. off)
  15624. (if regionp
  15625. (save-excursion
  15626. (goto-char beg)
  15627. (setq cc (current-column))
  15628. (beginning-of-line 1)
  15629. (setq off (looking-at re))
  15630. (while (> nlines 0)
  15631. (setq nlines (1- nlines))
  15632. (beginning-of-line 1)
  15633. (cond
  15634. (arg
  15635. (org-move-to-column cc t)
  15636. (insert ": \n")
  15637. (forward-line -1))
  15638. ((and off (looking-at re))
  15639. (replace-match "" t t nil 1))
  15640. ((not off) (org-move-to-column cc t) (insert ": ")))
  15641. (forward-line 1)))
  15642. (save-excursion
  15643. (org-back-to-heading)
  15644. (if (looking-at (concat outline-regexp
  15645. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15646. (replace-match "" t t nil 1)
  15647. (if (looking-at outline-regexp)
  15648. (progn
  15649. (goto-char (match-end 0))
  15650. (insert org-quote-string " "))))))))
  15651. (defun org-reftex-citation ()
  15652. "Use reftex-citation to insert a citation into the buffer.
  15653. This looks for a line like
  15654. #+BIBLIOGRAPHY: foo plain option:-d
  15655. and derives from it that foo.bib is the bibliography file relevant
  15656. for this document. It then installs the necessary environment for RefTeX
  15657. to work in this buffer and calls `reftex-citation' to insert a citation
  15658. into the buffer.
  15659. Export of such citations to both LaTeX and HTML is handled by the contributed
  15660. package org-exp-bibtex by Taru Karttunen."
  15661. (interactive)
  15662. (let ((reftex-docstruct-symbol 'rds)
  15663. (reftex-cite-format "\\cite{%l}")
  15664. rds bib)
  15665. (save-excursion
  15666. (save-restriction
  15667. (widen)
  15668. (let ((case-fold-search t)
  15669. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15670. (if (not (save-excursion
  15671. (or (re-search-forward re nil t)
  15672. (re-search-backward re nil t))))
  15673. (error "No bibliography defined in file")
  15674. (setq bib (concat (match-string 1) ".bib")
  15675. rds (list (list 'bib bib)))))))
  15676. (call-interactively 'reftex-citation)))
  15677. ;;;; Functions extending outline functionality
  15678. (defun org-beginning-of-line (&optional arg)
  15679. "Go to the beginning of the current line. If that is invisible, continue
  15680. to a visible line beginning. This makes the function of C-a more intuitive.
  15681. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15682. first attempt, and only move to after the tags when the cursor is already
  15683. beyond the end of the headline."
  15684. (interactive "P")
  15685. (let ((pos (point))
  15686. (special (if (consp org-special-ctrl-a/e)
  15687. (car org-special-ctrl-a/e)
  15688. org-special-ctrl-a/e))
  15689. refpos)
  15690. (if (org-bound-and-true-p line-move-visual)
  15691. (beginning-of-visual-line 1)
  15692. (beginning-of-line 1))
  15693. (if (and arg (fboundp 'move-beginning-of-line))
  15694. (call-interactively 'move-beginning-of-line)
  15695. (if (bobp)
  15696. nil
  15697. (backward-char 1)
  15698. (if (org-invisible-p)
  15699. (while (and (not (bobp)) (org-invisible-p))
  15700. (backward-char 1)
  15701. (beginning-of-line 1))
  15702. (forward-char 1))))
  15703. (when special
  15704. (cond
  15705. ((and (looking-at org-complex-heading-regexp)
  15706. (= (char-after (match-end 1)) ?\ ))
  15707. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15708. (point-at-eol)))
  15709. (goto-char
  15710. (if (eq special t)
  15711. (cond ((> pos refpos) refpos)
  15712. ((= pos (point)) refpos)
  15713. (t (point)))
  15714. (cond ((> pos (point)) (point))
  15715. ((not (eq last-command this-command)) (point))
  15716. (t refpos)))))
  15717. ((org-at-item-p)
  15718. (goto-char
  15719. (if (eq special t)
  15720. (cond ((> pos (match-end 4)) (match-end 4))
  15721. ((= pos (point)) (match-end 4))
  15722. (t (point)))
  15723. (cond ((> pos (point)) (point))
  15724. ((not (eq last-command this-command)) (point))
  15725. (t (match-end 4))))))))
  15726. (org-no-warnings
  15727. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15728. (defun org-end-of-line (&optional arg)
  15729. "Go to the end of the line.
  15730. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15731. first attempt, and only move to after the tags when the cursor is already
  15732. beyond the end of the headline."
  15733. (interactive "P")
  15734. (let ((special (if (consp org-special-ctrl-a/e)
  15735. (cdr org-special-ctrl-a/e)
  15736. org-special-ctrl-a/e)))
  15737. (if (or (not special)
  15738. (not (org-on-heading-p))
  15739. arg)
  15740. (call-interactively
  15741. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15742. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15743. (t 'end-of-line)))
  15744. (let ((pos (point)))
  15745. (beginning-of-line 1)
  15746. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  15747. (if (eq special t)
  15748. (if (or (< pos (match-beginning 1))
  15749. (= pos (match-end 0)))
  15750. (goto-char (match-beginning 1))
  15751. (goto-char (match-end 0)))
  15752. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15753. (goto-char (match-end 0))
  15754. (goto-char (match-beginning 1))))
  15755. (call-interactively (if (fboundp 'move-end-of-line)
  15756. 'move-end-of-line
  15757. 'end-of-line)))))
  15758. (org-no-warnings
  15759. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15760. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15761. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15762. (define-key org-mode-map [home] 'org-beginning-of-line)
  15763. (define-key org-mode-map [end] 'org-end-of-line)
  15764. (defun org-backward-sentence (&optional arg)
  15765. "Go to beginning of sentence, or beginning of table field.
  15766. This will call `backward-sentence' or `org-table-beginning-of-field',
  15767. depending on context."
  15768. (interactive "P")
  15769. (cond
  15770. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15771. (t (call-interactively 'backward-sentence))))
  15772. (defun org-forward-sentence (&optional arg)
  15773. "Go to end of sentence, or end of table field.
  15774. This will call `forward-sentence' or `org-table-end-of-field',
  15775. depending on context."
  15776. (interactive "P")
  15777. (cond
  15778. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15779. (t (call-interactively 'forward-sentence))))
  15780. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15781. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15782. (defun org-kill-line (&optional arg)
  15783. "Kill line, to tags or end of line."
  15784. (interactive "P")
  15785. (cond
  15786. ((or (not org-special-ctrl-k)
  15787. (bolp)
  15788. (not (org-on-heading-p)))
  15789. (call-interactively 'kill-line))
  15790. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15791. (kill-region (point) (match-beginning 1))
  15792. (org-set-tags nil t))
  15793. (t (kill-region (point) (point-at-eol)))))
  15794. (define-key org-mode-map "\C-k" 'org-kill-line)
  15795. (defun org-yank (&optional arg)
  15796. "Yank. If the kill is a subtree, treat it specially.
  15797. This command will look at the current kill and check if is a single
  15798. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15799. cursor is at the beginning of a line or after the stars of a currently
  15800. empty headline, then the yank is handled specially. How exactly depends
  15801. on the value of the following variables, both set by default.
  15802. org-yank-folded-subtrees
  15803. When set, the subtree(s) will be folded after insertion, but only
  15804. if doing so would now swallow text after the yanked text.
  15805. org-yank-adjusted-subtrees
  15806. When set, the subtree will be promoted or demoted in order to
  15807. fit into the local outline tree structure, which means that the level
  15808. will be adjusted so that it becomes the smaller one of the two
  15809. *visible* surrounding headings.
  15810. Any prefix to this command will cause `yank' to be called directly with
  15811. no special treatment. In particular, a simple `C-u' prefix will just
  15812. plainly yank the text as it is.
  15813. \[1] The test checks if the first non-white line is a heading
  15814. and if there are no other headings with fewer stars."
  15815. (interactive "P")
  15816. (org-yank-generic 'yank arg))
  15817. (defun org-yank-generic (command arg)
  15818. "Perform some yank-like command.
  15819. This function implements the behavior described in the `org-yank'
  15820. documentation. However, it has been generalized to work for any
  15821. interactive command with similar behavior."
  15822. ;; pretend to be command COMMAND
  15823. (setq this-command command)
  15824. (if arg
  15825. (call-interactively command)
  15826. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15827. (and (org-kill-is-subtree-p)
  15828. (or (bolp)
  15829. (and (looking-at "[ \t]*$")
  15830. (string-match
  15831. "\\`\\*+\\'"
  15832. (buffer-substring (point-at-bol) (point)))))))
  15833. swallowp)
  15834. (cond
  15835. ((and subtreep org-yank-folded-subtrees)
  15836. (let ((beg (point))
  15837. end)
  15838. (if (and subtreep org-yank-adjusted-subtrees)
  15839. (org-paste-subtree nil nil 'for-yank)
  15840. (call-interactively command))
  15841. (setq end (point))
  15842. (goto-char beg)
  15843. (when (and (bolp) subtreep
  15844. (not (setq swallowp
  15845. (org-yank-folding-would-swallow-text beg end))))
  15846. (or (looking-at outline-regexp)
  15847. (re-search-forward (concat "^" outline-regexp) end t))
  15848. (while (and (< (point) end) (looking-at outline-regexp))
  15849. (hide-subtree)
  15850. (org-cycle-show-empty-lines 'folded)
  15851. (condition-case nil
  15852. (outline-forward-same-level 1)
  15853. (error (goto-char end)))))
  15854. (when swallowp
  15855. (message
  15856. "Inserted text not folded because that would swallow text"))
  15857. (goto-char end)
  15858. (skip-chars-forward " \t\n\r")
  15859. (beginning-of-line 1)
  15860. (push-mark beg 'nomsg)))
  15861. ((and subtreep org-yank-adjusted-subtrees)
  15862. (let ((beg (point-at-bol)))
  15863. (org-paste-subtree nil nil 'for-yank)
  15864. (push-mark beg 'nomsg)))
  15865. (t
  15866. (call-interactively command))))))
  15867. (defun org-yank-folding-would-swallow-text (beg end)
  15868. "Would hide-subtree at BEG swallow any text after END?"
  15869. (let (level)
  15870. (save-excursion
  15871. (goto-char beg)
  15872. (when (or (looking-at outline-regexp)
  15873. (re-search-forward (concat "^" outline-regexp) end t))
  15874. (setq level (org-outline-level)))
  15875. (goto-char end)
  15876. (skip-chars-forward " \t\r\n\v\f")
  15877. (if (or (eobp)
  15878. (and (bolp) (looking-at org-outline-regexp)
  15879. (<= (org-outline-level) level)))
  15880. nil ; Nothing would be swallowed
  15881. t)))) ; something would swallow
  15882. (define-key org-mode-map "\C-y" 'org-yank)
  15883. (defun org-invisible-p ()
  15884. "Check if point is at a character currently not visible."
  15885. ;; Early versions of noutline don't have `outline-invisible-p'.
  15886. (if (fboundp 'outline-invisible-p)
  15887. (outline-invisible-p)
  15888. (get-char-property (point) 'invisible)))
  15889. (defun org-invisible-p2 ()
  15890. "Check if point is at a character currently not visible."
  15891. (save-excursion
  15892. (if (and (eolp) (not (bobp))) (backward-char 1))
  15893. ;; Early versions of noutline don't have `outline-invisible-p'.
  15894. (if (fboundp 'outline-invisible-p)
  15895. (outline-invisible-p)
  15896. (get-char-property (point) 'invisible))))
  15897. (defun org-back-to-heading (&optional invisible-ok)
  15898. "Call `outline-back-to-heading', but provide a better error message."
  15899. (condition-case nil
  15900. (outline-back-to-heading invisible-ok)
  15901. (error (error "Before first headline at position %d in buffer %s"
  15902. (point) (current-buffer)))))
  15903. (defun org-before-first-heading-p ()
  15904. "Before first heading?"
  15905. (save-excursion
  15906. (null (re-search-backward "^\\*+ " nil t))))
  15907. (defun org-on-heading-p (&optional ignored)
  15908. (outline-on-heading-p t))
  15909. (defun org-at-heading-p (&optional ignored)
  15910. (outline-on-heading-p t))
  15911. (defun org-at-heading-or-item-p ()
  15912. (or (org-on-heading-p) (org-at-item-p)))
  15913. (defun org-on-target-p ()
  15914. (or (org-in-regexp org-radio-target-regexp)
  15915. (org-in-regexp org-target-regexp)))
  15916. (defun org-up-heading-all (arg)
  15917. "Move to the heading line of which the present line is a subheading.
  15918. This function considers both visible and invisible heading lines.
  15919. With argument, move up ARG levels."
  15920. (if (fboundp 'outline-up-heading-all)
  15921. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15922. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15923. (defun org-up-heading-safe ()
  15924. "Move to the heading line of which the present line is a subheading.
  15925. This version will not throw an error. It will return the level of the
  15926. headline found, or nil if no higher level is found.
  15927. Also, this function will be a lot faster than `outline-up-heading',
  15928. because it relies on stars being the outline starters. This can really
  15929. make a significant difference in outlines with very many siblings."
  15930. (let (start-level re)
  15931. (org-back-to-heading t)
  15932. (setq start-level (funcall outline-level))
  15933. (if (equal start-level 1)
  15934. nil
  15935. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15936. (if (re-search-backward re nil t)
  15937. (funcall outline-level)))))
  15938. (defun org-first-sibling-p ()
  15939. "Is this heading the first child of its parents?"
  15940. (interactive)
  15941. (let ((re (concat "^" outline-regexp))
  15942. level l)
  15943. (unless (org-at-heading-p t)
  15944. (error "Not at a heading"))
  15945. (setq level (funcall outline-level))
  15946. (save-excursion
  15947. (if (not (re-search-backward re nil t))
  15948. t
  15949. (setq l (funcall outline-level))
  15950. (< l level)))))
  15951. (defun org-goto-sibling (&optional previous)
  15952. "Goto the next sibling, even if it is invisible.
  15953. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15954. when a sibling was found. When none is found, return nil and don't
  15955. move point."
  15956. (let ((fun (if previous 're-search-backward 're-search-forward))
  15957. (pos (point))
  15958. (re (concat "^" outline-regexp))
  15959. level l)
  15960. (when (condition-case nil (org-back-to-heading t) (error nil))
  15961. (setq level (funcall outline-level))
  15962. (catch 'exit
  15963. (or previous (forward-char 1))
  15964. (while (funcall fun re nil t)
  15965. (setq l (funcall outline-level))
  15966. (when (< l level) (goto-char pos) (throw 'exit nil))
  15967. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15968. (goto-char pos)
  15969. nil))))
  15970. (defun org-show-siblings ()
  15971. "Show all siblings of the current headline."
  15972. (save-excursion
  15973. (while (org-goto-sibling) (org-flag-heading nil)))
  15974. (save-excursion
  15975. (while (org-goto-sibling 'previous)
  15976. (org-flag-heading nil))))
  15977. (defun org-show-hidden-entry ()
  15978. "Show an entry where even the heading is hidden."
  15979. (save-excursion
  15980. (org-show-entry)))
  15981. (defun org-flag-heading (flag &optional entry)
  15982. "Flag the current heading. FLAG non-nil means make invisible.
  15983. When ENTRY is non-nil, show the entire entry."
  15984. (save-excursion
  15985. (org-back-to-heading t)
  15986. ;; Check if we should show the entire entry
  15987. (if entry
  15988. (progn
  15989. (org-show-entry)
  15990. (save-excursion
  15991. (and (outline-next-heading)
  15992. (org-flag-heading nil))))
  15993. (outline-flag-region (max (point-min) (1- (point)))
  15994. (save-excursion (outline-end-of-heading) (point))
  15995. flag))))
  15996. (defun org-get-next-sibling ()
  15997. "Move to next heading of the same level, and return point.
  15998. If there is no such heading, return nil.
  15999. This is like outline-next-sibling, but invisible headings are ok."
  16000. (let ((level (funcall outline-level)))
  16001. (outline-next-heading)
  16002. (while (and (not (eobp)) (> (funcall outline-level) level))
  16003. (outline-next-heading))
  16004. (if (or (eobp) (< (funcall outline-level) level))
  16005. nil
  16006. (point))))
  16007. (defun org-get-last-sibling ()
  16008. "Move to previous heading of the same level, and return point.
  16009. If there is no such heading, return nil."
  16010. (let ((opoint (point))
  16011. (level (funcall outline-level)))
  16012. (outline-previous-heading)
  16013. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16014. (while (and (> (funcall outline-level) level)
  16015. (not (bobp)))
  16016. (outline-previous-heading))
  16017. (if (< (funcall outline-level) level)
  16018. nil
  16019. (point)))))
  16020. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16021. ;; This contains an exact copy of the original function, but it uses
  16022. ;; `org-back-to-heading', to make it work also in invisible
  16023. ;; trees. And is uses an invisible-OK argument.
  16024. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16025. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16026. ;; with many children and grandchildren etc, this can be much faster
  16027. ;; than the outline version.
  16028. (org-back-to-heading invisible-OK)
  16029. (let ((first t)
  16030. (level (funcall outline-level)))
  16031. (if (and (org-mode-p) (< level 1000))
  16032. ;; A true heading (not a plain list item), in Org-mode
  16033. ;; This means we can easily find the end by looking
  16034. ;; only for the right number of stars. Using a regexp to do
  16035. ;; this is so much faster than using a Lisp loop.
  16036. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16037. (forward-char 1)
  16038. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16039. ;; something else, do it the slow way
  16040. (while (and (not (eobp))
  16041. (or first (> (funcall outline-level) level)))
  16042. (setq first nil)
  16043. (outline-next-heading)))
  16044. (unless to-heading
  16045. (if (memq (preceding-char) '(?\n ?\^M))
  16046. (progn
  16047. ;; Go to end of line before heading
  16048. (forward-char -1)
  16049. (if (memq (preceding-char) '(?\n ?\^M))
  16050. ;; leave blank line before heading
  16051. (forward-char -1))))))
  16052. (point))
  16053. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16054. "Use Org version in org-mode, for dramatic speed-up."
  16055. (if (eq major-mode 'org-mode)
  16056. (progn
  16057. (org-end-of-subtree nil t)
  16058. (unless (eobp) (backward-char 1)))
  16059. ad-do-it))
  16060. (defun org-forward-same-level (arg &optional invisible-ok)
  16061. "Move forward to the arg'th subheading at same level as this one.
  16062. Stop at the first and last subheadings of a superior heading."
  16063. (interactive "p")
  16064. (org-back-to-heading invisible-ok)
  16065. (org-on-heading-p)
  16066. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16067. (re (format "^\\*\\{1,%d\\} " level))
  16068. l)
  16069. (forward-char 1)
  16070. (while (> arg 0)
  16071. (while (and (re-search-forward re nil 'move)
  16072. (setq l (- (match-end 0) (match-beginning 0) 1))
  16073. (= l level)
  16074. (not invisible-ok)
  16075. (org-invisible-p))
  16076. (if (< l level) (setq arg 1)))
  16077. (setq arg (1- arg)))
  16078. (beginning-of-line 1)))
  16079. (defun org-backward-same-level (arg &optional invisible-ok)
  16080. "Move backward to the arg'th subheading at same level as this one.
  16081. Stop at the first and last subheadings of a superior heading."
  16082. (interactive "p")
  16083. (org-back-to-heading)
  16084. (org-on-heading-p)
  16085. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16086. (re (format "^\\*\\{1,%d\\} " level))
  16087. l)
  16088. (while (> arg 0)
  16089. (while (and (re-search-backward re nil 'move)
  16090. (setq l (- (match-end 0) (match-beginning 0) 1))
  16091. (= l level)
  16092. (not invisible-ok)
  16093. (org-invisible-p))
  16094. (if (< l level) (setq arg 1)))
  16095. (setq arg (1- arg)))))
  16096. (defun org-show-subtree ()
  16097. "Show everything after this heading at deeper levels."
  16098. (outline-flag-region
  16099. (point)
  16100. (save-excursion
  16101. (org-end-of-subtree t t))
  16102. nil))
  16103. (defun org-show-entry ()
  16104. "Show the body directly following this heading.
  16105. Show the heading too, if it is currently invisible."
  16106. (interactive)
  16107. (save-excursion
  16108. (condition-case nil
  16109. (progn
  16110. (org-back-to-heading t)
  16111. (outline-flag-region
  16112. (max (point-min) (1- (point)))
  16113. (save-excursion
  16114. (if (re-search-forward
  16115. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16116. (match-beginning 1)
  16117. (point-max)))
  16118. nil)
  16119. (org-cycle-hide-drawers 'children))
  16120. (error nil))))
  16121. (defun org-make-options-regexp (kwds &optional extra)
  16122. "Make a regular expression for keyword lines."
  16123. (concat
  16124. "^"
  16125. "#?[ \t]*\\+\\("
  16126. (mapconcat 'regexp-quote kwds "\\|")
  16127. (if extra (concat "\\|" extra))
  16128. "\\):[ \t]*"
  16129. "\\(.*\\)"))
  16130. ;; Make isearch reveal the necessary context
  16131. (defun org-isearch-end ()
  16132. "Reveal context after isearch exits."
  16133. (when isearch-success ; only if search was successful
  16134. (if (featurep 'xemacs)
  16135. ;; Under XEmacs, the hook is run in the correct place,
  16136. ;; we directly show the context.
  16137. (org-show-context 'isearch)
  16138. ;; In Emacs the hook runs *before* restoring the overlays.
  16139. ;; So we have to use a one-time post-command-hook to do this.
  16140. ;; (Emacs 22 has a special variable, see function `org-mode')
  16141. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16142. isearch-mode-end-hook-quit)
  16143. ;; Only when the isearch was not quitted.
  16144. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16145. 'append 'local)))))
  16146. (defun org-isearch-post-command ()
  16147. "Remove self from hook, and show context."
  16148. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16149. (org-show-context 'isearch))
  16150. ;;;; Integration with and fixes for other packages
  16151. ;;; Imenu support
  16152. (defvar org-imenu-markers nil
  16153. "All markers currently used by Imenu.")
  16154. (make-variable-buffer-local 'org-imenu-markers)
  16155. (defun org-imenu-new-marker (&optional pos)
  16156. "Return a new marker for use by Imenu, and remember the marker."
  16157. (let ((m (make-marker)))
  16158. (move-marker m (or pos (point)))
  16159. (push m org-imenu-markers)
  16160. m))
  16161. (defun org-imenu-get-tree ()
  16162. "Produce the index for Imenu."
  16163. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16164. (setq org-imenu-markers nil)
  16165. (let* ((n org-imenu-depth)
  16166. (re (concat "^" outline-regexp))
  16167. (subs (make-vector (1+ n) nil))
  16168. (last-level 0)
  16169. m level head)
  16170. (save-excursion
  16171. (save-restriction
  16172. (widen)
  16173. (goto-char (point-max))
  16174. (while (re-search-backward re nil t)
  16175. (setq level (org-reduced-level (funcall outline-level)))
  16176. (when (<= level n)
  16177. (looking-at org-complex-heading-regexp)
  16178. (setq head (org-link-display-format
  16179. (org-match-string-no-properties 4))
  16180. m (org-imenu-new-marker))
  16181. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16182. (if (>= level last-level)
  16183. (push (cons head m) (aref subs level))
  16184. (push (cons head (aref subs (1+ level))) (aref subs level))
  16185. (loop for i from (1+ level) to n do (aset subs i nil)))
  16186. (setq last-level level)))))
  16187. (aref subs 1)))
  16188. (eval-after-load "imenu"
  16189. '(progn
  16190. (add-hook 'imenu-after-jump-hook
  16191. (lambda ()
  16192. (if (eq major-mode 'org-mode)
  16193. (org-show-context 'org-goto))))))
  16194. (defun org-link-display-format (link)
  16195. "Replace a link with either the description, or the link target
  16196. if no description is present"
  16197. (save-match-data
  16198. (if (string-match org-bracket-link-analytic-regexp link)
  16199. (replace-match (if (match-end 5)
  16200. (match-string 5 link)
  16201. (concat (match-string 1 link)
  16202. (match-string 3 link)))
  16203. nil t link)
  16204. link)))
  16205. ;; Speedbar support
  16206. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16207. "Overlay marking the agenda restriction line in speedbar.")
  16208. (org-overlay-put org-speedbar-restriction-lock-overlay
  16209. 'face 'org-agenda-restriction-lock)
  16210. (org-overlay-put org-speedbar-restriction-lock-overlay
  16211. 'help-echo "Agendas are currently limited to this item.")
  16212. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16213. (defun org-speedbar-set-agenda-restriction ()
  16214. "Restrict future agenda commands to the location at point in speedbar.
  16215. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16216. (interactive)
  16217. (require 'org-agenda)
  16218. (let (p m tp np dir txt)
  16219. (cond
  16220. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16221. 'org-imenu t))
  16222. (setq m (get-text-property p 'org-imenu-marker))
  16223. (with-current-buffer (marker-buffer m)
  16224. (goto-char m)
  16225. (org-agenda-set-restriction-lock 'subtree)))
  16226. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16227. 'speedbar-function 'speedbar-find-file))
  16228. (setq tp (previous-single-property-change
  16229. (1+ p) 'speedbar-function)
  16230. np (next-single-property-change
  16231. tp 'speedbar-function)
  16232. dir (speedbar-line-directory)
  16233. txt (buffer-substring-no-properties (or tp (point-min))
  16234. (or np (point-max))))
  16235. (with-current-buffer (find-file-noselect
  16236. (let ((default-directory dir))
  16237. (expand-file-name txt)))
  16238. (unless (org-mode-p)
  16239. (error "Cannot restrict to non-Org-mode file"))
  16240. (org-agenda-set-restriction-lock 'file)))
  16241. (t (error "Don't know how to restrict Org-mode's agenda")))
  16242. (org-move-overlay org-speedbar-restriction-lock-overlay
  16243. (point-at-bol) (point-at-eol))
  16244. (setq current-prefix-arg nil)
  16245. (org-agenda-maybe-redo)))
  16246. (eval-after-load "speedbar"
  16247. '(progn
  16248. (speedbar-add-supported-extension ".org")
  16249. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16250. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16251. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16252. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16253. (add-hook 'speedbar-visiting-tag-hook
  16254. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16255. ;;; Fixes and Hacks for problems with other packages
  16256. ;; Make flyspell not check words in links, to not mess up our keymap
  16257. (defun org-mode-flyspell-verify ()
  16258. "Don't let flyspell put overlays at active buttons."
  16259. (and (not (get-text-property (point) 'keymap))
  16260. (not (get-text-property (point) 'org-no-flyspell))))
  16261. (defun org-remove-flyspell-overlays-in (beg end)
  16262. "Remove flyspell overlays in region."
  16263. (and (org-bound-and-true-p flyspell-mode)
  16264. (fboundp 'flyspell-delete-region-overlays)
  16265. (flyspell-delete-region-overlays beg end))
  16266. (add-text-properties beg end '(org-no-flyspell t)))
  16267. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16268. (eval-after-load "bookmark"
  16269. '(if (boundp 'bookmark-after-jump-hook)
  16270. ;; We can use the hook
  16271. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16272. ;; Hook not available, use advice
  16273. (defadvice bookmark-jump (after org-make-visible activate)
  16274. "Make the position visible."
  16275. (org-bookmark-jump-unhide))))
  16276. ;; Make sure saveplace shows the location if it was hidden
  16277. (eval-after-load "saveplace"
  16278. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16279. "Make the position visible."
  16280. (org-bookmark-jump-unhide)))
  16281. ;; Make sure ecb shows the location if it was hidden
  16282. (eval-after-load "ecb"
  16283. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16284. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16285. (if (eq major-mode 'org-mode)
  16286. (org-show-context))))
  16287. (defun org-bookmark-jump-unhide ()
  16288. "Unhide the current position, to show the bookmark location."
  16289. (and (org-mode-p)
  16290. (or (org-invisible-p)
  16291. (save-excursion (goto-char (max (point-min) (1- (point))))
  16292. (org-invisible-p)))
  16293. (org-show-context 'bookmark-jump)))
  16294. ;; Make session.el ignore our circular variable
  16295. (eval-after-load "session"
  16296. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16297. ;;;; Experimental code
  16298. (defun org-closed-in-range ()
  16299. "Sparse tree of items closed in a certain time range.
  16300. Still experimental, may disappear in the future."
  16301. (interactive)
  16302. ;; Get the time interval from the user.
  16303. (let* ((time1 (org-float-time
  16304. (org-read-date nil 'to-time nil "Starting date: ")))
  16305. (time2 (org-float-time
  16306. (org-read-date nil 'to-time nil "End date:")))
  16307. ;; callback function
  16308. (callback (lambda ()
  16309. (let ((time
  16310. (org-float-time
  16311. (apply 'encode-time
  16312. (org-parse-time-string
  16313. (match-string 1))))))
  16314. ;; check if time in interval
  16315. (and (>= time time1) (<= time time2))))))
  16316. ;; make tree, check each match with the callback
  16317. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16318. ;;;; Finish up
  16319. (provide 'org)
  16320. (run-hooks 'org-load-hook)
  16321. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16322. ;;; org.el ends here